1 /****************************************************************************** 2 * 3 * Copyright (C) 2009-2012 Broadcom Corporation 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 19 /************************************************************************************ 20 * 21 * Filename: bluetooth.c 22 * 23 * Description: Bluetooth HAL implementation 24 * 25 ***********************************************************************************/ 26 27 #define LOG_TAG "bt_btif" 28 29 #include <assert.h> 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <unistd.h> 34 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> 47 48 #include "bt_utils.h" 49 #include "btif_api.h" 50 #include "btif_debug.h" 51 #include "btsnoop.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_media.h" 66 67 /************************************************************************************ 68 ** Static variables 69 ************************************************************************************/ 70 71 bt_callbacks_t *bt_hal_cbacks = NULL; 72 bool restricted_mode = FALSE; 73 74 /************************************************************************************ 75 ** Externs 76 ************************************************************************************/ 77 78 /* list all extended interfaces here */ 79 80 /* handsfree profile */ 81 extern bthf_interface_t *btif_hf_get_interface(); 82 /* handsfree profile - client */ 83 extern bthf_client_interface_t *btif_hf_client_get_interface(); 84 /* advanced audio profile */ 85 extern btav_interface_t *btif_av_get_src_interface(); 86 extern btav_interface_t *btif_av_get_sink_interface(); 87 /*rfc l2cap*/ 88 extern btsock_interface_t *btif_sock_get_interface(); 89 /* hid host profile */ 90 extern bthh_interface_t *btif_hh_get_interface(); 91 /* health device profile */ 92 extern bthl_interface_t *btif_hl_get_interface(); 93 /*pan*/ 94 extern btpan_interface_t *btif_pan_get_interface(); 95 /*map client*/ 96 extern btmce_interface_t *btif_mce_get_interface(); 97 #if BLE_INCLUDED == TRUE 98 /* gatt */ 99 extern btgatt_interface_t *btif_gatt_get_interface(); 100 #endif 101 /* avrc target */ 102 extern btrc_interface_t *btif_rc_get_interface(); 103 /* avrc controller */ 104 extern btrc_interface_t *btif_rc_ctrl_get_interface(); 105 /*SDP search client*/ 106 extern btsdp_interface_t *btif_sdp_get_interface(); 107 108 /************************************************************************************ 109 ** Functions 110 ************************************************************************************/ 111 112 static bool interface_ready(void) { 113 return bt_hal_cbacks != NULL; 114 } 115 116 static bool is_profile(const char *p1, const char *p2) { 117 assert(p1); 118 assert(p2); 119 return strlen(p1) == strlen(p2) && strncmp(p1, p2, strlen(p2)) == 0; 120 } 121 122 /***************************************************************************** 123 ** 124 ** BLUETOOTH HAL INTERFACE FUNCTIONS 125 ** 126 *****************************************************************************/ 127 128 static int init(bt_callbacks_t *callbacks) { 129 LOG_INFO(LOG_TAG, "%s", __func__); 130 131 if (interface_ready()) 132 return BT_STATUS_DONE; 133 134 #ifdef BLUEDROID_DEBUG 135 allocation_tracker_init(); 136 #endif 137 138 bt_hal_cbacks = callbacks; 139 stack_manager_get_interface()->init_stack(); 140 btif_debug_init(); 141 return BT_STATUS_SUCCESS; 142 } 143 144 static int enable(bool start_restricted) { 145 LOG_INFO(LOG_TAG, "%s: start restricted = %d", __func__, start_restricted); 146 147 restricted_mode = start_restricted; 148 149 if (!interface_ready()) 150 return BT_STATUS_NOT_READY; 151 152 stack_manager_get_interface()->start_up_stack_async(); 153 return BT_STATUS_SUCCESS; 154 } 155 156 static int disable(void) { 157 if (!interface_ready()) 158 return BT_STATUS_NOT_READY; 159 160 stack_manager_get_interface()->shut_down_stack_async(); 161 return BT_STATUS_SUCCESS; 162 } 163 164 static void cleanup(void) { 165 stack_manager_get_interface()->clean_up_stack(); 166 } 167 168 bool is_restricted_mode() { 169 return restricted_mode; 170 } 171 172 static int get_adapter_properties(void) 173 { 174 /* sanity check */ 175 if (interface_ready() == FALSE) 176 return BT_STATUS_NOT_READY; 177 178 return btif_get_adapter_properties(); 179 } 180 181 static int get_adapter_property(bt_property_type_t type) 182 { 183 /* sanity check */ 184 if (interface_ready() == FALSE) 185 return BT_STATUS_NOT_READY; 186 187 return btif_get_adapter_property(type); 188 } 189 190 static int set_adapter_property(const bt_property_t *property) 191 { 192 /* sanity check */ 193 if (interface_ready() == FALSE) 194 return BT_STATUS_NOT_READY; 195 196 return btif_set_adapter_property(property); 197 } 198 199 int get_remote_device_properties(bt_bdaddr_t *remote_addr) 200 { 201 /* sanity check */ 202 if (interface_ready() == FALSE) 203 return BT_STATUS_NOT_READY; 204 205 return btif_get_remote_device_properties(remote_addr); 206 } 207 208 int get_remote_device_property(bt_bdaddr_t *remote_addr, bt_property_type_t type) 209 { 210 /* sanity check */ 211 if (interface_ready() == FALSE) 212 return BT_STATUS_NOT_READY; 213 214 return btif_get_remote_device_property(remote_addr, type); 215 } 216 217 int set_remote_device_property(bt_bdaddr_t *remote_addr, const bt_property_t *property) 218 { 219 /* sanity check */ 220 if (interface_ready() == FALSE) 221 return BT_STATUS_NOT_READY; 222 223 return btif_set_remote_device_property(remote_addr, property); 224 } 225 226 int get_remote_service_record(bt_bdaddr_t *remote_addr, bt_uuid_t *uuid) 227 { 228 /* sanity check */ 229 if (interface_ready() == FALSE) 230 return BT_STATUS_NOT_READY; 231 232 return btif_get_remote_service_record(remote_addr, uuid); 233 } 234 235 int get_remote_services(bt_bdaddr_t *remote_addr) 236 { 237 /* sanity check */ 238 if (interface_ready() == FALSE) 239 return BT_STATUS_NOT_READY; 240 241 return btif_dm_get_remote_services(remote_addr); 242 } 243 244 static int start_discovery(void) 245 { 246 /* sanity check */ 247 if (interface_ready() == FALSE) 248 return BT_STATUS_NOT_READY; 249 250 return btif_dm_start_discovery(); 251 } 252 253 static int cancel_discovery(void) 254 { 255 /* sanity check */ 256 if (interface_ready() == FALSE) 257 return BT_STATUS_NOT_READY; 258 259 return btif_dm_cancel_discovery(); 260 } 261 262 static int create_bond(const bt_bdaddr_t *bd_addr, int transport) 263 { 264 /* sanity check */ 265 if (interface_ready() == FALSE) 266 return BT_STATUS_NOT_READY; 267 268 return btif_dm_create_bond(bd_addr, transport); 269 } 270 271 static int create_bond_out_of_band(const bt_bdaddr_t *bd_addr, int transport, 272 const bt_out_of_band_data_t *oob_data) 273 { 274 /* sanity check */ 275 if (interface_ready() == FALSE) 276 return BT_STATUS_NOT_READY; 277 278 return btif_dm_create_bond_out_of_band(bd_addr, transport, oob_data); 279 } 280 281 static int cancel_bond(const bt_bdaddr_t *bd_addr) 282 { 283 /* sanity check */ 284 if (interface_ready() == FALSE) 285 return BT_STATUS_NOT_READY; 286 287 return btif_dm_cancel_bond(bd_addr); 288 } 289 290 static int remove_bond(const bt_bdaddr_t *bd_addr) 291 { 292 if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr)) 293 return BT_STATUS_SUCCESS; 294 295 /* sanity check */ 296 if (interface_ready() == FALSE) 297 return BT_STATUS_NOT_READY; 298 299 return btif_dm_remove_bond(bd_addr); 300 } 301 302 static int get_connection_state(const bt_bdaddr_t *bd_addr) 303 { 304 /* sanity check */ 305 if (interface_ready() == FALSE) 306 return 0; 307 308 return btif_dm_get_connection_state(bd_addr); 309 } 310 311 static int pin_reply(const bt_bdaddr_t *bd_addr, uint8_t accept, 312 uint8_t pin_len, bt_pin_code_t *pin_code) 313 { 314 /* sanity check */ 315 if (interface_ready() == FALSE) 316 return BT_STATUS_NOT_READY; 317 318 return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code); 319 } 320 321 static int ssp_reply(const bt_bdaddr_t *bd_addr, bt_ssp_variant_t variant, 322 uint8_t accept, uint32_t passkey) 323 { 324 /* sanity check */ 325 if (interface_ready() == FALSE) 326 return BT_STATUS_NOT_READY; 327 328 return btif_dm_ssp_reply(bd_addr, variant, accept, passkey); 329 } 330 331 static int read_energy_info() 332 { 333 if (interface_ready() == FALSE) 334 return BT_STATUS_NOT_READY; 335 btif_dm_read_energy_info(); 336 return BT_STATUS_SUCCESS; 337 } 338 339 static void dump(int fd, const char **arguments) 340 { 341 if (arguments != NULL && arguments[0] != NULL) { 342 if (strncmp(arguments[0], "--proto-text", 12) == 0) { 343 btif_update_a2dp_metrics(); 344 metrics_print(fd, true); 345 return; 346 } 347 if (strncmp(arguments[0], "--proto-bin", 11) == 0) { 348 btif_update_a2dp_metrics(); 349 metrics_write(fd, true); 350 return; 351 } 352 } 353 btif_debug_conn_dump(fd); 354 btif_debug_bond_event_dump(fd); 355 btif_debug_a2dp_dump(fd); 356 btif_debug_config_dump(fd); 357 wakelock_debug_dump(fd); 358 alarm_debug_dump(fd); 359 #if defined(BTSNOOP_MEM) && (BTSNOOP_MEM == TRUE) 360 btif_debug_btsnoop_dump(fd); 361 #endif 362 363 close(fd); 364 } 365 366 static const void* get_profile_interface (const char *profile_id) 367 { 368 LOG_INFO(LOG_TAG, "get_profile_interface %s", profile_id); 369 370 /* sanity check */ 371 if (interface_ready() == FALSE) 372 return NULL; 373 374 /* check for supported profile interfaces */ 375 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID)) 376 return btif_hf_get_interface(); 377 378 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID)) 379 return btif_hf_client_get_interface(); 380 381 if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID)) 382 return btif_sock_get_interface(); 383 384 if (is_profile(profile_id, BT_PROFILE_PAN_ID)) 385 return btif_pan_get_interface(); 386 387 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID)) 388 return btif_av_get_src_interface(); 389 390 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID)) 391 return btif_av_get_sink_interface(); 392 393 if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID)) 394 return btif_hh_get_interface(); 395 396 if (is_profile(profile_id, BT_PROFILE_HEALTH_ID)) 397 return btif_hl_get_interface(); 398 399 if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID)) 400 return btif_sdp_get_interface(); 401 402 #if ( BTA_GATT_INCLUDED == TRUE && BLE_INCLUDED == TRUE) 403 if (is_profile(profile_id, BT_PROFILE_GATT_ID)) 404 return btif_gatt_get_interface(); 405 #endif 406 407 if (is_profile(profile_id, BT_PROFILE_AV_RC_ID)) 408 return btif_rc_get_interface(); 409 410 if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID)) 411 return btif_rc_ctrl_get_interface(); 412 413 return NULL; 414 } 415 416 int dut_mode_configure(uint8_t enable) 417 { 418 LOG_INFO(LOG_TAG, "dut_mode_configure"); 419 420 /* sanity check */ 421 if (interface_ready() == FALSE) 422 return BT_STATUS_NOT_READY; 423 424 return btif_dut_mode_configure(enable); 425 } 426 427 int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) 428 { 429 LOG_INFO(LOG_TAG, "dut_mode_send"); 430 431 /* sanity check */ 432 if (interface_ready() == FALSE) 433 return BT_STATUS_NOT_READY; 434 435 return btif_dut_mode_send(opcode, buf, len); 436 } 437 438 #if BLE_INCLUDED == TRUE 439 int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) 440 { 441 LOG_INFO(LOG_TAG, "le_test_mode"); 442 443 /* sanity check */ 444 if (interface_ready() == FALSE) 445 return BT_STATUS_NOT_READY; 446 447 return btif_le_test_mode(opcode, buf, len); 448 } 449 #endif 450 451 int config_hci_snoop_log(uint8_t enable) 452 { 453 LOG_INFO(LOG_TAG, "config_hci_snoop_log"); 454 455 if (!interface_ready()) 456 return BT_STATUS_NOT_READY; 457 458 btsnoop_get_interface()->set_api_wants_to_log(enable); 459 return BT_STATUS_SUCCESS; 460 } 461 462 static int set_os_callouts(bt_os_callouts_t *callouts) { 463 wakelock_set_os_callouts(callouts); 464 return BT_STATUS_SUCCESS; 465 } 466 467 static int config_clear(void) { 468 LOG_INFO(LOG_TAG, "%s", __func__); 469 return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 470 } 471 472 static const bt_interface_t bluetoothInterface = { 473 sizeof(bluetoothInterface), 474 init, 475 enable, 476 disable, 477 cleanup, 478 get_adapter_properties, 479 get_adapter_property, 480 set_adapter_property, 481 get_remote_device_properties, 482 get_remote_device_property, 483 set_remote_device_property, 484 get_remote_service_record, 485 get_remote_services, 486 start_discovery, 487 cancel_discovery, 488 create_bond, 489 create_bond_out_of_band, 490 remove_bond, 491 cancel_bond, 492 get_connection_state, 493 pin_reply, 494 ssp_reply, 495 get_profile_interface, 496 dut_mode_configure, 497 dut_mode_send, 498 #if BLE_INCLUDED == TRUE 499 le_test_mode, 500 #else 501 NULL, 502 #endif 503 config_hci_snoop_log, 504 set_os_callouts, 505 read_energy_info, 506 dump, 507 config_clear, 508 interop_database_clear, 509 interop_database_add, 510 }; 511 512 const bt_interface_t* bluetooth__get_bluetooth_interface () 513 { 514 /* fixme -- add property to disable bt interface ? */ 515 516 return &bluetoothInterface; 517 } 518 519 static int close_bluetooth_stack(struct hw_device_t* device) 520 { 521 UNUSED(device); 522 cleanup(); 523 return 0; 524 } 525 526 static int open_bluetooth_stack(const struct hw_module_t *module, UNUSED_ATTR char const *name, struct hw_device_t **abstraction) { 527 static bluetooth_device_t device = { 528 .common = { 529 .tag = HARDWARE_DEVICE_TAG, 530 .version = 0, 531 .close = close_bluetooth_stack, 532 }, 533 .get_bluetooth_interface = bluetooth__get_bluetooth_interface 534 }; 535 536 device.common.module = (struct hw_module_t *)module; 537 *abstraction = (struct hw_device_t *)&device; 538 return 0; 539 } 540 541 static struct hw_module_methods_t bt_stack_module_methods = { 542 .open = open_bluetooth_stack, 543 }; 544 545 EXPORT_SYMBOL struct hw_module_t HAL_MODULE_INFO_SYM = { 546 .tag = HARDWARE_MODULE_TAG, 547 .version_major = 1, 548 .version_minor = 0, 549 .id = BT_HARDWARE_MODULE_ID, 550 .name = "Bluetooth Stack", 551 .author = "The Android Open Source Project", 552 .methods = &bt_stack_module_methods 553 }; 554