1 /*
2 * Copyright (C) 2018 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_SENSORS_HIDL_TEST_BASE_H
18 #define ANDROID_SENSORS_HIDL_TEST_BASE_H
19
20 #include "sensors-vts-utils/SensorEventsChecker.h"
21 #include "sensors-vts-utils/SensorsTestSharedMemory.h"
22 #include "sensors-vts-utils/SensorsVtsEnvironmentBase.h"
23
24 #include <android/hardware/sensors/1.0/ISensors.h>
25 #include <android/hardware/sensors/1.0/types.h>
26 #include <gtest/gtest.h>
27 #include <hardware/sensors.h>
28 #include <log/log.h>
29
30 #include <cinttypes>
31 #include <unordered_set>
32 #include <vector>
33
34 using ::android::sp;
35 using ::android::hardware::hidl_string;
36 using ::android::hardware::Return;
37 using ::android::hardware::Void;
38
39 using ::android::sp;
40 using ::android::hardware::hidl_string;
41 using ::android::hardware::sensors::V1_0::RateLevel;
42 using ::android::hardware::sensors::V1_0::Result;
43 using ::android::hardware::sensors::V1_0::SensorFlagBits;
44 using ::android::hardware::sensors::V1_0::SensorFlagShift;
45 using ::android::hardware::sensors::V1_0::SensorsEventFormatOffset;
46 using ::android::hardware::sensors::V1_0::SharedMemInfo;
47 using ::android::hardware::sensors::V1_0::SharedMemType;
48
49 template <class SensorTypeT>
assertTypeMatchStringType(SensorTypeT type,const hidl_string & stringType)50 static void assertTypeMatchStringType(SensorTypeT type, const hidl_string& stringType) {
51 if (type >= SensorTypeT::DEVICE_PRIVATE_BASE) {
52 return;
53 }
54
55 switch (type) {
56 #define CHECK_TYPE_STRING_FOR_SENSOR_TYPE(type) \
57 case SensorTypeT::type: \
58 ASSERT_STREQ(SENSOR_STRING_TYPE_##type, stringType.c_str()); \
59 break;
60 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(ACCELEROMETER);
61 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(ACCELEROMETER_UNCALIBRATED);
62 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(ADDITIONAL_INFO);
63 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(AMBIENT_TEMPERATURE);
64 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(DEVICE_ORIENTATION);
65 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(DYNAMIC_SENSOR_META);
66 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(GAME_ROTATION_VECTOR);
67 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(GEOMAGNETIC_ROTATION_VECTOR);
68 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(GLANCE_GESTURE);
69 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(GRAVITY);
70 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(GYROSCOPE);
71 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(GYROSCOPE_UNCALIBRATED);
72 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(HEART_BEAT);
73 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(HEART_RATE);
74 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(LIGHT);
75 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(LINEAR_ACCELERATION);
76 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(LOW_LATENCY_OFFBODY_DETECT);
77 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(MAGNETIC_FIELD);
78 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(MAGNETIC_FIELD_UNCALIBRATED);
79 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(MOTION_DETECT);
80 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(ORIENTATION);
81 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(PICK_UP_GESTURE);
82 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(POSE_6DOF);
83 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(PRESSURE);
84 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(PROXIMITY);
85 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(RELATIVE_HUMIDITY);
86 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(ROTATION_VECTOR);
87 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(SIGNIFICANT_MOTION);
88 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(STATIONARY_DETECT);
89 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(STEP_COUNTER);
90 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(STEP_DETECTOR);
91 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(TEMPERATURE);
92 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(TILT_DETECTOR);
93 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(WAKE_GESTURE);
94 CHECK_TYPE_STRING_FOR_SENSOR_TYPE(WRIST_TILT_GESTURE);
95 default:
96 FAIL() << "Type " << static_cast<int>(type)
97 << " in android defined range is not checked, "
98 << "stringType = " << stringType;
99 #undef CHECK_TYPE_STRING_FOR_SENSOR_TYPE
100 }
101 }
102
103 template <class SensorTypeT>
expectedReportModeForType(SensorTypeT type)104 static SensorFlagBits expectedReportModeForType(SensorTypeT type) {
105 switch (type) {
106 case SensorTypeT::ACCELEROMETER:
107 case SensorTypeT::ACCELEROMETER_UNCALIBRATED:
108 case SensorTypeT::GYROSCOPE:
109 case SensorTypeT::MAGNETIC_FIELD:
110 case SensorTypeT::ORIENTATION:
111 case SensorTypeT::PRESSURE:
112 case SensorTypeT::GRAVITY:
113 case SensorTypeT::LINEAR_ACCELERATION:
114 case SensorTypeT::ROTATION_VECTOR:
115 case SensorTypeT::MAGNETIC_FIELD_UNCALIBRATED:
116 case SensorTypeT::GAME_ROTATION_VECTOR:
117 case SensorTypeT::GYROSCOPE_UNCALIBRATED:
118 case SensorTypeT::GEOMAGNETIC_ROTATION_VECTOR:
119 case SensorTypeT::POSE_6DOF:
120 case SensorTypeT::HEART_BEAT:
121 return SensorFlagBits::CONTINUOUS_MODE;
122
123 case SensorTypeT::LIGHT:
124 case SensorTypeT::PROXIMITY:
125 case SensorTypeT::RELATIVE_HUMIDITY:
126 case SensorTypeT::AMBIENT_TEMPERATURE:
127 case SensorTypeT::HEART_RATE:
128 case SensorTypeT::DEVICE_ORIENTATION:
129 case SensorTypeT::STEP_COUNTER:
130 case SensorTypeT::LOW_LATENCY_OFFBODY_DETECT:
131 return SensorFlagBits::ON_CHANGE_MODE;
132
133 case SensorTypeT::SIGNIFICANT_MOTION:
134 case SensorTypeT::WAKE_GESTURE:
135 case SensorTypeT::GLANCE_GESTURE:
136 case SensorTypeT::PICK_UP_GESTURE:
137 case SensorTypeT::MOTION_DETECT:
138 case SensorTypeT::STATIONARY_DETECT:
139 return SensorFlagBits::ONE_SHOT_MODE;
140
141 case SensorTypeT::STEP_DETECTOR:
142 case SensorTypeT::TILT_DETECTOR:
143 case SensorTypeT::WRIST_TILT_GESTURE:
144 case SensorTypeT::DYNAMIC_SENSOR_META:
145 return SensorFlagBits::SPECIAL_REPORTING_MODE;
146
147 case SensorTypeT::TEMPERATURE:
148 ALOGW("Device temperature sensor is deprecated, ignoring for test");
149 return (SensorFlagBits)-1;
150
151 default:
152 ALOGW("Type %d is not implemented in expectedReportModeForType", (int)type);
153 return (SensorFlagBits)-1;
154 }
155 }
156
157 template <class SensorTypeVersion, class EventType, class SensorInfoType>
158 class SensorsHidlTestBase : public testing::TestWithParam<std::string> {
159 public:
160 using ISensors = ::android::hardware::sensors::V1_0::ISensors;
161
SensorsHidlTestBase()162 SensorsHidlTestBase()
163 : mAccelNormChecker(Vec3NormChecker<EventType>::byNominal(GRAVITY_EARTH, 1.0f /*m/s^2*/)),
164 mGyroNormChecker(Vec3NormChecker<EventType>::byNominal(0.f, 0.1f /*rad/s*/)) {}
165
166 virtual SensorsVtsEnvironmentBase<EventType>* getEnvironment() = 0;
167
SetUp()168 virtual void SetUp() override {}
169
TearDown()170 virtual void TearDown() override {
171 // stop all sensors
172 for (auto s : mSensorHandles) {
173 activate(s, false);
174 }
175 mSensorHandles.clear();
176
177 // stop all direct report and channels
178 for (auto c : mDirectChannelHandles) {
179 // disable all reports
180 configDirectReport(-1, c, RateLevel::STOP, [](auto, auto) {});
181 unregisterDirectChannel(c);
182 }
183 mDirectChannelHandles.clear();
184 }
185
186 // implementation wrapper
187 virtual SensorInfoType defaultSensorByType(SensorTypeVersion type) = 0;
188 virtual Return<void> getSensorsList(ISensors::getSensorsList_cb _hidl_cb) = 0;
189 virtual Return<Result> injectSensorData(const EventType& event) = 0;
190 virtual Return<Result> activate(int32_t sensorHandle, bool enabled) = 0;
191 virtual Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
192 int64_t maxReportLatencyNs) = 0;
193 virtual Return<Result> flush(int32_t sensorHandle) = 0;
194 virtual Return<void> registerDirectChannel(const SharedMemInfo& mem,
195 ISensors::registerDirectChannel_cb _hidl_cb) = 0;
196 virtual Return<Result> unregisterDirectChannel(int32_t channelHandle) = 0;
197 virtual Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle,
198 RateLevel rate,
199 ISensors::configDirectReport_cb _hidl_cb) = 0;
200
testStreamingOperation(SensorTypeVersion type,std::chrono::nanoseconds samplingPeriod,std::chrono::seconds duration,const SensorEventsChecker<EventType> & checker)201 void testStreamingOperation(SensorTypeVersion type, std::chrono::nanoseconds samplingPeriod,
202 std::chrono::seconds duration,
203 const SensorEventsChecker<EventType>& checker) {
204 std::vector<EventType> events;
205 std::vector<EventType> sensorEvents;
206
207 const int64_t samplingPeriodInNs = samplingPeriod.count();
208 const int64_t batchingPeriodInNs = 0; // no batching
209 const useconds_t minTimeUs = std::chrono::microseconds(duration).count();
210 const size_t minNEvent = duration / samplingPeriod;
211
212 SensorInfoType sensor = defaultSensorByType(type);
213
214 if (!isValidType(sensor.type)) {
215 // no default sensor of this type
216 return;
217 }
218
219 if (std::chrono::microseconds(sensor.minDelay) > samplingPeriod) {
220 // rate not supported
221 return;
222 }
223
224 int32_t handle = sensor.sensorHandle;
225
226 ASSERT_EQ(batch(handle, samplingPeriodInNs, batchingPeriodInNs), Result::OK);
227 ASSERT_EQ(activate(handle, 1), Result::OK);
228 events = getEnvironment()->collectEvents(
229 minTimeUs, minNEvent, true /* clearBeforeStart */, true /* changeCollection */,
230 [&type](const EventType& event) { return event.sensorType == type; });
231 ASSERT_EQ(activate(handle, 0), Result::OK);
232
233 ALOGI("Collected %zu samples", events.size());
234
235 ASSERT_GT(events.size(), 0u);
236
237 bool handleMismatchReported = false;
238 for (auto& e : events) {
239 // avoid generating hundreds of error
240 if (!handleMismatchReported) {
241 EXPECT_EQ(e.sensorHandle, handle)
242 << (handleMismatchReported = true,
243 "Event of the same type must come from the sensor registered");
244 }
245 sensorEvents.push_back(e);
246 }
247
248 std::string s;
249 EXPECT_TRUE(checker.check(sensorEvents, &s)) << s;
250
251 EXPECT_GE(sensorEvents.size(),
252 minNEvent / 2); // make sure returned events are not all meta
253 }
254
255 void testSamplingRateHotSwitchOperation(SensorTypeVersion type, bool fastToSlow = true) {
256 std::vector<EventType> events1, events2;
257
258 constexpr int64_t batchingPeriodInNs = 0; // no batching
259 constexpr int64_t collectionTimeoutUs = 60000000; // 60s
260 constexpr size_t minNEvent = 50;
261
262 SensorInfoType sensor = defaultSensorByType(type);
263
264 if (!isValidType(sensor.type)) {
265 // no default sensor of this type
266 return;
267 }
268
269 int32_t handle = sensor.sensorHandle;
270 int64_t minSamplingPeriodInNs = sensor.minDelay * 1000ll;
271 int64_t maxSamplingPeriodInNs = sensor.maxDelay * 1000ll;
272
273 if (minSamplingPeriodInNs == maxSamplingPeriodInNs) {
274 // only support single rate
275 return;
276 }
277
278 int64_t firstCollectionPeriod = fastToSlow ? minSamplingPeriodInNs : maxSamplingPeriodInNs;
279 int64_t secondCollectionPeriod =
280 !fastToSlow ? minSamplingPeriodInNs : maxSamplingPeriodInNs;
281
282 // first collection
283 ASSERT_EQ(batch(handle, firstCollectionPeriod, batchingPeriodInNs), Result::OK);
284 ASSERT_EQ(activate(handle, 1), Result::OK);
285
286 usleep(500000); // sleep 0.5 sec to wait for change rate to happen
287 events1 = getEnvironment()->collectEvents(collectionTimeoutUs, minNEvent);
288
289 // second collection, without stopping the sensor
290 ASSERT_EQ(batch(handle, secondCollectionPeriod, batchingPeriodInNs), Result::OK);
291
292 usleep(500000); // sleep 0.5 sec to wait for change rate to happen
293 events2 = getEnvironment()->collectEvents(collectionTimeoutUs, minNEvent);
294
295 // end of collection, stop sensor
296 ASSERT_EQ(activate(handle, 0), Result::OK);
297
298 ALOGI("Collected %zu fast samples and %zu slow samples", events1.size(), events2.size());
299
300 ASSERT_GT(events1.size(), 0u);
301 ASSERT_GT(events2.size(), 0u);
302
303 int64_t minDelayAverageInterval, maxDelayAverageInterval;
304 std::vector<EventType>& minDelayEvents(fastToSlow ? events1 : events2);
305 std::vector<EventType>& maxDelayEvents(fastToSlow ? events2 : events1);
306
307 size_t nEvent = 0;
308 int64_t prevTimestamp = -1;
309 int64_t timestampInterval = 0;
310 for (auto& e : minDelayEvents) {
311 if (e.sensorType == type) {
312 ASSERT_EQ(e.sensorHandle, handle);
313 if (prevTimestamp > 0) {
314 timestampInterval += e.timestamp - prevTimestamp;
315 }
316 prevTimestamp = e.timestamp;
317 ++nEvent;
318 }
319 }
320 ASSERT_GT(nEvent, 2u);
321 minDelayAverageInterval = timestampInterval / (nEvent - 1);
322
323 nEvent = 0;
324 prevTimestamp = -1;
325 timestampInterval = 0;
326 for (auto& e : maxDelayEvents) {
327 if (e.sensorType == type) {
328 ASSERT_EQ(e.sensorHandle, handle);
329 if (prevTimestamp > 0) {
330 timestampInterval += e.timestamp - prevTimestamp;
331 }
332 prevTimestamp = e.timestamp;
333 ++nEvent;
334 }
335 }
336 ASSERT_GT(nEvent, 2u);
337 maxDelayAverageInterval = timestampInterval / (nEvent - 1);
338
339 // change of rate is significant.
340 ALOGI("min/maxDelayAverageInterval = %" PRId64 " %" PRId64, minDelayAverageInterval,
341 maxDelayAverageInterval);
342 EXPECT_GT((maxDelayAverageInterval - minDelayAverageInterval),
343 minDelayAverageInterval / 10);
344
345 // fastest rate sampling time is close to spec
346 EXPECT_LE(std::abs(minDelayAverageInterval - minSamplingPeriodInNs),
347 minSamplingPeriodInNs / 10);
348
349 // slowest rate sampling time is close to spec
350 EXPECT_LE(std::abs(maxDelayAverageInterval - maxSamplingPeriodInNs),
351 maxSamplingPeriodInNs / 10);
352 }
353
testBatchingOperation(SensorTypeVersion type)354 void testBatchingOperation(SensorTypeVersion type) {
355 std::vector<EventType> events;
356
357 constexpr int64_t maxBatchingTestTimeNs = 30ull * 1000 * 1000 * 1000;
358 constexpr int64_t oneSecondInNs = 1ull * 1000 * 1000 * 1000;
359
360 SensorInfoType sensor = defaultSensorByType(type);
361
362 if (!isValidType(sensor.type)) {
363 // no default sensor of this type
364 return;
365 }
366
367 int32_t handle = sensor.sensorHandle;
368 int64_t minSamplingPeriodInNs = sensor.minDelay * 1000ll;
369 uint32_t minFifoCount = sensor.fifoReservedEventCount;
370 int64_t batchingPeriodInNs = minFifoCount * minSamplingPeriodInNs;
371
372 if (batchingPeriodInNs < oneSecondInNs) {
373 // batching size too small to test reliably
374 return;
375 }
376
377 if (batchingPeriodInNs > maxBatchingTestTimeNs) {
378 batchingPeriodInNs = maxBatchingTestTimeNs;
379 minFifoCount = (uint32_t)(batchingPeriodInNs / minSamplingPeriodInNs);
380 }
381
382 ALOGI("Test batching for %d ms", (int)(batchingPeriodInNs / 1000 / 1000));
383
384 int64_t allowedBatchDeliverTimeNs = std::max(oneSecondInNs, batchingPeriodInNs / 10);
385
386 ASSERT_EQ(batch(handle, minSamplingPeriodInNs, INT64_MAX), Result::OK);
387 ASSERT_EQ(activate(handle, 1), Result::OK);
388
389 usleep(500000); // sleep 0.5 sec to wait for initialization
390 ASSERT_EQ(flush(handle), Result::OK);
391
392 // wait for 80% of the reserved batching period
393 // there should not be any significant amount of events
394 // since collection is not enabled all events will go down the drain
395 usleep(batchingPeriodInNs / 1000 * 8 / 10);
396
397 getEnvironment()->setCollection(true);
398 // clean existing collections
399 getEnvironment()->collectEvents(0 /*timeLimitUs*/, 0 /*nEventLimit*/,
400 true /*clearBeforeStart*/, false /*change collection*/);
401
402 // 0.8 + 0.2 times the batching period
403 usleep(batchingPeriodInNs / 1000 * 2 / 10);
404 ASSERT_EQ(flush(handle), Result::OK);
405
406 // plus some time for the event to deliver
407 events = getEnvironment()->collectEvents(allowedBatchDeliverTimeNs / 1000, minFifoCount,
408 false /*clearBeforeStart*/,
409 false /*change collection*/);
410
411 getEnvironment()->setCollection(false);
412 ASSERT_EQ(activate(handle, 0), Result::OK);
413
414 size_t nEvent = 0;
415 for (auto& e : events) {
416 if (e.sensorType == type && e.sensorHandle == handle) {
417 ++nEvent;
418 }
419 }
420
421 // at least reach 90% of advertised capacity
422 ASSERT_GT(nEvent, (size_t)(minFifoCount * 9 / 10));
423 }
424
testDirectReportOperation(SensorTypeVersion type,SharedMemType memType,RateLevel rate,const SensorEventsChecker<EventType> & checker)425 void testDirectReportOperation(SensorTypeVersion type, SharedMemType memType, RateLevel rate,
426 const SensorEventsChecker<EventType>& checker) {
427 constexpr size_t kEventSize = static_cast<size_t>(SensorsEventFormatOffset::TOTAL_LENGTH);
428 constexpr size_t kNEvent = 4096;
429 constexpr size_t kMemSize = kEventSize * kNEvent;
430
431 constexpr float kNormalNominal = 50;
432 constexpr float kFastNominal = 200;
433 constexpr float kVeryFastNominal = 800;
434
435 constexpr float kNominalTestTimeSec = 1.f;
436 constexpr float kMaxTestTimeSec =
437 kNominalTestTimeSec + 0.5f; // 0.5 second for initialization
438
439 SensorInfoType sensor = defaultSensorByType(type);
440
441 if (!isValidType(sensor.type)) {
442 // no default sensor of this type
443 return;
444 }
445
446 if (!isDirectReportRateSupported(sensor, rate)) {
447 return;
448 }
449
450 if (!isDirectChannelTypeSupported(sensor, memType)) {
451 return;
452 }
453
454 std::unique_ptr<SensorsTestSharedMemory<SensorTypeVersion, EventType>> mem(
455 SensorsTestSharedMemory<SensorTypeVersion, EventType>::create(memType, kMemSize));
456 ASSERT_NE(mem, nullptr);
457
458 char* buffer = mem->getBuffer();
459 // fill memory with data
460 for (size_t i = 0; i < kMemSize; ++i) {
461 buffer[i] = '\xcc';
462 }
463
464 int32_t channelHandle;
465 registerDirectChannel(mem->getSharedMemInfo(),
466 [&channelHandle](auto result, auto channelHandle_) {
467 ASSERT_EQ(result, Result::OK);
468 channelHandle = channelHandle_;
469 });
470
471 // check memory is zeroed
472 for (size_t i = 0; i < kMemSize; ++i) {
473 ASSERT_EQ(buffer[i], '\0');
474 }
475
476 int32_t eventToken;
477 configDirectReport(sensor.sensorHandle, channelHandle, rate,
478 [&eventToken](auto result, auto token) {
479 ASSERT_EQ(result, Result::OK);
480 eventToken = token;
481 });
482
483 usleep(static_cast<useconds_t>(kMaxTestTimeSec * 1e6f));
484 auto events = mem->parseEvents();
485
486 // find norminal rate
487 float nominalFreq = 0.f;
488 switch (rate) {
489 case RateLevel::NORMAL:
490 nominalFreq = kNormalNominal;
491 break;
492 case RateLevel::FAST:
493 nominalFreq = kFastNominal;
494 break;
495 case RateLevel::VERY_FAST:
496 nominalFreq = kVeryFastNominal;
497 break;
498 case RateLevel::STOP:
499 FAIL();
500 }
501
502 // allowed to be between 55% and 220% of nominal freq
503 ASSERT_GT(events.size(), static_cast<size_t>(nominalFreq * 0.55f * kNominalTestTimeSec));
504 ASSERT_LT(events.size(), static_cast<size_t>(nominalFreq * 2.2f * kMaxTestTimeSec));
505
506 int64_t lastTimestamp = 0;
507 bool typeErrorReported = false;
508 bool tokenErrorReported = false;
509 bool timestampErrorReported = false;
510 std::vector<EventType> sensorEvents;
511 for (auto& e : events) {
512 if (!tokenErrorReported) {
513 EXPECT_EQ(eventToken, e.sensorHandle)
514 << (tokenErrorReported = true,
515 "Event token does not match that retured from configDirectReport");
516 }
517
518 if (isMetaSensorType(e.sensorType)) {
519 continue;
520 }
521 sensorEvents.push_back(e);
522
523 if (!typeErrorReported) {
524 EXPECT_EQ(type, e.sensorType)
525 << (typeErrorReported = true,
526 "Type in event does not match type of sensor registered.");
527 }
528 if (!timestampErrorReported) {
529 EXPECT_GT(e.timestamp, lastTimestamp) << (timestampErrorReported = true,
530 "Timestamp not monotonically increasing");
531 }
532 lastTimestamp = e.timestamp;
533 }
534
535 std::string s;
536 EXPECT_TRUE(checker.check(sensorEvents, &s)) << s;
537
538 // stop sensor and unregister channel
539 configDirectReport(sensor.sensorHandle, channelHandle, RateLevel::STOP,
540 [](auto result, auto) { EXPECT_EQ(result, Result::OK); });
541 EXPECT_EQ(unregisterDirectChannel(channelHandle), Result::OK);
542 }
543
extractReportMode(uint64_t flag)544 inline static SensorFlagBits extractReportMode(uint64_t flag) {
545 return (SensorFlagBits)(flag & ((uint64_t)SensorFlagBits::CONTINUOUS_MODE |
546 (uint64_t)SensorFlagBits::ON_CHANGE_MODE |
547 (uint64_t)SensorFlagBits::ONE_SHOT_MODE |
548 (uint64_t)SensorFlagBits::SPECIAL_REPORTING_MODE));
549 }
550
isMetaSensorType(SensorTypeVersion type)551 inline static bool isMetaSensorType(SensorTypeVersion type) {
552 return (type == SensorTypeVersion::META_DATA ||
553 type == SensorTypeVersion::DYNAMIC_SENSOR_META ||
554 type == SensorTypeVersion::ADDITIONAL_INFO);
555 }
556
isValidType(SensorTypeVersion type)557 inline static bool isValidType(SensorTypeVersion type) { return (int32_t)type > 0; }
558
assertDelayMatchReportMode(int32_t minDelay,int32_t maxDelay,SensorFlagBits reportMode)559 static void assertDelayMatchReportMode(int32_t minDelay, int32_t maxDelay,
560 SensorFlagBits reportMode) {
561 switch (reportMode) {
562 case SensorFlagBits::CONTINUOUS_MODE:
563 ASSERT_LT(0, minDelay);
564 ASSERT_LE(0, maxDelay);
565 break;
566 case SensorFlagBits::ON_CHANGE_MODE:
567 ASSERT_LE(0, minDelay);
568 ASSERT_LE(0, maxDelay);
569 break;
570 case SensorFlagBits::ONE_SHOT_MODE:
571 ASSERT_EQ(-1, minDelay);
572 ASSERT_EQ(0, maxDelay);
573 break;
574 case SensorFlagBits::SPECIAL_REPORTING_MODE:
575 // do not enforce anything for special reporting mode
576 break;
577 default:
578 FAIL() << "Report mode " << static_cast<int>(reportMode) << " not checked";
579 }
580 }
581
582 protected:
assertTypeMatchReportMode(SensorTypeVersion type,SensorFlagBits reportMode)583 static void assertTypeMatchReportMode(SensorTypeVersion type, SensorFlagBits reportMode) {
584 if (type >= SensorTypeVersion::DEVICE_PRIVATE_BASE) {
585 return;
586 }
587
588 SensorFlagBits expected = expectedReportModeForType(type);
589
590 ASSERT_TRUE(expected == (SensorFlagBits)-1 || expected == reportMode)
591 << "reportMode=" << static_cast<int>(reportMode)
592 << "expected=" << static_cast<int>(expected);
593 }
594
isDirectReportRateSupported(SensorInfoType sensor,RateLevel rate)595 static bool isDirectReportRateSupported(SensorInfoType sensor, RateLevel rate) {
596 unsigned int r =
597 static_cast<unsigned int>(sensor.flags & SensorFlagBits::MASK_DIRECT_REPORT) >>
598 static_cast<unsigned int>(SensorFlagShift::DIRECT_REPORT);
599 return r >= static_cast<unsigned int>(rate);
600 }
601
isDirectChannelTypeSupported(SensorInfoType sensor,SharedMemType type)602 static bool isDirectChannelTypeSupported(SensorInfoType sensor, SharedMemType type) {
603 switch (type) {
604 case SharedMemType::ASHMEM:
605 return (sensor.flags & SensorFlagBits::DIRECT_CHANNEL_ASHMEM) != 0;
606 case SharedMemType::GRALLOC:
607 return (sensor.flags & SensorFlagBits::DIRECT_CHANNEL_GRALLOC) != 0;
608 default:
609 return false;
610 }
611 }
612
613 // Checkers
614 Vec3NormChecker<EventType> mAccelNormChecker;
615 Vec3NormChecker<EventType> mGyroNormChecker;
616
617 // all sensors and direct channnels used
618 std::unordered_set<int32_t> mSensorHandles;
619 std::unordered_set<int32_t> mDirectChannelHandles;
620 };
621
622 #endif // ANDROID_SENSORS_HIDL_TEST_BASE_H
623