btif_dm.c revision f1029c7afb9b4c9fcf95b3f98ce9c68b10d5a02c
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 28#define LOG_TAG "bt_btif_dm" 29 30#include <assert.h> 31#include <signal.h> 32#include <stdio.h> 33#include <stdlib.h> 34#include <string.h> 35#include <sys/types.h> 36#include <unistd.h> 37 38#include <hardware/bluetooth.h> 39 40#include <cutils/properties.h> 41#include "gki.h" 42#include "btu.h" 43#include "btcore/include/bdaddr.h" 44#include "bta_api.h" 45#include "btif_api.h" 46#include "btif_util.h" 47#include "btif_dm.h" 48#include "btif_storage.h" 49#include "btif_hh.h" 50#include "btif_config.h" 51#include "btif_sdp.h" 52 53#include "bta_gatt_api.h" 54#include "include/stack_config.h" 55 56#include "osi/include/log.h" 57#include "osi/include/allocator.h" 58 59/****************************************************************************** 60** Device specific workarounds 61******************************************************************************/ 62 63/** 64 * The devices below have proven problematic during the pairing process, often 65 * requiring multiple retries to complete pairing. To avoid degrading the user 66 * experience for other devices, explicitely blacklist troubled devices here. 67 */ 68static const UINT8 blacklist_pairing_retries[][3] = { 69 {0x9C, 0xDF, 0x03} // BMW car kits (Harman/Becker) 70}; 71 72BOOLEAN blacklistPairingRetries(BD_ADDR bd_addr) 73{ 74 const unsigned blacklist_size = sizeof(blacklist_pairing_retries) 75 / sizeof(blacklist_pairing_retries[0]); 76 for (unsigned i = 0; i != blacklist_size; ++i) 77 { 78 if (blacklist_pairing_retries[i][0] == bd_addr[0] && 79 blacklist_pairing_retries[i][1] == bd_addr[1] && 80 blacklist_pairing_retries[i][2] == bd_addr[2]) 81 return TRUE; 82 } 83 return FALSE; 84} 85 86/****************************************************************************** 87** Constants & Macros 88******************************************************************************/ 89 90#define COD_UNCLASSIFIED ((0x1F) << 8) 91#define COD_HID_KEYBOARD 0x0540 92#define COD_HID_POINTING 0x0580 93#define COD_HID_COMBO 0x05C0 94#define COD_HID_MAJOR 0x0500 95#define COD_AV_HEADSETS 0x0404 96#define COD_AV_HANDSFREE 0x0408 97#define COD_AV_HEADPHONES 0x0418 98#define COD_AV_PORTABLE_AUDIO 0x041C 99#define COD_AV_HIFI_AUDIO 0x0428 100 101 102#define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0 103#define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10 104#define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2 105 106#define NUM_TIMEOUT_RETRIES 5 107 108#define PROPERTY_PRODUCT_MODEL "ro.product.model" 109#define DEFAULT_LOCAL_NAME_MAX 31 110#if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN) 111 #error "default btif local name size exceeds stack supported length" 112#endif 113 114#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE) 115#define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2 116#define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2 117#define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3 118#define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4 119#endif 120 121#define MAX_SDP_BL_ENTRIES 3 122 123#define BOND_TYPE_UNKNOWN 0 124#define BOND_TYPE_PERSISTENT 1 125#define BOND_TYPE_TEMPORARY 2 126 127#define ENCRYPTED_BREDR 2 128#define ENCRYPTED_LE 4 129 130typedef struct 131{ 132 bt_bond_state_t state; 133 bt_bdaddr_t static_bdaddr; 134 BD_ADDR bd_addr; 135 UINT8 bond_type; 136 UINT8 pin_code_len; 137 UINT8 is_ssp; 138 UINT8 auth_req; 139 UINT8 io_cap; 140 UINT8 autopair_attempts; 141 UINT8 timeout_retries; 142 UINT8 is_local_initiated; 143 UINT8 sdp_attempts; 144#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 145 BOOLEAN is_le_only; 146 BOOLEAN is_le_nc; /* LE Numeric comparison */ 147 btif_dm_ble_cb_t ble; 148#endif 149} btif_dm_pairing_cb_t; 150 151 152typedef struct 153{ 154 UINT8 ir[BT_OCTET16_LEN]; 155 UINT8 irk[BT_OCTET16_LEN]; 156 UINT8 dhk[BT_OCTET16_LEN]; 157}btif_dm_local_key_id_t; 158 159typedef struct 160{ 161 BOOLEAN is_er_rcvd; 162 UINT8 er[BT_OCTET16_LEN]; 163 BOOLEAN is_id_keys_rcvd; 164 btif_dm_local_key_id_t id_keys; /* ID kyes */ 165 166}btif_dm_local_key_cb_t; 167 168typedef struct 169{ 170 BD_ADDR bd_addr; 171 BD_NAME bd_name; 172} btif_dm_remote_name_t; 173 174typedef struct 175{ 176 BT_OCTET16 sp_c; 177 BT_OCTET16 sp_r; 178 BD_ADDR oob_bdaddr; /* peer bdaddr*/ 179} btif_dm_oob_cb_t; 180 181typedef struct 182{ 183 bt_bdaddr_t bdaddr; 184 UINT8 transport; /* 0=Unknown, 1=BR/EDR, 2=LE */ 185} btif_dm_create_bond_cb_t; 186 187typedef struct 188{ 189 uint8_t status; 190 uint8_t ctrl_state; 191 uint64_t tx_time; 192 uint64_t rx_time; 193 uint64_t idle_time; 194 uint64_t energy_used; 195} btif_activity_energy_info_cb_t; 196 197typedef struct 198{ 199 unsigned int manufact_id; 200}skip_sdp_entry_t; 201 202#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id)) 203 204#define MAX_SDP_BL_ENTRIES 3 205#define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb" 206 207static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard 208 209/* This flag will be true if HCI_Inquiry is in progress */ 210static BOOLEAN btif_dm_inquiry_in_progress = FALSE; 211 212 213 214/************************************************************************************ 215** Static variables 216************************************************************************************/ 217static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'}; 218 219/****************************************************************************** 220** Static functions 221******************************************************************************/ 222static btif_dm_pairing_cb_t pairing_cb; 223static btif_dm_oob_cb_t oob_cb; 224static void btif_dm_generic_evt(UINT16 event, char* p_param); 225static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport); 226static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name); 227static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name, 228 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type); 229#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 230static btif_dm_local_key_cb_t ble_local_key_cb; 231static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif); 232static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl); 233static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req); 234static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req) ; 235#endif 236 237static void bte_scan_filt_param_cfg_evt(UINT8 action_type, 238 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space, 239 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status); 240 241static char* btif_get_default_local_name(); 242/****************************************************************************** 243** Externs 244******************************************************************************/ 245extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID]; 246extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable); 247extern bt_status_t btif_av_execute_service(BOOLEAN b_enable); 248extern bt_status_t btif_av_sink_execute_service(BOOLEAN b_enable); 249extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable); 250extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable); 251extern bt_status_t btif_sdp_execute_service(BOOLEAN b_enable); 252extern int btif_hh_connect(bt_bdaddr_t *bd_addr); 253extern void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16); 254 255 256/****************************************************************************** 257** Functions 258******************************************************************************/ 259 260static void btif_dm_data_copy(uint16_t event, char *dst, char *src) 261{ 262 tBTA_DM_SEC *dst_dm_sec = (tBTA_DM_SEC*)dst; 263 tBTA_DM_SEC *src_dm_sec = (tBTA_DM_SEC*)src; 264 265 if (!src_dm_sec) 266 return; 267 268 assert(dst_dm_sec); 269 memcpy(dst_dm_sec, src_dm_sec, sizeof(tBTA_DM_SEC)); 270 271 if (event == BTA_DM_BLE_KEY_EVT) 272 { 273 dst_dm_sec->ble_key.p_key_value = osi_malloc(sizeof(tBTM_LE_KEY_VALUE)); 274 assert(src_dm_sec->ble_key.p_key_value); 275 assert(dst_dm_sec->ble_key.p_key_value); 276 memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value, sizeof(tBTM_LE_KEY_VALUE)); 277 } 278} 279 280static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC *dm_sec) 281{ 282 if (event == BTA_DM_BLE_KEY_EVT) 283 osi_free(dm_sec->ble_key.p_key_value); 284} 285 286bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id, 287 BOOLEAN b_enable) 288{ 289 BTIF_TRACE_DEBUG("%s service_id: %d", __FUNCTION__, service_id); 290 /* Check the service_ID and invoke the profile's BT state changed API */ 291 switch (service_id) 292 { 293 case BTA_HFP_SERVICE_ID: 294 case BTA_HSP_SERVICE_ID: 295 { 296 btif_hf_execute_service(b_enable); 297 }break; 298 case BTA_A2DP_SOURCE_SERVICE_ID: 299 { 300 btif_av_execute_service(b_enable); 301 }break; 302 case BTA_A2DP_SINK_SERVICE_ID: 303 { 304 btif_av_sink_execute_service(b_enable); 305 }break; 306 case BTA_HID_SERVICE_ID: 307 { 308 btif_hh_execute_service(b_enable); 309 }break; 310 case BTA_HFP_HS_SERVICE_ID: 311 { 312 btif_hf_client_execute_service(b_enable); 313 }break; 314 case BTA_SDP_SERVICE_ID: 315 { 316 btif_sdp_execute_service(b_enable); 317 }break; 318 default: 319 BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__); 320 return BT_STATUS_FAIL; 321 } 322 return BT_STATUS_SUCCESS; 323} 324 325/******************************************************************************* 326** 327** Function check_eir_remote_name 328** 329** Description Check if remote name is in the EIR data 330** 331** Returns TRUE if remote name found 332** Populate p_remote_name, if provided and remote name found 333** 334*******************************************************************************/ 335static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data, 336 UINT8 *p_remote_name, UINT8 *p_remote_name_len) 337{ 338 UINT8 *p_eir_remote_name = NULL; 339 UINT8 remote_name_len = 0; 340 341 /* Check EIR for remote name and services */ 342 if (p_search_data->inq_res.p_eir) 343 { 344 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir, 345 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len); 346 if (!p_eir_remote_name) 347 { 348 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir, 349 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len); 350 } 351 352 if (p_eir_remote_name) 353 { 354 if (remote_name_len > BD_NAME_LEN) 355 remote_name_len = BD_NAME_LEN; 356 357 if (p_remote_name && p_remote_name_len) 358 { 359 memcpy(p_remote_name, p_eir_remote_name, remote_name_len); 360 *(p_remote_name + remote_name_len) = 0; 361 *p_remote_name_len = remote_name_len; 362 } 363 364 return TRUE; 365 } 366 } 367 368 return FALSE; 369 370} 371 372/******************************************************************************* 373** 374** Function check_cached_remote_name 375** 376** Description Check if remote name is in the NVRAM cache 377** 378** Returns TRUE if remote name found 379** Populate p_remote_name, if provided and remote name found 380** 381*******************************************************************************/ 382static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data, 383 UINT8 *p_remote_name, UINT8 *p_remote_name_len) 384{ 385 bt_bdname_t bdname; 386 bt_bdaddr_t remote_bdaddr; 387 bt_property_t prop_name; 388 389 /* check if we already have it in our btif_storage cache */ 390 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr); 391 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME, 392 sizeof(bt_bdname_t), &bdname); 393 if (btif_storage_get_remote_device_property( 394 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS) 395 { 396 if (p_remote_name && p_remote_name_len) 397 { 398 strcpy((char *)p_remote_name, (char *)bdname.name); 399 *p_remote_name_len = strlen((char *)p_remote_name); 400 } 401 return TRUE; 402 } 403 404 return FALSE; 405} 406 407BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod) 408{ 409 uint32_t remote_cod; 410 bt_property_t prop_name; 411 412 /* check if we already have it in our btif_storage cache */ 413 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE, 414 sizeof(uint32_t), &remote_cod); 415 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS) 416 { 417 LOG_INFO("%s remote_cod = 0x%08x cod = 0x%08x", __func__, remote_cod, cod); 418 if ((remote_cod & 0x7ff) == cod) 419 return TRUE; 420 } 421 422 return FALSE; 423} 424 425BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod) 426{ 427 uint32_t remote_cod; 428 bt_property_t prop_name; 429 430 /* check if we already have it in our btif_storage cache */ 431 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE, 432 sizeof(uint32_t), &remote_cod); 433 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, 434 &prop_name) == BT_STATUS_SUCCESS) 435 { 436 BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod); 437 if ((remote_cod & 0x700) == cod) 438 return TRUE; 439 } 440 return FALSE; 441} 442 443BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr) 444{ 445 uint32_t remote_dev_type; 446 bt_property_t prop_name; 447 448 /* check if we already have it in our btif_storage cache */ 449 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE, 450 sizeof(uint32_t), &remote_dev_type); 451 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, 452 &prop_name) == BT_STATUS_SUCCESS) 453 { 454 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE) 455 { 456 bdstr_t bdstr; 457 bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr)); 458 if(btif_config_exist(bdstr, "HidAppId")) 459 return TRUE; 460 } 461 } 462 return FALSE; 463} 464 465/***************************************************************************** 466** 467** Function check_sdp_bl 468** 469** Description Checks if a given device is blacklisted to skip sdp 470** 471** Parameters skip_sdp_entry 472** 473** Returns TRUE if the device is present in blacklist, else FALSE 474** 475*******************************************************************************/ 476BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr) 477{ 478 UINT16 manufacturer = 0; 479 UINT8 lmp_ver = 0; 480 UINT16 lmp_subver = 0; 481 bt_property_t prop_name; 482 bt_remote_version_t info; 483 484 485 if (remote_bdaddr == NULL) 486 return FALSE; 487 488/* fetch additional info about remote device used in iop query */ 489 BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver, 490 &manufacturer, &lmp_subver); 491 492 493 494 /* if not available yet, try fetching from config database */ 495 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO, 496 sizeof(bt_remote_version_t), &info); 497 498 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, 499 &prop_name) != BT_STATUS_SUCCESS) 500 { 501 502 return FALSE; 503 } 504 manufacturer = info.manufacturer; 505 506 for (int i = 0; i < MAX_SDP_BL_ENTRIES; i++) 507 { 508 if (manufacturer == sdp_blacklist[i].manufact_id) 509 return TRUE; 510 } 511 return FALSE; 512} 513 514 515static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state) 516{ 517 // Send bonding state only once - based on outgoing/incoming we may receive duplicates 518 if ((pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING)) 519 { 520 // Cross key pairing so send callback for static address 521 if (!bdaddr_is_empty(&pairing_cb.static_bdaddr)) 522 { 523 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state); 524 } 525 return; 526 } 527 528 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY) 529 state = BT_BOND_STATE_NONE; 530 531 BTIF_TRACE_DEBUG("%s: state=%d, prev_state=%d, sdp_attempts = %d", __func__, 532 state, pairing_cb.state, pairing_cb.sdp_attempts); 533 534 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state); 535 536 if (state == BT_BOND_STATE_BONDING) 537 { 538 pairing_cb.state = state; 539 bdcpy(pairing_cb.bd_addr, bd_addr->address); 540 } else { 541 if (!pairing_cb.sdp_attempts) 542 memset(&pairing_cb, 0, sizeof(pairing_cb)); 543 else 544 BTIF_TRACE_DEBUG("%s: BR-EDR service discovery active", __func__); 545 } 546} 547 548/* store remote version in bt config to always have access 549 to it post pairing*/ 550static void btif_update_remote_version_property(bt_bdaddr_t *p_bd) 551{ 552 bt_property_t property; 553 UINT8 lmp_ver = 0; 554 UINT16 lmp_subver = 0; 555 UINT16 mfct_set = 0; 556 tBTM_STATUS btm_status; 557 bt_remote_version_t info; 558 bt_status_t status; 559 bdstr_t bdstr; 560 561 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver, 562 &mfct_set, &lmp_subver); 563 564 LOG_DEBUG("remote version info [%s]: %x, %x, %x", bdaddr_to_string(p_bd, bdstr, sizeof(bdstr)), 565 lmp_ver, mfct_set, lmp_subver); 566 567 if (btm_status == BTM_SUCCESS) 568 { 569 // Always update cache to ensure we have availability whenever BTM API is not populated 570 info.manufacturer = mfct_set; 571 info.sub_ver = lmp_subver; 572 info.version = lmp_ver; 573 BTIF_STORAGE_FILL_PROPERTY(&property, 574 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t), 575 &info); 576 status = btif_storage_set_remote_device_property(p_bd, &property); 577 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status); 578 } 579} 580 581 582static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name, 583 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type) 584{ 585 int num_properties = 0; 586 bt_property_t properties[3]; 587 bt_bdaddr_t bdaddr; 588 bt_status_t status; 589 UINT32 cod; 590 bt_device_type_t dev_type; 591 592 memset(properties, 0, sizeof(properties)); 593 bdcpy(bdaddr.address, bd_addr); 594 595 /* remote name */ 596 if (strlen((const char *) bd_name)) 597 { 598 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 599 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name); 600 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]); 601 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status); 602 num_properties++; 603 } 604 605 /* class of device */ 606 cod = devclass2uint(dev_class); 607 BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod); 608 if ( cod == 0) { 609 /* Try to retrieve cod from storage */ 610 BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__); 611 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 612 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod); 613 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]); 614 BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod); 615 if ( cod == 0) { 616 BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__); 617 cod = COD_UNCLASSIFIED; 618 } 619 } 620 621 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 622 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod); 623 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]); 624 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status); 625 num_properties++; 626 627 /* device type */ 628 bt_property_t prop_name; 629 uint8_t remote_dev_type; 630 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_TYPE_OF_DEVICE, 631 sizeof(uint8_t), &remote_dev_type); 632 if (btif_storage_get_remote_device_property(&bdaddr, &prop_name) == BT_STATUS_SUCCESS) 633 dev_type = remote_dev_type | device_type; 634 else 635 dev_type = device_type; 636 637 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 638 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type); 639 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]); 640 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status); 641 num_properties++; 642 643 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, 644 status, &bdaddr, num_properties, properties); 645} 646 647/******************************************************************************* 648** 649** Function btif_dm_cb_hid_remote_name 650** 651** Description Remote name callback for HID device. Called in btif context 652** Special handling for HID devices 653** 654** Returns void 655** 656*******************************************************************************/ 657static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name) 658{ 659 BTIF_TRACE_DEBUG("%s: status=%d pairing_cb.state=%d", __FUNCTION__, p_remote_name->status, pairing_cb.state); 660 if (pairing_cb.state == BT_BOND_STATE_BONDING) 661 { 662 bt_bdaddr_t remote_bd; 663 664 bdcpy(remote_bd.address, pairing_cb.bd_addr); 665 666 if (p_remote_name->status == BTM_SUCCESS) 667 { 668 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED); 669 } 670 else 671 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE); 672 } 673} 674 675/******************************************************************************* 676** 677** Function btif_dm_cb_create_bond 678** 679** Description Create bond initiated from the BTIF thread context 680** Special handling for HID devices 681** 682** Returns void 683** 684*******************************************************************************/ 685static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport) 686{ 687 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING); 688 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING); 689 690#if BLE_INCLUDED == TRUE 691 int device_type; 692 int addr_type; 693 bdstr_t bdstr; 694 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)); 695 if (transport == BT_TRANSPORT_LE) 696 { 697 if (!btif_config_get_int((char const *)&bdstr,"DevType", &device_type)) 698 { 699 btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE); 700 } 701 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS) 702 { 703 btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC); 704 } 705 } 706 if((btif_config_get_int((char const *)&bdstr,"DevType", &device_type) && 707 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) && 708 (device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE) || (transport == BT_TRANSPORT_LE)) 709 { 710 BTA_DmAddBleDevice(bd_addr->address, addr_type, device_type); 711 } 712#endif 713 714#if BLE_INCLUDED == TRUE 715 if(is_hid && (device_type & BT_DEVICE_TYPE_BLE) == 0) 716#else 717 if(is_hid) 718#endif 719 { 720 int status; 721 status = btif_hh_connect(bd_addr); 722 if(status != BT_STATUS_SUCCESS) 723 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE); 724 } 725 else 726 { 727 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport); 728 } 729 /* Track originator of bond creation */ 730 pairing_cb.is_local_initiated = TRUE; 731 732} 733 734/******************************************************************************* 735** 736** Function btif_dm_cb_remove_bond 737** 738** Description remove bond initiated from the BTIF thread context 739** Special handling for HID devices 740** 741** Returns void 742** 743*******************************************************************************/ 744void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr) 745{ 746 /*special handling for HID devices */ 747 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there 748 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/ 749#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE)) 750 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS) 751#endif 752 { 753 BTIF_TRACE_DEBUG("%s: Removing HH device", __func__); 754 BTA_DmRemoveDevice((UINT8 *)bd_addr->address); 755 } 756} 757 758/******************************************************************************* 759** 760** Function btif_dm_get_connection_state 761** 762** Description Returns whether the remote device is currently connected 763** and whether encryption is active for the connection 764** 765** Returns 0 if not connected; 1 if connected and > 1 if connection is 766** encrypted 767** 768*******************************************************************************/ 769uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr) 770{ 771 uint8_t *bda = (uint8_t*)bd_addr->address; 772 uint16_t rc = BTA_DmGetConnectionState(bda); 773 774 if (rc != 0) 775 { 776 uint8_t flags = 0; 777 778 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR); 779 BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __FUNCTION__, flags); 780 if (flags & BTM_SEC_FLAG_ENCRYPTED) 781 rc |= ENCRYPTED_BREDR; 782 783 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE); 784 BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __FUNCTION__, flags); 785 if (flags & BTM_SEC_FLAG_ENCRYPTED) 786 rc |= ENCRYPTED_LE; 787 } 788 789 return rc; 790} 791 792/******************************************************************************* 793** 794** Function search_devices_copy_cb 795** 796** Description Deep copy callback for search devices event 797** 798** Returns void 799** 800*******************************************************************************/ 801static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src) 802{ 803 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest; 804 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src; 805 806 if (!p_src) 807 return; 808 809 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event)); 810 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH)); 811 switch (event) 812 { 813 case BTA_DM_INQ_RES_EVT: 814 { 815 if (p_src_data->inq_res.p_eir) 816 { 817 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH)); 818 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN); 819 } 820 } 821 break; 822 823 case BTA_DM_DISC_RES_EVT: 824 { 825 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data) 826 { 827 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH)); 828 memcpy(p_dest_data->disc_res.p_raw_data, 829 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size); 830 } 831 } 832 break; 833 } 834} 835 836static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src) 837{ 838 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest; 839 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src; 840 841 if (!p_src) 842 return; 843 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH)); 844 switch (event) 845 { 846 case BTA_DM_DISC_RES_EVT: 847 { 848 if (p_src_data->disc_res.result == BTA_SUCCESS) 849 { 850 if (p_src_data->disc_res.num_uuids > 0) 851 { 852 p_dest_data->disc_res.p_uuid_list = 853 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH)); 854 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list, 855 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE); 856 GKI_freebuf(p_src_data->disc_res.p_uuid_list); 857 } 858 if (p_src_data->disc_res.p_raw_data != NULL) 859 { 860 GKI_freebuf(p_src_data->disc_res.p_raw_data); 861 } 862 } 863 } break; 864 } 865} 866/****************************************************************************** 867** 868** BTIF DM callback events 869** 870*****************************************************************************/ 871 872/******************************************************************************* 873** 874** Function btif_dm_pin_req_evt 875** 876** Description Executes pin request event in btif context 877** 878** Returns void 879** 880*******************************************************************************/ 881static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req) 882{ 883 bt_bdaddr_t bd_addr; 884 bt_bdname_t bd_name; 885 UINT32 cod; 886 bt_pin_code_t pin_code; 887 int dev_type; 888 889 /* Remote properties update */ 890 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type)) 891 { 892 dev_type = BT_DEVICE_TYPE_BREDR; 893 } 894 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name, 895 p_pin_req->dev_class, (tBT_DEVICE_TYPE) dev_type); 896 897 bdcpy(bd_addr.address, p_pin_req->bd_addr); 898 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN); 899 900 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); 901 902 cod = devclass2uint(p_pin_req->dev_class); 903 904 if (cod == 0) { 905 BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__); 906 cod = COD_UNCLASSIFIED; 907 } 908 909 /* check for auto pair possiblity only if bond was initiated by local device */ 910 if (pairing_cb.is_local_initiated && (p_pin_req->min_16_digit == FALSE)) 911 { 912 if (check_cod(&bd_addr, COD_AV_HEADSETS) || 913 check_cod(&bd_addr, COD_AV_HANDSFREE) || 914 check_cod(&bd_addr, COD_AV_HEADPHONES) || 915 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) || 916 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) || 917 check_cod(&bd_addr, COD_HID_POINTING)) 918 { 919 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__); 920 /* Check if this device can be auto paired */ 921 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) && 922 (pairing_cb.autopair_attempts == 0)) 923 { 924 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__); 925 pin_code.pin[0] = 0x30; 926 pin_code.pin[1] = 0x30; 927 pin_code.pin[2] = 0x30; 928 pin_code.pin[3] = 0x30; 929 930 pairing_cb.autopair_attempts++; 931 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin); 932 return; 933 } 934 } 935 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) || 936 check_cod(&bd_addr, COD_HID_COMBO)) 937 { 938 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) && 939 (pairing_cb.autopair_attempts == 0)) 940 { 941 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__); 942 pin_code.pin[0] = 0x30; 943 pin_code.pin[1] = 0x30; 944 pin_code.pin[2] = 0x30; 945 pin_code.pin[3] = 0x30; 946 947 pairing_cb.autopair_attempts++; 948 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin); 949 return; 950 } 951 } 952 } 953 HAL_CBACK(bt_hal_cbacks, pin_request_cb, 954 &bd_addr, &bd_name, cod, p_pin_req->min_16_digit); 955} 956 957/******************************************************************************* 958** 959** Function btif_dm_ssp_cfm_req_evt 960** 961** Description Executes SSP confirm request event in btif context 962** 963** Returns void 964** 965*******************************************************************************/ 966static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req) 967{ 968 bt_bdaddr_t bd_addr; 969 bt_bdname_t bd_name; 970 UINT32 cod; 971 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING); 972 int dev_type; 973 974 BTIF_TRACE_DEBUG("%s", __FUNCTION__); 975 976 /* Remote properties update */ 977 if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type)) 978 { 979 dev_type = BT_DEVICE_TYPE_BREDR; 980 } 981 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name, 982 p_ssp_cfm_req->dev_class, (tBT_DEVICE_TYPE) dev_type); 983 984 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr); 985 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN); 986 987 /* Set the pairing_cb based on the local & remote authentication requirements */ 988 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); 989 990 /* if just_works and bonding bit is not set treat this as temporary */ 991 if (p_ssp_cfm_req->just_works && !(p_ssp_cfm_req->loc_auth_req & BTM_AUTH_BONDS) && 992 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) && 993 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING))) 994 pairing_cb.bond_type = BOND_TYPE_TEMPORARY; 995 else 996 pairing_cb.bond_type = BOND_TYPE_PERSISTENT; 997 998 pairing_cb.is_ssp = TRUE; 999 1000 /* If JustWorks auto-accept */ 1001 if (p_ssp_cfm_req->just_works) 1002 { 1003 /* Pairing consent for JustWorks needed if: 1004 * 1. Incoming (non-temporary) pairing is detected AND 1005 * 2. local IO capabilities are DisplayYesNo AND 1006 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput; 1007 */ 1008 if (is_incoming && pairing_cb.bond_type != BOND_TYPE_TEMPORARY && 1009 ((p_ssp_cfm_req->loc_io_caps == HCI_IO_CAP_DISPLAY_YESNO) && 1010 (p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_DISPLAY_ONLY || 1011 p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_NO_IO))) 1012 { 1013 BTIF_TRACE_EVENT("%s: User consent needed for incoming pairing request. loc_io_caps: %d, rmt_io_caps: %d", 1014 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps); 1015 } 1016 else 1017 { 1018 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__); 1019 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0); 1020 return; 1021 } 1022 } 1023 1024 cod = devclass2uint(p_ssp_cfm_req->dev_class); 1025 1026 if (cod == 0) { 1027 LOG_DEBUG("%s cod is 0, set as unclassified", __func__); 1028 cod = COD_UNCLASSIFIED; 1029 } 1030 1031 pairing_cb.sdp_attempts = 0; 1032 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod, 1033 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION), 1034 p_ssp_cfm_req->num_val); 1035} 1036 1037static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif) 1038{ 1039 bt_bdaddr_t bd_addr; 1040 bt_bdname_t bd_name; 1041 UINT32 cod; 1042 int dev_type; 1043 1044 BTIF_TRACE_DEBUG("%s", __FUNCTION__); 1045 1046 /* Remote properties update */ 1047 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type)) 1048 { 1049 dev_type = BT_DEVICE_TYPE_BREDR; 1050 } 1051 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name, 1052 p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE) dev_type); 1053 1054 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr); 1055 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN); 1056 1057 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); 1058 pairing_cb.is_ssp = TRUE; 1059 cod = devclass2uint(p_ssp_key_notif->dev_class); 1060 1061 if (cod == 0) { 1062 LOG_DEBUG("%s cod is 0, set as unclassified", __func__); 1063 cod = COD_UNCLASSIFIED; 1064 } 1065 1066 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, 1067 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION, 1068 p_ssp_key_notif->passkey); 1069} 1070/******************************************************************************* 1071** 1072** Function btif_dm_auth_cmpl_evt 1073** 1074** Description Executes authentication complete event in btif context 1075** 1076** Returns void 1077** 1078*******************************************************************************/ 1079static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) 1080{ 1081 /* Save link key, if not temporary */ 1082 bt_bdaddr_t bd_addr; 1083 bt_status_t status = BT_STATUS_FAIL; 1084 bt_bond_state_t state = BT_BOND_STATE_NONE; 1085 BOOLEAN skip_sdp = FALSE; 1086 1087 BTIF_TRACE_DEBUG("%s: bond state=%d", __func__, pairing_cb.state); 1088 1089 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr); 1090 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) ) 1091 { 1092 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) || (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) || 1093 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || pairing_cb.bond_type == BOND_TYPE_PERSISTENT) 1094 { 1095 bt_status_t ret; 1096 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d", 1097 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type); 1098 ret = btif_storage_add_bonded_device(&bd_addr, 1099 p_auth_cmpl->key, p_auth_cmpl->key_type, 1100 pairing_cb.pin_code_len); 1101 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret); 1102 } 1103 else 1104 { 1105 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d", 1106 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type); 1107 if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY) 1108 { 1109 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing", 1110 __FUNCTION__); 1111 if (btif_storage_is_device_bonded(&bd_addr) == TRUE) { 1112 btif_storage_remove_bonded_device(&bd_addr); 1113 } 1114 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE); 1115 return; 1116 } 1117 } 1118 } 1119 1120 // Skip SDP for certain HID Devices 1121 if (p_auth_cmpl->success) 1122 { 1123#if BLE_INCLUDED == TRUE 1124 btif_storage_set_remote_addr_type(&bd_addr, p_auth_cmpl->addr_type); 1125#endif 1126 btif_update_remote_properties(p_auth_cmpl->bd_addr, 1127 p_auth_cmpl->bd_name, NULL, p_auth_cmpl->dev_type); 1128 pairing_cb.timeout_retries = 0; 1129 status = BT_STATUS_SUCCESS; 1130 state = BT_BOND_STATE_BONDED; 1131 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr); 1132 1133 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr, COD_HID_MAJOR)) 1134 { 1135 LOG_WARN("%s:skip SDP", __FUNCTION__); 1136 skip_sdp = TRUE; 1137 } 1138 if(!pairing_cb.is_local_initiated && skip_sdp) 1139 { 1140 bond_state_changed(status, &bd_addr, state); 1141 1142 LOG_WARN("%s: Incoming HID Connection",__FUNCTION__); 1143 bt_property_t prop; 1144 bt_bdaddr_t bd_addr; 1145 bt_uuid_t uuid; 1146 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE; 1147 1148 string_to_uuid(uuid_str, &uuid); 1149 1150 prop.type = BT_PROPERTY_UUIDS; 1151 prop.val = uuid.uu; 1152 prop.len = MAX_UUID_SIZE; 1153 1154 /* Send the event to the BTIF */ 1155 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, 1156 BT_STATUS_SUCCESS, &bd_addr, 1, &prop); 1157 } 1158 else 1159 { 1160 /* Trigger SDP on the device */ 1161 pairing_cb.sdp_attempts = 1;; 1162 1163#if BLE_INCLUDED == TRUE 1164 /* If bonded due to cross-key, save the static address too*/ 1165 if(pairing_cb.state == BT_BOND_STATE_BONDING && 1166 (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0)) 1167 { 1168 BTIF_TRACE_DEBUG("%s: bonding initiated due to cross key, adding static address", 1169 __func__); 1170 bdcpy(pairing_cb.static_bdaddr.address, p_auth_cmpl->bd_addr); 1171 } 1172#endif 1173 1174 if(btif_dm_inquiry_in_progress) 1175 btif_dm_cancel_discovery(); 1176 1177 btif_dm_get_remote_services(&bd_addr); 1178 } 1179 // Do not call bond_state_changed_cb yet. Wait until remote service discovery is complete 1180 } 1181 else 1182 { 1183 // Map the HCI fail reason to bt status 1184 switch(p_auth_cmpl->fail_reason) 1185 { 1186 case HCI_ERR_PAGE_TIMEOUT: 1187 if (blacklistPairingRetries(bd_addr.address) && pairing_cb.timeout_retries) 1188 { 1189 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __FUNCTION__, pairing_cb.timeout_retries); 1190 --pairing_cb.timeout_retries; 1191 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN); 1192 return; 1193 } 1194 /* Fall-through */ 1195 case HCI_ERR_CONNECTION_TOUT: 1196 status = BT_STATUS_RMT_DEV_DOWN; 1197 break; 1198 1199 case HCI_ERR_PAIRING_NOT_ALLOWED: 1200 status = BT_STATUS_AUTH_REJECTED; 1201 break; 1202 1203 case HCI_ERR_LMP_RESPONSE_TIMEOUT: 1204 status = BT_STATUS_AUTH_FAILURE; 1205 break; 1206 1207 /* map the auth failure codes, so we can retry pairing if necessary */ 1208 case HCI_ERR_AUTH_FAILURE: 1209 case HCI_ERR_KEY_MISSING: 1210 btif_storage_remove_bonded_device(&bd_addr); 1211 case HCI_ERR_HOST_REJECT_SECURITY: 1212 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE: 1213 case HCI_ERR_UNIT_KEY_USED: 1214 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED: 1215 case HCI_ERR_INSUFFCIENT_SECURITY: 1216 case HCI_ERR_PEER_USER: 1217 case HCI_ERR_UNSPECIFIED: 1218 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d", 1219 __FUNCTION__, p_auth_cmpl->fail_reason); 1220 if (pairing_cb.autopair_attempts == 1) 1221 { 1222 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__); 1223 1224 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */ 1225 if (check_cod(&bd_addr, COD_AV_HEADSETS) || 1226 check_cod(&bd_addr, COD_AV_HANDSFREE) || 1227 check_cod(&bd_addr, COD_AV_HEADPHONES) || 1228 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) || 1229 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) || 1230 check_cod(&bd_addr, COD_HID_POINTING)) 1231 { 1232 btif_storage_add_device_to_autopair_blacklist (&bd_addr); 1233 } 1234 pairing_cb.autopair_attempts++; 1235 1236 /* Create the Bond once again */ 1237 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__); 1238 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN); 1239 return; 1240 } 1241 else 1242 { 1243 /* if autopair attempts are more than 1, or not attempted */ 1244 status = BT_STATUS_AUTH_FAILURE; 1245 } 1246 break; 1247 1248 default: 1249 status = BT_STATUS_FAIL; 1250 } 1251 /* Special Handling for HID Devices */ 1252 if (check_cod(&bd_addr, COD_HID_POINTING)) { 1253 /* Remove Device as bonded in nvram as authentication failed */ 1254 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__); 1255 btif_storage_remove_bonded_device(&bd_addr); 1256 } 1257 bond_state_changed(status, &bd_addr, state); 1258 } 1259} 1260 1261/****************************************************************************** 1262** 1263** Function btif_dm_search_devices_evt 1264** 1265** Description Executes search devices callback events in btif context 1266** 1267** Returns void 1268** 1269******************************************************************************/ 1270static void btif_dm_search_devices_evt (UINT16 event, char *p_param) 1271{ 1272 tBTA_DM_SEARCH *p_search_data; 1273 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event)); 1274 1275 switch (event) 1276 { 1277 case BTA_DM_DISC_RES_EVT: 1278 { 1279 p_search_data = (tBTA_DM_SEARCH *)p_param; 1280 /* Remote name update */ 1281 if (strlen((const char *) p_search_data->disc_res.bd_name)) 1282 { 1283 bt_property_t properties[1]; 1284 bt_bdaddr_t bdaddr; 1285 bt_status_t status; 1286 1287 properties[0].type = BT_PROPERTY_BDNAME; 1288 properties[0].val = p_search_data->disc_res.bd_name; 1289 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name); 1290 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr); 1291 1292 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]); 1293 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status); 1294 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, 1295 status, &bdaddr, 1, properties); 1296 } 1297 /* TODO: Services? */ 1298 } 1299 break; 1300 1301 case BTA_DM_INQ_RES_EVT: 1302 { 1303 /* inquiry result */ 1304 UINT32 cod; 1305 bt_bdname_t bdname; 1306 bt_bdaddr_t bdaddr; 1307 UINT8 remote_name_len; 1308 tBTA_SERVICE_MASK services = 0; 1309 bdstr_t bdstr; 1310 1311 p_search_data = (tBTA_DM_SEARCH *)p_param; 1312 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr); 1313 1314 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bdaddr_to_string(&bdaddr, bdstr, sizeof(bdstr)), 1315#if (BLE_INCLUDED == TRUE) 1316 p_search_data->inq_res.device_type); 1317#else 1318 BT_DEVICE_TYPE_BREDR); 1319#endif 1320 bdname.name[0] = 0; 1321 1322 cod = devclass2uint (p_search_data->inq_res.dev_class); 1323 1324 if (cod == 0) { 1325 LOG_DEBUG("%s cod is 0, set as unclassified", __func__); 1326 cod = COD_UNCLASSIFIED; 1327 } 1328 1329 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len)) 1330 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len); 1331 1332 /* Check EIR for remote name and services */ 1333 if (p_search_data->inq_res.p_eir) 1334 { 1335 BTA_GetEirService(p_search_data->inq_res.p_eir, &services); 1336 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services); 1337 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */ 1338 } 1339 1340 1341 { 1342 bt_property_t properties[5]; 1343 bt_device_type_t dev_type; 1344 UINT8 addr_type; 1345 uint32_t num_properties = 0; 1346 bt_status_t status; 1347 1348 memset(properties, 0, sizeof(properties)); 1349 /* BD_ADDR */ 1350 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 1351 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr); 1352 num_properties++; 1353 /* BD_NAME */ 1354 /* Don't send BDNAME if it is empty */ 1355 if (bdname.name[0]) 1356 { 1357 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 1358 BT_PROPERTY_BDNAME, 1359 strlen((char *)bdname.name), &bdname); 1360 num_properties++; 1361 } 1362 1363 /* DEV_CLASS */ 1364 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 1365 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod); 1366 num_properties++; 1367 /* DEV_TYPE */ 1368#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 1369 /* FixMe: Assumption is that bluetooth.h and BTE enums match */ 1370 dev_type = p_search_data->inq_res.device_type; 1371 addr_type = p_search_data->inq_res.ble_addr_type; 1372#else 1373 dev_type = BT_DEVICE_TYPE_BREDR; 1374#endif 1375 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 1376 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type); 1377 num_properties++; 1378 /* RSSI */ 1379 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], 1380 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t), 1381 &(p_search_data->inq_res.rssi)); 1382 num_properties++; 1383 1384 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties); 1385 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status); 1386#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 1387 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type); 1388 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status); 1389#endif 1390 /* Callback to notify upper layer of device */ 1391 HAL_CBACK(bt_hal_cbacks, device_found_cb, 1392 num_properties, properties); 1393 } 1394 } 1395 break; 1396 1397 case BTA_DM_INQ_CMPL_EVT: 1398 { 1399#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 1400 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param; 1401 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS)); 1402 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL, 1403 bte_scan_filt_param_cfg_evt, 0); 1404#endif 1405 } 1406 break; 1407 case BTA_DM_DISC_CMPL_EVT: 1408 { 1409 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED); 1410 } 1411 break; 1412 case BTA_DM_SEARCH_CANCEL_CMPL_EVT: 1413 { 1414 /* if inquiry is not in progress and we get a cancel event, then 1415 * it means we are done with inquiry, but remote_name fetches are in 1416 * progress 1417 * 1418 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt 1419 * but instead wait for the cancel_cmpl_evt via the Busy Level 1420 * 1421 */ 1422 if (btif_dm_inquiry_in_progress == FALSE) 1423 { 1424#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 1425 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param; 1426 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS)); 1427 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL, 1428 bte_scan_filt_param_cfg_evt, 0); 1429#endif 1430 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED); 1431 } 1432 } 1433 break; 1434 } 1435} 1436 1437/******************************************************************************* 1438** 1439** Function btif_dm_search_services_evt 1440** 1441** Description Executes search services event in btif context 1442** 1443** Returns void 1444** 1445*******************************************************************************/ 1446static void btif_dm_search_services_evt(UINT16 event, char *p_param) 1447{ 1448 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param; 1449 1450 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event); 1451 switch (event) 1452 { 1453 case BTA_DM_DISC_RES_EVT: 1454 { 1455 bt_property_t prop; 1456 uint32_t i = 0; 1457 bt_bdaddr_t bd_addr; 1458 bt_status_t ret; 1459 1460 bdcpy(bd_addr.address, p_data->disc_res.bd_addr); 1461 1462 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__, 1463 p_data->disc_res.result, p_data->disc_res.services); 1464 if ((p_data->disc_res.result != BTA_SUCCESS) && 1465 (pairing_cb.state == BT_BOND_STATE_BONDING ) && 1466 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING)) 1467 { 1468 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__); 1469 pairing_cb.sdp_attempts++; 1470 btif_dm_get_remote_services(&bd_addr); 1471 return; 1472 } 1473 prop.type = BT_PROPERTY_UUIDS; 1474 prop.len = 0; 1475 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) 1476 { 1477 prop.val = p_data->disc_res.p_uuid_list; 1478 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE; 1479 for (i=0; i < p_data->disc_res.num_uuids; i++) 1480 { 1481 char temp[256]; 1482 uuid_to_string_legacy((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp); 1483 LOG_INFO("%s index:%d uuid:%s", __func__, i, temp); 1484 } 1485 } 1486 1487 /* onUuidChanged requires getBondedDevices to be populated. 1488 ** bond_state_changed needs to be sent prior to remote_device_property 1489 */ 1490 if ((pairing_cb.state == BT_BOND_STATE_BONDING) && 1491 ((bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0) || 1492 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0)) && 1493 pairing_cb.sdp_attempts > 0) 1494 { 1495 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED", 1496 __FUNCTION__); 1497 pairing_cb.sdp_attempts = 0; 1498 1499 // If bonding occured due to cross-key pairing, send bonding callback 1500 // for static address now 1501 if (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0) 1502 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); 1503 1504 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED); 1505 } 1506 1507 if (p_data->disc_res.num_uuids != 0) 1508 { 1509 /* Also write this to the NVRAM */ 1510 ret = btif_storage_set_remote_device_property(&bd_addr, &prop); 1511 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret); 1512 /* Send the event to the BTIF */ 1513 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, 1514 BT_STATUS_SUCCESS, &bd_addr, 1, &prop); 1515 } 1516 } 1517 break; 1518 1519 case BTA_DM_DISC_CMPL_EVT: 1520 /* fixme */ 1521 break; 1522 1523#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 1524 case BTA_DM_DISC_BLE_RES_EVT: 1525 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__, 1526 p_data->disc_ble_res.service.uu.uuid16); 1527 bt_uuid_t uuid; 1528 int i = 0; 1529 int j = 15; 1530 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID) 1531 { 1532 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__); 1533 bt_property_t prop; 1534 bt_bdaddr_t bd_addr; 1535 char temp[256]; 1536 bt_status_t ret; 1537 1538 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16); 1539 1540 while(i < j ) 1541 { 1542 unsigned char c = uuid.uu[j]; 1543 uuid.uu[j] = uuid.uu[i]; 1544 uuid.uu[i] = c; 1545 i++; 1546 j--; 1547 } 1548 1549 uuid_to_string_legacy(&uuid, temp); 1550 LOG_INFO("%s uuid:%s", __func__, temp); 1551 1552 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr); 1553 prop.type = BT_PROPERTY_UUIDS; 1554 prop.val = uuid.uu; 1555 prop.len = MAX_UUID_SIZE; 1556 1557 /* Also write this to the NVRAM */ 1558 ret = btif_storage_set_remote_device_property(&bd_addr, &prop); 1559 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret); 1560 1561 /* Send the event to the BTIF */ 1562 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, 1563 BT_STATUS_SUCCESS, &bd_addr, 1, &prop); 1564 1565 } 1566 break; 1567#endif /* BLE_INCLUDED */ 1568 1569 default: 1570 { 1571 ASSERTC(0, "unhandled search services event", event); 1572 } 1573 break; 1574 } 1575} 1576 1577/******************************************************************************* 1578** 1579** Function btif_dm_remote_service_record_evt 1580** 1581** Description Executes search service record event in btif context 1582** 1583** Returns void 1584** 1585*******************************************************************************/ 1586static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param) 1587{ 1588 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param; 1589 1590 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event); 1591 switch (event) 1592 { 1593 case BTA_DM_DISC_RES_EVT: 1594 { 1595 bt_service_record_t rec; 1596 bt_property_t prop; 1597 bt_bdaddr_t bd_addr; 1598 1599 memset(&rec, 0, sizeof(bt_service_record_t)); 1600 bdcpy(bd_addr.address, p_data->disc_res.bd_addr); 1601 1602 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__, 1603 p_data->disc_res.result, p_data->disc_res.services); 1604 prop.type = BT_PROPERTY_SERVICE_RECORD; 1605 prop.val = (void*)&rec; 1606 prop.len = sizeof(rec); 1607 1608 /* disc_res.result is overloaded with SCN. Cannot check result */ 1609 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK; 1610 /* TODO: Get the UUID as well */ 1611 rec.channel = p_data->disc_res.result - 3; 1612 /* TODO: Need to get the service name using p_raw_data */ 1613 rec.name[0] = 0; 1614 1615 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, 1616 BT_STATUS_SUCCESS, &bd_addr, 1, &prop); 1617 } 1618 break; 1619 1620 default: 1621 { 1622 ASSERTC(0, "unhandled remote service record event", event); 1623 } 1624 break; 1625 } 1626} 1627 1628/******************************************************************************* 1629** 1630** Function btif_dm_upstreams_cback 1631** 1632** Description Executes UPSTREAMS events in btif context 1633** 1634** Returns void 1635** 1636*******************************************************************************/ 1637static void btif_dm_upstreams_evt(UINT16 event, char* p_param) 1638{ 1639 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param; 1640 tBTA_SERVICE_MASK service_mask; 1641 uint32_t i; 1642 bt_bdaddr_t bd_addr; 1643 1644 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event)); 1645 1646 switch (event) 1647 { 1648 case BTA_DM_ENABLE_EVT: 1649 { 1650 BD_NAME bdname; 1651 bt_status_t status; 1652 bt_property_t prop; 1653 prop.type = BT_PROPERTY_BDNAME; 1654 prop.len = BD_NAME_LEN; 1655 prop.val = (void*)bdname; 1656 1657 status = btif_storage_get_adapter_property(&prop); 1658 if (status == BT_STATUS_SUCCESS) 1659 { 1660 /* A name exists in the storage. Make this the device name */ 1661 BTA_DmSetDeviceName((char*)prop.val); 1662 } 1663 else 1664 { 1665 /* Storage does not have a name yet. 1666 * Use the default name and write it to the chip 1667 */ 1668 BTA_DmSetDeviceName(btif_get_default_local_name()); 1669 } 1670 1671#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 1672 /* Enable local privacy */ 1673 BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED); 1674#endif 1675 1676 /* for each of the enabled services in the mask, trigger the profile 1677 * enable */ 1678 service_mask = btif_get_enabled_services_mask(); 1679 for (i=0; i <= BTA_MAX_SERVICE_ID; i++) 1680 { 1681 if (service_mask & 1682 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i))) 1683 { 1684 btif_in_execute_service_request(i, TRUE); 1685 } 1686 } 1687 /* clear control blocks */ 1688 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t)); 1689 pairing_cb.bond_type = BOND_TYPE_PERSISTENT; 1690 1691 /* This function will also trigger the adapter_properties_cb 1692 ** and bonded_devices_info_cb 1693 */ 1694 btif_storage_load_bonded_devices(); 1695 1696 btif_storage_load_autopair_device_list(); 1697 1698 btif_enable_bluetooth_evt(p_data->enable.status); 1699 } 1700 break; 1701 1702 case BTA_DM_DISABLE_EVT: 1703 /* for each of the enabled services in the mask, trigger the profile 1704 * disable */ 1705 service_mask = btif_get_enabled_services_mask(); 1706 for (i=0; i <= BTA_MAX_SERVICE_ID; i++) 1707 { 1708 if (service_mask & 1709 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i))) 1710 { 1711 btif_in_execute_service_request(i, FALSE); 1712 } 1713 } 1714 btif_disable_bluetooth_evt(); 1715 break; 1716 1717 case BTA_DM_PIN_REQ_EVT: 1718 btif_dm_pin_req_evt(&p_data->pin_req); 1719 break; 1720 1721 case BTA_DM_AUTH_CMPL_EVT: 1722 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl); 1723 break; 1724 1725 case BTA_DM_BOND_CANCEL_CMPL_EVT: 1726 if (pairing_cb.state == BT_BOND_STATE_BONDING) 1727 { 1728 bdcpy(bd_addr.address, pairing_cb.bd_addr); 1729 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE); 1730 } 1731 break; 1732 1733 case BTA_DM_SP_CFM_REQ_EVT: 1734 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req); 1735 break; 1736 case BTA_DM_SP_KEY_NOTIF_EVT: 1737 btif_dm_ssp_key_notif_evt(&p_data->key_notif); 1738 break; 1739 1740 case BTA_DM_DEV_UNPAIRED_EVT: 1741 bdcpy(bd_addr.address, p_data->link_down.bd_addr); 1742 1743 /*special handling for HID devices */ 1744 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE)) 1745 btif_hh_remove_device(bd_addr); 1746 #endif 1747 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 1748 btif_storage_remove_ble_bonding_keys(&bd_addr); 1749 #endif 1750 btif_storage_remove_bonded_device(&bd_addr); 1751 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE); 1752 break; 1753 1754 case BTA_DM_BUSY_LEVEL_EVT: 1755 { 1756 1757 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK) 1758 { 1759 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED) 1760 { 1761 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, 1762 BT_DISCOVERY_STARTED); 1763 btif_dm_inquiry_in_progress = TRUE; 1764 } 1765 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED) 1766 { 1767 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, 1768 BT_DISCOVERY_STOPPED); 1769 btif_dm_inquiry_in_progress = FALSE; 1770 } 1771 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE) 1772 { 1773 btif_dm_inquiry_in_progress = FALSE; 1774 } 1775 } 1776 }break; 1777 1778 case BTA_DM_LINK_UP_EVT: 1779 bdcpy(bd_addr.address, p_data->link_up.bd_addr); 1780 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED"); 1781 1782 btif_update_remote_version_property(&bd_addr); 1783 1784 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS, 1785 &bd_addr, BT_ACL_STATE_CONNECTED); 1786 break; 1787 1788 case BTA_DM_LINK_DOWN_EVT: 1789 bdcpy(bd_addr.address, p_data->link_down.bd_addr); 1790 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED"); 1791 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS, 1792 &bd_addr, BT_ACL_STATE_DISCONNECTED); 1793 break; 1794 1795 case BTA_DM_HW_ERROR_EVT: 1796 BTIF_TRACE_ERROR("Received H/W Error. "); 1797 /* Flush storage data */ 1798 btif_config_flush(); 1799 usleep(100000); /* 100milliseconds */ 1800 /* Killing the process to force a restart as part of fault tolerance */ 1801 kill(getpid(), SIGKILL); 1802 break; 1803 1804#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 1805 case BTA_DM_BLE_KEY_EVT: 1806 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", p_data->ble_key.key_type); 1807 1808 /* If this pairing is by-product of local initiated GATT client Read or Write, 1809 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not 1810 have setup properly. Setup pairing_cb and notify App about Bonding state now*/ 1811 if (pairing_cb.state != BT_BOND_STATE_BONDING) 1812 { 1813 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now"); 1814 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr, 1815 BT_BOND_STATE_BONDING); 1816 } 1817 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0) 1818 { 1819 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type); 1820 break; 1821 } 1822 1823 switch (p_data->ble_key.key_type) 1824 { 1825 case BTA_LE_KEY_PENC: 1826 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC"); 1827 pairing_cb.ble.is_penc_key_rcvd = TRUE; 1828 pairing_cb.ble.penc_key = p_data->ble_key.p_key_value->penc_key; 1829 break; 1830 1831 case BTA_LE_KEY_PID: 1832 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID"); 1833 pairing_cb.ble.is_pid_key_rcvd = TRUE; 1834 pairing_cb.ble.pid_key = p_data->ble_key.p_key_value->pid_key; 1835 break; 1836 1837 case BTA_LE_KEY_PCSRK: 1838 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK"); 1839 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE; 1840 pairing_cb.ble.pcsrk_key = p_data->ble_key.p_key_value->pcsrk_key; 1841 break; 1842 1843 case BTA_LE_KEY_LENC: 1844 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC"); 1845 pairing_cb.ble.is_lenc_key_rcvd = TRUE; 1846 pairing_cb.ble.lenc_key = p_data->ble_key.p_key_value->lenc_key; 1847 break; 1848 1849 case BTA_LE_KEY_LCSRK: 1850 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK"); 1851 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE; 1852 pairing_cb.ble.lcsrk_key = p_data->ble_key.p_key_value->lcsrk_key; 1853 break; 1854 1855 case BTA_LE_KEY_LID: 1856 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LID"); 1857 pairing_cb.ble.is_lidk_key_rcvd = TRUE; 1858 break; 1859 1860 default: 1861 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type); 1862 break; 1863 } 1864 break; 1865 case BTA_DM_BLE_SEC_REQ_EVT: 1866 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. "); 1867 btif_dm_ble_sec_req_evt(&p_data->ble_req); 1868 break; 1869 case BTA_DM_BLE_PASSKEY_NOTIF_EVT: 1870 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. "); 1871 btif_dm_ble_key_notif_evt(&p_data->key_notif); 1872 break; 1873 case BTA_DM_BLE_PASSKEY_REQ_EVT: 1874 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. "); 1875 btif_dm_ble_passkey_req_evt(&p_data->pin_req); 1876 break; 1877 case BTA_DM_BLE_NC_REQ_EVT: 1878 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. "); 1879 btif_dm_ble_key_nc_req_evt(&p_data->key_notif); 1880 break; 1881 case BTA_DM_BLE_OOB_REQ_EVT: 1882 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. "); 1883 break; 1884 case BTA_DM_BLE_LOCAL_IR_EVT: 1885 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. "); 1886 ble_local_key_cb.is_id_keys_rcvd = TRUE; 1887 memcpy(&ble_local_key_cb.id_keys.irk[0], 1888 &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16)); 1889 memcpy(&ble_local_key_cb.id_keys.ir[0], 1890 &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16)); 1891 memcpy(&ble_local_key_cb.id_keys.dhk[0], 1892 &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16)); 1893 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0], 1894 BTIF_DM_LE_LOCAL_KEY_IRK, 1895 BT_OCTET16_LEN); 1896 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0], 1897 BTIF_DM_LE_LOCAL_KEY_IR, 1898 BT_OCTET16_LEN); 1899 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0], 1900 BTIF_DM_LE_LOCAL_KEY_DHK, 1901 BT_OCTET16_LEN); 1902 break; 1903 case BTA_DM_BLE_LOCAL_ER_EVT: 1904 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. "); 1905 ble_local_key_cb.is_er_rcvd = TRUE; 1906 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16)); 1907 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0], 1908 BTIF_DM_LE_LOCAL_KEY_ER, 1909 BT_OCTET16_LEN); 1910 break; 1911 1912 case BTA_DM_BLE_AUTH_CMPL_EVT: 1913 BTIF_TRACE_DEBUG("BTA_DM_BLE_AUTH_CMPL_EVT. "); 1914 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl); 1915 break; 1916 1917 case BTA_DM_LE_FEATURES_READ: 1918 { 1919 tBTM_BLE_VSC_CB cmn_vsc_cb; 1920 bt_local_le_features_t local_le_features; 1921 char buf[512]; 1922 bt_property_t prop; 1923 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES; 1924 prop.val = (void*)buf; 1925 prop.len = sizeof(buf); 1926 1927 /* LE features are not stored in storage. Should be retrived from stack */ 1928 BTM_BleGetVendorCapabilities(&cmn_vsc_cb); 1929 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled(); 1930 1931 prop.len = sizeof (bt_local_le_features_t); 1932 if (cmn_vsc_cb.filter_support == 1) 1933 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter; 1934 else 1935 local_le_features.max_adv_filter_supported = 0; 1936 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max; 1937 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz; 1938 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading; 1939 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support; 1940 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg; 1941 local_le_features.version_supported = cmn_vsc_cb.version_supported; 1942 local_le_features.total_trackable_advertisers = 1943 cmn_vsc_cb.total_trackable_advertisers; 1944 1945 local_le_features.extended_scan_support = cmn_vsc_cb.extended_scan_support > 0; 1946 local_le_features.debug_logging_supported = cmn_vsc_cb.debug_logging_supported > 0; 1947 1948 memcpy(prop.val, &local_le_features, prop.len); 1949 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop); 1950 break; 1951 } 1952 1953 case BTA_DM_ENER_INFO_READ: 1954 { 1955 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param; 1956 bt_activity_energy_info energy_info; 1957 energy_info.status = p_ener_data->status; 1958 energy_info.ctrl_state = p_ener_data->ctrl_state; 1959 energy_info.rx_time = p_ener_data->rx_time; 1960 energy_info.tx_time = p_ener_data->tx_time; 1961 energy_info.idle_time = p_ener_data->idle_time; 1962 energy_info.energy_used = p_ener_data->energy_used; 1963 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info); 1964 break; 1965 } 1966#endif 1967 1968 case BTA_DM_AUTHORIZE_EVT: 1969 case BTA_DM_SIG_STRENGTH_EVT: 1970 case BTA_DM_SP_RMT_OOB_EVT: 1971 case BTA_DM_SP_KEYPRESS_EVT: 1972 case BTA_DM_ROLE_CHG_EVT: 1973 1974 default: 1975 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event ); 1976 break; 1977 } 1978 1979 btif_dm_data_free(event, p_data); 1980} 1981 1982/******************************************************************************* 1983** 1984** Function btif_dm_generic_evt 1985** 1986** Description Executes non-BTA upstream events in BTIF context 1987** 1988** Returns void 1989** 1990*******************************************************************************/ 1991static void btif_dm_generic_evt(UINT16 event, char* p_param) 1992{ 1993 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event); 1994 switch(event) 1995 { 1996 case BTIF_DM_CB_DISCOVERY_STARTED: 1997 { 1998 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED); 1999 } 2000 break; 2001 2002 case BTIF_DM_CB_CREATE_BOND: 2003 { 2004 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES; 2005 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param; 2006 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport); 2007 } 2008 break; 2009 2010 case BTIF_DM_CB_REMOVE_BOND: 2011 { 2012 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param); 2013 } 2014 break; 2015 2016 case BTIF_DM_CB_HID_REMOTE_NAME: 2017 { 2018 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param); 2019 } 2020 break; 2021 2022 case BTIF_DM_CB_BOND_STATE_BONDING: 2023 { 2024 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING); 2025 } 2026 break; 2027 case BTIF_DM_CB_LE_TX_TEST: 2028 case BTIF_DM_CB_LE_RX_TEST: 2029 { 2030 uint8_t status; 2031 STREAM_TO_UINT8(status, p_param); 2032 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb, 2033 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0); 2034 } 2035 break; 2036 case BTIF_DM_CB_LE_TEST_END: 2037 { 2038 uint8_t status; 2039 uint16_t count = 0; 2040 STREAM_TO_UINT8(status, p_param); 2041 if (status == 0) 2042 STREAM_TO_UINT16(count, p_param); 2043 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb, 2044 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count); 2045 } 2046 break; 2047 default: 2048 { 2049 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event); 2050 } 2051 break; 2052 } 2053} 2054 2055/******************************************************************************* 2056** 2057** Function bte_dm_evt 2058** 2059** Description Switches context from BTE to BTIF for all DM events 2060** 2061** Returns void 2062** 2063*******************************************************************************/ 2064 2065void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data) 2066{ 2067 /* switch context to btif task context (copy full union size for convenience) */ 2068 bt_status_t status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, 2069 (void*)p_data, sizeof(tBTA_DM_SEC), btif_dm_data_copy); 2070 2071 /* catch any failed context transfers */ 2072 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); 2073} 2074 2075/******************************************************************************* 2076** 2077** Function bte_search_devices_evt 2078** 2079** Description Switches context from BTE to BTIF for DM search events 2080** 2081** Returns void 2082** 2083*******************************************************************************/ 2084static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data) 2085{ 2086 UINT16 param_len = 0; 2087 2088 if (p_data) 2089 param_len += sizeof(tBTA_DM_SEARCH); 2090 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */ 2091 switch (event) 2092 { 2093 case BTA_DM_INQ_RES_EVT: 2094 { 2095 if (p_data->inq_res.p_eir) 2096 param_len += HCI_EXT_INQ_RESPONSE_LEN; 2097 } 2098 break; 2099 2100 case BTA_DM_DISC_RES_EVT: 2101 { 2102 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data) 2103 param_len += p_data->disc_res.raw_data_size; 2104 } 2105 break; 2106 } 2107 BTIF_TRACE_DEBUG("%s event=%s param_len=%d", __FUNCTION__, dump_dm_search_event(event), param_len); 2108 2109 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */ 2110 if (event == BTA_DM_INQ_RES_EVT) 2111 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL); 2112 2113 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len, 2114 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL); 2115} 2116 2117/******************************************************************************* 2118** 2119** Function bte_dm_search_services_evt 2120** 2121** Description Switches context from BTE to BTIF for DM search services 2122** event 2123** 2124** Returns void 2125** 2126*******************************************************************************/ 2127static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data) 2128{ 2129 UINT16 param_len = 0; 2130 if (p_data) 2131 param_len += sizeof(tBTA_DM_SEARCH); 2132 switch (event) 2133 { 2134 case BTA_DM_DISC_RES_EVT: 2135 { 2136 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) { 2137 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE); 2138 } 2139 } break; 2140 } 2141 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure 2142 * if raw_data is needed. */ 2143 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len, 2144 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL); 2145} 2146 2147/******************************************************************************* 2148** 2149** Function bte_dm_remote_service_record_evt 2150** 2151** Description Switches context from BTE to BTIF for DM search service 2152** record event 2153** 2154** Returns void 2155** 2156*******************************************************************************/ 2157static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data) 2158{ 2159 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */ 2160 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL); 2161} 2162 2163#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 2164/******************************************************************************* 2165** 2166** Function bta_energy_info_cb 2167** 2168** Description Switches context from BTE to BTIF for DM energy info event 2169** 2170** Returns void 2171** 2172*******************************************************************************/ 2173static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time, 2174 tBTA_DM_BLE_IDLE_TIME_MS idle_time, 2175 tBTA_DM_BLE_ENERGY_USED energy_used, 2176 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status) 2177{ 2178 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld", 2179 status, ctrl_state, tx_time, rx_time, idle_time, energy_used); 2180 2181 btif_activity_energy_info_cb_t btif_cb; 2182 btif_cb.status = status; 2183 btif_cb.ctrl_state = ctrl_state; 2184 btif_cb.tx_time = (uint64_t) tx_time; 2185 btif_cb.rx_time = (uint64_t) rx_time; 2186 btif_cb.idle_time =(uint64_t) idle_time; 2187 btif_cb.energy_used =(uint64_t) energy_used; 2188 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ, 2189 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL); 2190} 2191#endif 2192 2193/******************************************************************************* 2194** 2195** Function bte_scan_filt_param_cfg_evt 2196** 2197** Description Scan filter param config event 2198** 2199** Returns void 2200** 2201*******************************************************************************/ 2202static void bte_scan_filt_param_cfg_evt(UINT8 action_type, 2203 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space, 2204 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status) 2205{ 2206 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally, 2207 ** and that is why there is no HAL callback 2208 */ 2209 if(BTA_SUCCESS != status) 2210 { 2211 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status); 2212 } 2213 else 2214 { 2215 BTIF_TRACE_DEBUG("%s", __FUNCTION__); 2216 } 2217} 2218 2219/***************************************************************************** 2220** 2221** btif api functions (no context switch) 2222** 2223*****************************************************************************/ 2224 2225/******************************************************************************* 2226** 2227** Function btif_dm_start_discovery 2228** 2229** Description Start device discovery/inquiry 2230** 2231** Returns bt_status_t 2232** 2233*******************************************************************************/ 2234bt_status_t btif_dm_start_discovery(void) 2235{ 2236 tBTA_DM_INQ inq_params; 2237 tBTA_SERVICE_MASK services = 0; 2238 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param; 2239 2240 BTIF_TRACE_EVENT("%s", __FUNCTION__); 2241 2242#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 2243 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS)); 2244 /* Cleanup anything remaining on index 0 */ 2245 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL, 2246 bte_scan_filt_param_cfg_evt, 0); 2247 2248 /* Add an allow-all filter on index 0*/ 2249 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE; 2250 adv_filt_param.feat_seln = ALLOW_ALL_FILTER; 2251 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR; 2252 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR; 2253 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE; 2254 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE; 2255 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL, 2256 bte_scan_filt_param_cfg_evt, 0); 2257 2258 /* TODO: Do we need to handle multiple inquiries at the same time? */ 2259 2260 /* Set inquiry params and call API */ 2261 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY; 2262#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE) 2263 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE; 2264 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE; 2265 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO; 2266 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO; 2267#endif 2268#else 2269 inq_params.mode = BTA_DM_GENERAL_INQUIRY; 2270#endif 2271 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION; 2272 2273 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS; 2274 inq_params.report_dup = TRUE; 2275 2276 inq_params.filter_type = BTA_DM_INQ_CLR; 2277 /* TODO: Filter device by BDA needs to be implemented here */ 2278 2279 /* Will be enabled to TRUE once inquiry busy level has been received */ 2280 btif_dm_inquiry_in_progress = FALSE; 2281 /* find nearby devices */ 2282 BTA_DmSearch(&inq_params, services, bte_search_devices_evt); 2283 2284 return BT_STATUS_SUCCESS; 2285} 2286 2287/******************************************************************************* 2288** 2289** Function btif_dm_cancel_discovery 2290** 2291** Description Cancels search 2292** 2293** Returns bt_status_t 2294** 2295*******************************************************************************/ 2296bt_status_t btif_dm_cancel_discovery(void) 2297{ 2298 BTIF_TRACE_EVENT("%s", __FUNCTION__); 2299 BTA_DmSearchCancel(); 2300 return BT_STATUS_SUCCESS; 2301} 2302 2303/******************************************************************************* 2304** 2305** Function btif_dm_create_bond 2306** 2307** Description Initiate bonding with the specified device 2308** 2309** Returns bt_status_t 2310** 2311*******************************************************************************/ 2312bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport) 2313{ 2314 btif_dm_create_bond_cb_t create_bond_cb; 2315 create_bond_cb.transport = transport; 2316 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address); 2317 2318 bdstr_t bdstr; 2319 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport); 2320 if (pairing_cb.state != BT_BOND_STATE_NONE) 2321 return BT_STATUS_BUSY; 2322 2323 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND, 2324 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL); 2325 2326 return BT_STATUS_SUCCESS; 2327} 2328 2329/******************************************************************************* 2330** 2331** Function btif_dm_cancel_bond 2332** 2333** Description Initiate bonding with the specified device 2334** 2335** Returns bt_status_t 2336** 2337*******************************************************************************/ 2338 2339bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr) 2340{ 2341 bdstr_t bdstr; 2342 2343 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr))); 2344 2345 /* TODO: 2346 ** 1. Restore scan modes 2347 ** 2. special handling for HID devices 2348 */ 2349 if (pairing_cb.state == BT_BOND_STATE_BONDING) 2350 { 2351 2352#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 2353 2354 if (pairing_cb.is_ssp) 2355 { 2356 if (pairing_cb.is_le_only) 2357 { 2358 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT); 2359 } 2360 else 2361 { 2362 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE); 2363 BTA_DmBondCancel ((UINT8 *)bd_addr->address); 2364 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr); 2365 } 2366 } 2367 else 2368 { 2369 if (pairing_cb.is_le_only) 2370 { 2371 BTA_DmBondCancel ((UINT8 *)bd_addr->address); 2372 } 2373 else 2374 { 2375 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL); 2376 } 2377 /* Cancel bonding, in case it is in ACL connection setup state */ 2378 BTA_DmBondCancel ((UINT8 *)bd_addr->address); 2379 } 2380 2381#else 2382 if (pairing_cb.is_ssp) 2383 { 2384 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE); 2385 } 2386 else 2387 { 2388 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL); 2389 } 2390 /* Cancel bonding, in case it is in ACL connection setup state */ 2391 BTA_DmBondCancel ((UINT8 *)bd_addr->address); 2392 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr); 2393#endif 2394 } 2395 2396 return BT_STATUS_SUCCESS; 2397} 2398 2399/******************************************************************************* 2400** 2401** Function btif_dm_hh_open_failed 2402** 2403** Description informs the upper layers if the HH have failed during bonding 2404** 2405** Returns none 2406** 2407*******************************************************************************/ 2408 2409void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr) 2410{ 2411 if (pairing_cb.state == BT_BOND_STATE_BONDING && 2412 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0) 2413 { 2414 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE); 2415 } 2416} 2417 2418/******************************************************************************* 2419** 2420** Function btif_dm_remove_bond 2421** 2422** Description Removes bonding with the specified device 2423** 2424** Returns bt_status_t 2425** 2426*******************************************************************************/ 2427 2428bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr) 2429{ 2430 bdstr_t bdstr; 2431 2432 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr))); 2433 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND, 2434 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL); 2435 2436 return BT_STATUS_SUCCESS; 2437} 2438 2439/******************************************************************************* 2440** 2441** Function btif_dm_pin_reply 2442** 2443** Description BT legacy pairing - PIN code reply 2444** 2445** Returns bt_status_t 2446** 2447*******************************************************************************/ 2448 2449bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept, 2450 uint8_t pin_len, bt_pin_code_t *pin_code) 2451{ 2452 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept); 2453 if (pin_code == NULL) 2454 return BT_STATUS_FAIL; 2455#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 2456 2457 if (pairing_cb.is_le_only) 2458 { 2459 int i; 2460 UINT32 passkey = 0; 2461 int multi[] = {100000, 10000, 1000, 100, 10,1}; 2462 BD_ADDR remote_bd_addr; 2463 bdcpy(remote_bd_addr, bd_addr->address); 2464 for (i = 0; i < 6; i++) 2465 { 2466 passkey += (multi[i] * (pin_code->pin[i] - '0')); 2467 } 2468 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey); 2469 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey); 2470 2471 } 2472 else 2473 { 2474 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin); 2475 if (accept) 2476 pairing_cb.pin_code_len = pin_len; 2477 } 2478#else 2479 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin); 2480 2481 if (accept) 2482 pairing_cb.pin_code_len = pin_len; 2483#endif 2484 return BT_STATUS_SUCCESS; 2485} 2486 2487/******************************************************************************* 2488** 2489** Function btif_dm_ssp_reply 2490** 2491** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry 2492** 2493** Returns bt_status_t 2494** 2495*******************************************************************************/ 2496bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr, 2497 bt_ssp_variant_t variant, uint8_t accept, 2498 uint32_t passkey) 2499{ 2500 UNUSED(passkey); 2501 2502 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY) 2503 { 2504 /* This is not implemented in the stack. 2505 * For devices with display, this is not needed 2506 */ 2507 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__); 2508 return BT_STATUS_FAIL; 2509 } 2510 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */ 2511 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept); 2512#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 2513 if (pairing_cb.is_le_only) 2514 { 2515 if(pairing_cb.is_le_nc) 2516 { 2517 BTA_DmBleConfirmReply((UINT8 *)bd_addr->address,accept); 2518 } else { 2519 if (accept) 2520 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED); 2521 else 2522 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT); 2523 } 2524 } else { 2525 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept); 2526 } 2527#else 2528 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept); 2529#endif 2530 return BT_STATUS_SUCCESS; 2531} 2532 2533/******************************************************************************* 2534** 2535** Function btif_dm_get_adapter_property 2536** 2537** Description Queries the BTA for the adapter property 2538** 2539** Returns bt_status_t 2540** 2541*******************************************************************************/ 2542bt_status_t btif_dm_get_adapter_property(bt_property_t *prop) 2543{ 2544 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type); 2545 switch (prop->type) 2546 { 2547 case BT_PROPERTY_BDNAME: 2548 { 2549 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val; 2550 strncpy((char *)bd_name->name, (char *)btif_get_default_local_name(), 2551 sizeof(bd_name->name) - 1); 2552 bd_name->name[sizeof(bd_name->name) - 1] = 0; 2553 prop->len = strlen((char *)bd_name->name); 2554 } 2555 break; 2556 2557 case BT_PROPERTY_ADAPTER_SCAN_MODE: 2558 { 2559 /* if the storage does not have it. Most likely app never set it. Default is NONE */ 2560 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val; 2561 *mode = BT_SCAN_MODE_NONE; 2562 prop->len = sizeof(bt_scan_mode_t); 2563 } 2564 break; 2565 2566 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: 2567 { 2568 uint32_t *tmt = (uint32_t*)prop->val; 2569 *tmt = 120; /* default to 120s, if not found in NV */ 2570 prop->len = sizeof(uint32_t); 2571 } 2572 break; 2573 2574 default: 2575 prop->len = 0; 2576 return BT_STATUS_FAIL; 2577 } 2578 return BT_STATUS_SUCCESS; 2579} 2580 2581/******************************************************************************* 2582** 2583** Function btif_dm_get_remote_services 2584** 2585** Description Start SDP to get remote services 2586** 2587** Returns bt_status_t 2588** 2589*******************************************************************************/ 2590bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr) 2591{ 2592 bdstr_t bdstr; 2593 2594 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr))); 2595 2596 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK, 2597 bte_dm_search_services_evt, TRUE); 2598 2599 return BT_STATUS_SUCCESS; 2600} 2601 2602/******************************************************************************* 2603** 2604** Function btif_dm_get_remote_service_record 2605** 2606** Description Start SDP to get remote service record 2607** 2608** 2609** Returns bt_status_t 2610*******************************************************************************/ 2611bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr, 2612 bt_uuid_t *uuid) 2613{ 2614 tSDP_UUID sdp_uuid; 2615 bdstr_t bdstr; 2616 2617 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr))); 2618 2619 sdp_uuid.len = MAX_UUID_SIZE; 2620 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE); 2621 2622 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid, 2623 bte_dm_remote_service_record_evt, TRUE); 2624 2625 return BT_STATUS_SUCCESS; 2626} 2627 2628void btif_dm_execute_service_request(UINT16 event, char *p_param) 2629{ 2630 BOOLEAN b_enable = FALSE; 2631 bt_status_t status; 2632 if (event == BTIF_DM_ENABLE_SERVICE) 2633 { 2634 b_enable = TRUE; 2635 } 2636 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable); 2637 if (status == BT_STATUS_SUCCESS) 2638 { 2639 bt_property_t property; 2640 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS]; 2641 2642 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */ 2643 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS, 2644 sizeof(local_uuids), local_uuids); 2645 btif_storage_get_adapter_property(&property); 2646 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, 2647 BT_STATUS_SUCCESS, 1, &property); 2648 } 2649 return; 2650} 2651 2652void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data, 2653 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig) 2654{ 2655 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req; 2656 /* if local initiated: 2657 ** 1. set DD + MITM 2658 ** if remote initiated: 2659 ** 1. Copy over the auth_req from peer's io_rsp 2660 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone) 2661 ** as a fallback set MITM+GB if peer had MITM set 2662 */ 2663 UNUSED (bd_addr); 2664 UNUSED (p_io_cap); 2665 UNUSED (p_oob_data); 2666 2667 2668 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req); 2669 if(pairing_cb.is_local_initiated) 2670 { 2671 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */ 2672 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES; 2673 } 2674 else if (!is_orig) 2675 { 2676 /* peer initiated paring. They probably know what they want. 2677 ** Copy the mitm from peer device. 2678 */ 2679 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d", 2680 __FUNCTION__, pairing_cb.auth_req); 2681 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS); 2682 2683 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */ 2684 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) ) 2685 *p_auth_req |= BTA_AUTH_SP_YES; 2686 } 2687 else if (yes_no_bit) 2688 { 2689 /* set the general bonding bit for stored device */ 2690 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit; 2691 } 2692 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req); 2693} 2694 2695void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap, 2696 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req) 2697{ 2698 UNUSED (bd_addr); 2699 UNUSED (oob_data); 2700 2701 if(auth_req & BTA_AUTH_BONDS) 2702 { 2703 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req); 2704 pairing_cb.auth_req = auth_req; 2705 pairing_cb.io_cap = io_cap; 2706 } 2707} 2708 2709#if (BTM_OOB_INCLUDED == TRUE) 2710void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data) 2711{ 2712 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 && 2713 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 ) 2714 { 2715 *p_oob_data = FALSE; 2716 } 2717 else 2718 { 2719 *p_oob_data = TRUE; 2720 } 2721 BTIF_TRACE_DEBUG("btif_dm_set_oob_for_io_req *p_oob_data=%d", *p_oob_data); 2722} 2723#endif /* BTM_OOB_INCLUDED */ 2724 2725#ifdef BTIF_DM_OOB_TEST 2726void btif_dm_load_local_oob(void) 2727{ 2728 char prop_oob[PROPERTY_VALUE_MAX]; 2729 property_get("service.brcm.bt.oob", prop_oob, "3"); 2730 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob); 2731 if (prop_oob[0] != '3') 2732 { 2733#if (BTM_OOB_INCLUDED == TRUE) 2734 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 && 2735 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 ) 2736 { 2737 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()"); 2738 BTA_DmLocalOob(); 2739 } 2740#else 2741 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)"); 2742#endif 2743 } 2744} 2745 2746void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r) 2747{ 2748 FILE *fp; 2749 char *path_a = "/data/misc/bluedroid/LOCAL/a.key"; 2750 char *path_b = "/data/misc/bluedroid/LOCAL/b.key"; 2751 char *path = NULL; 2752 char prop_oob[PROPERTY_VALUE_MAX]; 2753 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid); 2754 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 && 2755 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 && 2756 valid) 2757 { 2758 BTIF_TRACE_DEBUG("save local OOB data in memory"); 2759 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN); 2760 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN); 2761 property_get("service.brcm.bt.oob", prop_oob, "3"); 2762 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob); 2763 if (prop_oob[0] == '1') 2764 path = path_a; 2765 else if (prop_oob[0] == '2') 2766 path = path_b; 2767 if (path) 2768 { 2769 fp = fopen(path, "wb+"); 2770 if (fp == NULL) 2771 { 2772 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: failed to save local OOB data to %s", path); 2773 } 2774 else 2775 { 2776 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: save local OOB data into file %s",path); 2777 fwrite (c , 1 , BT_OCTET16_LEN , fp ); 2778 fwrite (r , 1 , BT_OCTET16_LEN , fp ); 2779 fclose(fp); 2780 } 2781 } 2782 } 2783} 2784BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r) 2785{ 2786 char t[128]; 2787 FILE *fp; 2788 char *path_a = "/data/misc/bluedroid/LOCAL/a.key"; 2789 char *path_b = "/data/misc/bluedroid/LOCAL/b.key"; 2790 char *path = NULL; 2791 char prop_oob[PROPERTY_VALUE_MAX]; 2792 BOOLEAN result = FALSE; 2793 bt_bdaddr_t bt_bd_addr; 2794 bdcpy(oob_cb.oob_bdaddr, bd_addr); 2795 property_get("service.brcm.bt.oob", prop_oob, "3"); 2796 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob); 2797 if (prop_oob[0] == '1') 2798 path = path_b; 2799 else if (prop_oob[0] == '2') 2800 path = path_a; 2801 if (path) 2802 { 2803 fp = fopen(path, "rb"); 2804 if (fp == NULL) 2805 { 2806 BTIF_TRACE_DEBUG("btapp_dm_rmt_oob_reply: failed to read OOB keys from %s",path); 2807 return FALSE; 2808 } 2809 else 2810 { 2811 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path); 2812 fread (p_c , 1 , BT_OCTET16_LEN , fp ); 2813 fread (p_r , 1 , BT_OCTET16_LEN , fp ); 2814 fclose(fp); 2815 } 2816 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE"); 2817 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x", 2818 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2], 2819 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]); 2820 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t); 2821 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", 2822 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7], 2823 p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]); 2824 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t); 2825 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", 2826 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7], 2827 p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]); 2828 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t); 2829 bdcpy(bt_bd_addr.address, bd_addr); 2830 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING, 2831 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL); 2832 result = TRUE; 2833 } 2834 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result); 2835 return result; 2836} 2837#endif /* BTIF_DM_OOB_TEST */ 2838#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 2839 2840static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif) 2841{ 2842 bt_bdaddr_t bd_addr; 2843 bt_bdname_t bd_name; 2844 UINT32 cod; 2845 int dev_type; 2846 2847 BTIF_TRACE_DEBUG("%s", __FUNCTION__); 2848 2849 /* Remote name update */ 2850 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type)) 2851 { 2852 dev_type = BT_DEVICE_TYPE_BLE; 2853 } 2854 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name, 2855 (tBT_DEVICE_TYPE) dev_type); 2856 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr); 2857 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN); 2858 2859 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); 2860 pairing_cb.is_ssp = FALSE; 2861 cod = COD_UNCLASSIFIED; 2862 2863 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, 2864 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION, 2865 p_ssp_key_notif->passkey); 2866} 2867 2868/******************************************************************************* 2869** 2870** Function btif_dm_ble_auth_cmpl_evt 2871** 2872** Description Executes authentication complete event in btif context 2873** 2874** Returns void 2875** 2876*******************************************************************************/ 2877static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) 2878{ 2879 /* Save link key, if not temporary */ 2880 bt_bdaddr_t bd_addr; 2881 bt_status_t status = BT_STATUS_FAIL; 2882 bt_bond_state_t state = BT_BOND_STATE_NONE; 2883 2884 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr); 2885 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) ) 2886 { 2887 /* store keys */ 2888 } 2889 if (p_auth_cmpl->success) 2890 { 2891 status = BT_STATUS_SUCCESS; 2892 state = BT_BOND_STATE_BONDED; 2893 int addr_type; 2894 bt_bdaddr_t bdaddr; 2895 bdcpy(bdaddr.address, p_auth_cmpl->bd_addr); 2896 if (btif_storage_get_remote_addr_type(&bdaddr, &addr_type) != BT_STATUS_SUCCESS) 2897 btif_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type); 2898 btif_dm_save_ble_bonding_keys(); 2899 BTA_GATTC_Refresh(bd_addr.address); 2900 btif_dm_get_remote_services(&bd_addr); 2901 } 2902 else 2903 { 2904 /*Map the HCI fail reason to bt status */ 2905 switch (p_auth_cmpl->fail_reason) 2906 { 2907 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL: 2908 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL: 2909 btif_dm_remove_ble_bonding_keys(); 2910 status = BT_STATUS_AUTH_FAILURE; 2911 break; 2912 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT: 2913 status = BT_STATUS_AUTH_REJECTED; 2914 break; 2915 default: 2916 btif_dm_remove_ble_bonding_keys(); 2917 status = BT_STATUS_FAIL; 2918 break; 2919 } 2920 } 2921 bond_state_changed(status, &bd_addr, state); 2922} 2923 2924 2925 2926void btif_dm_load_ble_local_keys(void) 2927{ 2928 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t)); 2929 2930 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0], 2931 BT_OCTET16_LEN)== BT_STATUS_SUCCESS) 2932 { 2933 ble_local_key_cb.is_er_rcvd = TRUE; 2934 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ ); 2935 } 2936 2937 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0], 2938 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&& 2939 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0], 2940 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&& 2941 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0], 2942 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)) 2943 { 2944 ble_local_key_cb.is_id_keys_rcvd = TRUE; 2945 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ ); 2946 } 2947 2948} 2949void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er, 2950 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys) 2951{ 2952 if (ble_local_key_cb.is_er_rcvd ) 2953 { 2954 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16)); 2955 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER; 2956 } 2957 2958 if (ble_local_key_cb.is_id_keys_rcvd) 2959 { 2960 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16)); 2961 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16)); 2962 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16)); 2963 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID; 2964 } 2965 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask); 2966} 2967 2968void btif_dm_save_ble_bonding_keys(void) 2969{ 2970 2971 bt_bdaddr_t bd_addr; 2972 2973 BTIF_TRACE_DEBUG("%s",__FUNCTION__ ); 2974 2975 bdcpy(bd_addr.address, pairing_cb.bd_addr); 2976 2977 if (pairing_cb.ble.is_penc_key_rcvd) 2978 { 2979 btif_storage_add_ble_bonding_key(&bd_addr, 2980 (char *) &pairing_cb.ble.penc_key, 2981 BTIF_DM_LE_KEY_PENC, 2982 sizeof(tBTM_LE_PENC_KEYS)); 2983 } 2984 2985 if (pairing_cb.ble.is_pid_key_rcvd) 2986 { 2987 btif_storage_add_ble_bonding_key(&bd_addr, 2988 (char *) &pairing_cb.ble.pid_key, 2989 BTIF_DM_LE_KEY_PID, 2990 sizeof(tBTM_LE_PID_KEYS)); 2991 } 2992 2993 2994 if (pairing_cb.ble.is_pcsrk_key_rcvd) 2995 { 2996 btif_storage_add_ble_bonding_key(&bd_addr, 2997 (char *) &pairing_cb.ble.pcsrk_key, 2998 BTIF_DM_LE_KEY_PCSRK, 2999 sizeof(tBTM_LE_PCSRK_KEYS)); 3000 } 3001 3002 3003 if (pairing_cb.ble.is_lenc_key_rcvd) 3004 { 3005 btif_storage_add_ble_bonding_key(&bd_addr, 3006 (char *) &pairing_cb.ble.lenc_key, 3007 BTIF_DM_LE_KEY_LENC, 3008 sizeof(tBTM_LE_LENC_KEYS)); 3009 } 3010 3011 if (pairing_cb.ble.is_lcsrk_key_rcvd) 3012 { 3013 btif_storage_add_ble_bonding_key(&bd_addr, 3014 (char *) &pairing_cb.ble.lcsrk_key, 3015 BTIF_DM_LE_KEY_LCSRK, 3016 sizeof(tBTM_LE_LCSRK_KEYS)); 3017 } 3018 3019 if (pairing_cb.ble.is_lidk_key_rcvd) 3020 { 3021 btif_storage_add_ble_bonding_key(&bd_addr, 3022 NULL, 3023 BTIF_DM_LE_KEY_LID, 3024 0); 3025 } 3026 3027} 3028 3029 3030void btif_dm_remove_ble_bonding_keys(void) 3031{ 3032 bt_bdaddr_t bd_addr; 3033 3034 BTIF_TRACE_DEBUG("%s",__FUNCTION__ ); 3035 3036 bdcpy(bd_addr.address, pairing_cb.bd_addr); 3037 btif_storage_remove_ble_bonding_keys(&bd_addr); 3038} 3039 3040 3041/******************************************************************************* 3042** 3043** Function btif_dm_ble_sec_req_evt 3044** 3045** Description Eprocess security request event in btif context 3046** 3047** Returns void 3048** 3049*******************************************************************************/ 3050void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req) 3051{ 3052 bt_bdaddr_t bd_addr; 3053 bt_bdname_t bd_name; 3054 UINT32 cod; 3055 int dev_type; 3056 3057 BTIF_TRACE_DEBUG("%s", __FUNCTION__); 3058 3059 if (pairing_cb.state == BT_BOND_STATE_BONDING) 3060 { 3061 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__); 3062 return; 3063 } 3064 3065 /* Remote name update */ 3066 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type)) 3067 { 3068 dev_type = BT_DEVICE_TYPE_BLE; 3069 } 3070 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name, 3071 (tBT_DEVICE_TYPE) dev_type); 3072 3073 bdcpy(bd_addr.address, p_ble_req->bd_addr); 3074 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN); 3075 3076 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); 3077 3078 pairing_cb.bond_type = BOND_TYPE_PERSISTENT; 3079 pairing_cb.is_le_only = TRUE; 3080 pairing_cb.is_le_nc = FALSE; 3081 pairing_cb.is_ssp = TRUE; 3082 3083 cod = COD_UNCLASSIFIED; 3084 3085 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod, 3086 BT_SSP_VARIANT_CONSENT, 0); 3087} 3088 3089 3090 3091/******************************************************************************* 3092** 3093** Function btif_dm_ble_passkey_req_evt 3094** 3095** Description Executes pin request event in btif context 3096** 3097** Returns void 3098** 3099*******************************************************************************/ 3100static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req) 3101{ 3102 bt_bdaddr_t bd_addr; 3103 bt_bdname_t bd_name; 3104 UINT32 cod; 3105 int dev_type; 3106 3107 /* Remote name update */ 3108 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type)) 3109 { 3110 dev_type = BT_DEVICE_TYPE_BLE; 3111 } 3112 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name, 3113 (tBT_DEVICE_TYPE) dev_type); 3114 3115 bdcpy(bd_addr.address, p_pin_req->bd_addr); 3116 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN); 3117 3118 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); 3119 pairing_cb.is_le_only = TRUE; 3120 3121 cod = COD_UNCLASSIFIED; 3122 3123 HAL_CBACK(bt_hal_cbacks, pin_request_cb, 3124 &bd_addr, &bd_name, cod, FALSE); 3125} 3126static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req) 3127{ 3128 /* TODO implement key notification for numeric comparison */ 3129 BTIF_TRACE_DEBUG("%s", __FUNCTION__); 3130 3131 /* Remote name update */ 3132 btif_update_remote_properties(p_notif_req->bd_addr , p_notif_req->bd_name, 3133 NULL, BT_DEVICE_TYPE_BLE); 3134 3135 bt_bdaddr_t bd_addr; 3136 bdcpy(bd_addr.address, p_notif_req->bd_addr); 3137 3138 bt_bdname_t bd_name; 3139 memcpy(bd_name.name, p_notif_req->bd_name, BD_NAME_LEN); 3140 3141 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); 3142 pairing_cb.is_ssp = FALSE; 3143 pairing_cb.is_le_only = TRUE; 3144 pairing_cb.is_le_nc = TRUE; 3145 3146 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, 3147 COD_UNCLASSIFIED, BT_SSP_VARIANT_PASSKEY_CONFIRMATION, 3148 p_notif_req->passkey); 3149} 3150 3151void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name, 3152 tBT_DEVICE_TYPE dev_type) 3153{ 3154 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type); 3155} 3156 3157static void btif_dm_ble_tx_test_cback(void *p) 3158{ 3159 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST, 3160 (char *)p, 1, NULL); 3161} 3162 3163static void btif_dm_ble_rx_test_cback(void *p) 3164{ 3165 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST, 3166 (char *)p, 1, NULL); 3167} 3168 3169static void btif_dm_ble_test_end_cback(void *p) 3170{ 3171 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END, 3172 (char *)p, 3, NULL); 3173} 3174/******************************************************************************* 3175** 3176** Function btif_le_test_mode 3177** 3178** Description Sends a HCI BLE Test command to the Controller 3179** 3180** Returns BT_STATUS_SUCCESS on success 3181** 3182*******************************************************************************/ 3183bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len) 3184{ 3185 switch (opcode) { 3186 case HCI_BLE_TRANSMITTER_TEST: 3187 if (len != 3) return BT_STATUS_PARM_INVALID; 3188 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback); 3189 break; 3190 case HCI_BLE_RECEIVER_TEST: 3191 if (len != 1) return BT_STATUS_PARM_INVALID; 3192 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback); 3193 break; 3194 case HCI_BLE_TEST_END: 3195 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback); 3196 break; 3197 default: 3198 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode); 3199 return BT_STATUS_UNSUPPORTED; 3200 } 3201 return BT_STATUS_SUCCESS; 3202} 3203#endif 3204 3205void btif_dm_on_disable() 3206{ 3207 /* cancel any pending pairing requests */ 3208 if (pairing_cb.state == BT_BOND_STATE_BONDING) 3209 { 3210 bt_bdaddr_t bd_addr; 3211 3212 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__); 3213 bdcpy(bd_addr.address, pairing_cb.bd_addr); 3214 btif_dm_cancel_bond(&bd_addr); 3215 } 3216} 3217 3218/******************************************************************************* 3219** 3220** Function btif_dm_read_energy_info 3221** 3222** Description Reads the energy info from controller 3223** 3224** Returns void 3225** 3226*******************************************************************************/ 3227void btif_dm_read_energy_info() 3228{ 3229#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 3230 BTA_DmBleGetEnergyInfo(bta_energy_info_cb); 3231#endif 3232} 3233 3234static char* btif_get_default_local_name() { 3235 if (btif_default_local_name[0] == '\0') 3236 { 3237 int max_len = sizeof(btif_default_local_name) - 1; 3238 if (BTM_DEF_LOCAL_NAME[0] != '\0') 3239 { 3240 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len); 3241 } 3242 else 3243 { 3244 char prop_model[PROPERTY_VALUE_MAX]; 3245 property_get(PROPERTY_PRODUCT_MODEL, prop_model, ""); 3246 strncpy(btif_default_local_name, prop_model, max_len); 3247 } 3248 btif_default_local_name[max_len] = '\0'; 3249 } 3250 return btif_default_local_name; 3251} 3252