1 /*
2 * Copyright (C) 2019 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 <general_test/basic_flush_async_test.h>
18
19 #include <cinttypes>
20
21 #include <shared/macros.h>
22 #include <shared/send_message.h>
23 #include <shared/time_util.h>
24
25 #include <chre/util/nanoapp/log.h>
26 #include "chre/util/macros.h"
27
28 #define LOG_TAG "[BasicFlushAsyncTest]"
29
30 using nanoapp_testing::kOneMillisecondInNanoseconds;
31 using nanoapp_testing::kOneSecondInNanoseconds;
32 using nanoapp_testing::sendFatalFailureToHost;
33 using nanoapp_testing::sendSuccessToHost;
34
35 namespace general_test {
36
setUp(uint32_t messageSize,const void * message)37 void BasicSensorFlushAsyncTest::setUp(uint32_t messageSize,
38 const void *message) {
39 UNUSED_VAR(message);
40
41 constexpr uint64_t kFlushTestLatencyNs = 2 * kOneSecondInNanoseconds;
42 constexpr uint64_t kFlushTestStartTimerValueNs =
43 kFlushTestLatencyNs / 2; // start the test at (now + 1/2*latency)
44
45 if (messageSize != 0) {
46 sendFatalFailureToHost("Expected 0 byte message, got more bytes:",
47 &messageSize);
48 }
49
50 // TODO: Generalize this test for all sensors by making
51 // BasicSensorFlushAsyncTest a base class for sensor specific tests for the
52 // FlushAsync API
53 if (!chreSensorFindDefault(CHRE_SENSOR_TYPE_ACCELEROMETER, &mSensorHandle)) {
54 sendFatalFailureToHost("Default Accelerometer not found");
55 }
56
57 // We set the sampling period of the sensor to 2x the min interval,
58 // and set a variable to track that we get sensor samples within a
59 // reasonable (a small order of magnitude greater than the min interval)
60 // 'wiggle room' from when we start the flush request.
61 struct chreSensorInfo info;
62 if (!chreGetSensorInfo(mSensorHandle, &info)) {
63 sendFatalFailureToHost("Failed to get sensor info");
64 }
65 mFlushTestTimeWiggleRoomNs = 20 * info.minInterval;
66
67 if (!chreSensorConfigure(mSensorHandle, CHRE_SENSOR_CONFIGURE_MODE_CONTINUOUS,
68 2 * info.minInterval, kFlushTestLatencyNs)) {
69 sendFatalFailureToHost("Failed to configure the accelerometer");
70 }
71
72 // To exercise the test, we need to confirm that we actually get sensor
73 // samples from the flush request. to do this, set a timer to start a flush
74 // request at around latency/2 time from now, and request the flush when it
75 // expires, hoping to receive some of the data accumulated between configure
76 // time and flush request time
77 mFlushStartTimerHandle =
78 chreTimerSet(kFlushTestStartTimerValueNs, &mFlushStartTimerHandle,
79 true /* one shot */);
80 if (CHRE_TIMER_INVALID == mFlushStartTimerHandle) {
81 sendFatalFailureToHost("Failed to set flush start timer");
82 }
83 }
84
handleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)85 void BasicSensorFlushAsyncTest::handleEvent(uint32_t senderInstanceId,
86 uint16_t eventType,
87 const void *eventData) {
88 UNUSED_VAR(senderInstanceId);
89
90 switch (eventType) {
91 case CHRE_EVENT_SENSOR_ACCELEROMETER_DATA:
92 handleDataReceived(
93 static_cast<const struct chreSensorThreeAxisData *>(eventData));
94 break;
95
96 case CHRE_EVENT_SENSOR_FLUSH_COMPLETE:
97 handleFlushComplete(
98 static_cast<const struct chreSensorFlushCompleteEvent *>(eventData));
99 break;
100
101 case CHRE_EVENT_TIMER:
102 handleTimerExpired(static_cast<const uint32_t *>(eventData));
103 break;
104
105 default:
106 break;
107 }
108 }
109
start()110 void BasicSensorFlushAsyncTest::start() {
111 mStarted = true;
112 mFlushRequestTime = chreGetTime();
113
114 if (!chreSensorFlushAsync(mSensorHandle, &mCookie)) {
115 finish(false /* succeeded */, "Async flush failed");
116 }
117
118 mFlushTimeoutTimerHandle =
119 chreTimerSet(CHRE_SENSOR_FLUSH_COMPLETE_TIMEOUT_NS,
120 &mFlushTimeoutTimerHandle, true /* oneShot */);
121 if (CHRE_TIMER_INVALID == mFlushTimeoutTimerHandle) {
122 sendFatalFailureToHost("Failed to set flush start timer");
123 }
124 }
125
finish(bool succeeded,const char * message)126 void BasicSensorFlushAsyncTest::finish(bool succeeded, const char *message) {
127 mStarted = false;
128
129 if (mFlushTimeoutTimerHandle != CHRE_TIMER_INVALID) {
130 chreTimerCancel(mFlushTimeoutTimerHandle);
131 }
132
133 if (!chreSensorConfigureModeOnly(mSensorHandle,
134 CHRE_SENSOR_CONFIGURE_MODE_DONE)) {
135 sendFatalFailureToHost("Failed to release sensor handle");
136 }
137
138 if (!succeeded) {
139 ASSERT_NE(message, nullptr, "message cannot be null when the test failed");
140 sendFatalFailureToHost(message);
141 } else {
142 sendSuccessToHost();
143 }
144 }
145
handleDataReceived(const struct chreSensorThreeAxisData * eventData)146 void BasicSensorFlushAsyncTest::handleDataReceived(
147 const struct chreSensorThreeAxisData *eventData) {
148 // we're only interested in storing the latest timestamp of the sensor data
149 mLatestSensorDataTimestamp = eventData->header.baseTimestamp;
150 for (int i = 0; i < eventData->header.readingCount; ++i) {
151 mLatestSensorDataTimestamp += eventData->readings[i].timestampDelta;
152 }
153 }
154
handleFlushComplete(const struct chreSensorFlushCompleteEvent * eventData)155 void BasicSensorFlushAsyncTest::handleFlushComplete(
156 const struct chreSensorFlushCompleteEvent *eventData) {
157 if (mStarted) {
158 ASSERT_NE(mLatestSensorDataTimestamp, 0, "No sensor data was received");
159
160 // we should fail the test if we receive too old a sensor sample.
161 // ideally, we don't receive any samples that was sampled after
162 // our flush request, but for this test, we'll be lenient and assume
163 // that anything between [flushRequestTime - kFlushTestTimeWiggleRoomNs,
164 // now] is OK.
165 uint64_t oldestValidTimestamp =
166 mFlushRequestTime - mFlushTestTimeWiggleRoomNs;
167
168 ASSERT_GE(mLatestSensorDataTimestamp, oldestValidTimestamp,
169 "Received very old data");
170
171 LOGI("Flush test: flush request to complete time: %" PRIu64 " ms",
172 (chreGetTime() - mFlushRequestTime) / kOneMillisecondInNanoseconds);
173
174 // verify event data
175 ASSERT_NE(eventData, nullptr, "null event data");
176 ASSERT_EQ(eventData->sensorHandle, mSensorHandle,
177 "Got flush event from a different sensor handle");
178 ASSERT_EQ(eventData->errorCode, CHRE_ERROR_NONE,
179 "Flush Error code was not CHRE_ERROR_NONE");
180 ASSERT_NE(eventData->cookie, nullptr,
181 "Null cookie in flush complete event");
182 ASSERT_EQ(*(static_cast<const uint32_t *>(eventData->cookie)), mCookie,
183 "unexpected cookie in flush complete event");
184
185 finish(true /* succeeded */, nullptr /* message */);
186 }
187 }
188
handleTimerExpired(const uint32_t * timerHandle)189 void BasicSensorFlushAsyncTest::handleTimerExpired(
190 const uint32_t *timerHandle) {
191 if (timerHandle != nullptr) {
192 if (mFlushStartTimerHandle == *timerHandle) {
193 start();
194 } else if (mFlushTimeoutTimerHandle == *timerHandle) {
195 finish(false /* succeeded */,
196 "Did not receive flush complete event in time");
197 } else {
198 sendFatalFailureToHost("Unexpected timer handle received");
199 }
200 } else {
201 sendFatalFailureToHost("Null timer handle received");
202 }
203 }
204
205 } // namespace general_test