1 /*
2  * Copyright (C) 2018 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 #ifndef _GTS_NANOAPPS_GENERAL_TEST_BASIC_WIFI_TEST_H_
17 #define _GTS_NANOAPPS_GENERAL_TEST_BASIC_WIFI_TEST_H_
18 
19 #include <general_test/test.h>
20 
21 #include <cstdint>
22 
23 #include <shared/test_success_marker.h>
24 
25 #include "chre/util/buffer.h"
26 #include "chre/util/optional.h"
27 
28 namespace general_test {
29 
30 /**
31  * A class which tests chre WiFi APIs, including:
32  * chreWifiConfigureScanMonitorAsync
33  * chreWifiRequestScanAsync
34  * chreWifiRequestRangingAsync.
35  *
36  * Sends requests to those APIs and validates subsequent event data.
37  * Sends success to host if all data is valid, otherwise sends fatal failure.
38  */
39 class BasicWifiTest : public Test {
40  public:
41   BasicWifiTest();
42 
43  protected:
44   /**
45    * Handles WiFi events, including:
46    * CHRE_EVENT_WIFI_ASYNC_RESULT
47    * CHRE_EVENT_WIFI_SCAN_RESULT
48    * CHRE_EVENT_WIFI_RANGING_RESULT
49    *
50    * @param senderInstanceId instance id from which the event is sent.
51    * @param eventType one of the above events.
52    * @param eventData a pointer to the details of a WiFi event.
53    */
54   void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
55                    const void *eventData) override;
56 
57   /**
58    * Calls chreWifiConfigureScanMonitorAsync with enable = true
59    * if WiFi has scan monitor capability, otherwise calls
60    * chreWifiRequestScanAsync if WiFi has on demand scan capability.
61    *
62    * @param messageSize the size of received message.
63    * @param message a pointer to the received message.
64    */
65   void setUp(uint32_t messageSize, const void *message) override;
66 
67  private:
68   /**
69    * Validates chre WiFi async events.
70    * If validation result is true, makes subsequent requests:
71    * chreWifiConfigureScanMonitorAsync with enable = false
72    * chreWifiRequestScanAsyncDefault
73    *
74    * @param eventData received WiFi async result data.
75    */
76   void handleChreWifiAsyncEvent(const chreAsyncResult *result);
77 
78   /**
79    * Processes the result and move to the next action accordingly.
80    *
81    * @param result chreAsyncResult of an async request.
82    */
83   void processChreWifiAsyncResult(const chreAsyncResult *result);
84 
85   /**
86    * @param eventData received WiFi scan event data.
87    * @return true if scanType is CHRE_WIFI_SCAN_TYPE_ACTIVE, false otherwise.
88    */
89   bool isActiveWifiScanType(const chreWifiScanEvent *eventData);
90 
91   /**
92    * Makes an API call, if corresponding WiFi ability exists;
93    * otherwise procceeds to next test stage.
94    */
95   void startScanMonitorTestStage();
96   void startScanAsyncTestStage();
97   void startRangingAsyncTestStage();
98 
99   /**
100    * This method must be called after making an async request to CHRE.
101    *
102    * @param cookie pointer to request cookie.
103    * @param requestType a type of request.
104    * @param timeoutNs expected maximum elapse to receive chre WiFi result.
105    */
106   void resetCurrentWifiRequest(const void *cookie, uint8_t requestType,
107                                uint64_t timeoutNs);
108 
109   /**
110    * Validates a WiFi scan event, including event version, event order,
111    * and WiFi scan results. Sends fatal failure to host if event data is
112    * invalid, otherwise calls API chreWifiRequestRangingAsync.
113    *
114    * @param eventData received WiFi scan event data.
115    */
116   void validateWifiScanEvent(const chreWifiScanEvent *eventData);
117 
118   /**
119    * Validates ssidLen, band, RSSI, primaryChannel and centerFreqSecondary
120    * of all WiFi scan results. Sends fatal failure to host
121    * if there are invalid fields.
122    *
123    * @param count the size of results.
124    * @param results a pointer to the structure containing the results.
125    */
126   void validateWifiScanResult(uint8_t count, const chreWifiScanResult *results);
127 
128   /**
129    * Validates a ranging event, including the event version, the number of
130    * results, and the results themselves. Sends a fatal failure to host if
131    * anything is invalid.
132    *
133    * @param eventData received ranging event data.
134    */
135   void validateRangingEvent(const chreWifiRangingEvent *eventData);
136 
137   /**
138    * Verifies that the current test stage is expecting the event received.
139    *
140    * @return true if the event should be received in the current stage.
141    */
142   bool rangingEventExpected();
143   bool scanEventExpected();
144 
145   /**
146    * Basic WiFi test stages and total number of stages.
147    */
148   enum BasicWifiTestStage {
149     BASIC_WIFI_TEST_STAGE_SCAN_MONITOR = 0,
150     BASIC_WIFI_TEST_STAGE_SCAN_ASYNC,
151     BASIC_WIFI_TEST_STAGE_SCAN_RTT,
152     BASIC_WIFI_TEST_STAGE_COUNT,
153   };
154 
155   //! WiFi capabilities, used to make corresponding API calls.
156   uint32_t mWifiCapabilities;
157 
158   //! TestSuccessMarker object to mark success of a stage.
159   nanoapp_testing::TestSuccessMarker mTestSuccessMarker =
160       nanoapp_testing::TestSuccessMarker(BASIC_WIFI_TEST_STAGE_COUNT);
161 
162   //! Used to indicate if a chreAsyncResult is being expected.
163   chre::Optional<chreAsyncRequest> mCurrentWifiRequest;
164 
165   //! Used to store the latest WiFi scan access points received by the test.
166   chre::Buffer<struct chreWifiScanResult> mLatestWifiScanResults;
167 
168   //! Start timestamp used to timing an event.
169   uint64_t mStartTimestampNs = 0;
170 
171   //! Expected sequence number for an event within a series of events
172   //! comprising a complete scan result.
173   uint32_t mNextExpectedIndex = 0;
174 
175   //! The remaining results of WiFi scan.
176   //! Used to identify when all events have been received.
177   uint32_t mWiFiScanResultRemaining = 0;
178 
179   // Number of retries remained when an on demand wifi scan fails.
180   uint8_t mNumScanRetriesRemaining = 1;
181 
182   // The handle to identify the timer event for restarting a wifi scan.
183   uint32_t mScanTimeoutTimerHandle = CHRE_TIMER_INVALID;
184 };
185 
186 }  // namespace general_test
187 
188 #endif  // _GTS_NANOAPPS_GENERAL_TEST_BASIC_WIFI_TEST_H_
189