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/optional.h"
26 
27 namespace general_test {
28 
29 /**
30  * A class which tests chre WiFi APIs, including:
31  * chreWifiConfigureScanMonitorAsync
32  * chreWifiRequestScanAsync
33  * chreWifiRequestRangingAsync.
34  *
35  * Sends requests to those APIs and validates subsequent event data.
36  * Sends success to host if all data is valid, otherwise sends fatal failure.
37  */
38 class BasicWifiTest : public Test {
39  public:
40   BasicWifiTest();
41  protected:
42   /**
43    * Handles WiFi events, including:
44    * CHRE_EVENT_WIFI_ASYNC_RESULT
45    * CHRE_EVENT_WIFI_SCAN_RESULT
46    * CHRE_EVENT_WIFI_RANGING_RESULT
47    *
48    * @param senderInstanceId instance id from which the event is sent.
49    * @param eventType one of the above events.
50    * @param eventData a pointer to the details of a WiFi event.
51    */
52   void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
53                    const void *eventData) override;
54 
55   /**
56    * Calls chreWifiConfigureScanMonitorAsync with enable = true
57    * if WiFi has scan monitor capability, otherwise calls
58    * chreWifiRequestScanAsync if WiFi has on demand scan capability.
59    *
60    * @param messageSize the size of received message.
61    * @param message a pointer to the received message.
62    */
63   void setUp(uint32_t messageSize, const void *message) override;
64  private:
65   /**
66    * Validates chre WiFi async events.
67    * If validation result is true, makes subsequent requests:
68    * chreWifiConfigureScanMonitorAsync with enable = false
69    * chreWifiRequestScanAsyncDefault
70    *
71    * @param eventData received WiFi async result data.
72    */
73   void handleChreWifiAsyncEvent(const chreAsyncResult *result);
74 
75   /**
76    * @param eventData received WiFi scan event data.
77    * @return true if scanType is CHRE_WIFI_SCAN_TYPE_ACTIVE, false otherwise.
78    */
79   bool isActiveWifiScanType(const chreWifiScanEvent *eventData);
80 
81   /**
82    * Makes an API call, if corresponding WiFi ability exists;
83    * otherwise procceeds to next test stage.
84    */
85   void startScanMonitorTestStage();
86   void startScanAsyncTestStage();
87 
88   /**
89    * This method must be called after making an async request to CHRE.
90    *
91    * @param cookie pointer to request cookie.
92    * @param requestType a type of request.
93    * @param timeoutNs expected maximum elapse to receive chre WiFi result.
94    */
95   void resetCurrentWifiRequest(const void *cookie,
96                                uint8_t requestType,
97                                uint64_t timeoutNs);
98 
99   /**
100    * Validates a WiFi scan event, including event version, event order,
101    * and WiFi scan results. Sends fatal failure to host if event data is
102    * invalid, otherwise calls API chreWifiRequestRangingAsync.
103    *
104    * @param eventData received WiFi scan event data.
105    */
106   void validateWifiScanEvent(const chreWifiScanEvent *eventData);
107 
108   /**
109    * Validates ssidLen, band, RSSI, primaryChannel and centerFreqSecondary
110    * of all WiFi scan results. Sends fatal failure to host
111    * if there are invalid fields.
112    *
113    * @param count the size of results.
114    * @param results a pointer to the structure containing the results.
115    */
116   void validateWifiScanResult(uint8_t count,
117                               const chreWifiScanResult *results);
118 
119   /**
120    * Basic WiFi test stages and total number of stages.
121    */
122   enum BasicWifiTestStage {
123     BASIC_WIFI_TEST_STAGE_SCAN_MONITOR = 0,
124     BASIC_WIFI_TEST_STAGE_SCAN_ASYNC,
125     BASIC_WIFI_TEST_STAGE_COUNT,
126   };
127 
128   //! WiFi capabilities, used to make corresponding API calls.
129   uint32_t mWifiCapabilities;
130 
131   //! TestSuccessMarker object to mark success of a stage.
132   nanoapp_testing::TestSuccessMarker mTestSuccessMarker =
133       nanoapp_testing::TestSuccessMarker(BASIC_WIFI_TEST_STAGE_COUNT);
134 
135   //! Used to indicate if a chreAsyncResult is being expected.
136   chre::Optional<chreAsyncRequest> mCurrentWifiRequest;
137 
138   //! Start timestamp used to timing an event.
139   uint64_t mStartTimestampNs = 0;
140 
141   //! Expected sequence number for an event within a series of events
142   //! comprising a complete scan result.
143   uint32_t mNextExpectedIndex = 0;
144 
145   //! The remaining results of WiFi scan.
146   //! Used to identify when all events have been received.
147   uint32_t mWiFiScanResultRemaining = 0;
148 };
149 
150 } // namespace general_test
151 
152 #endif // _GTS_NANOAPPS_GENERAL_TEST_BASIC_WIFI_TEST_H_
153