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 | |
| 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 | |
| 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | namespace vehicle { |
| 28 | namespace V2_0 { |
| 29 | |
| 30 | const VehiclePropConfig kVehicleProperties[] = { |
| 31 | { |
| 32 | .prop = VehicleProperty::INFO_MAKE, |
| 33 | .access = VehiclePropertyAccess::READ, |
| 34 | .changeMode = VehiclePropertyChangeMode::STATIC, |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 35 | .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 Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 42 | .supportedAreas = static_cast<int32_t>( |
| 43 | VehicleAreaZone::ROW_1_LEFT | VehicleAreaZone::ROW_1_RIGHT), |
Pavel Maltsev | 0e0a925 | 2016-12-05 11:03:52 -0800 | [diff] [blame] | 44 | .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 Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 55 | }, |
| 56 | |
| 57 | // Write-only property |
| 58 | { |
| 59 | .prop = VehicleProperty::HVAC_SEAT_TEMPERATURE, |
| 60 | .access = VehiclePropertyAccess::WRITE, |
| 61 | .changeMode = VehiclePropertyChangeMode::ON_SET, |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 62 | .supportedAreas = static_cast<int32_t>( |
| 63 | VehicleAreaZone::ROW_1_LEFT | VehicleAreaZone::ROW_1_RIGHT), |
Pavel Maltsev | 0e0a925 | 2016-12-05 11:03:52 -0800 | [diff] [blame] | 64 | .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 Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 75 | }, |
| 76 | |
| 77 | { |
| 78 | .prop = VehicleProperty::INFO_FUEL_CAPACITY, |
| 79 | .access = VehiclePropertyAccess::READ, |
| 80 | .changeMode = VehiclePropertyChangeMode::ON_CHANGE, |
Pavel Maltsev | 0e0a925 | 2016-12-05 11:03:52 -0800 | [diff] [blame] | 81 | .areaConfigs = { |
| 82 | VehicleAreaConfig { |
| 83 | .minFloatValue = 0, |
| 84 | .maxFloatValue = 1.0 |
| 85 | } |
| 86 | } |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 87 | }, |
| 88 | |
| 89 | { |
| 90 | .prop = VehicleProperty::DISPLAY_BRIGHTNESS, |
| 91 | .access = VehiclePropertyAccess::READ_WRITE, |
| 92 | .changeMode = VehiclePropertyChangeMode::ON_CHANGE, |
Pavel Maltsev | 0e0a925 | 2016-12-05 11:03:52 -0800 | [diff] [blame] | 93 | .areaConfigs = { |
| 94 | VehicleAreaConfig { |
| 95 | .minInt32Value = 0, |
| 96 | .maxInt32Value = 10 |
| 97 | } |
| 98 | } |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 99 | }, |
| 100 | |
| 101 | { |
| 102 | .prop = VehicleProperty::MIRROR_FOLD, |
| 103 | .access = VehiclePropertyAccess::READ_WRITE, |
| 104 | .changeMode = VehiclePropertyChangeMode::ON_CHANGE, |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 105 | |
Pavel Maltsev | f21639f | 2016-12-22 11:17:29 -0800 | [diff] [blame] | 106 | }, |
| 107 | |
| 108 | // Complex data type. |
| 109 | { |
| 110 | .prop = VehicleProperty::VEHICLE_MAPS_DATA_SERVICE, |
| 111 | .access = VehiclePropertyAccess::READ_WRITE, |
| 112 | .changeMode = VehiclePropertyChangeMode::ON_CHANGE |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 113 | } |
| 114 | }; |
| 115 | |
| 116 | constexpr auto kTimeout = std::chrono::milliseconds(500); |
| 117 | |
| 118 | class MockedVehicleCallback : public IVehicleCallback { |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 119 | private: |
| 120 | using MuxGuard = std::lock_guard<std::mutex>; |
| 121 | using HidlVecOfValues = hidl_vec<VehiclePropValue>; |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 122 | public: |
| 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 Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 136 | Return<void> onPropertySetError(StatusCode errorCode, |
| 137 | VehicleProperty propId, |
| 138 | int32_t areaId) override { |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 139 | 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 Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 163 | const std::vector<HidlVecOfValues>& getReceivedEvents() { |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 164 | return mReceivedEvents; |
| 165 | } |
| 166 | |
| 167 | private: |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 168 | std::mutex mLock; |
| 169 | std::condition_variable mEventCond; |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 170 | std::vector<HidlVecOfValues> mReceivedEvents; |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | template<typename T> |
| 174 | inline std::string hexString(T value) { |
| 175 | std::stringstream ss; |
| 176 | ss << std::showbase << std::hex << value; |
| 177 | return ss.str(); |
| 178 | } |
| 179 | |
| 180 | template <typename T, typename Collection> |
| 181 | inline 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 | |
| 202 | template<typename T> |
| 203 | inline std::string enumToHexString(T value) { |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 204 | return hexString(toInt(value)); |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | template <typename T> |
Yifan Hong | 668fed7 | 2017-01-10 18:09:48 -0800 | [diff] [blame] | 208 | inline std::string vecToString(const hidl_vec<T>& vec) { |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 209 | 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 | |
| 218 | inline 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 Hong | 668fed7 | 2017-01-10 18:09:48 -0800 | [diff] [blame] | 225 | << " 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 Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 229 | << " string: " << v.value.stringValue.c_str() << ",\n" |
| 230 | << " }\n" |
| 231 | << "}\n"; |
| 232 | |
| 233 | return ss.str(); |
| 234 | } |
| 235 | |
| 236 | inline 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 Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 242 | << " 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 |