blob: b3b3ffa9799aa3799922912fb4f7f65766372743 [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),
46 .areaConfigs = init_hidl_vec({
47 VehicleAreaConfig {
48 .areaId = val(
49 VehicleAreaZone::ROW_2_LEFT),
50 .minInt32Value = 1,
51 .maxInt32Value = 7},
52 VehicleAreaConfig {
53 .areaId = val(
54 VehicleAreaZone::ROW_1_RIGHT),
55 .minInt32Value = 1,
56 .maxInt32Value = 5,
57 }
58 }),
59 },
60
61 {
62 .prop = VehicleProperty::INFO_FUEL_CAPACITY,
63 .access = VehiclePropertyAccess::READ,
64 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
65 .permissionModel = VehiclePermissionModel::OEM_ONLY,
66 .areaConfigs = init_hidl_vec({
67 VehicleAreaConfig {
68 .minFloatValue = 0,
69 .maxFloatValue = 1.0
70 }
71 })
72 },
73
74 {
75 .prop = VehicleProperty::DISPLAY_BRIGHTNESS,
76 .access = VehiclePropertyAccess::READ_WRITE,
77 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
78 .permissionModel = VehiclePermissionModel::OEM_ONLY,
79 .areaConfigs = init_hidl_vec({
80 VehicleAreaConfig {
81 .minInt32Value = 0,
82 .maxInt32Value = 10
83 }
84 })
85 }
86};
87
88constexpr auto kTimeout = std::chrono::milliseconds(500);
89
90class MockedVehicleCallback : public IVehicleCallback {
91public:
92 // Methods from ::android::hardware::vehicle::V2_0::IVehicleCallback follow.
93 Return<void> onPropertyEvent(
94 const hidl_vec<VehiclePropValue>& values) override {
95 {
96 MuxGuard g(mLock);
97 mReceivedEvents.push_back(values);
98 }
99 mEventCond.notify_one();
100 return Return<void>();
101 }
102 Return<void> onPropertySet(const VehiclePropValue& value) override {
103 return Return<void>();
104 }
105 Return<void> onError(StatusCode errorCode,
106 VehicleProperty propId,
107 VehiclePropertyOperation operation) override {
108 return Return<void>();
109 }
110
111 bool waitForExpectedEvents(size_t expectedEvents) {
112 std::unique_lock<std::mutex> g(mLock);
113
114 if (expectedEvents == 0 && mReceivedEvents.size() == 0) {
115 // No events expected, let's sleep a little bit to make sure
116 // nothing will show up.
117 return mEventCond.wait_for(g, kTimeout) == std::cv_status::timeout;
118 }
119
120 while (expectedEvents != mReceivedEvents.size()) {
121 if (mEventCond.wait_for(g, kTimeout) == std::cv_status::timeout) {
122 return false;
123 }
124 }
125 return true;
126 }
127
128 void reset() {
129 mReceivedEvents.clear();
130 }
131
132 const std::vector<hidl_vec<VehiclePropValue>>& getReceivedEvents() {
133 return mReceivedEvents;
134 }
135
136private:
137 using MuxGuard = std::lock_guard<std::mutex>;
138
139 std::mutex mLock;
140 std::condition_variable mEventCond;
141 std::vector<hidl_vec<VehiclePropValue>> mReceivedEvents;
142};
143
144template<typename T>
145inline std::string hexString(T value) {
146 std::stringstream ss;
147 ss << std::showbase << std::hex << value;
148 return ss.str();
149}
150
151template <typename T, typename Collection>
152inline void assertAllExistsAnyOrder(
153 std::initializer_list<T> expected,
154 const Collection& actual,
155 const char* msg) {
156 std::set<T> expectedSet = expected;
157
158 for (auto a: actual) {
159 ASSERT_EQ(1u, expectedSet.erase(a))
160 << msg << "\nContains not unexpected value.\n";
161 }
162
163 ASSERT_EQ(0u, expectedSet.size())
164 << msg
165 << "\nDoesn't contain expected value.";
166}
167
168#define ASSERT_ALL_EXISTS(...) \
169 assertAllExistsAnyOrder(__VA_ARGS__, (std::string("Called from: ") + \
170 std::string(__FILE__) + std::string(":") + \
171 std::to_string(__LINE__)).c_str()); \
172
173template<typename T>
174inline std::string enumToHexString(T value) {
175 return hexString(val(value));
176}
177
178template <typename T>
179inline std::string toString(const hidl_vec<T>& vec) {
180 std::stringstream ss("[");
181 for (size_t i = 0; i < vec.size(); i++) {
182 if (i != 0) ss << ",";
183 ss << vec[i];
184 }
185 ss << "]";
186 return ss.str();
187}
188
189inline std::string toString(const VehiclePropValue &v) {
190 std::stringstream ss;
191 ss << "VehiclePropValue {n"
192 << " prop: " << enumToHexString(v.prop) << ",\n"
193 << " areaId: " << hexString(v.areaId) << ",\n"
194 << " timestamp: " << v.timestamp << ",\n"
195 << " value {\n"
196 << " int32Values: " << toString(v.value.int32Values) << ",\n"
197 << " floatValues: " << toString(v.value.floatValues) << ",\n"
198 << " int64Values: " << toString(v.value.int64Values) << ",\n"
199 << " bytes: " << toString(v.value.bytes) << ",\n"
200 << " string: " << v.value.stringValue.c_str() << ",\n"
201 << " }\n"
202 << "}\n";
203
204 return ss.str();
205}
206
207inline std::string toString(const VehiclePropConfig &config) {
208 std::stringstream ss;
209 ss << "VehiclePropConfig {\n"
210 << " prop: " << enumToHexString(config.prop) << ",\n"
211 << " supportedAreas: " << hexString(config.supportedAreas) << ",\n"
212 << " access: " << enumToHexString(config.access) << ",\n"
213 << " permissionModel: " << enumToHexString(config.permissionModel) << ",\n"
214 << " changeMode: " << enumToHexString(config.changeMode) << ",\n"
215 << " configFlags: " << hexString(config.configFlags) << ",\n"
216 << " minSampleRate: " << config.minSampleRate << ",\n"
217 << " maxSampleRate: " << config.maxSampleRate << ",\n"
218 << " configString: " << config.configString.c_str() << ",\n";
219
220 ss << " areaConfigs {\n";
221 for (size_t i = 0; i < config.areaConfigs.size(); i++) {
222 const auto &area = config.areaConfigs[i];
223 ss << " areaId: " << hexString(area.areaId) << ",\n"
224 << " minFloatValue: " << area.minFloatValue << ",\n"
225 << " minFloatValue: " << area.maxFloatValue << ",\n"
226 << " minInt32Value: " << area.minInt32Value << ",\n"
227 << " minInt32Value: " << area.maxInt32Value << ",\n"
228 << " minInt64Value: " << area.minInt64Value << ",\n"
229 << " minInt64Value: " << area.maxInt64Value << ",\n";
230 }
231 ss << " }\n"
232 << "}\n";
233
234 return ss.str();
235}
236
237
238} // namespace V2_0
239} // namespace vehicle
240} // namespace hardware
241} // namespace android
242
243
244#endif //VEHICLEHALDEBUGUTILS_H