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