blob: ce1ed7d9802a80b4b262d9ad77412d246314a91f [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
Pavel Maltsev2579fb72017-02-02 12:39:36 -080017#ifndef android_hardware_automotive_vehicle_V2_0_VehicleDebugUtils_H_
18#define android_hardware_automotive_vehicle_V2_0_VehicleDebugUtils_H_
Pavel Maltseve2603e32016-10-25 16:03:23 -070019
Pavel Maltsev2579fb72017-02-02 12:39:36 -080020#include <android/hardware/automotive/vehicle/2.0/types.h>
Pavel Maltseve2603e32016-10-25 16:03:23 -070021#include <ios>
22#include <sstream>
23
Pavel Maltsevc5344ac2017-02-08 12:33:46 -080024#include "vhal_v2_0/VehicleUtils.h"
25
Pavel Maltseve2603e32016-10-25 16:03:23 -070026namespace android {
27namespace hardware {
Pavel Maltsev2579fb72017-02-02 12:39:36 -080028namespace automotive {
Pavel Maltseve2603e32016-10-25 16:03:23 -070029namespace vehicle {
30namespace V2_0 {
31
32const VehiclePropConfig kVehicleProperties[] = {
33 {
Pavel Maltsev8e624b32017-02-01 16:30:25 -080034 .prop = toInt(VehicleProperty::INFO_MAKE),
Pavel Maltseve2603e32016-10-25 16:03:23 -070035 .access = VehiclePropertyAccess::READ,
36 .changeMode = VehiclePropertyChangeMode::STATIC,
Pavel Maltseve2603e32016-10-25 16:03:23 -070037 .configString = "Some=config,options=if,you=have_any",
38 },
39
40 {
Pavel Maltsev8e624b32017-02-01 16:30:25 -080041 .prop = toInt(VehicleProperty::HVAC_FAN_SPEED),
Pavel Maltseve2603e32016-10-25 16:03:23 -070042 .access = VehiclePropertyAccess::READ_WRITE,
43 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
Pavel Maltseve2603e32016-10-25 16:03:23 -070044 .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 {
Pavel Maltsev8e624b32017-02-01 16:30:25 -080061 .prop = toInt(VehicleProperty::HVAC_SEAT_TEMPERATURE),
Pavel Maltsevdb179c52016-10-27 15:43:06 -070062 .access = VehiclePropertyAccess::WRITE,
63 .changeMode = VehiclePropertyChangeMode::ON_SET,
Pavel Maltsevdb179c52016-10-27 15:43:06 -070064 .supportedAreas = static_cast<int32_t>(
65 VehicleAreaZone::ROW_1_LEFT | VehicleAreaZone::ROW_1_RIGHT),
Pavel Maltsev0e0a9252016-12-05 11:03:52 -080066 .areaConfigs = {
67 VehicleAreaConfig {
68 .areaId = toInt(VehicleAreaZone::ROW_1_LEFT),
69 .minInt32Value = 64,
70 .maxInt32Value = 80},
71 VehicleAreaConfig {
72 .areaId = toInt(VehicleAreaZone::ROW_1_RIGHT),
73 .minInt32Value = 64,
74 .maxInt32Value = 80,
75 }
76 }
Pavel Maltseve2603e32016-10-25 16:03:23 -070077 },
78
79 {
Pavel Maltsev8e624b32017-02-01 16:30:25 -080080 .prop = toInt(VehicleProperty::INFO_FUEL_CAPACITY),
Pavel Maltseve2603e32016-10-25 16:03:23 -070081 .access = VehiclePropertyAccess::READ,
82 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
Pavel Maltsev0e0a9252016-12-05 11:03:52 -080083 .areaConfigs = {
84 VehicleAreaConfig {
85 .minFloatValue = 0,
86 .maxFloatValue = 1.0
87 }
88 }
Pavel Maltseve2603e32016-10-25 16:03:23 -070089 },
90
91 {
Pavel Maltsev8e624b32017-02-01 16:30:25 -080092 .prop = toInt(VehicleProperty::DISPLAY_BRIGHTNESS),
Pavel Maltseve2603e32016-10-25 16:03:23 -070093 .access = VehiclePropertyAccess::READ_WRITE,
94 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
Pavel Maltsev0e0a9252016-12-05 11:03:52 -080095 .areaConfigs = {
96 VehicleAreaConfig {
97 .minInt32Value = 0,
98 .maxInt32Value = 10
99 }
100 }
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700101 },
102
103 {
Pavel Maltsev8e624b32017-02-01 16:30:25 -0800104 .prop = toInt(VehicleProperty::MIRROR_FOLD),
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700105 .access = VehiclePropertyAccess::READ_WRITE,
106 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700107
Pavel Maltsevf21639f2016-12-22 11:17:29 -0800108 },
109
110 // Complex data type.
111 {
Pavel Maltsev8e624b32017-02-01 16:30:25 -0800112 .prop = toInt(VehicleProperty::VEHICLE_MAP_SERVICE),
Pavel Maltsevf21639f2016-12-22 11:17:29 -0800113 .access = VehiclePropertyAccess::READ_WRITE,
114 .changeMode = VehiclePropertyChangeMode::ON_CHANGE
Pavel Maltseve2603e32016-10-25 16:03:23 -0700115 }
116};
117
118constexpr auto kTimeout = std::chrono::milliseconds(500);
119
120class MockedVehicleCallback : public IVehicleCallback {
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700121private:
122 using MuxGuard = std::lock_guard<std::mutex>;
123 using HidlVecOfValues = hidl_vec<VehiclePropValue>;
Pavel Maltseve2603e32016-10-25 16:03:23 -0700124public:
Pavel Maltsev2579fb72017-02-02 12:39:36 -0800125 // Methods from ::android::hardware::automotive::vehicle::V2_0::IVehicleCallback follow.
Pavel Maltseve2603e32016-10-25 16:03:23 -0700126 Return<void> onPropertyEvent(
127 const hidl_vec<VehiclePropValue>& values) override {
128 {
129 MuxGuard g(mLock);
130 mReceivedEvents.push_back(values);
131 }
132 mEventCond.notify_one();
133 return Return<void>();
134 }
Pavel Maltsev308515f2017-02-06 18:50:52 -0800135 Return<void> onPropertySet(const VehiclePropValue& /* value */) override {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700136 return Return<void>();
137 }
Pavel Maltsev308515f2017-02-06 18:50:52 -0800138 Return<void> onPropertySetError(StatusCode /* errorCode */,
139 int32_t /* propId */,
140 int32_t /* areaId */) override {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700141 return Return<void>();
142 }
143
144 bool waitForExpectedEvents(size_t expectedEvents) {
145 std::unique_lock<std::mutex> g(mLock);
146
147 if (expectedEvents == 0 && mReceivedEvents.size() == 0) {
148 // No events expected, let's sleep a little bit to make sure
149 // nothing will show up.
150 return mEventCond.wait_for(g, kTimeout) == std::cv_status::timeout;
151 }
152
153 while (expectedEvents != mReceivedEvents.size()) {
154 if (mEventCond.wait_for(g, kTimeout) == std::cv_status::timeout) {
155 return false;
156 }
157 }
158 return true;
159 }
160
161 void reset() {
162 mReceivedEvents.clear();
163 }
164
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700165 const std::vector<HidlVecOfValues>& getReceivedEvents() {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700166 return mReceivedEvents;
167 }
168
169private:
Pavel Maltseve2603e32016-10-25 16:03:23 -0700170 std::mutex mLock;
171 std::condition_variable mEventCond;
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700172 std::vector<HidlVecOfValues> mReceivedEvents;
Pavel Maltseve2603e32016-10-25 16:03:23 -0700173};
174
175template<typename T>
176inline std::string hexString(T value) {
177 std::stringstream ss;
178 ss << std::showbase << std::hex << value;
179 return ss.str();
180}
181
182template <typename T, typename Collection>
183inline void assertAllExistsAnyOrder(
184 std::initializer_list<T> expected,
185 const Collection& actual,
186 const char* msg) {
187 std::set<T> expectedSet = expected;
188
189 for (auto a: actual) {
190 ASSERT_EQ(1u, expectedSet.erase(a))
191 << msg << "\nContains not unexpected value.\n";
192 }
193
194 ASSERT_EQ(0u, expectedSet.size())
195 << msg
196 << "\nDoesn't contain expected value.";
197}
198
199#define ASSERT_ALL_EXISTS(...) \
200 assertAllExistsAnyOrder(__VA_ARGS__, (std::string("Called from: ") + \
201 std::string(__FILE__) + std::string(":") + \
202 std::to_string(__LINE__)).c_str()); \
203
204template<typename T>
205inline std::string enumToHexString(T value) {
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700206 return hexString(toInt(value));
Pavel Maltseve2603e32016-10-25 16:03:23 -0700207}
208
209template <typename T>
Yifan Hong668fed72017-01-10 18:09:48 -0800210inline std::string vecToString(const hidl_vec<T>& vec) {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700211 std::stringstream ss("[");
212 for (size_t i = 0; i < vec.size(); i++) {
213 if (i != 0) ss << ",";
214 ss << vec[i];
215 }
216 ss << "]";
217 return ss.str();
218}
219
220inline std::string toString(const VehiclePropValue &v) {
221 std::stringstream ss;
222 ss << "VehiclePropValue {n"
Pavel Maltsev8e624b32017-02-01 16:30:25 -0800223 << " prop: " << hexString(v.prop) << ",\n"
Pavel Maltseve2603e32016-10-25 16:03:23 -0700224 << " areaId: " << hexString(v.areaId) << ",\n"
225 << " timestamp: " << v.timestamp << ",\n"
226 << " value {\n"
Yifan Hong668fed72017-01-10 18:09:48 -0800227 << " int32Values: " << vecToString(v.value.int32Values) << ",\n"
228 << " floatValues: " << vecToString(v.value.floatValues) << ",\n"
229 << " int64Values: " << vecToString(v.value.int64Values) << ",\n"
230 << " bytes: " << vecToString(v.value.bytes) << ",\n"
Pavel Maltseve2603e32016-10-25 16:03:23 -0700231 << " string: " << v.value.stringValue.c_str() << ",\n"
232 << " }\n"
233 << "}\n";
234
235 return ss.str();
236}
237
238inline std::string toString(const VehiclePropConfig &config) {
239 std::stringstream ss;
240 ss << "VehiclePropConfig {\n"
Pavel Maltsev8e624b32017-02-01 16:30:25 -0800241 << " prop: " << hexString(config.prop) << ",\n"
Pavel Maltseve2603e32016-10-25 16:03:23 -0700242 << " supportedAreas: " << hexString(config.supportedAreas) << ",\n"
243 << " access: " << enumToHexString(config.access) << ",\n"
Pavel Maltseve2603e32016-10-25 16:03:23 -0700244 << " changeMode: " << enumToHexString(config.changeMode) << ",\n"
245 << " configFlags: " << hexString(config.configFlags) << ",\n"
246 << " minSampleRate: " << config.minSampleRate << ",\n"
247 << " maxSampleRate: " << config.maxSampleRate << ",\n"
248 << " configString: " << config.configString.c_str() << ",\n";
249
250 ss << " areaConfigs {\n";
251 for (size_t i = 0; i < config.areaConfigs.size(); i++) {
252 const auto &area = config.areaConfigs[i];
253 ss << " areaId: " << hexString(area.areaId) << ",\n"
254 << " minFloatValue: " << area.minFloatValue << ",\n"
255 << " minFloatValue: " << area.maxFloatValue << ",\n"
256 << " minInt32Value: " << area.minInt32Value << ",\n"
257 << " minInt32Value: " << area.maxInt32Value << ",\n"
258 << " minInt64Value: " << area.minInt64Value << ",\n"
259 << " minInt64Value: " << area.maxInt64Value << ",\n";
260 }
261 ss << " }\n"
262 << "}\n";
263
264 return ss.str();
265}
266
267
268} // namespace V2_0
269} // namespace vehicle
Pavel Maltsev2579fb72017-02-02 12:39:36 -0800270} // namespace automotive
Pavel Maltseve2603e32016-10-25 16:03:23 -0700271} // namespace hardware
272} // namespace android
273
274
Pavel Maltsev2579fb72017-02-02 12:39:36 -0800275#endif //android_hardware_automotive_vehicle_V2_0_VehicleDebugUtils_H_