btif_storage.c revision 8fe58875ce67c6e1099e7ba2339dcd2b979491b0
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************************************************************************************/
156typedef 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************************************************************************************/
165extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
166extern bt_bdaddr_t btif_local_bd_addr;
167
168/************************************************************************************
169**  External functions
170************************************************************************************/
171
172extern void btif_gatts_add_bonded_dev_from_nv(BD_ADDR bda);
173
174/************************************************************************************
175**  Internal Functions
176************************************************************************************/
177
178bt_status_t btif_in_fetch_bonded_ble_device(char *remote_bd_addr,int add,
179                                              btif_bonded_devices_t *p_bonded_devices);
180bt_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*******************************************************************************/
197static 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*******************************************************************************/
226static 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}
247static 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}
339static 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*******************************************************************************/
466static 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
540static 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*******************************************************************************/
589bt_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*******************************************************************************/
681bt_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*******************************************************************************/
699bt_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*******************************************************************************/
715bt_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*******************************************************************************/
733bt_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
776bt_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*******************************************************************************/
801bt_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 = 1;
807    if(btif_config_exist("Remote", bdstr, "LinkKeyType"))
808        ret &= btif_config_remove("Remote", bdstr, "LinkKeyType");
809    if(btif_config_exist("Remote", bdstr, "PinLength"))
810        ret &= btif_config_remove("Remote", bdstr, "PinLength");
811    if(btif_config_exist("Remote", bdstr, "LinkKey"))
812        ret &= btif_config_remove("Remote", bdstr, "LinkKey");
813    /* write bonded info immediately */
814    btif_config_flush();
815    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
816
817}
818
819/*******************************************************************************
820**
821** Function         btif_storage_load_bonded_devices
822**
823** Description      BTIF storage API - Loads all the bonded devices from NVRAM
824**                  and adds to the BTA.
825**                  Additionally, this API also invokes the adaper_properties_cb
826**                  and remote_device_properties_cb for each of the bonded devices.
827**
828** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
829**
830*******************************************************************************/
831bt_status_t btif_storage_load_bonded_devices(void)
832{
833    char *fname;
834    btif_bonded_devices_t bonded_devices;
835    uint32_t i = 0;
836    bt_property_t adapter_props[6];
837    uint32_t num_props = 0;
838    bt_property_t remote_properties[8];
839    bt_bdaddr_t addr;
840    bt_bdname_t name, alias;
841    bt_scan_mode_t mode;
842    uint32_t disc_timeout;
843    bt_bdaddr_t *devices_list;
844    bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
845    bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
846    uint32_t cod, devtype;
847
848    btif_in_fetch_bonded_devices(&bonded_devices, 1);
849
850    /* Now send the adapter_properties_cb with all adapter_properties */
851    {
852        memset(adapter_props, 0, sizeof(adapter_props));
853
854        /* BD_ADDR */
855        BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDADDR, &addr, sizeof(addr),
856                                      adapter_props[num_props]);
857        num_props++;
858
859        /* BD_NAME */
860        BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDNAME, &name, sizeof(name),
861                                      adapter_props[num_props]);
862        num_props++;
863
864        /* SCAN_MODE */
865        /* TODO: At the time of BT on, always report the scan mode as 0 irrespective
866         of the scan_mode during the previous enable cycle.
867         This needs to be re-visited as part of the app/stack enable sequence
868         synchronization */
869        mode = BT_SCAN_MODE_NONE;
870        adapter_props[num_props].type = BT_PROPERTY_ADAPTER_SCAN_MODE;
871        adapter_props[num_props].len = sizeof(mode);
872        adapter_props[num_props].val = &mode;
873        num_props++;
874
875        /* DISC_TIMEOUT */
876        BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
877                                      &disc_timeout, sizeof(disc_timeout),
878                                      adapter_props[num_props]);
879        num_props++;
880
881        /* BONDED_DEVICES */
882        devices_list = (bt_bdaddr_t*)malloc(sizeof(bt_bdaddr_t)*bonded_devices.num_devices);
883        adapter_props[num_props].type = BT_PROPERTY_ADAPTER_BONDED_DEVICES;
884        adapter_props[num_props].len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
885        adapter_props[num_props].val = devices_list;
886        for (i=0; i < bonded_devices.num_devices; i++)
887        {
888            memcpy(devices_list + i, &bonded_devices.devices[i], sizeof(bt_bdaddr_t));
889        }
890        num_props++;
891
892        /* LOCAL UUIDs */
893        BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_UUIDS,
894                                      local_uuids, sizeof(local_uuids),
895                                      adapter_props[num_props]);
896        num_props++;
897
898        btif_adapter_properties_evt(BT_STATUS_SUCCESS, num_props, adapter_props);
899
900        free(devices_list);
901    }
902
903    BTIF_TRACE_EVENT2("%s: %d bonded devices found", __FUNCTION__, bonded_devices.num_devices);
904
905    {
906        for (i = 0; i < bonded_devices.num_devices; i++)
907        {
908            bt_bdaddr_t *p_remote_addr;
909
910            num_props = 0;
911            p_remote_addr = &bonded_devices.devices[i];
912            memset(remote_properties, 0, sizeof(remote_properties));
913            BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_BDNAME,
914                                         &name, sizeof(name),
915                                         remote_properties[num_props]);
916            num_props++;
917
918            BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_REMOTE_FRIENDLY_NAME,
919                                         &alias, sizeof(alias),
920                                         remote_properties[num_props]);
921            num_props++;
922
923            BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_CLASS_OF_DEVICE,
924                                         &cod, sizeof(cod),
925                                         remote_properties[num_props]);
926            num_props++;
927
928            BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_TYPE_OF_DEVICE,
929                                         &devtype, sizeof(devtype),
930                                         remote_properties[num_props]);
931            num_props++;
932
933            BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_UUIDS,
934                                         remote_uuids, sizeof(remote_uuids),
935                                         remote_properties[num_props]);
936            num_props++;
937
938            btif_remote_properties_evt(BT_STATUS_SUCCESS, p_remote_addr,
939                                       num_props, remote_properties);
940        }
941    }
942    return BT_STATUS_SUCCESS;
943}
944
945#if (BLE_INCLUDED == TRUE)
946
947/*******************************************************************************
948**
949** Function         btif_storage_add_ble_bonding_key
950**
951** Description      BTIF storage API - Adds the newly bonded device to NVRAM
952**                  along with the ble-key, Key type and Pin key length
953**
954** Returns          BT_STATUS_SUCCESS if the store was successful,
955**                  BT_STATUS_FAIL otherwise
956**
957*******************************************************************************/
958
959bt_status_t btif_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
960                                           char *key,
961                                           UINT8 key_type,
962                                           UINT8 key_length)
963{
964    bdstr_t bdstr;
965    bd2str(remote_bd_addr, &bdstr);
966    const char* name;
967    switch(key_type)
968    {
969        case BTIF_DM_LE_KEY_PENC:
970            name = "LE_KEY_PENC";
971            break;
972        case BTIF_DM_LE_KEY_PID:
973            name = "LE_KEY_PID";
974            break;
975        case BTIF_DM_LE_KEY_PCSRK:
976            name = "LE_KEY_PCSRK";
977            break;
978        case BTIF_DM_LE_KEY_LENC:
979            name = "LE_KEY_LENC";
980            break;
981        case BTIF_DM_LE_KEY_LCSRK:
982            name = "LE_KEY_LCSRK";
983            break;
984        default:
985            return BT_STATUS_FAIL;
986    }
987    int ret = btif_config_set("Remote", bdstr, name, (const char*)key, (int)key_length, BTIF_CFG_TYPE_BIN);
988    btif_config_save();
989    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
990}
991
992/*******************************************************************************
993**
994** Function         btif_storage_get_ble_bonding_key
995**
996** Description
997**
998** Returns          BT_STATUS_SUCCESS if the fetch was successful,
999**                  BT_STATUS_FAIL otherwise
1000**
1001*******************************************************************************/
1002bt_status_t btif_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
1003                                             UINT8 key_type,
1004                                             char *key_value,
1005                                             int key_length)
1006{
1007    bdstr_t bdstr;
1008    bd2str(remote_bd_addr, &bdstr);
1009    const char* name;
1010    int type = BTIF_CFG_TYPE_BIN;
1011    switch(key_type)
1012    {
1013        case BTIF_DM_LE_KEY_PENC:
1014            name = "LE_KEY_PENC";
1015            break;
1016        case BTIF_DM_LE_KEY_PID:
1017            name = "LE_KEY_PID";
1018            break;
1019        case BTIF_DM_LE_KEY_PCSRK:
1020            name = "LE_KEY_PCSRK";
1021            break;
1022        case BTIF_DM_LE_KEY_LENC:
1023            name = "LE_KEY_LENC";
1024            break;
1025        case BTIF_DM_LE_KEY_LCSRK:
1026            name = "LE_KEY_LCSRK";
1027            break;
1028        default:
1029            return BT_STATUS_FAIL;
1030    }
1031    int ret = btif_config_get("Remote", bdstr, name, key_value, &key_length, &type);
1032    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1033
1034}
1035
1036/*******************************************************************************
1037**
1038** Function         btif_storage_remove_ble_keys
1039**
1040** Description      BTIF storage API - Deletes the bonded device from NVRAM
1041**
1042** Returns          BT_STATUS_SUCCESS if the deletion was successful,
1043**                  BT_STATUS_FAIL otherwise
1044**
1045*******************************************************************************/
1046bt_status_t btif_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr)
1047{
1048    bdstr_t bdstr;
1049    bd2str(remote_bd_addr, &bdstr);
1050    BTIF_TRACE_DEBUG2(" %s in bd addr:%s",__FUNCTION__, bdstr);
1051    int ret = 1;
1052    if(btif_config_exist("Remote", bdstr, "LE_KEY_PENC"))
1053        ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PENC");
1054    if(btif_config_exist("Remote", bdstr, "LE_KEY_PID"))
1055        ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PID");
1056    if(btif_config_exist("Remote", bdstr, "LE_KEY_PCSRK"))
1057        ret &= btif_config_remove("Remote", bdstr, "LE_KEY_PCSRK");
1058    if(btif_config_exist("Remote", bdstr, "LE_KEY_LENC"))
1059        ret &= btif_config_remove("Remote", bdstr, "LE_KEY_LENC");
1060    if(btif_config_exist("Remote", bdstr, "LE_KEY_LCSRK"))
1061        ret &= btif_config_remove("Remote", bdstr, "LE_KEY_LCSRK");
1062    btif_config_save();
1063    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1064}
1065
1066/*******************************************************************************
1067**
1068** Function         btif_storage_add_ble_local_key
1069**
1070** Description      BTIF storage API - Adds the ble key to NVRAM
1071**
1072** Returns          BT_STATUS_SUCCESS if the store was successful,
1073**                  BT_STATUS_FAIL otherwise
1074**
1075*******************************************************************************/
1076bt_status_t btif_storage_add_ble_local_key(char *key,
1077                                           uint8_t key_type,
1078                                           uint8_t key_length)
1079{
1080    const char* name;
1081    switch(key_type)
1082    {
1083        case BTIF_DM_LE_LOCAL_KEY_IR:
1084            name = "LE_LOCAL_KEY_IR";
1085            break;
1086        case BTIF_DM_LE_LOCAL_KEY_IRK:
1087            name = "LE_LOCAL_KEY_IRK";
1088            break;
1089        case BTIF_DM_LE_LOCAL_KEY_DHK:
1090            name = "LE_LOCAL_KEY_DHK";
1091            break;
1092        case BTIF_DM_LE_LOCAL_KEY_ER:
1093            name = "LE_LOCAL_KEY_ER";
1094            break;
1095        default:
1096            return BT_STATUS_FAIL;
1097    }
1098    int ret = btif_config_set("Local", "Adapter", name, (const char*)key, key_length, BTIF_CFG_TYPE_BIN);
1099    btif_config_save();
1100    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1101}
1102
1103/*******************************************************************************
1104**
1105** Function         btif_storage_get_ble_local_key
1106**
1107** Description
1108**
1109** Returns          BT_STATUS_SUCCESS if the fetch was successful,
1110**                  BT_STATUS_FAIL otherwise
1111**
1112*******************************************************************************/
1113bt_status_t btif_storage_get_ble_local_key(UINT8 key_type,
1114                                           char *key_value,
1115                                           int key_length)
1116{
1117    const char* name;
1118    int type = BTIF_CFG_TYPE_BIN;
1119    switch(key_type)
1120    {
1121        case BTIF_DM_LE_LOCAL_KEY_IR:
1122            name = "LE_LOCAL_KEY_IR";
1123            break;
1124        case BTIF_DM_LE_LOCAL_KEY_IRK:
1125            name = "LE_LOCAL_KEY_IRK";
1126            break;
1127        case BTIF_DM_LE_LOCAL_KEY_DHK:
1128            name = "LE_LOCAL_KEY_DHK";
1129            break;
1130        case BTIF_DM_LE_LOCAL_KEY_ER:
1131            name = "LE_LOCAL_KEY_ER";
1132            break;
1133        default:
1134            return BT_STATUS_FAIL;
1135    }
1136    int ret = btif_config_get("Local", "Adapter", name, key_value, &key_length, &type);
1137    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1138
1139}
1140
1141/*******************************************************************************
1142**
1143** Function         btif_storage_remove_ble_local_keys
1144**
1145** Description      BTIF storage API - Deletes the bonded device from NVRAM
1146**
1147** Returns          BT_STATUS_SUCCESS if the deletion was successful,
1148**                  BT_STATUS_FAIL otherwise
1149**
1150*******************************************************************************/
1151bt_status_t btif_storage_remove_ble_local_keys(void)
1152{
1153    int ret = 1;
1154    if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_IR"))
1155        ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_IR");
1156    if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_IRK"))
1157        ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_IRK");
1158    if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_DHK"))
1159        ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_DHK");
1160    if(btif_config_exist("Local", "Adapter", "LE_LOCAL_KEY_ER"))
1161        ret &= btif_config_remove("Local", "Adapter", "LE_LOCAL_KEY_ER");
1162    btif_config_save();
1163    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1164}
1165
1166bt_status_t btif_in_fetch_bonded_ble_device(char *remote_bd_addr,int add, btif_bonded_devices_t *p_bonded_devices)
1167{
1168    int device_type;
1169    int addr_type;
1170    char buf[100];
1171    UINT32 i;
1172    bt_bdaddr_t bd_addr;
1173    BD_ADDR bta_bd_addr;
1174    BOOLEAN is_device_added =FALSE;
1175    tBTA_LE_KEY_VALUE *p;
1176
1177    if(!btif_config_get_int("Remote", remote_bd_addr,"DevType", &device_type))
1178        return BT_STATUS_FAIL;
1179    if(device_type == BT_DEVICE_TYPE_BLE)
1180    {
1181            BTIF_TRACE_DEBUG2("%s %s found a BLE device", __FUNCTION__,remote_bd_addr);
1182            str2bd(remote_bd_addr, &bd_addr);
1183            bdcpy(bta_bd_addr, bd_addr.address);
1184            if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS)
1185            {
1186                return BT_STATUS_FAIL;
1187            }
1188
1189            memset(buf, 0, sizeof(buf));
1190            if (btif_storage_get_ble_bonding_key(&bd_addr,
1191                                                 BTIF_DM_LE_KEY_PENC,
1192                                                 buf,
1193                                                 sizeof(btif_dm_ble_penc_keys_t)) == BT_STATUS_SUCCESS)
1194            {
1195                if(add)
1196                {
1197                    if (!is_device_added)
1198                    {
1199                        BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1200                        is_device_added = TRUE;
1201                    }
1202                    p = (tBTA_LE_KEY_VALUE *)buf;
1203                    for (i=0; i<16; i++)
1204                    {
1205                        BTIF_TRACE_DEBUG2("penc_key.ltk[%d]=0x%02x",i,p->penc_key.ltk[i]);
1206                    }
1207                    for (i=0; i<8; i++)
1208                    {
1209                        BTIF_TRACE_DEBUG2("penc_key.rand[%d]=0x%02x",i,p->penc_key.rand[i]);
1210                    }
1211                    BTIF_TRACE_DEBUG1("p->penc_key.ediv=0x%04x",p->penc_key.ediv);
1212                    BTIF_TRACE_DEBUG1("p->penc_key.sec_level=0x%02x",p->penc_key.sec_level);
1213                    BTIF_TRACE_DEBUG1("p->penc_key.key_size=0x%02x",p->penc_key.key_size);
1214                    BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PENC);
1215                }
1216            }
1217
1218            memset(buf, 0, sizeof(buf));
1219            if (btif_storage_get_ble_bonding_key(&bd_addr,
1220                                                 BTIF_DM_LE_KEY_PID,
1221                                                 buf,
1222                                                 sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1223            {
1224                if(add)
1225                {
1226                    if (!is_device_added)
1227                    {
1228                        BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1229                        is_device_added = TRUE;
1230                    }
1231                    p = (tBTA_LE_KEY_VALUE *)buf;
1232                    for (i=0; i<16; i++)
1233                    {
1234                        BTIF_TRACE_DEBUG2("p->pid_key[%d]=0x%02x",i,p->pid_key.irk[i]);
1235                    }
1236
1237                    BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PID);
1238                }
1239            }
1240
1241            memset(buf, 0, sizeof(buf));
1242            if (btif_storage_get_ble_bonding_key(&bd_addr,
1243                                                 BTIF_DM_LE_KEY_PCSRK,
1244                                                 buf,
1245                                                 sizeof(btif_dm_ble_pcsrk_keys_t)) == BT_STATUS_SUCCESS)
1246            {
1247                if(add)
1248                {
1249                    if (!is_device_added)
1250                    {
1251                        BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1252                        is_device_added = TRUE;
1253                    }
1254
1255                    p = (tBTA_LE_KEY_VALUE *)buf;
1256                    for (i=0; i<16; i++)
1257                    {
1258                        BTIF_TRACE_DEBUG2("p->pcsrk_key.csrk[%d]=0x%02x",i, p->psrk_key.csrk[i]);
1259                    }
1260                    BTIF_TRACE_DEBUG1("p->pcsrk_key.counter=0x%08x",p->psrk_key.counter);
1261                    BTIF_TRACE_DEBUG1("p->pcsrk_key.sec_level=0x%02x",p->psrk_key.sec_level);
1262
1263                    BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_PCSRK);
1264                }
1265            }
1266
1267            memset(buf, 0, sizeof(buf));
1268            if (btif_storage_get_ble_bonding_key(&bd_addr,
1269                                                 BTIF_DM_LE_KEY_LENC,
1270                                                 buf,
1271                                                 sizeof(btif_dm_ble_lenc_keys_t)) == BT_STATUS_SUCCESS)
1272            {
1273                if(add)
1274                {
1275                    if (!is_device_added)
1276                    {
1277                        BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1278                        is_device_added = TRUE;
1279                    }
1280                    p = (tBTA_LE_KEY_VALUE *)buf;
1281                    BTIF_TRACE_DEBUG1("p->lenc_key.div=0x%04x",p->lenc_key.div);
1282                    BTIF_TRACE_DEBUG1("p->lenc_key.key_size=0x%02x",p->lenc_key.key_size);
1283                    BTIF_TRACE_DEBUG1("p->lenc_key.sec_level=0x%02x",p->lenc_key.sec_level);
1284
1285                    BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LENC);
1286                }
1287            }
1288
1289            memset(buf, 0, sizeof(buf));
1290            if (btif_storage_get_ble_bonding_key(&bd_addr,
1291                                                 BTIF_DM_LE_KEY_LCSRK,
1292                                                 buf,
1293                                                 sizeof(btif_dm_ble_lcsrk_keys_t)) == BT_STATUS_SUCCESS)
1294            {
1295                if(add)
1296                {
1297                    if (!is_device_added)
1298                    {
1299                        BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE);
1300                        is_device_added = TRUE;
1301                    }
1302                    p = (tBTA_LE_KEY_VALUE *)buf;
1303                    BTIF_TRACE_DEBUG1("p->lcsrk_key.div=0x%04x",p->lcsrk_key.div);
1304                    BTIF_TRACE_DEBUG1("p->lcsrk_key.counter=0x%08x",p->lcsrk_key.counter);
1305                    BTIF_TRACE_DEBUG1("p->lcsrk_key.sec_level=0x%02x",p->lcsrk_key.sec_level);
1306
1307                    BTA_DmAddBleKey (bta_bd_addr, (tBTA_LE_KEY_VALUE *)buf, BTIF_DM_LE_KEY_LCSRK);
1308                }
1309            }
1310
1311            /* Fill in the bonded devices */
1312            if (is_device_added)
1313            {
1314                memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
1315                btif_gatts_add_bonded_dev_from_nv(bta_bd_addr);
1316            }
1317
1318            return BT_STATUS_SUCCESS;
1319    }
1320    return BT_STATUS_FAIL;
1321}
1322
1323bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1324                                              UINT8 addr_type)
1325{
1326    bdstr_t bdstr;
1327    bd2str(remote_bd_addr, &bdstr);
1328    int ret = btif_config_set_int("Remote", bdstr, "AddrType", (int)addr_type);
1329    btif_config_save();
1330    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1331}
1332
1333/*******************************************************************************
1334**
1335** Function         btif_storage_get_remote_addr_type
1336**
1337** Description      BTIF storage API - Fetches the remote addr type
1338**
1339** Returns          BT_STATUS_SUCCESS if the fetch was successful,
1340**                  BT_STATUS_FAIL otherwise
1341**
1342*******************************************************************************/
1343bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
1344                                              int*addr_type)
1345{
1346    bdstr_t bdstr;
1347    bd2str(remote_bd_addr, &bdstr);
1348    int ret = btif_config_get_int("Remote", bdstr, "AddrType", addr_type);
1349    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1350}
1351#endif
1352/*******************************************************************************
1353**
1354** Function         btif_storage_add_hid_device_info
1355**
1356** Description      BTIF storage API - Adds the hid information of bonded hid devices-to NVRAM
1357**
1358** Returns          BT_STATUS_SUCCESS if the store was successful,
1359**                  BT_STATUS_FAIL otherwise
1360**
1361*******************************************************************************/
1362
1363bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr,
1364                                                    UINT16 attr_mask, UINT8 sub_class,
1365                                                    UINT8 app_id, UINT16 vendor_id,
1366                                                    UINT16 product_id, UINT16 version,
1367                                                    UINT8 ctry_code, UINT16 ssr_max_latency,
1368                                                    UINT16 ssr_min_tout, UINT16 dl_len, UINT8 *dsc_list)
1369{
1370    bdstr_t bdstr;
1371    BTIF_TRACE_DEBUG0("btif_storage_add_hid_device_info:");
1372    bd2str(remote_bd_addr, &bdstr);
1373    btif_config_set_int("Remote", bdstr, "HidAttrMask", attr_mask);
1374    btif_config_set_int("Remote", bdstr, "HidSubClass", sub_class);
1375    btif_config_set_int("Remote", bdstr, "HidAppId", app_id);
1376    btif_config_set_int("Remote", bdstr, "HidVendorId", vendor_id);
1377    btif_config_set_int("Remote", bdstr, "HidProductId", product_id);
1378    btif_config_set_int("Remote", bdstr, "HidVersion", version);
1379    btif_config_set_int("Remote", bdstr, "HidCountryCode", ctry_code);
1380    btif_config_set_int("Remote", bdstr, "HidSSRMaxLatency", ssr_max_latency);
1381    btif_config_set_int("Remote", bdstr, "HidSSRMinTimeout", ssr_min_tout);
1382    if(dl_len > 0)
1383        btif_config_set("Remote", bdstr, "HidDescriptor", (const char*)dsc_list, dl_len,
1384                        BTIF_CFG_TYPE_BIN);
1385    btif_config_save();
1386    return BT_STATUS_SUCCESS;
1387}
1388
1389/*******************************************************************************
1390**
1391** Function         btif_storage_load_bonded_hid_info
1392**
1393** Description      BTIF storage API - Loads hid info for all the bonded devices from NVRAM
1394**                  and adds those devices  to the BTA_HH.
1395**
1396** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
1397**
1398*******************************************************************************/
1399bt_status_t btif_storage_load_bonded_hid_info(void)
1400{
1401    bt_bdaddr_t bd_addr;
1402    tBTA_HH_DEV_DSCP_INFO dscp_info;
1403    uint32_t i;
1404    uint16_t attr_mask;
1405    uint8_t  sub_class;
1406    uint8_t  app_id;
1407
1408    char kname[128], vname[128];
1409    short kpos;
1410    int kname_size;
1411    kname_size = sizeof(kname);
1412    kname[0] = 0;
1413    kpos = 0;
1414    memset(&dscp_info, 0, sizeof(dscp_info));
1415    do
1416    {
1417        kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
1418        BTIF_TRACE_DEBUG2("Remote device:%s, size:%d", kname, kname_size);
1419        int value;
1420        if(btif_config_get_int("Remote", kname, "HidAttrMask", &value))
1421        {
1422            attr_mask = (uint16_t)value;
1423
1424            btif_config_get_int("Remote", kname, "HidSubClass", &value);
1425            sub_class = (uint8_t)value;
1426
1427            btif_config_get_int("Remote", kname, "HidAppId", &value);
1428            app_id = (uint8_t)value;
1429
1430            btif_config_get_int("Remote", kname, "HidVendorId", &value);
1431            dscp_info.vendor_id = (uint16_t) value;
1432
1433            btif_config_get_int("Remote", kname, "HidProductId", &value);
1434            dscp_info.product_id = (uint16_t) value;
1435
1436            btif_config_get_int("Remote", kname, "HidVersion", &value);
1437            dscp_info.version = (uint8_t) value;
1438
1439            btif_config_get_int("Remote", kname, "HidCountryCode", &value);
1440            dscp_info.ctry_code = (uint8_t) value;
1441
1442            value = 0;
1443            btif_config_get_int("Remote", kname, "HidSSRMaxLatency", &value);
1444            dscp_info.ssr_max_latency = (uint16_t) value;
1445
1446            value = 0;
1447            btif_config_get_int("Remote", kname, "HidSSRMinTimeout", &value);
1448            dscp_info.ssr_min_tout = (uint16_t) value;
1449
1450            int len = 0;
1451            int type;
1452            btif_config_get("Remote", kname, "HidDescriptor", NULL, &len, &type);
1453            if(len > 0)
1454            {
1455                dscp_info.descriptor.dl_len = (uint16_t)len;
1456                dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len);
1457                btif_config_get("Remote", kname, "HidDescriptor", (char*)dscp_info.descriptor.dsc_list, &len, &type);
1458            }
1459            str2bd(kname, &bd_addr);
1460            // add extracted information to BTA HH
1461            if (btif_hh_add_added_dev(bd_addr,attr_mask))
1462            {
1463                BTA_HhAddDev(bd_addr.address, attr_mask, sub_class,
1464                        app_id, dscp_info);
1465            }
1466        }
1467    } while(kpos != -1);
1468
1469    return BT_STATUS_SUCCESS;
1470}
1471
1472/*******************************************************************************
1473**
1474** Function         btif_storage_remove_hid_info
1475**
1476** Description      BTIF storage API - Deletes the bonded hid device info from NVRAM
1477**
1478** Returns          BT_STATUS_SUCCESS if the deletion was successful,
1479**                  BT_STATUS_FAIL otherwise
1480**
1481*******************************************************************************/
1482bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr)
1483{
1484    char *fname;
1485    int ret;
1486    bdstr_t bdstr;
1487    bd2str(remote_bd_addr, &bdstr);
1488
1489    btif_config_remove("Remote", bdstr, "HidAttrMask");
1490    btif_config_remove("Remote", bdstr, "HidSubClass");
1491    btif_config_remove("Remote", bdstr, "HidAppId");
1492    btif_config_remove("Remote", bdstr, "HidVendorId");
1493    btif_config_remove("Remote", bdstr, "HidProductId");
1494    btif_config_remove("Remote", bdstr, "HidVersion");
1495    btif_config_remove("Remote", bdstr, "HidCountryCode");
1496    btif_config_remove("Remote", bdstr, "HidSSRMaxLatency");
1497    btif_config_remove("Remote", bdstr, "HidSSRMinTimeout");
1498    btif_config_remove("Remote", bdstr, "HidDescriptor");
1499    btif_config_save();
1500    return BT_STATUS_SUCCESS;
1501}
1502
1503/*******************************************************************************
1504**
1505** Function         btif_storage_read_hl_apps_cb
1506**
1507** Description      BTIF storage API - Read HL application control block from NVRAM
1508**
1509** Returns          BT_STATUS_SUCCESS if the operation was successful,
1510**                  BT_STATUS_FAIL otherwise
1511**
1512*******************************************************************************/
1513bt_status_t btif_storage_read_hl_apps_cb(char *value, int value_size)
1514{
1515    bt_status_t bt_status = BT_STATUS_SUCCESS;
1516    int read_size=value_size, read_type=BTIF_CFG_TYPE_BIN;
1517
1518    if (!btif_config_exist("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB))
1519    {
1520        memset(value, 0, value_size);
1521        if (!btif_config_set("Local", BTIF_STORAGE_HL_APP,BTIF_STORAGE_HL_APP_CB,
1522                             value, value_size, BTIF_CFG_TYPE_BIN))
1523        {
1524            bt_status = BT_STATUS_FAIL;
1525        }
1526        else
1527        {
1528            btif_config_save();
1529        }
1530    }
1531    else
1532    {
1533        if (!btif_config_get("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB,
1534                             value, &read_size, &read_type))
1535        {
1536            bt_status = BT_STATUS_FAIL;
1537        }
1538        else
1539        {
1540            if ((read_size != value_size) || (read_type != BTIF_CFG_TYPE_BIN) )
1541            {
1542                BTIF_TRACE_ERROR4("%s  value_size=%d read_size=%d read_type=%d",
1543                                  __FUNCTION__, value_size, read_size, read_type);
1544                bt_status = BT_STATUS_FAIL;
1545            }
1546        }
1547
1548    }
1549
1550    BTIF_TRACE_DEBUG3("%s  status=%d value_size=%d", __FUNCTION__, bt_status, value_size);
1551    return bt_status;
1552}
1553
1554
1555/*******************************************************************************
1556**
1557** Function         btif_storage_load_autopair_device_list
1558**
1559** Description      BTIF storage API - Populates auto pair device list
1560**
1561** Returns          BT_STATUS_SUCCESS if the auto pair blacklist is successfully populated
1562**                  BT_STATUS_FAIL otherwise
1563**
1564*******************************************************************************/
1565
1566bt_status_t btif_storage_load_autopair_device_list()
1567{
1568    char *key_name, *key_value;
1569    int i=0;
1570    char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1571    char *line;
1572    FILE *fp;
1573
1574    if(!btif_config_exist("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, NULL))
1575    {
1576        /* first time loading of auto pair blacklist configuration  */
1577
1578        fp = fopen (BTIF_AUTO_PAIR_CONF_FILE, "r");
1579
1580        if (fp == NULL)
1581        {
1582            ALOGE("%s: Failed to open auto pair blacklist conf file at %s", __FUNCTION__,BTIF_AUTO_PAIR_CONF_FILE );
1583            return BT_STATUS_FAIL;
1584        }
1585
1586        /* read through auto_pairing.conf file and create the key value pairs specific to  auto pair blacklist devices */
1587        while (fgets(linebuf, BTIF_STORAGE_MAX_LINE_SZ, fp) != NULL)
1588        {
1589            /* trip  leading white spaces */
1590            while (linebuf[i] == BTIF_AUTO_PAIR_CONF_SPACE)
1591                i++;
1592
1593            /* skip  commented lines */
1594            if (linebuf[i] == BTIF_AUTO_PAIR_CONF_COMMENT)
1595                continue;
1596
1597            line = (char*)&(linebuf[i]);
1598
1599            if (line == NULL)
1600                continue;
1601
1602            key_name = strtok(line, BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER);
1603
1604            if (key_name == NULL)
1605                continue;
1606            else if((strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR) == 0) ||
1607                    (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME) ==0) ||
1608                    (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST) ==0 ) ||
1609                    (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME) == 0) ||
1610                    (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR) == 0))
1611            {
1612                key_value = strtok(NULL, BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER);
1613                btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, key_name, key_value);
1614            }
1615        }
1616        fclose(fp);
1617    }
1618    return BT_STATUS_SUCCESS;
1619}
1620
1621/*******************************************************************************
1622**
1623** Function         btif_storage_is_device_autopair_blacklisted
1624**
1625** Description      BTIF storage API  Checks if the given device is blacklisted for auto pairing
1626**
1627** Returns          TRUE if the device is found in the auto pair blacklist
1628**                  FALSE otherwise
1629**
1630*******************************************************************************/
1631BOOLEAN  btif_storage_is_device_autopair_blacklisted(bt_bdaddr_t *remote_bd_addr)
1632{
1633    char *token;
1634    int ret;
1635    bdstr_t bdstr;
1636    char *dev_name_str;
1637    uint8_t i = 0;
1638    char value[BTIF_STORAGE_MAX_LINE_SZ];
1639    int value_size = sizeof(value);
1640
1641    bd2str(remote_bd_addr, &bdstr);
1642
1643    /* Consider only  Lower Address Part from BD Address */
1644    bdstr[8] = '\0';
1645
1646    if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1647                BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR, value, &value_size))
1648    {
1649        if (strcasestr(value,bdstr) != NULL)
1650            return TRUE;
1651    }
1652
1653    dev_name_str = BTM_SecReadDevName((remote_bd_addr->address));
1654
1655    if (dev_name_str != NULL)
1656    {
1657        value_size = sizeof(value);
1658        if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1659                    BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME, value, &value_size))
1660        {
1661            if (strstr(value,dev_name_str) != NULL)
1662                return TRUE;
1663        }
1664        value_size = sizeof(value);
1665        if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1666                    BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME, value, &value_size))
1667        {
1668            token = strtok(value, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1669            while (token != NULL)
1670            {
1671                if (strstr(dev_name_str, token) != NULL)
1672                    return TRUE;
1673
1674                token = strtok(NULL, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1675            }
1676        }
1677    }
1678    if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1679                BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, value, &value_size))
1680    {
1681        if (strstr(value,bdstr) != NULL)
1682            return TRUE;
1683    }
1684    return FALSE;
1685}
1686
1687/*******************************************************************************
1688**
1689** Function         btif_storage_add_device_to_autopair_blacklist
1690**
1691** Description      BTIF storage API - Add a remote device to the auto pairing blacklist
1692**
1693** Returns          BT_STATUS_SUCCESS if the device is successfully added to the auto pair blacklist
1694**                  BT_STATUS_FAIL otherwise
1695**
1696*******************************************************************************/
1697bt_status_t btif_storage_add_device_to_autopair_blacklist(bt_bdaddr_t *remote_bd_addr)
1698{
1699    int ret;
1700    bdstr_t bdstr;
1701    char linebuf[BTIF_STORAGE_MAX_LINE_SZ+20];
1702    char input_value [20];
1703
1704    bd2str(remote_bd_addr, &bdstr);
1705    strlcpy(input_value, (char*)bdstr, sizeof(input_value));
1706    strlcat(input_value,BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR, sizeof(input_value));
1707
1708    int line_size = sizeof(linebuf);
1709    if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1710                            BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf, &line_size))
1711    {
1712         /* Append this address to the dynamic List of BD address  */
1713        strncat (linebuf, input_value, BTIF_STORAGE_MAX_LINE_SZ);
1714    }
1715    else
1716    {
1717        strncpy( linebuf,input_value, BTIF_STORAGE_MAX_LINE_SZ);
1718    }
1719
1720    /* Write back the key value */
1721    ret = btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1722                        BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf);
1723
1724    return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1725}
1726
1727/*******************************************************************************
1728**
1729** Function         btif_storage_is_fixed_pin_zeros_keyboard
1730**
1731** Description      BTIF storage API - checks if this device has fixed PIN key device list
1732**
1733** Returns          TRUE   if the device is found in the fixed pin keyboard device list
1734**                  FALSE otherwise
1735**
1736*******************************************************************************/
1737BOOLEAN btif_storage_is_fixed_pin_zeros_keyboard(bt_bdaddr_t *remote_bd_addr)
1738{
1739    int ret;
1740    bdstr_t bdstr;
1741    char *dev_name_str;
1742    uint8_t i = 0;
1743    char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1744
1745    bd2str(remote_bd_addr, &bdstr);
1746
1747    /*consider on LAP part of BDA string*/
1748    bdstr[8] = '\0';
1749
1750    int line_size = sizeof(linebuf);
1751    if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1752                            BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST, linebuf, &line_size))
1753    {
1754        if (strcasestr(linebuf,bdstr) != NULL)
1755            return TRUE;
1756    }
1757    return FALSE;
1758
1759}
1760
1761/*******************************************************************************
1762**
1763** Function         btif_storage_set_dmt_support_type
1764**
1765** Description      Sets DMT support status for a remote device
1766**
1767** Returns          BT_STATUS_SUCCESS if config update is successful
1768**                  BT_STATUS_FAIL otherwise
1769**
1770*******************************************************************************/
1771
1772bt_status_t btif_storage_set_dmt_support_type(const bt_bdaddr_t *remote_bd_addr,
1773                                                   BOOLEAN dmt_supported)
1774{
1775    int ret;
1776    bdstr_t bdstr = {0};
1777    if(remote_bd_addr)
1778    {
1779        bd2str(remote_bd_addr, &bdstr);
1780    }
1781    else
1782    {
1783        BTIF_TRACE_ERROR1("%s  NULL BD Address", __FUNCTION__);
1784        return BT_STATUS_FAIL;
1785    }
1786
1787   ret = btif_config_set_int("Remote", bdstr,"DMTSupported", (int)dmt_supported);
1788   return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1789
1790}
1791
1792/*******************************************************************************
1793**
1794** Function         btif_storage_is_dmt_supported_device
1795**
1796** Description      checks if a device supports Dual mode topology
1797**
1798** Returns         TRUE if remote address is valid and supports DMT else FALSE
1799**
1800*******************************************************************************/
1801
1802BOOLEAN btif_storage_is_dmt_supported_device(const bt_bdaddr_t *remote_bd_addr)
1803{
1804    int    dmt_supported = 0;
1805    bdstr_t bdstr = {0};
1806    if(remote_bd_addr)
1807        bd2str(remote_bd_addr, &bdstr);
1808
1809    if(remote_bd_addr)
1810    {
1811        bd2str(remote_bd_addr, &bdstr);
1812    }
1813    else
1814    {
1815        BTIF_TRACE_ERROR1("%s  NULL BD Address", __FUNCTION__);
1816        return FALSE;
1817    }
1818
1819    btif_config_get_int("Remote", bdstr,"DMTSupported", &dmt_supported);
1820
1821    return dmt_supported == 1 ? TRUE:FALSE;
1822}
1823
1824
1825