OSDN Git Service

am 156c52bb: Send correct status for SMP AUTH failure
[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_DEBUG("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_ERROR("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_ERROR("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_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
320     if(prop->len <= 0)
321     {
322         BTIF_TRACE_ERROR("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_ERROR("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 (BLE_INCLUDED == TRUE)
461         if((btif_in_fetch_bonded_ble_device(bdstr, FALSE, NULL) != BT_STATUS_SUCCESS)
462                 && (!bt_linkkey_file_found))
463         {
464             BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", bdstr);
465             return BT_STATUS_FAIL;
466         }
467 #else
468         if((!bt_linkkey_file_found))
469         {
470             BTIF_TRACE_DEBUG("Remote device:%s, no link key found", bdstr);
471             return BT_STATUS_FAIL;
472         }
473 #endif
474     return BT_STATUS_SUCCESS;
475 }
476
477 /*******************************************************************************
478 **
479 ** Function         btif_in_fetch_bonded_devices
480 **
481 ** Description      Internal helper function to fetch the bonded devices
482 **                  from NVRAM
483 **
484 ** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
485 **
486 *******************************************************************************/
487 static bt_status_t btif_in_fetch_bonded_devices(btif_bonded_devices_t *p_bonded_devices, int add)
488 {
489     BTIF_TRACE_DEBUG("in add:%d", add);
490     memset(p_bonded_devices, 0, sizeof(btif_bonded_devices_t));
491
492     char kname[128], vname[128];
493     short kpos;
494     int kname_size;
495     kname_size = sizeof(kname);
496     kname[0] = 0;
497     kpos = 0;
498     BOOLEAN bt_linkkey_file_found=FALSE;
499     int device_type;
500
501     do
502     {
503         kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
504         BTIF_TRACE_DEBUG("Remote device:%s, size:%d", kname, kname_size);
505         int type = BTIF_CFG_TYPE_BIN;
506         LINK_KEY link_key;
507         int size = sizeof(link_key);
508         if(btif_config_get("Remote", kname, "LinkKey", (char*)link_key, &size, &type))
509         {
510             int linkkey_type;
511             if(btif_config_get_int("Remote", kname, "LinkKeyType", &linkkey_type))
512             {
513                 //int pin_len;
514                 //btif_config_get_int("Remote", kname, "PinLength", &pin_len))
515                 bt_bdaddr_t bd_addr;
516                 str2bd(kname, &bd_addr);
517                 if(add)
518                 {
519                     DEV_CLASS dev_class = {0, 0, 0};
520                     int cod;
521                     if(btif_config_get_int("Remote", kname, "DevClass", &cod))
522                         uint2devclass((UINT32)cod, dev_class);
523                     BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0, (UINT8)linkkey_type, 0);
524
525 #if BLE_INCLUDED == TRUE
526                     if (btif_config_get_int("Remote", kname, "DevType", &device_type) &&
527                        (device_type == BT_DEVICE_TYPE_DUMO) )
528                     {
529                         btif_gatts_add_bonded_dev_from_nv(bd_addr.address);
530                     }
531 #endif
532                 }
533                 bt_linkkey_file_found = TRUE;
534                 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
535             }
536             else
537             {
538 #if (BLE_INCLUDED == TRUE)
539                 bt_linkkey_file_found = FALSE;
540 #else
541                 BTIF_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid", kname);
542 #endif
543             }
544         }
545 #if (BLE_INCLUDED == TRUE)
546             if(!(btif_in_fetch_bonded_ble_device(kname,add, p_bonded_devices)) && (!bt_linkkey_file_found))
547             {
548                 BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", kname);
549             }
550 #else
551             if(!bt_linkkey_file_found)
552                 BTIF_TRACE_DEBUG("Remote device:%s, no link key", kname);
553 #endif
554         kname_size = sizeof(kname);
555         kname[0] = 0;
556     } while(kpos != -1);
557     BTIF_TRACE_DEBUG("out");
558     return BT_STATUS_SUCCESS;
559 }
560
561 /************************************************************************************
562 **  Externs
563 ************************************************************************************/
564
565 /************************************************************************************
566 **  Functions
567 ************************************************************************************/
568
569 /** functions are synchronous.
570  * functions can be called by both internal modules such as BTIF_DM and by external entiries from HAL via BTIF_context_switch
571  * For OUT parameters,  caller is expected to provide the memory.
572  * Caller is expected to provide a valid pointer to 'property->value' based on the property->type
573  */
574 /*******************************************************************************
575 **
576 ** Function         btif_storage_get_adapter_property
577 **
578 ** Description      BTIF storage API - Fetches the adapter property->type
579 **                  from NVRAM and fills property->val.
580 **                  Caller should provide memory for property->val and
581 **                  set the property->val
582 **
583 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
584 **                  BT_STATUS_FAIL otherwise
585 **
586 *******************************************************************************/
587 bt_status_t btif_storage_get_adapter_property(bt_property_t *property)
588 {
589
590     /* Special handling for adapter BD_ADDR and BONDED_DEVICES */
591     if (property->type == BT_PROPERTY_BDADDR)
592     {
593         BD_ADDR addr;
594         bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)property->val;
595         /* This has been cached in btif. Just fetch it from there */
596         memcpy(bd_addr, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
597         property->len = sizeof(bt_bdaddr_t);
598         return BT_STATUS_SUCCESS;
599     }
600     else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES)
601     {
602         btif_bonded_devices_t bonded_devices;
603
604         btif_in_fetch_bonded_devices(&bonded_devices, 0);
605
606         BTIF_TRACE_DEBUG("%s: Number of bonded devices: %d Property:BT_PROPERTY_ADAPTER_BONDED_DEVICES", __FUNCTION__, bonded_devices.num_devices);
607
608         if (bonded_devices.num_devices > 0)
609         {
610             property->len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
611             memcpy(property->val, bonded_devices.devices, property->len);
612         }
613
614         /* if there are no bonded_devices, then length shall be 0 */
615         return BT_STATUS_SUCCESS;
616     }
617     else if (property->type == BT_PROPERTY_UUIDS)
618     {
619         /* publish list of local supported services */
620         bt_uuid_t *p_uuid = (bt_uuid_t*)property->val;
621         uint32_t num_uuids = 0;
622         uint32_t i;
623
624         tBTA_SERVICE_MASK service_mask = btif_get_enabled_services_mask();
625         BTIF_TRACE_ERROR("%s service_mask:0x%x", __FUNCTION__, service_mask);
626         for (i=0; i < BTA_MAX_SERVICE_ID; i++)
627         {
628             /* This should eventually become a function when more services are enabled */
629             if (service_mask
630                 &(tBTA_SERVICE_MASK)(1 << i))
631             {
632                 switch (i)
633                 {
634                     case BTA_HFP_SERVICE_ID:
635                         {
636                             uuid16_to_uuid128(UUID_SERVCLASS_AG_HANDSFREE,
637                                               p_uuid+num_uuids);
638                             num_uuids++;
639                         }
640                     /* intentional fall through: Send both BFP & HSP UUIDs if HFP is enabled */
641                     case BTA_HSP_SERVICE_ID:
642                         {
643                             uuid16_to_uuid128(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,
644                                               p_uuid+num_uuids);
645                             num_uuids++;
646                         }break;
647                     case BTA_A2DP_SERVICE_ID:
648                         {
649                             uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SOURCE,
650                                               p_uuid+num_uuids);
651                             num_uuids++;
652                         }break;
653                     case BTA_HFP_HS_SERVICE_ID:
654                         {
655                             uuid16_to_uuid128(UUID_SERVCLASS_HF_HANDSFREE,
656                                               p_uuid+num_uuids);
657                             num_uuids++;
658                         }break;
659                 }
660             }
661         }
662         property->len = (num_uuids)*sizeof(bt_uuid_t);
663         return BT_STATUS_SUCCESS;
664     }
665
666     /* fall through for other properties */
667     if(!cfg2prop(NULL, property))
668     {
669         return btif_dm_get_adapter_property(property);
670     }
671     return BT_STATUS_SUCCESS;
672  }
673
674 /*******************************************************************************
675 **
676 ** Function         btif_storage_set_adapter_property
677 **
678 ** Description      BTIF storage API - Stores the adapter property
679 **                  to NVRAM
680 **
681 ** Returns          BT_STATUS_SUCCESS if the store was successful,
682 **                  BT_STATUS_FAIL otherwise
683 **
684 *******************************************************************************/
685 bt_status_t btif_storage_set_adapter_property(bt_property_t *property)
686 {
687     return prop2cfg(NULL, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
688 }
689
690 /*******************************************************************************
691 **
692 ** Function         btif_storage_get_remote_device_property
693 **
694 ** Description      BTIF storage API - Fetches the remote device property->type
695 **                  from NVRAM and fills property->val.
696 **                  Caller should provide memory for property->val and
697 **                  set the property->val
698 **
699 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
700 **                  BT_STATUS_FAIL otherwise
701 **
702 *******************************************************************************/
703 bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t *remote_bd_addr,
704                                                     bt_property_t *property)
705 {
706     return cfg2prop(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
707 }
708 /*******************************************************************************
709 **
710 ** Function         btif_storage_set_remote_device_property
711 **
712 ** Description      BTIF storage API - Stores the remote device property
713 **                  to NVRAM
714 **
715 ** Returns          BT_STATUS_SUCCESS if the store was successful,
716 **                  BT_STATUS_FAIL otherwise
717 **
718 *******************************************************************************/
719 bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t *remote_bd_addr,
720                                                     bt_property_t *property)
721 {
722     return prop2cfg(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
723 }
724
725 /*******************************************************************************
726 **
727 ** Function         btif_storage_add_remote_device
728 **
729 ** Description      BTIF storage API - Adds a newly discovered device to NVRAM
730 **                  along with the timestamp. Also, stores the various
731 **                  properties - RSSI, BDADDR, NAME (if found in EIR)
732 **
733 ** Returns          BT_STATUS_SUCCESS if the store was successful,
734 **                  BT_STATUS_FAIL otherwise
735 **
736 *******************************************************************************/
737 bt_status_t btif_storage_add_remote_device(bt_bdaddr_t *remote_bd_addr,
738                                            uint32_t num_properties,
739                                            bt_property_t *properties)
740 {
741     uint32_t i = 0;
742     /* TODO: If writing a property, fails do we go back undo the earlier
743      * written properties? */
744     for (i=0; i < num_properties; i++)
745     {
746         /* Ignore the RSSI as this is not stored in DB */
747         if (properties[i].type == BT_PROPERTY_REMOTE_RSSI)
748             continue;
749
750         /* BD_ADDR for remote device needs special handling as we also store timestamp */
751         if (properties[i].type == BT_PROPERTY_BDADDR)
752         {
753             bt_property_t addr_prop;
754             memcpy(&addr_prop, &properties[i], sizeof(bt_property_t));
755             addr_prop.type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP;
756             btif_storage_set_remote_device_property(remote_bd_addr,
757                                                     &addr_prop);
758         }
759         else
760         {
761             btif_storage_set_remote_device_property(remote_bd_addr,
762                                                     &properties[i]);
763         }
764     }
765     return BT_STATUS_SUCCESS;
766 }
767
768 /*******************************************************************************
769 **
770 ** Function         btif_storage_add_bonded_device
771 **
772 ** Description      BTIF storage API - Adds the newly bonded device to NVRAM
773 **                  along with the link-key, Key type and Pin key length
774 **
775 ** Returns          BT_STATUS_SUCCESS if the store was successful,
776 **                  BT_STATUS_FAIL otherwise
777 **
778 *******************************************************************************/
779
780 bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
781                                            LINK_KEY link_key,
782                                            uint8_t key_type,
783                                            uint8_t pin_length)
784 {
785     bdstr_t bdstr;
786     bd2str(remote_bd_addr, &bdstr);
787     int ret = btif_config_set_int("Remote", bdstr, "LinkKeyType", (int)key_type);
788     ret &= btif_config_set_int("Remote", bdstr, "PinLength", (int)pin_length);
789     ret &= btif_config_set("Remote", bdstr, "LinkKey", (const char*)link_key, sizeof(LINK_KEY), BTIF_CFG_TYPE_BIN);
790     /* write bonded info immediately */
791     btif_config_flush();
792     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
793 }
794
795 /*******************************************************************************
796 **
797 ** Function         btif_storage_remove_bonded_device
798 **
799 ** Description      BTIF storage API - Deletes the bonded device from NVRAM
800 **
801 ** Returns          BT_STATUS_SUCCESS if the deletion was successful,
802 **                  BT_STATUS_FAIL otherwise
803 **
804 *******************************************************************************/
805 bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
806 {
807     bdstr_t bdstr;
808     bd2str(remote_bd_addr, &bdstr);
809     BTIF_TRACE_DEBUG("in bd addr:%s", bdstr);
810     int ret = 1;
811     if(btif_config_exist("Remote", bdstr, "LinkKeyType"))
812         ret &= btif_config_remove("Remote", bdstr, "LinkKeyType");
813     if(btif_config_exist("Remote", bdstr, "PinLength"))
814         ret &= btif_config_remove("Remote", bdstr, "PinLength");
815     if(btif_config_exist("Remote", bdstr, "LinkKey"))
816         ret &= btif_config_remove("Remote", bdstr, "LinkKey");
817     /* write bonded info immediately */
818     btif_config_flush();
819     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
820
821 }
822
823 /*******************************************************************************
824 **
825 ** Function         btif_storage_load_bonded_devices
826 **
827 ** Description      BTIF storage API - Loads all the bonded devices from NVRAM
828 **                  and adds to the BTA.
829 **                  Additionally, this API also invokes the adaper_properties_cb
830 **                  and remote_device_properties_cb for each of the bonded devices.
831 **
832 ** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
833 **
834 *******************************************************************************/
835 bt_status_t btif_storage_load_bonded_devices(void)
836 {
837     char *fname;
838     btif_bonded_devices_t bonded_devices;
839     uint32_t i = 0;
840     bt_property_t adapter_props[6];
841     uint32_t num_props = 0;
842     bt_property_t remote_properties[8];
843     bt_bdaddr_t addr;
844     bt_bdname_t name, alias;
845     bt_scan_mode_t mode;
846     uint32_t disc_timeout;
847     bt_bdaddr_t *devices_list;
848     bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
849     bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
850     uint32_t cod, devtype;
851
852     btif_in_fetch_bonded_devices(&bonded_devices, 1);
853
854     /* Now send the adapter_properties_cb with all adapter_properties */
855     {
856         memset(adapter_props, 0, sizeof(adapter_props));
857
858         /* BD_ADDR */
859         BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDADDR, &addr, sizeof(addr),
860                                       adapter_props[num_props]);
861         num_props++;
862
863         /* BD_NAME */
864         BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDNAME, &name, sizeof(name),
865                                       adapter_props[num_props]);
866         num_props++;
867
868         /* SCAN_MODE */
869         /* TODO: At the time of BT on, always report the scan mode as 0 irrespective
870          of the scan_mode during the previous enable cycle.
871          This needs to be re-visited as part of the app/stack enable sequence
872          synchronization */
873         mode = BT_SCAN_MODE_NONE;
874         adapter_props[num_props].type = BT_PROPERTY_ADAPTER_SCAN_MODE;
875         adapter_props[num_props].len = sizeof(mode);
876         adapter_props[num_props].val = &mode;
877         num_props++;
878
879         /* DISC_TIMEOUT */
880         BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
881                                       &disc_timeout, sizeof(disc_timeout),
882                                       adapter_props[num_props]);
883         num_props++;
884
885         /* BONDED_DEVICES */
886         devices_list = (bt_bdaddr_t*)malloc(sizeof(bt_bdaddr_t)*bonded_devices.num_devices);
887         adapter_props[num_props].type = BT_PROPERTY_ADAPTER_BONDED_DEVICES;
888         adapter_props[num_props].len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
889         adapter_props[num_props].val = devices_list;
890         for (i=0; i < bonded_devices.num_devices; i++)
891         {
892             memcpy(devices_list + i, &bonded_devices.devices[i], sizeof(bt_bdaddr_t));
893         }
894         num_props++;
895
896         /* LOCAL UUIDs */
897         BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_UUIDS,
898                                       local_uuids, sizeof(local_uuids),
899                                       adapter_props[num_props]);
900         num_props++;
901
902         btif_adapter_properties_evt(BT_STATUS_SUCCESS, num_props, adapter_props);
903
904         free(devices_list);
905     }
906
907     BTIF_TRACE_EVENT("%s: %d bonded devices found", __FUNCTION__, bonded_devices.num_devices);
908
909     {
910         for (i = 0; i < bonded_devices.num_devices; i++)
911         {
912             bt_bdaddr_t *p_remote_addr;
913
914             num_props = 0;
915             p_remote_addr = &bonded_devices.devices[i];
916             memset(remote_properties, 0, sizeof(remote_properties));
917             BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_BDNAME,
918                                          &name, sizeof(name),
919                                          remote_properties[num_props]);
920             num_props++;
921
922             BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_REMOTE_FRIENDLY_NAME,
923                                          &alias, sizeof(alias),
924                                          remote_properties[num_props]);
925             num_props++;
926
927             BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_CLASS_OF_DEVICE,
928                                          &cod, sizeof(cod),
929                                          remote_properties[num_props]);
930             num_props++;
931
932             BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_TYPE_OF_DEVICE,
933                                          &devtype, sizeof(devtype),
934                                          remote_properties[num_props]);
935             num_props++;
936
937             BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_UUIDS,
938                                          remote_uuids, sizeof(remote_uuids),
939                                          remote_properties[num_props]);
940             num_props++;
941
942             btif_remote_properties_evt(BT_STATUS_SUCCESS, p_remote_addr,
943                                        num_props, remote_properties);
944         }
945     }
946     return BT_STATUS_SUCCESS;
947 }
948
949 #if (BLE_INCLUDED == TRUE)
950
951 /*******************************************************************************
952 **
953 ** Function         btif_storage_add_ble_bonding_key
954 **
955 ** Description      BTIF storage API - Adds the newly bonded device to NVRAM
956 **                  along with the ble-key, Key type and Pin key length
957 **
958 ** Returns          BT_STATUS_SUCCESS if the store was successful,
959 **                  BT_STATUS_FAIL otherwise
960 **
961 *******************************************************************************/
962
963 bt_status_t btif_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
964                                            char *key,
965                                            UINT8 key_type,
966                                            UINT8 key_length)
967 {
968     bdstr_t bdstr;
969     bd2str(remote_bd_addr, &bdstr);
970     const char* name;
971     switch(key_type)
972     {
973         case BTIF_DM_LE_KEY_PENC:
974             name = "LE_KEY_PENC";
975             break;
976         case BTIF_DM_LE_KEY_PID:
977             name = "LE_KEY_PID";
978             break;
979         case BTIF_DM_LE_KEY_PCSRK:
980             name = "LE_KEY_PCSRK";
981             break;
982         case BTIF_DM_LE_KEY_LENC:
983             name = "LE_KEY_LENC";
984             break;
985         case BTIF_DM_LE_KEY_LCSRK:
986             name = "LE_KEY_LCSRK";
987             break;
988         default:
989             return BT_STATUS_FAIL;
990     }
991     int ret = btif_config_set("Remote", bdstr, name, (const char*)key, (int)key_length, BTIF_CFG_TYPE_BIN);
992     btif_config_save();
993     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
994 }
995
996 /*******************************************************************************
997 **
998 ** Function         btif_storage_get_ble_bonding_key
999 **
1000 ** Description
1001 **
1002 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
1003 **                  BT_STATUS_FAIL otherwise
1004 **
1005 *******************************************************************************/
1006 bt_status_t btif_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
1007                                              UINT8 key_type,
1008                                              char *key_value,
1009                                              int key_length)
1010 {
1011     bdstr_t bdstr;
1012     bd2str(remote_bd_addr, &bdstr);
1013     const char* name;
1014     int type = BTIF_CFG_TYPE_BIN;
1015     switch(key_type)
1016     {
1017         case BTIF_DM_LE_KEY_PENC:
1018             name = "LE_KEY_PENC";
1019             break;
1020         case BTIF_DM_LE_KEY_PID:
1021             name = "LE_KEY_PID";
1022             break;
1023         case BTIF_DM_LE_KEY_PCSRK:
1024             name = "LE_KEY_PCSRK";
1025             break;
1026         case BTIF_DM_LE_KEY_LENC:
1027             name = "LE_KEY_LENC";
1028             break;
1029         case BTIF_DM_LE_KEY_LCSRK:
1030             name = "LE_KEY_LCSRK";
1031             break;
1032         default:
1033             return BT_STATUS_FAIL;
1034     }
1035     int ret = btif_config_get("Remote", bdstr, name, key_value, &key_length, &type);
1036     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1037
1038 }
1039
1040 /*******************************************************************************
1041 **
1042 ** Function         btif_storage_remove_ble_keys
1043 **
1044 ** Description      BTIF storage API - Deletes the bonded device from NVRAM
1045 **
1046 ** Returns          BT_STATUS_SUCCESS if the deletion was successful,
1047 **                  BT_STATUS_FAIL otherwise
1048 **
1049 *******************************************************************************/
1050 bt_status_t btif_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr)
1051 {
1052     bdstr_t bdstr;
1053     bd2str(remote_bd_addr, &bdstr);
1054     BTIF_TRACE_DEBUG(" %s in bd addr:%s",__FUNCTION__, bdstr);
1055     int ret = 1;
1056     if(btif_config_exist("Remote", bdstr, "LE_KEY_PENC"))
1057         ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PENC");
1058     if(btif_config_exist("Remote", bdstr, "LE_KEY_PID"))
1059         ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PID");
1060     if(btif_config_exist("Remote", bdstr, "LE_KEY_PCSRK"))
1061         ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PCSRK");
1062     if(btif_config_exist("Remote", bdstr, "LE_KEY_LENC"))
1063         ret &= btif_config_remove("Remote", bdstr, "LE_KEY_LENC");
1064     if(btif_config_exist("Remote", bdstr, "LE_KEY_LCSRK"))
1065         ret &= btif_config_remove("Remote", bdstr, "LE_KEY_LCSRK");
1066     btif_config_save();
1067     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1068 }
1069
1070 /*******************************************************************************
1071 **
1072 ** Function         btif_storage_add_ble_local_key
1073 **
1074 ** Description      BTIF storage API - Adds the ble key to NVRAM
1075 **
1076 ** Returns          BT_STATUS_SUCCESS if the store was successful,
1077 **                  BT_STATUS_FAIL otherwise
1078 **
1079 *******************************************************************************/
1080 bt_status_t btif_storage_add_ble_local_key(char *key,
1081                                            uint8_t key_type,
1082                                            uint8_t key_length)
1083 {
1084     const char* name;
1085     switch(key_type)
1086     {
1087         case BTIF_DM_LE_LOCAL_KEY_IR:
1088             name = "LE_LOCAL_KEY_IR";
1089             break;
1090         case BTIF_DM_LE_LOCAL_KEY_IRK:
1091             name = "LE_LOCAL_KEY_IRK";
1092             break;
1093         case BTIF_DM_LE_LOCAL_KEY_DHK:
1094             name = "LE_LOCAL_KEY_DHK";
1095             break;
1096         case BTIF_DM_LE_LOCAL_KEY_ER:
1097             name = "LE_LOCAL_KEY_ER";
1098             break;
1099         default:
1100             return BT_STATUS_FAIL;
1101     }
1102     int ret = btif_config_set("Local", "Adapter", name, (const char*)key, key_length, BTIF_CFG_TYPE_BIN);
1103     btif_config_save();
1104     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1105 }
1106
1107 /*******************************************************************************
1108 **
1109 ** Function         btif_storage_get_ble_local_key
1110 **
1111 ** Description
1112 **
1113 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
1114 **                  BT_STATUS_FAIL otherwise
1115 **
1116 *******************************************************************************/
1117 bt_status_t btif_storage_get_ble_local_key(UINT8 key_type,
1118                                            char *key_value,
1119                                            int key_length)
1120 {
1121     const char* name;
1122     int type = BTIF_CFG_TYPE_BIN;
1123     switch(key_type)
1124     {
1125         case BTIF_DM_LE_LOCAL_KEY_IR:
1126             name = "LE_LOCAL_KEY_IR";
1127             break;
1128         case BTIF_DM_LE_LOCAL_KEY_IRK:
1129             name = "LE_LOCAL_KEY_IRK";
1130             break;
1131         case BTIF_DM_LE_LOCAL_KEY_DHK:
1132             name = "LE_LOCAL_KEY_DHK";
1133             break;
1134         case BTIF_DM_LE_LOCAL_KEY_ER:
1135             name = "LE_LOCAL_KEY_ER";
1136             break;
1137         default:
1138             return BT_STATUS_FAIL;
1139     }
1140     int ret = btif_config_get("Local", "Adapter", name, key_value, &key_length, &type);
1141     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1142
1143 }
1144
1145 /*******************************************************************************
1146 **
1147 ** Function         btif_storage_remove_ble_local_keys
1148 **
1149 ** Description      BTIF storage API - Deletes the bonded device from NVRAM
1150 **
1151 ** Returns          BT_STATUS_SUCCESS if the deletion was successful,
1152 **                  BT_STATUS_FAIL otherwise
1153 **
1154 *******************************************************************************/
1155 bt_status_t btif_storage_remove_ble_local_keys(void)
1156 {
1157     int ret = 1;
1158     if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_IR"))
1159         ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_IR");
1160     if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_IRK"))
1161         ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_IRK");
1162     if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_DHK"))
1163         ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_DHK");
1164     if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_ER"))
1165         ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_ER");
1166     btif_config_save();
1167     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1168 }
1169
1170 bt_status_t btif_in_fetch_bonded_ble_device(char *remote_bd_addr,int add, btif_bonded_devices_t *p_bonded_devices)
1171 {
1172     int device_type;
1173     int addr_type;
1174     char buf[100];
1175     UINT32 i;
1176     bt_bdaddr_t bd_addr;
1177     BD_ADDR bta_bd_addr;
1178     BOOLEAN is_device_added =FALSE;
1179     BOOLEAN key_found = FALSE;
1180     tBTA_LE_KEY_VALUE *p;
1181
1182     if(!btif_config_get_int("Remote", remote_bd_addr,"DevType", &device_type))
1183         return BT_STATUS_FAIL;
1184     if(device_type == BT_DEVICE_TYPE_BLE)
1185     {
1186             BTIF_TRACE_DEBUG("%s %s found a BLE device", __FUNCTION__,remote_bd_addr);
1187             str2bd(remote_bd_addr, &bd_addr);
1188             bdcpy(bta_bd_addr, bd_addr.address);
1189             if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS)
1190             {
1191                 return BT_STATUS_FAIL;
1192             }
1193
1194             memset(buf, 0, sizeof(buf));
1195             if (btif_storage_get_ble_bonding_key(&bd_addr,
1196                                                  BTIF_DM_LE_KEY_PENC,
1197                                                  buf,
1198                                                  sizeof(btif_dm_ble_penc_keys_t)) == BT_STATUS_SUCCESS)
1199             {
1200                 if(add)
1201                 {
1202                     if (!is_device_added)
1203                     {
1204                         BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1205                         is_device_added = TRUE;
1206                     }
1207                     p = (tBTA_LE_KEY_VALUE *)buf;
1208                     for (i=0; i<16; i++)
1209                     {
1210                         BTIF_TRACE_DEBUG("penc_key.ltk[%d]=0x%02x",i,p->penc_key.ltk[i]);
1211                     }
1212                     for (i=0; i<8; i++)
1213                     {
1214                         BTIF_TRACE_DEBUG("penc_key.rand[%d]=0x%02x",i,p->penc_key.rand[i]);
1215                     }
1216                     BTIF_TRACE_DEBUG("p->penc_key.ediv=0x%04x",p->penc_key.ediv);
1217                     BTIF_TRACE_DEBUG("p->penc_key.sec_level=0x%02x",p->penc_key.sec_level);
1218                     BTIF_TRACE_DEBUG("p->penc_key.key_size=0x%02x",p->penc_key.key_size);
1219                     BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PENC);
1220                 }
1221                 key_found = TRUE;
1222             }
1223
1224             memset(buf, 0, sizeof(buf));
1225             if (btif_storage_get_ble_bonding_key(&bd_addr,
1226                                                  BTIF_DM_LE_KEY_PID,
1227                                                  buf,
1228                                                  sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1229             {
1230                 if(add)
1231                 {
1232                     if (!is_device_added)
1233                     {
1234                         BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1235                         is_device_added = TRUE;
1236                     }
1237                     p = (tBTA_LE_KEY_VALUE *)buf;
1238                     for (i=0; i<16; i++)
1239                     {
1240                         BTIF_TRACE_DEBUG("p->pid_key[%d]=0x%02x",i,p->pid_key.irk[i]);
1241                     }
1242
1243                     BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PID);
1244                 }
1245                 key_found = TRUE;
1246             }
1247
1248             memset(buf, 0, sizeof(buf));
1249             if (btif_storage_get_ble_bonding_key(&bd_addr,
1250                                                  BTIF_DM_LE_KEY_PCSRK,
1251                                                  buf,
1252                                                  sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1253             {
1254                 if(add)
1255                 {
1256                     if (!is_device_added)
1257                     {
1258                         BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1259                         is_device_added = TRUE;
1260                     }
1261
1262                     p = (tBTA_LE_KEY_VALUE *)buf;
1263                     for (i=0; i<16; i++)
1264                     {
1265                         BTIF_TRACE_DEBUG("p->pcsrk_key.csrk[%d]=0x%02x",i, p->psrk_key.csrk[i]);
1266                     }
1267                     BTIF_TRACE_DEBUG("p->pcsrk_key.counter=0x%08x",p->psrk_key.counter);
1268                     BTIF_TRACE_DEBUG("p->pcsrk_key.sec_level=0x%02x",p->psrk_key.sec_level);
1269
1270                     BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PCSRK);
1271                 }
1272                 key_found = TRUE;
1273             }
1274
1275             memset(buf, 0, sizeof(buf));
1276             if (btif_storage_get_ble_bonding_key(&bd_addr,
1277                                                  BTIF_DM_LE_KEY_LENC,
1278                                                  buf,
1279                                                  sizeof(btif_dm_ble_lenc_keys_t)) == BT_STATUS_SUCCESS)
1280             {
1281                 if(add)
1282                 {
1283                     if (!is_device_added)
1284                     {
1285                         BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1286                         is_device_added = TRUE;
1287                     }
1288                     p = (tBTA_LE_KEY_VALUE *)buf;
1289                     BTIF_TRACE_DEBUG("p->lenc_key.div=0x%04x",p->lenc_key.div);
1290                     BTIF_TRACE_DEBUG("p->lenc_key.key_size=0x%02x",p->lenc_key.key_size);
1291                     BTIF_TRACE_DEBUG("p->lenc_key.sec_level=0x%02x",p->lenc_key.sec_level);
1292
1293                     BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LENC);
1294                 }
1295                 key_found = TRUE;
1296             }
1297
1298             memset(buf, 0, sizeof(buf));
1299             if (btif_storage_get_ble_bonding_key(&bd_addr,
1300                                                  BTIF_DM_LE_KEY_LCSRK,
1301                                                  buf,
1302                                                  sizeof(btif_dm_ble_lcsrk_keys_t)) == BT_STATUS_SUCCESS)
1303             {
1304                 if(add)
1305                 {
1306                     if (!is_device_added)
1307                     {
1308                         BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1309                         is_device_added = TRUE;
1310                     }
1311                     p = (tBTA_LE_KEY_VALUE *)buf;
1312                     BTIF_TRACE_DEBUG("p->lcsrk_key.div=0x%04x",p->lcsrk_key.div);
1313                     BTIF_TRACE_DEBUG("p->lcsrk_key.counter=0x%08x",p->lcsrk_key.counter);
1314                     BTIF_TRACE_DEBUG("p->lcsrk_key.sec_level=0x%02x",p->lcsrk_key.sec_level);
1315
1316                     BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LCSRK);
1317                 }
1318                 key_found = TRUE;
1319             }
1320
1321             /* Fill in the bonded devices */
1322             if (is_device_added)
1323             {
1324                 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
1325                 btif_gatts_add_bonded_dev_from_nv(bta_bd_addr);
1326             }
1327
1328             if(key_found)
1329                 return BT_STATUS_SUCCESS;
1330             else
1331                 return BT_STATUS_FAIL;
1332     }
1333     return BT_STATUS_FAIL;
1334 }
1335
1336 bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1337                                               UINT8 addr_type)
1338 {
1339     bdstr_t bdstr;
1340     bd2str(remote_bd_addr, &bdstr);
1341     int ret = btif_config_set_int("Remote", bdstr, "AddrType", (int)addr_type);
1342     btif_config_save();
1343     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1344 }
1345
1346 /*******************************************************************************
1347 **
1348 ** Function         btif_storage_get_remote_addr_type
1349 **
1350 ** Description      BTIF storage API - Fetches the remote addr type
1351 **
1352 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
1353 **                  BT_STATUS_FAIL otherwise
1354 **
1355 *******************************************************************************/
1356 bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1357                                               int*addr_type)
1358 {
1359     bdstr_t bdstr;
1360     bd2str(remote_bd_addr, &bdstr);
1361     int ret = btif_config_get_int("Remote", bdstr, "AddrType", addr_type);
1362     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1363 }
1364 #endif
1365 /*******************************************************************************
1366 **
1367 ** Function         btif_storage_add_hid_device_info
1368 **
1369 ** Description      BTIF storage API - Adds the hid information of bonded hid devices-to NVRAM
1370 **
1371 ** Returns          BT_STATUS_SUCCESS if the store was successful,
1372 **                  BT_STATUS_FAIL otherwise
1373 **
1374 *******************************************************************************/
1375
1376 bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr,
1377                                                     UINT16 attr_mask, UINT8 sub_class,
1378                                                     UINT8 app_id, UINT16 vendor_id,
1379                                                     UINT16 product_id, UINT16 version,
1380                                                     UINT8 ctry_code, UINT16 ssr_max_latency,
1381                                                     UINT16 ssr_min_tout, UINT16 dl_len, UINT8 *dsc_list)
1382 {
1383     bdstr_t bdstr;
1384     BTIF_TRACE_DEBUG("btif_storage_add_hid_device_info:");
1385     bd2str(remote_bd_addr, &bdstr);
1386     btif_config_set_int("Remote", bdstr, "HidAttrMask", attr_mask);
1387     btif_config_set_int("Remote", bdstr, "HidSubClass", sub_class);
1388     btif_config_set_int("Remote", bdstr, "HidAppId", app_id);
1389     btif_config_set_int("Remote", bdstr, "HidVendorId", vendor_id);
1390     btif_config_set_int("Remote", bdstr, "HidProductId", product_id);
1391     btif_config_set_int("Remote", bdstr, "HidVersion", version);
1392     btif_config_set_int("Remote", bdstr, "HidCountryCode", ctry_code);
1393     btif_config_set_int("Remote", bdstr, "HidSSRMaxLatency", ssr_max_latency);
1394     btif_config_set_int("Remote", bdstr, "HidSSRMinTimeout", ssr_min_tout);
1395     if(dl_len > 0)
1396         btif_config_set("Remote", bdstr, "HidDescriptor", (const char*)dsc_list, dl_len,
1397                         BTIF_CFG_TYPE_BIN);
1398     btif_config_save();
1399     return BT_STATUS_SUCCESS;
1400 }
1401
1402 /*******************************************************************************
1403 **
1404 ** Function         btif_storage_load_bonded_hid_info
1405 **
1406 ** Description      BTIF storage API - Loads hid info for all the bonded devices from NVRAM
1407 **                  and adds those devices  to the BTA_HH.
1408 **
1409 ** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
1410 **
1411 *******************************************************************************/
1412 bt_status_t btif_storage_load_bonded_hid_info(void)
1413 {
1414     bt_bdaddr_t bd_addr;
1415     tBTA_HH_DEV_DSCP_INFO dscp_info;
1416     uint32_t i;
1417     uint16_t attr_mask;
1418     uint8_t  sub_class;
1419     uint8_t  app_id;
1420
1421     char kname[128], vname[128];
1422     short kpos;
1423     int kname_size;
1424     kname_size = sizeof(kname);
1425     kname[0] = 0;
1426     kpos = 0;
1427     memset(&dscp_info, 0, sizeof(dscp_info));
1428     do
1429     {
1430         kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
1431         BTIF_TRACE_DEBUG("Remote device:%s, size:%d", kname, kname_size);
1432         int value;
1433         if(btif_in_fetch_bonded_device(kname) == BT_STATUS_SUCCESS)
1434         {
1435             if(btif_config_get_int("Remote", kname, "HidAttrMask", &value))
1436             {
1437                 attr_mask = (uint16_t)value;
1438
1439                 btif_config_get_int("Remote", kname, "HidSubClass", &value);
1440                 sub_class = (uint8_t)value;
1441
1442                 btif_config_get_int("Remote", kname, "HidAppId", &value);
1443                 app_id = (uint8_t)value;
1444
1445                 btif_config_get_int("Remote", kname, "HidVendorId", &value);
1446                 dscp_info.vendor_id = (uint16_t) value;
1447
1448                 btif_config_get_int("Remote", kname, "HidProductId", &value);
1449                 dscp_info.product_id = (uint16_t) value;
1450
1451                 btif_config_get_int("Remote", kname, "HidVersion", &value);
1452                 dscp_info.version = (uint8_t) value;
1453
1454                 btif_config_get_int("Remote", kname, "HidCountryCode", &value);
1455                 dscp_info.ctry_code = (uint8_t) value;
1456
1457                 value = 0;
1458                 btif_config_get_int("Remote", kname, "HidSSRMaxLatency", &value);
1459                 dscp_info.ssr_max_latency = (uint16_t) value;
1460
1461                 value = 0;
1462                 btif_config_get_int("Remote", kname, "HidSSRMinTimeout", &value);
1463                 dscp_info.ssr_min_tout = (uint16_t) value;
1464
1465                 int len = 0;
1466                 int type;
1467                 btif_config_get("Remote", kname, "HidDescriptor", NULL, &len, &type);
1468                 if(len > 0)
1469                 {
1470                     dscp_info.descriptor.dl_len = (uint16_t)len;
1471                     dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len);
1472                     btif_config_get("Remote", kname, "HidDescriptor", (char*)dscp_info.descriptor.dsc_list, &len, &type);
1473                 }
1474                 str2bd(kname, &bd_addr);
1475                 // add extracted information to BTA HH
1476                 if (btif_hh_add_added_dev(bd_addr,attr_mask))
1477                 {
1478                     BTA_HhAddDev(bd_addr.address, attr_mask, sub_class,
1479                             app_id, dscp_info);
1480                 }
1481             }
1482         }
1483         else
1484         {
1485             if(btif_config_get_int("Remote", kname, "HidAttrMask", &value))
1486             {
1487                 btif_storage_remove_hid_info(&bd_addr);
1488                 str2bd(kname, &bd_addr);
1489             }
1490         }
1491     } while(kpos != -1);
1492
1493     return BT_STATUS_SUCCESS;
1494 }
1495
1496 /*******************************************************************************
1497 **
1498 ** Function         btif_storage_remove_hid_info
1499 **
1500 ** Description      BTIF storage API - Deletes the bonded hid device info from NVRAM
1501 **
1502 ** Returns          BT_STATUS_SUCCESS if the deletion was successful,
1503 **                  BT_STATUS_FAIL otherwise
1504 **
1505 *******************************************************************************/
1506 bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr)
1507 {
1508     char *fname;
1509     int ret;
1510     bdstr_t bdstr;
1511     bd2str(remote_bd_addr, &bdstr);
1512
1513     btif_config_remove("Remote", bdstr, "HidAttrMask");
1514     btif_config_remove("Remote", bdstr, "HidSubClass");
1515     btif_config_remove("Remote", bdstr, "HidAppId");
1516     btif_config_remove("Remote", bdstr, "HidVendorId");
1517     btif_config_remove("Remote", bdstr, "HidProductId");
1518     btif_config_remove("Remote", bdstr, "HidVersion");
1519     btif_config_remove("Remote", bdstr, "HidCountryCode");
1520     btif_config_remove("Remote", bdstr, "HidSSRMaxLatency");
1521     btif_config_remove("Remote", bdstr, "HidSSRMinTimeout");
1522     btif_config_remove("Remote", bdstr, "HidDescriptor");
1523     btif_config_save();
1524     return BT_STATUS_SUCCESS;
1525 }
1526
1527 /*******************************************************************************
1528 **
1529 ** Function         btif_storage_read_hl_apps_cb
1530 **
1531 ** Description      BTIF storage API - Read HL application control block from NVRAM
1532 **
1533 ** Returns          BT_STATUS_SUCCESS if the operation was successful,
1534 **                  BT_STATUS_FAIL otherwise
1535 **
1536 *******************************************************************************/
1537 bt_status_t btif_storage_read_hl_apps_cb(char *value, int value_size)
1538 {
1539     bt_status_t bt_status = BT_STATUS_SUCCESS;
1540     int read_size=value_size, read_type=BTIF_CFG_TYPE_BIN;
1541
1542     if (!btif_config_exist("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB))
1543     {
1544         memset(value, 0, value_size);
1545         if (!btif_config_set("Local", BTIF_STORAGE_HL_APP,BTIF_STORAGE_HL_APP_CB,
1546                              value, value_size, BTIF_CFG_TYPE_BIN))
1547         {
1548             bt_status = BT_STATUS_FAIL;
1549         }
1550         else
1551         {
1552             btif_config_save();
1553         }
1554     }
1555     else
1556     {
1557         if (!btif_config_get("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB,
1558                              value, &read_size, &read_type))
1559         {
1560             bt_status = BT_STATUS_FAIL;
1561         }
1562         else
1563         {
1564             if ((read_size != value_size) || (read_type != BTIF_CFG_TYPE_BIN) )
1565             {
1566                 BTIF_TRACE_ERROR("%s  value_size=%d read_size=%d read_type=%d",
1567                                   __FUNCTION__, value_size, read_size, read_type);
1568                 bt_status = BT_STATUS_FAIL;
1569             }
1570         }
1571
1572     }
1573
1574     BTIF_TRACE_DEBUG("%s  status=%d value_size=%d", __FUNCTION__, bt_status, value_size);
1575     return bt_status;
1576 }
1577
1578
1579 /*******************************************************************************
1580 **
1581 ** Function         btif_storage_load_autopair_device_list
1582 **
1583 ** Description      BTIF storage API - Populates auto pair device list
1584 **
1585 ** Returns          BT_STATUS_SUCCESS if the auto pair blacklist is successfully populated
1586 **                  BT_STATUS_FAIL otherwise
1587 **
1588 *******************************************************************************/
1589 bt_status_t btif_storage_load_autopair_device_list() {
1590     // Configuration has already been loaded. No need to reload.
1591     if (btif_config_exist("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, NULL)) {
1592         return BT_STATUS_SUCCESS;
1593     }
1594
1595     static const char *key_names[] = {
1596         BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR,
1597         BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME,
1598         BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST,
1599         BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME,
1600         BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR,
1601     };
1602
1603     config_t *config = config_new(BTIF_AUTO_PAIR_CONF_FILE);
1604     if (!config) {
1605         ALOGE("%s failed to open auto pair blacklist conf file '%s'.", __func__, BTIF_AUTO_PAIR_CONF_FILE);
1606         return BT_STATUS_FAIL;
1607     }
1608
1609     for (size_t i = 0; i < ARRAY_SIZE(key_names); ++i) {
1610         const char *value = config_get_string(config, CONFIG_DEFAULT_SECTION, key_names[i], NULL);
1611         if (value) {
1612             btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, key_names[i], value);
1613         }
1614     }
1615
1616     config_free(config);
1617     return BT_STATUS_SUCCESS;
1618 }
1619
1620 /*******************************************************************************
1621 **
1622 ** Function         btif_storage_is_device_autopair_blacklisted
1623 **
1624 ** Description      BTIF storage API  Checks if the given device is blacklisted for auto pairing
1625 **
1626 ** Returns          TRUE if the device is found in the auto pair blacklist
1627 **                  FALSE otherwise
1628 **
1629 *******************************************************************************/
1630 BOOLEAN  btif_storage_is_device_autopair_blacklisted(bt_bdaddr_t *remote_bd_addr)
1631 {
1632     char *token;
1633     int ret;
1634     bdstr_t bdstr;
1635     char *dev_name_str;
1636     uint8_t i = 0;
1637     char value[BTIF_STORAGE_MAX_LINE_SZ];
1638     int value_size = sizeof(value);
1639
1640     bd2str(remote_bd_addr, &bdstr);
1641
1642     /* Consider only  Lower Address Part from BD Address */
1643     bdstr[8] = '\0';
1644
1645     if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1646                 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR, value, &value_size))
1647     {
1648         if (strcasestr(value,bdstr) != NULL)
1649             return TRUE;
1650     }
1651
1652     dev_name_str = BTM_SecReadDevName((remote_bd_addr->address));
1653
1654     if (dev_name_str != NULL)
1655     {
1656         value_size = sizeof(value);
1657         if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1658                     BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME, value, &value_size))
1659         {
1660             if (strstr(value,dev_name_str) != NULL)
1661                 return TRUE;
1662         }
1663         value_size = sizeof(value);
1664         if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1665                     BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME, value, &value_size))
1666         {
1667             token = strtok(value, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1668             while (token != NULL)
1669             {
1670                 if (strstr(dev_name_str, token) != NULL)
1671                     return TRUE;
1672
1673                 token = strtok(NULL, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1674             }
1675         }
1676     }
1677     if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1678                 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, value, &value_size))
1679     {
1680         if (strstr(value,bdstr) != NULL)
1681             return TRUE;
1682     }
1683     return FALSE;
1684 }
1685
1686 /*******************************************************************************
1687 **
1688 ** Function         btif_storage_add_device_to_autopair_blacklist
1689 **
1690 ** Description      BTIF storage API - Add a remote device to the auto pairing blacklist
1691 **
1692 ** Returns          BT_STATUS_SUCCESS if the device is successfully added to the auto pair blacklist
1693 **                  BT_STATUS_FAIL otherwise
1694 **
1695 *******************************************************************************/
1696 bt_status_t btif_storage_add_device_to_autopair_blacklist(bt_bdaddr_t *remote_bd_addr)
1697 {
1698     int ret;
1699     bdstr_t bdstr;
1700     char linebuf[BTIF_STORAGE_MAX_LINE_SZ+20];
1701     char input_value [20];
1702
1703     bd2str(remote_bd_addr, &bdstr);
1704     strlcpy(input_value, (char*)bdstr, sizeof(input_value));
1705     strlcat(input_value,BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR, sizeof(input_value));
1706
1707     int line_size = sizeof(linebuf);
1708     if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1709                             BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf, &line_size))
1710     {
1711          /* Append this address to the dynamic List of BD address  */
1712         strncat (linebuf, input_value, BTIF_STORAGE_MAX_LINE_SZ);
1713     }
1714     else
1715     {
1716         strncpy( linebuf,input_value, BTIF_STORAGE_MAX_LINE_SZ);
1717     }
1718
1719     /* Write back the key value */
1720     ret = btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1721                         BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf);
1722
1723     return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1724 }
1725
1726 /*******************************************************************************
1727 **
1728 ** Function         btif_storage_is_fixed_pin_zeros_keyboard
1729 **
1730 ** Description      BTIF storage API - checks if this device has fixed PIN key device list
1731 **
1732 ** Returns          TRUE   if the device is found in the fixed pin keyboard device list
1733 **                  FALSE otherwise
1734 **
1735 *******************************************************************************/
1736 BOOLEAN btif_storage_is_fixed_pin_zeros_keyboard(bt_bdaddr_t *remote_bd_addr)
1737 {
1738     int ret;
1739     bdstr_t bdstr;
1740     char *dev_name_str;
1741     uint8_t i = 0;
1742     char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1743
1744     bd2str(remote_bd_addr, &bdstr);
1745
1746     /*consider on LAP part of BDA string*/
1747     bdstr[8] = '\0';
1748
1749     int line_size = sizeof(linebuf);
1750     if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1751                             BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST, linebuf, &line_size))
1752     {
1753         if (strcasestr(linebuf,bdstr) != NULL)
1754             return TRUE;
1755     }
1756     return FALSE;
1757
1758 }
1759
1760 /*******************************************************************************
1761 **
1762 ** Function         btif_storage_set_dmt_support_type
1763 **
1764 ** Description      Sets DMT support status for a remote device
1765 **
1766 ** Returns          BT_STATUS_SUCCESS if config update is successful
1767 **                  BT_STATUS_FAIL otherwise
1768 **
1769 *******************************************************************************/
1770
1771 bt_status_t btif_storage_set_dmt_support_type(const bt_bdaddr_t *remote_bd_addr,
1772                                                    BOOLEAN dmt_supported)
1773 {
1774     int ret;
1775     bdstr_t bdstr = {0};
1776     if(remote_bd_addr)
1777     {
1778         bd2str(remote_bd_addr, &bdstr);
1779     }
1780     else
1781     {
1782         BTIF_TRACE_ERROR("%s  NULL BD Address", __FUNCTION__);
1783         return BT_STATUS_FAIL;
1784     }
1785
1786    ret = btif_config_set_int("Remote", bdstr,"DMTSupported", (int)dmt_supported);
1787    return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1788
1789 }
1790
1791 /*******************************************************************************
1792 **
1793 ** Function         btif_storage_is_dmt_supported_device
1794 **
1795 ** Description      checks if a device supports Dual mode topology
1796 **
1797 ** Returns         TRUE if remote address is valid and supports DMT else FALSE
1798 **
1799 *******************************************************************************/
1800
1801 BOOLEAN btif_storage_is_dmt_supported_device(const bt_bdaddr_t *remote_bd_addr)
1802 {
1803     int    dmt_supported = 0;
1804     bdstr_t bdstr = {0};
1805     if(remote_bd_addr)
1806         bd2str(remote_bd_addr, &bdstr);
1807
1808     if(remote_bd_addr)
1809     {
1810         bd2str(remote_bd_addr, &bdstr);
1811     }
1812     else
1813     {
1814         BTIF_TRACE_ERROR("%s  NULL BD Address", __FUNCTION__);
1815         return FALSE;
1816     }
1817
1818     btif_config_get_int("Remote", bdstr,"DMTSupported", &dmt_supported);
1819
1820     return dmt_supported == 1 ? TRUE:FALSE;
1821 }
1822
1823