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