1 /****************************************************************************** 2 * 3 * Copyright (c) 2014 The Android Open Source Project 4 * Copyright 2009-2012 Broadcom Corporation 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at: 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 ******************************************************************************/ 19 20 /******************************************************************************* 21 * 22 * Filename: btif_storage.c 23 * 24 * Description: Stores the local BT adapter and remote device properties in 25 * NVRAM storage, typically as xml file in the 26 * mobile's filesystem 27 * 28 * 29 */ 30 31 #define LOG_TAG "bt_btif_storage" 32 33 #include "btif_storage.h" 34 35 #include <alloca.h> 36 #include <base/logging.h> 37 #include <ctype.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <time.h> 41 42 #include "bt_common.h" 43 #include "bta_closure_api.h" 44 #include "bta_hd_api.h" 45 #include "bta_hearing_aid_api.h" 46 #include "bta_hh_api.h" 47 #include "btif_api.h" 48 #include "btif_config.h" 49 #include "btif_hd.h" 50 #include "btif_hh.h" 51 #include "btif_util.h" 52 #include "device/include/controller.h" 53 #include "osi/include/allocator.h" 54 #include "osi/include/compat.h" 55 #include "osi/include/config.h" 56 #include "osi/include/log.h" 57 #include "osi/include/osi.h" 58 59 using base::Bind; 60 using bluetooth::Uuid; 61 62 /******************************************************************************* 63 * Constants & Macros 64 ******************************************************************************/ 65 66 // TODO(armansito): Find a better way than using a hardcoded path. 67 #define BTIF_STORAGE_PATH_BLUEDROID "/data/misc/bluedroid" 68 69 //#define BTIF_STORAGE_PATH_ADAPTER_INFO "adapter_info" 70 //#define BTIF_STORAGE_PATH_REMOTE_DEVICES "remote_devices" 71 #define BTIF_STORAGE_PATH_REMOTE_DEVTIME "Timestamp" 72 #define BTIF_STORAGE_PATH_REMOTE_DEVCLASS "DevClass" 73 #define BTIF_STORAGE_PATH_REMOTE_DEVTYPE "DevType" 74 #define BTIF_STORAGE_PATH_REMOTE_NAME "Name" 75 #define BTIF_STORAGE_PATH_REMOTE_VER_MFCT "Manufacturer" 76 #define BTIF_STORAGE_PATH_REMOTE_VER_VER "LmpVer" 77 #define BTIF_STORAGE_PATH_REMOTE_VER_SUBVER "LmpSubVer" 78 79 //#define BTIF_STORAGE_PATH_REMOTE_LINKKEYS "remote_linkkeys" 80 #define BTIF_STORAGE_PATH_REMOTE_ALIASE "Aliase" 81 #define BTIF_STORAGE_PATH_REMOTE_SERVICE "Service" 82 #define BTIF_STORAGE_PATH_REMOTE_HIDINFO "HidInfo" 83 #define BTIF_STORAGE_KEY_ADAPTER_NAME "Name" 84 #define BTIF_STORAGE_KEY_ADAPTER_SCANMODE "ScanMode" 85 #define BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT "DiscoveryTimeout" 86 87 /* This is a local property to add a device found */ 88 #define BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP 0xFF 89 90 // TODO: This macro should be converted to a function 91 #define BTIF_STORAGE_GET_ADAPTER_PROP(s, t, v, l, p) \ 92 do { \ 93 (p).type = (t); \ 94 (p).val = (v); \ 95 (p).len = (l); \ 96 (s) = btif_storage_get_adapter_property(&(p)); \ 97 } while (0) 98 99 // TODO: This macro should be converted to a function 100 #define BTIF_STORAGE_GET_REMOTE_PROP(b, t, v, l, p) \ 101 do { \ 102 (p).type = (t); \ 103 (p).val = (v); \ 104 (p).len = (l); \ 105 btif_storage_get_remote_device_property((b), &(p)); \ 106 } while (0) 107 108 #define STORAGE_BDADDR_STRING_SZ (18) /* 00:11:22:33:44:55 */ 109 #define STORAGE_UUID_STRING_SIZE \ 110 (36 + 1) /* 00001200-0000-1000-8000-00805f9b34fb; */ 111 #define STORAGE_PINLEN_STRING_MAX_SIZE (2) /* ascii pinlen max chars */ 112 #define STORAGE_KEYTYPE_STRING_MAX_SIZE (1) /* ascii keytype max chars */ 113 114 #define STORAGE_KEY_TYPE_MAX (10) 115 116 #define STORAGE_HID_ATRR_MASK_SIZE (4) 117 #define STORAGE_HID_SUB_CLASS_SIZE (2) 118 #define STORAGE_HID_APP_ID_SIZE (2) 119 #define STORAGE_HID_VENDOR_ID_SIZE (4) 120 #define STORAGE_HID_PRODUCT_ID_SIZE (4) 121 #define STORAGE_HID_VERSION_SIZE (4) 122 #define STORAGE_HID_CTRY_CODE_SIZE (2) 123 #define STORAGE_HID_DESC_LEN_SIZE (4) 124 #define STORAGE_HID_DESC_MAX_SIZE (2 * 512) 125 126 /* <18 char bd addr> <space> LIST< <36 char uuid> <;> > <keytype (dec)> <pinlen> 127 */ 128 #define BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX \ 129 (STORAGE_BDADDR_STRING_SZ + 1 + \ 130 STORAGE_UUID_STRING_SIZE * BT_MAX_NUM_UUIDS + \ 131 STORAGE_PINLEN_STRING_MAX_SIZE + STORAGE_KEYTYPE_STRING_MAX_SIZE) 132 133 #define STORAGE_REMOTE_LINKKEYS_ENTRY_SIZE (LINK_KEY_LEN * 2 + 1 + 2 + 1 + 2) 134 135 /* <18 char bd addr> <space>LIST <attr_mask> <space> > <sub_class> <space> 136 <app_id> <space> 137 <vendor_id> <space> > <product_id> <space> 138 <version> <space> 139 <ctry_code> <space> > <desc_len> <space> 140 <desc_list> <space> */ 141 #define BTIF_HID_INFO_ENTRY_SIZE_MAX \ 142 (STORAGE_BDADDR_STRING_SZ + 1 + STORAGE_HID_ATRR_MASK_SIZE + 1 + \ 143 STORAGE_HID_SUB_CLASS_SIZE + 1 + STORAGE_HID_APP_ID_SIZE + 1 + \ 144 STORAGE_HID_VENDOR_ID_SIZE + 1 + STORAGE_HID_PRODUCT_ID_SIZE + 1 + \ 145 STORAGE_HID_VERSION_SIZE + 1 + STORAGE_HID_CTRY_CODE_SIZE + 1 + \ 146 STORAGE_HID_DESC_LEN_SIZE + 1 + STORAGE_HID_DESC_MAX_SIZE + 1) 147 148 /* currently remote services is the potentially largest entry */ 149 #define BTIF_STORAGE_MAX_LINE_SZ BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX 150 151 /* check against unv max entry size at compile time */ 152 #if (BTIF_STORAGE_ENTRY_MAX_SIZE > UNV_MAXLINE_LENGTH) 153 #error "btif storage entry size exceeds unv max line size" 154 #endif 155 156 /******************************************************************************* 157 * Local type definitions 158 ******************************************************************************/ 159 typedef struct { 160 uint32_t num_devices; 161 RawAddress devices[BTM_SEC_MAX_DEVICE_RECORDS]; 162 } btif_bonded_devices_t; 163 164 /******************************************************************************* 165 * External functions 166 ******************************************************************************/ 167 168 extern void btif_gatts_add_bonded_dev_from_nv(const RawAddress& bda); 169 170 /******************************************************************************* 171 * Internal Functions 172 ******************************************************************************/ 173 174 static bt_status_t btif_in_fetch_bonded_ble_device( 175 const std::string& remote_bd_addr, int add, 176 btif_bonded_devices_t* p_bonded_devices); 177 static bt_status_t btif_in_fetch_bonded_device(const std::string& bdstr); 178 179 static bool btif_has_ble_keys(const std::string& bdstr); 180 181 /******************************************************************************* 182 * Static functions 183 ******************************************************************************/ 184 185 static int prop2cfg(const RawAddress* remote_bd_addr, bt_property_t* prop) { 186 std::string bdstr; 187 if (remote_bd_addr) { 188 bdstr = remote_bd_addr->ToString(); 189 } 190 191 BTIF_TRACE_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr.c_str(), 192 prop->type, prop->len); 193 char value[1024]; 194 if (prop->len <= 0 || prop->len > (int)sizeof(value) - 1) { 195 BTIF_TRACE_ERROR("property type:%d, len:%d is invalid", prop->type, 196 prop->len); 197 return false; 198 } 199 switch (prop->type) { 200 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: 201 btif_config_set_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTIME, 202 (int)time(NULL)); 203 break; 204 case BT_PROPERTY_BDNAME: { 205 int name_length = prop->len > BTM_MAX_LOC_BD_NAME_LEN 206 ? BTM_MAX_LOC_BD_NAME_LEN 207 : prop->len; 208 strncpy(value, (char*)prop->val, name_length); 209 value[name_length] = '\0'; 210 if (remote_bd_addr) 211 btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_NAME, value); 212 else 213 btif_config_set_str("Adapter", BTIF_STORAGE_KEY_ADAPTER_NAME, value); 214 break; 215 } 216 case BT_PROPERTY_REMOTE_FRIENDLY_NAME: 217 strncpy(value, (char*)prop->val, prop->len); 218 value[prop->len] = '\0'; 219 btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_ALIASE, value); 220 break; 221 case BT_PROPERTY_ADAPTER_SCAN_MODE: 222 btif_config_set_int("Adapter", BTIF_STORAGE_KEY_ADAPTER_SCANMODE, 223 *(int*)prop->val); 224 break; 225 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: 226 btif_config_set_int("Adapter", BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, 227 *(int*)prop->val); 228 break; 229 case BT_PROPERTY_CLASS_OF_DEVICE: 230 btif_config_set_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVCLASS, 231 *(int*)prop->val); 232 break; 233 case BT_PROPERTY_TYPE_OF_DEVICE: 234 btif_config_set_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTYPE, 235 *(int*)prop->val); 236 break; 237 case BT_PROPERTY_UUIDS: { 238 std::string val; 239 size_t cnt = (prop->len) / sizeof(Uuid); 240 for (size_t i = 0; i < cnt; i++) { 241 val += (reinterpret_cast<Uuid*>(prop->val) + i)->ToString() + " "; 242 } 243 btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, val); 244 break; 245 } 246 case BT_PROPERTY_REMOTE_VERSION_INFO: { 247 bt_remote_version_t* info = (bt_remote_version_t*)prop->val; 248 249 if (!info) return false; 250 251 btif_config_set_int(bdstr, BTIF_STORAGE_PATH_REMOTE_VER_MFCT, 252 info->manufacturer); 253 btif_config_set_int(bdstr, BTIF_STORAGE_PATH_REMOTE_VER_VER, 254 info->version); 255 btif_config_set_int(bdstr, BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, 256 info->sub_ver); 257 } break; 258 259 default: 260 BTIF_TRACE_ERROR("Unknown prop type:%d", prop->type); 261 return false; 262 } 263 264 /* No need to look for bonded device with address of NULL */ 265 if (remote_bd_addr && 266 btif_in_fetch_bonded_device(bdstr) == BT_STATUS_SUCCESS) { 267 /* save changes if the device was bonded */ 268 btif_config_flush(); 269 } 270 271 return true; 272 } 273 274 static int cfg2prop(const RawAddress* remote_bd_addr, bt_property_t* prop) { 275 std::string bdstr; 276 if (remote_bd_addr) { 277 bdstr = remote_bd_addr->ToString(); 278 } 279 BTIF_TRACE_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr.c_str(), 280 prop->type, prop->len); 281 if (prop->len <= 0) { 282 BTIF_TRACE_ERROR("property type:%d, len:%d is invalid", prop->type, 283 prop->len); 284 return false; 285 } 286 int ret = false; 287 switch (prop->type) { 288 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: 289 if (prop->len >= (int)sizeof(int)) 290 ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTIME, 291 (int*)prop->val); 292 break; 293 case BT_PROPERTY_BDNAME: { 294 int len = prop->len; 295 if (remote_bd_addr) 296 ret = btif_config_get_str(bdstr, BTIF_STORAGE_PATH_REMOTE_NAME, 297 (char*)prop->val, &len); 298 else 299 ret = btif_config_get_str("Adapter", BTIF_STORAGE_KEY_ADAPTER_NAME, 300 (char*)prop->val, &len); 301 if (ret && len && len <= prop->len) 302 prop->len = len - 1; 303 else { 304 prop->len = 0; 305 ret = false; 306 } 307 break; 308 } 309 case BT_PROPERTY_REMOTE_FRIENDLY_NAME: { 310 int len = prop->len; 311 ret = btif_config_get_str(bdstr, BTIF_STORAGE_PATH_REMOTE_ALIASE, 312 (char*)prop->val, &len); 313 if (ret && len && len <= prop->len) 314 prop->len = len - 1; 315 else { 316 prop->len = 0; 317 ret = false; 318 } 319 break; 320 } 321 case BT_PROPERTY_ADAPTER_SCAN_MODE: 322 if (prop->len >= (int)sizeof(int)) 323 ret = btif_config_get_int("Adapter", BTIF_STORAGE_KEY_ADAPTER_SCANMODE, 324 (int*)prop->val); 325 break; 326 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: 327 if (prop->len >= (int)sizeof(int)) 328 ret = btif_config_get_int( 329 "Adapter", BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, (int*)prop->val); 330 break; 331 case BT_PROPERTY_CLASS_OF_DEVICE: 332 if (prop->len >= (int)sizeof(int)) 333 ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVCLASS, 334 (int*)prop->val); 335 break; 336 case BT_PROPERTY_TYPE_OF_DEVICE: 337 if (prop->len >= (int)sizeof(int)) 338 ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTYPE, 339 (int*)prop->val); 340 break; 341 case BT_PROPERTY_UUIDS: { 342 char value[1280]; 343 int size = sizeof(value); 344 if (btif_config_get_str(bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value, 345 &size)) { 346 Uuid* p_uuid = reinterpret_cast<Uuid*>(prop->val); 347 size_t num_uuids = 348 btif_split_uuids_string(value, p_uuid, BT_MAX_NUM_UUIDS); 349 prop->len = num_uuids * sizeof(Uuid); 350 ret = true; 351 } else { 352 prop->val = NULL; 353 prop->len = 0; 354 } 355 } break; 356 357 case BT_PROPERTY_REMOTE_VERSION_INFO: { 358 bt_remote_version_t* info = (bt_remote_version_t*)prop->val; 359 360 if (prop->len >= (int)sizeof(bt_remote_version_t)) { 361 ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_VER_MFCT, 362 &info->manufacturer); 363 364 if (ret) 365 ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_VER_VER, 366 &info->version); 367 368 if (ret) 369 ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, 370 &info->sub_ver); 371 } 372 } break; 373 374 default: 375 BTIF_TRACE_ERROR("Unknow prop type:%d", prop->type); 376 return false; 377 } 378 return ret; 379 } 380 381 /******************************************************************************* 382 * 383 * Function btif_in_fetch_bonded_devices 384 * 385 * Description Internal helper function to fetch the bonded devices 386 * from NVRAM 387 * 388 * Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise 389 * 390 ******************************************************************************/ 391 static bt_status_t btif_in_fetch_bonded_device(const std::string& bdstr) { 392 bool bt_linkkey_file_found = false; 393 394 LINK_KEY link_key; 395 size_t size = sizeof(link_key); 396 if (btif_config_get_bin(bdstr, "LinkKey", (uint8_t*)link_key, &size)) { 397 int linkkey_type; 398 if (btif_config_get_int(bdstr, "LinkKeyType", &linkkey_type)) { 399 bt_linkkey_file_found = true; 400 } else { 401 bt_linkkey_file_found = false; 402 } 403 } 404 if ((btif_in_fetch_bonded_ble_device(bdstr, false, NULL) != 405 BT_STATUS_SUCCESS) && 406 (!bt_linkkey_file_found)) { 407 BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", 408 bdstr.c_str()); 409 return BT_STATUS_FAIL; 410 } 411 return BT_STATUS_SUCCESS; 412 } 413 414 /******************************************************************************* 415 * 416 * Function btif_in_fetch_bonded_devices 417 * 418 * Description Internal helper function to fetch the bonded devices 419 * from NVRAM 420 * 421 * Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise 422 * 423 ******************************************************************************/ 424 static bt_status_t btif_in_fetch_bonded_devices( 425 btif_bonded_devices_t* p_bonded_devices, int add) { 426 memset(p_bonded_devices, 0, sizeof(btif_bonded_devices_t)); 427 428 bool bt_linkkey_file_found = false; 429 int device_type; 430 431 // TODO: this code is not thread safe, it can corrupt config content. 432 // b/67595284 433 for (const section_t& section : btif_config_sections()) { 434 const std::string& name = section.name; 435 if (!RawAddress::IsValidAddress(name)) continue; 436 437 BTIF_TRACE_DEBUG("Remote device:%s", name.c_str()); 438 LINK_KEY link_key; 439 size_t size = sizeof(link_key); 440 if (btif_config_get_bin(name, "LinkKey", link_key, &size)) { 441 int linkkey_type; 442 if (btif_config_get_int(name, "LinkKeyType", &linkkey_type)) { 443 RawAddress bd_addr; 444 RawAddress::FromString(name, bd_addr); 445 if (add) { 446 DEV_CLASS dev_class = {0, 0, 0}; 447 int cod; 448 int pin_length = 0; 449 if (btif_config_get_int(name, "DevClass", &cod)) 450 uint2devclass((uint32_t)cod, dev_class); 451 btif_config_get_int(name, "PinLength", &pin_length); 452 BTA_DmAddDevice(bd_addr, dev_class, link_key, 0, 0, 453 (uint8_t)linkkey_type, 0, pin_length); 454 455 if (btif_config_get_int(name, "DevType", &device_type) && 456 (device_type == BT_DEVICE_TYPE_DUMO)) { 457 btif_gatts_add_bonded_dev_from_nv(bd_addr); 458 } 459 } 460 bt_linkkey_file_found = true; 461 p_bonded_devices->devices[p_bonded_devices->num_devices++] = bd_addr; 462 } else { 463 bt_linkkey_file_found = false; 464 } 465 } 466 if (!btif_in_fetch_bonded_ble_device(name.c_str(), add, p_bonded_devices) && 467 !bt_linkkey_file_found) { 468 BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", 469 name.c_str()); 470 } 471 } 472 return BT_STATUS_SUCCESS; 473 } 474 475 static void btif_read_le_key(const uint8_t key_type, const size_t key_len, 476 RawAddress bd_addr, const uint8_t addr_type, 477 const bool add_key, bool* device_added, 478 bool* key_found) { 479 CHECK(device_added); 480 CHECK(key_found); 481 482 tBTA_LE_KEY_VALUE key; 483 memset(&key, 0, sizeof(key)); 484 485 if (btif_storage_get_ble_bonding_key(&bd_addr, key_type, (uint8_t*)&key, 486 key_len) == BT_STATUS_SUCCESS) { 487 if (add_key) { 488 if (!*device_added) { 489 BTA_DmAddBleDevice(bd_addr, addr_type, BT_DEVICE_TYPE_BLE); 490 *device_added = true; 491 } 492 493 BTIF_TRACE_DEBUG("%s() Adding key type %d for %s", __func__, key_type, 494 bd_addr.ToString().c_str()); 495 BTA_DmAddBleKey(bd_addr, &key, key_type); 496 } 497 498 *key_found = true; 499 } 500 } 501 502 /******************************************************************************* 503 * Functions 504 * 505 * Functions are synchronous and can be called by both from internal modules 506 * such as BTIF_DM and by external entiries from HAL via BTIF_context_switch. 507 * For OUT parameters, the caller is expected to provide the memory. 508 * Caller is expected to provide a valid pointer to 'property->value' based on 509 * the property->type. 510 ******************************************************************************/ 511 512 /******************************************************************************* 513 * 514 * Function btif_split_uuids_string 515 * 516 * Description Internal helper function to split the string of UUIDs 517 * read from the NVRAM to an array 518 * 519 * Returns Number of UUIDs parsed from the supplied string 520 * 521 ******************************************************************************/ 522 size_t btif_split_uuids_string(const char* str, bluetooth::Uuid* p_uuid, 523 size_t max_uuids) { 524 CHECK(str); 525 CHECK(p_uuid); 526 527 size_t num_uuids = 0; 528 while (str && num_uuids < max_uuids) { 529 bool is_valid; 530 bluetooth::Uuid tmp = 531 Uuid::FromString(std::string(str, Uuid::kString128BitLen), &is_valid); 532 if (!is_valid) break; 533 534 *p_uuid = tmp; 535 p_uuid++; 536 537 num_uuids++; 538 str = strchr(str, ' '); 539 if (str) str++; 540 } 541 542 return num_uuids; 543 } 544 545 /******************************************************************************* 546 * 547 * Function btif_storage_get_adapter_property 548 * 549 * Description BTIF storage API - Fetches the adapter property->type 550 * from NVRAM and fills property->val. 551 * Caller should provide memory for property->val and 552 * set the property->val 553 * 554 * Returns BT_STATUS_SUCCESS if the fetch was successful, 555 * BT_STATUS_FAIL otherwise 556 * 557 ******************************************************************************/ 558 bt_status_t btif_storage_get_adapter_property(bt_property_t* property) { 559 /* Special handling for adapter address and BONDED_DEVICES */ 560 if (property->type == BT_PROPERTY_BDADDR) { 561 RawAddress* bd_addr = (RawAddress*)property->val; 562 /* Fetch the local BD ADDR */ 563 const controller_t* controller = controller_get_interface(); 564 if (!controller->get_is_ready()) { 565 LOG_ERROR(LOG_TAG, 566 "%s: Controller not ready! Unable to return Bluetooth Address", 567 __func__); 568 *bd_addr = RawAddress::kEmpty; 569 return BT_STATUS_FAIL; 570 } else { 571 LOG_ERROR(LOG_TAG, "%s: Controller ready!", __func__); 572 *bd_addr = *controller->get_address(); 573 } 574 property->len = RawAddress::kLength; 575 return BT_STATUS_SUCCESS; 576 } else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES) { 577 btif_bonded_devices_t bonded_devices; 578 579 btif_in_fetch_bonded_devices(&bonded_devices, 0); 580 581 BTIF_TRACE_DEBUG( 582 "%s: Number of bonded devices: %d " 583 "Property:BT_PROPERTY_ADAPTER_BONDED_DEVICES", 584 __func__, bonded_devices.num_devices); 585 586 if (bonded_devices.num_devices > 0) { 587 property->len = bonded_devices.num_devices * RawAddress::kLength; 588 memcpy(property->val, bonded_devices.devices, property->len); 589 } 590 591 /* if there are no bonded_devices, then length shall be 0 */ 592 return BT_STATUS_SUCCESS; 593 } else if (property->type == BT_PROPERTY_UUIDS) { 594 /* publish list of local supported services */ 595 Uuid* p_uuid = reinterpret_cast<Uuid*>(property->val); 596 uint32_t num_uuids = 0; 597 uint32_t i; 598 599 tBTA_SERVICE_MASK service_mask = btif_get_enabled_services_mask(); 600 LOG_INFO(LOG_TAG, "%s service_mask:0x%x", __func__, service_mask); 601 for (i = 0; i < BTA_MAX_SERVICE_ID; i++) { 602 /* This should eventually become a function when more services are enabled 603 */ 604 if (service_mask & (tBTA_SERVICE_MASK)(1 << i)) { 605 switch (i) { 606 case BTA_HFP_SERVICE_ID: { 607 *(p_uuid + num_uuids) = 608 Uuid::From16Bit(UUID_SERVCLASS_AG_HANDSFREE); 609 num_uuids++; 610 } 611 /* intentional fall through: Send both BFP & HSP UUIDs if HFP is 612 * enabled */ 613 case BTA_HSP_SERVICE_ID: { 614 *(p_uuid + num_uuids) = 615 Uuid::From16Bit(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY); 616 num_uuids++; 617 } break; 618 case BTA_A2DP_SOURCE_SERVICE_ID: { 619 *(p_uuid + num_uuids) = 620 Uuid::From16Bit(UUID_SERVCLASS_AUDIO_SOURCE); 621 num_uuids++; 622 } break; 623 case BTA_A2DP_SINK_SERVICE_ID: { 624 *(p_uuid + num_uuids) = Uuid::From16Bit(UUID_SERVCLASS_AUDIO_SINK); 625 num_uuids++; 626 } break; 627 case BTA_HFP_HS_SERVICE_ID: { 628 *(p_uuid + num_uuids) = 629 Uuid::From16Bit(UUID_SERVCLASS_HF_HANDSFREE); 630 num_uuids++; 631 } break; 632 } 633 } 634 } 635 property->len = (num_uuids) * sizeof(Uuid); 636 return BT_STATUS_SUCCESS; 637 } 638 639 /* fall through for other properties */ 640 if (!cfg2prop(NULL, property)) { 641 return btif_dm_get_adapter_property(property); 642 } 643 return BT_STATUS_SUCCESS; 644 } 645 646 /******************************************************************************* 647 * 648 * Function btif_storage_set_adapter_property 649 * 650 * Description BTIF storage API - Stores the adapter property 651 * to NVRAM 652 * 653 * Returns BT_STATUS_SUCCESS if the store was successful, 654 * BT_STATUS_FAIL otherwise 655 * 656 ******************************************************************************/ 657 bt_status_t btif_storage_set_adapter_property(bt_property_t* property) { 658 return prop2cfg(NULL, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 659 } 660 661 /******************************************************************************* 662 * 663 * Function btif_storage_get_remote_device_property 664 * 665 * Description BTIF storage API - Fetches the remote device property->type 666 * from NVRAM and fills property->val. 667 * Caller should provide memory for property->val and 668 * set the property->val 669 * 670 * Returns BT_STATUS_SUCCESS if the fetch was successful, 671 * BT_STATUS_FAIL otherwise 672 * 673 ******************************************************************************/ 674 bt_status_t btif_storage_get_remote_device_property( 675 const RawAddress* remote_bd_addr, bt_property_t* property) { 676 return cfg2prop(remote_bd_addr, property) ? BT_STATUS_SUCCESS 677 : BT_STATUS_FAIL; 678 } 679 /******************************************************************************* 680 * 681 * Function btif_storage_set_remote_device_property 682 * 683 * Description BTIF storage API - Stores the remote device property 684 * to NVRAM 685 * 686 * Returns BT_STATUS_SUCCESS if the store was successful, 687 * BT_STATUS_FAIL otherwise 688 * 689 ******************************************************************************/ 690 bt_status_t btif_storage_set_remote_device_property( 691 const RawAddress* remote_bd_addr, bt_property_t* property) { 692 return prop2cfg(remote_bd_addr, property) ? BT_STATUS_SUCCESS 693 : BT_STATUS_FAIL; 694 } 695 696 /******************************************************************************* 697 * 698 * Function btif_storage_add_remote_device 699 * 700 * Description BTIF storage API - Adds a newly discovered device to NVRAM 701 * along with the timestamp. Also, stores the various 702 * properties - RSSI, BDADDR, NAME (if found in EIR) 703 * 704 * Returns BT_STATUS_SUCCESS if the store was successful, 705 * BT_STATUS_FAIL otherwise 706 * 707 ******************************************************************************/ 708 bt_status_t btif_storage_add_remote_device(const RawAddress* remote_bd_addr, 709 uint32_t num_properties, 710 bt_property_t* properties) { 711 uint32_t i = 0; 712 /* TODO: If writing a property, fails do we go back undo the earlier 713 * written properties? */ 714 for (i = 0; i < num_properties; i++) { 715 /* Ignore the RSSI as this is not stored in DB */ 716 if (properties[i].type == BT_PROPERTY_REMOTE_RSSI) continue; 717 718 /* address for remote device needs special handling as we also store 719 * timestamp */ 720 if (properties[i].type == BT_PROPERTY_BDADDR) { 721 bt_property_t addr_prop; 722 memcpy(&addr_prop, &properties[i], sizeof(bt_property_t)); 723 addr_prop.type = (bt_property_type_t)BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP; 724 btif_storage_set_remote_device_property(remote_bd_addr, &addr_prop); 725 } else { 726 btif_storage_set_remote_device_property(remote_bd_addr, &properties[i]); 727 } 728 } 729 return BT_STATUS_SUCCESS; 730 } 731 732 /******************************************************************************* 733 * 734 * Function btif_storage_add_bonded_device 735 * 736 * Description BTIF storage API - Adds the newly bonded device to NVRAM 737 * along with the link-key, Key type and Pin key length 738 * 739 * Returns BT_STATUS_SUCCESS if the store was successful, 740 * BT_STATUS_FAIL otherwise 741 * 742 ******************************************************************************/ 743 744 bt_status_t btif_storage_add_bonded_device(RawAddress* remote_bd_addr, 745 LINK_KEY link_key, uint8_t key_type, 746 uint8_t pin_length) { 747 std::string bdstr = remote_bd_addr->ToString(); 748 int ret = btif_config_set_int(bdstr, "LinkKeyType", (int)key_type); 749 ret &= btif_config_set_int(bdstr, "PinLength", (int)pin_length); 750 ret &= btif_config_set_bin(bdstr, "LinkKey", link_key, sizeof(LINK_KEY)); 751 752 if (is_restricted_mode()) { 753 BTIF_TRACE_WARNING("%s: '%s' pairing will be removed if unrestricted", 754 __func__, bdstr.c_str()); 755 btif_config_set_int(bdstr, "Restricted", 1); 756 } 757 758 /* write bonded info immediately */ 759 btif_config_flush(); 760 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 761 } 762 763 /******************************************************************************* 764 * 765 * Function btif_storage_remove_bonded_device 766 * 767 * Description BTIF storage API - Deletes the bonded device from NVRAM 768 * 769 * Returns BT_STATUS_SUCCESS if the deletion was successful, 770 * BT_STATUS_FAIL otherwise 771 * 772 ******************************************************************************/ 773 bt_status_t btif_storage_remove_bonded_device( 774 const RawAddress* remote_bd_addr) { 775 std::string bdstr = remote_bd_addr->ToString(); 776 BTIF_TRACE_DEBUG("in bd addr:%s", bdstr.c_str()); 777 778 btif_storage_remove_ble_bonding_keys(remote_bd_addr); 779 780 int ret = 1; 781 if (btif_config_exist(bdstr, "LinkKeyType")) 782 ret &= btif_config_remove(bdstr, "LinkKeyType"); 783 if (btif_config_exist(bdstr, "PinLength")) 784 ret &= btif_config_remove(bdstr, "PinLength"); 785 if (btif_config_exist(bdstr, "LinkKey")) 786 ret &= btif_config_remove(bdstr, "LinkKey"); 787 if (btif_config_exist(bdstr, BTIF_STORAGE_PATH_REMOTE_ALIASE)) { 788 ret &= btif_config_remove(bdstr, BTIF_STORAGE_PATH_REMOTE_ALIASE); 789 } 790 /* write bonded info immediately */ 791 btif_config_flush(); 792 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 793 } 794 795 /******************************************************************************* 796 * 797 * Function btif_storage_load_bonded_devices 798 * 799 * Description BTIF storage API - Loads all the bonded devices from NVRAM 800 * and adds to the BTA. 801 * Additionally, this API also invokes the adaper_properties_cb 802 * and remote_device_properties_cb for each of the bonded 803 * devices. 804 * 805 * Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise 806 * 807 ******************************************************************************/ 808 bt_status_t btif_storage_load_bonded_devices(void) { 809 btif_bonded_devices_t bonded_devices; 810 uint32_t i = 0; 811 bt_property_t adapter_props[6]; 812 uint32_t num_props = 0; 813 bt_property_t remote_properties[8]; 814 RawAddress addr; 815 bt_bdname_t name, alias; 816 bt_scan_mode_t mode; 817 uint32_t disc_timeout; 818 Uuid local_uuids[BT_MAX_NUM_UUIDS]; 819 Uuid remote_uuids[BT_MAX_NUM_UUIDS]; 820 bt_status_t status; 821 822 btif_in_fetch_bonded_devices(&bonded_devices, 1); 823 824 /* Now send the adapter_properties_cb with all adapter_properties */ 825 { 826 memset(adapter_props, 0, sizeof(adapter_props)); 827 828 /* address */ 829 BTIF_STORAGE_GET_ADAPTER_PROP(status, BT_PROPERTY_BDADDR, &addr, 830 sizeof(addr), adapter_props[num_props]); 831 // Add BT_PROPERTY_BDADDR property into list only when successful. 832 // Otherwise, skip this property entry. 833 if (status == BT_STATUS_SUCCESS) { 834 num_props++; 835 } 836 837 /* BD_NAME */ 838 BTIF_STORAGE_GET_ADAPTER_PROP(status, BT_PROPERTY_BDNAME, &name, 839 sizeof(name), adapter_props[num_props]); 840 num_props++; 841 842 /* SCAN_MODE */ 843 /* TODO: At the time of BT on, always report the scan mode as 0 irrespective 844 of the scan_mode during the previous enable cycle. 845 This needs to be re-visited as part of the app/stack enable sequence 846 synchronization */ 847 mode = BT_SCAN_MODE_NONE; 848 adapter_props[num_props].type = BT_PROPERTY_ADAPTER_SCAN_MODE; 849 adapter_props[num_props].len = sizeof(mode); 850 adapter_props[num_props].val = &mode; 851 num_props++; 852 853 /* DISC_TIMEOUT */ 854 BTIF_STORAGE_GET_ADAPTER_PROP(status, BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT, 855 &disc_timeout, sizeof(disc_timeout), 856 adapter_props[num_props]); 857 num_props++; 858 859 /* BONDED_DEVICES */ 860 RawAddress* devices_list = (RawAddress*)osi_malloc( 861 sizeof(RawAddress) * bonded_devices.num_devices); 862 adapter_props[num_props].type = BT_PROPERTY_ADAPTER_BONDED_DEVICES; 863 adapter_props[num_props].len = 864 bonded_devices.num_devices * sizeof(RawAddress); 865 adapter_props[num_props].val = devices_list; 866 for (i = 0; i < bonded_devices.num_devices; i++) { 867 devices_list[i] = bonded_devices.devices[i]; 868 } 869 num_props++; 870 871 /* LOCAL UUIDs */ 872 BTIF_STORAGE_GET_ADAPTER_PROP(status, BT_PROPERTY_UUIDS, local_uuids, 873 sizeof(local_uuids), 874 adapter_props[num_props]); 875 num_props++; 876 877 btif_adapter_properties_evt(BT_STATUS_SUCCESS, num_props, adapter_props); 878 879 osi_free(devices_list); 880 } 881 882 BTIF_TRACE_EVENT("%s: %d bonded devices found", __func__, 883 bonded_devices.num_devices); 884 885 { 886 for (i = 0; i < bonded_devices.num_devices; i++) { 887 RawAddress* p_remote_addr; 888 889 /* 890 * TODO: improve handling of missing fields in NVRAM. 891 */ 892 uint32_t cod = 0; 893 uint32_t devtype = 0; 894 895 num_props = 0; 896 p_remote_addr = &bonded_devices.devices[i]; 897 memset(remote_properties, 0, sizeof(remote_properties)); 898 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_BDNAME, &name, 899 sizeof(name), remote_properties[num_props]); 900 num_props++; 901 902 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, 903 BT_PROPERTY_REMOTE_FRIENDLY_NAME, &alias, 904 sizeof(alias), remote_properties[num_props]); 905 num_props++; 906 907 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_CLASS_OF_DEVICE, 908 &cod, sizeof(cod), 909 remote_properties[num_props]); 910 num_props++; 911 912 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_TYPE_OF_DEVICE, 913 &devtype, sizeof(devtype), 914 remote_properties[num_props]); 915 num_props++; 916 917 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_UUIDS, 918 remote_uuids, sizeof(remote_uuids), 919 remote_properties[num_props]); 920 num_props++; 921 922 btif_remote_properties_evt(BT_STATUS_SUCCESS, p_remote_addr, num_props, 923 remote_properties); 924 } 925 } 926 return BT_STATUS_SUCCESS; 927 } 928 929 /******************************************************************************* 930 * 931 * Function btif_storage_add_ble_bonding_key 932 * 933 * Description BTIF storage API - Adds the newly bonded device to NVRAM 934 * along with the ble-key, Key type and Pin key length 935 * 936 * Returns BT_STATUS_SUCCESS if the store was successful, 937 * BT_STATUS_FAIL otherwise 938 * 939 ******************************************************************************/ 940 941 bt_status_t btif_storage_add_ble_bonding_key(RawAddress* remote_bd_addr, 942 const uint8_t* key, 943 uint8_t key_type, 944 uint8_t key_length) { 945 const char* name; 946 switch (key_type) { 947 case BTIF_DM_LE_KEY_PENC: 948 name = "LE_KEY_PENC"; 949 break; 950 case BTIF_DM_LE_KEY_PID: 951 name = "LE_KEY_PID"; 952 break; 953 case BTIF_DM_LE_KEY_PCSRK: 954 name = "LE_KEY_PCSRK"; 955 break; 956 case BTIF_DM_LE_KEY_LENC: 957 name = "LE_KEY_LENC"; 958 break; 959 case BTIF_DM_LE_KEY_LCSRK: 960 name = "LE_KEY_LCSRK"; 961 break; 962 case BTIF_DM_LE_KEY_LID: 963 name = "LE_KEY_LID"; 964 break; 965 default: 966 return BT_STATUS_FAIL; 967 } 968 int ret = 969 btif_config_set_bin(remote_bd_addr->ToString(), name, key, key_length); 970 btif_config_save(); 971 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 972 } 973 974 /******************************************************************************* 975 * 976 * Function btif_storage_get_ble_bonding_key 977 * 978 * Description 979 * 980 * Returns BT_STATUS_SUCCESS if the fetch was successful, 981 * BT_STATUS_FAIL otherwise 982 * 983 ******************************************************************************/ 984 bt_status_t btif_storage_get_ble_bonding_key(RawAddress* remote_bd_addr, 985 uint8_t key_type, 986 uint8_t* key_value, 987 int key_length) { 988 const char* name; 989 switch (key_type) { 990 case BTIF_DM_LE_KEY_PENC: 991 name = "LE_KEY_PENC"; 992 break; 993 case BTIF_DM_LE_KEY_PID: 994 name = "LE_KEY_PID"; 995 break; 996 case BTIF_DM_LE_KEY_PCSRK: 997 name = "LE_KEY_PCSRK"; 998 break; 999 case BTIF_DM_LE_KEY_LENC: 1000 name = "LE_KEY_LENC"; 1001 break; 1002 case BTIF_DM_LE_KEY_LCSRK: 1003 name = "LE_KEY_LCSRK"; 1004 break; 1005 case BTIF_DM_LE_KEY_LID: 1006 name = "LE_KEY_LID"; 1007 default: 1008 return BT_STATUS_FAIL; 1009 } 1010 size_t length = key_length; 1011 int ret = 1012 btif_config_get_bin(remote_bd_addr->ToString(), name, key_value, &length); 1013 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 1014 } 1015 1016 /******************************************************************************* 1017 * 1018 * Function btif_storage_remove_ble_keys 1019 * 1020 * Description BTIF storage API - Deletes the bonded device from NVRAM 1021 * 1022 * Returns BT_STATUS_SUCCESS if the deletion was successful, 1023 * BT_STATUS_FAIL otherwise 1024 * 1025 ******************************************************************************/ 1026 bt_status_t btif_storage_remove_ble_bonding_keys( 1027 const RawAddress* remote_bd_addr) { 1028 std::string bdstr = remote_bd_addr->ToString(); 1029 BTIF_TRACE_DEBUG(" %s in bd addr:%s", __func__, bdstr.c_str()); 1030 int ret = 1; 1031 if (btif_config_exist(bdstr, "LE_KEY_PENC")) 1032 ret &= btif_config_remove(bdstr, "LE_KEY_PENC"); 1033 if (btif_config_exist(bdstr, "LE_KEY_PID")) 1034 ret &= btif_config_remove(bdstr, "LE_KEY_PID"); 1035 if (btif_config_exist(bdstr, "LE_KEY_PCSRK")) 1036 ret &= btif_config_remove(bdstr, "LE_KEY_PCSRK"); 1037 if (btif_config_exist(bdstr, "LE_KEY_LENC")) 1038 ret &= btif_config_remove(bdstr, "LE_KEY_LENC"); 1039 if (btif_config_exist(bdstr, "LE_KEY_LCSRK")) 1040 ret &= btif_config_remove(bdstr, "LE_KEY_LCSRK"); 1041 btif_config_save(); 1042 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 1043 } 1044 1045 /******************************************************************************* 1046 * 1047 * Function btif_storage_add_ble_local_key 1048 * 1049 * Description BTIF storage API - Adds the ble key to NVRAM 1050 * 1051 * Returns BT_STATUS_SUCCESS if the store was successful, 1052 * BT_STATUS_FAIL otherwise 1053 * 1054 ******************************************************************************/ 1055 bt_status_t btif_storage_add_ble_local_key(char* key, uint8_t key_type, 1056 uint8_t key_length) { 1057 const char* name; 1058 switch (key_type) { 1059 case BTIF_DM_LE_LOCAL_KEY_IR: 1060 name = "LE_LOCAL_KEY_IR"; 1061 break; 1062 case BTIF_DM_LE_LOCAL_KEY_IRK: 1063 name = "LE_LOCAL_KEY_IRK"; 1064 break; 1065 case BTIF_DM_LE_LOCAL_KEY_DHK: 1066 name = "LE_LOCAL_KEY_DHK"; 1067 break; 1068 case BTIF_DM_LE_LOCAL_KEY_ER: 1069 name = "LE_LOCAL_KEY_ER"; 1070 break; 1071 default: 1072 return BT_STATUS_FAIL; 1073 } 1074 int ret = 1075 btif_config_set_bin("Adapter", name, (const uint8_t*)key, key_length); 1076 btif_config_save(); 1077 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 1078 } 1079 1080 /******************************************************************************* 1081 * 1082 * Function btif_storage_get_ble_local_key 1083 * 1084 * Description 1085 * 1086 * Returns BT_STATUS_SUCCESS if the fetch was successful, 1087 * BT_STATUS_FAIL otherwise 1088 * 1089 ******************************************************************************/ 1090 bt_status_t btif_storage_get_ble_local_key(uint8_t key_type, char* key_value, 1091 int key_length) { 1092 const char* name; 1093 switch (key_type) { 1094 case BTIF_DM_LE_LOCAL_KEY_IR: 1095 name = "LE_LOCAL_KEY_IR"; 1096 break; 1097 case BTIF_DM_LE_LOCAL_KEY_IRK: 1098 name = "LE_LOCAL_KEY_IRK"; 1099 break; 1100 case BTIF_DM_LE_LOCAL_KEY_DHK: 1101 name = "LE_LOCAL_KEY_DHK"; 1102 break; 1103 case BTIF_DM_LE_LOCAL_KEY_ER: 1104 name = "LE_LOCAL_KEY_ER"; 1105 break; 1106 default: 1107 return BT_STATUS_FAIL; 1108 } 1109 size_t length = key_length; 1110 int ret = btif_config_get_bin("Adapter", name, (uint8_t*)key_value, &length); 1111 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 1112 } 1113 1114 /******************************************************************************* 1115 * 1116 * Function btif_storage_remove_ble_local_keys 1117 * 1118 * Description BTIF storage API - Deletes the bonded device from NVRAM 1119 * 1120 * Returns BT_STATUS_SUCCESS if the deletion was successful, 1121 * BT_STATUS_FAIL otherwise 1122 * 1123 ******************************************************************************/ 1124 bt_status_t btif_storage_remove_ble_local_keys(void) { 1125 int ret = 1; 1126 if (btif_config_exist("Adapter", "LE_LOCAL_KEY_IR")) 1127 ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_IR"); 1128 if (btif_config_exist("Adapter", "LE_LOCAL_KEY_IRK")) 1129 ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_IRK"); 1130 if (btif_config_exist("Adapter", "LE_LOCAL_KEY_DHK")) 1131 ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_DHK"); 1132 if (btif_config_exist("Adapter", "LE_LOCAL_KEY_ER")) 1133 ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_ER"); 1134 btif_config_save(); 1135 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 1136 } 1137 1138 static bt_status_t btif_in_fetch_bonded_ble_device( 1139 const std::string& remote_bd_addr, int add, 1140 btif_bonded_devices_t* p_bonded_devices) { 1141 int device_type; 1142 int addr_type; 1143 bool device_added = false; 1144 bool key_found = false; 1145 1146 if (!btif_config_get_int(remote_bd_addr, "DevType", &device_type)) 1147 return BT_STATUS_FAIL; 1148 1149 if ((device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE || 1150 btif_has_ble_keys(remote_bd_addr)) { 1151 BTIF_TRACE_DEBUG("%s Found a LE device: %s", __func__, 1152 remote_bd_addr.c_str()); 1153 1154 RawAddress bd_addr; 1155 RawAddress::FromString(remote_bd_addr, bd_addr); 1156 1157 if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) != 1158 BT_STATUS_SUCCESS) { 1159 addr_type = BLE_ADDR_PUBLIC; 1160 btif_storage_set_remote_addr_type(&bd_addr, BLE_ADDR_PUBLIC); 1161 } 1162 1163 btif_read_le_key(BTIF_DM_LE_KEY_PENC, sizeof(tBTM_LE_PENC_KEYS), bd_addr, 1164 addr_type, add, &device_added, &key_found); 1165 1166 btif_read_le_key(BTIF_DM_LE_KEY_PID, sizeof(tBTM_LE_PID_KEYS), bd_addr, 1167 addr_type, add, &device_added, &key_found); 1168 1169 btif_read_le_key(BTIF_DM_LE_KEY_LID, sizeof(tBTM_LE_PID_KEYS), bd_addr, 1170 addr_type, add, &device_added, &key_found); 1171 1172 btif_read_le_key(BTIF_DM_LE_KEY_PCSRK, sizeof(tBTM_LE_PCSRK_KEYS), bd_addr, 1173 addr_type, add, &device_added, &key_found); 1174 1175 btif_read_le_key(BTIF_DM_LE_KEY_LENC, sizeof(tBTM_LE_LENC_KEYS), bd_addr, 1176 addr_type, add, &device_added, &key_found); 1177 1178 btif_read_le_key(BTIF_DM_LE_KEY_LCSRK, sizeof(tBTM_LE_LCSRK_KEYS), bd_addr, 1179 addr_type, add, &device_added, &key_found); 1180 1181 // Fill in the bonded devices 1182 if (device_added) { 1183 p_bonded_devices->devices[p_bonded_devices->num_devices++] = bd_addr; 1184 btif_gatts_add_bonded_dev_from_nv(bd_addr); 1185 } 1186 1187 if (key_found) return BT_STATUS_SUCCESS; 1188 } 1189 return BT_STATUS_FAIL; 1190 } 1191 1192 bt_status_t btif_storage_set_remote_addr_type(const RawAddress* remote_bd_addr, 1193 uint8_t addr_type) { 1194 int ret = btif_config_set_int(remote_bd_addr->ToString(), "AddrType", 1195 (int)addr_type); 1196 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 1197 } 1198 1199 bool btif_has_ble_keys(const std::string& bdstr) { 1200 return btif_config_exist(bdstr, "LE_KEY_PENC"); 1201 } 1202 1203 /******************************************************************************* 1204 * 1205 * Function btif_storage_get_remote_addr_type 1206 * 1207 * Description BTIF storage API - Fetches the remote addr type 1208 * 1209 * Returns BT_STATUS_SUCCESS if the fetch was successful, 1210 * BT_STATUS_FAIL otherwise 1211 * 1212 ******************************************************************************/ 1213 bt_status_t btif_storage_get_remote_addr_type(const RawAddress* remote_bd_addr, 1214 int* addr_type) { 1215 int ret = 1216 btif_config_get_int(remote_bd_addr->ToString(), "AddrType", addr_type); 1217 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; 1218 } 1219 /******************************************************************************* 1220 * 1221 * Function btif_storage_add_hid_device_info 1222 * 1223 * Description BTIF storage API - Adds the hid information of bonded hid 1224 * devices-to NVRAM 1225 * 1226 * Returns BT_STATUS_SUCCESS if the store was successful, 1227 * BT_STATUS_FAIL otherwise 1228 * 1229 ******************************************************************************/ 1230 1231 bt_status_t btif_storage_add_hid_device_info( 1232 RawAddress* remote_bd_addr, uint16_t attr_mask, uint8_t sub_class, 1233 uint8_t app_id, uint16_t vendor_id, uint16_t product_id, uint16_t version, 1234 uint8_t ctry_code, uint16_t ssr_max_latency, uint16_t ssr_min_tout, 1235 uint16_t dl_len, uint8_t* dsc_list) { 1236 BTIF_TRACE_DEBUG("btif_storage_add_hid_device_info:"); 1237 std::string bdstr = remote_bd_addr->ToString(); 1238 btif_config_set_int(bdstr, "HidAttrMask", attr_mask); 1239 btif_config_set_int(bdstr, "HidSubClass", sub_class); 1240 btif_config_set_int(bdstr, "HidAppId", app_id); 1241 btif_config_set_int(bdstr, "HidVendorId", vendor_id); 1242 btif_config_set_int(bdstr, "HidProductId", product_id); 1243 btif_config_set_int(bdstr, "HidVersion", version); 1244 btif_config_set_int(bdstr, "HidCountryCode", ctry_code); 1245 btif_config_set_int(bdstr, "HidSSRMaxLatency", ssr_max_latency); 1246 btif_config_set_int(bdstr, "HidSSRMinTimeout", ssr_min_tout); 1247 if (dl_len > 0) btif_config_set_bin(bdstr, "HidDescriptor", dsc_list, dl_len); 1248 btif_config_save(); 1249 return BT_STATUS_SUCCESS; 1250 } 1251 1252 /******************************************************************************* 1253 * 1254 * Function btif_storage_load_bonded_hid_info 1255 * 1256 * Description BTIF storage API - Loads hid info for all the bonded devices 1257 * from NVRAM and adds those devices to the BTA_HH. 1258 * 1259 * Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise 1260 * 1261 ******************************************************************************/ 1262 bt_status_t btif_storage_load_bonded_hid_info(void) { 1263 // TODO: this code is not thread safe, it can corrupt config content. 1264 // b/67595284 1265 for (const section_t& section : btif_config_sections()) { 1266 const std::string& name = section.name; 1267 if (!RawAddress::IsValidAddress(name)) continue; 1268 1269 BTIF_TRACE_DEBUG("Remote device:%s", name.c_str()); 1270 1271 int value; 1272 if (!btif_config_get_int(name, "HidAttrMask", &value)) continue; 1273 uint16_t attr_mask = (uint16_t)value; 1274 1275 if (btif_in_fetch_bonded_device(name) != BT_STATUS_SUCCESS) { 1276 RawAddress bd_addr; 1277 RawAddress::FromString(name, bd_addr); 1278 btif_storage_remove_hid_info(&bd_addr); 1279 continue; 1280 } 1281 1282 tBTA_HH_DEV_DSCP_INFO dscp_info; 1283 memset(&dscp_info, 0, sizeof(dscp_info)); 1284 1285 btif_config_get_int(name, "HidSubClass", &value); 1286 uint8_t sub_class = (uint8_t)value; 1287 1288 btif_config_get_int(name, "HidAppId", &value); 1289 uint8_t app_id = (uint8_t)value; 1290 1291 btif_config_get_int(name, "HidVendorId", &value); 1292 dscp_info.vendor_id = (uint16_t)value; 1293 1294 btif_config_get_int(name, "HidProductId", &value); 1295 dscp_info.product_id = (uint16_t)value; 1296 1297 btif_config_get_int(name, "HidVersion", &value); 1298 dscp_info.version = (uint8_t)value; 1299 1300 btif_config_get_int(name, "HidCountryCode", &value); 1301 dscp_info.ctry_code = (uint8_t)value; 1302 1303 value = 0; 1304 btif_config_get_int(name, "HidSSRMaxLatency", &value); 1305 dscp_info.ssr_max_latency = (uint16_t)value; 1306 1307 value = 0; 1308 btif_config_get_int(name, "HidSSRMinTimeout", &value); 1309 dscp_info.ssr_min_tout = (uint16_t)value; 1310 1311 size_t len = btif_config_get_bin_length(name, "HidDescriptor"); 1312 if (len > 0) { 1313 dscp_info.descriptor.dl_len = (uint16_t)len; 1314 dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len); 1315 btif_config_get_bin(name, "HidDescriptor", 1316 (uint8_t*)dscp_info.descriptor.dsc_list, &len); 1317 } 1318 1319 RawAddress bd_addr; 1320 RawAddress::FromString(name, bd_addr); 1321 // add extracted information to BTA HH 1322 if (btif_hh_add_added_dev(bd_addr, attr_mask)) { 1323 BTA_HhAddDev(bd_addr, attr_mask, sub_class, app_id, dscp_info); 1324 } 1325 } 1326 1327 return BT_STATUS_SUCCESS; 1328 } 1329 1330 /******************************************************************************* 1331 * 1332 * Function btif_storage_remove_hid_info 1333 * 1334 * Description BTIF storage API - Deletes the bonded hid device info from 1335 * NVRAM 1336 * 1337 * Returns BT_STATUS_SUCCESS if the deletion was successful, 1338 * BT_STATUS_FAIL otherwise 1339 * 1340 ******************************************************************************/ 1341 bt_status_t btif_storage_remove_hid_info(RawAddress* remote_bd_addr) { 1342 std::string bdstr = remote_bd_addr->ToString(); 1343 1344 btif_config_remove(bdstr, "HidAttrMask"); 1345 btif_config_remove(bdstr, "HidSubClass"); 1346 btif_config_remove(bdstr, "HidAppId"); 1347 btif_config_remove(bdstr, "HidVendorId"); 1348 btif_config_remove(bdstr, "HidProductId"); 1349 btif_config_remove(bdstr, "HidVersion"); 1350 btif_config_remove(bdstr, "HidCountryCode"); 1351 btif_config_remove(bdstr, "HidSSRMaxLatency"); 1352 btif_config_remove(bdstr, "HidSSRMinTimeout"); 1353 btif_config_remove(bdstr, "HidDescriptor"); 1354 btif_config_save(); 1355 return BT_STATUS_SUCCESS; 1356 } 1357 1358 constexpr char HEARING_AID_PSM[] = "HearingAidPsm"; 1359 constexpr char HEARING_AID_CAPABILITIES[] = "HearingAidCapabilities"; 1360 constexpr char HEARING_AID_CODECS[] = "HearingAidCodecs"; 1361 constexpr char HEARING_AID_AUDIO_CONTROL_POINT[] = 1362 "HearingAidAudioControlPoint"; 1363 constexpr char HEARING_AID_VOLUME_HANDLE[] = "HearingAidVolumeHandle"; 1364 constexpr char HEARING_AID_SYNC_ID[] = "HearingAidSyncId"; 1365 constexpr char HEARING_AID_RENDER_DELAY[] = "HearingAidRenderDelay"; 1366 constexpr char HEARING_AID_PREPARATION_DELAY[] = "HearingAidPreparationDelay"; 1367 constexpr char HEARING_AID_IS_WHITE_LISTED[] = "HearingAidIsWhiteListed"; 1368 1369 void btif_storage_add_hearing_aid(const RawAddress& address, uint16_t psm, 1370 uint8_t capabilities, uint16_t codecs, 1371 uint16_t audio_control_point_handle, 1372 uint16_t volume_handle, uint64_t hi_sync_id, 1373 uint16_t render_delay, 1374 uint16_t preparation_delay) { 1375 do_in_jni_thread( 1376 FROM_HERE, 1377 Bind( 1378 [](const RawAddress& address, uint16_t psm, uint8_t capabilities, 1379 uint16_t codecs, uint16_t audio_control_point_handle, 1380 uint16_t volume_handle, uint64_t hi_sync_id, uint16_t render_delay, 1381 uint16_t preparation_delay) { 1382 std::string bdstr = address.ToString(); 1383 VLOG(2) << "saving hearing aid device: " << bdstr; 1384 btif_config_set_int(bdstr, HEARING_AID_PSM, psm); 1385 btif_config_set_int(bdstr, HEARING_AID_CAPABILITIES, capabilities); 1386 btif_config_set_int(bdstr, HEARING_AID_CODECS, codecs); 1387 btif_config_set_int(bdstr, HEARING_AID_AUDIO_CONTROL_POINT, 1388 audio_control_point_handle); 1389 btif_config_set_int(bdstr, HEARING_AID_VOLUME_HANDLE, 1390 volume_handle); 1391 btif_config_set_uint64(bdstr, HEARING_AID_SYNC_ID, hi_sync_id); 1392 btif_config_set_int(bdstr, HEARING_AID_RENDER_DELAY, render_delay); 1393 btif_config_set_int(bdstr, HEARING_AID_PREPARATION_DELAY, 1394 preparation_delay); 1395 btif_config_set_int(bdstr, HEARING_AID_IS_WHITE_LISTED, true); 1396 btif_config_save(); 1397 }, 1398 address, psm, capabilities, codecs, audio_control_point_handle, 1399 volume_handle, hi_sync_id, render_delay, preparation_delay)); 1400 } 1401 1402 /** Loads information about bonded hearing aid devices */ 1403 void btif_storage_load_bonded_hearing_aids() { 1404 // TODO: this code is not thread safe, it can corrupt config content. 1405 // b/67595284 1406 for (const section_t& section : btif_config_sections()) { 1407 const std::string& name = section.name; 1408 if (!RawAddress::IsValidAddress(name)) continue; 1409 1410 BTIF_TRACE_DEBUG("Remote device:%s", name.c_str()); 1411 1412 int value; 1413 if (!btif_config_get_int(name, HEARING_AID_PSM, &value)) continue; 1414 uint16_t psm = value; 1415 1416 if (btif_in_fetch_bonded_device(name.c_str()) != BT_STATUS_SUCCESS) { 1417 RawAddress bd_addr; 1418 RawAddress::FromString(name, bd_addr); 1419 btif_storage_remove_hearing_aid(bd_addr); 1420 continue; 1421 } 1422 1423 uint8_t capabilities = 0; 1424 if (btif_config_get_int(name, HEARING_AID_CAPABILITIES, &value)) 1425 capabilities = value; 1426 1427 uint16_t codecs = 0; 1428 if (btif_config_get_int(name, HEARING_AID_CODECS, &value)) codecs = value; 1429 1430 uint16_t audio_control_point_handle = 0; 1431 if (btif_config_get_int(name, HEARING_AID_AUDIO_CONTROL_POINT, &value)) 1432 audio_control_point_handle = value; 1433 1434 uint16_t volume_handle = 0; 1435 if (btif_config_get_int(name, HEARING_AID_VOLUME_HANDLE, &value)) 1436 volume_handle = value; 1437 1438 uint64_t lvalue; 1439 uint64_t hi_sync_id = 0; 1440 if (btif_config_get_uint64(name, HEARING_AID_SYNC_ID, &lvalue)) 1441 hi_sync_id = lvalue; 1442 1443 uint16_t render_delay = 0; 1444 if (btif_config_get_int(name, HEARING_AID_RENDER_DELAY, &value)) 1445 render_delay = value; 1446 1447 uint16_t preparation_delay = 0; 1448 if (btif_config_get_int(name, HEARING_AID_PREPARATION_DELAY, &value)) 1449 preparation_delay = value; 1450 1451 uint16_t is_white_listed = 0; 1452 if (btif_config_get_int(name, HEARING_AID_IS_WHITE_LISTED, &value)) 1453 is_white_listed = value; 1454 1455 RawAddress bd_addr; 1456 RawAddress::FromString(name, bd_addr); 1457 // add extracted information to BTA Hearing Aid 1458 do_in_bta_thread( 1459 FROM_HERE, 1460 Bind(&HearingAid::AddFromStorage, bd_addr, psm, capabilities, codecs, 1461 audio_control_point_handle, volume_handle, hi_sync_id, 1462 render_delay, preparation_delay, is_white_listed)); 1463 } 1464 } 1465 1466 /** Deletes the bonded hearing aid device info from NVRAM */ 1467 void btif_storage_remove_hearing_aid(const RawAddress& address) { 1468 std::string addrstr = address.ToString(); 1469 1470 btif_config_remove(addrstr, HEARING_AID_PSM); 1471 btif_config_remove(addrstr, HEARING_AID_CAPABILITIES); 1472 btif_config_remove(addrstr, HEARING_AID_CODECS); 1473 btif_config_remove(addrstr, HEARING_AID_AUDIO_CONTROL_POINT); 1474 btif_config_remove(addrstr, HEARING_AID_VOLUME_HANDLE); 1475 btif_config_remove(addrstr, HEARING_AID_SYNC_ID); 1476 btif_config_remove(addrstr, HEARING_AID_IS_WHITE_LISTED); 1477 btif_config_save(); 1478 } 1479 1480 /** Remove the hearing aid device from white list */ 1481 void btif_storage_remove_hearing_aid_white_list(const RawAddress& address) { 1482 std::string addrstr = address.ToString(); 1483 1484 btif_config_set_int(addrstr, HEARING_AID_IS_WHITE_LISTED, false); 1485 } 1486 1487 /******************************************************************************* 1488 * 1489 * Function btif_storage_is_restricted_device 1490 * 1491 * Description BTIF storage API - checks if this device is a restricted 1492 * device 1493 * 1494 * Returns true if the device is labeled as restricted 1495 * false otherwise 1496 * 1497 ******************************************************************************/ 1498 bool btif_storage_is_restricted_device(const RawAddress* remote_bd_addr) { 1499 return btif_config_exist(remote_bd_addr->ToString(), "Restricted"); 1500 } 1501 1502 /******************************************************************************* 1503 * Function btif_storage_load_hidd 1504 * 1505 * Description Loads hidd bonded device and "plugs" it into hidd 1506 * 1507 * Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise 1508 * 1509 ******************************************************************************/ 1510 bt_status_t btif_storage_load_hidd(void) { 1511 // TODO: this code is not thread safe, it can corrupt config content. 1512 // b/67595284 1513 for (const section_t& section : btif_config_sections()) { 1514 const std::string& name = section.name; 1515 if (!RawAddress::IsValidAddress(name)) continue; 1516 1517 BTIF_TRACE_DEBUG("Remote device:%s", name.c_str()); 1518 int value; 1519 if (btif_in_fetch_bonded_device(name) == BT_STATUS_SUCCESS) { 1520 if (btif_config_get_int(name, "HidDeviceCabled", &value)) { 1521 RawAddress bd_addr; 1522 RawAddress::FromString(name, bd_addr); 1523 BTA_HdAddDevice(bd_addr); 1524 break; 1525 } 1526 } 1527 } 1528 1529 return BT_STATUS_SUCCESS; 1530 } 1531 1532 /******************************************************************************* 1533 * 1534 * Function btif_storage_set_hidd 1535 * 1536 * Description Stores currently used HIDD device info in nvram and remove 1537 * the "HidDeviceCabled" flag from unused devices 1538 * 1539 * Returns BT_STATUS_SUCCESS 1540 * 1541 ******************************************************************************/ 1542 bt_status_t btif_storage_set_hidd(RawAddress* remote_bd_addr) { 1543 std::string remote_device_address_string = remote_bd_addr->ToString(); 1544 for (const section_t& section : btif_config_sections()) { 1545 if (!RawAddress::IsValidAddress(section.name)) continue; 1546 if (section.name == remote_device_address_string) continue; 1547 if (btif_in_fetch_bonded_device(section.name) == BT_STATUS_SUCCESS) { 1548 btif_config_remove(section.name, "HidDeviceCabled"); 1549 } 1550 } 1551 1552 btif_config_set_int(remote_device_address_string, "HidDeviceCabled", 1); 1553 btif_config_save(); 1554 return BT_STATUS_SUCCESS; 1555 } 1556 1557 /******************************************************************************* 1558 * 1559 * Function btif_storage_remove_hidd 1560 * 1561 * Description Removes hidd bonded device info from nvram 1562 * 1563 * Returns BT_STATUS_SUCCESS 1564 * 1565 ******************************************************************************/ 1566 bt_status_t btif_storage_remove_hidd(RawAddress* remote_bd_addr) { 1567 btif_config_remove(remote_bd_addr->ToString(), "HidDeviceCabled"); 1568 btif_config_save(); 1569 1570 return BT_STATUS_SUCCESS; 1571 } 1572 1573 // Get the name of a device from btif for interop database matching. 1574 bool btif_storage_get_stored_remote_name(const RawAddress& bd_addr, 1575 char* name) { 1576 bt_property_t property; 1577 property.type = BT_PROPERTY_BDNAME; 1578 property.len = BTM_MAX_REM_BD_NAME_LEN; 1579 property.val = name; 1580 1581 return (btif_storage_get_remote_device_property(&bd_addr, &property) == 1582 BT_STATUS_SUCCESS); 1583 } 1584