OSDN Git Service

am f219040f: Merge "Reload Hid info only for bonded device" into klp-wireless-dev
[android-x86/system-bt.git] / btif / src / btif_storage.c
1 /******************************************************************************
2  *
3  *  Copyright (c) 2014 The Android Open Source Project
4  *  Copyright (C) 2009-2012 Broadcom Corporation
5  *
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:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  ******************************************************************************/
19
20 /************************************************************************************
21  *
22  *  Filename:      btif_storage.c
23  *
24  *  Description:   Stores the local BT adapter and remote device properties in
25  *                 NVRAM storage, typically as xml file in the
26  *                 mobile's filesystem
27  *
28  *
29  */
30 #include <stdlib.h>
31 #include <time.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <alloca.h>
35
36
37 #include <hardware/bluetooth.h>
38 #include "btif_config.h"
39 #define LOG_TAG "BTIF_STORAGE"
40
41 #include "btif_api.h"
42 #include "btif_storage.h"
43 #include "btif_util.h"
44 #include "bd.h"
45 #include "config.h"
46 #include "gki.h"
47 #include "osi.h"
48 #include "bta_hh_api.h"
49 #include "btif_hh.h"
50
51 #include <cutils/log.h>
52
53 /************************************************************************************
54 **  Constants & Macros
55 ************************************************************************************/
56
57 #define BTIF_STORAGE_PATH_BLUEDROID "/data/misc/bluedroid"
58
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"
68
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"
76
77
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"
85
86 #define BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR ","
87
88
89 /* This is a local property to add a device found */
90 #define BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP 0xFF
91
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);}
94
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);}
97
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 */
102
103 #define STORAGE_KEY_TYPE_MAX               (10)
104
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)
114
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)
120
121 #define STORAGE_REMOTE_LINKKEYS_ENTRY_SIZE (LINK_KEY_LEN*2 + 1 + 2 + 1 + 2)
122
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 )
136
137
138 /* currently remote services is the potentially largest entry */
139 #define BTIF_STORAGE_MAX_LINE_SZ BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX
140
141
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"
145 #endif
146
147
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_"
152
153 /************************************************************************************
154 **  Local type definitions
155 ************************************************************************************/
156 typedef struct
157 {
158     uint32_t num_devices;
159     bt_bdaddr_t devices[BTM_SEC_MAX_DEVICE_RECORDS];
160 } btif_bonded_devices_t;
161
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;
167
168 /************************************************************************************
169 **  External functions
170 ************************************************************************************/
171
172 extern void btif_gatts_add_bonded_dev_from_nv(BD_ADDR bda);
173
174 /************************************************************************************
175 **  Internal Functions
176 ************************************************************************************/
177
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,
181                                               int *addr_type);
182
183 /************************************************************************************
184 **  Static functions
185 ************************************************************************************/
186
187 /*******************************************************************************
188 **
189 ** Function         btif_in_split_uuids_string_to_list
190 **
191 ** Description      Internal helper function to split the string of UUIDs
192 **                  read from the NVRAM to an array
193 **
194 ** Returns          None
195 **
196 *******************************************************************************/
197 static void btif_in_split_uuids_string_to_list(char *str, bt_uuid_t *p_uuid,
198                                                uint32_t *p_num_uuid)
199 {
200     char buf[64];
201     char *p_start = str;
202     char *p_needle;
203     uint32_t num = 0;
204     do
205     {
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);
212         num++;
213         p_start = ++p_needle;
214
215     } while (*p_start != 0);
216     *p_num_uuid = num;
217 }
218 static int prop2cfg(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
219 {
220     bdstr_t bdstr = {0};
221     if(remote_bd_addr)
222         bd2str(remote_bd_addr, &bdstr);
223     BTIF_TRACE_DEBUG3("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
224     char value[1024];
225     if(prop->len <= 0 || prop->len > (int)sizeof(value) - 1)
226     {
227         BTIF_TRACE_ERROR2("property type:%d, len:%d is invalid", prop->type, prop->len);
228         return FALSE;
229     }
230     switch(prop->type)
231     {
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);
239             break;
240         case BT_PROPERTY_BDNAME:
241             strncpy(value, (char*)prop->val, prop->len);
242             value[prop->len]='\0';
243             if(remote_bd_addr)
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 */
249             btif_config_save();
250             break;
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 */
256             btif_config_save();
257             break;
258         case BT_PROPERTY_ADAPTER_SCAN_MODE:
259             btif_config_set_int("Local", "Adapter",
260                                 BTIF_STORAGE_KEY_ADAPTER_SCANMODE, *(int*)prop->val);
261             break;
262         case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
263             btif_config_set_int("Local", "Adapter",
264                                 BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, *(int*)prop->val);
265             break;
266         case BT_PROPERTY_CLASS_OF_DEVICE:
267             btif_config_set_int("Remote", bdstr,
268                                 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, *(int*)prop->val);
269             break;
270         case BT_PROPERTY_TYPE_OF_DEVICE:
271             btif_config_set_int("Remote", bdstr,
272                                 BTIF_STORAGE_PATH_REMOTE_DEVTYPE, *(int*)prop->val);
273             break;
274         case BT_PROPERTY_UUIDS:
275         {
276             uint32_t i;
277             char buf[64];
278             value[0] = 0;
279             for (i=0; i < (prop->len)/sizeof(bt_uuid_t); i++)
280             {
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);
284                 strcat(value, buf);
285                 //strcat(value, ";");
286                 strcat(value, " ");
287             }
288             btif_config_set_str("Remote", bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value);
289             btif_config_save();
290             break;
291         }
292         case BT_PROPERTY_REMOTE_VERSION_INFO:
293         {
294             bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
295
296             if (!info)
297                 return FALSE;
298
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);
305             btif_config_save();
306          } break;
307
308         default:
309              BTIF_TRACE_ERROR1("Unknow prop type:%d", prop->type);
310              return FALSE;
311     }
312     return TRUE;
313 }
314 static int cfg2prop(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
315 {
316     bdstr_t bdstr = {0};
317     if(remote_bd_addr)
318         bd2str(remote_bd_addr, &bdstr);
319     BTIF_TRACE_DEBUG3("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
320     if(prop->len <= 0)
321     {
322         BTIF_TRACE_ERROR2("property type:%d, len:%d is invalid", prop->type, prop->len);
323         return FALSE;
324     }
325     int ret = FALSE;
326     switch(prop->type)
327     {
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);
332             break;
333         case BT_PROPERTY_BDNAME:
334         {
335             int len = prop->len;
336             if(remote_bd_addr)
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)
342                 prop->len = len - 1;
343             else
344             {
345                 prop->len = 0;
346                 ret = FALSE;
347             }
348             break;
349         }
350         case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
351         {
352             int len = prop->len;
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)
356                 prop->len = len - 1;
357             else
358             {
359                 prop->len = 0;
360                 ret = FALSE;
361             }
362             break;
363         }
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);
368            break;
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);
373             break;
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);
378             break;
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);
383             break;
384         case BT_PROPERTY_UUIDS:
385         {
386             char value[1280];
387             int size = sizeof(value);
388             if(btif_config_get_str("Remote", bdstr,
389                                     BTIF_STORAGE_PATH_REMOTE_SERVICE, value, &size))
390             {
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);
395                 ret = TRUE;
396             }
397             else
398             {
399                 prop->val = NULL;
400                 prop->len = 0;
401             }
402         } break;
403
404         case BT_PROPERTY_REMOTE_VERSION_INFO:
405         {
406             bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
407
408             if(prop->len >= (int)sizeof(bt_remote_version_t))
409             {
410                 ret = btif_config_get_int("Remote", bdstr,
411                                 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, &info->manufacturer);
412
413                 if (ret == TRUE)
414                     ret = btif_config_get_int("Remote", bdstr,
415                                 BTIF_STORAGE_PATH_REMOTE_VER_VER, &info->version);
416
417                 if (ret == TRUE)
418                     ret = btif_config_get_int("Remote", bdstr,
419                                 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, &info->sub_ver);
420             }
421          } break;
422
423         default:
424             BTIF_TRACE_ERROR1("Unknow prop type:%d", prop->type);
425             return FALSE;
426     }
427     return ret;
428 }
429
430 /*******************************************************************************
431 **
432 ** Function         btif_in_fetch_bonded_devices
433 **
434 ** Description      Internal helper function to fetch the bonded devices
435 **                  from NVRAM
436 **
437 ** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
438 **
439 *******************************************************************************/
440 static bt_status_t btif_in_fetch_bonded_device(char *bdstr)
441 {
442     BOOLEAN bt_linkkey_file_found=FALSE;
443     int device_type;
444
445         int type = BTIF_CFG_TYPE_BIN;
446         LINK_KEY link_key;
447         int size = sizeof(link_key);
448         if(btif_config_get("Remote", bdstr, "LinkKey", (char*)link_key, &size, &type))
449         {
450             int linkkey_type;
451             if(btif_config_get_int("Remote", bdstr, "LinkKeyType", &linkkey_type))
452             {
453                 bt_linkkey_file_found = TRUE;
454             }
455             else
456             {
457                 bt_linkkey_file_found = FALSE;
458             }
459         }
460         if((btif_in_fetch_bonded_ble_device(bdstr, FALSE, NULL) != BT_STATUS_SUCCESS)
461                 && (!bt_linkkey_file_found))
462         {
463             BTIF_TRACE_DEBUG1("Remote device:%s, no link key or ble key found", bdstr);
464             return BT_STATUS_FAIL;
465         }
466     return BT_STATUS_SUCCESS;
467 }
468
469 /*******************************************************************************
470 **
471 ** Function         btif_in_fetch_bonded_devices
472 **
473 ** Description      Internal helper function to fetch the bonded devices
474 **                  from NVRAM
475 **
476 ** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
477 **
478 *******************************************************************************/
479 static bt_status_t btif_in_fetch_bonded_devices(btif_bonded_devices_t *p_bonded_devices, int add)
480 {
481     BTIF_TRACE_DEBUG1("in add:%d", add);
482     memset(p_bonded_devices, 0, sizeof(btif_bonded_devices_t));
483
484     char kname[128], vname[128];
485     short kpos;
486     int kname_size;
487     kname_size = sizeof(kname);
488     kname[0] = 0;
489     kpos = 0;
490     BOOLEAN bt_linkkey_file_found=FALSE;
491     int device_type;
492
493     do
494     {
495         kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
496         BTIF_TRACE_DEBUG2("Remote device:%s, size:%d", kname, kname_size);
497         int type = BTIF_CFG_TYPE_BIN;
498         LINK_KEY link_key;
499         int size = sizeof(link_key);
500         if(btif_config_get("Remote", kname, "LinkKey", (char*)link_key, &size, &type))
501         {
502             int linkkey_type;
503             if(btif_config_get_int("Remote", kname, "LinkKeyType", &linkkey_type))
504             {
505                 //int pin_len;
506                 //btif_config_get_int("Remote", kname, "PinLength", &pin_len))
507                 bt_bdaddr_t bd_addr;
508                 str2bd(kname, &bd_addr);
509                 if(add)
510                 {
511                     DEV_CLASS dev_class = {0, 0, 0};
512                     int cod;
513                     if(btif_config_get_int("Remote", kname, "DevClass", &cod))
514                         uint2devclass((UINT32)cod, dev_class);
515                     BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0, (UINT8)linkkey_type, 0);
516
517 #if BLE_INCLUDED == TRUE
518                     if (btif_config_get_int("Remote", kname, "DevType", &device_type) &&
519                        (device_type == BT_DEVICE_TYPE_DUMO) )
520                     {
521                         btif_gatts_add_bonded_dev_from_nv(bd_addr.address);
522                     }
523 #endif
524                 }
525                 bt_linkkey_file_found = TRUE;
526                 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
527             }
528             else
529             {
530 #if (BLE_INCLUDED == TRUE)
531                 bt_linkkey_file_found = FALSE;
532 #else
533                 BTIF_TRACE_ERROR1("bounded device:%s, LinkKeyType or PinLength is invalid", kname);
534 #endif
535             }
536         }
537 #if (BLE_INCLUDED == TRUE)
538             if(!(btif_in_fetch_bonded_ble_device(kname,add, p_bonded_devices)) && (!bt_linkkey_file_found))
539             {
540                 BTIF_TRACE_DEBUG1("Remote device:%s, no link key or ble key found", kname);
541             }
542 #else
543             if(!bt_linkkey_file_found)
544                 BTIF_TRACE_DEBUG1("Remote device:%s, no link key", kname);
545 #endif
546         kname_size = sizeof(kname);
547         kname[0] = 0;
548     } while(kpos != -1);
549     BTIF_TRACE_DEBUG0("out");
550     return BT_STATUS_SUCCESS;
551 }
552
553 /************************************************************************************
554 **  Externs
555 ************************************************************************************/
556
557 /************************************************************************************
558 **  Functions
559 ************************************************************************************/
560
561 /** functions are synchronous.
562  * functions can be called by both internal modules such as BTIF_DM and by external entiries from HAL via BTIF_context_switch
563  * For OUT parameters,  caller is expected to provide the memory.
564  * Caller is expected to provide a valid pointer to 'property->value' based on the property->type
565  */
566 /*******************************************************************************
567 **
568 ** Function         btif_storage_get_adapter_property
569 **
570 ** Description      BTIF storage API - Fetches the adapter property->type
571 **                  from NVRAM and fills property->val.
572 **                  Caller should provide memory for property->val and
573 **                  set the property->val
574 **
575 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
576 **                  BT_STATUS_FAIL otherwise
577 **
578 *******************************************************************************/
579 bt_status_t btif_storage_get_adapter_property(bt_property_t *property)
580 {
581
582     /* Special handling for adapter BD_ADDR and BONDED_DEVICES */
583     if (property->type == BT_PROPERTY_BDADDR)
584     {
585         BD_ADDR addr;
586         bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)property->val;
587         /* This has been cached in btif. Just fetch it from there */
588         memcpy(bd_addr, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
589         property->len = sizeof(bt_bdaddr_t);
590         return BT_STATUS_SUCCESS;
591     }
592     else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES)
593     {
594         btif_bonded_devices_t bonded_devices;
595
596         btif_in_fetch_bonded_devices(&bonded_devices, 0);
597
598         BTIF_TRACE_DEBUG2("%s: Number of bonded devices: %d Property:BT_PROPERTY_ADAPTER_BONDED_DEVICES", __FUNCTION__, bonded_devices.num_devices);
599
600         if (bonded_devices.num_devices > 0)
601         {
602             property->len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
603             memcpy(property->val, bonded_devices.devices, property->len);
604         }
605
606         /* if there are no bonded_devices, then length shall be 0 */
607         return BT_STATUS_SUCCESS;
608     }
609     else if (property->type == BT_PROPERTY_UUIDS)
610     {
611         /* publish list of local supported services */
612         bt_uuid_t *p_uuid = (bt_uuid_t*)property->val;
613         uint32_t num_uuids = 0;
614         uint32_t i;
615
616         tBTA_SERVICE_MASK service_mask = btif_get_enabled_services_mask();
617         BTIF_TRACE_ERROR2("%s service_mask:0x%x", __FUNCTION__, service_mask);
618         for (i=0; i < BTA_MAX_SERVICE_ID; i++)
619         {
620             /* This should eventually become a function when more services are enabled */
621             if (service_mask
622                 &(tBTA_SERVICE_MASK)(1 << i))
623             {
624                 switch (i)
625                 {
626                     case BTA_HFP_SERVICE_ID:
627                         {
628                             uuid16_to_uuid128(UUID_SERVCLASS_AG_HANDSFREE,
629                                               p_uuid+num_uuids);
630                             num_uuids++;
631                         }
632                     /* intentional fall through: Send both BFP & HSP UUIDs if HFP is enabled */
633                     case BTA_HSP_SERVICE_ID:
634                         {
635                             uuid16_to_uuid128(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,
636                                               p_uuid+num_uuids);
637                             num_uuids++;
638                         }break;
639                     case BTA_A2DP_SERVICE_ID:
640                         {
641                             uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SOURCE,
642                                               p_uuid+num_uuids);
643                             num_uuids++;
644                         }break;
645                     case BTA_HFP_HS_SERVICE_ID:
646                         {
647                             uuid16_to_uuid128(UUID_SERVCLASS_HF_HANDSFREE,
648                                               p_uuid+num_uuids);
649                             num_uuids++;
650                         }break;
651                 }
652             }
653         }
654         property->len = (num_uuids)*sizeof(bt_uuid_t);
655         return BT_STATUS_SUCCESS;
656     }
657
658     /* fall through for other properties */
659     if(!cfg2prop(NULL, property))
660     {
661         return btif_dm_get_adapter_property(property);
662     }
663     return BT_STATUS_SUCCESS;
664  }
665
666 /*******************************************************************************
667 **
668 ** Function         btif_storage_set_adapter_property
669 **
670 ** Description      BTIF storage API - Stores the adapter property
671 **                  to NVRAM
672 **
673 ** Returns          BT_STATUS_SUCCESS if the store was successful,
674 **                  BT_STATUS_FAIL otherwise
675 **
676 *******************************************************************************/
677 bt_status_t btif_storage_set_adapter_property(bt_property_t *property)
678 {
679     return prop2cfg(NULL, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
680 }
681
682 /*******************************************************************************
683 **
684 ** Function         btif_storage_get_remote_device_property
685 **
686 ** Description      BTIF storage API - Fetches the remote device property->type
687 **                  from NVRAM and fills property->val.
688 **                  Caller should provide memory for property->val and
689 **                  set the property->val
690 **
691 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
692 **                  BT_STATUS_FAIL otherwise
693 **
694 *******************************************************************************/
695 bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t *remote_bd_addr,
696                                                     bt_property_t *property)
697 {
698     return cfg2prop(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
699 }
700 /*******************************************************************************
701 **
702 ** Function         btif_storage_set_remote_device_property
703 **
704 ** Description      BTIF storage API - Stores the remote device property
705 **                  to NVRAM
706 **
707 ** Returns          BT_STATUS_SUCCESS if the store was successful,
708 **                  BT_STATUS_FAIL otherwise
709 **
710 *******************************************************************************/
711 bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t *remote_bd_addr,
712                                                     bt_property_t *property)
713 {
714     return prop2cfg(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
715 }
716
717 /*******************************************************************************
718 **
719 ** Function         btif_storage_add_remote_device
720 **
721 ** Description      BTIF storage API - Adds a newly discovered device to NVRAM
722 **                  along with the timestamp. Also, stores the various
723 **                  properties - RSSI, BDADDR, NAME (if found in EIR)
724 **
725 ** Returns          BT_STATUS_SUCCESS if the store was successful,
726 **                  BT_STATUS_FAIL otherwise
727 **
728 *******************************************************************************/
729 bt_status_t btif_storage_add_remote_device(bt_bdaddr_t *remote_bd_addr,
730                                            uint32_t num_properties,
731                                            bt_property_t *properties)
732 {
733     uint32_t i = 0;
734     /* TODO: If writing a property, fails do we go back undo the earlier
735      * written properties? */
736     for (i=0; i < num_properties; i++)
737     {
738         /* Ignore the RSSI as this is not stored in DB */
739         if (properties[i].type == BT_PROPERTY_REMOTE_RSSI)
740             continue;
741
742         /* BD_ADDR for remote device needs special handling as we also store timestamp */
743         if (properties[i].type == BT_PROPERTY_BDADDR)
744         {
745             bt_property_t addr_prop;
746             memcpy(&addr_prop, &properties[i], sizeof(bt_property_t));
747             addr_prop.type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP;
748             btif_storage_set_remote_device_property(remote_bd_addr,
749                                                     &addr_prop);
750         }
751         else
752         {
753             btif_storage_set_remote_device_property(remote_bd_addr,
754                                                     &properties[i]);
755         }
756     }
757     return BT_STATUS_SUCCESS;
758 }
759
760 /*******************************************************************************
761 **
762 ** Function         btif_storage_add_bonded_device
763 **
764 ** Description      BTIF storage API - Adds the newly bonded device to NVRAM
765 **                  along with the link-key, Key type and Pin key length
766 **
767 ** Returns          BT_STATUS_SUCCESS if the store was successful,
768 **                  BT_STATUS_FAIL otherwise
769 **
770 *******************************************************************************/
771
772 bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
773                                            LINK_KEY link_key,
774                                            uint8_t key_type,
775                                            uint8_t pin_length)
776 {
777     bdstr_t bdstr;
778     bd2str(remote_bd_addr, &bdstr);
779     int ret = btif_config_set_int("Remote", bdstr, "LinkKeyType", (int)key_type);
780     ret &= btif_config_set_int("Remote", bdstr, "PinLength", (int)pin_length);
781     ret &= btif_config_set("Remote", bdstr, "LinkKey", (const char*)link_key, sizeof(LINK_KEY), BTIF_CFG_TYPE_BIN);
782     /* write bonded info immediately */
783     btif_config_flush();
784     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
785 }
786
787 /*******************************************************************************
788 **
789 ** Function         btif_storage_remove_bonded_device
790 **
791 ** Description      BTIF storage API - Deletes the bonded device from NVRAM
792 **
793 ** Returns          BT_STATUS_SUCCESS if the deletion was successful,
794 **                  BT_STATUS_FAIL otherwise
795 **
796 *******************************************************************************/
797 bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
798 {
799     bdstr_t bdstr;
800     bd2str(remote_bd_addr, &bdstr);
801     BTIF_TRACE_DEBUG1("in bd addr:%s", bdstr);
802     int ret = 1;
803     if(btif_config_exist("Remote", bdstr, "LinkKeyType"))
804         ret &= btif_config_remove("Remote", bdstr, "LinkKeyType");
805     if(btif_config_exist("Remote", bdstr, "PinLength"))
806         ret &= btif_config_remove("Remote", bdstr, "PinLength");
807     if(btif_config_exist("Remote", bdstr, "LinkKey"))
808         ret &= btif_config_remove("Remote", bdstr, "LinkKey");
809     /* write bonded info immediately */
810     btif_config_flush();
811     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
812
813 }
814
815 /*******************************************************************************
816 **
817 ** Function         btif_storage_load_bonded_devices
818 **
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.
823 **
824 ** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
825 **
826 *******************************************************************************/
827 bt_status_t btif_storage_load_bonded_devices(void)
828 {
829     char *fname;
830     btif_bonded_devices_t bonded_devices;
831     uint32_t i = 0;
832     bt_property_t adapter_props[6];
833     uint32_t num_props = 0;
834     bt_property_t remote_properties[8];
835     bt_bdaddr_t addr;
836     bt_bdname_t name, alias;
837     bt_scan_mode_t mode;
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;
843
844     btif_in_fetch_bonded_devices(&bonded_devices, 1);
845
846     /* Now send the adapter_properties_cb with all adapter_properties */
847     {
848         memset(adapter_props, 0, sizeof(adapter_props));
849
850         /* BD_ADDR */
851         BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDADDR, &addr, sizeof(addr),
852                                       adapter_props[num_props]);
853         num_props++;
854
855         /* BD_NAME */
856         BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDNAME, &name, sizeof(name),
857                                       adapter_props[num_props]);
858         num_props++;
859
860         /* SCAN_MODE */
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
864          synchronization */
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;
869         num_props++;
870
871         /* DISC_TIMEOUT */
872         BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
873                                       &disc_timeout, sizeof(disc_timeout),
874                                       adapter_props[num_props]);
875         num_props++;
876
877         /* BONDED_DEVICES */
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++)
883         {
884             memcpy(devices_list + i, &bonded_devices.devices[i], sizeof(bt_bdaddr_t));
885         }
886         num_props++;
887
888         /* LOCAL UUIDs */
889         BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_UUIDS,
890                                       local_uuids, sizeof(local_uuids),
891                                       adapter_props[num_props]);
892         num_props++;
893
894         btif_adapter_properties_evt(BT_STATUS_SUCCESS, num_props, adapter_props);
895
896         free(devices_list);
897     }
898
899     BTIF_TRACE_EVENT2("%s: %d bonded devices found", __FUNCTION__, bonded_devices.num_devices);
900
901     {
902         for (i = 0; i < bonded_devices.num_devices; i++)
903         {
904             bt_bdaddr_t *p_remote_addr;
905
906             num_props = 0;
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,
910                                          &name, sizeof(name),
911                                          remote_properties[num_props]);
912             num_props++;
913
914             BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_REMOTE_FRIENDLY_NAME,
915                                          &alias, sizeof(alias),
916                                          remote_properties[num_props]);
917             num_props++;
918
919             BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_CLASS_OF_DEVICE,
920                                          &cod, sizeof(cod),
921                                          remote_properties[num_props]);
922             num_props++;
923
924             BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_TYPE_OF_DEVICE,
925                                          &devtype, sizeof(devtype),
926                                          remote_properties[num_props]);
927             num_props++;
928
929             BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_UUIDS,
930                                          remote_uuids, sizeof(remote_uuids),
931                                          remote_properties[num_props]);
932             num_props++;
933
934             btif_remote_properties_evt(BT_STATUS_SUCCESS, p_remote_addr,
935                                        num_props, remote_properties);
936         }
937     }
938     return BT_STATUS_SUCCESS;
939 }
940
941 #if (BLE_INCLUDED == TRUE)
942
943 /*******************************************************************************
944 **
945 ** Function         btif_storage_add_ble_bonding_key
946 **
947 ** Description      BTIF storage API - Adds the newly bonded device to NVRAM
948 **                  along with the ble-key, Key type and Pin key length
949 **
950 ** Returns          BT_STATUS_SUCCESS if the store was successful,
951 **                  BT_STATUS_FAIL otherwise
952 **
953 *******************************************************************************/
954
955 bt_status_t btif_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
956                                            char *key,
957                                            UINT8 key_type,
958                                            UINT8 key_length)
959 {
960     bdstr_t bdstr;
961     bd2str(remote_bd_addr, &bdstr);
962     const char* name;
963     switch(key_type)
964     {
965         case BTIF_DM_LE_KEY_PENC:
966             name = "LE_KEY_PENC";
967             break;
968         case BTIF_DM_LE_KEY_PID:
969             name = "LE_KEY_PID";
970             break;
971         case BTIF_DM_LE_KEY_PCSRK:
972             name = "LE_KEY_PCSRK";
973             break;
974         case BTIF_DM_LE_KEY_LENC:
975             name = "LE_KEY_LENC";
976             break;
977         case BTIF_DM_LE_KEY_LCSRK:
978             name = "LE_KEY_LCSRK";
979             break;
980         default:
981             return BT_STATUS_FAIL;
982     }
983     int ret = btif_config_set("Remote", bdstr, name, (const char*)key, (int)key_length, BTIF_CFG_TYPE_BIN);
984     btif_config_save();
985     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
986 }
987
988 /*******************************************************************************
989 **
990 ** Function         btif_storage_get_ble_bonding_key
991 **
992 ** Description
993 **
994 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
995 **                  BT_STATUS_FAIL otherwise
996 **
997 *******************************************************************************/
998 bt_status_t btif_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
999                                              UINT8 key_type,
1000                                              char *key_value,
1001                                              int key_length)
1002 {
1003     bdstr_t bdstr;
1004     bd2str(remote_bd_addr, &bdstr);
1005     const char* name;
1006     int type = BTIF_CFG_TYPE_BIN;
1007     switch(key_type)
1008     {
1009         case BTIF_DM_LE_KEY_PENC:
1010             name = "LE_KEY_PENC";
1011             break;
1012         case BTIF_DM_LE_KEY_PID:
1013             name = "LE_KEY_PID";
1014             break;
1015         case BTIF_DM_LE_KEY_PCSRK:
1016             name = "LE_KEY_PCSRK";
1017             break;
1018         case BTIF_DM_LE_KEY_LENC:
1019             name = "LE_KEY_LENC";
1020             break;
1021         case BTIF_DM_LE_KEY_LCSRK:
1022             name = "LE_KEY_LCSRK";
1023             break;
1024         default:
1025             return BT_STATUS_FAIL;
1026     }
1027     int ret = btif_config_get("Remote", bdstr, name, key_value, &key_length, &type);
1028     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1029
1030 }
1031
1032 /*******************************************************************************
1033 **
1034 ** Function         btif_storage_remove_ble_keys
1035 **
1036 ** Description      BTIF storage API - Deletes the bonded device from NVRAM
1037 **
1038 ** Returns          BT_STATUS_SUCCESS if the deletion was successful,
1039 **                  BT_STATUS_FAIL otherwise
1040 **
1041 *******************************************************************************/
1042 bt_status_t btif_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr)
1043 {
1044     bdstr_t bdstr;
1045     bd2str(remote_bd_addr, &bdstr);
1046     BTIF_TRACE_DEBUG2(" %s in bd addr:%s",__FUNCTION__, bdstr);
1047     int ret = 1;
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");
1058     btif_config_save();
1059     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1060 }
1061
1062 /*******************************************************************************
1063 **
1064 ** Function         btif_storage_add_ble_local_key
1065 **
1066 ** Description      BTIF storage API - Adds the ble key to NVRAM
1067 **
1068 ** Returns          BT_STATUS_SUCCESS if the store was successful,
1069 **                  BT_STATUS_FAIL otherwise
1070 **
1071 *******************************************************************************/
1072 bt_status_t btif_storage_add_ble_local_key(char *key,
1073                                            uint8_t key_type,
1074                                            uint8_t key_length)
1075 {
1076     const char* name;
1077     switch(key_type)
1078     {
1079         case BTIF_DM_LE_LOCAL_KEY_IR:
1080             name = "LE_LOCAL_KEY_IR";
1081             break;
1082         case BTIF_DM_LE_LOCAL_KEY_IRK:
1083             name = "LE_LOCAL_KEY_IRK";
1084             break;
1085         case BTIF_DM_LE_LOCAL_KEY_DHK:
1086             name = "LE_LOCAL_KEY_DHK";
1087             break;
1088         case BTIF_DM_LE_LOCAL_KEY_ER:
1089             name = "LE_LOCAL_KEY_ER";
1090             break;
1091         default:
1092             return BT_STATUS_FAIL;
1093     }
1094     int ret = btif_config_set("Local", "Adapter", name, (const char*)key, key_length, BTIF_CFG_TYPE_BIN);
1095     btif_config_save();
1096     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1097 }
1098
1099 /*******************************************************************************
1100 **
1101 ** Function         btif_storage_get_ble_local_key
1102 **
1103 ** Description
1104 **
1105 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
1106 **                  BT_STATUS_FAIL otherwise
1107 **
1108 *******************************************************************************/
1109 bt_status_t btif_storage_get_ble_local_key(UINT8 key_type,
1110                                            char *key_value,
1111                                            int key_length)
1112 {
1113     const char* name;
1114     int type = BTIF_CFG_TYPE_BIN;
1115     switch(key_type)
1116     {
1117         case BTIF_DM_LE_LOCAL_KEY_IR:
1118             name = "LE_LOCAL_KEY_IR";
1119             break;
1120         case BTIF_DM_LE_LOCAL_KEY_IRK:
1121             name = "LE_LOCAL_KEY_IRK";
1122             break;
1123         case BTIF_DM_LE_LOCAL_KEY_DHK:
1124             name = "LE_LOCAL_KEY_DHK";
1125             break;
1126         case BTIF_DM_LE_LOCAL_KEY_ER:
1127             name = "LE_LOCAL_KEY_ER";
1128             break;
1129         default:
1130             return BT_STATUS_FAIL;
1131     }
1132     int ret = btif_config_get("Local", "Adapter", name, key_value, &key_length, &type);
1133     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1134
1135 }
1136
1137 /*******************************************************************************
1138 **
1139 ** Function         btif_storage_remove_ble_local_keys
1140 **
1141 ** Description      BTIF storage API - Deletes the bonded device from NVRAM
1142 **
1143 ** Returns          BT_STATUS_SUCCESS if the deletion was successful,
1144 **                  BT_STATUS_FAIL otherwise
1145 **
1146 *******************************************************************************/
1147 bt_status_t btif_storage_remove_ble_local_keys(void)
1148 {
1149     int ret = 1;
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");
1158     btif_config_save();
1159     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1160 }
1161
1162 bt_status_t btif_in_fetch_bonded_ble_device(char *remote_bd_addr,int add, btif_bonded_devices_t *p_bonded_devices)
1163 {
1164     int device_type;
1165     int addr_type;
1166     char buf[100];
1167     UINT32 i;
1168     bt_bdaddr_t bd_addr;
1169     BD_ADDR bta_bd_addr;
1170     BOOLEAN is_device_added =FALSE;
1171     BOOLEAN key_found = FALSE;
1172     tBTA_LE_KEY_VALUE *p;
1173
1174     if(!btif_config_get_int("Remote", remote_bd_addr,"DevType", &device_type))
1175         return BT_STATUS_FAIL;
1176     if(device_type == BT_DEVICE_TYPE_BLE)
1177     {
1178             BTIF_TRACE_DEBUG2("%s %s found a BLE device", __FUNCTION__,remote_bd_addr);
1179             str2bd(remote_bd_addr, &bd_addr);
1180             bdcpy(bta_bd_addr, bd_addr.address);
1181             if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS)
1182             {
1183                 return BT_STATUS_FAIL;
1184             }
1185
1186             memset(buf, 0, sizeof(buf));
1187             if (btif_storage_get_ble_bonding_key(&bd_addr,
1188                                                  BTIF_DM_LE_KEY_PENC,
1189                                                  buf,
1190                                                  sizeof(btif_dm_ble_penc_keys_t)) == BT_STATUS_SUCCESS)
1191             {
1192                 if(add)
1193                 {
1194                     if (!is_device_added)
1195                     {
1196                         BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1197                         is_device_added = TRUE;
1198                     }
1199                     p = (tBTA_LE_KEY_VALUE *)buf;
1200                     for (i=0; i<16; i++)
1201                     {
1202                         BTIF_TRACE_DEBUG2("penc_key.ltk[%d]=0x%02x",i,p->penc_key.ltk[i]);
1203                     }
1204                     for (i=0; i<8; i++)
1205                     {
1206                         BTIF_TRACE_DEBUG2("penc_key.rand[%d]=0x%02x",i,p->penc_key.rand[i]);
1207                     }
1208                     BTIF_TRACE_DEBUG1("p->penc_key.ediv=0x%04x",p->penc_key.ediv);
1209                     BTIF_TRACE_DEBUG1("p->penc_key.sec_level=0x%02x",p->penc_key.sec_level);
1210                     BTIF_TRACE_DEBUG1("p->penc_key.key_size=0x%02x",p->penc_key.key_size);
1211                     BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PENC);
1212                 }
1213                 key_found = TRUE;
1214             }
1215
1216             memset(buf, 0, sizeof(buf));
1217             if (btif_storage_get_ble_bonding_key(&bd_addr,
1218                                                  BTIF_DM_LE_KEY_PID,
1219                                                  buf,
1220                                                  sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1221             {
1222                 if(add)
1223                 {
1224                     if (!is_device_added)
1225                     {
1226                         BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1227                         is_device_added = TRUE;
1228                     }
1229                     p = (tBTA_LE_KEY_VALUE *)buf;
1230                     for (i=0; i<16; i++)
1231                     {
1232                         BTIF_TRACE_DEBUG2("p->pid_key[%d]=0x%02x",i,p->pid_key.irk[i]);
1233                     }
1234
1235                     BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PID);
1236                 }
1237                 key_found = TRUE;
1238             }
1239
1240             memset(buf, 0, sizeof(buf));
1241             if (btif_storage_get_ble_bonding_key(&bd_addr,
1242                                                  BTIF_DM_LE_KEY_PCSRK,
1243                                                  buf,
1244                                                  sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1245             {
1246                 if(add)
1247                 {
1248                     if (!is_device_added)
1249                     {
1250                         BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1251                         is_device_added = TRUE;
1252                     }
1253
1254                     p = (tBTA_LE_KEY_VALUE *)buf;
1255                     for (i=0; i<16; i++)
1256                     {
1257                         BTIF_TRACE_DEBUG2("p->pcsrk_key.csrk[%d]=0x%02x",i, p->psrk_key.csrk[i]);
1258                     }
1259                     BTIF_TRACE_DEBUG1("p->pcsrk_key.counter=0x%08x",p->psrk_key.counter);
1260                     BTIF_TRACE_DEBUG1("p->pcsrk_key.sec_level=0x%02x",p->psrk_key.sec_level);
1261
1262                     BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PCSRK);
1263                 }
1264                 key_found = TRUE;
1265             }
1266
1267             memset(buf, 0, sizeof(buf));
1268             if (btif_storage_get_ble_bonding_key(&bd_addr,
1269                                                  BTIF_DM_LE_KEY_LENC,
1270                                                  buf,
1271                                                  sizeof(btif_dm_ble_lenc_keys_t)) == BT_STATUS_SUCCESS)
1272             {
1273                 if(add)
1274                 {
1275                     if (!is_device_added)
1276                     {
1277                         BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1278                         is_device_added = TRUE;
1279                     }
1280                     p = (tBTA_LE_KEY_VALUE *)buf;
1281                     BTIF_TRACE_DEBUG1("p->lenc_key.div=0x%04x",p->lenc_key.div);
1282                     BTIF_TRACE_DEBUG1("p->lenc_key.key_size=0x%02x",p->lenc_key.key_size);
1283                     BTIF_TRACE_DEBUG1("p->lenc_key.sec_level=0x%02x",p->lenc_key.sec_level);
1284
1285                     BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LENC);
1286                 }
1287                 key_found = TRUE;
1288             }
1289
1290             memset(buf, 0, sizeof(buf));
1291             if (btif_storage_get_ble_bonding_key(&bd_addr,
1292                                                  BTIF_DM_LE_KEY_LCSRK,
1293                                                  buf,
1294                                                  sizeof(btif_dm_ble_lcsrk_keys_t)) == BT_STATUS_SUCCESS)
1295             {
1296                 if(add)
1297                 {
1298                     if (!is_device_added)
1299                     {
1300                         BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1301                         is_device_added = TRUE;
1302                     }
1303                     p = (tBTA_LE_KEY_VALUE *)buf;
1304                     BTIF_TRACE_DEBUG1("p->lcsrk_key.div=0x%04x",p->lcsrk_key.div);
1305                     BTIF_TRACE_DEBUG1("p->lcsrk_key.counter=0x%08x",p->lcsrk_key.counter);
1306                     BTIF_TRACE_DEBUG1("p->lcsrk_key.sec_level=0x%02x",p->lcsrk_key.sec_level);
1307
1308                     BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LCSRK);
1309                 }
1310                 key_found = TRUE;
1311             }
1312
1313             /* Fill in the bonded devices */
1314             if (is_device_added)
1315             {
1316                 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
1317                 btif_gatts_add_bonded_dev_from_nv(bta_bd_addr);
1318             }
1319
1320             if(key_found)
1321                 return BT_STATUS_SUCCESS;
1322             else
1323                 return BT_STATUS_FAIL;
1324     }
1325     return BT_STATUS_FAIL;
1326 }
1327
1328 bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1329                                               UINT8 addr_type)
1330 {
1331     bdstr_t bdstr;
1332     bd2str(remote_bd_addr, &bdstr);
1333     int ret = btif_config_set_int("Remote", bdstr, "AddrType", (int)addr_type);
1334     btif_config_save();
1335     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1336 }
1337
1338 /*******************************************************************************
1339 **
1340 ** Function         btif_storage_get_remote_addr_type
1341 **
1342 ** Description      BTIF storage API - Fetches the remote addr type
1343 **
1344 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
1345 **                  BT_STATUS_FAIL otherwise
1346 **
1347 *******************************************************************************/
1348 bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1349                                               int*addr_type)
1350 {
1351     bdstr_t bdstr;
1352     bd2str(remote_bd_addr, &bdstr);
1353     int ret = btif_config_get_int("Remote", bdstr, "AddrType", addr_type);
1354     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1355 }
1356 #endif
1357 /*******************************************************************************
1358 **
1359 ** Function         btif_storage_add_hid_device_info
1360 **
1361 ** Description      BTIF storage API - Adds the hid information of bonded hid devices-to NVRAM
1362 **
1363 ** Returns          BT_STATUS_SUCCESS if the store was successful,
1364 **                  BT_STATUS_FAIL otherwise
1365 **
1366 *******************************************************************************/
1367
1368 bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr,
1369                                                     UINT16 attr_mask, UINT8 sub_class,
1370                                                     UINT8 app_id, UINT16 vendor_id,
1371                                                     UINT16 product_id, UINT16 version,
1372                                                     UINT8 ctry_code, UINT16 ssr_max_latency,
1373                                                     UINT16 ssr_min_tout, UINT16 dl_len, UINT8 *dsc_list)
1374 {
1375     bdstr_t bdstr;
1376     BTIF_TRACE_DEBUG0("btif_storage_add_hid_device_info:");
1377     bd2str(remote_bd_addr, &bdstr);
1378     btif_config_set_int("Remote", bdstr, "HidAttrMask", attr_mask);
1379     btif_config_set_int("Remote", bdstr, "HidSubClass", sub_class);
1380     btif_config_set_int("Remote", bdstr, "HidAppId", app_id);
1381     btif_config_set_int("Remote", bdstr, "HidVendorId", vendor_id);
1382     btif_config_set_int("Remote", bdstr, "HidProductId", product_id);
1383     btif_config_set_int("Remote", bdstr, "HidVersion", version);
1384     btif_config_set_int("Remote", bdstr, "HidCountryCode", ctry_code);
1385     btif_config_set_int("Remote", bdstr, "HidSSRMaxLatency", ssr_max_latency);
1386     btif_config_set_int("Remote", bdstr, "HidSSRMinTimeout", ssr_min_tout);
1387     if(dl_len > 0)
1388         btif_config_set("Remote", bdstr, "HidDescriptor", (const char*)dsc_list, dl_len,
1389                         BTIF_CFG_TYPE_BIN);
1390     btif_config_save();
1391     return BT_STATUS_SUCCESS;
1392 }
1393
1394 /*******************************************************************************
1395 **
1396 ** Function         btif_storage_load_bonded_hid_info
1397 **
1398 ** Description      BTIF storage API - Loads hid info for all the bonded devices from NVRAM
1399 **                  and adds those devices  to the BTA_HH.
1400 **
1401 ** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
1402 **
1403 *******************************************************************************/
1404 bt_status_t btif_storage_load_bonded_hid_info(void)
1405 {
1406     bt_bdaddr_t bd_addr;
1407     tBTA_HH_DEV_DSCP_INFO dscp_info;
1408     uint32_t i;
1409     uint16_t attr_mask;
1410     uint8_t  sub_class;
1411     uint8_t  app_id;
1412
1413     char kname[128], vname[128];
1414     short kpos;
1415     int kname_size;
1416     kname_size = sizeof(kname);
1417     kname[0] = 0;
1418     kpos = 0;
1419     memset(&dscp_info, 0, sizeof(dscp_info));
1420     do
1421     {
1422         kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
1423         BTIF_TRACE_DEBUG2("Remote device:%s, size:%d", kname, kname_size);
1424         int value;
1425         if(btif_in_fetch_bonded_device(kname) == BT_STATUS_SUCCESS)
1426         {
1427             if(btif_config_get_int("Remote", kname, "HidAttrMask", &value))
1428             {
1429                 attr_mask = (uint16_t)value;
1430
1431                 btif_config_get_int("Remote", kname, "HidSubClass", &value);
1432                 sub_class = (uint8_t)value;
1433
1434                 btif_config_get_int("Remote", kname, "HidAppId", &value);
1435                 app_id = (uint8_t)value;
1436
1437                 btif_config_get_int("Remote", kname, "HidVendorId", &value);
1438                 dscp_info.vendor_id = (uint16_t) value;
1439
1440                 btif_config_get_int("Remote", kname, "HidProductId", &value);
1441                 dscp_info.product_id = (uint16_t) value;
1442
1443                 btif_config_get_int("Remote", kname, "HidVersion", &value);
1444                 dscp_info.version = (uint8_t) value;
1445
1446                 btif_config_get_int("Remote", kname, "HidCountryCode", &value);
1447                 dscp_info.ctry_code = (uint8_t) value;
1448
1449                 value = 0;
1450                 btif_config_get_int("Remote", kname, "HidSSRMaxLatency", &value);
1451                 dscp_info.ssr_max_latency = (uint16_t) value;
1452
1453                 value = 0;
1454                 btif_config_get_int("Remote", kname, "HidSSRMinTimeout", &value);
1455                 dscp_info.ssr_min_tout = (uint16_t) value;
1456
1457                 int len = 0;
1458                 int type;
1459                 btif_config_get("Remote", kname, "HidDescriptor", NULL, &len, &type);
1460                 if(len > 0)
1461                 {
1462                     dscp_info.descriptor.dl_len = (uint16_t)len;
1463                     dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len);
1464                     btif_config_get("Remote", kname, "HidDescriptor", (char*)dscp_info.descriptor.dsc_list, &len, &type);
1465                 }
1466                 str2bd(kname, &bd_addr);
1467                 // add extracted information to BTA HH
1468                 if (btif_hh_add_added_dev(bd_addr,attr_mask))
1469                 {
1470                     BTA_HhAddDev(bd_addr.address, attr_mask, sub_class,
1471                             app_id, dscp_info);
1472                 }
1473             }
1474         }
1475         else
1476         {
1477             if(btif_config_get_int("Remote", kname, "HidAttrMask", &value))
1478             {
1479                 btif_storage_remove_hid_info(&bd_addr);
1480                 str2bd(kname, &bd_addr);
1481             }
1482         }
1483     } while(kpos != -1);
1484
1485     return BT_STATUS_SUCCESS;
1486 }
1487
1488 /*******************************************************************************
1489 **
1490 ** Function         btif_storage_remove_hid_info
1491 **
1492 ** Description      BTIF storage API - Deletes the bonded hid device info from NVRAM
1493 **
1494 ** Returns          BT_STATUS_SUCCESS if the deletion was successful,
1495 **                  BT_STATUS_FAIL otherwise
1496 **
1497 *******************************************************************************/
1498 bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr)
1499 {
1500     char *fname;
1501     int ret;
1502     bdstr_t bdstr;
1503     bd2str(remote_bd_addr, &bdstr);
1504
1505     btif_config_remove("Remote", bdstr, "HidAttrMask");
1506     btif_config_remove("Remote", bdstr, "HidSubClass");
1507     btif_config_remove("Remote", bdstr, "HidAppId");
1508     btif_config_remove("Remote", bdstr, "HidVendorId");
1509     btif_config_remove("Remote", bdstr, "HidProductId");
1510     btif_config_remove("Remote", bdstr, "HidVersion");
1511     btif_config_remove("Remote", bdstr, "HidCountryCode");
1512     btif_config_remove("Remote", bdstr, "HidSSRMaxLatency");
1513     btif_config_remove("Remote", bdstr, "HidSSRMinTimeout");
1514     btif_config_remove("Remote", bdstr, "HidDescriptor");
1515     btif_config_save();
1516     return BT_STATUS_SUCCESS;
1517 }
1518
1519 /*******************************************************************************
1520 **
1521 ** Function         btif_storage_read_hl_apps_cb
1522 **
1523 ** Description      BTIF storage API - Read HL application control block from NVRAM
1524 **
1525 ** Returns          BT_STATUS_SUCCESS if the operation was successful,
1526 **                  BT_STATUS_FAIL otherwise
1527 **
1528 *******************************************************************************/
1529 bt_status_t btif_storage_read_hl_apps_cb(char *value, int value_size)
1530 {
1531     bt_status_t bt_status = BT_STATUS_SUCCESS;
1532     int read_size=value_size, read_type=BTIF_CFG_TYPE_BIN;
1533
1534     if (!btif_config_exist("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB))
1535     {
1536         memset(value, 0, value_size);
1537         if (!btif_config_set("Local", BTIF_STORAGE_HL_APP,BTIF_STORAGE_HL_APP_CB,
1538                              value, value_size, BTIF_CFG_TYPE_BIN))
1539         {
1540             bt_status = BT_STATUS_FAIL;
1541         }
1542         else
1543         {
1544             btif_config_save();
1545         }
1546     }
1547     else
1548     {
1549         if (!btif_config_get("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB,
1550                              value, &read_size, &read_type))
1551         {
1552             bt_status = BT_STATUS_FAIL;
1553         }
1554         else
1555         {
1556             if ((read_size != value_size) || (read_type != BTIF_CFG_TYPE_BIN) )
1557             {
1558                 BTIF_TRACE_ERROR4("%s  value_size=%d read_size=%d read_type=%d",
1559                                   __FUNCTION__, value_size, read_size, read_type);
1560                 bt_status = BT_STATUS_FAIL;
1561             }
1562         }
1563
1564     }
1565
1566     BTIF_TRACE_DEBUG3("%s  status=%d value_size=%d", __FUNCTION__, bt_status, value_size);
1567     return bt_status;
1568 }
1569
1570
1571 /*******************************************************************************
1572 **
1573 ** Function         btif_storage_load_autopair_device_list
1574 **
1575 ** Description      BTIF storage API - Populates auto pair device list
1576 **
1577 ** Returns          BT_STATUS_SUCCESS if the auto pair blacklist is successfully populated
1578 **                  BT_STATUS_FAIL otherwise
1579 **
1580 *******************************************************************************/
1581 bt_status_t btif_storage_load_autopair_device_list() {
1582     // Configuration has already been loaded. No need to reload.
1583     if (btif_config_exist("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, NULL)) {
1584         return BT_STATUS_SUCCESS;
1585     }
1586
1587     static const char *key_names[] = {
1588         BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR,
1589         BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME,
1590         BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST,
1591         BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME,
1592         BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR,
1593     };
1594
1595     config_t *config = config_new(BTIF_AUTO_PAIR_CONF_FILE);
1596     if (!config) {
1597         ALOGE("%s failed to open auto pair blacklist conf file '%s'.", __func__, BTIF_AUTO_PAIR_CONF_FILE);
1598         return BT_STATUS_FAIL;
1599     }
1600
1601     for (size_t i = 0; i < ARRAY_SIZE(key_names); ++i) {
1602         const char *value = config_get_string(config, CONFIG_DEFAULT_SECTION, key_names[i], NULL);
1603         if (value) {
1604             btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, key_names[i], value);
1605         }
1606     }
1607
1608     config_free(config);
1609     return BT_STATUS_SUCCESS;
1610 }
1611
1612 /*******************************************************************************
1613 **
1614 ** Function         btif_storage_is_device_autopair_blacklisted
1615 **
1616 ** Description      BTIF storage API  Checks if the given device is blacklisted for auto pairing
1617 **
1618 ** Returns          TRUE if the device is found in the auto pair blacklist
1619 **                  FALSE otherwise
1620 **
1621 *******************************************************************************/
1622 BOOLEAN  btif_storage_is_device_autopair_blacklisted(bt_bdaddr_t *remote_bd_addr)
1623 {
1624     char *token;
1625     int ret;
1626     bdstr_t bdstr;
1627     char *dev_name_str;
1628     uint8_t i = 0;
1629     char value[BTIF_STORAGE_MAX_LINE_SZ];
1630     int value_size = sizeof(value);
1631
1632     bd2str(remote_bd_addr, &bdstr);
1633
1634     /* Consider only  Lower Address Part from BD Address */
1635     bdstr[8] = '\0';
1636
1637     if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1638                 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR, value, &value_size))
1639     {
1640         if (strcasestr(value,bdstr) != NULL)
1641             return TRUE;
1642     }
1643
1644     dev_name_str = BTM_SecReadDevName((remote_bd_addr->address));
1645
1646     if (dev_name_str != NULL)
1647     {
1648         value_size = sizeof(value);
1649         if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1650                     BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME, value, &value_size))
1651         {
1652             if (strstr(value,dev_name_str) != NULL)
1653                 return TRUE;
1654         }
1655         value_size = sizeof(value);
1656         if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1657                     BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME, value, &value_size))
1658         {
1659             token = strtok(value, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1660             while (token != NULL)
1661             {
1662                 if (strstr(dev_name_str, token) != NULL)
1663                     return TRUE;
1664
1665                 token = strtok(NULL, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1666             }
1667         }
1668     }
1669     if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1670                 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, value, &value_size))
1671     {
1672         if (strstr(value,bdstr) != NULL)
1673             return TRUE;
1674     }
1675     return FALSE;
1676 }
1677
1678 /*******************************************************************************
1679 **
1680 ** Function         btif_storage_add_device_to_autopair_blacklist
1681 **
1682 ** Description      BTIF storage API - Add a remote device to the auto pairing blacklist
1683 **
1684 ** Returns          BT_STATUS_SUCCESS if the device is successfully added to the auto pair blacklist
1685 **                  BT_STATUS_FAIL otherwise
1686 **
1687 *******************************************************************************/
1688 bt_status_t btif_storage_add_device_to_autopair_blacklist(bt_bdaddr_t *remote_bd_addr)
1689 {
1690     int ret;
1691     bdstr_t bdstr;
1692     char linebuf[BTIF_STORAGE_MAX_LINE_SZ+20];
1693     char input_value [20];
1694
1695     bd2str(remote_bd_addr, &bdstr);
1696     strlcpy(input_value, (char*)bdstr, sizeof(input_value));
1697     strlcat(input_value,BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR, sizeof(input_value));
1698
1699     int line_size = sizeof(linebuf);
1700     if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1701                             BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf, &line_size))
1702     {
1703          /* Append this address to the dynamic List of BD address  */
1704         strncat (linebuf, input_value, BTIF_STORAGE_MAX_LINE_SZ);
1705     }
1706     else
1707     {
1708         strncpy( linebuf,input_value, BTIF_STORAGE_MAX_LINE_SZ);
1709     }
1710
1711     /* Write back the key value */
1712     ret = btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1713                         BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf);
1714
1715     return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1716 }
1717
1718 /*******************************************************************************
1719 **
1720 ** Function         btif_storage_is_fixed_pin_zeros_keyboard
1721 **
1722 ** Description      BTIF storage API - checks if this device has fixed PIN key device list
1723 **
1724 ** Returns          TRUE   if the device is found in the fixed pin keyboard device list
1725 **                  FALSE otherwise
1726 **
1727 *******************************************************************************/
1728 BOOLEAN btif_storage_is_fixed_pin_zeros_keyboard(bt_bdaddr_t *remote_bd_addr)
1729 {
1730     int ret;
1731     bdstr_t bdstr;
1732     char *dev_name_str;
1733     uint8_t i = 0;
1734     char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1735
1736     bd2str(remote_bd_addr, &bdstr);
1737
1738     /*consider on LAP part of BDA string*/
1739     bdstr[8] = '\0';
1740
1741     int line_size = sizeof(linebuf);
1742     if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1743                             BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST, linebuf, &line_size))
1744     {
1745         if (strcasestr(linebuf,bdstr) != NULL)
1746             return TRUE;
1747     }
1748     return FALSE;
1749
1750 }
1751
1752 /*******************************************************************************
1753 **
1754 ** Function         btif_storage_set_dmt_support_type
1755 **
1756 ** Description      Sets DMT support status for a remote device
1757 **
1758 ** Returns          BT_STATUS_SUCCESS if config update is successful
1759 **                  BT_STATUS_FAIL otherwise
1760 **
1761 *******************************************************************************/
1762
1763 bt_status_t btif_storage_set_dmt_support_type(const bt_bdaddr_t *remote_bd_addr,
1764                                                    BOOLEAN dmt_supported)
1765 {
1766     int ret;
1767     bdstr_t bdstr = {0};
1768     if(remote_bd_addr)
1769     {
1770         bd2str(remote_bd_addr, &bdstr);
1771     }
1772     else
1773     {
1774         BTIF_TRACE_ERROR1("%s  NULL BD Address", __FUNCTION__);
1775         return BT_STATUS_FAIL;
1776     }
1777
1778    ret = btif_config_set_int("Remote", bdstr,"DMTSupported", (int)dmt_supported);
1779    return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1780
1781 }
1782
1783 /*******************************************************************************
1784 **
1785 ** Function         btif_storage_is_dmt_supported_device
1786 **
1787 ** Description      checks if a device supports Dual mode topology
1788 **
1789 ** Returns         TRUE if remote address is valid and supports DMT else FALSE
1790 **
1791 *******************************************************************************/
1792
1793 BOOLEAN btif_storage_is_dmt_supported_device(const bt_bdaddr_t *remote_bd_addr)
1794 {
1795     int    dmt_supported = 0;
1796     bdstr_t bdstr = {0};
1797     if(remote_bd_addr)
1798         bd2str(remote_bd_addr, &bdstr);
1799
1800     if(remote_bd_addr)
1801     {
1802         bd2str(remote_bd_addr, &bdstr);
1803     }
1804     else
1805     {
1806         BTIF_TRACE_ERROR1("%s  NULL BD Address", __FUNCTION__);
1807         return FALSE;
1808     }
1809
1810     btif_config_get_int("Remote", bdstr,"DMTSupported", &dmt_supported);
1811
1812     return dmt_supported == 1 ? TRUE:FALSE;
1813 }
1814
1815