• 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_PAL_WIFI_H_
18 #define CHRE_PAL_WIFI_H_
19 
20 /**
21  * @file
22  * Defines the interface between the common CHRE core system and the
23  * platform-specific WiFi module.
24  */
25 
26 #include <stdbool.h>
27 #include <stdint.h>
28 
29 #include "chre/pal/system.h"
30 #include "chre/pal/version.h"
31 #include "chre_api/chre/common.h"
32 #include "chre_api/chre/wifi.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /**
39  * Initial version of the CHRE WiFi PAL, tied to CHRE API v1.1.
40  */
41 #define CHRE_PAL_WIFI_API_V1_0 CHRE_PAL_CREATE_API_VERSION(1, 0)
42 
43 // v1.1 skipped to avoid confusion with CHRE API v1.1
44 
45 /**
46  * Introduced alongside CHRE API v1.2, adding support for RTT ranging and radio
47  * chain preference.
48  */
49 #define CHRE_PAL_WIFI_API_V1_2 CHRE_PAL_CREATE_API_VERSION(1, 2)
50 
51 /**
52  * Introduced alongside CHRE API v1.5, adding support for additional WiFi
53  * security modes.
54  */
55 #define CHRE_PAL_WIFI_API_V1_5 CHRE_PAL_CREATE_API_VERSION(1, 5)
56 
57 /**
58  * Introduced alongside CHRE API v1.6, adding support for WiFi NAN service
59  * subscriptions and ranging.
60  */
61 #define CHRE_PAL_WIFI_API_V1_6 CHRE_PAL_CREATE_API_VERSION(1, 6)
62 
63 /**
64  * Introduced alongside CHRE API v1.8, adding support for getting WiFi NAN
65  * capabilities.
66  */
67 #define CHRE_PAL_WIFI_API_V1_8 CHRE_PAL_CREATE_API_VERSION(1, 8)
68 
69 /**
70  * The version of the WiFi PAL defined in this header file.
71  */
72 #define CHRE_PAL_WIFI_API_CURRENT_VERSION CHRE_PAL_WIFI_API_V1_8
73 
74 struct chrePalWifiCallbacks {
75   /**
76    * Callback invoked to inform the CHRE of the result of changes to the scan
77    * monitor registration status requested via configureScanMonitor in struct
78    * chrePalWifiApi.
79    *
80    * Unsolicited calls to this function must not be made. In other words,
81    * this callback should only be invoked as the direct result of an earlier
82    * call to configureScanMonitor. If the scan monitor registration is lost,
83    * for example due to a reset of the WiFi subsystem, then the PAL
84    * implementation is required to silently re-register the scan monitor when
85    * it recovers, as needed.
86    *
87    * @param enabled true if the scan monitor is currently active and
88    *        scanEventCallback will receive unsolicited scan results, false
89    *        otherwise
90    * @param errorCode An error code from enum chreError
91    *
92    * @see chrePalWifiApi.configureScanMonitor
93    * @see #chreError
94    */
95   void (*scanMonitorStatusChangeCallback)(bool enabled, uint8_t errorCode);
96 
97   /**
98    * Callback invoked to inform the CHRE of the result of a request for a
99    * scan requested via requestScan in struct chrePalWifiApi.
100    *
101    * Unsolicited calls to this function must not be made. See
102    * scanMonitorStatusChangeCallback() for more information.
103    *
104    * This function must only be called after the final status of the scan
105    * request is known. For example, it must not be called at the point when
106    * the scan is initially scheduled if it can still fail prior to delivering
107    * a result.
108    *
109    * @param pending true if the request was successful and the results of the
110    *        scan are pending delivery (via scanEventCallback), false otherwise
111    * @param errorCode An error code from enum chreError
112    */
113   void (*scanResponseCallback)(bool pending, uint8_t errorCode);
114 
115   /**
116    * Callback used to pass scan results from the WiFi module to the core CHRE
117    * system, which distributes it to clients (nanoapps).
118    *
119    * This function call passes ownership of the event memory to the core CHRE
120    * system, i.e. the PAL module must not modify the referenced data until the
121    * associated API function is called to release the memory.
122    *
123    * If the results of a given scan are be split across multiple events, and
124    * therefore multiple calls to this callback, then the events must be
125    * delivered in order, and in one contiguous series of callbacks with no
126    * interleaving of events that correspond to any other scan.
127    *
128    * The PAL module must not deliver the same scan event twice. As a specific
129    * example: if an explicit scan request is made via requestScan(), the PAL
130    * implementation must not redeliver the result a second time because scan
131    * monitoring is enabled.
132    *
133    * @param event Event data to distribute to clients. The WiFi module
134    *        must ensure that this memory remains accessible until it is passed
135    *        to the releaseScanEvent() function in struct chrePalWifiApi.
136    *
137    * @see chrePalWifiApi.configureScanMonitor
138    * @see chrePalWifiApi.requestScan
139    */
140   void (*scanEventCallback)(struct chreWifiScanEvent *event);
141 
142   /**
143    * Callback used to pass RTT ranging results from the WiFi module to the
144    * core CHRE system, which distributes it to clients (nanoapps).
145    *
146    * Like scanEventCallback, this function call passes ownership of the event
147    * memory to the core CHRE system.
148    *
149    * Only valid if requestedApiVersion given to chrePalWifiGetApi() is greater
150    * than or equal to CHRE_PAL_WIFI_API_V1_2. WiFi NAN ranging is only
151    * supported from CHRE_PAL_WIFI_API_V1_6.
152    *
153    * @param errorCode An error code from enum chreError, with CHRE_ERROR_NONE
154    *        indicating successful completion of the ranging operation
155    * @param event Event data to distribute to clients. Unlike with scan
156    *        events, RTT ranging results must be provided for all requested MAC
157    *        addresses in a single event. Ignored and may be NULL if errorCode
158    *        is not CHRE_ERROR_NONE.
159    *
160    * @since v1.2
161    */
162   void (*rangingEventCallback)(uint8_t errorCode,
163                                struct chreWifiRangingEvent *event);
164 
165   /**
166    * Callback used to pass the asynchronous result of a NAN service subscribe
167    * call from the client (nanoapp). Note that the subscription ID assigned by
168    * the NAN discovery engine is only valid if the embedded chreAsyncResult
169    * in the event structure has a CHRE_ERROR_NONE status.
170    *
171    * Only valid if requestedApiVersion given to chrePalWifiGetApi() is greater
172    * than or equal to CHRE_PAL_WIFI_API_V1_6.
173    *
174    * @param errorCode An error code from enum chreError, with CHRE_ERROR_NONE
175    *        indicating successfully getting a subscription ID.
176    * @param subscriptionId The ID assigned by the NAN discovery engine for a
177    *        subscription request. This field is only considered valid if the
178    *        associated error code is CHRE_ERROR_NONE.
179    *
180    * @since v1.6
181    */
182   void (*nanServiceIdentifierCallback)(uint8_t errorCode,
183                                        uint32_t subscriptionId);
184 
185   /**
186    * Callback used to pass the result of a NAN service discovery to a client
187    * (nanoapp) that has requested a subscription. The
188    * nanServiceIdentifierCallback must be used to pass on the subscription ID
189    * to the client before an invocation of this function to enable the client
190    * to match an identifier to a subscription request.
191    *
192    * This function call passes ownership of the event memory to CHRE, and the
193    * PAL module must not modify the referenced data until the associated memory
194    * release API function (@ref releaseNanDiscoveryEvent) is called.
195    *
196    * Only valid if requestedApiVersion given to chrePalWifiGetApi() is greater
197    * than or equal to CHRE_PAL_WIFI_API_V1_6.
198    *
199    * @param event Event data containing NAN service discovery information. Must
200    *        not be NULL.
201    *
202    * @since v1.6
203    */
204   void (*nanServiceDiscoveryCallback)(struct chreWifiNanDiscoveryEvent *event);
205 
206   /**
207    * Callback used to inform a subscriber that the publisher connection has
208    * been lost due to the peer NAN device going out of range.
209    *
210    * @param subscriptionId ID of the subscriber who should be notified of the
211    *        connection loss.
212    * @param publisherId ID of the publisher which disappeared.
213    *
214    * @since v1.6
215    */
216   void (*nanServiceLostCallback)(uint32_t subscriptionId, uint32_t publisherId);
217 
218   /**
219    * Callback used to inform a subscriber that the subscription session has
220    * been terminated by the publisher or the NAN discovery engine.
221    *
222    * @param reason A value that maps to one of the termination reasons in
223    *        @ref enum chreWifiNanTerminatedReason, indicating why the subscribe
224    *        session was terminated.
225    * @param subscriptionId The ID of the subscribe session which has ended.
226    *
227    * @since v1.6
228    */
229   void (*nanServiceTerminatedCallback)(uint32_t reason,
230                                        uint32_t subscriptionId);
231 
232   /**
233    * Callback invoked when a NAN subscription has been canceled by an explicit
234    * cancel subscription request from CHRE. Note that this is different from
235    * a subscription cancelation or termination from an agent external to CHRE
236    * (eg: the publisher going away or the discovery engine terminating the
237    * subscription for any reason).
238    *
239    * @param errorCode An error code from enum chreError, with CHRE_ERROR_NONE
240    *        indicating successfully canceling a subscription.
241    * @param subscriptionId The ID of the subscribe session which has now been
242    *        canceled.
243    *
244    * @since v1.6
245    */
246   void (*nanSubscriptionCanceledCallback)(uint8_t errorCode,
247                                           uint32_t subscriptionId);
248 };
249 
250 struct chrePalWifiApi {
251   /**
252    * Version of the module providing this API. This value should be
253    * constructed from CHRE_PAL_CREATE_MODULE_VERSION using the supported
254    * API version constant (CHRE_PAL_WIFI_API_*) and the module-specific patch
255    * version.
256    */
257   uint32_t moduleVersion;
258 
259   /**
260    * Initializes the WiFi module. Initialization must complete synchronously.
261    *
262    * @param systemApi Structure containing CHRE system function pointers which
263    *        the PAL implementation should prefer to use over equivalent
264    *        functionality exposed by the underlying platform. The module does
265    *        not need to deep-copy this structure; its memory remains
266    *        accessible at least until after close() is called.
267    * @param callbacks Structure containing entry points to the core CHRE
268    *        system. The module does not need to deep-copy this structure; its
269    *        memory remains accessible at least until after close() is called.
270    *
271    * @return true if initialization was successful, false otherwise
272    */
273   bool (*open)(const struct chrePalSystemApi *systemApi,
274                const struct chrePalWifiCallbacks *callbacks);
275 
276   /**
277    * Performs clean shutdown of the WiFi module, usually done in preparation
278    * for stopping the CHRE. The WiFi module must ensure that it will not
279    * invoke any callbacks past this point, and complete any relevant teardown
280    * activities before returning from this function.
281    */
282   void (*close)(void);
283 
284   //! @see chreWifiGetCapabilities()
285   uint32_t (*getCapabilities)(void);
286 
287   /**
288    * Configures whether the scanEventCallback receives unsolicited scan
289    * results, i.e. the results of scans not performed at the request of CHRE.
290    *
291    * While not expected, a duplicate request, e.g. one that requests to enable
292    * scan monitoring when it is already enabled, must follow the successful
293    * callback flow.
294    *
295    * @param enable true to enable listening for all available scan results
296    *
297    * @return true if the request was accepted for processing, in which case a
298    *         subsequent call to scanMonitorStatusChangeCallback will be used
299    *         to communicate the result of the operation
300    *
301    * @see chreWifiConfigureScanMonitorAsync()
302    */
303   bool (*configureScanMonitor)(bool enable);
304 
305   /**
306    * Request that the WiFi chipset perform a scan, or deliver results from its
307    * cache if the parameters allow for it. If this function returns true, then
308    * the scanResponseCallback will be invoked to provide the result of the
309    * scan. If that indicates a successful result (the scan data is pending),
310    * then scanEventCallback() will be invoked one more more times to deliver
311    * the results of the scan. The results for the requested scan are delivered
312    * in scanEventCallback() regardless of the most recent setting passed to
313    * configureScanMonitor().
314    *
315    * The asynchronous flow of a scan request made through this API is
316    * as follows:
317    *
318    *  1. requestScan() called, returns true if request accepted, otherwise
319    *     false (in which case the request fails at this stage and further
320    *     steps do not occur)
321    *  2. Scan is performed, or an error is encountered preventing the
322    *     successful delivery of the scan result
323    *  3. scanResponseCallback() is invoked to indicate whether the scan
324    *     succeeded, or the reason for failure (in which case the request fails
325    *     at this stage and further steps do not occur)
326    *  4. scanEventCallback() is invoked 1 or more times (even if the scan
327    *     resulted in no visible APs)
328    *
329    * Note that the callbacks in steps 3 and 4 must complete in the sequence
330    * given, and the call(s) to scanEventCallback() occurring immediately after
331    * scanResponseCallback() must be associated with this scan request, and not
332    * results delivered pursuant to an active scan monitor registration.
333    *
334    * This function must follow the CHRE API-defined behavior regarding
335    * timeouts. In other words, if a successful scan result is not produced by
336    * the lower layers within CHRE_WIFI_SCAN_RESULT_TIMEOUT_NS,
337    * scanResponseCallback() must be invoked to indicate the failure, and any
338    * late arriving scan result from the lower layers must be dropped.
339    *
340    * At most 1 scan can be in progress from this API at any given time.
341    * In other words, the implementation should return false if another scan
342    * initiated via this function has not completed, i.e. it has not failed
343    * yet, or the final scan event has not yet been delivered via
344    * scanEventCallback(). However, this function must accept and queue a scan
345    * request made from this API while a scan requested by another client, such
346    * as the applications processor, is in progress.
347    *
348    * @param params See chreWifiRequestScanAsync(). If requestedApiVersion
349    *        supplied to chrePalWifiGetApi is at least CHRE_PAL_WIFI_API_V1_2,
350    *        then the new "radioChainPref" parameter will be included.
351    *
352    * @return true if the request was accepted for further processing, in which
353    *         case a subsequent call to scanResponseCallback will be used to
354    *         communicate the result of the operation
355    *
356    * @see #chreWifiScanParams
357    * @see chreWifiRequestScanAsync()
358    */
359   bool (*requestScan)(const struct chreWifiScanParams *params);
360 
361   /**
362    * Invoked when the core CHRE system no longer needs a WiFi scan event
363    * structure that was provided to it via scanEventCallback()
364    *
365    * @param event Event data to release
366    */
367   void (*releaseScanEvent)(struct chreWifiScanEvent *event);
368 
369   /**
370    * Request that the WiFi chipset perform RTT ranging against a set of access
371    * points specified in params. If this function returns true, then
372    * rangingEventCallback must be invoked once to deliver the final result of
373    * the operation, with the accompanying result structure if ranging was
374    * performed.
375    *
376    * RTT functionality in CHRE is based off the Android HAL definitions
377    * (hardware/interfaces/wifi/1.0/), but with less parameters. For
378    * example, CHRE only supports ranging against access points, and two-sided
379    * RTT. When mapping struct chreWifiRangingTarget into the equivalent fields
380    * defined in the HAL in struct RttConfig, the following default values
381    * should be used to fill the fields not specified in the CHRE structure:
382    *
383    * <pre>
384    *   type = TWO_SIDED
385    *   peer = AP
386    *   burstPeriod = 0
387    *   numBurst = 0
388    *   numFramesPerBurst = 8
389    *   numRetriesPerRttFrame = 0
390    *   numRetriesPerFtmr = 0
391    *   mustRequestLci = true
392    *   mustRequestLcr = false (can be true, but not exposed by CHRE)
393    *   burstDuration = 15
394    *   preamble = implementation-dependent**
395    *   bw = implementation-dependent**
396    * </pre>
397    *
398    * **These are used to populate the Format And Bandwidth field in the Fine
399    *   Timing Measurement Parameters element. Per the specification, proposed
400    *   values must fall within the capabilities of the requesting device, and
401    *   the configuration used is ultimately negotiated with the responding
402    *   STA. Therefore, it is up to the underlying WiFi implementation to pick
403    *   suitable values.
404    *
405    * Like {@link #requestScan}, this function must follow the CHRE API-defined
406    * behavior regarding timeouts, indicating failure via rangingEventCallback
407    * if the lower layers do not produce a result within
408    * CHRE_WIFI_RANGING_RESULT_TIMEOUT_NS.
409    *
410    * Also like {@link #requestScan}, at most 1 RTT ranging request can be in
411    * progress from this API at any given time. Implementations should return
412    * false if this condition is not met, but must queue a request made from
413    * this API while a request from another client, such as the applications
414    * processor, is in progress.
415    *
416    * @return true if the request was accepted for further processing, in which
417    *         case a subsequent call to rangingEventCallback will be used to
418    *         communicate the result of the operation
419    *
420    * @see #chreWifiRangingParams
421    * @see chreWifiRequestRangingAsync()
422    *
423    * @since v1.2
424    */
425   bool (*requestRanging)(const struct chreWifiRangingParams *params);
426 
427   /**
428    * Invoked when the core CHRE system no longer needs a WiFi ranging result
429    * event structure that was provided to it via rangingEventCallback()
430    *
431    * @param event Event data to release
432    *
433    * @since v1.2
434    */
435   void (*releaseRangingEvent)(struct chreWifiRangingEvent *event);
436 
437   /**
438    * Requests the NAN discovery engine to initiate a subscribe request to a
439    * publisher in accordance to a specified configuration. Upon completion of
440    * the operation, the nanServiceIdentifierCallback must be invoked with the
441    * subscription ID assigned by the NAN engine (if the request was successful)
442    * and an appropriate error code in the embedded chreAsyncResult structure.
443    *
444    * @param config Subscription configuration that specifies the subscription
445    *        type, publisher name and service specific information.
446    * @return true if the subscribe request was successful, in which case a
447    *         subsequent call to nanServiceIdentifierCallback will be used to
448    *         communicate the result of the operation.
449    *
450    * @since v1.6
451    */
452   bool (*nanSubscribe)(const struct chreWifiNanSubscribeConfig *config);
453 
454   /**
455    * Invoked when CHRE requests an explicit service subscription cancelation
456    * to a published service. Upon completion of the cancelation, the
457    * nanSubscriptionCanceledCallback function must be invoked with the result
458    * of the operation and the subscription ID of this cancelation.
459    *
460    * @param subscriptionId The ID assigned by the NAN discovery engine to the
461    *        service subscription session.
462    * @return true if the service subscription indicated by the ID exists, and
463    *         was successfully canceled.
464    *
465    * @since v1.6
466    */
467   bool (*nanSubscribeCancel)(const uint32_t subscriptionId);
468 
469   /**
470    * Invoked when the core CHRE system no longer needs a NAN service discovery
471    * event structure that was provided to it via nanServiceDiscoveryCallback().
472    *
473    * @param event Event data to release
474    *
475    * @since v1.6
476    */
477   void (*releaseNanDiscoveryEvent)(struct chreWifiNanDiscoveryEvent *event);
478 
479   /**
480    * Request that the WiFi chipset perform RTT ranging against a peer NAN
481    * device, whose MAC address is specified by the NAN ranging params. If this
482    * function returns true, then the rangingEventCallback must be invoked once
483    * to deliver the result, with the accompanying result structure (@see
484    * rangingEventCallback).
485    *
486    * Like {@link requestRanging}, this API must follow CHRE API-defined
487    * behavior regarding timeouts and failure indication, via
488    * rangingEventCallback if the lower layers do not produce a result within
489    * CHRE_WIFI_RANGING_RESULT_TIMEOUT_NS.
490    *
491    * Only a single  NAN ranging request can be in progress at any given time.
492    * Also, only one of a NAN ranging request or an AP (access point) ranging
493    * request can be in progress at any given time.
494    *
495    * @param params WiFi NAN ranging parameters for this request.
496    * @return true if the request was accepted for further processing, in
497    *         which case a subsequent call to rangingEventCallback will be used
498    *         to communicate the result of the operation.
499    *
500    * @see #chreWifiNanRangingParams
501    * @see chreWifiNanRequestRangingAsync()
502    *
503    * @since v1.6
504    */
505   bool (*requestNanRanging)(const struct chreWifiNanRangingParams *params);
506 
507   /**
508    * @see chreWifiNanGetCapabilities()
509    *
510    * @since v1.8
511    */
512   bool (*getNanCapabilities)(struct chreWifiNanCapabilities *capabilities);
513 };
514 
515 /**
516  * Retrieve a handle for the CHRE WiFi PAL.
517  *
518  * @param requestedApiVersion The implementation of this function must return a
519  *        pointer to a structure with the same major version as requested.
520  *
521  * @return Pointer to API handle, or NULL if a compatible API version is not
522  *         supported by the module, or the API as a whole is not implemented. If
523  *         non-NULL, the returned API handle must be valid as long as this
524  *         module is loaded.
525  */
526 const struct chrePalWifiApi *chrePalWifiGetApi(uint32_t requestedApiVersion);
527 
528 #ifdef __cplusplus
529 }
530 #endif
531 
532 #endif  // CHRE_PAL_WIFI_H_
533