1 #include "wifi_hal.h"
2 
3 #ifndef __WIFI_HAL_GSCAN_H__
4 #define __WIFI_HAL_GSCAN_H__
5 
6 /* AP Scans */
7 
8 typedef enum {
9     WIFI_BAND_UNSPECIFIED,
10     WIFI_BAND_BG = 1,                       // 2.4 GHz
11     WIFI_BAND_A = 2,                        // 5 GHz without DFS
12     WIFI_BAND_A_DFS = 4,                    // 5 GHz DFS only
13     WIFI_BAND_A_WITH_DFS = 6,               // 5 GHz with DFS
14     WIFI_BAND_ABG = 3,                      // 2.4 GHz + 5 GHz; no DFS
15     WIFI_BAND_ABG_WITH_DFS = 7,             // 2.4 GHz + 5 GHz with DFS
16 } wifi_band;
17 
18 #define MAX_CHANNELS                16
19 #define MAX_BUCKETS                 16
20 #define MAX_HOTLIST_APS             128
21 #define MAX_SIGNIFICANT_CHANGE_APS  64
22 #define MAX_EPNO_NETWORKS           64
23 #define MAX_HOTLIST_SSID            8
24 #define MAX_AP_CACHE_PER_SCAN       32
25 
26 wifi_error wifi_get_valid_channels(wifi_interface_handle handle,
27         int band, int max_channels, wifi_channel *channels, int *num_channels);
28 
29 typedef struct {
30     int max_scan_cache_size;                 // total space allocated for scan (in bytes)
31     int max_scan_buckets;                    // maximum number of channel buckets
32     int max_ap_cache_per_scan;               // maximum number of APs that can be stored per scan
33     int max_rssi_sample_size;                // number of RSSI samples used for averaging RSSI
34     int max_scan_reporting_threshold;        // max possible report_threshold as described
35                                              // in wifi_scan_cmd_params
36     int max_hotlist_bssids;                  // maximum number of entries for hotlist BSSIDs
37     int max_hotlist_ssids;                   // maximum number of entries for hotlist SSIDs
38     int max_significant_wifi_change_aps;     // maximum number of entries for
39                                              // significant wifi change APs
40     int max_bssid_history_entries;           // number of BSSID/RSSI entries that device can hold
41     int max_number_epno_networks;            // max number of epno entries
42     int max_number_epno_networks_by_ssid;    // max number of epno entries if ssid is specified,
43                                              // that is, epno entries for which an exact match is
44                                              // required, or entries corresponding to hidden ssids
45     int max_number_of_white_listed_ssid;     // max number of white listed SSIDs, M target is 2 to 4
46 } wifi_gscan_capabilities;
47 
48 wifi_error wifi_get_gscan_capabilities(wifi_interface_handle handle,
49         wifi_gscan_capabilities *capabilities);
50 
51 typedef enum {
52     WIFI_SCAN_RESULTS_AVAILABLE,   // reported when REPORT_EVENTS_EACH_SCAN is set and a scan
53                                    // completes. WIFI_SCAN_THRESHOLD_NUM_SCANS or
54                                    // WIFI_SCAN_THRESHOLD_PERCENT can be reported instead if the
55                                    // reason for the event is available; however, at most one of
56                                    // these events should be reported per scan. If there are
57                                    // multiple buckets that were scanned this period and one has the
58                                    // EACH_SCAN flag set then this event should be prefered.
59     WIFI_SCAN_THRESHOLD_NUM_SCANS, // can be reported when REPORT_EVENTS_EACH_SCAN is not set and
60                                    // report_threshold_num_scans is reached.
61     WIFI_SCAN_THRESHOLD_PERCENT,   // can be reported when REPORT_EVENTS_EACH_SCAN is not set and
62                                    // report_threshold_percent is reached.
63     WIFI_SCAN_FAILED,              // reported when currently executing gscans have failed.
64                                    // start_gscan will need to be called again in order to continue
65                                    // scanning. This is intended to indicate abnormal scan
66                                    // terminations (not those as a result of stop_gscan).
67 } wifi_scan_event;
68 
69 
70 /* Format of information elements found in the beacon */
71 typedef struct {
72     byte id;                            // element identifier
73     byte len;                           // number of bytes to follow
74     byte data[];
75 } wifi_information_element;
76 
77 typedef struct {
78     wifi_timestamp ts;                  // time since boot (in microsecond) when the result was
79                                         // retrieved
80     char ssid[32+1];                    // null terminated
81     mac_addr bssid;
82     wifi_channel channel;               // channel frequency in MHz
83     wifi_rssi rssi;                     // in db
84     wifi_timespan rtt;                  // in nanoseconds
85     wifi_timespan rtt_sd;               // standard deviation in rtt
86     unsigned short beacon_period;       // period advertised in the beacon
87     unsigned short capability;          // capabilities advertised in the beacon
88     unsigned int ie_length;             // size of the ie_data blob
89     char         ie_data[1];            // blob of all the information elements found in the
90                                         // beacon; this data should be a packed list of
91                                         // wifi_information_element objects, one after the other.
92     // other fields
93 } wifi_scan_result;
94 
95 static_assert(MAX_BUCKETS <= 8 * sizeof(unsigned),
96         "The buckets_scanned bitset is represented by an unsigned int and cannot support this many "
97         "buckets on this platform.");
98 typedef struct {
99     /* reported when each probe response is received, if report_events
100      * enabled in wifi_scan_cmd_params. buckets_scanned is a bitset of the
101      * buckets that are currently being scanned. See the buckets_scanned field
102      * in the wifi_cached_scan_results struct for more details.
103      */
104     void (*on_full_scan_result) (wifi_request_id id, wifi_scan_result *result,
105                                  unsigned buckets_scanned);
106 
107     /* indicates progress of scanning statemachine */
108     void (*on_scan_event) (wifi_request_id id, wifi_scan_event event);
109 
110 } wifi_scan_result_handler;
111 
112 typedef struct {
113     wifi_channel channel;               // frequency
114     int dwellTimeMs;                    // dwell time hint
115     int passive;                        // 0 => active, 1 => passive scan; ignored for DFS
116     /* Add channel class */
117 } wifi_scan_channel_spec;
118 
119 #define REPORT_EVENTS_EACH_SCAN        (1 << 0)
120 #define REPORT_EVENTS_FULL_RESULTS     (1 << 1)
121 #define REPORT_EVENTS_NO_BATCH         (1 << 2)
122 
123 typedef struct {
124     int bucket;                         // bucket index, 0 based
125     wifi_band band;                     // when UNSPECIFIED, use channel list
126     int period;                         // desired period, in millisecond; if this is too
127                                         // low, the firmware should choose to generate results as
128                                         // fast as it can instead of failing the command.
129                                         // for exponential backoff bucket this is the min_period
130     /* report_events semantics -
131      *  This is a bit field; which defines following bits -
132      *  REPORT_EVENTS_EACH_SCAN    => report a scan completion event after scan. If this is not set
133      *                                 then scan completion events should be reported if
134      *                                 report_threshold_percent or report_threshold_num_scans is
135      *                                 reached.
136      *  REPORT_EVENTS_FULL_RESULTS => forward scan results (beacons/probe responses + IEs)
137      *                                 in real time to HAL, in addition to completion events
138      *                                 Note: To keep backward compatibility, fire completion
139      *                                 events regardless of REPORT_EVENTS_EACH_SCAN.
140      *  REPORT_EVENTS_NO_BATCH     => controls if scans for this bucket should be placed in the
141      *                                 history buffer
142      */
143     byte report_events;
144     int max_period; // if max_period is non zero or different than period, then this bucket is
145                     // an exponential backoff bucket and the scan period will grow exponentially
146                     // as per formula: actual_period(N) = period * (base ^ (N/step_count))
147                     // to a maximum period of max_period
148     int base;       // for exponential back off bucket: multiplier: new_period=old_period*base
149     int step_count; // for exponential back off bucket, number of scans to perform for a given
150                     // period
151 
152     int num_channels;
153     // channels to scan; these may include DFS channels
154     // Note that a given channel may appear in multiple buckets
155     wifi_scan_channel_spec channels[MAX_CHANNELS];
156 } wifi_scan_bucket_spec;
157 
158 typedef struct {
159     int base_period;                    // base timer period in ms
160     int max_ap_per_scan;                // number of access points to store in each scan entry in
161                                         // the BSSID/RSSI history buffer (keep the highest RSSI
162                                         // access points)
163     int report_threshold_percent;       // in %, when scan buffer is this much full, wake up apps
164                                         // processor
165     int report_threshold_num_scans;     // in number of scans, wake up AP after these many scans
166     int num_buckets;
167     wifi_scan_bucket_spec buckets[MAX_BUCKETS];
168 } wifi_scan_cmd_params;
169 
170 /*
171  * Start periodic GSCAN
172  * When this is called all requested buckets should be scanned, starting the beginning of the cycle
173  *
174  * For example:
175  * If there are two buckets specified
176  *  - Bucket 1: period=10s
177  *  - Bucket 2: period=20s
178  *  - Bucket 3: period=30s
179  * Then the following scans should occur
180  *  - t=0  buckets 1, 2, and 3 are scanned
181  *  - t=10 bucket 1 is scanned
182  *  - t=20 bucket 1 and 2 are scanned
183  *  - t=30 bucket 1 and 3 are scanned
184  *  - t=40 bucket 1 and 2 are scanned
185  *  - t=50 bucket 1 is scanned
186  *  - t=60 buckets 1, 2, and 3 are scanned
187  *  - and the patter repeats
188  *
189  * If any scan does not occur or is incomplete (error, interrupted, etc) then a cached scan result
190  * should still be recorded with the WIFI_SCAN_FLAG_INTERRUPTED flag set.
191  */
192 wifi_error wifi_start_gscan(wifi_request_id id, wifi_interface_handle iface,
193         wifi_scan_cmd_params params, wifi_scan_result_handler handler);
194 
195 /* Stop periodic GSCAN */
196 wifi_error wifi_stop_gscan(wifi_request_id id, wifi_interface_handle iface);
197 
198 typedef enum {
199     WIFI_SCAN_FLAG_INTERRUPTED = 1      // Indicates that scan results are not complete because
200                                         // probes were not sent on some channels
201 } wifi_scan_flags;
202 
203 /* Get the GSCAN cached scan results */
204 typedef struct {
205     int scan_id;                                     // a unique identifier for the scan unit
206     int flags;                                       // a bitmask with additional
207                                                      // information about scan.
208     unsigned buckets_scanned;                        // a bitset of the buckets that were scanned.
209                                                      // for example a value of 13 (0b1101) would
210                                                      // indicate that buckets 0, 2 and 3 were
211                                                      // scanned to produce this list of results.
212                                                      // should be set to 0 if this information is
213                                                      // not available.
214     int num_results;                                 // number of bssids retrieved by the scan
215     wifi_scan_result results[MAX_AP_CACHE_PER_SCAN]; // scan results - one for each bssid
216 } wifi_cached_scan_results;
217 
218 wifi_error wifi_get_cached_gscan_results(wifi_interface_handle iface, byte flush,
219         int max, wifi_cached_scan_results *results, int *num);
220 
221 /* BSSID Hotlist */
222 typedef struct {
223     void (*on_hotlist_ap_found)(wifi_request_id id,
224             unsigned num_results, wifi_scan_result *results);
225     void (*on_hotlist_ap_lost)(wifi_request_id id,
226             unsigned num_results, wifi_scan_result *results);
227 } wifi_hotlist_ap_found_handler;
228 
229 typedef struct {
230     mac_addr  bssid;                    // AP BSSID
231     wifi_rssi low;                      // low threshold
232     wifi_rssi high;                     // high threshold
233 } ap_threshold_param;
234 
235 typedef struct {
236     int lost_ap_sample_size;
237     int num_bssid;                                 // number of hotlist APs
238     ap_threshold_param ap[MAX_HOTLIST_APS];     // hotlist APs
239 } wifi_bssid_hotlist_params;
240 
241 /* Set the BSSID Hotlist */
242 wifi_error wifi_set_bssid_hotlist(wifi_request_id id, wifi_interface_handle iface,
243         wifi_bssid_hotlist_params params, wifi_hotlist_ap_found_handler handler);
244 
245 /* Clear the BSSID Hotlist */
246 wifi_error wifi_reset_bssid_hotlist(wifi_request_id id, wifi_interface_handle iface);
247 
248 /* SSID Hotlist */
249 typedef struct {
250     void (*on_hotlist_ssid_found)(wifi_request_id id,
251             unsigned num_results, wifi_scan_result *results);
252     void (*on_hotlist_ssid_lost)(wifi_request_id id,
253             unsigned num_results, wifi_scan_result *results);
254 } wifi_hotlist_ssid_handler;
255 
256 typedef struct {
257     char  ssid[32+1];                   // SSID
258     wifi_band band;                     // band for this set of threshold params
259     wifi_rssi low;                      // low threshold
260     wifi_rssi high;                     // high threshold
261 } ssid_threshold_param;
262 
263 typedef struct {
264     int lost_ssid_sample_size;
265     int num_ssid;                                   // number of hotlist SSIDs
266     ssid_threshold_param ssid[MAX_HOTLIST_SSID];    // hotlist SSIDs
267 } wifi_ssid_hotlist_params;
268 
269 /* Significant wifi change */
270 typedef struct {
271     mac_addr bssid;                     // BSSID
272     wifi_channel channel;               // channel frequency in MHz
273     int num_rssi;                       // number of rssi samples
274     wifi_rssi rssi[];                   // RSSI history in db
275 } wifi_significant_change_result;
276 
277 typedef struct {
278     void (*on_significant_change)(wifi_request_id id,
279             unsigned num_results, wifi_significant_change_result **results);
280 } wifi_significant_change_handler;
281 
282 // The sample size parameters in the wifi_significant_change_params structure
283 // represent the number of occurence of a g-scan where the BSSID was seen and RSSI was
284 // collected for that BSSID, or, the BSSID was expected to be seen and didn't.
285 // for instance: lost_ap_sample_size : number of time a g-scan was performed on the
286 // channel the BSSID was seen last, and the BSSID was not seen during those g-scans
287 typedef struct {
288     int rssi_sample_size;               // number of samples for averaging RSSI
289     int lost_ap_sample_size;            // number of samples to confirm AP loss
290     int min_breaching;                  // number of APs breaching threshold
291     int num_bssid;                         // max 64
292     ap_threshold_param ap[MAX_SIGNIFICANT_CHANGE_APS];
293 } wifi_significant_change_params;
294 
295 /* Set the Signifcant AP change list */
296 wifi_error wifi_set_significant_change_handler(wifi_request_id id, wifi_interface_handle iface,
297         wifi_significant_change_params params, wifi_significant_change_handler handler);
298 
299 /* Clear the Signifcant AP change list */
300 wifi_error wifi_reset_significant_change_handler(wifi_request_id id, wifi_interface_handle iface);
301 
302 /* Random MAC OUI for PNO */
303 wifi_error wifi_set_scanning_mac_oui(wifi_interface_handle handle, oui scan_oui);
304 
305 
306 // Enhanced PNO:
307 // Enhanced PNO feature is expected to be enabled all of the time (e.g. screen lit) and may thus
308 // require firmware to store a large number of networks, covering the whole list of known networks.
309 // Therefore, it is acceptable for firmware to store a crc24, crc32 or other short hash of the SSID,
310 // such that a low but non-zero probability of collision exist. With that scheme it should be
311 // possible for firmware to keep an entry as small as 4 bytes for each pno network.
312 // For instance, a firmware pn0 entry can be implemented in the form of:
313 //          PNO ENTRY = crc24(3 bytes) | flags>>3 (5 bits) | auth flags(3 bits)
314 //
315 // No scans should be automatically performed by the chip. Instead all scan results from gscan
316 // should be scored and the wifi_epno_handler on_network_found callback should be called with
317 // the scan results.
318 //
319 // A PNO network shall be reported once, that is, once a network is reported by firmware
320 // its entry shall be marked as "done" until framework calls wifi_set_epno_list again.
321 // Calling wifi_set_epno_list shall reset the "done" status of pno networks in firmware.
322 //
323 // A network should only be considered found if its RSSI is above the minimum RSSI for its
324 // frequency range (min5GHz_rssi and min24GHz_rssi for 5GHz and 2.4GHz networks respectively).
325 // When disconnected the list of scan results should be returned if any network is found.
326 // When connected the scan results shall be reported only if the score of any network in the scan
327 // is greater than that of the currently connected BSSID.
328 //
329 // The FW should calculate the score of all the candidates (including currently connected one)
330 //   with following equation:
331 //     RSSI score = (RSSI + 85) * 4;
332 //     If RSSI score > initial_score_max , RSSI score = initial_score_max;
333 //     final score = RSSI score
334 //         + current_connection_bonus (if currently connected BSSID)
335 //         + same_network_bonus (if network has SAME_NETWORK flag)
336 //         + secure_bonus (if the network is not open)
337 //         + band5GHz_bonus (if BSSID is on 5G)
338 //     If there is a BSSID’s score > current BSSID’s score, then report the cached scan results
339 //         at the end of the scan (excluding the ones on blacklist) to the upper layer.
340 // Additionally, all BSSIDs that are in the BSSID blacklist should be ignored by Enhanced PNO
341 
342 // Whether directed scan needs to be performed (for hidden SSIDs)
343 #define WIFI_PNO_FLAG_DIRECTED_SCAN (1 << 0)
344 // Whether PNO event shall be triggered if the network is found on A band
345 #define WIFI_PNO_FLAG_A_BAND (1 << 1)
346 // Whether PNO event shall be triggered if the network is found on G band
347 #define WIFI_PNO_FLAG_G_BAND (1 << 2)
348 // Whether strict matching is required
349 // If required then the firmware must store the network's SSID and not just a hash
350 #define WIFI_PNO_FLAG_STRICT_MATCH (1 << 3)
351 // If this SSID should be considered the same network as the currently connected one for scoring
352 #define WIFI_PNO_FLAG_SAME_NETWORK (1 << 4)
353 
354 // Code for matching the beacon AUTH IE - additional codes TBD
355 #define WIFI_PNO_AUTH_CODE_OPEN  (1 << 0) // open
356 #define WIFI_PNO_AUTH_CODE_PSK   (1 << 1) // WPA_PSK or WPA2PSK
357 #define WIFI_PNO_AUTH_CODE_EAPOL (1 << 2) // any EAPOL
358 
359 typedef struct {
360     char ssid[32+1];     // null terminated
361     byte flags;          // WIFI_PNO_FLAG_XXX
362     byte auth_bit_field; // auth bit field for matching WPA IE
363 } wifi_epno_network;
364 
365 /* ePNO Parameters */
366 typedef struct {
367     int min5GHz_rssi;               // minimum 5GHz RSSI for a BSSID to be considered
368     int min24GHz_rssi;              // minimum 2.4GHz RSSI for a BSSID to be considered
369     int initial_score_max;          // the maximum score that a network can have before bonuses
370     int current_connection_bonus;   // only report when there is a network's score this much higher
371                                     // than the current connection.
372     int same_network_bonus;         // score bonus for all networks with the same network flag
373     int secure_bonus;               // score bonus for networks that are not open
374     int band5GHz_bonus;             // 5GHz RSSI score bonus (applied to all 5GHz networks)
375     int num_networks;               // number of wifi_epno_network objects
376     wifi_epno_network networks[MAX_EPNO_NETWORKS];   // PNO networks
377 } wifi_epno_params;
378 
379 typedef struct {
380     // on results
381     void (*on_network_found)(wifi_request_id id,
382             unsigned num_results, wifi_scan_result *results);
383 } wifi_epno_handler;
384 
385 
386 /* Set the ePNO list - enable ePNO with the given parameters */
387 wifi_error wifi_set_epno_list(wifi_request_id id, wifi_interface_handle iface,
388         const wifi_epno_params *epno_params, wifi_epno_handler handler);
389 
390 /* Reset the ePNO list - no ePNO networks should be matched after this */
391 wifi_error wifi_reset_epno_list(wifi_request_id id, wifi_interface_handle iface);
392 
393 
394 typedef struct {
395     int  id;                            // identifier of this network block, report this in event
396     char realm[256];                    // null terminated UTF8 encoded realm, 0 if unspecified
397     int64_t roamingConsortiumIds[16];   // roaming consortium ids to match, 0s if unspecified
398     byte plmn[3];                       // mcc/mnc combination as per rules, 0s if unspecified
399 } wifi_passpoint_network;
400 
401 typedef struct {
402     void (*on_passpoint_network_found)(
403             wifi_request_id id,
404             int net_id,                        // network block identifier for the matched network
405             wifi_scan_result *result,          // scan result, with channel and beacon information
406             int anqp_len,                      // length of ANQP blob
407             byte *anqp                         // ANQP data, in the information_element format
408             );
409 } wifi_passpoint_event_handler;
410 
411 /* Sets a list for passpoint networks for PNO purposes; it should be matched
412  * against any passpoint networks (designated by Interworking element) found
413  * during regular PNO scan. */
414 wifi_error wifi_set_passpoint_list(wifi_request_id id, wifi_interface_handle iface, int num,
415         wifi_passpoint_network *networks, wifi_passpoint_event_handler handler);
416 
417 /* Reset passpoint network list - no Passpoint networks should be matched after this */
418 wifi_error wifi_reset_passpoint_list(wifi_request_id id, wifi_interface_handle iface);
419 
420 #endif
421