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