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 | |
Steve Paik | 1ae977d | 2018-01-22 19:13:28 -0800 | [diff] [blame] | 32 | constexpr int32_t kCustomComplexProperty = |
| 33 | 0xbeef | VehiclePropertyGroup::VENDOR | VehiclePropertyType::MIXED | VehicleArea::GLOBAL; |
Antonio Cortes | dd1b9d3 | 2017-02-22 07:30:13 -0800 | [diff] [blame] | 34 | |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 35 | const VehiclePropConfig kVehicleProperties[] = { |
| 36 | { |
Pavel Maltsev | 8e624b3 | 2017-02-01 16:30:25 -0800 | [diff] [blame] | 37 | .prop = toInt(VehicleProperty::INFO_MAKE), |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 38 | .access = VehiclePropertyAccess::READ, |
| 39 | .changeMode = VehiclePropertyChangeMode::STATIC, |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 40 | .configString = "Some=config,options=if,you=have_any", |
| 41 | }, |
| 42 | |
| 43 | { |
Pavel Maltsev | 8e624b3 | 2017-02-01 16:30:25 -0800 | [diff] [blame] | 44 | .prop = toInt(VehicleProperty::HVAC_FAN_SPEED), |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 45 | .access = VehiclePropertyAccess::READ_WRITE, |
| 46 | .changeMode = VehiclePropertyChangeMode::ON_CHANGE, |
Pavel Maltsev | 0e0a925 | 2016-12-05 11:03:52 -0800 | [diff] [blame] | 47 | .areaConfigs = { |
| 48 | VehicleAreaConfig { |
| 49 | .areaId = toInt(VehicleAreaZone::ROW_1_LEFT), |
| 50 | .minInt32Value = 1, |
| 51 | .maxInt32Value = 7}, |
| 52 | VehicleAreaConfig { |
| 53 | .areaId = toInt(VehicleAreaZone::ROW_1_RIGHT), |
| 54 | .minInt32Value = 1, |
| 55 | .maxInt32Value = 5, |
| 56 | } |
| 57 | } |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 58 | }, |
| 59 | |
| 60 | // Write-only property |
| 61 | { |
Pavel Maltsev | 8e624b3 | 2017-02-01 16:30:25 -0800 | [diff] [blame] | 62 | .prop = toInt(VehicleProperty::HVAC_SEAT_TEMPERATURE), |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 63 | .access = VehiclePropertyAccess::WRITE, |
| 64 | .changeMode = VehiclePropertyChangeMode::ON_SET, |
Pavel Maltsev | 0e0a925 | 2016-12-05 11:03:52 -0800 | [diff] [blame] | 65 | .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 Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 76 | }, |
| 77 | |
| 78 | { |
Pavel Maltsev | 8e624b3 | 2017-02-01 16:30:25 -0800 | [diff] [blame] | 79 | .prop = toInt(VehicleProperty::INFO_FUEL_CAPACITY), |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 80 | .access = VehiclePropertyAccess::READ, |
| 81 | .changeMode = VehiclePropertyChangeMode::ON_CHANGE, |
Pavel Maltsev | 0e0a925 | 2016-12-05 11:03:52 -0800 | [diff] [blame] | 82 | .areaConfigs = { |
| 83 | VehicleAreaConfig { |
| 84 | .minFloatValue = 0, |
| 85 | .maxFloatValue = 1.0 |
| 86 | } |
| 87 | } |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 88 | }, |
| 89 | |
| 90 | { |
Pavel Maltsev | 8e624b3 | 2017-02-01 16:30:25 -0800 | [diff] [blame] | 91 | .prop = toInt(VehicleProperty::DISPLAY_BRIGHTNESS), |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 92 | .access = VehiclePropertyAccess::READ_WRITE, |
| 93 | .changeMode = VehiclePropertyChangeMode::ON_CHANGE, |
Pavel Maltsev | 0e0a925 | 2016-12-05 11:03:52 -0800 | [diff] [blame] | 94 | .areaConfigs = { |
| 95 | VehicleAreaConfig { |
| 96 | .minInt32Value = 0, |
| 97 | .maxInt32Value = 10 |
| 98 | } |
| 99 | } |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 100 | }, |
| 101 | |
| 102 | { |
Pavel Maltsev | 8e624b3 | 2017-02-01 16:30:25 -0800 | [diff] [blame] | 103 | .prop = toInt(VehicleProperty::MIRROR_FOLD), |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 104 | .access = VehiclePropertyAccess::READ_WRITE, |
| 105 | .changeMode = VehiclePropertyChangeMode::ON_CHANGE, |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 106 | |
Pavel Maltsev | f21639f | 2016-12-22 11:17:29 -0800 | [diff] [blame] | 107 | }, |
| 108 | |
| 109 | // Complex data type. |
| 110 | { |
Antonio Cortes | dd1b9d3 | 2017-02-22 07:30:13 -0800 | [diff] [blame] | 111 | .prop = kCustomComplexProperty, |
Pavel Maltsev | f21639f | 2016-12-22 11:17:29 -0800 | [diff] [blame] | 112 | .access = VehiclePropertyAccess::READ_WRITE, |
| 113 | .changeMode = VehiclePropertyChangeMode::ON_CHANGE |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 114 | } |
| 115 | }; |
| 116 | |
| 117 | constexpr auto kTimeout = std::chrono::milliseconds(500); |
| 118 | |
| 119 | class MockedVehicleCallback : public IVehicleCallback { |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 120 | private: |
| 121 | using MuxGuard = std::lock_guard<std::mutex>; |
| 122 | using HidlVecOfValues = hidl_vec<VehiclePropValue>; |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 123 | public: |
Pavel Maltsev | 2579fb7 | 2017-02-02 12:39:36 -0800 | [diff] [blame] | 124 | // Methods from ::android::hardware::automotive::vehicle::V2_0::IVehicleCallback follow. |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 125 | 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 Maltsev | 308515f | 2017-02-06 18:50:52 -0800 | [diff] [blame] | 134 | Return<void> onPropertySet(const VehiclePropValue& /* value */) override { |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 135 | return Return<void>(); |
| 136 | } |
Pavel Maltsev | 308515f | 2017-02-06 18:50:52 -0800 | [diff] [blame] | 137 | Return<void> onPropertySetError(StatusCode /* errorCode */, |
| 138 | int32_t /* propId */, |
| 139 | int32_t /* areaId */) override { |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 140 | 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 Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 164 | const std::vector<HidlVecOfValues>& getReceivedEvents() { |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 165 | return mReceivedEvents; |
| 166 | } |
| 167 | |
| 168 | private: |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 169 | std::mutex mLock; |
| 170 | std::condition_variable mEventCond; |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 171 | std::vector<HidlVecOfValues> mReceivedEvents; |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | template<typename T> |
| 175 | inline std::string hexString(T value) { |
| 176 | std::stringstream ss; |
| 177 | ss << std::showbase << std::hex << value; |
| 178 | return ss.str(); |
| 179 | } |
| 180 | |
| 181 | template <typename T, typename Collection> |
| 182 | inline 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 | |
| 203 | template<typename T> |
| 204 | inline std::string enumToHexString(T value) { |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 205 | return hexString(toInt(value)); |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | template <typename T> |
Yifan Hong | 668fed7 | 2017-01-10 18:09:48 -0800 | [diff] [blame] | 209 | inline std::string vecToString(const hidl_vec<T>& vec) { |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 210 | 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 | |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 219 | } // namespace V2_0 |
| 220 | } // namespace vehicle |
Pavel Maltsev | 2579fb7 | 2017-02-02 12:39:36 -0800 | [diff] [blame] | 221 | } // namespace automotive |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 222 | } // namespace hardware |
| 223 | } // namespace android |
| 224 | |
| 225 | |
Pavel Maltsev | 2579fb7 | 2017-02-02 12:39:36 -0800 | [diff] [blame] | 226 | #endif //android_hardware_automotive_vehicle_V2_0_VehicleDebugUtils_H_ |