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