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);
255 /* save friendly name immediately */
258 case BT_PROPERTY_ADAPTER_SCAN_MODE:
259 btif_config_set_int("Local", "Adapter",
260 BTIF_STORAGE_KEY_ADAPTER_SCANMODE, *(int*)prop->val);
262 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
263 btif_config_set_int("Local", "Adapter",
264 BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, *(int*)prop->val);
266 case BT_PROPERTY_CLASS_OF_DEVICE:
267 btif_config_set_int("Remote", bdstr,
268 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, *(int*)prop->val);
270 case BT_PROPERTY_TYPE_OF_DEVICE:
271 btif_config_set_int("Remote", bdstr,
272 BTIF_STORAGE_PATH_REMOTE_DEVTYPE, *(int*)prop->val);
274 case BT_PROPERTY_UUIDS:
279 for (i=0; i < (prop->len)/sizeof(bt_uuid_t); i++)
281 bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val + i;
282 memset(buf, 0, sizeof(buf));
283 uuid_to_string(p_uuid, buf);
285 //strcat(value, ";");
288 btif_config_set_str("Remote", bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value);
292 case BT_PROPERTY_REMOTE_VERSION_INFO:
294 bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
299 btif_config_set_int("Remote", bdstr,
300 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, info->manufacturer);
301 btif_config_set_int("Remote", bdstr,
302 BTIF_STORAGE_PATH_REMOTE_VER_VER, info->version);
303 btif_config_set_int("Remote", bdstr,
304 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, info->sub_ver);
309 BTIF_TRACE_ERROR1("Unknow prop type:%d", prop->type);
314 static int cfg2prop(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
318 bd2str(remote_bd_addr, &bdstr);
319 BTIF_TRACE_DEBUG3("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
322 BTIF_TRACE_ERROR2("property type:%d, len:%d is invalid", prop->type, prop->len);
328 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
329 if(prop->len >= (int)sizeof(int))
330 ret = btif_config_get_int("Remote", bdstr,
331 BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int*)prop->val);
333 case BT_PROPERTY_BDNAME:
337 ret = btif_config_get_str("Remote", bdstr,
338 BTIF_STORAGE_PATH_REMOTE_NAME, (char*)prop->val, &len);
339 else ret = btif_config_get_str("Local", "Adapter",
340 BTIF_STORAGE_KEY_ADAPTER_NAME, (char*)prop->val, &len);
341 if(ret && len && len <= prop->len)
350 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
353 ret = btif_config_get_str("Remote", bdstr,
354 BTIF_STORAGE_PATH_REMOTE_ALIASE, (char*)prop->val, &len);
355 if(ret && len && len <= prop->len)
364 case BT_PROPERTY_ADAPTER_SCAN_MODE:
365 if(prop->len >= (int)sizeof(int))
366 ret = btif_config_get_int("Local", "Adapter",
367 BTIF_STORAGE_KEY_ADAPTER_SCANMODE, (int*)prop->val);
369 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
370 if(prop->len >= (int)sizeof(int))
371 ret = btif_config_get_int("Local", "Adapter",
372 BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, (int*)prop->val);
374 case BT_PROPERTY_CLASS_OF_DEVICE:
375 if(prop->len >= (int)sizeof(int))
376 ret = btif_config_get_int("Remote", bdstr,
377 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, (int*)prop->val);
379 case BT_PROPERTY_TYPE_OF_DEVICE:
380 if(prop->len >= (int)sizeof(int))
381 ret = btif_config_get_int("Remote",
382 bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTYPE, (int*)prop->val);
384 case BT_PROPERTY_UUIDS:
387 int size = sizeof(value);
388 if(btif_config_get_str("Remote", bdstr,
389 BTIF_STORAGE_PATH_REMOTE_SERVICE, value, &size))
391 bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val;
392 uint32_t num_uuids = 0;
393 btif_in_split_uuids_string_to_list(value, p_uuid, &num_uuids);
394 prop->len = num_uuids * sizeof(bt_uuid_t);
404 case BT_PROPERTY_REMOTE_VERSION_INFO:
406 bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
408 if(prop->len >= (int)sizeof(bt_remote_version_t))
410 ret = btif_config_get_int("Remote", bdstr,
411 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, &info->manufacturer);
414 ret = btif_config_get_int("Remote", bdstr,
415 BTIF_STORAGE_PATH_REMOTE_VER_VER, &info->version);
418 ret = btif_config_get_int("Remote", bdstr,
419 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, &info->sub_ver);
424 BTIF_TRACE_ERROR1("Unknow prop type:%d", prop->type);
430 /*******************************************************************************
432 ** Function btif_in_fetch_bonded_devices
434 ** Description Internal helper function to fetch the bonded devices
437 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
439 *******************************************************************************/
440 static bt_status_t btif_in_fetch_bonded_device(char *bdstr)
442 BOOLEAN bt_linkkey_file_found=FALSE;
445 int type = BTIF_CFG_TYPE_BIN;
447 int size = sizeof(link_key);
448 if(btif_config_get("Remote", bdstr, "LinkKey", (char*)link_key, &size, &type))
451 if(btif_config_get_int("Remote", bdstr, "LinkKeyType", &linkkey_type))
453 bt_linkkey_file_found = TRUE;
457 bt_linkkey_file_found = FALSE;
460 if((btif_in_fetch_bonded_ble_device(bdstr, FALSE, NULL) != BT_STATUS_SUCCESS)
461 && (!bt_linkkey_file_found))
463 BTIF_TRACE_DEBUG1("Remote device:%s, no link key or ble key found", bdstr);
464 return BT_STATUS_FAIL;
466 return BT_STATUS_SUCCESS;
469 /*******************************************************************************
471 ** Function btif_in_fetch_bonded_devices
473 ** Description Internal helper function to fetch the bonded devices
476 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
478 *******************************************************************************/
479 static bt_status_t btif_in_fetch_bonded_devices(btif_bonded_devices_t *p_bonded_devices, int add)
481 BTIF_TRACE_DEBUG1("in add:%d", add);
482 memset(p_bonded_devices, 0, sizeof(btif_bonded_devices_t));
484 char kname[128], vname[128];
487 kname_size = sizeof(kname);
490 BOOLEAN bt_linkkey_file_found=FALSE;
495 kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
496 BTIF_TRACE_DEBUG2("Remote device:%s, size:%d", kname, kname_size);
497 int type = BTIF_CFG_TYPE_BIN;
499 int size = sizeof(link_key);
500 if(btif_config_get("Remote", kname, "LinkKey", (char*)link_key, &size, &type))
503 if(btif_config_get_int("Remote", kname, "LinkKeyType", &linkkey_type))
506 //btif_config_get_int("Remote", kname, "PinLength", &pin_len))
508 str2bd(kname, &bd_addr);
511 DEV_CLASS dev_class = {0, 0, 0};
513 if(btif_config_get_int("Remote", kname, "DevClass", &cod))
514 uint2devclass((UINT32)cod, dev_class);
515 BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0, (UINT8)linkkey_type, 0);
517 #if BLE_INCLUDED == TRUE
518 if (btif_config_get_int("Remote", kname, "DevType", &device_type) &&
519 (device_type == BT_DEVICE_TYPE_DUMO) )
521 btif_gatts_add_bonded_dev_from_nv(bd_addr.address);
525 bt_linkkey_file_found = TRUE;
526 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
530 #if (BLE_INCLUDED == TRUE)
531 bt_linkkey_file_found = FALSE;
533 BTIF_TRACE_ERROR1("bounded device:%s, LinkKeyType or PinLength is invalid", kname);
537 #if (BLE_INCLUDED == TRUE)
538 if(!(btif_in_fetch_bonded_ble_device(kname,add, p_bonded_devices)) && (!bt_linkkey_file_found))
540 BTIF_TRACE_DEBUG1("Remote device:%s, no link key or ble key found", kname);
543 if(!bt_linkkey_file_found)
544 BTIF_TRACE_DEBUG1("Remote device:%s, no link key", kname);
546 kname_size = sizeof(kname);
549 BTIF_TRACE_DEBUG0("out");
550 return BT_STATUS_SUCCESS;
553 /************************************************************************************
555 ************************************************************************************/
557 /************************************************************************************
559 ************************************************************************************/
561 /** functions are synchronous.
562 * functions can be called by both internal modules such as BTIF_DM and by external entiries from HAL via BTIF_context_switch
563 * For OUT parameters, caller is expected to provide the memory.
564 * Caller is expected to provide a valid pointer to 'property->value' based on the property->type
566 /*******************************************************************************
568 ** Function btif_storage_get_adapter_property
570 ** Description BTIF storage API - Fetches the adapter property->type
571 ** from NVRAM and fills property->val.
572 ** Caller should provide memory for property->val and
573 ** set the property->val
575 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
576 ** BT_STATUS_FAIL otherwise
578 *******************************************************************************/
579 bt_status_t btif_storage_get_adapter_property(bt_property_t *property)
582 /* Special handling for adapter BD_ADDR and BONDED_DEVICES */
583 if (property->type == BT_PROPERTY_BDADDR)
586 bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)property->val;
587 /* This has been cached in btif. Just fetch it from there */
588 memcpy(bd_addr, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
589 property->len = sizeof(bt_bdaddr_t);
590 return BT_STATUS_SUCCESS;
592 else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES)
594 btif_bonded_devices_t bonded_devices;
596 btif_in_fetch_bonded_devices(&bonded_devices, 0);
598 BTIF_TRACE_DEBUG2("%s: Number of bonded devices: %d Property:BT_PROPERTY_ADAPTER_BONDED_DEVICES", __FUNCTION__, bonded_devices.num_devices);
600 if (bonded_devices.num_devices > 0)
602 property->len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
603 memcpy(property->val, bonded_devices.devices, property->len);
606 /* if there are no bonded_devices, then length shall be 0 */
607 return BT_STATUS_SUCCESS;
609 else if (property->type == BT_PROPERTY_UUIDS)
611 /* publish list of local supported services */
612 bt_uuid_t *p_uuid = (bt_uuid_t*)property->val;
613 uint32_t num_uuids = 0;
616 tBTA_SERVICE_MASK service_mask = btif_get_enabled_services_mask();
617 BTIF_TRACE_ERROR2("%s service_mask:0x%x", __FUNCTION__, service_mask);
618 for (i=0; i < BTA_MAX_SERVICE_ID; i++)
620 /* This should eventually become a function when more services are enabled */
622 &(tBTA_SERVICE_MASK)(1 << i))
626 case BTA_HFP_SERVICE_ID:
628 uuid16_to_uuid128(UUID_SERVCLASS_AG_HANDSFREE,
632 /* intentional fall through: Send both BFP & HSP UUIDs if HFP is enabled */
633 case BTA_HSP_SERVICE_ID:
635 uuid16_to_uuid128(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,
639 case BTA_A2DP_SERVICE_ID:
641 uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SOURCE,
645 case BTA_HFP_HS_SERVICE_ID:
647 uuid16_to_uuid128(UUID_SERVCLASS_HF_HANDSFREE,
654 property->len = (num_uuids)*sizeof(bt_uuid_t);
655 return BT_STATUS_SUCCESS;
658 /* fall through for other properties */
659 if(!cfg2prop(NULL, property))
661 return btif_dm_get_adapter_property(property);
663 return BT_STATUS_SUCCESS;
666 /*******************************************************************************
668 ** Function btif_storage_set_adapter_property
670 ** Description BTIF storage API - Stores the adapter property
673 ** Returns BT_STATUS_SUCCESS if the store was successful,
674 ** BT_STATUS_FAIL otherwise
676 *******************************************************************************/
677 bt_status_t btif_storage_set_adapter_property(bt_property_t *property)
679 return prop2cfg(NULL, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
682 /*******************************************************************************
684 ** Function btif_storage_get_remote_device_property
686 ** Description BTIF storage API - Fetches the remote device property->type
687 ** from NVRAM and fills property->val.
688 ** Caller should provide memory for property->val and
689 ** set the property->val
691 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
692 ** BT_STATUS_FAIL otherwise
694 *******************************************************************************/
695 bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t *remote_bd_addr,
696 bt_property_t *property)
698 return cfg2prop(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
700 /*******************************************************************************
702 ** Function btif_storage_set_remote_device_property
704 ** Description BTIF storage API - Stores the remote device property
707 ** Returns BT_STATUS_SUCCESS if the store was successful,
708 ** BT_STATUS_FAIL otherwise
710 *******************************************************************************/
711 bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t *remote_bd_addr,
712 bt_property_t *property)
714 return prop2cfg(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
717 /*******************************************************************************
719 ** Function btif_storage_add_remote_device
721 ** Description BTIF storage API - Adds a newly discovered device to NVRAM
722 ** along with the timestamp. Also, stores the various
723 ** properties - RSSI, BDADDR, NAME (if found in EIR)
725 ** Returns BT_STATUS_SUCCESS if the store was successful,
726 ** BT_STATUS_FAIL otherwise
728 *******************************************************************************/
729 bt_status_t btif_storage_add_remote_device(bt_bdaddr_t *remote_bd_addr,
730 uint32_t num_properties,
731 bt_property_t *properties)
734 /* TODO: If writing a property, fails do we go back undo the earlier
735 * written properties? */
736 for (i=0; i < num_properties; i++)
738 /* Ignore the RSSI as this is not stored in DB */
739 if (properties[i].type == BT_PROPERTY_REMOTE_RSSI)
742 /* BD_ADDR for remote device needs special handling as we also store timestamp */
743 if (properties[i].type == BT_PROPERTY_BDADDR)
745 bt_property_t addr_prop;
746 memcpy(&addr_prop, &properties[i], sizeof(bt_property_t));
747 addr_prop.type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP;
748 btif_storage_set_remote_device_property(remote_bd_addr,
753 btif_storage_set_remote_device_property(remote_bd_addr,
757 return BT_STATUS_SUCCESS;
760 /*******************************************************************************
762 ** Function btif_storage_add_bonded_device
764 ** Description BTIF storage API - Adds the newly bonded device to NVRAM
765 ** along with the link-key, Key type and Pin key length
767 ** Returns BT_STATUS_SUCCESS if the store was successful,
768 ** BT_STATUS_FAIL otherwise
770 *******************************************************************************/
772 bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
778 bd2str(remote_bd_addr, &bdstr);
779 int ret = btif_config_set_int("Remote", bdstr, "LinkKeyType", (int)key_type);
780 ret &= btif_config_set_int("Remote", bdstr, "PinLength", (int)pin_length);
781 ret &= btif_config_set("Remote", bdstr, "LinkKey", (const char*)link_key, sizeof(LINK_KEY), BTIF_CFG_TYPE_BIN);
782 /* write bonded info immediately */
784 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
787 /*******************************************************************************
789 ** Function btif_storage_remove_bonded_device
791 ** Description BTIF storage API - Deletes the bonded device from NVRAM
793 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
794 ** BT_STATUS_FAIL otherwise
796 *******************************************************************************/
797 bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
800 bd2str(remote_bd_addr, &bdstr);
801 BTIF_TRACE_DEBUG1("in bd addr:%s", bdstr);
803 if(btif_config_exist("Remote", bdstr, "LinkKeyType"))
804 ret &= btif_config_remove("Remote", bdstr, "LinkKeyType");
805 if(btif_config_exist("Remote", bdstr, "PinLength"))
806 ret &= btif_config_remove("Remote", bdstr, "PinLength");
807 if(btif_config_exist("Remote", bdstr, "LinkKey"))
808 ret &= btif_config_remove("Remote", bdstr, "LinkKey");
809 /* write bonded info immediately */
811 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
815 /*******************************************************************************
817 ** Function btif_storage_load_bonded_devices
819 ** Description BTIF storage API - Loads all the bonded devices from NVRAM
820 ** and adds to the BTA.
821 ** Additionally, this API also invokes the adaper_properties_cb
822 ** and remote_device_properties_cb for each of the bonded devices.
824 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
826 *******************************************************************************/
827 bt_status_t btif_storage_load_bonded_devices(void)
830 btif_bonded_devices_t bonded_devices;
832 bt_property_t adapter_props[6];
833 uint32_t num_props = 0;
834 bt_property_t remote_properties[8];
836 bt_bdname_t name, alias;
838 uint32_t disc_timeout;
839 bt_bdaddr_t *devices_list;
840 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
841 bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
842 uint32_t cod, devtype;
844 btif_in_fetch_bonded_devices(&bonded_devices, 1);
846 /* Now send the adapter_properties_cb with all adapter_properties */
848 memset(adapter_props, 0, sizeof(adapter_props));
851 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDADDR, &addr, sizeof(addr),
852 adapter_props[num_props]);
856 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDNAME, &name, sizeof(name),
857 adapter_props[num_props]);
861 /* TODO: At the time of BT on, always report the scan mode as 0 irrespective
862 of the scan_mode during the previous enable cycle.
863 This needs to be re-visited as part of the app/stack enable sequence
865 mode = BT_SCAN_MODE_NONE;
866 adapter_props[num_props].type = BT_PROPERTY_ADAPTER_SCAN_MODE;
867 adapter_props[num_props].len = sizeof(mode);
868 adapter_props[num_props].val = &mode;
872 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
873 &disc_timeout, sizeof(disc_timeout),
874 adapter_props[num_props]);
878 devices_list = (bt_bdaddr_t*)malloc(sizeof(bt_bdaddr_t)*bonded_devices.num_devices);
879 adapter_props[num_props].type = BT_PROPERTY_ADAPTER_BONDED_DEVICES;
880 adapter_props[num_props].len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
881 adapter_props[num_props].val = devices_list;
882 for (i=0; i < bonded_devices.num_devices; i++)
884 memcpy(devices_list + i, &bonded_devices.devices[i], sizeof(bt_bdaddr_t));
889 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_UUIDS,
890 local_uuids, sizeof(local_uuids),
891 adapter_props[num_props]);
894 btif_adapter_properties_evt(BT_STATUS_SUCCESS, num_props, adapter_props);
899 BTIF_TRACE_EVENT2("%s: %d bonded devices found", __FUNCTION__, bonded_devices.num_devices);
902 for (i = 0; i < bonded_devices.num_devices; i++)
904 bt_bdaddr_t *p_remote_addr;
907 p_remote_addr = &bonded_devices.devices[i];
908 memset(remote_properties, 0, sizeof(remote_properties));
909 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_BDNAME,
911 remote_properties[num_props]);
914 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_REMOTE_FRIENDLY_NAME,
915 &alias, sizeof(alias),
916 remote_properties[num_props]);
919 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_CLASS_OF_DEVICE,
921 remote_properties[num_props]);
924 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_TYPE_OF_DEVICE,
925 &devtype, sizeof(devtype),
926 remote_properties[num_props]);
929 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_UUIDS,
930 remote_uuids, sizeof(remote_uuids),
931 remote_properties[num_props]);
934 btif_remote_properties_evt(BT_STATUS_SUCCESS, p_remote_addr,
935 num_props, remote_properties);
938 return BT_STATUS_SUCCESS;
941 #if (BLE_INCLUDED == TRUE)
943 /*******************************************************************************
945 ** Function btif_storage_add_ble_bonding_key
947 ** Description BTIF storage API - Adds the newly bonded device to NVRAM
948 ** along with the ble-key, Key type and Pin key length
950 ** Returns BT_STATUS_SUCCESS if the store was successful,
951 ** BT_STATUS_FAIL otherwise
953 *******************************************************************************/
955 bt_status_t btif_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
961 bd2str(remote_bd_addr, &bdstr);
965 case BTIF_DM_LE_KEY_PENC:
966 name = "LE_KEY_PENC";
968 case BTIF_DM_LE_KEY_PID:
971 case BTIF_DM_LE_KEY_PCSRK:
972 name = "LE_KEY_PCSRK";
974 case BTIF_DM_LE_KEY_LENC:
975 name = "LE_KEY_LENC";
977 case BTIF_DM_LE_KEY_LCSRK:
978 name = "LE_KEY_LCSRK";
981 return BT_STATUS_FAIL;
983 int ret = btif_config_set("Remote", bdstr, name, (const char*)key, (int)key_length, BTIF_CFG_TYPE_BIN);
985 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
988 /*******************************************************************************
990 ** Function btif_storage_get_ble_bonding_key
994 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
995 ** BT_STATUS_FAIL otherwise
997 *******************************************************************************/
998 bt_status_t btif_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
1004 bd2str(remote_bd_addr, &bdstr);
1006 int type = BTIF_CFG_TYPE_BIN;
1009 case BTIF_DM_LE_KEY_PENC:
1010 name = "LE_KEY_PENC";
1012 case BTIF_DM_LE_KEY_PID:
1013 name = "LE_KEY_PID";
1015 case BTIF_DM_LE_KEY_PCSRK:
1016 name = "LE_KEY_PCSRK";
1018 case BTIF_DM_LE_KEY_LENC:
1019 name = "LE_KEY_LENC";
1021 case BTIF_DM_LE_KEY_LCSRK:
1022 name = "LE_KEY_LCSRK";
1025 return BT_STATUS_FAIL;
1027 int ret = btif_config_get("Remote", bdstr, name, key_value, &key_length, &type);
1028 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1032 /*******************************************************************************
1034 ** Function btif_storage_remove_ble_keys
1036 ** Description BTIF storage API - Deletes the bonded device from NVRAM
1038 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1039 ** BT_STATUS_FAIL otherwise
1041 *******************************************************************************/
1042 bt_status_t btif_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr)
1045 bd2str(remote_bd_addr, &bdstr);
1046 BTIF_TRACE_DEBUG2(" %s in bd addr:%s",__FUNCTION__, bdstr);
1048 if(btif_config_exist("Remote", bdstr, "LE_KEY_PENC"))
1049 ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PENC");
1050 if(btif_config_exist("Remote", bdstr, "LE_KEY_PID"))
1051 ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PID");
1052 if(btif_config_exist("Remote", bdstr, "LE_KEY_PCSRK"))
1053 ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PCSRK");
1054 if(btif_config_exist("Remote", bdstr, "LE_KEY_LENC"))
1055 ret &= btif_config_remove("Remote", bdstr, "LE_KEY_LENC");
1056 if(btif_config_exist("Remote", bdstr, "LE_KEY_LCSRK"))
1057 ret &= btif_config_remove("Remote", bdstr, "LE_KEY_LCSRK");
1059 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1062 /*******************************************************************************
1064 ** Function btif_storage_add_ble_local_key
1066 ** Description BTIF storage API - Adds the ble key to NVRAM
1068 ** Returns BT_STATUS_SUCCESS if the store was successful,
1069 ** BT_STATUS_FAIL otherwise
1071 *******************************************************************************/
1072 bt_status_t btif_storage_add_ble_local_key(char *key,
1079 case BTIF_DM_LE_LOCAL_KEY_IR:
1080 name = "LE_LOCAL_KEY_IR";
1082 case BTIF_DM_LE_LOCAL_KEY_IRK:
1083 name = "LE_LOCAL_KEY_IRK";
1085 case BTIF_DM_LE_LOCAL_KEY_DHK:
1086 name = "LE_LOCAL_KEY_DHK";
1088 case BTIF_DM_LE_LOCAL_KEY_ER:
1089 name = "LE_LOCAL_KEY_ER";
1092 return BT_STATUS_FAIL;
1094 int ret = btif_config_set("Local", "Adapter", name, (const char*)key, key_length, BTIF_CFG_TYPE_BIN);
1096 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1099 /*******************************************************************************
1101 ** Function btif_storage_get_ble_local_key
1105 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
1106 ** BT_STATUS_FAIL otherwise
1108 *******************************************************************************/
1109 bt_status_t btif_storage_get_ble_local_key(UINT8 key_type,
1114 int type = BTIF_CFG_TYPE_BIN;
1117 case BTIF_DM_LE_LOCAL_KEY_IR:
1118 name = "LE_LOCAL_KEY_IR";
1120 case BTIF_DM_LE_LOCAL_KEY_IRK:
1121 name = "LE_LOCAL_KEY_IRK";
1123 case BTIF_DM_LE_LOCAL_KEY_DHK:
1124 name = "LE_LOCAL_KEY_DHK";
1126 case BTIF_DM_LE_LOCAL_KEY_ER:
1127 name = "LE_LOCAL_KEY_ER";
1130 return BT_STATUS_FAIL;
1132 int ret = btif_config_get("Local", "Adapter", name, key_value, &key_length, &type);
1133 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1137 /*******************************************************************************
1139 ** Function btif_storage_remove_ble_local_keys
1141 ** Description BTIF storage API - Deletes the bonded device from NVRAM
1143 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1144 ** BT_STATUS_FAIL otherwise
1146 *******************************************************************************/
1147 bt_status_t btif_storage_remove_ble_local_keys(void)
1150 if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_IR"))
1151 ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_IR");
1152 if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_IRK"))
1153 ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_IRK");
1154 if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_DHK"))
1155 ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_DHK");
1156 if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_ER"))
1157 ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_ER");
1159 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1162 bt_status_t btif_in_fetch_bonded_ble_device(char *remote_bd_addr,int add, btif_bonded_devices_t *p_bonded_devices)
1168 bt_bdaddr_t bd_addr;
1169 BD_ADDR bta_bd_addr;
1170 BOOLEAN is_device_added =FALSE;
1171 BOOLEAN key_found = FALSE;
1172 tBTA_LE_KEY_VALUE *p;
1174 if(!btif_config_get_int("Remote", remote_bd_addr,"DevType", &device_type))
1175 return BT_STATUS_FAIL;
1176 if(device_type == BT_DEVICE_TYPE_BLE)
1178 BTIF_TRACE_DEBUG2("%s %s found a BLE device", __FUNCTION__,remote_bd_addr);
1179 str2bd(remote_bd_addr, &bd_addr);
1180 bdcpy(bta_bd_addr, bd_addr.address);
1181 if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS)
1183 return BT_STATUS_FAIL;
1186 memset(buf, 0, sizeof(buf));
1187 if (btif_storage_get_ble_bonding_key(&bd_addr,
1188 BTIF_DM_LE_KEY_PENC,
1190 sizeof(btif_dm_ble_penc_keys_t)) == BT_STATUS_SUCCESS)
1194 if (!is_device_added)
1196 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1197 is_device_added = TRUE;
1199 p = (tBTA_LE_KEY_VALUE *)buf;
1200 for (i=0; i<16; i++)
1202 BTIF_TRACE_DEBUG2("penc_key.ltk[%d]=0x%02x",i,p->penc_key.ltk[i]);
1206 BTIF_TRACE_DEBUG2("penc_key.rand[%d]=0x%02x",i,p->penc_key.rand[i]);
1208 BTIF_TRACE_DEBUG1("p->penc_key.ediv=0x%04x",p->penc_key.ediv);
1209 BTIF_TRACE_DEBUG1("p->penc_key.sec_level=0x%02x",p->penc_key.sec_level);
1210 BTIF_TRACE_DEBUG1("p->penc_key.key_size=0x%02x",p->penc_key.key_size);
1211 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PENC);
1216 memset(buf, 0, sizeof(buf));
1217 if (btif_storage_get_ble_bonding_key(&bd_addr,
1220 sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1224 if (!is_device_added)
1226 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1227 is_device_added = TRUE;
1229 p = (tBTA_LE_KEY_VALUE *)buf;
1230 for (i=0; i<16; i++)
1232 BTIF_TRACE_DEBUG2("p->pid_key[%d]=0x%02x",i,p->pid_key.irk[i]);
1235 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PID);
1240 memset(buf, 0, sizeof(buf));
1241 if (btif_storage_get_ble_bonding_key(&bd_addr,
1242 BTIF_DM_LE_KEY_PCSRK,
1244 sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1248 if (!is_device_added)
1250 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1251 is_device_added = TRUE;
1254 p = (tBTA_LE_KEY_VALUE *)buf;
1255 for (i=0; i<16; i++)
1257 BTIF_TRACE_DEBUG2("p->pcsrk_key.csrk[%d]=0x%02x",i, p->psrk_key.csrk[i]);
1259 BTIF_TRACE_DEBUG1("p->pcsrk_key.counter=0x%08x",p->psrk_key.counter);
1260 BTIF_TRACE_DEBUG1("p->pcsrk_key.sec_level=0x%02x",p->psrk_key.sec_level);
1262 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PCSRK);
1267 memset(buf, 0, sizeof(buf));
1268 if (btif_storage_get_ble_bonding_key(&bd_addr,
1269 BTIF_DM_LE_KEY_LENC,
1271 sizeof(btif_dm_ble_lenc_keys_t)) == BT_STATUS_SUCCESS)
1275 if (!is_device_added)
1277 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1278 is_device_added = TRUE;
1280 p = (tBTA_LE_KEY_VALUE *)buf;
1281 BTIF_TRACE_DEBUG1("p->lenc_key.div=0x%04x",p->lenc_key.div);
1282 BTIF_TRACE_DEBUG1("p->lenc_key.key_size=0x%02x",p->lenc_key.key_size);
1283 BTIF_TRACE_DEBUG1("p->lenc_key.sec_level=0x%02x",p->lenc_key.sec_level);
1285 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LENC);
1290 memset(buf, 0, sizeof(buf));
1291 if (btif_storage_get_ble_bonding_key(&bd_addr,
1292 BTIF_DM_LE_KEY_LCSRK,
1294 sizeof(btif_dm_ble_lcsrk_keys_t)) == BT_STATUS_SUCCESS)
1298 if (!is_device_added)
1300 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1301 is_device_added = TRUE;
1303 p = (tBTA_LE_KEY_VALUE *)buf;
1304 BTIF_TRACE_DEBUG1("p->lcsrk_key.div=0x%04x",p->lcsrk_key.div);
1305 BTIF_TRACE_DEBUG1("p->lcsrk_key.counter=0x%08x",p->lcsrk_key.counter);
1306 BTIF_TRACE_DEBUG1("p->lcsrk_key.sec_level=0x%02x",p->lcsrk_key.sec_level);
1308 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LCSRK);
1313 /* Fill in the bonded devices */
1314 if (is_device_added)
1316 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
1317 btif_gatts_add_bonded_dev_from_nv(bta_bd_addr);
1321 return BT_STATUS_SUCCESS;
1323 return BT_STATUS_FAIL;
1325 return BT_STATUS_FAIL;
1328 bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1332 bd2str(remote_bd_addr, &bdstr);
1333 int ret = btif_config_set_int("Remote", bdstr, "AddrType", (int)addr_type);
1335 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1338 /*******************************************************************************
1340 ** Function btif_storage_get_remote_addr_type
1342 ** Description BTIF storage API - Fetches the remote addr type
1344 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
1345 ** BT_STATUS_FAIL otherwise
1347 *******************************************************************************/
1348 bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1352 bd2str(remote_bd_addr, &bdstr);
1353 int ret = btif_config_get_int("Remote", bdstr, "AddrType", addr_type);
1354 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1357 /*******************************************************************************
1359 ** Function btif_storage_add_hid_device_info
1361 ** Description BTIF storage API - Adds the hid information of bonded hid devices-to NVRAM
1363 ** Returns BT_STATUS_SUCCESS if the store was successful,
1364 ** BT_STATUS_FAIL otherwise
1366 *******************************************************************************/
1368 bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr,
1369 UINT16 attr_mask, UINT8 sub_class,
1370 UINT8 app_id, UINT16 vendor_id,
1371 UINT16 product_id, UINT16 version,
1372 UINT8 ctry_code, UINT16 ssr_max_latency,
1373 UINT16 ssr_min_tout, UINT16 dl_len, UINT8 *dsc_list)
1376 BTIF_TRACE_DEBUG0("btif_storage_add_hid_device_info:");
1377 bd2str(remote_bd_addr, &bdstr);
1378 btif_config_set_int("Remote", bdstr, "HidAttrMask", attr_mask);
1379 btif_config_set_int("Remote", bdstr, "HidSubClass", sub_class);
1380 btif_config_set_int("Remote", bdstr, "HidAppId", app_id);
1381 btif_config_set_int("Remote", bdstr, "HidVendorId", vendor_id);
1382 btif_config_set_int("Remote", bdstr, "HidProductId", product_id);
1383 btif_config_set_int("Remote", bdstr, "HidVersion", version);
1384 btif_config_set_int("Remote", bdstr, "HidCountryCode", ctry_code);
1385 btif_config_set_int("Remote", bdstr, "HidSSRMaxLatency", ssr_max_latency);
1386 btif_config_set_int("Remote", bdstr, "HidSSRMinTimeout", ssr_min_tout);
1388 btif_config_set("Remote", bdstr, "HidDescriptor", (const char*)dsc_list, dl_len,
1391 return BT_STATUS_SUCCESS;
1394 /*******************************************************************************
1396 ** Function btif_storage_load_bonded_hid_info
1398 ** Description BTIF storage API - Loads hid info for all the bonded devices from NVRAM
1399 ** and adds those devices to the BTA_HH.
1401 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
1403 *******************************************************************************/
1404 bt_status_t btif_storage_load_bonded_hid_info(void)
1406 bt_bdaddr_t bd_addr;
1407 tBTA_HH_DEV_DSCP_INFO dscp_info;
1413 char kname[128], vname[128];
1416 kname_size = sizeof(kname);
1419 memset(&dscp_info, 0, sizeof(dscp_info));
1422 kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
1423 BTIF_TRACE_DEBUG2("Remote device:%s, size:%d", kname, kname_size);
1425 if(btif_in_fetch_bonded_device(kname) == BT_STATUS_SUCCESS)
1427 if(btif_config_get_int("Remote", kname, "HidAttrMask", &value))
1429 attr_mask = (uint16_t)value;
1431 btif_config_get_int("Remote", kname, "HidSubClass", &value);
1432 sub_class = (uint8_t)value;
1434 btif_config_get_int("Remote", kname, "HidAppId", &value);
1435 app_id = (uint8_t)value;
1437 btif_config_get_int("Remote", kname, "HidVendorId", &value);
1438 dscp_info.vendor_id = (uint16_t) value;
1440 btif_config_get_int("Remote", kname, "HidProductId", &value);
1441 dscp_info.product_id = (uint16_t) value;
1443 btif_config_get_int("Remote", kname, "HidVersion", &value);
1444 dscp_info.version = (uint8_t) value;
1446 btif_config_get_int("Remote", kname, "HidCountryCode", &value);
1447 dscp_info.ctry_code = (uint8_t) value;
1450 btif_config_get_int("Remote", kname, "HidSSRMaxLatency", &value);
1451 dscp_info.ssr_max_latency = (uint16_t) value;
1454 btif_config_get_int("Remote", kname, "HidSSRMinTimeout", &value);
1455 dscp_info.ssr_min_tout = (uint16_t) value;
1459 btif_config_get("Remote", kname, "HidDescriptor", NULL, &len, &type);
1462 dscp_info.descriptor.dl_len = (uint16_t)len;
1463 dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len);
1464 btif_config_get("Remote", kname, "HidDescriptor", (char*)dscp_info.descriptor.dsc_list, &len, &type);
1466 str2bd(kname, &bd_addr);
1467 // add extracted information to BTA HH
1468 if (btif_hh_add_added_dev(bd_addr,attr_mask))
1470 BTA_HhAddDev(bd_addr.address, attr_mask, sub_class,
1477 if(btif_config_get_int("Remote", kname, "HidAttrMask", &value))
1479 btif_storage_remove_hid_info(&bd_addr);
1480 str2bd(kname, &bd_addr);
1483 } while(kpos != -1);
1485 return BT_STATUS_SUCCESS;
1488 /*******************************************************************************
1490 ** Function btif_storage_remove_hid_info
1492 ** Description BTIF storage API - Deletes the bonded hid device info from NVRAM
1494 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1495 ** BT_STATUS_FAIL otherwise
1497 *******************************************************************************/
1498 bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr)
1503 bd2str(remote_bd_addr, &bdstr);
1505 btif_config_remove("Remote", bdstr, "HidAttrMask");
1506 btif_config_remove("Remote", bdstr, "HidSubClass");
1507 btif_config_remove("Remote", bdstr, "HidAppId");
1508 btif_config_remove("Remote", bdstr, "HidVendorId");
1509 btif_config_remove("Remote", bdstr, "HidProductId");
1510 btif_config_remove("Remote", bdstr, "HidVersion");
1511 btif_config_remove("Remote", bdstr, "HidCountryCode");
1512 btif_config_remove("Remote", bdstr, "HidSSRMaxLatency");
1513 btif_config_remove("Remote", bdstr, "HidSSRMinTimeout");
1514 btif_config_remove("Remote", bdstr, "HidDescriptor");
1516 return BT_STATUS_SUCCESS;
1519 /*******************************************************************************
1521 ** Function btif_storage_read_hl_apps_cb
1523 ** Description BTIF storage API - Read HL application control block from NVRAM
1525 ** Returns BT_STATUS_SUCCESS if the operation was successful,
1526 ** BT_STATUS_FAIL otherwise
1528 *******************************************************************************/
1529 bt_status_t btif_storage_read_hl_apps_cb(char *value, int value_size)
1531 bt_status_t bt_status = BT_STATUS_SUCCESS;
1532 int read_size=value_size, read_type=BTIF_CFG_TYPE_BIN;
1534 if (!btif_config_exist("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB))
1536 memset(value, 0, value_size);
1537 if (!btif_config_set("Local", BTIF_STORAGE_HL_APP,BTIF_STORAGE_HL_APP_CB,
1538 value, value_size, BTIF_CFG_TYPE_BIN))
1540 bt_status = BT_STATUS_FAIL;
1549 if (!btif_config_get("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB,
1550 value, &read_size, &read_type))
1552 bt_status = BT_STATUS_FAIL;
1556 if ((read_size != value_size) || (read_type != BTIF_CFG_TYPE_BIN) )
1558 BTIF_TRACE_ERROR4("%s value_size=%d read_size=%d read_type=%d",
1559 __FUNCTION__, value_size, read_size, read_type);
1560 bt_status = BT_STATUS_FAIL;
1566 BTIF_TRACE_DEBUG3("%s status=%d value_size=%d", __FUNCTION__, bt_status, value_size);
1571 /*******************************************************************************
1573 ** Function btif_storage_load_autopair_device_list
1575 ** Description BTIF storage API - Populates auto pair device list
1577 ** Returns BT_STATUS_SUCCESS if the auto pair blacklist is successfully populated
1578 ** BT_STATUS_FAIL otherwise
1580 *******************************************************************************/
1581 bt_status_t btif_storage_load_autopair_device_list() {
1582 // Configuration has already been loaded. No need to reload.
1583 if (btif_config_exist("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, NULL)) {
1584 return BT_STATUS_SUCCESS;
1587 static const char *key_names[] = {
1588 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR,
1589 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME,
1590 BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST,
1591 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME,
1592 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR,
1595 config_t *config = config_new(BTIF_AUTO_PAIR_CONF_FILE);
1597 ALOGE("%s failed to open auto pair blacklist conf file '%s'.", __func__, BTIF_AUTO_PAIR_CONF_FILE);
1598 return BT_STATUS_FAIL;
1601 for (size_t i = 0; i < ARRAY_SIZE(key_names); ++i) {
1602 const char *value = config_get_string(config, CONFIG_DEFAULT_SECTION, key_names[i], NULL);
1604 btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, key_names[i], value);
1608 config_free(config);
1609 return BT_STATUS_SUCCESS;
1612 /*******************************************************************************
1614 ** Function btif_storage_is_device_autopair_blacklisted
1616 ** Description BTIF storage API Checks if the given device is blacklisted for auto pairing
1618 ** Returns TRUE if the device is found in the auto pair blacklist
1621 *******************************************************************************/
1622 BOOLEAN btif_storage_is_device_autopair_blacklisted(bt_bdaddr_t *remote_bd_addr)
1629 char value[BTIF_STORAGE_MAX_LINE_SZ];
1630 int value_size = sizeof(value);
1632 bd2str(remote_bd_addr, &bdstr);
1634 /* Consider only Lower Address Part from BD Address */
1637 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1638 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR, value, &value_size))
1640 if (strcasestr(value,bdstr) != NULL)
1644 dev_name_str = BTM_SecReadDevName((remote_bd_addr->address));
1646 if (dev_name_str != NULL)
1648 value_size = sizeof(value);
1649 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1650 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME, value, &value_size))
1652 if (strstr(value,dev_name_str) != NULL)
1655 value_size = sizeof(value);
1656 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1657 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME, value, &value_size))
1659 token = strtok(value, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1660 while (token != NULL)
1662 if (strstr(dev_name_str, token) != NULL)
1665 token = strtok(NULL, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1669 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1670 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, value, &value_size))
1672 if (strstr(value,bdstr) != NULL)
1678 /*******************************************************************************
1680 ** Function btif_storage_add_device_to_autopair_blacklist
1682 ** Description BTIF storage API - Add a remote device to the auto pairing blacklist
1684 ** Returns BT_STATUS_SUCCESS if the device is successfully added to the auto pair blacklist
1685 ** BT_STATUS_FAIL otherwise
1687 *******************************************************************************/
1688 bt_status_t btif_storage_add_device_to_autopair_blacklist(bt_bdaddr_t *remote_bd_addr)
1692 char linebuf[BTIF_STORAGE_MAX_LINE_SZ+20];
1693 char input_value [20];
1695 bd2str(remote_bd_addr, &bdstr);
1696 strlcpy(input_value, (char*)bdstr, sizeof(input_value));
1697 strlcat(input_value,BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR, sizeof(input_value));
1699 int line_size = sizeof(linebuf);
1700 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1701 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf, &line_size))
1703 /* Append this address to the dynamic List of BD address */
1704 strncat (linebuf, input_value, BTIF_STORAGE_MAX_LINE_SZ);
1708 strncpy( linebuf,input_value, BTIF_STORAGE_MAX_LINE_SZ);
1711 /* Write back the key value */
1712 ret = btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1713 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf);
1715 return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1718 /*******************************************************************************
1720 ** Function btif_storage_is_fixed_pin_zeros_keyboard
1722 ** Description BTIF storage API - checks if this device has fixed PIN key device list
1724 ** Returns TRUE if the device is found in the fixed pin keyboard device list
1727 *******************************************************************************/
1728 BOOLEAN btif_storage_is_fixed_pin_zeros_keyboard(bt_bdaddr_t *remote_bd_addr)
1734 char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1736 bd2str(remote_bd_addr, &bdstr);
1738 /*consider on LAP part of BDA string*/
1741 int line_size = sizeof(linebuf);
1742 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1743 BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST, linebuf, &line_size))
1745 if (strcasestr(linebuf,bdstr) != NULL)
1752 /*******************************************************************************
1754 ** Function btif_storage_set_dmt_support_type
1756 ** Description Sets DMT support status for a remote device
1758 ** Returns BT_STATUS_SUCCESS if config update is successful
1759 ** BT_STATUS_FAIL otherwise
1761 *******************************************************************************/
1763 bt_status_t btif_storage_set_dmt_support_type(const bt_bdaddr_t *remote_bd_addr,
1764 BOOLEAN dmt_supported)
1767 bdstr_t bdstr = {0};
1770 bd2str(remote_bd_addr, &bdstr);
1774 BTIF_TRACE_ERROR1("%s NULL BD Address", __FUNCTION__);
1775 return BT_STATUS_FAIL;
1778 ret = btif_config_set_int("Remote", bdstr,"DMTSupported", (int)dmt_supported);
1779 return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1783 /*******************************************************************************
1785 ** Function btif_storage_is_dmt_supported_device
1787 ** Description checks if a device supports Dual mode topology
1789 ** Returns TRUE if remote address is valid and supports DMT else FALSE
1791 *******************************************************************************/
1793 BOOLEAN btif_storage_is_dmt_supported_device(const bt_bdaddr_t *remote_bd_addr)
1795 int dmt_supported = 0;
1796 bdstr_t bdstr = {0};
1798 bd2str(remote_bd_addr, &bdstr);
1802 bd2str(remote_bd_addr, &bdstr);
1806 BTIF_TRACE_ERROR1("%s NULL BD Address", __FUNCTION__);
1810 btif_config_get_int("Remote", bdstr,"DMTSupported", &dmt_supported);
1812 return dmt_supported == 1 ? TRUE:FALSE;