• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #ifndef CHRE_STRESS_TEST_MANAGER_H_
18 #define CHRE_STRESS_TEST_MANAGER_H_
19 
20 #include "chre_stress_test.nanopb.h"
21 
22 #include <cinttypes>
23 
24 #include "chre/util/optional.h"
25 #include "chre/util/singleton.h"
26 #include "chre/util/time.h"
27 #include "chre_api/chre.h"
28 
29 namespace chre {
30 
31 namespace stress_test {
32 
33 //! Lists types of BLE scan request.
34 enum BleScanRequestType {
35   NO_FILTER = 0,
36   SERVICE_DATA_16 = 1,
37   STOP_SCAN = 2,
38 };
39 
40 /**
41  * A class to manage a CHRE stress test session.
42  */
43 class Manager {
44  public:
45   /**
46    * Handles an event from CHRE. Semantics are the same as nanoappHandleEvent.
47    */
48   void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
49                    const void *eventData);
50 
51  private:
52   struct AsyncRequest {
AsyncRequestAsyncRequest53     AsyncRequest(const void *cookie_) {
54       cookie = cookie_;
55     }
56 
57     uint64_t requestTimeNs = chreGetTime();
58     const void *cookie;
59   };
60 
61   struct SensorState {
62     //! Corresponds to types defined in chre_api/sensor_types.h.
63     const uint8_t type;
64 
65     //! The sampling interval for the next sensor request.
66     uint64_t samplingInterval;
67 
68     //! The sensor handle obtained from chreSensorFindDefault().
69     uint32_t handle;
70 
71     //! Indicate if the sensor is already configured.
72     bool enabled;
73 
74     //! Information about this sensor.
75     chreSensorInfo info;
76   };
77 
78   /**
79    * Handles a message from the host.
80    *
81    * @param senderInstanceId The sender instance ID of this message.
82    * @param hostData The data from the host.
83    */
84   void handleMessageFromHost(uint32_t senderInstanceId,
85                              const chreMessageFromHostData *hostData);
86   /**
87    * Processes data from CHRE.
88    *
89    * @param eventType The event type as defined by CHRE.
90    * @param eventData A pointer to the data.
91    */
92   void handleDataFromChre(uint16_t eventType, const void *eventData);
93 
94   /**
95    * @param handle A pointer to the timer handle.
96    */
97   void handleTimerEvent(const uint32_t *handle);
98 
99   /**
100    * Validates a timestamp of an event where the timestamp is expected
101    * to be monotonically increasing.
102    *
103    * @param timestamp The timestamp.
104    * @param pastTimestamp The previous timestamp.
105    */
106   void checkTimestamp(uint64_t timestamp, uint64_t pastTimestamp);
107 
108   /**
109    * Validates the difference between timestamps is below a certain interval
110    *
111    * @param timestamp The timestamp.
112    * @param pastTimestamp The previous timestamp.
113    * @param maxInterval The max interval allowed between the two timestamps.
114    */
115   void checkTimestampInterval(uint64_t timestamp, uint64_t pastTimestamp,
116                               uint64_t maxInterval);
117 
118   /**
119    * Handles a start command from the host.
120    *
121    * @param start true to start the test, stop otherwise.
122    */
123   void handleWifiStartCommand(bool start);
124   void handleGnssLocationStartCommand(bool start);
125   void handleGnssMeasurementStartCommand(bool start);
126   void handleWwanStartCommand(bool start);
127   void handleWifiScanMonitoringCommand(bool start);
128   void handleSensorStartCommand(bool start);
129   void handleAudioStartCommand(bool start);
130   void handleBleStartCommand(bool start);
131 
132   /**
133    * @param result The WiFi async result from CHRE.
134    */
135   void handleWifiAsyncResult(const chreAsyncResult *result);
136 
137   /**
138    * @param result The WiFi scan event from CHRE.
139    */
140   void handleWifiScanEvent(const chreWifiScanEvent *event);
141 
142   /**
143    * Sets up a WiFi scan request after some time.
144    */
145   void requestDelayedWifiScan();
146   void handleDelayedWifiTimer();
147 
148   /**
149    * Sends the failure to the host.
150    *
151    * @param errorMessage The error message string.
152    */
153   void sendFailure(const char *errorMessage);
154 
155   /**
156    * Sets/cancels a timer and asserts success.
157    *
158    * @param delayNs The delay of the timer in nanoseconds.
159    * @param oneShot true if the timer request is one-shot.
160    * @param timerHandle A non-null pointer to where the timer handle is stored.
161    */
162   void setTimer(uint64_t delayNs, bool oneShot, uint32_t *timerHandle);
163   void cancelTimer(uint32_t *timerHandle);
164 
165   /**
166    * Makes the next GNSS request.
167    */
168   void makeGnssLocationRequest();
169   void makeGnssMeasurementRequest();
170 
171   /**
172    * @param result The GNSS async result from CHRE.
173    */
174   void handleGnssAsyncResult(const chreAsyncResult *result);
175 
176   /**
177    * @param result The result to validate.
178    * @param request The async request associated with this result.
179    * @param asyncTimerHandle The async timer handle for this request.
180    */
181   void validateGnssAsyncResult(const chreAsyncResult *result,
182                                Optional<AsyncRequest> &request,
183                                uint32_t *asyncTimerHandle);
184 
185   /**
186    * @param event The GNSS event from CHRE.
187    */
188   void handleGnssLocationEvent(const chreGnssLocationEvent *event);
189   void handleGnssDataEvent(const chreGnssDataEvent *event);
190 
191   /**
192    * Makes the next cell info request.
193    */
194   void makeWwanCellInfoRequest();
195 
196   /**
197    * Send the capabilities to the host.
198    */
199   void sendCapabilitiesMessage();
200 
201   /**
202    * @param event The cell info event from CHRE.
203    */
204   void handleCellInfoResult(const chreWwanCellInfoResult *event);
205 
206   /**
207    * @param eventData The sensor data from CHRE.
208    */
209   void handleAccelSensorDataEvent(const chreSensorThreeAxisData *eventData);
210   void handleGyroSensorDataEvent(const chreSensorThreeAxisData *eventData);
211   void handleInstantMotionSensorDataEvent(
212       const chreSensorOccurrenceData *eventData);
213   void handleSensorSamplingChangeEvent(
214       const chreSensorSamplingStatusEvent *eventData);
215 
216   /**
217    * Makes the next sensor request.
218    */
219   void makeSensorRequests();
220 
221   /**
222    * Send a disable request to all sensors.
223    */
224   void stopSensorRequests();
225 
226   /**
227    * @param event The audio event from CHRE.
228    */
229   void handleAudioDataEvent(const chreAudioDataEvent *event);
230   void handleAudioSamplingChangeEvent(const chreAudioSourceStatusEvent *event);
231 
232   /**
233    * Makes the next audio request.
234    */
235   void makeAudioRequest();
236 
237   /**
238    * @param event The BLE advertisement event from CHRE.
239    */
240   void handleBleAdvertismentEvent(const chreBleAdvertisementEvent *event);
241 
242   /**
243    * @param event The BLE event from CHRE.
244    */
245   void handleBleAsyncResult(const chreAsyncResult *result);
246 
247   /**
248    * Makes the next Ble request.
249    */
250   void makeBleScanRequest();
251 
252   /**
253    * Enables a BLE scan.
254    *
255    * @return true if scan was enabled successfully.
256    */
257   bool enableBleScan();
258 
259   /**
260    * Disables a BLE scan.
261    *
262    * @return true if scan was disabled successfully.
263    */
264   bool disableBleScan();
265 
266   /**
267    * @param scanRequestType The current BLE scan request type.
268    *
269    * @return The pointer to a chreBleScanFilter that corresponds to the scan
270    * request type.
271    */
272   chreBleScanFilter *getBleScanFilter(BleScanRequestType &scanRequestType);
273 
274   //! The host endpoint of the current test host.
275   Optional<uint16_t> mHostEndpoint;
276 
277   //! The timer handle for performing requests.
278   uint32_t mWifiScanTimerHandle = CHRE_TIMER_INVALID;
279   uint32_t mWifiScanAsyncTimerHandle = CHRE_TIMER_INVALID;
280   uint32_t mGnssLocationTimerHandle = CHRE_TIMER_INVALID;
281   uint32_t mGnssLocationAsyncTimerHandle = CHRE_TIMER_INVALID;
282   uint32_t mGnssMeasurementTimerHandle = CHRE_TIMER_INVALID;
283   uint32_t mGnssMeasurementAsyncTimerHandle = CHRE_TIMER_INVALID;
284   uint32_t mWwanTimerHandle = CHRE_TIMER_INVALID;
285   uint32_t mWifiScanMonitorAsyncTimerHandle = CHRE_TIMER_INVALID;
286   uint32_t mSensorTimerHandle = CHRE_TIMER_INVALID;
287   uint32_t mAudioTimerHandle = CHRE_TIMER_INVALID;
288   uint32_t mBleScanTimerHandle = CHRE_TIMER_INVALID;
289 
290   //! true if the test has been started for the feature.
291   bool mWifiTestStarted = false;
292   bool mGnssLocationTestStarted = false;
293   bool mGnssMeasurementTestStarted = false;
294   bool mWwanTestStarted = false;
295   bool mSensorTestStarted = false;
296   bool mAudioTestStarted = false;
297   bool mBleTestStarted = false;
298 
299   //! true if scan monitor is enabled for the nanoapp.
300   bool mWifiScanMonitorEnabled = false;
301 
302   //! True if audio is enabled for the nanoapp.
303   bool mAudioEnabled = false;
304 
305   //! The cookie to use for requests.
306   const uint32_t kOnDemandWifiScanCookie = 0xface;
307   const uint32_t kGnssLocationCookie = 0xbeef;
308   const uint32_t kGnssMeasurementCookie = 0xbead;
309   const uint32_t kWwanCellInfoCookie = 0x1337;
310   const uint32_t kBleScanCookie = 0x1234;
311 
312   //! The pending requests.
313   Optional<AsyncRequest> mWifiScanAsyncRequest;
314   Optional<AsyncRequest> mGnssLocationAsyncRequest;
315   Optional<AsyncRequest> mGnssMeasurementAsyncRequest;
316   Optional<AsyncRequest> mWwanCellInfoAsyncRequest;
317   Optional<AsyncRequest> mBleScanAsyncRequest;
318 
319   //! The previous timestamp of events.
320   uint64_t mPrevGnssLocationEventTimestampMs = 0;
321   uint64_t mPrevGnssMeasurementEventTimestampNs = 0;
322   uint64_t mPrevWifiScanEventTimestampNs = 0;
323   uint64_t mPrevWwanCellInfoEventTimestampNs = 0;
324   uint64_t mPrevAccelEventTimestampNs = 0;
325   uint64_t mPrevGyroEventTimestampNs = 0;
326   uint64_t mPrevInstantMotionEventTimestampNs = 0;
327   uint64_t mPrevAudioEventTimestampMs = 0;
328   uint64_t mPrevBleAdTimestampMs = 0;
329 
330   //! Current number of sensors tested.
331   static constexpr uint32_t kNumSensors = 3;
332 
333   //! List of sensors.
334   SensorState mSensors[kNumSensors] = {
335       {
336           .type = CHRE_SENSOR_TYPE_ACCELEROMETER,
337           .samplingInterval = CHRE_SENSOR_INTERVAL_DEFAULT,
338           .handle = 0,
339           .enabled = true,
340           .info = {},
341       },
342       {
343           .type = CHRE_SENSOR_TYPE_GYROSCOPE,
344           .samplingInterval = CHRE_SENSOR_INTERVAL_DEFAULT,
345           .handle = 0,
346           .enabled = true,
347           .info = {},
348       },
349       {
350           .type = CHRE_SENSOR_TYPE_INSTANT_MOTION_DETECT,
351           .samplingInterval = CHRE_SENSOR_INTERVAL_DEFAULT,
352           .handle = 0,
353           .enabled = true,
354           .info = {},
355       }};
356 
357   //! Controls BLE scan testing stage
358   bool mShouldEnableBleScan = true;
359   chreBleScanMode mBleScanMode = CHRE_BLE_SCAN_MODE_BACKGROUND;
360 };
361 
362 // The stress test manager singleton.
363 typedef chre::Singleton<Manager> ManagerSingleton;
364 
365 }  // namespace stress_test
366 
367 }  // namespace chre
368 
369 #endif  // CHRE_STRESS_TEST_MANAGER_H_
370