Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 <aidl/Gtest.h> |
| 17 | #include <aidl/Vintf.h> |
| 18 | |
| 19 | #include <aidl/android/hardware/sensors/BnSensors.h> |
| 20 | #include <aidl/android/hardware/sensors/ISensors.h> |
| 21 | #include <android/binder_manager.h> |
| 22 | #include <binder/IServiceManager.h> |
| 23 | #include <binder/ProcessState.h> |
| 24 | #include <hardware/sensors.h> |
| 25 | #include <log/log.h> |
| 26 | #include <utils/SystemClock.h> |
| 27 | |
| 28 | #include "SensorsAidlEnvironment.h" |
Grace Cheng | 629b3a4 | 2022-01-06 12:19:17 +0000 | [diff] [blame] | 29 | #include "SensorsAidlTestSharedMemory.h" |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 30 | #include "sensors-vts-utils/SensorsVtsEnvironmentBase.h" |
| 31 | |
| 32 | #include <cinttypes> |
| 33 | #include <condition_variable> |
| 34 | #include <map> |
| 35 | #include <unordered_map> |
| 36 | #include <unordered_set> |
| 37 | #include <vector> |
| 38 | |
| 39 | using aidl::android::hardware::sensors::Event; |
| 40 | using aidl::android::hardware::sensors::ISensors; |
| 41 | using aidl::android::hardware::sensors::SensorInfo; |
| 42 | using aidl::android::hardware::sensors::SensorStatus; |
| 43 | using aidl::android::hardware::sensors::SensorType; |
| 44 | using android::ProcessState; |
| 45 | using std::chrono::duration_cast; |
| 46 | |
Grace Cheng | 629b3a4 | 2022-01-06 12:19:17 +0000 | [diff] [blame] | 47 | constexpr size_t kEventSize = |
| 48 | static_cast<size_t>(ISensors::DIRECT_REPORT_SENSOR_EVENT_TOTAL_LENGTH); |
| 49 | |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 50 | namespace { |
| 51 | |
| 52 | static void assertTypeMatchStringType(SensorType type, const std::string& stringType) { |
| 53 | if (type >= SensorType::DEVICE_PRIVATE_BASE) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | switch (type) { |
| 58 | #define CHECK_TYPE_STRING_FOR_SENSOR_TYPE(type) \ |
| 59 | case SensorType::type: \ |
| 60 | ASSERT_STREQ(SENSOR_STRING_TYPE_##type, stringType.c_str()); \ |
| 61 | break; |
| 62 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(ACCELEROMETER); |
Tyler Trephan | 38e65b1 | 2022-01-25 23:04:55 +0000 | [diff] [blame] | 63 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(ACCELEROMETER_LIMITED_AXES); |
| 64 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(ACCELEROMETER_LIMITED_AXES_UNCALIBRATED); |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 65 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(ACCELEROMETER_UNCALIBRATED); |
| 66 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(ADDITIONAL_INFO); |
| 67 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(AMBIENT_TEMPERATURE); |
| 68 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(DEVICE_ORIENTATION); |
| 69 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(DYNAMIC_SENSOR_META); |
| 70 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(GAME_ROTATION_VECTOR); |
| 71 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(GEOMAGNETIC_ROTATION_VECTOR); |
| 72 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(GLANCE_GESTURE); |
| 73 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(GRAVITY); |
| 74 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(GYROSCOPE); |
Tyler Trephan | 38e65b1 | 2022-01-25 23:04:55 +0000 | [diff] [blame] | 75 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(GYROSCOPE_LIMITED_AXES); |
| 76 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(GYROSCOPE_LIMITED_AXES_UNCALIBRATED); |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 77 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(GYROSCOPE_UNCALIBRATED); |
Tyler Trephan | 12cf91d | 2022-01-28 21:09:35 +0000 | [diff] [blame] | 78 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(HEADING); |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 79 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(HEART_BEAT); |
| 80 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(HEART_RATE); |
| 81 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(LIGHT); |
| 82 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(LINEAR_ACCELERATION); |
| 83 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(LOW_LATENCY_OFFBODY_DETECT); |
| 84 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(MAGNETIC_FIELD); |
| 85 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(MAGNETIC_FIELD_UNCALIBRATED); |
| 86 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(MOTION_DETECT); |
| 87 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(ORIENTATION); |
| 88 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(PICK_UP_GESTURE); |
| 89 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(POSE_6DOF); |
| 90 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(PRESSURE); |
| 91 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(PROXIMITY); |
| 92 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(RELATIVE_HUMIDITY); |
| 93 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(ROTATION_VECTOR); |
| 94 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(SIGNIFICANT_MOTION); |
| 95 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(STATIONARY_DETECT); |
| 96 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(STEP_COUNTER); |
| 97 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(STEP_DETECTOR); |
| 98 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(TILT_DETECTOR); |
| 99 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(WAKE_GESTURE); |
| 100 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(WRIST_TILT_GESTURE); |
Dezhi Huang | b5425cb | 2023-03-13 16:38:46 +0800 | [diff] [blame] | 101 | CHECK_TYPE_STRING_FOR_SENSOR_TYPE(HINGE_ANGLE); |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 102 | default: |
| 103 | FAIL() << "Type " << static_cast<int>(type) |
| 104 | << " in android defined range is not checked, " |
| 105 | << "stringType = " << stringType; |
| 106 | #undef CHECK_TYPE_STRING_FOR_SENSOR_TYPE |
| 107 | } |
| 108 | } |
| 109 | |
Grace Cheng | 629b3a4 | 2022-01-06 12:19:17 +0000 | [diff] [blame] | 110 | bool isDirectChannelTypeSupported(SensorInfo sensor, ISensors::SharedMemInfo::SharedMemType type) { |
| 111 | switch (type) { |
| 112 | case ISensors::SharedMemInfo::SharedMemType::ASHMEM: |
| 113 | return (sensor.flags & SensorInfo::SENSOR_FLAG_BITS_DIRECT_CHANNEL_ASHMEM) != 0; |
| 114 | case ISensors::SharedMemInfo::SharedMemType::GRALLOC: |
| 115 | return (sensor.flags & SensorInfo::SENSOR_FLAG_BITS_DIRECT_CHANNEL_GRALLOC) != 0; |
| 116 | default: |
| 117 | return false; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | bool isDirectReportRateSupported(SensorInfo sensor, ISensors::RateLevel rate) { |
| 122 | unsigned int r = static_cast<unsigned int>(sensor.flags & |
| 123 | SensorInfo::SENSOR_FLAG_BITS_MASK_DIRECT_REPORT) >> |
| 124 | static_cast<unsigned int>(SensorInfo::SENSOR_FLAG_SHIFT_DIRECT_REPORT); |
| 125 | return r >= static_cast<unsigned int>(rate); |
| 126 | } |
| 127 | |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 128 | int expectedReportModeForType(SensorType type) { |
| 129 | switch (type) { |
| 130 | case SensorType::ACCELEROMETER: |
Tyler Trephan | 38e65b1 | 2022-01-25 23:04:55 +0000 | [diff] [blame] | 131 | case SensorType::ACCELEROMETER_LIMITED_AXES: |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 132 | case SensorType::ACCELEROMETER_UNCALIBRATED: |
Tyler Trephan | 38e65b1 | 2022-01-25 23:04:55 +0000 | [diff] [blame] | 133 | case SensorType::ACCELEROMETER_LIMITED_AXES_UNCALIBRATED: |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 134 | case SensorType::GYROSCOPE: |
Tyler Trephan | 38e65b1 | 2022-01-25 23:04:55 +0000 | [diff] [blame] | 135 | case SensorType::GYROSCOPE_LIMITED_AXES: |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 136 | case SensorType::MAGNETIC_FIELD: |
| 137 | case SensorType::ORIENTATION: |
| 138 | case SensorType::PRESSURE: |
| 139 | case SensorType::GRAVITY: |
| 140 | case SensorType::LINEAR_ACCELERATION: |
| 141 | case SensorType::ROTATION_VECTOR: |
| 142 | case SensorType::MAGNETIC_FIELD_UNCALIBRATED: |
| 143 | case SensorType::GAME_ROTATION_VECTOR: |
| 144 | case SensorType::GYROSCOPE_UNCALIBRATED: |
Tyler Trephan | 38e65b1 | 2022-01-25 23:04:55 +0000 | [diff] [blame] | 145 | case SensorType::GYROSCOPE_LIMITED_AXES_UNCALIBRATED: |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 146 | case SensorType::GEOMAGNETIC_ROTATION_VECTOR: |
| 147 | case SensorType::POSE_6DOF: |
| 148 | case SensorType::HEART_BEAT: |
Tyler Trephan | 12cf91d | 2022-01-28 21:09:35 +0000 | [diff] [blame] | 149 | case SensorType::HEADING: |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 150 | return SensorInfo::SENSOR_FLAG_BITS_CONTINUOUS_MODE; |
| 151 | |
| 152 | case SensorType::LIGHT: |
| 153 | case SensorType::PROXIMITY: |
| 154 | case SensorType::RELATIVE_HUMIDITY: |
| 155 | case SensorType::AMBIENT_TEMPERATURE: |
| 156 | case SensorType::HEART_RATE: |
| 157 | case SensorType::DEVICE_ORIENTATION: |
| 158 | case SensorType::STEP_COUNTER: |
| 159 | case SensorType::LOW_LATENCY_OFFBODY_DETECT: |
| 160 | return SensorInfo::SENSOR_FLAG_BITS_ON_CHANGE_MODE; |
| 161 | |
| 162 | case SensorType::SIGNIFICANT_MOTION: |
| 163 | case SensorType::WAKE_GESTURE: |
| 164 | case SensorType::GLANCE_GESTURE: |
| 165 | case SensorType::PICK_UP_GESTURE: |
| 166 | case SensorType::MOTION_DETECT: |
| 167 | case SensorType::STATIONARY_DETECT: |
| 168 | return SensorInfo::SENSOR_FLAG_BITS_ONE_SHOT_MODE; |
| 169 | |
| 170 | case SensorType::STEP_DETECTOR: |
| 171 | case SensorType::TILT_DETECTOR: |
| 172 | case SensorType::WRIST_TILT_GESTURE: |
| 173 | case SensorType::DYNAMIC_SENSOR_META: |
| 174 | return SensorInfo::SENSOR_FLAG_BITS_SPECIAL_REPORTING_MODE; |
| 175 | |
| 176 | default: |
| 177 | ALOGW("Type %d is not implemented in expectedReportModeForType", (int)type); |
| 178 | return INT32_MAX; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | void assertTypeMatchReportMode(SensorType type, int reportMode) { |
| 183 | if (type >= SensorType::DEVICE_PRIVATE_BASE) { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | int expected = expectedReportModeForType(type); |
| 188 | |
| 189 | ASSERT_TRUE(expected == INT32_MAX || expected == reportMode) |
| 190 | << "reportMode=" << static_cast<int>(reportMode) |
| 191 | << "expected=" << static_cast<int>(expected); |
| 192 | } |
| 193 | |
| 194 | void assertDelayMatchReportMode(int32_t minDelayUs, int32_t maxDelayUs, int reportMode) { |
| 195 | switch (reportMode) { |
| 196 | case SensorInfo::SENSOR_FLAG_BITS_CONTINUOUS_MODE: |
| 197 | ASSERT_LT(0, minDelayUs); |
| 198 | ASSERT_LE(0, maxDelayUs); |
| 199 | break; |
| 200 | case SensorInfo::SENSOR_FLAG_BITS_ON_CHANGE_MODE: |
| 201 | ASSERT_LE(0, minDelayUs); |
| 202 | ASSERT_LE(0, maxDelayUs); |
| 203 | break; |
| 204 | case SensorInfo::SENSOR_FLAG_BITS_ONE_SHOT_MODE: |
| 205 | ASSERT_EQ(-1, minDelayUs); |
| 206 | ASSERT_EQ(0, maxDelayUs); |
| 207 | break; |
| 208 | case SensorInfo::SENSOR_FLAG_BITS_SPECIAL_REPORTING_MODE: |
| 209 | // do not enforce anything for special reporting mode |
| 210 | break; |
| 211 | default: |
| 212 | FAIL() << "Report mode " << static_cast<int>(reportMode) << " not checked"; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void checkIsOk(ndk::ScopedAStatus status) { |
| 217 | ASSERT_TRUE(status.isOk()); |
| 218 | } |
| 219 | |
| 220 | } // namespace |
| 221 | |
| 222 | class EventCallback : public IEventCallback<Event> { |
| 223 | public: |
| 224 | void reset() { |
| 225 | mFlushMap.clear(); |
| 226 | mEventMap.clear(); |
| 227 | } |
| 228 | |
| 229 | void onEvent(const Event& event) override { |
| 230 | if (event.sensorType == SensorType::META_DATA && |
| 231 | event.payload.get<Event::EventPayload::Tag::meta>().what == |
| 232 | Event::EventPayload::MetaData::MetaDataEventType::META_DATA_FLUSH_COMPLETE) { |
| 233 | std::unique_lock<std::recursive_mutex> lock(mFlushMutex); |
| 234 | mFlushMap[event.sensorHandle]++; |
| 235 | mFlushCV.notify_all(); |
| 236 | } else if (event.sensorType != SensorType::ADDITIONAL_INFO) { |
| 237 | std::unique_lock<std::recursive_mutex> lock(mEventMutex); |
| 238 | mEventMap[event.sensorHandle].push_back(event); |
| 239 | mEventCV.notify_all(); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | int32_t getFlushCount(int32_t sensorHandle) { |
| 244 | std::unique_lock<std::recursive_mutex> lock(mFlushMutex); |
| 245 | return mFlushMap[sensorHandle]; |
| 246 | } |
| 247 | |
| 248 | void waitForFlushEvents(const std::vector<SensorInfo>& sensorsToWaitFor, |
| 249 | int32_t numCallsToFlush, std::chrono::milliseconds timeout) { |
| 250 | std::unique_lock<std::recursive_mutex> lock(mFlushMutex); |
| 251 | mFlushCV.wait_for(lock, timeout, |
| 252 | [&] { return flushesReceived(sensorsToWaitFor, numCallsToFlush); }); |
| 253 | } |
| 254 | |
| 255 | const std::vector<Event> getEvents(int32_t sensorHandle) { |
| 256 | std::unique_lock<std::recursive_mutex> lock(mEventMutex); |
| 257 | return mEventMap[sensorHandle]; |
| 258 | } |
| 259 | |
| 260 | void waitForEvents(const std::vector<SensorInfo>& sensorsToWaitFor, |
| 261 | std::chrono::milliseconds timeout) { |
| 262 | std::unique_lock<std::recursive_mutex> lock(mEventMutex); |
| 263 | mEventCV.wait_for(lock, timeout, [&] { return eventsReceived(sensorsToWaitFor); }); |
| 264 | } |
| 265 | |
| 266 | protected: |
| 267 | bool flushesReceived(const std::vector<SensorInfo>& sensorsToWaitFor, int32_t numCallsToFlush) { |
| 268 | for (const SensorInfo& sensor : sensorsToWaitFor) { |
| 269 | if (getFlushCount(sensor.sensorHandle) < numCallsToFlush) { |
| 270 | return false; |
| 271 | } |
| 272 | } |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | bool eventsReceived(const std::vector<SensorInfo>& sensorsToWaitFor) { |
| 277 | for (const SensorInfo& sensor : sensorsToWaitFor) { |
| 278 | if (getEvents(sensor.sensorHandle).size() == 0) { |
| 279 | return false; |
| 280 | } |
| 281 | } |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | std::map<int32_t, int32_t> mFlushMap; |
| 286 | std::recursive_mutex mFlushMutex; |
| 287 | std::condition_variable_any mFlushCV; |
| 288 | |
| 289 | std::map<int32_t, std::vector<Event>> mEventMap; |
| 290 | std::recursive_mutex mEventMutex; |
| 291 | std::condition_variable_any mEventCV; |
| 292 | }; |
| 293 | |
| 294 | class SensorsAidlTest : public testing::TestWithParam<std::string> { |
| 295 | public: |
| 296 | virtual void SetUp() override { |
| 297 | mEnvironment = new SensorsAidlEnvironment(GetParam()); |
| 298 | mEnvironment->SetUp(); |
| 299 | |
| 300 | // Ensure that we have a valid environment before performing tests |
| 301 | ASSERT_NE(getSensors(), nullptr); |
| 302 | } |
| 303 | |
| 304 | virtual void TearDown() override { |
| 305 | for (int32_t handle : mSensorHandles) { |
| 306 | activate(handle, false); |
| 307 | } |
| 308 | mSensorHandles.clear(); |
| 309 | |
| 310 | mEnvironment->TearDown(); |
| 311 | delete mEnvironment; |
| 312 | mEnvironment = nullptr; |
| 313 | } |
| 314 | |
| 315 | protected: |
| 316 | std::vector<SensorInfo> getNonOneShotSensors(); |
| 317 | std::vector<SensorInfo> getNonOneShotAndNonSpecialSensors(); |
| 318 | std::vector<SensorInfo> getNonOneShotAndNonOnChangeAndNonSpecialSensors(); |
| 319 | std::vector<SensorInfo> getOneShotSensors(); |
| 320 | std::vector<SensorInfo> getInjectEventSensors(); |
| 321 | |
Grace Cheng | 629b3a4 | 2022-01-06 12:19:17 +0000 | [diff] [blame] | 322 | void verifyDirectChannel(ISensors::SharedMemInfo::SharedMemType memType); |
| 323 | |
| 324 | void verifyRegisterDirectChannel( |
| 325 | std::shared_ptr<SensorsAidlTestSharedMemory<SensorType, Event>> mem, |
| 326 | int32_t* directChannelHandle, bool supportsSharedMemType, |
| 327 | bool supportsAnyDirectChannel); |
| 328 | |
| 329 | void verifyConfigure(const SensorInfo& sensor, ISensors::SharedMemInfo::SharedMemType memType, |
| 330 | int32_t directChannelHandle, bool directChannelSupported); |
| 331 | |
| 332 | void queryDirectChannelSupport(ISensors::SharedMemInfo::SharedMemType memType, |
| 333 | bool* supportsSharedMemType, bool* supportsAnyDirectChannel); |
| 334 | |
| 335 | void verifyUnregisterDirectChannel(int32_t* directChannelHandle, bool supportsAnyDirectChannel); |
| 336 | |
| 337 | void checkRateLevel(const SensorInfo& sensor, int32_t directChannelHandle, |
| 338 | ISensors::RateLevel rateLevel, int32_t* reportToken); |
| 339 | |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 340 | inline std::shared_ptr<ISensors>& getSensors() { return mEnvironment->mSensors; } |
| 341 | |
| 342 | inline SensorsAidlEnvironment* getEnvironment() { return mEnvironment; } |
| 343 | |
| 344 | inline bool isValidType(SensorType sensorType) { return (int)sensorType > 0; } |
| 345 | |
| 346 | std::vector<SensorInfo> getSensorsList(); |
| 347 | |
| 348 | int32_t getInvalidSensorHandle() { |
| 349 | // Find a sensor handle that does not exist in the sensor list |
| 350 | int32_t maxHandle = 0; |
| 351 | for (const SensorInfo& sensor : getSensorsList()) { |
| 352 | maxHandle = std::max(maxHandle, sensor.sensorHandle); |
| 353 | } |
| 354 | return maxHandle + 1; |
| 355 | } |
| 356 | |
| 357 | ndk::ScopedAStatus activate(int32_t sensorHandle, bool enable); |
| 358 | void activateAllSensors(bool enable); |
| 359 | |
| 360 | ndk::ScopedAStatus batch(int32_t sensorHandle, int64_t samplingPeriodNs, |
| 361 | int64_t maxReportLatencyNs) { |
| 362 | return getSensors()->batch(sensorHandle, samplingPeriodNs, maxReportLatencyNs); |
| 363 | } |
| 364 | |
| 365 | ndk::ScopedAStatus flush(int32_t sensorHandle) { return getSensors()->flush(sensorHandle); } |
| 366 | |
Grace Cheng | 629b3a4 | 2022-01-06 12:19:17 +0000 | [diff] [blame] | 367 | ndk::ScopedAStatus registerDirectChannel(const ISensors::SharedMemInfo& mem, |
| 368 | int32_t* aidlReturn); |
| 369 | |
| 370 | ndk::ScopedAStatus unregisterDirectChannel(int32_t* channelHandle) { |
| 371 | return getSensors()->unregisterDirectChannel(*channelHandle); |
| 372 | } |
| 373 | |
| 374 | ndk::ScopedAStatus configDirectReport(int32_t sensorHandle, int32_t channelHandle, |
| 375 | ISensors::RateLevel rate, int32_t* reportToken) { |
| 376 | return getSensors()->configDirectReport(sensorHandle, channelHandle, rate, reportToken); |
| 377 | } |
| 378 | |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 379 | void runSingleFlushTest(const std::vector<SensorInfo>& sensors, bool activateSensor, |
| 380 | int32_t expectedFlushCount, bool expectedResult); |
| 381 | |
| 382 | void runFlushTest(const std::vector<SensorInfo>& sensors, bool activateSensor, |
| 383 | int32_t flushCalls, int32_t expectedFlushCount, bool expectedResult); |
| 384 | |
| 385 | inline static int32_t extractReportMode(int32_t flag) { |
| 386 | return (flag & (SensorInfo::SENSOR_FLAG_BITS_CONTINUOUS_MODE | |
| 387 | SensorInfo::SENSOR_FLAG_BITS_ON_CHANGE_MODE | |
| 388 | SensorInfo::SENSOR_FLAG_BITS_ONE_SHOT_MODE | |
| 389 | SensorInfo::SENSOR_FLAG_BITS_SPECIAL_REPORTING_MODE)); |
| 390 | } |
| 391 | |
| 392 | // All sensors and direct channnels used |
| 393 | std::unordered_set<int32_t> mSensorHandles; |
| 394 | std::unordered_set<int32_t> mDirectChannelHandles; |
| 395 | |
| 396 | private: |
| 397 | SensorsAidlEnvironment* mEnvironment; |
| 398 | }; |
| 399 | |
Grace Cheng | 629b3a4 | 2022-01-06 12:19:17 +0000 | [diff] [blame] | 400 | ndk::ScopedAStatus SensorsAidlTest::registerDirectChannel(const ISensors::SharedMemInfo& mem, |
| 401 | int32_t* aidlReturn) { |
| 402 | // If registeration of a channel succeeds, add the handle of channel to a set so that it can be |
| 403 | // unregistered when test fails. Unregister a channel does not remove the handle on purpose. |
| 404 | // Unregistering a channel more than once should not have negative effect. |
| 405 | |
| 406 | ndk::ScopedAStatus status = getSensors()->registerDirectChannel(mem, aidlReturn); |
| 407 | if (status.isOk()) { |
| 408 | mDirectChannelHandles.insert(*aidlReturn); |
| 409 | } |
| 410 | return status; |
| 411 | } |
| 412 | |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 413 | std::vector<SensorInfo> SensorsAidlTest::getSensorsList() { |
| 414 | std::vector<SensorInfo> sensorInfoList; |
| 415 | checkIsOk(getSensors()->getSensorsList(&sensorInfoList)); |
| 416 | return sensorInfoList; |
| 417 | } |
| 418 | |
| 419 | ndk::ScopedAStatus SensorsAidlTest::activate(int32_t sensorHandle, bool enable) { |
| 420 | // If activating a sensor, add the handle in a set so that when test fails it can be turned off. |
| 421 | // The handle is not removed when it is deactivating on purpose so that it is not necessary to |
| 422 | // check the return value of deactivation. Deactivating a sensor more than once does not have |
| 423 | // negative effect. |
| 424 | if (enable) { |
| 425 | mSensorHandles.insert(sensorHandle); |
| 426 | } |
| 427 | return getSensors()->activate(sensorHandle, enable); |
| 428 | } |
| 429 | |
| 430 | void SensorsAidlTest::activateAllSensors(bool enable) { |
| 431 | for (const SensorInfo& sensorInfo : getSensorsList()) { |
| 432 | if (isValidType(sensorInfo.type)) { |
| 433 | checkIsOk(batch(sensorInfo.sensorHandle, sensorInfo.minDelayUs, |
| 434 | 0 /* maxReportLatencyNs */)); |
| 435 | checkIsOk(activate(sensorInfo.sensorHandle, enable)); |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | std::vector<SensorInfo> SensorsAidlTest::getNonOneShotSensors() { |
| 441 | std::vector<SensorInfo> sensors; |
| 442 | for (const SensorInfo& info : getSensorsList()) { |
| 443 | if (extractReportMode(info.flags) != SensorInfo::SENSOR_FLAG_BITS_ONE_SHOT_MODE) { |
| 444 | sensors.push_back(info); |
| 445 | } |
| 446 | } |
| 447 | return sensors; |
| 448 | } |
| 449 | |
| 450 | std::vector<SensorInfo> SensorsAidlTest::getNonOneShotAndNonSpecialSensors() { |
| 451 | std::vector<SensorInfo> sensors; |
| 452 | for (const SensorInfo& info : getSensorsList()) { |
| 453 | int reportMode = extractReportMode(info.flags); |
| 454 | if (reportMode != SensorInfo::SENSOR_FLAG_BITS_ONE_SHOT_MODE && |
| 455 | reportMode != SensorInfo::SENSOR_FLAG_BITS_SPECIAL_REPORTING_MODE) { |
| 456 | sensors.push_back(info); |
| 457 | } |
| 458 | } |
| 459 | return sensors; |
| 460 | } |
| 461 | |
| 462 | std::vector<SensorInfo> SensorsAidlTest::getNonOneShotAndNonOnChangeAndNonSpecialSensors() { |
| 463 | std::vector<SensorInfo> sensors; |
| 464 | for (const SensorInfo& info : getSensorsList()) { |
| 465 | int reportMode = extractReportMode(info.flags); |
| 466 | if (reportMode != SensorInfo::SENSOR_FLAG_BITS_ONE_SHOT_MODE && |
| 467 | reportMode != SensorInfo::SENSOR_FLAG_BITS_ON_CHANGE_MODE && |
| 468 | reportMode != SensorInfo::SENSOR_FLAG_BITS_SPECIAL_REPORTING_MODE) { |
| 469 | sensors.push_back(info); |
| 470 | } |
| 471 | } |
| 472 | return sensors; |
| 473 | } |
| 474 | |
| 475 | std::vector<SensorInfo> SensorsAidlTest::getOneShotSensors() { |
| 476 | std::vector<SensorInfo> sensors; |
| 477 | for (const SensorInfo& info : getSensorsList()) { |
| 478 | if (extractReportMode(info.flags) == SensorInfo::SENSOR_FLAG_BITS_ONE_SHOT_MODE) { |
| 479 | sensors.push_back(info); |
| 480 | } |
| 481 | } |
| 482 | return sensors; |
| 483 | } |
| 484 | |
| 485 | std::vector<SensorInfo> SensorsAidlTest::getInjectEventSensors() { |
| 486 | std::vector<SensorInfo> out; |
| 487 | std::vector<SensorInfo> sensorInfoList = getSensorsList(); |
| 488 | for (const SensorInfo& info : sensorInfoList) { |
| 489 | if (info.flags & SensorInfo::SENSOR_FLAG_BITS_DATA_INJECTION) { |
| 490 | out.push_back(info); |
| 491 | } |
| 492 | } |
| 493 | return out; |
| 494 | } |
| 495 | |
| 496 | void SensorsAidlTest::runSingleFlushTest(const std::vector<SensorInfo>& sensors, |
| 497 | bool activateSensor, int32_t expectedFlushCount, |
| 498 | bool expectedResult) { |
| 499 | runFlushTest(sensors, activateSensor, 1 /* flushCalls */, expectedFlushCount, expectedResult); |
| 500 | } |
| 501 | |
| 502 | void SensorsAidlTest::runFlushTest(const std::vector<SensorInfo>& sensors, bool activateSensor, |
| 503 | int32_t flushCalls, int32_t expectedFlushCount, |
| 504 | bool expectedResult) { |
| 505 | EventCallback callback; |
| 506 | getEnvironment()->registerCallback(&callback); |
| 507 | |
| 508 | for (const SensorInfo& sensor : sensors) { |
| 509 | // Configure and activate the sensor |
| 510 | batch(sensor.sensorHandle, sensor.maxDelayUs, 0 /* maxReportLatencyNs */); |
| 511 | activate(sensor.sensorHandle, activateSensor); |
| 512 | |
| 513 | // Flush the sensor |
| 514 | for (int32_t i = 0; i < flushCalls; i++) { |
| 515 | SCOPED_TRACE(::testing::Message() |
| 516 | << "Flush " << i << "/" << flushCalls << ": " |
| 517 | << " handle=0x" << std::hex << std::setw(8) << std::setfill('0') |
| 518 | << sensor.sensorHandle << std::dec |
| 519 | << " type=" << static_cast<int>(sensor.type) << " name=" << sensor.name); |
| 520 | |
| 521 | EXPECT_EQ(flush(sensor.sensorHandle).isOk(), expectedResult); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | // Wait up to one second for the flush events |
| 526 | callback.waitForFlushEvents(sensors, flushCalls, std::chrono::milliseconds(1000) /* timeout */); |
| 527 | |
| 528 | // Deactivate all sensors after waiting for flush events so pending flush events are not |
| 529 | // abandoned by the HAL. |
| 530 | for (const SensorInfo& sensor : sensors) { |
| 531 | activate(sensor.sensorHandle, false); |
| 532 | } |
| 533 | getEnvironment()->unregisterCallback(); |
| 534 | |
| 535 | // Check that the correct number of flushes are present for each sensor |
| 536 | for (const SensorInfo& sensor : sensors) { |
| 537 | SCOPED_TRACE(::testing::Message() |
| 538 | << " handle=0x" << std::hex << std::setw(8) << std::setfill('0') |
| 539 | << sensor.sensorHandle << std::dec << " type=" << static_cast<int>(sensor.type) |
| 540 | << " name=" << sensor.name); |
| 541 | ASSERT_EQ(callback.getFlushCount(sensor.sensorHandle), expectedFlushCount); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | TEST_P(SensorsAidlTest, SensorListValid) { |
| 546 | std::vector<SensorInfo> sensorInfoList = getSensorsList(); |
| 547 | std::unordered_map<int32_t, std::vector<std::string>> sensorTypeNameMap; |
| 548 | for (size_t i = 0; i < sensorInfoList.size(); ++i) { |
| 549 | const SensorInfo& info = sensorInfoList[i]; |
| 550 | SCOPED_TRACE(::testing::Message() |
| 551 | << i << "/" << sensorInfoList.size() << ": " |
| 552 | << " handle=0x" << std::hex << std::setw(8) << std::setfill('0') |
| 553 | << info.sensorHandle << std::dec << " type=" << static_cast<int>(info.type) |
| 554 | << " name=" << info.name); |
| 555 | |
| 556 | // Test type string non-empty only for private sensor typeinfo. |
| 557 | if (info.type >= SensorType::DEVICE_PRIVATE_BASE) { |
| 558 | EXPECT_FALSE(info.typeAsString.empty()); |
| 559 | } else if (!info.typeAsString.empty()) { |
| 560 | // Test type string matches framework string if specified for non-private typeinfo. |
| 561 | EXPECT_NO_FATAL_FAILURE(assertTypeMatchStringType(info.type, info.typeAsString)); |
| 562 | } |
| 563 | |
| 564 | // Test if all sensor has name and vendor |
| 565 | EXPECT_FALSE(info.name.empty()); |
| 566 | EXPECT_FALSE(info.vendor.empty()); |
| 567 | |
| 568 | // Make sure that sensors of the same type have a unique name. |
| 569 | std::vector<std::string>& v = sensorTypeNameMap[static_cast<int32_t>(info.type)]; |
| 570 | bool isUniqueName = std::find(v.begin(), v.end(), info.name) == v.end(); |
| 571 | EXPECT_TRUE(isUniqueName) << "Duplicate sensor Name: " << info.name; |
| 572 | if (isUniqueName) { |
| 573 | v.push_back(info.name); |
| 574 | } |
| 575 | |
| 576 | EXPECT_LE(0, info.power); |
| 577 | EXPECT_LT(0, info.maxRange); |
| 578 | |
| 579 | // Info type, should have no sensor |
| 580 | EXPECT_FALSE(info.type == SensorType::ADDITIONAL_INFO || |
| 581 | info.type == SensorType::META_DATA); |
| 582 | |
| 583 | EXPECT_GE(info.fifoMaxEventCount, info.fifoReservedEventCount); |
| 584 | |
| 585 | // Test Reporting mode valid |
| 586 | EXPECT_NO_FATAL_FAILURE( |
| 587 | assertTypeMatchReportMode(info.type, extractReportMode(info.flags))); |
| 588 | |
| 589 | // Test min max are in the right order |
| 590 | EXPECT_LE(info.minDelayUs, info.maxDelayUs); |
| 591 | // Test min/max delay matches reporting mode |
| 592 | EXPECT_NO_FATAL_FAILURE(assertDelayMatchReportMode(info.minDelayUs, info.maxDelayUs, |
| 593 | extractReportMode(info.flags))); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | TEST_P(SensorsAidlTest, SetOperationMode) { |
| 598 | if (getInjectEventSensors().size() > 0) { |
| 599 | ASSERT_TRUE(getSensors()->setOperationMode(ISensors::OperationMode::NORMAL).isOk()); |
| 600 | ASSERT_TRUE(getSensors()->setOperationMode(ISensors::OperationMode::DATA_INJECTION).isOk()); |
| 601 | ASSERT_TRUE(getSensors()->setOperationMode(ISensors::OperationMode::NORMAL).isOk()); |
| 602 | } else { |
Arthur Ishiguro | e9cb293 | 2022-04-12 15:43:51 +0000 | [diff] [blame] | 603 | int errorCode = |
| 604 | getSensors() |
| 605 | ->setOperationMode(ISensors::OperationMode::DATA_INJECTION) |
| 606 | .getExceptionCode(); |
| 607 | ASSERT_TRUE((errorCode == EX_UNSUPPORTED_OPERATION) || |
| 608 | (errorCode == EX_ILLEGAL_ARGUMENT)); |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
| 612 | TEST_P(SensorsAidlTest, InjectSensorEventData) { |
| 613 | std::vector<SensorInfo> sensors = getInjectEventSensors(); |
| 614 | if (sensors.size() == 0) { |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | ASSERT_TRUE(getSensors()->setOperationMode(ISensors::OperationMode::DATA_INJECTION).isOk()); |
| 619 | |
| 620 | EventCallback callback; |
| 621 | getEnvironment()->registerCallback(&callback); |
| 622 | |
| 623 | // AdditionalInfo event should not be sent to Event FMQ |
| 624 | Event additionalInfoEvent; |
| 625 | additionalInfoEvent.sensorType = SensorType::ADDITIONAL_INFO; |
| 626 | additionalInfoEvent.timestamp = android::elapsedRealtimeNano(); |
| 627 | |
| 628 | Event injectedEvent; |
| 629 | injectedEvent.timestamp = android::elapsedRealtimeNano(); |
| 630 | Event::EventPayload::Vec3 data = {1, 2, 3, SensorStatus::ACCURACY_HIGH}; |
| 631 | injectedEvent.payload.set<Event::EventPayload::Tag::vec3>(data); |
| 632 | |
| 633 | for (const auto& s : sensors) { |
| 634 | additionalInfoEvent.sensorHandle = s.sensorHandle; |
| 635 | ASSERT_TRUE(getSensors()->injectSensorData(additionalInfoEvent).isOk()); |
| 636 | |
| 637 | injectedEvent.sensorType = s.type; |
| 638 | injectedEvent.sensorHandle = s.sensorHandle; |
| 639 | ASSERT_TRUE(getSensors()->injectSensorData(injectedEvent).isOk()); |
| 640 | } |
| 641 | |
| 642 | // Wait for events to be written back to the Event FMQ |
| 643 | callback.waitForEvents(sensors, std::chrono::milliseconds(1000) /* timeout */); |
| 644 | getEnvironment()->unregisterCallback(); |
| 645 | |
| 646 | for (const auto& s : sensors) { |
| 647 | auto events = callback.getEvents(s.sensorHandle); |
| 648 | if (events.empty()) { |
| 649 | FAIL() << "Received no events"; |
| 650 | } else { |
| 651 | auto lastEvent = events.back(); |
| 652 | SCOPED_TRACE(::testing::Message() |
| 653 | << " handle=0x" << std::hex << std::setw(8) << std::setfill('0') |
| 654 | << s.sensorHandle << std::dec << " type=" << static_cast<int>(s.type) |
| 655 | << " name=" << s.name); |
| 656 | |
| 657 | // Verify that only a single event has been received |
| 658 | ASSERT_EQ(events.size(), 1); |
| 659 | |
| 660 | // Verify that the event received matches the event injected and is not the additional |
| 661 | // info event |
| 662 | ASSERT_EQ(lastEvent.sensorType, s.type); |
| 663 | ASSERT_EQ(lastEvent.timestamp, injectedEvent.timestamp); |
| 664 | ASSERT_EQ(lastEvent.payload.get<Event::EventPayload::Tag::vec3>().x, |
| 665 | injectedEvent.payload.get<Event::EventPayload::Tag::vec3>().x); |
| 666 | ASSERT_EQ(lastEvent.payload.get<Event::EventPayload::Tag::vec3>().y, |
| 667 | injectedEvent.payload.get<Event::EventPayload::Tag::vec3>().y); |
| 668 | ASSERT_EQ(lastEvent.payload.get<Event::EventPayload::Tag::vec3>().z, |
| 669 | injectedEvent.payload.get<Event::EventPayload::Tag::vec3>().z); |
| 670 | ASSERT_EQ(lastEvent.payload.get<Event::EventPayload::Tag::vec3>().status, |
| 671 | injectedEvent.payload.get<Event::EventPayload::Tag::vec3>().status); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | ASSERT_TRUE(getSensors()->setOperationMode(ISensors::OperationMode::NORMAL).isOk()); |
| 676 | } |
| 677 | |
| 678 | TEST_P(SensorsAidlTest, CallInitializeTwice) { |
| 679 | // Create a helper class so that a second environment is able to be instantiated |
| 680 | class SensorsAidlEnvironmentTest : public SensorsAidlEnvironment { |
| 681 | public: |
| 682 | SensorsAidlEnvironmentTest(const std::string& service_name) |
| 683 | : SensorsAidlEnvironment(service_name) {} |
| 684 | }; |
| 685 | |
| 686 | if (getSensorsList().size() == 0) { |
| 687 | // No sensors |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | constexpr useconds_t kCollectionTimeoutUs = 1000 * 1000; // 1s |
| 692 | constexpr int32_t kNumEvents = 1; |
| 693 | |
| 694 | // Create a new environment that calls initialize() |
| 695 | std::unique_ptr<SensorsAidlEnvironmentTest> newEnv = |
| 696 | std::make_unique<SensorsAidlEnvironmentTest>(GetParam()); |
| 697 | newEnv->SetUp(); |
| 698 | if (HasFatalFailure()) { |
| 699 | return; // Exit early if setting up the new environment failed |
| 700 | } |
| 701 | |
| 702 | activateAllSensors(true); |
| 703 | // Verify that the old environment does not receive any events |
| 704 | EXPECT_EQ(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), 0); |
| 705 | // Verify that the new event queue receives sensor events |
| 706 | EXPECT_GE(newEnv.get()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents); |
| 707 | activateAllSensors(false); |
| 708 | |
| 709 | // Cleanup the test environment |
| 710 | newEnv->TearDown(); |
| 711 | |
| 712 | // Restore the test environment for future tests |
| 713 | getEnvironment()->TearDown(); |
| 714 | getEnvironment()->SetUp(); |
| 715 | if (HasFatalFailure()) { |
| 716 | return; // Exit early if resetting the environment failed |
| 717 | } |
| 718 | |
| 719 | // Ensure that the original environment is receiving events |
| 720 | activateAllSensors(true); |
| 721 | EXPECT_GE(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents); |
| 722 | activateAllSensors(false); |
| 723 | } |
| 724 | |
| 725 | TEST_P(SensorsAidlTest, CleanupConnectionsOnInitialize) { |
| 726 | activateAllSensors(true); |
| 727 | |
| 728 | // Verify that events are received |
| 729 | constexpr useconds_t kCollectionTimeoutUs = 1000 * 1000; // 1s |
| 730 | constexpr int32_t kNumEvents = 1; |
| 731 | ASSERT_GE(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents); |
| 732 | |
| 733 | // Clear the active sensor handles so they are not disabled during TearDown |
| 734 | auto handles = mSensorHandles; |
| 735 | mSensorHandles.clear(); |
| 736 | getEnvironment()->TearDown(); |
| 737 | getEnvironment()->SetUp(); |
| 738 | if (HasFatalFailure()) { |
| 739 | return; // Exit early if resetting the environment failed |
| 740 | } |
| 741 | |
| 742 | // Verify no events are received until sensors are re-activated |
| 743 | ASSERT_EQ(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), 0); |
| 744 | activateAllSensors(true); |
| 745 | ASSERT_GE(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents); |
| 746 | |
| 747 | // Disable sensors |
| 748 | activateAllSensors(false); |
| 749 | |
| 750 | // Restore active sensors prior to clearing the environment |
| 751 | mSensorHandles = handles; |
| 752 | } |
| 753 | |
| 754 | TEST_P(SensorsAidlTest, FlushSensor) { |
| 755 | std::vector<SensorInfo> sensors = getNonOneShotSensors(); |
| 756 | if (sensors.size() == 0) { |
| 757 | return; |
| 758 | } |
| 759 | |
| 760 | constexpr int32_t kFlushes = 5; |
| 761 | runSingleFlushTest(sensors, true /* activateSensor */, 1 /* expectedFlushCount */, |
| 762 | true /* expectedResult */); |
| 763 | runFlushTest(sensors, true /* activateSensor */, kFlushes, kFlushes, true /* expectedResult */); |
| 764 | } |
| 765 | |
| 766 | TEST_P(SensorsAidlTest, FlushOneShotSensor) { |
| 767 | // Find a sensor that is a one-shot sensor |
| 768 | std::vector<SensorInfo> sensors = getOneShotSensors(); |
| 769 | if (sensors.size() == 0) { |
| 770 | return; |
| 771 | } |
| 772 | |
| 773 | runSingleFlushTest(sensors, true /* activateSensor */, 0 /* expectedFlushCount */, |
| 774 | false /* expectedResult */); |
| 775 | } |
| 776 | |
| 777 | TEST_P(SensorsAidlTest, FlushInactiveSensor) { |
| 778 | // Attempt to find a non-one shot sensor, then a one-shot sensor if necessary |
| 779 | std::vector<SensorInfo> sensors = getNonOneShotSensors(); |
| 780 | if (sensors.size() == 0) { |
| 781 | sensors = getOneShotSensors(); |
| 782 | if (sensors.size() == 0) { |
| 783 | return; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | runSingleFlushTest(sensors, false /* activateSensor */, 0 /* expectedFlushCount */, |
| 788 | false /* expectedResult */); |
| 789 | } |
| 790 | |
| 791 | TEST_P(SensorsAidlTest, Batch) { |
| 792 | if (getSensorsList().size() == 0) { |
| 793 | return; |
| 794 | } |
| 795 | |
| 796 | activateAllSensors(false /* enable */); |
| 797 | for (const SensorInfo& sensor : getSensorsList()) { |
| 798 | SCOPED_TRACE(::testing::Message() |
| 799 | << " handle=0x" << std::hex << std::setw(8) << std::setfill('0') |
| 800 | << sensor.sensorHandle << std::dec << " type=" << static_cast<int>(sensor.type) |
| 801 | << " name=" << sensor.name); |
| 802 | |
| 803 | // Call batch on inactive sensor |
| 804 | // One shot sensors have minDelay set to -1 which is an invalid |
| 805 | // parameter. Use 0 instead to avoid errors. |
| 806 | int64_t samplingPeriodNs = |
| 807 | extractReportMode(sensor.flags) == SensorInfo::SENSOR_FLAG_BITS_ONE_SHOT_MODE |
| 808 | ? 0 |
| 809 | : sensor.minDelayUs; |
| 810 | checkIsOk(batch(sensor.sensorHandle, samplingPeriodNs, 0 /* maxReportLatencyNs */)); |
| 811 | |
| 812 | // Activate the sensor |
| 813 | activate(sensor.sensorHandle, true /* enabled */); |
| 814 | |
| 815 | // Call batch on an active sensor |
| 816 | checkIsOk(batch(sensor.sensorHandle, sensor.maxDelayUs, 0 /* maxReportLatencyNs */)); |
| 817 | } |
| 818 | activateAllSensors(false /* enable */); |
| 819 | |
| 820 | // Call batch on an invalid sensor |
| 821 | SensorInfo sensor = getSensorsList().front(); |
| 822 | sensor.sensorHandle = getInvalidSensorHandle(); |
| 823 | ASSERT_EQ(batch(sensor.sensorHandle, sensor.minDelayUs, 0 /* maxReportLatencyNs */) |
| 824 | .getExceptionCode(), |
| 825 | EX_ILLEGAL_ARGUMENT); |
| 826 | } |
| 827 | |
| 828 | TEST_P(SensorsAidlTest, Activate) { |
| 829 | if (getSensorsList().size() == 0) { |
| 830 | return; |
| 831 | } |
| 832 | |
| 833 | // Verify that sensor events are generated when activate is called |
| 834 | for (const SensorInfo& sensor : getSensorsList()) { |
| 835 | SCOPED_TRACE(::testing::Message() |
| 836 | << " handle=0x" << std::hex << std::setw(8) << std::setfill('0') |
| 837 | << sensor.sensorHandle << std::dec << " type=" << static_cast<int>(sensor.type) |
| 838 | << " name=" << sensor.name); |
| 839 | |
| 840 | checkIsOk(batch(sensor.sensorHandle, sensor.minDelayUs, 0 /* maxReportLatencyNs */)); |
| 841 | checkIsOk(activate(sensor.sensorHandle, true)); |
| 842 | |
| 843 | // Call activate on a sensor that is already activated |
| 844 | checkIsOk(activate(sensor.sensorHandle, true)); |
| 845 | |
| 846 | // Deactivate the sensor |
| 847 | checkIsOk(activate(sensor.sensorHandle, false)); |
| 848 | |
| 849 | // Call deactivate on a sensor that is already deactivated |
| 850 | checkIsOk(activate(sensor.sensorHandle, false)); |
| 851 | } |
| 852 | |
| 853 | // Attempt to activate an invalid sensor |
| 854 | int32_t invalidHandle = getInvalidSensorHandle(); |
| 855 | ASSERT_EQ(activate(invalidHandle, true).getExceptionCode(), EX_ILLEGAL_ARGUMENT); |
| 856 | ASSERT_EQ(activate(invalidHandle, false).getExceptionCode(), EX_ILLEGAL_ARGUMENT); |
| 857 | } |
| 858 | |
| 859 | TEST_P(SensorsAidlTest, NoStaleEvents) { |
| 860 | constexpr std::chrono::milliseconds kFiveHundredMs(500); |
| 861 | constexpr std::chrono::milliseconds kOneSecond(1000); |
| 862 | |
| 863 | // Register the callback to receive sensor events |
| 864 | EventCallback callback; |
| 865 | getEnvironment()->registerCallback(&callback); |
| 866 | |
| 867 | // This test is not valid for one-shot, on-change or special-report-mode sensors |
| 868 | const std::vector<SensorInfo> sensors = getNonOneShotAndNonOnChangeAndNonSpecialSensors(); |
| 869 | std::chrono::milliseconds maxMinDelay(0); |
| 870 | for (const SensorInfo& sensor : sensors) { |
| 871 | std::chrono::milliseconds minDelay = duration_cast<std::chrono::milliseconds>( |
| 872 | std::chrono::microseconds(sensor.minDelayUs)); |
| 873 | maxMinDelay = std::chrono::milliseconds(std::max(maxMinDelay.count(), minDelay.count())); |
| 874 | } |
| 875 | |
| 876 | // Activate the sensors so that they start generating events |
| 877 | activateAllSensors(true); |
| 878 | |
| 879 | // According to the CDD, the first sample must be generated within 400ms + 2 * sample_time |
| 880 | // and the maximum reporting latency is 100ms + 2 * sample_time. Wait a sufficient amount |
| 881 | // of time to guarantee that a sample has arrived. |
| 882 | callback.waitForEvents(sensors, kFiveHundredMs + (5 * maxMinDelay)); |
| 883 | activateAllSensors(false); |
| 884 | |
| 885 | // Save the last received event for each sensor |
| 886 | std::map<int32_t, int64_t> lastEventTimestampMap; |
| 887 | for (const SensorInfo& sensor : sensors) { |
| 888 | SCOPED_TRACE(::testing::Message() |
| 889 | << " handle=0x" << std::hex << std::setw(8) << std::setfill('0') |
| 890 | << sensor.sensorHandle << std::dec << " type=" << static_cast<int>(sensor.type) |
| 891 | << " name=" << sensor.name); |
| 892 | |
| 893 | if (callback.getEvents(sensor.sensorHandle).size() >= 1) { |
| 894 | lastEventTimestampMap[sensor.sensorHandle] = |
| 895 | callback.getEvents(sensor.sensorHandle).back().timestamp; |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | // Allow some time to pass, reset the callback, then reactivate the sensors |
| 900 | usleep(duration_cast<std::chrono::microseconds>(kOneSecond + (5 * maxMinDelay)).count()); |
| 901 | callback.reset(); |
| 902 | activateAllSensors(true); |
| 903 | callback.waitForEvents(sensors, kFiveHundredMs + (5 * maxMinDelay)); |
| 904 | activateAllSensors(false); |
| 905 | |
| 906 | getEnvironment()->unregisterCallback(); |
| 907 | |
| 908 | for (const SensorInfo& sensor : sensors) { |
| 909 | SCOPED_TRACE(::testing::Message() |
| 910 | << " handle=0x" << std::hex << std::setw(8) << std::setfill('0') |
| 911 | << sensor.sensorHandle << std::dec << " type=" << static_cast<int>(sensor.type) |
| 912 | << " name=" << sensor.name); |
| 913 | |
| 914 | // Skip sensors that did not previously report an event |
| 915 | if (lastEventTimestampMap.find(sensor.sensorHandle) == lastEventTimestampMap.end()) { |
| 916 | continue; |
| 917 | } |
| 918 | |
| 919 | // Ensure that the first event received is not stale by ensuring that its timestamp is |
| 920 | // sufficiently different from the previous event |
| 921 | const Event newEvent = callback.getEvents(sensor.sensorHandle).front(); |
| 922 | std::chrono::milliseconds delta = |
| 923 | duration_cast<std::chrono::milliseconds>(std::chrono::nanoseconds( |
| 924 | newEvent.timestamp - lastEventTimestampMap[sensor.sensorHandle])); |
| 925 | std::chrono::milliseconds sensorMinDelay = duration_cast<std::chrono::milliseconds>( |
| 926 | std::chrono::microseconds(sensor.minDelayUs)); |
| 927 | ASSERT_GE(delta, kFiveHundredMs + (3 * sensorMinDelay)); |
| 928 | } |
| 929 | } |
| 930 | |
Grace Cheng | 629b3a4 | 2022-01-06 12:19:17 +0000 | [diff] [blame] | 931 | void SensorsAidlTest::checkRateLevel(const SensorInfo& sensor, int32_t directChannelHandle, |
| 932 | ISensors::RateLevel rateLevel, int32_t* reportToken) { |
| 933 | ndk::ScopedAStatus status = |
| 934 | configDirectReport(sensor.sensorHandle, directChannelHandle, rateLevel, reportToken); |
| 935 | |
| 936 | SCOPED_TRACE(::testing::Message() |
| 937 | << " handle=0x" << std::hex << std::setw(8) << std::setfill('0') |
| 938 | << sensor.sensorHandle << std::dec << " type=" << static_cast<int>(sensor.type) |
| 939 | << " name=" << sensor.name); |
| 940 | |
| 941 | if (isDirectReportRateSupported(sensor, rateLevel)) { |
| 942 | ASSERT_TRUE(status.isOk()); |
| 943 | if (rateLevel != ISensors::RateLevel::STOP) { |
Arthur Ishiguro | e9cb293 | 2022-04-12 15:43:51 +0000 | [diff] [blame] | 944 | ASSERT_GT(*reportToken, 0); |
Grace Cheng | 629b3a4 | 2022-01-06 12:19:17 +0000 | [diff] [blame] | 945 | } |
Arthur Ishiguro | e9cb293 | 2022-04-12 15:43:51 +0000 | [diff] [blame] | 946 | } else { |
| 947 | ASSERT_EQ(status.getExceptionCode(), EX_ILLEGAL_ARGUMENT); |
Grace Cheng | 629b3a4 | 2022-01-06 12:19:17 +0000 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | |
| 951 | void SensorsAidlTest::queryDirectChannelSupport(ISensors::SharedMemInfo::SharedMemType memType, |
| 952 | bool* supportsSharedMemType, |
| 953 | bool* supportsAnyDirectChannel) { |
| 954 | *supportsSharedMemType = false; |
| 955 | *supportsAnyDirectChannel = false; |
| 956 | for (const SensorInfo& curSensor : getSensorsList()) { |
| 957 | if (isDirectChannelTypeSupported(curSensor, memType)) { |
| 958 | *supportsSharedMemType = true; |
| 959 | } |
| 960 | if (isDirectChannelTypeSupported(curSensor, |
| 961 | ISensors::SharedMemInfo::SharedMemType::ASHMEM) || |
| 962 | isDirectChannelTypeSupported(curSensor, |
| 963 | ISensors::SharedMemInfo::SharedMemType::GRALLOC)) { |
| 964 | *supportsAnyDirectChannel = true; |
| 965 | } |
| 966 | |
| 967 | if (*supportsSharedMemType && *supportsAnyDirectChannel) { |
| 968 | break; |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | void SensorsAidlTest::verifyRegisterDirectChannel( |
| 974 | std::shared_ptr<SensorsAidlTestSharedMemory<SensorType, Event>> mem, |
| 975 | int32_t* directChannelHandle, bool supportsSharedMemType, bool supportsAnyDirectChannel) { |
| 976 | char* buffer = mem->getBuffer(); |
| 977 | size_t size = mem->getSize(); |
| 978 | |
| 979 | if (supportsSharedMemType) { |
| 980 | memset(buffer, 0xff, size); |
| 981 | } |
| 982 | |
| 983 | int32_t channelHandle; |
| 984 | |
| 985 | ::ndk::ScopedAStatus status = registerDirectChannel(mem->getSharedMemInfo(), &channelHandle); |
| 986 | if (supportsSharedMemType) { |
| 987 | ASSERT_TRUE(status.isOk()); |
Arthur Ishiguro | e9cb293 | 2022-04-12 15:43:51 +0000 | [diff] [blame] | 988 | ASSERT_GT(channelHandle, 0); |
| 989 | |
| 990 | // Verify that the memory has been zeroed |
| 991 | for (size_t i = 0; i < mem->getSize(); i++) { |
| 992 | ASSERT_EQ(buffer[i], 0x00); |
| 993 | } |
Grace Cheng | 629b3a4 | 2022-01-06 12:19:17 +0000 | [diff] [blame] | 994 | } else { |
| 995 | int32_t error = supportsAnyDirectChannel ? EX_ILLEGAL_ARGUMENT : EX_UNSUPPORTED_OPERATION; |
| 996 | ASSERT_EQ(status.getExceptionCode(), error); |
Grace Cheng | 629b3a4 | 2022-01-06 12:19:17 +0000 | [diff] [blame] | 997 | } |
Greg Kaiser | aae3061 | 2022-01-14 07:54:49 -0800 | [diff] [blame] | 998 | *directChannelHandle = channelHandle; |
Grace Cheng | 629b3a4 | 2022-01-06 12:19:17 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | void SensorsAidlTest::verifyUnregisterDirectChannel(int32_t* channelHandle, |
| 1002 | bool supportsAnyDirectChannel) { |
| 1003 | int result = supportsAnyDirectChannel ? EX_NONE : EX_UNSUPPORTED_OPERATION; |
| 1004 | ndk::ScopedAStatus status = unregisterDirectChannel(channelHandle); |
| 1005 | ASSERT_EQ(status.getExceptionCode(), result); |
| 1006 | } |
| 1007 | |
| 1008 | void SensorsAidlTest::verifyDirectChannel(ISensors::SharedMemInfo::SharedMemType memType) { |
| 1009 | constexpr size_t kNumEvents = 1; |
| 1010 | constexpr size_t kMemSize = kNumEvents * kEventSize; |
| 1011 | |
| 1012 | std::shared_ptr<SensorsAidlTestSharedMemory<SensorType, Event>> mem( |
| 1013 | SensorsAidlTestSharedMemory<SensorType, Event>::create(memType, kMemSize)); |
| 1014 | ASSERT_NE(mem, nullptr); |
| 1015 | |
| 1016 | bool supportsSharedMemType; |
| 1017 | bool supportsAnyDirectChannel; |
| 1018 | queryDirectChannelSupport(memType, &supportsSharedMemType, &supportsAnyDirectChannel); |
| 1019 | |
| 1020 | for (const SensorInfo& sensor : getSensorsList()) { |
| 1021 | int32_t directChannelHandle = 0; |
| 1022 | verifyRegisterDirectChannel(mem, &directChannelHandle, supportsSharedMemType, |
| 1023 | supportsAnyDirectChannel); |
| 1024 | verifyConfigure(sensor, memType, directChannelHandle, supportsAnyDirectChannel); |
| 1025 | verifyUnregisterDirectChannel(&directChannelHandle, supportsAnyDirectChannel); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | void SensorsAidlTest::verifyConfigure(const SensorInfo& sensor, |
| 1030 | ISensors::SharedMemInfo::SharedMemType memType, |
| 1031 | int32_t directChannelHandle, bool supportsAnyDirectChannel) { |
| 1032 | SCOPED_TRACE(::testing::Message() |
| 1033 | << " handle=0x" << std::hex << std::setw(8) << std::setfill('0') |
| 1034 | << sensor.sensorHandle << std::dec << " type=" << static_cast<int>(sensor.type) |
| 1035 | << " name=" << sensor.name); |
| 1036 | |
| 1037 | int32_t reportToken = 0; |
| 1038 | if (isDirectChannelTypeSupported(sensor, memType)) { |
| 1039 | // Verify that each rate level is properly supported |
| 1040 | checkRateLevel(sensor, directChannelHandle, ISensors::RateLevel::NORMAL, &reportToken); |
| 1041 | checkRateLevel(sensor, directChannelHandle, ISensors::RateLevel::FAST, &reportToken); |
| 1042 | checkRateLevel(sensor, directChannelHandle, ISensors::RateLevel::VERY_FAST, &reportToken); |
| 1043 | checkRateLevel(sensor, directChannelHandle, ISensors::RateLevel::STOP, &reportToken); |
| 1044 | |
| 1045 | // Verify that a sensor handle of -1 is only acceptable when using RateLevel::STOP |
| 1046 | ndk::ScopedAStatus status = configDirectReport(-1 /* sensorHandle */, directChannelHandle, |
| 1047 | ISensors::RateLevel::NORMAL, &reportToken); |
Arthur Ishiguro | e9cb293 | 2022-04-12 15:43:51 +0000 | [diff] [blame] | 1048 | ASSERT_EQ(status.getExceptionCode(), EX_ILLEGAL_ARGUMENT); |
Grace Cheng | 629b3a4 | 2022-01-06 12:19:17 +0000 | [diff] [blame] | 1049 | |
| 1050 | status = configDirectReport(-1 /* sensorHandle */, directChannelHandle, |
| 1051 | ISensors::RateLevel::STOP, &reportToken); |
| 1052 | ASSERT_TRUE(status.isOk()); |
| 1053 | } else { |
| 1054 | // directChannelHandle will be -1 here, HAL should either reject it as a bad value if there |
| 1055 | // is some level of direct channel report, otherwise return INVALID_OPERATION if direct |
| 1056 | // channel is not supported at all |
| 1057 | int error = supportsAnyDirectChannel ? EX_ILLEGAL_ARGUMENT : EX_UNSUPPORTED_OPERATION; |
| 1058 | ndk::ScopedAStatus status = configDirectReport(sensor.sensorHandle, directChannelHandle, |
| 1059 | ISensors::RateLevel::NORMAL, &reportToken); |
| 1060 | ASSERT_EQ(status.getExceptionCode(), error); |
| 1061 | } |
| 1062 | } |
| 1063 | |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 1064 | TEST_P(SensorsAidlTest, DirectChannelAshmem) { |
Grace Cheng | 629b3a4 | 2022-01-06 12:19:17 +0000 | [diff] [blame] | 1065 | verifyDirectChannel(ISensors::SharedMemInfo::SharedMemType::ASHMEM); |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | TEST_P(SensorsAidlTest, DirectChannelGralloc) { |
Grace Cheng | 629b3a4 | 2022-01-06 12:19:17 +0000 | [diff] [blame] | 1069 | verifyDirectChannel(ISensors::SharedMemInfo::SharedMemType::GRALLOC); |
Arthur Ishiguro | c7ac0b2 | 2021-10-13 16:12:37 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SensorsAidlTest); |
| 1073 | INSTANTIATE_TEST_SUITE_P(Sensors, SensorsAidlTest, |
| 1074 | testing::ValuesIn(android::getAidlHalInstanceNames(ISensors::descriptor)), |
| 1075 | android::PrintInstanceNameToString); |
| 1076 | |
| 1077 | int main(int argc, char** argv) { |
| 1078 | ::testing::InitGoogleTest(&argc, argv); |
| 1079 | ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 1080 | ProcessState::self()->startThreadPool(); |
| 1081 | return RUN_ALL_TESTS(); |
| 1082 | } |