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