bluetooth.c revision 3e8a242fcbeebea2857fa964ca48624d8433333e
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#include <stdio.h> 28#include <stdlib.h> 29#include <unistd.h> 30 31#include <hardware/bluetooth.h> 32#include <hardware/bt_hf.h> 33#include <hardware/bt_hf_client.h> 34#include <hardware/bt_av.h> 35#include <hardware/bt_sock.h> 36#include <hardware/bt_hh.h> 37#include <hardware/bt_hl.h> 38#include <hardware/bt_pan.h> 39#include <hardware/bt_gatt.h> 40#include <hardware/bt_rc.h> 41 42#define LOG_NDDEBUG 0 43#define LOG_TAG "bluedroid" 44 45#include "btif_api.h" 46#include "bt_utils.h" 47 48/************************************************************************************ 49** Constants & Macros 50************************************************************************************/ 51 52#define is_profile(profile, str) ((strlen(str) == strlen(profile)) && strncmp((const char *)profile, str, strlen(str)) == 0) 53 54/************************************************************************************ 55** Local type definitions 56************************************************************************************/ 57 58/************************************************************************************ 59** Static variables 60************************************************************************************/ 61 62bt_callbacks_t *bt_hal_cbacks = NULL; 63 64/** Operating System specific callouts for resource management */ 65bt_os_callouts_t *bt_os_callouts = NULL; 66 67/************************************************************************************ 68** Static functions 69************************************************************************************/ 70 71/************************************************************************************ 72** Externs 73************************************************************************************/ 74 75/* list all extended interfaces here */ 76 77/* handsfree profile */ 78extern bthf_interface_t *btif_hf_get_interface(); 79/* handsfree profile - client */ 80extern bthf_client_interface_t *btif_hf_client_get_interface(); 81/* advanced audio profile */ 82extern btav_interface_t *btif_av_get_src_interface(); 83extern btav_interface_t *btif_av_get_sink_interface(); 84/*rfc l2cap*/ 85extern btsock_interface_t *btif_sock_get_interface(); 86/* hid host profile */ 87extern bthh_interface_t *btif_hh_get_interface(); 88/* health device profile */ 89extern bthl_interface_t *btif_hl_get_interface(); 90/*pan*/ 91extern btpan_interface_t *btif_pan_get_interface(); 92#if BLE_INCLUDED == TRUE 93/* gatt */ 94extern btgatt_interface_t *btif_gatt_get_interface(); 95#endif 96/* avrc */ 97extern btrc_interface_t *btif_rc_get_interface(); 98 99/************************************************************************************ 100** Functions 101************************************************************************************/ 102 103static uint8_t interface_ready(void) 104{ 105 /* add checks here that would prevent API calls other than init to be executed */ 106 if (bt_hal_cbacks == NULL) 107 return FALSE; 108 109 return TRUE; 110} 111 112 113/***************************************************************************** 114** 115** BLUETOOTH HAL INTERFACE FUNCTIONS 116** 117*****************************************************************************/ 118 119static int init(bt_callbacks_t* callbacks ) 120{ 121 ALOGI("init"); 122 123 /* sanity check */ 124 if (interface_ready() == TRUE) 125 return BT_STATUS_DONE; 126 127 /* store reference to user callbacks */ 128 bt_hal_cbacks = callbacks; 129 130 /* add checks for individual callbacks ? */ 131 132 bt_utils_init(); 133 134 /* init btif */ 135 btif_init_bluetooth(); 136 137 return BT_STATUS_SUCCESS; 138} 139 140static int enable( void ) 141{ 142 ALOGI("enable"); 143 144 /* sanity check */ 145 if (interface_ready() == FALSE) 146 return BT_STATUS_NOT_READY; 147 148 return btif_enable_bluetooth(); 149} 150 151static int disable(void) 152{ 153 /* sanity check */ 154 if (interface_ready() == FALSE) 155 return BT_STATUS_NOT_READY; 156 157 return btif_disable_bluetooth(); 158} 159 160static void cleanup( void ) 161{ 162 /* sanity check */ 163 if (interface_ready() == FALSE) 164 return; 165 166 btif_shutdown_bluetooth(); 167 168 /* hal callbacks reset upon shutdown complete callback */ 169 170 return; 171} 172 173static int get_adapter_properties(void) 174{ 175 /* sanity check */ 176 if (interface_ready() == FALSE) 177 return BT_STATUS_NOT_READY; 178 179 return btif_get_adapter_properties(); 180} 181 182static int get_adapter_property(bt_property_type_t type) 183{ 184 /* sanity check */ 185 if (interface_ready() == FALSE) 186 return BT_STATUS_NOT_READY; 187 188 return btif_get_adapter_property(type); 189} 190 191static int set_adapter_property(const bt_property_t *property) 192{ 193 /* sanity check */ 194 if (interface_ready() == FALSE) 195 return BT_STATUS_NOT_READY; 196 197 return btif_set_adapter_property(property); 198} 199 200int get_remote_device_properties(bt_bdaddr_t *remote_addr) 201{ 202 /* sanity check */ 203 if (interface_ready() == FALSE) 204 return BT_STATUS_NOT_READY; 205 206 return btif_get_remote_device_properties(remote_addr); 207} 208 209int get_remote_device_property(bt_bdaddr_t *remote_addr, bt_property_type_t type) 210{ 211 /* sanity check */ 212 if (interface_ready() == FALSE) 213 return BT_STATUS_NOT_READY; 214 215 return btif_get_remote_device_property(remote_addr, type); 216} 217 218int set_remote_device_property(bt_bdaddr_t *remote_addr, const bt_property_t *property) 219{ 220 /* sanity check */ 221 if (interface_ready() == FALSE) 222 return BT_STATUS_NOT_READY; 223 224 return btif_set_remote_device_property(remote_addr, property); 225} 226 227int get_remote_service_record(bt_bdaddr_t *remote_addr, bt_uuid_t *uuid) 228{ 229 /* sanity check */ 230 if (interface_ready() == FALSE) 231 return BT_STATUS_NOT_READY; 232 233 return btif_get_remote_service_record(remote_addr, uuid); 234} 235 236int get_remote_services(bt_bdaddr_t *remote_addr) 237{ 238 /* sanity check */ 239 if (interface_ready() == FALSE) 240 return BT_STATUS_NOT_READY; 241 242 return btif_dm_get_remote_services(remote_addr); 243} 244 245static int start_discovery(void) 246{ 247 /* sanity check */ 248 if (interface_ready() == FALSE) 249 return BT_STATUS_NOT_READY; 250 251 return btif_dm_start_discovery(); 252} 253 254static int cancel_discovery(void) 255{ 256 /* sanity check */ 257 if (interface_ready() == FALSE) 258 return BT_STATUS_NOT_READY; 259 260 return btif_dm_cancel_discovery(); 261} 262 263static int create_bond(const bt_bdaddr_t *bd_addr) 264{ 265 /* sanity check */ 266 if (interface_ready() == FALSE) 267 return BT_STATUS_NOT_READY; 268 269 return btif_dm_create_bond(bd_addr); 270} 271 272static int cancel_bond(const bt_bdaddr_t *bd_addr) 273{ 274 /* sanity check */ 275 if (interface_ready() == FALSE) 276 return BT_STATUS_NOT_READY; 277 278 return btif_dm_cancel_bond(bd_addr); 279} 280 281static int remove_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_remove_bond(bd_addr); 288} 289 290static int pin_reply(const bt_bdaddr_t *bd_addr, uint8_t accept, 291 uint8_t pin_len, bt_pin_code_t *pin_code) 292{ 293 /* sanity check */ 294 if (interface_ready() == FALSE) 295 return BT_STATUS_NOT_READY; 296 297 return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code); 298} 299 300static int ssp_reply(const bt_bdaddr_t *bd_addr, bt_ssp_variant_t variant, 301 uint8_t accept, uint32_t passkey) 302{ 303 /* sanity check */ 304 if (interface_ready() == FALSE) 305 return BT_STATUS_NOT_READY; 306 307 return btif_dm_ssp_reply(bd_addr, variant, accept, passkey); 308} 309 310static const void* get_profile_interface (const char *profile_id) 311{ 312 ALOGI("get_profile_interface %s", profile_id); 313 314 /* sanity check */ 315 if (interface_ready() == FALSE) 316 return NULL; 317 318 /* check for supported profile interfaces */ 319 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID)) 320 return btif_hf_get_interface(); 321 322 if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID)) 323 return btif_hf_client_get_interface(); 324 325 if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID)) 326 return btif_sock_get_interface(); 327 328 if (is_profile(profile_id, BT_PROFILE_PAN_ID)) 329 return btif_pan_get_interface(); 330 331 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID)) 332 return btif_av_get_src_interface(); 333 334 if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID)) 335 return btif_av_get_sink_interface(); 336 337 if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID)) 338 return btif_hh_get_interface(); 339 340 if (is_profile(profile_id, BT_PROFILE_HEALTH_ID)) 341 return btif_hl_get_interface(); 342 343#if ( BTA_GATT_INCLUDED == TRUE &&BLE_INCLUDED == TRUE) 344 if (is_profile(profile_id, BT_PROFILE_GATT_ID)) 345 return btif_gatt_get_interface(); 346#endif 347 348 if (is_profile(profile_id, BT_PROFILE_AV_RC_ID)) 349 return btif_rc_get_interface(); 350 351 return NULL; 352} 353 354int dut_mode_configure(uint8_t enable) 355{ 356 ALOGI("dut_mode_configure"); 357 358 /* sanity check */ 359 if (interface_ready() == FALSE) 360 return BT_STATUS_NOT_READY; 361 362 return btif_dut_mode_configure(enable); 363} 364 365int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) 366{ 367 ALOGI("dut_mode_send"); 368 369 /* sanity check */ 370 if (interface_ready() == FALSE) 371 return BT_STATUS_NOT_READY; 372 373 return btif_dut_mode_send(opcode, buf, len); 374} 375 376#if BLE_INCLUDED == TRUE 377int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) 378{ 379 ALOGI("le_test_mode"); 380 381 /* sanity check */ 382 if (interface_ready() == FALSE) 383 return BT_STATUS_NOT_READY; 384 385 return btif_le_test_mode(opcode, buf, len); 386} 387#endif 388 389int config_hci_snoop_log(uint8_t enable) 390{ 391 ALOGI("config_hci_snoop_log"); 392 393 /* sanity check */ 394 if (interface_ready() == FALSE) 395 return BT_STATUS_NOT_READY; 396 397 return btif_config_hci_snoop_log(enable); 398} 399 400static int set_os_callouts(bt_os_callouts_t *callouts) { 401 bt_os_callouts = callouts; 402 return BT_STATUS_SUCCESS; 403} 404 405static const bt_interface_t bluetoothInterface = { 406 sizeof(bluetoothInterface), 407 init, 408 enable, 409 disable, 410 cleanup, 411 get_adapter_properties, 412 get_adapter_property, 413 set_adapter_property, 414 get_remote_device_properties, 415 get_remote_device_property, 416 set_remote_device_property, 417 get_remote_service_record, 418 get_remote_services, 419 start_discovery, 420 cancel_discovery, 421 create_bond, 422 remove_bond, 423 cancel_bond, 424 pin_reply, 425 ssp_reply, 426 get_profile_interface, 427 dut_mode_configure, 428 dut_mode_send, 429#if BLE_INCLUDED == TRUE 430 le_test_mode, 431#else 432 NULL, 433#endif 434 config_hci_snoop_log, 435 set_os_callouts, 436}; 437 438const bt_interface_t* bluetooth__get_bluetooth_interface () 439{ 440 /* fixme -- add property to disable bt interface ? */ 441 442 return &bluetoothInterface; 443} 444 445static int close_bluetooth_stack(struct hw_device_t* device) 446{ 447 UNUSED(device); 448 cleanup(); 449 return 0; 450} 451 452static int open_bluetooth_stack (const struct hw_module_t* module, char const* name, 453 struct hw_device_t** abstraction) 454{ 455 UNUSED(name); 456 457 bluetooth_device_t *stack = malloc(sizeof(bluetooth_device_t) ); 458 memset(stack, 0, sizeof(bluetooth_device_t) ); 459 stack->common.tag = HARDWARE_DEVICE_TAG; 460 stack->common.version = 0; 461 stack->common.module = (struct hw_module_t*)module; 462 stack->common.close = close_bluetooth_stack; 463 stack->get_bluetooth_interface = bluetooth__get_bluetooth_interface; 464 *abstraction = (struct hw_device_t*)stack; 465 return 0; 466} 467 468 469static struct hw_module_methods_t bt_stack_module_methods = { 470 .open = open_bluetooth_stack, 471}; 472 473struct hw_module_t HAL_MODULE_INFO_SYM = { 474 .tag = HARDWARE_MODULE_TAG, 475 .version_major = 1, 476 .version_minor = 0, 477 .id = BT_HARDWARE_MODULE_ID, 478 .name = "Bluetooth Stack", 479 .author = "The Android Open Source Project", 480 .methods = &bt_stack_module_methods 481}; 482 483