btif_dm.c revision 95664a9acc57b5e51365454f26d2fac0f2e1ecd4
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: btif_dm.c 22 * 23 * Description: Contains Device Management (DM) related functionality 24 * 25 * 26 ***********************************************************************************/ 27#include <stdio.h> 28#include <stdlib.h> 29#include <unistd.h> 30 31#include <hardware/bluetooth.h> 32 33#include <utils/Log.h> 34#include <cutils/properties.h> 35#include "gki.h" 36#include "btu.h" 37#include "bd.h" 38#include "bta_api.h" 39#include "btif_api.h" 40#include "btif_util.h" 41#include "btif_dm.h" 42#include "btif_storage.h" 43#include "btif_hh.h" 44#include "btif_config.h" 45 46#include "bta_gatt_api.h" 47/****************************************************************************** 48** Constants & Macros 49******************************************************************************/ 50 51#define COD_UNCLASSIFIED ((0x1F) << 8) 52#define COD_HID_KEYBOARD 0x0540 53#define COD_HID_POINTING 0x0580 54#define COD_HID_COMBO 0x05C0 55#define COD_HID_MAJOR 0x0500 56#define COD_AV_HEADSETS 0x0404 57#define COD_AV_HANDSFREE 0x0408 58#define COD_AV_HEADPHONES 0x0418 59#define COD_AV_PORTABLE_AUDIO 0x041C 60#define COD_AV_HIFI_AUDIO 0x0428 61 62 63#define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0 64#define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10 65 66 67 68typedef struct 69{ 70 bt_bond_state_t state; 71 BD_ADDR bd_addr; 72 UINT8 is_temp; 73 UINT8 pin_code_len; 74 UINT8 is_ssp; 75 UINT8 autopair_attempts; 76 UINT8 is_local_initiated; 77 UINT8 bonded_pending_sdp; 78#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 79 BOOLEAN is_le_only; 80 btif_dm_ble_cb_t ble; 81#endif 82} btif_dm_pairing_cb_t; 83 84 85typedef struct 86{ 87 UINT8 ir[BT_OCTET16_LEN]; 88 UINT8 irk[BT_OCTET16_LEN]; 89 UINT8 dhk[BT_OCTET16_LEN]; 90}btif_dm_local_key_id_t; 91 92typedef struct 93{ 94 BOOLEAN is_er_rcvd; 95 UINT8 er[BT_OCTET16_LEN]; 96 BOOLEAN is_id_keys_rcvd; 97 btif_dm_local_key_id_t id_keys; /* ID kyes */ 98 99}btif_dm_local_key_cb_t; 100 101typedef struct 102{ 103 BD_ADDR bd_addr; 104 BD_NAME bd_name; 105} btif_dm_remote_name_t; 106 107typedef struct 108{ 109 BT_OCTET16 sp_c; 110 BT_OCTET16 sp_r; 111 BD_ADDR oob_bdaddr; /* peer bdaddr*/ 112} btif_dm_oob_cb_t; 113#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id)) 114 115/* This flag will be true if HCI_Inquiry is in progress */ 116static BOOLEAN btif_dm_inquiry_in_progress = FALSE; 117 118/****************************************************************************** 119** Static functions 120******************************************************************************/ 121static btif_dm_pairing_cb_t pairing_cb; 122static btif_dm_oob_cb_t oob_cb; 123static void btif_dm_generic_evt(UINT16 event, char* p_param); 124static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr); 125static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name); 126static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name, 127 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type); 128#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 129static btif_dm_local_key_cb_t ble_local_key_cb; 130static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif); 131static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl); 132static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req); 133#endif 134/****************************************************************************** 135** Externs 136******************************************************************************/ 137extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID]; 138extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable); 139extern bt_status_t btif_av_execute_service(BOOLEAN b_enable); 140extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable); 141extern int btif_hh_connect(bt_bdaddr_t *bd_addr); 142 143 144/****************************************************************************** 145** Functions 146******************************************************************************/ 147 148bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id, 149 BOOLEAN b_enable) 150{ 151 /* Check the service_ID and invoke the profile's BT state changed API */ 152 switch (service_id) 153 { 154 case BTA_HFP_SERVICE_ID: 155 case BTA_HSP_SERVICE_ID: 156 { 157 btif_hf_execute_service(b_enable); 158 }break; 159 case BTA_A2DP_SERVICE_ID: 160 { 161 btif_av_execute_service(b_enable); 162 }break; 163 case BTA_HID_SERVICE_ID: 164 { 165 btif_hh_execute_service(b_enable); 166 }break; 167 168 default: 169 BTIF_TRACE_ERROR1("%s: Unknown service being enabled", __FUNCTION__); 170 return BT_STATUS_FAIL; 171 } 172 return BT_STATUS_SUCCESS; 173} 174 175/******************************************************************************* 176** 177** Function check_eir_remote_name 178** 179** Description Check if remote name is in the EIR data 180** 181** Returns TRUE if remote name found 182** Populate p_remote_name, if provided and remote name found 183** 184*******************************************************************************/ 185static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data, 186 UINT8 *p_remote_name, UINT8 *p_remote_name_len) 187{ 188 UINT8 *p_eir_remote_name = NULL; 189 UINT8 remote_name_len = 0; 190 191 /* Check EIR for remote name and services */ 192 if (p_search_data->inq_res.p_eir) 193 { 194 p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir, 195 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len); 196 if (!p_eir_remote_name) 197 { 198 p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir, 199 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len); 200 } 201 202 if (p_eir_remote_name) 203 { 204 if (remote_name_len > BD_NAME_LEN) 205 remote_name_len = BD_NAME_LEN; 206 207 if (p_remote_name && p_remote_name_len) 208 { 209 memcpy(p_remote_name, p_eir_remote_name, remote_name_len); 210 *(p_remote_name + remote_name_len) = 0; 211 *p_remote_name_len = remote_name_len; 212 } 213 214 return TRUE; 215 } 216 } 217 218 return FALSE; 219 220} 221 222/******************************************************************************* 223** 224** Function check_cached_remote_name 225** 226** Description Check if remote name is in the NVRAM cache 227** 228** Returns TRUE if remote name found 229** Populate p_remote_name, if provided and remote name found 230** 231*******************************************************************************/ 232static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data, 233 UINT8 *p_remote_name, UINT8 *p_remote_name_len) 234{ 235 bt_bdname_t bdname; 236 bt_bdaddr_t remote_bdaddr; 237 bt_property_t prop_name; 238 239 /* check if we already have it in our btif_storage cache */ 240 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr); 241 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME, 242 sizeof(bt_bdname_t), &bdname); 243 if (btif_storage_get_remote_device_property( 244 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS) 245 { 246 if (p_remote_name && p_remote_name_len) 247 { 248 strcpy((char *)p_remote_name, (char *)bdname.name); 249 *p_remote_name_len = strlen((char *)p_remote_name); 250 } 251 return TRUE; 252 } 253 254 return FALSE; 255} 256 257BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod) 258{ 259 uint32_t remote_cod; 260 bt_property_t prop_name; 261 262 /* check if we already have it in our btif_storage cache */ 263 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE, 264 sizeof(uint32_t), &remote_cod); 265 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS) 266 { 267 if ((remote_cod & 0x7ff) == cod) 268 return TRUE; 269 } 270 271 return FALSE; 272} 273 274BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod) 275{ 276 uint32_t remote_cod; 277 bt_property_t prop_name; 278 279 /* check if we already have it in our btif_storage cache */ 280 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE, 281 sizeof(uint32_t), &remote_cod); 282 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, 283 &prop_name) == BT_STATUS_SUCCESS) 284 { 285 if ((remote_cod & 0x700) == cod) 286 return TRUE; 287 } 288 289 return FALSE; 290} 291 292static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state) 293{ 294 /* Send bonding state only once - based on outgoing/incoming we may receive duplicates */ 295 if ( (pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING) ) 296 return; 297 298 if (pairing_cb.is_temp) 299 { 300 state = BT_BOND_STATE_NONE; 301 } 302 BTIF_TRACE_DEBUG3("%s: state=%d prev_state=%d", __FUNCTION__, state, pairing_cb.state); 303 304 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state); 305 306 if (state == BT_BOND_STATE_BONDING) 307 { 308 pairing_cb.state = state; 309 bdcpy(pairing_cb.bd_addr, bd_addr->address); 310 } 311 else 312 { 313 memset(&pairing_cb, 0, sizeof(pairing_cb)); 314 } 315 316} 317 318/* store remote version in bt config to always have access 319 to it post pairing*/ 320static void btif_update_remote_version_property(bt_bdaddr_t *p_bd) 321{ 322 bt_property_t property; 323 UINT8 lmp_ver = 0; 324 UINT16 lmp_subver = 0; 325 UINT16 mfct_set = 0; 326 tBTM_STATUS btm_status; 327 bt_remote_version_t info; 328 bt_status_t status; 329 bdstr_t bdstr; 330 331 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver, 332 &mfct_set, &lmp_subver); 333 334 ALOGD("remote version info [%s]: %x, %x, %x", bd2str(p_bd, &bdstr), 335 lmp_ver, mfct_set, lmp_subver); 336 337 if (btm_status == BTM_SUCCESS) 338 { 339 /* always update cache to ensure we have availability whenever BTM API 340 is not populated */ 341 info.manufacturer = mfct_set; 342 info.sub_ver = lmp_subver; 343 info.version = lmp_ver; 344 BTIF_STORAGE_FILL_PROPERTY(&property, 345 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t), 346 &info); 347 status = btif_storage_set_remote_device_property(p_bd, &property); 348 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status); 349 } 350} 351 352 353static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name, 354 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type) 355{ 356 int num_properties = 0; 357 bt_property_t properties[3]; 358 bt_bdaddr_t bdaddr; 359 bt_status_t status; 360 UINT32 cod; 361 bt_device_type_t dev_type; 362 363 memset(properties, 0, sizeof(properties)); 364 bdcpy(bdaddr.address, bd_addr); 365 366 /* remote name */ 367 if (strlen((const char *) bd_name)) 368 { 369 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 370 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name); 371 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]); 372 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status); 373 num_properties++; 374 } 375 376 /* class of device */ 377 cod = devclass2uint(dev_class); 378 if ( cod == 0) { 379 BTIF_TRACE_DEBUG1("%s():cod is 0, set as unclassified", __FUNCTION__); 380 cod = COD_UNCLASSIFIED; 381 } 382 383 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 384 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod); 385 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]); 386 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status); 387 num_properties++; 388 389 /* device type */ 390 dev_type = device_type; 391 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 392 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type); 393 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]); 394 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status); 395 num_properties++; 396 397 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, 398 status, &bdaddr, num_properties, properties); 399} 400/******************************************************************************* 401** 402** Function hid_remote_name_cback 403** 404** Description Remote name callback for HID device. Called in stack context 405** Special handling for HID devices 406** 407** Returns void 408** 409*******************************************************************************/ 410static void hid_remote_name_cback(void *p_param) 411{ 412 BTIF_TRACE_DEBUG1("%s", __FUNCTION__); 413 414 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_HID_REMOTE_NAME, 415 (char *)p_param, sizeof(tBTM_REMOTE_DEV_NAME), NULL); 416} 417 418/******************************************************************************* 419** 420** Function btif_dm_cb_hid_remote_name 421** 422** Description Remote name callback for HID device. Called in btif context 423** Special handling for HID devices 424** 425** Returns void 426** 427*******************************************************************************/ 428static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name) 429{ 430 BTIF_TRACE_DEBUG3("%s: status=%d pairing_cb.state=%d", __FUNCTION__, p_remote_name->status, pairing_cb.state); 431 if (pairing_cb.state == BT_BOND_STATE_BONDING) 432 { 433 bt_bdaddr_t remote_bd; 434 435 bdcpy(remote_bd.address, pairing_cb.bd_addr); 436 437 if (p_remote_name->status == BTM_SUCCESS) 438 { 439 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED); 440 } 441 else 442 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE); 443 } 444} 445 446int remove_hid_bond(bt_bdaddr_t *bd_addr) 447{ 448 /* For HID device, inorder to avoid the HID device from re-connecting again after unpairing, 449 * we need to do virtual unplug 450 */ 451 bdstr_t bdstr; 452 BTIF_TRACE_DEBUG2("%s---Removing HID bond--%s", __FUNCTION__,bd2str((bt_bdaddr_t *)bd_addr, &bdstr)); 453 return btif_hh_virtual_unplug(bd_addr); 454 455} 456/******************************************************************************* 457** 458** Function btif_dm_cb_create_bond 459** 460** Description Create bond initiated from the BTIF thread context 461** Special handling for HID devices 462** 463** Returns void 464** 465*******************************************************************************/ 466static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr) 467{ 468 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING); 469 470 471 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING); 472 473 if (is_hid){ 474 475 int status; 476 status = btif_hh_connect(bd_addr); 477 if(status != BT_STATUS_SUCCESS) 478 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE); 479 } 480 else 481 { 482 int device_type; 483 int addr_type; 484 bdstr_t bdstr; 485 bd2str(bd_addr, &bdstr); 486 if(btif_config_get_int("Remote", (char const *)&bdstr,"DevType", &device_type) && 487 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) && 488 (device_type == BT_DEVICE_TYPE_BLE)) 489 { 490 BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE); 491 } 492 BTA_DmBond ((UINT8 *)bd_addr->address); 493 } 494 /* Track originator of bond creation */ 495 pairing_cb.is_local_initiated = TRUE; 496 497} 498 499/******************************************************************************* 500** 501** Function btif_dm_cb_remove_bond 502** 503** Description remove bond initiated from the BTIF thread context 504** Special handling for HID devices 505** 506** Returns void 507** 508*******************************************************************************/ 509void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr) 510{ 511 bdstr_t bdstr; 512 /*special handling for HID devices */ 513 if (check_cod_hid(bd_addr, COD_HID_MAJOR)) 514 { 515 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE)) 516 if(remove_hid_bond(bd_addr) != BTA_SUCCESS) 517 BTA_DmRemoveDevice((UINT8 *)bd_addr->address); 518 #endif 519 } 520 else 521 { 522 if (BTA_DmRemoveDevice((UINT8 *)bd_addr->address) == BTA_SUCCESS) 523 { 524 BTIF_TRACE_DEBUG1("Successfully removed bonding with device: %s", 525 bd2str((bt_bdaddr_t *)bd_addr, &bdstr)); 526 } 527 else 528 BTIF_TRACE_DEBUG1("Removed bonding with device failed: %s", 529 bd2str((bt_bdaddr_t *)bd_addr, &bdstr)); 530 } 531} 532 533/******************************************************************************* 534** 535** Function search_devices_copy_cb 536** 537** Description Deep copy callback for search devices event 538** 539** Returns void 540** 541*******************************************************************************/ 542static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src) 543{ 544 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest; 545 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src; 546 547 if (!p_src) 548 return; 549 550 BTIF_TRACE_DEBUG2("%s: event=%s", __FUNCTION__, dump_dm_search_event(event)); 551 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH)); 552 switch (event) 553 { 554 case BTA_DM_INQ_RES_EVT: 555 { 556 if (p_src_data->inq_res.p_eir) 557 { 558 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH)); 559 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN); 560 } 561 } 562 break; 563 564 case BTA_DM_DISC_RES_EVT: 565 { 566 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data) 567 { 568 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH)); 569 memcpy(p_dest_data->disc_res.p_raw_data, 570 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size); 571 } 572 } 573 break; 574 } 575} 576 577static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src) 578{ 579 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest; 580 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src; 581 582 if (!p_src) 583 return; 584 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH)); 585 switch (event) 586 { 587 case BTA_DM_DISC_RES_EVT: 588 { 589 if (p_src_data->disc_res.result == BTA_SUCCESS) 590 { 591 if (p_src_data->disc_res.num_uuids > 0) 592 { 593 p_dest_data->disc_res.p_uuid_list = 594 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH)); 595 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list, 596 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE); 597 GKI_freebuf(p_src_data->disc_res.p_uuid_list); 598 } 599 if (p_src_data->disc_res.p_raw_data != NULL) 600 { 601 GKI_freebuf(p_src_data->disc_res.p_raw_data); 602 } 603 } 604 } break; 605 } 606} 607/****************************************************************************** 608** 609** BTIF DM callback events 610** 611*****************************************************************************/ 612 613/******************************************************************************* 614** 615** Function btif_dm_pin_req_evt 616** 617** Description Executes pin request event in btif context 618** 619** Returns void 620** 621*******************************************************************************/ 622static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req) 623{ 624 bt_bdaddr_t bd_addr; 625 bt_bdname_t bd_name; 626 UINT32 cod; 627 bt_pin_code_t pin_code; 628 629 /* Remote properties update */ 630 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name, 631 p_pin_req->dev_class, BT_DEVICE_TYPE_BREDR); 632 633 bdcpy(bd_addr.address, p_pin_req->bd_addr); 634 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN); 635 636 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); 637 638 cod = devclass2uint(p_pin_req->dev_class); 639 640 if ( cod == 0) { 641 BTIF_TRACE_DEBUG1("%s():cod is 0, set as unclassified", __FUNCTION__); 642 cod = COD_UNCLASSIFIED; 643 } 644 645 /* check for auto pair possiblity only if bond was initiated by local device */ 646 if (pairing_cb.is_local_initiated) 647 { 648 if (check_cod(&bd_addr, COD_AV_HEADSETS) || 649 check_cod(&bd_addr, COD_AV_HANDSFREE) || 650 check_cod(&bd_addr, COD_AV_HEADPHONES) || 651 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) || 652 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) || 653 check_cod(&bd_addr, COD_HID_POINTING)) 654 { 655 BTIF_TRACE_DEBUG1("%s()cod matches for auto pair", __FUNCTION__); 656 /* Check if this device can be auto paired */ 657 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) && 658 (pairing_cb.autopair_attempts == 0)) 659 { 660 BTIF_TRACE_DEBUG1("%s() Attempting auto pair", __FUNCTION__); 661 pin_code.pin[0] = 0x30; 662 pin_code.pin[1] = 0x30; 663 pin_code.pin[2] = 0x30; 664 pin_code.pin[3] = 0x30; 665 666 pairing_cb.autopair_attempts++; 667 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin); 668 return; 669 } 670 } 671 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) || 672 check_cod(&bd_addr, COD_HID_COMBO)) 673 { 674 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) && 675 (pairing_cb.autopair_attempts == 0)) 676 { 677 BTIF_TRACE_DEBUG1("%s() Attempting auto pair", __FUNCTION__); 678 pin_code.pin[0] = 0x30; 679 pin_code.pin[1] = 0x30; 680 pin_code.pin[2] = 0x30; 681 pin_code.pin[3] = 0x30; 682 683 pairing_cb.autopair_attempts++; 684 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin); 685 return; 686 } 687 } 688 } 689 HAL_CBACK(bt_hal_cbacks, pin_request_cb, 690 &bd_addr, &bd_name, cod); 691} 692 693/******************************************************************************* 694** 695** Function btif_dm_ssp_cfm_req_evt 696** 697** Description Executes SSP confirm request event in btif context 698** 699** Returns void 700** 701*******************************************************************************/ 702static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req) 703{ 704 bt_bdaddr_t bd_addr; 705 bt_bdname_t bd_name; 706 UINT32 cod; 707 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING); 708 709 BTIF_TRACE_DEBUG1("%s", __FUNCTION__); 710 711 /* Remote properties update */ 712 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name, 713 p_ssp_cfm_req->dev_class, BT_DEVICE_TYPE_BREDR); 714 715 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr); 716 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN); 717 718 /* Set the pairing_cb based on the local & remote authentication requirements */ 719 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); 720 721 /* if just_works and bonding bit is not set treat this as temporary */ 722 if (p_ssp_cfm_req->just_works && !(p_ssp_cfm_req->loc_auth_req & BTM_AUTH_BONDS) && 723 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) && 724 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING))) 725 pairing_cb.is_temp = TRUE; 726 else 727 pairing_cb.is_temp = FALSE; 728 729 pairing_cb.is_ssp = TRUE; 730 731 /* If JustWorks auto-accept */ 732 if (p_ssp_cfm_req->just_works) 733 { 734 /* Pairing consent for JustWorks needed if: 735 * 1. Incoming pairing is detected AND 736 * 2. local IO capabilities are DisplayYesNo AND 737 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput; 738 */ 739 if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) && 740 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03))) 741 { 742 BTIF_TRACE_EVENT3("%s: User consent needed for incoming pairing request. loc_io_caps: %d, rmt_io_caps: %d", 743 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps); 744 } 745 else 746 { 747 BTIF_TRACE_EVENT1("%s: Auto-accept JustWorks pairing", __FUNCTION__); 748 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0); 749 return; 750 } 751 } 752 753 cod = devclass2uint(p_ssp_cfm_req->dev_class); 754 755 if ( cod == 0) { 756 ALOGD("cod is 0, set as unclassified"); 757 cod = COD_UNCLASSIFIED; 758 } 759 760 pairing_cb.bonded_pending_sdp = FALSE; 761 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod, 762 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION), 763 p_ssp_cfm_req->num_val); 764} 765 766static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif) 767{ 768 bt_bdaddr_t bd_addr; 769 bt_bdname_t bd_name; 770 UINT32 cod; 771 772 BTIF_TRACE_DEBUG1("%s", __FUNCTION__); 773 774 /* Remote properties update */ 775 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name, 776 p_ssp_key_notif->dev_class, BT_DEVICE_TYPE_BREDR); 777 778 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr); 779 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN); 780 781 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); 782 pairing_cb.is_ssp = TRUE; 783 cod = devclass2uint(p_ssp_key_notif->dev_class); 784 785 if ( cod == 0) { 786 ALOGD("cod is 0, set as unclassified"); 787 cod = COD_UNCLASSIFIED; 788 } 789 790 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, 791 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION, 792 p_ssp_key_notif->passkey); 793} 794/******************************************************************************* 795** 796** Function btif_dm_auth_cmpl_evt 797** 798** Description Executes authentication complete event in btif context 799** 800** Returns void 801** 802*******************************************************************************/ 803static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) 804{ 805 /* Save link key, if not temporary */ 806 bt_bdaddr_t bd_addr; 807 bt_status_t status = BT_STATUS_FAIL; 808 bt_bond_state_t state = BT_BOND_STATE_NONE; 809 810 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr); 811 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) ) 812 { 813 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) || (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) || 814 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || (!pairing_cb.is_temp)) 815 { 816 bt_status_t ret; 817 BTIF_TRACE_DEBUG3("%s: Storing link key. key_type=0x%x, is_temp=%d", 818 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp); 819 ret = btif_storage_add_bonded_device(&bd_addr, 820 p_auth_cmpl->key, p_auth_cmpl->key_type, 821 pairing_cb.pin_code_len); 822 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret); 823 } 824 else 825 { 826 BTIF_TRACE_DEBUG3("%s: Temporary key. Not storing. key_type=0x%x, is_temp=%d", 827 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp); 828 } 829 } 830 if (p_auth_cmpl->success) 831 { 832 status = BT_STATUS_SUCCESS; 833 state = BT_BOND_STATE_BONDED; 834 835 /* Trigger SDP on the device */ 836 pairing_cb.bonded_pending_sdp = TRUE; 837 838 if(btif_dm_inquiry_in_progress) 839 btif_dm_cancel_discovery(); 840 841 btif_dm_get_remote_services(&bd_addr); 842 /* Do not call bond_state_changed_cb yet. Wait till fetch remote service is complete */ 843 } 844 else 845 { 846 /*Map the HCI fail reason to bt status */ 847 switch(p_auth_cmpl->fail_reason) 848 { 849 case HCI_ERR_PAGE_TIMEOUT: 850 case HCI_ERR_CONNECTION_TOUT: 851 status = BT_STATUS_RMT_DEV_DOWN; 852 break; 853 854 /* map the auth failure codes, so we can retry pairing if necessary */ 855 case HCI_ERR_AUTH_FAILURE: 856 case HCI_ERR_HOST_REJECT_SECURITY: 857 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE: 858 case HCI_ERR_UNIT_KEY_USED: 859 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED: 860 case HCI_ERR_INSUFFCIENT_SECURITY: 861 BTIF_TRACE_DEBUG1(" %s() Authentication fail ", __FUNCTION__); 862 if (pairing_cb.autopair_attempts == 1) 863 { 864 BTIF_TRACE_DEBUG1("%s(): Adding device to blacklist ", __FUNCTION__); 865 866 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */ 867 if (check_cod(&bd_addr, COD_AV_HEADSETS) || 868 check_cod(&bd_addr, COD_AV_HANDSFREE) || 869 check_cod(&bd_addr, COD_AV_HEADPHONES) || 870 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) || 871 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) || 872 check_cod(&bd_addr, COD_HID_POINTING)) 873 { 874 btif_storage_add_device_to_autopair_blacklist (&bd_addr); 875 } 876 pairing_cb.autopair_attempts++; 877 878 /* Create the Bond once again */ 879 BTIF_TRACE_DEBUG1("%s() auto pair failed. Reinitiate Bond", __FUNCTION__); 880 btif_dm_cb_create_bond (&bd_addr); 881 return; 882 } 883 else 884 { 885 /* if autopair attempts are more than 1, or not attempted */ 886 status = BT_STATUS_AUTH_FAILURE; 887 } 888 break; 889 890 default: 891 status = BT_STATUS_FAIL; 892 } 893 bond_state_changed(status, &bd_addr, state); 894 } 895} 896 897/****************************************************************************** 898** 899** Function btif_dm_search_devices_evt 900** 901** Description Executes search devices callback events in btif context 902** 903** Returns void 904** 905******************************************************************************/ 906static void btif_dm_search_devices_evt (UINT16 event, char *p_param) 907{ 908 tBTA_DM_SEARCH *p_search_data; 909 BTIF_TRACE_EVENT2("%s event=%s", __FUNCTION__, dump_dm_search_event(event)); 910 911 switch (event) 912 { 913 case BTA_DM_DISC_RES_EVT: 914 { 915 p_search_data = (tBTA_DM_SEARCH *)p_param; 916 /* Remote name update */ 917 if (strlen((const char *) p_search_data->disc_res.bd_name)) 918 { 919 bt_property_t properties[1]; 920 bt_bdaddr_t bdaddr; 921 bt_status_t status; 922 923 properties[0].type = BT_PROPERTY_BDNAME; 924 properties[0].val = p_search_data->disc_res.bd_name; 925 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name); 926 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr); 927 928 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]); 929 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status); 930 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, 931 status, &bdaddr, 1, properties); 932 } 933 /* TODO: Services? */ 934 } 935 break; 936 937 case BTA_DM_INQ_RES_EVT: 938 { 939 /* inquiry result */ 940 UINT32 cod; 941 UINT8 *p_eir_remote_name = NULL; 942 bt_bdname_t bdname; 943 bt_bdaddr_t bdaddr; 944 UINT8 remote_name_len; 945 UINT8 *p_cached_name = NULL; 946 tBTA_SERVICE_MASK services = 0; 947 bdstr_t bdstr; 948 949 p_search_data = (tBTA_DM_SEARCH *)p_param; 950 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr); 951 952 BTIF_TRACE_DEBUG3("%s() %s device_type = 0x%x\n", __FUNCTION__, bd2str(&bdaddr, &bdstr), 953#if (BLE_INCLUDED == TRUE) 954 p_search_data->inq_res.device_type); 955#else 956 BT_DEVICE_TYPE_BREDR); 957#endif 958 bdname.name[0] = 0; 959 960 cod = devclass2uint (p_search_data->inq_res.dev_class); 961 962 if ( cod == 0) { 963 ALOGD("cod is 0, set as unclassified"); 964 cod = COD_UNCLASSIFIED; 965 } 966 967 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len)) 968 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len); 969 970 /* Check EIR for remote name and services */ 971 if (p_search_data->inq_res.p_eir) 972 { 973 BTA_GetEirService(p_search_data->inq_res.p_eir, &services); 974 BTIF_TRACE_DEBUG2("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services); 975 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */ 976 } 977 978 979 { 980 bt_property_t properties[5]; 981 bt_device_type_t dev_type; 982 uint32_t num_properties = 0; 983 bt_status_t status; 984 985 memset(properties, 0, sizeof(properties)); 986 /* BD_ADDR */ 987 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 988 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr); 989 num_properties++; 990 /* BD_NAME */ 991 /* Don't send BDNAME if it is empty */ 992 if (bdname.name[0]) { 993 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 994 BT_PROPERTY_BDNAME, 995 strlen((char *)bdname.name), &bdname); 996 num_properties++; 997 } 998 999 /* DEV_CLASS */ 1000 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 1001 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod); 1002 num_properties++; 1003 /* DEV_TYPE */ 1004#if (BLE_INCLUDED == TRUE) 1005 /* FixMe: Assumption is that bluetooth.h and BTE enums match */ 1006 dev_type = p_search_data->inq_res.device_type; 1007#else 1008 dev_type = BT_DEVICE_TYPE_BREDR; 1009#endif 1010 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 1011 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type); 1012 num_properties++; 1013 /* RSSI */ 1014 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 1015 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t), 1016 &(p_search_data->inq_res.rssi)); 1017 num_properties++; 1018 1019 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties); 1020 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status); 1021 1022 /* Callback to notify upper layer of device */ 1023 HAL_CBACK(bt_hal_cbacks, device_found_cb, 1024 num_properties, properties); 1025 } 1026 } 1027 break; 1028 1029 case BTA_DM_INQ_CMPL_EVT: 1030 { 1031 } 1032 break; 1033 case BTA_DM_DISC_CMPL_EVT: 1034 { 1035 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED); 1036 } 1037 break; 1038 case BTA_DM_SEARCH_CANCEL_CMPL_EVT: 1039 { 1040 /* if inquiry is not in progress and we get a cancel event, then 1041 * it means we are done with inquiry, but remote_name fetches are in 1042 * progress 1043 * 1044 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt 1045 * but instead wait for the cancel_cmpl_evt via the Busy Level 1046 * 1047 */ 1048 if (btif_dm_inquiry_in_progress == FALSE) 1049 { 1050 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED); 1051 } 1052 } 1053 break; 1054 } 1055} 1056 1057/******************************************************************************* 1058** 1059** Function btif_dm_search_services_evt 1060** 1061** Description Executes search services event in btif context 1062** 1063** Returns void 1064** 1065*******************************************************************************/ 1066static void btif_dm_search_services_evt(UINT16 event, char *p_param) 1067{ 1068 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param; 1069 1070 BTIF_TRACE_EVENT2("%s: event = %d", __FUNCTION__, event); 1071 switch (event) 1072 { 1073 case BTA_DM_DISC_RES_EVT: 1074 { 1075 bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */ 1076 bt_property_t prop; 1077 uint32_t i = 0, j = 0; 1078 bt_bdaddr_t bd_addr; 1079 bt_status_t ret; 1080 1081 bdcpy(bd_addr.address, p_data->disc_res.bd_addr); 1082 1083 BTIF_TRACE_DEBUG3("%s:(result=0x%x, services 0x%x)", __FUNCTION__, 1084 p_data->disc_res.result, p_data->disc_res.services); 1085 prop.type = BT_PROPERTY_UUIDS; 1086 prop.len = 0; 1087 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) 1088 { 1089 prop.val = p_data->disc_res.p_uuid_list; 1090 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE; 1091 for (i=0; i < p_data->disc_res.num_uuids; i++) 1092 { 1093 char temp[256]; 1094 uuid_to_string((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp); 1095 BTIF_TRACE_ERROR2("Index: %d uuid:%s", i, temp); 1096 } 1097 } 1098 1099 /* onUuidChanged requires getBondedDevices to be populated. 1100 ** bond_state_changed needs to be sent prior to remote_device_property 1101 */ 1102 if ((pairing_cb.state == BT_BOND_STATE_BONDING) && 1103 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&& 1104 pairing_cb.bonded_pending_sdp == TRUE) 1105 { 1106 BTIF_TRACE_DEBUG1("%s Remote Service SDP done. Call bond_state_changed_cb BONDED", 1107 __FUNCTION__); 1108 pairing_cb.bonded_pending_sdp = FALSE; 1109 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED); 1110 } 1111 1112 /* Also write this to the NVRAM */ 1113 ret = btif_storage_set_remote_device_property(&bd_addr, &prop); 1114 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret); 1115 /* Send the event to the BTIF */ 1116 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, 1117 BT_STATUS_SUCCESS, &bd_addr, 1, &prop); 1118 } 1119 break; 1120 1121 case BTA_DM_DISC_CMPL_EVT: 1122 /* fixme */ 1123 break; 1124 1125 default: 1126 { 1127 ASSERTC(0, "unhandled search services event", event); 1128 } 1129 break; 1130 } 1131} 1132 1133/******************************************************************************* 1134** 1135** Function btif_dm_remote_service_record_evt 1136** 1137** Description Executes search service record event in btif context 1138** 1139** Returns void 1140** 1141*******************************************************************************/ 1142static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param) 1143{ 1144 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param; 1145 1146 BTIF_TRACE_EVENT2("%s: event = %d", __FUNCTION__, event); 1147 switch (event) 1148 { 1149 case BTA_DM_DISC_RES_EVT: 1150 { 1151 bt_service_record_t rec; 1152 bt_property_t prop; 1153 uint32_t i = 0; 1154 bt_bdaddr_t bd_addr; 1155 1156 memset(&rec, 0, sizeof(bt_service_record_t)); 1157 bdcpy(bd_addr.address, p_data->disc_res.bd_addr); 1158 1159 BTIF_TRACE_DEBUG3("%s:(result=0x%x, services 0x%x)", __FUNCTION__, 1160 p_data->disc_res.result, p_data->disc_res.services); 1161 prop.type = BT_PROPERTY_SERVICE_RECORD; 1162 prop.val = (void*)&rec; 1163 prop.len = sizeof(rec); 1164 1165 /* disc_res.result is overloaded with SCN. Cannot check result */ 1166 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK; 1167 /* TODO: Get the UUID as well */ 1168 rec.channel = p_data->disc_res.result - 3; 1169 /* TODO: Need to get the service name using p_raw_data */ 1170 rec.name[0] = 0; 1171 1172 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, 1173 BT_STATUS_SUCCESS, &bd_addr, 1, &prop); 1174 } 1175 break; 1176 1177 default: 1178 { 1179 ASSERTC(0, "unhandled remote service record event", event); 1180 } 1181 break; 1182 } 1183} 1184 1185/******************************************************************************* 1186** 1187** Function btif_dm_upstreams_cback 1188** 1189** Description Executes UPSTREAMS events in btif context 1190** 1191** Returns void 1192** 1193*******************************************************************************/ 1194static void btif_dm_upstreams_evt(UINT16 event, char* p_param) 1195{ 1196 tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event; 1197 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param; 1198 tBTA_SERVICE_MASK service_mask; 1199 uint32_t i; 1200 bt_bdaddr_t bd_addr; 1201 1202 BTIF_TRACE_EVENT1("btif_dm_upstreams_cback ev: %s", dump_dm_event(event)); 1203 1204 switch (event) 1205 { 1206 case BTA_DM_ENABLE_EVT: 1207 { 1208 BD_NAME bdname; 1209 bt_status_t status; 1210 bt_property_t prop; 1211 prop.type = BT_PROPERTY_BDNAME; 1212 prop.len = BD_NAME_LEN; 1213 prop.val = (void*)bdname; 1214 1215 status = btif_storage_get_adapter_property(&prop); 1216 /* Storage does not have a name yet. 1217 ** Use the default name and write it to the chip 1218 */ 1219 if (status != BT_STATUS_SUCCESS) 1220 { 1221 BTA_DmSetDeviceName((char *)BTM_DEF_LOCAL_NAME); 1222 /* Hmmm...Should we store this too??? */ 1223 } 1224 else 1225 { 1226 /* A name exists in the storage. Make this the device name */ 1227 BTA_DmSetDeviceName((char*)prop.val); 1228 } 1229 1230 /* for each of the enabled services in the mask, trigger the profile 1231 * enable */ 1232 service_mask = btif_get_enabled_services_mask(); 1233 for (i=0; i <= BTA_MAX_SERVICE_ID; i++) 1234 { 1235 if (service_mask & 1236 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i))) 1237 { 1238 btif_in_execute_service_request(i, TRUE); 1239 } 1240 } 1241 /* clear control blocks */ 1242 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t)); 1243 1244 /* This function will also trigger the adapter_properties_cb 1245 ** and bonded_devices_info_cb 1246 */ 1247 btif_storage_load_bonded_devices(); 1248 1249 btif_storage_load_autopair_device_list(); 1250 1251 btif_enable_bluetooth_evt(p_data->enable.status, p_data->enable.bd_addr); 1252 } 1253 break; 1254 1255 case BTA_DM_DISABLE_EVT: 1256 /* for each of the enabled services in the mask, trigger the profile 1257 * disable */ 1258 service_mask = btif_get_enabled_services_mask(); 1259 for (i=0; i <= BTA_MAX_SERVICE_ID; i++) 1260 { 1261 if (service_mask & 1262 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i))) 1263 { 1264 btif_in_execute_service_request(i, FALSE); 1265 } 1266 } 1267 btif_disable_bluetooth_evt(); 1268 break; 1269 1270 case BTA_DM_PIN_REQ_EVT: 1271 btif_dm_pin_req_evt(&p_data->pin_req); 1272 break; 1273 1274 case BTA_DM_AUTH_CMPL_EVT: 1275 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl); 1276 break; 1277 1278 case BTA_DM_BOND_CANCEL_CMPL_EVT: 1279 if (pairing_cb.state == BT_BOND_STATE_BONDING) 1280 { 1281 bdcpy(bd_addr.address, pairing_cb.bd_addr); 1282 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE); 1283 } 1284 break; 1285 1286 case BTA_DM_SP_CFM_REQ_EVT: 1287 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req); 1288 break; 1289 case BTA_DM_SP_KEY_NOTIF_EVT: 1290 btif_dm_ssp_key_notif_evt(&p_data->key_notif); 1291 break; 1292 1293 case BTA_DM_DEV_UNPAIRED_EVT: 1294 bdcpy(bd_addr.address, p_data->link_down.bd_addr); 1295 1296 /*special handling for HID devices */ 1297 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE)) 1298 if (check_cod_hid(&bd_addr, COD_HID_MAJOR)) 1299 { 1300 btif_hh_remove_device(bd_addr); 1301 } 1302 #endif 1303 btif_storage_remove_bonded_device(&bd_addr); 1304 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE); 1305 break; 1306 1307 case BTA_DM_BUSY_LEVEL_EVT: 1308 { 1309 1310 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK) 1311 { 1312 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED) 1313 { 1314 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, 1315 BT_DISCOVERY_STARTED); 1316 btif_dm_inquiry_in_progress = TRUE; 1317 } 1318 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED) 1319 { 1320 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, 1321 BT_DISCOVERY_STOPPED); 1322 btif_dm_inquiry_in_progress = FALSE; 1323 } 1324 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE) 1325 { 1326 btif_dm_inquiry_in_progress = FALSE; 1327 } 1328 } 1329 }break; 1330 1331 case BTA_DM_LINK_UP_EVT: 1332 bdcpy(bd_addr.address, p_data->link_up.bd_addr); 1333 BTIF_TRACE_DEBUG0("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED"); 1334 1335 btif_update_remote_version_property(&bd_addr); 1336 1337 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS, 1338 &bd_addr, BT_ACL_STATE_CONNECTED); 1339 break; 1340 1341 case BTA_DM_LINK_DOWN_EVT: 1342 bdcpy(bd_addr.address, p_data->link_down.bd_addr); 1343 if ((pairing_cb.state == BT_BOND_STATE_BONDING) && 1344 (bdcmp(p_data->link_down.bd_addr, pairing_cb.bd_addr) == 0)) 1345 { 1346 bond_state_changed(BT_STATUS_RMT_DEV_DOWN,&bd_addr, BT_BOND_STATE_NONE); 1347 } 1348 BTIF_TRACE_DEBUG0("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED"); 1349 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS, 1350 &bd_addr, BT_ACL_STATE_DISCONNECTED); 1351 break; 1352 1353 case BTA_DM_HW_ERROR_EVT: 1354 BTIF_TRACE_ERROR0("Received H/W Error. "); 1355 /* Flush storage data */ 1356 btif_config_flush(); 1357 usleep(100000); /* 100milliseconds */ 1358 /* Killing the process to force a restart as part of fault tolerance */ 1359 kill(getpid(), SIGKILL); 1360 break; 1361 1362#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 1363 case BTA_DM_BLE_KEY_EVT: 1364 BTIF_TRACE_DEBUG1("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", p_data->ble_key.key_type); 1365 1366 /* If this pairing is by-product of local initiated GATT client Read or Write, 1367 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not 1368 have setup properly. Setup pairing_cb and notify App about Bonding state now*/ 1369 if (pairing_cb.state != BT_BOND_STATE_BONDING) 1370 { 1371 BTIF_TRACE_DEBUG0("Bond state not sent to App so far.Notify the app now"); 1372 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr, 1373 BT_BOND_STATE_BONDING); 1374 } 1375 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0) 1376 { 1377 BTIF_TRACE_ERROR1("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type); 1378 break; 1379 } 1380 1381 switch (p_data->ble_key.key_type) 1382 { 1383 case BTA_LE_KEY_PENC: 1384 BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PENC"); 1385 pairing_cb.ble.is_penc_key_rcvd = TRUE; 1386 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16); 1387 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8); 1388 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv; 1389 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level; 1390 1391 for (i=0; i<16; i++) 1392 { 1393 BTIF_TRACE_DEBUG2("pairing_cb.ble.penc_key.ltk[%d]=0x%02x",i,pairing_cb.ble.penc_key.ltk[i]); 1394 } 1395 for (i=0; i<8; i++) 1396 { 1397 BTIF_TRACE_DEBUG2("pairing_cb.ble.penc_key.rand[%d]=0x%02x",i,pairing_cb.ble.penc_key.rand[i]); 1398 } 1399 BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv); 1400 BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level); 1401 BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.key_size=0x%02x",pairing_cb.ble.penc_key.key_size); 1402 break; 1403 1404 case BTA_LE_KEY_PID: 1405 BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PID"); 1406 pairing_cb.ble.is_pid_key_rcvd = TRUE; 1407 memcpy(pairing_cb.ble.pid_key, p_data->ble_key.key_value.pid_key.irk, 16); 1408 for (i=0; i<16; i++) 1409 { 1410 BTIF_TRACE_DEBUG2("pairing_cb.ble.pid_key[%d]=0x%02x",i,pairing_cb.ble.pid_key[i]); 1411 } 1412 break; 1413 1414 case BTA_LE_KEY_PCSRK: 1415 BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PCSRK"); 1416 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE; 1417 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter; 1418 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level; 1419 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16); 1420 1421 for (i=0; i<16; i++) 1422 { 1423 BTIF_TRACE_DEBUG2("pairing_cb.ble.pcsrk_key.csrk[%d]=0x%02x",i,pairing_cb.ble.pcsrk_key.csrk[i]); 1424 } 1425 BTIF_TRACE_DEBUG1("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter); 1426 BTIF_TRACE_DEBUG1("pairing_cb.ble.pcsrk_key.sec_level=0x%02x",pairing_cb.ble.pcsrk_key.sec_level); 1427 break; 1428 1429 case BTA_LE_KEY_LENC: 1430 BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_LENC"); 1431 pairing_cb.ble.is_lenc_key_rcvd = TRUE; 1432 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div; 1433 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size; 1434 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level; 1435 1436 BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div); 1437 BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size); 1438 BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.sec_level=0x%02x",pairing_cb.ble.lenc_key.sec_level); 1439 break; 1440 1441 1442 1443 case BTA_LE_KEY_LCSRK: 1444 BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_LCSRK"); 1445 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE; 1446 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter; 1447 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div; 1448 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level; 1449 1450 BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div); 1451 BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter); 1452 BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.sec_level=0x%02x",pairing_cb.ble.lcsrk_key.sec_level); 1453 1454 break; 1455 1456 default: 1457 BTIF_TRACE_ERROR1("unknown BLE key type (0x%02x)", p_data->ble_key.key_type); 1458 break; 1459 } 1460 1461 break; 1462 case BTA_DM_BLE_SEC_REQ_EVT: 1463 BTIF_TRACE_DEBUG0("BTA_DM_BLE_SEC_REQ_EVT. "); 1464 btif_dm_ble_sec_req_evt(&p_data->ble_req); 1465 break; 1466 case BTA_DM_BLE_PASSKEY_NOTIF_EVT: 1467 BTIF_TRACE_DEBUG0("BTA_DM_BLE_PASSKEY_NOTIF_EVT. "); 1468 btif_dm_ble_key_notif_evt(&p_data->key_notif); 1469 break; 1470 case BTA_DM_BLE_PASSKEY_REQ_EVT: 1471 BTIF_TRACE_DEBUG0("BTA_DM_BLE_PASSKEY_REQ_EVT. "); 1472 btif_dm_ble_passkey_req_evt(&p_data->pin_req); 1473 break; 1474 case BTA_DM_BLE_OOB_REQ_EVT: 1475 BTIF_TRACE_DEBUG0("BTA_DM_BLE_OOB_REQ_EVT. "); 1476 break; 1477 case BTA_DM_BLE_LOCAL_IR_EVT: 1478 BTIF_TRACE_DEBUG0("BTA_DM_BLE_LOCAL_IR_EVT. "); 1479 ble_local_key_cb.is_id_keys_rcvd = TRUE; 1480 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16)); 1481 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16)); 1482 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16)); 1483 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0], 1484 BTIF_DM_LE_LOCAL_KEY_IR, 1485 BT_OCTET16_LEN); 1486 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0], 1487 BTIF_DM_LE_LOCAL_KEY_IRK, 1488 BT_OCTET16_LEN); 1489 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0], 1490 BTIF_DM_LE_LOCAL_KEY_DHK, 1491 BT_OCTET16_LEN); 1492 break; 1493 case BTA_DM_BLE_LOCAL_ER_EVT: 1494 BTIF_TRACE_DEBUG0("BTA_DM_BLE_LOCAL_ER_EVT. "); 1495 ble_local_key_cb.is_er_rcvd = TRUE; 1496 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16)); 1497 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0], 1498 BTIF_DM_LE_LOCAL_KEY_ER, 1499 BT_OCTET16_LEN); 1500 break; 1501 1502 case BTA_DM_BLE_AUTH_CMPL_EVT: 1503 BTIF_TRACE_DEBUG0("BTA_DM_BLE_KEY_EVT. "); 1504 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl); 1505 break; 1506#endif 1507 1508 case BTA_DM_AUTHORIZE_EVT: 1509 case BTA_DM_SIG_STRENGTH_EVT: 1510 case BTA_DM_SP_RMT_OOB_EVT: 1511 case BTA_DM_SP_KEYPRESS_EVT: 1512 case BTA_DM_ROLE_CHG_EVT: 1513 1514 default: 1515 BTIF_TRACE_WARNING1( "btif_dm_cback : unhandled event (%d)", event ); 1516 break; 1517 } 1518} /* btui_security_cback() */ 1519 1520 1521/******************************************************************************* 1522** 1523** Function btif_dm_generic_evt 1524** 1525** Description Executes non-BTA upstream events in BTIF context 1526** 1527** Returns void 1528** 1529*******************************************************************************/ 1530static void btif_dm_generic_evt(UINT16 event, char* p_param) 1531{ 1532 BTIF_TRACE_EVENT2("%s: event=%d", __FUNCTION__, event); 1533 switch(event) 1534 { 1535 case BTIF_DM_CB_DISCOVERY_STARTED: 1536 { 1537 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED); 1538 } 1539 break; 1540 1541 case BTIF_DM_CB_CREATE_BOND: 1542 { 1543 btif_dm_cb_create_bond((bt_bdaddr_t *)p_param); 1544 } 1545 break; 1546 1547 case BTIF_DM_CB_REMOVE_BOND: 1548 { 1549 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param); 1550 } 1551 break; 1552 1553 case BTIF_DM_CB_HID_REMOTE_NAME: 1554 { 1555 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param); 1556 } 1557 break; 1558 1559 case BTIF_DM_CB_BOND_STATE_BONDING: 1560 { 1561 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING); 1562 } 1563 break; 1564 case BTIF_DM_CB_LE_TX_TEST: 1565 case BTIF_DM_CB_LE_RX_TEST: 1566 { 1567 uint8_t status; 1568 STREAM_TO_UINT8(status, p_param); 1569 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb, 1570 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0); 1571 } 1572 break; 1573 case BTIF_DM_CB_LE_TEST_END: 1574 { 1575 uint8_t status; 1576 uint16_t count = 0; 1577 STREAM_TO_UINT8(status, p_param); 1578 if (status == 0) 1579 STREAM_TO_UINT16(count, p_param); 1580 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb, 1581 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count); 1582 } 1583 break; 1584 default: 1585 { 1586 BTIF_TRACE_WARNING2("%s : Unknown event 0x%x", __FUNCTION__, event); 1587 } 1588 break; 1589 } 1590} 1591 1592/******************************************************************************* 1593** 1594** Function bte_dm_evt 1595** 1596** Description Switches context from BTE to BTIF for all DM events 1597** 1598** Returns void 1599** 1600*******************************************************************************/ 1601 1602void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data) 1603{ 1604 bt_status_t status; 1605 1606 /* switch context to btif task context (copy full union size for convenience) */ 1607 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL); 1608 1609 /* catch any failed context transfers */ 1610 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); 1611} 1612 1613/******************************************************************************* 1614** 1615** Function bte_search_devices_evt 1616** 1617** Description Switches context from BTE to BTIF for DM search events 1618** 1619** Returns void 1620** 1621*******************************************************************************/ 1622static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data) 1623{ 1624 UINT16 param_len = 0; 1625 1626 if (p_data) 1627 param_len += sizeof(tBTA_DM_SEARCH); 1628 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */ 1629 switch (event) 1630 { 1631 case BTA_DM_INQ_RES_EVT: 1632 { 1633 if (p_data->inq_res.p_eir) 1634 param_len += HCI_EXT_INQ_RESPONSE_LEN; 1635 } 1636 break; 1637 1638 case BTA_DM_DISC_RES_EVT: 1639 { 1640 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data) 1641 param_len += p_data->disc_res.raw_data_size; 1642 } 1643 break; 1644 } 1645 BTIF_TRACE_DEBUG3("%s event=%s param_len=%d", __FUNCTION__, dump_dm_search_event(event), param_len); 1646 1647 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */ 1648 if (event == BTA_DM_INQ_RES_EVT) 1649 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL); 1650 1651 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len, 1652 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL); 1653} 1654 1655/******************************************************************************* 1656** 1657** Function bte_dm_search_services_evt 1658** 1659** Description Switches context from BTE to BTIF for DM search services 1660** event 1661** 1662** Returns void 1663** 1664*******************************************************************************/ 1665static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data) 1666{ 1667 UINT16 param_len = 0; 1668 if (p_data) 1669 param_len += sizeof(tBTA_DM_SEARCH); 1670 switch (event) 1671 { 1672 case BTA_DM_DISC_RES_EVT: 1673 { 1674 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) { 1675 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE); 1676 } 1677 } break; 1678 } 1679 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure 1680 * if raw_data is needed. */ 1681 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len, 1682 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL); 1683} 1684 1685/******************************************************************************* 1686** 1687** Function bte_dm_remote_service_record_evt 1688** 1689** Description Switches context from BTE to BTIF for DM search service 1690** record event 1691** 1692** Returns void 1693** 1694*******************************************************************************/ 1695static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data) 1696{ 1697 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */ 1698 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL); 1699} 1700 1701/***************************************************************************** 1702** 1703** btif api functions (no context switch) 1704** 1705*****************************************************************************/ 1706 1707/******************************************************************************* 1708** 1709** Function btif_dm_start_discovery 1710** 1711** Description Start device discovery/inquiry 1712** 1713** Returns bt_status_t 1714** 1715*******************************************************************************/ 1716bt_status_t btif_dm_start_discovery(void) 1717{ 1718 tBTA_DM_INQ inq_params; 1719 tBTA_SERVICE_MASK services = 0; 1720 1721 BTIF_TRACE_EVENT1("%s", __FUNCTION__); 1722 /* TODO: Do we need to handle multiple inquiries at the same time? */ 1723 1724 /* Set inquiry params and call API */ 1725#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 1726 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY; 1727#else 1728 inq_params.mode = BTA_DM_GENERAL_INQUIRY; 1729#endif 1730 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION; 1731 1732 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS; 1733 inq_params.report_dup = TRUE; 1734 1735 inq_params.filter_type = BTA_DM_INQ_CLR; 1736 /* TODO: Filter device by BDA needs to be implemented here */ 1737 1738 /* Will be enabled to TRUE once inquiry busy level has been received */ 1739 btif_dm_inquiry_in_progress = FALSE; 1740 /* find nearby devices */ 1741 BTA_DmSearch(&inq_params, services, bte_search_devices_evt); 1742 1743 return BT_STATUS_SUCCESS; 1744} 1745 1746/******************************************************************************* 1747** 1748** Function btif_dm_cancel_discovery 1749** 1750** Description Cancels search 1751** 1752** Returns bt_status_t 1753** 1754*******************************************************************************/ 1755bt_status_t btif_dm_cancel_discovery(void) 1756{ 1757 BTIF_TRACE_EVENT1("%s", __FUNCTION__); 1758 BTA_DmSearchCancel(); 1759 return BT_STATUS_SUCCESS; 1760} 1761 1762/******************************************************************************* 1763** 1764** Function btif_dm_create_bond 1765** 1766** Description Initiate bonding with the specified device 1767** 1768** Returns bt_status_t 1769** 1770*******************************************************************************/ 1771bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr) 1772{ 1773 bdstr_t bdstr; 1774 1775 BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *) bd_addr, &bdstr)); 1776 if (pairing_cb.state != BT_BOND_STATE_NONE) 1777 return BT_STATUS_BUSY; 1778 1779 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND, 1780 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL); 1781 1782 return BT_STATUS_SUCCESS; 1783} 1784 1785/******************************************************************************* 1786** 1787** Function btif_dm_cancel_bond 1788** 1789** Description Initiate bonding with the specified device 1790** 1791** Returns bt_status_t 1792** 1793*******************************************************************************/ 1794 1795bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr) 1796{ 1797 bdstr_t bdstr; 1798 1799 BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr)); 1800 1801 /* TODO: 1802 ** 1. Restore scan modes 1803 ** 2. special handling for HID devices 1804 */ 1805 if (pairing_cb.state == BT_BOND_STATE_BONDING) 1806 { 1807 1808#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 1809 1810 if (pairing_cb.is_ssp) 1811 { 1812 if (pairing_cb.is_le_only) 1813 { 1814 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT); 1815 } 1816 else 1817 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE); 1818 } 1819 else 1820 { 1821 if (pairing_cb.is_le_only) 1822 { 1823 BTA_DmBondCancel ((UINT8 *)bd_addr->address); 1824 } 1825 else 1826 { 1827 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL); 1828 } 1829 /* Cancel bonding, in case it is in ACL connection setup state */ 1830 BTA_DmBondCancel ((UINT8 *)bd_addr->address); 1831 } 1832 1833#else 1834 if (pairing_cb.is_ssp) 1835 { 1836 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE); 1837 } 1838 else 1839 { 1840 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL); 1841 } 1842 /* Cancel bonding, in case it is in ACL connection setup state */ 1843 BTA_DmBondCancel ((UINT8 *)bd_addr->address); 1844 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr); 1845#endif 1846 } 1847 1848 return BT_STATUS_SUCCESS; 1849} 1850 1851/******************************************************************************* 1852** 1853** Function btif_dm_remove_bond 1854** 1855** Description Removes bonding with the specified device 1856** 1857** Returns bt_status_t 1858** 1859*******************************************************************************/ 1860 1861bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr) 1862{ 1863 bdstr_t bdstr; 1864 1865 BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr)); 1866 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND, 1867 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL); 1868 1869 return BT_STATUS_SUCCESS; 1870} 1871 1872/******************************************************************************* 1873** 1874** Function btif_dm_pin_reply 1875** 1876** Description BT legacy pairing - PIN code reply 1877** 1878** Returns bt_status_t 1879** 1880*******************************************************************************/ 1881 1882bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept, 1883 uint8_t pin_len, bt_pin_code_t *pin_code) 1884{ 1885 BTIF_TRACE_EVENT2("%s: accept=%d", __FUNCTION__, accept); 1886#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 1887 1888 if (pairing_cb.is_le_only) 1889 { 1890 int i; 1891 UINT32 passkey = 0; 1892 int multi[] = {100000, 10000, 1000, 100, 10,1}; 1893 BD_ADDR remote_bd_addr; 1894 bdcpy(remote_bd_addr, bd_addr->address); 1895 for (i = 0; i < 6; i++) 1896 { 1897 passkey += (multi[i] * (pin_code->pin[i] - '0')); 1898 } 1899 BTIF_TRACE_DEBUG1("btif_dm_pin_reply: passkey: %d", passkey); 1900 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey); 1901 1902 } 1903 else 1904 { 1905 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin); 1906 if (accept) 1907 pairing_cb.pin_code_len = pin_len; 1908 } 1909#else 1910 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin); 1911 1912 if (accept) 1913 pairing_cb.pin_code_len = pin_len; 1914#endif 1915 return BT_STATUS_SUCCESS; 1916} 1917 1918/******************************************************************************* 1919** 1920** Function btif_dm_ssp_reply 1921** 1922** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry 1923** 1924** Returns bt_status_t 1925** 1926*******************************************************************************/ 1927 1928bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr, 1929 bt_ssp_variant_t variant, uint8_t accept, 1930 uint32_t passkey) 1931{ 1932 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY) 1933 { 1934 /* This is not implemented in the stack. 1935 * For devices with display, this is not needed 1936 */ 1937 BTIF_TRACE_WARNING1("%s: Not implemented", __FUNCTION__); 1938 return BT_STATUS_FAIL; 1939 } 1940 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */ 1941 BTIF_TRACE_EVENT2("%s: accept=%d", __FUNCTION__, accept); 1942#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 1943 if (pairing_cb.is_le_only) 1944 { 1945 if (accept) 1946 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED); 1947 else 1948 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT); 1949 } 1950 else 1951 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept); 1952 1953#else 1954 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept); 1955#endif 1956 return BT_STATUS_SUCCESS; 1957} 1958 1959/******************************************************************************* 1960** 1961** Function btif_dm_get_adapter_property 1962** 1963** Description Queries the BTA for the adapter property 1964** 1965** Returns bt_status_t 1966** 1967*******************************************************************************/ 1968bt_status_t btif_dm_get_adapter_property(bt_property_t *prop) 1969{ 1970 bt_status_t status; 1971 1972 BTIF_TRACE_EVENT2("%s: type=0x%x", __FUNCTION__, prop->type); 1973 switch (prop->type) 1974 { 1975 case BT_PROPERTY_BDNAME: 1976 { 1977 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val; 1978 strcpy((char *)bd_name->name, (char *)BTM_DEF_LOCAL_NAME); 1979 prop->len = strlen((char *)bd_name->name); 1980 } 1981 break; 1982 1983 case BT_PROPERTY_ADAPTER_SCAN_MODE: 1984 { 1985 /* if the storage does not have it. Most likely app never set it. Default is NONE */ 1986 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val; 1987 *mode = BT_SCAN_MODE_NONE; 1988 prop->len = sizeof(bt_scan_mode_t); 1989 } 1990 break; 1991 1992 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: 1993 { 1994 uint32_t *tmt = (uint32_t*)prop->val; 1995 *tmt = 120; /* default to 120s, if not found in NV */ 1996 prop->len = sizeof(uint32_t); 1997 } 1998 break; 1999 2000 default: 2001 prop->len = 0; 2002 return BT_STATUS_FAIL; 2003 } 2004 return BT_STATUS_SUCCESS; 2005} 2006 2007/******************************************************************************* 2008** 2009** Function btif_dm_get_remote_services 2010** 2011** Description Start SDP to get remote services 2012** 2013** Returns bt_status_t 2014** 2015*******************************************************************************/ 2016bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr) 2017{ 2018 bdstr_t bdstr; 2019 2020 BTIF_TRACE_EVENT2("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr)); 2021 2022 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK, 2023 bte_dm_search_services_evt, TRUE); 2024 2025 return BT_STATUS_SUCCESS; 2026} 2027 2028/******************************************************************************* 2029** 2030** Function btif_dm_get_remote_service_record 2031** 2032** Description Start SDP to get remote service record 2033** 2034** 2035** Returns bt_status_t 2036*******************************************************************************/ 2037bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr, 2038 bt_uuid_t *uuid) 2039{ 2040 tSDP_UUID sdp_uuid; 2041 bdstr_t bdstr; 2042 2043 BTIF_TRACE_EVENT2("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr)); 2044 2045 sdp_uuid.len = MAX_UUID_SIZE; 2046 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE); 2047 2048 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid, 2049 bte_dm_remote_service_record_evt, TRUE); 2050 2051 return BT_STATUS_SUCCESS; 2052} 2053 2054void btif_dm_execute_service_request(UINT16 event, char *p_param) 2055{ 2056 BOOLEAN b_enable = FALSE; 2057 bt_status_t status; 2058 if (event == BTIF_DM_ENABLE_SERVICE) 2059 { 2060 b_enable = TRUE; 2061 } 2062 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable); 2063 if (status == BT_STATUS_SUCCESS) 2064 { 2065 bt_property_t property; 2066 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS]; 2067 2068 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */ 2069 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS, 2070 sizeof(local_uuids), local_uuids); 2071 btif_storage_get_adapter_property(&property); 2072 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, 2073 BT_STATUS_SUCCESS, 1, &property); 2074 } 2075 return; 2076} 2077 2078#if (BTM_OOB_INCLUDED == TRUE) 2079void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data) 2080{ 2081 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 && 2082 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 ) 2083 { 2084 *p_oob_data = FALSE; 2085 } 2086 else 2087 { 2088 *p_oob_data = TRUE; 2089 } 2090 BTIF_TRACE_DEBUG1("btif_dm_set_oob_for_io_req *p_oob_data=%d", *p_oob_data); 2091} 2092#endif /* BTM_OOB_INCLUDED */ 2093 2094#ifdef BTIF_DM_OOB_TEST 2095void btif_dm_load_local_oob(void) 2096{ 2097 char prop_oob[PROPERTY_VALUE_MAX]; 2098 property_get("service.brcm.bt.oob", prop_oob, "3"); 2099 BTIF_TRACE_DEBUG1("btif_dm_load_local_oob prop_oob = %s",prop_oob); 2100 if (prop_oob[0] != '3') 2101 { 2102#if (BTM_OOB_INCLUDED == TRUE) 2103 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 && 2104 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 ) 2105 { 2106 BTIF_TRACE_DEBUG0("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()"); 2107 BTA_DmLocalOob(); 2108 } 2109#else 2110 BTIF_TRACE_ERROR0("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)"); 2111#endif 2112 } 2113} 2114 2115void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r) 2116{ 2117 FILE *fp; 2118 char *path_a = "/data/misc/bluedroid/LOCAL/a.key"; 2119 char *path_b = "/data/misc/bluedroid/LOCAL/b.key"; 2120 char *path = NULL; 2121 char prop_oob[PROPERTY_VALUE_MAX]; 2122 BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: valid=%d", valid); 2123 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 && 2124 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 && 2125 valid) 2126 { 2127 BTIF_TRACE_DEBUG0("save local OOB data in memory"); 2128 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN); 2129 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN); 2130 property_get("service.brcm.bt.oob", prop_oob, "3"); 2131 BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob prop_oob = %s",prop_oob); 2132 if (prop_oob[0] == '1') 2133 path = path_a; 2134 else if (prop_oob[0] == '2') 2135 path = path_b; 2136 if (path) 2137 { 2138 fp = fopen(path, "wb+"); 2139 if (fp == NULL) 2140 { 2141 BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: failed to save local OOB data to %s", path); 2142 } 2143 else 2144 { 2145 BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: save local OOB data into file %s",path); 2146 fwrite (c , 1 , BT_OCTET16_LEN , fp ); 2147 fwrite (r , 1 , BT_OCTET16_LEN , fp ); 2148 fclose(fp); 2149 } 2150 } 2151 } 2152} 2153BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r) 2154{ 2155 char t[128]; 2156 FILE *fp; 2157 char *path_a = "/data/misc/bluedroid/LOCAL/a.key"; 2158 char *path_b = "/data/misc/bluedroid/LOCAL/b.key"; 2159 char *path = NULL; 2160 char prop_oob[PROPERTY_VALUE_MAX]; 2161 BOOLEAN result = FALSE; 2162 bt_bdaddr_t bt_bd_addr; 2163 bdcpy(oob_cb.oob_bdaddr, bd_addr); 2164 property_get("service.brcm.bt.oob", prop_oob, "3"); 2165 BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob); 2166 if (prop_oob[0] == '1') 2167 path = path_b; 2168 else if (prop_oob[0] == '2') 2169 path = path_a; 2170 if (path) 2171 { 2172 fp = fopen(path, "rb"); 2173 if (fp == NULL) 2174 { 2175 BTIF_TRACE_DEBUG1("btapp_dm_rmt_oob_reply: failed to read OOB keys from %s",path); 2176 return FALSE; 2177 } 2178 else 2179 { 2180 BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob: read OOB data from %s",path); 2181 fread (p_c , 1 , BT_OCTET16_LEN , fp ); 2182 fread (p_r , 1 , BT_OCTET16_LEN , fp ); 2183 fclose(fp); 2184 } 2185 BTIF_TRACE_DEBUG0("----btif_dm_proc_rmt_oob: TRUE"); 2186 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x", 2187 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2], 2188 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]); 2189 BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t); 2190 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", 2191 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7], 2192 p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]); 2193 BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: c = %s",t); 2194 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", 2195 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7], 2196 p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]); 2197 BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: r = %s",t); 2198 bdcpy(bt_bd_addr.address, bd_addr); 2199 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING, 2200 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL); 2201 result = TRUE; 2202 } 2203 BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob result=%d",result); 2204 return result; 2205} 2206#endif /* BTIF_DM_OOB_TEST */ 2207#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 2208 2209static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif) 2210{ 2211 bt_bdaddr_t bd_addr; 2212 bt_bdname_t bd_name; 2213 UINT32 cod; 2214 2215 BTIF_TRACE_DEBUG1("%s", __FUNCTION__); 2216 2217 /* Remote name update */ 2218 btif_update_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name, 2219 NULL, BT_DEVICE_TYPE_BLE); 2220 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr); 2221 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN); 2222 2223 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); 2224 pairing_cb.is_ssp = FALSE; 2225 cod = COD_UNCLASSIFIED; 2226 2227 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, 2228 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION, 2229 p_ssp_key_notif->passkey); 2230} 2231 2232/******************************************************************************* 2233** 2234** Function btif_dm_ble_auth_cmpl_evt 2235** 2236** Description Executes authentication complete event in btif context 2237** 2238** Returns void 2239** 2240*******************************************************************************/ 2241static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) 2242{ 2243 /* Save link key, if not temporary */ 2244 bt_bdaddr_t bd_addr; 2245 bt_status_t status = BT_STATUS_FAIL; 2246 bt_bond_state_t state = BT_BOND_STATE_NONE; 2247 2248 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr); 2249 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) ) 2250 { 2251 /* store keys */ 2252 } 2253 if (p_auth_cmpl->success) 2254 { 2255 status = BT_STATUS_SUCCESS; 2256 state = BT_BOND_STATE_BONDED; 2257 2258 btif_dm_save_ble_bonding_keys(); 2259 BTA_GATTC_Refresh(bd_addr.address); 2260 btif_dm_get_remote_services(&bd_addr); 2261 } 2262 else 2263 { 2264 /*Map the HCI fail reason to bt status */ 2265 switch (p_auth_cmpl->fail_reason) 2266 { 2267 default: 2268 btif_dm_remove_ble_bonding_keys(); 2269 status = BT_STATUS_FAIL; 2270 break; 2271 } 2272 } 2273 bond_state_changed(status, &bd_addr, state); 2274} 2275 2276 2277 2278void btif_dm_load_ble_local_keys(void) 2279{ 2280 bt_status_t bt_status; 2281 2282 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t)); 2283 2284 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0], 2285 BT_OCTET16_LEN)== BT_STATUS_SUCCESS) 2286 { 2287 ble_local_key_cb.is_er_rcvd = TRUE; 2288 BTIF_TRACE_DEBUG1("%s BLE ER key loaded",__FUNCTION__ ); 2289 } 2290 2291 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0], 2292 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&& 2293 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0], 2294 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&& 2295 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0], 2296 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)) 2297 { 2298 ble_local_key_cb.is_id_keys_rcvd = TRUE; 2299 BTIF_TRACE_DEBUG1("%s BLE ID keys loaded",__FUNCTION__ ); 2300 } 2301 2302} 2303void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er, 2304 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys) 2305{ 2306 if (ble_local_key_cb.is_er_rcvd ) 2307 { 2308 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16)); 2309 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER; 2310 } 2311 2312 if (ble_local_key_cb.is_id_keys_rcvd) 2313 { 2314 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16)); 2315 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16)); 2316 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16)); 2317 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID; 2318 } 2319 BTIF_TRACE_DEBUG2("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask); 2320} 2321 2322void btif_dm_save_ble_bonding_keys(void) 2323{ 2324 2325 bt_bdaddr_t bd_addr; 2326 2327 BTIF_TRACE_DEBUG1("%s",__FUNCTION__ ); 2328 2329 bdcpy(bd_addr.address, pairing_cb.bd_addr); 2330 2331 if (pairing_cb.ble.is_penc_key_rcvd) 2332 { 2333 btif_storage_add_ble_bonding_key(&bd_addr, 2334 (char *) &pairing_cb.ble.penc_key, 2335 BTIF_DM_LE_KEY_PENC, 2336 sizeof(btif_dm_ble_penc_keys_t)); 2337 } 2338 2339 if (pairing_cb.ble.is_pid_key_rcvd) 2340 { 2341 btif_storage_add_ble_bonding_key(&bd_addr, 2342 (char *) &pairing_cb.ble.pid_key[0], 2343 BTIF_DM_LE_KEY_PID, 2344 BT_OCTET16_LEN); 2345 } 2346 2347 2348 if (pairing_cb.ble.is_pcsrk_key_rcvd) 2349 { 2350 btif_storage_add_ble_bonding_key(&bd_addr, 2351 (char *) &pairing_cb.ble.pcsrk_key, 2352 BTIF_DM_LE_KEY_PCSRK, 2353 sizeof(btif_dm_ble_pcsrk_keys_t)); 2354 } 2355 2356 2357 if (pairing_cb.ble.is_lenc_key_rcvd) 2358 { 2359 btif_storage_add_ble_bonding_key(&bd_addr, 2360 (char *) &pairing_cb.ble.lenc_key, 2361 BTIF_DM_LE_KEY_LENC, 2362 sizeof(btif_dm_ble_lenc_keys_t)); 2363 } 2364 2365 if (pairing_cb.ble.is_lcsrk_key_rcvd) 2366 { 2367 btif_storage_add_ble_bonding_key(&bd_addr, 2368 (char *) &pairing_cb.ble.lcsrk_key, 2369 BTIF_DM_LE_KEY_LCSRK, 2370 sizeof(btif_dm_ble_lcsrk_keys_t)); 2371 } 2372 2373} 2374 2375 2376void btif_dm_remove_ble_bonding_keys(void) 2377{ 2378 bt_bdaddr_t bd_addr; 2379 2380 BTIF_TRACE_DEBUG1("%s",__FUNCTION__ ); 2381 2382 bdcpy(bd_addr.address, pairing_cb.bd_addr); 2383 btif_storage_remove_ble_bonding_keys(&bd_addr); 2384} 2385 2386 2387/******************************************************************************* 2388** 2389** Function btif_dm_ble_sec_req_evt 2390** 2391** Description Eprocess security request event in btif context 2392** 2393** Returns void 2394** 2395*******************************************************************************/ 2396void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req) 2397{ 2398 bt_bdaddr_t bd_addr; 2399 bt_bdname_t bd_name; 2400 UINT32 cod; 2401 BTIF_TRACE_DEBUG1("%s", __FUNCTION__); 2402 2403 if (pairing_cb.state == BT_BOND_STATE_BONDING) 2404 { 2405 BTIF_TRACE_DEBUG1("%s Discard security request", __FUNCTION__); 2406 return; 2407 } 2408 2409 /* Remote name update */ 2410 btif_update_remote_properties(p_ble_req->bd_addr,p_ble_req->bd_name,NULL,BT_DEVICE_TYPE_BLE); 2411 2412 bdcpy(bd_addr.address, p_ble_req->bd_addr); 2413 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN); 2414 2415 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); 2416 2417 pairing_cb.is_temp = FALSE; 2418 pairing_cb.is_le_only = TRUE; 2419 pairing_cb.is_ssp = TRUE; 2420 2421 cod = COD_UNCLASSIFIED; 2422 2423 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod, 2424 BT_SSP_VARIANT_CONSENT, 0); 2425} 2426 2427 2428 2429/******************************************************************************* 2430** 2431** Function btif_dm_ble_passkey_req_evt 2432** 2433** Description Executes pin request event in btif context 2434** 2435** Returns void 2436** 2437*******************************************************************************/ 2438static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req) 2439{ 2440 bt_bdaddr_t bd_addr; 2441 bt_bdname_t bd_name; 2442 UINT32 cod; 2443 2444 /* Remote name update */ 2445 btif_update_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,NULL,BT_DEVICE_TYPE_BLE); 2446 2447 bdcpy(bd_addr.address, p_pin_req->bd_addr); 2448 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN); 2449 2450 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); 2451 pairing_cb.is_le_only = TRUE; 2452 2453 cod = COD_UNCLASSIFIED; 2454 2455 HAL_CBACK(bt_hal_cbacks, pin_request_cb, 2456 &bd_addr, &bd_name, cod); 2457} 2458 2459 2460void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name, 2461 tBT_DEVICE_TYPE dev_type) 2462{ 2463 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type); 2464} 2465 2466static void btif_dm_ble_tx_test_cback(void *p) 2467{ 2468 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST, 2469 (char *)p, 1, NULL); 2470} 2471 2472static void btif_dm_ble_rx_test_cback(void *p) 2473{ 2474 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST, 2475 (char *)p, 1, NULL); 2476} 2477 2478static void btif_dm_ble_test_end_cback(void *p) 2479{ 2480 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END, 2481 (char *)p, 3, NULL); 2482} 2483/******************************************************************************* 2484** 2485** Function btif_le_test_mode 2486** 2487** Description Sends a HCI BLE Test command to the Controller 2488** 2489** Returns BT_STATUS_SUCCESS on success 2490** 2491*******************************************************************************/ 2492bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len) 2493{ 2494 switch (opcode) { 2495 case HCI_BLE_TRANSMITTER_TEST: 2496 if (len != 3) return BT_STATUS_PARM_INVALID; 2497 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback); 2498 break; 2499 case HCI_BLE_RECEIVER_TEST: 2500 if (len != 1) return BT_STATUS_PARM_INVALID; 2501 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback); 2502 break; 2503 case HCI_BLE_TEST_END: 2504 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback); 2505 break; 2506 default: 2507 BTIF_TRACE_ERROR2("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode); 2508 return BT_STATUS_UNSUPPORTED; 2509 } 2510 return BT_STATUS_SUCCESS; 2511} 2512 2513#endif 2514 2515void btif_dm_on_disable() 2516{ 2517 /* cancel any pending pairing requests */ 2518 if (pairing_cb.state == BT_BOND_STATE_BONDING) 2519 { 2520 bt_bdaddr_t bd_addr; 2521 2522 BTIF_TRACE_DEBUG1("%s: Cancel pending pairing request", __FUNCTION__); 2523 bdcpy(bd_addr.address, pairing_cb.bd_addr); 2524 btif_dm_cancel_bond(&bd_addr); 2525 } 2526} 2527