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    * @param eventData received WiFi scan event data.
80    * @return true if scanType is CHRE_WIFI_SCAN_TYPE_ACTIVE, false otherwise.
81    */
82   bool isActiveWifiScanType(const chreWifiScanEvent *eventData);
83 
84   /**
85    * Makes an API call, if corresponding WiFi ability exists;
86    * otherwise procceeds to next test stage.
87    */
88   void startScanMonitorTestStage();
89   void startScanAsyncTestStage();
90   void startRangingAsyncTestStage();
91 
92   /**
93    * This method must be called after making an async request to CHRE.
94    *
95    * @param cookie pointer to request cookie.
96    * @param requestType a type of request.
97    * @param timeoutNs expected maximum elapse to receive chre WiFi result.
98    */
99   void resetCurrentWifiRequest(const void *cookie, uint8_t requestType,
100                                uint64_t timeoutNs);
101 
102   /**
103    * Validates a WiFi scan event, including event version, event order,
104    * and WiFi scan results. Sends fatal failure to host if event data is
105    * invalid, otherwise calls API chreWifiRequestRangingAsync.
106    *
107    * @param eventData received WiFi scan event data.
108    */
109   void validateWifiScanEvent(const chreWifiScanEvent *eventData);
110 
111   /**
112    * Validates ssidLen, band, RSSI, primaryChannel and centerFreqSecondary
113    * of all WiFi scan results. Sends fatal failure to host
114    * if there are invalid fields.
115    *
116    * @param count the size of results.
117    * @param results a pointer to the structure containing the results.
118    */
119   void validateWifiScanResult(uint8_t count, const chreWifiScanResult *results);
120 
121   /**
122    * Validates a ranging event, including the event version, the number of
123    * results, and the results themselves. Sends a fatal failure to host if
124    * anything is invalid.
125    *
126    * @param eventData received ranging event data.
127    */
128   void validateRangingEvent(const chreWifiRangingEvent *eventData);
129 
130   /**
131    * Verifies that the current test stage is expecting the event received.
132    *
133    * @return true if the event should be received in the current stage.
134    */
135   bool rangingEventExpected();
136   bool scanEventExpected();
137 
138   /**
139    * Basic WiFi test stages and total number of stages.
140    */
141   enum BasicWifiTestStage {
142     BASIC_WIFI_TEST_STAGE_SCAN_MONITOR = 0,
143     BASIC_WIFI_TEST_STAGE_SCAN_ASYNC,
144     BASIC_WIFI_TEST_STAGE_SCAN_RTT,
145     BASIC_WIFI_TEST_STAGE_COUNT,
146   };
147 
148   //! WiFi capabilities, used to make corresponding API calls.
149   uint32_t mWifiCapabilities;
150 
151   //! TestSuccessMarker object to mark success of a stage.
152   nanoapp_testing::TestSuccessMarker mTestSuccessMarker =
153       nanoapp_testing::TestSuccessMarker(BASIC_WIFI_TEST_STAGE_COUNT);
154 
155   //! Used to indicate if a chreAsyncResult is being expected.
156   chre::Optional<chreAsyncRequest> mCurrentWifiRequest;
157 
158   //! Used to store the latest WiFi scan access points received by the test.
159   chre::Buffer<struct chreWifiScanResult> mLatestWifiScanResults;
160 
161   //! Start timestamp used to timing an event.
162   uint64_t mStartTimestampNs = 0;
163 
164   //! Expected sequence number for an event within a series of events
165   //! comprising a complete scan result.
166   uint32_t mNextExpectedIndex = 0;
167 
168   //! The remaining results of WiFi scan.
169   //! Used to identify when all events have been received.
170   uint32_t mWiFiScanResultRemaining = 0;
171 };
172 
173 }  // namespace general_test
174 
175 #endif  // _GTS_NANOAPPS_GENERAL_TEST_BASIC_WIFI_TEST_H_
176