OSDN Git Service

am 39277ce1: enhance the "invalid rfc slot id" log to include the caller\'s name
[android-x86/system-bt.git] / btif / src / btif_storage.c
1 /******************************************************************************
2  *
3  *  Copyright (C) 2009-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18
19 /************************************************************************************
20  *
21  *  Filename:      btif_storage.c
22  *
23  *  Description:   Stores the local BT adapter and remote device properties in
24  *                 NVRAM storage, typically as xml file in the
25  *                 mobile's filesystem
26  *
27  *
28  */
29 #include <stdlib.h>
30 #include <time.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <alloca.h>
34
35
36 #include <hardware/bluetooth.h>
37 #include "btif_config.h"
38 #define LOG_TAG "BTIF_STORAGE"
39
40 #include "btif_api.h"
41 #include "btif_storage.h"
42 #include "btif_util.h"
43 #include "bd.h"
44 #include "gki.h"
45 #include "bta_hh_api.h"
46 #include "btif_hh.h"
47
48 #include <cutils/log.h>
49
50 /************************************************************************************
51 **  Constants & Macros
52 ************************************************************************************/
53
54 #define BTIF_STORAGE_PATH_BLUEDROID "/data/misc/bluedroid"
55
56 //#define BTIF_STORAGE_PATH_ADAPTER_INFO "adapter_info"
57 //#define BTIF_STORAGE_PATH_REMOTE_DEVICES "remote_devices"
58 #define BTIF_STORAGE_PATH_REMOTE_DEVTIME "Timestamp"
59 #define BTIF_STORAGE_PATH_REMOTE_DEVCLASS "DevClass"
60 #define BTIF_STORAGE_PATH_REMOTE_DEVTYPE "DevType"
61 #define BTIF_STORAGE_PATH_REMOTE_NAME "Name"
62 #define BTIF_STORAGE_PATH_REMOTE_VER_MFCT "Manufacturer"
63 #define BTIF_STORAGE_PATH_REMOTE_VER_VER "LmpVer"
64 #define BTIF_STORAGE_PATH_REMOTE_VER_SUBVER "LmpSubVer"
65
66 //#define BTIF_STORAGE_PATH_REMOTE_LINKKEYS "remote_linkkeys"
67 #define BTIF_STORAGE_PATH_REMOTE_ALIASE "Aliase"
68 #define BTIF_STORAGE_PATH_REMOTE_SERVICE "Service"
69 #define BTIF_STORAGE_PATH_REMOTE_HIDINFO "HidInfo"
70 #define BTIF_STORAGE_KEY_ADAPTER_NAME "Name"
71 #define BTIF_STORAGE_KEY_ADAPTER_SCANMODE "ScanMode"
72 #define BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT "DiscoveryTimeout"
73
74
75 #define BTIF_AUTO_PAIR_CONF_FILE  "/etc/bluetooth/auto_pair_devlist.conf"
76 #define BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST "AutoPairBlacklist"
77 #define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR "AddressBlacklist"
78 #define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME "ExactNameBlacklist"
79 #define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME "PartialNameBlacklist"
80 #define BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST "FixedPinZerosKeyboardBlacklist"
81 #define BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR "DynamicAddressBlacklist"
82
83 #define BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR ","
84 #define BTIF_AUTO_PAIR_CONF_SPACE ' '
85 #define BTIF_AUTO_PAIR_CONF_COMMENT '#'
86 #define BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER "="
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_make_filename
190 **
191 ** Description      Internal helper function to create NVRAM file path
192 **                  from address and filename
193 **
194 ** Returns          NVRAM file path if successfull, NULL otherwise
195 **
196 *******************************************************************************/
197 static char* btif_in_make_filename(bt_bdaddr_t *bd_addr, char *fname)
198 {
199     static char path[256];
200     bdstr_t bdstr;
201
202     if (fname == NULL)return NULL;
203     if (bd_addr)
204     {
205         sprintf(path, "%s/%s/%s", BTIF_STORAGE_PATH_BLUEDROID,
206                 bd2str(bd_addr, &bdstr), fname);
207     }
208     else
209     {
210         /* local adapter */
211         sprintf(path, "%s/LOCAL/%s", BTIF_STORAGE_PATH_BLUEDROID, fname);
212     }
213
214     return(char*)path;
215 }
216 /*******************************************************************************
217 **
218 ** Function         btif_in_split_uuids_string_to_list
219 **
220 ** Description      Internal helper function to split the string of UUIDs
221 **                  read from the NVRAM to an array
222 **
223 ** Returns          None
224 **
225 *******************************************************************************/
226 static void btif_in_split_uuids_string_to_list(char *str, bt_uuid_t *p_uuid,
227                                                uint32_t *p_num_uuid)
228 {
229     char buf[64];
230     char *p_start = str;
231     char *p_needle;
232     uint32_t num = 0;
233     do
234     {
235         //p_needle = strchr(p_start, ';');
236         p_needle = strchr(p_start, ' ');
237         if (p_needle < p_start) break;
238         memset(buf, 0, sizeof(buf));
239         strncpy(buf, p_start, (p_needle-p_start));
240         string_to_uuid(buf, p_uuid + num);
241         num++;
242         p_start = ++p_needle;
243
244     } while (*p_start != 0);
245     *p_num_uuid = num;
246 }
247 static int prop2cfg(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
248 {
249     bdstr_t bdstr = {0};
250     if(remote_bd_addr)
251         bd2str(remote_bd_addr, &bdstr);
252     BTIF_TRACE_DEBUG3("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
253     char value[1024];
254     if(prop->len <= 0 || prop->len > (int)sizeof(value) - 1)
255     {
256         BTIF_TRACE_ERROR2("property type:%d, len:%d is invalid", prop->type, prop->len);
257         return FALSE;
258     }
259     switch(prop->type)
260     {
261        case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
262             btif_config_set_int("Remote", bdstr,
263                                 BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int)time(NULL));
264             static const char* exclude_filter[] =
265                         {"LinkKey", "LE_KEY_PENC", "LE_KEY_PID", "LE_KEY_PCSRK", "LE_KEY_LENC", "LE_KEY_LCSRK"};
266             btif_config_filter_remove("Remote", exclude_filter, sizeof(exclude_filter)/sizeof(char*),
267                         BTIF_STORAGE_MAX_ALLOWED_REMOTE_DEVICE);
268             break;
269         case BT_PROPERTY_BDNAME:
270             strncpy(value, (char*)prop->val, prop->len);
271             value[prop->len]='\0';
272             if(remote_bd_addr)
273                 btif_config_set_str("Remote", bdstr,
274                                 BTIF_STORAGE_PATH_REMOTE_NAME, value);
275             else btif_config_set_str("Local", "Adapter",
276                                 BTIF_STORAGE_KEY_ADAPTER_NAME, value);
277             break;
278         case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
279             strncpy(value, (char*)prop->val, prop->len);
280             value[prop->len]='\0';
281             btif_config_set_str("Remote", bdstr, BTIF_STORAGE_PATH_REMOTE_ALIASE, value);
282             break;
283         case BT_PROPERTY_ADAPTER_SCAN_MODE:
284             btif_config_set_int("Local", "Adapter",
285                                 BTIF_STORAGE_KEY_ADAPTER_SCANMODE, *(int*)prop->val);
286             break;
287         case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
288             btif_config_set_int("Local", "Adapter",
289                                 BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, *(int*)prop->val);
290             break;
291         case BT_PROPERTY_CLASS_OF_DEVICE:
292             btif_config_set_int("Remote", bdstr,
293                                 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, *(int*)prop->val);
294             break;
295         case BT_PROPERTY_TYPE_OF_DEVICE:
296             btif_config_set_int("Remote", bdstr,
297                                 BTIF_STORAGE_PATH_REMOTE_DEVTYPE, *(int*)prop->val);
298             break;
299         case BT_PROPERTY_UUIDS:
300         {
301             uint32_t i;
302             char buf[64];
303             value[0] = 0;
304             for (i=0; i < (prop->len)/sizeof(bt_uuid_t); i++)
305             {
306                 bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val + i;
307                 memset(buf, 0, sizeof(buf));
308                 uuid_to_string(p_uuid, buf);
309                 strcat(value, buf);
310                 //strcat(value, ";");
311                 strcat(value, " ");
312             }
313             btif_config_set_str("Remote", bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value);
314             btif_config_save();
315             break;
316         }
317         case BT_PROPERTY_REMOTE_VERSION_INFO:
318         {
319             bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
320
321             if (!info)
322                 return FALSE;
323
324             btif_config_set_int("Remote", bdstr,
325                                 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, info->manufacturer);
326             btif_config_set_int("Remote", bdstr,
327                                 BTIF_STORAGE_PATH_REMOTE_VER_VER, info->version);
328             btif_config_set_int("Remote", bdstr,
329                                 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, info->sub_ver);
330             btif_config_save();
331          } break;
332
333         default:
334              BTIF_TRACE_ERROR1("Unknow prop type:%d", prop->type);
335              return FALSE;
336     }
337     return TRUE;
338 }
339 static int cfg2prop(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
340 {
341     bdstr_t bdstr = {0};
342     if(remote_bd_addr)
343         bd2str(remote_bd_addr, &bdstr);
344     BTIF_TRACE_DEBUG3("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
345     if(prop->len <= 0)
346     {
347         BTIF_TRACE_ERROR2("property type:%d, len:%d is invalid", prop->type, prop->len);
348         return FALSE;
349     }
350     int ret = FALSE;
351     switch(prop->type)
352     {
353        case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
354             if(prop->len >= (int)sizeof(int))
355                 ret = btif_config_get_int("Remote", bdstr,
356                                         BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int*)prop->val);
357             break;
358         case BT_PROPERTY_BDNAME:
359         {
360             int len = prop->len;
361             if(remote_bd_addr)
362                 ret = btif_config_get_str("Remote", bdstr,
363                                         BTIF_STORAGE_PATH_REMOTE_NAME, (char*)prop->val, &len);
364             else ret = btif_config_get_str("Local", "Adapter",
365                                         BTIF_STORAGE_KEY_ADAPTER_NAME, (char*)prop->val, &len);
366             if(ret && len && len <= prop->len)
367                 prop->len = len - 1;
368             else
369             {
370                 prop->len = 0;
371                 ret = FALSE;
372             }
373             break;
374         }
375         case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
376         {
377             int len = prop->len;
378             ret = btif_config_get_str("Remote", bdstr,
379                                        BTIF_STORAGE_PATH_REMOTE_ALIASE, (char*)prop->val, &len);
380             if(ret && len && len <= prop->len)
381                 prop->len = len - 1;
382             else
383             {
384                 prop->len = 0;
385                 ret = FALSE;
386             }
387             break;
388         }
389         case BT_PROPERTY_ADAPTER_SCAN_MODE:
390            if(prop->len >= (int)sizeof(int))
391                 ret = btif_config_get_int("Local", "Adapter",
392                                           BTIF_STORAGE_KEY_ADAPTER_SCANMODE, (int*)prop->val);
393            break;
394         case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
395            if(prop->len >= (int)sizeof(int))
396                 ret = btif_config_get_int("Local", "Adapter",
397                                           BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, (int*)prop->val);
398             break;
399         case BT_PROPERTY_CLASS_OF_DEVICE:
400             if(prop->len >= (int)sizeof(int))
401                 ret = btif_config_get_int("Remote", bdstr,
402                                 BTIF_STORAGE_PATH_REMOTE_DEVCLASS, (int*)prop->val);
403             break;
404         case BT_PROPERTY_TYPE_OF_DEVICE:
405             if(prop->len >= (int)sizeof(int))
406                 ret = btif_config_get_int("Remote",
407                                     bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTYPE, (int*)prop->val);
408             break;
409         case BT_PROPERTY_UUIDS:
410         {
411             char value[1280];
412             int size = sizeof(value);
413             if(btif_config_get_str("Remote", bdstr,
414                                     BTIF_STORAGE_PATH_REMOTE_SERVICE, value, &size))
415             {
416                 bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val;
417                 uint32_t num_uuids = 0;
418                 btif_in_split_uuids_string_to_list(value, p_uuid, &num_uuids);
419                 prop->len = num_uuids * sizeof(bt_uuid_t);
420                 ret = TRUE;
421             }
422             else
423             {
424                 prop->val = NULL;
425                 prop->len = 0;
426             }
427         } break;
428
429         case BT_PROPERTY_REMOTE_VERSION_INFO:
430         {
431             bt_remote_version_t *info = (bt_remote_version_t *)prop->val;
432
433             if(prop->len >= (int)sizeof(bt_remote_version_t))
434             {
435                 ret = btif_config_get_int("Remote", bdstr,
436                                 BTIF_STORAGE_PATH_REMOTE_VER_MFCT, &info->manufacturer);
437
438                 if (ret == TRUE)
439                     ret = btif_config_get_int("Remote", bdstr,
440                                 BTIF_STORAGE_PATH_REMOTE_VER_VER, &info->version);
441
442                 if (ret == TRUE)
443                     ret = btif_config_get_int("Remote", bdstr,
444                                 BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, &info->sub_ver);
445             }
446          } break;
447
448         default:
449             BTIF_TRACE_ERROR1("Unknow prop type:%d", prop->type);
450             return FALSE;
451     }
452     return ret;
453 }
454
455
456 /*******************************************************************************
457 **
458 ** Function         btif_in_fetch_bonded_devices
459 **
460 ** Description      Internal helper function to fetch the bonded devices
461 **                  from NVRAM
462 **
463 ** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
464 **
465 *******************************************************************************/
466 static bt_status_t btif_in_fetch_bonded_devices(btif_bonded_devices_t *p_bonded_devices, int add)
467 {
468     BTIF_TRACE_DEBUG1("in add:%d", add);
469     memset(p_bonded_devices, 0, sizeof(btif_bonded_devices_t));
470
471     char kname[128], vname[128];
472     short kpos;
473     int kname_size;
474     kname_size = sizeof(kname);
475     kname[0] = 0;
476     kpos = 0;
477     BOOLEAN bt_linkkey_file_found=FALSE;
478     int device_type;
479
480     do
481     {
482         kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
483         BTIF_TRACE_DEBUG2("Remote device:%s, size:%d", kname, kname_size);
484         int type = BTIF_CFG_TYPE_BIN;
485         LINK_KEY link_key;
486         int size = sizeof(link_key);
487         if(btif_config_get("Remote", kname, "LinkKey", (char*)link_key, &size, &type))
488         {
489             int linkkey_type;
490             if(btif_config_get_int("Remote", kname, "LinkKeyType", &linkkey_type))
491             {
492                 //int pin_len;
493                 //btif_config_get_int("Remote", kname, "PinLength", &pin_len))
494                 bt_bdaddr_t bd_addr;
495                 str2bd(kname, &bd_addr);
496                 if(add)
497                 {
498                     DEV_CLASS dev_class = {0, 0, 0};
499                     int cod;
500                     if(btif_config_get_int("Remote", kname, "DevClass", &cod))
501                         uint2devclass((UINT32)cod, dev_class);
502                     BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0, (UINT8)linkkey_type, 0);
503
504 #if BLE_INCLUDED == TRUE
505                     if (btif_config_get_int("Remote", kname, "DevType", &device_type) &&
506                        (device_type == BT_DEVICE_TYPE_DUMO) )
507                     {
508                         btif_gatts_add_bonded_dev_from_nv(bd_addr.address);
509                     }
510 #endif
511                 }
512                 bt_linkkey_file_found = TRUE;
513                 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
514             }
515             else
516             {
517 #if (BLE_INCLUDED == TRUE)
518                 bt_linkkey_file_found = FALSE;
519 #else
520                 BTIF_TRACE_ERROR1("bounded device:%s, LinkKeyType or PinLength is invalid", kname);
521 #endif
522             }
523         }
524 #if (BLE_INCLUDED == TRUE)
525             if(!(btif_in_fetch_bonded_ble_device(kname,add, p_bonded_devices)) && (!bt_linkkey_file_found))
526             {
527                 BTIF_TRACE_DEBUG1("Remote device:%s, no link key or ble key found", kname);
528             }
529 #else
530             if(!bt_linkkey_file_found)
531                 BTIF_TRACE_DEBUG1("Remote device:%s, no link key", kname);
532 #endif
533         kname_size = sizeof(kname);
534         kname[0] = 0;
535     } while(kpos != -1);
536     BTIF_TRACE_DEBUG0("out");
537     return BT_STATUS_SUCCESS;
538 }
539
540 static int hex_str_to_int(const char* str, int size)
541 {
542     int  n = 0;
543     char c = *str++;
544     while (size-- != 0)
545     {
546         n <<= 4;
547         if (c >= '0' && c <= '9') {
548             n |= c - '0';
549         }
550         else if (c >= 'a' && c <= 'z') {
551             n |= c - 'a' + 10;
552         }
553         else // (c >= 'A' && c <= 'Z')
554         {
555             n |= c - 'A' + 10;
556         }
557
558         c = *str++;
559     }
560     return n;
561 }
562
563 /************************************************************************************
564 **  Externs
565 ************************************************************************************/
566
567 /************************************************************************************
568 **  Functions
569 ************************************************************************************/
570
571 /** functions are synchronous.
572  * functions can be called by both internal modules such as BTIF_DM and by external entiries from HAL via BTIF_context_switch
573  * For OUT parameters,  caller is expected to provide the memory.
574  * Caller is expected to provide a valid pointer to 'property->value' based on the property->type
575  */
576 /*******************************************************************************
577 **
578 ** Function         btif_storage_get_adapter_property
579 **
580 ** Description      BTIF storage API - Fetches the adapter property->type
581 **                  from NVRAM and fills property->val.
582 **                  Caller should provide memory for property->val and
583 **                  set the property->val
584 **
585 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
586 **                  BT_STATUS_FAIL otherwise
587 **
588 *******************************************************************************/
589 bt_status_t btif_storage_get_adapter_property(bt_property_t *property)
590 {
591
592     /* Special handling for adapter BD_ADDR and BONDED_DEVICES */
593     if (property->type == BT_PROPERTY_BDADDR)
594     {
595         BD_ADDR addr;
596         bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)property->val;
597         /* This has been cached in btif. Just fetch it from there */
598         memcpy(bd_addr, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
599         property->len = sizeof(bt_bdaddr_t);
600         return BT_STATUS_SUCCESS;
601     }
602     else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES)
603     {
604         btif_bonded_devices_t bonded_devices;
605
606         btif_in_fetch_bonded_devices(&bonded_devices, 0);
607
608         BTIF_TRACE_DEBUG2("%s: Number of bonded devices: %d Property:BT_PROPERTY_ADAPTER_BONDED_DEVICES", __FUNCTION__, bonded_devices.num_devices);
609
610         if (bonded_devices.num_devices > 0)
611         {
612             property->len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
613             memcpy(property->val, bonded_devices.devices, property->len);
614         }
615
616         /* if there are no bonded_devices, then length shall be 0 */
617         return BT_STATUS_SUCCESS;
618     }
619     else if (property->type == BT_PROPERTY_UUIDS)
620     {
621         /* publish list of local supported services */
622         bt_uuid_t *p_uuid = (bt_uuid_t*)property->val;
623         uint32_t num_uuids = 0;
624         uint32_t i;
625
626         tBTA_SERVICE_MASK service_mask = btif_get_enabled_services_mask();
627         BTIF_TRACE_ERROR2("%s service_mask:0x%x", __FUNCTION__, service_mask);
628         for (i=0; i < BTA_MAX_SERVICE_ID; i++)
629         {
630             /* This should eventually become a function when more services are enabled */
631             if (service_mask
632                 &(tBTA_SERVICE_MASK)(1 << i))
633             {
634                 switch (i)
635                 {
636                     case BTA_HFP_SERVICE_ID:
637                         {
638                             uuid16_to_uuid128(UUID_SERVCLASS_AG_HANDSFREE,
639                                               p_uuid+num_uuids);
640                             num_uuids++;
641                         }
642                     /* intentional fall through: Send both BFP & HSP UUIDs if HFP is enabled */
643                     case BTA_HSP_SERVICE_ID:
644                         {
645                             uuid16_to_uuid128(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,
646                                               p_uuid+num_uuids);
647                             num_uuids++;
648                         }break;
649                     case BTA_A2DP_SERVICE_ID:
650                         {
651                             uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SOURCE,
652                                               p_uuid+num_uuids);
653                             num_uuids++;
654                         }break;
655                 }
656             }
657         }
658         property->len = (num_uuids)*sizeof(bt_uuid_t);
659         return BT_STATUS_SUCCESS;
660     }
661
662     /* fall through for other properties */
663     if(!cfg2prop(NULL, property))
664     {
665         return btif_dm_get_adapter_property(property);
666     }
667     return BT_STATUS_SUCCESS;
668  }
669
670 /*******************************************************************************
671 **
672 ** Function         btif_storage_set_adapter_property
673 **
674 ** Description      BTIF storage API - Stores the adapter property
675 **                  to NVRAM
676 **
677 ** Returns          BT_STATUS_SUCCESS if the store was successful,
678 **                  BT_STATUS_FAIL otherwise
679 **
680 *******************************************************************************/
681 bt_status_t btif_storage_set_adapter_property(bt_property_t *property)
682 {
683     return prop2cfg(NULL, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
684 }
685
686 /*******************************************************************************
687 **
688 ** Function         btif_storage_get_remote_device_property
689 **
690 ** Description      BTIF storage API - Fetches the remote device property->type
691 **                  from NVRAM and fills property->val.
692 **                  Caller should provide memory for property->val and
693 **                  set the property->val
694 **
695 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
696 **                  BT_STATUS_FAIL otherwise
697 **
698 *******************************************************************************/
699 bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t *remote_bd_addr,
700                                                     bt_property_t *property)
701 {
702     return cfg2prop(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
703 }
704 /*******************************************************************************
705 **
706 ** Function         btif_storage_set_remote_device_property
707 **
708 ** Description      BTIF storage API - Stores the remote device property
709 **                  to NVRAM
710 **
711 ** Returns          BT_STATUS_SUCCESS if the store was successful,
712 **                  BT_STATUS_FAIL otherwise
713 **
714 *******************************************************************************/
715 bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t *remote_bd_addr,
716                                                     bt_property_t *property)
717 {
718     return prop2cfg(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
719 }
720
721 /*******************************************************************************
722 **
723 ** Function         btif_storage_add_remote_device
724 **
725 ** Description      BTIF storage API - Adds a newly discovered device to NVRAM
726 **                  along with the timestamp. Also, stores the various
727 **                  properties - RSSI, BDADDR, NAME (if found in EIR)
728 **
729 ** Returns          BT_STATUS_SUCCESS if the store was successful,
730 **                  BT_STATUS_FAIL otherwise
731 **
732 *******************************************************************************/
733 bt_status_t btif_storage_add_remote_device(bt_bdaddr_t *remote_bd_addr,
734                                            uint32_t num_properties,
735                                            bt_property_t *properties)
736 {
737     uint32_t i = 0;
738     /* TODO: If writing a property, fails do we go back undo the earlier
739      * written properties? */
740     for (i=0; i < num_properties; i++)
741     {
742         /* Ignore the RSSI as this is not stored in DB */
743         if (properties[i].type == BT_PROPERTY_REMOTE_RSSI)
744             continue;
745
746         /* BD_ADDR for remote device needs special handling as we also store timestamp */
747         if (properties[i].type == BT_PROPERTY_BDADDR)
748         {
749             bt_property_t addr_prop;
750             memcpy(&addr_prop, &properties[i], sizeof(bt_property_t));
751             addr_prop.type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP;
752             btif_storage_set_remote_device_property(remote_bd_addr,
753                                                     &addr_prop);
754         }
755         else
756         {
757             btif_storage_set_remote_device_property(remote_bd_addr,
758                                                     &properties[i]);
759         }
760     }
761     return BT_STATUS_SUCCESS;
762 }
763
764 /*******************************************************************************
765 **
766 ** Function         btif_storage_add_bonded_device
767 **
768 ** Description      BTIF storage API - Adds the newly bonded device to NVRAM
769 **                  along with the link-key, Key type and Pin key length
770 **
771 ** Returns          BT_STATUS_SUCCESS if the store was successful,
772 **                  BT_STATUS_FAIL otherwise
773 **
774 *******************************************************************************/
775
776 bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
777                                            LINK_KEY link_key,
778                                            uint8_t key_type,
779                                            uint8_t pin_length)
780 {
781     bdstr_t bdstr;
782     bd2str(remote_bd_addr, &bdstr);
783     int ret = btif_config_set_int("Remote", bdstr, "LinkKeyType", (int)key_type);
784     ret &= btif_config_set_int("Remote", bdstr, "PinLength", (int)pin_length);
785     ret &= btif_config_set("Remote", bdstr, "LinkKey", (const char*)link_key, sizeof(LINK_KEY), BTIF_CFG_TYPE_BIN);
786     /* write bonded info immediately */
787     btif_config_flush();
788     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
789 }
790
791 /*******************************************************************************
792 **
793 ** Function         btif_storage_remove_bonded_device
794 **
795 ** Description      BTIF storage API - Deletes the bonded device from NVRAM
796 **
797 ** Returns          BT_STATUS_SUCCESS if the deletion was successful,
798 **                  BT_STATUS_FAIL otherwise
799 **
800 *******************************************************************************/
801 bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
802 {
803     bdstr_t bdstr;
804     bd2str(remote_bd_addr, &bdstr);
805     BTIF_TRACE_DEBUG1("in bd addr:%s", bdstr);
806     int ret = btif_config_remove("Remote", bdstr, "LinkKeyType");
807     ret &= btif_config_remove("Remote", bdstr, "PinLength");
808     ret &= btif_config_remove("Remote", bdstr, "LinkKey");
809     /* write bonded info immediately */
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     tBTA_LE_KEY_VALUE *p;
1172
1173     if(!btif_config_get_int("Remote", remote_bd_addr,"DevType", &device_type))
1174         return BT_STATUS_FAIL;
1175     if(device_type == BT_DEVICE_TYPE_BLE)
1176     {
1177             BTIF_TRACE_DEBUG2("%s %s found a BLE device", __FUNCTION__,remote_bd_addr);
1178             str2bd(remote_bd_addr, &bd_addr);
1179             bdcpy(bta_bd_addr, bd_addr.address);
1180             if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS)
1181             {
1182                 return BT_STATUS_FAIL;
1183             }
1184
1185             memset(buf, 0, sizeof(buf));
1186             if (btif_storage_get_ble_bonding_key(&bd_addr,
1187                                                  BTIF_DM_LE_KEY_PENC,
1188                                                  buf,
1189                                                  sizeof(btif_dm_ble_penc_keys_t)) == BT_STATUS_SUCCESS)
1190             {
1191                 if(add)
1192                 {
1193                     if (!is_device_added)
1194                     {
1195                         BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1196                         is_device_added = TRUE;
1197                     }
1198                     p = (tBTA_LE_KEY_VALUE *)buf;
1199                     for (i=0; i<16; i++)
1200                     {
1201                         BTIF_TRACE_DEBUG2("penc_key.ltk[%d]=0x%02x",i,p->penc_key.ltk[i]);
1202                     }
1203                     for (i=0; i<8; i++)
1204                     {
1205                         BTIF_TRACE_DEBUG2("penc_key.rand[%d]=0x%02x",i,p->penc_key.rand[i]);
1206                     }
1207                     BTIF_TRACE_DEBUG1("p->penc_key.ediv=0x%04x",p->penc_key.ediv);
1208                     BTIF_TRACE_DEBUG1("p->penc_key.sec_level=0x%02x",p->penc_key.sec_level);
1209                     BTIF_TRACE_DEBUG1("p->penc_key.key_size=0x%02x",p->penc_key.key_size);
1210                     BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PENC);
1211                 }
1212             }
1213
1214             memset(buf, 0, sizeof(buf));
1215             if (btif_storage_get_ble_bonding_key(&bd_addr,
1216                                                  BTIF_DM_LE_KEY_PID,
1217                                                  buf,
1218                                                  sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1219             {
1220                 if(add)
1221                 {
1222                     if (!is_device_added)
1223                     {
1224                         BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1225                         is_device_added = TRUE;
1226                     }
1227                     p = (tBTA_LE_KEY_VALUE *)buf;
1228                     for (i=0; i<16; i++)
1229                     {
1230                         BTIF_TRACE_DEBUG2("p->pid_key[%d]=0x%02x",i,p->pid_key.irk[i]);
1231                     }
1232
1233                     BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PID);
1234                 }
1235             }
1236
1237             memset(buf, 0, sizeof(buf));
1238             if (btif_storage_get_ble_bonding_key(&bd_addr,
1239                                                  BTIF_DM_LE_KEY_PCSRK,
1240                                                  buf,
1241                                                  sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1242             {
1243                 if(add)
1244                 {
1245                     if (!is_device_added)
1246                     {
1247                         BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1248                         is_device_added = TRUE;
1249                     }
1250
1251                     p = (tBTA_LE_KEY_VALUE *)buf;
1252                     for (i=0; i<16; i++)
1253                     {
1254                         BTIF_TRACE_DEBUG2("p->pcsrk_key.csrk[%d]=0x%02x",i, p->psrk_key.csrk[i]);
1255                     }
1256                     BTIF_TRACE_DEBUG1("p->pcsrk_key.counter=0x%08x",p->psrk_key.counter);
1257                     BTIF_TRACE_DEBUG1("p->pcsrk_key.sec_level=0x%02x",p->psrk_key.sec_level);
1258
1259                     BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PCSRK);
1260                 }
1261             }
1262
1263             memset(buf, 0, sizeof(buf));
1264             if (btif_storage_get_ble_bonding_key(&bd_addr,
1265                                                  BTIF_DM_LE_KEY_LENC,
1266                                                  buf,
1267                                                  sizeof(btif_dm_ble_lenc_keys_t)) == BT_STATUS_SUCCESS)
1268             {
1269                 if(add)
1270                 {
1271                     if (!is_device_added)
1272                     {
1273                         BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1274                         is_device_added = TRUE;
1275                     }
1276                     p = (tBTA_LE_KEY_VALUE *)buf;
1277                     BTIF_TRACE_DEBUG1("p->lenc_key.div=0x%04x",p->lenc_key.div);
1278                     BTIF_TRACE_DEBUG1("p->lenc_key.key_size=0x%02x",p->lenc_key.key_size);
1279                     BTIF_TRACE_DEBUG1("p->lenc_key.sec_level=0x%02x",p->lenc_key.sec_level);
1280
1281                     BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LENC);
1282                 }
1283             }
1284
1285             memset(buf, 0, sizeof(buf));
1286             if (btif_storage_get_ble_bonding_key(&bd_addr,
1287                                                  BTIF_DM_LE_KEY_LCSRK,
1288                                                  buf,
1289                                                  sizeof(btif_dm_ble_lcsrk_keys_t)) == BT_STATUS_SUCCESS)
1290             {
1291                 if(add)
1292                 {
1293                     if (!is_device_added)
1294                     {
1295                         BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1296                         is_device_added = TRUE;
1297                     }
1298                     p = (tBTA_LE_KEY_VALUE *)buf;
1299                     BTIF_TRACE_DEBUG1("p->lcsrk_key.div=0x%04x",p->lcsrk_key.div);
1300                     BTIF_TRACE_DEBUG1("p->lcsrk_key.counter=0x%08x",p->lcsrk_key.counter);
1301                     BTIF_TRACE_DEBUG1("p->lcsrk_key.sec_level=0x%02x",p->lcsrk_key.sec_level);
1302
1303                     BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LCSRK);
1304                 }
1305             }
1306
1307             /* Fill in the bonded devices */
1308             if (is_device_added)
1309             {
1310                 memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
1311                 btif_gatts_add_bonded_dev_from_nv(bta_bd_addr);
1312             }
1313
1314             return BT_STATUS_SUCCESS;
1315     }
1316     return BT_STATUS_FAIL;
1317 }
1318
1319 bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1320                                               UINT8 addr_type)
1321 {
1322     bdstr_t bdstr;
1323     bd2str(remote_bd_addr, &bdstr);
1324     int ret = btif_config_set_int("Remote", bdstr, "AddrType", (int)addr_type);
1325     btif_config_save();
1326     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1327 }
1328
1329 /*******************************************************************************
1330 **
1331 ** Function         btif_storage_get_remote_addr_type
1332 **
1333 ** Description      BTIF storage API - Fetches the remote addr type
1334 **
1335 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
1336 **                  BT_STATUS_FAIL otherwise
1337 **
1338 *******************************************************************************/
1339 bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1340                                               int*addr_type)
1341 {
1342     bdstr_t bdstr;
1343     bd2str(remote_bd_addr, &bdstr);
1344     int ret = btif_config_get_int("Remote", bdstr, "AddrType", addr_type);
1345     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1346 }
1347 #endif
1348 /*******************************************************************************
1349 **
1350 ** Function         btif_storage_add_hid_device_info
1351 **
1352 ** Description      BTIF storage API - Adds the hid information of bonded hid devices-to NVRAM
1353 **
1354 ** Returns          BT_STATUS_SUCCESS if the store was successful,
1355 **                  BT_STATUS_FAIL otherwise
1356 **
1357 *******************************************************************************/
1358
1359 bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr,
1360                                                     UINT16 attr_mask, UINT8 sub_class,
1361                                                     UINT8 app_id, UINT16 vendor_id,
1362                                                     UINT16 product_id, UINT16 version,
1363                                                     UINT8 ctry_code, UINT16 ssr_max_latency,
1364                                                     UINT16 ssr_min_tout, UINT16 dl_len, UINT8 *dsc_list)
1365 {
1366     bdstr_t bdstr;
1367     BTIF_TRACE_DEBUG0("btif_storage_add_hid_device_info:");
1368     bd2str(remote_bd_addr, &bdstr);
1369     btif_config_set_int("Remote", bdstr, "HidAttrMask", attr_mask);
1370     btif_config_set_int("Remote", bdstr, "HidSubClass", sub_class);
1371     btif_config_set_int("Remote", bdstr, "HidAppId", app_id);
1372     btif_config_set_int("Remote", bdstr, "HidVendorId", vendor_id);
1373     btif_config_set_int("Remote", bdstr, "HidProductId", product_id);
1374     btif_config_set_int("Remote", bdstr, "HidVersion", version);
1375     btif_config_set_int("Remote", bdstr, "HidCountryCode", ctry_code);
1376     btif_config_set_int("Remote", bdstr, "HidSSRMaxLatency", ssr_max_latency);
1377     btif_config_set_int("Remote", bdstr, "HidSSRMinTimeout", ssr_min_tout);
1378     if(dl_len > 0)
1379         btif_config_set("Remote", bdstr, "HidDescriptor", (const char*)dsc_list, dl_len,
1380                         BTIF_CFG_TYPE_BIN);
1381     btif_config_save();
1382     return BT_STATUS_SUCCESS;
1383 }
1384
1385 /*******************************************************************************
1386 **
1387 ** Function         btif_storage_load_bonded_hid_info
1388 **
1389 ** Description      BTIF storage API - Loads hid info for all the bonded devices from NVRAM
1390 **                  and adds those devices  to the BTA_HH.
1391 **
1392 ** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
1393 **
1394 *******************************************************************************/
1395 bt_status_t btif_storage_load_bonded_hid_info(void)
1396 {
1397     bt_bdaddr_t bd_addr;
1398     tBTA_HH_DEV_DSCP_INFO dscp_info;
1399     uint32_t i;
1400     uint16_t attr_mask;
1401     uint8_t  sub_class;
1402     uint8_t  app_id;
1403
1404     char kname[128], vname[128];
1405     short kpos;
1406     int kname_size;
1407     kname_size = sizeof(kname);
1408     kname[0] = 0;
1409     kpos = 0;
1410     memset(&dscp_info, 0, sizeof(dscp_info));
1411     do
1412     {
1413         kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
1414         BTIF_TRACE_DEBUG2("Remote device:%s, size:%d", kname, kname_size);
1415         int value;
1416         if(btif_config_get_int("Remote", kname, "HidAttrMask", &value))
1417         {
1418             attr_mask = (uint16_t)value;
1419
1420             btif_config_get_int("Remote", kname, "HidSubClass", &value);
1421             sub_class = (uint8_t)value;
1422
1423             btif_config_get_int("Remote", kname, "HidAppId", &value);
1424             app_id = (uint8_t)value;
1425
1426             btif_config_get_int("Remote", kname, "HidVendorId", &value);
1427             dscp_info.vendor_id = (uint16_t) value;
1428
1429             btif_config_get_int("Remote", kname, "HidProductId", &value);
1430             dscp_info.product_id = (uint16_t) value;
1431
1432             btif_config_get_int("Remote", kname, "HidVersion", &value);
1433             dscp_info.version = (uint8_t) value;
1434
1435             btif_config_get_int("Remote", kname, "HidCountryCode", &value);
1436             dscp_info.ctry_code = (uint8_t) value;
1437
1438             value = 0;
1439             btif_config_get_int("Remote", kname, "HidSSRMaxLatency", &value);
1440             dscp_info.ssr_max_latency = (uint16_t) value;
1441
1442             value = 0;
1443             btif_config_get_int("Remote", kname, "HidSSRMinTimeout", &value);
1444             dscp_info.ssr_min_tout = (uint16_t) value;
1445
1446             int len = 0;
1447             int type;
1448             btif_config_get("Remote", kname, "HidDescriptor", NULL, &len, &type);
1449             if(len > 0)
1450             {
1451                 dscp_info.descriptor.dl_len = (uint16_t)len;
1452                 dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len);
1453                 btif_config_get("Remote", kname, "HidDescriptor", (char*)dscp_info.descriptor.dsc_list, &len, &type);
1454             }
1455             str2bd(kname, &bd_addr);
1456             // add extracted information to BTA HH
1457             if (btif_hh_add_added_dev(bd_addr,attr_mask))
1458             {
1459                 BTA_HhAddDev(bd_addr.address, attr_mask, sub_class,
1460                         app_id, dscp_info);
1461             }
1462         }
1463     } while(kpos != -1);
1464
1465     return BT_STATUS_SUCCESS;
1466 }
1467
1468 /*******************************************************************************
1469 **
1470 ** Function         btif_storage_remove_hid_info
1471 **
1472 ** Description      BTIF storage API - Deletes the bonded hid device info from NVRAM
1473 **
1474 ** Returns          BT_STATUS_SUCCESS if the deletion was successful,
1475 **                  BT_STATUS_FAIL otherwise
1476 **
1477 *******************************************************************************/
1478 bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr)
1479 {
1480     char *fname;
1481     int ret;
1482     bdstr_t bdstr;
1483     bd2str(remote_bd_addr, &bdstr);
1484
1485     btif_config_remove("Remote", bdstr, "HidAttrMask");
1486     btif_config_remove("Remote", bdstr, "HidSubClass");
1487     btif_config_remove("Remote", bdstr, "HidAppId");
1488     btif_config_remove("Remote", bdstr, "HidVendorId");
1489     btif_config_remove("Remote", bdstr, "HidProductId");
1490     btif_config_remove("Remote", bdstr, "HidVersion");
1491     btif_config_remove("Remote", bdstr, "HidCountryCode");
1492     btif_config_remove("Remote", bdstr, "HidSSRMaxLatency");
1493     btif_config_remove("Remote", bdstr, "HidSSRMinTimeout");
1494     btif_config_remove("Remote", bdstr, "HidDescriptor");
1495     btif_config_save();
1496     return BT_STATUS_SUCCESS;
1497 }
1498
1499 /*******************************************************************************
1500 **
1501 ** Function         btif_storage_read_hl_apps_cb
1502 **
1503 ** Description      BTIF storage API - Read HL application control block from NVRAM
1504 **
1505 ** Returns          BT_STATUS_SUCCESS if the operation was successful,
1506 **                  BT_STATUS_FAIL otherwise
1507 **
1508 *******************************************************************************/
1509 bt_status_t btif_storage_read_hl_apps_cb(char *value, int value_size)
1510 {
1511     bt_status_t bt_status = BT_STATUS_SUCCESS;
1512     int read_size=value_size, read_type=BTIF_CFG_TYPE_BIN;
1513
1514     if (!btif_config_exist("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB))
1515     {
1516         memset(value, 0, value_size);
1517         if (!btif_config_set("Local", BTIF_STORAGE_HL_APP,BTIF_STORAGE_HL_APP_CB,
1518                              value, value_size, BTIF_CFG_TYPE_BIN))
1519         {
1520             bt_status = BT_STATUS_FAIL;
1521         }
1522         else
1523         {
1524             btif_config_save();
1525         }
1526     }
1527     else
1528     {
1529         if (!btif_config_get("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB,
1530                              value, &read_size, &read_type))
1531         {
1532             bt_status = BT_STATUS_FAIL;
1533         }
1534         else
1535         {
1536             if ((read_size != value_size) || (read_type != BTIF_CFG_TYPE_BIN) )
1537             {
1538                 BTIF_TRACE_ERROR4("%s  value_size=%d read_size=%d read_type=%d",
1539                                   __FUNCTION__, value_size, read_size, read_type);
1540                 bt_status = BT_STATUS_FAIL;
1541             }
1542         }
1543
1544     }
1545
1546     BTIF_TRACE_DEBUG3("%s  status=%d value_size=%d", __FUNCTION__, bt_status, value_size);
1547     return bt_status;
1548 }
1549
1550
1551 /*******************************************************************************
1552 **
1553 ** Function         btif_storage_load_autopair_device_list
1554 **
1555 ** Description      BTIF storage API - Populates auto pair device list
1556 **
1557 ** Returns          BT_STATUS_SUCCESS if the auto pair blacklist is successfully populated
1558 **                  BT_STATUS_FAIL otherwise
1559 **
1560 *******************************************************************************/
1561
1562 bt_status_t btif_storage_load_autopair_device_list()
1563 {
1564     char *key_name, *key_value;
1565     int i=0;
1566     char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1567     char *line;
1568     FILE *fp;
1569
1570     if(!btif_config_exist("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, NULL))
1571     {
1572         /* first time loading of auto pair blacklist configuration  */
1573
1574         fp = fopen (BTIF_AUTO_PAIR_CONF_FILE, "r");
1575
1576         if (fp == NULL)
1577         {
1578             ALOGE("%s: Failed to open auto pair blacklist conf file at %s", __FUNCTION__,BTIF_AUTO_PAIR_CONF_FILE );
1579             return BT_STATUS_FAIL;
1580         }
1581
1582         /* read through auto_pairing.conf file and create the key value pairs specific to  auto pair blacklist devices */
1583         while (fgets(linebuf, BTIF_STORAGE_MAX_LINE_SZ, fp) != NULL)
1584         {
1585             /* trip  leading white spaces */
1586             while (linebuf[i] == BTIF_AUTO_PAIR_CONF_SPACE)
1587                 i++;
1588
1589             /* skip  commented lines */
1590             if (linebuf[i] == BTIF_AUTO_PAIR_CONF_COMMENT)
1591                 continue;
1592
1593             line = (char*)&(linebuf[i]);
1594
1595             if (line == NULL)
1596                 continue;
1597
1598             key_name = strtok(line, BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER);
1599
1600             if (key_name == NULL)
1601                 continue;
1602             else if((strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR) == 0) ||
1603                     (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME) ==0) ||
1604                     (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST) ==0 ) ||
1605                     (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME) == 0) ||
1606                     (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR) == 0))
1607             {
1608                 key_value = strtok(NULL, BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER);
1609                 btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, key_name, key_value);
1610             }
1611         }
1612         fclose(fp);
1613     }
1614     return BT_STATUS_SUCCESS;
1615 }
1616
1617 /*******************************************************************************
1618 **
1619 ** Function         btif_storage_is_device_autopair_blacklisted
1620 **
1621 ** Description      BTIF storage API  Checks if the given device is blacklisted for auto pairing
1622 **
1623 ** Returns          TRUE if the device is found in the auto pair blacklist
1624 **                  FALSE otherwise
1625 **
1626 *******************************************************************************/
1627 BOOLEAN  btif_storage_is_device_autopair_blacklisted(bt_bdaddr_t *remote_bd_addr)
1628 {
1629     char *token;
1630     int ret;
1631     bdstr_t bdstr;
1632     char *dev_name_str;
1633     uint8_t i = 0;
1634     char value[BTIF_STORAGE_MAX_LINE_SZ];
1635     int value_size = sizeof(value);
1636
1637     bd2str(remote_bd_addr, &bdstr);
1638
1639     /* Consider only  Lower Address Part from BD Address */
1640     bdstr[8] = '\0';
1641
1642     if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1643                 BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR, value, &value_size))
1644     {
1645         if (strcasestr(value,bdstr) != NULL)
1646             return TRUE;
1647     }
1648
1649     dev_name_str = BTM_SecReadDevName((remote_bd_addr->address));
1650
1651     if (dev_name_str != NULL)
1652     {
1653         value_size = sizeof(value);
1654         if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1655                     BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME, value, &value_size))
1656         {
1657             if (strstr(value,dev_name_str) != NULL)
1658                 return TRUE;
1659         }
1660         value_size = sizeof(value);
1661         if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1662                     BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME, value, &value_size))
1663         {
1664             token = strtok(value, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1665             while (token != NULL)
1666             {
1667                 if (strstr(dev_name_str, token) != NULL)
1668                     return TRUE;
1669
1670                 token = strtok(NULL, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1671             }
1672         }
1673     }
1674     if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1675                 BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, value, &value_size))
1676     {
1677         if (strstr(value,bdstr) != NULL)
1678             return TRUE;
1679     }
1680     return FALSE;
1681 }
1682
1683 /*******************************************************************************
1684 **
1685 ** Function         btif_storage_add_device_to_autopair_blacklist
1686 **
1687 ** Description      BTIF storage API - Add a remote device to the auto pairing blacklist
1688 **
1689 ** Returns          BT_STATUS_SUCCESS if the device is successfully added to the auto pair blacklist
1690 **                  BT_STATUS_FAIL otherwise
1691 **
1692 *******************************************************************************/
1693 bt_status_t btif_storage_add_device_to_autopair_blacklist(bt_bdaddr_t *remote_bd_addr)
1694 {
1695     int ret;
1696     bdstr_t bdstr;
1697     char linebuf[BTIF_STORAGE_MAX_LINE_SZ+20];
1698     char input_value [20];
1699
1700     bd2str(remote_bd_addr, &bdstr);
1701     strlcpy(input_value, (char*)bdstr, sizeof(input_value));
1702     strlcat(input_value,BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR, sizeof(input_value));
1703
1704     int line_size = sizeof(linebuf);
1705     if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1706                             BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf, &line_size))
1707     {
1708          /* Append this address to the dynamic List of BD address  */
1709         strncat (linebuf, input_value, BTIF_STORAGE_MAX_LINE_SZ);
1710     }
1711     else
1712     {
1713         strncpy( linebuf,input_value, BTIF_STORAGE_MAX_LINE_SZ);
1714     }
1715
1716     /* Write back the key value */
1717     ret = btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1718                         BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf);
1719
1720     return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1721 }
1722
1723 /*******************************************************************************
1724 **
1725 ** Function         btif_storage_is_fixed_pin_zeros_keyboard
1726 **
1727 ** Description      BTIF storage API - checks if this device has fixed PIN key device list
1728 **
1729 ** Returns          TRUE   if the device is found in the fixed pin keyboard device list
1730 **                  FALSE otherwise
1731 **
1732 *******************************************************************************/
1733 BOOLEAN btif_storage_is_fixed_pin_zeros_keyboard(bt_bdaddr_t *remote_bd_addr)
1734 {
1735     int ret;
1736     bdstr_t bdstr;
1737     char *dev_name_str;
1738     uint8_t i = 0;
1739     char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1740
1741     bd2str(remote_bd_addr, &bdstr);
1742
1743         /*consider on LAP part of BDA string*/
1744         bdstr[8] = '\0';
1745
1746     int line_size = sizeof(linebuf);
1747     if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1748                             BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST, linebuf, &line_size))
1749     {
1750         if (strcasestr(linebuf,bdstr) != NULL)
1751             return TRUE;
1752     }
1753     return FALSE;
1754
1755 }