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 * Starts a test for a given feature. 116 * 117 * @param feature The feature to test. 118 * 119 * @return true if the test successfully began. 120 */ 121 bool startTestForFeature(Feature feature); 122 123 /** 124 * @param result The async result. 125 * @param expectedCookie The expected cookie value. 126 * 127 * @return true if the async result matches expected values. 128 */ 129 bool validateAsyncResult(const chreAsyncResult *result, 130 const void *expectedCookie); 131 132 /** 133 * @param result The async result provided by CHRE. 134 */ 135 void handleWifiAsyncResult(const chreAsyncResult *result); 136 void handleGnssAsyncResult(const chreAsyncResult *result); 137 138 /** 139 * @param result The cell info result from CHRE. 140 */ 141 void handleWwanCellInfoResult(const chreWwanCellInfoResult *result); 142 143 /** 144 * @param result The WiFi scan event result. 145 */ 146 void handleWifiScanResult(const chreWifiScanEvent *result); 147 148 /** 149 * @param event CHRE Audio Source Status Event 150 */ 151 void handleAudioSourceStatusEvent( 152 const struct chreAudioSourceStatusEvent *event); 153 154 /** 155 * @param event CHRE Audio Data Event 156 */ 157 void handleAudioDataEvent(const struct chreAudioDataEvent *event); 158 159 /* 160 * @param data CHRE event data containing the cookie used to set the timer. 161 */ 162 void handleTimeout(const void *data); 163 164 /** 165 * Handles the BLE async result 166 * 167 * @param result The BLE scan event result 168 */ 169 void handleBleAsyncResult(const chreAsyncResult *result); 170 171 /** 172 * End the current test session and sends result to host. 173 * 174 * @param hostEndpointId The host to send the result to. 175 * @param success True if the test succeeded. 176 */ 177 void sendTestResult(uint16_t hostEndpointId, bool success); 178 179 //! The current test session. 180 chre::Optional<TestSession> mTestSession; 181 182 //! The cached target to issue an RTT ranging request. 183 chre::Optional<chreWifiRangingTarget> mCachedRangingTarget; 184 185 //! The number of scan result received when after getting a wifi async result. 186 uint16_t mReceivedScanResults; 187 }; 188 189 // The settings test manager singleton. 190 typedef chre::Singleton<Manager> ManagerSingleton; 191 192 } // namespace settings_test 193 194 } // namespace chre 195 196 #endif // CHRE_SETTINGS_TEST_MANAGER_H_ 197