Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 1 | /* |
| 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 Maltsev | 2579fb7 | 2017-02-02 12:39:36 -0800 | [diff] [blame] | 17 | #ifndef android_hardware_automotive_vehicle_V2_0_VehicleDebugUtils_H_ |
| 18 | #define android_hardware_automotive_vehicle_V2_0_VehicleDebugUtils_H_ |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 19 | |
Pavel Maltsev | 2579fb7 | 2017-02-02 12:39:36 -0800 | [diff] [blame] | 20 | #include <android/hardware/automotive/vehicle/2.0/types.h> |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 21 | #include <ios> |
| 22 | #include <sstream> |
| 23 | |
Pavel Maltsev | c5344ac | 2017-02-08 12:33:46 -0800 | [diff] [blame] | 24 | #include "vhal_v2_0/VehicleUtils.h" |
| 25 | |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 26 | namespace android { |
| 27 | namespace hardware { |
Pavel Maltsev | 2579fb7 | 2017-02-02 12:39:36 -0800 | [diff] [blame] | 28 | namespace automotive { |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 29 | namespace vehicle { |
| 30 | namespace V2_0 { |
| 31 | |
| 32 | const VehiclePropConfig kVehicleProperties[] = { |
| 33 | { |
Pavel Maltsev | 8e624b3 | 2017-02-01 16:30:25 -0800 | [diff] [blame] | 34 | .prop = toInt(VehicleProperty::INFO_MAKE), |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 35 | .access = VehiclePropertyAccess::READ, |
| 36 | .changeMode = VehiclePropertyChangeMode::STATIC, |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 37 | .configString = "Some=config,options=if,you=have_any", |
| 38 | }, |
| 39 | |
| 40 | { |
Pavel Maltsev | 8e624b3 | 2017-02-01 16:30:25 -0800 | [diff] [blame] | 41 | .prop = toInt(VehicleProperty::HVAC_FAN_SPEED), |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 42 | .access = VehiclePropertyAccess::READ_WRITE, |
| 43 | .changeMode = VehiclePropertyChangeMode::ON_CHANGE, |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 44 | .supportedAreas = static_cast<int32_t>( |
| 45 | VehicleAreaZone::ROW_1_LEFT | VehicleAreaZone::ROW_1_RIGHT), |
Pavel Maltsev | 0e0a925 | 2016-12-05 11:03:52 -0800 | [diff] [blame] | 46 | .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 Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 57 | }, |
| 58 | |
| 59 | // Write-only property |
| 60 | { |
Pavel Maltsev | 8e624b3 | 2017-02-01 16:30:25 -0800 | [diff] [blame] | 61 | .prop = toInt(VehicleProperty::HVAC_SEAT_TEMPERATURE), |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 62 | .access = VehiclePropertyAccess::WRITE, |
| 63 | .changeMode = VehiclePropertyChangeMode::ON_SET, |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 64 | .supportedAreas = static_cast<int32_t>( |
| 65 | VehicleAreaZone::ROW_1_LEFT | VehicleAreaZone::ROW_1_RIGHT), |
Pavel Maltsev | 0e0a925 | 2016-12-05 11:03:52 -0800 | [diff] [blame] | 66 | .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 Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 77 | }, |
| 78 | |
| 79 | { |
Pavel Maltsev | 8e624b3 | 2017-02-01 16:30:25 -0800 | [diff] [blame] | 80 | .prop = toInt(VehicleProperty::INFO_FUEL_CAPACITY), |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 81 | .access = VehiclePropertyAccess::READ, |
| 82 | .changeMode = VehiclePropertyChangeMode::ON_CHANGE, |
Pavel Maltsev | 0e0a925 | 2016-12-05 11:03:52 -0800 | [diff] [blame] | 83 | .areaConfigs = { |
| 84 | VehicleAreaConfig { |
| 85 | .minFloatValue = 0, |
| 86 | .maxFloatValue = 1.0 |
| 87 | } |
| 88 | } |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 89 | }, |
| 90 | |
| 91 | { |
Pavel Maltsev | 8e624b3 | 2017-02-01 16:30:25 -0800 | [diff] [blame] | 92 | .prop = toInt(VehicleProperty::DISPLAY_BRIGHTNESS), |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 93 | .access = VehiclePropertyAccess::READ_WRITE, |
| 94 | .changeMode = VehiclePropertyChangeMode::ON_CHANGE, |
Pavel Maltsev | 0e0a925 | 2016-12-05 11:03:52 -0800 | [diff] [blame] | 95 | .areaConfigs = { |
| 96 | VehicleAreaConfig { |
| 97 | .minInt32Value = 0, |
| 98 | .maxInt32Value = 10 |
| 99 | } |
| 100 | } |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 101 | }, |
| 102 | |
| 103 | { |
Pavel Maltsev | 8e624b3 | 2017-02-01 16:30:25 -0800 | [diff] [blame] | 104 | .prop = toInt(VehicleProperty::MIRROR_FOLD), |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 105 | .access = VehiclePropertyAccess::READ_WRITE, |
| 106 | .changeMode = VehiclePropertyChangeMode::ON_CHANGE, |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 107 | |
Pavel Maltsev | f21639f | 2016-12-22 11:17:29 -0800 | [diff] [blame] | 108 | }, |
| 109 | |
| 110 | // Complex data type. |
| 111 | { |
Pavel Maltsev | 8e624b3 | 2017-02-01 16:30:25 -0800 | [diff] [blame] | 112 | .prop = toInt(VehicleProperty::VEHICLE_MAP_SERVICE), |
Pavel Maltsev | f21639f | 2016-12-22 11:17:29 -0800 | [diff] [blame] | 113 | .access = VehiclePropertyAccess::READ_WRITE, |
| 114 | .changeMode = VehiclePropertyChangeMode::ON_CHANGE |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 115 | } |
| 116 | }; |
| 117 | |
| 118 | constexpr auto kTimeout = std::chrono::milliseconds(500); |
| 119 | |
| 120 | class MockedVehicleCallback : public IVehicleCallback { |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 121 | private: |
| 122 | using MuxGuard = std::lock_guard<std::mutex>; |
| 123 | using HidlVecOfValues = hidl_vec<VehiclePropValue>; |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 124 | public: |
Pavel Maltsev | 2579fb7 | 2017-02-02 12:39:36 -0800 | [diff] [blame] | 125 | // Methods from ::android::hardware::automotive::vehicle::V2_0::IVehicleCallback follow. |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 126 | 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 Maltsev | 308515f | 2017-02-06 18:50:52 -0800 | [diff] [blame] | 135 | Return<void> onPropertySet(const VehiclePropValue& /* value */) override { |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 136 | return Return<void>(); |
| 137 | } |
Pavel Maltsev | 308515f | 2017-02-06 18:50:52 -0800 | [diff] [blame] | 138 | Return<void> onPropertySetError(StatusCode /* errorCode */, |
| 139 | int32_t /* propId */, |
| 140 | int32_t /* areaId */) override { |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 141 | 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 Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 165 | const std::vector<HidlVecOfValues>& getReceivedEvents() { |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 166 | return mReceivedEvents; |
| 167 | } |
| 168 | |
| 169 | private: |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 170 | std::mutex mLock; |
| 171 | std::condition_variable mEventCond; |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 172 | std::vector<HidlVecOfValues> mReceivedEvents; |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | template<typename T> |
| 176 | inline std::string hexString(T value) { |
| 177 | std::stringstream ss; |
| 178 | ss << std::showbase << std::hex << value; |
| 179 | return ss.str(); |
| 180 | } |
| 181 | |
| 182 | template <typename T, typename Collection> |
| 183 | inline 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 | |
| 204 | template<typename T> |
| 205 | inline std::string enumToHexString(T value) { |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 206 | return hexString(toInt(value)); |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | template <typename T> |
Yifan Hong | 668fed7 | 2017-01-10 18:09:48 -0800 | [diff] [blame] | 210 | inline std::string vecToString(const hidl_vec<T>& vec) { |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 211 | 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 | |
| 220 | inline std::string toString(const VehiclePropValue &v) { |
| 221 | std::stringstream ss; |
| 222 | ss << "VehiclePropValue {n" |
Pavel Maltsev | 8e624b3 | 2017-02-01 16:30:25 -0800 | [diff] [blame] | 223 | << " prop: " << hexString(v.prop) << ",\n" |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 224 | << " areaId: " << hexString(v.areaId) << ",\n" |
| 225 | << " timestamp: " << v.timestamp << ",\n" |
| 226 | << " value {\n" |
Yifan Hong | 668fed7 | 2017-01-10 18:09:48 -0800 | [diff] [blame] | 227 | << " 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 Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 231 | << " string: " << v.value.stringValue.c_str() << ",\n" |
| 232 | << " }\n" |
| 233 | << "}\n"; |
| 234 | |
| 235 | return ss.str(); |
| 236 | } |
| 237 | |
| 238 | inline std::string toString(const VehiclePropConfig &config) { |
| 239 | std::stringstream ss; |
| 240 | ss << "VehiclePropConfig {\n" |
Pavel Maltsev | 8e624b3 | 2017-02-01 16:30:25 -0800 | [diff] [blame] | 241 | << " prop: " << hexString(config.prop) << ",\n" |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 242 | << " supportedAreas: " << hexString(config.supportedAreas) << ",\n" |
| 243 | << " access: " << enumToHexString(config.access) << ",\n" |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 244 | << " 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 Maltsev | 2579fb7 | 2017-02-02 12:39:36 -0800 | [diff] [blame] | 270 | } // namespace automotive |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 271 | } // namespace hardware |
| 272 | } // namespace android |
| 273 | |
| 274 | |
Pavel Maltsev | 2579fb7 | 2017-02-02 12:39:36 -0800 | [diff] [blame] | 275 | #endif //android_hardware_automotive_vehicle_V2_0_VehicleDebugUtils_H_ |