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