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
38 #include <hardware/bluetooth.h>
39 #include "btif_config.h"
40 #define LOG_TAG "bt_btif_storage"
43 #include "btif_storage.h"
44 #include "btif_util.h"
45 #include "btcore/include/bdaddr.h"
46 #include "osi/include/allocator.h"
47 #include "osi/include/compat.h"
48 #include "osi/include/config.h"
50 #include "osi/include/osi.h"
51 #include "bta_hh_api.h"
53 #include "osi/include/log.h"
55 /************************************************************************************
57 ************************************************************************************/
59 #define BTIF_STORAGE_PATH_BLUEDROID "/data/misc/bluedroid"
61 //#define BTIF_STORAGE_PATH_ADAPTER_INFO "adapter_info"
62 //#define BTIF_STORAGE_PATH_REMOTE_DEVICES "remote_devices"
63 #define BTIF_STORAGE_PATH_REMOTE_DEVTIME "Timestamp"
64 #define BTIF_STORAGE_PATH_REMOTE_DEVCLASS "DevClass"
65 #define BTIF_STORAGE_PATH_REMOTE_DEVTYPE "DevType"
66 #define BTIF_STORAGE_PATH_REMOTE_NAME "Name"
67 #define BTIF_STORAGE_PATH_REMOTE_VER_MFCT "Manufacturer"
68 #define BTIF_STORAGE_PATH_REMOTE_VER_VER "LmpVer"
69 #define BTIF_STORAGE_PATH_REMOTE_VER_SUBVER "LmpSubVer"
71 //#define BTIF_STORAGE_PATH_REMOTE_LINKKEYS "remote_linkkeys"
72 #define BTIF_STORAGE_PATH_REMOTE_ALIASE "Aliase"
73 #define BTIF_STORAGE_PATH_REMOTE_SERVICE "Service"
74 #define BTIF_STORAGE_PATH_REMOTE_HIDINFO "HidInfo"
75 #define BTIF_STORAGE_KEY_ADAPTER_NAME "Name"
76 #define BTIF_STORAGE_KEY_ADAPTER_SCANMODE "ScanMode"
77 #define BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT "DiscoveryTimeout"
80 #define BTIF_AUTO_PAIR_CONF_FILE "/etc/bluetooth/auto_pair_devlist.conf"
81 #define BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST "AutoPairBlacklist"
82 #define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR "AddressBlacklist"
83 #define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME "ExactNameBlacklist"
84 #define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME "PartialNameBlacklist"
85 #define BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST "FixedPinZerosKeyboardBlacklist"
86 #define BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR "DynamicAddressBlacklist"
88 #define BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR ","
91 /* This is a local property to add a device found */
92 #define BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP 0xFF
94 #define BTIF_STORAGE_GET_ADAPTER_PROP(t,v,l,p) \
95 {p.type=t;p.val=v;p.len=l; btif_storage_get_adapter_property(&p);}
97 #define BTIF_STORAGE_GET_REMOTE_PROP(b,t,v,l,p) \
98 {p.type=t;p.val=v;p.len=l;btif_storage_get_remote_device_property(b,&p);}
100 #define STORAGE_BDADDR_STRING_SZ (18) /* 00:11:22:33:44:55 */
101 #define STORAGE_UUID_STRING_SIZE (36+1) /* 00001200-0000-1000-8000-00805f9b34fb; */
102 #define STORAGE_PINLEN_STRING_MAX_SIZE (2) /* ascii pinlen max chars */
103 #define STORAGE_KEYTYPE_STRING_MAX_SIZE (1) /* ascii keytype max chars */
105 #define STORAGE_KEY_TYPE_MAX (10)
107 #define STORAGE_HID_ATRR_MASK_SIZE (4)
108 #define STORAGE_HID_SUB_CLASS_SIZE (2)
109 #define STORAGE_HID_APP_ID_SIZE (2)
110 #define STORAGE_HID_VENDOR_ID_SIZE (4)
111 #define STORAGE_HID_PRODUCT_ID_SIZE (4)
112 #define STORAGE_HID_VERSION_SIZE (4)
113 #define STORAGE_HID_CTRY_CODE_SIZE (2)
114 #define STORAGE_HID_DESC_LEN_SIZE (4)
115 #define STORAGE_HID_DESC_MAX_SIZE (2*512)
117 /* <18 char bd addr> <space> LIST< <36 char uuid> <;> > <keytype (dec)> <pinlen> */
118 #define BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX (STORAGE_BDADDR_STRING_SZ + 1 +\
119 STORAGE_UUID_STRING_SIZE*BT_MAX_NUM_UUIDS + \
120 STORAGE_PINLEN_STRING_MAX_SIZE +\
121 STORAGE_KEYTYPE_STRING_MAX_SIZE)
123 #define STORAGE_REMOTE_LINKKEYS_ENTRY_SIZE (LINK_KEY_LEN*2 + 1 + 2 + 1 + 2)
125 /* <18 char bd addr> <space>LIST <attr_mask> <space> > <sub_class> <space> <app_id> <space>
126 <vendor_id> <space> > <product_id> <space> <version> <space>
127 <ctry_code> <space> > <desc_len> <space> <desc_list> <space> */
128 #define BTIF_HID_INFO_ENTRY_SIZE_MAX (STORAGE_BDADDR_STRING_SZ + 1 +\
129 STORAGE_HID_ATRR_MASK_SIZE + 1 +\
130 STORAGE_HID_SUB_CLASS_SIZE + 1 +\
131 STORAGE_HID_APP_ID_SIZE+ 1 +\
132 STORAGE_HID_VENDOR_ID_SIZE+ 1 +\
133 STORAGE_HID_PRODUCT_ID_SIZE+ 1 +\
134 STORAGE_HID_VERSION_SIZE+ 1 +\
135 STORAGE_HID_CTRY_CODE_SIZE+ 1 +\
136 STORAGE_HID_DESC_LEN_SIZE+ 1 +\
137 STORAGE_HID_DESC_MAX_SIZE+ 1 )
140 /* currently remote services is the potentially largest entry */
141 #define BTIF_STORAGE_MAX_LINE_SZ BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX
144 /* check against unv max entry size at compile time */
145 #if (BTIF_STORAGE_ENTRY_MAX_SIZE > UNV_MAXLINE_LENGTH)
146 #error "btif storage entry size exceeds unv max line size"
150 #define BTIF_STORAGE_HL_APP "hl_app"
151 #define BTIF_STORAGE_HL_APP_CB "hl_app_cb"
152 #define BTIF_STORAGE_HL_APP_DATA "hl_app_data_"
153 #define BTIF_STORAGE_HL_APP_MDL_DATA "hl_app_mdl_data_"
155 /************************************************************************************
156 ** Local type definitions
157 ************************************************************************************/
160 uint32_t num_devices;
161 bt_bdaddr_t devices[BTM_SEC_MAX_DEVICE_RECORDS];
162 } btif_bonded_devices_t;
164 /************************************************************************************
165 ** External variables
166 ************************************************************************************/
167 extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
168 extern bt_bdaddr_t btif_local_bd_addr;
170 /************************************************************************************
171 ** External functions
172 ************************************************************************************/
174 extern void btif_gatts_add_bonded_dev_from_nv(BD_ADDR bda);
176 /************************************************************************************
177 ** Internal Functions
178 ************************************************************************************/
180 bt_status_t btif_in_fetch_bonded_ble_device(const char *remote_bd_addr,int add,
181 btif_bonded_devices_t *p_bonded_devices);
182 bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
185 /************************************************************************************
187 ************************************************************************************/
189 /*******************************************************************************
191 ** Function btif_in_split_uuids_string_to_list
193 ** Description Internal helper function to split the string of UUIDs
194 ** read from the NVRAM to an array
198 *******************************************************************************/
199 static void btif_in_split_uuids_string_to_list(char *str, bt_uuid_t *p_uuid,
200 uint32_t *p_num_uuid)
208 //p_needle = strchr(p_start, ';');
209 p_needle = strchr(p_start, ' ');
210 if (p_needle < p_start) break;
211 memset(buf, 0, sizeof(buf));
212 strncpy(buf, p_start, (p_needle-p_start));
213 string_to_uuid(buf, p_uuid + num);
215 p_start = ++p_needle;
217 } while (*p_start != 0);
221 static int prop2cfg(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
225 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
226 BTIF_TRACE_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
228 if(prop->len <= 0 || prop->len > (int)sizeof(value) - 1)
230 BTIF_TRACE_ERROR("property type:%d, len:%d is invalid", prop->type, prop->len);
235 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
236 btif_config_set_int(bdstr,
237 BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int)time(NULL));
239 case BT_PROPERTY_BDNAME:
240 strncpy(value, (char*)prop->val, prop->len);
241 value[prop->len]='\0';
243 btif_config_set_str(bdstr,
244 BTIF_STORAGE_PATH_REMOTE_NAME, value);
245 else btif_config_set_str("Adapter",
246 BTIF_STORAGE_KEY_ADAPTER_NAME, value);
247 /* save name immediately */
250 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
251 strncpy(value, (char*)prop->val, prop->len);
252 value[prop->len]='\0';
253 btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_ALIASE, value);
254 /* save friendly name immediately */
257 case BT_PROPERTY_ADAPTER_SCAN_MODE:
258 btif_config_set_int("Adapter",
259 BTIF_STORAGE_KEY_ADAPTER_SCANMODE, *(int*)prop->val);
261 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
262 btif_config_set_int("Adapter",
263 BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, *(int*)prop->val);
265 case BT_PROPERTY_CLASS_OF_DEVICE:
266 btif_config_set_int(bdstr,
267 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, *(int*)prop->val);
269 case BT_PROPERTY_TYPE_OF_DEVICE:
270 btif_config_set_int(bdstr,
271 BTIF_STORAGE_PATH_REMOTE_DEVTYPE, *(int*)prop->val);
273 case BT_PROPERTY_UUIDS:
278 for (i=0; i < (prop->len)/sizeof(bt_uuid_t); i++)
280 bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val + i;
281 memset(buf, 0, sizeof(buf));
282 uuid_to_string_legacy(p_uuid, buf);
284 //strcat(value, ";");
287 btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value);
291 case BT_PROPERTY_REMOTE_VERSION_INFO:
293 bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
298 btif_config_set_int(bdstr,
299 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, info->manufacturer);
300 btif_config_set_int(bdstr,
301 BTIF_STORAGE_PATH_REMOTE_VER_VER, info->version);
302 btif_config_set_int(bdstr,
303 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, info->sub_ver);
308 BTIF_TRACE_ERROR("Unknow prop type:%d", prop->type);
314 static int cfg2prop(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
318 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
319 BTIF_TRACE_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
322 BTIF_TRACE_ERROR("property type:%d, len:%d is invalid", prop->type, prop->len);
328 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
329 if(prop->len >= (int)sizeof(int))
330 ret = btif_config_get_int(bdstr,
331 BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int*)prop->val);
333 case BT_PROPERTY_BDNAME:
337 ret = btif_config_get_str(bdstr,
338 BTIF_STORAGE_PATH_REMOTE_NAME, (char*)prop->val, &len);
339 else ret = btif_config_get_str("Adapter",
340 BTIF_STORAGE_KEY_ADAPTER_NAME, (char*)prop->val, &len);
341 if(ret && len && len <= prop->len)
350 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
353 ret = btif_config_get_str(bdstr,
354 BTIF_STORAGE_PATH_REMOTE_ALIASE, (char*)prop->val, &len);
355 if(ret && len && len <= prop->len)
364 case BT_PROPERTY_ADAPTER_SCAN_MODE:
365 if(prop->len >= (int)sizeof(int))
366 ret = btif_config_get_int("Adapter",
367 BTIF_STORAGE_KEY_ADAPTER_SCANMODE, (int*)prop->val);
369 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
370 if(prop->len >= (int)sizeof(int))
371 ret = btif_config_get_int("Adapter",
372 BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, (int*)prop->val);
374 case BT_PROPERTY_CLASS_OF_DEVICE:
375 if(prop->len >= (int)sizeof(int))
376 ret = btif_config_get_int(bdstr,
377 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, (int*)prop->val);
379 case BT_PROPERTY_TYPE_OF_DEVICE:
380 if(prop->len >= (int)sizeof(int))
381 ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTYPE, (int*)prop->val);
383 case BT_PROPERTY_UUIDS:
386 int size = sizeof(value);
387 if(btif_config_get_str(bdstr,
388 BTIF_STORAGE_PATH_REMOTE_SERVICE, value, &size))
390 bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val;
391 uint32_t num_uuids = 0;
392 btif_in_split_uuids_string_to_list(value, p_uuid, &num_uuids);
393 prop->len = num_uuids * sizeof(bt_uuid_t);
403 case BT_PROPERTY_REMOTE_VERSION_INFO:
405 bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
407 if(prop->len >= (int)sizeof(bt_remote_version_t))
409 ret = btif_config_get_int(bdstr,
410 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, &info->manufacturer);
413 ret = btif_config_get_int(bdstr,
414 BTIF_STORAGE_PATH_REMOTE_VER_VER, &info->version);
417 ret = btif_config_get_int(bdstr,
418 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, &info->sub_ver);
423 BTIF_TRACE_ERROR("Unknow prop type:%d", prop->type);
429 /*******************************************************************************
431 ** Function btif_in_fetch_bonded_devices
433 ** Description Internal helper function to fetch the bonded devices
436 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
438 *******************************************************************************/
439 static bt_status_t btif_in_fetch_bonded_device(const char *bdstr)
441 BOOLEAN bt_linkkey_file_found=FALSE;
444 size_t size = sizeof(link_key);
445 if(btif_config_get_bin(bdstr, "LinkKey", (uint8_t *)link_key, &size))
448 if(btif_config_get_int(bdstr, "LinkKeyType", &linkkey_type))
450 bt_linkkey_file_found = TRUE;
454 bt_linkkey_file_found = FALSE;
457 #if (BLE_INCLUDED == TRUE)
458 if((btif_in_fetch_bonded_ble_device(bdstr, FALSE, NULL) != BT_STATUS_SUCCESS)
459 && (!bt_linkkey_file_found))
461 BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", bdstr);
462 return BT_STATUS_FAIL;
465 if((!bt_linkkey_file_found))
467 BTIF_TRACE_DEBUG("Remote device:%s, no link key found", bdstr);
468 return BT_STATUS_FAIL;
471 return BT_STATUS_SUCCESS;
474 /*******************************************************************************
476 ** Function btif_in_fetch_bonded_devices
478 ** Description Internal helper function to fetch the bonded devices
481 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
483 *******************************************************************************/
484 static bt_status_t btif_in_fetch_bonded_devices(btif_bonded_devices_t *p_bonded_devices, int add)
486 memset(p_bonded_devices, 0, sizeof(btif_bonded_devices_t));
488 BOOLEAN bt_linkkey_file_found=FALSE;
491 for (const btif_config_section_iter_t *iter = btif_config_section_begin(); iter != btif_config_section_end(); iter = btif_config_section_next(iter)) {
492 const char *name = btif_config_section_name(iter);
493 if (!string_is_bdaddr(name))
496 BTIF_TRACE_DEBUG("Remote device:%s", name);
498 size_t size = sizeof(link_key);
499 if(btif_config_get_bin(name, "LinkKey", link_key, &size))
502 if(btif_config_get_int(name, "LinkKeyType", &linkkey_type))
505 //btif_config_get_int(name, "PinLength", &pin_len))
507 string_to_bdaddr(name, &bd_addr);
510 DEV_CLASS dev_class = {0, 0, 0};
513 if(btif_config_get_int(name, "DevClass", &cod))
514 uint2devclass((UINT32)cod, dev_class);
515 btif_config_get_int(name, "PinLength", &pin_length);
516 BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0,
517 (UINT8)linkkey_type, 0, pin_length);
519 #if BLE_INCLUDED == TRUE
520 if (btif_config_get_int(name, "DevType", &device_type) &&
521 (device_type == BT_DEVICE_TYPE_DUMO) )
523 btif_gatts_add_bonded_dev_from_nv(bd_addr.address);
527 bt_linkkey_file_found = TRUE;
528 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
532 #if (BLE_INCLUDED == TRUE)
533 bt_linkkey_file_found = FALSE;
535 BTIF_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid", name);
539 #if (BLE_INCLUDED == TRUE)
540 if(!(btif_in_fetch_bonded_ble_device(name, add, p_bonded_devices)) && (!bt_linkkey_file_found))
542 BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", name);
545 if(!bt_linkkey_file_found)
546 BTIF_TRACE_DEBUG("Remote device:%s, no link key", name);
549 return BT_STATUS_SUCCESS;
552 static void btif_read_le_key(const uint8_t key_type, const size_t key_len, bt_bdaddr_t bd_addr,
553 const uint8_t addr_type, const bool add_key, bool *device_added, bool *key_found)
555 assert(device_added);
559 memset(buffer, 0, sizeof(buffer));
561 if (btif_storage_get_ble_bonding_key(&bd_addr, key_type, buffer, key_len) == BT_STATUS_SUCCESS)
566 bdcpy(bta_bd_addr, bd_addr.address);
570 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
571 *device_added = true;
574 char bd_str[20] = {0};
575 BTIF_TRACE_DEBUG("%s() Adding key type %d for %s", __func__,
576 key_type, bdaddr_to_string(&bd_addr, bd_str, sizeof(bd_str)));
577 BTA_DmAddBleKey(bta_bd_addr, (tBTA_LE_KEY_VALUE *)buffer, key_type);
584 /*******************************************************************************
587 * Functions are synchronous and can be called by both from internal modules
588 * such as BTIF_DM and by external entiries from HAL via BTIF_context_switch.
589 * For OUT parameters, the caller is expected to provide the memory.
590 * Caller is expected to provide a valid pointer to 'property->value' based on
591 * the property->type.
592 *******************************************************************************/
594 /*******************************************************************************
596 ** Function btif_storage_get_adapter_property
598 ** Description BTIF storage API - Fetches the adapter property->type
599 ** from NVRAM and fills property->val.
600 ** Caller should provide memory for property->val and
601 ** set the property->val
603 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
604 ** BT_STATUS_FAIL otherwise
606 *******************************************************************************/
607 bt_status_t btif_storage_get_adapter_property(bt_property_t *property)
610 /* Special handling for adapter BD_ADDR and BONDED_DEVICES */
611 if (property->type == BT_PROPERTY_BDADDR)
613 bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)property->val;
614 /* This has been cached in btif. Just fetch it from there */
615 memcpy(bd_addr, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
616 property->len = sizeof(bt_bdaddr_t);
617 return BT_STATUS_SUCCESS;
619 else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES)
621 btif_bonded_devices_t bonded_devices;
623 btif_in_fetch_bonded_devices(&bonded_devices, 0);
625 BTIF_TRACE_DEBUG("%s: Number of bonded devices: %d Property:BT_PROPERTY_ADAPTER_BONDED_DEVICES", __FUNCTION__, bonded_devices.num_devices);
627 if (bonded_devices.num_devices > 0)
629 property->len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
630 memcpy(property->val, bonded_devices.devices, property->len);
633 /* if there are no bonded_devices, then length shall be 0 */
634 return BT_STATUS_SUCCESS;
636 else if (property->type == BT_PROPERTY_UUIDS)
638 /* publish list of local supported services */
639 bt_uuid_t *p_uuid = (bt_uuid_t*)property->val;
640 uint32_t num_uuids = 0;
643 tBTA_SERVICE_MASK service_mask = btif_get_enabled_services_mask();
644 LOG_INFO("%s service_mask:0x%x", __FUNCTION__, service_mask);
645 for (i=0; i < BTA_MAX_SERVICE_ID; i++)
647 /* This should eventually become a function when more services are enabled */
649 &(tBTA_SERVICE_MASK)(1 << i))
653 case BTA_HFP_SERVICE_ID:
655 uuid16_to_uuid128(UUID_SERVCLASS_AG_HANDSFREE,
659 /* intentional fall through: Send both BFP & HSP UUIDs if HFP is enabled */
660 case BTA_HSP_SERVICE_ID:
662 uuid16_to_uuid128(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,
666 case BTA_A2DP_SOURCE_SERVICE_ID:
668 uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SOURCE,
672 case BTA_HFP_HS_SERVICE_ID:
674 uuid16_to_uuid128(UUID_SERVCLASS_HF_HANDSFREE,
681 property->len = (num_uuids)*sizeof(bt_uuid_t);
682 return BT_STATUS_SUCCESS;
685 /* fall through for other properties */
686 if(!cfg2prop(NULL, property))
688 return btif_dm_get_adapter_property(property);
690 return BT_STATUS_SUCCESS;
693 /*******************************************************************************
695 ** Function btif_storage_set_adapter_property
697 ** Description BTIF storage API - Stores the adapter property
700 ** Returns BT_STATUS_SUCCESS if the store was successful,
701 ** BT_STATUS_FAIL otherwise
703 *******************************************************************************/
704 bt_status_t btif_storage_set_adapter_property(bt_property_t *property)
706 return prop2cfg(NULL, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
709 /*******************************************************************************
711 ** Function btif_storage_get_remote_device_property
713 ** Description BTIF storage API - Fetches the remote device property->type
714 ** from NVRAM and fills property->val.
715 ** Caller should provide memory for property->val and
716 ** set the property->val
718 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
719 ** BT_STATUS_FAIL otherwise
721 *******************************************************************************/
722 bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t *remote_bd_addr,
723 bt_property_t *property)
725 return cfg2prop(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
727 /*******************************************************************************
729 ** Function btif_storage_set_remote_device_property
731 ** Description BTIF storage API - Stores the remote device property
734 ** Returns BT_STATUS_SUCCESS if the store was successful,
735 ** BT_STATUS_FAIL otherwise
737 *******************************************************************************/
738 bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t *remote_bd_addr,
739 bt_property_t *property)
741 return prop2cfg(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
744 /*******************************************************************************
746 ** Function btif_storage_add_remote_device
748 ** Description BTIF storage API - Adds a newly discovered device to NVRAM
749 ** along with the timestamp. Also, stores the various
750 ** properties - RSSI, BDADDR, NAME (if found in EIR)
752 ** Returns BT_STATUS_SUCCESS if the store was successful,
753 ** BT_STATUS_FAIL otherwise
755 *******************************************************************************/
756 bt_status_t btif_storage_add_remote_device(bt_bdaddr_t *remote_bd_addr,
757 uint32_t num_properties,
758 bt_property_t *properties)
761 /* TODO: If writing a property, fails do we go back undo the earlier
762 * written properties? */
763 for (i=0; i < num_properties; i++)
765 /* Ignore the RSSI as this is not stored in DB */
766 if (properties[i].type == BT_PROPERTY_REMOTE_RSSI)
769 /* BD_ADDR for remote device needs special handling as we also store timestamp */
770 if (properties[i].type == BT_PROPERTY_BDADDR)
772 bt_property_t addr_prop;
773 memcpy(&addr_prop, &properties[i], sizeof(bt_property_t));
774 addr_prop.type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP;
775 btif_storage_set_remote_device_property(remote_bd_addr,
780 btif_storage_set_remote_device_property(remote_bd_addr,
784 return BT_STATUS_SUCCESS;
787 /*******************************************************************************
789 ** Function btif_storage_add_bonded_device
791 ** Description BTIF storage API - Adds the newly bonded device to NVRAM
792 ** along with the link-key, Key type and Pin key length
794 ** Returns BT_STATUS_SUCCESS if the store was successful,
795 ** BT_STATUS_FAIL otherwise
797 *******************************************************************************/
799 bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
805 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
806 int ret = btif_config_set_int(bdstr, "LinkKeyType", (int)key_type);
807 ret &= btif_config_set_int(bdstr, "PinLength", (int)pin_length);
808 ret &= btif_config_set_bin(bdstr, "LinkKey", link_key, sizeof(LINK_KEY));
809 /* write bonded info immediately */
811 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
814 /*******************************************************************************
816 ** Function btif_storage_remove_bonded_device
818 ** Description BTIF storage API - Deletes the bonded device from NVRAM
820 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
821 ** BT_STATUS_FAIL otherwise
823 *******************************************************************************/
824 bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
827 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
828 BTIF_TRACE_DEBUG("in bd addr:%s", bdstr);
830 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
831 btif_storage_remove_ble_bonding_keys(remote_bd_addr);
835 if(btif_config_exist(bdstr, "LinkKeyType"))
836 ret &= btif_config_remove(bdstr, "LinkKeyType");
837 if(btif_config_exist(bdstr, "PinLength"))
838 ret &= btif_config_remove(bdstr, "PinLength");
839 if(btif_config_exist(bdstr, "LinkKey"))
840 ret &= btif_config_remove(bdstr, "LinkKey");
841 /* write bonded info immediately */
843 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
847 /*******************************************************************************
849 ** Function btif_storage_load_bonded_devices
851 ** Description BTIF storage API - Loads all the bonded devices from NVRAM
852 ** and adds to the BTA.
853 ** Additionally, this API also invokes the adaper_properties_cb
854 ** and remote_device_properties_cb for each of the bonded devices.
856 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
858 *******************************************************************************/
859 bt_status_t btif_storage_load_bonded_devices(void)
861 btif_bonded_devices_t bonded_devices;
863 bt_property_t adapter_props[6];
864 uint32_t num_props = 0;
865 bt_property_t remote_properties[8];
867 bt_bdname_t name, alias;
869 uint32_t disc_timeout;
870 bt_bdaddr_t *devices_list;
871 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
872 bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
873 uint32_t cod, devtype;
875 btif_in_fetch_bonded_devices(&bonded_devices, 1);
877 /* Now send the adapter_properties_cb with all adapter_properties */
879 memset(adapter_props, 0, sizeof(adapter_props));
882 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDADDR, &addr, sizeof(addr),
883 adapter_props[num_props]);
887 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDNAME, &name, sizeof(name),
888 adapter_props[num_props]);
892 /* TODO: At the time of BT on, always report the scan mode as 0 irrespective
893 of the scan_mode during the previous enable cycle.
894 This needs to be re-visited as part of the app/stack enable sequence
896 mode = BT_SCAN_MODE_NONE;
897 adapter_props[num_props].type = BT_PROPERTY_ADAPTER_SCAN_MODE;
898 adapter_props[num_props].len = sizeof(mode);
899 adapter_props[num_props].val = &mode;
903 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
904 &disc_timeout, sizeof(disc_timeout),
905 adapter_props[num_props]);
909 devices_list = (bt_bdaddr_t*)osi_malloc(sizeof(bt_bdaddr_t)*bonded_devices.num_devices);
910 adapter_props[num_props].type = BT_PROPERTY_ADAPTER_BONDED_DEVICES;
911 adapter_props[num_props].len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
912 adapter_props[num_props].val = devices_list;
913 for (i=0; i < bonded_devices.num_devices; i++)
915 memcpy(devices_list + i, &bonded_devices.devices[i], sizeof(bt_bdaddr_t));
920 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_UUIDS,
921 local_uuids, sizeof(local_uuids),
922 adapter_props[num_props]);
925 btif_adapter_properties_evt(BT_STATUS_SUCCESS, num_props, adapter_props);
927 osi_free(devices_list);
930 BTIF_TRACE_EVENT("%s: %d bonded devices found", __FUNCTION__, bonded_devices.num_devices);
933 for (i = 0; i < bonded_devices.num_devices; i++)
935 bt_bdaddr_t *p_remote_addr;
938 p_remote_addr = &bonded_devices.devices[i];
939 memset(remote_properties, 0, sizeof(remote_properties));
940 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_BDNAME,
942 remote_properties[num_props]);
945 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_REMOTE_FRIENDLY_NAME,
946 &alias, sizeof(alias),
947 remote_properties[num_props]);
950 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_CLASS_OF_DEVICE,
952 remote_properties[num_props]);
955 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_TYPE_OF_DEVICE,
956 &devtype, sizeof(devtype),
957 remote_properties[num_props]);
960 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_UUIDS,
961 remote_uuids, sizeof(remote_uuids),
962 remote_properties[num_props]);
965 btif_remote_properties_evt(BT_STATUS_SUCCESS, p_remote_addr,
966 num_props, remote_properties);
969 return BT_STATUS_SUCCESS;
972 #if (BLE_INCLUDED == TRUE)
974 /*******************************************************************************
976 ** Function btif_storage_add_ble_bonding_key
978 ** Description BTIF storage API - Adds the newly bonded device to NVRAM
979 ** along with the ble-key, Key type and Pin key length
981 ** Returns BT_STATUS_SUCCESS if the store was successful,
982 ** BT_STATUS_FAIL otherwise
984 *******************************************************************************/
986 bt_status_t btif_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
992 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
996 case BTIF_DM_LE_KEY_PENC:
997 name = "LE_KEY_PENC";
999 case BTIF_DM_LE_KEY_PID:
1000 name = "LE_KEY_PID";
1002 case BTIF_DM_LE_KEY_PCSRK:
1003 name = "LE_KEY_PCSRK";
1005 case BTIF_DM_LE_KEY_LENC:
1006 name = "LE_KEY_LENC";
1008 case BTIF_DM_LE_KEY_LCSRK:
1009 name = "LE_KEY_LCSRK";
1011 case BTIF_DM_LE_KEY_LID:
1012 name = "LE_KEY_LID";
1015 return BT_STATUS_FAIL;
1017 int ret = btif_config_set_bin(bdstr, name, (const uint8_t *)key, key_length);
1019 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1022 /*******************************************************************************
1024 ** Function btif_storage_get_ble_bonding_key
1028 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
1029 ** BT_STATUS_FAIL otherwise
1031 *******************************************************************************/
1032 bt_status_t btif_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
1038 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1042 case BTIF_DM_LE_KEY_PENC:
1043 name = "LE_KEY_PENC";
1045 case BTIF_DM_LE_KEY_PID:
1046 name = "LE_KEY_PID";
1048 case BTIF_DM_LE_KEY_PCSRK:
1049 name = "LE_KEY_PCSRK";
1051 case BTIF_DM_LE_KEY_LENC:
1052 name = "LE_KEY_LENC";
1054 case BTIF_DM_LE_KEY_LCSRK:
1055 name = "LE_KEY_LCSRK";
1057 case BTIF_DM_LE_KEY_LID:
1058 name = "LE_KEY_LID";
1060 return BT_STATUS_FAIL;
1062 size_t length = key_length;
1063 int ret = btif_config_get_bin(bdstr, name, (uint8_t *)key_value, &length);
1064 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1068 /*******************************************************************************
1070 ** Function btif_storage_remove_ble_keys
1072 ** Description BTIF storage API - Deletes the bonded device from NVRAM
1074 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1075 ** BT_STATUS_FAIL otherwise
1077 *******************************************************************************/
1078 bt_status_t btif_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr)
1081 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1082 BTIF_TRACE_DEBUG(" %s in bd addr:%s",__FUNCTION__, bdstr);
1084 if(btif_config_exist(bdstr, "LE_KEY_PENC"))
1085 ret &= btif_config_remove(bdstr, "LE_KEY_PENC");
1086 if(btif_config_exist(bdstr, "LE_KEY_PID"))
1087 ret &= btif_config_remove(bdstr, "LE_KEY_PID");
1088 if(btif_config_exist(bdstr, "LE_KEY_PCSRK"))
1089 ret &= btif_config_remove(bdstr, "LE_KEY_PCSRK");
1090 if(btif_config_exist(bdstr, "LE_KEY_LENC"))
1091 ret &= btif_config_remove(bdstr, "LE_KEY_LENC");
1092 if(btif_config_exist(bdstr, "LE_KEY_LCSRK"))
1093 ret &= btif_config_remove(bdstr, "LE_KEY_LCSRK");
1095 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1098 /*******************************************************************************
1100 ** Function btif_storage_add_ble_local_key
1102 ** Description BTIF storage API - Adds the ble key to NVRAM
1104 ** Returns BT_STATUS_SUCCESS if the store was successful,
1105 ** BT_STATUS_FAIL otherwise
1107 *******************************************************************************/
1108 bt_status_t btif_storage_add_ble_local_key(char *key,
1115 case BTIF_DM_LE_LOCAL_KEY_IR:
1116 name = "LE_LOCAL_KEY_IR";
1118 case BTIF_DM_LE_LOCAL_KEY_IRK:
1119 name = "LE_LOCAL_KEY_IRK";
1121 case BTIF_DM_LE_LOCAL_KEY_DHK:
1122 name = "LE_LOCAL_KEY_DHK";
1124 case BTIF_DM_LE_LOCAL_KEY_ER:
1125 name = "LE_LOCAL_KEY_ER";
1128 return BT_STATUS_FAIL;
1130 int ret = btif_config_set_bin("Adapter", name, (const uint8_t *)key, key_length);
1132 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1135 /*******************************************************************************
1137 ** Function btif_storage_get_ble_local_key
1141 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
1142 ** BT_STATUS_FAIL otherwise
1144 *******************************************************************************/
1145 bt_status_t btif_storage_get_ble_local_key(UINT8 key_type,
1152 case BTIF_DM_LE_LOCAL_KEY_IR:
1153 name = "LE_LOCAL_KEY_IR";
1155 case BTIF_DM_LE_LOCAL_KEY_IRK:
1156 name = "LE_LOCAL_KEY_IRK";
1158 case BTIF_DM_LE_LOCAL_KEY_DHK:
1159 name = "LE_LOCAL_KEY_DHK";
1161 case BTIF_DM_LE_LOCAL_KEY_ER:
1162 name = "LE_LOCAL_KEY_ER";
1165 return BT_STATUS_FAIL;
1167 size_t length = key_length;
1168 int ret = btif_config_get_bin("Adapter", name, (uint8_t *)key_value, &length);
1169 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1172 /*******************************************************************************
1174 ** Function btif_storage_remove_ble_local_keys
1176 ** Description BTIF storage API - Deletes the bonded device from NVRAM
1178 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1179 ** BT_STATUS_FAIL otherwise
1181 *******************************************************************************/
1182 bt_status_t btif_storage_remove_ble_local_keys(void)
1185 if(btif_config_exist("Adapter", "LE_LOCAL_KEY_IR"))
1186 ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_IR");
1187 if(btif_config_exist("Adapter", "LE_LOCAL_KEY_IRK"))
1188 ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_IRK");
1189 if(btif_config_exist("Adapter", "LE_LOCAL_KEY_DHK"))
1190 ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_DHK");
1191 if(btif_config_exist("Adapter", "LE_LOCAL_KEY_ER"))
1192 ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_ER");
1194 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1197 bt_status_t btif_in_fetch_bonded_ble_device(const char *remote_bd_addr, int add, btif_bonded_devices_t *p_bonded_devices)
1202 bt_bdaddr_t bd_addr;
1203 BD_ADDR bta_bd_addr;
1204 bool device_added = false;
1205 bool key_found = false;
1207 if (!btif_config_get_int(remote_bd_addr, "DevType", &device_type))
1208 return BT_STATUS_FAIL;
1210 if ((device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE)
1212 BTIF_TRACE_DEBUG("%s Found a LE device: %s", __func__, remote_bd_addr);
1214 string_to_bdaddr(remote_bd_addr, &bd_addr);
1215 bdcpy(bta_bd_addr, bd_addr.address);
1217 if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS)
1219 addr_type = BLE_ADDR_PUBLIC;
1220 btif_storage_set_remote_addr_type(&bd_addr, BLE_ADDR_PUBLIC);
1223 btif_read_le_key(BTIF_DM_LE_KEY_PENC, sizeof(tBTM_LE_PENC_KEYS),
1224 bd_addr, addr_type, add, &device_added, &key_found);
1226 btif_read_le_key(BTIF_DM_LE_KEY_PID, sizeof(tBTM_LE_PID_KEYS),
1227 bd_addr, addr_type, add, &device_added, &key_found);
1229 btif_read_le_key(BTIF_DM_LE_KEY_LID, sizeof(tBTM_LE_PID_KEYS),
1230 bd_addr, addr_type, add, &device_added, &key_found);
1232 btif_read_le_key(BTIF_DM_LE_KEY_PCSRK, sizeof(tBTM_LE_PCSRK_KEYS),
1233 bd_addr, addr_type, add, &device_added, &key_found);
1235 btif_read_le_key(BTIF_DM_LE_KEY_LENC, sizeof(tBTM_LE_LENC_KEYS),
1236 bd_addr, addr_type, add, &device_added, &key_found);
1238 btif_read_le_key(BTIF_DM_LE_KEY_LCSRK, sizeof(tBTM_LE_LCSRK_KEYS),
1239 bd_addr, addr_type, add, &device_added, &key_found);
1241 // Fill in the bonded devices
1244 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
1245 btif_gatts_add_bonded_dev_from_nv(bta_bd_addr);
1249 return BT_STATUS_SUCCESS;
1251 return BT_STATUS_FAIL;
1254 bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1258 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1259 int ret = btif_config_set_int(bdstr, "AddrType", (int)addr_type);
1261 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1264 /*******************************************************************************
1266 ** Function btif_storage_get_remote_addr_type
1268 ** Description BTIF storage API - Fetches the remote addr type
1270 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
1271 ** BT_STATUS_FAIL otherwise
1273 *******************************************************************************/
1274 bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1278 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1279 int ret = btif_config_get_int(bdstr, "AddrType", addr_type);
1280 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1283 /*******************************************************************************
1285 ** Function btif_storage_add_hid_device_info
1287 ** Description BTIF storage API - Adds the hid information of bonded hid devices-to NVRAM
1289 ** Returns BT_STATUS_SUCCESS if the store was successful,
1290 ** BT_STATUS_FAIL otherwise
1292 *******************************************************************************/
1294 bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr,
1295 UINT16 attr_mask, UINT8 sub_class,
1296 UINT8 app_id, UINT16 vendor_id,
1297 UINT16 product_id, UINT16 version,
1298 UINT8 ctry_code, UINT16 ssr_max_latency,
1299 UINT16 ssr_min_tout, UINT16 dl_len, UINT8 *dsc_list)
1302 BTIF_TRACE_DEBUG("btif_storage_add_hid_device_info:");
1303 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1304 btif_config_set_int(bdstr, "HidAttrMask", attr_mask);
1305 btif_config_set_int(bdstr, "HidSubClass", sub_class);
1306 btif_config_set_int(bdstr, "HidAppId", app_id);
1307 btif_config_set_int(bdstr, "HidVendorId", vendor_id);
1308 btif_config_set_int(bdstr, "HidProductId", product_id);
1309 btif_config_set_int(bdstr, "HidVersion", version);
1310 btif_config_set_int(bdstr, "HidCountryCode", ctry_code);
1311 btif_config_set_int(bdstr, "HidSSRMaxLatency", ssr_max_latency);
1312 btif_config_set_int(bdstr, "HidSSRMinTimeout", ssr_min_tout);
1314 btif_config_set_bin(bdstr, "HidDescriptor", dsc_list, dl_len);
1316 return BT_STATUS_SUCCESS;
1319 /*******************************************************************************
1321 ** Function btif_storage_load_bonded_hid_info
1323 ** Description BTIF storage API - Loads hid info for all the bonded devices from NVRAM
1324 ** and adds those devices to the BTA_HH.
1326 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
1328 *******************************************************************************/
1329 bt_status_t btif_storage_load_bonded_hid_info(void)
1331 bt_bdaddr_t bd_addr;
1332 tBTA_HH_DEV_DSCP_INFO dscp_info;
1337 memset(&dscp_info, 0, sizeof(dscp_info));
1338 for (const btif_config_section_iter_t *iter = btif_config_section_begin(); iter != btif_config_section_end(); iter = btif_config_section_next(iter)) {
1339 const char *name = btif_config_section_name(iter);
1340 if (!string_is_bdaddr(name))
1343 BTIF_TRACE_DEBUG("Remote device:%s", name);
1345 if(btif_in_fetch_bonded_device(name) == BT_STATUS_SUCCESS)
1347 if(btif_config_get_int(name, "HidAttrMask", &value))
1349 attr_mask = (uint16_t)value;
1351 btif_config_get_int(name, "HidSubClass", &value);
1352 sub_class = (uint8_t)value;
1354 btif_config_get_int(name, "HidAppId", &value);
1355 app_id = (uint8_t)value;
1357 btif_config_get_int(name, "HidVendorId", &value);
1358 dscp_info.vendor_id = (uint16_t) value;
1360 btif_config_get_int(name, "HidProductId", &value);
1361 dscp_info.product_id = (uint16_t) value;
1363 btif_config_get_int(name, "HidVersion", &value);
1364 dscp_info.version = (uint8_t) value;
1366 btif_config_get_int(name, "HidCountryCode", &value);
1367 dscp_info.ctry_code = (uint8_t) value;
1370 btif_config_get_int(name, "HidSSRMaxLatency", &value);
1371 dscp_info.ssr_max_latency = (uint16_t) value;
1374 btif_config_get_int(name, "HidSSRMinTimeout", &value);
1375 dscp_info.ssr_min_tout = (uint16_t) value;
1377 size_t len = btif_config_get_bin_length(name, "HidDescriptor");
1380 dscp_info.descriptor.dl_len = (uint16_t)len;
1381 dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len);
1382 btif_config_get_bin(name, "HidDescriptor", (uint8_t *)dscp_info.descriptor.dsc_list, &len);
1384 string_to_bdaddr(name, &bd_addr);
1385 // add extracted information to BTA HH
1386 if (btif_hh_add_added_dev(bd_addr,attr_mask))
1388 BTA_HhAddDev(bd_addr.address, attr_mask, sub_class,
1395 if(btif_config_get_int(name, "HidAttrMask", &value))
1397 btif_storage_remove_hid_info(&bd_addr);
1398 string_to_bdaddr(name, &bd_addr);
1403 return BT_STATUS_SUCCESS;
1406 /*******************************************************************************
1408 ** Function btif_storage_remove_hid_info
1410 ** Description BTIF storage API - Deletes the bonded hid device info from NVRAM
1412 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1413 ** BT_STATUS_FAIL otherwise
1415 *******************************************************************************/
1416 bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr)
1419 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1421 btif_config_remove(bdstr, "HidAttrMask");
1422 btif_config_remove(bdstr, "HidSubClass");
1423 btif_config_remove(bdstr, "HidAppId");
1424 btif_config_remove(bdstr, "HidVendorId");
1425 btif_config_remove(bdstr, "HidProductId");
1426 btif_config_remove(bdstr, "HidVersion");
1427 btif_config_remove(bdstr, "HidCountryCode");
1428 btif_config_remove(bdstr, "HidSSRMaxLatency");
1429 btif_config_remove(bdstr, "HidSSRMinTimeout");
1430 btif_config_remove(bdstr, "HidDescriptor");
1432 return BT_STATUS_SUCCESS;
1435 /*******************************************************************************
1437 ** Function btif_storage_read_hl_apps_cb
1439 ** Description BTIF storage API - Read HL application control block from NVRAM
1441 ** Returns BT_STATUS_SUCCESS if the operation was successful,
1442 ** BT_STATUS_FAIL otherwise
1444 *******************************************************************************/
1445 bt_status_t btif_storage_read_hl_apps_cb(char *value, int value_size)
1447 bt_status_t bt_status = BT_STATUS_SUCCESS;
1449 if (!btif_config_exist(BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB))
1451 memset(value, 0, value_size);
1452 if (!btif_config_set_bin(BTIF_STORAGE_HL_APP,BTIF_STORAGE_HL_APP_CB,
1453 (const uint8_t *)value, value_size))
1455 bt_status = BT_STATUS_FAIL;
1464 size_t read_size = value_size;
1465 if (!btif_config_get_bin(BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB,
1466 (uint8_t *)value, &read_size))
1468 bt_status = BT_STATUS_FAIL;
1472 if (read_size != (size_t)value_size)
1474 BTIF_TRACE_ERROR("%s value_size=%d read_size=%d",
1475 __FUNCTION__, value_size, read_size);
1476 bt_status = BT_STATUS_FAIL;
1482 BTIF_TRACE_DEBUG("%s status=%d value_size=%d", __FUNCTION__, bt_status, value_size);
1487 /*******************************************************************************
1489 ** Function btif_storage_load_autopair_device_list
1491 ** Description BTIF storage API - Populates auto pair device list
1493 ** Returns BT_STATUS_SUCCESS if the auto pair blacklist is successfully populated
1494 ** BT_STATUS_FAIL otherwise
1496 *******************************************************************************/
1497 bt_status_t btif_storage_load_autopair_device_list() {
1498 // Configuration has already been loaded. No need to reload.
1499 if (btif_config_has_section(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST)) {
1500 return BT_STATUS_SUCCESS;
1503 static const char *key_names[] = {
1504 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR,
1505 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME,
1506 BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST,
1507 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME,
1508 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR,
1511 config_t *config = config_new(BTIF_AUTO_PAIR_CONF_FILE);
1513 LOG_ERROR("%s failed to open auto pair blacklist conf file '%s'.", __func__, BTIF_AUTO_PAIR_CONF_FILE);
1514 return BT_STATUS_FAIL;
1517 for (size_t i = 0; i < ARRAY_SIZE(key_names); ++i) {
1518 const char *value = config_get_string(config, CONFIG_DEFAULT_SECTION, key_names[i], NULL);
1520 btif_config_set_str(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, key_names[i], value);
1524 config_free(config);
1525 return BT_STATUS_SUCCESS;
1528 /*******************************************************************************
1530 ** Function btif_storage_is_device_autopair_blacklisted
1532 ** Description BTIF storage API Checks if the given device is blacklisted for auto pairing
1534 ** Returns TRUE if the device is found in the auto pair blacklist
1537 *******************************************************************************/
1538 BOOLEAN btif_storage_is_device_autopair_blacklisted(bt_bdaddr_t *remote_bd_addr)
1543 char value[BTIF_STORAGE_MAX_LINE_SZ];
1544 int value_size = sizeof(value);
1546 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1548 /* Consider only Lower Address Part from BD Address */
1551 if(btif_config_get_str(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1552 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR, value, &value_size))
1554 if (strcasestr(value,bdstr) != NULL)
1558 dev_name_str = BTM_SecReadDevName((remote_bd_addr->address));
1560 if (dev_name_str != NULL)
1562 value_size = sizeof(value);
1563 if(btif_config_get_str(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1564 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME, value, &value_size))
1566 if (strstr(value,dev_name_str) != NULL)
1569 value_size = sizeof(value);
1570 if(btif_config_get_str(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1571 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME, value, &value_size))
1573 token = strtok(value, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1574 while (token != NULL)
1576 if (strstr(dev_name_str, token) != NULL)
1579 token = strtok(NULL, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1583 if(btif_config_get_str(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1584 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, value, &value_size))
1586 if (strstr(value,bdstr) != NULL)
1592 /*******************************************************************************
1594 ** Function btif_storage_add_device_to_autopair_blacklist
1596 ** Description BTIF storage API - Add a remote device to the auto pairing blacklist
1598 ** Returns BT_STATUS_SUCCESS if the device is successfully added to the auto pair blacklist
1599 ** BT_STATUS_FAIL otherwise
1601 *******************************************************************************/
1602 bt_status_t btif_storage_add_device_to_autopair_blacklist(bt_bdaddr_t *remote_bd_addr)
1606 char linebuf[BTIF_STORAGE_MAX_LINE_SZ+20];
1607 char input_value [20];
1609 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1610 strlcpy(input_value, (char*)bdstr, sizeof(input_value));
1611 strlcat(input_value,BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR, sizeof(input_value));
1613 int line_size = sizeof(linebuf);
1614 if(btif_config_get_str(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1615 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf, &line_size))
1617 /* Append this address to the dynamic List of BD address */
1618 strncat (linebuf, input_value, BTIF_STORAGE_MAX_LINE_SZ);
1622 strncpy( linebuf,input_value, BTIF_STORAGE_MAX_LINE_SZ);
1625 /* Write back the key value */
1626 ret = btif_config_set_str(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1627 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf);
1629 return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1632 /*******************************************************************************
1634 ** Function btif_storage_is_fixed_pin_zeros_keyboard
1636 ** Description BTIF storage API - checks if this device has fixed PIN key device list
1638 ** Returns TRUE if the device is found in the fixed pin keyboard device list
1641 *******************************************************************************/
1642 BOOLEAN btif_storage_is_fixed_pin_zeros_keyboard(bt_bdaddr_t *remote_bd_addr)
1645 char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1647 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1649 /*consider on LAP part of BDA string*/
1652 int line_size = sizeof(linebuf);
1653 if(btif_config_get_str(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1654 BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST, linebuf, &line_size))
1656 if (strcasestr(linebuf,bdstr) != NULL)