1 /******************************************************************************
3 * Copyright (C) 2009-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /************************************************************************************
21 * Filename: bluetooth.c
23 * Description: Bluetooth HAL implementation
25 ***********************************************************************************/
27 #define LOG_TAG "bt_btif"
35 #include <hardware/bluetooth.h>
36 #include <hardware/bt_av.h>
37 #include <hardware/bt_gatt.h>
38 #include <hardware/bt_hf.h>
39 #include <hardware/bt_hf_client.h>
40 #include <hardware/bt_hh.h>
41 #include <hardware/bt_hl.h>
42 #include <hardware/bt_mce.h>
43 #include <hardware/bt_pan.h>
44 #include <hardware/bt_rc.h>
45 #include <hardware/bt_sdp.h>
46 #include <hardware/bt_sock.h>
50 #include "btif_debug.h"
52 #include "btsnoop_mem.h"
53 #include "device/include/interop.h"
54 #include "osi/include/allocation_tracker.h"
55 #include "osi/include/alarm.h"
56 #include "osi/include/log.h"
57 #include "osi/include/metrics.h"
58 #include "osi/include/osi.h"
59 #include "osi/include/wakelock.h"
60 #include "stack_manager.h"
61 #include "btif_config.h"
62 #include "btif_storage.h"
63 #include "btif/include/btif_debug_btsnoop.h"
64 #include "btif/include/btif_debug_conn.h"
65 #include "btif/include/btif_debug_l2c.h"
66 #include "btif/include/btif_media.h"
68 /************************************************************************************
70 ************************************************************************************/
72 bt_callbacks_t *bt_hal_cbacks = NULL;
73 bool restricted_mode = FALSE;
75 /************************************************************************************
77 ************************************************************************************/
79 /* list all extended interfaces here */
81 /* handsfree profile */
82 extern bthf_interface_t *btif_hf_get_interface();
83 /* handsfree profile - client */
84 extern bthf_client_interface_t *btif_hf_client_get_interface();
85 /* advanced audio profile */
86 extern btav_interface_t *btif_av_get_src_interface();
87 extern btav_interface_t *btif_av_get_sink_interface();
89 extern btsock_interface_t *btif_sock_get_interface();
90 /* hid host profile */
91 extern bthh_interface_t *btif_hh_get_interface();
92 /* health device profile */
93 extern bthl_interface_t *btif_hl_get_interface();
95 extern btpan_interface_t *btif_pan_get_interface();
97 extern btmce_interface_t *btif_mce_get_interface();
98 #if BLE_INCLUDED == TRUE
100 extern btgatt_interface_t *btif_gatt_get_interface();
103 extern btrc_interface_t *btif_rc_get_interface();
104 /* avrc controller */
105 extern btrc_interface_t *btif_rc_ctrl_get_interface();
106 /*SDP search client*/
107 extern btsdp_interface_t *btif_sdp_get_interface();
109 /************************************************************************************
111 ************************************************************************************/
113 static bool interface_ready(void) {
114 return bt_hal_cbacks != NULL;
117 static bool is_profile(const char *p1, const char *p2) {
120 return strlen(p1) == strlen(p2) && strncmp(p1, p2, strlen(p2)) == 0;
123 /*****************************************************************************
125 ** BLUETOOTH HAL INTERFACE FUNCTIONS
127 *****************************************************************************/
129 static int init(bt_callbacks_t *callbacks) {
130 LOG_INFO(LOG_TAG, "%s", __func__);
132 if (interface_ready())
133 return BT_STATUS_DONE;
135 #ifdef BLUEDROID_DEBUG
136 allocation_tracker_init();
139 bt_hal_cbacks = callbacks;
140 stack_manager_get_interface()->init_stack();
142 return BT_STATUS_SUCCESS;
145 static int enable(bool start_restricted) {
146 LOG_INFO(LOG_TAG, "%s: start restricted = %d", __func__, start_restricted);
148 restricted_mode = start_restricted;
150 if (!interface_ready())
151 return BT_STATUS_NOT_READY;
153 stack_manager_get_interface()->start_up_stack_async();
154 return BT_STATUS_SUCCESS;
157 static int disable(void) {
158 if (!interface_ready())
159 return BT_STATUS_NOT_READY;
161 stack_manager_get_interface()->shut_down_stack_async();
162 return BT_STATUS_SUCCESS;
165 static void cleanup(void) {
166 stack_manager_get_interface()->clean_up_stack();
169 bool is_restricted_mode() {
170 return restricted_mode;
173 static int get_adapter_properties(void)
176 if (interface_ready() == FALSE)
177 return BT_STATUS_NOT_READY;
179 return btif_get_adapter_properties();
182 static int get_adapter_property(bt_property_type_t type)
185 if (interface_ready() == FALSE)
186 return BT_STATUS_NOT_READY;
188 return btif_get_adapter_property(type);
191 static int set_adapter_property(const bt_property_t *property)
194 if (interface_ready() == FALSE)
195 return BT_STATUS_NOT_READY;
197 return btif_set_adapter_property(property);
200 int get_remote_device_properties(bt_bdaddr_t *remote_addr)
203 if (interface_ready() == FALSE)
204 return BT_STATUS_NOT_READY;
206 return btif_get_remote_device_properties(remote_addr);
209 int get_remote_device_property(bt_bdaddr_t *remote_addr, bt_property_type_t type)
212 if (interface_ready() == FALSE)
213 return BT_STATUS_NOT_READY;
215 return btif_get_remote_device_property(remote_addr, type);
218 int set_remote_device_property(bt_bdaddr_t *remote_addr, const bt_property_t *property)
221 if (interface_ready() == FALSE)
222 return BT_STATUS_NOT_READY;
224 return btif_set_remote_device_property(remote_addr, property);
227 int get_remote_service_record(bt_bdaddr_t *remote_addr, bt_uuid_t *uuid)
230 if (interface_ready() == FALSE)
231 return BT_STATUS_NOT_READY;
233 return btif_get_remote_service_record(remote_addr, uuid);
236 int get_remote_services(bt_bdaddr_t *remote_addr)
239 if (interface_ready() == FALSE)
240 return BT_STATUS_NOT_READY;
242 return btif_dm_get_remote_services(remote_addr);
245 static int start_discovery(void)
248 if (interface_ready() == FALSE)
249 return BT_STATUS_NOT_READY;
251 return btif_dm_start_discovery();
254 static int cancel_discovery(void)
257 if (interface_ready() == FALSE)
258 return BT_STATUS_NOT_READY;
260 return btif_dm_cancel_discovery();
263 static int create_bond(const bt_bdaddr_t *bd_addr, int transport)
266 if (interface_ready() == FALSE)
267 return BT_STATUS_NOT_READY;
269 return btif_dm_create_bond(bd_addr, transport);
272 static int create_bond_out_of_band(const bt_bdaddr_t *bd_addr, int transport,
273 const bt_out_of_band_data_t *oob_data)
276 if (interface_ready() == FALSE)
277 return BT_STATUS_NOT_READY;
279 return btif_dm_create_bond_out_of_band(bd_addr, transport, oob_data);
282 static int cancel_bond(const bt_bdaddr_t *bd_addr)
285 if (interface_ready() == FALSE)
286 return BT_STATUS_NOT_READY;
288 return btif_dm_cancel_bond(bd_addr);
291 static int remove_bond(const bt_bdaddr_t *bd_addr)
293 if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr))
294 return BT_STATUS_SUCCESS;
297 if (interface_ready() == FALSE)
298 return BT_STATUS_NOT_READY;
300 return btif_dm_remove_bond(bd_addr);
303 static int get_connection_state(const bt_bdaddr_t *bd_addr)
306 if (interface_ready() == FALSE)
309 return btif_dm_get_connection_state(bd_addr);
312 static int pin_reply(const bt_bdaddr_t *bd_addr, uint8_t accept,
313 uint8_t pin_len, bt_pin_code_t *pin_code)
316 if (interface_ready() == FALSE)
317 return BT_STATUS_NOT_READY;
319 return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code);
322 static int ssp_reply(const bt_bdaddr_t *bd_addr, bt_ssp_variant_t variant,
323 uint8_t accept, uint32_t passkey)
326 if (interface_ready() == FALSE)
327 return BT_STATUS_NOT_READY;
329 return btif_dm_ssp_reply(bd_addr, variant, accept, passkey);
332 static int read_energy_info()
334 if (interface_ready() == FALSE)
335 return BT_STATUS_NOT_READY;
336 btif_dm_read_energy_info();
337 return BT_STATUS_SUCCESS;
340 static void dump(int fd, const char **arguments)
342 if (arguments != NULL && arguments[0] != NULL) {
343 if (strncmp(arguments[0], "--proto-bin", 11) == 0) {
344 metrics_write_base64(fd, true);
348 btif_debug_conn_dump(fd);
349 btif_debug_bond_event_dump(fd);
350 btif_debug_a2dp_dump(fd);
351 btif_debug_l2c_dump(fd);
352 btif_debug_config_dump(fd);
353 wakelock_debug_dump(fd);
354 alarm_debug_dump(fd);
355 #if defined(BTSNOOP_MEM) && (BTSNOOP_MEM == TRUE)
356 btif_debug_btsnoop_dump(fd);
362 static const void* get_profile_interface (const char *profile_id)
364 LOG_INFO(LOG_TAG, "get_profile_interface %s", profile_id);
367 if (interface_ready() == FALSE)
370 /* check for supported profile interfaces */
371 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
372 return btif_hf_get_interface();
374 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
375 return btif_hf_client_get_interface();
377 if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
378 return btif_sock_get_interface();
380 if (is_profile(profile_id, BT_PROFILE_PAN_ID))
381 return btif_pan_get_interface();
383 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
384 return btif_av_get_src_interface();
386 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
387 return btif_av_get_sink_interface();
389 if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
390 return btif_hh_get_interface();
392 if (is_profile(profile_id, BT_PROFILE_HEALTH_ID))
393 return btif_hl_get_interface();
395 if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
396 return btif_sdp_get_interface();
398 #if ( BTA_GATT_INCLUDED == TRUE && BLE_INCLUDED == TRUE)
399 if (is_profile(profile_id, BT_PROFILE_GATT_ID))
400 return btif_gatt_get_interface();
403 if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
404 return btif_rc_get_interface();
406 if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
407 return btif_rc_ctrl_get_interface();
412 int dut_mode_configure(uint8_t enable)
414 LOG_INFO(LOG_TAG, "dut_mode_configure");
417 if (interface_ready() == FALSE)
418 return BT_STATUS_NOT_READY;
420 return btif_dut_mode_configure(enable);
423 int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len)
425 LOG_INFO(LOG_TAG, "dut_mode_send");
428 if (interface_ready() == FALSE)
429 return BT_STATUS_NOT_READY;
431 return btif_dut_mode_send(opcode, buf, len);
434 #if BLE_INCLUDED == TRUE
435 int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len)
437 LOG_INFO(LOG_TAG, "le_test_mode");
440 if (interface_ready() == FALSE)
441 return BT_STATUS_NOT_READY;
443 return btif_le_test_mode(opcode, buf, len);
447 int config_hci_snoop_log(uint8_t enable)
449 LOG_INFO(LOG_TAG, "config_hci_snoop_log");
451 if (!interface_ready())
452 return BT_STATUS_NOT_READY;
454 btsnoop_get_interface()->set_api_wants_to_log(enable);
455 return BT_STATUS_SUCCESS;
458 static int set_os_callouts(bt_os_callouts_t *callouts) {
459 wakelock_set_os_callouts(callouts);
460 return BT_STATUS_SUCCESS;
463 static int config_clear(void) {
464 LOG_INFO(LOG_TAG, "%s", __func__);
465 return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
468 static const bt_interface_t bluetoothInterface = {
469 sizeof(bluetoothInterface),
474 get_adapter_properties,
475 get_adapter_property,
476 set_adapter_property,
477 get_remote_device_properties,
478 get_remote_device_property,
479 set_remote_device_property,
480 get_remote_service_record,
485 create_bond_out_of_band,
488 get_connection_state,
491 get_profile_interface,
494 #if BLE_INCLUDED == TRUE
499 config_hci_snoop_log,
504 interop_database_clear,
505 interop_database_add,
508 const bt_interface_t* bluetooth__get_bluetooth_interface ()
510 /* fixme -- add property to disable bt interface ? */
512 return &bluetoothInterface;
515 static int close_bluetooth_stack(struct hw_device_t* device)
522 static int open_bluetooth_stack(const struct hw_module_t *module, UNUSED_ATTR char const *name, struct hw_device_t **abstraction) {
523 static bluetooth_device_t device = {
525 .tag = HARDWARE_DEVICE_TAG,
527 .close = close_bluetooth_stack,
529 .get_bluetooth_interface = bluetooth__get_bluetooth_interface
532 device.common.module = (struct hw_module_t *)module;
533 *abstraction = (struct hw_device_t *)&device;
537 static struct hw_module_methods_t bt_stack_module_methods = {
538 .open = open_bluetooth_stack,
541 EXPORT_SYMBOL struct hw_module_t HAL_MODULE_INFO_SYM = {
542 .tag = HARDWARE_MODULE_TAG,
545 .id = BT_HARDWARE_MODULE_ID,
546 .name = "Bluetooth Stack",
547 .author = "The Android Open Source Project",
548 .methods = &bt_stack_module_methods