blob: 8ba36d8206022ed0a4a40da3919b83ab8acac9a8 [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 <vehicle_hal_manager/VehicleUtils.h>
22#include <ios>
23#include <sstream>
24
25namespace android {
26namespace hardware {
Pavel Maltsev2579fb72017-02-02 12:39:36 -080027namespace automotive {
Pavel Maltseve2603e32016-10-25 16:03:23 -070028namespace vehicle {
29namespace V2_0 {
30
31const VehiclePropConfig kVehicleProperties[] = {
32 {
Pavel Maltsev8e624b32017-02-01 16:30:25 -080033 .prop = toInt(VehicleProperty::INFO_MAKE),
Pavel Maltseve2603e32016-10-25 16:03:23 -070034 .access = VehiclePropertyAccess::READ,
35 .changeMode = VehiclePropertyChangeMode::STATIC,
Pavel Maltseve2603e32016-10-25 16:03:23 -070036 .configString = "Some=config,options=if,you=have_any",
37 },
38
39 {
Pavel Maltsev8e624b32017-02-01 16:30:25 -080040 .prop = toInt(VehicleProperty::HVAC_FAN_SPEED),
Pavel Maltseve2603e32016-10-25 16:03:23 -070041 .access = VehiclePropertyAccess::READ_WRITE,
42 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
Pavel Maltseve2603e32016-10-25 16:03:23 -070043 .supportedAreas = static_cast<int32_t>(
44 VehicleAreaZone::ROW_1_LEFT | VehicleAreaZone::ROW_1_RIGHT),
Pavel Maltsev0e0a9252016-12-05 11:03:52 -080045 .areaConfigs = {
46 VehicleAreaConfig {
47 .areaId = toInt(VehicleAreaZone::ROW_1_LEFT),
48 .minInt32Value = 1,
49 .maxInt32Value = 7},
50 VehicleAreaConfig {
51 .areaId = toInt(VehicleAreaZone::ROW_1_RIGHT),
52 .minInt32Value = 1,
53 .maxInt32Value = 5,
54 }
55 }
Pavel Maltsevdb179c52016-10-27 15:43:06 -070056 },
57
58 // Write-only property
59 {
Pavel Maltsev8e624b32017-02-01 16:30:25 -080060 .prop = toInt(VehicleProperty::HVAC_SEAT_TEMPERATURE),
Pavel Maltsevdb179c52016-10-27 15:43:06 -070061 .access = VehiclePropertyAccess::WRITE,
62 .changeMode = VehiclePropertyChangeMode::ON_SET,
Pavel Maltsevdb179c52016-10-27 15:43:06 -070063 .supportedAreas = static_cast<int32_t>(
64 VehicleAreaZone::ROW_1_LEFT | VehicleAreaZone::ROW_1_RIGHT),
Pavel Maltsev0e0a9252016-12-05 11:03:52 -080065 .areaConfigs = {
66 VehicleAreaConfig {
67 .areaId = toInt(VehicleAreaZone::ROW_1_LEFT),
68 .minInt32Value = 64,
69 .maxInt32Value = 80},
70 VehicleAreaConfig {
71 .areaId = toInt(VehicleAreaZone::ROW_1_RIGHT),
72 .minInt32Value = 64,
73 .maxInt32Value = 80,
74 }
75 }
Pavel Maltseve2603e32016-10-25 16:03:23 -070076 },
77
78 {
Pavel Maltsev8e624b32017-02-01 16:30:25 -080079 .prop = toInt(VehicleProperty::INFO_FUEL_CAPACITY),
Pavel Maltseve2603e32016-10-25 16:03:23 -070080 .access = VehiclePropertyAccess::READ,
81 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
Pavel Maltsev0e0a9252016-12-05 11:03:52 -080082 .areaConfigs = {
83 VehicleAreaConfig {
84 .minFloatValue = 0,
85 .maxFloatValue = 1.0
86 }
87 }
Pavel Maltseve2603e32016-10-25 16:03:23 -070088 },
89
90 {
Pavel Maltsev8e624b32017-02-01 16:30:25 -080091 .prop = toInt(VehicleProperty::DISPLAY_BRIGHTNESS),
Pavel Maltseve2603e32016-10-25 16:03:23 -070092 .access = VehiclePropertyAccess::READ_WRITE,
93 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
Pavel Maltsev0e0a9252016-12-05 11:03:52 -080094 .areaConfigs = {
95 VehicleAreaConfig {
96 .minInt32Value = 0,
97 .maxInt32Value = 10
98 }
99 }
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700100 },
101
102 {
Pavel Maltsev8e624b32017-02-01 16:30:25 -0800103 .prop = toInt(VehicleProperty::MIRROR_FOLD),
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700104 .access = VehiclePropertyAccess::READ_WRITE,
105 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700106
Pavel Maltsevf21639f2016-12-22 11:17:29 -0800107 },
108
109 // Complex data type.
110 {
Pavel Maltsev8e624b32017-02-01 16:30:25 -0800111 .prop = toInt(VehicleProperty::VEHICLE_MAP_SERVICE),
Pavel Maltsevf21639f2016-12-22 11:17:29 -0800112 .access = VehiclePropertyAccess::READ_WRITE,
113 .changeMode = VehiclePropertyChangeMode::ON_CHANGE
Pavel Maltseve2603e32016-10-25 16:03:23 -0700114 }
115};
116
117constexpr auto kTimeout = std::chrono::milliseconds(500);
118
119class MockedVehicleCallback : public IVehicleCallback {
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700120private:
121 using MuxGuard = std::lock_guard<std::mutex>;
122 using HidlVecOfValues = hidl_vec<VehiclePropValue>;
Pavel Maltseve2603e32016-10-25 16:03:23 -0700123public:
Pavel Maltsev2579fb72017-02-02 12:39:36 -0800124 // Methods from ::android::hardware::automotive::vehicle::V2_0::IVehicleCallback follow.
Pavel Maltseve2603e32016-10-25 16:03:23 -0700125 Return<void> onPropertyEvent(
126 const hidl_vec<VehiclePropValue>& values) override {
127 {
128 MuxGuard g(mLock);
129 mReceivedEvents.push_back(values);
130 }
131 mEventCond.notify_one();
132 return Return<void>();
133 }
Pavel Maltsev308515f2017-02-06 18:50:52 -0800134 Return<void> onPropertySet(const VehiclePropValue& /* value */) override {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700135 return Return<void>();
136 }
Pavel Maltsev308515f2017-02-06 18:50:52 -0800137 Return<void> onPropertySetError(StatusCode /* errorCode */,
138 int32_t /* propId */,
139 int32_t /* areaId */) override {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700140 return Return<void>();
141 }
142
143 bool waitForExpectedEvents(size_t expectedEvents) {
144 std::unique_lock<std::mutex> g(mLock);
145
146 if (expectedEvents == 0 && mReceivedEvents.size() == 0) {
147 // No events expected, let's sleep a little bit to make sure
148 // nothing will show up.
149 return mEventCond.wait_for(g, kTimeout) == std::cv_status::timeout;
150 }
151
152 while (expectedEvents != mReceivedEvents.size()) {
153 if (mEventCond.wait_for(g, kTimeout) == std::cv_status::timeout) {
154 return false;
155 }
156 }
157 return true;
158 }
159
160 void reset() {
161 mReceivedEvents.clear();
162 }
163
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700164 const std::vector<HidlVecOfValues>& getReceivedEvents() {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700165 return mReceivedEvents;
166 }
167
168private:
Pavel Maltseve2603e32016-10-25 16:03:23 -0700169 std::mutex mLock;
170 std::condition_variable mEventCond;
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700171 std::vector<HidlVecOfValues> mReceivedEvents;
Pavel Maltseve2603e32016-10-25 16:03:23 -0700172};
173
174template<typename T>
175inline std::string hexString(T value) {
176 std::stringstream ss;
177 ss << std::showbase << std::hex << value;
178 return ss.str();
179}
180
181template <typename T, typename Collection>
182inline void assertAllExistsAnyOrder(
183 std::initializer_list<T> expected,
184 const Collection& actual,
185 const char* msg) {
186 std::set<T> expectedSet = expected;
187
188 for (auto a: actual) {
189 ASSERT_EQ(1u, expectedSet.erase(a))
190 << msg << "\nContains not unexpected value.\n";
191 }
192
193 ASSERT_EQ(0u, expectedSet.size())
194 << msg
195 << "\nDoesn't contain expected value.";
196}
197
198#define ASSERT_ALL_EXISTS(...) \
199 assertAllExistsAnyOrder(__VA_ARGS__, (std::string("Called from: ") + \
200 std::string(__FILE__) + std::string(":") + \
201 std::to_string(__LINE__)).c_str()); \
202
203template<typename T>
204inline std::string enumToHexString(T value) {
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700205 return hexString(toInt(value));
Pavel Maltseve2603e32016-10-25 16:03:23 -0700206}
207
208template <typename T>
Yifan Hong668fed72017-01-10 18:09:48 -0800209inline std::string vecToString(const hidl_vec<T>& vec) {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700210 std::stringstream ss("[");
211 for (size_t i = 0; i < vec.size(); i++) {
212 if (i != 0) ss << ",";
213 ss << vec[i];
214 }
215 ss << "]";
216 return ss.str();
217}
218
219inline std::string toString(const VehiclePropValue &v) {
220 std::stringstream ss;
221 ss << "VehiclePropValue {n"
Pavel Maltsev8e624b32017-02-01 16:30:25 -0800222 << " prop: " << hexString(v.prop) << ",\n"
Pavel Maltseve2603e32016-10-25 16:03:23 -0700223 << " areaId: " << hexString(v.areaId) << ",\n"
224 << " timestamp: " << v.timestamp << ",\n"
225 << " value {\n"
Yifan Hong668fed72017-01-10 18:09:48 -0800226 << " int32Values: " << vecToString(v.value.int32Values) << ",\n"
227 << " floatValues: " << vecToString(v.value.floatValues) << ",\n"
228 << " int64Values: " << vecToString(v.value.int64Values) << ",\n"
229 << " bytes: " << vecToString(v.value.bytes) << ",\n"
Pavel Maltseve2603e32016-10-25 16:03:23 -0700230 << " string: " << v.value.stringValue.c_str() << ",\n"
231 << " }\n"
232 << "}\n";
233
234 return ss.str();
235}
236
237inline std::string toString(const VehiclePropConfig &config) {
238 std::stringstream ss;
239 ss << "VehiclePropConfig {\n"
Pavel Maltsev8e624b32017-02-01 16:30:25 -0800240 << " prop: " << hexString(config.prop) << ",\n"
Pavel Maltseve2603e32016-10-25 16:03:23 -0700241 << " supportedAreas: " << hexString(config.supportedAreas) << ",\n"
242 << " access: " << enumToHexString(config.access) << ",\n"
Pavel Maltseve2603e32016-10-25 16:03:23 -0700243 << " changeMode: " << enumToHexString(config.changeMode) << ",\n"
244 << " configFlags: " << hexString(config.configFlags) << ",\n"
245 << " minSampleRate: " << config.minSampleRate << ",\n"
246 << " maxSampleRate: " << config.maxSampleRate << ",\n"
247 << " configString: " << config.configString.c_str() << ",\n";
248
249 ss << " areaConfigs {\n";
250 for (size_t i = 0; i < config.areaConfigs.size(); i++) {
251 const auto &area = config.areaConfigs[i];
252 ss << " areaId: " << hexString(area.areaId) << ",\n"
253 << " minFloatValue: " << area.minFloatValue << ",\n"
254 << " minFloatValue: " << area.maxFloatValue << ",\n"
255 << " minInt32Value: " << area.minInt32Value << ",\n"
256 << " minInt32Value: " << area.maxInt32Value << ",\n"
257 << " minInt64Value: " << area.minInt64Value << ",\n"
258 << " minInt64Value: " << area.maxInt64Value << ",\n";
259 }
260 ss << " }\n"
261 << "}\n";
262
263 return ss.str();
264}
265
266
267} // namespace V2_0
268} // namespace vehicle
Pavel Maltsev2579fb72017-02-02 12:39:36 -0800269} // namespace automotive
Pavel Maltseve2603e32016-10-25 16:03:23 -0700270} // namespace hardware
271} // namespace android
272
273
Pavel Maltsev2579fb72017-02-02 12:39:36 -0800274#endif //android_hardware_automotive_vehicle_V2_0_VehicleDebugUtils_H_