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_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
225 if(prop->len <= 0 || prop->len > (int)sizeof(value) - 1)
227 BTIF_TRACE_ERROR("property type:%d, len:%d is invalid", prop->type, prop->len);
232 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
233 btif_config_set_int("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_ERROR("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_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
322 BTIF_TRACE_ERROR("property type:%d, len:%d is invalid", prop->type, prop->len);
328 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
329 if(prop->len >= (int)sizeof(int))
330 ret = btif_config_get_int("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_ERROR("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 (BLE_INCLUDED == TRUE)
461 if((btif_in_fetch_bonded_ble_device(bdstr, FALSE, NULL) != BT_STATUS_SUCCESS)
462 && (!bt_linkkey_file_found))
464 BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", bdstr);
465 return BT_STATUS_FAIL;
468 if((!bt_linkkey_file_found))
470 BTIF_TRACE_DEBUG("Remote device:%s, no link key found", bdstr);
471 return BT_STATUS_FAIL;
474 return BT_STATUS_SUCCESS;
477 /*******************************************************************************
479 ** Function btif_in_fetch_bonded_devices
481 ** Description Internal helper function to fetch the bonded devices
484 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
486 *******************************************************************************/
487 static bt_status_t btif_in_fetch_bonded_devices(btif_bonded_devices_t *p_bonded_devices, int add)
489 BTIF_TRACE_DEBUG("in add:%d", add);
490 memset(p_bonded_devices, 0, sizeof(btif_bonded_devices_t));
492 char kname[128], vname[128];
495 kname_size = sizeof(kname);
498 BOOLEAN bt_linkkey_file_found=FALSE;
503 kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
504 BTIF_TRACE_DEBUG("Remote device:%s, size:%d", kname, kname_size);
505 int type = BTIF_CFG_TYPE_BIN;
507 int size = sizeof(link_key);
508 if(btif_config_get("Remote", kname, "LinkKey", (char*)link_key, &size, &type))
511 if(btif_config_get_int("Remote", kname, "LinkKeyType", &linkkey_type))
514 //btif_config_get_int("Remote", kname, "PinLength", &pin_len))
516 str2bd(kname, &bd_addr);
519 DEV_CLASS dev_class = {0, 0, 0};
521 if(btif_config_get_int("Remote", kname, "DevClass", &cod))
522 uint2devclass((UINT32)cod, dev_class);
523 BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0, (UINT8)linkkey_type, 0);
525 #if BLE_INCLUDED == TRUE
526 if (btif_config_get_int("Remote", kname, "DevType", &device_type) &&
527 (device_type == BT_DEVICE_TYPE_DUMO) )
529 btif_gatts_add_bonded_dev_from_nv(bd_addr.address);
533 bt_linkkey_file_found = TRUE;
534 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
538 #if (BLE_INCLUDED == TRUE)
539 bt_linkkey_file_found = FALSE;
541 BTIF_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid", kname);
545 #if (BLE_INCLUDED == TRUE)
546 if(!(btif_in_fetch_bonded_ble_device(kname,add, p_bonded_devices)) && (!bt_linkkey_file_found))
548 BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", kname);
551 if(!bt_linkkey_file_found)
552 BTIF_TRACE_DEBUG("Remote device:%s, no link key", kname);
554 kname_size = sizeof(kname);
557 BTIF_TRACE_DEBUG("out");
558 return BT_STATUS_SUCCESS;
561 /************************************************************************************
563 ************************************************************************************/
565 /************************************************************************************
567 ************************************************************************************/
569 /** functions are synchronous.
570 * functions can be called by both internal modules such as BTIF_DM and by external entiries from HAL via BTIF_context_switch
571 * For OUT parameters, caller is expected to provide the memory.
572 * Caller is expected to provide a valid pointer to 'property->value' based on the property->type
574 /*******************************************************************************
576 ** Function btif_storage_get_adapter_property
578 ** Description BTIF storage API - Fetches the adapter property->type
579 ** from NVRAM and fills property->val.
580 ** Caller should provide memory for property->val and
581 ** set the property->val
583 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
584 ** BT_STATUS_FAIL otherwise
586 *******************************************************************************/
587 bt_status_t btif_storage_get_adapter_property(bt_property_t *property)
590 /* Special handling for adapter BD_ADDR and BONDED_DEVICES */
591 if (property->type == BT_PROPERTY_BDADDR)
594 bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)property->val;
595 /* This has been cached in btif. Just fetch it from there */
596 memcpy(bd_addr, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
597 property->len = sizeof(bt_bdaddr_t);
598 return BT_STATUS_SUCCESS;
600 else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES)
602 btif_bonded_devices_t bonded_devices;
604 btif_in_fetch_bonded_devices(&bonded_devices, 0);
606 BTIF_TRACE_DEBUG("%s: Number of bonded devices: %d Property:BT_PROPERTY_ADAPTER_BONDED_DEVICES", __FUNCTION__, bonded_devices.num_devices);
608 if (bonded_devices.num_devices > 0)
610 property->len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
611 memcpy(property->val, bonded_devices.devices, property->len);
614 /* if there are no bonded_devices, then length shall be 0 */
615 return BT_STATUS_SUCCESS;
617 else if (property->type == BT_PROPERTY_UUIDS)
619 /* publish list of local supported services */
620 bt_uuid_t *p_uuid = (bt_uuid_t*)property->val;
621 uint32_t num_uuids = 0;
624 tBTA_SERVICE_MASK service_mask = btif_get_enabled_services_mask();
625 BTIF_TRACE_ERROR("%s service_mask:0x%x", __FUNCTION__, service_mask);
626 for (i=0; i < BTA_MAX_SERVICE_ID; i++)
628 /* This should eventually become a function when more services are enabled */
630 &(tBTA_SERVICE_MASK)(1 << i))
634 case BTA_HFP_SERVICE_ID:
636 uuid16_to_uuid128(UUID_SERVCLASS_AG_HANDSFREE,
640 /* intentional fall through: Send both BFP & HSP UUIDs if HFP is enabled */
641 case BTA_HSP_SERVICE_ID:
643 uuid16_to_uuid128(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,
647 case BTA_A2DP_SERVICE_ID:
649 uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SOURCE,
653 case BTA_HFP_HS_SERVICE_ID:
655 uuid16_to_uuid128(UUID_SERVCLASS_HF_HANDSFREE,
662 property->len = (num_uuids)*sizeof(bt_uuid_t);
663 return BT_STATUS_SUCCESS;
666 /* fall through for other properties */
667 if(!cfg2prop(NULL, property))
669 return btif_dm_get_adapter_property(property);
671 return BT_STATUS_SUCCESS;
674 /*******************************************************************************
676 ** Function btif_storage_set_adapter_property
678 ** Description BTIF storage API - Stores the adapter property
681 ** Returns BT_STATUS_SUCCESS if the store was successful,
682 ** BT_STATUS_FAIL otherwise
684 *******************************************************************************/
685 bt_status_t btif_storage_set_adapter_property(bt_property_t *property)
687 return prop2cfg(NULL, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
690 /*******************************************************************************
692 ** Function btif_storage_get_remote_device_property
694 ** Description BTIF storage API - Fetches the remote device property->type
695 ** from NVRAM and fills property->val.
696 ** Caller should provide memory for property->val and
697 ** set the property->val
699 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
700 ** BT_STATUS_FAIL otherwise
702 *******************************************************************************/
703 bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t *remote_bd_addr,
704 bt_property_t *property)
706 return cfg2prop(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
708 /*******************************************************************************
710 ** Function btif_storage_set_remote_device_property
712 ** Description BTIF storage API - Stores the remote device property
715 ** Returns BT_STATUS_SUCCESS if the store was successful,
716 ** BT_STATUS_FAIL otherwise
718 *******************************************************************************/
719 bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t *remote_bd_addr,
720 bt_property_t *property)
722 return prop2cfg(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
725 /*******************************************************************************
727 ** Function btif_storage_add_remote_device
729 ** Description BTIF storage API - Adds a newly discovered device to NVRAM
730 ** along with the timestamp. Also, stores the various
731 ** properties - RSSI, BDADDR, NAME (if found in EIR)
733 ** Returns BT_STATUS_SUCCESS if the store was successful,
734 ** BT_STATUS_FAIL otherwise
736 *******************************************************************************/
737 bt_status_t btif_storage_add_remote_device(bt_bdaddr_t *remote_bd_addr,
738 uint32_t num_properties,
739 bt_property_t *properties)
742 /* TODO: If writing a property, fails do we go back undo the earlier
743 * written properties? */
744 for (i=0; i < num_properties; i++)
746 /* Ignore the RSSI as this is not stored in DB */
747 if (properties[i].type == BT_PROPERTY_REMOTE_RSSI)
750 /* BD_ADDR for remote device needs special handling as we also store timestamp */
751 if (properties[i].type == BT_PROPERTY_BDADDR)
753 bt_property_t addr_prop;
754 memcpy(&addr_prop, &properties[i], sizeof(bt_property_t));
755 addr_prop.type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP;
756 btif_storage_set_remote_device_property(remote_bd_addr,
761 btif_storage_set_remote_device_property(remote_bd_addr,
765 return BT_STATUS_SUCCESS;
768 /*******************************************************************************
770 ** Function btif_storage_add_bonded_device
772 ** Description BTIF storage API - Adds the newly bonded device to NVRAM
773 ** along with the link-key, Key type and Pin key length
775 ** Returns BT_STATUS_SUCCESS if the store was successful,
776 ** BT_STATUS_FAIL otherwise
778 *******************************************************************************/
780 bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
786 bd2str(remote_bd_addr, &bdstr);
787 int ret = btif_config_set_int("Remote", bdstr, "LinkKeyType", (int)key_type);
788 ret &= btif_config_set_int("Remote", bdstr, "PinLength", (int)pin_length);
789 ret &= btif_config_set("Remote", bdstr, "LinkKey", (const char*)link_key, sizeof(LINK_KEY), BTIF_CFG_TYPE_BIN);
790 /* write bonded info immediately */
792 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
795 /*******************************************************************************
797 ** Function btif_storage_remove_bonded_device
799 ** Description BTIF storage API - Deletes the bonded device from NVRAM
801 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
802 ** BT_STATUS_FAIL otherwise
804 *******************************************************************************/
805 bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
808 bd2str(remote_bd_addr, &bdstr);
809 BTIF_TRACE_DEBUG("in bd addr:%s", bdstr);
811 if(btif_config_exist("Remote", bdstr, "LinkKeyType"))
812 ret &= btif_config_remove("Remote", bdstr, "LinkKeyType");
813 if(btif_config_exist("Remote", bdstr, "PinLength"))
814 ret &= btif_config_remove("Remote", bdstr, "PinLength");
815 if(btif_config_exist("Remote", bdstr, "LinkKey"))
816 ret &= btif_config_remove("Remote", bdstr, "LinkKey");
817 /* write bonded info immediately */
819 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
823 /*******************************************************************************
825 ** Function btif_storage_load_bonded_devices
827 ** Description BTIF storage API - Loads all the bonded devices from NVRAM
828 ** and adds to the BTA.
829 ** Additionally, this API also invokes the adaper_properties_cb
830 ** and remote_device_properties_cb for each of the bonded devices.
832 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
834 *******************************************************************************/
835 bt_status_t btif_storage_load_bonded_devices(void)
838 btif_bonded_devices_t bonded_devices;
840 bt_property_t adapter_props[6];
841 uint32_t num_props = 0;
842 bt_property_t remote_properties[8];
844 bt_bdname_t name, alias;
846 uint32_t disc_timeout;
847 bt_bdaddr_t *devices_list;
848 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
849 bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
850 uint32_t cod, devtype;
852 btif_in_fetch_bonded_devices(&bonded_devices, 1);
854 /* Now send the adapter_properties_cb with all adapter_properties */
856 memset(adapter_props, 0, sizeof(adapter_props));
859 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDADDR, &addr, sizeof(addr),
860 adapter_props[num_props]);
864 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDNAME, &name, sizeof(name),
865 adapter_props[num_props]);
869 /* TODO: At the time of BT on, always report the scan mode as 0 irrespective
870 of the scan_mode during the previous enable cycle.
871 This needs to be re-visited as part of the app/stack enable sequence
873 mode = BT_SCAN_MODE_NONE;
874 adapter_props[num_props].type = BT_PROPERTY_ADAPTER_SCAN_MODE;
875 adapter_props[num_props].len = sizeof(mode);
876 adapter_props[num_props].val = &mode;
880 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
881 &disc_timeout, sizeof(disc_timeout),
882 adapter_props[num_props]);
886 devices_list = (bt_bdaddr_t*)malloc(sizeof(bt_bdaddr_t)*bonded_devices.num_devices);
887 adapter_props[num_props].type = BT_PROPERTY_ADAPTER_BONDED_DEVICES;
888 adapter_props[num_props].len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
889 adapter_props[num_props].val = devices_list;
890 for (i=0; i < bonded_devices.num_devices; i++)
892 memcpy(devices_list + i, &bonded_devices.devices[i], sizeof(bt_bdaddr_t));
897 BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_UUIDS,
898 local_uuids, sizeof(local_uuids),
899 adapter_props[num_props]);
902 btif_adapter_properties_evt(BT_STATUS_SUCCESS, num_props, adapter_props);
907 BTIF_TRACE_EVENT("%s: %d bonded devices found", __FUNCTION__, bonded_devices.num_devices);
910 for (i = 0; i < bonded_devices.num_devices; i++)
912 bt_bdaddr_t *p_remote_addr;
915 p_remote_addr = &bonded_devices.devices[i];
916 memset(remote_properties, 0, sizeof(remote_properties));
917 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_BDNAME,
919 remote_properties[num_props]);
922 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_REMOTE_FRIENDLY_NAME,
923 &alias, sizeof(alias),
924 remote_properties[num_props]);
927 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_CLASS_OF_DEVICE,
929 remote_properties[num_props]);
932 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_TYPE_OF_DEVICE,
933 &devtype, sizeof(devtype),
934 remote_properties[num_props]);
937 BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_UUIDS,
938 remote_uuids, sizeof(remote_uuids),
939 remote_properties[num_props]);
942 btif_remote_properties_evt(BT_STATUS_SUCCESS, p_remote_addr,
943 num_props, remote_properties);
946 return BT_STATUS_SUCCESS;
949 #if (BLE_INCLUDED == TRUE)
951 /*******************************************************************************
953 ** Function btif_storage_add_ble_bonding_key
955 ** Description BTIF storage API - Adds the newly bonded device to NVRAM
956 ** along with the ble-key, Key type and Pin key length
958 ** Returns BT_STATUS_SUCCESS if the store was successful,
959 ** BT_STATUS_FAIL otherwise
961 *******************************************************************************/
963 bt_status_t btif_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
969 bd2str(remote_bd_addr, &bdstr);
973 case BTIF_DM_LE_KEY_PENC:
974 name = "LE_KEY_PENC";
976 case BTIF_DM_LE_KEY_PID:
979 case BTIF_DM_LE_KEY_PCSRK:
980 name = "LE_KEY_PCSRK";
982 case BTIF_DM_LE_KEY_LENC:
983 name = "LE_KEY_LENC";
985 case BTIF_DM_LE_KEY_LCSRK:
986 name = "LE_KEY_LCSRK";
989 return BT_STATUS_FAIL;
991 int ret = btif_config_set("Remote", bdstr, name, (const char*)key, (int)key_length, BTIF_CFG_TYPE_BIN);
993 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
996 /*******************************************************************************
998 ** Function btif_storage_get_ble_bonding_key
1002 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
1003 ** BT_STATUS_FAIL otherwise
1005 *******************************************************************************/
1006 bt_status_t btif_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
1012 bd2str(remote_bd_addr, &bdstr);
1014 int type = BTIF_CFG_TYPE_BIN;
1017 case BTIF_DM_LE_KEY_PENC:
1018 name = "LE_KEY_PENC";
1020 case BTIF_DM_LE_KEY_PID:
1021 name = "LE_KEY_PID";
1023 case BTIF_DM_LE_KEY_PCSRK:
1024 name = "LE_KEY_PCSRK";
1026 case BTIF_DM_LE_KEY_LENC:
1027 name = "LE_KEY_LENC";
1029 case BTIF_DM_LE_KEY_LCSRK:
1030 name = "LE_KEY_LCSRK";
1033 return BT_STATUS_FAIL;
1035 int ret = btif_config_get("Remote", bdstr, name, key_value, &key_length, &type);
1036 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1040 /*******************************************************************************
1042 ** Function btif_storage_remove_ble_keys
1044 ** Description BTIF storage API - Deletes the bonded device from NVRAM
1046 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1047 ** BT_STATUS_FAIL otherwise
1049 *******************************************************************************/
1050 bt_status_t btif_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr)
1053 bd2str(remote_bd_addr, &bdstr);
1054 BTIF_TRACE_DEBUG(" %s in bd addr:%s",__FUNCTION__, bdstr);
1056 if(btif_config_exist("Remote", bdstr, "LE_KEY_PENC"))
1057 ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PENC");
1058 if(btif_config_exist("Remote", bdstr, "LE_KEY_PID"))
1059 ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PID");
1060 if(btif_config_exist("Remote", bdstr, "LE_KEY_PCSRK"))
1061 ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PCSRK");
1062 if(btif_config_exist("Remote", bdstr, "LE_KEY_LENC"))
1063 ret &= btif_config_remove("Remote", bdstr, "LE_KEY_LENC");
1064 if(btif_config_exist("Remote", bdstr, "LE_KEY_LCSRK"))
1065 ret &= btif_config_remove("Remote", bdstr, "LE_KEY_LCSRK");
1067 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1070 /*******************************************************************************
1072 ** Function btif_storage_add_ble_local_key
1074 ** Description BTIF storage API - Adds the ble key to NVRAM
1076 ** Returns BT_STATUS_SUCCESS if the store was successful,
1077 ** BT_STATUS_FAIL otherwise
1079 *******************************************************************************/
1080 bt_status_t btif_storage_add_ble_local_key(char *key,
1087 case BTIF_DM_LE_LOCAL_KEY_IR:
1088 name = "LE_LOCAL_KEY_IR";
1090 case BTIF_DM_LE_LOCAL_KEY_IRK:
1091 name = "LE_LOCAL_KEY_IRK";
1093 case BTIF_DM_LE_LOCAL_KEY_DHK:
1094 name = "LE_LOCAL_KEY_DHK";
1096 case BTIF_DM_LE_LOCAL_KEY_ER:
1097 name = "LE_LOCAL_KEY_ER";
1100 return BT_STATUS_FAIL;
1102 int ret = btif_config_set("Local", "Adapter", name, (const char*)key, key_length, BTIF_CFG_TYPE_BIN);
1104 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1107 /*******************************************************************************
1109 ** Function btif_storage_get_ble_local_key
1113 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
1114 ** BT_STATUS_FAIL otherwise
1116 *******************************************************************************/
1117 bt_status_t btif_storage_get_ble_local_key(UINT8 key_type,
1122 int type = BTIF_CFG_TYPE_BIN;
1125 case BTIF_DM_LE_LOCAL_KEY_IR:
1126 name = "LE_LOCAL_KEY_IR";
1128 case BTIF_DM_LE_LOCAL_KEY_IRK:
1129 name = "LE_LOCAL_KEY_IRK";
1131 case BTIF_DM_LE_LOCAL_KEY_DHK:
1132 name = "LE_LOCAL_KEY_DHK";
1134 case BTIF_DM_LE_LOCAL_KEY_ER:
1135 name = "LE_LOCAL_KEY_ER";
1138 return BT_STATUS_FAIL;
1140 int ret = btif_config_get("Local", "Adapter", name, key_value, &key_length, &type);
1141 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1145 /*******************************************************************************
1147 ** Function btif_storage_remove_ble_local_keys
1149 ** Description BTIF storage API - Deletes the bonded device from NVRAM
1151 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1152 ** BT_STATUS_FAIL otherwise
1154 *******************************************************************************/
1155 bt_status_t btif_storage_remove_ble_local_keys(void)
1158 if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_IR"))
1159 ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_IR");
1160 if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_IRK"))
1161 ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_IRK");
1162 if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_DHK"))
1163 ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_DHK");
1164 if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_ER"))
1165 ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_ER");
1167 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1170 bt_status_t btif_in_fetch_bonded_ble_device(char *remote_bd_addr,int add, btif_bonded_devices_t *p_bonded_devices)
1176 bt_bdaddr_t bd_addr;
1177 BD_ADDR bta_bd_addr;
1178 BOOLEAN is_device_added =FALSE;
1179 BOOLEAN key_found = FALSE;
1180 tBTA_LE_KEY_VALUE *p;
1182 if(!btif_config_get_int("Remote", remote_bd_addr,"DevType", &device_type))
1183 return BT_STATUS_FAIL;
1184 if(device_type == BT_DEVICE_TYPE_BLE)
1186 BTIF_TRACE_DEBUG("%s %s found a BLE device", __FUNCTION__,remote_bd_addr);
1187 str2bd(remote_bd_addr, &bd_addr);
1188 bdcpy(bta_bd_addr, bd_addr.address);
1189 if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS)
1191 return BT_STATUS_FAIL;
1194 memset(buf, 0, sizeof(buf));
1195 if (btif_storage_get_ble_bonding_key(&bd_addr,
1196 BTIF_DM_LE_KEY_PENC,
1198 sizeof(btif_dm_ble_penc_keys_t)) == BT_STATUS_SUCCESS)
1202 if (!is_device_added)
1204 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1205 is_device_added = TRUE;
1207 p = (tBTA_LE_KEY_VALUE *)buf;
1208 for (i=0; i<16; i++)
1210 BTIF_TRACE_DEBUG("penc_key.ltk[%d]=0x%02x",i,p->penc_key.ltk[i]);
1214 BTIF_TRACE_DEBUG("penc_key.rand[%d]=0x%02x",i,p->penc_key.rand[i]);
1216 BTIF_TRACE_DEBUG("p->penc_key.ediv=0x%04x",p->penc_key.ediv);
1217 BTIF_TRACE_DEBUG("p->penc_key.sec_level=0x%02x",p->penc_key.sec_level);
1218 BTIF_TRACE_DEBUG("p->penc_key.key_size=0x%02x",p->penc_key.key_size);
1219 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PENC);
1224 memset(buf, 0, sizeof(buf));
1225 if (btif_storage_get_ble_bonding_key(&bd_addr,
1228 sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1232 if (!is_device_added)
1234 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1235 is_device_added = TRUE;
1237 p = (tBTA_LE_KEY_VALUE *)buf;
1238 for (i=0; i<16; i++)
1240 BTIF_TRACE_DEBUG("p->pid_key[%d]=0x%02x",i,p->pid_key.irk[i]);
1243 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PID);
1248 memset(buf, 0, sizeof(buf));
1249 if (btif_storage_get_ble_bonding_key(&bd_addr,
1250 BTIF_DM_LE_KEY_PCSRK,
1252 sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1256 if (!is_device_added)
1258 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1259 is_device_added = TRUE;
1262 p = (tBTA_LE_KEY_VALUE *)buf;
1263 for (i=0; i<16; i++)
1265 BTIF_TRACE_DEBUG("p->pcsrk_key.csrk[%d]=0x%02x",i, p->psrk_key.csrk[i]);
1267 BTIF_TRACE_DEBUG("p->pcsrk_key.counter=0x%08x",p->psrk_key.counter);
1268 BTIF_TRACE_DEBUG("p->pcsrk_key.sec_level=0x%02x",p->psrk_key.sec_level);
1270 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PCSRK);
1275 memset(buf, 0, sizeof(buf));
1276 if (btif_storage_get_ble_bonding_key(&bd_addr,
1277 BTIF_DM_LE_KEY_LENC,
1279 sizeof(btif_dm_ble_lenc_keys_t)) == BT_STATUS_SUCCESS)
1283 if (!is_device_added)
1285 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1286 is_device_added = TRUE;
1288 p = (tBTA_LE_KEY_VALUE *)buf;
1289 BTIF_TRACE_DEBUG("p->lenc_key.div=0x%04x",p->lenc_key.div);
1290 BTIF_TRACE_DEBUG("p->lenc_key.key_size=0x%02x",p->lenc_key.key_size);
1291 BTIF_TRACE_DEBUG("p->lenc_key.sec_level=0x%02x",p->lenc_key.sec_level);
1293 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LENC);
1298 memset(buf, 0, sizeof(buf));
1299 if (btif_storage_get_ble_bonding_key(&bd_addr,
1300 BTIF_DM_LE_KEY_LCSRK,
1302 sizeof(btif_dm_ble_lcsrk_keys_t)) == BT_STATUS_SUCCESS)
1306 if (!is_device_added)
1308 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1309 is_device_added = TRUE;
1311 p = (tBTA_LE_KEY_VALUE *)buf;
1312 BTIF_TRACE_DEBUG("p->lcsrk_key.div=0x%04x",p->lcsrk_key.div);
1313 BTIF_TRACE_DEBUG("p->lcsrk_key.counter=0x%08x",p->lcsrk_key.counter);
1314 BTIF_TRACE_DEBUG("p->lcsrk_key.sec_level=0x%02x",p->lcsrk_key.sec_level);
1316 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LCSRK);
1321 /* Fill in the bonded devices */
1322 if (is_device_added)
1324 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
1325 btif_gatts_add_bonded_dev_from_nv(bta_bd_addr);
1329 return BT_STATUS_SUCCESS;
1331 return BT_STATUS_FAIL;
1333 return BT_STATUS_FAIL;
1336 bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1340 bd2str(remote_bd_addr, &bdstr);
1341 int ret = btif_config_set_int("Remote", bdstr, "AddrType", (int)addr_type);
1343 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1346 /*******************************************************************************
1348 ** Function btif_storage_get_remote_addr_type
1350 ** Description BTIF storage API - Fetches the remote addr type
1352 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
1353 ** BT_STATUS_FAIL otherwise
1355 *******************************************************************************/
1356 bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1360 bd2str(remote_bd_addr, &bdstr);
1361 int ret = btif_config_get_int("Remote", bdstr, "AddrType", addr_type);
1362 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1365 /*******************************************************************************
1367 ** Function btif_storage_add_hid_device_info
1369 ** Description BTIF storage API - Adds the hid information of bonded hid devices-to NVRAM
1371 ** Returns BT_STATUS_SUCCESS if the store was successful,
1372 ** BT_STATUS_FAIL otherwise
1374 *******************************************************************************/
1376 bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr,
1377 UINT16 attr_mask, UINT8 sub_class,
1378 UINT8 app_id, UINT16 vendor_id,
1379 UINT16 product_id, UINT16 version,
1380 UINT8 ctry_code, UINT16 ssr_max_latency,
1381 UINT16 ssr_min_tout, UINT16 dl_len, UINT8 *dsc_list)
1384 BTIF_TRACE_DEBUG("btif_storage_add_hid_device_info:");
1385 bd2str(remote_bd_addr, &bdstr);
1386 btif_config_set_int("Remote", bdstr, "HidAttrMask", attr_mask);
1387 btif_config_set_int("Remote", bdstr, "HidSubClass", sub_class);
1388 btif_config_set_int("Remote", bdstr, "HidAppId", app_id);
1389 btif_config_set_int("Remote", bdstr, "HidVendorId", vendor_id);
1390 btif_config_set_int("Remote", bdstr, "HidProductId", product_id);
1391 btif_config_set_int("Remote", bdstr, "HidVersion", version);
1392 btif_config_set_int("Remote", bdstr, "HidCountryCode", ctry_code);
1393 btif_config_set_int("Remote", bdstr, "HidSSRMaxLatency", ssr_max_latency);
1394 btif_config_set_int("Remote", bdstr, "HidSSRMinTimeout", ssr_min_tout);
1396 btif_config_set("Remote", bdstr, "HidDescriptor", (const char*)dsc_list, dl_len,
1399 return BT_STATUS_SUCCESS;
1402 /*******************************************************************************
1404 ** Function btif_storage_load_bonded_hid_info
1406 ** Description BTIF storage API - Loads hid info for all the bonded devices from NVRAM
1407 ** and adds those devices to the BTA_HH.
1409 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
1411 *******************************************************************************/
1412 bt_status_t btif_storage_load_bonded_hid_info(void)
1414 bt_bdaddr_t bd_addr;
1415 tBTA_HH_DEV_DSCP_INFO dscp_info;
1421 char kname[128], vname[128];
1424 kname_size = sizeof(kname);
1427 memset(&dscp_info, 0, sizeof(dscp_info));
1430 kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
1431 BTIF_TRACE_DEBUG("Remote device:%s, size:%d", kname, kname_size);
1433 if(btif_in_fetch_bonded_device(kname) == BT_STATUS_SUCCESS)
1435 if(btif_config_get_int("Remote", kname, "HidAttrMask", &value))
1437 attr_mask = (uint16_t)value;
1439 btif_config_get_int("Remote", kname, "HidSubClass", &value);
1440 sub_class = (uint8_t)value;
1442 btif_config_get_int("Remote", kname, "HidAppId", &value);
1443 app_id = (uint8_t)value;
1445 btif_config_get_int("Remote", kname, "HidVendorId", &value);
1446 dscp_info.vendor_id = (uint16_t) value;
1448 btif_config_get_int("Remote", kname, "HidProductId", &value);
1449 dscp_info.product_id = (uint16_t) value;
1451 btif_config_get_int("Remote", kname, "HidVersion", &value);
1452 dscp_info.version = (uint8_t) value;
1454 btif_config_get_int("Remote", kname, "HidCountryCode", &value);
1455 dscp_info.ctry_code = (uint8_t) value;
1458 btif_config_get_int("Remote", kname, "HidSSRMaxLatency", &value);
1459 dscp_info.ssr_max_latency = (uint16_t) value;
1462 btif_config_get_int("Remote", kname, "HidSSRMinTimeout", &value);
1463 dscp_info.ssr_min_tout = (uint16_t) value;
1467 btif_config_get("Remote", kname, "HidDescriptor", NULL, &len, &type);
1470 dscp_info.descriptor.dl_len = (uint16_t)len;
1471 dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len);
1472 btif_config_get("Remote", kname, "HidDescriptor", (char*)dscp_info.descriptor.dsc_list, &len, &type);
1474 str2bd(kname, &bd_addr);
1475 // add extracted information to BTA HH
1476 if (btif_hh_add_added_dev(bd_addr,attr_mask))
1478 BTA_HhAddDev(bd_addr.address, attr_mask, sub_class,
1485 if(btif_config_get_int("Remote", kname, "HidAttrMask", &value))
1487 btif_storage_remove_hid_info(&bd_addr);
1488 str2bd(kname, &bd_addr);
1491 } while(kpos != -1);
1493 return BT_STATUS_SUCCESS;
1496 /*******************************************************************************
1498 ** Function btif_storage_remove_hid_info
1500 ** Description BTIF storage API - Deletes the bonded hid device info from NVRAM
1502 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1503 ** BT_STATUS_FAIL otherwise
1505 *******************************************************************************/
1506 bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr)
1511 bd2str(remote_bd_addr, &bdstr);
1513 btif_config_remove("Remote", bdstr, "HidAttrMask");
1514 btif_config_remove("Remote", bdstr, "HidSubClass");
1515 btif_config_remove("Remote", bdstr, "HidAppId");
1516 btif_config_remove("Remote", bdstr, "HidVendorId");
1517 btif_config_remove("Remote", bdstr, "HidProductId");
1518 btif_config_remove("Remote", bdstr, "HidVersion");
1519 btif_config_remove("Remote", bdstr, "HidCountryCode");
1520 btif_config_remove("Remote", bdstr, "HidSSRMaxLatency");
1521 btif_config_remove("Remote", bdstr, "HidSSRMinTimeout");
1522 btif_config_remove("Remote", bdstr, "HidDescriptor");
1524 return BT_STATUS_SUCCESS;
1527 /*******************************************************************************
1529 ** Function btif_storage_read_hl_apps_cb
1531 ** Description BTIF storage API - Read HL application control block from NVRAM
1533 ** Returns BT_STATUS_SUCCESS if the operation was successful,
1534 ** BT_STATUS_FAIL otherwise
1536 *******************************************************************************/
1537 bt_status_t btif_storage_read_hl_apps_cb(char *value, int value_size)
1539 bt_status_t bt_status = BT_STATUS_SUCCESS;
1540 int read_size=value_size, read_type=BTIF_CFG_TYPE_BIN;
1542 if (!btif_config_exist("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB))
1544 memset(value, 0, value_size);
1545 if (!btif_config_set("Local", BTIF_STORAGE_HL_APP,BTIF_STORAGE_HL_APP_CB,
1546 value, value_size, BTIF_CFG_TYPE_BIN))
1548 bt_status = BT_STATUS_FAIL;
1557 if (!btif_config_get("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB,
1558 value, &read_size, &read_type))
1560 bt_status = BT_STATUS_FAIL;
1564 if ((read_size != value_size) || (read_type != BTIF_CFG_TYPE_BIN) )
1566 BTIF_TRACE_ERROR("%s value_size=%d read_size=%d read_type=%d",
1567 __FUNCTION__, value_size, read_size, read_type);
1568 bt_status = BT_STATUS_FAIL;
1574 BTIF_TRACE_DEBUG("%s status=%d value_size=%d", __FUNCTION__, bt_status, value_size);
1579 /*******************************************************************************
1581 ** Function btif_storage_load_autopair_device_list
1583 ** Description BTIF storage API - Populates auto pair device list
1585 ** Returns BT_STATUS_SUCCESS if the auto pair blacklist is successfully populated
1586 ** BT_STATUS_FAIL otherwise
1588 *******************************************************************************/
1589 bt_status_t btif_storage_load_autopair_device_list() {
1590 // Configuration has already been loaded. No need to reload.
1591 if (btif_config_exist("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, NULL)) {
1592 return BT_STATUS_SUCCESS;
1595 static const char *key_names[] = {
1596 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR,
1597 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME,
1598 BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST,
1599 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME,
1600 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR,
1603 config_t *config = config_new(BTIF_AUTO_PAIR_CONF_FILE);
1605 ALOGE("%s failed to open auto pair blacklist conf file '%s'.", __func__, BTIF_AUTO_PAIR_CONF_FILE);
1606 return BT_STATUS_FAIL;
1609 for (size_t i = 0; i < ARRAY_SIZE(key_names); ++i) {
1610 const char *value = config_get_string(config, CONFIG_DEFAULT_SECTION, key_names[i], NULL);
1612 btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, key_names[i], value);
1616 config_free(config);
1617 return BT_STATUS_SUCCESS;
1620 /*******************************************************************************
1622 ** Function btif_storage_is_device_autopair_blacklisted
1624 ** Description BTIF storage API Checks if the given device is blacklisted for auto pairing
1626 ** Returns TRUE if the device is found in the auto pair blacklist
1629 *******************************************************************************/
1630 BOOLEAN btif_storage_is_device_autopair_blacklisted(bt_bdaddr_t *remote_bd_addr)
1637 char value[BTIF_STORAGE_MAX_LINE_SZ];
1638 int value_size = sizeof(value);
1640 bd2str(remote_bd_addr, &bdstr);
1642 /* Consider only Lower Address Part from BD Address */
1645 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1646 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR, value, &value_size))
1648 if (strcasestr(value,bdstr) != NULL)
1652 dev_name_str = BTM_SecReadDevName((remote_bd_addr->address));
1654 if (dev_name_str != NULL)
1656 value_size = sizeof(value);
1657 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1658 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME, value, &value_size))
1660 if (strstr(value,dev_name_str) != NULL)
1663 value_size = sizeof(value);
1664 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1665 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME, value, &value_size))
1667 token = strtok(value, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1668 while (token != NULL)
1670 if (strstr(dev_name_str, token) != NULL)
1673 token = strtok(NULL, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1677 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1678 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, value, &value_size))
1680 if (strstr(value,bdstr) != NULL)
1686 /*******************************************************************************
1688 ** Function btif_storage_add_device_to_autopair_blacklist
1690 ** Description BTIF storage API - Add a remote device to the auto pairing blacklist
1692 ** Returns BT_STATUS_SUCCESS if the device is successfully added to the auto pair blacklist
1693 ** BT_STATUS_FAIL otherwise
1695 *******************************************************************************/
1696 bt_status_t btif_storage_add_device_to_autopair_blacklist(bt_bdaddr_t *remote_bd_addr)
1700 char linebuf[BTIF_STORAGE_MAX_LINE_SZ+20];
1701 char input_value [20];
1703 bd2str(remote_bd_addr, &bdstr);
1704 strlcpy(input_value, (char*)bdstr, sizeof(input_value));
1705 strlcat(input_value,BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR, sizeof(input_value));
1707 int line_size = sizeof(linebuf);
1708 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1709 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf, &line_size))
1711 /* Append this address to the dynamic List of BD address */
1712 strncat (linebuf, input_value, BTIF_STORAGE_MAX_LINE_SZ);
1716 strncpy( linebuf,input_value, BTIF_STORAGE_MAX_LINE_SZ);
1719 /* Write back the key value */
1720 ret = btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1721 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf);
1723 return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1726 /*******************************************************************************
1728 ** Function btif_storage_is_fixed_pin_zeros_keyboard
1730 ** Description BTIF storage API - checks if this device has fixed PIN key device list
1732 ** Returns TRUE if the device is found in the fixed pin keyboard device list
1735 *******************************************************************************/
1736 BOOLEAN btif_storage_is_fixed_pin_zeros_keyboard(bt_bdaddr_t *remote_bd_addr)
1742 char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1744 bd2str(remote_bd_addr, &bdstr);
1746 /*consider on LAP part of BDA string*/
1749 int line_size = sizeof(linebuf);
1750 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1751 BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST, linebuf, &line_size))
1753 if (strcasestr(linebuf,bdstr) != NULL)
1760 /*******************************************************************************
1762 ** Function btif_storage_set_dmt_support_type
1764 ** Description Sets DMT support status for a remote device
1766 ** Returns BT_STATUS_SUCCESS if config update is successful
1767 ** BT_STATUS_FAIL otherwise
1769 *******************************************************************************/
1771 bt_status_t btif_storage_set_dmt_support_type(const bt_bdaddr_t *remote_bd_addr,
1772 BOOLEAN dmt_supported)
1775 bdstr_t bdstr = {0};
1778 bd2str(remote_bd_addr, &bdstr);
1782 BTIF_TRACE_ERROR("%s NULL BD Address", __FUNCTION__);
1783 return BT_STATUS_FAIL;
1786 ret = btif_config_set_int("Remote", bdstr,"DMTSupported", (int)dmt_supported);
1787 return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1791 /*******************************************************************************
1793 ** Function btif_storage_is_dmt_supported_device
1795 ** Description checks if a device supports Dual mode topology
1797 ** Returns TRUE if remote address is valid and supports DMT else FALSE
1799 *******************************************************************************/
1801 BOOLEAN btif_storage_is_dmt_supported_device(const bt_bdaddr_t *remote_bd_addr)
1803 int dmt_supported = 0;
1804 bdstr_t bdstr = {0};
1806 bd2str(remote_bd_addr, &bdstr);
1810 bd2str(remote_bd_addr, &bdstr);
1814 BTIF_TRACE_ERROR("%s NULL BD Address", __FUNCTION__);
1818 btif_config_get_int("Remote", bdstr,"DMTSupported", &dmt_supported);
1820 return dmt_supported == 1 ? TRUE:FALSE;