1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define LOG_TAG "sensors_hidl_hal_test" 18 19 #include "SensorsHidlEnvironmentV1_0.h" 20 #include "sensors-vts-utils/SensorsHidlTestBase.h" 21 22 #include <android/hardware/sensors/1.0/ISensors.h> 23 #include <android/hardware/sensors/1.0/types.h> 24 #include <log/log.h> 25 #include <utils/SystemClock.h> 26 27 #include <cinttypes> 28 #include <vector> 29 30 using ::android::hardware::Return; 31 using ::android::hardware::Void; 32 using ::android::sp; 33 using namespace ::android::hardware::sensors::V1_0; 34 35 // The main test class for SENSORS HIDL HAL. 36 37 class SensorsHidlTest : public SensorsHidlTestBase { 38 protected: 39 SensorInfo defaultSensorByType(SensorType type) override; 40 std::vector<SensorInfo> getSensorsList(); 41 // implementation wrapper 42 Return<void> getSensorsList(ISensors::getSensorsList_cb _hidl_cb) override { 43 return S()->getSensorsList(_hidl_cb); 44 } 45 46 Return<Result> activate(int32_t sensorHandle, bool enabled) override; 47 48 Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs, 49 int64_t maxReportLatencyNs) override { 50 return S()->batch(sensorHandle, samplingPeriodNs, maxReportLatencyNs); 51 } 52 53 Return<Result> flush(int32_t sensorHandle) override { return S()->flush(sensorHandle); } 54 55 Return<Result> injectSensorData(const Event& event) override { 56 return S()->injectSensorData(event); 57 } 58 59 Return<void> registerDirectChannel(const SharedMemInfo& mem, 60 ISensors::registerDirectChannel_cb _hidl_cb) override; 61 62 Return<Result> unregisterDirectChannel(int32_t channelHandle) override { 63 return S()->unregisterDirectChannel(channelHandle); 64 } 65 66 Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate, 67 ISensors::configDirectReport_cb _hidl_cb) override { 68 return S()->configDirectReport(sensorHandle, channelHandle, rate, _hidl_cb); 69 } 70 71 inline sp<ISensors>& S() { return SensorsHidlEnvironmentV1_0::Instance()->sensors; } 72 73 SensorsHidlEnvironmentBase* getEnvironment() override { 74 return SensorsHidlEnvironmentV1_0::Instance(); 75 } 76 }; 77 78 Return<Result> SensorsHidlTest::activate(int32_t sensorHandle, bool enabled) { 79 // If activating a sensor, add the handle in a set so that when test fails it can be turned off. 80 // The handle is not removed when it is deactivating on purpose so that it is not necessary to 81 // check the return value of deactivation. Deactivating a sensor more than once does not have 82 // negative effect. 83 if (enabled) { 84 mSensorHandles.insert(sensorHandle); 85 } 86 return S()->activate(sensorHandle, enabled); 87 } 88 89 Return<void> SensorsHidlTest::registerDirectChannel( 90 const SharedMemInfo& mem, ISensors::registerDirectChannel_cb cb) { 91 // If registeration of a channel succeeds, add the handle of channel to a set so that it can be 92 // unregistered when test fails. Unregister a channel does not remove the handle on purpose. 93 // Unregistering a channel more than once should not have negative effect. 94 S()->registerDirectChannel(mem, 95 [&] (auto result, auto channelHandle) { 96 if (result == Result::OK) { 97 mDirectChannelHandles.insert(channelHandle); 98 } 99 cb(result, channelHandle); 100 }); 101 return Void(); 102 } 103 104 SensorInfo SensorsHidlTest::defaultSensorByType(SensorType type) { 105 SensorInfo ret; 106 107 ret.type = (SensorType) -1; 108 S()->getSensorsList( 109 [&] (const auto &list) { 110 const size_t count = list.size(); 111 for (size_t i = 0; i < count; ++i) { 112 if (list[i].type == type) { 113 ret = list[i]; 114 return; 115 } 116 } 117 }); 118 119 return ret; 120 } 121 122 std::vector<SensorInfo> SensorsHidlTest::getSensorsList() { 123 std::vector<SensorInfo> ret; 124 125 S()->getSensorsList( 126 [&] (const auto &list) { 127 const size_t count = list.size(); 128 ret.reserve(list.size()); 129 for (size_t i = 0; i < count; ++i) { 130 ret.push_back(list[i]); 131 } 132 }); 133 134 return ret; 135 } 136 137 // Test if sensor list returned is valid 138 TEST_F(SensorsHidlTest, SensorListValid) { 139 S()->getSensorsList( 140 [&] (const auto &list) { 141 const size_t count = list.size(); 142 for (size_t i = 0; i < count; ++i) { 143 const auto &s = list[i]; 144 SCOPED_TRACE(::testing::Message() << i << "/" << count << ": " 145 << " handle=0x" << std::hex << std::setw(8) << std::setfill('0') 146 << s.sensorHandle << std::dec 147 << " type=" << static_cast<int>(s.type) 148 << " name=" << s.name); 149 150 // Test non-empty type string 151 EXPECT_FALSE(s.typeAsString.empty()); 152 153 // Test defined type matches defined string type 154 EXPECT_NO_FATAL_FAILURE(assertTypeMatchStringType(s.type, s.typeAsString)); 155 156 // Test if all sensor has name and vendor 157 EXPECT_FALSE(s.name.empty()); 158 EXPECT_FALSE(s.vendor.empty()); 159 160 // Test power > 0, maxRange > 0 161 EXPECT_LE(0, s.power); 162 EXPECT_LT(0, s.maxRange); 163 164 // Info type, should have no sensor 165 EXPECT_FALSE( 166 s.type == SensorType::ADDITIONAL_INFO 167 || s.type == SensorType::META_DATA); 168 169 // Test fifoMax >= fifoReserved 170 EXPECT_GE(s.fifoMaxEventCount, s.fifoReservedEventCount) 171 << "max=" << s.fifoMaxEventCount << " reserved=" << s.fifoReservedEventCount; 172 173 // Test Reporting mode valid 174 EXPECT_NO_FATAL_FAILURE(assertTypeMatchReportMode(s.type, extractReportMode(s.flags))); 175 176 // Test min max are in the right order 177 EXPECT_LE(s.minDelay, s.maxDelay); 178 // Test min/max delay matches reporting mode 179 EXPECT_NO_FATAL_FAILURE( 180 assertDelayMatchReportMode(s.minDelay, s.maxDelay, extractReportMode(s.flags))); 181 } 182 }); 183 } 184 185 // Test if sensor list returned is valid 186 TEST_F(SensorsHidlTest, SetOperationMode) { 187 std::vector<SensorInfo> sensorList = getSensorsList(); 188 189 bool needOperationModeSupport = 190 std::any_of(sensorList.begin(), sensorList.end(), 191 [] (const auto& s) { 192 return (s.flags & SensorFlagBits::DATA_INJECTION) != 0; 193 }); 194 if (!needOperationModeSupport) { 195 return; 196 } 197 198 ASSERT_EQ(Result::OK, S()->setOperationMode(OperationMode::NORMAL)); 199 ASSERT_EQ(Result::OK, S()->setOperationMode(OperationMode::DATA_INJECTION)); 200 ASSERT_EQ(Result::OK, S()->setOperationMode(OperationMode::NORMAL)); 201 } 202 203 // Test if sensor list returned is valid 204 TEST_F(SensorsHidlTest, InjectSensorEventData) { 205 std::vector<SensorInfo> sensorList = getSensorsList(); 206 std::vector<SensorInfo> sensorSupportInjection; 207 208 bool needOperationModeSupport = 209 std::any_of(sensorList.begin(), sensorList.end(), 210 [&sensorSupportInjection] (const auto& s) { 211 bool ret = (s.flags & SensorFlagBits::DATA_INJECTION) != 0; 212 if (ret) { 213 sensorSupportInjection.push_back(s); 214 } 215 return ret; 216 }); 217 if (!needOperationModeSupport) { 218 return; 219 } 220 221 ASSERT_EQ(Result::OK, S()->setOperationMode(OperationMode::NORMAL)); 222 ASSERT_EQ(Result::OK, S()->setOperationMode(OperationMode::DATA_INJECTION)); 223 224 for (const auto &s : sensorSupportInjection) { 225 switch (s.type) { 226 case SensorType::ACCELEROMETER: 227 case SensorType::GYROSCOPE: 228 case SensorType::MAGNETIC_FIELD: { 229 usleep(100000); // sleep 100ms 230 231 Event dummy; 232 dummy.timestamp = android::elapsedRealtimeNano(); 233 dummy.sensorType = s.type; 234 dummy.sensorHandle = s.sensorHandle; 235 Vec3 v = {1, 2, 3, SensorStatus::ACCURACY_HIGH}; 236 dummy.u.vec3 = v; 237 238 EXPECT_EQ(Result::OK, S()->injectSensorData(dummy)); 239 break; 240 } 241 default: 242 break; 243 } 244 } 245 ASSERT_EQ(Result::OK, S()->setOperationMode(OperationMode::NORMAL)); 246 } 247 248 // Test if sensor hal can do UI speed accelerometer streaming properly 249 TEST_F(SensorsHidlTest, AccelerometerStreamingOperationSlow) { 250 testStreamingOperation(SensorType::ACCELEROMETER, 251 std::chrono::milliseconds(200), 252 std::chrono::seconds(5), 253 sAccelNormChecker); 254 } 255 256 // Test if sensor hal can do normal speed accelerometer streaming properly 257 TEST_F(SensorsHidlTest, AccelerometerStreamingOperationNormal) { 258 testStreamingOperation(SensorType::ACCELEROMETER, 259 std::chrono::milliseconds(20), 260 std::chrono::seconds(5), 261 sAccelNormChecker); 262 } 263 264 // Test if sensor hal can do game speed accelerometer streaming properly 265 TEST_F(SensorsHidlTest, AccelerometerStreamingOperationFast) { 266 testStreamingOperation(SensorType::ACCELEROMETER, 267 std::chrono::milliseconds(5), 268 std::chrono::seconds(5), 269 sAccelNormChecker); 270 } 271 272 // Test if sensor hal can do UI speed gyroscope streaming properly 273 TEST_F(SensorsHidlTest, GyroscopeStreamingOperationSlow) { 274 testStreamingOperation(SensorType::GYROSCOPE, 275 std::chrono::milliseconds(200), 276 std::chrono::seconds(5), 277 sGyroNormChecker); 278 } 279 280 // Test if sensor hal can do normal speed gyroscope streaming properly 281 TEST_F(SensorsHidlTest, GyroscopeStreamingOperationNormal) { 282 testStreamingOperation(SensorType::GYROSCOPE, 283 std::chrono::milliseconds(20), 284 std::chrono::seconds(5), 285 sGyroNormChecker); 286 } 287 288 // Test if sensor hal can do game speed gyroscope streaming properly 289 TEST_F(SensorsHidlTest, GyroscopeStreamingOperationFast) { 290 testStreamingOperation(SensorType::GYROSCOPE, 291 std::chrono::milliseconds(5), 292 std::chrono::seconds(5), 293 sGyroNormChecker); 294 } 295 296 // Test if sensor hal can do UI speed magnetometer streaming properly 297 TEST_F(SensorsHidlTest, MagnetometerStreamingOperationSlow) { 298 testStreamingOperation(SensorType::MAGNETIC_FIELD, 299 std::chrono::milliseconds(200), 300 std::chrono::seconds(5), 301 NullChecker()); 302 } 303 304 // Test if sensor hal can do normal speed magnetometer streaming properly 305 TEST_F(SensorsHidlTest, MagnetometerStreamingOperationNormal) { 306 testStreamingOperation(SensorType::MAGNETIC_FIELD, 307 std::chrono::milliseconds(20), 308 std::chrono::seconds(5), 309 NullChecker()); 310 } 311 312 // Test if sensor hal can do game speed magnetometer streaming properly 313 TEST_F(SensorsHidlTest, MagnetometerStreamingOperationFast) { 314 testStreamingOperation(SensorType::MAGNETIC_FIELD, 315 std::chrono::milliseconds(5), 316 std::chrono::seconds(5), 317 NullChecker()); 318 } 319 320 // Test if sensor hal can do accelerometer sampling rate switch properly when sensor is active 321 TEST_F(SensorsHidlTest, AccelerometerSamplingPeriodHotSwitchOperation) { 322 testSamplingRateHotSwitchOperation(SensorType::ACCELEROMETER); 323 testSamplingRateHotSwitchOperation(SensorType::ACCELEROMETER, false /*fastToSlow*/); 324 } 325 326 // Test if sensor hal can do gyroscope sampling rate switch properly when sensor is active 327 TEST_F(SensorsHidlTest, GyroscopeSamplingPeriodHotSwitchOperation) { 328 testSamplingRateHotSwitchOperation(SensorType::GYROSCOPE); 329 testSamplingRateHotSwitchOperation(SensorType::GYROSCOPE, false /*fastToSlow*/); 330 } 331 332 // Test if sensor hal can do magnetometer sampling rate switch properly when sensor is active 333 TEST_F(SensorsHidlTest, MagnetometerSamplingPeriodHotSwitchOperation) { 334 testSamplingRateHotSwitchOperation(SensorType::MAGNETIC_FIELD); 335 testSamplingRateHotSwitchOperation(SensorType::MAGNETIC_FIELD, false /*fastToSlow*/); 336 } 337 338 // Test if sensor hal can do accelerometer batching properly 339 TEST_F(SensorsHidlTest, AccelerometerBatchingOperation) { 340 testBatchingOperation(SensorType::ACCELEROMETER); 341 } 342 343 // Test if sensor hal can do gyroscope batching properly 344 TEST_F(SensorsHidlTest, GyroscopeBatchingOperation) { 345 testBatchingOperation(SensorType::GYROSCOPE); 346 } 347 348 // Test if sensor hal can do magnetometer batching properly 349 TEST_F(SensorsHidlTest, MagnetometerBatchingOperation) { 350 testBatchingOperation(SensorType::MAGNETIC_FIELD); 351 } 352 353 // Test sensor event direct report with ashmem for accel sensor at normal rate 354 TEST_F(SensorsHidlTest, AccelerometerAshmemDirectReportOperationNormal) { 355 testDirectReportOperation(SensorType::ACCELEROMETER, SharedMemType::ASHMEM, RateLevel::NORMAL, 356 sAccelNormChecker); 357 } 358 359 // Test sensor event direct report with ashmem for accel sensor at fast rate 360 TEST_F(SensorsHidlTest, AccelerometerAshmemDirectReportOperationFast) { 361 testDirectReportOperation(SensorType::ACCELEROMETER, SharedMemType::ASHMEM, RateLevel::FAST, 362 sAccelNormChecker); 363 } 364 365 // Test sensor event direct report with ashmem for accel sensor at very fast rate 366 TEST_F(SensorsHidlTest, AccelerometerAshmemDirectReportOperationVeryFast) { 367 testDirectReportOperation(SensorType::ACCELEROMETER, SharedMemType::ASHMEM, RateLevel::VERY_FAST, 368 sAccelNormChecker); 369 } 370 371 // Test sensor event direct report with ashmem for gyro sensor at normal rate 372 TEST_F(SensorsHidlTest, GyroscopeAshmemDirectReportOperationNormal) { 373 testDirectReportOperation(SensorType::GYROSCOPE, SharedMemType::ASHMEM, RateLevel::NORMAL, 374 sGyroNormChecker); 375 } 376 377 // Test sensor event direct report with ashmem for gyro sensor at fast rate 378 TEST_F(SensorsHidlTest, GyroscopeAshmemDirectReportOperationFast) { 379 testDirectReportOperation(SensorType::GYROSCOPE, SharedMemType::ASHMEM, RateLevel::FAST, 380 sGyroNormChecker); 381 } 382 383 // Test sensor event direct report with ashmem for gyro sensor at very fast rate 384 TEST_F(SensorsHidlTest, GyroscopeAshmemDirectReportOperationVeryFast) { 385 testDirectReportOperation(SensorType::GYROSCOPE, SharedMemType::ASHMEM, RateLevel::VERY_FAST, 386 sGyroNormChecker); 387 } 388 389 // Test sensor event direct report with ashmem for mag sensor at normal rate 390 TEST_F(SensorsHidlTest, MagnetometerAshmemDirectReportOperationNormal) { 391 testDirectReportOperation(SensorType::MAGNETIC_FIELD, SharedMemType::ASHMEM, RateLevel::NORMAL, 392 NullChecker()); 393 } 394 395 // Test sensor event direct report with ashmem for mag sensor at fast rate 396 TEST_F(SensorsHidlTest, MagnetometerAshmemDirectReportOperationFast) { 397 testDirectReportOperation(SensorType::MAGNETIC_FIELD, SharedMemType::ASHMEM, RateLevel::FAST, 398 NullChecker()); 399 } 400 401 // Test sensor event direct report with ashmem for mag sensor at very fast rate 402 TEST_F(SensorsHidlTest, MagnetometerAshmemDirectReportOperationVeryFast) { 403 testDirectReportOperation( 404 SensorType::MAGNETIC_FIELD, SharedMemType::ASHMEM, RateLevel::VERY_FAST, NullChecker()); 405 } 406 407 // Test sensor event direct report with gralloc for accel sensor at normal rate 408 TEST_F(SensorsHidlTest, AccelerometerGrallocDirectReportOperationNormal) { 409 testDirectReportOperation(SensorType::ACCELEROMETER, SharedMemType::GRALLOC, RateLevel::NORMAL, 410 sAccelNormChecker); 411 } 412 413 // Test sensor event direct report with gralloc for accel sensor at fast rate 414 TEST_F(SensorsHidlTest, AccelerometerGrallocDirectReportOperationFast) { 415 testDirectReportOperation(SensorType::ACCELEROMETER, SharedMemType::GRALLOC, RateLevel::FAST, 416 sAccelNormChecker); 417 } 418 419 // Test sensor event direct report with gralloc for accel sensor at very fast rate 420 TEST_F(SensorsHidlTest, AccelerometerGrallocDirectReportOperationVeryFast) { 421 testDirectReportOperation(SensorType::ACCELEROMETER, SharedMemType::GRALLOC, RateLevel::VERY_FAST, 422 sAccelNormChecker); 423 } 424 425 // Test sensor event direct report with gralloc for gyro sensor at normal rate 426 TEST_F(SensorsHidlTest, GyroscopeGrallocDirectReportOperationNormal) { 427 testDirectReportOperation(SensorType::GYROSCOPE, SharedMemType::GRALLOC, RateLevel::NORMAL, 428 sGyroNormChecker); 429 } 430 431 // Test sensor event direct report with gralloc for gyro sensor at fast rate 432 TEST_F(SensorsHidlTest, GyroscopeGrallocDirectReportOperationFast) { 433 testDirectReportOperation(SensorType::GYROSCOPE, SharedMemType::GRALLOC, RateLevel::FAST, 434 sGyroNormChecker); 435 } 436 437 // Test sensor event direct report with gralloc for gyro sensor at very fast rate 438 TEST_F(SensorsHidlTest, GyroscopeGrallocDirectReportOperationVeryFast) { 439 testDirectReportOperation(SensorType::GYROSCOPE, SharedMemType::GRALLOC, RateLevel::VERY_FAST, 440 sGyroNormChecker); 441 } 442 443 // Test sensor event direct report with gralloc for mag sensor at normal rate 444 TEST_F(SensorsHidlTest, MagnetometerGrallocDirectReportOperationNormal) { 445 testDirectReportOperation(SensorType::MAGNETIC_FIELD, SharedMemType::GRALLOC, RateLevel::NORMAL, 446 NullChecker()); 447 } 448 449 // Test sensor event direct report with gralloc for mag sensor at fast rate 450 TEST_F(SensorsHidlTest, MagnetometerGrallocDirectReportOperationFast) { 451 testDirectReportOperation(SensorType::MAGNETIC_FIELD, SharedMemType::GRALLOC, RateLevel::FAST, 452 NullChecker()); 453 } 454 455 // Test sensor event direct report with gralloc for mag sensor at very fast rate 456 TEST_F(SensorsHidlTest, MagnetometerGrallocDirectReportOperationVeryFast) { 457 testDirectReportOperation( 458 SensorType::MAGNETIC_FIELD, SharedMemType::GRALLOC, RateLevel::VERY_FAST, NullChecker()); 459 } 460 461 int main(int argc, char **argv) { 462 ::testing::AddGlobalTestEnvironment(SensorsHidlEnvironmentV1_0::Instance()); 463 ::testing::InitGoogleTest(&argc, argv); 464 SensorsHidlEnvironmentV1_0::Instance()->init(&argc, argv); 465 int status = RUN_ALL_TESTS(); 466 ALOGI("Test result = %d", status); 467 return status; 468 } 469 // vim: set ts=2 sw=2 470