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