1 /*
2 * Copyright (C) 2016 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 /**
18 * Nanoapp which performs a number of operations within nanoappStart().
19 *
20 * This nanoapp is to confirm a number of CHRE methods can be invoked from
21 * within nanoappStart(). There are other tests which test each of these
22 * CHRE methods more in depth. We're just doing a consistency check that
23 * calling from nanoappStart() works at all.
24 *
25 * Specifically, we're testing:
26 * o chreHeapAlloc() and chreHeapFree()
27 * o chreGetInstanceId()
28 * o chreSendEvent() [*]
29 * o chreTimerSet() [*]
30 * o chreSensorFindDefault() and chreSensorConfigure() [*]
31 * o chreSendMessageToHostEndpoint() [**]
32 *
33 * [*] These require nanoappHandleEvent() to be called successfully in order
34 * to confirm.
35 * [**] This is confirmed by the host receiving this message.
36 *
37 * This isn't a "general" test, so it doesn't have a standard communication
38 * protocol. Notably, the Host doesn't send any messages to this nanoapp.
39 *
40 * Protocol:
41 * Nanoapp to Host: kContinue
42 * Nanoapp to Host: kSuccess
43 */
44
45 #include <cinttypes>
46
47 #include <chre.h>
48
49 #include <shared/send_message.h>
50 #include <shared/test_success_marker.h>
51 #include <shared/time_util.h>
52
53 using nanoapp_testing::MessageType;
54 using nanoapp_testing::sendFatalFailureToHost;
55 using nanoapp_testing::sendFatalFailureToHostUint8;
56 using nanoapp_testing::sendMessageToHost;
57 using nanoapp_testing::sendSuccessToHost;
58 using nanoapp_testing::TestSuccessMarker;
59
60 static bool gInMethod = false;
61 static uint32_t gInstanceId;
62 static uint32_t gTimerId;
63 static uint32_t gSensorHandle;
64
65 /**
66 * Busy startup stages and total number of stages.
67 */
68 enum BusyStartupStage {
69 BUSY_STARTUP_STAGE_SELF_EVENT = 0,
70 BUSY_STARTUP_STAGE_TIMER,
71 BUSY_STARTUP_STAGE_SENSOR,
72 BUSY_STARTUP_STAGE_COUNT,
73 };
74
75 //! TestSuccessMarker object to mark success of a stage.
76 TestSuccessMarker gTestSuccessMarker =
77 TestSuccessMarker(BUSY_STARTUP_STAGE_COUNT);
78
79 constexpr uint16_t kEventType = CHRE_EVENT_FIRST_USER_VALUE;
80
checkSelfEvent(uint16_t eventType,const uint32_t * eventData)81 static void checkSelfEvent(uint16_t eventType, const uint32_t *eventData) {
82 if (eventType != kEventType) {
83 uint32_t e = eventType;
84 sendFatalFailureToHost("Event from self, bad event type:", &e);
85 }
86 if (eventData == nullptr) {
87 sendFatalFailureToHost("Event from self, null data");
88 }
89 if (*eventData != gInstanceId) {
90 sendFatalFailureToHost("Event from self, bad data:", eventData);
91 }
92 gTestSuccessMarker.markStageAndSuccessOnFinish(BUSY_STARTUP_STAGE_SELF_EVENT);
93 }
94
checkTimerEvent(const uint32_t * eventData)95 static void checkTimerEvent(const uint32_t *eventData) {
96 if (eventData == nullptr) {
97 sendFatalFailureToHost("TimerEvent, null data");
98 }
99 if (*eventData != gInstanceId) {
100 sendFatalFailureToHost("TimerEvent, bad data:", eventData);
101 }
102 gTestSuccessMarker.markStageAndSuccessOnFinish(BUSY_STARTUP_STAGE_TIMER);
103 }
104
checkSensorEvent(const void * eventData)105 static void checkSensorEvent(const void *eventData) {
106 const chreSensorDataHeader *header =
107 static_cast<const chreSensorDataHeader *>(eventData);
108 if (header == nullptr) {
109 sendFatalFailureToHost("sensorEvent, null data");
110 }
111 if (header->sensorHandle != gSensorHandle) {
112 sendFatalFailureToHost("sensorEvent for wrong handle",
113 &header->sensorHandle);
114 }
115 if (header->readingCount == 0) {
116 sendFatalFailureToHost("sensorEvent has readingCount of 0");
117 }
118 if (header->reserved != 0) {
119 sendFatalFailureToHost("sensorEvent has non-zero reserved field");
120 }
121
122 if (chreGetApiVersion() < CHRE_API_VERSION_1_3) {
123 if (header->accuracy != 0) {
124 sendFatalFailureToHost("sensorEvent has non-zero reserved field");
125 }
126 } else if (header->accuracy > CHRE_SENSOR_ACCURACY_HIGH) {
127 sendFatalFailureToHostUint8("Sensor accuracy is not within valid range: ",
128 header->accuracy);
129 }
130
131 gTestSuccessMarker.markStageAndSuccessOnFinish(BUSY_STARTUP_STAGE_SENSOR);
132 }
133
nanoappHandleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)134 extern "C" void nanoappHandleEvent(uint32_t senderInstanceId,
135 uint16_t eventType, const void *eventData) {
136 if (gInMethod) {
137 sendFatalFailureToHost("CHRE reentered nanoapp");
138 }
139 gInMethod = true;
140 const uint32_t *intData = static_cast<const uint32_t *>(eventData);
141 if (senderInstanceId == gInstanceId) {
142 checkSelfEvent(eventType, intData);
143
144 } else if (senderInstanceId == CHRE_INSTANCE_ID) {
145 if (eventType == CHRE_EVENT_TIMER) {
146 checkTimerEvent(intData);
147 } else if (eventType == CHRE_EVENT_SENSOR_ACCELEROMETER_DATA) {
148 checkSensorEvent(eventData);
149 } else if (eventType == CHRE_EVENT_SENSOR_SAMPLING_CHANGE ||
150 eventType == CHRE_EVENT_SENSOR_ACCELEROMETER_BIAS_INFO) {
151 // This could have been generated when we configured the
152 // sensor. We just ignore it.
153 } else {
154 uint32_t e = eventType;
155 sendFatalFailureToHost("Unexpected event from CHRE:", &e);
156 }
157 } else {
158 sendFatalFailureToHost("Unexpected senderInstanceId", &senderInstanceId);
159 }
160 gInMethod = false;
161 }
162
nanoappStart(void)163 extern "C" bool nanoappStart(void) {
164 gInMethod = true;
165 void *ptr = chreHeapAlloc(15);
166 if (ptr == nullptr) {
167 // TODO(b/32326854): We're not able to send messages from
168 // nanoappStart(), so we just use chreLog() here, and make
169 // the user look through the logs to determine why this failed.
170 chreLog(CHRE_LOG_ERROR, "Unable to malloc in start");
171 return false;
172 }
173 gInstanceId = chreGetInstanceId();
174 if (gInstanceId == CHRE_INSTANCE_ID) {
175 chreLog(CHRE_LOG_ERROR, "Got bad instance ID in start");
176 return false;
177 }
178
179 // Send an event to ourself.
180 if (!chreSendEvent(kEventType, &gInstanceId, nullptr, gInstanceId)) {
181 chreLog(CHRE_LOG_ERROR, "Failed chreSendEvent in start");
182 return false;
183 }
184
185 // One shot timer that should trigger very quickly.
186 gTimerId = chreTimerSet(1, &gInstanceId, true);
187 if (gTimerId == CHRE_TIMER_INVALID) {
188 chreLog(CHRE_LOG_ERROR, "Failed chreTimerSet in start");
189 return false;
190 }
191
192 // We don't have a way to confirm the 'free' worked, we'll just look
193 // to see that we didn't crash. We intentionally move this 'free' to
194 // be not immediately after the 'alloc', and still before we're done
195 // calling other methods.
196 chreHeapFree(ptr);
197
198 // Confirm we can find and configure a sensor.
199 if (!chreSensorFindDefault(CHRE_SENSOR_TYPE_ACCELEROMETER, &gSensorHandle)) {
200 chreLog(CHRE_LOG_ERROR, "Failed sensorFindDefault in start");
201 return false;
202 }
203
204 // Configure accel request at 50 Hz (reasonable rate, e.g. for AR)
205 // TODO: Add a way to find the range of possible sample rates
206 if (!chreSensorConfigure(gSensorHandle, CHRE_SENSOR_CONFIGURE_MODE_CONTINUOUS,
207 20 * nanoapp_testing::kOneMillisecondInNanoseconds,
208 CHRE_SENSOR_LATENCY_ASAP)) {
209 chreLog(CHRE_LOG_ERROR, "Failed sensorConfigure in start");
210 return false;
211 }
212
213 // TODO(b/32326854): Confirm we can send a message to the host.
214
215 gInMethod = false;
216 return true;
217 }
218
nanoappEnd(void)219 extern "C" void nanoappEnd(void) {
220 if (!chreSensorConfigureModeOnly(gSensorHandle,
221 CHRE_SENSOR_CONFIGURE_MODE_DONE)) {
222 sendFatalFailureToHost("Unable to configure sensor mode to DONE");
223 }
224
225 if (gInMethod) {
226 // This message won't be noticed by the host; but hopefully the
227 // fatal failure prevents a clean unload of the app and fails the test.
228 sendFatalFailureToHost("nanoappEnd called in reentrant manner");
229 }
230 }
231