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
37 #include <hardware/bluetooth.h>
38 #include "btif_config.h"
39 #define LOG_TAG "BTIF_STORAGE"
42 #include "btif_storage.h"
43 #include "btif_util.h"
48 #include "bta_hh_api.h"
51 #include <cutils/log.h>
53 /************************************************************************************
55 ************************************************************************************/
57 #define BTIF_STORAGE_PATH_BLUEDROID "/data/misc/bluedroid"
59 //#define BTIF_STORAGE_PATH_ADAPTER_INFO "adapter_info"
60 //#define BTIF_STORAGE_PATH_REMOTE_DEVICES "remote_devices"
61 #define BTIF_STORAGE_PATH_REMOTE_DEVTIME "Timestamp"
62 #define BTIF_STORAGE_PATH_REMOTE_DEVCLASS "DevClass"
63 #define BTIF_STORAGE_PATH_REMOTE_DEVTYPE "DevType"
64 #define BTIF_STORAGE_PATH_REMOTE_NAME "Name"
65 #define BTIF_STORAGE_PATH_REMOTE_VER_MFCT "Manufacturer"
66 #define BTIF_STORAGE_PATH_REMOTE_VER_VER "LmpVer"
67 #define BTIF_STORAGE_PATH_REMOTE_VER_SUBVER "LmpSubVer"
69 //#define BTIF_STORAGE_PATH_REMOTE_LINKKEYS "remote_linkkeys"
70 #define BTIF_STORAGE_PATH_REMOTE_ALIASE "Aliase"
71 #define BTIF_STORAGE_PATH_REMOTE_SERVICE "Service"
72 #define BTIF_STORAGE_PATH_REMOTE_HIDINFO "HidInfo"
73 #define BTIF_STORAGE_KEY_ADAPTER_NAME "Name"
74 #define BTIF_STORAGE_KEY_ADAPTER_SCANMODE "ScanMode"
75 #define BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT "DiscoveryTimeout"
78 #define BTIF_AUTO_PAIR_CONF_FILE "/etc/bluetooth/auto_pair_devlist.conf"
79 #define BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST "AutoPairBlacklist"
80 #define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR "AddressBlacklist"
81 #define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME "ExactNameBlacklist"
82 #define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME "PartialNameBlacklist"
83 #define BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST "FixedPinZerosKeyboardBlacklist"
84 #define BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR "DynamicAddressBlacklist"
86 #define BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR ","
89 /* This is a local property to add a device found */
90 #define BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP 0xFF
92 #define BTIF_STORAGE_GET_ADAPTER_PROP(t,v,l,p) \
93 {p.type=t;p.val=v;p.len=l; btif_storage_get_adapter_property(&p);}
95 #define BTIF_STORAGE_GET_REMOTE_PROP(b,t,v,l,p) \
96 {p.type=t;p.val=v;p.len=l;btif_storage_get_remote_device_property(b,&p);}
98 #define STORAGE_BDADDR_STRING_SZ (18) /* 00:11:22:33:44:55 */
99 #define STORAGE_UUID_STRING_SIZE (36+1) /* 00001200-0000-1000-8000-00805f9b34fb; */
100 #define STORAGE_PINLEN_STRING_MAX_SIZE (2) /* ascii pinlen max chars */
101 #define STORAGE_KEYTYPE_STRING_MAX_SIZE (1) /* ascii keytype max chars */
103 #define STORAGE_KEY_TYPE_MAX (10)
105 #define STORAGE_HID_ATRR_MASK_SIZE (4)
106 #define STORAGE_HID_SUB_CLASS_SIZE (2)
107 #define STORAGE_HID_APP_ID_SIZE (2)
108 #define STORAGE_HID_VENDOR_ID_SIZE (4)
109 #define STORAGE_HID_PRODUCT_ID_SIZE (4)
110 #define STORAGE_HID_VERSION_SIZE (4)
111 #define STORAGE_HID_CTRY_CODE_SIZE (2)
112 #define STORAGE_HID_DESC_LEN_SIZE (4)
113 #define STORAGE_HID_DESC_MAX_SIZE (2*512)
115 /* <18 char bd addr> <space> LIST< <36 char uuid> <;> > <keytype (dec)> <pinlen> */
116 #define BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX (STORAGE_BDADDR_STRING_SZ + 1 +\
117 STORAGE_UUID_STRING_SIZE*BT_MAX_NUM_UUIDS + \
118 STORAGE_PINLEN_STRING_MAX_SIZE +\
119 STORAGE_KEYTYPE_STRING_MAX_SIZE)
121 #define STORAGE_REMOTE_LINKKEYS_ENTRY_SIZE (LINK_KEY_LEN*2 + 1 + 2 + 1 + 2)
123 /* <18 char bd addr> <space>LIST <attr_mask> <space> > <sub_class> <space> <app_id> <space>
124 <vendor_id> <space> > <product_id> <space> <version> <space>
125 <ctry_code> <space> > <desc_len> <space> <desc_list> <space> */
126 #define BTIF_HID_INFO_ENTRY_SIZE_MAX (STORAGE_BDADDR_STRING_SZ + 1 +\
127 STORAGE_HID_ATRR_MASK_SIZE + 1 +\
128 STORAGE_HID_SUB_CLASS_SIZE + 1 +\
129 STORAGE_HID_APP_ID_SIZE+ 1 +\
130 STORAGE_HID_VENDOR_ID_SIZE+ 1 +\
131 STORAGE_HID_PRODUCT_ID_SIZE+ 1 +\
132 STORAGE_HID_VERSION_SIZE+ 1 +\
133 STORAGE_HID_CTRY_CODE_SIZE+ 1 +\
134 STORAGE_HID_DESC_LEN_SIZE+ 1 +\
135 STORAGE_HID_DESC_MAX_SIZE+ 1 )
138 /* currently remote services is the potentially largest entry */
139 #define BTIF_STORAGE_MAX_LINE_SZ BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX
142 /* check against unv max entry size at compile time */
143 #if (BTIF_STORAGE_ENTRY_MAX_SIZE > UNV_MAXLINE_LENGTH)
144 #error "btif storage entry size exceeds unv max line size"
148 #define BTIF_STORAGE_HL_APP "hl_app"
149 #define BTIF_STORAGE_HL_APP_CB "hl_app_cb"
150 #define BTIF_STORAGE_HL_APP_DATA "hl_app_data_"
151 #define BTIF_STORAGE_HL_APP_MDL_DATA "hl_app_mdl_data_"
153 /************************************************************************************
154 ** Local type definitions
155 ************************************************************************************/
158 uint32_t num_devices;
159 bt_bdaddr_t devices[BTM_SEC_MAX_DEVICE_RECORDS];
160 } btif_bonded_devices_t;
162 /************************************************************************************
163 ** External variables
164 ************************************************************************************/
165 extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
166 extern bt_bdaddr_t btif_local_bd_addr;
168 /************************************************************************************
169 ** External functions
170 ************************************************************************************/
172 extern void btif_gatts_add_bonded_dev_from_nv(BD_ADDR bda);
174 /************************************************************************************
175 ** Internal Functions
176 ************************************************************************************/
178 bt_status_t btif_in_fetch_bonded_ble_device(const char *remote_bd_addr,int add,
179 btif_bonded_devices_t *p_bonded_devices);
180 bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
183 /************************************************************************************
185 ************************************************************************************/
187 /*******************************************************************************
189 ** Function btif_in_split_uuids_string_to_list
191 ** Description Internal helper function to split the string of UUIDs
192 ** read from the NVRAM to an array
196 *******************************************************************************/
197 static void btif_in_split_uuids_string_to_list(char *str, bt_uuid_t *p_uuid,
198 uint32_t *p_num_uuid)
206 //p_needle = strchr(p_start, ';');
207 p_needle = strchr(p_start, ' ');
208 if (p_needle < p_start) break;
209 memset(buf, 0, sizeof(buf));
210 strncpy(buf, p_start, (p_needle-p_start));
211 string_to_uuid(buf, p_uuid + num);
213 p_start = ++p_needle;
215 } while (*p_start != 0);
218 static int prop2cfg(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
222 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
223 BTIF_TRACE_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
225 if(prop->len <= 0 || prop->len > (int)sizeof(value) - 1)
227 BTIF_TRACE_ERROR("property type:%d, len:%d is invalid", prop->type, prop->len);
232 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
233 btif_config_set_int(bdstr,
234 BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int)time(NULL));
236 case BT_PROPERTY_BDNAME:
237 strncpy(value, (char*)prop->val, prop->len);
238 value[prop->len]='\0';
240 btif_config_set_str(bdstr,
241 BTIF_STORAGE_PATH_REMOTE_NAME, value);
242 else btif_config_set_str("Adapter",
243 BTIF_STORAGE_KEY_ADAPTER_NAME, value);
244 /* save name immediately */
247 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
248 strncpy(value, (char*)prop->val, prop->len);
249 value[prop->len]='\0';
250 btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_ALIASE, value);
251 /* save friendly name immediately */
254 case BT_PROPERTY_ADAPTER_SCAN_MODE:
255 btif_config_set_int("Adapter",
256 BTIF_STORAGE_KEY_ADAPTER_SCANMODE, *(int*)prop->val);
258 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
259 btif_config_set_int("Adapter",
260 BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, *(int*)prop->val);
262 case BT_PROPERTY_CLASS_OF_DEVICE:
263 btif_config_set_int(bdstr,
264 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, *(int*)prop->val);
266 case BT_PROPERTY_TYPE_OF_DEVICE:
267 btif_config_set_int(bdstr,
268 BTIF_STORAGE_PATH_REMOTE_DEVTYPE, *(int*)prop->val);
270 case BT_PROPERTY_UUIDS:
275 for (i=0; i < (prop->len)/sizeof(bt_uuid_t); i++)
277 bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val + i;
278 memset(buf, 0, sizeof(buf));
279 uuid_to_string(p_uuid, buf);
281 //strcat(value, ";");
284 btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value);
288 case BT_PROPERTY_REMOTE_VERSION_INFO:
290 bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
295 btif_config_set_int(bdstr,
296 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, info->manufacturer);
297 btif_config_set_int(bdstr,
298 BTIF_STORAGE_PATH_REMOTE_VER_VER, info->version);
299 btif_config_set_int(bdstr,
300 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, info->sub_ver);
305 BTIF_TRACE_ERROR("Unknow prop type:%d", prop->type);
310 static int cfg2prop(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
314 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
315 BTIF_TRACE_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
318 BTIF_TRACE_ERROR("property type:%d, len:%d is invalid", prop->type, prop->len);
324 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
325 if(prop->len >= (int)sizeof(int))
326 ret = btif_config_get_int(bdstr,
327 BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int*)prop->val);
329 case BT_PROPERTY_BDNAME:
333 ret = btif_config_get_str(bdstr,
334 BTIF_STORAGE_PATH_REMOTE_NAME, (char*)prop->val, &len);
335 else ret = btif_config_get_str("Adapter",
336 BTIF_STORAGE_KEY_ADAPTER_NAME, (char*)prop->val, &len);
337 if(ret && len && len <= prop->len)
346 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
349 ret = btif_config_get_str(bdstr,
350 BTIF_STORAGE_PATH_REMOTE_ALIASE, (char*)prop->val, &len);
351 if(ret && len && len <= prop->len)
360 case BT_PROPERTY_ADAPTER_SCAN_MODE:
361 if(prop->len >= (int)sizeof(int))
362 ret = btif_config_get_int("Adapter",
363 BTIF_STORAGE_KEY_ADAPTER_SCANMODE, (int*)prop->val);
365 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
366 if(prop->len >= (int)sizeof(int))
367 ret = btif_config_get_int("Adapter",
368 BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, (int*)prop->val);
370 case BT_PROPERTY_CLASS_OF_DEVICE:
371 if(prop->len >= (int)sizeof(int))
372 ret = btif_config_get_int(bdstr,
373 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, (int*)prop->val);
375 case BT_PROPERTY_TYPE_OF_DEVICE:
376 if(prop->len >= (int)sizeof(int))
377 ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTYPE, (int*)prop->val);
379 case BT_PROPERTY_UUIDS:
382 int size = sizeof(value);
383 if(btif_config_get_str(bdstr,
384 BTIF_STORAGE_PATH_REMOTE_SERVICE, value, &size))
386 bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val;
387 uint32_t num_uuids = 0;
388 btif_in_split_uuids_string_to_list(value, p_uuid, &num_uuids);
389 prop->len = num_uuids * sizeof(bt_uuid_t);
399 case BT_PROPERTY_REMOTE_VERSION_INFO:
401 bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
403 if(prop->len >= (int)sizeof(bt_remote_version_t))
405 ret = btif_config_get_int(bdstr,
406 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, &info->manufacturer);
409 ret = btif_config_get_int(bdstr,
410 BTIF_STORAGE_PATH_REMOTE_VER_VER, &info->version);
413 ret = btif_config_get_int(bdstr,
414 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, &info->sub_ver);
419 BTIF_TRACE_ERROR("Unknow prop type:%d", prop->type);
425 /*******************************************************************************
427 ** Function btif_in_fetch_bonded_devices
429 ** Description Internal helper function to fetch the bonded devices
432 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
434 *******************************************************************************/
435 static bt_status_t btif_in_fetch_bonded_device(const char *bdstr)
437 BOOLEAN bt_linkkey_file_found=FALSE;
441 size_t size = sizeof(link_key);
442 if(btif_config_get_bin(bdstr, "LinkKey", (uint8_t *)link_key, &size))
445 if(btif_config_get_int(bdstr, "LinkKeyType", &linkkey_type))
447 bt_linkkey_file_found = TRUE;
451 bt_linkkey_file_found = FALSE;
454 #if (BLE_INCLUDED == TRUE)
455 if((btif_in_fetch_bonded_ble_device(bdstr, FALSE, NULL) != BT_STATUS_SUCCESS)
456 && (!bt_linkkey_file_found))
458 BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", bdstr);
459 return BT_STATUS_FAIL;
462 if((!bt_linkkey_file_found))
464 BTIF_TRACE_DEBUG("Remote device:%s, no link key found", bdstr);
465 return BT_STATUS_FAIL;
468 return BT_STATUS_SUCCESS;
471 /*******************************************************************************
473 ** Function btif_in_fetch_bonded_devices
475 ** Description Internal helper function to fetch the bonded devices
478 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
480 *******************************************************************************/
481 static bt_status_t btif_in_fetch_bonded_devices(btif_bonded_devices_t *p_bonded_devices, int add)
483 memset(p_bonded_devices, 0, sizeof(btif_bonded_devices_t));
485 BOOLEAN bt_linkkey_file_found=FALSE;
488 for (const btif_config_section_iter_t *iter = btif_config_section_begin(); iter != btif_config_section_end(); iter = btif_config_section_next(iter)) {
489 const char *name = btif_config_section_name(iter);
490 if (!string_is_bdaddr(name))
493 BTIF_TRACE_DEBUG("Remote device:%s", name);
495 size_t size = sizeof(link_key);
496 if(btif_config_get_bin(name, "LinkKey", link_key, &size))
499 if(btif_config_get_int(name, "LinkKeyType", &linkkey_type))
502 //btif_config_get_int(name, "PinLength", &pin_len))
504 string_to_bdaddr(name, &bd_addr);
507 DEV_CLASS dev_class = {0, 0, 0};
509 if(btif_config_get_int(name, "DevClass", &cod))
510 uint2devclass((UINT32)cod, dev_class);
511 BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0, (UINT8)linkkey_type, 0);
513 #if BLE_INCLUDED == TRUE
514 if (btif_config_get_int(name, "DevType", &device_type) &&
515 (device_type == BT_DEVICE_TYPE_DUMO) )
517 btif_gatts_add_bonded_dev_from_nv(bd_addr.address);
521 bt_linkkey_file_found = TRUE;
522 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
526 #if (BLE_INCLUDED == TRUE)
527 bt_linkkey_file_found = FALSE;
529 BTIF_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid", name);
533 #if (BLE_INCLUDED == TRUE)
534 if(!(btif_in_fetch_bonded_ble_device(name, add, p_bonded_devices)) && (!bt_linkkey_file_found))
536 BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", name);
539 if(!bt_linkkey_file_found)
540 BTIF_TRACE_DEBUG("Remote device:%s, no link key", name);
543 return BT_STATUS_SUCCESS;
546 /************************************************************************************
548 ************************************************************************************/
550 /************************************************************************************
552 ************************************************************************************/
554 /** functions are synchronous.
555 * functions can be called by both internal modules such as BTIF_DM and by external entiries from HAL via BTIF_context_switch
556 * For OUT parameters, caller is expected to provide the memory.
557 * Caller is expected to provide a valid pointer to 'property->value' based on the property->type
559 /*******************************************************************************
561 ** Function btif_storage_get_adapter_property
563 ** Description BTIF storage API - Fetches the adapter property->type
564 ** from NVRAM and fills property->val.
565 ** Caller should provide memory for property->val and
566 ** set the property->val
568 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
569 ** BT_STATUS_FAIL otherwise
571 *******************************************************************************/
572 bt_status_t btif_storage_get_adapter_property(bt_property_t *property)
575 /* Special handling for adapter BD_ADDR and BONDED_DEVICES */
576 if (property->type == BT_PROPERTY_BDADDR)
579 bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)property->val;
580 /* This has been cached in btif. Just fetch it from there */
581 memcpy(bd_addr, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
582 property->len = sizeof(bt_bdaddr_t);
583 return BT_STATUS_SUCCESS;
585 else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES)
587 btif_bonded_devices_t bonded_devices;
589 btif_in_fetch_bonded_devices(&bonded_devices, 0);
591 BTIF_TRACE_DEBUG("%s: Number of bonded devices: %d Property:BT_PROPERTY_ADAPTER_BONDED_DEVICES", __FUNCTION__, bonded_devices.num_devices);
593 if (bonded_devices.num_devices > 0)
595 property->len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
596 memcpy(property->val, bonded_devices.devices, property->len);
599 /* if there are no bonded_devices, then length shall be 0 */
600 return BT_STATUS_SUCCESS;
602 else if (property->type == BT_PROPERTY_UUIDS)
604 /* publish list of local supported services */
605 bt_uuid_t *p_uuid = (bt_uuid_t*)property->val;
606 uint32_t num_uuids = 0;
609 tBTA_SERVICE_MASK service_mask = btif_get_enabled_services_mask();
610 BTIF_TRACE_ERROR("%s service_mask:0x%x", __FUNCTION__, service_mask);
611 for (i=0; i < BTA_MAX_SERVICE_ID; i++)
613 /* This should eventually become a function when more services are enabled */
615 &(tBTA_SERVICE_MASK)(1 << i))
619 case BTA_HFP_SERVICE_ID:
621 uuid16_to_uuid128(UUID_SERVCLASS_AG_HANDSFREE,
625 /* intentional fall through: Send both BFP & HSP UUIDs if HFP is enabled */
626 case BTA_HSP_SERVICE_ID:
628 uuid16_to_uuid128(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,
632 case BTA_A2DP_SOURCE_SERVICE_ID:
634 uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SOURCE,
638 case BTA_HFP_HS_SERVICE_ID:
640 uuid16_to_uuid128(UUID_SERVCLASS_HF_HANDSFREE,
647 property->len = (num_uuids)*sizeof(bt_uuid_t);
648 return BT_STATUS_SUCCESS;
651 /* fall through for other properties */
652 if(!cfg2prop(NULL, property))
654 return btif_dm_get_adapter_property(property);
656 return BT_STATUS_SUCCESS;
659 /*******************************************************************************
661 ** Function btif_storage_set_adapter_property
663 ** Description BTIF storage API - Stores the adapter property
666 ** Returns BT_STATUS_SUCCESS if the store was successful,
667 ** BT_STATUS_FAIL otherwise
669 *******************************************************************************/
670 bt_status_t btif_storage_set_adapter_property(bt_property_t *property)
672 return prop2cfg(NULL, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
675 /*******************************************************************************
677 ** Function btif_storage_get_remote_device_property
679 ** Description BTIF storage API - Fetches the remote device property->type
680 ** from NVRAM and fills property->val.
681 ** Caller should provide memory for property->val and
682 ** set the property->val
684 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
685 ** BT_STATUS_FAIL otherwise
687 *******************************************************************************/
688 bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t *remote_bd_addr,
689 bt_property_t *property)
691 return cfg2prop(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
693 /*******************************************************************************
695 ** Function btif_storage_set_remote_device_property
697 ** Description BTIF storage API - Stores the remote device property
700 ** Returns BT_STATUS_SUCCESS if the store was successful,
701 ** BT_STATUS_FAIL otherwise
703 *******************************************************************************/
704 bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t *remote_bd_addr,
705 bt_property_t *property)
707 return prop2cfg(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
710 /*******************************************************************************
712 ** Function btif_storage_add_remote_device
714 ** Description BTIF storage API - Adds a newly discovered device to NVRAM
715 ** along with the timestamp. Also, stores the various
716 ** properties - RSSI, BDADDR, NAME (if found in EIR)
718 ** Returns BT_STATUS_SUCCESS if the store was successful,
719 ** BT_STATUS_FAIL otherwise
721 *******************************************************************************/
722 bt_status_t btif_storage_add_remote_device(bt_bdaddr_t *remote_bd_addr,
723 uint32_t num_properties,
724 bt_property_t *properties)
727 /* TODO: If writing a property, fails do we go back undo the earlier
728 * written properties? */
729 for (i=0; i < num_properties; i++)
731 /* Ignore the RSSI as this is not stored in DB */
732 if (properties[i].type == BT_PROPERTY_REMOTE_RSSI)
735 /* BD_ADDR for remote device needs special handling as we also store timestamp */
736 if (properties[i].type == BT_PROPERTY_BDADDR)
738 bt_property_t addr_prop;
739 memcpy(&addr_prop, &properties[i], sizeof(bt_property_t));
740 addr_prop.type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP;
741 btif_storage_set_remote_device_property(remote_bd_addr,
746 btif_storage_set_remote_device_property(remote_bd_addr,
750 return BT_STATUS_SUCCESS;
753 /*******************************************************************************
755 ** Function btif_storage_add_bonded_device
757 ** Description BTIF storage API - Adds the newly bonded device to NVRAM
758 ** along with the link-key, Key type and Pin key length
760 ** Returns BT_STATUS_SUCCESS if the store was successful,
761 ** BT_STATUS_FAIL otherwise
763 *******************************************************************************/
765 bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
771 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
772 int ret = btif_config_set_int(bdstr, "LinkKeyType", (int)key_type);
773 ret &= btif_config_set_int(bdstr, "PinLength", (int)pin_length);
774 ret &= btif_config_set_bin(bdstr, "LinkKey", link_key, sizeof(LINK_KEY));
775 /* write bonded info immediately */
777 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
780 /*******************************************************************************
782 ** Function btif_storage_remove_bonded_device
784 ** Description BTIF storage API - Deletes the bonded device from NVRAM
786 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
787 ** BT_STATUS_FAIL otherwise
789 *******************************************************************************/
790 bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
793 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
794 BTIF_TRACE_DEBUG("in bd addr:%s", bdstr);
796 if(btif_config_exist(bdstr, "LinkKeyType"))
797 ret &= btif_config_remove(bdstr, "LinkKeyType");
798 if(btif_config_exist(bdstr, "PinLength"))
799 ret &= btif_config_remove(bdstr, "PinLength");
800 if(btif_config_exist(bdstr, "LinkKey"))
801 ret &= btif_config_remove(bdstr, "LinkKey");
802 /* write bonded info immediately */
804 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
808 /*******************************************************************************
810 ** Function btif_storage_load_bonded_devices
812 ** Description BTIF storage API - Loads all the bonded devices from NVRAM
813 ** and adds to the BTA.
814 ** Additionally, this API also invokes the adaper_properties_cb
815 ** and remote_device_properties_cb for each of the bonded devices.
817 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
819 *******************************************************************************/
820 bt_status_t btif_storage_load_bonded_devices(void)
823 btif_bonded_devices_t bonded_devices;
825 bt_property_t adapter_props[6];
826 uint32_t num_props = 0;
827 bt_property_t remote_properties[8];
829 bt_bdname_t name, alias;
831 uint32_t disc_timeout;
832 bt_bdaddr_t *devices_list;
833 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
834 bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
835 uint32_t cod, devtype;
837 btif_in_fetch_bonded_devices(&bonded_devices, 1);
839 /* Now send the adapter_properties_cb with all adapter_properties */
841 memset(adapter_props, 0, sizeof(adapter_props));
844 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDADDR, &addr, sizeof(addr),
845 adapter_props[num_props]);
849 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDNAME, &name, sizeof(name),
850 adapter_props[num_props]);
854 /* TODO: At the time of BT on, always report the scan mode as 0 irrespective
855 of the scan_mode during the previous enable cycle.
856 This needs to be re-visited as part of the app/stack enable sequence
858 mode = BT_SCAN_MODE_NONE;
859 adapter_props[num_props].type = BT_PROPERTY_ADAPTER_SCAN_MODE;
860 adapter_props[num_props].len = sizeof(mode);
861 adapter_props[num_props].val = &mode;
865 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
866 &disc_timeout, sizeof(disc_timeout),
867 adapter_props[num_props]);
871 devices_list = (bt_bdaddr_t*)malloc(sizeof(bt_bdaddr_t)*bonded_devices.num_devices);
872 adapter_props[num_props].type = BT_PROPERTY_ADAPTER_BONDED_DEVICES;
873 adapter_props[num_props].len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
874 adapter_props[num_props].val = devices_list;
875 for (i=0; i < bonded_devices.num_devices; i++)
877 memcpy(devices_list + i, &bonded_devices.devices[i], sizeof(bt_bdaddr_t));
882 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_UUIDS,
883 local_uuids, sizeof(local_uuids),
884 adapter_props[num_props]);
887 btif_adapter_properties_evt(BT_STATUS_SUCCESS, num_props, adapter_props);
892 BTIF_TRACE_EVENT("%s: %d bonded devices found", __FUNCTION__, bonded_devices.num_devices);
895 for (i = 0; i < bonded_devices.num_devices; i++)
897 bt_bdaddr_t *p_remote_addr;
900 p_remote_addr = &bonded_devices.devices[i];
901 memset(remote_properties, 0, sizeof(remote_properties));
902 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_BDNAME,
904 remote_properties[num_props]);
907 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_REMOTE_FRIENDLY_NAME,
908 &alias, sizeof(alias),
909 remote_properties[num_props]);
912 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_CLASS_OF_DEVICE,
914 remote_properties[num_props]);
917 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_TYPE_OF_DEVICE,
918 &devtype, sizeof(devtype),
919 remote_properties[num_props]);
922 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_UUIDS,
923 remote_uuids, sizeof(remote_uuids),
924 remote_properties[num_props]);
927 btif_remote_properties_evt(BT_STATUS_SUCCESS, p_remote_addr,
928 num_props, remote_properties);
931 return BT_STATUS_SUCCESS;
934 #if (BLE_INCLUDED == TRUE)
936 /*******************************************************************************
938 ** Function btif_storage_add_ble_bonding_key
940 ** Description BTIF storage API - Adds the newly bonded device to NVRAM
941 ** along with the ble-key, Key type and Pin key length
943 ** Returns BT_STATUS_SUCCESS if the store was successful,
944 ** BT_STATUS_FAIL otherwise
946 *******************************************************************************/
948 bt_status_t btif_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
954 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
958 case BTIF_DM_LE_KEY_PENC:
959 name = "LE_KEY_PENC";
961 case BTIF_DM_LE_KEY_PID:
964 case BTIF_DM_LE_KEY_PCSRK:
965 name = "LE_KEY_PCSRK";
967 case BTIF_DM_LE_KEY_LENC:
968 name = "LE_KEY_LENC";
970 case BTIF_DM_LE_KEY_LCSRK:
971 name = "LE_KEY_LCSRK";
974 return BT_STATUS_FAIL;
976 int ret = btif_config_set_bin(bdstr, name, (const uint8_t *)key, key_length);
978 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
981 /*******************************************************************************
983 ** Function btif_storage_get_ble_bonding_key
987 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
988 ** BT_STATUS_FAIL otherwise
990 *******************************************************************************/
991 bt_status_t btif_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
997 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1001 case BTIF_DM_LE_KEY_PENC:
1002 name = "LE_KEY_PENC";
1004 case BTIF_DM_LE_KEY_PID:
1005 name = "LE_KEY_PID";
1007 case BTIF_DM_LE_KEY_PCSRK:
1008 name = "LE_KEY_PCSRK";
1010 case BTIF_DM_LE_KEY_LENC:
1011 name = "LE_KEY_LENC";
1013 case BTIF_DM_LE_KEY_LCSRK:
1014 name = "LE_KEY_LCSRK";
1017 return BT_STATUS_FAIL;
1019 size_t length = key_length;
1020 int ret = btif_config_get_bin(bdstr, name, (uint8_t *)key_value, &length);
1021 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1025 /*******************************************************************************
1027 ** Function btif_storage_remove_ble_keys
1029 ** Description BTIF storage API - Deletes the bonded device from NVRAM
1031 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1032 ** BT_STATUS_FAIL otherwise
1034 *******************************************************************************/
1035 bt_status_t btif_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr)
1038 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1039 BTIF_TRACE_DEBUG(" %s in bd addr:%s",__FUNCTION__, bdstr);
1041 if(btif_config_exist(bdstr, "LE_KEY_PENC"))
1042 ret &= btif_config_remove(bdstr, "LE_KEY_PENC");
1043 if(btif_config_exist(bdstr, "LE_KEY_PID"))
1044 ret &= btif_config_remove(bdstr, "LE_KEY_PID");
1045 if(btif_config_exist(bdstr, "LE_KEY_PCSRK"))
1046 ret &= btif_config_remove(bdstr, "LE_KEY_PCSRK");
1047 if(btif_config_exist(bdstr, "LE_KEY_LENC"))
1048 ret &= btif_config_remove(bdstr, "LE_KEY_LENC");
1049 if(btif_config_exist(bdstr, "LE_KEY_LCSRK"))
1050 ret &= btif_config_remove(bdstr, "LE_KEY_LCSRK");
1052 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1055 /*******************************************************************************
1057 ** Function btif_storage_add_ble_local_key
1059 ** Description BTIF storage API - Adds the ble key to NVRAM
1061 ** Returns BT_STATUS_SUCCESS if the store was successful,
1062 ** BT_STATUS_FAIL otherwise
1064 *******************************************************************************/
1065 bt_status_t btif_storage_add_ble_local_key(char *key,
1072 case BTIF_DM_LE_LOCAL_KEY_IR:
1073 name = "LE_LOCAL_KEY_IR";
1075 case BTIF_DM_LE_LOCAL_KEY_IRK:
1076 name = "LE_LOCAL_KEY_IRK";
1078 case BTIF_DM_LE_LOCAL_KEY_DHK:
1079 name = "LE_LOCAL_KEY_DHK";
1081 case BTIF_DM_LE_LOCAL_KEY_ER:
1082 name = "LE_LOCAL_KEY_ER";
1085 return BT_STATUS_FAIL;
1087 int ret = btif_config_set_bin("Adapter", name, (const uint8_t *)key, key_length);
1089 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1092 /*******************************************************************************
1094 ** Function btif_storage_get_ble_local_key
1098 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
1099 ** BT_STATUS_FAIL otherwise
1101 *******************************************************************************/
1102 bt_status_t btif_storage_get_ble_local_key(UINT8 key_type,
1109 case BTIF_DM_LE_LOCAL_KEY_IR:
1110 name = "LE_LOCAL_KEY_IR";
1112 case BTIF_DM_LE_LOCAL_KEY_IRK:
1113 name = "LE_LOCAL_KEY_IRK";
1115 case BTIF_DM_LE_LOCAL_KEY_DHK:
1116 name = "LE_LOCAL_KEY_DHK";
1118 case BTIF_DM_LE_LOCAL_KEY_ER:
1119 name = "LE_LOCAL_KEY_ER";
1122 return BT_STATUS_FAIL;
1124 size_t length = key_length;
1125 int ret = btif_config_get_bin("Adapter", name, (uint8_t *)key_value, &length);
1126 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1129 /*******************************************************************************
1131 ** Function btif_storage_remove_ble_local_keys
1133 ** Description BTIF storage API - Deletes the bonded device from NVRAM
1135 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1136 ** BT_STATUS_FAIL otherwise
1138 *******************************************************************************/
1139 bt_status_t btif_storage_remove_ble_local_keys(void)
1142 if(btif_config_exist("Adapter", "LE_LOCAL_KEY_IR"))
1143 ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_IR");
1144 if(btif_config_exist("Adapter", "LE_LOCAL_KEY_IRK"))
1145 ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_IRK");
1146 if(btif_config_exist("Adapter", "LE_LOCAL_KEY_DHK"))
1147 ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_DHK");
1148 if(btif_config_exist("Adapter", "LE_LOCAL_KEY_ER"))
1149 ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_ER");
1151 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1154 bt_status_t btif_in_fetch_bonded_ble_device(const char *remote_bd_addr,int add, btif_bonded_devices_t *p_bonded_devices)
1160 bt_bdaddr_t bd_addr;
1161 BD_ADDR bta_bd_addr;
1162 BOOLEAN is_device_added =FALSE;
1163 BOOLEAN key_found = FALSE;
1164 tBTA_LE_KEY_VALUE *p;
1166 if(!btif_config_get_int(remote_bd_addr,"DevType", &device_type))
1167 return BT_STATUS_FAIL;
1168 if(device_type == BT_DEVICE_TYPE_BLE)
1170 BTIF_TRACE_DEBUG("%s %s found a BLE device", __FUNCTION__,remote_bd_addr);
1171 string_to_bdaddr(remote_bd_addr, &bd_addr);
1172 bdcpy(bta_bd_addr, bd_addr.address);
1173 if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS)
1175 return BT_STATUS_FAIL;
1178 memset(buf, 0, sizeof(buf));
1179 if (btif_storage_get_ble_bonding_key(&bd_addr,
1180 BTIF_DM_LE_KEY_PENC,
1182 sizeof(btif_dm_ble_penc_keys_t)) == BT_STATUS_SUCCESS)
1186 if (!is_device_added)
1188 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1189 is_device_added = TRUE;
1191 p = (tBTA_LE_KEY_VALUE *)buf;
1192 for (i=0; i<16; i++)
1194 BTIF_TRACE_DEBUG("penc_key.ltk[%d]=0x%02x",i,p->penc_key.ltk[i]);
1198 BTIF_TRACE_DEBUG("penc_key.rand[%d]=0x%02x",i,p->penc_key.rand[i]);
1200 BTIF_TRACE_DEBUG("p->penc_key.ediv=0x%04x",p->penc_key.ediv);
1201 BTIF_TRACE_DEBUG("p->penc_key.sec_level=0x%02x",p->penc_key.sec_level);
1202 BTIF_TRACE_DEBUG("p->penc_key.key_size=0x%02x",p->penc_key.key_size);
1203 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PENC);
1208 memset(buf, 0, sizeof(buf));
1209 if (btif_storage_get_ble_bonding_key(&bd_addr,
1212 sizeof(btif_dm_ble_pid_keys_t)) == BT_STATUS_SUCCESS)
1216 if (!is_device_added)
1218 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1219 is_device_added = TRUE;
1221 p = (tBTA_LE_KEY_VALUE *)buf;
1222 for (i=0; i<16; i++)
1224 BTIF_TRACE_DEBUG("p->pid_key.irk[%d]=0x%02x"
1225 ,i,p->pid_key.irk[i]);
1227 BTIF_TRACE_DEBUG("p->pid_key.addr_type=%d",p->pid_key.addr_type);
1228 for (i=0; i<BD_ADDR_LEN; i++)
1230 BTIF_TRACE_DEBUG("p->pid_key.static_addr[%d]=%02x"
1231 ,i,p->pid_key.static_addr[i]);
1234 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PID);
1239 memset(buf, 0, sizeof(buf));
1240 if (btif_storage_get_ble_bonding_key(&bd_addr,
1241 BTIF_DM_LE_KEY_PCSRK,
1243 sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1247 if (!is_device_added)
1249 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1250 is_device_added = TRUE;
1253 p = (tBTA_LE_KEY_VALUE *)buf;
1254 for (i=0; i<16; i++)
1256 BTIF_TRACE_DEBUG("p->pcsrk_key.csrk[%d]=0x%02x",i, p->psrk_key.csrk[i]);
1258 BTIF_TRACE_DEBUG("p->pcsrk_key.counter=0x%08x",p->psrk_key.counter);
1259 BTIF_TRACE_DEBUG("p->pcsrk_key.sec_level=0x%02x",p->psrk_key.sec_level);
1261 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PCSRK);
1266 memset(buf, 0, sizeof(buf));
1267 if (btif_storage_get_ble_bonding_key(&bd_addr,
1268 BTIF_DM_LE_KEY_LENC,
1270 sizeof(btif_dm_ble_lenc_keys_t)) == BT_STATUS_SUCCESS)
1274 if (!is_device_added)
1276 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1277 is_device_added = TRUE;
1279 p = (tBTA_LE_KEY_VALUE *)buf;
1280 BTIF_TRACE_DEBUG("p->lenc_key.div=0x%04x",p->lenc_key.div);
1281 BTIF_TRACE_DEBUG("p->lenc_key.key_size=0x%02x",p->lenc_key.key_size);
1282 BTIF_TRACE_DEBUG("p->lenc_key.sec_level=0x%02x",p->lenc_key.sec_level);
1284 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LENC);
1289 memset(buf, 0, sizeof(buf));
1290 if (btif_storage_get_ble_bonding_key(&bd_addr,
1291 BTIF_DM_LE_KEY_LCSRK,
1293 sizeof(btif_dm_ble_lcsrk_keys_t)) == BT_STATUS_SUCCESS)
1297 if (!is_device_added)
1299 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1300 is_device_added = TRUE;
1302 p = (tBTA_LE_KEY_VALUE *)buf;
1303 BTIF_TRACE_DEBUG("p->lcsrk_key.div=0x%04x",p->lcsrk_key.div);
1304 BTIF_TRACE_DEBUG("p->lcsrk_key.counter=0x%08x",p->lcsrk_key.counter);
1305 BTIF_TRACE_DEBUG("p->lcsrk_key.sec_level=0x%02x",p->lcsrk_key.sec_level);
1307 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LCSRK);
1312 /* Fill in the bonded devices */
1313 if (is_device_added)
1315 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
1316 btif_gatts_add_bonded_dev_from_nv(bta_bd_addr);
1320 return BT_STATUS_SUCCESS;
1322 return BT_STATUS_FAIL;
1324 return BT_STATUS_FAIL;
1327 bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1331 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1332 int ret = btif_config_set_int(bdstr, "AddrType", (int)addr_type);
1334 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1337 /*******************************************************************************
1339 ** Function btif_storage_get_remote_addr_type
1341 ** Description BTIF storage API - Fetches the remote addr type
1343 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
1344 ** BT_STATUS_FAIL otherwise
1346 *******************************************************************************/
1347 bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1351 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1352 int ret = btif_config_get_int(bdstr, "AddrType", addr_type);
1353 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1356 /*******************************************************************************
1358 ** Function btif_storage_add_hid_device_info
1360 ** Description BTIF storage API - Adds the hid information of bonded hid devices-to NVRAM
1362 ** Returns BT_STATUS_SUCCESS if the store was successful,
1363 ** BT_STATUS_FAIL otherwise
1365 *******************************************************************************/
1367 bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr,
1368 UINT16 attr_mask, UINT8 sub_class,
1369 UINT8 app_id, UINT16 vendor_id,
1370 UINT16 product_id, UINT16 version,
1371 UINT8 ctry_code, UINT16 ssr_max_latency,
1372 UINT16 ssr_min_tout, UINT16 dl_len, UINT8 *dsc_list)
1375 BTIF_TRACE_DEBUG("btif_storage_add_hid_device_info:");
1376 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1377 btif_config_set_int(bdstr, "HidAttrMask", attr_mask);
1378 btif_config_set_int(bdstr, "HidSubClass", sub_class);
1379 btif_config_set_int(bdstr, "HidAppId", app_id);
1380 btif_config_set_int(bdstr, "HidVendorId", vendor_id);
1381 btif_config_set_int(bdstr, "HidProductId", product_id);
1382 btif_config_set_int(bdstr, "HidVersion", version);
1383 btif_config_set_int(bdstr, "HidCountryCode", ctry_code);
1384 btif_config_set_int(bdstr, "HidSSRMaxLatency", ssr_max_latency);
1385 btif_config_set_int(bdstr, "HidSSRMinTimeout", ssr_min_tout);
1387 btif_config_set_bin(bdstr, "HidDescriptor", dsc_list, dl_len);
1389 return BT_STATUS_SUCCESS;
1392 /*******************************************************************************
1394 ** Function btif_storage_load_bonded_hid_info
1396 ** Description BTIF storage API - Loads hid info for all the bonded devices from NVRAM
1397 ** and adds those devices to the BTA_HH.
1399 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
1401 *******************************************************************************/
1402 bt_status_t btif_storage_load_bonded_hid_info(void)
1404 bt_bdaddr_t bd_addr;
1405 tBTA_HH_DEV_DSCP_INFO dscp_info;
1411 memset(&dscp_info, 0, sizeof(dscp_info));
1412 for (const btif_config_section_iter_t *iter = btif_config_section_begin(); iter != btif_config_section_end(); iter = btif_config_section_next(iter)) {
1413 const char *name = btif_config_section_name(iter);
1414 if (!string_is_bdaddr(name))
1417 BTIF_TRACE_DEBUG("Remote device:%s", name);
1419 if(btif_in_fetch_bonded_device(name) == BT_STATUS_SUCCESS)
1421 if(btif_config_get_int(name, "HidAttrMask", &value))
1423 attr_mask = (uint16_t)value;
1425 btif_config_get_int(name, "HidSubClass", &value);
1426 sub_class = (uint8_t)value;
1428 btif_config_get_int(name, "HidAppId", &value);
1429 app_id = (uint8_t)value;
1431 btif_config_get_int(name, "HidVendorId", &value);
1432 dscp_info.vendor_id = (uint16_t) value;
1434 btif_config_get_int(name, "HidProductId", &value);
1435 dscp_info.product_id = (uint16_t) value;
1437 btif_config_get_int(name, "HidVersion", &value);
1438 dscp_info.version = (uint8_t) value;
1440 btif_config_get_int(name, "HidCountryCode", &value);
1441 dscp_info.ctry_code = (uint8_t) value;
1444 btif_config_get_int(name, "HidSSRMaxLatency", &value);
1445 dscp_info.ssr_max_latency = (uint16_t) value;
1448 btif_config_get_int(name, "HidSSRMinTimeout", &value);
1449 dscp_info.ssr_min_tout = (uint16_t) value;
1451 size_t len = btif_config_get_bin_length(name, "HidDescriptor");
1454 dscp_info.descriptor.dl_len = (uint16_t)len;
1455 dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len);
1456 btif_config_get_bin(name, "HidDescriptor", (uint8_t *)dscp_info.descriptor.dsc_list, &len);
1458 string_to_bdaddr(name, &bd_addr);
1459 // add extracted information to BTA HH
1460 if (btif_hh_add_added_dev(bd_addr,attr_mask))
1462 BTA_HhAddDev(bd_addr.address, attr_mask, sub_class,
1469 if(btif_config_get_int(name, "HidAttrMask", &value))
1471 btif_storage_remove_hid_info(&bd_addr);
1472 string_to_bdaddr(name, &bd_addr);
1477 return BT_STATUS_SUCCESS;
1480 /*******************************************************************************
1482 ** Function btif_storage_remove_hid_info
1484 ** Description BTIF storage API - Deletes the bonded hid device info from NVRAM
1486 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1487 ** BT_STATUS_FAIL otherwise
1489 *******************************************************************************/
1490 bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr)
1495 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1497 btif_config_remove(bdstr, "HidAttrMask");
1498 btif_config_remove(bdstr, "HidSubClass");
1499 btif_config_remove(bdstr, "HidAppId");
1500 btif_config_remove(bdstr, "HidVendorId");
1501 btif_config_remove(bdstr, "HidProductId");
1502 btif_config_remove(bdstr, "HidVersion");
1503 btif_config_remove(bdstr, "HidCountryCode");
1504 btif_config_remove(bdstr, "HidSSRMaxLatency");
1505 btif_config_remove(bdstr, "HidSSRMinTimeout");
1506 btif_config_remove(bdstr, "HidDescriptor");
1508 return BT_STATUS_SUCCESS;
1511 /*******************************************************************************
1513 ** Function btif_storage_read_hl_apps_cb
1515 ** Description BTIF storage API - Read HL application control block from NVRAM
1517 ** Returns BT_STATUS_SUCCESS if the operation was successful,
1518 ** BT_STATUS_FAIL otherwise
1520 *******************************************************************************/
1521 bt_status_t btif_storage_read_hl_apps_cb(char *value, int value_size)
1523 bt_status_t bt_status = BT_STATUS_SUCCESS;
1525 if (!btif_config_exist(BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB))
1527 memset(value, 0, value_size);
1528 if (!btif_config_set_bin(BTIF_STORAGE_HL_APP,BTIF_STORAGE_HL_APP_CB,
1529 (const uint8_t *)value, value_size))
1531 bt_status = BT_STATUS_FAIL;
1540 size_t read_size = value_size;
1541 if (!btif_config_get_bin(BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB,
1542 (uint8_t *)value, &read_size))
1544 bt_status = BT_STATUS_FAIL;
1548 if (read_size != (size_t)value_size)
1550 BTIF_TRACE_ERROR("%s value_size=%d read_size=%d",
1551 __FUNCTION__, value_size, read_size);
1552 bt_status = BT_STATUS_FAIL;
1558 BTIF_TRACE_DEBUG("%s status=%d value_size=%d", __FUNCTION__, bt_status, value_size);
1563 /*******************************************************************************
1565 ** Function btif_storage_load_autopair_device_list
1567 ** Description BTIF storage API - Populates auto pair device list
1569 ** Returns BT_STATUS_SUCCESS if the auto pair blacklist is successfully populated
1570 ** BT_STATUS_FAIL otherwise
1572 *******************************************************************************/
1573 bt_status_t btif_storage_load_autopair_device_list() {
1574 // Configuration has already been loaded. No need to reload.
1575 if (btif_config_has_section(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST)) {
1576 return BT_STATUS_SUCCESS;
1579 static const char *key_names[] = {
1580 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR,
1581 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME,
1582 BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST,
1583 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME,
1584 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR,
1587 config_t *config = config_new(BTIF_AUTO_PAIR_CONF_FILE);
1589 ALOGE("%s failed to open auto pair blacklist conf file '%s'.", __func__, BTIF_AUTO_PAIR_CONF_FILE);
1590 return BT_STATUS_FAIL;
1593 for (size_t i = 0; i < ARRAY_SIZE(key_names); ++i) {
1594 const char *value = config_get_string(config, CONFIG_DEFAULT_SECTION, key_names[i], NULL);
1596 btif_config_set_str(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, key_names[i], value);
1600 config_free(config);
1601 return BT_STATUS_SUCCESS;
1604 /*******************************************************************************
1606 ** Function btif_storage_is_device_autopair_blacklisted
1608 ** Description BTIF storage API Checks if the given device is blacklisted for auto pairing
1610 ** Returns TRUE if the device is found in the auto pair blacklist
1613 *******************************************************************************/
1614 BOOLEAN btif_storage_is_device_autopair_blacklisted(bt_bdaddr_t *remote_bd_addr)
1621 char value[BTIF_STORAGE_MAX_LINE_SZ];
1622 int value_size = sizeof(value);
1624 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1626 /* Consider only Lower Address Part from BD Address */
1629 if(btif_config_get_str(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1630 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR, value, &value_size))
1632 if (strcasestr(value,bdstr) != NULL)
1636 dev_name_str = BTM_SecReadDevName((remote_bd_addr->address));
1638 if (dev_name_str != NULL)
1640 value_size = sizeof(value);
1641 if(btif_config_get_str(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1642 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME, value, &value_size))
1644 if (strstr(value,dev_name_str) != NULL)
1647 value_size = sizeof(value);
1648 if(btif_config_get_str(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1649 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME, value, &value_size))
1651 token = strtok(value, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1652 while (token != NULL)
1654 if (strstr(dev_name_str, token) != NULL)
1657 token = strtok(NULL, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1661 if(btif_config_get_str(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1662 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, value, &value_size))
1664 if (strstr(value,bdstr) != NULL)
1670 /*******************************************************************************
1672 ** Function btif_storage_add_device_to_autopair_blacklist
1674 ** Description BTIF storage API - Add a remote device to the auto pairing blacklist
1676 ** Returns BT_STATUS_SUCCESS if the device is successfully added to the auto pair blacklist
1677 ** BT_STATUS_FAIL otherwise
1679 *******************************************************************************/
1680 bt_status_t btif_storage_add_device_to_autopair_blacklist(bt_bdaddr_t *remote_bd_addr)
1684 char linebuf[BTIF_STORAGE_MAX_LINE_SZ+20];
1685 char input_value [20];
1687 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1688 strlcpy(input_value, (char*)bdstr, sizeof(input_value));
1689 strlcat(input_value,BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR, sizeof(input_value));
1691 int line_size = sizeof(linebuf);
1692 if(btif_config_get_str(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1693 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf, &line_size))
1695 /* Append this address to the dynamic List of BD address */
1696 strncat (linebuf, input_value, BTIF_STORAGE_MAX_LINE_SZ);
1700 strncpy( linebuf,input_value, BTIF_STORAGE_MAX_LINE_SZ);
1703 /* Write back the key value */
1704 ret = btif_config_set_str(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1705 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf);
1707 return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1710 /*******************************************************************************
1712 ** Function btif_storage_is_fixed_pin_zeros_keyboard
1714 ** Description BTIF storage API - checks if this device has fixed PIN key device list
1716 ** Returns TRUE if the device is found in the fixed pin keyboard device list
1719 *******************************************************************************/
1720 BOOLEAN btif_storage_is_fixed_pin_zeros_keyboard(bt_bdaddr_t *remote_bd_addr)
1726 char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1728 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1730 /*consider on LAP part of BDA string*/
1733 int line_size = sizeof(linebuf);
1734 if(btif_config_get_str(BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1735 BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST, linebuf, &line_size))
1737 if (strcasestr(linebuf,bdstr) != NULL)
1744 /*******************************************************************************
1746 ** Function btif_storage_set_dmt_support_type
1748 ** Description Sets DMT support status for a remote device
1750 ** Returns BT_STATUS_SUCCESS if config update is successful
1751 ** BT_STATUS_FAIL otherwise
1753 *******************************************************************************/
1755 bt_status_t btif_storage_set_dmt_support_type(const bt_bdaddr_t *remote_bd_addr,
1756 BOOLEAN dmt_supported)
1759 bdstr_t bdstr = {0};
1762 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1766 BTIF_TRACE_ERROR("%s NULL BD Address", __FUNCTION__);
1767 return BT_STATUS_FAIL;
1770 ret = btif_config_set_int(bdstr,"DMTSupported", (int)dmt_supported);
1771 return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1775 /*******************************************************************************
1777 ** Function btif_storage_is_dmt_supported_device
1779 ** Description checks if a device supports Dual mode topology
1781 ** Returns TRUE if remote address is valid and supports DMT else FALSE
1783 *******************************************************************************/
1785 BOOLEAN btif_storage_is_dmt_supported_device(const bt_bdaddr_t *remote_bd_addr)
1787 int dmt_supported = 0;
1788 bdstr_t bdstr = {0};
1790 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1794 bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1798 BTIF_TRACE_ERROR("%s NULL BD Address", __FUNCTION__);
1802 btif_config_get_int(bdstr,"DMTSupported", &dmt_supported);
1804 return dmt_supported == 1 ? TRUE:FALSE;