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