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