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(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 bd2str(remote_bd_addr, &bdstr);
223 BTIF_TRACE_DEBUG3("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_ERROR2("property type:%d, len:%d is invalid", prop->type, prop->len);
232 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
233 btif_config_set_int("Remote", bdstr,
234 BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int)time(NULL));
235 static const char* exclude_filter[] =
236 {"LinkKey", "LE_KEY_PENC", "LE_KEY_PID", "LE_KEY_PCSRK", "LE_KEY_LENC", "LE_KEY_LCSRK"};
237 btif_config_filter_remove("Remote", exclude_filter, sizeof(exclude_filter)/sizeof(char*),
238 BTIF_STORAGE_MAX_ALLOWED_REMOTE_DEVICE);
240 case BT_PROPERTY_BDNAME:
241 strncpy(value, (char*)prop->val, prop->len);
242 value[prop->len]='\0';
244 btif_config_set_str("Remote", bdstr,
245 BTIF_STORAGE_PATH_REMOTE_NAME, value);
246 else btif_config_set_str("Local", "Adapter",
247 BTIF_STORAGE_KEY_ADAPTER_NAME, value);
248 /* save name immediately */
251 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
252 strncpy(value, (char*)prop->val, prop->len);
253 value[prop->len]='\0';
254 btif_config_set_str("Remote", bdstr, BTIF_STORAGE_PATH_REMOTE_ALIASE, value);
256 case BT_PROPERTY_ADAPTER_SCAN_MODE:
257 btif_config_set_int("Local", "Adapter",
258 BTIF_STORAGE_KEY_ADAPTER_SCANMODE, *(int*)prop->val);
260 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
261 btif_config_set_int("Local", "Adapter",
262 BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, *(int*)prop->val);
264 case BT_PROPERTY_CLASS_OF_DEVICE:
265 btif_config_set_int("Remote", bdstr,
266 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, *(int*)prop->val);
268 case BT_PROPERTY_TYPE_OF_DEVICE:
269 btif_config_set_int("Remote", bdstr,
270 BTIF_STORAGE_PATH_REMOTE_DEVTYPE, *(int*)prop->val);
272 case BT_PROPERTY_UUIDS:
277 for (i=0; i < (prop->len)/sizeof(bt_uuid_t); i++)
279 bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val + i;
280 memset(buf, 0, sizeof(buf));
281 uuid_to_string(p_uuid, buf);
283 //strcat(value, ";");
286 btif_config_set_str("Remote", bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value);
290 case BT_PROPERTY_REMOTE_VERSION_INFO:
292 bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
297 btif_config_set_int("Remote", bdstr,
298 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, info->manufacturer);
299 btif_config_set_int("Remote", bdstr,
300 BTIF_STORAGE_PATH_REMOTE_VER_VER, info->version);
301 btif_config_set_int("Remote", bdstr,
302 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, info->sub_ver);
307 BTIF_TRACE_ERROR1("Unknow prop type:%d", prop->type);
312 static int cfg2prop(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
316 bd2str(remote_bd_addr, &bdstr);
317 BTIF_TRACE_DEBUG3("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
320 BTIF_TRACE_ERROR2("property type:%d, len:%d is invalid", prop->type, prop->len);
326 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
327 if(prop->len >= (int)sizeof(int))
328 ret = btif_config_get_int("Remote", bdstr,
329 BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int*)prop->val);
331 case BT_PROPERTY_BDNAME:
335 ret = btif_config_get_str("Remote", bdstr,
336 BTIF_STORAGE_PATH_REMOTE_NAME, (char*)prop->val, &len);
337 else ret = btif_config_get_str("Local", "Adapter",
338 BTIF_STORAGE_KEY_ADAPTER_NAME, (char*)prop->val, &len);
339 if(ret && len && len <= prop->len)
348 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
351 ret = btif_config_get_str("Remote", bdstr,
352 BTIF_STORAGE_PATH_REMOTE_ALIASE, (char*)prop->val, &len);
353 if(ret && len && len <= prop->len)
362 case BT_PROPERTY_ADAPTER_SCAN_MODE:
363 if(prop->len >= (int)sizeof(int))
364 ret = btif_config_get_int("Local", "Adapter",
365 BTIF_STORAGE_KEY_ADAPTER_SCANMODE, (int*)prop->val);
367 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
368 if(prop->len >= (int)sizeof(int))
369 ret = btif_config_get_int("Local", "Adapter",
370 BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, (int*)prop->val);
372 case BT_PROPERTY_CLASS_OF_DEVICE:
373 if(prop->len >= (int)sizeof(int))
374 ret = btif_config_get_int("Remote", bdstr,
375 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, (int*)prop->val);
377 case BT_PROPERTY_TYPE_OF_DEVICE:
378 if(prop->len >= (int)sizeof(int))
379 ret = btif_config_get_int("Remote",
380 bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTYPE, (int*)prop->val);
382 case BT_PROPERTY_UUIDS:
385 int size = sizeof(value);
386 if(btif_config_get_str("Remote", bdstr,
387 BTIF_STORAGE_PATH_REMOTE_SERVICE, value, &size))
389 bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val;
390 uint32_t num_uuids = 0;
391 btif_in_split_uuids_string_to_list(value, p_uuid, &num_uuids);
392 prop->len = num_uuids * sizeof(bt_uuid_t);
402 case BT_PROPERTY_REMOTE_VERSION_INFO:
404 bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
406 if(prop->len >= (int)sizeof(bt_remote_version_t))
408 ret = btif_config_get_int("Remote", bdstr,
409 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, &info->manufacturer);
412 ret = btif_config_get_int("Remote", bdstr,
413 BTIF_STORAGE_PATH_REMOTE_VER_VER, &info->version);
416 ret = btif_config_get_int("Remote", bdstr,
417 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, &info->sub_ver);
422 BTIF_TRACE_ERROR1("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_devices(btif_bonded_devices_t *p_bonded_devices, int add)
441 BTIF_TRACE_DEBUG1("in add:%d", add);
442 memset(p_bonded_devices, 0, sizeof(btif_bonded_devices_t));
444 char kname[128], vname[128];
447 kname_size = sizeof(kname);
450 BOOLEAN bt_linkkey_file_found=FALSE;
455 kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
456 BTIF_TRACE_DEBUG2("Remote device:%s, size:%d", kname, kname_size);
457 int type = BTIF_CFG_TYPE_BIN;
459 int size = sizeof(link_key);
460 if(btif_config_get("Remote", kname, "LinkKey", (char*)link_key, &size, &type))
463 if(btif_config_get_int("Remote", kname, "LinkKeyType", &linkkey_type))
466 //btif_config_get_int("Remote", kname, "PinLength", &pin_len))
468 str2bd(kname, &bd_addr);
471 DEV_CLASS dev_class = {0, 0, 0};
473 if(btif_config_get_int("Remote", kname, "DevClass", &cod))
474 uint2devclass((UINT32)cod, dev_class);
475 BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0, (UINT8)linkkey_type, 0);
477 #if BLE_INCLUDED == TRUE
478 if (btif_config_get_int("Remote", kname, "DevType", &device_type) &&
479 (device_type == BT_DEVICE_TYPE_DUMO) )
481 btif_gatts_add_bonded_dev_from_nv(bd_addr.address);
485 bt_linkkey_file_found = TRUE;
486 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
490 #if (BLE_INCLUDED == TRUE)
491 bt_linkkey_file_found = FALSE;
493 BTIF_TRACE_ERROR1("bounded device:%s, LinkKeyType or PinLength is invalid", kname);
497 #if (BLE_INCLUDED == TRUE)
498 if(!(btif_in_fetch_bonded_ble_device(kname,add, p_bonded_devices)) && (!bt_linkkey_file_found))
500 BTIF_TRACE_DEBUG1("Remote device:%s, no link key or ble key found", kname);
503 if(!bt_linkkey_file_found)
504 BTIF_TRACE_DEBUG1("Remote device:%s, no link key", kname);
506 kname_size = sizeof(kname);
509 BTIF_TRACE_DEBUG0("out");
510 return BT_STATUS_SUCCESS;
513 /************************************************************************************
515 ************************************************************************************/
517 /************************************************************************************
519 ************************************************************************************/
521 /** functions are synchronous.
522 * functions can be called by both internal modules such as BTIF_DM and by external entiries from HAL via BTIF_context_switch
523 * For OUT parameters, caller is expected to provide the memory.
524 * Caller is expected to provide a valid pointer to 'property->value' based on the property->type
526 /*******************************************************************************
528 ** Function btif_storage_get_adapter_property
530 ** Description BTIF storage API - Fetches the adapter property->type
531 ** from NVRAM and fills property->val.
532 ** Caller should provide memory for property->val and
533 ** set the property->val
535 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
536 ** BT_STATUS_FAIL otherwise
538 *******************************************************************************/
539 bt_status_t btif_storage_get_adapter_property(bt_property_t *property)
542 /* Special handling for adapter BD_ADDR and BONDED_DEVICES */
543 if (property->type == BT_PROPERTY_BDADDR)
546 bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)property->val;
547 /* This has been cached in btif. Just fetch it from there */
548 memcpy(bd_addr, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
549 property->len = sizeof(bt_bdaddr_t);
550 return BT_STATUS_SUCCESS;
552 else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES)
554 btif_bonded_devices_t bonded_devices;
556 btif_in_fetch_bonded_devices(&bonded_devices, 0);
558 BTIF_TRACE_DEBUG2("%s: Number of bonded devices: %d Property:BT_PROPERTY_ADAPTER_BONDED_DEVICES", __FUNCTION__, bonded_devices.num_devices);
560 if (bonded_devices.num_devices > 0)
562 property->len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
563 memcpy(property->val, bonded_devices.devices, property->len);
566 /* if there are no bonded_devices, then length shall be 0 */
567 return BT_STATUS_SUCCESS;
569 else if (property->type == BT_PROPERTY_UUIDS)
571 /* publish list of local supported services */
572 bt_uuid_t *p_uuid = (bt_uuid_t*)property->val;
573 uint32_t num_uuids = 0;
576 tBTA_SERVICE_MASK service_mask = btif_get_enabled_services_mask();
577 BTIF_TRACE_ERROR2("%s service_mask:0x%x", __FUNCTION__, service_mask);
578 for (i=0; i < BTA_MAX_SERVICE_ID; i++)
580 /* This should eventually become a function when more services are enabled */
582 &(tBTA_SERVICE_MASK)(1 << i))
586 case BTA_HFP_SERVICE_ID:
588 uuid16_to_uuid128(UUID_SERVCLASS_AG_HANDSFREE,
592 /* intentional fall through: Send both BFP & HSP UUIDs if HFP is enabled */
593 case BTA_HSP_SERVICE_ID:
595 uuid16_to_uuid128(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,
599 case BTA_A2DP_SERVICE_ID:
601 uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SOURCE,
605 case BTA_HFP_HS_SERVICE_ID:
607 uuid16_to_uuid128(UUID_SERVCLASS_HF_HANDSFREE,
614 property->len = (num_uuids)*sizeof(bt_uuid_t);
615 return BT_STATUS_SUCCESS;
618 /* fall through for other properties */
619 if(!cfg2prop(NULL, property))
621 return btif_dm_get_adapter_property(property);
623 return BT_STATUS_SUCCESS;
626 /*******************************************************************************
628 ** Function btif_storage_set_adapter_property
630 ** Description BTIF storage API - Stores the adapter property
633 ** Returns BT_STATUS_SUCCESS if the store was successful,
634 ** BT_STATUS_FAIL otherwise
636 *******************************************************************************/
637 bt_status_t btif_storage_set_adapter_property(bt_property_t *property)
639 return prop2cfg(NULL, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
642 /*******************************************************************************
644 ** Function btif_storage_get_remote_device_property
646 ** Description BTIF storage API - Fetches the remote device property->type
647 ** from NVRAM and fills property->val.
648 ** Caller should provide memory for property->val and
649 ** set the property->val
651 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
652 ** BT_STATUS_FAIL otherwise
654 *******************************************************************************/
655 bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t *remote_bd_addr,
656 bt_property_t *property)
658 return cfg2prop(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
660 /*******************************************************************************
662 ** Function btif_storage_set_remote_device_property
664 ** Description BTIF storage API - Stores the remote device property
667 ** Returns BT_STATUS_SUCCESS if the store was successful,
668 ** BT_STATUS_FAIL otherwise
670 *******************************************************************************/
671 bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t *remote_bd_addr,
672 bt_property_t *property)
674 return prop2cfg(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
677 /*******************************************************************************
679 ** Function btif_storage_add_remote_device
681 ** Description BTIF storage API - Adds a newly discovered device to NVRAM
682 ** along with the timestamp. Also, stores the various
683 ** properties - RSSI, BDADDR, NAME (if found in EIR)
685 ** Returns BT_STATUS_SUCCESS if the store was successful,
686 ** BT_STATUS_FAIL otherwise
688 *******************************************************************************/
689 bt_status_t btif_storage_add_remote_device(bt_bdaddr_t *remote_bd_addr,
690 uint32_t num_properties,
691 bt_property_t *properties)
694 /* TODO: If writing a property, fails do we go back undo the earlier
695 * written properties? */
696 for (i=0; i < num_properties; i++)
698 /* Ignore the RSSI as this is not stored in DB */
699 if (properties[i].type == BT_PROPERTY_REMOTE_RSSI)
702 /* BD_ADDR for remote device needs special handling as we also store timestamp */
703 if (properties[i].type == BT_PROPERTY_BDADDR)
705 bt_property_t addr_prop;
706 memcpy(&addr_prop, &properties[i], sizeof(bt_property_t));
707 addr_prop.type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP;
708 btif_storage_set_remote_device_property(remote_bd_addr,
713 btif_storage_set_remote_device_property(remote_bd_addr,
717 return BT_STATUS_SUCCESS;
720 /*******************************************************************************
722 ** Function btif_storage_add_bonded_device
724 ** Description BTIF storage API - Adds the newly bonded device to NVRAM
725 ** along with the link-key, Key type and Pin key length
727 ** Returns BT_STATUS_SUCCESS if the store was successful,
728 ** BT_STATUS_FAIL otherwise
730 *******************************************************************************/
732 bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
738 bd2str(remote_bd_addr, &bdstr);
739 int ret = btif_config_set_int("Remote", bdstr, "LinkKeyType", (int)key_type);
740 ret &= btif_config_set_int("Remote", bdstr, "PinLength", (int)pin_length);
741 ret &= btif_config_set("Remote", bdstr, "LinkKey", (const char*)link_key, sizeof(LINK_KEY), BTIF_CFG_TYPE_BIN);
742 /* write bonded info immediately */
744 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
747 /*******************************************************************************
749 ** Function btif_storage_remove_bonded_device
751 ** Description BTIF storage API - Deletes the bonded device from NVRAM
753 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
754 ** BT_STATUS_FAIL otherwise
756 *******************************************************************************/
757 bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
760 bd2str(remote_bd_addr, &bdstr);
761 BTIF_TRACE_DEBUG1("in bd addr:%s", bdstr);
763 if(btif_config_exist("Remote", bdstr, "LinkKeyType"))
764 ret &= btif_config_remove("Remote", bdstr, "LinkKeyType");
765 if(btif_config_exist("Remote", bdstr, "PinLength"))
766 ret &= btif_config_remove("Remote", bdstr, "PinLength");
767 if(btif_config_exist("Remote", bdstr, "LinkKey"))
768 ret &= btif_config_remove("Remote", bdstr, "LinkKey");
769 /* write bonded info immediately */
771 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
775 /*******************************************************************************
777 ** Function btif_storage_load_bonded_devices
779 ** Description BTIF storage API - Loads all the bonded devices from NVRAM
780 ** and adds to the BTA.
781 ** Additionally, this API also invokes the adaper_properties_cb
782 ** and remote_device_properties_cb for each of the bonded devices.
784 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
786 *******************************************************************************/
787 bt_status_t btif_storage_load_bonded_devices(void)
790 btif_bonded_devices_t bonded_devices;
792 bt_property_t adapter_props[6];
793 uint32_t num_props = 0;
794 bt_property_t remote_properties[8];
796 bt_bdname_t name, alias;
798 uint32_t disc_timeout;
799 bt_bdaddr_t *devices_list;
800 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
801 bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
802 uint32_t cod, devtype;
804 btif_in_fetch_bonded_devices(&bonded_devices, 1);
806 /* Now send the adapter_properties_cb with all adapter_properties */
808 memset(adapter_props, 0, sizeof(adapter_props));
811 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDADDR, &addr, sizeof(addr),
812 adapter_props[num_props]);
816 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDNAME, &name, sizeof(name),
817 adapter_props[num_props]);
821 /* TODO: At the time of BT on, always report the scan mode as 0 irrespective
822 of the scan_mode during the previous enable cycle.
823 This needs to be re-visited as part of the app/stack enable sequence
825 mode = BT_SCAN_MODE_NONE;
826 adapter_props[num_props].type = BT_PROPERTY_ADAPTER_SCAN_MODE;
827 adapter_props[num_props].len = sizeof(mode);
828 adapter_props[num_props].val = &mode;
832 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
833 &disc_timeout, sizeof(disc_timeout),
834 adapter_props[num_props]);
838 devices_list = (bt_bdaddr_t*)malloc(sizeof(bt_bdaddr_t)*bonded_devices.num_devices);
839 adapter_props[num_props].type = BT_PROPERTY_ADAPTER_BONDED_DEVICES;
840 adapter_props[num_props].len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
841 adapter_props[num_props].val = devices_list;
842 for (i=0; i < bonded_devices.num_devices; i++)
844 memcpy(devices_list + i, &bonded_devices.devices[i], sizeof(bt_bdaddr_t));
849 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_UUIDS,
850 local_uuids, sizeof(local_uuids),
851 adapter_props[num_props]);
854 btif_adapter_properties_evt(BT_STATUS_SUCCESS, num_props, adapter_props);
859 BTIF_TRACE_EVENT2("%s: %d bonded devices found", __FUNCTION__, bonded_devices.num_devices);
862 for (i = 0; i < bonded_devices.num_devices; i++)
864 bt_bdaddr_t *p_remote_addr;
867 p_remote_addr = &bonded_devices.devices[i];
868 memset(remote_properties, 0, sizeof(remote_properties));
869 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_BDNAME,
871 remote_properties[num_props]);
874 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_REMOTE_FRIENDLY_NAME,
875 &alias, sizeof(alias),
876 remote_properties[num_props]);
879 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_CLASS_OF_DEVICE,
881 remote_properties[num_props]);
884 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_TYPE_OF_DEVICE,
885 &devtype, sizeof(devtype),
886 remote_properties[num_props]);
889 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_UUIDS,
890 remote_uuids, sizeof(remote_uuids),
891 remote_properties[num_props]);
894 btif_remote_properties_evt(BT_STATUS_SUCCESS, p_remote_addr,
895 num_props, remote_properties);
898 return BT_STATUS_SUCCESS;
901 #if (BLE_INCLUDED == TRUE)
903 /*******************************************************************************
905 ** Function btif_storage_add_ble_bonding_key
907 ** Description BTIF storage API - Adds the newly bonded device to NVRAM
908 ** along with the ble-key, Key type and Pin key length
910 ** Returns BT_STATUS_SUCCESS if the store was successful,
911 ** BT_STATUS_FAIL otherwise
913 *******************************************************************************/
915 bt_status_t btif_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
921 bd2str(remote_bd_addr, &bdstr);
925 case BTIF_DM_LE_KEY_PENC:
926 name = "LE_KEY_PENC";
928 case BTIF_DM_LE_KEY_PID:
931 case BTIF_DM_LE_KEY_PCSRK:
932 name = "LE_KEY_PCSRK";
934 case BTIF_DM_LE_KEY_LENC:
935 name = "LE_KEY_LENC";
937 case BTIF_DM_LE_KEY_LCSRK:
938 name = "LE_KEY_LCSRK";
941 return BT_STATUS_FAIL;
943 int ret = btif_config_set("Remote", bdstr, name, (const char*)key, (int)key_length, BTIF_CFG_TYPE_BIN);
945 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
948 /*******************************************************************************
950 ** Function btif_storage_get_ble_bonding_key
954 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
955 ** BT_STATUS_FAIL otherwise
957 *******************************************************************************/
958 bt_status_t btif_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
964 bd2str(remote_bd_addr, &bdstr);
966 int type = BTIF_CFG_TYPE_BIN;
969 case BTIF_DM_LE_KEY_PENC:
970 name = "LE_KEY_PENC";
972 case BTIF_DM_LE_KEY_PID:
975 case BTIF_DM_LE_KEY_PCSRK:
976 name = "LE_KEY_PCSRK";
978 case BTIF_DM_LE_KEY_LENC:
979 name = "LE_KEY_LENC";
981 case BTIF_DM_LE_KEY_LCSRK:
982 name = "LE_KEY_LCSRK";
985 return BT_STATUS_FAIL;
987 int ret = btif_config_get("Remote", bdstr, name, key_value, &key_length, &type);
988 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
992 /*******************************************************************************
994 ** Function btif_storage_remove_ble_keys
996 ** Description BTIF storage API - Deletes the bonded device from NVRAM
998 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
999 ** BT_STATUS_FAIL otherwise
1001 *******************************************************************************/
1002 bt_status_t btif_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr)
1005 bd2str(remote_bd_addr, &bdstr);
1006 BTIF_TRACE_DEBUG2(" %s in bd addr:%s",__FUNCTION__, bdstr);
1008 if(btif_config_exist("Remote", bdstr, "LE_KEY_PENC"))
1009 ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PENC");
1010 if(btif_config_exist("Remote", bdstr, "LE_KEY_PID"))
1011 ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PID");
1012 if(btif_config_exist("Remote", bdstr, "LE_KEY_PCSRK"))
1013 ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PCSRK");
1014 if(btif_config_exist("Remote", bdstr, "LE_KEY_LENC"))
1015 ret &= btif_config_remove("Remote", bdstr, "LE_KEY_LENC");
1016 if(btif_config_exist("Remote", bdstr, "LE_KEY_LCSRK"))
1017 ret &= btif_config_remove("Remote", bdstr, "LE_KEY_LCSRK");
1019 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1022 /*******************************************************************************
1024 ** Function btif_storage_add_ble_local_key
1026 ** Description BTIF storage API - Adds the ble key to NVRAM
1028 ** Returns BT_STATUS_SUCCESS if the store was successful,
1029 ** BT_STATUS_FAIL otherwise
1031 *******************************************************************************/
1032 bt_status_t btif_storage_add_ble_local_key(char *key,
1039 case BTIF_DM_LE_LOCAL_KEY_IR:
1040 name = "LE_LOCAL_KEY_IR";
1042 case BTIF_DM_LE_LOCAL_KEY_IRK:
1043 name = "LE_LOCAL_KEY_IRK";
1045 case BTIF_DM_LE_LOCAL_KEY_DHK:
1046 name = "LE_LOCAL_KEY_DHK";
1048 case BTIF_DM_LE_LOCAL_KEY_ER:
1049 name = "LE_LOCAL_KEY_ER";
1052 return BT_STATUS_FAIL;
1054 int ret = btif_config_set("Local", "Adapter", name, (const char*)key, key_length, BTIF_CFG_TYPE_BIN);
1056 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1059 /*******************************************************************************
1061 ** Function btif_storage_get_ble_local_key
1065 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
1066 ** BT_STATUS_FAIL otherwise
1068 *******************************************************************************/
1069 bt_status_t btif_storage_get_ble_local_key(UINT8 key_type,
1074 int type = BTIF_CFG_TYPE_BIN;
1077 case BTIF_DM_LE_LOCAL_KEY_IR:
1078 name = "LE_LOCAL_KEY_IR";
1080 case BTIF_DM_LE_LOCAL_KEY_IRK:
1081 name = "LE_LOCAL_KEY_IRK";
1083 case BTIF_DM_LE_LOCAL_KEY_DHK:
1084 name = "LE_LOCAL_KEY_DHK";
1086 case BTIF_DM_LE_LOCAL_KEY_ER:
1087 name = "LE_LOCAL_KEY_ER";
1090 return BT_STATUS_FAIL;
1092 int ret = btif_config_get("Local", "Adapter", name, key_value, &key_length, &type);
1093 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1097 /*******************************************************************************
1099 ** Function btif_storage_remove_ble_local_keys
1101 ** Description BTIF storage API - Deletes the bonded device from NVRAM
1103 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1104 ** BT_STATUS_FAIL otherwise
1106 *******************************************************************************/
1107 bt_status_t btif_storage_remove_ble_local_keys(void)
1110 if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_IR"))
1111 ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_IR");
1112 if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_IRK"))
1113 ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_IRK");
1114 if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_DHK"))
1115 ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_DHK");
1116 if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_ER"))
1117 ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_ER");
1119 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1122 bt_status_t btif_in_fetch_bonded_ble_device(char *remote_bd_addr,int add, btif_bonded_devices_t *p_bonded_devices)
1128 bt_bdaddr_t bd_addr;
1129 BD_ADDR bta_bd_addr;
1130 BOOLEAN is_device_added =FALSE;
1131 tBTA_LE_KEY_VALUE *p;
1133 if(!btif_config_get_int("Remote", remote_bd_addr,"DevType", &device_type))
1134 return BT_STATUS_FAIL;
1135 if(device_type == BT_DEVICE_TYPE_BLE)
1137 BTIF_TRACE_DEBUG2("%s %s found a BLE device", __FUNCTION__,remote_bd_addr);
1138 str2bd(remote_bd_addr, &bd_addr);
1139 bdcpy(bta_bd_addr, bd_addr.address);
1140 if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS)
1142 return BT_STATUS_FAIL;
1145 memset(buf, 0, sizeof(buf));
1146 if (btif_storage_get_ble_bonding_key(&bd_addr,
1147 BTIF_DM_LE_KEY_PENC,
1149 sizeof(btif_dm_ble_penc_keys_t)) == BT_STATUS_SUCCESS)
1153 if (!is_device_added)
1155 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1156 is_device_added = TRUE;
1158 p = (tBTA_LE_KEY_VALUE *)buf;
1159 for (i=0; i<16; i++)
1161 BTIF_TRACE_DEBUG2("penc_key.ltk[%d]=0x%02x",i,p->penc_key.ltk[i]);
1165 BTIF_TRACE_DEBUG2("penc_key.rand[%d]=0x%02x",i,p->penc_key.rand[i]);
1167 BTIF_TRACE_DEBUG1("p->penc_key.ediv=0x%04x",p->penc_key.ediv);
1168 BTIF_TRACE_DEBUG1("p->penc_key.sec_level=0x%02x",p->penc_key.sec_level);
1169 BTIF_TRACE_DEBUG1("p->penc_key.key_size=0x%02x",p->penc_key.key_size);
1170 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PENC);
1174 memset(buf, 0, sizeof(buf));
1175 if (btif_storage_get_ble_bonding_key(&bd_addr,
1178 sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1182 if (!is_device_added)
1184 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1185 is_device_added = TRUE;
1187 p = (tBTA_LE_KEY_VALUE *)buf;
1188 for (i=0; i<16; i++)
1190 BTIF_TRACE_DEBUG2("p->pid_key[%d]=0x%02x",i,p->pid_key.irk[i]);
1193 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PID);
1197 memset(buf, 0, sizeof(buf));
1198 if (btif_storage_get_ble_bonding_key(&bd_addr,
1199 BTIF_DM_LE_KEY_PCSRK,
1201 sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1205 if (!is_device_added)
1207 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1208 is_device_added = TRUE;
1211 p = (tBTA_LE_KEY_VALUE *)buf;
1212 for (i=0; i<16; i++)
1214 BTIF_TRACE_DEBUG2("p->pcsrk_key.csrk[%d]=0x%02x",i, p->psrk_key.csrk[i]);
1216 BTIF_TRACE_DEBUG1("p->pcsrk_key.counter=0x%08x",p->psrk_key.counter);
1217 BTIF_TRACE_DEBUG1("p->pcsrk_key.sec_level=0x%02x",p->psrk_key.sec_level);
1219 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PCSRK);
1223 memset(buf, 0, sizeof(buf));
1224 if (btif_storage_get_ble_bonding_key(&bd_addr,
1225 BTIF_DM_LE_KEY_LENC,
1227 sizeof(btif_dm_ble_lenc_keys_t)) == BT_STATUS_SUCCESS)
1231 if (!is_device_added)
1233 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1234 is_device_added = TRUE;
1236 p = (tBTA_LE_KEY_VALUE *)buf;
1237 BTIF_TRACE_DEBUG1("p->lenc_key.div=0x%04x",p->lenc_key.div);
1238 BTIF_TRACE_DEBUG1("p->lenc_key.key_size=0x%02x",p->lenc_key.key_size);
1239 BTIF_TRACE_DEBUG1("p->lenc_key.sec_level=0x%02x",p->lenc_key.sec_level);
1241 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LENC);
1245 memset(buf, 0, sizeof(buf));
1246 if (btif_storage_get_ble_bonding_key(&bd_addr,
1247 BTIF_DM_LE_KEY_LCSRK,
1249 sizeof(btif_dm_ble_lcsrk_keys_t)) == BT_STATUS_SUCCESS)
1253 if (!is_device_added)
1255 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1256 is_device_added = TRUE;
1258 p = (tBTA_LE_KEY_VALUE *)buf;
1259 BTIF_TRACE_DEBUG1("p->lcsrk_key.div=0x%04x",p->lcsrk_key.div);
1260 BTIF_TRACE_DEBUG1("p->lcsrk_key.counter=0x%08x",p->lcsrk_key.counter);
1261 BTIF_TRACE_DEBUG1("p->lcsrk_key.sec_level=0x%02x",p->lcsrk_key.sec_level);
1263 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LCSRK);
1267 /* Fill in the bonded devices */
1268 if (is_device_added)
1270 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
1271 btif_gatts_add_bonded_dev_from_nv(bta_bd_addr);
1274 return BT_STATUS_SUCCESS;
1276 return BT_STATUS_FAIL;
1279 bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1283 bd2str(remote_bd_addr, &bdstr);
1284 int ret = btif_config_set_int("Remote", bdstr, "AddrType", (int)addr_type);
1286 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1289 /*******************************************************************************
1291 ** Function btif_storage_get_remote_addr_type
1293 ** Description BTIF storage API - Fetches the remote addr type
1295 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
1296 ** BT_STATUS_FAIL otherwise
1298 *******************************************************************************/
1299 bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1303 bd2str(remote_bd_addr, &bdstr);
1304 int ret = btif_config_get_int("Remote", bdstr, "AddrType", addr_type);
1305 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1308 /*******************************************************************************
1310 ** Function btif_storage_add_hid_device_info
1312 ** Description BTIF storage API - Adds the hid information of bonded hid devices-to NVRAM
1314 ** Returns BT_STATUS_SUCCESS if the store was successful,
1315 ** BT_STATUS_FAIL otherwise
1317 *******************************************************************************/
1319 bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr,
1320 UINT16 attr_mask, UINT8 sub_class,
1321 UINT8 app_id, UINT16 vendor_id,
1322 UINT16 product_id, UINT16 version,
1323 UINT8 ctry_code, UINT16 ssr_max_latency,
1324 UINT16 ssr_min_tout, UINT16 dl_len, UINT8 *dsc_list)
1327 BTIF_TRACE_DEBUG0("btif_storage_add_hid_device_info:");
1328 bd2str(remote_bd_addr, &bdstr);
1329 btif_config_set_int("Remote", bdstr, "HidAttrMask", attr_mask);
1330 btif_config_set_int("Remote", bdstr, "HidSubClass", sub_class);
1331 btif_config_set_int("Remote", bdstr, "HidAppId", app_id);
1332 btif_config_set_int("Remote", bdstr, "HidVendorId", vendor_id);
1333 btif_config_set_int("Remote", bdstr, "HidProductId", product_id);
1334 btif_config_set_int("Remote", bdstr, "HidVersion", version);
1335 btif_config_set_int("Remote", bdstr, "HidCountryCode", ctry_code);
1336 btif_config_set_int("Remote", bdstr, "HidSSRMaxLatency", ssr_max_latency);
1337 btif_config_set_int("Remote", bdstr, "HidSSRMinTimeout", ssr_min_tout);
1339 btif_config_set("Remote", bdstr, "HidDescriptor", (const char*)dsc_list, dl_len,
1342 return BT_STATUS_SUCCESS;
1345 /*******************************************************************************
1347 ** Function btif_storage_load_bonded_hid_info
1349 ** Description BTIF storage API - Loads hid info for all the bonded devices from NVRAM
1350 ** and adds those devices to the BTA_HH.
1352 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
1354 *******************************************************************************/
1355 bt_status_t btif_storage_load_bonded_hid_info(void)
1357 bt_bdaddr_t bd_addr;
1358 tBTA_HH_DEV_DSCP_INFO dscp_info;
1364 char kname[128], vname[128];
1367 kname_size = sizeof(kname);
1370 memset(&dscp_info, 0, sizeof(dscp_info));
1373 kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
1374 BTIF_TRACE_DEBUG2("Remote device:%s, size:%d", kname, kname_size);
1376 if(btif_config_get_int("Remote", kname, "HidAttrMask", &value))
1378 attr_mask = (uint16_t)value;
1380 btif_config_get_int("Remote", kname, "HidSubClass", &value);
1381 sub_class = (uint8_t)value;
1383 btif_config_get_int("Remote", kname, "HidAppId", &value);
1384 app_id = (uint8_t)value;
1386 btif_config_get_int("Remote", kname, "HidVendorId", &value);
1387 dscp_info.vendor_id = (uint16_t) value;
1389 btif_config_get_int("Remote", kname, "HidProductId", &value);
1390 dscp_info.product_id = (uint16_t) value;
1392 btif_config_get_int("Remote", kname, "HidVersion", &value);
1393 dscp_info.version = (uint8_t) value;
1395 btif_config_get_int("Remote", kname, "HidCountryCode", &value);
1396 dscp_info.ctry_code = (uint8_t) value;
1399 btif_config_get_int("Remote", kname, "HidSSRMaxLatency", &value);
1400 dscp_info.ssr_max_latency = (uint16_t) value;
1403 btif_config_get_int("Remote", kname, "HidSSRMinTimeout", &value);
1404 dscp_info.ssr_min_tout = (uint16_t) value;
1408 btif_config_get("Remote", kname, "HidDescriptor", NULL, &len, &type);
1411 dscp_info.descriptor.dl_len = (uint16_t)len;
1412 dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len);
1413 btif_config_get("Remote", kname, "HidDescriptor", (char*)dscp_info.descriptor.dsc_list, &len, &type);
1415 str2bd(kname, &bd_addr);
1416 // add extracted information to BTA HH
1417 if (btif_hh_add_added_dev(bd_addr,attr_mask))
1419 BTA_HhAddDev(bd_addr.address, attr_mask, sub_class,
1423 } while(kpos != -1);
1425 return BT_STATUS_SUCCESS;
1428 /*******************************************************************************
1430 ** Function btif_storage_remove_hid_info
1432 ** Description BTIF storage API - Deletes the bonded hid device info from NVRAM
1434 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1435 ** BT_STATUS_FAIL otherwise
1437 *******************************************************************************/
1438 bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr)
1443 bd2str(remote_bd_addr, &bdstr);
1445 btif_config_remove("Remote", bdstr, "HidAttrMask");
1446 btif_config_remove("Remote", bdstr, "HidSubClass");
1447 btif_config_remove("Remote", bdstr, "HidAppId");
1448 btif_config_remove("Remote", bdstr, "HidVendorId");
1449 btif_config_remove("Remote", bdstr, "HidProductId");
1450 btif_config_remove("Remote", bdstr, "HidVersion");
1451 btif_config_remove("Remote", bdstr, "HidCountryCode");
1452 btif_config_remove("Remote", bdstr, "HidSSRMaxLatency");
1453 btif_config_remove("Remote", bdstr, "HidSSRMinTimeout");
1454 btif_config_remove("Remote", bdstr, "HidDescriptor");
1456 return BT_STATUS_SUCCESS;
1459 /*******************************************************************************
1461 ** Function btif_storage_read_hl_apps_cb
1463 ** Description BTIF storage API - Read HL application control block from NVRAM
1465 ** Returns BT_STATUS_SUCCESS if the operation was successful,
1466 ** BT_STATUS_FAIL otherwise
1468 *******************************************************************************/
1469 bt_status_t btif_storage_read_hl_apps_cb(char *value, int value_size)
1471 bt_status_t bt_status = BT_STATUS_SUCCESS;
1472 int read_size=value_size, read_type=BTIF_CFG_TYPE_BIN;
1474 if (!btif_config_exist("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB))
1476 memset(value, 0, value_size);
1477 if (!btif_config_set("Local", BTIF_STORAGE_HL_APP,BTIF_STORAGE_HL_APP_CB,
1478 value, value_size, BTIF_CFG_TYPE_BIN))
1480 bt_status = BT_STATUS_FAIL;
1489 if (!btif_config_get("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB,
1490 value, &read_size, &read_type))
1492 bt_status = BT_STATUS_FAIL;
1496 if ((read_size != value_size) || (read_type != BTIF_CFG_TYPE_BIN) )
1498 BTIF_TRACE_ERROR4("%s value_size=%d read_size=%d read_type=%d",
1499 __FUNCTION__, value_size, read_size, read_type);
1500 bt_status = BT_STATUS_FAIL;
1506 BTIF_TRACE_DEBUG3("%s status=%d value_size=%d", __FUNCTION__, bt_status, value_size);
1511 /*******************************************************************************
1513 ** Function btif_storage_load_autopair_device_list
1515 ** Description BTIF storage API - Populates auto pair device list
1517 ** Returns BT_STATUS_SUCCESS if the auto pair blacklist is successfully populated
1518 ** BT_STATUS_FAIL otherwise
1520 *******************************************************************************/
1521 bt_status_t btif_storage_load_autopair_device_list() {
1522 // Configuration has already been loaded. No need to reload.
1523 if (btif_config_exist("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, NULL)) {
1524 return BT_STATUS_SUCCESS;
1527 static const char *key_names[] = {
1528 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR,
1529 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME,
1530 BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST,
1531 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME,
1532 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR,
1535 config_t *config = config_new(BTIF_AUTO_PAIR_CONF_FILE);
1537 ALOGE("%s failed to open auto pair blacklist conf file '%s'.", __func__, BTIF_AUTO_PAIR_CONF_FILE);
1538 return BT_STATUS_FAIL;
1541 for (size_t i = 0; i < ARRAY_SIZE(key_names); ++i) {
1542 const char *value = config_get_string(config, CONFIG_DEFAULT_SECTION, key_names[i], NULL);
1544 btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, key_names[i], value);
1548 config_free(config);
1549 return BT_STATUS_SUCCESS;
1552 /*******************************************************************************
1554 ** Function btif_storage_is_device_autopair_blacklisted
1556 ** Description BTIF storage API Checks if the given device is blacklisted for auto pairing
1558 ** Returns TRUE if the device is found in the auto pair blacklist
1561 *******************************************************************************/
1562 BOOLEAN btif_storage_is_device_autopair_blacklisted(bt_bdaddr_t *remote_bd_addr)
1569 char value[BTIF_STORAGE_MAX_LINE_SZ];
1570 int value_size = sizeof(value);
1572 bd2str(remote_bd_addr, &bdstr);
1574 /* Consider only Lower Address Part from BD Address */
1577 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1578 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR, value, &value_size))
1580 if (strcasestr(value,bdstr) != NULL)
1584 dev_name_str = BTM_SecReadDevName((remote_bd_addr->address));
1586 if (dev_name_str != NULL)
1588 value_size = sizeof(value);
1589 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1590 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME, value, &value_size))
1592 if (strstr(value,dev_name_str) != NULL)
1595 value_size = sizeof(value);
1596 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1597 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME, value, &value_size))
1599 token = strtok(value, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1600 while (token != NULL)
1602 if (strstr(dev_name_str, token) != NULL)
1605 token = strtok(NULL, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1609 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1610 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, value, &value_size))
1612 if (strstr(value,bdstr) != NULL)
1618 /*******************************************************************************
1620 ** Function btif_storage_add_device_to_autopair_blacklist
1622 ** Description BTIF storage API - Add a remote device to the auto pairing blacklist
1624 ** Returns BT_STATUS_SUCCESS if the device is successfully added to the auto pair blacklist
1625 ** BT_STATUS_FAIL otherwise
1627 *******************************************************************************/
1628 bt_status_t btif_storage_add_device_to_autopair_blacklist(bt_bdaddr_t *remote_bd_addr)
1632 char linebuf[BTIF_STORAGE_MAX_LINE_SZ+20];
1633 char input_value [20];
1635 bd2str(remote_bd_addr, &bdstr);
1636 strlcpy(input_value, (char*)bdstr, sizeof(input_value));
1637 strlcat(input_value,BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR, sizeof(input_value));
1639 int line_size = sizeof(linebuf);
1640 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1641 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf, &line_size))
1643 /* Append this address to the dynamic List of BD address */
1644 strncat (linebuf, input_value, BTIF_STORAGE_MAX_LINE_SZ);
1648 strncpy( linebuf,input_value, BTIF_STORAGE_MAX_LINE_SZ);
1651 /* Write back the key value */
1652 ret = btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1653 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf);
1655 return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1658 /*******************************************************************************
1660 ** Function btif_storage_is_fixed_pin_zeros_keyboard
1662 ** Description BTIF storage API - checks if this device has fixed PIN key device list
1664 ** Returns TRUE if the device is found in the fixed pin keyboard device list
1667 *******************************************************************************/
1668 BOOLEAN btif_storage_is_fixed_pin_zeros_keyboard(bt_bdaddr_t *remote_bd_addr)
1674 char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1676 bd2str(remote_bd_addr, &bdstr);
1678 /*consider on LAP part of BDA string*/
1681 int line_size = sizeof(linebuf);
1682 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1683 BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST, linebuf, &line_size))
1685 if (strcasestr(linebuf,bdstr) != NULL)
1692 /*******************************************************************************
1694 ** Function btif_storage_set_dmt_support_type
1696 ** Description Sets DMT support status for a remote device
1698 ** Returns BT_STATUS_SUCCESS if config update is successful
1699 ** BT_STATUS_FAIL otherwise
1701 *******************************************************************************/
1703 bt_status_t btif_storage_set_dmt_support_type(const bt_bdaddr_t *remote_bd_addr,
1704 BOOLEAN dmt_supported)
1707 bdstr_t bdstr = {0};
1710 bd2str(remote_bd_addr, &bdstr);
1714 BTIF_TRACE_ERROR1("%s NULL BD Address", __FUNCTION__);
1715 return BT_STATUS_FAIL;
1718 ret = btif_config_set_int("Remote", bdstr,"DMTSupported", (int)dmt_supported);
1719 return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1723 /*******************************************************************************
1725 ** Function btif_storage_is_dmt_supported_device
1727 ** Description checks if a device supports Dual mode topology
1729 ** Returns TRUE if remote address is valid and supports DMT else FALSE
1731 *******************************************************************************/
1733 BOOLEAN btif_storage_is_dmt_supported_device(const bt_bdaddr_t *remote_bd_addr)
1735 int dmt_supported = 0;
1736 bdstr_t bdstr = {0};
1738 bd2str(remote_bd_addr, &bdstr);
1742 bd2str(remote_bd_addr, &bdstr);
1746 BTIF_TRACE_ERROR1("%s NULL BD Address", __FUNCTION__);
1750 btif_config_get_int("Remote", bdstr,"DMTSupported", &dmt_supported);
1752 return dmt_supported == 1 ? TRUE:FALSE;