blob: 586b5abffd9404b20e33a525b9fe0a9ea26d487a [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,
35 .permissionModel = VehiclePermissionModel::OEM_ONLY,
36 .configString = "Some=config,options=if,you=have_any",
37 },
38
39 {
40 .prop = VehicleProperty::HVAC_FAN_SPEED,
41 .access = VehiclePropertyAccess::READ_WRITE,
42 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
43 .permissionModel = VehiclePermissionModel::NO_RESTRICTION,
44 .supportedAreas = static_cast<int32_t>(
45 VehicleAreaZone::ROW_1_LEFT | VehicleAreaZone::ROW_1_RIGHT),
Pavel Maltsev0e0a9252016-12-05 11:03:52 -080046 .areaConfigs = {
47 VehicleAreaConfig {
48 .areaId = toInt(VehicleAreaZone::ROW_1_LEFT),
49 .minInt32Value = 1,
50 .maxInt32Value = 7},
51 VehicleAreaConfig {
52 .areaId = toInt(VehicleAreaZone::ROW_1_RIGHT),
53 .minInt32Value = 1,
54 .maxInt32Value = 5,
55 }
56 }
Pavel Maltsevdb179c52016-10-27 15:43:06 -070057 },
58
59 // Write-only property
60 {
61 .prop = VehicleProperty::HVAC_SEAT_TEMPERATURE,
62 .access = VehiclePropertyAccess::WRITE,
63 .changeMode = VehiclePropertyChangeMode::ON_SET,
64 .permissionModel = VehiclePermissionModel::NO_RESTRICTION,
65 .supportedAreas = static_cast<int32_t>(
66 VehicleAreaZone::ROW_1_LEFT | VehicleAreaZone::ROW_1_RIGHT),
Pavel Maltsev0e0a9252016-12-05 11:03:52 -080067 .areaConfigs = {
68 VehicleAreaConfig {
69 .areaId = toInt(VehicleAreaZone::ROW_1_LEFT),
70 .minInt32Value = 64,
71 .maxInt32Value = 80},
72 VehicleAreaConfig {
73 .areaId = toInt(VehicleAreaZone::ROW_1_RIGHT),
74 .minInt32Value = 64,
75 .maxInt32Value = 80,
76 }
77 }
Pavel Maltseve2603e32016-10-25 16:03:23 -070078 },
79
80 {
81 .prop = VehicleProperty::INFO_FUEL_CAPACITY,
82 .access = VehiclePropertyAccess::READ,
83 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
84 .permissionModel = VehiclePermissionModel::OEM_ONLY,
Pavel Maltsev0e0a9252016-12-05 11:03:52 -080085 .areaConfigs = {
86 VehicleAreaConfig {
87 .minFloatValue = 0,
88 .maxFloatValue = 1.0
89 }
90 }
Pavel Maltseve2603e32016-10-25 16:03:23 -070091 },
92
93 {
94 .prop = VehicleProperty::DISPLAY_BRIGHTNESS,
95 .access = VehiclePropertyAccess::READ_WRITE,
96 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
97 .permissionModel = VehiclePermissionModel::OEM_ONLY,
Pavel Maltsev0e0a9252016-12-05 11:03:52 -080098 .areaConfigs = {
99 VehicleAreaConfig {
100 .minInt32Value = 0,
101 .maxInt32Value = 10
102 }
103 }
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700104 },
105
106 {
107 .prop = VehicleProperty::MIRROR_FOLD,
108 .access = VehiclePropertyAccess::READ_WRITE,
109 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
110 .permissionModel = VehiclePermissionModel::OEM_ONLY,
111
Pavel Maltseve2603e32016-10-25 16:03:23 -0700112 }
113};
114
115constexpr auto kTimeout = std::chrono::milliseconds(500);
116
117class MockedVehicleCallback : public IVehicleCallback {
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700118private:
119 using MuxGuard = std::lock_guard<std::mutex>;
120 using HidlVecOfValues = hidl_vec<VehiclePropValue>;
Pavel Maltseve2603e32016-10-25 16:03:23 -0700121public:
122 // Methods from ::android::hardware::vehicle::V2_0::IVehicleCallback follow.
123 Return<void> onPropertyEvent(
124 const hidl_vec<VehiclePropValue>& values) override {
125 {
126 MuxGuard g(mLock);
127 mReceivedEvents.push_back(values);
128 }
129 mEventCond.notify_one();
130 return Return<void>();
131 }
132 Return<void> onPropertySet(const VehiclePropValue& value) override {
133 return Return<void>();
134 }
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700135 Return<void> onPropertySetError(StatusCode errorCode,
136 VehicleProperty propId,
137 int32_t areaId) override {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700138 return Return<void>();
139 }
140
141 bool waitForExpectedEvents(size_t expectedEvents) {
142 std::unique_lock<std::mutex> g(mLock);
143
144 if (expectedEvents == 0 && mReceivedEvents.size() == 0) {
145 // No events expected, let's sleep a little bit to make sure
146 // nothing will show up.
147 return mEventCond.wait_for(g, kTimeout) == std::cv_status::timeout;
148 }
149
150 while (expectedEvents != mReceivedEvents.size()) {
151 if (mEventCond.wait_for(g, kTimeout) == std::cv_status::timeout) {
152 return false;
153 }
154 }
155 return true;
156 }
157
158 void reset() {
159 mReceivedEvents.clear();
160 }
161
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700162 const std::vector<HidlVecOfValues>& getReceivedEvents() {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700163 return mReceivedEvents;
164 }
165
166private:
Pavel Maltseve2603e32016-10-25 16:03:23 -0700167 std::mutex mLock;
168 std::condition_variable mEventCond;
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700169 std::vector<HidlVecOfValues> mReceivedEvents;
Pavel Maltseve2603e32016-10-25 16:03:23 -0700170};
171
172template<typename T>
173inline std::string hexString(T value) {
174 std::stringstream ss;
175 ss << std::showbase << std::hex << value;
176 return ss.str();
177}
178
179template <typename T, typename Collection>
180inline void assertAllExistsAnyOrder(
181 std::initializer_list<T> expected,
182 const Collection& actual,
183 const char* msg) {
184 std::set<T> expectedSet = expected;
185
186 for (auto a: actual) {
187 ASSERT_EQ(1u, expectedSet.erase(a))
188 << msg << "\nContains not unexpected value.\n";
189 }
190
191 ASSERT_EQ(0u, expectedSet.size())
192 << msg
193 << "\nDoesn't contain expected value.";
194}
195
196#define ASSERT_ALL_EXISTS(...) \
197 assertAllExistsAnyOrder(__VA_ARGS__, (std::string("Called from: ") + \
198 std::string(__FILE__) + std::string(":") + \
199 std::to_string(__LINE__)).c_str()); \
200
201template<typename T>
202inline std::string enumToHexString(T value) {
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700203 return hexString(toInt(value));
Pavel Maltseve2603e32016-10-25 16:03:23 -0700204}
205
206template <typename T>
207inline std::string toString(const hidl_vec<T>& vec) {
208 std::stringstream ss("[");
209 for (size_t i = 0; i < vec.size(); i++) {
210 if (i != 0) ss << ",";
211 ss << vec[i];
212 }
213 ss << "]";
214 return ss.str();
215}
216
217inline std::string toString(const VehiclePropValue &v) {
218 std::stringstream ss;
219 ss << "VehiclePropValue {n"
220 << " prop: " << enumToHexString(v.prop) << ",\n"
221 << " areaId: " << hexString(v.areaId) << ",\n"
222 << " timestamp: " << v.timestamp << ",\n"
223 << " value {\n"
224 << " int32Values: " << toString(v.value.int32Values) << ",\n"
225 << " floatValues: " << toString(v.value.floatValues) << ",\n"
226 << " int64Values: " << toString(v.value.int64Values) << ",\n"
227 << " bytes: " << toString(v.value.bytes) << ",\n"
228 << " string: " << v.value.stringValue.c_str() << ",\n"
229 << " }\n"
230 << "}\n";
231
232 return ss.str();
233}
234
235inline std::string toString(const VehiclePropConfig &config) {
236 std::stringstream ss;
237 ss << "VehiclePropConfig {\n"
238 << " prop: " << enumToHexString(config.prop) << ",\n"
239 << " supportedAreas: " << hexString(config.supportedAreas) << ",\n"
240 << " access: " << enumToHexString(config.access) << ",\n"
241 << " permissionModel: " << enumToHexString(config.permissionModel) << ",\n"
242 << " changeMode: " << enumToHexString(config.changeMode) << ",\n"
243 << " configFlags: " << hexString(config.configFlags) << ",\n"
244 << " minSampleRate: " << config.minSampleRate << ",\n"
245 << " maxSampleRate: " << config.maxSampleRate << ",\n"
246 << " configString: " << config.configString.c_str() << ",\n";
247
248 ss << " areaConfigs {\n";
249 for (size_t i = 0; i < config.areaConfigs.size(); i++) {
250 const auto &area = config.areaConfigs[i];
251 ss << " areaId: " << hexString(area.areaId) << ",\n"
252 << " minFloatValue: " << area.minFloatValue << ",\n"
253 << " minFloatValue: " << area.maxFloatValue << ",\n"
254 << " minInt32Value: " << area.minInt32Value << ",\n"
255 << " minInt32Value: " << area.maxInt32Value << ",\n"
256 << " minInt64Value: " << area.minInt64Value << ",\n"
257 << " minInt64Value: " << area.maxInt64Value << ",\n";
258 }
259 ss << " }\n"
260 << "}\n";
261
262 return ss.str();
263}
264
265
266} // namespace V2_0
267} // namespace vehicle
268} // namespace hardware
269} // namespace android
270
271
272#endif //VEHICLEHALDEBUGUTILS_H