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