1 /******************************************************************************
3 * Copyright (c) 2014 The Android Open Source Project
4 * Copyright (C) 2009-2012 Broadcom Corporation
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 ******************************************************************************/
20 /************************************************************************************
22 * Filename: btif_storage.c
24 * Description: Stores the local BT adapter and remote device properties in
25 * NVRAM storage, typically as xml file in the
31 #define LOG_TAG "bt_btif_storage"
33 #include "btif_storage.h"
43 #include "bta_hh_api.h"
44 #include "btcore/include/bdaddr.h"
46 #include "btif_config.h"
48 #include "btif_util.h"
49 #include "bt_common.h"
50 #include "osi/include/allocator.h"
51 #include "osi/include/compat.h"
52 #include "osi/include/config.h"
53 #include "osi/include/list.h"
54 #include "osi/include/log.h"
55 #include "osi/include/osi.h"
57 /************************************************************************************
59 ************************************************************************************/
61 // TODO(armansito): Find a better way than using a hardcoded path.
62 #define BTIF_STORAGE_PATH_BLUEDROID "/data/misc/bluedroid"
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"
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"
82 /* This is a local property to add a device found */
83 #define BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP 0xFF
85 #define BTIF_STORAGE_GET_ADAPTER_PROP(t,v,l,p) \
86 {p.type=t;p.val=v;p.len=l; btif_storage_get_adapter_property(&p);}
88 #define BTIF_STORAGE_GET_REMOTE_PROP(b,t,v,l,p) \
89 {p.type=t;p.val=v;p.len=l;btif_storage_get_remote_device_property(b,&p);}
91 #define STORAGE_BDADDR_STRING_SZ (18) /* 00:11:22:33:44:55 */
92 #define STORAGE_UUID_STRING_SIZE (36+1) /* 00001200-0000-1000-8000-00805f9b34fb; */
93 #define STORAGE_PINLEN_STRING_MAX_SIZE (2) /* ascii pinlen max chars */
94 #define STORAGE_KEYTYPE_STRING_MAX_SIZE (1) /* ascii keytype max chars */
96 #define STORAGE_KEY_TYPE_MAX (10)
98 #define STORAGE_HID_ATRR_MASK_SIZE (4)
99 #define STORAGE_HID_SUB_CLASS_SIZE (2)
100 #define STORAGE_HID_APP_ID_SIZE (2)
101 #define STORAGE_HID_VENDOR_ID_SIZE (4)
102 #define STORAGE_HID_PRODUCT_ID_SIZE (4)
103 #define STORAGE_HID_VERSION_SIZE (4)
104 #define STORAGE_HID_CTRY_CODE_SIZE (2)
105 #define STORAGE_HID_DESC_LEN_SIZE (4)
106 #define STORAGE_HID_DESC_MAX_SIZE (2*512)
108 /* <18 char bd addr> <space> LIST< <36 char uuid> <;> > <keytype (dec)> <pinlen> */
109 #define BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX (STORAGE_BDADDR_STRING_SZ + 1 +\
110 STORAGE_UUID_STRING_SIZE*BT_MAX_NUM_UUIDS + \
111 STORAGE_PINLEN_STRING_MAX_SIZE +\
112 STORAGE_KEYTYPE_STRING_MAX_SIZE)
114 #define STORAGE_REMOTE_LINKKEYS_ENTRY_SIZE (LINK_KEY_LEN*2 + 1 + 2 + 1 + 2)
116 /* <18 char bd addr> <space>LIST <attr_mask> <space> > <sub_class> <space> <app_id> <space>
117 <vendor_id> <space> > <product_id> <space> <version> <space>
118 <ctry_code> <space> > <desc_len> <space> <desc_list> <space> */
119 #define BTIF_HID_INFO_ENTRY_SIZE_MAX (STORAGE_BDADDR_STRING_SZ + 1 +\
120 STORAGE_HID_ATRR_MASK_SIZE + 1 +\
121 STORAGE_HID_SUB_CLASS_SIZE + 1 +\
122 STORAGE_HID_APP_ID_SIZE+ 1 +\
123 STORAGE_HID_VENDOR_ID_SIZE+ 1 +\
124 STORAGE_HID_PRODUCT_ID_SIZE+ 1 +\
125 STORAGE_HID_VERSION_SIZE+ 1 +\
126 STORAGE_HID_CTRY_CODE_SIZE+ 1 +\
127 STORAGE_HID_DESC_LEN_SIZE+ 1 +\
128 STORAGE_HID_DESC_MAX_SIZE+ 1 )
130 /* currently remote services is the potentially largest entry */
131 #define BTIF_STORAGE_MAX_LINE_SZ BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX
133 /* check against unv max entry size at compile time */
134 #if (BTIF_STORAGE_ENTRY_MAX_SIZE > UNV_MAXLINE_LENGTH)
135 #error "btif storage entry size exceeds unv max line size"
138 #define BTIF_STORAGE_HL_APP "hl_app"
139 #define BTIF_STORAGE_HL_APP_CB "hl_app_cb"
140 #define BTIF_STORAGE_HL_APP_DATA "hl_app_data_"
141 #define BTIF_STORAGE_HL_APP_MDL_DATA "hl_app_mdl_data_"
143 /************************************************************************************
144 ** Local type definitions
145 ************************************************************************************/
148 uint32_t num_devices;
149 bt_bdaddr_t devices[BTM_SEC_MAX_DEVICE_RECORDS];
150 } btif_bonded_devices_t;
152 /************************************************************************************
153 ** External variables
154 ************************************************************************************/
155 extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
156 extern bt_bdaddr_t btif_local_bd_addr;
158 /************************************************************************************
159 ** External functions
160 ************************************************************************************/
162 extern void btif_gatts_add_bonded_dev_from_nv(BD_ADDR bda);
164 /************************************************************************************
165 ** Internal Functions
166 ************************************************************************************/
168 static bt_status_t btif_in_fetch_bonded_ble_device(const char *remote_bd_addr,int add,
169 btif_bonded_devices_t *p_bonded_devices);
170 static bt_status_t btif_in_fetch_bonded_device(const char *bdstr);
172 /************************************************************************************
174 ************************************************************************************/
176 static int prop2cfg(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
180 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
181 BTIF_TRACE_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
183 if(prop->len <= 0 || prop->len > (int)sizeof(value) - 1)
185 BTIF_TRACE_ERROR("property type:%d, len:%d is invalid", prop->type, prop->len);
190 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
191 btif_config_set_int(bdstr,
192 BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int)time(NULL));
194 case BT_PROPERTY_BDNAME:
195 strncpy(value, (char*)prop->val, prop->len);
196 value[prop->len]='\0';
198 btif_config_set_str(bdstr,
199 BTIF_STORAGE_PATH_REMOTE_NAME, value);
200 else btif_config_set_str("Adapter",
201 BTIF_STORAGE_KEY_ADAPTER_NAME, value);
203 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
204 strncpy(value, (char*)prop->val, prop->len);
205 value[prop->len]='\0';
206 btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_ALIASE, value);
208 case BT_PROPERTY_ADAPTER_SCAN_MODE:
209 btif_config_set_int("Adapter",
210 BTIF_STORAGE_KEY_ADAPTER_SCANMODE, *(int*)prop->val);
212 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
213 btif_config_set_int("Adapter",
214 BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, *(int*)prop->val);
216 case BT_PROPERTY_CLASS_OF_DEVICE:
217 btif_config_set_int(bdstr,
218 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, *(int*)prop->val);
220 case BT_PROPERTY_TYPE_OF_DEVICE:
221 btif_config_set_int(bdstr,
222 BTIF_STORAGE_PATH_REMOTE_DEVTYPE, *(int*)prop->val);
224 case BT_PROPERTY_UUIDS:
229 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);
234 if (strlen(value) + strlen(buf) + 1 > (int) sizeof(value) - 1) {
235 android_errorWriteLog(0x534e4554, "73963551");
239 //strcat(value, ";");
242 btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value);
245 case BT_PROPERTY_REMOTE_VERSION_INFO:
247 bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
252 btif_config_set_int(bdstr,
253 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, info->manufacturer);
254 btif_config_set_int(bdstr,
255 BTIF_STORAGE_PATH_REMOTE_VER_VER, info->version);
256 btif_config_set_int(bdstr,
257 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, info->sub_ver);
261 BTIF_TRACE_ERROR("Unknown prop type:%d", prop->type);
265 /* save changes if the device was bonded */
266 if (btif_in_fetch_bonded_device(bdstr) == BT_STATUS_SUCCESS) {
273 static int cfg2prop(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
277 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
278 BTIF_TRACE_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
281 BTIF_TRACE_ERROR("property type:%d, len:%d is invalid", prop->type, prop->len);
287 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
288 if(prop->len >= (int)sizeof(int))
289 ret = btif_config_get_int(bdstr,
290 BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int*)prop->val);
292 case BT_PROPERTY_BDNAME:
296 ret = btif_config_get_str(bdstr,
297 BTIF_STORAGE_PATH_REMOTE_NAME, (char*)prop->val, &len);
298 else ret = btif_config_get_str("Adapter",
299 BTIF_STORAGE_KEY_ADAPTER_NAME, (char*)prop->val, &len);
300 if(ret && len && len <= prop->len)
309 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
312 ret = btif_config_get_str(bdstr,
313 BTIF_STORAGE_PATH_REMOTE_ALIASE, (char*)prop->val, &len);
314 if(ret && len && len <= prop->len)
323 case BT_PROPERTY_ADAPTER_SCAN_MODE:
324 if(prop->len >= (int)sizeof(int))
325 ret = btif_config_get_int("Adapter",
326 BTIF_STORAGE_KEY_ADAPTER_SCANMODE, (int*)prop->val);
328 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
329 if(prop->len >= (int)sizeof(int))
330 ret = btif_config_get_int("Adapter",
331 BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, (int*)prop->val);
333 case BT_PROPERTY_CLASS_OF_DEVICE:
334 if(prop->len >= (int)sizeof(int))
335 ret = btif_config_get_int(bdstr,
336 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, (int*)prop->val);
338 case BT_PROPERTY_TYPE_OF_DEVICE:
339 if(prop->len >= (int)sizeof(int))
340 ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTYPE, (int*)prop->val);
342 case BT_PROPERTY_UUIDS:
345 int size = sizeof(value);
346 if(btif_config_get_str(bdstr,
347 BTIF_STORAGE_PATH_REMOTE_SERVICE, value, &size))
349 bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val;
350 size_t num_uuids = btif_split_uuids_string(value, p_uuid, BT_MAX_NUM_UUIDS);
351 prop->len = num_uuids * sizeof(bt_uuid_t);
361 case BT_PROPERTY_REMOTE_VERSION_INFO:
363 bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
365 if(prop->len >= (int)sizeof(bt_remote_version_t))
367 ret = btif_config_get_int(bdstr,
368 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, &info->manufacturer);
371 ret = btif_config_get_int(bdstr,
372 BTIF_STORAGE_PATH_REMOTE_VER_VER, &info->version);
375 ret = btif_config_get_int(bdstr,
376 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, &info->sub_ver);
381 BTIF_TRACE_ERROR("Unknow prop type:%d", prop->type);
387 /*******************************************************************************
389 ** Function btif_in_fetch_bonded_devices
391 ** Description Internal helper function to fetch the bonded devices
394 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
396 *******************************************************************************/
397 static bt_status_t btif_in_fetch_bonded_device(const char *bdstr)
399 BOOLEAN bt_linkkey_file_found=FALSE;
402 size_t size = sizeof(link_key);
403 if(btif_config_get_bin(bdstr, "LinkKey", (uint8_t *)link_key, &size))
406 if(btif_config_get_int(bdstr, "LinkKeyType", &linkkey_type))
408 bt_linkkey_file_found = TRUE;
412 bt_linkkey_file_found = FALSE;
415 #if (BLE_INCLUDED == TRUE)
416 if((btif_in_fetch_bonded_ble_device(bdstr, FALSE, NULL) != BT_STATUS_SUCCESS)
417 && (!bt_linkkey_file_found))
419 BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", bdstr);
420 return BT_STATUS_FAIL;
423 if((!bt_linkkey_file_found))
425 BTIF_TRACE_DEBUG("Remote device:%s, no link key found", bdstr);
426 return BT_STATUS_FAIL;
429 return BT_STATUS_SUCCESS;
432 /*******************************************************************************
434 ** Function btif_in_fetch_bonded_devices
436 ** Description Internal helper function to fetch the bonded devices
439 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
441 *******************************************************************************/
442 static bt_status_t btif_in_fetch_bonded_devices(btif_bonded_devices_t *p_bonded_devices, int add)
444 memset(p_bonded_devices, 0, sizeof(btif_bonded_devices_t));
446 BOOLEAN bt_linkkey_file_found=FALSE;
449 for (const btif_config_section_iter_t *iter = btif_config_section_begin(); iter != btif_config_section_end(); iter = btif_config_section_next(iter)) {
450 const char *name = btif_config_section_name(iter);
451 if (!string_is_bdaddr(name))
454 BTIF_TRACE_DEBUG("Remote device:%s", name);
456 size_t size = sizeof(link_key);
457 if (btif_config_get_bin(name, "LinkKey", link_key, &size)) {
459 if (btif_config_get_int(name, "LinkKeyType", &linkkey_type)) {
461 string_to_bdaddr(name, &bd_addr);
463 DEV_CLASS dev_class = {0, 0, 0};
466 if (btif_config_get_int(name, "DevClass", &cod))
467 uint2devclass((UINT32)cod, dev_class);
468 btif_config_get_int(name, "PinLength", &pin_length);
469 BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0,
470 (UINT8)linkkey_type, 0, pin_length);
472 #if BLE_INCLUDED == TRUE
473 if (btif_config_get_int(name, "DevType", &device_type) &&
474 (device_type == BT_DEVICE_TYPE_DUMO) ) {
475 btif_gatts_add_bonded_dev_from_nv(bd_addr.address);
479 bt_linkkey_file_found = TRUE;
480 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
482 #if (BLE_INCLUDED == TRUE)
483 bt_linkkey_file_found = FALSE;
485 BTIF_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid", name);
489 #if (BLE_INCLUDED == TRUE)
490 if (!btif_in_fetch_bonded_ble_device(name, add, p_bonded_devices) &&
491 !bt_linkkey_file_found) {
492 BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", name);
495 if(!bt_linkkey_file_found)
496 BTIF_TRACE_DEBUG("Remote device:%s, no link key", name);
499 return BT_STATUS_SUCCESS;
502 static void btif_read_le_key(const uint8_t key_type, const size_t key_len, bt_bdaddr_t bd_addr,
503 const uint8_t addr_type, const bool add_key, bool *device_added, bool *key_found)
505 assert(device_added);
509 memset(buffer, 0, sizeof(buffer));
511 if (btif_storage_get_ble_bonding_key(&bd_addr, key_type, buffer, key_len) == BT_STATUS_SUCCESS)
516 bdcpy(bta_bd_addr, bd_addr.address);
520 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
521 *device_added = true;
524 char bd_str[20] = {0};
525 BTIF_TRACE_DEBUG("%s() Adding key type %d for %s", __func__,
526 key_type, bdaddr_to_string(&bd_addr, bd_str, sizeof(bd_str)));
527 BTA_DmAddBleKey(bta_bd_addr, (tBTA_LE_KEY_VALUE *)buffer, key_type);
534 /*******************************************************************************
537 * Functions are synchronous and can be called by both from internal modules
538 * such as BTIF_DM and by external entiries from HAL via BTIF_context_switch.
539 * For OUT parameters, the caller is expected to provide the memory.
540 * Caller is expected to provide a valid pointer to 'property->value' based on
541 * the property->type.
542 *******************************************************************************/
544 /*******************************************************************************
546 ** Function btif_split_uuids_string
548 ** Description Internal helper function to split the string of UUIDs
549 ** read from the NVRAM to an array
551 ** Returns Number of UUIDs parsed from the supplied string
553 *******************************************************************************/
554 size_t btif_split_uuids_string(const char *str, bt_uuid_t *p_uuid, size_t max_uuids)
559 size_t num_uuids = 0;
560 while (str && num_uuids < max_uuids)
562 bool rc = string_to_uuid(str, p_uuid++);
565 str = strchr(str, ' ');
572 /*******************************************************************************
574 ** Function btif_storage_get_adapter_property
576 ** Description BTIF storage API - Fetches the adapter property->type
577 ** from NVRAM and fills property->val.
578 ** Caller should provide memory for property->val and
579 ** set the property->val
581 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
582 ** BT_STATUS_FAIL otherwise
584 *******************************************************************************/
585 bt_status_t btif_storage_get_adapter_property(bt_property_t *property)
588 /* Special handling for adapter BD_ADDR and BONDED_DEVICES */
589 if (property->type == BT_PROPERTY_BDADDR)
591 bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)property->val;
592 /* This has been cached in btif. Just fetch it from there */
593 memcpy(bd_addr, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
594 property->len = sizeof(bt_bdaddr_t);
595 return BT_STATUS_SUCCESS;
597 else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES)
599 btif_bonded_devices_t bonded_devices;
601 btif_in_fetch_bonded_devices(&bonded_devices, 0);
603 BTIF_TRACE_DEBUG("%s: Number of bonded devices: %d Property:BT_PROPERTY_ADAPTER_BONDED_DEVICES", __FUNCTION__, bonded_devices.num_devices);
605 if (bonded_devices.num_devices > 0)
607 property->len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
608 memcpy(property->val, bonded_devices.devices, property->len);
611 /* if there are no bonded_devices, then length shall be 0 */
612 return BT_STATUS_SUCCESS;
614 else if (property->type == BT_PROPERTY_UUIDS)
616 /* publish list of local supported services */
617 bt_uuid_t *p_uuid = (bt_uuid_t*)property->val;
618 uint32_t num_uuids = 0;
621 tBTA_SERVICE_MASK service_mask = btif_get_enabled_services_mask();
622 LOG_INFO(LOG_TAG, "%s service_mask:0x%x", __FUNCTION__, service_mask);
623 for (i=0; i < BTA_MAX_SERVICE_ID; i++)
625 /* This should eventually become a function when more services are enabled */
627 &(tBTA_SERVICE_MASK)(1 << i))
631 case BTA_HFP_SERVICE_ID:
633 uuid16_to_uuid128(UUID_SERVCLASS_AG_HANDSFREE,
637 /* intentional fall through: Send both BFP & HSP UUIDs if HFP is enabled */
638 case BTA_HSP_SERVICE_ID:
640 uuid16_to_uuid128(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,
644 case BTA_A2DP_SOURCE_SERVICE_ID:
646 uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SOURCE,
650 case BTA_A2DP_SINK_SERVICE_ID:
652 uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SINK,
656 case BTA_HFP_HS_SERVICE_ID:
658 uuid16_to_uuid128(UUID_SERVCLASS_HF_HANDSFREE,
665 property->len = (num_uuids)*sizeof(bt_uuid_t);
666 return BT_STATUS_SUCCESS;
669 /* fall through for other properties */
670 if(!cfg2prop(NULL, property))
672 return btif_dm_get_adapter_property(property);
674 return BT_STATUS_SUCCESS;
677 /*******************************************************************************
679 ** Function btif_storage_set_adapter_property
681 ** Description BTIF storage API - Stores the adapter property
684 ** Returns BT_STATUS_SUCCESS if the store was successful,
685 ** BT_STATUS_FAIL otherwise
687 *******************************************************************************/
688 bt_status_t btif_storage_set_adapter_property(bt_property_t *property)
690 return prop2cfg(NULL, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
693 /*******************************************************************************
695 ** Function btif_storage_get_remote_device_property
697 ** Description BTIF storage API - Fetches the remote device property->type
698 ** from NVRAM and fills property->val.
699 ** Caller should provide memory for property->val and
700 ** set the property->val
702 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
703 ** BT_STATUS_FAIL otherwise
705 *******************************************************************************/
706 bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t *remote_bd_addr,
707 bt_property_t *property)
709 return cfg2prop(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
711 /*******************************************************************************
713 ** Function btif_storage_set_remote_device_property
715 ** Description BTIF storage API - Stores the remote device property
718 ** Returns BT_STATUS_SUCCESS if the store was successful,
719 ** BT_STATUS_FAIL otherwise
721 *******************************************************************************/
722 bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t *remote_bd_addr,
723 bt_property_t *property)
725 return prop2cfg(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
728 /*******************************************************************************
730 ** Function btif_storage_add_remote_device
732 ** Description BTIF storage API - Adds a newly discovered device to NVRAM
733 ** along with the timestamp. Also, stores the various
734 ** properties - RSSI, BDADDR, NAME (if found in EIR)
736 ** Returns BT_STATUS_SUCCESS if the store was successful,
737 ** BT_STATUS_FAIL otherwise
739 *******************************************************************************/
740 bt_status_t btif_storage_add_remote_device(bt_bdaddr_t *remote_bd_addr,
741 uint32_t num_properties,
742 bt_property_t *properties)
745 /* TODO: If writing a property, fails do we go back undo the earlier
746 * written properties? */
747 for (i=0; i < num_properties; i++)
749 /* Ignore the RSSI as this is not stored in DB */
750 if (properties[i].type == BT_PROPERTY_REMOTE_RSSI)
753 /* BD_ADDR for remote device needs special handling as we also store timestamp */
754 if (properties[i].type == BT_PROPERTY_BDADDR)
756 bt_property_t addr_prop;
757 memcpy(&addr_prop, &properties[i], sizeof(bt_property_t));
758 addr_prop.type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP;
759 btif_storage_set_remote_device_property(remote_bd_addr,
764 btif_storage_set_remote_device_property(remote_bd_addr,
768 return BT_STATUS_SUCCESS;
771 /*******************************************************************************
773 ** Function btif_storage_add_bonded_device
775 ** Description BTIF storage API - Adds the newly bonded device to NVRAM
776 ** along with the link-key, Key type and Pin key length
778 ** Returns BT_STATUS_SUCCESS if the store was successful,
779 ** BT_STATUS_FAIL otherwise
781 *******************************************************************************/
783 bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
789 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
790 int ret = btif_config_set_int(bdstr, "LinkKeyType", (int)key_type);
791 ret &= btif_config_set_int(bdstr, "PinLength", (int)pin_length);
792 ret &= btif_config_set_bin(bdstr, "LinkKey", link_key, sizeof(LINK_KEY));
794 if (is_restricted_mode()) {
795 BTIF_TRACE_WARNING("%s: '%s' pairing will be removed if unrestricted",
797 btif_config_set_int(bdstr, "Restricted", 1);
800 /* write bonded info immediately */
802 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
805 /*******************************************************************************
807 ** Function btif_storage_remove_bonded_device
809 ** Description BTIF storage API - Deletes the bonded device from NVRAM
811 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
812 ** BT_STATUS_FAIL otherwise
814 *******************************************************************************/
815 bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
818 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
819 BTIF_TRACE_DEBUG("in bd addr:%s", bdstr);
821 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
822 btif_storage_remove_ble_bonding_keys(remote_bd_addr);
826 if(btif_config_exist(bdstr, "LinkKeyType"))
827 ret &= btif_config_remove(bdstr, "LinkKeyType");
828 if(btif_config_exist(bdstr, "PinLength"))
829 ret &= btif_config_remove(bdstr, "PinLength");
830 if(btif_config_exist(bdstr, "LinkKey"))
831 ret &= btif_config_remove(bdstr, "LinkKey");
832 /* write bonded info immediately */
834 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
838 /* Some devices hardcode sample LTK value from spec, instead of generating one.
839 * Treat such devices as insecure, and remove such bonds when bluetooth restarts.
840 * Removing them after disconnection is handled separately.
842 * We still allow such devices to bond in order to give the user a chance to update
845 static void remove_devices_with_sample_ltk() {
846 list_t *bad_ltk = list_new(osi_free);
848 for (const btif_config_section_iter_t *iter = btif_config_section_begin(); iter != btif_config_section_end(); iter = btif_config_section_next(iter)) {
849 const char *name = btif_config_section_name(iter);
850 if (!string_is_bdaddr(name)) {
854 bt_bdaddr_t *bda = osi_malloc(sizeof(bt_bdaddr_t));
855 string_to_bdaddr(name, bda);
857 tBTA_LE_KEY_VALUE key;
858 memset(&key, 0, sizeof(key));
860 if (btif_storage_get_ble_bonding_key(bda, BTIF_DM_LE_KEY_PENC, (char*)&key, sizeof(tBTM_LE_PENC_KEYS)) ==
862 if (is_sample_ltk(key.penc_key.ltk)) {
863 list_append(bad_ltk, (void*)bda);
868 for (list_node_t *sn = list_begin(bad_ltk); sn != list_end(bad_ltk); sn = list_next(sn)) {
869 android_errorWriteLog(0x534e4554, "128437297");
870 BTIF_TRACE_ERROR("%s: removing bond to device using test TLK", __func__);
872 bt_bdaddr_t *bda = (bt_bdaddr_t*)list_node(sn);
873 btif_storage_remove_bonded_device(bda);
879 /*******************************************************************************
881 ** Function btif_storage_load_bonded_devices
883 ** Description BTIF storage API - Loads all the bonded devices from NVRAM
884 ** and adds to the BTA.
885 ** Additionally, this API also invokes the adaper_properties_cb
886 ** and remote_device_properties_cb for each of the bonded devices.
888 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
890 *******************************************************************************/
891 bt_status_t btif_storage_load_bonded_devices(void)
893 btif_bonded_devices_t bonded_devices;
895 bt_property_t adapter_props[6];
896 uint32_t num_props = 0;
897 bt_property_t remote_properties[8];
899 bt_bdname_t name, alias;
901 uint32_t disc_timeout;
902 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
903 bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
905 remove_devices_with_sample_ltk();
907 btif_in_fetch_bonded_devices(&bonded_devices, 1);
909 /* Now send the adapter_properties_cb with all adapter_properties */
911 memset(adapter_props, 0, sizeof(adapter_props));
914 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDADDR, &addr, sizeof(addr),
915 adapter_props[num_props]);
919 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDNAME, &name, sizeof(name),
920 adapter_props[num_props]);
924 /* TODO: At the time of BT on, always report the scan mode as 0 irrespective
925 of the scan_mode during the previous enable cycle.
926 This needs to be re-visited as part of the app/stack enable sequence
928 mode = BT_SCAN_MODE_NONE;
929 adapter_props[num_props].type = BT_PROPERTY_ADAPTER_SCAN_MODE;
930 adapter_props[num_props].len = sizeof(mode);
931 adapter_props[num_props].val = &mode;
935 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
936 &disc_timeout, sizeof(disc_timeout),
937 adapter_props[num_props]);
941 bt_bdaddr_t *devices_list =
942 (bt_bdaddr_t *)osi_malloc(sizeof(bt_bdaddr_t) * bonded_devices.num_devices);
943 adapter_props[num_props].type = BT_PROPERTY_ADAPTER_BONDED_DEVICES;
944 adapter_props[num_props].len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
945 adapter_props[num_props].val = devices_list;
946 for (i=0; i < bonded_devices.num_devices; i++)
948 memcpy(devices_list + i, &bonded_devices.devices[i], sizeof(bt_bdaddr_t));
953 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_UUIDS,
954 local_uuids, sizeof(local_uuids),
955 adapter_props[num_props]);
958 btif_adapter_properties_evt(BT_STATUS_SUCCESS, num_props, adapter_props);
960 osi_free(devices_list);
963 BTIF_TRACE_EVENT("%s: %d bonded devices found", __FUNCTION__, bonded_devices.num_devices);
966 for (i = 0; i < bonded_devices.num_devices; i++)
968 bt_bdaddr_t *p_remote_addr;
971 * TODO: improve handling of missing fields in NVRAM.
974 uint32_t devtype = 0;
977 p_remote_addr = &bonded_devices.devices[i];
978 memset(remote_properties, 0, sizeof(remote_properties));
979 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_BDNAME,
981 remote_properties[num_props]);
984 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_REMOTE_FRIENDLY_NAME,
985 &alias, sizeof(alias),
986 remote_properties[num_props]);
989 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_CLASS_OF_DEVICE,
991 remote_properties[num_props]);
994 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_TYPE_OF_DEVICE,
995 &devtype, sizeof(devtype),
996 remote_properties[num_props]);
999 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_UUIDS,
1000 remote_uuids, sizeof(remote_uuids),
1001 remote_properties[num_props]);
1004 btif_remote_properties_evt(BT_STATUS_SUCCESS, p_remote_addr,
1005 num_props, remote_properties);
1008 return BT_STATUS_SUCCESS;
1011 #if (BLE_INCLUDED == TRUE)
1013 /*******************************************************************************
1015 ** Function btif_storage_add_ble_bonding_key
1017 ** Description BTIF storage API - Adds the newly bonded device to NVRAM
1018 ** along with the ble-key, Key type and Pin key length
1020 ** Returns BT_STATUS_SUCCESS if the store was successful,
1021 ** BT_STATUS_FAIL otherwise
1023 *******************************************************************************/
1025 bt_status_t btif_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
1031 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1035 case BTIF_DM_LE_KEY_PENC:
1036 name = "LE_KEY_PENC";
1038 case BTIF_DM_LE_KEY_PID:
1039 name = "LE_KEY_PID";
1041 case BTIF_DM_LE_KEY_PCSRK:
1042 name = "LE_KEY_PCSRK";
1044 case BTIF_DM_LE_KEY_LENC:
1045 name = "LE_KEY_LENC";
1047 case BTIF_DM_LE_KEY_LCSRK:
1048 name = "LE_KEY_LCSRK";
1050 case BTIF_DM_LE_KEY_LID:
1051 name = "LE_KEY_LID";
1054 return BT_STATUS_FAIL;
1056 int ret = btif_config_set_bin(bdstr, name, (const uint8_t *)key, key_length);
1058 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1061 /*******************************************************************************
1063 ** Function btif_storage_get_ble_bonding_key
1067 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
1068 ** BT_STATUS_FAIL otherwise
1070 *******************************************************************************/
1071 bt_status_t btif_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
1077 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1081 case BTIF_DM_LE_KEY_PENC:
1082 name = "LE_KEY_PENC";
1084 case BTIF_DM_LE_KEY_PID:
1085 name = "LE_KEY_PID";
1087 case BTIF_DM_LE_KEY_PCSRK:
1088 name = "LE_KEY_PCSRK";
1090 case BTIF_DM_LE_KEY_LENC:
1091 name = "LE_KEY_LENC";
1093 case BTIF_DM_LE_KEY_LCSRK:
1094 name = "LE_KEY_LCSRK";
1096 case BTIF_DM_LE_KEY_LID:
1097 name = "LE_KEY_LID";
1099 return BT_STATUS_FAIL;
1101 size_t length = key_length;
1102 int ret = btif_config_get_bin(bdstr, name, (uint8_t *)key_value, &length);
1103 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1107 /*******************************************************************************
1109 ** Function btif_storage_remove_ble_keys
1111 ** Description BTIF storage API - Deletes the bonded device from NVRAM
1113 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1114 ** BT_STATUS_FAIL otherwise
1116 *******************************************************************************/
1117 bt_status_t btif_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr)
1120 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1121 BTIF_TRACE_DEBUG(" %s in bd addr:%s",__FUNCTION__, bdstr);
1123 if(btif_config_exist(bdstr, "LE_KEY_PENC"))
1124 ret &= btif_config_remove(bdstr, "LE_KEY_PENC");
1125 if(btif_config_exist(bdstr, "LE_KEY_PID"))
1126 ret &= btif_config_remove(bdstr, "LE_KEY_PID");
1127 if(btif_config_exist(bdstr, "LE_KEY_PCSRK"))
1128 ret &= btif_config_remove(bdstr, "LE_KEY_PCSRK");
1129 if(btif_config_exist(bdstr, "LE_KEY_LENC"))
1130 ret &= btif_config_remove(bdstr, "LE_KEY_LENC");
1131 if(btif_config_exist(bdstr, "LE_KEY_LCSRK"))
1132 ret &= btif_config_remove(bdstr, "LE_KEY_LCSRK");
1134 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1137 /*******************************************************************************
1139 ** Function btif_storage_add_ble_local_key
1141 ** Description BTIF storage API - Adds the ble key to NVRAM
1143 ** Returns BT_STATUS_SUCCESS if the store was successful,
1144 ** BT_STATUS_FAIL otherwise
1146 *******************************************************************************/
1147 bt_status_t btif_storage_add_ble_local_key(char *key,
1154 case BTIF_DM_LE_LOCAL_KEY_IR:
1155 name = "LE_LOCAL_KEY_IR";
1157 case BTIF_DM_LE_LOCAL_KEY_IRK:
1158 name = "LE_LOCAL_KEY_IRK";
1160 case BTIF_DM_LE_LOCAL_KEY_DHK:
1161 name = "LE_LOCAL_KEY_DHK";
1163 case BTIF_DM_LE_LOCAL_KEY_ER:
1164 name = "LE_LOCAL_KEY_ER";
1167 return BT_STATUS_FAIL;
1169 int ret = btif_config_set_bin("Adapter", name, (const uint8_t *)key, key_length);
1171 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1174 /*******************************************************************************
1176 ** Function btif_storage_get_ble_local_key
1180 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
1181 ** BT_STATUS_FAIL otherwise
1183 *******************************************************************************/
1184 bt_status_t btif_storage_get_ble_local_key(UINT8 key_type,
1191 case BTIF_DM_LE_LOCAL_KEY_IR:
1192 name = "LE_LOCAL_KEY_IR";
1194 case BTIF_DM_LE_LOCAL_KEY_IRK:
1195 name = "LE_LOCAL_KEY_IRK";
1197 case BTIF_DM_LE_LOCAL_KEY_DHK:
1198 name = "LE_LOCAL_KEY_DHK";
1200 case BTIF_DM_LE_LOCAL_KEY_ER:
1201 name = "LE_LOCAL_KEY_ER";
1204 return BT_STATUS_FAIL;
1206 size_t length = key_length;
1207 int ret = btif_config_get_bin("Adapter", name, (uint8_t *)key_value, &length);
1208 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1211 /*******************************************************************************
1213 ** Function btif_storage_remove_ble_local_keys
1215 ** Description BTIF storage API - Deletes the bonded device from NVRAM
1217 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1218 ** BT_STATUS_FAIL otherwise
1220 *******************************************************************************/
1221 bt_status_t btif_storage_remove_ble_local_keys(void)
1224 if(btif_config_exist("Adapter", "LE_LOCAL_KEY_IR"))
1225 ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_IR");
1226 if(btif_config_exist("Adapter", "LE_LOCAL_KEY_IRK"))
1227 ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_IRK");
1228 if(btif_config_exist("Adapter", "LE_LOCAL_KEY_DHK"))
1229 ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_DHK");
1230 if(btif_config_exist("Adapter", "LE_LOCAL_KEY_ER"))
1231 ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_ER");
1233 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1236 static bt_status_t btif_in_fetch_bonded_ble_device(const char *remote_bd_addr, int add, btif_bonded_devices_t *p_bonded_devices)
1240 bt_bdaddr_t bd_addr;
1241 BD_ADDR bta_bd_addr;
1242 bool device_added = false;
1243 bool key_found = false;
1245 if (!btif_config_get_int(remote_bd_addr, "DevType", &device_type))
1246 return BT_STATUS_FAIL;
1248 if ((device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE)
1250 BTIF_TRACE_DEBUG("%s Found a LE device: %s", __func__, remote_bd_addr);
1252 string_to_bdaddr(remote_bd_addr, &bd_addr);
1253 bdcpy(bta_bd_addr, bd_addr.address);
1255 if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS)
1257 addr_type = BLE_ADDR_PUBLIC;
1258 btif_storage_set_remote_addr_type(&bd_addr, BLE_ADDR_PUBLIC);
1261 btif_read_le_key(BTIF_DM_LE_KEY_PENC, sizeof(tBTM_LE_PENC_KEYS),
1262 bd_addr, addr_type, add, &device_added, &key_found);
1264 btif_read_le_key(BTIF_DM_LE_KEY_PID, sizeof(tBTM_LE_PID_KEYS),
1265 bd_addr, addr_type, add, &device_added, &key_found);
1267 btif_read_le_key(BTIF_DM_LE_KEY_LID, sizeof(tBTM_LE_PID_KEYS),
1268 bd_addr, addr_type, add, &device_added, &key_found);
1270 btif_read_le_key(BTIF_DM_LE_KEY_PCSRK, sizeof(tBTM_LE_PCSRK_KEYS),
1271 bd_addr, addr_type, add, &device_added, &key_found);
1273 btif_read_le_key(BTIF_DM_LE_KEY_LENC, sizeof(tBTM_LE_LENC_KEYS),
1274 bd_addr, addr_type, add, &device_added, &key_found);
1276 btif_read_le_key(BTIF_DM_LE_KEY_LCSRK, sizeof(tBTM_LE_LCSRK_KEYS),
1277 bd_addr, addr_type, add, &device_added, &key_found);
1279 // Fill in the bonded devices
1282 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
1283 btif_gatts_add_bonded_dev_from_nv(bta_bd_addr);
1287 return BT_STATUS_SUCCESS;
1289 return BT_STATUS_FAIL;
1292 bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1296 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1297 int ret = btif_config_set_int(bdstr, "AddrType", (int)addr_type);
1298 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1301 /*******************************************************************************
1303 ** Function btif_storage_get_remote_addr_type
1305 ** Description BTIF storage API - Fetches the remote addr type
1307 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
1308 ** BT_STATUS_FAIL otherwise
1310 *******************************************************************************/
1311 bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1315 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1316 int ret = btif_config_get_int(bdstr, "AddrType", addr_type);
1317 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1320 /*******************************************************************************
1322 ** Function btif_storage_add_hid_device_info
1324 ** Description BTIF storage API - Adds the hid information of bonded hid devices-to NVRAM
1326 ** Returns BT_STATUS_SUCCESS if the store was successful,
1327 ** BT_STATUS_FAIL otherwise
1329 *******************************************************************************/
1331 bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr,
1332 UINT16 attr_mask, UINT8 sub_class,
1333 UINT8 app_id, UINT16 vendor_id,
1334 UINT16 product_id, UINT16 version,
1335 UINT8 ctry_code, UINT16 ssr_max_latency,
1336 UINT16 ssr_min_tout, UINT16 dl_len, UINT8 *dsc_list)
1339 BTIF_TRACE_DEBUG("btif_storage_add_hid_device_info:");
1340 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1341 btif_config_set_int(bdstr, "HidAttrMask", attr_mask);
1342 btif_config_set_int(bdstr, "HidSubClass", sub_class);
1343 btif_config_set_int(bdstr, "HidAppId", app_id);
1344 btif_config_set_int(bdstr, "HidVendorId", vendor_id);
1345 btif_config_set_int(bdstr, "HidProductId", product_id);
1346 btif_config_set_int(bdstr, "HidVersion", version);
1347 btif_config_set_int(bdstr, "HidCountryCode", ctry_code);
1348 btif_config_set_int(bdstr, "HidSSRMaxLatency", ssr_max_latency);
1349 btif_config_set_int(bdstr, "HidSSRMinTimeout", ssr_min_tout);
1351 btif_config_set_bin(bdstr, "HidDescriptor", dsc_list, dl_len);
1353 return BT_STATUS_SUCCESS;
1356 /*******************************************************************************
1358 ** Function btif_storage_load_bonded_hid_info
1360 ** Description BTIF storage API - Loads hid info for all the bonded devices from NVRAM
1361 ** and adds those devices to the BTA_HH.
1363 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
1365 *******************************************************************************/
1366 bt_status_t btif_storage_load_bonded_hid_info(void)
1368 bt_bdaddr_t bd_addr;
1369 tBTA_HH_DEV_DSCP_INFO dscp_info;
1374 memset(&dscp_info, 0, sizeof(dscp_info));
1375 for (const btif_config_section_iter_t *iter = btif_config_section_begin(); iter != btif_config_section_end(); iter = btif_config_section_next(iter)) {
1376 const char *name = btif_config_section_name(iter);
1377 if (!string_is_bdaddr(name))
1380 BTIF_TRACE_DEBUG("Remote device:%s", name);
1382 if(btif_in_fetch_bonded_device(name) == BT_STATUS_SUCCESS)
1384 if(btif_config_get_int(name, "HidAttrMask", &value))
1386 attr_mask = (uint16_t)value;
1388 btif_config_get_int(name, "HidSubClass", &value);
1389 sub_class = (uint8_t)value;
1391 btif_config_get_int(name, "HidAppId", &value);
1392 app_id = (uint8_t)value;
1394 btif_config_get_int(name, "HidVendorId", &value);
1395 dscp_info.vendor_id = (uint16_t) value;
1397 btif_config_get_int(name, "HidProductId", &value);
1398 dscp_info.product_id = (uint16_t) value;
1400 btif_config_get_int(name, "HidVersion", &value);
1401 dscp_info.version = (uint8_t) value;
1403 btif_config_get_int(name, "HidCountryCode", &value);
1404 dscp_info.ctry_code = (uint8_t) value;
1407 btif_config_get_int(name, "HidSSRMaxLatency", &value);
1408 dscp_info.ssr_max_latency = (uint16_t) value;
1411 btif_config_get_int(name, "HidSSRMinTimeout", &value);
1412 dscp_info.ssr_min_tout = (uint16_t) value;
1414 size_t len = btif_config_get_bin_length(name, "HidDescriptor");
1417 dscp_info.descriptor.dl_len = (uint16_t)len;
1418 dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len);
1419 btif_config_get_bin(name, "HidDescriptor", (uint8_t *)dscp_info.descriptor.dsc_list, &len);
1421 string_to_bdaddr(name, &bd_addr);
1422 // add extracted information to BTA HH
1423 if (btif_hh_add_added_dev(bd_addr,attr_mask))
1425 BTA_HhAddDev(bd_addr.address, attr_mask, sub_class,
1432 if(btif_config_get_int(name, "HidAttrMask", &value))
1434 btif_storage_remove_hid_info(&bd_addr);
1435 string_to_bdaddr(name, &bd_addr);
1440 return BT_STATUS_SUCCESS;
1443 /*******************************************************************************
1445 ** Function btif_storage_remove_hid_info
1447 ** Description BTIF storage API - Deletes the bonded hid device info from NVRAM
1449 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1450 ** BT_STATUS_FAIL otherwise
1452 *******************************************************************************/
1453 bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr)
1456 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1458 btif_config_remove(bdstr, "HidAttrMask");
1459 btif_config_remove(bdstr, "HidSubClass");
1460 btif_config_remove(bdstr, "HidAppId");
1461 btif_config_remove(bdstr, "HidVendorId");
1462 btif_config_remove(bdstr, "HidProductId");
1463 btif_config_remove(bdstr, "HidVersion");
1464 btif_config_remove(bdstr, "HidCountryCode");
1465 btif_config_remove(bdstr, "HidSSRMaxLatency");
1466 btif_config_remove(bdstr, "HidSSRMinTimeout");
1467 btif_config_remove(bdstr, "HidDescriptor");
1469 return BT_STATUS_SUCCESS;
1472 /*******************************************************************************
1474 ** Function btif_storage_get_num_bonded_devices
1476 ** Description BTIF storage API - Gets the number of bonded devices
1478 ** Returns the number of bonded devices
1480 *******************************************************************************/
1481 int btif_storage_get_num_bonded_devices(void) {
1482 btif_bonded_devices_t bonded_devices;
1483 btif_in_fetch_bonded_devices(&bonded_devices, 0);
1484 return bonded_devices.num_devices;
1487 /*******************************************************************************
1489 ** Function btif_storage_read_hl_apps_cb
1491 ** Description BTIF storage API - Read HL application control block from NVRAM
1493 ** Returns BT_STATUS_SUCCESS if the operation was successful,
1494 ** BT_STATUS_FAIL otherwise
1496 *******************************************************************************/
1497 bt_status_t btif_storage_read_hl_apps_cb(char *value, int value_size)
1499 bt_status_t bt_status = BT_STATUS_SUCCESS;
1501 if (!btif_config_exist(BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB)) {
1502 memset(value, 0, value_size);
1503 if (!btif_config_set_bin(BTIF_STORAGE_HL_APP,BTIF_STORAGE_HL_APP_CB,
1504 (const uint8_t *)value, value_size)) {
1505 bt_status = BT_STATUS_FAIL;
1510 size_t read_size = value_size;
1511 if (!btif_config_get_bin(BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB,
1512 (uint8_t *)value, &read_size)) {
1513 bt_status = BT_STATUS_FAIL;
1515 if (read_size != (size_t)value_size) {
1516 BTIF_TRACE_ERROR("%s value_size=%d read_size=%d",
1517 __FUNCTION__, value_size, read_size);
1518 bt_status = BT_STATUS_FAIL;
1523 BTIF_TRACE_DEBUG("%s status=%d value_size=%d", __FUNCTION__, bt_status, value_size);
1527 /*******************************************************************************
1529 ** Function btif_storage_is_restricted_device
1531 ** Description BTIF storage API - checks if this device is a restricted device
1533 ** Returns TRUE if the device is labeled as restricted
1536 *******************************************************************************/
1537 BOOLEAN btif_storage_is_restricted_device(const bt_bdaddr_t *remote_bd_addr)
1540 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1542 return btif_config_exist(bdstr, "Restricted");