bluetooth.cc revision 0b4f3f7efb2b64e931cea7460c742628b8d0b8c0
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 osi_allocator_debug_dump(fd); 313 alarm_debug_dump(fd); 314#if (BTSNOOP_MEM == TRUE) 315 btif_debug_btsnoop_dump(fd); 316#endif 317 318 close(fd); 319} 320 321static const void* get_profile_interface(const char* profile_id) { 322 LOG_INFO(LOG_TAG, "%s: id = %s", __func__, profile_id); 323 324 /* sanity check */ 325 if (interface_ready() == false) return NULL; 326 327 /* check for supported profile interfaces */ 328 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID)) 329 return btif_hf_get_interface(); 330 331 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID)) 332 return btif_hf_client_get_interface(); 333 334 if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID)) 335 return btif_sock_get_interface(); 336 337 if (is_profile(profile_id, BT_PROFILE_PAN_ID)) 338 return btif_pan_get_interface(); 339 340 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID)) 341 return btif_av_get_src_interface(); 342 343 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID)) 344 return btif_av_get_sink_interface(); 345 346 if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID)) 347 return btif_hh_get_interface(); 348 349 if (is_profile(profile_id, BT_PROFILE_HIDDEV_ID)) 350 return btif_hd_get_interface(); 351 352 if (is_profile(profile_id, BT_PROFILE_HEALTH_ID)) 353 return btif_hl_get_interface(); 354 355 if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID)) 356 return btif_sdp_get_interface(); 357 358 if (is_profile(profile_id, BT_PROFILE_GATT_ID)) 359 return btif_gatt_get_interface(); 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, "%s", __func__); 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, "%s", __func__); 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 388int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) { 389 LOG_INFO(LOG_TAG, "%s", __func__); 390 391 /* sanity check */ 392 if (interface_ready() == false) return BT_STATUS_NOT_READY; 393 394 return btif_le_test_mode(opcode, buf, len); 395} 396 397int config_hci_snoop_log(uint8_t enable) { 398 LOG_INFO(LOG_TAG, "%s", __func__); 399 400 if (!interface_ready()) return BT_STATUS_NOT_READY; 401 402 btsnoop_get_interface()->set_api_wants_to_log(enable); 403 return BT_STATUS_SUCCESS; 404} 405 406static int set_os_callouts(bt_os_callouts_t* callouts) { 407 wakelock_set_os_callouts(callouts); 408 return BT_STATUS_SUCCESS; 409} 410 411static int config_clear(void) { 412 LOG_INFO(LOG_TAG, "%s", __func__); 413 return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 414} 415 416static const bt_interface_t bluetoothInterface = { 417 sizeof(bluetoothInterface), 418 init, 419 enable, 420 disable, 421 cleanup, 422 get_adapter_properties, 423 get_adapter_property, 424 set_adapter_property, 425 get_remote_device_properties, 426 get_remote_device_property, 427 set_remote_device_property, 428 get_remote_service_record, 429 get_remote_services, 430 start_discovery, 431 cancel_discovery, 432 create_bond, 433 create_bond_out_of_band, 434 remove_bond, 435 cancel_bond, 436 get_connection_state, 437 pin_reply, 438 ssp_reply, 439 get_profile_interface, 440 dut_mode_configure, 441 dut_mode_send, 442 le_test_mode, 443 config_hci_snoop_log, 444 set_os_callouts, 445 read_energy_info, 446 dump, 447 config_clear, 448 interop_database_clear, 449 interop_database_add, 450}; 451 452const bt_interface_t* bluetooth__get_bluetooth_interface() { 453 /* fixme -- add property to disable bt interface ? */ 454 455 return &bluetoothInterface; 456} 457 458static int close_bluetooth_stack(UNUSED_ATTR struct hw_device_t* device) { 459 cleanup(); 460 return 0; 461} 462 463static int open_bluetooth_stack(const struct hw_module_t* module, 464 UNUSED_ATTR char const* name, 465 struct hw_device_t** abstraction) { 466 static bluetooth_device_t device; 467 device.common.tag = HARDWARE_DEVICE_TAG; 468 device.common.version = 0; 469 device.common.close = close_bluetooth_stack; 470 device.get_bluetooth_interface = bluetooth__get_bluetooth_interface; 471 device.common.module = (struct hw_module_t*)module; 472 *abstraction = (struct hw_device_t*)&device; 473 return 0; 474} 475 476static struct hw_module_methods_t bt_stack_module_methods = { 477 .open = open_bluetooth_stack, 478}; 479 480EXPORT_SYMBOL struct hw_module_t HAL_MODULE_INFO_SYM = { 481 .tag = HARDWARE_MODULE_TAG, 482 .version_major = 1, 483 .version_minor = 0, 484 .id = BT_HARDWARE_MODULE_ID, 485 .name = "Bluetooth Stack", 486 .author = "The Android Open Source Project", 487 .methods = &bt_stack_module_methods}; 488