blob: 21fe473fb21a74fe7dcd2a4ffc1c3bb16e938836 [file] [log] [blame]
Jakub Pawlowskif2f207a2017-09-23 14:47:20 -07001/*
2 * Copyright (C) 2012 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#ifndef ANDROID_INCLUDE_BLUETOOTH_H
18#define ANDROID_INCLUDE_BLUETOOTH_H
19
20#include <stdbool.h>
21#include <stdint.h>
22#include <sys/cdefs.h>
23#include <sys/types.h>
24
25#include <bluetooth/uuid.h>
26#include <raw_address.h>
27
28/**
29 * The Bluetooth Hardware Module ID
30 */
31
32#define BT_HARDWARE_MODULE_ID "bluetooth"
33#define BT_STACK_MODULE_ID "bluetooth"
34
35
36/** Bluetooth profile interface IDs */
37
38#define BT_PROFILE_HANDSFREE_ID "handsfree"
39#define BT_PROFILE_HANDSFREE_CLIENT_ID "handsfree_client"
40#define BT_PROFILE_ADVANCED_AUDIO_ID "a2dp"
41#define BT_PROFILE_ADVANCED_AUDIO_SINK_ID "a2dp_sink"
42#define BT_PROFILE_HEALTH_ID "health"
43#define BT_PROFILE_SOCKETS_ID "socket"
44#define BT_PROFILE_HIDHOST_ID "hidhost"
45#define BT_PROFILE_HIDDEV_ID "hiddev"
46#define BT_PROFILE_PAN_ID "pan"
47#define BT_PROFILE_MAP_CLIENT_ID "map_client"
48#define BT_PROFILE_SDP_CLIENT_ID "sdp"
49#define BT_PROFILE_GATT_ID "gatt"
50#define BT_PROFILE_AV_RC_ID "avrcp"
51#define BT_PROFILE_AV_RC_CTRL_ID "avrcp_ctrl"
52
53/** Bluetooth test interface IDs */
54#define BT_TEST_INTERFACE_MCAP_ID "mcap_test"
55
56/** Bluetooth Device Name */
57typedef struct {
58 uint8_t name[249];
59} __attribute__((packed))bt_bdname_t;
60
61/** Bluetooth Adapter Visibility Modes*/
62typedef enum {
63 BT_SCAN_MODE_NONE,
64 BT_SCAN_MODE_CONNECTABLE,
65 BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE
66} bt_scan_mode_t;
67
68/** Bluetooth Adapter State */
69typedef enum {
70 BT_STATE_OFF,
71 BT_STATE_ON
72} bt_state_t;
73
74/** Bluetooth Error Status */
75/** We need to build on this */
76
77typedef enum {
78 BT_STATUS_SUCCESS,
79 BT_STATUS_FAIL,
80 BT_STATUS_NOT_READY,
81 BT_STATUS_NOMEM,
82 BT_STATUS_BUSY,
83 BT_STATUS_DONE, /* request already completed */
84 BT_STATUS_UNSUPPORTED,
85 BT_STATUS_PARM_INVALID,
86 BT_STATUS_UNHANDLED,
87 BT_STATUS_AUTH_FAILURE,
88 BT_STATUS_RMT_DEV_DOWN,
89 BT_STATUS_AUTH_REJECTED,
90 BT_STATUS_JNI_ENVIRONMENT_ERROR,
91 BT_STATUS_JNI_THREAD_ATTACH_ERROR,
92 BT_STATUS_WAKELOCK_ERROR
93} bt_status_t;
94
95/** Bluetooth PinKey Code */
96typedef struct {
97 uint8_t pin[16];
98} __attribute__((packed))bt_pin_code_t;
99
100typedef struct {
101 uint8_t status;
102 uint8_t ctrl_state; /* stack reported state */
103 uint64_t tx_time; /* in ms */
104 uint64_t rx_time; /* in ms */
105 uint64_t idle_time; /* in ms */
106 uint64_t energy_used; /* a product of mA, V and ms */
107} __attribute__((packed))bt_activity_energy_info;
108
109typedef struct {
110 int32_t app_uid;
111 uint64_t tx_bytes;
112 uint64_t rx_bytes;
113} __attribute__((packed))bt_uid_traffic_t;
114
115/** Bluetooth Adapter Discovery state */
116typedef enum {
117 BT_DISCOVERY_STOPPED,
118 BT_DISCOVERY_STARTED
119} bt_discovery_state_t;
120
121/** Bluetooth ACL connection state */
122typedef enum {
123 BT_ACL_STATE_CONNECTED,
124 BT_ACL_STATE_DISCONNECTED
125} bt_acl_state_t;
126
127/** Bluetooth SDP service record */
128typedef struct
129{
130 bluetooth::Uuid uuid;
131 uint16_t channel;
132 char name[256]; // what's the maximum length
133} bt_service_record_t;
134
135
136/** Bluetooth Remote Version info */
137typedef struct
138{
139 int version;
140 int sub_ver;
141 int manufacturer;
142} bt_remote_version_t;
143
144typedef struct
145{
146 uint16_t version_supported;
147 uint8_t local_privacy_enabled;
148 uint8_t max_adv_instance;
149 uint8_t rpa_offload_supported;
150 uint8_t max_irk_list_size;
151 uint8_t max_adv_filter_supported;
152 uint8_t activity_energy_info_supported;
153 uint16_t scan_result_storage_size;
154 uint16_t total_trackable_advertisers;
155 bool extended_scan_support;
156 bool debug_logging_supported;
157 bool le_2m_phy_supported;
158 bool le_coded_phy_supported;
159 bool le_extended_advertising_supported;
160 bool le_periodic_advertising_supported;
161 uint16_t le_maximum_advertising_data_length;
162}bt_local_le_features_t;
163
164/* Bluetooth Adapter and Remote Device property types */
165typedef enum {
166 /* Properties common to both adapter and remote device */
167 /**
168 * Description - Bluetooth Device Name
169 * Access mode - Adapter name can be GET/SET. Remote device can be GET
170 * Data type - bt_bdname_t
171 */
172 BT_PROPERTY_BDNAME = 0x1,
173 /**
174 * Description - Bluetooth Device Address
175 * Access mode - Only GET.
176 * Data type - RawAddress
177 */
178 BT_PROPERTY_BDADDR,
179 /**
180 * Description - Bluetooth Service 128-bit UUIDs
181 * Access mode - Only GET.
182 * Data type - Array of bluetooth::Uuid (Array size inferred from property
183 * length).
184 */
185 BT_PROPERTY_UUIDS,
186 /**
187 * Description - Bluetooth Class of Device as found in Assigned Numbers
188 * Access mode - Only GET.
189 * Data type - uint32_t.
190 */
191 BT_PROPERTY_CLASS_OF_DEVICE,
192 /**
193 * Description - Device Type - BREDR, BLE or DUAL Mode
194 * Access mode - Only GET.
195 * Data type - bt_device_type_t
196 */
197 BT_PROPERTY_TYPE_OF_DEVICE,
198 /**
199 * Description - Bluetooth Service Record
200 * Access mode - Only GET.
201 * Data type - bt_service_record_t
202 */
203 BT_PROPERTY_SERVICE_RECORD,
204
205 /* Properties unique to adapter */
206 /**
207 * Description - Bluetooth Adapter scan mode
208 * Access mode - GET and SET
209 * Data type - bt_scan_mode_t.
210 */
211 BT_PROPERTY_ADAPTER_SCAN_MODE,
212 /**
213 * Description - List of bonded devices
214 * Access mode - Only GET.
215 * Data type - Array of RawAddress of the bonded remote devices
216 * (Array size inferred from property length).
217 */
218 BT_PROPERTY_ADAPTER_BONDED_DEVICES,
219 /**
220 * Description - Bluetooth Adapter Discovery timeout (in seconds)
221 * Access mode - GET and SET
222 * Data type - uint32_t
223 */
224 BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
225
226 /* Properties unique to remote device */
227 /**
228 * Description - User defined friendly name of the remote device
229 * Access mode - GET and SET
230 * Data type - bt_bdname_t.
231 */
232 BT_PROPERTY_REMOTE_FRIENDLY_NAME,
233 /**
234 * Description - RSSI value of the inquired remote device
235 * Access mode - Only GET.
236 * Data type - int32_t.
237 */
238 BT_PROPERTY_REMOTE_RSSI,
239 /**
240 * Description - Remote version info
241 * Access mode - SET/GET.
242 * Data type - bt_remote_version_t.
243 */
244
245 BT_PROPERTY_REMOTE_VERSION_INFO,
246
247 /**
248 * Description - Local LE features
249 * Access mode - GET.
250 * Data type - bt_local_le_features_t.
251 */
252 BT_PROPERTY_LOCAL_LE_FEATURES,
253
254 BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP = 0xFF,
255} bt_property_type_t;
256
257/** Bluetooth Adapter Property data structure */
258typedef struct
259{
260 bt_property_type_t type;
261 int len;
262 void *val;
263} bt_property_t;
264
265/** Bluetooth Out Of Band data for bonding */
266typedef struct
267{
268 uint8_t le_bt_dev_addr[7]; /* LE Bluetooth Device Address */
269 uint8_t c192[16]; /* Simple Pairing Hash C-192 */
270 uint8_t r192[16]; /* Simple Pairing Randomizer R-192 */
271 uint8_t c256[16]; /* Simple Pairing Hash C-256 */
272 uint8_t r256[16]; /* Simple Pairing Randomizer R-256 */
273 uint8_t sm_tk[16]; /* Security Manager TK Value */
274 uint8_t le_sc_c[16]; /* LE Secure Connections Confirmation Value */
275 uint8_t le_sc_r[16]; /* LE Secure Connections Random Value */
276} bt_out_of_band_data_t;
277
278
279
280/** Bluetooth Device Type */
281typedef enum {
282 BT_DEVICE_DEVTYPE_BREDR = 0x1,
283 BT_DEVICE_DEVTYPE_BLE,
284 BT_DEVICE_DEVTYPE_DUAL
285} bt_device_type_t;
286/** Bluetooth Bond state */
287typedef enum {
288 BT_BOND_STATE_NONE,
289 BT_BOND_STATE_BONDING,
290 BT_BOND_STATE_BONDED
291} bt_bond_state_t;
292
293/** Bluetooth SSP Bonding Variant */
294typedef enum {
295 BT_SSP_VARIANT_PASSKEY_CONFIRMATION,
296 BT_SSP_VARIANT_PASSKEY_ENTRY,
297 BT_SSP_VARIANT_CONSENT,
298 BT_SSP_VARIANT_PASSKEY_NOTIFICATION
299} bt_ssp_variant_t;
300
301#define BT_MAX_NUM_UUIDS 32
302
303/** Bluetooth Interface callbacks */
304
305/** Bluetooth Enable/Disable Callback. */
306typedef void (*adapter_state_changed_callback)(bt_state_t state);
307
308/** GET/SET Adapter Properties callback */
309/* TODO: For the GET/SET property APIs/callbacks, we may need a session
310 * identifier to associate the call with the callback. This would be needed
311 * whenever more than one simultaneous instance of the same adapter_type
312 * is get/set.
313 *
314 * If this is going to be handled in the Java framework, then we do not need
315 * to manage sessions here.
316 */
317typedef void (*adapter_properties_callback)(bt_status_t status,
318 int num_properties,
319 bt_property_t *properties);
320
321/** GET/SET Remote Device Properties callback */
322/** TODO: For remote device properties, do not see a need to get/set
323 * multiple properties - num_properties shall be 1
324 */
325typedef void (*remote_device_properties_callback)(bt_status_t status,
326 RawAddress *bd_addr,
327 int num_properties,
328 bt_property_t *properties);
329
330/** New device discovered callback */
331/** If EIR data is not present, then BD_NAME and RSSI shall be NULL and -1
332 * respectively */
333typedef void (*device_found_callback)(int num_properties,
334 bt_property_t *properties);
335
336/** Discovery state changed callback */
337typedef void (*discovery_state_changed_callback)(bt_discovery_state_t state);
338
339/** Bluetooth Legacy PinKey Request callback */
340typedef void (*pin_request_callback)(RawAddress *remote_bd_addr,
341 bt_bdname_t *bd_name, uint32_t cod, bool min_16_digit);
342
343/** Bluetooth SSP Request callback - Just Works & Numeric Comparison*/
344/** pass_key - Shall be 0 for BT_SSP_PAIRING_VARIANT_CONSENT &
345 * BT_SSP_PAIRING_PASSKEY_ENTRY */
346/* TODO: Passkey request callback shall not be needed for devices with display
347 * capability. We still need support this in the stack for completeness */
348typedef void (*ssp_request_callback)(RawAddress *remote_bd_addr,
349 bt_bdname_t *bd_name,
350 uint32_t cod,
351 bt_ssp_variant_t pairing_variant,
352 uint32_t pass_key);
353
354/** Bluetooth Bond state changed callback */
355/* Invoked in response to create_bond, cancel_bond or remove_bond */
356typedef void (*bond_state_changed_callback)(bt_status_t status,
357 RawAddress *remote_bd_addr,
358 bt_bond_state_t state);
359
360/** Bluetooth ACL connection state changed callback */
361typedef void (*acl_state_changed_callback)(bt_status_t status, RawAddress *remote_bd_addr,
362 bt_acl_state_t state);
363
364typedef enum {
365 ASSOCIATE_JVM,
366 DISASSOCIATE_JVM
367} bt_cb_thread_evt;
368
369/** Thread Associate/Disassociate JVM Callback */
370/* Callback that is invoked by the callback thread to allow upper layer to attach/detach to/from
371 * the JVM */
372typedef void (*callback_thread_event)(bt_cb_thread_evt evt);
373
374/** Bluetooth Test Mode Callback */
375/* Receive any HCI event from controller. Must be in DUT Mode for this callback to be received */
376typedef void (*dut_mode_recv_callback)(uint16_t opcode, uint8_t *buf, uint8_t len);
377
378/* LE Test mode callbacks
379* This callback shall be invoked whenever the le_tx_test, le_rx_test or le_test_end is invoked
380* The num_packets is valid only for le_test_end command */
381typedef void (*le_test_mode_callback)(bt_status_t status, uint16_t num_packets);
382
383/** Callback invoked when energy details are obtained */
384/* Ctrl_state-Current controller state-Active-1,scan-2,or idle-3 state as defined by HCI spec.
385 * If the ctrl_state value is 0, it means the API call failed
386 * Time values-In milliseconds as returned by the controller
387 * Energy used-Value as returned by the controller
388 * Status-Provides the status of the read_energy_info API call
389 * uid_data provides an array of bt_uid_traffic_t, where the array is terminated by an element with
390 * app_uid set to -1.
391 */
392typedef void (*energy_info_callback)(bt_activity_energy_info *energy_info,
393 bt_uid_traffic_t *uid_data);
394
395/** TODO: Add callbacks for Link Up/Down and other generic
396 * notifications/callbacks */
397
398/** Bluetooth DM callback structure. */
399typedef struct {
400 /** set to sizeof(bt_callbacks_t) */
401 size_t size;
402 adapter_state_changed_callback adapter_state_changed_cb;
403 adapter_properties_callback adapter_properties_cb;
404 remote_device_properties_callback remote_device_properties_cb;
405 device_found_callback device_found_cb;
406 discovery_state_changed_callback discovery_state_changed_cb;
407 pin_request_callback pin_request_cb;
408 ssp_request_callback ssp_request_cb;
409 bond_state_changed_callback bond_state_changed_cb;
410 acl_state_changed_callback acl_state_changed_cb;
411 callback_thread_event thread_evt_cb;
412 dut_mode_recv_callback dut_mode_recv_cb;
413 le_test_mode_callback le_test_mode_cb;
414 energy_info_callback energy_info_cb;
415} bt_callbacks_t;
416
417typedef void (*alarm_cb)(void *data);
418typedef bool (*set_wake_alarm_callout)(uint64_t delay_millis, bool should_wake, alarm_cb cb, void *data);
419typedef int (*acquire_wake_lock_callout)(const char *lock_name);
420typedef int (*release_wake_lock_callout)(const char *lock_name);
421
422/** The set of functions required by bluedroid to set wake alarms and
423 * grab wake locks. This struct is passed into the stack through the
424 * |set_os_callouts| function on |bt_interface_t|.
425 */
426typedef struct {
427 /* set to sizeof(bt_os_callouts_t) */
428 size_t size;
429
430 set_wake_alarm_callout set_wake_alarm;
431 acquire_wake_lock_callout acquire_wake_lock;
432 release_wake_lock_callout release_wake_lock;
433} bt_os_callouts_t;
434
435/** NOTE: By default, no profiles are initialized at the time of init/enable.
436 * Whenever the application invokes the 'init' API of a profile, then one of
437 * the following shall occur:
438 *
439 * 1.) If Bluetooth is not enabled, then the Bluetooth core shall mark the
440 * profile as enabled. Subsequently, when the application invokes the
441 * Bluetooth 'enable', as part of the enable sequence the profile that were
442 * marked shall be enabled by calling appropriate stack APIs. The
443 * 'adapter_properties_cb' shall return the list of UUIDs of the
444 * enabled profiles.
445 *
446 * 2.) If Bluetooth is enabled, then the Bluetooth core shall invoke the stack
447 * profile API to initialize the profile and trigger a
448 * 'adapter_properties_cb' with the current list of UUIDs including the
449 * newly added profile's UUID.
450 *
451 * The reverse shall occur whenever the profile 'cleanup' APIs are invoked
452 */
453
454/** Represents the standard Bluetooth DM interface. */
455typedef struct {
456 /** set to sizeof(bt_interface_t) */
457 size_t size;
458 /**
459 * Opens the interface and provides the callback routines
460 * to the implemenation of this interface.
461 */
462 int (*init)(bt_callbacks_t* callbacks );
463
464 /** Enable Bluetooth. */
465 int (*enable)(bool guest_mode);
466
467 /** Disable Bluetooth. */
468 int (*disable)(void);
469
470 /** Closes the interface. */
471 void (*cleanup)(void);
472
473 /** Get all Bluetooth Adapter properties at init */
474 int (*get_adapter_properties)(void);
475
476 /** Get Bluetooth Adapter property of 'type' */
477 int (*get_adapter_property)(bt_property_type_t type);
478
479 /** Set Bluetooth Adapter property of 'type' */
480 /* Based on the type, val shall be one of
481 * RawAddress or bt_bdname_t or bt_scanmode_t etc
482 */
483 int (*set_adapter_property)(const bt_property_t *property);
484
485 /** Get all Remote Device properties */
486 int (*get_remote_device_properties)(RawAddress *remote_addr);
487
488 /** Get Remote Device property of 'type' */
489 int (*get_remote_device_property)(RawAddress *remote_addr,
490 bt_property_type_t type);
491
492 /** Set Remote Device property of 'type' */
493 int (*set_remote_device_property)(RawAddress *remote_addr,
494 const bt_property_t *property);
495
496 /** Get Remote Device's service record for the given UUID */
497 int (*get_remote_service_record)(const RawAddress& remote_addr,
498 const bluetooth::Uuid& uuid);
499
500 /** Start SDP to get remote services */
501 int (*get_remote_services)(RawAddress *remote_addr);
502
503 /** Start Discovery */
504 int (*start_discovery)(void);
505
506 /** Cancel Discovery */
507 int (*cancel_discovery)(void);
508
509 /** Create Bluetooth Bonding */
510 int (*create_bond)(const RawAddress *bd_addr, int transport);
511
512 /** Create Bluetooth Bond using out of band data */
513 int (*create_bond_out_of_band)(const RawAddress *bd_addr, int transport,
514 const bt_out_of_band_data_t *oob_data);
515
516 /** Remove Bond */
517 int (*remove_bond)(const RawAddress *bd_addr);
518
519 /** Cancel Bond */
520 int (*cancel_bond)(const RawAddress *bd_addr);
521
522 /**
523 * Get the connection status for a given remote device.
524 * return value of 0 means the device is not connected,
525 * non-zero return status indicates an active connection.
526 */
527 int (*get_connection_state)(const RawAddress *bd_addr);
528
529 /** BT Legacy PinKey Reply */
530 /** If accept==FALSE, then pin_len and pin_code shall be 0x0 */
531 int (*pin_reply)(const RawAddress *bd_addr, uint8_t accept,
532 uint8_t pin_len, bt_pin_code_t *pin_code);
533
534 /** BT SSP Reply - Just Works, Numeric Comparison and Passkey
535 * passkey shall be zero for BT_SSP_VARIANT_PASSKEY_COMPARISON &
536 * BT_SSP_VARIANT_CONSENT
537 * For BT_SSP_VARIANT_PASSKEY_ENTRY, if accept==FALSE, then passkey
538 * shall be zero */
539 int (*ssp_reply)(const RawAddress *bd_addr, bt_ssp_variant_t variant,
540 uint8_t accept, uint32_t passkey);
541
542 /** Get Bluetooth profile interface */
543 const void* (*get_profile_interface) (const char *profile_id);
544
545 /** Bluetooth Test Mode APIs - Bluetooth must be enabled for these APIs */
546 /* Configure DUT Mode - Use this mode to enter/exit DUT mode */
547 int (*dut_mode_configure)(uint8_t enable);
548
549 /* Send any test HCI (vendor-specific) command to the controller. Must be in DUT Mode */
550 int (*dut_mode_send)(uint16_t opcode, uint8_t *buf, uint8_t len);
551 /** BLE Test Mode APIs */
552 /* opcode MUST be one of: LE_Receiver_Test, LE_Transmitter_Test, LE_Test_End */
553 int (*le_test_mode)(uint16_t opcode, uint8_t *buf, uint8_t len);
554
555 /** Sets the OS call-out functions that bluedroid needs for alarms and wake locks.
556 * This should be called immediately after a successful |init|.
557 */
558 int (*set_os_callouts)(bt_os_callouts_t *callouts);
559
560 /** Read Energy info details - return value indicates BT_STATUS_SUCCESS or BT_STATUS_NOT_READY
561 * Success indicates that the VSC command was sent to controller
562 */
563 int (*read_energy_info)();
564
565 /**
566 * Native support for dumpsys function
567 * Function is synchronous and |fd| is owned by caller.
568 * |arguments| are arguments which may affect the output, encoded as
569 * UTF-8 strings.
570 */
571 void (*dump)(int fd, const char **arguments);
572
573 /**
574 * Clear /data/misc/bt_config.conf and erase all stored connections
575 */
576 int (*config_clear)(void);
577
578 /**
579 * Clear (reset) the dynamic portion of the device interoperability database.
580 */
581 void (*interop_database_clear)(void);
582
583 /**
584 * Add a new device interoperability workaround for a remote device whose
585 * first |len| bytes of the its device address match |addr|.
586 * NOTE: |feature| has to match an item defined in interop_feature_t (interop.h).
587 */
588 void (*interop_database_add)(uint16_t feature, const RawAddress *addr, size_t len);
589} bt_interface_t;
590
591#define BLUETOOTH_INTERFACE_STRING "bluetoothInterface"
592
593#endif /* ANDROID_INCLUDE_BLUETOOTH_H */