bluetooth.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /* SPDX-License-Identifier: Apache-2.0 */
  2. /*
  3. * Copyright (C) 2012 The Android Open Source Project
  4. *
  5. */
  6. #ifndef ANDROID_INCLUDE_BLUETOOTH_H
  7. #define ANDROID_INCLUDE_BLUETOOTH_H
  8. #include <stdbool.h>
  9. #include <stdint.h>
  10. #include <sys/cdefs.h>
  11. #include <sys/types.h>
  12. #include <hardware/hardware.h>
  13. __BEGIN_DECLS
  14. /**
  15. * The Bluetooth Hardware Module ID
  16. */
  17. #define BT_HARDWARE_MODULE_ID "bluetooth"
  18. #define BT_STACK_MODULE_ID "bluetooth"
  19. #define BT_STACK_TEST_MODULE_ID "bluetooth_test"
  20. /* Bluetooth profile interface IDs */
  21. #define BT_PROFILE_HANDSFREE_ID "handsfree"
  22. #define BT_PROFILE_HANDSFREE_CLIENT_ID "handsfree_client"
  23. #define BT_PROFILE_ADVANCED_AUDIO_ID "a2dp"
  24. #define BT_PROFILE_ADVANCED_AUDIO_SINK_ID "a2dp_sink"
  25. #define BT_PROFILE_HEALTH_ID "health"
  26. #define BT_PROFILE_SOCKETS_ID "socket"
  27. #define BT_PROFILE_HIDHOST_ID "hidhost"
  28. #define BT_PROFILE_PAN_ID "pan"
  29. #define BT_PROFILE_MAP_CLIENT_ID "map_client"
  30. #define BT_PROFILE_GATT_ID "gatt"
  31. #define BT_PROFILE_AV_RC_ID "avrcp"
  32. #define BT_PROFILE_AV_RC_CTRL_ID "avrcp_ctrl"
  33. /** Bluetooth Address */
  34. typedef struct {
  35. uint8_t address[6];
  36. } __attribute__((packed))bt_bdaddr_t;
  37. /** Bluetooth Device Name */
  38. typedef struct {
  39. uint8_t name[249];
  40. } __attribute__((packed))bt_bdname_t;
  41. /** Bluetooth Adapter Visibility Modes*/
  42. typedef enum {
  43. BT_SCAN_MODE_NONE,
  44. BT_SCAN_MODE_CONNECTABLE,
  45. BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE
  46. } bt_scan_mode_t;
  47. /** Bluetooth Adapter State */
  48. typedef enum {
  49. BT_STATE_OFF,
  50. BT_STATE_ON
  51. } bt_state_t;
  52. /** Bluetooth Error Status */
  53. /** We need to build on this */
  54. typedef enum {
  55. BT_STATUS_SUCCESS,
  56. BT_STATUS_FAIL,
  57. BT_STATUS_NOT_READY,
  58. BT_STATUS_NOMEM,
  59. BT_STATUS_BUSY,
  60. BT_STATUS_DONE, /* request already completed */
  61. BT_STATUS_UNSUPPORTED,
  62. BT_STATUS_PARM_INVALID,
  63. BT_STATUS_UNHANDLED,
  64. BT_STATUS_AUTH_FAILURE,
  65. BT_STATUS_RMT_DEV_DOWN,
  66. BT_STATUS_AUTH_REJECTED
  67. } bt_status_t;
  68. /** Bluetooth PinKey Code */
  69. typedef struct {
  70. uint8_t pin[16];
  71. } __attribute__((packed))bt_pin_code_t;
  72. typedef struct {
  73. uint8_t status;
  74. uint8_t ctrl_state; /* stack reported state */
  75. uint64_t tx_time; /* in ms */
  76. uint64_t rx_time; /* in ms */
  77. uint64_t idle_time; /* in ms */
  78. uint64_t energy_used; /* a product of mA, V and ms */
  79. } __attribute__((packed))bt_activity_energy_info;
  80. /** Bluetooth Adapter Discovery state */
  81. typedef enum {
  82. BT_DISCOVERY_STOPPED,
  83. BT_DISCOVERY_STARTED
  84. } bt_discovery_state_t;
  85. /** Bluetooth ACL connection state */
  86. typedef enum {
  87. BT_ACL_STATE_CONNECTED,
  88. BT_ACL_STATE_DISCONNECTED
  89. } bt_acl_state_t;
  90. /** Bluetooth 128-bit UUID */
  91. typedef struct {
  92. uint8_t uu[16];
  93. } bt_uuid_t;
  94. /** Bluetooth SDP service record */
  95. typedef struct
  96. {
  97. bt_uuid_t uuid;
  98. uint16_t channel;
  99. char name[256]; // what's the maximum length
  100. } bt_service_record_t;
  101. /** Bluetooth Remote Version info */
  102. typedef struct
  103. {
  104. int version;
  105. int sub_ver;
  106. int manufacturer;
  107. } bt_remote_version_t;
  108. typedef struct
  109. {
  110. uint8_t local_privacy_enabled;
  111. uint8_t max_adv_instance;
  112. uint8_t rpa_offload_supported;
  113. uint8_t max_irk_list_size;
  114. uint8_t max_adv_filter_supported;
  115. uint8_t scan_result_storage_size_lobyte;
  116. uint8_t scan_result_storage_size_hibyte;
  117. uint8_t activity_energy_info_supported;
  118. }bt_local_le_features_t;
  119. /* Bluetooth Adapter and Remote Device property types */
  120. typedef enum {
  121. /* Properties common to both adapter and remote device */
  122. /**
  123. * Description - Bluetooth Device Name
  124. * Access mode - Adapter name can be GET/SET. Remote device can be GET
  125. * Data type - bt_bdname_t
  126. */
  127. BT_PROPERTY_BDNAME = 0x1,
  128. /**
  129. * Description - Bluetooth Device Address
  130. * Access mode - Only GET.
  131. * Data type - bt_bdaddr_t
  132. */
  133. BT_PROPERTY_BDADDR,
  134. /**
  135. * Description - Bluetooth Service 128-bit UUIDs
  136. * Access mode - Only GET.
  137. * Data type - Array of bt_uuid_t (Array size inferred from property length).
  138. */
  139. BT_PROPERTY_UUIDS,
  140. /**
  141. * Description - Bluetooth Class of Device as found in Assigned Numbers
  142. * Access mode - Only GET.
  143. * Data type - uint32_t.
  144. */
  145. BT_PROPERTY_CLASS_OF_DEVICE,
  146. /**
  147. * Description - Device Type - BREDR, BLE or DUAL Mode
  148. * Access mode - Only GET.
  149. * Data type - bt_device_type_t
  150. */
  151. BT_PROPERTY_TYPE_OF_DEVICE,
  152. /**
  153. * Description - Bluetooth Service Record
  154. * Access mode - Only GET.
  155. * Data type - bt_service_record_t
  156. */
  157. BT_PROPERTY_SERVICE_RECORD,
  158. /* Properties unique to adapter */
  159. /**
  160. * Description - Bluetooth Adapter scan mode
  161. * Access mode - GET and SET
  162. * Data type - bt_scan_mode_t.
  163. */
  164. BT_PROPERTY_ADAPTER_SCAN_MODE,
  165. /**
  166. * Description - List of bonded devices
  167. * Access mode - Only GET.
  168. * Data type - Array of bt_bdaddr_t of the bonded remote devices
  169. * (Array size inferred from property length).
  170. */
  171. BT_PROPERTY_ADAPTER_BONDED_DEVICES,
  172. /**
  173. * Description - Bluetooth Adapter Discovery timeout (in seconds)
  174. * Access mode - GET and SET
  175. * Data type - uint32_t
  176. */
  177. BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
  178. /* Properties unique to remote device */
  179. /**
  180. * Description - User defined friendly name of the remote device
  181. * Access mode - GET and SET
  182. * Data type - bt_bdname_t.
  183. */
  184. BT_PROPERTY_REMOTE_FRIENDLY_NAME,
  185. /**
  186. * Description - RSSI value of the inquired remote device
  187. * Access mode - Only GET.
  188. * Data type - int32_t.
  189. */
  190. BT_PROPERTY_REMOTE_RSSI,
  191. /**
  192. * Description - Remote version info
  193. * Access mode - SET/GET.
  194. * Data type - bt_remote_version_t.
  195. */
  196. BT_PROPERTY_REMOTE_VERSION_INFO,
  197. /**
  198. * Description - Local LE features
  199. * Access mode - GET.
  200. * Data type - bt_local_le_features_t.
  201. */
  202. BT_PROPERTY_LOCAL_LE_FEATURES,
  203. BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP = 0xFF,
  204. } bt_property_type_t;
  205. /** Bluetooth Adapter Property data structure */
  206. typedef struct
  207. {
  208. bt_property_type_t type;
  209. int len;
  210. void *val;
  211. } bt_property_t;
  212. /** Bluetooth Device Type */
  213. typedef enum {
  214. BT_DEVICE_DEVTYPE_BREDR = 0x1,
  215. BT_DEVICE_DEVTYPE_BLE,
  216. BT_DEVICE_DEVTYPE_DUAL
  217. } bt_device_type_t;
  218. /** Bluetooth Bond state */
  219. typedef enum {
  220. BT_BOND_STATE_NONE,
  221. BT_BOND_STATE_BONDING,
  222. BT_BOND_STATE_BONDED
  223. } bt_bond_state_t;
  224. /** Bluetooth SSP Bonding Variant */
  225. typedef enum {
  226. BT_SSP_VARIANT_PASSKEY_CONFIRMATION,
  227. BT_SSP_VARIANT_PASSKEY_ENTRY,
  228. BT_SSP_VARIANT_CONSENT,
  229. BT_SSP_VARIANT_PASSKEY_NOTIFICATION
  230. } bt_ssp_variant_t;
  231. #define BT_MAX_NUM_UUIDS 32
  232. /** Bluetooth Interface callbacks */
  233. /** Bluetooth Enable/Disable Callback. */
  234. typedef void (*adapter_state_changed_callback)(bt_state_t state);
  235. /** GET/SET Adapter Properties callback */
  236. /* TODO: For the GET/SET property APIs/callbacks, we may need a session
  237. * identifier to associate the call with the callback. This would be needed
  238. * whenever more than one simultaneous instance of the same adapter_type
  239. * is get/set.
  240. *
  241. * If this is going to be handled in the Java framework, then we do not need
  242. * to manage sessions here.
  243. */
  244. typedef void (*adapter_properties_callback)(bt_status_t status,
  245. int num_properties,
  246. bt_property_t *properties);
  247. /** GET/SET Remote Device Properties callback */
  248. /** TODO: For remote device properties, do not see a need to get/set
  249. * multiple properties - num_properties shall be 1
  250. */
  251. typedef void (*remote_device_properties_callback)(bt_status_t status,
  252. bt_bdaddr_t *bd_addr,
  253. int num_properties,
  254. bt_property_t *properties);
  255. /** New device discovered callback */
  256. /** If EIR data is not present, then BD_NAME and RSSI shall be NULL and -1
  257. * respectively */
  258. typedef void (*device_found_callback)(int num_properties,
  259. bt_property_t *properties);
  260. /** Discovery state changed callback */
  261. typedef void (*discovery_state_changed_callback)(bt_discovery_state_t state);
  262. /** Bluetooth Legacy PinKey Request callback */
  263. typedef void (*pin_request_callback)(bt_bdaddr_t *remote_bd_addr,
  264. bt_bdname_t *bd_name, uint32_t cod);
  265. /** Bluetooth SSP Request callback - Just Works & Numeric Comparison*/
  266. /** pass_key - Shall be 0 for BT_SSP_PAIRING_VARIANT_CONSENT &
  267. * BT_SSP_PAIRING_PASSKEY_ENTRY */
  268. /* TODO: Passkey request callback shall not be needed for devices with display
  269. * capability. We still need support this in the stack for completeness */
  270. typedef void (*ssp_request_callback)(bt_bdaddr_t *remote_bd_addr,
  271. bt_bdname_t *bd_name,
  272. uint32_t cod,
  273. bt_ssp_variant_t pairing_variant,
  274. uint32_t pass_key);
  275. /** Bluetooth Bond state changed callback */
  276. /* Invoked in response to create_bond, cancel_bond or remove_bond */
  277. typedef void (*bond_state_changed_callback)(bt_status_t status,
  278. bt_bdaddr_t *remote_bd_addr,
  279. bt_bond_state_t state);
  280. /** Bluetooth ACL connection state changed callback */
  281. typedef void (*acl_state_changed_callback)(bt_status_t status, bt_bdaddr_t *remote_bd_addr,
  282. bt_acl_state_t state);
  283. typedef enum {
  284. ASSOCIATE_JVM,
  285. DISASSOCIATE_JVM
  286. } bt_cb_thread_evt;
  287. /** Thread Associate/Disassociate JVM Callback */
  288. /* Callback that is invoked by the callback thread to allow upper layer to attach/detach to/from
  289. * the JVM */
  290. typedef void (*callback_thread_event)(bt_cb_thread_evt evt);
  291. /** Bluetooth Test Mode Callback */
  292. /* Receive any HCI event from controller. Must be in DUT Mode for this callback to be received */
  293. typedef void (*dut_mode_recv_callback)(uint16_t opcode, uint8_t *buf, uint8_t len);
  294. /* LE Test mode callbacks
  295. * This callback shall be invoked whenever the le_tx_test, le_rx_test or le_test_end is invoked
  296. * The num_packets is valid only for le_test_end command */
  297. typedef void (*le_test_mode_callback)(bt_status_t status, uint16_t num_packets);
  298. /** Callback invoked when energy details are obtained */
  299. /* Ctrl_state-Current controller state-Active-1,scan-2,or idle-3 state as defined by HCI spec.
  300. * If the ctrl_state value is 0, it means the API call failed
  301. * Time values-In milliseconds as returned by the controller
  302. * Energy used-Value as returned by the controller
  303. * Status-Provides the status of the read_energy_info API call */
  304. typedef void (*energy_info_callback)(bt_activity_energy_info *energy_info);
  305. /** TODO: Add callbacks for Link Up/Down and other generic
  306. * notifications/callbacks */
  307. /** Bluetooth DM callback structure. */
  308. typedef struct {
  309. /** set to sizeof(bt_callbacks_t) */
  310. size_t size;
  311. adapter_state_changed_callback adapter_state_changed_cb;
  312. adapter_properties_callback adapter_properties_cb;
  313. remote_device_properties_callback remote_device_properties_cb;
  314. device_found_callback device_found_cb;
  315. discovery_state_changed_callback discovery_state_changed_cb;
  316. pin_request_callback pin_request_cb;
  317. ssp_request_callback ssp_request_cb;
  318. bond_state_changed_callback bond_state_changed_cb;
  319. acl_state_changed_callback acl_state_changed_cb;
  320. callback_thread_event thread_evt_cb;
  321. dut_mode_recv_callback dut_mode_recv_cb;
  322. le_test_mode_callback le_test_mode_cb;
  323. energy_info_callback energy_info_cb;
  324. } bt_callbacks_t;
  325. typedef void (*alarm_cb)(void *data);
  326. typedef bool (*set_wake_alarm_callout)(uint64_t delay_millis, bool should_wake, alarm_cb cb, void *data);
  327. typedef int (*acquire_wake_lock_callout)(const char *lock_name);
  328. typedef int (*release_wake_lock_callout)(const char *lock_name);
  329. /** The set of functions required by bluedroid to set wake alarms and
  330. * grab wake locks. This struct is passed into the stack through the
  331. * |set_os_callouts| function on |bt_interface_t|.
  332. */
  333. typedef struct {
  334. /* set to sizeof(bt_os_callouts_t) */
  335. size_t size;
  336. set_wake_alarm_callout set_wake_alarm;
  337. acquire_wake_lock_callout acquire_wake_lock;
  338. release_wake_lock_callout release_wake_lock;
  339. } bt_os_callouts_t;
  340. /** NOTE: By default, no profiles are initialized at the time of init/enable.
  341. * Whenever the application invokes the 'init' API of a profile, then one of
  342. * the following shall occur:
  343. *
  344. * 1.) If Bluetooth is not enabled, then the Bluetooth core shall mark the
  345. * profile as enabled. Subsequently, when the application invokes the
  346. * Bluetooth 'enable', as part of the enable sequence the profile that were
  347. * marked shall be enabled by calling appropriate stack APIs. The
  348. * 'adapter_properties_cb' shall return the list of UUIDs of the
  349. * enabled profiles.
  350. *
  351. * 2.) If Bluetooth is enabled, then the Bluetooth core shall invoke the stack
  352. * profile API to initialize the profile and trigger a
  353. * 'adapter_properties_cb' with the current list of UUIDs including the
  354. * newly added profile's UUID.
  355. *
  356. * The reverse shall occur whenever the profile 'cleanup' APIs are invoked
  357. */
  358. /** Represents the standard Bluetooth DM interface. */
  359. typedef struct {
  360. /** set to sizeof(bt_interface_t) */
  361. size_t size;
  362. /**
  363. * Opens the interface and provides the callback routines
  364. * to the implemenation of this interface.
  365. */
  366. int (*init)(bt_callbacks_t* callbacks );
  367. /** Enable Bluetooth. */
  368. int (*enable)(void);
  369. /** Disable Bluetooth. */
  370. int (*disable)(void);
  371. /** Closes the interface. */
  372. void (*cleanup)(void);
  373. /** Get all Bluetooth Adapter properties at init */
  374. int (*get_adapter_properties)(void);
  375. /** Get Bluetooth Adapter property of 'type' */
  376. int (*get_adapter_property)(bt_property_type_t type);
  377. /** Set Bluetooth Adapter property of 'type' */
  378. /* Based on the type, val shall be one of
  379. * bt_bdaddr_t or bt_bdname_t or bt_scanmode_t etc
  380. */
  381. int (*set_adapter_property)(const bt_property_t *property);
  382. /** Get all Remote Device properties */
  383. int (*get_remote_device_properties)(bt_bdaddr_t *remote_addr);
  384. /** Get Remote Device property of 'type' */
  385. int (*get_remote_device_property)(bt_bdaddr_t *remote_addr,
  386. bt_property_type_t type);
  387. /** Set Remote Device property of 'type' */
  388. int (*set_remote_device_property)(bt_bdaddr_t *remote_addr,
  389. const bt_property_t *property);
  390. /** Get Remote Device's service record for the given UUID */
  391. int (*get_remote_service_record)(bt_bdaddr_t *remote_addr,
  392. bt_uuid_t *uuid);
  393. /** Start SDP to get remote services */
  394. int (*get_remote_services)(bt_bdaddr_t *remote_addr);
  395. /** Start Discovery */
  396. int (*start_discovery)(void);
  397. /** Cancel Discovery */
  398. int (*cancel_discovery)(void);
  399. /** Create Bluetooth Bonding */
  400. int (*create_bond)(const bt_bdaddr_t *bd_addr, int transport);
  401. /** Remove Bond */
  402. int (*remove_bond)(const bt_bdaddr_t *bd_addr);
  403. /** Cancel Bond */
  404. int (*cancel_bond)(const bt_bdaddr_t *bd_addr);
  405. /**
  406. * Get the connection status for a given remote device.
  407. * return value of 0 means the device is not connected,
  408. * non-zero return status indicates an active connection.
  409. */
  410. int (*get_connection_state)(const bt_bdaddr_t *bd_addr);
  411. /** BT Legacy PinKey Reply */
  412. /** If accept==FALSE, then pin_len and pin_code shall be 0x0 */
  413. int (*pin_reply)(const bt_bdaddr_t *bd_addr, uint8_t accept,
  414. uint8_t pin_len, bt_pin_code_t *pin_code);
  415. /** BT SSP Reply - Just Works, Numeric Comparison and Passkey
  416. * passkey shall be zero for BT_SSP_VARIANT_PASSKEY_COMPARISON &
  417. * BT_SSP_VARIANT_CONSENT
  418. * For BT_SSP_VARIANT_PASSKEY_ENTRY, if accept==FALSE, then passkey
  419. * shall be zero */
  420. int (*ssp_reply)(const bt_bdaddr_t *bd_addr, bt_ssp_variant_t variant,
  421. uint8_t accept, uint32_t passkey);
  422. /** Get Bluetooth profile interface */
  423. const void* (*get_profile_interface) (const char *profile_id);
  424. /** Bluetooth Test Mode APIs - Bluetooth must be enabled for these APIs */
  425. /* Configure DUT Mode - Use this mode to enter/exit DUT mode */
  426. int (*dut_mode_configure)(uint8_t enable);
  427. /* Send any test HCI (vendor-specific) command to the controller. Must be in DUT Mode */
  428. int (*dut_mode_send)(uint16_t opcode, uint8_t *buf, uint8_t len);
  429. /** BLE Test Mode APIs */
  430. /* opcode MUST be one of: LE_Receiver_Test, LE_Transmitter_Test, LE_Test_End */
  431. int (*le_test_mode)(uint16_t opcode, uint8_t *buf, uint8_t len);
  432. /* enable or disable bluetooth HCI snoop log */
  433. int (*config_hci_snoop_log)(uint8_t enable);
  434. /** Sets the OS call-out functions that bluedroid needs for alarms and wake locks.
  435. * This should be called immediately after a successful |init|.
  436. */
  437. int (*set_os_callouts)(bt_os_callouts_t *callouts);
  438. /** Read Energy info details - return value indicates BT_STATUS_SUCCESS or BT_STATUS_NOT_READY
  439. * Success indicates that the VSC command was sent to controller
  440. */
  441. int (*read_energy_info)();
  442. } bt_interface_t;
  443. /** TODO: Need to add APIs for Service Discovery, Service authorization and
  444. * connection management. Also need to add APIs for configuring
  445. * properties of remote bonded devices such as name, UUID etc. */
  446. typedef struct {
  447. struct hw_device_t common;
  448. const bt_interface_t* (*get_bluetooth_interface)();
  449. } bluetooth_device_t;
  450. typedef bluetooth_device_t bluetooth_module_t;
  451. __END_DECLS
  452. #endif /* ANDROID_INCLUDE_BLUETOOTH_H */