blob: 108e32fef294029c2931fd585693b4cf952cc364 [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
Steve Paik1ae977d2018-01-22 19:13:28 -080032constexpr int32_t kCustomComplexProperty =
33 0xbeef | VehiclePropertyGroup::VENDOR | VehiclePropertyType::MIXED | VehicleArea::GLOBAL;
Antonio Cortesdd1b9d32017-02-22 07:30:13 -080034
Pavel Maltseve2603e32016-10-25 16:03:23 -070035const VehiclePropConfig kVehicleProperties[] = {
36 {
Pavel Maltsev8e624b32017-02-01 16:30:25 -080037 .prop = toInt(VehicleProperty::INFO_MAKE),
Pavel Maltseve2603e32016-10-25 16:03:23 -070038 .access = VehiclePropertyAccess::READ,
39 .changeMode = VehiclePropertyChangeMode::STATIC,
Pavel Maltseve2603e32016-10-25 16:03:23 -070040 .configString = "Some=config,options=if,you=have_any",
41 },
42
Steve Paik28b9f8b2018-03-29 23:18:13 -070043 {.prop = toInt(VehicleProperty::HVAC_FAN_SPEED),
44 .access = VehiclePropertyAccess::READ_WRITE,
45 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
46 .areaConfigs =
47 {VehicleAreaConfig{
48 .areaId = toInt(VehicleAreaSeat::ROW_1_LEFT), .minInt32Value = 1, .maxInt32Value = 7},
49 VehicleAreaConfig{
50 .areaId = toInt(VehicleAreaSeat::ROW_1_RIGHT), .minInt32Value = 1, .maxInt32Value = 5,
51 }}},
Pavel Maltsevdb179c52016-10-27 15:43:06 -070052
53 // Write-only property
Steve Paik28b9f8b2018-03-29 23:18:13 -070054 {.prop = toInt(VehicleProperty::HVAC_SEAT_TEMPERATURE),
55 .access = VehiclePropertyAccess::WRITE,
56 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
57 .areaConfigs = {VehicleAreaConfig{.areaId = toInt(VehicleAreaSeat::ROW_1_LEFT),
58 .minInt32Value = 64,
59 .maxInt32Value = 80},
60 VehicleAreaConfig{
61 .areaId = toInt(VehicleAreaSeat::ROW_1_RIGHT),
62 .minInt32Value = 64,
63 .maxInt32Value = 80,
64 }}},
Pavel Maltseve2603e32016-10-25 16:03:23 -070065
Steve Paik28b9f8b2018-03-29 23:18:13 -070066 {.prop = toInt(VehicleProperty::INFO_FUEL_CAPACITY),
67 .access = VehiclePropertyAccess::READ,
68 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
69 .areaConfigs = {VehicleAreaConfig{.minFloatValue = 0, .maxFloatValue = 1.0}}},
Pavel Maltseve2603e32016-10-25 16:03:23 -070070
Steve Paik28b9f8b2018-03-29 23:18:13 -070071 {.prop = toInt(VehicleProperty::DISPLAY_BRIGHTNESS),
72 .access = VehiclePropertyAccess::READ_WRITE,
73 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
74 .areaConfigs = {VehicleAreaConfig{.minInt32Value = 0, .maxInt32Value = 10}}},
Pavel Maltsevdb179c52016-10-27 15:43:06 -070075
76 {
Pavel Maltsev8e624b32017-02-01 16:30:25 -080077 .prop = toInt(VehicleProperty::MIRROR_FOLD),
Pavel Maltsevdb179c52016-10-27 15:43:06 -070078 .access = VehiclePropertyAccess::READ_WRITE,
79 .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
Pavel Maltsevdb179c52016-10-27 15:43:06 -070080
Pavel Maltsevf21639f2016-12-22 11:17:29 -080081 },
82
83 // Complex data type.
Steve Paik28b9f8b2018-03-29 23:18:13 -070084 {.prop = kCustomComplexProperty,
85 .access = VehiclePropertyAccess::READ_WRITE,
86 .changeMode = VehiclePropertyChangeMode::ON_CHANGE}};
Pavel Maltseve2603e32016-10-25 16:03:23 -070087
88constexpr auto kTimeout = std::chrono::milliseconds(500);
89
90class MockedVehicleCallback : public IVehicleCallback {
Pavel Maltsevdb179c52016-10-27 15:43:06 -070091private:
92 using MuxGuard = std::lock_guard<std::mutex>;
93 using HidlVecOfValues = hidl_vec<VehiclePropValue>;
Pavel Maltseve2603e32016-10-25 16:03:23 -070094public:
Pavel Maltsev2579fb72017-02-02 12:39:36 -080095 // Methods from ::android::hardware::automotive::vehicle::V2_0::IVehicleCallback follow.
Pavel Maltseve2603e32016-10-25 16:03:23 -070096 Return<void> onPropertyEvent(
97 const hidl_vec<VehiclePropValue>& values) override {
98 {
99 MuxGuard g(mLock);
100 mReceivedEvents.push_back(values);
101 }
102 mEventCond.notify_one();
103 return Return<void>();
104 }
Pavel Maltsev308515f2017-02-06 18:50:52 -0800105 Return<void> onPropertySet(const VehiclePropValue& /* value */) override {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700106 return Return<void>();
107 }
Pavel Maltsev308515f2017-02-06 18:50:52 -0800108 Return<void> onPropertySetError(StatusCode /* errorCode */,
109 int32_t /* propId */,
110 int32_t /* areaId */) override {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700111 return Return<void>();
112 }
113
114 bool waitForExpectedEvents(size_t expectedEvents) {
115 std::unique_lock<std::mutex> g(mLock);
116
117 if (expectedEvents == 0 && mReceivedEvents.size() == 0) {
118 // No events expected, let's sleep a little bit to make sure
119 // nothing will show up.
120 return mEventCond.wait_for(g, kTimeout) == std::cv_status::timeout;
121 }
122
123 while (expectedEvents != mReceivedEvents.size()) {
124 if (mEventCond.wait_for(g, kTimeout) == std::cv_status::timeout) {
125 return false;
126 }
127 }
128 return true;
129 }
130
131 void reset() {
132 mReceivedEvents.clear();
133 }
134
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700135 const std::vector<HidlVecOfValues>& getReceivedEvents() {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700136 return mReceivedEvents;
137 }
138
139private:
Pavel Maltseve2603e32016-10-25 16:03:23 -0700140 std::mutex mLock;
141 std::condition_variable mEventCond;
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700142 std::vector<HidlVecOfValues> mReceivedEvents;
Pavel Maltseve2603e32016-10-25 16:03:23 -0700143};
144
145template<typename T>
146inline std::string hexString(T value) {
147 std::stringstream ss;
148 ss << std::showbase << std::hex << value;
149 return ss.str();
150}
151
152template <typename T, typename Collection>
153inline void assertAllExistsAnyOrder(
154 std::initializer_list<T> expected,
155 const Collection& actual,
156 const char* msg) {
157 std::set<T> expectedSet = expected;
158
159 for (auto a: actual) {
160 ASSERT_EQ(1u, expectedSet.erase(a))
161 << msg << "\nContains not unexpected value.\n";
162 }
163
164 ASSERT_EQ(0u, expectedSet.size())
165 << msg
166 << "\nDoesn't contain expected value.";
167}
168
169#define ASSERT_ALL_EXISTS(...) \
170 assertAllExistsAnyOrder(__VA_ARGS__, (std::string("Called from: ") + \
171 std::string(__FILE__) + std::string(":") + \
172 std::to_string(__LINE__)).c_str()); \
173
174template<typename T>
175inline std::string enumToHexString(T value) {
Pavel Maltsevdb179c52016-10-27 15:43:06 -0700176 return hexString(toInt(value));
Pavel Maltseve2603e32016-10-25 16:03:23 -0700177}
178
179template <typename T>
Yifan Hong668fed72017-01-10 18:09:48 -0800180inline std::string vecToString(const hidl_vec<T>& vec) {
Pavel Maltseve2603e32016-10-25 16:03:23 -0700181 std::stringstream ss("[");
182 for (size_t i = 0; i < vec.size(); i++) {
183 if (i != 0) ss << ",";
184 ss << vec[i];
185 }
186 ss << "]";
187 return ss.str();
188}
189
Pavel Maltseve2603e32016-10-25 16:03:23 -0700190} // namespace V2_0
191} // namespace vehicle
Pavel Maltsev2579fb72017-02-02 12:39:36 -0800192} // namespace automotive
Pavel Maltseve2603e32016-10-25 16:03:23 -0700193} // namespace hardware
194} // namespace android
195
196
Pavel Maltsev2579fb72017-02-02 12:39:36 -0800197#endif //android_hardware_automotive_vehicle_V2_0_VehicleDebugUtils_H_