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 #ifndef CHRE_BLE_H_
17 #define CHRE_BLE_H_
18
19 /**
20 * @file
21 * CHRE BLE (Bluetooth Low Energy, Bluetooth LE) API.
22 * The CHRE BLE API currently supports BLE scanning features.
23 *
24 * The features in the CHRE BLE API are a subset and adaptation of Android
25 * capabilities as described in the Android BLE API and HCI requirements.
26 * ref:
27 * https://developer.android.com/guide/topics/connectivity/bluetooth/ble-overview
28 * ref: https://source.android.com/devices/bluetooth/hci_requirements
29 */
30
31 #include <chre/common.h>
32 #include <stdbool.h>
33 #include <stddef.h>
34 #include <stdint.h>
35 #include <string.h>
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 /**
42 * The set of flags returned by chreBleGetCapabilities().
43 *
44 * @defgroup CHRE_BLE_CAPABILITIES
45 * @{
46 */
47 //! No BLE APIs are supported
48 #define CHRE_BLE_CAPABILITIES_NONE UINT32_C(0)
49
50 //! CHRE supports BLE scanning
51 #define CHRE_BLE_CAPABILITIES_SCAN UINT32_C(1 << 0)
52
53 //! CHRE BLE supports batching of scan results, either through Android-specific
54 //! HCI (OCF: 0x156), or by the CHRE framework, internally.
55 //! @since v1.7 Platforms with this capability must also support flushing scan
56 //! results during a batched scan.
57 #define CHRE_BLE_CAPABILITIES_SCAN_RESULT_BATCHING UINT32_C(1 << 1)
58
59 //! CHRE BLE scan supports best-effort hardware filtering. If filtering is
60 //! available, chreBleGetFilterCapabilities() returns a bitmap indicating the
61 //! specific filtering capabilities that are supported.
62 //! To differentiate best-effort vs. no filtering, the following requirement
63 //! must be met for this flag:
64 //! If only one nanoapp is requesting BLE scans and there are no BLE scans from
65 //! the AP, only filtered results will be provided to the nanoapp.
66 #define CHRE_BLE_CAPABILITIES_SCAN_FILTER_BEST_EFFORT UINT32_C(1 << 2)
67 /** @} */
68
69 /**
70 * The set of flags returned by chreBleGetFilterCapabilities().
71 *
72 * The representative bit for each filtering capability is based on the sub-OCF
73 * of the Android filtering HCI vendor-specific command (LE_APCF_Command, OCF:
74 * 0x0157) for that particular filtering capability, as found in
75 * https://source.android.com/devices/bluetooth/hci_requirements
76 *
77 * For example, the Service Data filter has a sub-command of 0x7; hence
78 * the filtering capability is indicated by (1 << 0x7).
79 *
80 * @defgroup CHRE_BLE_FILTER_CAPABILITIES
81 * @{
82 */
83 //! No CHRE BLE filters are supported
84 #define CHRE_BLE_FILTER_CAPABILITIES_NONE UINT32_C(0)
85
86 //! CHRE BLE supports RSSI filters
87 #define CHRE_BLE_FILTER_CAPABILITIES_RSSI UINT32_C(1 << 1)
88
89 //! CHRE BLE supports Service Data filters (Corresponding HCI OCF: 0x0157,
90 //! Sub-command: 0x07)
91 #define CHRE_BLE_FILTER_CAPABILITIES_SERVICE_DATA UINT32_C(1 << 7)
92 /** @} */
93
94 /**
95 * Produce an event ID in the block of IDs reserved for BLE.
96 *
97 * Valid input range is [0, 15]. Do not add new events with ID > 15
98 * (see chre/event.h)
99 *
100 * @param offset Index into BLE event ID block; valid range is [0, 15].
101 *
102 * @defgroup CHRE_BLE_EVENT_ID
103 * @{
104 */
105 #define CHRE_BLE_EVENT_ID(offset) (CHRE_EVENT_BLE_FIRST_EVENT + (offset))
106
107 /**
108 * nanoappHandleEvent argument: struct chreAsyncResult
109 *
110 * Communicates the asynchronous result of a request to the BLE API. The
111 * requestType field in {@link #chreAsyncResult} is set to a value from enum
112 * chreBleRequestType.
113 *
114 * This is used for results of async config operations which need to
115 * interop with lower level code (potentially in a different thread) or send an
116 * HCI command to the FW and wait on the response.
117 */
118 #define CHRE_EVENT_BLE_ASYNC_RESULT CHRE_BLE_EVENT_ID(0)
119
120 /**
121 * nanoappHandleEvent argument: struct chreBleAdvertisementEvent
122 *
123 * Provides results of a BLE scan.
124 */
125 #define CHRE_EVENT_BLE_ADVERTISEMENT CHRE_BLE_EVENT_ID(1)
126
127 /**
128 * nanoappHandleEvent argument: struct chreAsyncResult
129 *
130 * Indicates that a flush request made via chreBleFlushAsync() is complete, and
131 * all batched advertisements resulting from the flush have been delivered via
132 * preceding CHRE_EVENT_BLE_ADVERTISEMENT events.
133 *
134 * @since v1.7
135 */
136 #define CHRE_EVENT_BLE_FLUSH_COMPLETE CHRE_BLE_EVENT_ID(2)
137
138 // NOTE: Do not add new events with ID > 15
139 /** @} */
140
141 /**
142 * Maximum BLE (legacy) advertisement payload data length, in bytes
143 * This is calculated by subtracting 2 (type + len) from 31 (max payload).
144 */
145 #define CHRE_BLE_DATA_LEN_MAX (29)
146
147 /**
148 * BLE device address length, in bytes.
149 */
150 #define CHRE_BLE_ADDRESS_LEN (6)
151
152 /**
153 * RSSI value (int8_t) indicating no RSSI threshold.
154 */
155 #define CHRE_BLE_RSSI_THRESHOLD_NONE (-128)
156
157 /**
158 * RSSI value (int8_t) indicating no RSSI value available.
159 */
160 #define CHRE_BLE_RSSI_NONE (127)
161
162 /**
163 * Tx power value (int8_t) indicating no Tx power value available.
164 */
165 #define CHRE_BLE_TX_POWER_NONE (127)
166
167 /**
168 * Indicates ADI field was not provided in advertisement.
169 */
170 #define CHRE_BLE_ADI_NONE (0xFF)
171
172 /**
173 * The CHRE BLE advertising event type is based on the BT Core Spec v5.2,
174 * Vol 4, Part E, Section 7.7.65.13, LE Extended Advertising Report event,
175 * Event_Type.
176 *
177 * Note: helper functions are provided to avoid bugs, e.g. a nanoapp doing
178 * (eventTypeAndDataStatus == ADV_IND) instead of properly masking off reserved
179 * and irrelevant bits.
180 *
181 * @defgroup CHRE_BLE_EVENT
182 * @{
183 */
184 // Extended event types
185 #define CHRE_BLE_EVENT_MASK_TYPE (0x1f)
186 #define CHRE_BLE_EVENT_TYPE_FLAG_CONNECTABLE (1 << 0)
187 #define CHRE_BLE_EVENT_TYPE_FLAG_SCANNABLE (1 << 1)
188 #define CHRE_BLE_EVENT_TYPE_FLAG_DIRECTED (1 << 2)
189 #define CHRE_BLE_EVENT_TYPE_FLAG_SCAN_RSP (1 << 3)
190 #define CHRE_BLE_EVENT_TYPE_FLAG_LEGACY (1 << 4)
191
192 // Data status
193 #define CHRE_BLE_EVENT_MASK_DATA_STATUS (0x3 << 5)
194 #define CHRE_BLE_EVENT_DATA_STATUS_COMPLETE (0x0 << 5)
195 #define CHRE_BLE_EVENT_DATA_STATUS_MORE_DATA_PENDING (0x1 << 5)
196 #define CHRE_BLE_EVENT_DATA_STATUS_DATA_TRUNCATED (0x2 << 5)
197
198 // Legacy event types
199 #define CHRE_BLE_EVENT_TYPE_LEGACY_ADV_IND \
200 (CHRE_BLE_EVENT_TYPE_FLAG_LEGACY | CHRE_BLE_EVENT_TYPE_FLAG_CONNECTABLE | \
201 CHRE_BLE_EVENT_TYPE_FLAG_SCANNABLE)
202 #define CHRE_BLE_EVENT_TYPE_LEGACY_DIRECT_IND \
203 (CHRE_BLE_EVENT_TYPE_FLAG_LEGACY | CHRE_BLE_EVENT_TYPE_FLAG_CONNECTABLE)
204 #define CHRE_BLE_EVENT_TYPE_LEGACY_ADV_SCAN_IND \
205 (CHRE_BLE_EVENT_TYPE_FLAG_LEGACY | CHRE_BLE_EVENT_TYPE_FLAG_SCANNABLE)
206 #define CHRE_BLE_EVENT_TYPE_LEGACY_ADV_NONCONN_IND \
207 (CHRE_BLE_EVENT_TYPE_FLAG_LEGACY)
208 #define CHRE_BLE_EVENT_TYPE_LEGACY_SCAN_RESP_ADV_IND \
209 (CHRE_BLE_EVENT_TYPE_FLAG_SCAN_RSP | CHRE_BLE_EVENT_TYPE_LEGACY_ADV_IND)
210 #define CHRE_BLE_EVENT_TYPE_LEGACY_SCAN_RESP_ADV_SCAN_IND \
211 (CHRE_BLE_EVENT_TYPE_FLAG_SCAN_RSP | CHRE_BLE_EVENT_TYPE_LEGACY_ADV_SCAN_IND)
212 /** @} */
213
214 /**
215 * The maximum amount of time allowed to elapse between the call to
216 * chreBleFlushAsync() and when CHRE_EVENT_BLE_FLUSH_COMPLETE is delivered to
217 * the nanoapp on a successful flush.
218 */
219 #define CHRE_BLE_FLUSH_COMPLETE_TIMEOUT_NS (5 * CHRE_NSEC_PER_SEC)
220
221 /**
222 * Indicates a type of request made in this API. Used to populate the resultType
223 * field of struct chreAsyncResult sent with CHRE_EVENT_BLE_ASYNC_RESULT.
224 */
225 enum chreBleRequestType {
226 CHRE_BLE_REQUEST_TYPE_START_SCAN = 1,
227 CHRE_BLE_REQUEST_TYPE_STOP_SCAN = 2,
228 CHRE_BLE_REQUEST_TYPE_FLUSH = 3, //!< @since v1.7
229 };
230
231 /**
232 * CHRE BLE scan modes identify functional scan levels without specifying or
233 * guaranteeing particular scan parameters (e.g. duty cycle, interval, radio
234 * chain).
235 *
236 * The actual scan parameters may be platform dependent and may change without
237 * notice in real time based on contextual cues, etc.
238 *
239 * Scan modes should be selected based on use cases as described.
240 */
241 enum chreBleScanMode {
242 //! A background scan level for always-running ambient applications.
243 //! A representative duty cycle may be between 3 - 10 % (tentative, and
244 //! with no guarantees).
245 CHRE_BLE_SCAN_MODE_BACKGROUND = 1,
246
247 //! A foreground scan level to be used for short periods.
248 //! A representative duty cycle may be between 10 - 20 % (tentative, and
249 //! with no guarantees).
250 CHRE_BLE_SCAN_MODE_FOREGROUND = 2,
251
252 //! A very high duty cycle scan level to be used for very short durations.
253 //! A representative duty cycle may be between 50 - 100 % (tentative, and
254 //! with no guarantees).
255 CHRE_BLE_SCAN_MODE_AGGRESSIVE = 3,
256 };
257
258 /**
259 * Selected AD Types are available among those defined in the Bluetooth spec.
260 * Assigned Numbers, Generic Access Profile.
261 * ref: https://www.bluetooth.com/specifications/assigned-numbers/
262 */
263 enum chreBleAdType {
264 //! Service Data with 16-bit UUID
265 CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16 = 0x16,
266 };
267
268 /**
269 * Generic scan filters definition based on AD Type, mask, and values. The
270 * maximum data length is limited to the maximum possible legacy advertisement
271 * payload data length (29 bytes).
272 *
273 * The filter is matched when
274 * data & dataMask == advData & dataMask
275 * where advData is the advertisement packet data for the specified AD type.
276 *
277 * The CHRE generic filter structure represents a generic filter on an AD Type
278 * as defined in the Bluetooth spec Assigned Numbers, Generic Access Profile
279 * (ref: https://www.bluetooth.com/specifications/assigned-numbers/). This
280 * generic structure is used by the Advertising Packet Content Filter
281 * (APCF) HCI generic AD type sub-command 0x08 (ref:
282 * https://source.android.com/devices/bluetooth/hci_requirements#le_apcf_command).
283 *
284 * Note that the CHRE implementation may not support every kind of filter that
285 * can be represented by this structure. Use chreBleGetFilterCapabilities() to
286 * discover supported filtering capabilities at runtime.
287 *
288 * For example, to filter on a 16 bit service data UUID of 0xFE2C, the following
289 * settings would be used:
290 * type = CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16
291 * len = 2
292 * data = {0xFE, 0x2C}
293 * dataMask = {0xFF, 0xFF}
294 */
295 struct chreBleGenericFilter {
296 //! Acceptable values among enum chreBleAdType
297 uint8_t type;
298
299 /**
300 * Length of data and dataMask. AD payloads shorter than this length will not
301 * be matched by the filter. Length must be greater than 0.
302 */
303 uint8_t len;
304
305 //! Used in combination with dataMask to filter an advertisement
306 uint8_t data[CHRE_BLE_DATA_LEN_MAX];
307
308 //! Used in combination with data to filter an advertisement
309 uint8_t dataMask[CHRE_BLE_DATA_LEN_MAX];
310 };
311
312 /**
313 * CHRE Bluetooth LE scan filters are based on a combination of an RSSI
314 * threshold and generic scan filters as defined by AD Type, mask, and values.
315 *
316 * CHRE-provided filters are implemented in a best-effort manner, depending on
317 * HW capabilities of the system and available resources. Therefore, provided
318 * scan results may be a superset of the specified filters. Nanoapps should try
319 * to take advantage of CHRE scan filters as much as possible, but must design
320 * their logic as to not depend on CHRE filtering.
321 *
322 * The syntax of CHRE scan filter definitions are based on the Android
323 * Advertising Packet Content Filter (APCF) HCI requirement subtype 0x08
324 * ref:
325 * https://source.android.com/devices/bluetooth/hci_requirements#le_apcf_command-set_filtering_parameters_sub_cmd
326 * and AD Types as defined in the Bluetooth spec Assigned Numbers, Generic
327 * Access Profile
328 * ref: https://www.bluetooth.com/specifications/assigned-numbers/
329 *
330 * Even though the scan filters are defined in a generic manner, CHRE Bluetooth
331 * is expected to initially support only a limited set of AD Types.
332 */
333 struct chreBleScanFilter {
334 //! RSSI threshold filter (Corresponding HCI OCF: 0x0157, Sub: 0x01), where
335 //! advertisements with RSSI values below this threshold may be disregarded.
336 //! An rssiThreshold value of CHRE_BLE_RSSI_THRESHOLD_NONE indicates no RSSI
337 //! filtering.
338 int8_t rssiThreshold;
339
340 //! Number of generic scan filters provided in the scanFilters array.
341 //! A scanFilterCount value of 0 indicates no generic scan filters.
342 uint8_t scanFilterCount;
343
344 //! Pointer to an array of scan filters. If the array contains more than one
345 //! entry, advertisements matching any of the entries will be returned
346 //! (functional OR).
347 const struct chreBleGenericFilter *scanFilters;
348 };
349
350 /**
351 * CHRE BLE advertising address type is based on the BT Core Spec v5.2, Vol 4,
352 * Part E, Section 7.7.65.13, LE Extended Advertising Report event,
353 * Address_Type.
354 */
355 enum chreBleAddressType {
356 //! Public device address.
357 CHRE_BLE_ADDRESS_TYPE_PUBLIC = 0x00,
358
359 //! Random device address.
360 CHRE_BLE_ADDRESS_TYPE_RANDOM = 0x01,
361
362 //! Public identity address (corresponds to resolved private address).
363 CHRE_BLE_ADDRESS_TYPE_PUBLIC_IDENTITY = 0x02,
364
365 //! Random (static) Identity Address (corresponds to resolved private
366 //! address)
367 CHRE_BLE_ADDRESS_TYPE_RANDOM_IDENTITY = 0x03,
368
369 //! No address provided (anonymous advertisement).
370 CHRE_BLE_ADDRESS_TYPE_NONE = 0xff,
371 };
372
373 /**
374 * CHRE BLE physical (PHY) channel encoding type, if supported, is based on the
375 * BT Core Spec v5.2, Vol 4, Part E, Section 7.7.65.13, LE Extended Advertising
376 * Report event, entries Primary_PHY and Secondary_PHY.
377 */
378 enum chreBlePhyType {
379 //! No packets on this PHY (only on the secondary channel), or feature not
380 //! supported.
381 CHRE_BLE_PHY_NONE = 0x00,
382
383 //! LE 1 MBPS PHY encoding.
384 CHRE_BLE_PHY_1M = 0x01,
385
386 //! LE 2 MBPS PHY encoding (only on the secondary channel).
387 CHRE_BLE_PHY_2M = 0x02,
388
389 //! LE long-range coded PHY encoding.
390 CHRE_BLE_PHY_CODED = 0x03,
391 };
392
393 /**
394 * The CHRE BLE Advertising Report event is based on the BT Core Spec v5.2,
395 * Vol 4, Part E, Section 7.7.65.13, LE Extended Advertising Report event, with
396 * the following differences:
397 *
398 * 1) A CHRE timestamp field, which can be useful if CHRE is batching results.
399 * 2) Reordering of the rssi and periodicAdvertisingInterval fields for memory
400 * alignment (prevent padding).
401 * 3) Addition of four reserved bytes to reclaim padding.
402 */
403 struct chreBleAdvertisingReport {
404 //! The base timestamp, in nanoseconds, in the same time base as chreGetTime()
405 uint64_t timestamp;
406
407 //! @see CHRE_BLE_EVENT
408 uint8_t eventTypeAndDataStatus;
409
410 //! Advertising address type as defined in enum chreBleAddressType
411 uint8_t addressType;
412
413 //! Advertising device address
414 uint8_t address[CHRE_BLE_ADDRESS_LEN];
415
416 //! Advertiser PHY on primary advertising physical channel, if supported, as
417 //! defined in enum chreBlePhyType.
418 uint8_t primaryPhy;
419
420 //! Advertiser PHY on secondary advertising physical channel, if supported, as
421 //! defined in enum chreBlePhyType.
422 uint8_t secondaryPhy;
423
424 //! Value of the Advertising SID subfield in the ADI field of the PDU among
425 //! the range of [0, 0x0f].
426 //! CHRE_BLE_ADI_NONE indicates no ADI field was provided.
427 //! Other values are reserved.
428 uint8_t advertisingSid;
429
430 //! Transmit (Tx) power in dBm. Typical values are [-127, 20].
431 //! CHRE_BLE_TX_POWER_NONE indicates Tx power not available.
432 int8_t txPower;
433
434 //! Interval of the periodic advertising in 1.25 ms intervals, i.e.
435 //! time = periodicAdvertisingInterval * 1.25 ms
436 //! 0 means no periodic advertising. Minimum value is otherwise 6 (7.5 ms).
437 uint16_t periodicAdvertisingInterval;
438
439 //! RSSI in dBm. Typical values are [-127, 20].
440 //! CHRE_BLE_RSSI_NONE indicates RSSI is not available.
441 int8_t rssi;
442
443 //! Direct address type (i.e. only accept connection requests from a known
444 //! peer device) as defined in enum chreBleAddressType.
445 uint8_t directAddressType;
446
447 //! Direct address (i.e. only accept connection requests from a known peer
448 //! device).
449 uint8_t directAddress[CHRE_BLE_ADDRESS_LEN];
450
451 //! Length of data field. Acceptable range is [0, 31] for legacy and
452 //! [0, 229] for extended advertisements.
453 uint16_t dataLength;
454
455 //! dataLength bytes of data, or null if dataLength is 0
456 const uint8_t *data;
457
458 //! Reserved for future use; set to 0
459 uint32_t reserved;
460 };
461
462 /**
463 * A CHRE BLE Advertising Event can contain any number of CHRE BLE Advertising
464 * Reports (i.e. advertisements).
465 */
466 struct chreBleAdvertisementEvent {
467 //! Reserved for future use; set to 0
468 uint16_t reserved;
469
470 //! Number of advertising reports in this event
471 uint16_t numReports;
472
473 //! Array of length numReports
474 const struct chreBleAdvertisingReport *reports;
475 };
476
477 /**
478 * Retrieves a set of flags indicating the BLE features supported by the
479 * current CHRE implementation. The value returned by this function must be
480 * consistent for the entire duration of the nanoapp's execution.
481 *
482 * The client must allow for more flags to be set in this response than it knows
483 * about, for example if the implementation supports a newer version of the API
484 * than the client was compiled against.
485 *
486 * @return A bitmask with zero or more CHRE_BLE_CAPABILITIES_* flags set. @see
487 * CHRE_BLE_CAPABILITIES
488 *
489 * @since v1.6
490 */
491 uint32_t chreBleGetCapabilities(void);
492
493 /**
494 * Retrieves a set of flags indicating the BLE filtering features supported by
495 * the current CHRE implementation. The value returned by this function must be
496 * consistent for the entire duration of the nanoapp's execution.
497 *
498 * The client must allow for more flags to be set in this response than it knows
499 * about, for example if the implementation supports a newer version of the API
500 * than the client was compiled against.
501 *
502 * @return A bitmask with zero or more CHRE_BLE_FILTER_CAPABILITIES_* flags set.
503 * @see CHRE_BLE_FILTER_CAPABILITIES
504 *
505 * @since v1.6
506 */
507 uint32_t chreBleGetFilterCapabilities(void);
508
509 /**
510 * Helper function to extract event type from eventTypeAndDataStatus as defined
511 * in the BT Core Spec v5.2, Vol 4, Part E, Section 7.7.65.13, LE Extended
512 * Advertising Report event, entry Event_Type.
513 *
514 * @see CHRE_BLE_EVENT
515 *
516 * @param eventTypeAndDataStatus Combined event type and data status
517 *
518 * @return The event type portion of eventTypeAndDataStatus
519 */
chreBleGetEventType(uint8_t eventTypeAndDataStatus)520 static inline uint8_t chreBleGetEventType(uint8_t eventTypeAndDataStatus) {
521 return (eventTypeAndDataStatus & CHRE_BLE_EVENT_MASK_TYPE);
522 }
523
524 /**
525 * Helper function to extract data status from eventTypeAndDataStatus as defined
526 * in the BT Core Spec v5.2, Vol 4, Part E, Section 7.7.65.13, LE Extended
527 * Advertising Report event, entry Event_Type.
528 *
529 * @see CHRE_BLE_EVENT
530 *
531 * @param eventTypeAndDataStatus Combined event type and data status
532 *
533 * @return The data status portion of eventTypeAndDataStatus
534 */
chreBleGetDataStatus(uint8_t eventTypeAndDataStatus)535 static inline uint8_t chreBleGetDataStatus(uint8_t eventTypeAndDataStatus) {
536 return (eventTypeAndDataStatus & CHRE_BLE_EVENT_MASK_DATA_STATUS);
537 }
538
539 /**
540 * Helper function to to combine an event type with a data status to create
541 * eventTypeAndDataStatus as defined in the BT Core Spec v5.2, Vol 4, Part E,
542 * Section 7.7.65.13, LE Extended Advertising Report event, entry Event_Type.
543 *
544 * @see CHRE_BLE_EVENT
545 *
546 * @param eventType Event type
547 * @param dataStatus Data status
548 *
549 * @return A combined eventTypeAndDataStatus
550 */
chreBleGetEventTypeAndDataStatus(uint8_t eventType,uint8_t dataStatus)551 static inline uint8_t chreBleGetEventTypeAndDataStatus(uint8_t eventType,
552 uint8_t dataStatus) {
553 return ((eventType & CHRE_BLE_EVENT_MASK_TYPE) |
554 (dataStatus & CHRE_BLE_EVENT_MASK_DATA_STATUS));
555 }
556
557 /**
558 * Nanoapps must define CHRE_NANOAPP_USES_BLE somewhere in their build
559 * system (e.g. Makefile) if the nanoapp needs to use the following BLE APIs.
560 * In addition to allowing access to these APIs, defining this macro will also
561 * ensure CHRE enforces that all host clients this nanoapp talks to have the
562 * required Android permissions needed to access BLE functionality by adding
563 * metadata to the nanoapp.
564 */
565 #if defined(CHRE_NANOAPP_USES_BLE) || !defined(CHRE_IS_NANOAPP_BUILD)
566
567 /**
568 * Start Bluetooth LE (BLE) scanning on CHRE.
569 *
570 * The result of the operation will be delivered asynchronously via the CHRE
571 * event CHRE_EVENT_BLE_ASYNC_RESULT.
572 *
573 * The scan results will be delivered asynchronously via the CHRE event
574 * CHRE_EVENT_BLE_ADVERTISEMENT.
575 *
576 * If the Bluetooth setting is disabled at the Android level, CHRE is expected
577 * to return a result with CHRE_ERROR_FUNCTION_DISABLED.
578 *
579 * If chreBleStartScanAsync() is called while a previous scan has been started,
580 * the previous scan will be stopped first and replaced with the new scan.
581 *
582 * Note that some corresponding Android parameters are missing from the CHRE
583 * API, where the following default or typical parameters are used:
584 * Callback type: CALLBACK_TYPE_ALL_MATCHES
585 * Result type: SCAN_RESULT_TYPE_FULL
586 * Match mode: MATCH_MODE_AGGRESSIVE
587 * Number of matches per filter: MATCH_NUM_MAX_ADVERTISEMENT
588 * Legacy-only: false
589 * PHY type: PHY_LE_ALL_SUPPORTED
590 *
591 * @param mode Scanning mode selected among enum chreBleScanMode
592 * @param reportDelayMs Maximum requested batching delay in ms. 0 indicates no
593 * batching. Note that the system may deliver results
594 * before the maximum specified delay is reached.
595 * @param filter Pointer to the requested best-effort filter configuration as
596 * defined by struct chreBleScanFilter. The ownership of filter
597 * and its nested elements remains with the caller, and the caller
598 * may release it as soon as chreBleStartScanAsync() returns.
599 *
600 * @return True to indicate that the request was accepted. False otherwise.
601 *
602 * @since v1.6
603 */
604 bool chreBleStartScanAsync(enum chreBleScanMode mode, uint32_t reportDelayMs,
605 const struct chreBleScanFilter *filter);
606 /**
607 * Stops a CHRE BLE scan.
608 *
609 * The result of the operation will be delivered asynchronously via the CHRE
610 * event CHRE_EVENT_BLE_ASYNC_RESULT.
611 *
612 * @return True to indicate that the request was accepted. False otherwise.
613 *
614 * @since v1.6
615 */
616 bool chreBleStopScanAsync(void);
617
618 /**
619 * Requests to immediately deliver batched scan results. The nanoapp must
620 * have an active BLE scan request. If a request is accepted, it will be treated
621 * as though the reportDelayMs has expired for a batched scan. Upon accepting
622 * the request, CHRE works to immediately deliver scan results currently kept in
623 * batching memory, if any, via regular CHRE_EVENT_BLE_ADVERTISEMENT events,
624 * followed by a CHRE_EVENT_BLE_FLUSH_COMPLETE event.
625 *
626 * If the underlying system fails to complete the flush operation within
627 * CHRE_BLE_FLUSH_COMPLETE_TIMEOUT_NS, CHRE will send a
628 * CHRE_EVENT_BLE_FLUSH_COMPLETE event with CHRE_ERROR_TIMEOUT.
629 *
630 * If multiple flush requests are made prior to flush completion, then the
631 * requesting nanoapp will receive all batched samples existing at the time of
632 * the latest flush request. In this case, the number of
633 * CHRE_EVENT_BLE_FLUSH_COMPLETE events received must equal the number of flush
634 * requests made.
635 *
636 * If chreBleStopScanAsync() is called while a flush operation is in progress,
637 * it is unspecified whether the flush operation will complete successfully or
638 * return an error, such as CHRE_ERROR_FUNCTION_DISABLED, but in any case,
639 * CHRE_EVENT_BLE_FLUSH_COMPLETE must still be delivered. The same applies if
640 * the Bluetooth user setting is disabled during a flush operation.
641 *
642 * If called while running on a CHRE API version below v1.7, this function
643 * returns false and has no effect.
644 *
645 * @param cookie An opaque value that will be included in the chreAsyncResult
646 * sent as a response to this request.
647 *
648 * @return True to indicate the request was accepted. False otherwise.
649 *
650 * @since v1.7
651 */
652 bool chreBleFlushAsync(const void *cookie);
653
654 /**
655 * Definitions for handling unsupported CHRE BLE scenarios.
656 */
657 #else // defined(CHRE_NANOAPP_USES_BLE) || !defined(CHRE_IS_NANOAPP_BUILD)
658
659 #define CHRE_BLE_PERM_ERROR_STRING \
660 "CHRE_NANOAPP_USES_BLE must be defined when building this nanoapp in " \
661 "order to refer to "
662
663 #define chreBleStartScanAsync(...) \
664 CHRE_BUILD_ERROR(CHRE_BLE_PERM_ERROR_STRING "chreBleStartScanAsync")
665
666 #define chreBleStopScanAsync(...) \
667 CHRE_BUILD_ERROR(CHRE_BLE_PERM_ERROR_STRING "chreBleStopScanAsync")
668
669 #define chreBleFlushAsync(...) \
670 CHRE_BUILD_ERROR(CHRE_BLE_PERM_ERROR_STRING "chreBleFlushAsync")
671
672 #endif // defined(CHRE_NANOAPP_USES_BLE) || !defined(CHRE_IS_NANOAPP_BUILD)
673
674 #ifdef __cplusplus
675 }
676 #endif
677
678 #endif /* CHRE_BLE_H_ */
679