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