xref: /aosp_15_r20/system/chre/chre_api/include/chre_api/chre/ble.h (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // IWYU pragma: private, include "chre_api/chre.h"
18 // IWYU pragma: friend chre/.*\.h
19 
20 #ifndef CHRE_BLE_H_
21 #define CHRE_BLE_H_
22 
23 /**
24  * @file
25  * CHRE BLE (Bluetooth Low Energy, Bluetooth LE) API.
26  * The CHRE BLE API currently supports BLE scanning features.
27  *
28  * The features in the CHRE BLE API are a subset and adaptation of Android
29  * capabilities as described in the Android BLE API and HCI requirements.
30  * ref:
31  * https://developer.android.com/guide/topics/connectivity/bluetooth/ble-overview
32  * ref: https://source.android.com/devices/bluetooth/hci_requirements
33  *
34  * All byte arrays in the CHRE BLE API follow the byte order used OTA unless
35  * specified otherwise, and multi-byte types, for example uint16_t, follow the
36  * processor's native byte order. One notable exception is addresses. Address
37  * fields in both scan filters and advertising reports must be in big endian
38  * byte order to match the Android Bluetooth API (ref:
39  * https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#getRemoteDevice(byte[])).
40  */
41 
42 #include <chre/common.h>
43 #include <stdbool.h>
44 #include <stddef.h>
45 #include <stdint.h>
46 #include <string.h>
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 /**
53  * The set of flags returned by chreBleGetCapabilities().
54  *
55  * @defgroup CHRE_BLE_CAPABILITIES
56  * @{
57  */
58 //! No BLE APIs are supported
59 #define CHRE_BLE_CAPABILITIES_NONE (UINT32_C(0))
60 
61 //! CHRE supports BLE scanning
62 #define CHRE_BLE_CAPABILITIES_SCAN (UINT32_C(1) << 0)
63 
64 //! CHRE BLE supports batching of scan results, either through Android-specific
65 //! HCI (OCF: 0x156), or by the CHRE framework, internally.
66 //! @since v1.7 Platforms with this capability must also support flushing scan
67 //! results during a batched scan.
68 #define CHRE_BLE_CAPABILITIES_SCAN_RESULT_BATCHING (UINT32_C(1) << 1)
69 
70 //! CHRE BLE scan supports best-effort hardware filtering. If filtering is
71 //! available, chreBleGetFilterCapabilities() returns a bitmap indicating the
72 //! specific filtering capabilities that are supported.
73 //! To differentiate best-effort vs. no filtering, the following requirement
74 //! must be met for this flag:
75 //! If only one nanoapp is requesting BLE scans and there are no BLE scans from
76 //! the AP, only filtered results will be provided to the nanoapp.
77 #define CHRE_BLE_CAPABILITIES_SCAN_FILTER_BEST_EFFORT (UINT32_C(1) << 2)
78 
79 //! CHRE BLE supports reading the RSSI of a specified LE-ACL connection handle.
80 #define CHRE_BLE_CAPABILITIES_READ_RSSI (UINT32_C(1) << 3)
81 
82 //! CHRE supports offloading a Bluetooth connection socket for bidirectional
83 //! data transfer over a Connection-Oriented Channel (COC).
84 #define CHRE_BLE_CAPABILITIES_LE_COC_SOCKET UINT32_C (UINT32_C(1) << 4)
85 /** @} */
86 
87 /**
88  * The set of flags returned by chreBleGetFilterCapabilities().
89  *
90  * The representative bit for each filtering capability is based on the sub-OCF
91  * of the Android filtering HCI vendor-specific command (LE_APCF_Command, OCF:
92  * 0x0157) for that particular filtering capability, as found in
93  * https://source.android.com/devices/bluetooth/hci_requirements
94  *
95  * For example, the Service Data filter has a sub-command of 0x7; hence
96  * the filtering capability is indicated by (1 << 0x7).
97  *
98  * @defgroup CHRE_BLE_FILTER_CAPABILITIES
99  * @{
100  */
101 //! No CHRE BLE filters are supported
102 #define CHRE_BLE_FILTER_CAPABILITIES_NONE (UINT32_C(0))
103 
104 //! CHRE BLE supports RSSI filters
105 #define CHRE_BLE_FILTER_CAPABILITIES_RSSI (UINT32_C(1) << 1)
106 
107 //! CHRE BLE supports Broadcaster Address filters (Corresponding HCI OCF:
108 //! 0x0157, Sub-command: 0x02)
109 //! @since v1.9
110 #define CHRE_BLE_FILTER_CAPABILITIES_BROADCASTER_ADDRESS (UINT32_C(1) << 2)
111 
112 //! CHRE BLE supports Manufacturer Data filters (Corresponding HCI OCF: 0x0157,
113 //! Sub-command: 0x06)
114 //! @since v1.8
115 #define CHRE_BLE_FILTER_CAPABILITIES_MANUFACTURER_DATA (UINT32_C(1) << 6)
116 
117 //! CHRE BLE supports Service Data filters (Corresponding HCI OCF: 0x0157,
118 //! Sub-command: 0x07)
119 #define CHRE_BLE_FILTER_CAPABILITIES_SERVICE_DATA (UINT32_C(1) << 7)
120 /** @} */
121 
122 /**
123  * Produce an event ID in the block of IDs reserved for BLE.
124  *
125  * Valid input range is [0, 15]. Do not add new events with ID > 15
126  * (see chre/event.h)
127  *
128  * @param offset Index into BLE event ID block; valid range is [0, 15].
129  *
130  * @defgroup CHRE_BLE_EVENT_ID
131  * @{
132  */
133 #define CHRE_BLE_EVENT_ID(offset) (CHRE_EVENT_BLE_FIRST_EVENT + (offset))
134 
135 /**
136  * nanoappHandleEvent argument: struct chreAsyncResult
137  *
138  * Communicates the asynchronous result of a request to the BLE API. The
139  * requestType field in {@link #chreAsyncResult} is set to a value from enum
140  * chreBleRequestType.
141  *
142  * This is used for results of async config operations which need to
143  * interop with lower level code (potentially in a different thread) or send an
144  * HCI command to the FW and wait on the response.
145  */
146 #define CHRE_EVENT_BLE_ASYNC_RESULT CHRE_BLE_EVENT_ID(0)
147 
148 /**
149  * nanoappHandleEvent argument: struct chreBleAdvertisementEvent
150  *
151  * Provides results of a BLE scan.
152  */
153 #define CHRE_EVENT_BLE_ADVERTISEMENT CHRE_BLE_EVENT_ID(1)
154 
155 /**
156  * nanoappHandleEvent argument: struct chreAsyncResult
157  *
158  * Indicates that a flush request made via chreBleFlushAsync() is complete, and
159  * all batched advertisements resulting from the flush have been delivered via
160  * preceding CHRE_EVENT_BLE_ADVERTISEMENT events.
161  *
162  * @since v1.7
163  */
164 #define CHRE_EVENT_BLE_FLUSH_COMPLETE CHRE_BLE_EVENT_ID(2)
165 
166 /**
167  * nanoappHandleEvent argument: struct chreBleReadRssiEvent
168  *
169  * Provides the RSSI of an LE ACL connection following a call to
170  * chreBleReadRssiAsync().
171  *
172  * @since v1.8
173  */
174 #define CHRE_EVENT_BLE_RSSI_READ CHRE_BLE_EVENT_ID(3)
175 
176 /**
177  * nanoappHandleEvent argument: struct chreBatchCompleteEvent
178  *
179  * This event is generated if the platform enabled batching, and when all
180  * events in a single batch has been delivered (for example, batching
181  * CHRE_EVENT_BLE_ADVERTISEMENT events if the platform has
182  * CHRE_BLE_CAPABILITIES_SCAN_RESULT_BATCHING enabled, and a non-zero
183  * reportDelayMs in chreBleStartScanAsync() was accepted).
184  *
185  * If the nanoapp receives a CHRE_EVENT_BLE_SCAN_STATUS_CHANGE with a non-zero
186  * reportDelayMs and enabled set to true, then this event must be generated.
187  *
188  * @since v1.8
189  */
190 #define CHRE_EVENT_BLE_BATCH_COMPLETE CHRE_BLE_EVENT_ID(4)
191 
192 /**
193  * nanoappHandleEvent argument: struct chreBleScanStatus
194  *
195  * This event is generated when the values in chreBleScanStatus changes.
196  *
197  * @since v1.8
198  */
199 #define CHRE_EVENT_BLE_SCAN_STATUS_CHANGE CHRE_BLE_EVENT_ID(5)
200 
201 /**
202  * nanoappHandleEvent argument: struct chreBleSocketConnectionEvent
203  *
204  * This is a unicast event that is sent to a nanoapp when an offloaded socket is
205  * connected and is available to be used by the nanoapp. The nanoapp must call
206  * chreBleSocketAccept() to accept ownership of the socket and
207  * subscribe to CHRE_EVENT_BLE_SOCKET_PACKET events.
208  *
209  * @since v1.11
210  */
211 #define CHRE_EVENT_BLE_SOCKET_CONNECTION CHRE_BLE_EVENT_ID(6)
212 
213 /**
214  * nanoappHandleEvent argument: struct chreBleSocketDisconnectionEvent
215  *
216  * This is a unicast event that is sent to a nanoapp when an offloaded socket is
217  * disconnected and can no longer be used by the nanoapp.
218  *
219  * @since v1.11
220  */
221 #define CHRE_EVENT_BLE_SOCKET_DISCONNECTION CHRE_BLE_EVENT_ID(7)
222 
223 /**
224  * nanoappHandleEvent argument: struct chreBleSocketPacketEvent
225  *
226  * This event is sent when the nanoapp receives a packet from the offload
227  * socket.
228  *
229  * @since v1.11
230  */
231 #define CHRE_EVENT_BLE_SOCKET_PACKET CHRE_BLE_EVENT_ID(8)
232 
233 /**
234  * nanoappHandleEvent argument: NULL
235  *
236  * This event is sent when the socket is available to send packets again.
237  *
238  * @since v1.11
239  */
240 #define CHRE_EVENT_BLE_SOCKET_SEND_AVAILABLE CHRE_BLE_EVENT_ID(9)
241 
242 // NOTE: Do not add new events with ID > 15
243 /** @} */
244 
245 /**
246  * Maximum BLE (legacy) advertisement payload data length, in bytes
247  * This is calculated by subtracting 2 (type + len) from 31 (max payload).
248  */
249 #define CHRE_BLE_DATA_LEN_MAX (29)
250 
251 /**
252  * BLE device address length, in bytes.
253  */
254 #define CHRE_BLE_ADDRESS_LEN (6)
255 
256 /**
257  * RSSI value (int8_t) indicating no RSSI threshold.
258  */
259 #define CHRE_BLE_RSSI_THRESHOLD_NONE (-128)
260 
261 /**
262  * RSSI value (int8_t) indicating no RSSI value available.
263  */
264 #define CHRE_BLE_RSSI_NONE (127)
265 
266 /**
267  * Tx power value (int8_t) indicating no Tx power value available.
268  */
269 #define CHRE_BLE_TX_POWER_NONE (127)
270 
271 /**
272  * Indicates ADI field was not provided in advertisement.
273  */
274 #define CHRE_BLE_ADI_NONE (0xFF)
275 
276 /**
277  * The CHRE BLE advertising event type is based on the BT Core Spec v5.2,
278  * Vol 4, Part E, Section 7.7.65.13, LE Extended Advertising Report event,
279  * Event_Type.
280  *
281  * Note: helper functions are provided to avoid bugs, e.g. a nanoapp doing
282  * (eventTypeAndDataStatus == ADV_IND) instead of properly masking off reserved
283  * and irrelevant bits.
284  *
285  * @defgroup CHRE_BLE_EVENT
286  * @{
287  */
288 // Extended event types
289 #define CHRE_BLE_EVENT_MASK_TYPE (0x1f)
290 #define CHRE_BLE_EVENT_TYPE_FLAG_CONNECTABLE (1 << 0)
291 #define CHRE_BLE_EVENT_TYPE_FLAG_SCANNABLE (1 << 1)
292 #define CHRE_BLE_EVENT_TYPE_FLAG_DIRECTED (1 << 2)
293 #define CHRE_BLE_EVENT_TYPE_FLAG_SCAN_RSP (1 << 3)
294 #define CHRE_BLE_EVENT_TYPE_FLAG_LEGACY (1 << 4)
295 
296 // Data status
297 #define CHRE_BLE_EVENT_MASK_DATA_STATUS (0x3 << 5)
298 #define CHRE_BLE_EVENT_DATA_STATUS_COMPLETE (0x0 << 5)
299 #define CHRE_BLE_EVENT_DATA_STATUS_MORE_DATA_PENDING (0x1 << 5)
300 #define CHRE_BLE_EVENT_DATA_STATUS_DATA_TRUNCATED (0x2 << 5)
301 
302 // Legacy event types
303 #define CHRE_BLE_EVENT_TYPE_LEGACY_ADV_IND                                  \
304   (CHRE_BLE_EVENT_TYPE_FLAG_LEGACY | CHRE_BLE_EVENT_TYPE_FLAG_CONNECTABLE | \
305    CHRE_BLE_EVENT_TYPE_FLAG_SCANNABLE)
306 #define CHRE_BLE_EVENT_TYPE_LEGACY_DIRECT_IND \
307   (CHRE_BLE_EVENT_TYPE_FLAG_LEGACY | CHRE_BLE_EVENT_TYPE_FLAG_CONNECTABLE)
308 #define CHRE_BLE_EVENT_TYPE_LEGACY_ADV_SCAN_IND \
309   (CHRE_BLE_EVENT_TYPE_FLAG_LEGACY | CHRE_BLE_EVENT_TYPE_FLAG_SCANNABLE)
310 #define CHRE_BLE_EVENT_TYPE_LEGACY_ADV_NONCONN_IND \
311   (CHRE_BLE_EVENT_TYPE_FLAG_LEGACY)
312 #define CHRE_BLE_EVENT_TYPE_LEGACY_SCAN_RESP_ADV_IND \
313   (CHRE_BLE_EVENT_TYPE_FLAG_SCAN_RSP | CHRE_BLE_EVENT_TYPE_LEGACY_ADV_IND)
314 #define CHRE_BLE_EVENT_TYPE_LEGACY_SCAN_RESP_ADV_SCAN_IND \
315   (CHRE_BLE_EVENT_TYPE_FLAG_SCAN_RSP | CHRE_BLE_EVENT_TYPE_LEGACY_ADV_SCAN_IND)
316 /** @} */
317 
318 /**
319  * The maximum amount of time allowed to elapse between the call to
320  * chreBleFlushAsync() and when CHRE_EVENT_BLE_FLUSH_COMPLETE is delivered to
321  * the nanoapp on a successful flush.
322  */
323 #define CHRE_BLE_FLUSH_COMPLETE_TIMEOUT_NS (5 * CHRE_NSEC_PER_SEC)
324 
325 /**
326  * Indicates a type of request made in this API. Used to populate the resultType
327  * field of struct chreAsyncResult sent with CHRE_EVENT_BLE_ASYNC_RESULT.
328  */
329 enum chreBleRequestType {
330   CHRE_BLE_REQUEST_TYPE_START_SCAN = 1,
331   CHRE_BLE_REQUEST_TYPE_STOP_SCAN = 2,
332   CHRE_BLE_REQUEST_TYPE_FLUSH = 3,      //!< @since v1.7
333   CHRE_BLE_REQUEST_TYPE_READ_RSSI = 4,  //!< @since v1.8
334 };
335 
336 /**
337  * CHRE BLE scan modes identify functional scan levels without specifying or
338  * guaranteeing particular scan parameters (e.g. duty cycle, interval, radio
339  * chain).
340  *
341  * The actual scan parameters may be platform dependent and may change without
342  * notice in real time based on contextual cues, etc.
343  *
344  * Scan modes should be selected based on use cases as described.
345  */
346 enum chreBleScanMode {
347   //! A background scan level for always-running ambient applications.
348   //! A representative duty cycle may be between 3 - 10 % (tentative, and
349   //! with no guarantees).
350   CHRE_BLE_SCAN_MODE_BACKGROUND = 1,
351 
352   //! A foreground scan level to be used for short periods.
353   //! A representative duty cycle may be between 10 - 20 % (tentative, and
354   //! with no guarantees).
355   CHRE_BLE_SCAN_MODE_FOREGROUND = 2,
356 
357   //! A very high duty cycle scan level to be used for very short durations.
358   //! A representative duty cycle may be between 50 - 100 % (tentative, and
359   //! with no guarantees).
360   CHRE_BLE_SCAN_MODE_AGGRESSIVE = 3,
361 };
362 
363 /**
364  * Selected AD Types are available among those defined in the Bluetooth spec.
365  * Assigned Numbers, Generic Access Profile.
366  * ref: https://www.bluetooth.com/specifications/assigned-numbers/
367  */
368 enum chreBleAdType {
369   //! Service Data with 16-bit UUID
370   //! @since v1.8 CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16 was renamed
371   //! CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16_LE to reflect that nanoapps
372   //! compiled against v1.8+ should use OTA format for service data filters.
373   CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16_LE = 0x16,
374 
375   //! Manufacturer Specific Data
376   //! @since v1.8
377   CHRE_BLE_AD_TYPE_MANUFACTURER_DATA = 0xff,
378 };
379 
380 /**
381  * Generic filters are used to filter for the presence of AD structures in the
382  * data field of LE Extended Advertising Report events (ref: BT Core Spec v5.3,
383  * Vol 3, Part E, Section 11).
384  *
385  * The CHRE generic filter structure represents a generic filter on an AD Type
386  * as defined in the Bluetooth spec Assigned Numbers, Generic Access Profile
387  * (ref: https://www.bluetooth.com/specifications/assigned-numbers/). This
388  * generic structure is used by the Android HCI Advertising Packet Content
389  * Filter (APCF) AD Type sub-command 0x09 (ref:
390  * https://source.android.com/docs/core/connect/bluetooth/hci_requirements#le_apcf_command-ad_type_sub_cmd).
391  *
392  * The filter is matched when an advertisement event contains an AD structure in
393  * its data field that matches the following criteria:
394  *   AdStructure.type == type
395  *   AdStructure.data & dataMask == data & dataMask
396  *
397  * The maximum data length is limited to the maximum possible legacy
398  * advertisement payload data length (29 bytes). The data and dataMask must be
399  * in OTA format. For each zero bit of the dataMask, the corresponding
400  * data bit must also be zero.
401  *
402  * Note that the CHRE implementation may not support every kind of filter that
403  * can be represented by this structure. Use chreBleGetFilterCapabilities() to
404  * discover supported filtering capabilities at runtime.
405  *
406  * Example 1: To filter on a 16 bit service data UUID of 0xFE2C, the following
407  * settings would be used:
408  *   type = CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16_LE
409  *   len = 2
410  *   data = {0x2C, 0xFE}
411  *   dataMask = {0xFF, 0xFF}
412  *
413  * Example 2: To filter for manufacturer data of 0x12, 0x34 from Google (0x00E0),
414  * the following settings would be used:
415  *   type = CHRE_BLE_AD_TYPE_MANUFACTURER_DATA
416  *   len = 4
417  *   data = {0xE0, 0x00, 0x12, 0x34}
418  *   dataMask = {0xFF, 0xFF, 0xFF, 0xFF}
419  *
420  * Refer to "Supplement to the Bluetooth Core Specification for details (v9,
421  * Part A, Section 1.4)" for details regarding the manufacturer data format.
422  */
423 struct chreBleGenericFilter {
424   //! Acceptable values among enum chreBleAdType
425   uint8_t type;
426 
427   /**
428    * Length of data and dataMask. AD payloads shorter than this length will not
429    * be matched by the filter. Length must be greater than 0.
430    */
431   uint8_t len;
432 
433   //! Used in combination with dataMask to filter an advertisement
434   uint8_t data[CHRE_BLE_DATA_LEN_MAX];
435 
436   //! Used in combination with data to filter an advertisement
437   uint8_t dataMask[CHRE_BLE_DATA_LEN_MAX];
438 };
439 
440 /**
441  * Broadcaster address filters are used to filter by the address field of the LE
442  * Extended Advertising Report event which is defined in the BT Core Spec v5.3,
443  * Vol 4, Part E, Section 7.7.65.13.
444  *
445  * The CHRE broadcaster address filter structure is modeled after the
446  * Advertising Packet Content Filter (APCF) HCI broadcaster address sub-command
447  * 0x02 (ref:
448  * https://source.android.com/docs/core/connect/bluetooth/hci_requirements#le_apcf_command-broadcast_address_sub_cmd).
449  * However, it differs from this HCI command in two major ways:
450  *
451  * 1) The CHRE broadcaster address filter does not filter by address type at
452  *    this time. If a nanoapp wants to filter for a particular address type, it
453  *    must check the addressType field of the chreBleAdvertisingReport.
454  *
455  * 2) The broadcasterAddress must be in big endian byte order to match the
456  *    format of the Android Bluetooth API (ref:
457  *    https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#getRemoteDevice(byte[])).
458  *    This is intended to allow easier integration between nanoapp and Host
459  *    code.
460  *
461  * The filter is matched when an advertisement even meets the following
462  * criteria:
463  *   broadcasterAddress == chreBleAdvertisingReport.address.
464  *
465  * Example: To filter on the address (01:02:03:AB:CD:EF), the following
466  * settings would be used:
467  *   broadcasterAddress = {0x01, 0x02, 0x03, 0xAB, 0xCD, 0xEF}
468  *
469  * @since v1.9
470  */
471 struct chreBleBroadcasterAddressFilter {
472   //! 6-byte Broadcaster address
473   uint8_t broadcasterAddress[CHRE_BLE_ADDRESS_LEN];
474 };
475 
476 /**
477  * CHRE Bluetooth LE scan filters.
478  *
479  * @see chreBleScanFilterV1_9 for further details.
480  *
481  * @deprecated as of v1.9 due to the addition of the
482  * chreBleBroadcasterAddressFilter. New code should use chreBleScanFilterV1_9
483  * instead of this struct. This struct will be removed in a future version.
484  */
485 struct chreBleScanFilter {
486   //! RSSI threshold filter (Corresponding HCI OCF: 0x0157, Sub: 0x01), where
487   //! advertisements with RSSI values below this threshold may be disregarded.
488   //! An rssiThreshold value of CHRE_BLE_RSSI_THRESHOLD_NONE indicates no RSSI
489   //! filtering.
490   int8_t rssiThreshold;
491 
492   //! Number of generic scan filters provided in the scanFilters array.
493   //! A scanFilterCount value of 0 indicates no generic scan filters.
494   uint8_t scanFilterCount;
495 
496   //! Pointer to an array of scan filters. If the array contains more than one
497   //! entry, advertisements matching any of the entries will be returned
498   //! (functional OR).
499   const struct chreBleGenericFilter *scanFilters;
500 };
501 
502 /**
503  * CHRE Bluetooth LE scan filters are based on a combination of an RSSI
504  * threshold, generic filters, and broadcaster address filters.
505  *
506  * When multiple filters are specified, rssiThreshold is combined with the other
507  * filters via functional AND, and the other filters are all combined as
508  * functional OR. In other words, an advertisement matches the filter if:
509  *   rssi >= rssiThreshold
510  *   AND (matchAny(genericFilters) OR matchAny(broadcasterAddressFilters))
511  *
512  * CHRE-provided filters are implemented in a best-effort manner, depending on
513  * HW capabilities of the system and available resources. Therefore, provided
514  * scan results may be a superset of the specified filters. Nanoapps should try
515  * to take advantage of CHRE scan filters as much as possible, but must design
516  * their logic as to not depend on CHRE filtering.
517  *
518  * The syntax of CHRE scan filter definition is modeled after a combination of
519  * multiple Android HCI Advertising Packet Content Filter (APCF) sub commands
520  * including the RSSI threshold from the set filtering parameters sub command
521  * (ref:
522  * https://source.android.com/docs/core/connect/bluetooth/hci_requirements#le_apcf_command-set_filtering_parameters_sub_cmd).
523  * @see chreBleGenericFilter and chreBleBroadcasterAddressFilter for details
524  * about other APCF sub commands referenced.
525  *
526  * @since v1.9
527  */
528 struct chreBleScanFilterV1_9 {
529   //! RSSI threshold filter (Corresponding HCI OCF: 0x0157, Sub: 0x01), where
530   //! advertisements with RSSI values below this threshold may be disregarded.
531   //! An rssiThreshold value of CHRE_BLE_RSSI_THRESHOLD_NONE indicates no RSSI
532   //! filtering.
533   int8_t rssiThreshold;
534 
535   //! Number of generic filters provided in the scanFilters array. A
536   //! genericFilterCount value of 0 indicates no generic filters.
537   uint8_t genericFilterCount;
538 
539   //! Pointer to an array of generic filters. If the array contains more than
540   //! one entry, advertisements matching any of the entries will be returned
541   //! (functional OR). This is expected to be null if genericFilterCount is 0.
542   const struct chreBleGenericFilter *genericFilters;
543 
544   //! Number of broadcaster address filters provided in the
545   //! broadcasterAddressFilters array. A broadcasterAddressFilterCount value
546   //! of 0 indicates no broadcaster address filters.
547   uint8_t broadcasterAddressFilterCount;
548 
549   //! Pointer to an array of broadcaster address filters. If the array contains
550   //! more than one entry, advertisements matching any of the entries will be
551   //! returned (functional OR). This is expected to be null if
552   //! broadcasterAddressFilterCount is 0.
553   const struct chreBleBroadcasterAddressFilter *broadcasterAddressFilters;
554 };
555 
556 /**
557  * CHRE BLE advertising address type is based on the BT Core Spec v5.2, Vol 4,
558  * Part E, Section 7.7.65.13, LE Extended Advertising Report event,
559  * Address_Type.
560  */
561 enum chreBleAddressType {
562   //! Public device address.
563   CHRE_BLE_ADDRESS_TYPE_PUBLIC = 0x00,
564 
565   //! Random device address.
566   CHRE_BLE_ADDRESS_TYPE_RANDOM = 0x01,
567 
568   //! Public identity address (corresponds to resolved private address).
569   CHRE_BLE_ADDRESS_TYPE_PUBLIC_IDENTITY = 0x02,
570 
571   //! Random (static) Identity Address (corresponds to resolved private
572   //! address)
573   CHRE_BLE_ADDRESS_TYPE_RANDOM_IDENTITY = 0x03,
574 
575   //! No address provided (anonymous advertisement).
576   CHRE_BLE_ADDRESS_TYPE_NONE = 0xff,
577 };
578 
579 /**
580  * CHRE BLE physical (PHY) channel encoding type, if supported, is based on the
581  * BT Core Spec v5.2, Vol 4, Part E, Section 7.7.65.13, LE Extended Advertising
582  * Report event, entries Primary_PHY and Secondary_PHY.
583  */
584 enum chreBlePhyType {
585   //! No packets on this PHY (only on the secondary channel), or feature not
586   //! supported.
587   CHRE_BLE_PHY_NONE = 0x00,
588 
589   //! LE 1 MBPS PHY encoding.
590   CHRE_BLE_PHY_1M = 0x01,
591 
592   //! LE 2 MBPS PHY encoding (only on the secondary channel).
593   CHRE_BLE_PHY_2M = 0x02,
594 
595   //! LE long-range coded PHY encoding.
596   CHRE_BLE_PHY_CODED = 0x03,
597 };
598 
599 /**
600  * The CHRE BLE Advertising Report event is based on the BT Core Spec v5.2,
601  * Vol 4, Part E, Section 7.7.65.13, LE Extended Advertising Report event, with
602  * the following differences:
603  *
604  * 1) A CHRE timestamp field, which can be useful if CHRE is batching results.
605  * 2) Reordering of the rssi and periodicAdvertisingInterval fields for memory
606  *    alignment (prevent padding).
607  * 3) Addition of four reserved bytes to reclaim padding.
608  * 4) The address fields are formatted in big endian byte order to match the
609  *    order specified for BluetoothDevices in the Android Bluetooth API (ref:
610  *    https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#getRemoteDevice(byte[])).
611  */
612 struct chreBleAdvertisingReport {
613   //! The base timestamp, in nanoseconds, in the same time base as chreGetTime()
614   uint64_t timestamp;
615 
616   //! @see CHRE_BLE_EVENT
617   uint8_t eventTypeAndDataStatus;
618 
619   //! Advertising address type as defined in enum chreBleAddressType
620   uint8_t addressType;
621 
622   //! Advertising device address. Formatted in big endian byte order.
623   uint8_t address[CHRE_BLE_ADDRESS_LEN];
624 
625   //! Advertiser PHY on primary advertising physical channel, if supported, as
626   //! defined in enum chreBlePhyType.
627   uint8_t primaryPhy;
628 
629   //! Advertiser PHY on secondary advertising physical channel, if supported, as
630   //! defined in enum chreBlePhyType.
631   uint8_t secondaryPhy;
632 
633   //! Value of the Advertising SID subfield in the ADI field of the PDU among
634   //! the range of [0, 0x0f].
635   //! CHRE_BLE_ADI_NONE indicates no ADI field was provided.
636   //! Other values are reserved.
637   uint8_t advertisingSid;
638 
639   //! Transmit (Tx) power in dBm. Typical values are [-127, 20].
640   //! CHRE_BLE_TX_POWER_NONE indicates Tx power not available.
641   int8_t txPower;
642 
643   //! Interval of the periodic advertising in 1.25 ms intervals, i.e.
644   //! time = periodicAdvertisingInterval * 1.25 ms
645   //! 0 means no periodic advertising. Minimum value is otherwise 6 (7.5 ms).
646   uint16_t periodicAdvertisingInterval;
647 
648   //! RSSI in dBm. Typical values are [-127, 20].
649   //! CHRE_BLE_RSSI_NONE indicates RSSI is not available.
650   int8_t rssi;
651 
652   //! Direct address type (i.e. only accept connection requests from a known
653   //! peer device) as defined in enum chreBleAddressType.
654   uint8_t directAddressType;
655 
656   //! Direct address (i.e. only accept connection requests from a known peer
657   //! device). Formatted in big endian byte order.
658   uint8_t directAddress[CHRE_BLE_ADDRESS_LEN];
659 
660   //! Length of data field. Acceptable range is [0, 62] for legacy and
661   //! [0, 255] for extended advertisements.
662   uint16_t dataLength;
663 
664   //! dataLength bytes of data, or null if dataLength is 0. This represents
665   //! the ADV_IND payload, optionally concatenated with SCAN_RSP, as indicated
666   //! by eventTypeAndDataStatus.
667   const uint8_t *data;
668 
669   //! Reserved for future use; set to 0
670   uint32_t reserved;
671 };
672 
673 /**
674  * A CHRE BLE Advertising Event can contain any number of CHRE BLE Advertising
675  * Reports (i.e. advertisements).
676  */
677 struct chreBleAdvertisementEvent {
678   //! Reserved for future use; set to 0
679   uint16_t reserved;
680 
681   //! Number of advertising reports in this event
682   uint16_t numReports;
683 
684   //! Array of length numReports
685   const struct chreBleAdvertisingReport *reports;
686 };
687 
688 /**
689  * The RSSI read on a particular LE connection handle, based on the parameters
690  * in BT Core Spec v5.3, Vol 4, Part E, Section 7.5.4, Read RSSI command
691  */
692 struct chreBleReadRssiEvent {
693   //! Structure which contains the cookie associated with the original request,
694   //! along with an error code that indicates request success or failure.
695   struct chreAsyncResult result;
696 
697   //! The handle upon which CHRE attempted to read RSSI.
698   uint16_t connectionHandle;
699 
700   //! The RSSI of the last packet received on this connection, if valid
701   //! (-127 to 20)
702   int8_t rssi;
703 };
704 
705 /**
706  * Describes the current status of the BLE request in the platform.
707  *
708  * @since v1.8
709  */
710 struct chreBleScanStatus {
711   //! The currently configured report delay in the scan configuration.
712   //! If enabled is false, this value does not have meaning.
713   uint32_t reportDelayMs;
714 
715   //! True if the BLE scan is currently enabled. This can be set to false
716   //! if BLE scan was temporarily disabled (e.g. BT subsystem is down,
717   //! or due to user settings).
718   bool enabled;
719 
720   //! Reserved for future use - set to zero.
721   uint8_t reserved[3];
722 };
723 
724 /**
725  * Notifies a nanoapp that a socket has been connected and offloaded and is
726  * ready to be used. The nanoapp is expected to accept ownership of the socket
727  * by calling the chreBleSocketAccept() API. If the nanoapp does not accept
728  * ownership of the socket, the transfer of ownership to the nanoapp is aborted.
729  *
730  * @since v1.11
731  */
732 struct chreBleSocketConnectionEvent {
733   //! Unique identifier for this socket connection. This ID in CHRE matches the
734   //! ID used on the host side. It is valid only while the socket is connected.
735   uint64_t socketId;
736 
737   //! Descriptive socket name provided by the host app that initiated the socket
738   //! offload request. This is not guaranteed to be unique across the system,
739   //! but can help the offload app understand the purpose of the socket when it
740   //! receives a socket connection event. This pointer is only valid for the
741   //! duration of the event.
742   const char *socketName;
743 
744   //! When sending a packet to the socket via chreBleSocketSend(), the length
745   //! must not exceed this value.
746   uint16_t maxTxPacketLength;
747 
748   //! When the nanoapp receives packets from the socket via the
749   //! chreBleSocketPacketEvent, the length will not exceed this value.
750   uint16_t maxRxPacketLength;
751 };
752 
753 /**
754  * Notifies a nanoapp that a socket has been disconnected and can no longer be
755  * used by the nanoapp. Once a socket is disconnected, the same socket ID will
756  * not be reconnected. If the nanoapp wants to continue using an offloaded
757  * socket, a new offloaded socket must be created and connected.
758  *
759  * @since v1.11
760  */
761 struct chreBleSocketDisconnectionEvent {
762   //! @see chreBleSocketConnectionEvent.socketId
763   uint64_t socketId;
764 };
765 
766 /**
767  * Notifies a nanoapp that it has received a packet from a socket.
768  *
769  * @since v1.11
770  */
771 struct chreBleSocketPacketEvent {
772   //! @see chreBleSocketConnectionEvent.socketId
773   uint64_t socketId;
774 
775   //! Length of data in bytes. The length will not exceed the maxRxPacketLength
776   //! provided in the CHRE event CHRE_EVENT_BLE_SOCKET_CONNECTION.
777   uint16_t length;
778 
779   //! Packet payload that is length bytes.
780   const uint8_t *data;
781 };
782 
783 /**
784  * Result code used with chreBleSocketSend().
785  *
786  * @since v1.11
787  */
788 enum chreBleSocketSendStatus {
789   //! The packet has successfully been sent to the platform layer.
790   CHRE_BLE_SOCKET_SEND_STATUS_SUCCESS = 1,
791 
792   //! The packet will not be sent.
793   CHRE_BLE_SOCKET_SEND_STATUS_FAILURE = 2,
794 
795   //! The packet cannot be sent at this time because too many packets are in
796   //! flight. The nanoapp will be notified via a
797   //! CHRE_EVENT_BLE_SOCKET_SEND_AVAILABLE event when the socket is available to
798   //! send the packet.
799   CHRE_BLE_SOCKET_SEND_STATUS_QUEUE_FULL = 3,
800 };
801 
802 /**
803  * Callback which frees the packet sent via chreBleSocketSend().
804  *
805  * This callback is (optionally) provided to the chreBleSocketSend() function as
806  * a means for freeing the packet. When this callback is invoked, the packet is
807  * no longer needed and can be released. Note that this in no way assures that
808  * said packet was sent to the offload socket, simply that this memory is no
809  * longer needed.
810  *
811  * @param data The data argument from chreBleSocketSend().
812  * @param length The length argument from chreBleSocketSend().
813  *
814  * @see chreBleSocketSend()
815  *
816  * @since v1.11
817  */
818 typedef void(chreBleSocketPacketFreeFunction)(void *data, uint16_t length);
819 
820 /**
821  * Retrieves a set of flags indicating the BLE features supported by the
822  * current CHRE implementation. The value returned by this function must be
823  * consistent for the entire duration of the nanoapp's execution.
824  *
825  * The client must allow for more flags to be set in this response than it knows
826  * about, for example if the implementation supports a newer version of the API
827  * than the client was compiled against.
828  *
829  * @return A bitmask with zero or more CHRE_BLE_CAPABILITIES_* flags set. @see
830  *         CHRE_BLE_CAPABILITIES
831  *
832  * @since v1.6
833  */
834 uint32_t chreBleGetCapabilities(void);
835 
836 /**
837  * Retrieves a set of flags indicating the BLE filtering features supported by
838  * the current CHRE implementation. The value returned by this function must be
839  * consistent for the entire duration of the nanoapp's execution.
840  *
841  * The client must allow for more flags to be set in this response than it knows
842  * about, for example if the implementation supports a newer version of the API
843  * than the client was compiled against.
844  *
845  * @return A bitmask with zero or more CHRE_BLE_FILTER_CAPABILITIES_* flags set.
846  *         @see CHRE_BLE_FILTER_CAPABILITIES
847  *
848  * @since v1.6
849  */
850 uint32_t chreBleGetFilterCapabilities(void);
851 
852 /**
853  * Helper function to extract event type from eventTypeAndDataStatus as defined
854  * in the BT Core Spec v5.2, Vol 4, Part E, Section 7.7.65.13, LE Extended
855  * Advertising Report event, entry Event_Type.
856  *
857  * @see CHRE_BLE_EVENT
858  *
859  * @param eventTypeAndDataStatus Combined event type and data status
860  *
861  * @return The event type portion of eventTypeAndDataStatus
862  */
chreBleGetEventType(uint8_t eventTypeAndDataStatus)863 static inline uint8_t chreBleGetEventType(uint8_t eventTypeAndDataStatus) {
864   return (eventTypeAndDataStatus & CHRE_BLE_EVENT_MASK_TYPE);
865 }
866 
867 /**
868  * Helper function to extract data status from eventTypeAndDataStatus as defined
869  * in the BT Core Spec v5.2, Vol 4, Part E, Section 7.7.65.13, LE Extended
870  * Advertising Report event, entry Event_Type.
871  *
872  * @see CHRE_BLE_EVENT
873  *
874  * @param eventTypeAndDataStatus Combined event type and data status
875  *
876  * @return The data status portion of eventTypeAndDataStatus
877  */
chreBleGetDataStatus(uint8_t eventTypeAndDataStatus)878 static inline uint8_t chreBleGetDataStatus(uint8_t eventTypeAndDataStatus) {
879   return (eventTypeAndDataStatus & CHRE_BLE_EVENT_MASK_DATA_STATUS);
880 }
881 
882 /**
883  * Helper function to to combine an event type with a data status to create
884  * eventTypeAndDataStatus as defined in the BT Core Spec v5.2, Vol 4, Part E,
885  * Section 7.7.65.13, LE Extended Advertising Report event, entry Event_Type.
886  *
887  * @see CHRE_BLE_EVENT
888  *
889  * @param eventType Event type
890  * @param dataStatus Data status
891  *
892  * @return A combined eventTypeAndDataStatus
893  */
chreBleGetEventTypeAndDataStatus(uint8_t eventType,uint8_t dataStatus)894 static inline uint8_t chreBleGetEventTypeAndDataStatus(uint8_t eventType,
895                                                        uint8_t dataStatus) {
896   return ((eventType & CHRE_BLE_EVENT_MASK_TYPE) |
897           (dataStatus & CHRE_BLE_EVENT_MASK_DATA_STATUS));
898 }
899 
900 /**
901  * Nanoapps must define CHRE_NANOAPP_USES_BLE somewhere in their build
902  * system (e.g. Makefile) if the nanoapp needs to use the following BLE APIs.
903  * In addition to allowing access to these APIs, defining this macro will also
904  * ensure CHRE enforces that all host clients this nanoapp talks to have the
905  * required Android permissions needed to access BLE functionality by adding
906  * metadata to the nanoapp.
907  */
908 #if defined(CHRE_NANOAPP_USES_BLE) || !defined(CHRE_IS_NANOAPP_BUILD)
909 
910 /**
911  * Start Bluetooth LE (BLE) scanning on CHRE.
912  *
913  * @see chreBleStartScanAsyncV1_9 for further details.
914  *
915  * @deprecated as of v1.9 due to the addition of the chreBleScanFilterV1_9
916  * struct and a cookie parameter. New code should use
917  * chreBleStartScanAsyncV1_9() instead of this function. This function will be
918  * removed in a future version.
919  */
920 bool chreBleStartScanAsync(enum chreBleScanMode mode, uint32_t reportDelayMs,
921                            const struct chreBleScanFilter *filter);
922 
923 /**
924  * Start Bluetooth LE (BLE) scanning on CHRE.
925  *
926  * The result of the operation will be delivered asynchronously via the CHRE
927  * event CHRE_EVENT_BLE_ASYNC_RESULT.
928  *
929  * The scan results will be delivered asynchronously via the CHRE event
930  * CHRE_EVENT_BLE_ADVERTISEMENT.
931  *
932  * If CHRE_USER_SETTING_BLE_AVAILABLE is disabled, CHRE is expected to return an
933  * async result with error CHRE_ERROR_FUNCTION_DISABLED. If this setting is
934  * enabled, the Bluetooth subsystem may still be powered down in the scenario
935  * where the main Bluetooth toggle is disabled, but the Bluetooth scanning
936  * setting is enabled, and there is no request for BLE to be enabled at the
937  * Android level. In this scenario, CHRE will return an async result with error
938  * CHRE_ERROR_FUNCTION_DISABLED.
939  *
940  * To ensure that Bluetooth remains powered on in this settings configuration so
941  * that a nanoapp can scan, the nanoapp's Android host entity should use the
942  * BluetoothAdapter.enableBLE() API to register this request with the Android
943  * Bluetooth stack.
944  *
945  * If chreBleStartScanAsync() is called while a previous scan has been started,
946  * the previous scan will be stopped first and replaced with the new scan.
947  *
948  * Note that some corresponding Android parameters are missing from the CHRE
949  * API, where the following default or typical parameters are used:
950  * Callback type: CALLBACK_TYPE_ALL_MATCHES
951  * Result type: SCAN_RESULT_TYPE_FULL
952  * Match mode: MATCH_MODE_AGGRESSIVE
953  * Number of matches per filter: MATCH_NUM_MAX_ADVERTISEMENT
954  * Legacy-only: false
955  * PHY type: PHY_LE_ALL_SUPPORTED
956  *
957  * A CHRE_EVENT_BLE_SCAN_STATUS_CHANGE will be generated if the values in
958  * chreBleScanStatus changes as a result of this call.
959  *
960  * @param mode Scanning mode selected among enum chreBleScanMode
961  * @param reportDelayMs Maximum requested batching delay in ms. 0 indicates no
962  *                      batching. Note that the system may deliver results
963  *                      before the maximum specified delay is reached.
964  * @param filter Pointer to the requested best-effort filter configuration as
965  *               defined by struct chreBleScanFilter. The ownership of filter
966  *               and its nested elements remains with the caller, and the caller
967  *               may release it as soon as chreBleStartScanAsync() returns.
968  * @param cookie An opaque value that will be included in the chreAsyncResult
969  *               sent as a response to this request.
970  *
971  * @return True to indicate that the request was accepted. False otherwise.
972  *
973  * @since v1.9
974  */
975 bool chreBleStartScanAsyncV1_9(enum chreBleScanMode mode,
976                                uint32_t reportDelayMs,
977                                const struct chreBleScanFilterV1_9 *filter,
978                                const void *cookie);
979 
980 /**
981  * Stops a CHRE BLE scan.
982  *
983  * @see chreBleStopScanAsyncV1_9 for further details.
984  *
985  * @deprecated as of v1.9 due to the addition of the cookie parameter. New code
986  * should use chreBleStopScanAsyncV1_9() instead of this function. This function
987  * will be removed in a future version.
988  */
989 bool chreBleStopScanAsync(void);
990 
991 /**
992  * Stops a CHRE BLE scan.
993  *
994  * The result of the operation will be delivered asynchronously via the CHRE
995  * event CHRE_EVENT_BLE_ASYNC_RESULT.
996  *
997  * @param cookie An opaque value that will be included in the chreAsyncResult
998  *               sent as a response to this request.
999  *
1000  * @return True to indicate that the request was accepted. False otherwise.
1001  *
1002  * @since v1.9
1003  */
1004 bool chreBleStopScanAsyncV1_9(const void *cookie);
1005 
1006 /**
1007  * Requests to immediately deliver batched scan results. The nanoapp must
1008  * have an active BLE scan request. If a request is accepted, it will be treated
1009  * as though the reportDelayMs has expired for a batched scan. Upon accepting
1010  * the request, CHRE works to immediately deliver scan results currently kept in
1011  * batching memory, if any, via regular CHRE_EVENT_BLE_ADVERTISEMENT events,
1012  * followed by a CHRE_EVENT_BLE_FLUSH_COMPLETE event.
1013  *
1014  * If the underlying system fails to complete the flush operation within
1015  * CHRE_BLE_FLUSH_COMPLETE_TIMEOUT_NS, CHRE will send a
1016  * CHRE_EVENT_BLE_FLUSH_COMPLETE event with CHRE_ERROR_TIMEOUT.
1017  *
1018  * If multiple flush requests are made prior to flush completion, then the
1019  * requesting nanoapp will receive all batched samples existing at the time of
1020  * the latest flush request. In this case, the number of
1021  * CHRE_EVENT_BLE_FLUSH_COMPLETE events received must equal the number of flush
1022  * requests made.
1023  *
1024  * If chreBleStopScanAsync() is called while a flush operation is in progress,
1025  * it is unspecified whether the flush operation will complete successfully or
1026  * return an error, such as CHRE_ERROR_FUNCTION_DISABLED, but in any case,
1027  * CHRE_EVENT_BLE_FLUSH_COMPLETE must still be delivered. The same applies if
1028  * the Bluetooth user setting is disabled during a flush operation.
1029  *
1030  * If called while running on a CHRE API version below v1.7, this function
1031  * returns false and has no effect.
1032  *
1033  * @param cookie An opaque value that will be included in the chreAsyncResult
1034  *               sent as a response to this request.
1035  *
1036  * @return True to indicate the request was accepted. False otherwise.
1037  *
1038  * @since v1.7
1039  */
1040 bool chreBleFlushAsync(const void *cookie);
1041 
1042 /**
1043  * Requests to read the RSSI of a peer device on the given LE connection
1044  * handle.
1045  *
1046  * If the request is accepted, the response will be delivered in a
1047  * CHRE_EVENT_BLE_RSSI_READ event with the same cookie.
1048  *
1049  * The request may be rejected if resources are not available to service the
1050  * request (such as if too many outstanding requests already exist). If so, the
1051  * client may retry later.
1052  *
1053  * Note that the connectionHandle is valid only while the connection remains
1054  * active. If a peer device disconnects then reconnects, the handle may change.
1055  * BluetoothDevice#getConnectionHandle() can be used from the Android framework
1056  * to get the latest handle upon reconnection.
1057  *
1058  * @param connectionHandle
1059  * @param cookie An opaque value that will be included in the chreAsyncResult
1060  *               embedded in the response to this request.
1061  * @return True if the request has been accepted and dispatched to the
1062  *         controller. False otherwise.
1063  *
1064  * @since v1.8
1065  *
1066  */
1067 bool chreBleReadRssiAsync(uint16_t connectionHandle, const void *cookie);
1068 
1069 /**
1070  * Retrieves the current state of the BLE scan on the platform.
1071  *
1072  * @param status A non-null pointer to where the scan status will be
1073  *               populated.
1074  *
1075  * @return True if the status was obtained successfully.
1076  *
1077  * @since v1.8
1078  */
1079 bool chreBleGetScanStatus(struct chreBleScanStatus *status);
1080 
1081 /**
1082  * Accepts that this nanoapp owns the socket and subscribes to
1083  * CHRE_EVENT_BLE_SOCKET_PACKET events from this socket. This API is only
1084  * valid to call while handling the CHRE_EVENT_BLE_SOCKET_CONNECTION event.
1085  *
1086  * @param socketId @see chreBleSocketConnectionEvent.socketId
1087  * @return True if CHRE confirms that socket ownership has been transferred.
1088  *
1089  * @since v1.11
1090  */
1091 bool chreBleSocketAccept(uint64_t socketId);
1092 
1093 /**
1094  * Sends a packet to the socket with the corresponding socketId. This API can
1095  * only be used after the nanoapp has received a
1096  * CHRE_EVENT_BLE_SOCKET_CONNECTION event indicating the offloaded socket is
1097  * connected and has accepted ownership of the socket by calling
1098  * chreBleSocketAccept().
1099  *
1100  * NOTE: freeCallback WILL NOT be invoked if the return status is
1101  * CHRE_BLE_SOCKET_SEND_STATUS_QUEUE_FULL.
1102  *
1103  * @param socketId @see chreBleSocketConnectionEvent.socketId
1104  * @param data Packet to be sent to the socket that is length bytes. After this
1105  *     API is called, ownership of this memory passes to CHRE and the nanoapp
1106  *     must ensure that the packet remains valid and unmodified until the
1107  *     freeCallback is invoked.
1108   * @param length Length of packet to be sent to the socket in bytes. Cannot
1109  *     exceed the maxTxPacketLength provided in the CHRE event
1110  *     CHRE_EVENT_BLE_SOCKET_CONNECTION.
1111  * @param freeCallback Callback invoked to indicate that the packet data buffer
1112  *     is not needed by CHRE anymore. Note that invocation of this function does
1113  *     not mean that the packet has been delivered, only that memory can be
1114  *     released. This is guaranteed to be invoked if this function returns
1115  *     CHRE_BLE_SOCKET_SEND_STATUS_SUCCESS or
1116  *     CHRE_BLE_SOCKET_SEND_STATUS_FAILURE, but WILL NOT be invoked for
1117  *     CHRE_BLE_SOCKET_SEND_STATUS_QUEUE_FULL. This may be invoked
1118  *     synchronously, so nanoapp developers should not call chreBleSocketSend()
1119  *     from within the callback to avoid potential infinite recursion.
1120  * @return A value from enum chreBleSocketSendStatus.
1121  *
1122  * @since v1.11
1123  */
1124 int32_t chreBleSocketSend(uint64_t socketId, const void *data, uint16_t length,
1125                           chreBleSocketPacketFreeFunction *freeCallback);
1126 
1127 /**
1128  * Definitions for handling unsupported CHRE BLE scenarios.
1129  */
1130 #else  // defined(CHRE_NANOAPP_USES_BLE) || !defined(CHRE_IS_NANOAPP_BUILD)
1131 
1132 #define CHRE_BLE_PERM_ERROR_STRING                                       \
1133   "CHRE_NANOAPP_USES_BLE must be defined when building this nanoapp in " \
1134   "order to refer to "
1135 
1136 #define chreBleStartScanAsync(...) \
1137   CHRE_BUILD_ERROR(CHRE_BLE_PERM_ERROR_STRING "chreBleStartScanAsync")
1138 
1139 #define chreBleStopScanAsync(...) \
1140   CHRE_BUILD_ERROR(CHRE_BLE_PERM_ERROR_STRING "chreBleStopScanAsync")
1141 
1142 #define chreBleFlushAsync(...) \
1143   CHRE_BUILD_ERROR(CHRE_BLE_PERM_ERROR_STRING "chreBleFlushAsync")
1144 
1145 #define chreBleReadRssiAsync(...) \
1146   CHRE_BUILD_ERROR(CHRE_BLE_PERM_ERROR_STRING "chreBleReadRssiAsync")
1147 
1148 #define chreBleGetScanStatus(...) \
1149   CHRE_BUILD_ERROR(CHRE_BLE_PERM_ERROR_STRING "chreBleGetScanStatus")
1150 
1151 #define chreBleSocketAccept(...) \
1152   CHRE_BUILD_ERROR(CHRE_BLE_PERM_ERROR_STRING "chreBleSocketAccept")
1153 
1154 #define chreBleSocketSend(...) \
1155   CHRE_BUILD_ERROR(CHRE_BLE_PERM_ERROR_STRING "chreBleSocketSend")
1156 
1157 #endif  // defined(CHRE_NANOAPP_USES_BLE) || !defined(CHRE_IS_NANOAPP_BUILD)
1158 
1159 #ifdef __cplusplus
1160 }
1161 #endif
1162 
1163 #endif /* CHRE_BLE_H_ */
1164