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 <chre.h> 23 #include <cinttypes> 24 25 #include "chre/util/optional.h" 26 #include "chre/util/singleton.h" 27 #include "chre/util/time.h" 28 29 namespace chre { 30 31 namespace stress_test { 32 33 /** 34 * A class to manage a CHRE stress test session. 35 */ 36 class Manager { 37 public: 38 /** 39 * Handles an event from CHRE. Semantics are the same as nanoappHandleEvent. 40 */ 41 void handleEvent(uint32_t senderInstanceId, uint16_t eventType, 42 const void *eventData); 43 44 private: 45 struct AsyncRequest { AsyncRequestAsyncRequest46 AsyncRequest(const void *cookie_) { 47 cookie = cookie_; 48 } 49 50 uint64_t requestTimeNs = chreGetTime(); 51 const void *cookie; 52 }; 53 54 /** 55 * Handles a message from the host. 56 * 57 * @param senderInstanceId The sender instance ID of this message. 58 * @param hostData The data from the host. 59 */ 60 void handleMessageFromHost(uint32_t senderInstanceId, 61 const chreMessageFromHostData *hostData); 62 /** 63 * Processes data from CHRE. 64 * 65 * @param eventType The event type as defined by CHRE. 66 * @param eventData A pointer to the data. 67 */ 68 void handleDataFromChre(uint16_t eventType, const void *eventData); 69 70 /** 71 * @param handle A pointer to the timer handle. 72 */ 73 void handleTimerEvent(const uint32_t *handle); 74 75 /** 76 * Validates a timestamp of an event where the timestamp is expected 77 * to be monotonically increasing. 78 * 79 * @param timestamp The timestamp. 80 * @param pastTimestamp The previous timestamp. 81 */ 82 void checkTimestamp(uint64_t timestamp, uint64_t pastTimestamp); 83 84 /** 85 * Handles a start command from the host. 86 * 87 * @param start true to start the test, stop otherwise. 88 */ 89 void handleWifiStartCommand(bool start); 90 void handleGnssLocationStartCommand(bool start); 91 void handleGnssMeasurementStartCommand(bool start); 92 void handleWwanStartCommand(bool start); 93 void handleWifiScanMonitoringCommand(bool start); 94 95 /** 96 * @param result The WiFi async result from CHRE. 97 */ 98 void handleWifiAsyncResult(const chreAsyncResult *result); 99 100 /** 101 * @param result The WiFi scan event from CHRE. 102 */ 103 void handleWifiScanEvent(const chreWifiScanEvent *event); 104 105 /** 106 * Sets up a WiFi scan request after some time. 107 */ 108 void requestDelayedWifiScan(); 109 void handleDelayedWifiTimer(); 110 111 /** 112 * Sends the failure to the host. 113 * 114 * @param errorMessage The error message string. 115 */ 116 void sendFailure(const char *errorMessage); 117 118 /** 119 * Sets/cancels a timer and asserts success. 120 * 121 * @param delayNs The delay of the timer in nanoseconds. 122 * @param oneShot true if the timer request is one-shot. 123 * @param timerHandle A non-null pointer to where the timer handle is stored. 124 */ 125 void setTimer(uint64_t delayNs, bool oneShot, uint32_t *timerHandle); 126 void cancelTimer(uint32_t *timerHandle); 127 128 /** 129 * Makes the next GNSS request. 130 */ 131 void makeGnssLocationRequest(); 132 void makeGnssMeasurementRequest(); 133 134 /** 135 * @param result The GNSS async result from CHRE. 136 */ 137 void handleGnssAsyncResult(const chreAsyncResult *result); 138 139 /** 140 * @param result The result to validate. 141 * @param request The async request associated with this result. 142 * @param asyncTimerHandle The async timer handle for this request. 143 */ 144 void validateGnssAsyncResult(const chreAsyncResult *result, 145 Optional<AsyncRequest> &request, 146 uint32_t *asyncTimerHandle); 147 148 /** 149 * @param event The GNSS event from CHRE. 150 */ 151 void handleGnssLocationEvent(const chreGnssLocationEvent *event); 152 void handleGnssDataEvent(const chreGnssDataEvent *event); 153 154 /** 155 * Makes the next cell info request. 156 */ 157 void makeWwanCellInfoRequest(); 158 159 /** 160 * @param event The cell info event from CHRE. 161 */ 162 void handleCellInfoResult(const chreWwanCellInfoResult *event); 163 164 //! The host endpoint of the current test host. 165 Optional<uint16_t> mHostEndpoint; 166 167 //! The timer handle for performing requests. 168 uint32_t mWifiScanTimerHandle = CHRE_TIMER_INVALID; 169 uint32_t mWifiScanAsyncTimerHandle = CHRE_TIMER_INVALID; 170 uint32_t mGnssLocationTimerHandle = CHRE_TIMER_INVALID; 171 uint32_t mGnssLocationAsyncTimerHandle = CHRE_TIMER_INVALID; 172 uint32_t mGnssMeasurementTimerHandle = CHRE_TIMER_INVALID; 173 uint32_t mGnssMeasurementAsyncTimerHandle = CHRE_TIMER_INVALID; 174 uint32_t mWwanTimerHandle = CHRE_TIMER_INVALID; 175 uint32_t mWifiScanMonitorAsyncTimerHandle = CHRE_TIMER_INVALID; 176 177 //! true if the test has been started for the feature. 178 bool mWifiTestStarted = false; 179 bool mGnssLocationTestStarted = false; 180 bool mGnssMeasurementTestStarted = false; 181 bool mWwanTestStarted = false; 182 183 //! true if scan monitor is enabled for the nanoapp. 184 bool mWifiScanMonitorEnabled = false; 185 186 //! The cookie to use for requests. 187 const uint32_t kOnDemandWifiScanCookie = 0xface; 188 const uint32_t kGnssLocationCookie = 0xbeef; 189 const uint32_t kGnssMeasurementCookie = 0xbead; 190 const uint32_t kWwanCellInfoCookie = 0x1337; 191 192 //! The pending requests. 193 Optional<AsyncRequest> mWifiScanAsyncRequest; 194 Optional<AsyncRequest> mGnssLocationAsyncRequest; 195 Optional<AsyncRequest> mGnssMeasurementAsyncRequest; 196 Optional<AsyncRequest> mWwanCellInfoAsyncRequest; 197 198 //! The previous timestamp of events. 199 uint64_t mPrevGnssLocationEventTimestampMs = 0; 200 uint64_t mPrevGnssMeasurementEventTimestampNs = 0; 201 uint64_t mPrevWifiScanEventTimestampNs = 0; 202 uint64_t mPrevWwanCellInfoEventTimestampNs = 0; 203 }; 204 205 // The stress test manager singleton. 206 typedef chre::Singleton<Manager> ManagerSingleton; 207 208 } // namespace stress_test 209 210 } // namespace chre 211 212 #endif // CHRE_STRESS_TEST_MANAGER_H_ 213