1 //
2 // Copyright (C) 2019 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 #include <gtest/gtest.h>
17
18 #include <android/hardware/sensors/1.0/types.h>
19 #include <android/hardware/sensors/2.0/types.h>
20 #include <android/hardware/sensors/2.1/types.h>
21 #include <fmq/MessageQueue.h>
22
23 #include "HalProxy.h"
24 #include "SensorsSubHal.h"
25 #include "V2_0/ScopedWakelock.h"
26 #include "convertV2_1.h"
27
28 #include <chrono>
29 #include <set>
30 #include <thread>
31 #include <vector>
32
33 namespace {
34
35 using ::android::hardware::EventFlag;
36 using ::android::hardware::hidl_vec;
37 using ::android::hardware::MessageQueue;
38 using ::android::hardware::Return;
39 using ::android::hardware::sensors::V1_0::EventPayload;
40 using ::android::hardware::sensors::V1_0::SensorFlagBits;
41 using ::android::hardware::sensors::V1_0::SensorInfo;
42 using ::android::hardware::sensors::V1_0::SensorType;
43 using ::android::hardware::sensors::V2_0::EventQueueFlagBits;
44 using ::android::hardware::sensors::V2_0::WakeLockQueueFlagBits;
45 using ::android::hardware::sensors::V2_0::implementation::HalProxyCallbackBase;
46 using ::android::hardware::sensors::V2_0::implementation::ScopedWakelock;
47 using ::android::hardware::sensors::V2_1::implementation::convertToNewEvents;
48 using ::android::hardware::sensors::V2_1::implementation::convertToNewSensorInfos;
49 using ::android::hardware::sensors::V2_1::implementation::HalProxy;
50 using ::android::hardware::sensors::V2_1::subhal::implementation::AddAndRemoveDynamicSensorsSubHal;
51 using ::android::hardware::sensors::V2_1::subhal::implementation::AllSensorsSubHal;
52 using ::android::hardware::sensors::V2_1::subhal::implementation::
53 AllSupportDirectChannelSensorsSubHal;
54 using ::android::hardware::sensors::V2_1::subhal::implementation::ContinuousSensorsSubHal;
55 using ::android::hardware::sensors::V2_1::subhal::implementation::
56 DoesNotSupportDirectChannelSensorsSubHal;
57 using ::android::hardware::sensors::V2_1::subhal::implementation::OnChangeSensorsSubHal;
58 using ::android::hardware::sensors::V2_1::subhal::implementation::SensorsSubHalV2_0;
59 using ::android::hardware::sensors::V2_1::subhal::implementation::SensorsSubHalV2_1;
60 using ::android::hardware::sensors::V2_1::subhal::implementation::
61 SetOperationModeFailingSensorsSubHal;
62
63 using ISensorsCallbackV2_0 = ::android::hardware::sensors::V2_0::ISensorsCallback;
64 using ISensorsCallbackV2_1 = ::android::hardware::sensors::V2_1::ISensorsCallback;
65 using EventV1_0 = ::android::hardware::sensors::V1_0::Event;
66 using EventV2_1 = ::android::hardware::sensors::V2_1::Event;
67 using EventMessageQueueV2_1 = MessageQueue<EventV2_1, ::android::hardware::kSynchronizedReadWrite>;
68 using EventMessageQueueV2_0 = MessageQueue<EventV1_0, ::android::hardware::kSynchronizedReadWrite>;
69 using WakeupMessageQueue = MessageQueue<uint32_t, ::android::hardware::kSynchronizedReadWrite>;
70
71 // The barebones sensors callback class passed into halproxy initialize calls
72 class SensorsCallback : public ISensorsCallbackV2_0 {
73 public:
onDynamicSensorsConnected(const hidl_vec<SensorInfo> &)74 Return<void> onDynamicSensorsConnected(
75 const hidl_vec<SensorInfo>& /*dynamicSensorsAdded*/) override {
76 // Nothing yet
77 return Return<void>();
78 }
79
onDynamicSensorsDisconnected(const hidl_vec<int32_t> &)80 Return<void> onDynamicSensorsDisconnected(
81 const hidl_vec<int32_t>& /*dynamicSensorHandlesRemoved*/) override {
82 // Nothing yet
83 return Return<void>();
84 }
85 };
86
87 class SensorsCallbackV2_1 : public ISensorsCallbackV2_1 {
88 public:
onDynamicSensorsConnected_2_1(const hidl_vec<::android::hardware::sensors::V2_1::SensorInfo> &)89 Return<void> onDynamicSensorsConnected_2_1(
90 const hidl_vec<::android::hardware::sensors::V2_1::SensorInfo>& /*dynamicSensorsAdded*/)
91 override {
92 // Nothing yet
93 return Return<void>();
94 }
95
onDynamicSensorsConnected(const hidl_vec<SensorInfo> &)96 Return<void> onDynamicSensorsConnected(
97 const hidl_vec<SensorInfo>& /*dynamicSensorsAdded*/) override {
98 // Nothing yet
99 return Return<void>();
100 }
101
onDynamicSensorsDisconnected(const hidl_vec<int32_t> &)102 Return<void> onDynamicSensorsDisconnected(
103 const hidl_vec<int32_t>& /*dynamicSensorHandlesRemoved*/) override {
104 // Nothing yet
105 return Return<void>();
106 }
107 };
108
109 // The sensors callback that expects a variable list of sensors to be added
110 class TestSensorsCallback : public ISensorsCallbackV2_0 {
111 public:
onDynamicSensorsConnected(const hidl_vec<SensorInfo> & dynamicSensorsAdded)112 Return<void> onDynamicSensorsConnected(
113 const hidl_vec<SensorInfo>& dynamicSensorsAdded) override {
114 mSensorsConnected.insert(mSensorsConnected.end(), dynamicSensorsAdded.begin(),
115 dynamicSensorsAdded.end());
116 return Return<void>();
117 }
118
onDynamicSensorsDisconnected(const hidl_vec<int32_t> & dynamicSensorHandlesRemoved)119 Return<void> onDynamicSensorsDisconnected(
120 const hidl_vec<int32_t>& dynamicSensorHandlesRemoved) override {
121 mSensorHandlesDisconnected.insert(mSensorHandlesDisconnected.end(),
122 dynamicSensorHandlesRemoved.begin(),
123 dynamicSensorHandlesRemoved.end());
124 return Return<void>();
125 }
126
getSensorsConnected() const127 const std::vector<SensorInfo>& getSensorsConnected() const { return mSensorsConnected; }
getSensorHandlesDisconnected() const128 const std::vector<int32_t>& getSensorHandlesDisconnected() const {
129 return mSensorHandlesDisconnected;
130 }
131
132 private:
133 std::vector<SensorInfo> mSensorsConnected;
134 std::vector<int32_t> mSensorHandlesDisconnected;
135 };
136
137 // Helper declarations follow
138
139 /**
140 * Tests that for each SensorInfo object from a proxy getSensorsList call each corresponding
141 * object from a subhal getSensorsList call has the same type and its last 3 bytes are the
142 * same for sensorHandle field.
143 *
144 * @param proxySensorsList The list of SensorInfo objects from the proxy.getSensorsList callback.
145 * @param subHalSenosrsList The list of SensorInfo objects from the subHal.getSensorsList callback.
146 */
147 void testSensorsListFromProxyAndSubHal(const std::vector<SensorInfo>& proxySensorsList,
148 const std::vector<SensorInfo>& subHalSensorsList);
149
150 /**
151 * Tests that there is exactly one subhal that allows its sensors to have direct channel enabled.
152 * Therefore, all SensorInfo objects that are not from the enabled subhal should be disabled for
153 * direct channel.
154 *
155 * @param sensorsList The SensorInfo object list from proxy.getSensorsList call.
156 * @param enabledSubHalIndex The index of the subhal in the halproxy that is expected to be
157 * enabled.
158 */
159 void testSensorsListForOneDirectChannelEnabledSubHal(const std::vector<SensorInfo>& sensorsList,
160 size_t enabledSubHalIndex);
161
162 void ackWakeupEventsToHalProxy(size_t numEvents, std::unique_ptr<WakeupMessageQueue>& wakelockQueue,
163 EventFlag* wakelockQueueFlag);
164
165 bool readEventsOutOfQueue(size_t numEvents, std::unique_ptr<EventMessageQueueV2_0>& eventQueue,
166 EventFlag* eventQueueFlag);
167
168 std::unique_ptr<EventMessageQueueV2_0> makeEventFMQ(size_t size);
169
170 std::unique_ptr<WakeupMessageQueue> makeWakelockFMQ(size_t size);
171
172 /**
173 * Construct and return a HIDL Event type thats sensorHandle refers to a proximity sensor
174 * which is a wakeup type sensor.
175 *
176 * @return A proximity event.
177 */
178 EventV1_0 makeProximityEvent();
179
180 /**
181 * Construct and return a HIDL Event type thats sensorHandle refers to a proximity sensor
182 * which is a wakeup type sensor.
183 *
184 * @return A proximity event.
185 */
186 EventV1_0 makeAccelerometerEvent();
187
188 /**
189 * Make a certain number of proximity type events with the sensorHandle field set to
190 * the proper number for AllSensorsSubHal subhal type.
191 *
192 * @param numEvents The number of events to make.
193 *
194 * @return The created list of events.
195 */
196 std::vector<EventV1_0> makeMultipleProximityEvents(size_t numEvents);
197
198 /**
199 * Make a certain number of accelerometer type events with the sensorHandle field set to
200 * the proper number for AllSensorsSubHal subhal type.
201 *
202 * @param numEvents The number of events to make.
203 *
204 * @return The created list of events.
205 */
206 std::vector<EventV1_0> makeMultipleAccelerometerEvents(size_t numEvents);
207
208 /**
209 * Given a SensorInfo vector and a sensor handles vector populate 'sensors' with SensorInfo
210 * objects that have the sensorHandle property set to int32_ts from start to start + size
211 * (exclusive) and push those sensorHandles also onto 'sensorHandles'.
212 *
213 * @param start The starting sensorHandle value.
214 * @param size The ending (not included) sensorHandle value.
215 * @param sensors The SensorInfo object vector reference to push_back to.
216 * @param sensorHandles The sensor handles int32_t vector reference to push_back to.
217 */
218 void makeSensorsAndSensorHandlesStartingAndOfSize(int32_t start, size_t size,
219 std::vector<SensorInfo>& sensors,
220 std::vector<int32_t>& sensorHandles);
221
222 // Tests follow
TEST(HalProxyTest,GetSensorsListOneSubHalTest)223 TEST(HalProxyTest, GetSensorsListOneSubHalTest) {
224 AllSensorsSubHal<SensorsSubHalV2_0> subHal;
225 std::vector<ISensorsSubHal*> fakeSubHals{&subHal};
226 HalProxy proxy(fakeSubHals);
227
228 proxy.getSensorsList([&](const auto& proxySensorsList) {
229 subHal.getSensorsList([&](const auto& subHalSensorsList) {
230 testSensorsListFromProxyAndSubHal(proxySensorsList, subHalSensorsList);
231 });
232 });
233 }
234
TEST(HalProxyTest,GetSensorsListTwoSubHalTest)235 TEST(HalProxyTest, GetSensorsListTwoSubHalTest) {
236 ContinuousSensorsSubHal<SensorsSubHalV2_0> continuousSubHal;
237 OnChangeSensorsSubHal<SensorsSubHalV2_0> onChangeSubHal;
238 std::vector<ISensorsSubHal*> fakeSubHals;
239 fakeSubHals.push_back(&continuousSubHal);
240 fakeSubHals.push_back(&onChangeSubHal);
241 HalProxy proxy(fakeSubHals);
242
243 std::vector<SensorInfo> proxySensorsList, combinedSubHalSensorsList;
244
245 proxy.getSensorsList([&](const auto& list) { proxySensorsList = list; });
246 continuousSubHal.getSensorsList([&](const auto& list) {
247 combinedSubHalSensorsList.insert(combinedSubHalSensorsList.end(), list.begin(), list.end());
248 });
249 onChangeSubHal.getSensorsList([&](const auto& list) {
250 combinedSubHalSensorsList.insert(combinedSubHalSensorsList.end(), list.begin(), list.end());
251 });
252
253 testSensorsListFromProxyAndSubHal(proxySensorsList, combinedSubHalSensorsList);
254 }
255
TEST(HalProxyTest,SetOperationModeTwoSubHalSuccessTest)256 TEST(HalProxyTest, SetOperationModeTwoSubHalSuccessTest) {
257 ContinuousSensorsSubHal<SensorsSubHalV2_0> subHal1;
258 OnChangeSensorsSubHal<SensorsSubHalV2_0> subHal2;
259
260 std::vector<ISensorsSubHal*> fakeSubHals{&subHal1, &subHal2};
261 HalProxy proxy(fakeSubHals);
262
263 EXPECT_EQ(subHal1.getOperationMode(), OperationMode::NORMAL);
264 EXPECT_EQ(subHal2.getOperationMode(), OperationMode::NORMAL);
265
266 Result result = proxy.setOperationMode(OperationMode::DATA_INJECTION);
267
268 EXPECT_EQ(result, Result::OK);
269 EXPECT_EQ(subHal1.getOperationMode(), OperationMode::DATA_INJECTION);
270 EXPECT_EQ(subHal2.getOperationMode(), OperationMode::DATA_INJECTION);
271 }
272
TEST(HalProxyTest,SetOperationModeTwoSubHalFailTest)273 TEST(HalProxyTest, SetOperationModeTwoSubHalFailTest) {
274 AllSensorsSubHal<SensorsSubHalV2_0> subHal1;
275 SetOperationModeFailingSensorsSubHal subHal2;
276
277 std::vector<ISensorsSubHal*> fakeSubHals{&subHal1, &subHal2};
278 HalProxy proxy(fakeSubHals);
279
280 EXPECT_EQ(subHal1.getOperationMode(), OperationMode::NORMAL);
281 EXPECT_EQ(subHal2.getOperationMode(), OperationMode::NORMAL);
282
283 Result result = proxy.setOperationMode(OperationMode::DATA_INJECTION);
284
285 EXPECT_NE(result, Result::OK);
286 EXPECT_EQ(subHal1.getOperationMode(), OperationMode::NORMAL);
287 EXPECT_EQ(subHal2.getOperationMode(), OperationMode::NORMAL);
288 }
289
TEST(HalProxyTest,InitDirectChannelTwoSubHalsUnitTest)290 TEST(HalProxyTest, InitDirectChannelTwoSubHalsUnitTest) {
291 AllSupportDirectChannelSensorsSubHal subHal1;
292 AllSupportDirectChannelSensorsSubHal subHal2;
293
294 std::vector<ISensorsSubHal*> fakeSubHals{&subHal1, &subHal2};
295 HalProxy proxy(fakeSubHals);
296
297 proxy.getSensorsList([&](const auto& sensorsList) {
298 testSensorsListForOneDirectChannelEnabledSubHal(sensorsList, 0);
299 });
300 }
301
TEST(HalProxyTest,InitDirectChannelThreeSubHalsUnitTest)302 TEST(HalProxyTest, InitDirectChannelThreeSubHalsUnitTest) {
303 DoesNotSupportDirectChannelSensorsSubHal subHal1;
304 AllSupportDirectChannelSensorsSubHal subHal2, subHal3;
305 std::vector<ISensorsSubHal*> fakeSubHals{&subHal1, &subHal2, &subHal3};
306 HalProxy proxy(fakeSubHals);
307
308 proxy.getSensorsList([&](const auto& sensorsList) {
309 testSensorsListForOneDirectChannelEnabledSubHal(sensorsList, 1);
310 });
311 }
312
TEST(HalProxyTest,PostSingleNonWakeupEvent)313 TEST(HalProxyTest, PostSingleNonWakeupEvent) {
314 constexpr size_t kQueueSize = 5;
315 AllSensorsSubHal<SensorsSubHalV2_0> subHal;
316 std::vector<ISensorsSubHal*> subHals{&subHal};
317 HalProxy proxy(subHals);
318 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
319 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
320 ::android::sp<ISensorsCallbackV2_0> callback = new SensorsCallback();
321 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
322
323 std::vector<EventV1_0> events{makeAccelerometerEvent()};
324 subHal.postEvents(convertToNewEvents(events), false /* wakeup */);
325
326 EXPECT_EQ(eventQueue->availableToRead(), 1);
327 }
328
TEST(HalProxyTest,PostMultipleNonWakeupEvent)329 TEST(HalProxyTest, PostMultipleNonWakeupEvent) {
330 constexpr size_t kQueueSize = 5;
331 constexpr size_t kNumEvents = 3;
332 AllSensorsSubHal<SensorsSubHalV2_0> subHal;
333 std::vector<ISensorsSubHal*> subHals{&subHal};
334 HalProxy proxy(subHals);
335 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
336 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
337 ::android::sp<ISensorsCallbackV2_0> callback = new SensorsCallback();
338 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
339
340 std::vector<EventV1_0> events = makeMultipleAccelerometerEvents(kNumEvents);
341 subHal.postEvents(convertToNewEvents(events), false /* wakeup */);
342
343 EXPECT_EQ(eventQueue->availableToRead(), kNumEvents);
344 }
345
TEST(HalProxyTest,PostSingleWakeupEvent)346 TEST(HalProxyTest, PostSingleWakeupEvent) {
347 constexpr size_t kQueueSize = 5;
348 AllSensorsSubHal<SensorsSubHalV2_0> subHal;
349 std::vector<ISensorsSubHal*> subHals{&subHal};
350 HalProxy proxy(subHals);
351 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
352 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
353 ::android::sp<ISensorsCallbackV2_0> callback = new SensorsCallback();
354 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
355
356 EventFlag* eventQueueFlag;
357 EventFlag::createEventFlag(eventQueue->getEventFlagWord(), &eventQueueFlag);
358
359 EventFlag* wakelockQueueFlag;
360 EventFlag::createEventFlag(wakeLockQueue->getEventFlagWord(), &wakelockQueueFlag);
361
362 std::vector<EventV1_0> events{makeProximityEvent()};
363 subHal.postEvents(convertToNewEvents(events), true /* wakeup */);
364
365 EXPECT_EQ(eventQueue->availableToRead(), 1);
366
367 readEventsOutOfQueue(1, eventQueue, eventQueueFlag);
368 ackWakeupEventsToHalProxy(1, wakeLockQueue, wakelockQueueFlag);
369 }
370
TEST(HalProxyTest,PostMultipleWakeupEvents)371 TEST(HalProxyTest, PostMultipleWakeupEvents) {
372 constexpr size_t kQueueSize = 5;
373 constexpr size_t kNumEvents = 3;
374 AllSensorsSubHal<SensorsSubHalV2_0> subHal;
375 std::vector<ISensorsSubHal*> subHals{&subHal};
376 HalProxy proxy(subHals);
377 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
378 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
379 ::android::sp<ISensorsCallbackV2_0> callback = new SensorsCallback();
380 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
381
382 EventFlag* eventQueueFlag;
383 EventFlag::createEventFlag(eventQueue->getEventFlagWord(), &eventQueueFlag);
384
385 EventFlag* wakelockQueueFlag;
386 EventFlag::createEventFlag(wakeLockQueue->getEventFlagWord(), &wakelockQueueFlag);
387
388 std::vector<EventV1_0> events = makeMultipleProximityEvents(kNumEvents);
389 subHal.postEvents(convertToNewEvents(events), true /* wakeup */);
390
391 EXPECT_EQ(eventQueue->availableToRead(), kNumEvents);
392
393 readEventsOutOfQueue(kNumEvents, eventQueue, eventQueueFlag);
394 ackWakeupEventsToHalProxy(kNumEvents, wakeLockQueue, wakelockQueueFlag);
395 }
396
TEST(HalProxyTest,PostEventsMultipleSubhals)397 TEST(HalProxyTest, PostEventsMultipleSubhals) {
398 constexpr size_t kQueueSize = 5;
399 constexpr size_t kNumEvents = 2;
400 AllSensorsSubHal<SensorsSubHalV2_0> subHal1, subHal2;
401 std::vector<ISensorsSubHal*> subHals{&subHal1, &subHal2};
402 HalProxy proxy(subHals);
403 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
404 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
405 ::android::sp<ISensorsCallbackV2_0> callback = new SensorsCallback();
406 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
407
408 std::vector<EventV1_0> events = makeMultipleAccelerometerEvents(kNumEvents);
409 subHal1.postEvents(convertToNewEvents(events), false /* wakeup */);
410
411 EXPECT_EQ(eventQueue->availableToRead(), kNumEvents);
412
413 subHal2.postEvents(convertToNewEvents(events), false /* wakeup */);
414
415 EXPECT_EQ(eventQueue->availableToRead(), kNumEvents * 2);
416 }
417
TEST(HalProxyTest,PostEventsDelayedWrite)418 TEST(HalProxyTest, PostEventsDelayedWrite) {
419 constexpr size_t kQueueSize = 5;
420 constexpr size_t kNumEvents = 6;
421 AllSensorsSubHal<SensorsSubHalV2_0> subHal1, subHal2;
422 std::vector<ISensorsSubHal*> subHals{&subHal1, &subHal2};
423 HalProxy proxy(subHals);
424 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
425 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
426 ::android::sp<ISensorsCallbackV2_0> callback = new SensorsCallback();
427 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
428
429 EventFlag* eventQueueFlag;
430 EventFlag::createEventFlag(eventQueue->getEventFlagWord(), &eventQueueFlag);
431
432 std::vector<EventV1_0> events = makeMultipleAccelerometerEvents(kNumEvents);
433 subHal1.postEvents(convertToNewEvents(events), false /* wakeup */);
434
435 EXPECT_EQ(eventQueue->availableToRead(), kQueueSize);
436
437 // readblock a full queue size worth of events out of queue, timeout for half a second
438 EXPECT_TRUE(readEventsOutOfQueue(kQueueSize, eventQueue, eventQueueFlag));
439
440 // proxy background thread should have wrote remaining events when it saw space
441 EXPECT_TRUE(readEventsOutOfQueue(kNumEvents - kQueueSize, eventQueue, eventQueueFlag));
442
443 EXPECT_EQ(eventQueue->availableToRead(), 0);
444 }
445
TEST(HalProxyTest,PostEventsMultipleSubhalsThreaded)446 TEST(HalProxyTest, PostEventsMultipleSubhalsThreaded) {
447 constexpr size_t kQueueSize = 5;
448 constexpr size_t kNumEvents = 2;
449 AllSensorsSubHal<SensorsSubHalV2_0> subHal1, subHal2;
450 std::vector<ISensorsSubHal*> subHals{&subHal1, &subHal2};
451 HalProxy proxy(subHals);
452 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
453 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
454 ::android::sp<ISensorsCallbackV2_0> callback = new SensorsCallback();
455 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
456
457 std::vector<EventV1_0> events = makeMultipleAccelerometerEvents(kNumEvents);
458
459 std::thread t1(&AllSensorsSubHal<SensorsSubHalV2_0>::postEvents, &subHal1,
460 convertToNewEvents(events), false);
461 std::thread t2(&AllSensorsSubHal<SensorsSubHalV2_0>::postEvents, &subHal2,
462 convertToNewEvents(events), false);
463
464 t1.join();
465 t2.join();
466
467 EXPECT_EQ(eventQueue->availableToRead(), kNumEvents * 2);
468 }
469
TEST(HalProxyTest,DestructingWithEventsPendingOnBackgroundThread)470 TEST(HalProxyTest, DestructingWithEventsPendingOnBackgroundThread) {
471 constexpr size_t kQueueSize = 5;
472 constexpr size_t kNumEvents = 6;
473 AllSensorsSubHal<SensorsSubHalV2_0> subHal;
474 std::vector<ISensorsSubHal*> subHals{&subHal};
475
476 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
477 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
478 ::android::sp<ISensorsCallbackV2_0> callback = new SensorsCallback();
479 HalProxy proxy(subHals);
480 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
481
482 std::vector<EventV1_0> events = makeMultipleAccelerometerEvents(kNumEvents);
483 subHal.postEvents(convertToNewEvents(events), false /* wakeup */);
484
485 // Destructing HalProxy object with events on the background thread
486 }
487
TEST(HalProxyTest,DestructingWithUnackedWakeupEventsPosted)488 TEST(HalProxyTest, DestructingWithUnackedWakeupEventsPosted) {
489 constexpr size_t kQueueSize = 5;
490 AllSensorsSubHal<SensorsSubHalV2_0> subHal;
491 std::vector<ISensorsSubHal*> subHals{&subHal};
492
493 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
494 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
495 ::android::sp<ISensorsCallbackV2_0> callback = new SensorsCallback();
496 HalProxy proxy(subHals);
497 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
498
499 std::vector<EventV1_0> events{makeProximityEvent()};
500 subHal.postEvents(convertToNewEvents(events), true /* wakeup */);
501
502 // Not sending any acks back through wakeLockQueue
503
504 // Destructing HalProxy object with unacked wakeup events posted
505 }
506
TEST(HalProxyTest,ReinitializeWithEventsPendingOnBackgroundThread)507 TEST(HalProxyTest, ReinitializeWithEventsPendingOnBackgroundThread) {
508 constexpr size_t kQueueSize = 5;
509 constexpr size_t kNumEvents = 10;
510 AllSensorsSubHal<SensorsSubHalV2_0> subHal;
511 std::vector<ISensorsSubHal*> subHals{&subHal};
512
513 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
514 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
515 ::android::sp<ISensorsCallbackV2_0> callback = new SensorsCallback();
516 HalProxy proxy(subHals);
517 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
518
519 std::vector<EventV1_0> events = makeMultipleAccelerometerEvents(kNumEvents);
520 subHal.postEvents(convertToNewEvents(events), false /* wakeup */);
521
522 eventQueue = makeEventFMQ(kQueueSize);
523 wakeLockQueue = makeWakelockFMQ(kQueueSize);
524
525 Result secondInitResult =
526 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
527 EXPECT_EQ(secondInitResult, Result::OK);
528 // Small sleep so that pending writes thread has a change to hit writeBlocking call.
529 std::this_thread::sleep_for(std::chrono::milliseconds(5));
530 EventV1_0 eventOut;
531 EXPECT_FALSE(eventQueue->read(&eventOut));
532 }
533
TEST(HalProxyTest,ReinitializingWithUnackedWakeupEventsPosted)534 TEST(HalProxyTest, ReinitializingWithUnackedWakeupEventsPosted) {
535 constexpr size_t kQueueSize = 5;
536 AllSensorsSubHal<SensorsSubHalV2_0> subHal;
537 std::vector<ISensorsSubHal*> subHals{&subHal};
538
539 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
540 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
541 ::android::sp<ISensorsCallbackV2_0> callback = new SensorsCallback();
542 HalProxy proxy(subHals);
543 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
544
545 std::vector<EventV1_0> events{makeProximityEvent()};
546 subHal.postEvents(convertToNewEvents(events), true /* wakeup */);
547
548 // Not sending any acks back through wakeLockQueue
549
550 eventQueue = makeEventFMQ(kQueueSize);
551 wakeLockQueue = makeWakelockFMQ(kQueueSize);
552
553 Result secondInitResult =
554 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
555 EXPECT_EQ(secondInitResult, Result::OK);
556 }
557
TEST(HalProxyTest,InitializeManyTimesInARow)558 TEST(HalProxyTest, InitializeManyTimesInARow) {
559 constexpr size_t kQueueSize = 5;
560 constexpr size_t kNumTimesToInit = 100;
561 AllSensorsSubHal<SensorsSubHalV2_0> subHal;
562 std::vector<ISensorsSubHal*> subHals{&subHal};
563
564 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
565 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
566 ::android::sp<ISensorsCallbackV2_0> callback = new SensorsCallback();
567 HalProxy proxy(subHals);
568
569 for (size_t i = 0; i < kNumTimesToInit; i++) {
570 Result secondInitResult =
571 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
572 EXPECT_EQ(secondInitResult, Result::OK);
573 }
574 }
575
TEST(HalProxyTest,OperationModeResetOnInitialize)576 TEST(HalProxyTest, OperationModeResetOnInitialize) {
577 constexpr size_t kQueueSize = 5;
578 AllSensorsSubHal<SensorsSubHalV2_0> subHal;
579 std::vector<ISensorsSubHal*> subHals{&subHal};
580 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
581 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
582 ::android::sp<ISensorsCallbackV2_0> callback = new SensorsCallback();
583 HalProxy proxy(subHals);
584 proxy.setOperationMode(OperationMode::DATA_INJECTION);
585 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
586 EventV1_0 event = makeAccelerometerEvent();
587 // Should not be able to inject a non AdditionInfo type event because operation mode should
588 // have been reset to NORMAL
589 EXPECT_EQ(proxy.injectSensorData(event), Result::BAD_VALUE);
590 }
591
TEST(HalProxyTest,DynamicSensorsDiscardedOnInitialize)592 TEST(HalProxyTest, DynamicSensorsDiscardedOnInitialize) {
593 constexpr size_t kQueueSize = 5;
594 constexpr size_t kNumSensors = 5;
595 AddAndRemoveDynamicSensorsSubHal subHal;
596 std::vector<ISensorsSubHal*> subHals{&subHal};
597 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
598 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
599 HalProxy proxy(subHals);
600
601 std::vector<SensorInfo> sensorsToConnect;
602 std::vector<int32_t> sensorHandlesToAttemptToRemove;
603 makeSensorsAndSensorHandlesStartingAndOfSize(1, kNumSensors, sensorsToConnect,
604 sensorHandlesToAttemptToRemove);
605
606 std::vector<int32_t> nonDynamicSensorHandles;
607 for (int32_t sensorHandle = 1; sensorHandle < 10; sensorHandle++) {
608 nonDynamicSensorHandles.push_back(sensorHandle);
609 }
610
611 TestSensorsCallback* callback = new TestSensorsCallback();
612 ::android::sp<ISensorsCallbackV2_0> callbackPtr = callback;
613 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callbackPtr);
614 subHal.addDynamicSensors(convertToNewSensorInfos(sensorsToConnect));
615
616 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callbackPtr);
617 subHal.removeDynamicSensors(sensorHandlesToAttemptToRemove);
618
619 std::vector<int32_t> sensorHandlesActuallyRemoved = callback->getSensorHandlesDisconnected();
620
621 // Should not have received the sensorHandles for any dynamic sensors that were removed since
622 // all of them should have been removed in the second initialize call.
623 EXPECT_TRUE(sensorHandlesActuallyRemoved.empty());
624 }
625
TEST(HalProxyTest,DynamicSensorsConnectedTest)626 TEST(HalProxyTest, DynamicSensorsConnectedTest) {
627 constexpr size_t kNumSensors = 3;
628 AddAndRemoveDynamicSensorsSubHal subHal;
629 std::vector<ISensorsSubHal*> subHals{&subHal};
630 HalProxy proxy(subHals);
631 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(0);
632 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(0);
633
634 std::vector<SensorInfo> sensorsToConnect;
635 std::vector<int32_t> sensorHandlesToExpect;
636 makeSensorsAndSensorHandlesStartingAndOfSize(1, kNumSensors, sensorsToConnect,
637 sensorHandlesToExpect);
638
639 TestSensorsCallback* callback = new TestSensorsCallback();
640 ::android::sp<ISensorsCallbackV2_0> callbackPtr = callback;
641 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callbackPtr);
642 subHal.addDynamicSensors(convertToNewSensorInfos(sensorsToConnect));
643
644 std::vector<SensorInfo> sensorsSeen = callback->getSensorsConnected();
645 EXPECT_EQ(kNumSensors, sensorsSeen.size());
646 for (size_t i = 0; i < kNumSensors; i++) {
647 auto sensorHandleSeen = sensorsSeen[i].sensorHandle;
648 // Note since only one subhal we do not need to change first byte for expected
649 auto sensorHandleExpected = sensorHandlesToExpect[i];
650 EXPECT_EQ(sensorHandleSeen, sensorHandleExpected);
651 }
652 }
653
TEST(HalProxyTest,DynamicSensorsDisconnectedTest)654 TEST(HalProxyTest, DynamicSensorsDisconnectedTest) {
655 constexpr size_t kNumSensors = 3;
656 AddAndRemoveDynamicSensorsSubHal subHal;
657 std::vector<ISensorsSubHal*> subHals{&subHal};
658 HalProxy proxy(subHals);
659 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(0);
660 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(0);
661
662 std::vector<SensorInfo> sensorsToConnect;
663 std::vector<int32_t> sensorHandlesToExpect;
664 makeSensorsAndSensorHandlesStartingAndOfSize(20, kNumSensors, sensorsToConnect,
665 sensorHandlesToExpect);
666
667 std::vector<int32_t> nonDynamicSensorHandles;
668 for (int32_t sensorHandle = 1; sensorHandle < 10; sensorHandle++) {
669 nonDynamicSensorHandles.push_back(sensorHandle);
670 }
671
672 std::set<int32_t> nonDynamicSensorHandlesSet(nonDynamicSensorHandles.begin(),
673 nonDynamicSensorHandles.end());
674
675 std::vector<int32_t> sensorHandlesToAttemptToRemove;
676 sensorHandlesToAttemptToRemove.insert(sensorHandlesToAttemptToRemove.end(),
677 sensorHandlesToExpect.begin(),
678 sensorHandlesToExpect.end());
679 sensorHandlesToAttemptToRemove.insert(sensorHandlesToAttemptToRemove.end(),
680 nonDynamicSensorHandles.begin(),
681 nonDynamicSensorHandles.end());
682
683 TestSensorsCallback* callback = new TestSensorsCallback();
684 ::android::sp<ISensorsCallbackV2_0> callbackPtr = callback;
685 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callbackPtr);
686 subHal.addDynamicSensors(convertToNewSensorInfos(sensorsToConnect));
687 subHal.removeDynamicSensors(sensorHandlesToAttemptToRemove);
688
689 std::vector<int32_t> sensorHandlesSeen = callback->getSensorHandlesDisconnected();
690 EXPECT_EQ(kNumSensors, sensorHandlesSeen.size());
691 for (size_t i = 0; i < kNumSensors; i++) {
692 auto sensorHandleSeen = sensorHandlesSeen[i];
693 // Note since only one subhal we do not need to change first byte for expected
694 auto sensorHandleExpected = sensorHandlesToExpect[i];
695 EXPECT_EQ(sensorHandleSeen, sensorHandleExpected);
696 EXPECT_TRUE(nonDynamicSensorHandlesSet.find(sensorHandleSeen) ==
697 nonDynamicSensorHandlesSet.end());
698 }
699 }
700
TEST(HalProxyTest,InvalidSensorHandleSubHalIndexProxyCalls)701 TEST(HalProxyTest, InvalidSensorHandleSubHalIndexProxyCalls) {
702 constexpr size_t kNumSubHals = 3;
703 constexpr size_t kQueueSize = 5;
704 int32_t kNumSubHalsInt32 = static_cast<int32_t>(kNumSubHals);
705 std::vector<AllSensorsSubHal<SensorsSubHalV2_0>> subHalObjs(kNumSubHals);
706 std::vector<ISensorsSubHal*> subHals;
707 for (const auto& subHal : subHalObjs) {
708 subHals.push_back((ISensorsSubHal*)(&subHal));
709 }
710
711 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
712 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
713 ::android::sp<ISensorsCallbackV2_0> callback = new SensorsCallback();
714 HalProxy proxy(subHals);
715 // Initialize for the injectSensorData call so callback postEvents is valid
716 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
717
718 // For testing proxy.injectSensorData properly
719 proxy.setOperationMode(OperationMode::DATA_INJECTION);
720
721 // kNumSubHalsInt32 index is one off the end of mSubHalList in proxy object
722 EXPECT_EQ(proxy.activate(0x00000001 | (kNumSubHalsInt32 << 24), true), Result::BAD_VALUE);
723 EXPECT_EQ(proxy.batch(0x00000001 | (kNumSubHalsInt32 << 24), 0, 0), Result::BAD_VALUE);
724 EXPECT_EQ(proxy.flush(0x00000001 | (kNumSubHalsInt32 << 24)), Result::BAD_VALUE);
725 EventV1_0 event;
726 event.sensorHandle = 0x00000001 | (kNumSubHalsInt32 << 24);
727 EXPECT_EQ(proxy.injectSensorData(event), Result::BAD_VALUE);
728 }
729
TEST(HalProxyTest,PostedEventSensorHandleSubHalIndexValid)730 TEST(HalProxyTest, PostedEventSensorHandleSubHalIndexValid) {
731 constexpr size_t kQueueSize = 5;
732 constexpr int32_t subhal1Index = 0;
733 constexpr int32_t subhal2Index = 1;
734 AllSensorsSubHal<SensorsSubHalV2_0> subhal1;
735 AllSensorsSubHal<SensorsSubHalV2_0> subhal2;
736 std::vector<ISensorsSubHal*> subHals{&subhal1, &subhal2};
737
738 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
739 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
740 ::android::sp<ISensorsCallbackV2_0> callback = new SensorsCallback();
741 HalProxy proxy(subHals);
742 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
743
744 int32_t sensorHandleToPost = 0x00000001;
745 EventV1_0 eventIn = makeAccelerometerEvent();
746 eventIn.sensorHandle = sensorHandleToPost;
747 std::vector<EventV1_0> eventsToPost{eventIn};
748 subhal1.postEvents(convertToNewEvents(eventsToPost), false);
749
750 EventV1_0 eventOut;
751 EXPECT_TRUE(eventQueue->read(&eventOut));
752
753 EXPECT_EQ(eventOut.sensorHandle, (subhal1Index << 24) | sensorHandleToPost);
754
755 subhal2.postEvents(convertToNewEvents(eventsToPost), false);
756
757 EXPECT_TRUE(eventQueue->read(&eventOut));
758
759 EXPECT_EQ(eventOut.sensorHandle, (subhal2Index << 24) | sensorHandleToPost);
760 }
761
TEST(HalProxyTest,FillAndDrainPendingQueueTest)762 TEST(HalProxyTest, FillAndDrainPendingQueueTest) {
763 constexpr size_t kQueueSize = 5;
764 // TODO: Make this constant linked to same limit in HalProxy.h
765 constexpr size_t kMaxPendingQueueSize = 100000;
766 AllSensorsSubHal<SensorsSubHalV2_0> subhal;
767 std::vector<ISensorsSubHal*> subHals{&subhal};
768
769 std::unique_ptr<EventMessageQueueV2_0> eventQueue = makeEventFMQ(kQueueSize);
770 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
771 ::android::sp<ISensorsCallbackV2_0> callback = new SensorsCallback();
772 EventFlag* eventQueueFlag;
773 EventFlag::createEventFlag(eventQueue->getEventFlagWord(), &eventQueueFlag);
774 HalProxy proxy(subHals);
775 proxy.initialize(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
776
777 // Fill pending queue
778 std::vector<EventV1_0> events = makeMultipleAccelerometerEvents(kQueueSize);
779 subhal.postEvents(convertToNewEvents(events), false);
780 events = makeMultipleAccelerometerEvents(kMaxPendingQueueSize);
781 subhal.postEvents(convertToNewEvents(events), false);
782
783 // Drain pending queue
784 for (int i = 0; i < kMaxPendingQueueSize + kQueueSize; i += kQueueSize) {
785 ASSERT_TRUE(readEventsOutOfQueue(kQueueSize, eventQueue, eventQueueFlag));
786 }
787
788 // Put one event on pending queue
789 events = makeMultipleAccelerometerEvents(kQueueSize);
790 subhal.postEvents(convertToNewEvents(events), false);
791 events = {makeAccelerometerEvent()};
792 subhal.postEvents(convertToNewEvents(events), false);
793
794 // Read out to make room for one event on pending queue to write to FMQ
795 ASSERT_TRUE(readEventsOutOfQueue(kQueueSize, eventQueue, eventQueueFlag));
796
797 // Should be able to read that last event off queue
798 EXPECT_TRUE(readEventsOutOfQueue(1, eventQueue, eventQueueFlag));
799 }
800
TEST(HalProxyTest,PostEventsMultipleSubhalsThreadedV2_1)801 TEST(HalProxyTest, PostEventsMultipleSubhalsThreadedV2_1) {
802 constexpr size_t kQueueSize = 5;
803 constexpr size_t kNumEvents = 2;
804 AllSensorsSubHal<SensorsSubHalV2_0> subHal1;
805 AllSensorsSubHal<SensorsSubHalV2_1> subHal2;
806 std::vector<::android::hardware::sensors::V2_0::implementation::ISensorsSubHal*> subHalsV2_0{
807 &subHal1};
808 std::vector<::android::hardware::sensors::V2_1::implementation::ISensorsSubHal*> subHalsV2_1{
809 &subHal2};
810 HalProxy proxy(subHalsV2_0, subHalsV2_1);
811 std::unique_ptr<EventMessageQueueV2_1> eventQueue =
812 std::make_unique<EventMessageQueueV2_1>(kQueueSize, true);
813 std::unique_ptr<WakeupMessageQueue> wakeLockQueue = makeWakelockFMQ(kQueueSize);
814 ::android::sp<ISensorsCallbackV2_1> callback = new SensorsCallbackV2_1();
815 proxy.initialize_2_1(*eventQueue->getDesc(), *wakeLockQueue->getDesc(), callback);
816
817 std::vector<EventV1_0> events = makeMultipleAccelerometerEvents(kNumEvents);
818
819 std::thread t1(&AllSensorsSubHal<SensorsSubHalV2_0>::postEvents, &subHal1,
820 convertToNewEvents(events), false);
821 std::thread t2(&AllSensorsSubHal<SensorsSubHalV2_1>::postEvents, &subHal2,
822 convertToNewEvents(events), false);
823
824 t1.join();
825 t2.join();
826
827 EXPECT_EQ(eventQueue->availableToRead(), kNumEvents * 2);
828 }
829
830 // Helper implementations follow
testSensorsListFromProxyAndSubHal(const std::vector<SensorInfo> & proxySensorsList,const std::vector<SensorInfo> & subHalSensorsList)831 void testSensorsListFromProxyAndSubHal(const std::vector<SensorInfo>& proxySensorsList,
832 const std::vector<SensorInfo>& subHalSensorsList) {
833 EXPECT_EQ(proxySensorsList.size(), subHalSensorsList.size());
834
835 for (size_t i = 0; i < proxySensorsList.size(); i++) {
836 const SensorInfo& proxySensor = proxySensorsList[i];
837 const SensorInfo& subHalSensor = subHalSensorsList[i];
838
839 EXPECT_EQ(proxySensor.type, subHalSensor.type);
840 EXPECT_EQ(proxySensor.sensorHandle & 0x00FFFFFF, subHalSensor.sensorHandle);
841 }
842 }
843
testSensorsListForOneDirectChannelEnabledSubHal(const std::vector<SensorInfo> & sensorsList,size_t enabledSubHalIndex)844 void testSensorsListForOneDirectChannelEnabledSubHal(const std::vector<SensorInfo>& sensorsList,
845 size_t enabledSubHalIndex) {
846 for (const SensorInfo& sensor : sensorsList) {
847 size_t subHalIndex = static_cast<size_t>(sensor.sensorHandle >> 24);
848 if (subHalIndex == enabledSubHalIndex) {
849 // First subhal should have been picked as the direct channel subhal
850 // and so have direct channel enabled on all of its sensors
851 EXPECT_NE(sensor.flags & SensorFlagBits::MASK_DIRECT_REPORT, 0);
852 EXPECT_NE(sensor.flags & SensorFlagBits::MASK_DIRECT_CHANNEL, 0);
853 } else {
854 // All other subhals should have direct channel disabled for all sensors
855 EXPECT_EQ(sensor.flags & SensorFlagBits::MASK_DIRECT_REPORT, 0);
856 EXPECT_EQ(sensor.flags & SensorFlagBits::MASK_DIRECT_CHANNEL, 0);
857 }
858 }
859 }
860
ackWakeupEventsToHalProxy(size_t numEvents,std::unique_ptr<WakeupMessageQueue> & wakelockQueue,EventFlag * wakelockQueueFlag)861 void ackWakeupEventsToHalProxy(size_t numEvents, std::unique_ptr<WakeupMessageQueue>& wakelockQueue,
862 EventFlag* wakelockQueueFlag) {
863 uint32_t numEventsUInt32 = static_cast<uint32_t>(numEvents);
864 wakelockQueue->write(&numEventsUInt32);
865 wakelockQueueFlag->wake(static_cast<uint32_t>(WakeLockQueueFlagBits::DATA_WRITTEN));
866 }
867
readEventsOutOfQueue(size_t numEvents,std::unique_ptr<EventMessageQueueV2_0> & eventQueue,EventFlag * eventQueueFlag)868 bool readEventsOutOfQueue(size_t numEvents, std::unique_ptr<EventMessageQueueV2_0>& eventQueue,
869 EventFlag* eventQueueFlag) {
870 constexpr int64_t kReadBlockingTimeout = INT64_C(500000000);
871 std::vector<EventV1_0> events(numEvents);
872 return eventQueue->readBlocking(events.data(), numEvents,
873 static_cast<uint32_t>(EventQueueFlagBits::EVENTS_READ),
874 static_cast<uint32_t>(EventQueueFlagBits::READ_AND_PROCESS),
875 kReadBlockingTimeout, eventQueueFlag);
876 }
877
makeEventFMQ(size_t size)878 std::unique_ptr<EventMessageQueueV2_0> makeEventFMQ(size_t size) {
879 return std::make_unique<EventMessageQueueV2_0>(size, true);
880 }
881
makeWakelockFMQ(size_t size)882 std::unique_ptr<WakeupMessageQueue> makeWakelockFMQ(size_t size) {
883 return std::make_unique<WakeupMessageQueue>(size, true);
884 }
885
makeProximityEvent()886 EventV1_0 makeProximityEvent() {
887 EventV1_0 event;
888 event.timestamp = 0xFF00FF00;
889 // This is the sensorhandle of proximity, which is wakeup type
890 event.sensorHandle = 0x00000008;
891 event.sensorType = SensorType::PROXIMITY;
892 event.u = EventPayload();
893 return event;
894 }
895
makeAccelerometerEvent()896 EventV1_0 makeAccelerometerEvent() {
897 EventV1_0 event;
898 event.timestamp = 0xFF00FF00;
899 // This is the sensorhandle of proximity, which is wakeup type
900 event.sensorHandle = 0x00000001;
901 event.sensorType = SensorType::ACCELEROMETER;
902 event.u = EventPayload();
903 return event;
904 }
905
makeMultipleProximityEvents(size_t numEvents)906 std::vector<EventV1_0> makeMultipleProximityEvents(size_t numEvents) {
907 std::vector<EventV1_0> events;
908 for (size_t i = 0; i < numEvents; i++) {
909 events.push_back(makeProximityEvent());
910 }
911 return events;
912 }
913
makeMultipleAccelerometerEvents(size_t numEvents)914 std::vector<EventV1_0> makeMultipleAccelerometerEvents(size_t numEvents) {
915 std::vector<EventV1_0> events;
916 for (size_t i = 0; i < numEvents; i++) {
917 events.push_back(makeAccelerometerEvent());
918 }
919 return events;
920 }
921
makeSensorsAndSensorHandlesStartingAndOfSize(int32_t start,size_t size,std::vector<SensorInfo> & sensors,std::vector<int32_t> & sensorHandles)922 void makeSensorsAndSensorHandlesStartingAndOfSize(int32_t start, size_t size,
923 std::vector<SensorInfo>& sensors,
924 std::vector<int32_t>& sensorHandles) {
925 for (int32_t sensorHandle = start; sensorHandle < start + static_cast<int32_t>(size);
926 sensorHandle++) {
927 SensorInfo sensor;
928 // Just set the sensorHandle field to the correct value so as to not have
929 // to compare every field
930 sensor.sensorHandle = sensorHandle;
931 sensors.push_back(sensor);
932 sensorHandles.push_back(sensorHandle);
933 }
934 }
935
936 } // namespace
937