• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <unordered_map>
18 #include <iostream>
19 
20 #include <android-base/macros.h>
21 #include <utils/SystemClock.h>
22 
23 #include <gtest/gtest.h>
24 
25 #include "vhal_v2_0/VehicleHalManager.h"
26 
27 #include "VehicleHalTestUtils.h"
28 
29 namespace android {
30 namespace hardware {
31 namespace automotive {
32 namespace vehicle {
33 namespace V2_0 {
34 
35 namespace {
36 
37 using namespace std::placeholders;
38 
39 constexpr char kCarMake[] = "Default Car";
40 constexpr int kRetriablePropMockedAttempts = 3;
41 
42 class MockedVehicleHal : public VehicleHal {
43 public:
MockedVehicleHal()44     MockedVehicleHal() {
45         mConfigs.assign(std::begin(kVehicleProperties),
46                         std::end(kVehicleProperties));
47     }
48 
listProperties()49     std::vector<VehiclePropConfig> listProperties() override {
50         return mConfigs;
51     }
52 
get(const VehiclePropValue & requestedPropValue,StatusCode * outStatus)53     VehiclePropValuePtr get(const VehiclePropValue& requestedPropValue,
54              StatusCode* outStatus) override {
55         *outStatus = StatusCode::OK;
56         VehiclePropValuePtr pValue;
57         auto property = static_cast<VehicleProperty>(requestedPropValue.prop);
58         int32_t areaId = requestedPropValue.areaId;
59 
60         switch (property) {
61             case VehicleProperty::INFO_MAKE:
62                 pValue = getValuePool()->obtainString(kCarMake);
63                 break;
64             case VehicleProperty::INFO_FUEL_CAPACITY:
65                 if (fuelCapacityAttemptsLeft-- > 0) {
66                     // Emulate property not ready yet.
67                     *outStatus = StatusCode::TRY_AGAIN;
68                 } else {
69                     pValue = getValuePool()->obtainFloat(42.42);
70                 }
71                 break;
72             default:
73                 if (requestedPropValue.prop == kCustomComplexProperty) {
74                     pValue = getValuePool()->obtainComplex();
75                     pValue->value.int32Values = hidl_vec<int32_t> { 10, 20 };
76                     pValue->value.int64Values = hidl_vec<int64_t> { 30, 40 };
77                     pValue->value.floatValues = hidl_vec<float_t> { 1.1, 2.2 };
78                     pValue->value.bytes = hidl_vec<uint8_t> { 1, 2, 3 };
79                     pValue->value.stringValue = kCarMake;
80                     break;
81                 }
82                 auto key = makeKey(toInt(property), areaId);
83                 if (mValues.count(key) == 0) {
84                     ALOGW("");
85                 }
86                 pValue = getValuePool()->obtain(mValues[key]);
87         }
88 
89         if (*outStatus == StatusCode::OK && pValue.get() != nullptr) {
90             pValue->prop = toInt(property);
91             pValue->areaId = areaId;
92             pValue->timestamp = elapsedRealtimeNano();
93         }
94 
95         return pValue;
96     }
97 
set(const VehiclePropValue & propValue)98     StatusCode set(const VehiclePropValue& propValue) override {
99         if (toInt(VehicleProperty::MIRROR_FOLD) == propValue.prop
100                 && mirrorFoldAttemptsLeft-- > 0) {
101             return StatusCode::TRY_AGAIN;
102         }
103 
104         mValues[makeKey(propValue)] = propValue;
105         return StatusCode::OK;
106     }
107 
subscribe(int32_t,float)108     StatusCode subscribe(int32_t /* property */,
109                          float /* sampleRate */) override {
110         return StatusCode::OK;
111     }
112 
unsubscribe(int32_t)113     StatusCode unsubscribe(int32_t /* property */) override {
114         return StatusCode::OK;
115     }
116 
sendPropEvent(recyclable_ptr<VehiclePropValue> value)117     void sendPropEvent(recyclable_ptr<VehiclePropValue> value) {
118         doHalEvent(std::move(value));
119     }
120 
sendHalError(StatusCode error,int32_t property,int32_t areaId)121     void sendHalError(StatusCode error, int32_t property, int32_t areaId) {
122         doHalPropertySetError(error, property, areaId);
123     }
124 
125 public:
126     int fuelCapacityAttemptsLeft = kRetriablePropMockedAttempts;
127     int mirrorFoldAttemptsLeft = kRetriablePropMockedAttempts;
128 
129 private:
makeKey(const VehiclePropValue & v) const130     int64_t makeKey(const VehiclePropValue& v) const {
131         return makeKey(v.prop, v.areaId);
132     }
133 
makeKey(int32_t prop,int32_t area) const134     int64_t makeKey(int32_t prop, int32_t area) const {
135         return (static_cast<int64_t>(prop) << 32) | area;
136     }
137 
138 private:
139     std::vector<VehiclePropConfig> mConfigs;
140     std::unordered_map<int64_t, VehiclePropValue> mValues;
141 };
142 
143 class VehicleHalManagerTest : public ::testing::Test {
144 protected:
SetUp()145     void SetUp() override {
146         hal.reset(new MockedVehicleHal);
147         manager.reset(new VehicleHalManager(hal.get()));
148 
149         objectPool = hal->getValuePool();
150     }
151 
TearDown()152     void TearDown() override {
153         manager.reset(nullptr);
154         hal.reset(nullptr);
155     }
156 public:
invokeGet(int32_t property,int32_t areaId)157     void invokeGet(int32_t property, int32_t areaId) {
158         VehiclePropValue requestedValue {};
159         requestedValue.prop = property;
160         requestedValue.areaId = areaId;
161 
162         invokeGet(requestedValue);
163     }
164 
invokeGet(const VehiclePropValue & requestedPropValue)165     void invokeGet(const VehiclePropValue& requestedPropValue) {
166         actualValue = VehiclePropValue {};  // reset previous values
167 
168         StatusCode refStatus;
169         VehiclePropValue refValue;
170         bool called = false;
171         manager->get(requestedPropValue, [&refStatus, &refValue, &called]
172             (StatusCode status, const VehiclePropValue& value) {
173             refStatus = status;
174             refValue = value;
175             called = true;
176         });
177         ASSERT_TRUE(called) << "callback wasn't called for prop: "
178                             << hexString(requestedPropValue.prop);
179 
180         actualValue = refValue;
181         actualStatusCode = refStatus;
182     }
183 
184 public:
185     VehiclePropValue actualValue;
186     StatusCode actualStatusCode;
187 
188     VehiclePropValuePool* objectPool;
189     std::unique_ptr<MockedVehicleHal> hal;
190     std::unique_ptr<VehicleHalManager> manager;
191 };
192 
TEST_F(VehicleHalManagerTest,getPropConfigs)193 TEST_F(VehicleHalManagerTest, getPropConfigs) {
194     hidl_vec<int32_t> properties =
195         { toInt(VehicleProperty::HVAC_FAN_SPEED),
196           toInt(VehicleProperty::INFO_MAKE) };
197     bool called = false;
198 
199     manager->getPropConfigs(properties,
200             [&called] (StatusCode status,
201                        const hidl_vec<VehiclePropConfig>& c) {
202         ASSERT_EQ(StatusCode::OK, status);
203         ASSERT_EQ(2u, c.size());
204         called = true;
205     });
206 
207     ASSERT_TRUE(called);  // Verify callback received.
208 
209     called = false;
210     manager->getPropConfigs({ toInt(VehicleProperty::HVAC_FAN_SPEED) },
211             [&called] (StatusCode status,
212                        const hidl_vec<VehiclePropConfig>& c) {
213         ASSERT_EQ(StatusCode::OK, status);
214         ASSERT_EQ(1u, c.size());
215         ASSERT_EQ(toString(kVehicleProperties[1]), toString(c[0]));
216         called = true;
217     });
218     ASSERT_TRUE(called);  // Verify callback received.
219 
220     // TODO(pavelm): add case case when property was not declared.
221 }
222 
TEST_F(VehicleHalManagerTest,getAllPropConfigs)223 TEST_F(VehicleHalManagerTest, getAllPropConfigs) {
224     bool called = false;
225     manager->getAllPropConfigs(
226             [&called] (const hidl_vec<VehiclePropConfig>& propConfigs) {
227         ASSERT_EQ(arraysize(kVehicleProperties), propConfigs.size());
228 
229         for (size_t i = 0; i < propConfigs.size(); i++) {
230             ASSERT_EQ(toString(kVehicleProperties[i]),
231                       toString(propConfigs[i]));
232         }
233         called = true;
234     });
235     ASSERT_TRUE(called);  // Verify callback received.
236 }
237 
TEST_F(VehicleHalManagerTest,halErrorEvent)238 TEST_F(VehicleHalManagerTest, halErrorEvent) {
239     const auto PROP = toInt(VehicleProperty::DISPLAY_BRIGHTNESS);
240 
241     sp<MockedVehicleCallback> cb = new MockedVehicleCallback();
242 
243     hidl_vec<SubscribeOptions> options = {
244         SubscribeOptions{.propId = PROP, .flags = SubscribeFlags::EVENTS_FROM_CAR},
245     };
246 
247     StatusCode res = manager->subscribe(cb, options);
248     ASSERT_EQ(StatusCode::OK, res);
249 
250     hal->sendHalError(StatusCode::TRY_AGAIN, PROP, 0 /* area id*/);
251 }
252 
TEST_F(VehicleHalManagerTest,subscribe)253 TEST_F(VehicleHalManagerTest, subscribe) {
254     const auto PROP = toInt(VehicleProperty::DISPLAY_BRIGHTNESS);
255 
256     sp<MockedVehicleCallback> cb = new MockedVehicleCallback();
257 
258     hidl_vec<SubscribeOptions> options = {
259         SubscribeOptions{.propId = PROP, .flags = SubscribeFlags::EVENTS_FROM_CAR}};
260 
261     StatusCode res = manager->subscribe(cb, options);
262     ASSERT_EQ(StatusCode::OK, res);
263 
264     auto unsubscribedValue = objectPool->obtain(VehiclePropertyType::INT32);
265     unsubscribedValue->prop = toInt(VehicleProperty::HVAC_FAN_SPEED);
266 
267     hal->sendPropEvent(std::move(unsubscribedValue));
268     auto& receivedEnvents = cb->getReceivedEvents();
269 
270     ASSERT_TRUE(cb->waitForExpectedEvents(0)) << " Unexpected events received: "
271                                               << receivedEnvents.size()
272                                               << (receivedEnvents.size() > 0
273                                                   ? toString(receivedEnvents.front()[0]) : "");
274 
275     auto subscribedValue = objectPool->obtain(VehiclePropertyType::INT32);
276     subscribedValue->prop = PROP;
277     subscribedValue->value.int32Values[0] = 42;
278 
279     cb->reset();
280     VehiclePropValue actualValue(*subscribedValue.get());
281     hal->sendPropEvent(std::move(subscribedValue));
282 
283     ASSERT_TRUE(cb->waitForExpectedEvents(1)) << "Events received: "
284                                               << receivedEnvents.size();
285 
286     ASSERT_EQ(toString(actualValue),
287               toString(cb->getReceivedEvents().front()[0]));
288 }
289 
TEST_F(VehicleHalManagerTest,subscribe_WriteOnly)290 TEST_F(VehicleHalManagerTest, subscribe_WriteOnly) {
291     const auto PROP = toInt(VehicleProperty::HVAC_SEAT_TEMPERATURE);
292 
293     sp<MockedVehicleCallback> cb = new MockedVehicleCallback();
294 
295     hidl_vec<SubscribeOptions> options = {
296         SubscribeOptions{.propId = PROP, .flags = SubscribeFlags::EVENTS_FROM_CAR},
297     };
298 
299     StatusCode res = manager->subscribe(cb, options);
300     // Unable to subscribe on Hal Events for write-only properties.
301     ASSERT_EQ(StatusCode::INVALID_ARG, res);
302 
303     options[0].flags = SubscribeFlags::EVENTS_FROM_ANDROID;
304 
305     res = manager->subscribe(cb, options);
306     // OK to subscribe on SET method call for write-only properties.
307     ASSERT_EQ(StatusCode::OK, res);
308 }
309 
TEST_F(VehicleHalManagerTest,get_Complex)310 TEST_F(VehicleHalManagerTest, get_Complex) {
311     invokeGet(kCustomComplexProperty, 0);
312 
313     ASSERT_EQ(StatusCode::OK, actualStatusCode);
314     ASSERT_EQ(kCustomComplexProperty, actualValue.prop);
315 
316     ASSERT_EQ(3u, actualValue.value.bytes.size());
317     ASSERT_EQ(1, actualValue.value.bytes[0]);
318     ASSERT_EQ(2, actualValue.value.bytes[1]);
319     ASSERT_EQ(3, actualValue.value.bytes[2]);
320 
321     ASSERT_EQ(2u, actualValue.value.int32Values.size());
322     ASSERT_EQ(10, actualValue.value.int32Values[0]);
323     ASSERT_EQ(20, actualValue.value.int32Values[1]);
324 
325     ASSERT_EQ(2u, actualValue.value.floatValues.size());
326     ASSERT_FLOAT_EQ(1.1, actualValue.value.floatValues[0]);
327     ASSERT_FLOAT_EQ(2.2, actualValue.value.floatValues[1]);
328 
329     ASSERT_EQ(2u, actualValue.value.int64Values.size());
330     ASSERT_FLOAT_EQ(30, actualValue.value.int64Values[0]);
331     ASSERT_FLOAT_EQ(40, actualValue.value.int64Values[1]);
332 
333     ASSERT_STREQ(kCarMake, actualValue.value.stringValue.c_str());
334 }
335 
TEST_F(VehicleHalManagerTest,get_StaticString)336 TEST_F(VehicleHalManagerTest, get_StaticString) {
337     invokeGet(toInt(VehicleProperty::INFO_MAKE), 0);
338 
339     ASSERT_EQ(StatusCode::OK, actualStatusCode);
340     ASSERT_EQ(toInt(VehicleProperty::INFO_MAKE), actualValue.prop);
341     ASSERT_STREQ(kCarMake, actualValue.value.stringValue.c_str());
342 }
343 
TEST_F(VehicleHalManagerTest,get_NegativeCases)344 TEST_F(VehicleHalManagerTest, get_NegativeCases) {
345     // Write-only property must fail.
346     invokeGet(toInt(VehicleProperty::HVAC_SEAT_TEMPERATURE), 0);
347     ASSERT_EQ(StatusCode::ACCESS_DENIED, actualStatusCode);
348 
349     // Unknown property must fail.
350     invokeGet(toInt(VehicleProperty::MIRROR_Z_MOVE), 0);
351     ASSERT_EQ(StatusCode::INVALID_ARG, actualStatusCode);
352 }
353 
TEST_F(VehicleHalManagerTest,get_Retriable)354 TEST_F(VehicleHalManagerTest, get_Retriable) {
355     actualStatusCode = StatusCode::TRY_AGAIN;
356     int attempts = 0;
357     while (StatusCode::TRY_AGAIN == actualStatusCode && ++attempts < 10) {
358         invokeGet(toInt(VehicleProperty::INFO_FUEL_CAPACITY), 0);
359 
360     }
361     ASSERT_EQ(StatusCode::OK, actualStatusCode);
362     ASSERT_EQ(kRetriablePropMockedAttempts + 1, attempts);
363     ASSERT_FLOAT_EQ(42.42, actualValue.value.floatValues[0]);
364 }
365 
TEST_F(VehicleHalManagerTest,set_Basic)366 TEST_F(VehicleHalManagerTest, set_Basic) {
367     const auto PROP = toInt(VehicleProperty::DISPLAY_BRIGHTNESS);
368     const auto VAL = 7;
369 
370     auto expectedValue = hal->getValuePool()->obtainInt32(VAL);
371     expectedValue->prop = PROP;
372     expectedValue->areaId = 0;
373 
374     actualStatusCode = manager->set(*expectedValue.get());
375     ASSERT_EQ(StatusCode::OK, actualStatusCode);
376 
377     invokeGet(PROP, 0);
378     ASSERT_EQ(StatusCode::OK, actualStatusCode);
379     ASSERT_EQ(PROP, actualValue.prop);
380     ASSERT_EQ(VAL, actualValue.value.int32Values[0]);
381 }
382 
TEST_F(VehicleHalManagerTest,set_DifferentAreas)383 TEST_F(VehicleHalManagerTest, set_DifferentAreas) {
384     const auto PROP = toInt(VehicleProperty::HVAC_FAN_SPEED);
385     const auto VAL1 = 1;
386     const auto VAL2 = 2;
387     const auto AREA1 = toInt(VehicleAreaSeat::ROW_1_LEFT);
388     const auto AREA2 = toInt(VehicleAreaSeat::ROW_1_RIGHT);
389 
390     {
391         auto expectedValue1 = hal->getValuePool()->obtainInt32(VAL1);
392         expectedValue1->prop = PROP;
393         expectedValue1->areaId = AREA1;
394         actualStatusCode = manager->set(*expectedValue1.get());
395         ASSERT_EQ(StatusCode::OK, actualStatusCode);
396 
397         auto expectedValue2 = hal->getValuePool()->obtainInt32(VAL2);
398         expectedValue2->prop = PROP;
399         expectedValue2->areaId = AREA2;
400         actualStatusCode = manager->set(*expectedValue2.get());
401         ASSERT_EQ(StatusCode::OK, actualStatusCode);
402     }
403 
404     {
405         invokeGet(PROP, AREA1);
406         ASSERT_EQ(StatusCode::OK, actualStatusCode);
407         ASSERT_EQ(PROP, actualValue.prop);
408         ASSERT_EQ(AREA1, actualValue.areaId);
409         ASSERT_EQ(VAL1, actualValue.value.int32Values[0]);
410 
411         invokeGet(PROP, AREA2);
412         ASSERT_EQ(StatusCode::OK, actualStatusCode);
413         ASSERT_EQ(PROP, actualValue.prop);
414         ASSERT_EQ(AREA2, actualValue.areaId);
415         ASSERT_EQ(VAL2, actualValue.value.int32Values[0]);
416     }
417 }
418 
TEST_F(VehicleHalManagerTest,set_Retriable)419 TEST_F(VehicleHalManagerTest, set_Retriable) {
420     const auto PROP = toInt(VehicleProperty::MIRROR_FOLD);
421 
422     auto v = hal->getValuePool()->obtainBoolean(true);
423     v->prop = PROP;
424     v->areaId = 0;
425 
426     actualStatusCode = StatusCode::TRY_AGAIN;
427     int attempts = 0;
428     while (StatusCode::TRY_AGAIN == actualStatusCode && ++attempts < 10) {
429         actualStatusCode = manager->set(*v.get());
430     }
431 
432     ASSERT_EQ(StatusCode::OK, actualStatusCode);
433     ASSERT_EQ(kRetriablePropMockedAttempts + 1, attempts);
434 
435     invokeGet(PROP, 0);
436     ASSERT_EQ(StatusCode::OK, actualStatusCode);
437     ASSERT_TRUE(actualValue.value.int32Values[0]);
438 }
439 
TEST(HalClientVectorTest,basic)440 TEST(HalClientVectorTest, basic) {
441     HalClientVector clients;
442     sp<IVehicleCallback> callback1 = new MockedVehicleCallback();
443 
444     sp<HalClient> c1 = new HalClient(callback1);
445     sp<HalClient> c2 = new HalClient(callback1);
446 
447     clients.addOrUpdate(c1);
448     clients.addOrUpdate(c1);
449     clients.addOrUpdate(c2);
450     ASSERT_EQ(2u, clients.size());
451     ASSERT_FALSE(clients.isEmpty());
452     ASSERT_LE(0, clients.indexOf(c1));
453     ASSERT_LE(0, clients.remove(c1));
454     ASSERT_GT(0, clients.indexOf(c1));  // c1 was already removed
455     ASSERT_GT(0, clients.remove(c1));   // attempt to remove c1 again
456     ASSERT_LE(0, clients.remove(c2));
457 
458     ASSERT_TRUE(clients.isEmpty());
459 }
460 
461 }  // namespace anonymous
462 
463 }  // namespace V2_0
464 }  // namespace vehicle
465 }  // namespace automotive
466 }  // namespace hardware
467 }  // namespace android
468