blob: b6a5c31dde6eeb2784cc3bab64a64f04b95c3eee [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 Maltsevf21639f2016-12-22 11:17:29 -0800106 },
107
108 // Complex data type.
109 {
110 .prop = VehicleProperty::VEHICLE_MAPS_DATA_SERVICE,
111 .access = VehiclePropertyAccess::READ_WRITE,
112 .changeMode = VehiclePropertyChangeMode::ON_CHANGE
Pavel Maltseve2603e32016-10-25 16:03:23 -0700113 }
114};
115
116constexpr auto kTimeout = std::chrono::milliseconds(500);
117
118class MockedVehicleCallback : public IVehicleCallback {
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700119private:
120 using MuxGuard = std::lock_guard<std::mutex>;
121 using HidlVecOfValues = hidl_vec<VehiclePropValue>;
Pavel Maltseve2603e32016-10-25 16:03:23 -0700122public:
123 // Methods from ::android::hardware::vehicle::V2_0::IVehicleCallback follow.
124 Return<void> onPropertyEvent(
125 const hidl_vec<VehiclePropValue>& values) override {
126 {
127 MuxGuard g(mLock);
128 mReceivedEvents.push_back(values);
129 }
130 mEventCond.notify_one();
131 return Return<void>();
132 }
133 Return<void> onPropertySet(const VehiclePropValue& value) override {
134 return Return<void>();
135 }
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700136 Return<void> onPropertySetError(StatusCode errorCode,
137 VehicleProperty propId,
138 int32_t areaId) override {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700139 return Return<void>();
140 }
141
142 bool waitForExpectedEvents(size_t expectedEvents) {
143 std::unique_lock<std::mutex> g(mLock);
144
145 if (expectedEvents == 0 && mReceivedEvents.size() == 0) {
146 // No events expected, let's sleep a little bit to make sure
147 // nothing will show up.
148 return mEventCond.wait_for(g, kTimeout) == std::cv_status::timeout;
149 }
150
151 while (expectedEvents != mReceivedEvents.size()) {
152 if (mEventCond.wait_for(g, kTimeout) == std::cv_status::timeout) {
153 return false;
154 }
155 }
156 return true;
157 }
158
159 void reset() {
160 mReceivedEvents.clear();
161 }
162
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700163 const std::vector<HidlVecOfValues>& getReceivedEvents() {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700164 return mReceivedEvents;
165 }
166
167private:
Pavel Maltseve2603e32016-10-25 16:03:23 -0700168 std::mutex mLock;
169 std::condition_variable mEventCond;
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700170 std::vector<HidlVecOfValues> mReceivedEvents;
Pavel Maltseve2603e32016-10-25 16:03:23 -0700171};
172
173template<typename T>
174inline std::string hexString(T value) {
175 std::stringstream ss;
176 ss << std::showbase << std::hex << value;
177 return ss.str();
178}
179
180template <typename T, typename Collection>
181inline void assertAllExistsAnyOrder(
182 std::initializer_list<T> expected,
183 const Collection& actual,
184 const char* msg) {
185 std::set<T> expectedSet = expected;
186
187 for (auto a: actual) {
188 ASSERT_EQ(1u, expectedSet.erase(a))
189 << msg << "\nContains not unexpected value.\n";
190 }
191
192 ASSERT_EQ(0u, expectedSet.size())
193 << msg
194 << "\nDoesn't contain expected value.";
195}
196
197#define ASSERT_ALL_EXISTS(...) \
198 assertAllExistsAnyOrder(__VA_ARGS__, (std::string("Called from: ") + \
199 std::string(__FILE__) + std::string(":") + \
200 std::to_string(__LINE__)).c_str()); \
201
202template<typename T>
203inline std::string enumToHexString(T value) {
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700204 return hexString(toInt(value));
Pavel Maltseve2603e32016-10-25 16:03:23 -0700205}
206
207template <typename T>
Yifan Hong668fed72017-01-10 18:09:48 -0800208inline std::string vecToString(const hidl_vec<T>& vec) {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700209 std::stringstream ss("[");
210 for (size_t i = 0; i < vec.size(); i++) {
211 if (i != 0) ss << ",";
212 ss << vec[i];
213 }
214 ss << "]";
215 return ss.str();
216}
217
218inline std::string toString(const VehiclePropValue &v) {
219 std::stringstream ss;
220 ss << "VehiclePropValue {n"
221 << " prop: " << enumToHexString(v.prop) << ",\n"
222 << " areaId: " << hexString(v.areaId) << ",\n"
223 << " timestamp: " << v.timestamp << ",\n"
224 << " value {\n"
Yifan Hong668fed72017-01-10 18:09:48 -0800225 << " int32Values: " << vecToString(v.value.int32Values) << ",\n"
226 << " floatValues: " << vecToString(v.value.floatValues) << ",\n"
227 << " int64Values: " << vecToString(v.value.int64Values) << ",\n"
228 << " bytes: " << vecToString(v.value.bytes) << ",\n"
Pavel Maltseve2603e32016-10-25 16:03:23 -0700229 << " string: " << v.value.stringValue.c_str() << ",\n"
230 << " }\n"
231 << "}\n";
232
233 return ss.str();
234}
235
236inline std::string toString(const VehiclePropConfig &config) {
237 std::stringstream ss;
238 ss << "VehiclePropConfig {\n"
239 << " prop: " << enumToHexString(config.prop) << ",\n"
240 << " supportedAreas: " << hexString(config.supportedAreas) << ",\n"
241 << " access: " << enumToHexString(config.access) << ",\n"
Pavel Maltseve2603e32016-10-25 16:03:23 -0700242 << " 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