blob: 77541fcb18531266a75208b45f96989dd2cd51cc [file] [log] [blame]
Pavel Maltseve2603e32016-10-25 16:03:23 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef android_hardware_vehicle_V2_0_VehicleDebugUtils_H_
18#define android_hardware_vehicle_V2_0_VehicleDebugUtils_H_
19
20#include <android/hardware/vehicle/2.0/types.h>
21#include <vehicle_hal_manager/VehicleUtils.h>
22#include <ios>
23#include <sstream>
24
25namespace android {
26namespace hardware {
27namespace vehicle {
28namespace V2_0 {
29
30const VehiclePropConfig kVehicleProperties[] = {
31 {
32 .prop = VehicleProperty::INFO_MAKE,
33 .access = VehiclePropertyAccess::READ,
34 .changeMode = VehiclePropertyChangeMode::STATIC,
Pavel Maltseve2603e32016-10-25 16:03:23 -070035 .configString = "Some=config,options=if,you=have_any",
36 },
37
38 {
39 .prop = VehicleProperty::HVAC_FAN_SPEED,
40 .access = VehiclePropertyAccess::READ_WRITE,
41 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
Pavel Maltseve2603e32016-10-25 16:03:23 -070042 .supportedAreas = static_cast<int32_t>(
43 VehicleAreaZone::ROW_1_LEFT | VehicleAreaZone::ROW_1_RIGHT),
Pavel Maltsev0e0a9252016-12-05 11:03:52 -080044 .areaConfigs = {
45 VehicleAreaConfig {
46 .areaId = toInt(VehicleAreaZone::ROW_1_LEFT),
47 .minInt32Value = 1,
48 .maxInt32Value = 7},
49 VehicleAreaConfig {
50 .areaId = toInt(VehicleAreaZone::ROW_1_RIGHT),
51 .minInt32Value = 1,
52 .maxInt32Value = 5,
53 }
54 }
Pavel Maltsevdb179c52016-10-27 15:43:06 -070055 },
56
57 // Write-only property
58 {
59 .prop = VehicleProperty::HVAC_SEAT_TEMPERATURE,
60 .access = VehiclePropertyAccess::WRITE,
61 .changeMode = VehiclePropertyChangeMode::ON_SET,
Pavel Maltsevdb179c52016-10-27 15:43:06 -070062 .supportedAreas = static_cast<int32_t>(
63 VehicleAreaZone::ROW_1_LEFT | VehicleAreaZone::ROW_1_RIGHT),
Pavel Maltsev0e0a9252016-12-05 11:03:52 -080064 .areaConfigs = {
65 VehicleAreaConfig {
66 .areaId = toInt(VehicleAreaZone::ROW_1_LEFT),
67 .minInt32Value = 64,
68 .maxInt32Value = 80},
69 VehicleAreaConfig {
70 .areaId = toInt(VehicleAreaZone::ROW_1_RIGHT),
71 .minInt32Value = 64,
72 .maxInt32Value = 80,
73 }
74 }
Pavel Maltseve2603e32016-10-25 16:03:23 -070075 },
76
77 {
78 .prop = VehicleProperty::INFO_FUEL_CAPACITY,
79 .access = VehiclePropertyAccess::READ,
80 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
Pavel Maltsev0e0a9252016-12-05 11:03:52 -080081 .areaConfigs = {
82 VehicleAreaConfig {
83 .minFloatValue = 0,
84 .maxFloatValue = 1.0
85 }
86 }
Pavel Maltseve2603e32016-10-25 16:03:23 -070087 },
88
89 {
90 .prop = VehicleProperty::DISPLAY_BRIGHTNESS,
91 .access = VehiclePropertyAccess::READ_WRITE,
92 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
Pavel Maltsev0e0a9252016-12-05 11:03:52 -080093 .areaConfigs = {
94 VehicleAreaConfig {
95 .minInt32Value = 0,
96 .maxInt32Value = 10
97 }
98 }
Pavel Maltsevdb179c52016-10-27 15:43:06 -070099 },
100
101 {
102 .prop = VehicleProperty::MIRROR_FOLD,
103 .access = VehiclePropertyAccess::READ_WRITE,
104 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700105
Pavel Maltseve2603e32016-10-25 16:03:23 -0700106 }
107};
108
109constexpr auto kTimeout = std::chrono::milliseconds(500);
110
111class MockedVehicleCallback : public IVehicleCallback {
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700112private:
113 using MuxGuard = std::lock_guard<std::mutex>;
114 using HidlVecOfValues = hidl_vec<VehiclePropValue>;
Pavel Maltseve2603e32016-10-25 16:03:23 -0700115public:
116 // Methods from ::android::hardware::vehicle::V2_0::IVehicleCallback follow.
117 Return<void> onPropertyEvent(
118 const hidl_vec<VehiclePropValue>& values) override {
119 {
120 MuxGuard g(mLock);
121 mReceivedEvents.push_back(values);
122 }
123 mEventCond.notify_one();
124 return Return<void>();
125 }
126 Return<void> onPropertySet(const VehiclePropValue& value) override {
127 return Return<void>();
128 }
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700129 Return<void> onPropertySetError(StatusCode errorCode,
130 VehicleProperty propId,
131 int32_t areaId) override {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700132 return Return<void>();
133 }
134
135 bool waitForExpectedEvents(size_t expectedEvents) {
136 std::unique_lock<std::mutex> g(mLock);
137
138 if (expectedEvents == 0 && mReceivedEvents.size() == 0) {
139 // No events expected, let's sleep a little bit to make sure
140 // nothing will show up.
141 return mEventCond.wait_for(g, kTimeout) == std::cv_status::timeout;
142 }
143
144 while (expectedEvents != mReceivedEvents.size()) {
145 if (mEventCond.wait_for(g, kTimeout) == std::cv_status::timeout) {
146 return false;
147 }
148 }
149 return true;
150 }
151
152 void reset() {
153 mReceivedEvents.clear();
154 }
155
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700156 const std::vector<HidlVecOfValues>& getReceivedEvents() {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700157 return mReceivedEvents;
158 }
159
160private:
Pavel Maltseve2603e32016-10-25 16:03:23 -0700161 std::mutex mLock;
162 std::condition_variable mEventCond;
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700163 std::vector<HidlVecOfValues> mReceivedEvents;
Pavel Maltseve2603e32016-10-25 16:03:23 -0700164};
165
166template<typename T>
167inline std::string hexString(T value) {
168 std::stringstream ss;
169 ss << std::showbase << std::hex << value;
170 return ss.str();
171}
172
173template <typename T, typename Collection>
174inline void assertAllExistsAnyOrder(
175 std::initializer_list<T> expected,
176 const Collection& actual,
177 const char* msg) {
178 std::set<T> expectedSet = expected;
179
180 for (auto a: actual) {
181 ASSERT_EQ(1u, expectedSet.erase(a))
182 << msg << "\nContains not unexpected value.\n";
183 }
184
185 ASSERT_EQ(0u, expectedSet.size())
186 << msg
187 << "\nDoesn't contain expected value.";
188}
189
190#define ASSERT_ALL_EXISTS(...) \
191 assertAllExistsAnyOrder(__VA_ARGS__, (std::string("Called from: ") + \
192 std::string(__FILE__) + std::string(":") + \
193 std::to_string(__LINE__)).c_str()); \
194
195template<typename T>
196inline std::string enumToHexString(T value) {
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700197 return hexString(toInt(value));
Pavel Maltseve2603e32016-10-25 16:03:23 -0700198}
199
200template <typename T>
201inline std::string toString(const hidl_vec<T>& vec) {
202 std::stringstream ss("[");
203 for (size_t i = 0; i < vec.size(); i++) {
204 if (i != 0) ss << ",";
205 ss << vec[i];
206 }
207 ss << "]";
208 return ss.str();
209}
210
211inline std::string toString(const VehiclePropValue &v) {
212 std::stringstream ss;
213 ss << "VehiclePropValue {n"
214 << " prop: " << enumToHexString(v.prop) << ",\n"
215 << " areaId: " << hexString(v.areaId) << ",\n"
216 << " timestamp: " << v.timestamp << ",\n"
217 << " value {\n"
218 << " int32Values: " << toString(v.value.int32Values) << ",\n"
219 << " floatValues: " << toString(v.value.floatValues) << ",\n"
220 << " int64Values: " << toString(v.value.int64Values) << ",\n"
221 << " bytes: " << toString(v.value.bytes) << ",\n"
222 << " string: " << v.value.stringValue.c_str() << ",\n"
223 << " }\n"
224 << "}\n";
225
226 return ss.str();
227}
228
229inline std::string toString(const VehiclePropConfig &config) {
230 std::stringstream ss;
231 ss << "VehiclePropConfig {\n"
232 << " prop: " << enumToHexString(config.prop) << ",\n"
233 << " supportedAreas: " << hexString(config.supportedAreas) << ",\n"
234 << " access: " << enumToHexString(config.access) << ",\n"
Pavel Maltseve2603e32016-10-25 16:03:23 -0700235 << " changeMode: " << enumToHexString(config.changeMode) << ",\n"
236 << " configFlags: " << hexString(config.configFlags) << ",\n"
237 << " minSampleRate: " << config.minSampleRate << ",\n"
238 << " maxSampleRate: " << config.maxSampleRate << ",\n"
239 << " configString: " << config.configString.c_str() << ",\n";
240
241 ss << " areaConfigs {\n";
242 for (size_t i = 0; i < config.areaConfigs.size(); i++) {
243 const auto &area = config.areaConfigs[i];
244 ss << " areaId: " << hexString(area.areaId) << ",\n"
245 << " minFloatValue: " << area.minFloatValue << ",\n"
246 << " minFloatValue: " << area.maxFloatValue << ",\n"
247 << " minInt32Value: " << area.minInt32Value << ",\n"
248 << " minInt32Value: " << area.maxInt32Value << ",\n"
249 << " minInt64Value: " << area.minInt64Value << ",\n"
250 << " minInt64Value: " << area.maxInt64Value << ",\n";
251 }
252 ss << " }\n"
253 << "}\n";
254
255 return ss.str();
256}
257
258
259} // namespace V2_0
260} // namespace vehicle
261} // namespace hardware
262} // namespace android
263
264
265#endif //VEHICLEHALDEBUGUTILS_H