1 /* 2 * Copyright (C) 2020 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_SETTINGS_TEST_MANAGER_H_ 18 #define CHRE_SETTINGS_TEST_MANAGER_H_ 19 20 #include "chre_settings_test.nanopb.h" 21 22 #include <cinttypes> 23 24 #include "chre/util/optional.h" 25 #include "chre/util/singleton.h" 26 #include "chre_api/chre.h" 27 28 namespace chre { 29 30 namespace settings_test { 31 32 /** 33 * A class to manage a CHRE settings test session. 34 */ 35 class Manager { 36 public: 37 enum class Feature : uint8_t { 38 WIFI_SCANNING = 0, 39 WIFI_RTT, 40 GNSS_LOCATION, 41 GNSS_MEASUREMENT, 42 WWAN_CELL_INFO, 43 AUDIO, 44 BLE_SCANNING, 45 }; 46 47 enum class FeatureState : uint8_t { 48 DISABLED = 0, 49 ENABLED, 50 }; 51 52 enum class TestStep : uint8_t { 53 SETUP = 0, 54 START, 55 }; 56 57 /** 58 * Handles an event from CHRE. Semantics are the same as nanoappHandleEvent. 59 */ 60 void handleEvent(uint32_t senderInstanceId, uint16_t eventType, 61 const void *eventData); 62 63 private: 64 struct TestSession { 65 uint16_t hostEndpointId; 66 Feature feature; 67 FeatureState featureState; 68 TestStep step; 69 TestSessionTestSession70 TestSession(uint16_t id, Feature currentFeature, FeatureState state, 71 TestStep currentStep) { 72 this->hostEndpointId = id; 73 this->feature = currentFeature; 74 this->featureState = state; 75 this->step = currentStep; 76 } 77 }; 78 79 /** 80 * @return true if the provided feature is supported by CHRE. 81 */ 82 bool isFeatureSupported(Feature feature); 83 84 /** 85 * Handles a message from the host. 86 * 87 * @param senderInstanceId The sender instance ID of this message. 88 * @param hostData The data from the host. 89 */ 90 void handleMessageFromHost(uint32_t senderInstanceId, 91 const chreMessageFromHostData *hostData); 92 93 /** 94 * Initiates the test given a start command from the host. If a test was 95 * already in progress, a new start message will override and start a new 96 * test. 97 * 98 * @param hostEndpointId The test host endpoint ID. 99 * @param feature The feature to test. 100 * @param state The feature state. 101 * @param step The test step. 102 */ 103 void handleStartTestMessage(uint16_t hostEndpointId, Feature feature, 104 FeatureState state, TestStep step); 105 106 /** 107 * Processes data from CHRE. 108 * 109 * @param eventType The event type as defined by CHRE. 110 * @param eventData A pointer to the data. 111 */ 112 void handleDataFromChre(uint16_t eventType, const void *eventData); 113 114 /** 115 * Requests the ranging wifi scan for the WIFI_RTT Feature. 116 * 117 * @return true if the request was accepted by CHRE 118 */ 119 bool requestRangingForFeatureWifiRtt(); 120 121 /** 122 * Starts a test for a given feature. 123 * 124 * @param feature The feature to test. 125 * 126 * @return true if the test successfully began. 127 */ 128 bool startTestForFeature(Feature feature); 129 130 /** 131 * @param result The async result. 132 * @param expectedCookie The expected cookie value. 133 * 134 * @return true if the async result matches expected values. 135 */ 136 bool validateAsyncResult(const chreAsyncResult *result, 137 const void *expectedCookie); 138 139 /** 140 * @param result The async result provided by CHRE. 141 */ 142 void handleWifiAsyncResult(const chreAsyncResult *result); 143 void handleGnssAsyncResult(const chreAsyncResult *result); 144 145 /** 146 * @param result The cell info result from CHRE. 147 */ 148 void handleWwanCellInfoResult(const chreWwanCellInfoResult *result); 149 150 /** 151 * @param result The WiFi scan event result. 152 */ 153 void handleWifiScanResult(const chreWifiScanEvent *result); 154 155 /** 156 * @param event CHRE Audio Source Status Event 157 */ 158 void handleAudioSourceStatusEvent( 159 const struct chreAudioSourceStatusEvent *event); 160 161 /** 162 * @param event CHRE Audio Data Event 163 */ 164 void handleAudioDataEvent(const struct chreAudioDataEvent *event); 165 166 /* 167 * @param data CHRE event data containing the cookie used to set the timer. 168 */ 169 void handleTimerEvent(const void *data); 170 171 /** 172 * Handles the BLE async result 173 * 174 * @param result The BLE scan event result 175 */ 176 void handleBleAsyncResult(const chreAsyncResult *result); 177 178 /** 179 * End the current test session and sends result to host. 180 * 181 * @param hostEndpointId The host to send the result to. 182 * @param success True if the test succeeded. 183 */ 184 void sendTestResult(uint16_t hostEndpointId, bool success); 185 186 //! The current test session. 187 chre::Optional<TestSession> mTestSession; 188 189 //! The cached target to issue an RTT ranging request. 190 chre::Optional<chreWifiRangingTarget> mCachedRangingTarget; 191 192 //! The number of scan result received when after getting a wifi async result. 193 uint16_t mReceivedScanResults; 194 195 //! True if we have received a chreAudioSourceStatusEvent with suspended == 196 //! false. 197 bool mAudioSamplingEnabled; 198 199 //! The number of retries available for requesting wifi scans before quitting 200 uint8_t mWifiRequestRetries; 201 }; 202 203 // The settings test manager singleton. 204 typedef chre::Singleton<Manager> ManagerSingleton; 205 206 } // namespace settings_test 207 208 } // namespace chre 209 210 #endif // CHRE_SETTINGS_TEST_MANAGER_H_ 211