1 /******************************************************************************
3 * Copyright (C) 2009-2012 Broadcom Corporation
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 /************************************************************************************
21 * Filename: btif_storage.c
23 * Description: Stores the local BT adapter and remote device properties in
24 * NVRAM storage, typically as xml file in the
36 #include <hardware/bluetooth.h>
37 #include "btif_config.h"
38 #define LOG_TAG "BTIF_STORAGE"
41 #include "btif_storage.h"
42 #include "btif_util.h"
45 #include "bta_hh_api.h"
48 #include <cutils/log.h>
50 /************************************************************************************
52 ************************************************************************************/
54 #define BTIF_STORAGE_PATH_BLUEDROID "/data/misc/bluedroid"
56 //#define BTIF_STORAGE_PATH_ADAPTER_INFO "adapter_info"
57 //#define BTIF_STORAGE_PATH_REMOTE_DEVICES "remote_devices"
58 #define BTIF_STORAGE_PATH_REMOTE_DEVTIME "Timestamp"
59 #define BTIF_STORAGE_PATH_REMOTE_DEVCLASS "DevClass"
60 #define BTIF_STORAGE_PATH_REMOTE_DEVTYPE "DevType"
61 #define BTIF_STORAGE_PATH_REMOTE_NAME "Name"
62 #define BTIF_STORAGE_PATH_REMOTE_VER_MFCT "Manufacturer"
63 #define BTIF_STORAGE_PATH_REMOTE_VER_VER "LmpVer"
64 #define BTIF_STORAGE_PATH_REMOTE_VER_SUBVER "LmpSubVer"
66 //#define BTIF_STORAGE_PATH_REMOTE_LINKKEYS "remote_linkkeys"
67 #define BTIF_STORAGE_PATH_REMOTE_ALIASE "Aliase"
68 #define BTIF_STORAGE_PATH_REMOTE_SERVICE "Service"
69 #define BTIF_STORAGE_PATH_REMOTE_HIDINFO "HidInfo"
70 #define BTIF_STORAGE_KEY_ADAPTER_NAME "Name"
71 #define BTIF_STORAGE_KEY_ADAPTER_SCANMODE "ScanMode"
72 #define BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT "DiscoveryTimeout"
75 #define BTIF_AUTO_PAIR_CONF_FILE "/etc/bluetooth/auto_pair_devlist.conf"
76 #define BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST "AutoPairBlacklist"
77 #define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR "AddressBlacklist"
78 #define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME "ExactNameBlacklist"
79 #define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME "PartialNameBlacklist"
80 #define BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST "FixedPinZerosKeyboardBlacklist"
81 #define BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR "DynamicAddressBlacklist"
83 #define BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR ","
84 #define BTIF_AUTO_PAIR_CONF_SPACE ' '
85 #define BTIF_AUTO_PAIR_CONF_COMMENT '#'
86 #define BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER "="
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_make_filename
191 ** Description Internal helper function to create NVRAM file path
192 ** from address and filename
194 ** Returns NVRAM file path if successfull, NULL otherwise
196 *******************************************************************************/
197 static char* btif_in_make_filename(bt_bdaddr_t *bd_addr, char *fname)
199 static char path[256];
202 if (fname == NULL)return NULL;
205 sprintf(path, "%s/%s/%s", BTIF_STORAGE_PATH_BLUEDROID,
206 bd2str(bd_addr, &bdstr), fname);
211 sprintf(path, "%s/LOCAL/%s", BTIF_STORAGE_PATH_BLUEDROID, fname);
216 /*******************************************************************************
218 ** Function btif_in_split_uuids_string_to_list
220 ** Description Internal helper function to split the string of UUIDs
221 ** read from the NVRAM to an array
225 *******************************************************************************/
226 static void btif_in_split_uuids_string_to_list(char *str, bt_uuid_t *p_uuid,
227 uint32_t *p_num_uuid)
235 //p_needle = strchr(p_start, ';');
236 p_needle = strchr(p_start, ' ');
237 if (p_needle < p_start) break;
238 memset(buf, 0, sizeof(buf));
239 strncpy(buf, p_start, (p_needle-p_start));
240 string_to_uuid(buf, p_uuid + num);
242 p_start = ++p_needle;
244 } while (*p_start != 0);
247 static int prop2cfg(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
251 bd2str(remote_bd_addr, &bdstr);
252 BTIF_TRACE_DEBUG3("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
254 if(prop->len <= 0 || prop->len > (int)sizeof(value) - 1)
256 BTIF_TRACE_ERROR2("property type:%d, len:%d is invalid", prop->type, prop->len);
261 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
262 btif_config_set_int("Remote", bdstr,
263 BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int)time(NULL));
264 static const char* exclude_filter[] =
265 {"LinkKey", "LE_KEY_PENC", "LE_KEY_PID", "LE_KEY_PCSRK", "LE_KEY_LENC", "LE_KEY_LCSRK"};
266 btif_config_filter_remove("Remote", exclude_filter, sizeof(exclude_filter)/sizeof(char*),
267 BTIF_STORAGE_MAX_ALLOWED_REMOTE_DEVICE);
269 case BT_PROPERTY_BDNAME:
270 strncpy(value, (char*)prop->val, prop->len);
271 value[prop->len]='\0';
273 btif_config_set_str("Remote", bdstr,
274 BTIF_STORAGE_PATH_REMOTE_NAME, value);
275 else btif_config_set_str("Local", "Adapter",
276 BTIF_STORAGE_KEY_ADAPTER_NAME, value);
278 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
279 strncpy(value, (char*)prop->val, prop->len);
280 value[prop->len]='\0';
281 btif_config_set_str("Remote", bdstr, BTIF_STORAGE_PATH_REMOTE_ALIASE, value);
283 case BT_PROPERTY_ADAPTER_SCAN_MODE:
284 btif_config_set_int("Local", "Adapter",
285 BTIF_STORAGE_KEY_ADAPTER_SCANMODE, *(int*)prop->val);
287 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
288 btif_config_set_int("Local", "Adapter",
289 BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, *(int*)prop->val);
291 case BT_PROPERTY_CLASS_OF_DEVICE:
292 btif_config_set_int("Remote", bdstr,
293 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, *(int*)prop->val);
295 case BT_PROPERTY_TYPE_OF_DEVICE:
296 btif_config_set_int("Remote", bdstr,
297 BTIF_STORAGE_PATH_REMOTE_DEVTYPE, *(int*)prop->val);
299 case BT_PROPERTY_UUIDS:
304 for (i=0; i < (prop->len)/sizeof(bt_uuid_t); i++)
306 bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val + i;
307 memset(buf, 0, sizeof(buf));
308 uuid_to_string(p_uuid, buf);
310 //strcat(value, ";");
313 btif_config_set_str("Remote", bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value);
317 case BT_PROPERTY_REMOTE_VERSION_INFO:
319 bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
324 btif_config_set_int("Remote", bdstr,
325 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, info->manufacturer);
326 btif_config_set_int("Remote", bdstr,
327 BTIF_STORAGE_PATH_REMOTE_VER_VER, info->version);
328 btif_config_set_int("Remote", bdstr,
329 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, info->sub_ver);
334 BTIF_TRACE_ERROR1("Unknow prop type:%d", prop->type);
339 static int cfg2prop(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
343 bd2str(remote_bd_addr, &bdstr);
344 BTIF_TRACE_DEBUG3("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
347 BTIF_TRACE_ERROR2("property type:%d, len:%d is invalid", prop->type, prop->len);
353 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
354 if(prop->len >= (int)sizeof(int))
355 ret = btif_config_get_int("Remote", bdstr,
356 BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int*)prop->val);
358 case BT_PROPERTY_BDNAME:
362 ret = btif_config_get_str("Remote", bdstr,
363 BTIF_STORAGE_PATH_REMOTE_NAME, (char*)prop->val, &len);
364 else ret = btif_config_get_str("Local", "Adapter",
365 BTIF_STORAGE_KEY_ADAPTER_NAME, (char*)prop->val, &len);
366 if(ret && len && len <= prop->len)
375 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
378 ret = btif_config_get_str("Remote", bdstr,
379 BTIF_STORAGE_PATH_REMOTE_ALIASE, (char*)prop->val, &len);
380 if(ret && len && len <= prop->len)
389 case BT_PROPERTY_ADAPTER_SCAN_MODE:
390 if(prop->len >= (int)sizeof(int))
391 ret = btif_config_get_int("Local", "Adapter",
392 BTIF_STORAGE_KEY_ADAPTER_SCANMODE, (int*)prop->val);
394 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
395 if(prop->len >= (int)sizeof(int))
396 ret = btif_config_get_int("Local", "Adapter",
397 BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, (int*)prop->val);
399 case BT_PROPERTY_CLASS_OF_DEVICE:
400 if(prop->len >= (int)sizeof(int))
401 ret = btif_config_get_int("Remote", bdstr,
402 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, (int*)prop->val);
404 case BT_PROPERTY_TYPE_OF_DEVICE:
405 if(prop->len >= (int)sizeof(int))
406 ret = btif_config_get_int("Remote",
407 bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTYPE, (int*)prop->val);
409 case BT_PROPERTY_UUIDS:
412 int size = sizeof(value);
413 if(btif_config_get_str("Remote", bdstr,
414 BTIF_STORAGE_PATH_REMOTE_SERVICE, value, &size))
416 bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val;
417 uint32_t num_uuids = 0;
418 btif_in_split_uuids_string_to_list(value, p_uuid, &num_uuids);
419 prop->len = num_uuids * sizeof(bt_uuid_t);
429 case BT_PROPERTY_REMOTE_VERSION_INFO:
431 bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
433 if(prop->len >= (int)sizeof(bt_remote_version_t))
435 ret = btif_config_get_int("Remote", bdstr,
436 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, &info->manufacturer);
439 ret = btif_config_get_int("Remote", bdstr,
440 BTIF_STORAGE_PATH_REMOTE_VER_VER, &info->version);
443 ret = btif_config_get_int("Remote", bdstr,
444 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, &info->sub_ver);
449 BTIF_TRACE_ERROR1("Unknow prop type:%d", prop->type);
456 /*******************************************************************************
458 ** Function btif_in_fetch_bonded_devices
460 ** Description Internal helper function to fetch the bonded devices
463 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
465 *******************************************************************************/
466 static bt_status_t btif_in_fetch_bonded_devices(btif_bonded_devices_t *p_bonded_devices, int add)
468 BTIF_TRACE_DEBUG1("in add:%d", add);
469 memset(p_bonded_devices, 0, sizeof(btif_bonded_devices_t));
471 char kname[128], vname[128];
474 kname_size = sizeof(kname);
477 BOOLEAN bt_linkkey_file_found=FALSE;
482 kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
483 BTIF_TRACE_DEBUG2("Remote device:%s, size:%d", kname, kname_size);
484 int type = BTIF_CFG_TYPE_BIN;
486 int size = sizeof(link_key);
487 if(btif_config_get("Remote", kname, "LinkKey", (char*)link_key, &size, &type))
490 if(btif_config_get_int("Remote", kname, "LinkKeyType", &linkkey_type))
493 //btif_config_get_int("Remote", kname, "PinLength", &pin_len))
495 str2bd(kname, &bd_addr);
498 DEV_CLASS dev_class = {0, 0, 0};
500 if(btif_config_get_int("Remote", kname, "DevClass", &cod))
501 uint2devclass((UINT32)cod, dev_class);
502 BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0, (UINT8)linkkey_type, 0);
504 #if BLE_INCLUDED == TRUE
505 if (btif_config_get_int("Remote", kname, "DevType", &device_type) &&
506 (device_type == BT_DEVICE_TYPE_DUMO) )
508 btif_gatts_add_bonded_dev_from_nv(bd_addr.address);
512 bt_linkkey_file_found = TRUE;
513 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
517 #if (BLE_INCLUDED == TRUE)
518 bt_linkkey_file_found = FALSE;
520 BTIF_TRACE_ERROR1("bounded device:%s, LinkKeyType or PinLength is invalid", kname);
524 #if (BLE_INCLUDED == TRUE)
525 if(!(btif_in_fetch_bonded_ble_device(kname,add, p_bonded_devices)) && (!bt_linkkey_file_found))
527 BTIF_TRACE_DEBUG1("Remote device:%s, no link key or ble key found", kname);
530 if(!bt_linkkey_file_found)
531 BTIF_TRACE_DEBUG1("Remote device:%s, no link key", kname);
533 kname_size = sizeof(kname);
536 BTIF_TRACE_DEBUG0("out");
537 return BT_STATUS_SUCCESS;
540 static int hex_str_to_int(const char* str, int size)
547 if (c >= '0' && c <= '9') {
550 else if (c >= 'a' && c <= 'z') {
553 else // (c >= 'A' && c <= 'Z')
563 /************************************************************************************
565 ************************************************************************************/
567 /************************************************************************************
569 ************************************************************************************/
571 /** functions are synchronous.
572 * functions can be called by both internal modules such as BTIF_DM and by external entiries from HAL via BTIF_context_switch
573 * For OUT parameters, caller is expected to provide the memory.
574 * Caller is expected to provide a valid pointer to 'property->value' based on the property->type
576 /*******************************************************************************
578 ** Function btif_storage_get_adapter_property
580 ** Description BTIF storage API - Fetches the adapter property->type
581 ** from NVRAM and fills property->val.
582 ** Caller should provide memory for property->val and
583 ** set the property->val
585 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
586 ** BT_STATUS_FAIL otherwise
588 *******************************************************************************/
589 bt_status_t btif_storage_get_adapter_property(bt_property_t *property)
592 /* Special handling for adapter BD_ADDR and BONDED_DEVICES */
593 if (property->type == BT_PROPERTY_BDADDR)
596 bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)property->val;
597 /* This has been cached in btif. Just fetch it from there */
598 memcpy(bd_addr, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
599 property->len = sizeof(bt_bdaddr_t);
600 return BT_STATUS_SUCCESS;
602 else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES)
604 btif_bonded_devices_t bonded_devices;
606 btif_in_fetch_bonded_devices(&bonded_devices, 0);
608 BTIF_TRACE_DEBUG2("%s: Number of bonded devices: %d Property:BT_PROPERTY_ADAPTER_BONDED_DEVICES", __FUNCTION__, bonded_devices.num_devices);
610 if (bonded_devices.num_devices > 0)
612 property->len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
613 memcpy(property->val, bonded_devices.devices, property->len);
616 /* if there are no bonded_devices, then length shall be 0 */
617 return BT_STATUS_SUCCESS;
619 else if (property->type == BT_PROPERTY_UUIDS)
621 /* publish list of local supported services */
622 bt_uuid_t *p_uuid = (bt_uuid_t*)property->val;
623 uint32_t num_uuids = 0;
626 tBTA_SERVICE_MASK service_mask = btif_get_enabled_services_mask();
627 BTIF_TRACE_ERROR2("%s service_mask:0x%x", __FUNCTION__, service_mask);
628 for (i=0; i < BTA_MAX_SERVICE_ID; i++)
630 /* This should eventually become a function when more services are enabled */
632 &(tBTA_SERVICE_MASK)(1 << i))
636 case BTA_HFP_SERVICE_ID:
638 uuid16_to_uuid128(UUID_SERVCLASS_AG_HANDSFREE,
642 /* intentional fall through: Send both BFP & HSP UUIDs if HFP is enabled */
643 case BTA_HSP_SERVICE_ID:
645 uuid16_to_uuid128(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,
649 case BTA_A2DP_SERVICE_ID:
651 uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SOURCE,
658 property->len = (num_uuids)*sizeof(bt_uuid_t);
659 return BT_STATUS_SUCCESS;
662 /* fall through for other properties */
663 if(!cfg2prop(NULL, property))
665 return btif_dm_get_adapter_property(property);
667 return BT_STATUS_SUCCESS;
670 /*******************************************************************************
672 ** Function btif_storage_set_adapter_property
674 ** Description BTIF storage API - Stores the adapter property
677 ** Returns BT_STATUS_SUCCESS if the store was successful,
678 ** BT_STATUS_FAIL otherwise
680 *******************************************************************************/
681 bt_status_t btif_storage_set_adapter_property(bt_property_t *property)
683 return prop2cfg(NULL, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
686 /*******************************************************************************
688 ** Function btif_storage_get_remote_device_property
690 ** Description BTIF storage API - Fetches the remote device property->type
691 ** from NVRAM and fills property->val.
692 ** Caller should provide memory for property->val and
693 ** set the property->val
695 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
696 ** BT_STATUS_FAIL otherwise
698 *******************************************************************************/
699 bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t *remote_bd_addr,
700 bt_property_t *property)
702 return cfg2prop(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
704 /*******************************************************************************
706 ** Function btif_storage_set_remote_device_property
708 ** Description BTIF storage API - Stores the remote device property
711 ** Returns BT_STATUS_SUCCESS if the store was successful,
712 ** BT_STATUS_FAIL otherwise
714 *******************************************************************************/
715 bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t *remote_bd_addr,
716 bt_property_t *property)
718 return prop2cfg(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
721 /*******************************************************************************
723 ** Function btif_storage_add_remote_device
725 ** Description BTIF storage API - Adds a newly discovered device to NVRAM
726 ** along with the timestamp. Also, stores the various
727 ** properties - RSSI, BDADDR, NAME (if found in EIR)
729 ** Returns BT_STATUS_SUCCESS if the store was successful,
730 ** BT_STATUS_FAIL otherwise
732 *******************************************************************************/
733 bt_status_t btif_storage_add_remote_device(bt_bdaddr_t *remote_bd_addr,
734 uint32_t num_properties,
735 bt_property_t *properties)
738 /* TODO: If writing a property, fails do we go back undo the earlier
739 * written properties? */
740 for (i=0; i < num_properties; i++)
742 /* Ignore the RSSI as this is not stored in DB */
743 if (properties[i].type == BT_PROPERTY_REMOTE_RSSI)
746 /* BD_ADDR for remote device needs special handling as we also store timestamp */
747 if (properties[i].type == BT_PROPERTY_BDADDR)
749 bt_property_t addr_prop;
750 memcpy(&addr_prop, &properties[i], sizeof(bt_property_t));
751 addr_prop.type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP;
752 btif_storage_set_remote_device_property(remote_bd_addr,
757 btif_storage_set_remote_device_property(remote_bd_addr,
761 return BT_STATUS_SUCCESS;
764 /*******************************************************************************
766 ** Function btif_storage_add_bonded_device
768 ** Description BTIF storage API - Adds the newly bonded device to NVRAM
769 ** along with the link-key, Key type and Pin key length
771 ** Returns BT_STATUS_SUCCESS if the store was successful,
772 ** BT_STATUS_FAIL otherwise
774 *******************************************************************************/
776 bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
782 bd2str(remote_bd_addr, &bdstr);
783 int ret = btif_config_set_int("Remote", bdstr, "LinkKeyType", (int)key_type);
784 ret &= btif_config_set_int("Remote", bdstr, "PinLength", (int)pin_length);
785 ret &= btif_config_set("Remote", bdstr, "LinkKey", (const char*)link_key, sizeof(LINK_KEY), BTIF_CFG_TYPE_BIN);
786 /* write bonded info immediately */
788 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
791 /*******************************************************************************
793 ** Function btif_storage_remove_bonded_device
795 ** Description BTIF storage API - Deletes the bonded device from NVRAM
797 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
798 ** BT_STATUS_FAIL otherwise
800 *******************************************************************************/
801 bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
804 bd2str(remote_bd_addr, &bdstr);
805 BTIF_TRACE_DEBUG1("in bd addr:%s", bdstr);
806 int ret = btif_config_remove("Remote", bdstr, "LinkKeyType");
807 ret &= btif_config_remove("Remote", bdstr, "PinLength");
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 tBTA_LE_KEY_VALUE *p;
1173 if(!btif_config_get_int("Remote", remote_bd_addr,"DevType", &device_type))
1174 return BT_STATUS_FAIL;
1175 if(device_type == BT_DEVICE_TYPE_BLE)
1177 BTIF_TRACE_DEBUG2("%s %s found a BLE device", __FUNCTION__,remote_bd_addr);
1178 str2bd(remote_bd_addr, &bd_addr);
1179 bdcpy(bta_bd_addr, bd_addr.address);
1180 if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS)
1182 return BT_STATUS_FAIL;
1185 memset(buf, 0, sizeof(buf));
1186 if (btif_storage_get_ble_bonding_key(&bd_addr,
1187 BTIF_DM_LE_KEY_PENC,
1189 sizeof(btif_dm_ble_penc_keys_t)) == BT_STATUS_SUCCESS)
1193 if (!is_device_added)
1195 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1196 is_device_added = TRUE;
1198 p = (tBTA_LE_KEY_VALUE *)buf;
1199 for (i=0; i<16; i++)
1201 BTIF_TRACE_DEBUG2("penc_key.ltk[%d]=0x%02x",i,p->penc_key.ltk[i]);
1205 BTIF_TRACE_DEBUG2("penc_key.rand[%d]=0x%02x",i,p->penc_key.rand[i]);
1207 BTIF_TRACE_DEBUG1("p->penc_key.ediv=0x%04x",p->penc_key.ediv);
1208 BTIF_TRACE_DEBUG1("p->penc_key.sec_level=0x%02x",p->penc_key.sec_level);
1209 BTIF_TRACE_DEBUG1("p->penc_key.key_size=0x%02x",p->penc_key.key_size);
1210 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PENC);
1214 memset(buf, 0, sizeof(buf));
1215 if (btif_storage_get_ble_bonding_key(&bd_addr,
1218 sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1222 if (!is_device_added)
1224 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1225 is_device_added = TRUE;
1227 p = (tBTA_LE_KEY_VALUE *)buf;
1228 for (i=0; i<16; i++)
1230 BTIF_TRACE_DEBUG2("p->pid_key[%d]=0x%02x",i,p->pid_key.irk[i]);
1233 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PID);
1237 memset(buf, 0, sizeof(buf));
1238 if (btif_storage_get_ble_bonding_key(&bd_addr,
1239 BTIF_DM_LE_KEY_PCSRK,
1241 sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1245 if (!is_device_added)
1247 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1248 is_device_added = TRUE;
1251 p = (tBTA_LE_KEY_VALUE *)buf;
1252 for (i=0; i<16; i++)
1254 BTIF_TRACE_DEBUG2("p->pcsrk_key.csrk[%d]=0x%02x",i, p->psrk_key.csrk[i]);
1256 BTIF_TRACE_DEBUG1("p->pcsrk_key.counter=0x%08x",p->psrk_key.counter);
1257 BTIF_TRACE_DEBUG1("p->pcsrk_key.sec_level=0x%02x",p->psrk_key.sec_level);
1259 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PCSRK);
1263 memset(buf, 0, sizeof(buf));
1264 if (btif_storage_get_ble_bonding_key(&bd_addr,
1265 BTIF_DM_LE_KEY_LENC,
1267 sizeof(btif_dm_ble_lenc_keys_t)) == BT_STATUS_SUCCESS)
1271 if (!is_device_added)
1273 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1274 is_device_added = TRUE;
1276 p = (tBTA_LE_KEY_VALUE *)buf;
1277 BTIF_TRACE_DEBUG1("p->lenc_key.div=0x%04x",p->lenc_key.div);
1278 BTIF_TRACE_DEBUG1("p->lenc_key.key_size=0x%02x",p->lenc_key.key_size);
1279 BTIF_TRACE_DEBUG1("p->lenc_key.sec_level=0x%02x",p->lenc_key.sec_level);
1281 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LENC);
1285 memset(buf, 0, sizeof(buf));
1286 if (btif_storage_get_ble_bonding_key(&bd_addr,
1287 BTIF_DM_LE_KEY_LCSRK,
1289 sizeof(btif_dm_ble_lcsrk_keys_t)) == BT_STATUS_SUCCESS)
1293 if (!is_device_added)
1295 BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1296 is_device_added = TRUE;
1298 p = (tBTA_LE_KEY_VALUE *)buf;
1299 BTIF_TRACE_DEBUG1("p->lcsrk_key.div=0x%04x",p->lcsrk_key.div);
1300 BTIF_TRACE_DEBUG1("p->lcsrk_key.counter=0x%08x",p->lcsrk_key.counter);
1301 BTIF_TRACE_DEBUG1("p->lcsrk_key.sec_level=0x%02x",p->lcsrk_key.sec_level);
1303 BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LCSRK);
1307 /* Fill in the bonded devices */
1308 if (is_device_added)
1310 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
1311 btif_gatts_add_bonded_dev_from_nv(bta_bd_addr);
1314 return BT_STATUS_SUCCESS;
1316 return BT_STATUS_FAIL;
1319 bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1323 bd2str(remote_bd_addr, &bdstr);
1324 int ret = btif_config_set_int("Remote", bdstr, "AddrType", (int)addr_type);
1326 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1329 /*******************************************************************************
1331 ** Function btif_storage_get_remote_addr_type
1333 ** Description BTIF storage API - Fetches the remote addr type
1335 ** Returns BT_STATUS_SUCCESS if the fetch was successful,
1336 ** BT_STATUS_FAIL otherwise
1338 *******************************************************************************/
1339 bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1343 bd2str(remote_bd_addr, &bdstr);
1344 int ret = btif_config_get_int("Remote", bdstr, "AddrType", addr_type);
1345 return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1348 /*******************************************************************************
1350 ** Function btif_storage_add_hid_device_info
1352 ** Description BTIF storage API - Adds the hid information of bonded hid devices-to NVRAM
1354 ** Returns BT_STATUS_SUCCESS if the store was successful,
1355 ** BT_STATUS_FAIL otherwise
1357 *******************************************************************************/
1359 bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr,
1360 UINT16 attr_mask, UINT8 sub_class,
1361 UINT8 app_id, UINT16 vendor_id,
1362 UINT16 product_id, UINT16 version,
1363 UINT8 ctry_code, UINT16 ssr_max_latency,
1364 UINT16 ssr_min_tout, UINT16 dl_len, UINT8 *dsc_list)
1367 BTIF_TRACE_DEBUG0("btif_storage_add_hid_device_info:");
1368 bd2str(remote_bd_addr, &bdstr);
1369 btif_config_set_int("Remote", bdstr, "HidAttrMask", attr_mask);
1370 btif_config_set_int("Remote", bdstr, "HidSubClass", sub_class);
1371 btif_config_set_int("Remote", bdstr, "HidAppId", app_id);
1372 btif_config_set_int("Remote", bdstr, "HidVendorId", vendor_id);
1373 btif_config_set_int("Remote", bdstr, "HidProductId", product_id);
1374 btif_config_set_int("Remote", bdstr, "HidVersion", version);
1375 btif_config_set_int("Remote", bdstr, "HidCountryCode", ctry_code);
1376 btif_config_set_int("Remote", bdstr, "HidSSRMaxLatency", ssr_max_latency);
1377 btif_config_set_int("Remote", bdstr, "HidSSRMinTimeout", ssr_min_tout);
1379 btif_config_set("Remote", bdstr, "HidDescriptor", (const char*)dsc_list, dl_len,
1382 return BT_STATUS_SUCCESS;
1385 /*******************************************************************************
1387 ** Function btif_storage_load_bonded_hid_info
1389 ** Description BTIF storage API - Loads hid info for all the bonded devices from NVRAM
1390 ** and adds those devices to the BTA_HH.
1392 ** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
1394 *******************************************************************************/
1395 bt_status_t btif_storage_load_bonded_hid_info(void)
1397 bt_bdaddr_t bd_addr;
1398 tBTA_HH_DEV_DSCP_INFO dscp_info;
1404 char kname[128], vname[128];
1407 kname_size = sizeof(kname);
1410 memset(&dscp_info, 0, sizeof(dscp_info));
1413 kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
1414 BTIF_TRACE_DEBUG2("Remote device:%s, size:%d", kname, kname_size);
1416 if(btif_config_get_int("Remote", kname, "HidAttrMask", &value))
1418 attr_mask = (uint16_t)value;
1420 btif_config_get_int("Remote", kname, "HidSubClass", &value);
1421 sub_class = (uint8_t)value;
1423 btif_config_get_int("Remote", kname, "HidAppId", &value);
1424 app_id = (uint8_t)value;
1426 btif_config_get_int("Remote", kname, "HidVendorId", &value);
1427 dscp_info.vendor_id = (uint16_t) value;
1429 btif_config_get_int("Remote", kname, "HidProductId", &value);
1430 dscp_info.product_id = (uint16_t) value;
1432 btif_config_get_int("Remote", kname, "HidVersion", &value);
1433 dscp_info.version = (uint8_t) value;
1435 btif_config_get_int("Remote", kname, "HidCountryCode", &value);
1436 dscp_info.ctry_code = (uint8_t) value;
1439 btif_config_get_int("Remote", kname, "HidSSRMaxLatency", &value);
1440 dscp_info.ssr_max_latency = (uint16_t) value;
1443 btif_config_get_int("Remote", kname, "HidSSRMinTimeout", &value);
1444 dscp_info.ssr_min_tout = (uint16_t) value;
1448 btif_config_get("Remote", kname, "HidDescriptor", NULL, &len, &type);
1451 dscp_info.descriptor.dl_len = (uint16_t)len;
1452 dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len);
1453 btif_config_get("Remote", kname, "HidDescriptor", (char*)dscp_info.descriptor.dsc_list, &len, &type);
1455 str2bd(kname, &bd_addr);
1456 // add extracted information to BTA HH
1457 if (btif_hh_add_added_dev(bd_addr,attr_mask))
1459 BTA_HhAddDev(bd_addr.address, attr_mask, sub_class,
1463 } while(kpos != -1);
1465 return BT_STATUS_SUCCESS;
1468 /*******************************************************************************
1470 ** Function btif_storage_remove_hid_info
1472 ** Description BTIF storage API - Deletes the bonded hid device info from NVRAM
1474 ** Returns BT_STATUS_SUCCESS if the deletion was successful,
1475 ** BT_STATUS_FAIL otherwise
1477 *******************************************************************************/
1478 bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr)
1483 bd2str(remote_bd_addr, &bdstr);
1485 btif_config_remove("Remote", bdstr, "HidAttrMask");
1486 btif_config_remove("Remote", bdstr, "HidSubClass");
1487 btif_config_remove("Remote", bdstr, "HidAppId");
1488 btif_config_remove("Remote", bdstr, "HidVendorId");
1489 btif_config_remove("Remote", bdstr, "HidProductId");
1490 btif_config_remove("Remote", bdstr, "HidVersion");
1491 btif_config_remove("Remote", bdstr, "HidCountryCode");
1492 btif_config_remove("Remote", bdstr, "HidSSRMaxLatency");
1493 btif_config_remove("Remote", bdstr, "HidSSRMinTimeout");
1494 btif_config_remove("Remote", bdstr, "HidDescriptor");
1496 return BT_STATUS_SUCCESS;
1499 /*******************************************************************************
1501 ** Function btif_storage_read_hl_apps_cb
1503 ** Description BTIF storage API - Read HL application control block from NVRAM
1505 ** Returns BT_STATUS_SUCCESS if the operation was successful,
1506 ** BT_STATUS_FAIL otherwise
1508 *******************************************************************************/
1509 bt_status_t btif_storage_read_hl_apps_cb(char *value, int value_size)
1511 bt_status_t bt_status = BT_STATUS_SUCCESS;
1512 int read_size=value_size, read_type=BTIF_CFG_TYPE_BIN;
1514 if (!btif_config_exist("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB))
1516 memset(value, 0, value_size);
1517 if (!btif_config_set("Local", BTIF_STORAGE_HL_APP,BTIF_STORAGE_HL_APP_CB,
1518 value, value_size, BTIF_CFG_TYPE_BIN))
1520 bt_status = BT_STATUS_FAIL;
1529 if (!btif_config_get("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB,
1530 value, &read_size, &read_type))
1532 bt_status = BT_STATUS_FAIL;
1536 if ((read_size != value_size) || (read_type != BTIF_CFG_TYPE_BIN) )
1538 BTIF_TRACE_ERROR4("%s value_size=%d read_size=%d read_type=%d",
1539 __FUNCTION__, value_size, read_size, read_type);
1540 bt_status = BT_STATUS_FAIL;
1546 BTIF_TRACE_DEBUG3("%s status=%d value_size=%d", __FUNCTION__, bt_status, value_size);
1551 /*******************************************************************************
1553 ** Function btif_storage_load_autopair_device_list
1555 ** Description BTIF storage API - Populates auto pair device list
1557 ** Returns BT_STATUS_SUCCESS if the auto pair blacklist is successfully populated
1558 ** BT_STATUS_FAIL otherwise
1560 *******************************************************************************/
1562 bt_status_t btif_storage_load_autopair_device_list()
1564 char *key_name, *key_value;
1566 char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1570 if(!btif_config_exist("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, NULL))
1572 /* first time loading of auto pair blacklist configuration */
1574 fp = fopen (BTIF_AUTO_PAIR_CONF_FILE, "r");
1578 ALOGE("%s: Failed to open auto pair blacklist conf file at %s", __FUNCTION__,BTIF_AUTO_PAIR_CONF_FILE );
1579 return BT_STATUS_FAIL;
1582 /* read through auto_pairing.conf file and create the key value pairs specific to auto pair blacklist devices */
1583 while (fgets(linebuf, BTIF_STORAGE_MAX_LINE_SZ, fp) != NULL)
1585 /* trip leading white spaces */
1586 while (linebuf[i] == BTIF_AUTO_PAIR_CONF_SPACE)
1589 /* skip commented lines */
1590 if (linebuf[i] == BTIF_AUTO_PAIR_CONF_COMMENT)
1593 line = (char*)&(linebuf[i]);
1598 key_name = strtok(line, BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER);
1600 if (key_name == NULL)
1602 else if((strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR) == 0) ||
1603 (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME) ==0) ||
1604 (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST) ==0 ) ||
1605 (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME) == 0) ||
1606 (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR) == 0))
1608 key_value = strtok(NULL, BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER);
1609 btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, key_name, key_value);
1614 return BT_STATUS_SUCCESS;
1617 /*******************************************************************************
1619 ** Function btif_storage_is_device_autopair_blacklisted
1621 ** Description BTIF storage API Checks if the given device is blacklisted for auto pairing
1623 ** Returns TRUE if the device is found in the auto pair blacklist
1626 *******************************************************************************/
1627 BOOLEAN btif_storage_is_device_autopair_blacklisted(bt_bdaddr_t *remote_bd_addr)
1634 char value[BTIF_STORAGE_MAX_LINE_SZ];
1635 int value_size = sizeof(value);
1637 bd2str(remote_bd_addr, &bdstr);
1639 /* Consider only Lower Address Part from BD Address */
1642 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1643 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR, value, &value_size))
1645 if (strcasestr(value,bdstr) != NULL)
1649 dev_name_str = BTM_SecReadDevName((remote_bd_addr->address));
1651 if (dev_name_str != NULL)
1653 value_size = sizeof(value);
1654 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1655 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME, value, &value_size))
1657 if (strstr(value,dev_name_str) != NULL)
1660 value_size = sizeof(value);
1661 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1662 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME, value, &value_size))
1664 token = strtok(value, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1665 while (token != NULL)
1667 if (strstr(dev_name_str, token) != NULL)
1670 token = strtok(NULL, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1674 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1675 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, value, &value_size))
1677 if (strstr(value,bdstr) != NULL)
1683 /*******************************************************************************
1685 ** Function btif_storage_add_device_to_autopair_blacklist
1687 ** Description BTIF storage API - Add a remote device to the auto pairing blacklist
1689 ** Returns BT_STATUS_SUCCESS if the device is successfully added to the auto pair blacklist
1690 ** BT_STATUS_FAIL otherwise
1692 *******************************************************************************/
1693 bt_status_t btif_storage_add_device_to_autopair_blacklist(bt_bdaddr_t *remote_bd_addr)
1697 char linebuf[BTIF_STORAGE_MAX_LINE_SZ+20];
1698 char input_value [20];
1700 bd2str(remote_bd_addr, &bdstr);
1701 strlcpy(input_value, (char*)bdstr, sizeof(input_value));
1702 strlcat(input_value,BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR, sizeof(input_value));
1704 int line_size = sizeof(linebuf);
1705 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1706 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf, &line_size))
1708 /* Append this address to the dynamic List of BD address */
1709 strncat (linebuf, input_value, BTIF_STORAGE_MAX_LINE_SZ);
1713 strncpy( linebuf,input_value, BTIF_STORAGE_MAX_LINE_SZ);
1716 /* Write back the key value */
1717 ret = btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1718 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf);
1720 return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1723 /*******************************************************************************
1725 ** Function btif_storage_is_fixed_pin_zeros_keyboard
1727 ** Description BTIF storage API - checks if this device has fixed PIN key device list
1729 ** Returns TRUE if the device is found in the fixed pin keyboard device list
1732 *******************************************************************************/
1733 BOOLEAN btif_storage_is_fixed_pin_zeros_keyboard(bt_bdaddr_t *remote_bd_addr)
1739 char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1741 bd2str(remote_bd_addr, &bdstr);
1743 /*consider on LAP part of BDA string*/
1746 int line_size = sizeof(linebuf);
1747 if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1748 BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST, linebuf, &line_size))
1750 if (strcasestr(linebuf,bdstr) != NULL)