Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "SensorTest.h"
     18 #include <errno.h>
     19 
     20 namespace android {
     21 namespace SensorTest {
     22 
     23 // Test if test environment is correctly initialized
     24 void SensorTest::testInitialized(JNIEnv *env) {
     25     ASSERT_TRUE(mManager->isValid());
     26 }
     27 
     28 // Test if invalid parameter cases are handled correctly
     29 void SensorTest::testInvalidParameter(JNIEnv *env) {
     30     ASensorList dummyList;
     31     ASSERT_EQ(ASensorManager_getSensorList(nullptr, nullptr), -EINVAL);
     32     ASSERT_EQ(ASensorManager_getSensorList(nullptr, &dummyList), -EINVAL);
     33 
     34     ASSERT_EQ(ASensorManager_getDefaultSensor(nullptr, ASENSOR_TYPE_ACCELEROMETER), nullptr);
     35 
     36     ASSERT_EQ(ASensorManager_getDefaultSensorEx(
     37             nullptr, ASENSOR_TYPE_ACCELEROMETER, false), nullptr);
     38 
     39     ALooper *nonNullLooper = reinterpret_cast<ALooper *>(1);
     40     ASensorManager *nonNullManager = reinterpret_cast<ASensorManager *>(1);
     41     ASSERT_EQ(ASensorManager_createEventQueue(nullptr, nullptr, 0, nullptr, nullptr), nullptr);
     42     ASSERT_EQ(ASensorManager_createEventQueue(
     43             nullptr, nonNullLooper, 0, nullptr, nullptr), nullptr);
     44     ASSERT_EQ(ASensorManager_createEventQueue(
     45             nonNullManager, nullptr, 0, nullptr, nullptr), nullptr);
     46 
     47     ASensorEventQueue *nonNullQueue = reinterpret_cast<ASensorEventQueue *>(1);
     48     ASSERT_EQ(ASensorManager_destroyEventQueue(nullptr, nullptr), -EINVAL);
     49     ASSERT_EQ(ASensorManager_destroyEventQueue(nullptr, nonNullQueue), -EINVAL);
     50     ASSERT_EQ(ASensorManager_destroyEventQueue(nonNullManager, nullptr), -EINVAL);
     51 
     52     int fakeValidFd = 1;
     53     int invalidFd = -1;
     54     ASSERT_EQ(ASensorManager_createSharedMemoryDirectChannel(
     55             nullptr, fakeValidFd, sizeof(ASensorEvent)), -EINVAL);
     56     ASSERT_EQ(ASensorManager_createSharedMemoryDirectChannel(
     57             nonNullManager, invalidFd, sizeof(ASensorEvent)), -EINVAL);
     58     ASSERT_EQ(ASensorManager_createSharedMemoryDirectChannel(
     59             nonNullManager, fakeValidFd, sizeof(ASensorEvent) - 1), -EINVAL);
     60     ASSERT_EQ(ASensorManager_createSharedMemoryDirectChannel(
     61             nonNullManager, fakeValidFd, 0), -EINVAL);
     62 
     63     AHardwareBuffer *nonNullHardwareBuffer = reinterpret_cast<AHardwareBuffer *>(1);
     64     ASSERT_EQ(ASensorManager_createHardwareBufferDirectChannel(
     65             nullptr, nonNullHardwareBuffer, sizeof(ASensorEvent)), -EINVAL);
     66     ASSERT_EQ(ASensorManager_createHardwareBufferDirectChannel(
     67             nonNullManager, nullptr, sizeof(ASensorEvent)), -EINVAL);
     68     ASSERT_EQ(ASensorManager_createHardwareBufferDirectChannel(
     69             nonNullManager, nonNullHardwareBuffer, sizeof(ASensorEvent) - 1), -EINVAL);
     70     ASSERT_EQ(ASensorManager_createHardwareBufferDirectChannel(
     71             nonNullManager, nonNullHardwareBuffer, 0), -EINVAL);
     72 
     73     // no return value to test, but call this to test if it will crash
     74     ASensorManager_destroyDirectChannel(nullptr, 1);
     75 
     76     ASensor *nonNullSensor = reinterpret_cast<ASensor *>(1);
     77     ASSERT_EQ(ASensorManager_configureDirectReport(
     78             nullptr, nullptr, 1, ASENSOR_DIRECT_RATE_NORMAL), -EINVAL);
     79     ASSERT_EQ(ASensorManager_configureDirectReport(
     80             nullptr, nonNullSensor, 1, ASENSOR_DIRECT_RATE_NORMAL), -EINVAL);
     81     ASSERT_EQ(ASensorManager_configureDirectReport(
     82             nullptr, nonNullSensor, 1, ASENSOR_DIRECT_RATE_STOP), -EINVAL);
     83     ASSERT_EQ(ASensorManager_configureDirectReport(
     84             nonNullManager, nullptr, 1, ASENSOR_DIRECT_RATE_NORMAL), -EINVAL);
     85 
     86     ASSERT_EQ(ASensorEventQueue_registerSensor(nullptr, nullptr, 1, 1), -EINVAL);
     87     ASSERT_EQ(ASensorEventQueue_registerSensor(nullptr, nonNullSensor, 1, 1), -EINVAL);
     88     ASSERT_EQ(ASensorEventQueue_registerSensor(nonNullQueue, nullptr, 1, 1), -EINVAL);
     89     ASSERT_EQ(ASensorEventQueue_registerSensor(nonNullQueue, nonNullSensor, -1, 1), -EINVAL);
     90     ASSERT_EQ(ASensorEventQueue_registerSensor(nonNullQueue, nonNullSensor, 1, -1), -EINVAL);
     91     ASSERT_EQ(ASensorEventQueue_registerSensor(nonNullQueue, nonNullSensor, -1, -1), -EINVAL);
     92 
     93     ASSERT_EQ(ASensorEventQueue_enableSensor(nullptr, nullptr), -EINVAL);
     94     ASSERT_EQ(ASensorEventQueue_enableSensor(nullptr, nonNullSensor), -EINVAL);
     95     ASSERT_EQ(ASensorEventQueue_enableSensor(nonNullQueue, nullptr), -EINVAL);
     96 
     97     ASSERT_EQ(ASensorEventQueue_disableSensor(nullptr, nullptr), -EINVAL);
     98     ASSERT_EQ(ASensorEventQueue_disableSensor(nullptr, nonNullSensor), -EINVAL);
     99     ASSERT_EQ(ASensorEventQueue_disableSensor(nonNullQueue, nullptr), -EINVAL);
    100 
    101     ASSERT_EQ(ASensorEventQueue_setEventRate(nullptr, nullptr, 1), -EINVAL);
    102     ASSERT_EQ(ASensorEventQueue_setEventRate(nullptr, nonNullSensor, 1), -EINVAL);
    103     ASSERT_EQ(ASensorEventQueue_setEventRate(nonNullQueue, nullptr, 1), -EINVAL);
    104     ASSERT_EQ(ASensorEventQueue_setEventRate(nonNullQueue, nonNullSensor, -1), -EINVAL);
    105 
    106     ASSERT_EQ(ASensorEventQueue_hasEvents(nullptr), -EINVAL);
    107 
    108     ASensorEvent event;
    109     ASensorEvent *nonNullEvent = &event;
    110     ASSERT_EQ(ASensorEventQueue_getEvents(nullptr, nullptr, 1), -EINVAL)
    111     ASSERT_EQ(ASensorEventQueue_getEvents(nullptr, nullptr, 0), -EINVAL)
    112     ASSERT_EQ(ASensorEventQueue_getEvents(nullptr, nonNullEvent, 1), -EINVAL)
    113     ASSERT_EQ(ASensorEventQueue_getEvents(nullptr, nonNullEvent, 0), -EINVAL);
    114     ASSERT_EQ(ASensorEventQueue_getEvents(nonNullQueue, nullptr, 1), -EINVAL)
    115     ASSERT_EQ(ASensorEventQueue_getEvents(nonNullQueue, nullptr, 0), -EINVAL);
    116 
    117     ASSERT_NULL(ASensor_getName(nullptr));
    118     ASSERT_NULL(ASensor_getVendor(nullptr));
    119     ASSERT_EQ(ASensor_getType(nullptr), ASENSOR_TYPE_INVALID);
    120     // cannot use ASSERT_EQ as nan compare always returns false
    121     ASSERT_NAN(ASensor_getResolution(nullptr));
    122     ASSERT_EQ(ASensor_getMinDelay(nullptr), ASENSOR_DELAY_INVALID);
    123     ASSERT_EQ(ASensor_getFifoMaxEventCount(nullptr), ASENSOR_FIFO_COUNT_INVALID);
    124     ASSERT_EQ(ASensor_getFifoReservedEventCount(nullptr), ASENSOR_FIFO_COUNT_INVALID);
    125     ASSERT_NULL(ASensor_getStringType(nullptr));
    126     ASSERT_EQ(ASensor_getReportingMode(nullptr), AREPORTING_MODE_INVALID);
    127     ASSERT_EQ(ASensor_isWakeUpSensor(nullptr), false);
    128     ASSERT_EQ(ASensor_isDirectChannelTypeSupported(
    129             nullptr, ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY), false);
    130     ASSERT_EQ(ASensor_isDirectChannelTypeSupported(
    131             nullptr, ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER), false);
    132     ASSERT_EQ(ASensor_getHighestDirectReportRateLevel(nullptr), ASENSOR_DIRECT_RATE_STOP);
    133 }
    134 
    135 // Test sensor direct report functionality
    136 void SensorTest::testDirectReport(JNIEnv* env, int32_t sensorType, int32_t channelType, int32_t rateLevel) {
    137     constexpr size_t kEventSize = sizeof(ASensorEvent);
    138     constexpr size_t kNEvent = 4096; // enough to contain 1.5 * 800 * 2.2 events
    139     constexpr size_t kMemSize = kEventSize * kNEvent;
    140 
    141     // value check criterion
    142     constexpr float GRAVITY_MIN = 9.81f - 0.5f;
    143     constexpr float GRAVITY_MAX = 9.81f + 0.5f;
    144     constexpr float GYRO_MAX = 0.1f; // ~5 dps
    145 
    146     constexpr float RATE_NORMAL_NOMINAL = 50;
    147     constexpr float RATE_FAST_NOMINAL = 200;
    148     constexpr float RATE_VERY_FAST_NOMINAL = 800;
    149 
    150     TestSensor sensor = mManager->getDefaultSensor(sensorType);
    151     if (!sensor.isValid()
    152         || sensor.getHighestDirectReportRateLevel() < rateLevel
    153         || !sensor.isDirectChannelTypeSupported(channelType)) {
    154         // no sensor of type sensorType or it does not declare support of channelType or rateLevel
    155         return;
    156     }
    157 
    158     std::unique_ptr<TestSharedMemory> mem(TestSharedMemory::create(channelType, kMemSize));
    159     ASSERT_NE(mem, nullptr);
    160     ASSERT_NE(mem->getBuffer(), nullptr);
    161     switch (channelType) {
    162         case ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY:
    163             ASSERT_GT(mem->getSharedMemoryFd(), 0);
    164             break;
    165         case ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER:
    166             ASSERT_NOT_NULL(mem->getHardwareBuffer());
    167             break;
    168     }
    169 
    170     char* buffer = mem->getBuffer();
    171     // fill memory with data
    172     for (size_t i = 0; i < kMemSize; ++i) {
    173         buffer[i] = '\xcc';
    174     }
    175 
    176     int32_t channel;
    177     channel = mManager->createDirectChannel(*mem);
    178     ASSERT_GT(channel, 0);
    179 
    180     // check memory is zeroed
    181     for (size_t i = 0; i < kMemSize; ++i) {
    182         ASSERT_EQ(buffer[i], '\0');
    183     }
    184 
    185     int32_t eventToken;
    186     eventToken = mManager->configureDirectReport(sensor, channel, rateLevel);
    187     usleep(1500000); // sleep 1 sec for data, plus 0.5 sec for initialization
    188     auto events = mem->parseEvents();
    189 
    190     // find norminal rate
    191     float nominalFreq = 0.f;
    192     float nominalTestTimeSec = 1.f;
    193     float maxTestTimeSec = 1.5f;
    194     switch (rateLevel) {
    195         case ASENSOR_DIRECT_RATE_NORMAL:
    196             nominalFreq = RATE_NORMAL_NOMINAL;
    197             break;
    198         case ASENSOR_DIRECT_RATE_FAST:
    199             nominalFreq = RATE_FAST_NOMINAL;
    200             break;
    201         case ASENSOR_DIRECT_RATE_VERY_FAST:
    202             nominalFreq = RATE_VERY_FAST_NOMINAL;
    203             break;
    204     }
    205 
    206     // allowed to be between 55% and 220% of nominal freq
    207     ASSERT_GT(events.size(), static_cast<size_t>(nominalFreq * 0.55f * nominalTestTimeSec));
    208     ASSERT_LT(events.size(), static_cast<size_t>(nominalFreq * 2.2f * maxTestTimeSec));
    209 
    210     int64_t lastTimestamp = 0;
    211     for (auto &e : events) {
    212         ASSERT_EQ(e.type, sensorType);
    213         ASSERT_EQ(e.sensor, eventToken);
    214         ASSERT_GT(e.timestamp, lastTimestamp);
    215 
    216         // type specific value check
    217         switch(sensorType) {
    218             case ASENSOR_TYPE_ACCELEROMETER: {
    219                 ASensorVector &acc = e.vector;
    220                 double accNorm = std::sqrt(acc.x * acc.x + acc.y * acc.y + acc.z * acc.z);
    221                 if (accNorm > GRAVITY_MAX || accNorm < GRAVITY_MIN) {
    222                     ALOGE("Gravity norm = %f", accNorm);
    223                 }
    224                 ASSERT_GE(accNorm, GRAVITY_MIN);
    225                 ASSERT_LE(accNorm, GRAVITY_MAX);
    226                 break;
    227             }
    228             case ASENSOR_TYPE_GYROSCOPE: {
    229                 ASensorVector &gyro = e.vector;
    230                 double gyroNorm = std::sqrt(gyro.x * gyro.x + gyro.y * gyro.y + gyro.z * gyro.z);
    231                 // assert not drifting
    232                 ASSERT_LE(gyroNorm, GYRO_MAX);  // < ~2.5 degree/s
    233                 break;
    234             }
    235         }
    236 
    237         lastTimestamp = e.timestamp;
    238     }
    239 
    240     // stop sensor and unregister channel
    241     mManager->configureDirectReport(sensor, channel, ASENSOR_DIRECT_RATE_STOP);
    242     mManager->destroyDirectChannel(channel);
    243 }
    244 } // namespace SensorTest
    245 } // namespace android
    246