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 <chre.h>
23 #include <cinttypes>
24 
25 #include "chre/util/optional.h"
26 #include "chre/util/singleton.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   };
45 
46   enum class FeatureState : uint8_t {
47     DISABLED = 0,
48     ENABLED,
49   };
50 
51   enum class TestStep : uint8_t {
52     SETUP = 0,
53     START,
54   };
55 
56   /**
57    * Handles an event from CHRE. Semantics are the same as nanoappHandleEvent.
58    */
59   void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
60                    const void *eventData);
61 
62  private:
63   struct TestSession {
64     uint16_t hostEndpointId;
65     Feature feature;
66     FeatureState featureState;
67     TestStep step;
68 
TestSessionTestSession69     TestSession(uint16_t id, Feature feature, FeatureState state,
70                 TestStep step) {
71       this->hostEndpointId = id;
72       this->feature = feature;
73       this->featureState = state;
74       this->step = step;
75     }
76   };
77 
78   /**
79    * @return true if the provided feature is supported by CHRE.
80    */
81   bool isFeatureSupported(Feature feature);
82 
83   /**
84    * Handles a message from the host.
85    *
86    * @param senderInstanceId The sender instance ID of this message.
87    * @param hostData The data from the host.
88    */
89   void handleMessageFromHost(uint32_t senderInstanceId,
90                              const chreMessageFromHostData *hostData);
91 
92   /**
93    * Initiates the test given a start command from the host. If a test was
94    * already in progress, a new start message will override and start a new
95    * test.
96    *
97    * @param hostEndpointId The test host endpoint ID.
98    * @param feature The feature to test.
99    * @param state The feature state.
100    * @param step The test step.
101    */
102   void handleStartTestMessage(uint16_t hostEndpointId, Feature feature,
103                               FeatureState state, TestStep step);
104 
105   /**
106    * Processes data from CHRE.
107    *
108    * @param eventType The event type as defined by CHRE.
109    * @param eventData A pointer to the data.
110    */
111   void handleDataFromChre(uint16_t eventType, const void *eventData);
112 
113   /**
114    * Starts a test for a given feature.
115    *
116    * @param feature The feature to test.
117    *
118    * @return true if the test successfully began.
119    */
120   bool startTestForFeature(Feature feature);
121 
122   /**
123    * @param result The async result.
124    * @param expectedCookie The expected cookie value.
125    *
126    * @return true if the async result matches expected values.
127    */
128   bool validateAsyncResult(const chreAsyncResult *result,
129                            const void *expectedCookie);
130 
131   /**
132    * @param result The async result provided by CHRE.
133    */
134   void handleWifiAsyncResult(const chreAsyncResult *result);
135   void handleGnssAsyncResult(const chreAsyncResult *result);
136 
137   /**
138    * @param result The cell info result from CHRE.
139    */
140   void handleWwanCellInfoResult(const chreWwanCellInfoResult *result);
141 
142   /**
143    * @param result The WiFi scan event result.
144    */
145   void handleWifiScanResult(const chreWifiScanEvent *result);
146 
147   /**
148    * @param event CHRE Audio Source Status Event
149    */
150   void handleAudioSourceStatusEvent(
151       const struct chreAudioSourceStatusEvent *event);
152 
153   /**
154    * @param event CHRE Audio Data Event
155    */
156   void handleAudioDataEvent(const struct chreAudioDataEvent *event);
157 
158   void handleTimeout();
159 
160   /**
161    * End the current test session and sends result to host.
162    *
163    * @param hostEndpointId The host to send the result to.
164    * @param success True if the test succeeded.
165    */
166   void sendTestResult(uint16_t hostEndpointId, bool success);
167 
168   //! The current test session.
169   chre::Optional<TestSession> mTestSession;
170 
171   //! The cached target to issue an RTT ranging request.
172   chre::Optional<chreWifiRangingTarget> mCachedRangingTarget;
173 };
174 
175 // The settings test manager singleton.
176 typedef chre::Singleton<Manager> ManagerSingleton;
177 
178 }  // namespace settings_test
179 
180 }  // namespace chre
181 
182 #endif  // CHRE_SETTINGS_TEST_MANAGER_H_
183