aboutsummaryrefslogtreecommitdiff
path: root/pal/include/chre/pal/wifi.h
blob: f40a3ea4e77cfd1b985615d056609f445abba17c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef CHRE_PAL_WIFI_H_
#define CHRE_PAL_WIFI_H_

/**
 * @file
 * Defines the interface between the common CHRE core system and the
 * platform-specific WiFi module.
 */

#include <stdbool.h>
#include <stdint.h>

#include "chre/pal/system.h"
#include "chre/pal/version.h"
#include "chre_api/chre/common.h"
#include "chre_api/chre/wifi.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * Initial version of the CHRE WiFi PAL, tied to CHRE API v1.1.
 */
#define CHRE_PAL_WIFI_API_V1_0 CHRE_PAL_CREATE_API_VERSION(1, 0)

// v1.1 skipped to avoid confusion with CHRE API v1.1

/**
 * Introduced alongside CHRE API v1.2, adding support for RTT ranging and radio
 * chain preference.
 */
#define CHRE_PAL_WIFI_API_V1_2 CHRE_PAL_CREATE_API_VERSION(1, 2)

/**
 * Introduced alongside CHRE API v1.5, adding support for additional WiFi
 * security modes.
 */
#define CHRE_PAL_WIFI_API_V1_5 CHRE_PAL_CREATE_API_VERSION(1, 5)

/**
 * The version of the WiFi PAL defined in this header file.
 */
#define CHRE_PAL_WIFI_API_CURRENT_VERSION CHRE_PAL_WIFI_API_V1_5

struct chrePalWifiCallbacks {
  /**
   * Callback invoked to inform the CHRE of the result of changes to the scan
   * monitor registration status requested via configureScanMonitor in struct
   * chrePalWifiApi.
   *
   * Unsolicited calls to this function must not be made. In other words,
   * this callback should only be invoked as the direct result of an earlier
   * call to configureScanMonitor. If the scan monitor registration is lost,
   * for example due to a reset of the WiFi subsystem, then the PAL
   * implementation is required to silently re-register the scan monitor when
   * it recovers, as needed.
   *
   * @param enabled true if the scan monitor is currently active and
   *        scanEventCallback will receive unsolicited scan results, false
   *        otherwise
   * @param errorCode An error code from enum chreError
   *
   * @see chrePalWifiApi.configureScanMonitor
   * @see #chreError
   */
  void (*scanMonitorStatusChangeCallback)(bool enabled, uint8_t errorCode);

  /**
   * Callback invoked to inform the CHRE of the result of a request for a
   * scan requested via requestScan in struct chrePalWifiApi.
   *
   * Unsolicited calls to this function must not be made. See
   * scanMonitorStatusChangeCallback() for more information.
   *
   * This function must only be called after the final status of the scan
   * request is known. For example, it must not be called at the point when
   * the scan is initially scheduled if it can still fail prior to delivering
   * a result.
   *
   * @param pending true if the request was successful and the results of the
   *        scan are pending delivery (via scanEventCallback), false otherwise
   * @param errorCode An error code from enum chreError
   */
  void (*scanResponseCallback)(bool pending, uint8_t errorCode);

  /**
   * Callback used to pass scan results from the WiFi module to the core CHRE
   * system, which distributes it to clients (nanoapps).
   *
   * This function call passes ownership of the event memory to the core CHRE
   * system, i.e. the PAL module must not modify the referenced data until the
   * associated API function is called to release the memory.
   *
   * If the results of a given scan are be split across multiple events, and
   * therefore multiple calls to this callback, then the events must be
   * delivered in order, and in one contiguous series of callbacks with no
   * interleaving of events that correspond to any other scan.
   *
   * The PAL module must not deliver the same scan event twice. As a specific
   * example: if an explicit scan request is made via requestScan(), the PAL
   * implementation must not redeliver the result a second time because scan
   * monitoring is enabled.
   *
   * @param event Event data to distribute to clients. The WiFi module
   *        must ensure that this memory remains accessible until it is passed
   *        to the releaseScanEvent() function in struct chrePalWifiApi.
   *
   * @see chrePalWifiApi.configureScanMonitor
   * @see chrePalWifiApi.requestScan
   */
  void (*scanEventCallback)(struct chreWifiScanEvent *event);

  /**
   * Callback used to pass RTT ranging results from the WiFi module to the
   * core CHRE system, which distributes it to clients (nanoapps).
   *
   * Like scanEventCallback, this function call passes ownership of the event
   * memory to the core CHRE system.
   *
   * Only valid if requestedApiVersion given to chrePalWifiGetApi() is greater
   * than or equal to CHRE_PAL_WIFI_API_V1_2.
   *
   * @param errorCode An error code from enum chreError, with CHRE_ERROR_NONE
   *        indicating successful completion of the ranging operation
   * @param event Event data to distribute to clients. Unlike with scan
   *        events, RTT ranging results must be provided for all requested MAC
   *        addresses in a single event. Ignored and may be NULL if errorCode
   *        is not CHRE_ERROR_NONE.
   *
   * @since v1.2
   */
  void (*rangingEventCallback)(uint8_t errorCode,
                               struct chreWifiRangingEvent *event);
};

struct chrePalWifiApi {
  /**
   * Version of the module providing this API. This value should be
   * constructed from CHRE_PAL_CREATE_MODULE_VERSION using the supported
   * API version constant (CHRE_PAL_WIFI_API_*) and the module-specific patch
   * version.
   */
  uint32_t moduleVersion;

  /**
   * Initializes the WiFi module. Initialization must complete synchronously.
   *
   * @param systemApi Structure containing CHRE system function pointers which
   *        the PAL implementation should prefer to use over equivalent
   *        functionality exposed by the underlying platform. The module does
   *        not need to deep-copy this structure; its memory remains
   *        accessible at least until after close() is called.
   * @param callbacks Structure containing entry points to the core CHRE
   *        system. The module does not need to deep-copy this structure; its
   *        memory remains accessible at least until after close() is called.
   *
   * @return true if initialization was successful, false otherwise
   */
  bool (*open)(const struct chrePalSystemApi *systemApi,
               const struct chrePalWifiCallbacks *callbacks);

  /**
   * Performs clean shutdown of the WiFi module, usually done in preparation
   * for stopping the CHRE. The WiFi module must ensure that it will not
   * invoke any callbacks past this point, and complete any relevant teardown
   * activities before returning from this function.
   */
  void (*close)(void);

  //! @see chreWifiGetCapabilities()
  uint32_t (*getCapabilities)(void);

  /**
   * Configures whether the scanEventCallback receives unsolicited scan
   * results, i.e. the results of scans not performed at the request of CHRE.
   *
   * While not expected, a duplicate request, e.g. one that requests to enable
   * scan monitoring when it is already enabled, must follow the successful
   * callback flow.
   *
   * @param enable true to enable listening for all available scan results
   *
   * @return true if the request was accepted for processing, in which case a
   *         subsequent call to scanMonitorStatusChangeCallback will be used
   *         to communicate the result of the operation
   *
   * @see chreWifiConfigureScanMonitorAsync()
   */
  bool (*configureScanMonitor)(bool enable);

  /**
   * Request that the WiFi chipset perform a scan, or deliver results from its
   * cache if the parameters allow for it. If this function returns true, then
   * the scanResponseCallback will be invoked to provide the result of the
   * scan. If that indicates a successful result (the scan data is pending),
   * then scanEventCallback() will be invoked one more more times to deliver
   * the results of the scan. The results for the requested scan are delivered
   * in scanEventCallback() regardless of the most recent setting passed to
   * configureScanMonitor().
   *
   * The asynchronous flow of a scan request made through this API is
   * as follows:
   *
   *  1. requestScan() called, returns true if request accepted, otherwise
   *     false (in which case the request fails at this stage and further
   *     steps do not occur)
   *  2. Scan is performed, or an error is encountered preventing the
   *     successful delivery of the scan result
   *  3. scanResponseCallback() is invoked to indicate whether the scan
   *     succeeded, or the reason for failure (in which case the request fails
   *     at this stage and further steps do not occur)
   *  4. scanEventCallback() is invoked 1 or more times (even if the scan
   *     resulted in no visible APs)
   *
   * Note that the callbacks in steps 3 and 4 must complete in the sequence
   * given, and the call(s) to scanEventCallback() occurring immediately after
   * scanResponseCallback() must be associated with this scan request, and not
   * results delivered pursuant to an active scan monitor registration.
   *
   * This function must follow the CHRE API-defined behavior regarding
   * timeouts. In other words, if a successful scan result is not produced by
   * the lower layers within CHRE_WIFI_SCAN_RESULT_TIMEOUT_NS,
   * scanResponseCallback() must be invoked to indicate the failure, and any
   * late arriving scan result from the lower layers must be dropped.
   *
   * At most 1 scan can be in progress from this API at any given time.
   * In other words, the implementation should return false if another scan
   * initiated via this function has not completed, i.e. it has not failed
   * yet, or the final scan event has not yet been delivered via
   * scanEventCallback(). However, this function must accept and queue a scan
   * request made from this API while a scan requested by another client, such
   * as the applications processor, is in progress.
   *
   * @param params See chreWifiRequestScanAsync(). If requestedApiVersion
   *        supplied to chrePalWifiGetApi is at least CHRE_PAL_WIFI_API_V1_2,
   *        then the new "radioChainPref" parameter will be included.
   *
   * @return true if the request was accepted for further processing, in which
   *         case a subsequent call to scanResponseCallback will be used to
   *         communicate the result of the operation
   *
   * @see #chreWifiScanParams
   * @see chreWifiRequestScanAsync()
   */
  bool (*requestScan)(const struct chreWifiScanParams *params);

  /**
   * Invoked when the core CHRE system no longer needs a WiFi scan event
   * structure that was provided to it via scanEventCallback()
   *
   * @param event Event data to release
   */
  void (*releaseScanEvent)(struct chreWifiScanEvent *event);

  /**
   * Request that the WiFi chipset perform RTT ranging against a set of access
   * points specified in params. If this function returns true, then
   * rangingEventCallback must be invoked once to deliver the final result of
   * the operation, with the accompanying result structure if ranging was
   * performed.
   *
   * RTT functionality in CHRE is based off the Android HAL definitions
   * (hardware/interfaces/wifi/1.0/), but with less parameters. For
   * example, CHRE only supports ranging against access points, and two-sided
   * RTT. When mapping struct chreWifiRangingTarget into the equivalent fields
   * defined in the HAL in struct RttConfig, the following default values
   * should be used to fill the fields not specified in the CHRE structure:
   *
   * <pre>
   *   type = TWO_SIDED
   *   peer = AP
   *   burstPeriod = 0
   *   numBurst = 0
   *   numFramesPerBurst = 8
   *   numRetriesPerRttFrame = 0
   *   numRetriesPerFtmr = 0
   *   mustRequestLci = true
   *   mustRequestLcr = false (can be true, but not exposed by CHRE)
   *   burstDuration = 15
   *   preamble = implementation-dependent**
   *   bw = implementation-dependent**
   * </pre>
   *
   * **These are used to populate the Format And Bandwidth field in the Fine
   *   Timing Measurement Parameters element. Per the specification, proposed
   *   values must fall within the capabilities of the requesting device, and
   *   the configuration used is ultimately negotiated with the responding
   *   STA. Therefore, it is up to the underlying WiFi implementation to pick
   *   suitable values.
   *
   * Like {@link #requestScan}, this function must follow the CHRE API-defined
   * behavior regarding timeouts, indicating failure via rangingEventCallback
   * if the lower layers do not produce a result within
   * CHRE_WIFI_RANGING_RESULT_TIMEOUT_NS.
   *
   * Also like {@link #requestScan}, at most 1 RTT ranging request can be in
   * progress from this API at any given time. Implementations should return
   * false if this condition is not met, but must queue a request made from
   * this API while a request from another client, such as the applications
   * processor, is in progress.
   *
   * @return true if the request was accepted for further processing, in which
   *         case a subsequent call to rangingEventCallback will be used to
   *         communicate the result of the operation
   *
   * @see #chreWifiRangingParams
   * @see chreWifiRequestRangingAsync()
   *
   * @since v1.2
   */
  bool (*requestRanging)(const struct chreWifiRangingParams *params);

  /**
   * Invoked when the core CHRE system no longer needs a WiFi ranging result
   * event structure that was provided to it via rangingEventCallback()
   *
   * @param event Event data to release
   *
   * @since v1.2
   */
  void (*releaseRangingEvent)(struct chreWifiRangingEvent *event);
};

/**
 * Retrieve a handle for the CHRE WiFi PAL.
 *
 * @param requestedApiVersion The implementation of this function must return a
 *        pointer to a structure with the same major version as requested.
 *
 * @return Pointer to API handle, or NULL if a compatible API version is not
 *         supported by the module, or the API as a whole is not implemented. If
 *         non-NULL, the returned API handle must be valid as long as this
 *         module is loaded.
 */
const struct chrePalWifiApi *chrePalWifiGetApi(uint32_t requestedApiVersion);

#ifdef __cplusplus
}
#endif

#endif  // CHRE_PAL_WIFI_H_