• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Linux Foundation
3  * Copyright (C) 2012 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef ANDROID_INCLUDE_BLUETOOTH_H
19 #define ANDROID_INCLUDE_BLUETOOTH_H
20 
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <sys/cdefs.h>
24 #include <sys/types.h>
25 
26 #include <vector>
27 
28 #include "avrcp/avrcp.h"
29 #include "types/bluetooth/uuid.h"
30 #include "types/bt_transport.h"
31 #include "types/raw_address.h"
32 
33 /**
34  * The Bluetooth Hardware Module ID
35  */
36 
37 #define BT_HARDWARE_MODULE_ID "bluetooth"
38 #define BT_STACK_MODULE_ID "bluetooth"
39 
40 /** Bluetooth profile interface IDs */
41 #define BT_PROFILE_HANDSFREE_ID "handsfree"
42 #define BT_PROFILE_HANDSFREE_CLIENT_ID "handsfree_client"
43 #define BT_PROFILE_ADVANCED_AUDIO_ID "a2dp"
44 #define BT_PROFILE_ADVANCED_AUDIO_SINK_ID "a2dp_sink"
45 #define BT_PROFILE_SOCKETS_ID "socket"
46 #define BT_PROFILE_HIDHOST_ID "hidhost"
47 #define BT_PROFILE_HIDDEV_ID "hiddev"
48 #define BT_PROFILE_PAN_ID "pan"
49 #define BT_PROFILE_MAP_CLIENT_ID "map_client"
50 #define BT_PROFILE_SDP_CLIENT_ID "sdp"
51 #define BT_PROFILE_GATT_ID "gatt"
52 #define BT_PROFILE_AV_RC_CTRL_ID "avrcp_ctrl"
53 #define BT_PROFILE_HEARING_AID_ID "hearing_aid"
54 #define BT_PROFILE_HAP_CLIENT_ID "has_client"
55 #define BT_PROFILE_LE_AUDIO_ID "le_audio"
56 #define BT_KEYSTORE_ID "bluetooth_keystore"
57 #define BT_PROFILE_VC_ID "volume_control"
58 #define BT_PROFILE_CSIS_CLIENT_ID "csis_client"
59 #define BT_PROFILE_LE_AUDIO_ID "le_audio"
60 #define BT_PROFILE_LE_AUDIO_BROADCASTER_ID "le_audio_broadcaster"
61 #define BT_BQR_ID "bqr"
62 
63 /** Bluetooth Device Name */
64 typedef struct {
65   uint8_t name[249];
66 } __attribute__((packed)) bt_bdname_t;
67 
68 /** Bluetooth Adapter Visibility Modes*/
69 typedef enum {
70   BT_SCAN_MODE_NONE,
71   BT_SCAN_MODE_CONNECTABLE,
72   BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE,
73   BT_SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE
74 } bt_scan_mode_t;
75 
76 /** Bluetooth Adapter State */
77 typedef enum { BT_STATE_OFF, BT_STATE_ON } bt_state_t;
78 
79 /** Bluetooth Adapter Input Output Capabilities which determine Pairing/Security
80  */
81 typedef enum {
82   BT_IO_CAP_OUT,    /* DisplayOnly */
83   BT_IO_CAP_IO,     /* DisplayYesNo */
84   BT_IO_CAP_IN,     /* KeyboardOnly */
85   BT_IO_CAP_NONE,   /* NoInputNoOutput */
86   BT_IO_CAP_KBDISP, /* Keyboard display */
87   BT_IO_CAP_MAX,
88   BT_IO_CAP_UNKNOWN = 0xFF /* Unknown value */
89 } bt_io_cap_t;
90 
91 /** Bluetooth Error Status */
92 /** We need to build on this */
93 
94 typedef enum {
95   BT_STATUS_SUCCESS = 0,
96   BT_STATUS_FAIL,
97   BT_STATUS_NOT_READY,
98   BT_STATUS_NOMEM,
99   BT_STATUS_BUSY, /* retryable error */
100   BT_STATUS_DONE, /* request already completed */
101   BT_STATUS_UNSUPPORTED,
102   BT_STATUS_PARM_INVALID,
103   BT_STATUS_UNHANDLED,
104   BT_STATUS_AUTH_FAILURE,
105   BT_STATUS_RMT_DEV_DOWN,
106   BT_STATUS_AUTH_REJECTED,
107   BT_STATUS_JNI_ENVIRONMENT_ERROR,
108   BT_STATUS_JNI_THREAD_ATTACH_ERROR,
109   BT_STATUS_WAKELOCK_ERROR,
110   BT_STATUS_TIMEOUT,
111   BT_STATUS_DEVICE_NOT_FOUND,
112   BT_STATUS_UNEXPECTED_STATE,
113   BT_STATUS_SOCKET_ERROR
114 } bt_status_t;
115 
bt_status_text(const bt_status_t & status)116 inline std::string bt_status_text(const bt_status_t& status) {
117   switch (status) {
118     case BT_STATUS_SUCCESS:
119       return std::string("success");
120     case BT_STATUS_FAIL:
121       return std::string("fail");
122     case BT_STATUS_NOT_READY:
123       return std::string("not_ready");
124     case BT_STATUS_NOMEM:
125       return std::string("no_memory");
126     case BT_STATUS_BUSY:
127       return std::string("busy");
128     case BT_STATUS_DONE:
129       return std::string("already_done");
130     case BT_STATUS_UNSUPPORTED:
131       return std::string("unsupported");
132     case BT_STATUS_PARM_INVALID:
133       return std::string("parameter_invalid");
134     case BT_STATUS_UNHANDLED:
135       return std::string("unhandled");
136     case BT_STATUS_AUTH_FAILURE:
137       return std::string("auth_failure");
138     case BT_STATUS_RMT_DEV_DOWN:
139       return std::string("remote_device_down");
140     case BT_STATUS_AUTH_REJECTED:
141       return std::string("auth_rejected");
142     case BT_STATUS_JNI_ENVIRONMENT_ERROR:
143       return std::string("jni_env_error");
144     case BT_STATUS_JNI_THREAD_ATTACH_ERROR:
145       return std::string("jni_thread_error");
146     case BT_STATUS_WAKELOCK_ERROR:
147       return std::string("wakelock_error");
148     case BT_STATUS_TIMEOUT:
149       return std::string("timeout_error");
150     case BT_STATUS_DEVICE_NOT_FOUND:
151       return std::string("device_not_found");
152     case BT_STATUS_UNEXPECTED_STATE:
153       return std::string("unexpected_state");
154     case BT_STATUS_SOCKET_ERROR:
155       return std::string("socket_error");
156     default:
157       return std::string("UNKNOWN");
158   }
159 }
160 
161 /** Bluetooth HCI Error Codes */
162 /** Corresponding to [Vol 2] Part D, "Error Codes" of Core_v5.1 specs */
163 typedef uint8_t bt_hci_error_code_t;
164 
165 /** Bluetooth PinKey Code */
166 typedef struct {
167   uint8_t pin[16];
168 } __attribute__((packed)) bt_pin_code_t;
169 
170 typedef struct {
171   uint8_t status;
172   uint32_t ctrl_state;  /* stack reported state */
173   uint64_t tx_time;     /* in ms */
174   uint64_t rx_time;     /* in ms */
175   uint64_t idle_time;   /* in ms */
176   uint64_t energy_used; /* a product of mA, V and ms */
177 } __attribute__((packed)) bt_activity_energy_info;
178 
179 typedef struct {
180   int32_t app_uid;
181   uint64_t tx_bytes;
182   uint64_t rx_bytes;
183 } __attribute__((packed)) bt_uid_traffic_t;
184 
185 /** Bluetooth Adapter Discovery state */
186 typedef enum { BT_DISCOVERY_STOPPED, BT_DISCOVERY_STARTED } bt_discovery_state_t;
187 
188 /** Bluetooth ACL connection state */
189 typedef enum { BT_ACL_STATE_CONNECTED, BT_ACL_STATE_DISCONNECTED } bt_acl_state_t;
190 
191 /** Bluetooth ACL connection direction */
192 typedef enum {
193   BT_CONN_DIRECTION_UNKNOWN,
194   BT_CONN_DIRECTION_OUTGOING,
195   BT_CONN_DIRECTION_INCOMING
196 } bt_conn_direction_t;
197 
198 constexpr uint16_t INVALID_ACL_HANDLE = 0xFFFF;
199 
200 /** Bluetooth SDP service record */
201 typedef struct {
202   bluetooth::Uuid uuid;
203   uint16_t channel;
204   char name[256];  // what's the maximum length
205 } bt_service_record_t;
206 
207 /** Bluetooth Remote Version info */
208 typedef struct {
209   int version;
210   int sub_ver;
211   int manufacturer;
212 } bt_remote_version_t;
213 
214 typedef struct {
215   uint16_t version_supported;
216   uint8_t local_privacy_enabled;
217   uint8_t max_adv_instance;
218   uint8_t rpa_offload_supported;
219   uint8_t max_irk_list_size;
220   uint8_t max_adv_filter_supported;
221   uint8_t activity_energy_info_supported;
222   uint16_t scan_result_storage_size;
223   uint16_t total_trackable_advertisers;
224   bool extended_scan_support;
225   bool debug_logging_supported;
226   bool le_2m_phy_supported;
227   bool le_coded_phy_supported;
228   bool le_extended_advertising_supported;
229   bool le_periodic_advertising_supported;
230   uint16_t le_maximum_advertising_data_length;
231   uint32_t dynamic_audio_buffer_supported;
232   bool le_periodic_advertising_sync_transfer_sender_supported;
233   bool le_connected_isochronous_stream_central_supported;
234   bool le_isochronous_broadcast_supported;
235   bool le_periodic_advertising_sync_transfer_recipient_supported;
236   uint16_t adv_filter_extended_features_mask;
237   bool le_channel_sounding_supported;
238 } bt_local_le_features_t;
239 
240 typedef struct {
241   uint8_t number_of_supported_offloaded_le_coc_sockets;
242   uint8_t number_of_supported_offloaded_rfcomm_sockets;
243 } bt_lpp_offload_features_t;
244 
245 /** Bluetooth Vendor and Product ID info */
246 typedef struct {
247   uint8_t vendor_id_src;
248   uint16_t vendor_id;
249   uint16_t product_id;
250   uint16_t version;
251 } bt_vendor_product_info_t;
252 
253 /* Stored the default/maximum/minimum buffer time for dynamic audio buffer.
254  * For A2DP offload usage, the unit is millisecond.
255  * For A2DP legacy usage, the unit is buffer queue size*/
256 typedef struct {
257   uint16_t default_buffer_time;
258   uint16_t maximum_buffer_time;
259   uint16_t minimum_buffer_time;
260 } bt_dynamic_audio_buffer_type_t;
261 
262 typedef struct {
263   bt_dynamic_audio_buffer_type_t dab_item[32];
264 } bt_dynamic_audio_buffer_item_t;
265 
266 /* Bluetooth Adapter and Remote Device property types */
267 typedef enum {
268   /* Properties common to both adapter and remote device */
269   /**
270    * Description - Bluetooth Device Name
271    * Access mode - Adapter name can be GET/SET. Remote device can be GET
272    * Data type   - bt_bdname_t
273    */
274   BT_PROPERTY_BDNAME = 0x1,
275   /**
276    * Description - Bluetooth Device Address
277    * Access mode - Only GET.
278    * Data type   - RawAddress
279    */
280   BT_PROPERTY_BDADDR,
281   /**
282    * Description - Bluetooth Service 128-bit UUIDs
283    * Access mode - Only GET.
284    * Data type   - Array of bluetooth::Uuid (Array size inferred from property
285    *               length).
286    */
287   BT_PROPERTY_UUIDS,
288   /**
289    * Description - Bluetooth Class of Device as found in Assigned Numbers
290    * Access mode - Only GET.
291    * Data type   - uint32_t.
292    */
293   BT_PROPERTY_CLASS_OF_DEVICE,
294   /**
295    * Description - Device Type - BREDR, BLE or DUAL Mode
296    * Access mode - Only GET.
297    * Data type   - bt_device_type_t
298    */
299   BT_PROPERTY_TYPE_OF_DEVICE,
300   /**
301    * Description - Bluetooth Service Record, UUIDs on BREDR transport
302    * Access mode - Only GET.
303    * Data type   - bt_service_record_t
304    */
305   BT_PROPERTY_SERVICE_RECORD,
306 
307   /* Properties unique to adapter */
308   BT_PROPERTY_RESERVED_07,
309   /**
310    * Description - List of bonded devices
311    * Access mode - Only GET.
312    * Data type   - Array of RawAddress of the bonded remote devices
313    *               (Array size inferred from property length).
314    */
315   BT_PROPERTY_ADAPTER_BONDED_DEVICES,
316   /**
317    * Description - Bluetooth Adapter Discoverable timeout (in seconds)
318    * Access mode - GET and SET
319    * Data type   - uint32_t
320    */
321   BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT,
322 
323   /* Properties unique to remote device */
324   /**
325    * Description - User defined friendly name of the remote device
326    * Access mode - GET and SET
327    * Data type   - bt_bdname_t.
328    */
329   BT_PROPERTY_REMOTE_FRIENDLY_NAME,
330   /**
331    * Description - RSSI value of the inquired remote device
332    * Access mode - Only GET.
333    * Data type   - int8_t.
334    */
335   BT_PROPERTY_REMOTE_RSSI,
336   /**
337    * Description - Remote version info
338    * Access mode - SET/GET.
339    * Data type   - bt_remote_version_t.
340    */
341 
342   BT_PROPERTY_REMOTE_VERSION_INFO,
343 
344   /**
345    * Description - Local LE features
346    * Access mode - GET.
347    * Data type   - bt_local_le_features_t.
348    */
349   BT_PROPERTY_LOCAL_LE_FEATURES,
350 
351   BT_PROPERTY_RESERVED_0E,
352 
353   BT_PROPERTY_RESERVED_0F,
354 
355   BT_PROPERTY_DYNAMIC_AUDIO_BUFFER,
356 
357   /**
358    * Description - True if Remote is a Member of a Coordinated Set.
359    * Access mode - GET.
360    * Data Type - bool.
361    */
362   BT_PROPERTY_REMOTE_IS_COORDINATED_SET_MEMBER,
363 
364   /**
365    * Description - Appearance as specified in Assigned Numbers.
366    * Access mode - GET.
367    * Data Type - uint16_t.
368    */
369   BT_PROPERTY_APPEARANCE,
370 
371   /**
372    * Description - Peer devices' vendor and product ID.
373    * Access mode - GET.
374    * Data Type - bt_vendor_product_info_t.
375    */
376   BT_PROPERTY_VENDOR_PRODUCT_INFO,
377 
378   BT_PROPERTY_RESERVED_0x14,
379 
380   /**
381    * Description - ASHA capability.
382    * Access mode - GET.
383    * Data Type - int16_t.
384    */
385   BT_PROPERTY_REMOTE_ASHA_CAPABILITY,
386 
387   /**
388    * Description - ASHA truncated HiSyncID.
389    * Access mode - GET.
390    * Data Type - uint32_t.
391    */
392   BT_PROPERTY_REMOTE_ASHA_TRUNCATED_HISYNCID,
393 
394   /**
395    * Description - Model name read from Device Information Service(DIS).
396    * Access mode - GET and SET.
397    * Data Type - char array.
398    */
399   BT_PROPERTY_REMOTE_MODEL_NUM,
400 
401   /**
402    * Description - Address type of the remote device - PUBLIC or REMOTE
403    * Access mode - GET.
404    * Data Type - uint8_t.
405    */
406   BT_PROPERTY_REMOTE_ADDR_TYPE,
407 
408   /**
409    * Description - Whether remote device supports Secure Connections mode
410    * Access mode - GET and SET.
411    * Data Type - uint8_t.
412    */
413   BT_PROPERTY_REMOTE_SECURE_CONNECTIONS_SUPPORTED,
414 
415   /**
416    * Description - Maximum observed session key for remote device
417    * Access mode - GET and SET.
418    * Data Type - uint8_t.
419    */
420   BT_PROPERTY_REMOTE_MAX_SESSION_KEY_SIZE,
421 
422   /**
423    * Description - Low power processor offload features
424    * Access mode - GET.
425    * Data Type   - bt_lpp_offload_features_t.
426    */
427   BT_PROPERTY_LPP_OFFLOAD_FEATURES,
428 
429   /**
430    * Description - Bluetooth Service 128-bit UUIDs on LE transport
431    * Access mode - Only GET.
432    * Data type   - Array of bluetooth::Uuid (Array size inferred from property
433    *               length).
434    */
435   BT_PROPERTY_UUIDS_LE,
436 
437   BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP = 0xFF,
438 } bt_property_type_t;
439 
440 /** Bluetooth Adapter Property data structure */
441 typedef struct {
442   bt_property_type_t type;
443   int len;
444   void* val;
445 } bt_property_t;
446 
447 // OOB_ADDRESS_SIZE is 6 bytes address + 1 byte address type
448 #define OOB_ADDRESS_SIZE 7
449 #define OOB_C_SIZE 16
450 #define OOB_R_SIZE 16
451 #define OOB_NAME_MAX_SIZE 256
452 // Classic
453 #define OOB_DATA_LEN_SIZE 2
454 #define OOB_COD_SIZE 3
455 // LE
456 #define OOB_TK_SIZE 16
457 #define OOB_LE_FLAG_SIZE 1
458 #define OOB_LE_ROLE_SIZE 1
459 #define OOB_LE_APPEARANCE_SIZE 2
460 /** Represents the actual Out of Band data itself */
461 typedef struct bt_oob_data_s {
462   // Both
463   bool is_valid = false; /* Default to invalid data; force caller to verify */
464   uint8_t address[OOB_ADDRESS_SIZE];
465   uint8_t c[OOB_C_SIZE];                  /* Simple Pairing Hash C-192/256 (Classic or LE) */
466   uint8_t r[OOB_R_SIZE];                  /* Simple Pairing Randomizer R-192/256 (Classic or LE) */
467   uint8_t device_name[OOB_NAME_MAX_SIZE]; /* Name of the device */
468 
469   // Classic
470   uint8_t oob_data_length[OOB_DATA_LEN_SIZE]; /* Classic only data Length. Value includes this
471                                                  in length */
472   uint8_t class_of_device[OOB_COD_SIZE];      /* Class of Device (Classic or LE) */
473 
474   // LE
475   uint8_t le_device_role;                        /* Supported and preferred role of device */
476   uint8_t sm_tk[OOB_TK_SIZE];                    /* Security Manager TK Value (LE Only) */
477   uint8_t le_flags;                              /* LE Flags for discoverability and features */
478   uint8_t le_appearance[OOB_LE_APPEARANCE_SIZE]; /* For the appearance of the device */
479 } bt_oob_data_t;
480 
481 /** Bluetooth Device Type */
482 typedef enum {
483   BT_DEVICE_DEVTYPE_BREDR = 0x1,
484   BT_DEVICE_DEVTYPE_BLE,
485   BT_DEVICE_DEVTYPE_DUAL
486 } bt_device_type_t;
487 
488 /** Bluetooth Bond state */
489 typedef enum { BT_BOND_STATE_NONE, BT_BOND_STATE_BONDING, BT_BOND_STATE_BONDED } bt_bond_state_t;
490 
491 /** Bluetooth SSP Bonding Variant */
492 typedef enum {
493   BT_SSP_VARIANT_PASSKEY_CONFIRMATION,
494   BT_SSP_VARIANT_PASSKEY_ENTRY,
495   BT_SSP_VARIANT_CONSENT,
496   BT_SSP_VARIANT_PASSKEY_NOTIFICATION
497 } bt_ssp_variant_t;
498 
499 typedef struct {
500   RawAddress bd_addr;
501   uint8_t status; /* bt_hci_error_code_t */
502   bool encr_enable;
503   uint8_t key_size;
504   tBT_TRANSPORT transport;
505   bool secure_connections;
506 } bt_encryption_change_evt;
507 
508 #define BT_MAX_NUM_UUIDS 32
509 
510 /** Bluetooth Interface callbacks */
511 
512 /** Bluetooth Enable/Disable Callback. */
513 typedef void (*adapter_state_changed_callback)(bt_state_t state);
514 
515 /** GET/SET Adapter Properties callback */
516 /* TODO: For the GET/SET property APIs/callbacks, we may need a session
517  * identifier to associate the call with the callback. This would be needed
518  * whenever more than one simultaneous instance of the same adapter_type
519  * is get/set.
520  *
521  * If this is going to be handled in the Java framework, then we do not need
522  * to manage sessions here.
523  */
524 typedef void (*adapter_properties_callback)(bt_status_t status, int num_properties,
525                                             bt_property_t* properties);
526 
527 /** GET/SET Remote Device Properties callback */
528 /** TODO: For remote device properties, do not see a need to get/set
529  * multiple properties - num_properties shall be 1
530  */
531 typedef void (*remote_device_properties_callback)(bt_status_t status, RawAddress* bd_addr,
532                                                   int num_properties, bt_property_t* properties);
533 
534 /** New device discovered callback */
535 /** If EIR data is not present, then BD_NAME and RSSI shall be NULL and -1
536  * respectively */
537 typedef void (*device_found_callback)(int num_properties, bt_property_t* properties);
538 
539 /** Discovery state changed callback */
540 typedef void (*discovery_state_changed_callback)(bt_discovery_state_t state);
541 
542 /** Bluetooth Legacy PinKey Request callback */
543 typedef void (*pin_request_callback)(RawAddress* remote_bd_addr, bt_bdname_t* bd_name, uint32_t cod,
544                                      bool min_16_digit);
545 
546 /** Bluetooth SSP Request callback - Just Works & Numeric Comparison*/
547 /** pass_key - Shall be 0 for BT_SSP_PAIRING_VARIANT_CONSENT &
548  *  BT_SSP_PAIRING_PASSKEY_ENTRY */
549 /* TODO: Passkey request callback shall not be needed for devices with display
550  * capability. We still need support this in the stack for completeness */
551 typedef void (*ssp_request_callback)(RawAddress* remote_bd_addr, bt_ssp_variant_t pairing_variant,
552                                      uint32_t pass_key);
553 
554 /** Bluetooth Bond state changed callback */
555 /* Invoked in response to create_bond, cancel_bond or remove_bond */
556 typedef void (*bond_state_changed_callback)(bt_status_t status, RawAddress* remote_bd_addr,
557                                             bt_bond_state_t state, int fail_reason);
558 
559 /** Bluetooth Address consolidate callback */
560 /* Callback to inform upper layer that these two addresses come from same
561  * bluetooth device (DUAL mode) */
562 typedef void (*address_consolidate_callback)(RawAddress* main_bd_addr,
563                                              RawAddress* secondary_bd_addr);
564 
565 /** Bluetooth LE Address association callback */
566 /* Callback for the upper layer to associate the LE-only device's RPA to the
567  * identity address and identity address type */
568 typedef void (*le_address_associate_callback)(RawAddress* main_bd_addr,
569                                               RawAddress* secondary_bd_addr,
570                                               uint8_t identity_address_type);
571 
572 /** Bluetooth ACL connection state changed callback */
573 typedef void (*acl_state_changed_callback)(bt_status_t status, RawAddress* remote_bd_addr,
574                                            bt_acl_state_t state, int transport_link_type,
575                                            bt_hci_error_code_t hci_reason,
576                                            bt_conn_direction_t direction, uint16_t acl_handle);
577 
578 /** Bluetooth link quality report callback */
579 typedef void (*link_quality_report_callback)(uint64_t timestamp, int report_id, int rssi, int snr,
580                                              int retransmission_count,
581                                              int packets_not_receive_count,
582                                              int negative_acknowledgement_count);
583 
584 /** Switch the buffer size callback */
585 typedef void (*switch_buffer_size_callback)(bool is_low_latency_buffer_size);
586 
587 /** Switch the codec callback */
588 typedef void (*switch_codec_callback)(bool is_low_latency_buffer_size);
589 
590 typedef void (*le_rand_callback)(uint64_t random);
591 
592 typedef enum { ASSOCIATE_JVM, DISASSOCIATE_JVM } bt_cb_thread_evt;
593 
594 /** Thread Associate/Disassociate JVM Callback */
595 /* Callback that is invoked by the callback thread to allow upper layer to
596  * attach/detach to/from the JVM */
597 typedef void (*callback_thread_event)(bt_cb_thread_evt evt);
598 
599 /** Bluetooth Test Mode Callback */
600 /* Receive any HCI event from controller. Must be in DUT Mode for this callback
601  * to be received */
602 typedef void (*dut_mode_recv_callback)(uint16_t opcode, uint8_t* buf, uint8_t len);
603 
604 /* LE Test mode callbacks
605  * This callback shall be invoked whenever the le_tx_test, le_rx_test or
606  * le_test_end is invoked The num_packets is valid only for le_test_end command
607  */
608 typedef void (*le_test_mode_callback)(bt_status_t status, uint16_t num_packets);
609 
610 /** Callback invoked when energy details are obtained */
611 /* Ctrl_state-Current controller state-Active-1,scan-2,or idle-3 state as
612  * defined by HCI spec. If the ctrl_state value is 0, it means the API call
613  * failed Time values-In milliseconds as returned by the controller Energy
614  * used-Value as returned by the controller Status-Provides the status of the
615  * read_energy_info API call uid_data provides an array of bt_uid_traffic_t,
616  * where the array is terminated by an element with app_uid set to -1.
617  */
618 typedef void (*energy_info_callback)(bt_activity_energy_info* energy_info,
619                                      bt_uid_traffic_t* uid_data);
620 
621 /** Callback invoked when OOB data is returned from the controller */
622 typedef void (*generate_local_oob_data_callback)(tBT_TRANSPORT transport, bt_oob_data_t oob_data);
623 
624 typedef void (*key_missing_callback)(const RawAddress bd_addr);
625 
626 typedef void (*encryption_change_callback)(const bt_encryption_change_evt encryption_change);
627 
628 /** TODO: Add callbacks for Link Up/Down and other generic
629  *  notifications/callbacks */
630 
631 /** Bluetooth DM callback structure. */
632 typedef struct {
633   /** set to sizeof(bt_callbacks_t) */
634   size_t size;
635   adapter_state_changed_callback adapter_state_changed_cb;
636   adapter_properties_callback adapter_properties_cb;
637   remote_device_properties_callback remote_device_properties_cb;
638   device_found_callback device_found_cb;
639   discovery_state_changed_callback discovery_state_changed_cb;
640   pin_request_callback pin_request_cb;
641   ssp_request_callback ssp_request_cb;
642   bond_state_changed_callback bond_state_changed_cb;
643   address_consolidate_callback address_consolidate_cb;
644   le_address_associate_callback le_address_associate_cb;
645   acl_state_changed_callback acl_state_changed_cb;
646   callback_thread_event thread_evt_cb;
647   dut_mode_recv_callback dut_mode_recv_cb;
648   le_test_mode_callback le_test_mode_cb;
649   energy_info_callback energy_info_cb;
650   link_quality_report_callback link_quality_report_cb;
651   generate_local_oob_data_callback generate_local_oob_data_cb;
652   switch_buffer_size_callback switch_buffer_size_cb;
653   switch_codec_callback switch_codec_cb;
654   le_rand_callback le_rand_cb;
655   key_missing_callback key_missing_cb;
656   encryption_change_callback encryption_change_cb;
657 } bt_callbacks_t;
658 
659 typedef int (*acquire_wake_lock_callout)(const char* lock_name);
660 typedef int (*release_wake_lock_callout)(const char* lock_name);
661 
662 /** The set of functions required by bluedroid to set wake alarms and
663  * grab wake locks. This struct is passed into the stack through the
664  * |set_os_callouts| function on |bt_interface_t|.
665  */
666 typedef struct {
667   /* set to sizeof(bt_os_callouts_t) */
668   size_t size;
669 
670   acquire_wake_lock_callout acquire_wake_lock;
671   release_wake_lock_callout release_wake_lock;
672 } bt_os_callouts_t;
673 
674 /** NOTE: By default, no profiles are initialized at the time of init/enable.
675  *  Whenever the application invokes the 'init' API of a profile, then one of
676  *  the following shall occur:
677  *
678  *    1.) If Bluetooth is not enabled, then the Bluetooth core shall mark the
679  *        profile as enabled. Subsequently, when the application invokes the
680  *        Bluetooth 'enable', as part of the enable sequence the profile that
681  * were marked shall be enabled by calling appropriate stack APIs. The
682  *        'adapter_properties_cb' shall return the list of UUIDs of the
683  *        enabled profiles.
684  *
685  *    2.) If Bluetooth is enabled, then the Bluetooth core shall invoke the
686  * stack profile API to initialize the profile and trigger a
687  *        'adapter_properties_cb' with the current list of UUIDs including the
688  *        newly added profile's UUID.
689  *
690  *   The reverse shall occur whenever the profile 'cleanup' APIs are invoked
691  */
692 
693 /** Represents the standard Bluetooth DM interface. */
694 typedef struct {
695   /** set to sizeof(bt_interface_t) */
696   size_t size;
697 #ifdef TARGET_FLOSS
698   /** set index of the adapter to use */
699   void (*set_adapter_index)(int adapter_index);
700 #endif
701 
702   /**
703    * Opens the interface and provides the callback routines
704    * to the implementation of this interface.
705    * The |start_restricted| flag inits the adapter in restricted mode. In
706    * restricted mode, bonds that are created are marked as restricted in the
707    * config file. These devices are deleted upon leaving restricted mode.
708    * The |is_common_criteria_mode| flag inits the adapter in common criteria
709    * mode. The |config_compare_result| flag show the config checksum check
710    * result if is in common criteria mode. The |is_atv| flag indicates whether
711    * the local device is an Android TV
712    */
713   int (*init)(bt_callbacks_t* callbacks, bool guest_mode, bool is_common_criteria_mode,
714               int config_compare_result, bool is_atv);
715 
716   /** Enable Bluetooth. */
717   int (*enable)();
718 
719   /** Disable Bluetooth. */
720   int (*disable)(void);
721 
722   /** Closes the interface. */
723   void (*cleanup)(void);
724 
725   /** Start Rust Module */
726   void (*start_rust_module)(void);
727 
728   /** Stop Rust Module */
729   void (*stop_rust_module)(void);
730 
731   /** Get all Bluetooth Adapter properties at init */
732   int (*get_adapter_properties)(void);
733 
734   /** Get Bluetooth Adapter property of 'type' */
735   int (*get_adapter_property)(bt_property_type_t type);
736 
737   void (*set_scan_mode)(bt_scan_mode_t mode);
738 
739   /** Set Bluetooth Adapter property of 'type' */
740   /* Based on the type, val shall be one of
741    * RawAddress or bt_bdname_t etc
742    */
743   int (*set_adapter_property)(const bt_property_t* property);
744 
745   /** Get all Remote Device properties */
746   int (*get_remote_device_properties)(RawAddress* remote_addr);
747 
748   /** Get Remote Device property of 'type' */
749   int (*get_remote_device_property)(RawAddress* remote_addr, bt_property_type_t type);
750 
751   /** Set Remote Device property of 'type' */
752   int (*set_remote_device_property)(RawAddress* remote_addr, const bt_property_t* property);
753 
754   /** Get Remote Device's service record  for the given UUID */
755   int (*get_remote_service_record)(const RawAddress& remote_addr, const bluetooth::Uuid& uuid);
756 
757   /** Start service discovery with transport to get remote services */
758   int (*get_remote_services)(RawAddress* remote_addr, int transport);
759 
760   /** Start Discovery */
761   int (*start_discovery)(void);
762 
763   /** Cancel Discovery */
764   int (*cancel_discovery)(void);
765 
766   /** Create Bluetooth Bonding */
767   int (*create_bond)(const RawAddress* bd_addr, int transport);
768 
769   /** Create Bluetooth Bonding over le transport */
770   int (*create_bond_le)(const RawAddress* bd_addr, uint8_t addr_type);
771 
772   /** Create Bluetooth Bond using out of band data */
773   int (*create_bond_out_of_band)(const RawAddress* bd_addr, int transport,
774                                  const bt_oob_data_t* p192_data, const bt_oob_data_t* p256_data);
775 
776   /** Remove Bond */
777   int (*remove_bond)(const RawAddress* bd_addr);
778 
779   /** Cancel Bond */
780   int (*cancel_bond)(const RawAddress* bd_addr);
781 
782   bool (*pairing_is_busy)();
783 
784   /**
785    * Get the connection status for a given remote device.
786    * return value of 0 means the device is not connected,
787    * non-zero return status indicates an active connection.
788    */
789   int (*get_connection_state)(const RawAddress* bd_addr);
790 
791   /** BT Legacy PinKey Reply */
792   /** If accept==FALSE, then pin_len and pin_code shall be 0x0 */
793   int (*pin_reply)(const RawAddress* bd_addr, uint8_t accept, uint8_t pin_len,
794                    bt_pin_code_t* pin_code);
795 
796   /** BT SSP Reply - Just Works, Numeric Comparison and Passkey
797    * passkey shall be zero for BT_SSP_VARIANT_PASSKEY_COMPARISON &
798    * BT_SSP_VARIANT_CONSENT
799    * For BT_SSP_VARIANT_PASSKEY_ENTRY, if accept==FALSE, then passkey
800    * shall be zero */
801   int (*ssp_reply)(const RawAddress* bd_addr, bt_ssp_variant_t variant, uint8_t accept,
802                    uint32_t passkey);
803 
804   /** Get Bluetooth profile interface */
805   const void* (*get_profile_interface)(const char* profile_id);
806 
807   /** Bluetooth Test Mode APIs - Bluetooth must be enabled for these APIs */
808   /* Configure DUT Mode - Use this mode to enter/exit DUT mode */
809   int (*dut_mode_configure)(uint8_t enable);
810 
811   /* Send any test HCI (vendor-specific) command to the controller. Must be in
812    * DUT Mode */
813   int (*dut_mode_send)(uint16_t opcode, uint8_t* buf, uint8_t len);
814   /** BLE Test Mode APIs */
815   /* opcode MUST be one of: LE_Receiver_Test, LE_Transmitter_Test, LE_Test_End
816    */
817   int (*le_test_mode)(uint16_t opcode, uint8_t* buf, uint8_t len);
818 
819   /** Sets the OS call-out functions that bluedroid needs for alarms and wake
820    * locks. This should be called immediately after a successful |init|.
821    */
822   int (*set_os_callouts)(bt_os_callouts_t* callouts);
823 
824   /** Read Energy info details - return value indicates BT_STATUS_SUCCESS or
825    * BT_STATUS_NOT_READY Success indicates that the VSC command was sent to
826    * controller
827    */
828   int (*read_energy_info)();
829 
830   /**
831    * Native support for dumpsys function
832    * Function is synchronous and |fd| is owned by caller.
833    * |arguments| are arguments which may affect the output, encoded as
834    * UTF-8 strings.
835    */
836   void (*dump)(int fd, const char** arguments);
837 
838   /**
839    * Clear /data/misc/bt_config.conf and erase all stored connections
840    */
841   int (*config_clear)(void);
842 
843   /**
844    * Clear (reset) the dynamic portion of the device interoperability database.
845    */
846   void (*interop_database_clear)(void);
847 
848   /**
849    * Add a new device interoperability workaround for a remote device whose
850    * first |len| bytes of the its device address match |addr|.
851    * NOTE: |feature| has to match an item defined in interop_feature_t
852    * (interop.h).
853    */
854   void (*interop_database_add)(uint16_t feature, const RawAddress* addr, size_t len);
855 
856   /**
857    * Get the AvrcpTarget Service interface to interact with the Avrcp Service
858    */
859   bluetooth::avrcp::ServiceInterface* (*get_avrcp_service)(void);
860 
861   /**
862    * Obfuscate Bluetooth MAC address into a PII free ID string
863    *
864    * @param address Bluetooth MAC address to be obfuscated
865    * @return a string of uint8_t that is unique to this MAC address
866    */
867   std::string (*obfuscate_address)(const RawAddress& address);
868 
869   /**
870    * Get an incremental id for as primary key for Bluetooth metric and log
871    *
872    * @param address Bluetooth MAC address of Bluetooth device
873    * @return int incremental Bluetooth id
874    */
875   int (*get_metric_id)(const RawAddress& address);
876 
877   /**
878    * Set the dynamic audio buffer size to the Controller
879    */
880   int (*set_dynamic_audio_buffer_size)(int codec, int size);
881 
882   /**
883    * Fetches the local Out of Band data.
884    */
885   int (*generate_local_oob_data)(tBT_TRANSPORT transport);
886 
887   /**
888    * Allow or disallow audio low latency
889    *
890    * @param allowed true if allowing audio low latency
891    * @param address Bluetooth MAC address of Bluetooth device
892    * @return true if audio low latency is successfully allowed or disallowed
893    */
894   bool (*allow_low_latency_audio)(bool allowed, const RawAddress& address);
895 
896   /**
897    * Set the event filter for the controller
898    */
899   int (*clear_event_filter)();
900 
901   /**
902    * Call to clear event mask
903    */
904   int (*clear_event_mask)();
905 
906   /**
907    * Call to clear out the filter accept list
908    */
909   int (*clear_filter_accept_list)();
910 
911   /**
912    * Call to disconnect all ACL connections
913    */
914   int (*disconnect_all_acls)();
915 
916   /**
917    * Call to disconnect ACL connection to device
918    */
919   int (*disconnect_acl)(const RawAddress& bd_addr, int transport);
920 
921   /**
922    * Call to retrieve a generated random
923    */
924   int (*le_rand)();
925 
926   /**
927    *
928    * Floss: Set the event filter to inquiry result device all
929    *
930    */
931   int (*set_event_filter_inquiry_result_all_devices)();
932 
933   /**
934    *
935    * Floss: Set the default event mask for Classic and LE except the given
936    *        values (they will be disabled in the final set mask).
937    *
938    */
939   int (*set_default_event_mask_except)(uint64_t mask, uint64_t le_mask);
940 
941   /**
942    *
943    * Floss: Restore the state of the for the filter accept list
944    *
945    */
946   int (*restore_filter_accept_list)();
947 
948   /**
949    *
950    * Allow the device to be woken by HID devices
951    *
952    */
953   int (*allow_wake_by_hid)();
954 
955   /**
956    *
957    * Tell the controller to allow all devices
958    *
959    */
960   int (*set_event_filter_connection_setup_all_devices)();
961 
962   /**
963    *
964    * Is wbs supported by the controller
965    *
966    */
967   bool (*get_wbs_supported)();
968 
969   /**
970    *
971    * Is swb supported by the controller
972    *
973    */
974   bool (*get_swb_supported)();
975 
976   /**
977    *
978    * Is the specified coding format supported by the adapter
979    *
980    */
981   bool (*is_coding_format_supported)(uint8_t coding_format);
982 
983   /**
984    * Data passed from BluetoothDevice.metadata_changed
985    *
986    * @param remote_bd_addr remote address
987    * @param key Metadata key
988    * @param value Metadata value
989    */
990   void (*metadata_changed)(const RawAddress& remote_bd_addr, int key, std::vector<uint8_t> value);
991 
992   /** interop match address */
993   bool (*interop_match_addr)(const char* feature_name, const RawAddress* addr);
994 
995   /** interop match name */
996   bool (*interop_match_name)(const char* feature_name, const char* name);
997 
998   /** interop match address or name */
999   bool (*interop_match_addr_or_name)(const char* feature_name, const RawAddress* addr);
1000 
1001   /** add or remove address entry to interop database */
1002   void (*interop_database_add_remove_addr)(bool do_add, const char* feature_name,
1003                                            const RawAddress* addr, int length);
1004 
1005   /** add or remove name entry to interop database */
1006   void (*interop_database_add_remove_name)(bool do_add, const char* feature_name, const char* name);
1007 
1008   /** get remote Pbap PCE  version*/
1009   int (*get_remote_pbap_pce_version)(const RawAddress* bd_addr);
1010 
1011   /** check if pbap pse dynamic version upgrade is enable */
1012   bool (*pbap_pse_dynamic_version_upgrade_is_enabled)();
1013 } bt_interface_t;
1014 
1015 #define BLUETOOTH_INTERFACE_STRING "bluetoothInterface"
1016 
1017 #if __has_include(<bluetooth/log.h>)
1018 #include <bluetooth/log.h>
1019 
1020 namespace std {
1021 template <>
1022 struct formatter<bt_status_t> : enum_formatter<bt_status_t> {};
1023 template <>
1024 struct formatter<bt_scan_mode_t> : enum_formatter<bt_scan_mode_t> {};
1025 template <>
1026 struct formatter<bt_bond_state_t> : enum_formatter<bt_bond_state_t> {};
1027 template <>
1028 struct formatter<bt_property_type_t> : enum_formatter<bt_property_type_t> {};
1029 template <>
1030 struct formatter<bt_ssp_variant_t> : enum_formatter<bt_ssp_variant_t> {};
1031 }  // namespace std
1032 
1033 #endif  // __has_include(<bluetooth/log.h>)
1034 
1035 #endif /* ANDROID_INCLUDE_BLUETOOTH_H */
1036