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