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