xref: /aosp_15_r20/system/chre/chre_api/include/chre_api/chre/wifi.h (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2016 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 
17 // IWYU pragma: private, include "chre_api/chre.h"
18 // IWYU pragma: friend chre/.*\.h
19 
20 #ifndef _CHRE_WIFI_H_
21 #define _CHRE_WIFI_H_
22 
23 /**
24  * @file
25  * WiFi (IEEE 802.11) API, currently covering scanning features useful for
26  * determining location and offloading certain connectivity scans.
27  *
28  * In this file, specification references use the following shorthand:
29  *
30  *    Shorthand | Full specification name
31  *   ---------- | ------------------------
32  *     "802.11" | IEEE Std 802.11-2007
33  *     "HT"     | IEEE Std 802.11n-2009
34  *     "VHT"    | IEEE Std 802.11ac-2013
35  *     "WiFi 6" | IEEE Std 802.11ax draft
36  *     "NAN"    | Wi-Fi Neighbor Awareness Networking (NAN) Technical
37  *                Specification (v3.2)
38  *
39  * In the current version of CHRE API, the 6GHz band introduced in WiFi 6 is
40  * not supported. A scan request from CHRE should not result in scanning 6GHz
41  * channels. In particular, if a 6GHz channel is specified in scanning or
42  * ranging request parameter, CHRE should return an error code of
43  * CHRE_ERROR_NOT_SUPPORTED. Additionally, CHRE implementations must not include
44  * observations of access points on 6GHz channels in scan results, especially
45  * those produced due to scan monitoring.
46  */
47 
48 #include "common.h"
49 #include <chre/common.h>
50 
51 #include <stdbool.h>
52 #include <stddef.h>
53 #include <stdint.h>
54 #include <string.h>
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
60 /**
61  * The set of flags returned by chreWifiGetCapabilities().
62  * @defgroup CHRE_WIFI_CAPABILITIES
63  * @{
64  */
65 
66 //! No WiFi APIs are supported
67 #define CHRE_WIFI_CAPABILITIES_NONE              (UINT32_C(0))
68 
69 //! Listening to scan results is supported, as enabled via
70 //! chreWifiConfigureScanMonitorAsync()
71 #define CHRE_WIFI_CAPABILITIES_SCAN_MONITORING   (UINT32_C(1) << 0)
72 
73 //! Requesting WiFi scans on-demand is supported via chreWifiRequestScanAsync()
74 #define CHRE_WIFI_CAPABILITIES_ON_DEMAND_SCAN    (UINT32_C(1) << 1)
75 
76 //! Specifying the radio chain preference in on-demand scan requests, and
77 //! reporting it in scan events is supported
78 //! @since v1.2
79 #define CHRE_WIFI_CAPABILITIES_RADIO_CHAIN_PREF  (UINT32_C(1) << 2)
80 
81 //! Requesting RTT ranging is supported via chreWifiRequestRangingAsync()
82 //! @since v1.2
83 #define CHRE_WIFI_CAPABILITIES_RTT_RANGING       (UINT32_C(1) << 3)
84 
85 //! Specifies if WiFi NAN service subscription is supported. If a platform
86 //! supports subscriptions, then it must also support RTT ranging for NAN
87 //! services via chreWifiNanRequestRangingAsync()
88 //! @since v1.6
89 #define CHRE_WIFI_CAPABILITIES_NAN_SUB           (UINT32_C(1) << 4)
90 
91 /** @} */
92 
93 /**
94  * Produce an event ID in the block of IDs reserved for WiFi
95  * @param offset  Index into WiFi event ID block; valid range [0,15]
96  */
97 #define CHRE_WIFI_EVENT_ID(offset)  (CHRE_EVENT_WIFI_FIRST_EVENT + (offset))
98 
99 /**
100  * nanoappHandleEvent argument: struct chreAsyncResult
101  *
102  * Communicates the asynchronous result of a request to the WiFi API. The
103  * requestType field in {@link #chreAsyncResult} is set to a value from enum
104  * chreWifiRequestType.
105  */
106 #define CHRE_EVENT_WIFI_ASYNC_RESULT  CHRE_WIFI_EVENT_ID(0)
107 
108 /**
109  * nanoappHandleEvent argument: struct chreWifiScanEvent
110  *
111  * Provides results of a WiFi scan.
112  */
113 #define CHRE_EVENT_WIFI_SCAN_RESULT  CHRE_WIFI_EVENT_ID(1)
114 
115 /**
116  * nanoappHandleEvent argument: struct chreWifiRangingEvent
117  *
118  * Provides results of an RTT ranging request.
119  */
120 #define CHRE_EVENT_WIFI_RANGING_RESULT  CHRE_WIFI_EVENT_ID(2)
121 
122 /**
123  * nanoappHandleEvent argument: struct chreWifiNanIdentifierEvent
124  *
125  * Lets the client know if the NAN engine was able to successfully assign
126  * an identifier to the subscribe call. The 'cookie' field in the event
127  * argument struct can be used to track which subscribe request this identifier
128  * maps to.
129  */
130 #define CHRE_EVENT_WIFI_NAN_IDENTIFIER_RESULT   CHRE_WIFI_EVENT_ID(3)
131 
132 /**
133  * nanoappHandleEvent argument: struct chreWifiNanDiscoveryEvent
134  *
135  * Event that is sent whenever a NAN service matches the criteria specified
136  * in a subscription request.
137  */
138 #define CHRE_EVENT_WIFI_NAN_DISCOVERY_RESULT  CHRE_WIFI_EVENT_ID(4)
139 
140 /**
141  * nanoappHandleEvent argument: struct chreWifiNanSessionLostEvent
142  *
143  * Informs the client that a discovered service is no longer available or
144  * visible.
145  * The ID of the service on the client that was communicating with the extinct
146  * service is indicated by the event argument.
147  */
148 #define CHRE_EVENT_WIFI_NAN_SESSION_LOST  CHRE_WIFI_EVENT_ID(5)
149 
150 /**
151  * nanoappHandleEvent argument: struct chreWifiNanSessionTerminatedEvent
152  *
153  * Signals the end of a NAN subscription session. The termination can be due to
154  * the user turning the WiFi off, or other platform reasons like not being able
155  * to support NAN concurrency with the host. The terminated event will have a
156  * reason code appropriately populated to denote why the event was sent.
157  */
158 #define CHRE_EVENT_WIFI_NAN_SESSION_TERMINATED  CHRE_WIFI_EVENT_ID(6)
159 
160 // NOTE: Do not add new events with ID > 15; only values 0-15 are reserved
161 // (see chre/event.h)
162 
163 /**
164  * The maximum amount of time that is allowed to elapse between a call to
165  * chreWifiRequestScanAsync() that returns true, and the associated
166  * CHRE_EVENT_WIFI_ASYNC_RESULT used to indicate whether the scan completed
167  * successfully or not.
168  */
169 #define CHRE_WIFI_SCAN_RESULT_TIMEOUT_NS  (30 * CHRE_NSEC_PER_SEC)
170 
171 /**
172  * The maximum amount of time that is allowed to elapse between a call to
173  * chreWifiRequestRangingAsync() that returns true, and the associated
174  * CHRE_EVENT_WIFI_RANGING_RESULT used to indicate whether the ranging operation
175  * completed successfully or not.
176  */
177 #define CHRE_WIFI_RANGING_RESULT_TIMEOUT_NS  (30 * CHRE_NSEC_PER_SEC)
178 
179 /**
180  * The current compatibility version of the chreWifiScanEvent structure,
181  * including nested structures.
182  */
183 #define CHRE_WIFI_SCAN_EVENT_VERSION  UINT8_C(1)
184 
185 /**
186  * The current compatibility version of the chreWifiRangingEvent structure,
187  * including nested structures.
188  */
189 #define CHRE_WIFI_RANGING_EVENT_VERSION  UINT8_C(0)
190 
191 /**
192  * Maximum number of frequencies that can be explicitly specified when
193  * requesting a scan
194  * @see #chreWifiScanParams
195  */
196 #define CHRE_WIFI_FREQUENCY_LIST_MAX_LEN  (20)
197 
198 /**
199  * Maximum number of SSIDs that can be explicitly specified when requesting a
200  * scan
201  * @see #chreWifiScanParams
202  */
203 #define CHRE_WIFI_SSID_LIST_MAX_LEN  (20)
204 
205 /**
206  * The maximum number of devices that can be specified in a single RTT ranging
207  * request.
208  * @see #chreWifiRangingParams
209  */
210 #define CHRE_WIFI_RANGING_LIST_MAX_LEN  (10)
211 
212 /**
213  * The maximum number of octets in an SSID (see 802.11 7.3.2.1)
214  */
215 #define CHRE_WIFI_SSID_MAX_LEN  (32)
216 
217 /**
218  * The number of octets in a BSSID (see 802.11 7.1.3.3.3)
219  */
220 #define CHRE_WIFI_BSSID_LEN  (6)
221 
222 /**
223  * Set of flags which can either indicate a frequency band. Specified as a bit
224  * mask to allow for combinations in future API versions.
225  * @defgroup CHRE_WIFI_BAND_MASK
226  * @{
227  */
228 
229 #define CHRE_WIFI_BAND_MASK_2_4_GHZ  (UINT8_C(1) << 0)  //!< 2.4 GHz
230 #define CHRE_WIFI_BAND_MASK_5_GHZ    (UINT8_C(1) << 1)  //!< 5 GHz
231 
232 /** @} */
233 
234 /**
235  * Characteristics of a scanned device given in struct chreWifiScanResult.flags
236  * @defgroup CHRE_WIFI_SCAN_RESULT_FLAGS
237  * @{
238  */
239 
240 #define CHRE_WIFI_SCAN_RESULT_FLAGS_NONE                       UINT8_C(0)
241 
242 //! Element ID 61 (HT Operation) is present (see HT 7.3.2)
243 #define CHRE_WIFI_SCAN_RESULT_FLAGS_HT_OPS_PRESENT             (UINT8_C(1) << 0)
244 
245 //! Element ID 192 (VHT Operation) is present (see VHT 8.4.2)
246 #define CHRE_WIFI_SCAN_RESULT_FLAGS_VHT_OPS_PRESENT            (UINT8_C(1) << 1)
247 
248 //! Element ID 127 (Extended Capabilities) is present, and bit 70 (Fine Timing
249 //! Measurement Responder) is set to 1 (see IEEE Std 802.11-2016 9.4.2.27)
250 #define CHRE_WIFI_SCAN_RESULT_FLAGS_IS_FTM_RESPONDER           (UINT8_C(1) << 2)
251 
252 //! Retained for backwards compatibility
253 //! @see CHRE_WIFI_SCAN_RESULT_FLAGS_IS_FTM_RESPONDER
254 #define CHRE_WIFI_SCAN_RESULT_FLAGS_IS_80211MC_RTT_RESPONDER \
255     CHRE_WIFI_SCAN_RESULT_FLAGS_IS_FTM_RESPONDER
256 
257 //! HT Operation element indicates that a secondary channel is present
258 //! (see HT 7.3.2.57)
259 #define CHRE_WIFI_SCAN_RESULT_FLAGS_HAS_SECONDARY_CHANNEL_OFFSET \
260                                                                (UINT8_C(1) << 3)
261 
262 //! HT Operation element indicates that the secondary channel is below the
263 //! primary channel (see HT 7.3.2.57)
264 #define CHRE_WIFI_SCAN_RESULT_FLAGS_SECONDARY_CHANNEL_OFFSET_IS_BELOW  \
265                                                                (UINT8_C(1) << 4)
266 
267 /** @} */
268 
269 /**
270  * Identifies the authentication methods supported by an AP. Note that not every
271  * combination of flags may be possible. Based on WIFI_PNO_AUTH_CODE_* from
272  * hardware/libhardware_legacy/include/hardware_legacy/gscan.h in Android.
273  * @defgroup CHRE_WIFI_SECURITY_MODE_FLAGS
274  * @{
275  */
276 
277 #define CHRE_WIFI_SECURITY_MODE_UNKNOWN  (UINT8_C(0))
278 //! @deprecated since v1.10. Use CHRE_WIFI_SECURITY_MODE_UNKNOWN instead.
279 #define CHRE_WIFI_SECURITY_MODE_UNKONWN  CHRE_WIFI_SECURITY_MODE_UNKNOWN
280 
281 #define CHRE_WIFI_SECURITY_MODE_OPEN (UINT8_C(1) << 0)  //!< No auth/security
282 #define CHRE_WIFI_SECURITY_MODE_WEP  (UINT8_C(1) << 1)
283 #define CHRE_WIFI_SECURITY_MODE_PSK  (UINT8_C(1) << 2)  //!< WPA-PSK or WPA2-PSK
284 #define CHRE_WIFI_SECURITY_MODE_EAP  (UINT8_C(1) << 3)  //!< WPA-EAP or WPA2-EAP
285 
286 //! @since v1.5
287 #define CHRE_WIFI_SECURITY_MODE_SAE  (UINT8_C(1) << 4)
288 
289 //! @since v1.5
290 #define CHRE_WIFI_SECURITY_MODE_EAP_SUITE_B  (UINT8_C(1) << 5)
291 
292 //! @since v1.5
293 #define CHRE_WIFI_SECURITY_MODE_OWE  (UINT8_C(1) << 6)
294 
295 /** @} */
296 
297 /**
298  * Identifies which radio chain was used to discover an AP. The underlying
299  * hardware does not necessarily support more than one radio chain.
300  * @defgroup CHRE_WIFI_RADIO_CHAIN_FLAGS
301  * @{
302  */
303 
304 #define CHRE_WIFI_RADIO_CHAIN_UNKNOWN  (UINT8_C(0))
305 #define CHRE_WIFI_RADIO_CHAIN_0        (UINT8_C(1) << 0)
306 #define CHRE_WIFI_RADIO_CHAIN_1        (UINT8_C(1) << 1)
307 
308 /** @} */
309 
310 //! Special value indicating that an LCI uncertainty fields is not provided
311 //! Ref: RFC 6225
312 #define CHRE_WIFI_LCI_UNCERTAINTY_UNKNOWN  UINT8_C(0)
313 
314 /**
315  * Defines the flags that may be returned in
316  * {@link #chreWifiRangingResult.flags}. Undefined bits are reserved for future
317  * use and must be ignored by nanoapps.
318  * @defgroup CHRE_WIFI_RTT_RESULT_FLAGS
319  * @{
320  */
321 
322 //! If set, the nested chreWifiLci structure is populated; otherwise it is
323 //! invalid and must be ignored
324 #define CHRE_WIFI_RTT_RESULT_HAS_LCI  (UINT8_C(1) << 0)
325 
326 /** @} */
327 
328 /**
329  * Identifies a WiFi frequency band
330  */
331 enum chreWifiBand {
332     CHRE_WIFI_BAND_2_4_GHZ = CHRE_WIFI_BAND_MASK_2_4_GHZ,
333     CHRE_WIFI_BAND_5_GHZ   = CHRE_WIFI_BAND_MASK_5_GHZ,
334 };
335 
336 /**
337  * Indicates the BSS operating channel width determined from the VHT and/or HT
338  * Operation elements. Refer to VHT 8.4.2.161 and HT 7.3.2.57.
339  */
340 enum chreWifiChannelWidth {
341     CHRE_WIFI_CHANNEL_WIDTH_20_MHZ         = 0,
342     CHRE_WIFI_CHANNEL_WIDTH_40_MHZ         = 1,
343     CHRE_WIFI_CHANNEL_WIDTH_80_MHZ         = 2,
344     CHRE_WIFI_CHANNEL_WIDTH_160_MHZ        = 3,
345     CHRE_WIFI_CHANNEL_WIDTH_80_PLUS_80_MHZ = 4,
346 };
347 
348 /**
349  * Indicates the type of scan requested or performed
350  */
351 enum chreWifiScanType {
352     //! Perform a purely active scan using probe requests. Do not scan channels
353     //! restricted to use via Dynamic Frequency Selection (DFS) only.
354     CHRE_WIFI_SCAN_TYPE_ACTIVE = 0,
355 
356     //! Perform an active scan on unrestricted channels, and also perform a
357     //! passive scan on channels that are restricted to use via Dynamic
358     //! Frequency Selection (DFS), e.g. the U-NII bands 5250-5350MHz and
359     //! 5470-5725MHz in the USA as mandated by FCC regulation.
360     CHRE_WIFI_SCAN_TYPE_ACTIVE_PLUS_PASSIVE_DFS = 1,
361 
362     //! Perform a passive scan, only listening for beacons.
363     CHRE_WIFI_SCAN_TYPE_PASSIVE = 2,
364 
365     //! Client has no preference for a particular scan type.
366     //! Only valid in a {@link #chreWifiScanParams}.
367     //!
368     //! On a v1.4 or earlier platform, this will fall back to
369     //! CHRE_WIFI_SCAN_TYPE_ACTIVE if {@link #chreWifiScanParams.channelSet} is
370     //! set to CHRE_WIFI_CHANNEL_SET_NON_DFS, and to
371     //! CHRE_WIFI_SCAN_TYPE_ACTIVE_PLUS_PASSIVE_DFS otherwise.
372     //!
373     //! If CHRE_WIFI_CAPABILITIES_RADIO_CHAIN_PREF is supported, a v1.5 or
374     //! later platform shall perform a type of scan optimized for {@link
375     //! #chreWifiScanParams.radioChainPref}.
376     //!
377     //! Clients are strongly encouraged to set this value in {@link
378     //! #chreWifiScanParams.scanType} and instead express their preferences
379     //! through {@link #chreWifiRadioChainPref} and {@link #chreWifiChannelSet}
380     //! so the platform can best optimize power and performance.
381     //!
382     //! @since v1.5
383     CHRE_WIFI_SCAN_TYPE_NO_PREFERENCE = 3,
384 };
385 
386 /**
387  * Indicates whether RTT ranging with a specific device succeeded
388  */
389 enum chreWifiRangingStatus {
390     //! Ranging completed successfully
391     CHRE_WIFI_RANGING_STATUS_SUCCESS = 0,
392 
393     //! Ranging failed due to an unspecified error
394     CHRE_WIFI_RANGING_STATUS_ERROR   = 1,
395 };
396 
397 /**
398  * Possible values for {@link #chreWifiLci.altitudeType}. Ref: RFC 6225 2.4
399  */
400 enum chreWifiLciAltitudeType {
401     CHRE_WIFI_LCI_ALTITUDE_TYPE_UNKNOWN = 0,
402     CHRE_WIFI_LCI_ALTITUDE_TYPE_METERS  = 1,
403     CHRE_WIFI_LCI_ALTITUDE_TYPE_FLOORS  = 2,
404 };
405 
406 /**
407  * Indicates a type of request made in this API. Used to populate the resultType
408  * field of struct chreAsyncResult sent with CHRE_EVENT_WIFI_ASYNC_RESULT.
409  */
410 enum chreWifiRequestType {
411     CHRE_WIFI_REQUEST_TYPE_CONFIGURE_SCAN_MONITOR = 1,
412     CHRE_WIFI_REQUEST_TYPE_REQUEST_SCAN           = 2,
413     CHRE_WIFI_REQUEST_TYPE_RANGING                = 3,
414     CHRE_WIFI_REQUEST_TYPE_NAN_SUBSCRIBE          = 4,
415 };
416 
417 /**
418  * Allows a nanoapp to express its preference for how multiple available
419  * radio chains should be used when performing an on-demand scan. This is only a
420  * preference from the nanoapp and is not guaranteed to be honored by the WiFi
421  * firmware.
422  */
423 enum chreWifiRadioChainPref {
424     //! No preference for radio chain usage
425     CHRE_WIFI_RADIO_CHAIN_PREF_DEFAULT = 0,
426 
427     //! In a scan result, indicates that the radio chain preference used for the
428     //! scan is not known
429     CHRE_WIFI_RADIO_CHAIN_PREF_UNKNOWN = CHRE_WIFI_RADIO_CHAIN_PREF_DEFAULT,
430 
431     //! Prefer to use available radio chains in a way that minimizes time to
432     //! complete the scan
433     CHRE_WIFI_RADIO_CHAIN_PREF_LOW_LATENCY = 1,
434 
435     //! Prefer to use available radio chains in a way that minimizes total power
436     //! consumed for the scan
437     CHRE_WIFI_RADIO_CHAIN_PREF_LOW_POWER = 2,
438 
439     //! Prefer to use available radio chains in a way that maximizes accuracy of
440     //! the scan result, e.g. RSSI measurements
441     CHRE_WIFI_RADIO_CHAIN_PREF_HIGH_ACCURACY = 3,
442 };
443 
444 /**
445  * WiFi NAN subscription type.
446  */
447 enum chreWifiNanSubscribeType {
448     //! In the active mode, explicit transmission of a subscribe message is
449     //! requested, and publish messages are processed.
450     CHRE_WIFI_NAN_SUBSCRIBE_TYPE_ACTIVE = 0,
451 
452     //! In the passive mode, no transmission of a subscribe message is
453     //! requested, but received publish messages are checked for matches.
454     CHRE_WIFI_NAN_SUBSCRIBE_TYPE_PASSIVE = 1,
455 };
456 
457 /**
458  * Indicates the reason for a subscribe session termination.
459  */
460 enum chreWifiNanTerminatedReason {
461     CHRE_WIFI_NAN_TERMINATED_BY_USER_REQUEST = 0,
462     CHRE_WIFI_NAN_TERMINATED_BY_TIMEOUT = 1,
463     CHRE_WIFI_NAN_TERMINATED_BY_FAILURE = 2,
464 };
465 
466 /**
467  * SSID with an explicit length field, used when an array of SSIDs is supplied.
468  */
469 struct chreWifiSsidListItem {
470     //! Number of valid bytes in ssid. Valid range [0, CHRE_WIFI_SSID_MAX_LEN]
471     uint8_t ssidLen;
472 
473     //! Service Set Identifier (SSID)
474     uint8_t ssid[CHRE_WIFI_SSID_MAX_LEN];
475 };
476 
477 /**
478  * Indicates the set of channels to be scanned.
479  *
480  * @since v1.5
481  */
482 enum chreWifiChannelSet {
483     //! The set of channels that allows active scan using probe request.
484     CHRE_WIFI_CHANNEL_SET_NON_DFS = 0,
485 
486     //! The set of all channels supported.
487     CHRE_WIFI_CHANNEL_SET_ALL = 1,
488 };
489 
490 /**
491  * Data structure passed to chreWifiRequestScanAsync
492  */
493 struct chreWifiScanParams {
494     //! Set to a value from @ref enum chreWifiScanType
495     uint8_t scanType;
496 
497     //! Indicates whether the client is willing to tolerate receiving cached
498     //! results of a previous scan, and if so, the maximum age of the scan that
499     //! the client will accept. "Age" in this case is defined as the elapsed
500     //! time between when the most recent scan was completed and the request is
501     //! received, in milliseconds. If set to 0, no cached results may be
502     //! provided, and all scan results must come from a "fresh" WiFi scan, i.e.
503     //! one that completes strictly after this request is received. If more than
504     //! one scan is cached and meets this age threshold, only the newest scan is
505     //! provided.
506     uint32_t maxScanAgeMs;
507 
508     //! If set to 0, scan all frequencies. Otherwise, this indicates the number
509     //! of frequencies to scan, as specified in the frequencyList array. Valid
510     //! range [0, CHRE_WIFI_FREQUENCY_LIST_MAX_LEN].
511     uint16_t frequencyListLen;
512 
513     //! Pointer to an array of frequencies to scan, given as channel center
514     //! frequencies in MHz. This field may be NULL if frequencyListLen is 0.
515     const uint32_t *frequencyList;
516 
517     //! If set to 0, do not restrict scan to any SSIDs. Otherwise, this
518     //! indicates the number of SSIDs in the ssidList array to be used for
519     //! directed probe requests. Not applicable and ignore when scanType is
520     //! CHRE_WIFI_SCAN_TYPE_PASSIVE.
521     uint8_t ssidListLen;
522 
523     //! Pointer to an array of SSIDs to use for directed probe requests. May be
524     //! NULL if ssidListLen is 0.
525     const struct chreWifiSsidListItem *ssidList;
526 
527     //! Set to a value from enum chreWifiRadioChainPref to specify the desired
528     //! trade-off between power consumption, accuracy, etc. If
529     //! chreWifiGetCapabilities() does not have the applicable bit set, this
530     //! parameter is ignored.
531     //! @since v1.2
532     uint8_t radioChainPref;
533 
534     //! Set to a value from enum chreWifiChannelSet to specify the set of
535     //! channels to be scanned. This field is considered by the platform only
536     //! if scanType is CHRE_WIFI_SCAN_TYPE_NO_PREFERENCE and frequencyListLen
537     //! is equal to zero.
538     //!
539     //! @since v1.5
540     uint8_t channelSet;
541 };
542 
543 /**
544  * Provides information about a single access point (AP) detected in a scan.
545  */
546 struct chreWifiScanResult {
547     //! Number of milliseconds prior to referenceTime in the enclosing
548     //! chreWifiScanEvent struct when the probe response or beacon frame that
549     //! was used to populate this structure was received.
550     uint32_t ageMs;
551 
552     //! Capability Information field sent by the AP (see 802.11 7.3.1.4). This
553     //! field must reflect native byte order and bit ordering, such that
554     //! (capabilityInfo & 1) gives the bit for the ESS subfield.
555     uint16_t capabilityInfo;
556 
557     //! Number of valid bytes in ssid. Valid range [0, CHRE_WIFI_SSID_MAX_LEN]
558     uint8_t ssidLen;
559 
560     //! Service Set Identifier (SSID), a series of 0 to 32 octets identifying
561     //! the access point. Note that this is commonly a human-readable ASCII
562     //! string, but this is not the required encoding per the standard.
563     uint8_t ssid[CHRE_WIFI_SSID_MAX_LEN];
564 
565     //! Basic Service Set Identifier (BSSID), represented in big-endian byte
566     //! order, such that the first octet of the OUI is accessed in byte index 0.
567     uint8_t bssid[CHRE_WIFI_BSSID_LEN];
568 
569     //! A set of flags from CHRE_WIFI_SCAN_RESULT_FLAGS_*
570     uint8_t flags;
571 
572     //! RSSI (Received Signal Strength Indicator), in dBm. Typically negative.
573     //! If multiple radio chains were used to scan this AP, this is a "best
574     //! available" measure that may be a composite of measurements taken across
575     //! the radio chains.
576     int8_t  rssi;
577 
578     //! Operating band, set to a value from enum chreWifiBand
579     uint8_t band;
580 
581     /**
582      * Indicates the center frequency of the primary 20MHz channel, given in
583      * MHz. This value is derived from the channel number via the formula:
584      *
585      *     primaryChannel (MHz) = CSF + 5 * primaryChannelNumber
586      *
587      * Where CSF is the channel starting frequency (in MHz) given by the
588      * operating class/band (i.e. 2407 or 5000), and primaryChannelNumber is the
589      * channel number in the range [1, 200].
590      *
591      * Refer to VHT 22.3.14.
592      */
593     uint32_t primaryChannel;
594 
595     /**
596      * If the channel width is 20 MHz, this field is not relevant and set to 0.
597      * If the channel width is 40, 80, or 160 MHz, then this denotes the channel
598      * center frequency (in MHz). If the channel is 80+80 MHz, then this denotes
599      * the center frequency of segment 0, which contains the primary channel.
600      * This value is derived from the frequency index using the same formula as
601      * for primaryChannel.
602      *
603      * Refer to VHT 8.4.2.161, and VHT 22.3.14.
604      *
605      * @see #primaryChannel
606      */
607     uint32_t centerFreqPrimary;
608 
609     /**
610      * If the channel width is 80+80MHz, then this denotes the center frequency
611      * of segment 1, which does not contain the primary channel. Otherwise, this
612      * field is not relevant and set to 0.
613      *
614      * @see #centerFreqPrimary
615      */
616     uint32_t centerFreqSecondary;
617 
618     //! @see #chreWifiChannelWidth
619     uint8_t channelWidth;
620 
621     //! Flags from CHRE_WIFI_SECURITY_MODE_* indicating supported authentication
622     //! and associated security modes
623     //! @see CHRE_WIFI_SECURITY_MODE_FLAGS
624     uint8_t securityMode;
625 
626     //! Identifies the radio chain(s) used to discover this AP
627     //! @see CHRE_WIFI_RADIO_CHAIN_FLAGS
628     //! @since v1.2
629     uint8_t radioChain;
630 
631     //! If the CHRE_WIFI_RADIO_CHAIN_0 bit is set in radioChain, gives the RSSI
632     //! measured on radio chain 0 in dBm; otherwise invalid and set to 0. This
633     //! field, along with its relative rssiChain1, can be used to determine RSSI
634     //! measurements from each radio chain when multiple chains were used to
635     //! discover this AP.
636     //! @see #radioChain
637     //! @since v1.2
638     int8_t rssiChain0;
639     int8_t rssiChain1;  //!< @see #rssiChain0
640 
641     //! Reserved; set to 0
642     uint8_t reserved[7];
643 };
644 
645 /**
646  * Data structure sent with events of type CHRE_EVENT_WIFI_SCAN_RESULT.
647  */
648 struct chreWifiScanEvent {
649     //! Indicates the version of the structure, for compatibility purposes.
650     //! Clients do not normally need to worry about this field; the CHRE
651     //! implementation guarantees that the client only receives the structure
652     //! version it expects.
653     uint8_t version;
654 
655     //! The number of entries in the results array in this event. The CHRE
656     //! implementation may split scan results across multiple events for memory
657     //! concerns, etc.
658     uint8_t resultCount;
659 
660     //! The total number of results returned by the scan. Allows an event
661     //! consumer to identify when it has received all events associated with a
662     //! scan.
663     uint8_t resultTotal;
664 
665     //! Sequence number for this event within the series of events comprising a
666     //! complete scan result. Scan events are delivered strictly in order, i.e.
667     //! this is monotonically increasing for the results of a single scan. Valid
668     //! range [0, <number of events for scan> - 1]. The number of events for a
669     //! scan is typically given by
670     //! ceil(resultTotal / <max results per event supported by platform>).
671     uint8_t eventIndex;
672 
673     //! A value from enum chreWifiScanType indicating the type of scan performed
674     uint8_t scanType;
675 
676     //! If a directed scan was performed to a limited set of SSIDs, then this
677     //! identifies the number of unique SSIDs included in the probe requests.
678     //! Otherwise, this is set to 0, indicating that the scan was not limited by
679     //! SSID. Note that if this is non-zero, the list of SSIDs used is not
680     //! included in the scan event.
681     uint8_t ssidSetSize;
682 
683     //! If 0, indicates that all frequencies applicable for the scanType were
684     //! scanned. Otherwise, indicates the number of frequencies scanned, as
685     //! specified in scannedFreqList.
686     uint16_t scannedFreqListLen;
687 
688     //! Timestamp when the scan was completed, from the same time base as
689     //! chreGetTime() (in nanoseconds)
690     uint64_t referenceTime;
691 
692     //! Pointer to an array containing scannedFreqListLen values comprising the
693     //! set of frequencies that were scanned. Frequencies are specified as
694     //! channel center frequencies in MHz. May be NULL if scannedFreqListLen is
695     //! 0.
696     const uint32_t *scannedFreqList;
697 
698     //! Pointer to an array containing resultCount entries. May be NULL if
699     //! resultCount is 0.
700     const struct chreWifiScanResult *results;
701 
702     //! Set to a value from enum chreWifiRadioChainPref indicating the radio
703     //! chain preference used for the scan. If the applicable bit is not set in
704     //! chreWifiGetCapabilities(), this will always be set to
705     //! CHRE_WIFI_RADIO_CHAIN_PREF_UNKNOWN.
706     //! @since v1.2
707     uint8_t radioChainPref;
708 };
709 
710 /**
711  * Identifies a device to perform RTT ranging against. These values are normally
712  * populated based on the contents of a scan result.
713  * @see #chreWifiScanResult
714  * @see chreWifiRangingTargetFromScanResult()
715  */
716 struct chreWifiRangingTarget {
717     //! Device MAC address, specified in the same byte order as
718     //! {@link #chreWifiScanResult.bssid}
719     uint8_t macAddress[CHRE_WIFI_BSSID_LEN];
720 
721     //! Center frequency of the primary 20MHz channel, in MHz
722     //! @see #chreWifiScanResult.primaryChannel
723     uint32_t primaryChannel;
724 
725     //! Channel center frequency, in MHz, or 0 if not relevant
726     //! @see #chreWifiScanResult.centerFreqPrimary
727     uint32_t centerFreqPrimary;
728 
729     //! Channel center frequency of segment 1 if channel width is 80+80MHz,
730     //! otherwise 0
731     //! @see #chreWifiScanResult.centerFreqSecondary
732     uint32_t centerFreqSecondary;
733 
734     //! @see #chreWifiChannelWidth
735     uint8_t channelWidth;
736 
737     //! Reserved for future use and ignored by CHRE
738     uint8_t reserved[3];
739 };
740 
741 /**
742  * Parameters for an RTT ("Fine Timing Measurement" in terms of 802.11-2016)
743  * ranging request, supplied to chreWifiRequestRangingAsync().
744  */
745 struct chreWifiRangingParams {
746     //! Number of devices to perform ranging against and the length of
747     //! targetList, in range [1, CHRE_WIFI_RANGING_LIST_MAX_LEN].
748     uint8_t targetListLen;
749 
750     //! Array of macAddressListLen MAC addresses (e.g. BSSIDs) with which to
751     //! attempt RTT ranging.
752     const struct chreWifiRangingTarget *targetList;
753 };
754 
755 /**
756  * Provides the result of RTT ranging with a single device.
757  */
758 struct chreWifiRangingResult {
759     //! Time when the ranging operation on this device was performed, in the
760     //! same time base as chreGetTime() (in nanoseconds)
761     uint64_t timestamp;
762 
763     //! MAC address of the device for which ranging was requested
764     uint8_t macAddress[CHRE_WIFI_BSSID_LEN];
765 
766     //! Gives the result of ranging to this device. If not set to
767     //! CHRE_WIFI_RANGING_STATUS_SUCCESS, the ranging attempt to this device
768     //! failed, and other fields in this structure may be invalid.
769     //! @see #chreWifiRangingStatus
770     uint8_t status;
771 
772     //! The mean RSSI measured during the RTT burst, in dBm. Typically negative.
773     //! If status is not CHRE_WIFI_RANGING_STATUS_SUCCESS, will be set to 0.
774     int8_t rssi;
775 
776     //! Estimated distance to the device with the given BSSID, in millimeters.
777     //! Generally the mean of multiple measurements performed in a single burst.
778     //! If status is not CHRE_WIFI_RANGING_STATUS_SUCCESS, will be set to 0.
779     uint32_t distance;
780 
781     //! Standard deviation of estimated distance across multiple measurements
782     //! performed in a single RTT burst, in millimeters. If status is not
783     //! CHRE_WIFI_RANGING_STATUS_SUCCESS, will be set to 0.
784     uint32_t distanceStdDev;
785 
786     //! Location Configuration Information (LCI) information optionally returned
787     //! during the ranging procedure. Only valid if {@link #flags} has the
788     //! CHRE_WIFI_RTT_RESULT_HAS_LCI bit set. Refer to IEEE 802.11-2016
789     //! 9.4.2.22.10, 11.24.6.7, and RFC 6225 (July 2011) for more information.
790     //! Coordinates are to be interpreted according to the WGS84 datum.
791     struct chreWifiLci {
792         //! Latitude in degrees as 2's complement fixed-point with 25 fractional
793         //! bits, i.e. degrees * 2^25. Ref: RFC 6225 2.3
794         int64_t latitude;
795 
796         //! Longitude, same format as {@link #latitude}
797         int64_t longitude;
798 
799         //! Altitude represented as a 2's complement fixed-point value with 8
800         //! fractional bits. Interpretation depends on {@link #altitudeType}. If
801         //! UNKNOWN, this field must be ignored. If *METERS, distance relative
802         //! to the zero point in the vertical datum. If *FLOORS, a floor value
803         //! relative to the ground floor, potentially fractional, e.g. to
804         //! indicate mezzanine levels. Ref: RFC 6225 2.4
805         int32_t altitude;
806 
807         //! Maximum extent of latitude uncertainty in degrees, decoded via this
808         //! formula: 2 ^ (8 - x) where "x" is the encoded value passed in this
809         //! field. Unknown if set to CHRE_WIFI_LCI_UNCERTAINTY_UNKNOWN.
810         //! Ref: RFC 6225 2.3.2
811         uint8_t latitudeUncertainty;
812 
813         //! @see #latitudeUncertainty
814         uint8_t longitudeUncertainty;
815 
816         //! Defines how to interpret altitude, set to a value from enum
817         //! chreWifiLciAltitudeType
818         uint8_t altitudeType;
819 
820         //! Uncertainty in altitude, decoded via this formula: 2 ^ (21 - x)
821         //! where "x" is the encoded value passed in this field. Unknown if set
822         //! to CHRE_WIFI_LCI_UNCERTAINTY_UNKNOWN. Only applies when altitudeType
823         //! is CHRE_WIFI_LCI_ALTITUDE_TYPE_METERS. Ref: RFC 6225 2.4.5
824         uint8_t altitudeUncertainty;
825     } lci;
826 
827     //! Refer to CHRE_WIFI_RTT_RESULT_FLAGS
828     uint8_t flags;
829 
830     //! Reserved; set to 0
831     uint8_t reserved[7];
832 };
833 
834 /**
835  * Data structure sent with events of type CHRE_EVENT_WIFI_RANGING_RESULT.
836  */
837 struct chreWifiRangingEvent {
838     //! Indicates the version of the structure, for compatibility purposes.
839     //! Clients do not normally need to worry about this field; the CHRE
840     //! implementation guarantees that the client only receives the structure
841     //! version it expects.
842     uint8_t version;
843 
844     //! The number of ranging results included in the results array; matches the
845     //! number of MAC addresses specified in the request
846     uint8_t resultCount;
847 
848     //! Reserved; set to 0
849     uint8_t reserved[2];
850 
851     //! Pointer to an array containing resultCount entries
852     const struct chreWifiRangingResult *results;
853 };
854 
855 /**
856  * Indicates the WiFi NAN capabilities of the device. Must contain non-zero
857  * values if WiFi NAN is supported.
858  */
859 struct chreWifiNanCapabilities {
860     //! Maximum length of the match filter arrays (applies to both tx and rx
861     //! match filters).
862     uint32_t maxMatchFilterLength;
863 
864     //! Maximum length of the service specific information byte array.
865     uint32_t maxServiceSpecificInfoLength;
866 
867     //! Maximum length of the service name. Includes the NULL terminator.
868     uint8_t maxServiceNameLength;
869 
870     //! Reserved for future use.
871     uint8_t reserved[3];
872 };
873 
874 /**
875  * Data structure sent with events of type
876  * CHRE_EVENT_WIFI_NAN_IDENTIFIER_RESULT
877  */
878 struct chreWifiNanIdentifierEvent {
879     //! A unique ID assigned by the NAN engine for the subscribe request
880     //! associated with the cookie encapsulated in the async result below. The
881     //! ID is set to 0 if there was a request failure in which case the async
882     //! result below contains the appropriate error code indicating the failure
883     //! reason.
884     uint32_t id;
885 
886     //! Structure which contains the cookie associated with the publish/
887     //! subscribe request, along with an error code that indicates request
888     //! success or failure.
889     struct chreAsyncResult result;
890 };
891 
892 /**
893  * Indicates the desired configuration for a WiFi NAN ranging request.
894  */
895 struct chreWifiNanRangingParams {
896     //! MAC address of the NAN device for which range is to be determined.
897     uint8_t macAddress[CHRE_WIFI_BSSID_LEN];
898 };
899 
900 /**
901  * Configuration parameters specific to the Subscribe Function (Spec 4.1.1.1)
902  */
903 struct chreWifiNanSubscribeConfig {
904     //! Indicates the subscribe type, set to a value from @ref
905     //! chreWifiNanSubscribeType.
906     uint8_t subscribeType;
907 
908     //! UTF-8 name string that identifies the service/application. Must be NULL
909     //! terminated. Note that the string length cannot be greater than the
910     //! maximum length specified by @ref chreWifiNanCapabilities. No
911     //! restriction is placed on the string case, since the service name
912     //! matching is expected to be case insensitive.
913     const char *service;
914 
915     //! An array of bytes (and the associated array length) of service-specific
916     //! information. Note that the array length must be less than the
917     //! maxServiceSpecificInfoLength parameter obtained from the NAN
918     //! capabilities (@see struct chreWifiNanCapabilities).
919     const uint8_t *serviceSpecificInfo;
920     uint32_t serviceSpecificInfoSize;
921 
922     //! Ordered sequence of {length | value} pairs that specify match criteria
923     //! beyond the service name. 'length' uses 1 byte, and its value indicates
924     //! the number of bytes of the match criteria that follow. The length of
925     //! the match filter array should not exceed the maximum match filter
926     //! length obtained from @ref chreWifiNanGetCapabilities. When a service
927     //! publish message discovery frame containing the Service ID being
928     //! subscribed to is received, the matching is done as follows:
929     //! Each {length | value} pair in the kth position (1 <= k <= #length-value
930     //! pairs) is compared against the kth {length | value} pair in the
931     //! matching filter field of the publish message.
932     //! - For a kth position {length | value} pair in the rx match filter with
933     //!   a length of 0, a match is declared regardless of the tx match filter
934     //!   contents.
935     //! - For a kth position {length | value} pair in the rx match with a non-
936     //!   zero length, there must be an exact match with the kth position pair
937     //!    in the match filter field of the received service descriptor for a
938     //!    match to be found.
939     //! Please refer to Appendix H of the NAN spec for examples on matching.
940     //! The match filter length should not exceed the maxMatchFilterLength
941     //! obtained from @ref chreWifiNanCapabilities.
942     const uint8_t *matchFilter;
943     uint32_t matchFilterLength;
944 };
945 
946 /**
947  * Data structure sent with events of type
948  * CHRE_EVENT_WIFI_NAN_DISCOVERY_RESULT.
949  */
950 struct chreWifiNanDiscoveryEvent {
951     //! Identifier of the subscribe function instance that requested a
952     //! discovery.
953     uint32_t subscribeId;
954 
955     //! Identifier of the publisher on the remote NAN device.
956     uint32_t publishId;
957 
958     //! NAN interface address of the publisher
959     uint8_t publisherAddress[CHRE_WIFI_BSSID_LEN];
960 
961     //! An array of bytes (and the associated array length) of service-specific
962     //! information. Note that the array length must be less than the
963     //! maxServiceSpecificInfoLength parameter obtained from the NAN
964     //! capabilities (@see struct chreWifiNanCapabilities).
965     const uint8_t *serviceSpecificInfo;
966     uint32_t serviceSpecificInfoSize;
967 };
968 
969 /**
970  * Data structure sent with events of type CHRE_EVENT_WIFI_NAN_SESSION_LOST.
971  */
972 struct chreWifiNanSessionLostEvent {
973     //! The original ID (returned by the NAN discovery engine) of the subscriber
974     //! instance.
975     uint32_t id;
976 
977     //! The ID of the previously discovered publisher on a peer NAN device that
978     //! is no longer connected.
979     uint32_t peerId;
980 };
981 
982 /**
983  * Data structure sent with events of type
984  * CHRE_EVENT_WIFI_NAN_SESSION_TERMINATED.
985  */
986 struct chreWifiNanSessionTerminatedEvent {
987     //! The original ID (returned by the NAN discovery engine) of the subscriber
988     //! instance that was terminated.
989     uint32_t id;
990 
991     //! A value that maps to one of the termination reasons in @ref enum
992     //! chreWifiNanTerminatedReason.
993     uint8_t reason;
994 
995     //! Reserved for future use.
996     uint8_t reserved[3];
997 };
998 
999 /**
1000  * Retrieves a set of flags indicating the WiFi features supported by the
1001  * current CHRE implementation. The value returned by this function must be
1002  * consistent for the entire duration of the Nanoapp's execution.
1003  *
1004  * The client must allow for more flags to be set in this response than it knows
1005  * about, for example if the implementation supports a newer version of the API
1006  * than the client was compiled against.
1007  *
1008  * @return A bitmask with zero or more CHRE_WIFI_CAPABILITIES_* flags set
1009  *
1010  * @since v1.1
1011  */
1012 uint32_t chreWifiGetCapabilities(void);
1013 
1014 /**
1015  * Retrieves device-specific WiFi NAN capabilities, and populates them in
1016  * the @ref chreWifiNanCapabilities structure.
1017  *
1018  * @param capabilities Structure into which the WiFi NAN capabilities of
1019  *        the device are populated into. Must not be NULL.
1020  * @return true if WiFi NAN is supported, false otherwise.
1021  *
1022  * @since v1.6
1023  */
1024 bool chreWifiNanGetCapabilities(struct chreWifiNanCapabilities *capabilities);
1025 
1026 /**
1027  * Nanoapps must define CHRE_NANOAPP_USES_WIFI somewhere in their build
1028  * system (e.g. Makefile) if the nanoapp needs to use the following WiFi APIs.
1029  * In addition to allowing access to these APIs, defining this macro will also
1030  * ensure CHRE enforces that all host clients this nanoapp talks to have the
1031  * required Android permissions needed to listen to WiFi data by adding metadata
1032  * to the nanoapp.
1033  */
1034 #if defined(CHRE_NANOAPP_USES_WIFI) || !defined(CHRE_IS_NANOAPP_BUILD)
1035 
1036 /**
1037  * Manages a client's request to receive the results of WiFi scans performed for
1038  * other purposes, for example scans done to maintain connectivity and scans
1039  * requested by other clients. The presence of this request has no effect on the
1040  * frequency or configuration of the WiFi scans performed - it is purely a
1041  * registration by the client to receive the results of scans that would
1042  * otherwise occur normally. This should include all available scan results,
1043  * including those that are not normally sent to the applications processor,
1044  * such as Preferred Network Offload (PNO) scans. Scan results provided because
1045  * of this registration must not contain cached results - they are always
1046  * expected to contain the fresh results from a recent scan.
1047  *
1048  * An active scan monitor subscription must persist across temporary conditions
1049  * under which no WiFi scans will be performed, for example if WiFi is
1050  * completely disabled via user-controlled settings, or if the WiFi system
1051  * restarts independently of CHRE. Likewise, a request to enable a scan monitor
1052  * subscription must succeed under normal conditions, even in circumstances
1053  * where no WiFi scans will be performed. In these cases, the scan monitor
1054  * implementation must produce scan results once the temporary condition is
1055  * cleared, for example after WiFi is enabled by the user.
1056  *
1057  * These scan results are delivered to the Nanoapp's handle event callback using
1058  * CHRE_EVENT_WIFI_SCAN_RESULT.
1059  *
1060  * An active scan monitor subscription is not necessary to receive the results
1061  * of an on-demand scan request sent via chreWifiRequestScanAsync(), and it does
1062  * not result in duplicate delivery of scan results generated from
1063  * chreWifiRequestScanAsync().
1064  *
1065  * If no monitor subscription is active at the time of a request with
1066  * enable=false, it is treated as if an active subscription was successfully
1067  * ended.
1068  *
1069  * The result of this request is delivered asynchronously via an event of type
1070  * CHRE_EVENT_WIFI_ASYNC_RESULT. Refer to the note in {@link #chreAsyncResult}
1071  * for more details.
1072  *
1073  * @param enable Set to true to enable monitoring scan results, false to
1074  *        disable
1075  * @param cookie An opaque value that will be included in the chreAsyncResult
1076  *        sent in relation to this request.
1077  * @return true if the request was accepted for processing, false otherwise
1078  *
1079  * @since v1.1
1080  * @note Requires WiFi permission
1081  */
1082 bool chreWifiConfigureScanMonitorAsync(bool enable, const void *cookie);
1083 
1084 /**
1085  * Sends an on-demand request for WiFi scan results. This may trigger a new
1086  * scan, or be entirely serviced from cache, depending on the maxScanAgeMs
1087  * parameter.
1088  *
1089  * This resulting status of this request is delivered asynchronously via an
1090  * event of type CHRE_EVENT_WIFI_ASYNC_RESULT. The result must be delivered
1091  * within CHRE_WIFI_SCAN_RESULT_TIMEOUT_NS of the this request. Refer to the
1092  * note in {@link #chreAsyncResult} for more details.
1093  *
1094  * A successful result provided in CHRE_EVENT_WIFI_ASYNC_RESULT indicates that
1095  * the scan results are ready to be delivered in a subsequent event (or events,
1096  * which arrive consecutively without any other scan results in between)
1097  * of type CHRE_EVENT_WIFI_SCAN_RESULT.
1098  *
1099  * WiFi scanning must be disabled if both "WiFi scanning" and "WiFi" settings
1100  * are disabled at the Android level. In this case, the CHRE implementation is
1101  * expected to return a result with CHRE_ERROR_FUNCTION_DISABLED.
1102  *
1103  * It is not valid for a client to request a new scan while a result is pending
1104  * based on a previous scan request from the same client. In this situation, the
1105  * CHRE implementation is expected to return a result with CHRE_ERROR_BUSY.
1106  * However, if a scan is currently pending or in progress due to a request from
1107  * another client, whether within the CHRE or otherwise, the implementation must
1108  * not fail the request for this reason. If the pending scan satisfies the
1109  * client's request parameters, then the implementation should use its results
1110  * to satisfy the request rather than scheduling a new scan.
1111  *
1112  * @param params A set of parameters for the scan request. Must not be NULL.
1113  * @param cookie An opaque value that will be included in the chreAsyncResult
1114  *        sent in relation to this request.
1115  * @return true if the request was accepted for processing, false otherwise
1116  *
1117  * @since v1.1
1118  * @note Requires WiFi permission
1119  */
1120 bool chreWifiRequestScanAsync(const struct chreWifiScanParams *params,
1121                               const void *cookie);
1122 
1123 /**
1124  * Convenience function which calls chreWifiRequestScanAsync() with a default
1125  * set of scan parameters.
1126  *
1127  * @param cookie An opaque value that will be included in the chreAsyncResult
1128  *        sent in relation to this request.
1129  * @return true if the request was accepted for processing, false otherwise
1130  *
1131  * @since v1.1
1132  * @note Requires WiFi permission
1133  */
chreWifiRequestScanAsyncDefault(const void * cookie)1134 static inline bool chreWifiRequestScanAsyncDefault(const void *cookie) {
1135     static const struct chreWifiScanParams params = {
1136         /*.scanType=*/         CHRE_WIFI_SCAN_TYPE_NO_PREFERENCE,
1137         /*.maxScanAgeMs=*/     5000,  // 5 seconds
1138         /*.frequencyListLen=*/ 0,
1139         /*.frequencyList=*/    NULL,
1140         /*.ssidListLen=*/      0,
1141         /*.ssidList=*/         NULL,
1142         /*.radioChainPref=*/   CHRE_WIFI_RADIO_CHAIN_PREF_DEFAULT,
1143         /*.channelSet=*/       CHRE_WIFI_CHANNEL_SET_NON_DFS
1144     };
1145     return chreWifiRequestScanAsync(&params, cookie);
1146 }
1147 
1148 /**
1149  * Issues a request to initiate distance measurements using round-trip time
1150  * (RTT), aka Fine Timing Measurement (FTM), to one or more devices identified
1151  * by MAC address. Within CHRE, MACs are typically the BSSIDs of scanned APs
1152  * that have the CHRE_WIFI_SCAN_RESULT_FLAGS_IS_FTM_RESPONDER flag set.
1153  *
1154  * This resulting status of this request is delivered asynchronously via an
1155  * event of type CHRE_EVENT_WIFI_ASYNC_RESULT. The result must be delivered
1156  * within CHRE_WIFI_RANGING_RESULT_TIMEOUT_NS of the this request. Refer to the
1157  * note in {@link #chreAsyncResult} for more details.
1158  *
1159  * WiFi RTT ranging must be disabled if any of the following is true:
1160  * - Both "WiFi" and "WiFi Scanning" settings are disabled at the Android level.
1161  * - The "Location" setting is disabled at the Android level.
1162  * In this case, the CHRE implementation is expected to return a result with
1163  * CHRE_ERROR_FUNCTION_DISABLED.
1164  *
1165  * A successful result provided in CHRE_EVENT_WIFI_ASYNC_RESULT indicates that
1166  * the results of ranging will be delivered in a subsequent event of type
1167  * CHRE_EVENT_WIFI_RANGING_RESULT. Note that the CHRE_EVENT_WIFI_ASYNC_RESULT
1168  * gives an overall status - for example, it is used to indicate failure if the
1169  * entire ranging request was rejected because WiFi is disabled. However, it is
1170  * valid for this event to indicate success, but RTT ranging to fail for all
1171  * requested devices - for example, they may be out of range. Therefore, it is
1172  * also necessary to check the status field in {@link #chreWifiRangingResult}.
1173  *
1174  * @param params Structure containing the parameters of the scan request,
1175  *        including the list of devices to attempt ranging.
1176  * @param cookie An opaque value that will be included in the chreAsyncResult
1177  *        sent in relation to this request.
1178  * @return true if the request was accepted for processing, false otherwise
1179  *
1180  * @since v1.2
1181  * @note Requires WiFi permission
1182  */
1183 bool chreWifiRequestRangingAsync(const struct chreWifiRangingParams *params,
1184                                  const void *cookie);
1185 
1186 /**
1187  * Helper function to populate an instance of struct chreWifiRangingTarget with
1188  * the contents of a scan result provided in struct chreWifiScanResult.
1189  * Populates other parameters that are not directly derived from the scan result
1190  * with default values.
1191  *
1192  * @param scanResult The scan result to parse as input
1193  * @param rangingTarget The RTT ranging target to populate as output
1194  *
1195  * @note Requires WiFi permission
1196  */
chreWifiRangingTargetFromScanResult(const struct chreWifiScanResult * scanResult,struct chreWifiRangingTarget * rangingTarget)1197 static inline void chreWifiRangingTargetFromScanResult(
1198         const struct chreWifiScanResult *scanResult,
1199         struct chreWifiRangingTarget *rangingTarget) {
1200     memcpy(rangingTarget->macAddress, scanResult->bssid,
1201            sizeof(rangingTarget->macAddress));
1202     rangingTarget->primaryChannel      = scanResult->primaryChannel;
1203     rangingTarget->centerFreqPrimary   = scanResult->centerFreqPrimary;
1204     rangingTarget->centerFreqSecondary = scanResult->centerFreqSecondary;
1205     rangingTarget->channelWidth        = scanResult->channelWidth;
1206 
1207     // Note that this is not strictly necessary (CHRE can see which API version
1208     // the nanoapp was built against, so it knows to ignore these fields), but
1209     // we do it here to keep things nice and tidy
1210     memset(rangingTarget->reserved, 0, sizeof(rangingTarget->reserved));
1211 }
1212 
1213 /**
1214  * Subscribe to a NAN service.
1215  *
1216  * Sends a subscription request to the NAN discovery engine with the
1217  * specified configuration parameters. If successful, a unique non-zero
1218  * subscription ID associated with this instance of the subscription
1219  * request is assigned by the NAN discovery engine. The subscription request
1220  * is active until explicitly canceled, or if the connection was interrupted.
1221  *
1222  * Note that CHRE forwards any discovery events that it receives to the
1223  * subscribe function instance, and does no duplicate filtering. If
1224  * multiple events of the same discovery are undesirable, it is up to the
1225  * platform NAN discovery engine implementation to implement redundancy
1226  * detection mechanisms.
1227  *
1228  * If WiFi is turned off by the user at the Android level, an existing
1229  * subscribe session is canceled, and a CHRE_EVENT_WIFI_ASYNC_RESULT event is
1230  * event is sent to the subscriber. Nanoapps are expected to register for user
1231  * settings notifications (@see chreUserSettingConfigureEvents), and
1232  * re-establish a subscribe session on a WiFi re-enabled settings changed
1233  * notification.
1234  *
1235  * @param config Service subscription configuration
1236  * @param cookie A value that the nanoapp uses to track this particular
1237  *        subscription request.
1238  * @return true if NAN is enabled and a subscription request was successfully
1239  *         made to the NAN engine. The actual result of the service discovery
1240  *         is sent via a CHRE_EVENT_WIFI_NAN_DISCOVERY_RESULT event.
1241  *
1242  * @since v1.6
1243  * @note Requires WiFi permission
1244  */
1245 bool chreWifiNanSubscribe(struct chreWifiNanSubscribeConfig *config,
1246                           const void *cookie);
1247 
1248 /**
1249  * Cancel a subscribe function instance.
1250  *
1251  * @param subscriptionId The ID that was originally assigned to this instance
1252  *        of the subscribe function.
1253  * @return true if NAN is enabled, the subscribe ID  was found and the instance
1254  *         successfully canceled.
1255  *
1256  * @since v1.6
1257  * @note Requires WiFi permission
1258  */
1259 bool chreWifiNanSubscribeCancel(uint32_t subscriptionID);
1260 
1261 /**
1262  * Request RTT ranging from a peer NAN device.
1263  *
1264  * Nanoapps can use this API to explicitly request measurement reports from
1265  * the peer device. Note that both end points have to support ranging for a
1266  * successful request. The MAC address of the peer NAN device for which ranging
1267  * is desired may be obtained either from a NAN service discovery or from an
1268  * out-of-band source (HAL service, BLE, etc.).
1269  *
1270  * If WiFi is turned off by the user at the Android level, an existing
1271  * ranging session is canceled, and a CHRE_EVENT_WIFI_ASYNC_RESULT event is
1272  * sent to the subscriber. Nanoapps are expected to register for user settings
1273  * notifications (@see chreUserSettingConfigureEvents), and perform another
1274  * ranging request on a WiFi re-enabled settings changed notification.
1275  *
1276  * A successful result provided in CHRE_EVENT_WIFI_ASYNC_RESULT indicates that
1277  * the results of ranging will be delivered in a subsequent event of type
1278  * CHRE_EVENT_WIFI_RANGING_RESULT.
1279  *
1280  * @param params Structure containing the parameters of the ranging request,
1281  *        including the MAC address of the peer NAN device to attempt ranging.
1282  * @param cookie An opaque value that will be included in the chreAsyncResult
1283  *        sent in relation to this request.
1284  * @return true if the request was accepted for processing, false otherwise.
1285  * @since v1.6
1286  * @note Requires WiFi permission
1287  */
1288 bool chreWifiNanRequestRangingAsync(const struct chreWifiNanRangingParams *params,
1289                                     const void *cookie);
1290 
1291 #else  /* defined(CHRE_NANOAPP_USES_WIFI) || !defined(CHRE_IS_NANOAPP_BUILD) */
1292 #define CHRE_WIFI_PERM_ERROR_STRING \
1293     "CHRE_NANOAPP_USES_WIFI must be defined when building this nanoapp in " \
1294     "order to refer to "
1295 #define chreWifiConfigureScanMonitorAsync(...) \
1296     CHRE_BUILD_ERROR(CHRE_WIFI_PERM_ERROR_STRING \
1297                      "chreWifiConfigureScanMonitorAsync")
1298 #define chreWifiRequestScanAsync(...) \
1299     CHRE_BUILD_ERROR(CHRE_WIFI_PERM_ERROR_STRING \
1300                      "chreWifiRequestScanAsync")
1301 #define chreWifiRequestScanAsyncDefault(...) \
1302     CHRE_BUILD_ERROR(CHRE_WIFI_PERM_ERROR_STRING \
1303                      "chreWifiRequestScanAsyncDefault")
1304 #define chreWifiRequestRangingAsync(...) \
1305     CHRE_BUILD_ERROR(CHRE_WIFI_PERM_ERROR_STRING "chreWifiRequestRangingAsync")
1306 #define chreWifiRangingTargetFromScanResult(...) \
1307     CHRE_BUILD_ERROR(CHRE_WIFI_PERM_ERROR_STRING \
1308                      "chreWifiRangingTargetFromScanResult")
1309 #define chreWifiNanSubscribe(...) \
1310     CHRE_BUILD_ERROR(CHRE_WIFI_PERM_ERROR_STRING "chreWifiNanSubscribe")
1311 #define chreWifiNanSubscribeCancel(...) \
1312     CHRE_BUILD_ERROR(CHRE_WIFI_PERM_ERROR_STRING "chreWifiNanSubscribeCancel")
1313 #define chreWifiNanRequestRangingAsync(...) \
1314     CHRE_BUILD_ERROR(CHRE_WIFI_PERM_ERROR_STRING "chreWifiNanRequestRangingAsync")
1315 #endif  /* defined(CHRE_NANOAPP_USES_WIFI) || !defined(CHRE_IS_NANOAPP_BUILD) */
1316 
1317 #ifdef __cplusplus
1318 }
1319 #endif
1320 
1321 #endif  /* _CHRE_WIFI_H_ */
1322