btif_storage.c revision 5738f83aeb59361a0a2eda2460113f6dc9194271
1/******************************************************************************
2 *
3 *  Copyright (C) 2009-2012 Broadcom Corporation
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at:
8 *
9 *  http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *
17 ******************************************************************************/
18
19/************************************************************************************
20 *
21 *  Filename:      btif_storage.c
22 *
23 *  Description:   Stores the local BT adapter and remote device properties in
24 *                 NVRAM storage, typically as xml file in the
25 *                 mobile's filesystem
26 *
27 *
28 */
29#include <stdlib.h>
30#include <time.h>
31#include <string.h>
32#include <ctype.h>
33#include <alloca.h>
34
35
36#include <hardware/bluetooth.h>
37#include "btif_config.h"
38#define LOG_TAG "BTIF_STORAGE"
39
40#include "btif_api.h"
41
42#include "btif_util.h"
43#include "bd.h"
44#include "gki.h"
45#include "bta_hh_api.h"
46#include "btif_hh.h"
47
48#include <cutils/log.h>
49#define info(fmt, ...)  ALOGI ("%s(L%d): " fmt,__FUNCTION__, __LINE__,  ## __VA_ARGS__)
50#define debug(fmt, ...) ALOGD ("%s(L%d): " fmt,__FUNCTION__, __LINE__,  ## __VA_ARGS__)
51#define warn(fmt, ...) ALOGW ("## WARNING : %s(L%d): " fmt "##",__FUNCTION__, __LINE__, ## __VA_ARGS__)
52#define error(fmt, ...) ALOGE ("## ERROR : %s(L%d): " fmt "##",__FUNCTION__, __LINE__, ## __VA_ARGS__)
53#define asrt(s) if(!(s)) ALOGE ("## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__)
54
55/************************************************************************************
56**  Constants & Macros
57************************************************************************************/
58
59#define BTIF_STORAGE_PATH_BLUEDROID "/data/misc/bluedroid"
60
61//#define BTIF_STORAGE_PATH_ADAPTER_INFO "adapter_info"
62//#define BTIF_STORAGE_PATH_REMOTE_DEVICES "remote_devices"
63#define BTIF_STORAGE_PATH_REMOTE_DEVTIME "Timestamp"
64#define BTIF_STORAGE_PATH_REMOTE_DEVCLASS "DevClass"
65#define BTIF_STORAGE_PATH_REMOTE_DEVTYPE "DevType"
66#define BTIF_STORAGE_PATH_REMOTE_NAME "Name"
67//#define BTIF_STORAGE_PATH_REMOTE_LINKKEYS "remote_linkkeys"
68#define BTIF_STORAGE_PATH_REMOTE_ALIASE "Aliase"
69#define BTIF_STORAGE_PATH_REMOTE_SERVICE "Service"
70#define BTIF_STORAGE_PATH_REMOTE_HIDINFO "HidInfo"
71#define BTIF_STORAGE_KEY_ADAPTER_NAME "Name"
72#define BTIF_STORAGE_KEY_ADAPTER_SCANMODE "ScanMode"
73#define BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT "DiscoveryTimeout"
74
75
76#define BTIF_AUTO_PAIR_CONF_FILE  "/etc/bluetooth/auto_pair_devlist.conf"
77#define BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST "AutoPairBlacklist"
78#define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR "AddressBlacklist"
79#define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME "ExactNameBlacklist"
80#define BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME "PartialNameBlacklist"
81#define BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST "FixedPinZerosKeyboardBlacklist"
82#define BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR "DynamicAddressBlacklist"
83
84#define BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR ","
85#define BTIF_AUTO_PAIR_CONF_SPACE ' '
86#define BTIF_AUTO_PAIR_CONF_COMMENT '#'
87#define BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER "="
88
89
90/* This is a local property to add a device found */
91#define BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP 0xFF
92
93#define BTIF_STORAGE_GET_ADAPTER_PROP(t,v,l,p) \
94      {p.type=t;p.val=v;p.len=l; btif_storage_get_adapter_property(&p);}
95
96#define BTIF_STORAGE_GET_REMOTE_PROP(b,t,v,l,p) \
97      {p.type=t;p.val=v;p.len=l;btif_storage_get_remote_device_property(b,&p);}
98
99#define STORAGE_BDADDR_STRING_SZ           (18)      /* 00:11:22:33:44:55 */
100#define STORAGE_UUID_STRING_SIZE           (36+1)    /* 00001200-0000-1000-8000-00805f9b34fb; */
101#define STORAGE_PINLEN_STRING_MAX_SIZE     (2)       /* ascii pinlen max chars */
102#define STORAGE_KEYTYPE_STRING_MAX_SIZE    (1)       /* ascii keytype max chars */
103
104#define STORAGE_KEY_TYPE_MAX               (10)
105
106#define STORAGE_HID_ATRR_MASK_SIZE           (4)
107#define STORAGE_HID_SUB_CLASS_SIZE           (2)
108#define STORAGE_HID_APP_ID_SIZE              (2)
109#define STORAGE_HID_VENDOR_ID_SIZE           (4)
110#define STORAGE_HID_PRODUCT_ID_SIZE          (4)
111#define STORAGE_HID_VERSION_SIZE             (4)
112#define STORAGE_HID_CTRY_CODE_SIZE           (2)
113#define STORAGE_HID_DESC_LEN_SIZE            (4)
114#define STORAGE_HID_DESC_MAX_SIZE            (2*512)
115
116/* <18 char bd addr> <space> LIST< <36 char uuid> <;> > <keytype (dec)> <pinlen> */
117#define BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX (STORAGE_BDADDR_STRING_SZ + 1 +\
118                                             STORAGE_UUID_STRING_SIZE*BT_MAX_NUM_UUIDS + \
119                                             STORAGE_PINLEN_STRING_MAX_SIZE +\
120                                             STORAGE_KEYTYPE_STRING_MAX_SIZE)
121
122#define STORAGE_REMOTE_LINKKEYS_ENTRY_SIZE (LINK_KEY_LEN*2 + 1 + 2 + 1 + 2)
123
124/* <18 char bd addr> <space>LIST <attr_mask> <space> > <sub_class> <space> <app_id> <space>
125                                <vendor_id> <space> > <product_id> <space> <version> <space>
126                                <ctry_code> <space> > <desc_len> <space> <desc_list> <space> */
127#define BTIF_HID_INFO_ENTRY_SIZE_MAX    (STORAGE_BDADDR_STRING_SZ + 1 +\
128                                         STORAGE_HID_ATRR_MASK_SIZE + 1 +\
129                                         STORAGE_HID_SUB_CLASS_SIZE + 1 +\
130                                         STORAGE_HID_APP_ID_SIZE+ 1 +\
131                                         STORAGE_HID_VENDOR_ID_SIZE+ 1 +\
132                                         STORAGE_HID_PRODUCT_ID_SIZE+ 1 +\
133                                         STORAGE_HID_VERSION_SIZE+ 1 +\
134                                         STORAGE_HID_CTRY_CODE_SIZE+ 1 +\
135                                         STORAGE_HID_DESC_LEN_SIZE+ 1 +\
136                                         STORAGE_HID_DESC_MAX_SIZE+ 1 )
137
138
139/* currently remote services is the potentially largest entry */
140#define BTIF_STORAGE_MAX_LINE_SZ BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX
141
142
143/* check against unv max entry size at compile time */
144#if (BTIF_STORAGE_ENTRY_MAX_SIZE > UNV_MAXLINE_LENGTH)
145    #error "btif storage entry size exceeds unv max line size"
146#endif
147
148
149#define BTIF_STORAGE_HL_APP          "hl_app"
150#define BTIF_STORAGE_HL_APP_CB       "hl_app_cb"
151#define BTIF_STORAGE_HL_APP_DATA     "hl_app_data_"
152#define BTIF_STORAGE_HL_APP_MDL_DATA "hl_app_mdl_data_"
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**  Extern 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**  Static variables
169************************************************************************************/
170
171/************************************************************************************
172**  Static functions
173************************************************************************************/
174/*******************************************************************************
175**
176** Function         btif_in_make_filename
177**
178** Description      Internal helper function to create NVRAM file path
179**                  from address and filename
180**
181** Returns          NVRAM file path if successfull, NULL otherwise
182**
183*******************************************************************************/
184static char* btif_in_make_filename(bt_bdaddr_t *bd_addr, char *fname)
185{
186    static char path[256];
187    bdstr_t bdstr;
188
189    if (fname == NULL)return NULL;
190    if (bd_addr)
191    {
192        sprintf(path, "%s/%s/%s", BTIF_STORAGE_PATH_BLUEDROID,
193                bd2str(bd_addr, &bdstr), fname);
194    }
195    else
196    {
197        /* local adapter */
198        sprintf(path, "%s/LOCAL/%s", BTIF_STORAGE_PATH_BLUEDROID, fname);
199    }
200
201    return(char*)path;
202}
203/*******************************************************************************
204**
205** Function         btif_in_split_uuids_string_to_list
206**
207** Description      Internal helper function to split the string of UUIDs
208**                  read from the NVRAM to an array
209**
210** Returns          None
211**
212*******************************************************************************/
213static void btif_in_split_uuids_string_to_list(char *str, bt_uuid_t *p_uuid,
214                                               uint32_t *p_num_uuid)
215{
216    char buf[64];
217    char *p_start = str;
218    char *p_needle;
219    uint32_t num = 0;
220    do
221    {
222        //p_needle = strchr(p_start, ';');
223        p_needle = strchr(p_start, ' ');
224        if (p_needle < p_start) break;
225        memset(buf, 0, sizeof(buf));
226        strncpy(buf, p_start, (p_needle-p_start));
227        string_to_uuid(buf, p_uuid + num);
228        num++;
229        p_start = ++p_needle;
230
231    } while (*p_start != 0);
232    *p_num_uuid = num;
233}
234static int prop2cfg(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
235{
236    bdstr_t bdstr = {0};
237    if(remote_bd_addr)
238        bd2str(remote_bd_addr, &bdstr);
239    debug("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
240    char value[1024];
241    if(prop->len <= 0 || prop->len > (int)sizeof(value) - 1)
242    {
243        error("property type:%d, len:%d is invalid", prop->type, prop->len);
244        return FALSE;
245    }
246    switch(prop->type)
247    {
248       case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
249            btif_config_set_int("Remote", bdstr,
250                                BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int)time(NULL));
251            break;
252        case BT_PROPERTY_BDNAME:
253            strncpy(value, (char*)prop->val, prop->len);
254            value[prop->len]='\0';
255            if(remote_bd_addr)
256                btif_config_set_str("Remote", bdstr,
257                                BTIF_STORAGE_PATH_REMOTE_NAME, value);
258            else btif_config_set_str("Local", "Adapter",
259                                BTIF_STORAGE_KEY_ADAPTER_NAME, value);
260            break;
261        case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
262            strncpy(value, (char*)prop->val, prop->len);
263            value[prop->len]='\0';
264            btif_config_set_str("Remote", bdstr, BTIF_STORAGE_PATH_REMOTE_ALIASE, value);
265            break;
266        case BT_PROPERTY_ADAPTER_SCAN_MODE:
267            btif_config_set_int("Local", "Adapter",
268                                BTIF_STORAGE_KEY_ADAPTER_SCANMODE, *(int*)prop->val);
269            break;
270        case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
271            btif_config_set_int("Local", "Adapter",
272                                BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, *(int*)prop->val);
273            break;
274        case BT_PROPERTY_CLASS_OF_DEVICE:
275            btif_config_set_int("Remote", bdstr,
276                                BTIF_STORAGE_PATH_REMOTE_DEVCLASS, *(int*)prop->val);
277            break;
278        case BT_PROPERTY_TYPE_OF_DEVICE:
279            btif_config_set_int("Remote", bdstr,
280                                BTIF_STORAGE_PATH_REMOTE_DEVTYPE, *(int*)prop->val);
281            break;
282        case BT_PROPERTY_UUIDS:
283        {
284            uint32_t i;
285            char buf[64];
286            value[0] = 0;
287            for (i=0; i < (prop->len)/sizeof(bt_uuid_t); i++)
288            {
289                bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val + i;
290                memset(buf, 0, sizeof(buf));
291                uuid_to_string(p_uuid, buf);
292                strcat(value, buf);
293                //strcat(value, ";");
294                strcat(value, " ");
295            }
296            btif_config_set_str("Remote", bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value);
297            btif_config_save();
298            break;
299        }
300        default:
301             error("Unknow prop type:%d", prop->type);
302             return FALSE;
303    }
304    return TRUE;
305}
306static int cfg2prop(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
307{
308    bdstr_t bdstr = {0};
309    if(remote_bd_addr)
310        bd2str(remote_bd_addr, &bdstr);
311    debug("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len);
312    if(prop->len <= 0)
313    {
314        error("property type:%d, len:%d is invalid", prop->type, prop->len);
315        return FALSE;
316    }
317    int ret = FALSE;
318    switch(prop->type)
319    {
320       case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
321            if(prop->len >= (int)sizeof(int))
322                ret = btif_config_get_int("Remote", bdstr,
323                                        BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int*)prop->val);
324            break;
325        case BT_PROPERTY_BDNAME:
326        {
327            int len = prop->len;
328            if(remote_bd_addr)
329                ret = btif_config_get_str("Remote", bdstr,
330                                        BTIF_STORAGE_PATH_REMOTE_NAME, (char*)prop->val, &len);
331            else ret = btif_config_get_str("Local", "Adapter",
332                                        BTIF_STORAGE_KEY_ADAPTER_NAME, (char*)prop->val, &len);
333            if(ret && len && len <= prop->len)
334                prop->len = len - 1;
335            else
336            {
337                prop->len = 0;
338                ret = FALSE;
339            }
340            break;
341        }
342        case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
343        {
344            int len = prop->len;
345            ret = btif_config_get_str("Remote", bdstr,
346                                       BTIF_STORAGE_PATH_REMOTE_ALIASE, (char*)prop->val, &len);
347            if(ret && len && len <= prop->len)
348                prop->len = len - 1;
349            else
350            {
351                prop->len = 0;
352                ret = FALSE;
353            }
354            break;
355        }
356        case BT_PROPERTY_ADAPTER_SCAN_MODE:
357           if(prop->len >= (int)sizeof(int))
358                ret = btif_config_get_int("Local", "Adapter",
359                                          BTIF_STORAGE_KEY_ADAPTER_SCANMODE, (int*)prop->val);
360           break;
361        case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
362           if(prop->len >= (int)sizeof(int))
363                ret = btif_config_get_int("Local", "Adapter",
364                                          BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, (int*)prop->val);
365            break;
366        case BT_PROPERTY_CLASS_OF_DEVICE:
367            if(prop->len >= (int)sizeof(int))
368                ret = btif_config_get_int("Remote", bdstr,
369                                BTIF_STORAGE_PATH_REMOTE_DEVCLASS, (int*)prop->val);
370            break;
371        case BT_PROPERTY_TYPE_OF_DEVICE:
372            if(prop->len >= (int)sizeof(int))
373                ret = btif_config_get_int("Remote",
374                                    bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTYPE, (int*)prop->val);
375            break;
376        case BT_PROPERTY_UUIDS:
377        {
378            char value[1280];
379            int size = sizeof(value);
380            if(btif_config_get_str("Remote", bdstr,
381                                    BTIF_STORAGE_PATH_REMOTE_SERVICE, value, &size))
382            {
383                bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val;
384                uint32_t num_uuids = 0;
385                btif_in_split_uuids_string_to_list(value, p_uuid, &num_uuids);
386                prop->len = num_uuids * sizeof(bt_uuid_t);
387                ret = TRUE;
388            }
389            break;
390        }
391        default:
392            error("Unknow prop type:%d", prop->type);
393            return FALSE;
394    }
395    return ret;
396}
397
398
399/*******************************************************************************
400**
401** Function         btif_in_fetch_bonded_devices
402**
403** Description      Internal helper function to fetch the bonded devices
404**                  from NVRAM
405**
406** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
407**
408*******************************************************************************/
409static bt_status_t btif_in_fetch_bonded_devices(btif_bonded_devices_t *p_bonded_devices, int add)
410{
411    debug("in add:%d", add);
412    memset(p_bonded_devices, 0, sizeof(btif_bonded_devices_t));
413
414    char kname[128], vname[128];
415    short kpos;
416    int kname_size;
417    kname_size = sizeof(kname);
418    kname[0] = 0;
419    kpos = 0;
420    do
421    {
422        kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
423        debug("Remote device:%s, size:%d", kname, kname_size);
424        int type = BTIF_CFG_TYPE_BIN;
425        LINK_KEY link_key;
426        int size = sizeof(link_key);
427        if(btif_config_get("Remote", kname, "LinkKey", (char*)link_key, &size, &type))
428        {
429            int linkkey_type;
430            if(btif_config_get_int("Remote", kname, "LinkKeyType", &linkkey_type))
431            {
432                //int pin_len;
433                //btif_config_get_int("Remote", kname, "PinLength", &pin_len))
434                bt_bdaddr_t bd_addr;
435                str2bd(kname, &bd_addr);
436                if(add)
437                {
438                    DEV_CLASS dev_class = {0, 0, 0};
439                    int cod;
440                    if(btif_config_get_int("Remote", kname, "DevClass", &cod))
441                        uint2devclass((UINT32)cod, dev_class);
442                    BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0, (UINT8)linkkey_type, 0);
443                }
444                memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t));
445            }
446            else error("bounded device:%s, LinkKeyType or PinLength is invalid", kname);
447        }
448        else debug("Remote device:%s, no link key", kname);
449        kname_size = sizeof(kname);
450        kname[0] = 0;
451    } while(kpos != -1);
452    debug("out");
453    return BT_STATUS_SUCCESS;
454}
455
456static int hex_str_to_int(const char* str, int size)
457{
458    int  n = 0;
459    char c = *str++;
460    while (size-- != 0)
461    {
462        n <<= 4;
463        if (c >= '0' && c <= '9') {
464            n |= c - '0';
465        }
466        else if (c >= 'a' && c <= 'z') {
467            n |= c - 'a' + 10;
468        }
469        else // (c >= 'A' && c <= 'Z')
470        {
471            n |= c - 'A' + 10;
472        }
473
474        c = *str++;
475    }
476    return n;
477}
478
479/************************************************************************************
480**  Externs
481************************************************************************************/
482
483/************************************************************************************
484**  Functions
485************************************************************************************/
486
487/** functions are synchronous.
488 * functions can be called by both internal modules such as BTIF_DM and by external entiries from HAL via BTIF_context_switch
489 * For OUT parameters,  caller is expected to provide the memory.
490 * Caller is expected to provide a valid pointer to 'property->value' based on the property->type
491 */
492/*******************************************************************************
493**
494** Function         btif_storage_get_adapter_property
495**
496** Description      BTIF storage API - Fetches the adapter property->type
497**                  from NVRAM and fills property->val.
498**                  Caller should provide memory for property->val and
499**                  set the property->val
500**
501** Returns          BT_STATUS_SUCCESS if the fetch was successful,
502**                  BT_STATUS_FAIL otherwise
503**
504*******************************************************************************/
505bt_status_t btif_storage_get_adapter_property(bt_property_t *property)
506{
507
508    /* Special handling for adapter BD_ADDR and BONDED_DEVICES */
509    if (property->type == BT_PROPERTY_BDADDR)
510    {
511        BD_ADDR addr;
512        bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)property->val;
513        /* This has been cached in btif. Just fetch it from there */
514        memcpy(bd_addr, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
515        property->len = sizeof(bt_bdaddr_t);
516        return BT_STATUS_SUCCESS;
517    }
518    else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES)
519    {
520        btif_bonded_devices_t bonded_devices;
521
522        btif_in_fetch_bonded_devices(&bonded_devices, 0);
523
524        BTIF_TRACE_DEBUG2("%s: Number of bonded devices: %d Property:BT_PROPERTY_ADAPTER_BONDED_DEVICES", __FUNCTION__, bonded_devices.num_devices);
525
526        if (bonded_devices.num_devices > 0)
527        {
528            property->len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
529            memcpy(property->val, bonded_devices.devices, property->len);
530        }
531
532        /* if there are no bonded_devices, then length shall be 0 */
533        return BT_STATUS_SUCCESS;
534    }
535    else if (property->type == BT_PROPERTY_UUIDS)
536    {
537        /* publish list of local supported services */
538        bt_uuid_t *p_uuid = (bt_uuid_t*)property->val;
539        uint32_t num_uuids = 0;
540        uint32_t i;
541
542        tBTA_SERVICE_MASK service_mask = btif_get_enabled_services_mask();
543        BTIF_TRACE_ERROR2("%s service_mask:0x%x", __FUNCTION__, service_mask);
544        for (i=0; i < BTA_MAX_SERVICE_ID; i++)
545        {
546            /* This should eventually become a function when more services are enabled */
547            if (service_mask
548                &(tBTA_SERVICE_MASK)(1 << i))
549            {
550                switch (i)
551                {
552                    case BTA_HFP_SERVICE_ID:
553                        {
554                            uuid16_to_uuid128(UUID_SERVCLASS_AG_HANDSFREE,
555                                              p_uuid+num_uuids);
556                            num_uuids++;
557                        }
558                    /* intentional fall through: Send both BFP & HSP UUIDs if HFP is enabled */
559                    case BTA_HSP_SERVICE_ID:
560                        {
561                            uuid16_to_uuid128(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,
562                                              p_uuid+num_uuids);
563                            num_uuids++;
564                        }break;
565                    case BTA_A2DP_SERVICE_ID:
566                        {
567                            uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SOURCE,
568                                              p_uuid+num_uuids);
569                            num_uuids++;
570                        }break;
571                }
572            }
573        }
574        property->len = (num_uuids)*sizeof(bt_uuid_t);
575        return BT_STATUS_SUCCESS;
576    }
577
578    /* fall through for other properties */
579    if(!cfg2prop(NULL, property))
580    {
581        return btif_dm_get_adapter_property(property);
582    }
583    return BT_STATUS_SUCCESS;
584 }
585
586/*******************************************************************************
587**
588** Function         btif_storage_set_adapter_property
589**
590** Description      BTIF storage API - Stores the adapter property
591**                  to NVRAM
592**
593** Returns          BT_STATUS_SUCCESS if the store was successful,
594**                  BT_STATUS_FAIL otherwise
595**
596*******************************************************************************/
597bt_status_t btif_storage_set_adapter_property(bt_property_t *property)
598{
599    return prop2cfg(NULL, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
600}
601
602/*******************************************************************************
603**
604** Function         btif_storage_get_remote_device_property
605**
606** Description      BTIF storage API - Fetches the remote device property->type
607**                  from NVRAM and fills property->val.
608**                  Caller should provide memory for property->val and
609**                  set the property->val
610**
611** Returns          BT_STATUS_SUCCESS if the fetch was successful,
612**                  BT_STATUS_FAIL otherwise
613**
614*******************************************************************************/
615bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t *remote_bd_addr,
616                                                    bt_property_t *property)
617{
618    return cfg2prop(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
619}
620/*******************************************************************************
621**
622** Function         btif_storage_set_remote_device_property
623**
624** Description      BTIF storage API - Stores the remote device property
625**                  to NVRAM
626**
627** Returns          BT_STATUS_SUCCESS if the store was successful,
628**                  BT_STATUS_FAIL otherwise
629**
630*******************************************************************************/
631bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t *remote_bd_addr,
632                                                    bt_property_t *property)
633{
634    return prop2cfg(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
635}
636
637/*******************************************************************************
638**
639** Function         btif_storage_add_remote_device
640**
641** Description      BTIF storage API - Adds a newly discovered device to NVRAM
642**                  along with the timestamp. Also, stores the various
643**                  properties - RSSI, BDADDR, NAME (if found in EIR)
644**
645** Returns          BT_STATUS_SUCCESS if the store was successful,
646**                  BT_STATUS_FAIL otherwise
647**
648*******************************************************************************/
649bt_status_t btif_storage_add_remote_device(bt_bdaddr_t *remote_bdaddr,
650                                           uint32_t num_properties,
651                                           bt_property_t *properties)
652{
653    uint32_t i = 0;
654    /* TODO: If writing a property, fails do we go back undo the earlier
655     * written properties? */
656    for (i=0; i < num_properties; i++)
657    {
658        /* Ignore the RSSI as this is not stored in DB */
659        if (properties[i].type == BT_PROPERTY_REMOTE_RSSI)
660            continue;
661
662        /* BD_ADDR for remote device needs special handling as we also store timestamp */
663        if (properties[i].type == BT_PROPERTY_BDADDR)
664        {
665            bt_property_t addr_prop;
666            memcpy(&addr_prop, &properties[i], sizeof(bt_property_t));
667            addr_prop.type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP;
668            btif_storage_set_remote_device_property(remote_bdaddr,
669                                                    &addr_prop);
670        }
671        else
672        {
673            btif_storage_set_remote_device_property(remote_bdaddr,
674                                                    &properties[i]);
675        }
676    }
677    return BT_STATUS_SUCCESS;
678}
679
680/*******************************************************************************
681**
682** Function         btif_storage_add_bonded_device
683**
684** Description      BTIF storage API - Adds the newly bonded device to NVRAM
685**                  along with the link-key, Key type and Pin key length
686**
687** Returns          BT_STATUS_SUCCESS if the store was successful,
688**                  BT_STATUS_FAIL otherwise
689**
690*******************************************************************************/
691
692bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
693                                           LINK_KEY link_key,
694                                           uint8_t key_type,
695                                           uint8_t pin_length)
696{
697    bdstr_t bdstr;
698    bd2str(remote_bd_addr, &bdstr);
699    int ret = btif_config_set_int("Remote", bdstr, "LinkKeyType", (int)key_type);
700    ret &= btif_config_set_int("Remote", bdstr, "PinLength", (int)pin_length);
701    ret &= btif_config_set("Remote", bdstr, "LinkKey", (const char*)link_key, sizeof(LINK_KEY), BTIF_CFG_TYPE_BIN);
702    btif_config_save();
703    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
704}
705
706/*******************************************************************************
707**
708** Function         btif_storage_remove_bonded_device
709**
710** Description      BTIF storage API - Deletes the bonded device from NVRAM
711**
712** Returns          BT_STATUS_SUCCESS if the deletion was successful,
713**                  BT_STATUS_FAIL otherwise
714**
715*******************************************************************************/
716bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
717{
718    bdstr_t bdstr;
719    bd2str(remote_bd_addr, &bdstr);
720    debug("in bd addr:%s", bdstr);
721    int ret = btif_config_remove("Remote", bdstr, "LinkKeyType");
722    ret &= btif_config_remove("Remote", bdstr, "PinLength");
723    ret &= btif_config_remove("Remote", bdstr, "LinkKey");
724    btif_config_save();
725    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
726
727}
728
729/*******************************************************************************
730**
731** Function         btif_storage_load_bonded_devices
732**
733** Description      BTIF storage API - Loads all the bonded devices from NVRAM
734**                  and adds to the BTA.
735**                  Additionally, this API also invokes the adaper_properties_cb
736**                  and remote_device_properties_cb for each of the bonded devices.
737**
738** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
739**
740*******************************************************************************/
741bt_status_t btif_storage_load_bonded_devices(void)
742{
743    char *fname;
744    btif_bonded_devices_t bonded_devices;
745    uint32_t i = 0;
746    bt_property_t adapter_props[6];
747    uint32_t num_props = 0;
748    bt_property_t remote_properties[8];
749    bt_bdaddr_t addr;
750    bt_bdname_t name, alias;
751    bt_scan_mode_t mode;
752    uint32_t disc_timeout;
753    bt_bdaddr_t *devices_list;
754    bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
755    bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
756    uint32_t cod, devtype;
757
758    btif_in_fetch_bonded_devices(&bonded_devices, 1);
759
760    /* Now send the adapter_properties_cb with all adapter_properties */
761    {
762        memset(adapter_props, 0, sizeof(adapter_props));
763
764        /* BD_ADDR */
765        BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDADDR, &addr, sizeof(addr),
766                                      adapter_props[num_props]);
767        num_props++;
768
769        /* BD_NAME */
770        BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDNAME, &name, sizeof(name),
771                                      adapter_props[num_props]);
772        num_props++;
773
774        /* SCAN_MODE */
775        /* TODO: At the time of BT on, always report the scan mode as 0 irrespective
776         of the scan_mode during the previous enable cycle.
777         This needs to be re-visited as part of the app/stack enable sequence
778         synchronization */
779        mode = BT_SCAN_MODE_NONE;
780        adapter_props[num_props].type = BT_PROPERTY_ADAPTER_SCAN_MODE;
781        adapter_props[num_props].len = sizeof(mode);
782        adapter_props[num_props].val = &mode;
783        num_props++;
784
785        /* DISC_TIMEOUT */
786        BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
787                                      &disc_timeout, sizeof(disc_timeout),
788                                      adapter_props[num_props]);
789        num_props++;
790
791        /* BONDED_DEVICES */
792        devices_list = (bt_bdaddr_t*)malloc(sizeof(bt_bdaddr_t)*bonded_devices.num_devices);
793        adapter_props[num_props].type = BT_PROPERTY_ADAPTER_BONDED_DEVICES;
794        adapter_props[num_props].len = bonded_devices.num_devices * sizeof(bt_bdaddr_t);
795        adapter_props[num_props].val = devices_list;
796        for (i=0; i < bonded_devices.num_devices; i++)
797        {
798            memcpy(devices_list + i, &bonded_devices.devices[i], sizeof(bt_bdaddr_t));
799        }
800        num_props++;
801
802        /* LOCAL UUIDs */
803        BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_UUIDS,
804                                      local_uuids, sizeof(local_uuids),
805                                      adapter_props[num_props]);
806        num_props++;
807
808        btif_adapter_properties_evt(BT_STATUS_SUCCESS, num_props, adapter_props);
809
810        free(devices_list);
811    }
812
813    BTIF_TRACE_EVENT2("%s: %d bonded devices found", __FUNCTION__, bonded_devices.num_devices);
814
815    {
816        for (i = 0; i < bonded_devices.num_devices; i++)
817        {
818            bt_bdaddr_t *p_remote_addr;
819
820            num_props = 0;
821            p_remote_addr = &bonded_devices.devices[i];
822            memset(remote_properties, 0, sizeof(remote_properties));
823            BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_BDNAME,
824                                         &name, sizeof(name),
825                                         remote_properties[num_props]);
826            num_props++;
827
828            BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_REMOTE_FRIENDLY_NAME,
829                                         &alias, sizeof(alias),
830                                         remote_properties[num_props]);
831            num_props++;
832
833            BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_CLASS_OF_DEVICE,
834                                         &cod, sizeof(cod),
835                                         remote_properties[num_props]);
836            num_props++;
837
838            BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_TYPE_OF_DEVICE,
839                                         &devtype, sizeof(devtype),
840                                         remote_properties[num_props]);
841            num_props++;
842
843            BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_UUIDS,
844                                         remote_uuids, sizeof(remote_uuids),
845                                         remote_properties[num_props]);
846            num_props++;
847
848            btif_remote_properties_evt(BT_STATUS_SUCCESS, p_remote_addr,
849                                       num_props, remote_properties);
850        }
851    }
852    return BT_STATUS_SUCCESS;
853}
854
855/*******************************************************************************
856**
857** Function         btif_storage_add_hid_device_info
858**
859** Description      BTIF storage API - Adds the hid information of bonded hid devices-to NVRAM
860**
861** Returns          BT_STATUS_SUCCESS if the store was successful,
862**                  BT_STATUS_FAIL otherwise
863**
864*******************************************************************************/
865
866bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr,
867                                                    UINT16 attr_mask, UINT8 sub_class,
868                                                    UINT8 app_id, UINT16 vendor_id,
869                                                    UINT16 product_id, UINT16 version,
870                                                    UINT8 ctry_code, UINT16 dl_len, UINT8 *dsc_list)
871{
872    bdstr_t bdstr;
873    bd2str(remote_bd_addr, &bdstr);
874    btif_config_set_int("Remote", bdstr, "HidAttrMask", attr_mask);
875    btif_config_set_int("Remote", bdstr, "HidSubClass", sub_class);
876    btif_config_set_int("Remote", bdstr, "HidAppId", app_id);
877    btif_config_set_int("Remote", bdstr, "HidVendorId", vendor_id);
878    btif_config_set_int("Remote", bdstr, "HidProductId", product_id);
879    btif_config_set_int("Remote", bdstr, "HidVersion", version);
880    btif_config_set_int("Remote", bdstr, "HidCountryCode", ctry_code);
881    if(dl_len > 0)
882        btif_config_set("Remote", bdstr, "HidDescriptor", (const char*)dsc_list, dl_len, BTIF_CFG_TYPE_BIN);
883    return BT_STATUS_SUCCESS;
884}
885
886/*******************************************************************************
887**
888** Function         btif_storage_load_bonded_hid_info
889**
890** Description      BTIF storage API - Loads hid info for all the bonded devices from NVRAM
891**                  and adds those devices  to the BTA_HH.
892**
893** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
894**
895*******************************************************************************/
896bt_status_t btif_storage_load_bonded_hid_info(void)
897{
898    debug("in");
899    bt_bdaddr_t bd_addr;
900    tBTA_HH_DEV_DSCP_INFO dscp_info;
901    uint32_t i;
902    uint16_t attr_mask;
903    uint8_t  sub_class;
904    uint8_t  app_id;
905
906    char kname[128], vname[128];
907    short kpos;
908    int kname_size;
909    kname_size = sizeof(kname);
910    kname[0] = 0;
911    kpos = 0;
912    memset(&dscp_info, 0, sizeof(dscp_info));
913    do
914    {
915        kpos = btif_config_next_key(kpos, "Remote", kname, &kname_size);
916        debug("Remote device:%s, size:%d", kname, kname_size);
917        int value;
918        if(btif_config_get_int("Remote", kname, "HidAttrMask", &value))
919        {
920            attr_mask = (uint16_t)value;
921
922            btif_config_get_int("Remote", kname, "HidSubClass", &value);
923            sub_class = (uint8_t)value;
924
925            btif_config_get_int("Remote", kname, "HidAppId", &value);
926            app_id = (uint8_t)value;
927
928            btif_config_get_int("Remote", kname, "HidVendorId", &value);
929            dscp_info.vendor_id = (uint16_t) value;
930
931            btif_config_get_int("Remote", kname, "HidProductId", &value);
932            dscp_info.product_id = (uint16_t) value;
933
934            btif_config_get_int("Remote", kname, "HidVersion", &value);
935            dscp_info.version = (uint8_t) value;
936
937            btif_config_get_int("Remote", kname, "HidCountryCode", &value);
938            dscp_info.ctry_code = (uint8_t) value;
939
940            int len = 0;
941            int type;
942            btif_config_get("Remote", kname, "HidDescriptor", NULL, &len, &type);
943            if(len > 0)
944            {
945                dscp_info.descriptor.dl_len = (uint16_t)len;
946                dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len);
947                btif_config_get("Remote", kname, "HidDescriptor", (char*)dscp_info.descriptor.dsc_list, &len, &type);
948            }
949            str2bd(kname, &bd_addr);
950            // add extracted information to BTA HH
951            if (btif_hh_add_added_dev(bd_addr,attr_mask))
952            {
953                BTA_HhAddDev(bd_addr.address, attr_mask, sub_class,
954                        app_id, dscp_info);
955            }
956        }
957    } while(kpos != -1);
958
959    return BT_STATUS_SUCCESS;
960}
961
962/*******************************************************************************
963**
964** Function         btif_storage_remove_hid_info
965**
966** Description      BTIF storage API - Deletes the bonded hid device info from NVRAM
967**
968** Returns          BT_STATUS_SUCCESS if the deletion was successful,
969**                  BT_STATUS_FAIL otherwise
970**
971*******************************************************************************/
972bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr)
973{
974    char *fname;
975    int ret;
976    bdstr_t bdstr;
977    bd2str(remote_bd_addr, &bdstr);
978
979    btif_config_remove("Remote", bdstr, "HidAttrMask");
980    btif_config_remove("Remote", bdstr, "HidSubClass");
981    btif_config_remove("Remote", bdstr, "HidAppId");
982    btif_config_remove("Remote", bdstr, "HidVendorId");
983    btif_config_remove("Remote", bdstr, "HidProductId");
984    btif_config_remove("Remote", bdstr, "HidVersion");
985    btif_config_remove("Remote", bdstr, "HidCountryCode");
986    btif_config_remove("Remote", bdstr, "HidDescriptor");
987    return BT_STATUS_SUCCESS;
988}
989
990/*******************************************************************************
991**
992** Function         btif_storage_read_hl_apps_cb
993**
994** Description      BTIF storage API - Read HL application control block from NVRAM
995**
996** Returns          BT_STATUS_SUCCESS if the operation was successful,
997**                  BT_STATUS_FAIL otherwise
998**
999*******************************************************************************/
1000bt_status_t btif_storage_read_hl_apps_cb(char *value, int value_size)
1001{
1002    bt_status_t bt_status = BT_STATUS_SUCCESS;
1003    int read_size=value_size, read_type=BTIF_CFG_TYPE_BIN;
1004
1005    if (!btif_config_exist("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB))
1006    {
1007        memset(value, value_size, 0);
1008        if (!btif_config_set("Local", BTIF_STORAGE_HL_APP,BTIF_STORAGE_HL_APP_CB,
1009                             value, value_size, BTIF_CFG_TYPE_BIN))
1010        {
1011            bt_status = BT_STATUS_FAIL;
1012        }
1013        else
1014        {
1015            btif_config_save();
1016        }
1017    }
1018    else
1019    {
1020        if (!btif_config_get("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB,
1021                             value, &read_size, &read_type))
1022        {
1023            bt_status = BT_STATUS_FAIL;
1024        }
1025        else
1026        {
1027            if ((read_size != value_size) || (read_type != BTIF_CFG_TYPE_BIN) )
1028            {
1029                BTIF_TRACE_ERROR4("%s  value_size=%d read_size=%d read_type=%d",
1030                                  __FUNCTION__, value_size, read_size, read_type);
1031                bt_status = BT_STATUS_FAIL;
1032            }
1033        }
1034
1035    }
1036
1037    BTIF_TRACE_DEBUG3("%s  status=%d value_size=%d", __FUNCTION__, bt_status, value_size);
1038    return bt_status;
1039}
1040
1041
1042/*******************************************************************************
1043**
1044** Function         btif_storage_write_hl_apps_cb
1045**
1046** Description      BTIF storage API - Write HL application control block to NVRAM
1047**
1048** Returns          BT_STATUS_SUCCESS if the operation was successful,
1049**                  BT_STATUS_FAIL otherwise
1050**
1051*******************************************************************************/
1052bt_status_t btif_storage_write_hl_apps_cb(char *value, int value_size)
1053{
1054    bt_status_t bt_status = BT_STATUS_SUCCESS;
1055
1056    if (!btif_config_set("Local", BTIF_STORAGE_HL_APP, BTIF_STORAGE_HL_APP_CB,
1057                         value, value_size, BTIF_CFG_TYPE_BIN))
1058    {
1059        bt_status = BT_STATUS_FAIL;
1060    }
1061    else
1062    {
1063        btif_config_save();
1064    }
1065    BTIF_TRACE_DEBUG3("%s  status=%d value_size=%d", __FUNCTION__, bt_status, value_size);
1066
1067    return bt_status;
1068}
1069
1070bt_status_t btif_storage_read_hl_data(char *fname, char *value, int value_size)
1071{
1072    bt_status_t bt_status = BT_STATUS_SUCCESS;
1073    int read_size=value_size, read_type=BTIF_CFG_TYPE_BIN;
1074
1075    if (!btif_config_get("Local", BTIF_STORAGE_HL_APP, fname, value, &read_size, &read_type))
1076    {
1077        bt_status = BT_STATUS_FAIL;
1078    }
1079    else
1080    {
1081        if ((read_size != value_size) || (read_type != BTIF_CFG_TYPE_BIN) )
1082        {
1083            BTIF_TRACE_ERROR4("%s  value_size=%d read_size=%d read_type=%d",
1084                              __FUNCTION__, value_size, read_size, read_type);
1085            bt_status = BT_STATUS_FAIL;
1086        }
1087    }
1088
1089    return bt_status;
1090}
1091
1092bt_status_t btif_storage_write_hl_data(char *fname, char *value, int value_size)
1093{
1094    bt_status_t bt_status = BT_STATUS_SUCCESS;
1095
1096    if (!btif_config_set("Local", BTIF_STORAGE_HL_APP, fname, value, value_size, BTIF_CFG_TYPE_BIN))
1097    {
1098        bt_status = BT_STATUS_FAIL;
1099    }
1100    else
1101    {
1102         btif_config_save();
1103    }
1104    BTIF_TRACE_DEBUG3("%s  status=%d value_size=%d", __FUNCTION__, bt_status, value_size);
1105
1106    return bt_status;
1107
1108}
1109
1110/*******************************************************************************
1111**
1112** Function         btif_storage_read_hl_app_data
1113**
1114** Description      BTIF storage API - Read HL application configuration from NVRAM
1115**
1116** Returns          BT_STATUS_SUCCESS if the operation was successful,
1117**                  BT_STATUS_FAIL otherwise
1118**
1119*******************************************************************************/
1120bt_status_t btif_storage_read_hl_app_data(UINT8 app_idx, char *value, int value_size)
1121{
1122    char  fname[256];
1123    bt_status_t bt_status = BT_STATUS_SUCCESS;
1124
1125    BTIF_TRACE_DEBUG1("%s ", __FUNCTION__);
1126    sprintf(fname, "%s%d", BTIF_STORAGE_HL_APP_DATA, app_idx);
1127    bt_status = btif_storage_read_hl_data(fname,  value, value_size);
1128    BTIF_TRACE_DEBUG3("%s read item:(%s) bt_status=%d", __FUNCTION__, fname,   bt_status);
1129
1130    return bt_status;
1131}
1132
1133/*******************************************************************************
1134**
1135** Function         btif_storage_write_hl_app_data
1136**
1137** Description      BTIF storage API - Write HL application MDL configuration from NVRAM
1138**
1139** Returns          BT_STATUS_SUCCESS if the operation was successful,
1140**                  BT_STATUS_FAIL otherwise
1141**
1142*******************************************************************************/
1143bt_status_t btif_storage_write_hl_app_data(UINT8 app_idx, char *value, int value_size)
1144{
1145    char  fname[256];
1146    bt_status_t bt_status = BT_STATUS_SUCCESS;
1147
1148
1149    BTIF_TRACE_DEBUG1("%s ", __FUNCTION__);
1150    sprintf(fname, "%s%d", BTIF_STORAGE_HL_APP_DATA, app_idx);
1151    bt_status = btif_storage_write_hl_data(fname,  value, value_size);
1152    BTIF_TRACE_DEBUG3("%s write item:(%s) bt_status=%d", __FUNCTION__, fname,   bt_status);
1153
1154    return bt_status;
1155}
1156
1157/*******************************************************************************
1158**
1159** Function         btif_storage_read_hl_mdl_data
1160**
1161** Description      BTIF storage API - Read HL application MDL configuration from NVRAM
1162**
1163** Returns          BT_STATUS_SUCCESS if the operation was successful,
1164**                  BT_STATUS_FAIL otherwise
1165**
1166*******************************************************************************/
1167bt_status_t btif_storage_read_hl_mdl_data(UINT8 app_idx, char *value, int value_size)
1168{
1169    char  fname[256],  tmp[3];
1170    bt_status_t bt_status = BT_STATUS_SUCCESS;
1171    int   status, i, buf_size;
1172    char *p_buf;
1173
1174    BTIF_TRACE_DEBUG1("%s ", __FUNCTION__);
1175    sprintf(fname, "%s%d", BTIF_STORAGE_HL_APP_MDL_DATA, app_idx);
1176    bt_status = btif_storage_read_hl_data(fname,  value, value_size);
1177    BTIF_TRACE_DEBUG3("%s read item:(%s) bt_status=%d", __FUNCTION__, fname,   bt_status);
1178
1179    return bt_status;
1180}
1181
1182/*******************************************************************************
1183**
1184** Function         btif_storage_write_hl_mdl_data
1185**
1186** Description      BTIF storage API - Write HL application MDL configuration from NVRAM
1187**
1188** Returns          BT_STATUS_SUCCESS if the operation was successful,
1189**                  BT_STATUS_FAIL otherwise
1190**
1191*******************************************************************************/
1192bt_status_t btif_storage_write_hl_mdl_data(UINT8 app_idx, char *value, int value_size)
1193{
1194    char  fname[256];
1195    bt_status_t bt_status = BT_STATUS_SUCCESS;
1196    int   status, i, buf_size;
1197    char *p_buf;
1198
1199    BTIF_TRACE_DEBUG1("%s ", __FUNCTION__);
1200    sprintf(fname, "%s%d", BTIF_STORAGE_HL_APP_MDL_DATA, app_idx);
1201    bt_status = btif_storage_write_hl_data(fname,  value, value_size);
1202    BTIF_TRACE_DEBUG3("%s write item:(%s) bt_status=%d", __FUNCTION__, fname,   bt_status);
1203
1204    return bt_status;
1205}
1206
1207/*******************************************************************************
1208**
1209** Function         btif_storage_load_autopair_device_list
1210**
1211** Description      BTIF storage API - Populates auto pair device list
1212**
1213** Returns          BT_STATUS_SUCCESS if the auto pair blacklist is successfully populated
1214**                  BT_STATUS_FAIL otherwise
1215**
1216*******************************************************************************/
1217
1218bt_status_t btif_storage_load_autopair_device_list()
1219{
1220    char *key_name, *key_value;
1221    int i=0;
1222    char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1223    char *line;
1224    FILE *fp;
1225
1226    if(!btif_config_exist("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, NULL))
1227    {
1228        /* first time loading of auto pair blacklist configuration  */
1229
1230        fp = fopen (BTIF_AUTO_PAIR_CONF_FILE, "r");
1231
1232        if (fp == NULL)
1233        {
1234            ALOGE("%s: Failed to open auto pair blacklist conf file at %s", __FUNCTION__,BTIF_AUTO_PAIR_CONF_FILE );
1235            return BT_STATUS_FAIL;
1236        }
1237
1238        /* read through auto_pairing.conf file and create the key value pairs specific to  auto pair blacklist devices */
1239        while (fgets(linebuf, BTIF_STORAGE_MAX_LINE_SZ, fp) != NULL)
1240        {
1241            /* trip  leading white spaces */
1242            while (linebuf[i] == BTIF_AUTO_PAIR_CONF_SPACE)
1243                i++;
1244
1245            /* skip  commented lines */
1246            if (linebuf[i] == BTIF_AUTO_PAIR_CONF_COMMENT)
1247                continue;
1248
1249            line = (char*)&(linebuf[i]);
1250
1251            if (line == NULL)
1252                continue;
1253
1254            key_name = strtok(line, BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER);
1255
1256            if (key_name == NULL)
1257                continue;
1258            else if((strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR) == 0) ||
1259                    (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME) ==0) ||
1260                    (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST) ==0 ) ||
1261                    (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME) == 0) ||
1262                    (strcmp(key_name, BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR) == 0))
1263            {
1264                key_value = strtok(NULL, BTIF_AUTO_PAIR_CONF_KEY_VAL_DELIMETER);
1265                btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST, key_name, key_value);
1266            }
1267        }
1268        fclose(fp);
1269    }
1270    return BT_STATUS_SUCCESS;
1271}
1272
1273/*******************************************************************************
1274**
1275** Function         btif_storage_is_device_autopair_blacklisted
1276**
1277** Description      BTIF storage API  Checks if the given device is blacklisted for auto pairing
1278**
1279** Returns          TRUE if the device is found in the auto pair blacklist
1280**                  FALSE otherwise
1281**
1282*******************************************************************************/
1283BOOLEAN  btif_storage_is_device_autopair_blacklisted(bt_bdaddr_t *remote_dev_addr)
1284{
1285    char *token;
1286    int ret;
1287    bdstr_t bdstr;
1288    char *dev_name_str;
1289    uint8_t i = 0;
1290    char value[BTIF_STORAGE_MAX_LINE_SZ];
1291    int value_size = sizeof(value);
1292
1293    bd2str(remote_dev_addr, &bdstr);
1294
1295    /* Consider only  Lower Address Part from BD Address */
1296    bdstr[8] = '\0';
1297
1298    if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1299                BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_ADDR, value, &value_size))
1300    {
1301        if (strcasestr(value,bdstr) != NULL)
1302            return TRUE;
1303    }
1304
1305    dev_name_str = BTM_SecReadDevName((remote_dev_addr->address));
1306
1307    if (dev_name_str != NULL)
1308    {
1309        value_size = sizeof(value);
1310        if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1311                    BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_EXACTNAME, value, &value_size))
1312        {
1313            if (strstr(value,dev_name_str) != NULL)
1314                return TRUE;
1315        }
1316        value_size = sizeof(value);
1317        if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1318                    BTIF_STORAGE_KEY_AUTOPAIR_BLACKLIST_PARTIALNAME, value, &value_size))
1319        {
1320            token = strtok(value, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1321            while (token != NULL)
1322            {
1323                if (strstr(dev_name_str, token) != NULL)
1324                    return TRUE;
1325
1326                token = strtok(NULL, BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR);
1327            }
1328        }
1329    }
1330    if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1331                BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, value, &value_size))
1332    {
1333        if (strstr(value,bdstr) != NULL)
1334            return TRUE;
1335    }
1336    return FALSE;
1337}
1338
1339/*******************************************************************************
1340**
1341** Function         btif_storage_add_device_to_autopair_blacklist
1342**
1343** Description      BTIF storage API - Add a remote device to the auto pairing blacklist
1344**
1345** Returns          BT_STATUS_SUCCESS if the device is successfully added to the auto pair blacklist
1346**                  BT_STATUS_FAIL otherwise
1347**
1348*******************************************************************************/
1349bt_status_t btif_storage_add_device_to_autopair_blacklist(bt_bdaddr_t *remote_dev_addr)
1350{
1351    int ret;
1352    bdstr_t bdstr;
1353    char linebuf[BTIF_STORAGE_MAX_LINE_SZ+20];
1354    char input_value [20];
1355
1356    bd2str(remote_dev_addr, &bdstr);
1357    strncpy(input_value, (char*)bdstr, 20);
1358    strncat(input_value,BTIF_AUTO_PAIR_CONF_VALUE_SEPARATOR, 20);
1359    int line_size = sizeof(linebuf);
1360    if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1361                            BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf, &line_size))
1362    {
1363         /* Append this address to the dynamic List of BD address  */
1364        strncat (linebuf, input_value, BTIF_STORAGE_MAX_LINE_SZ);
1365    }
1366    else
1367    {
1368        strncpy( linebuf,input_value, BTIF_STORAGE_MAX_LINE_SZ);
1369    }
1370
1371    /* Write back the key value */
1372    ret = btif_config_set_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1373                        BTIF_STORAGE_KEY_AUTOPAIR_DYNAMIC_BLACKLIST_ADDR, linebuf);
1374
1375    return ret ? BT_STATUS_SUCCESS:BT_STATUS_FAIL;
1376}
1377
1378/*******************************************************************************
1379**
1380** Function         btif_storage_is_fixed_pin_zeros_keyboard
1381**
1382** Description      BTIF storage API - checks if this device has fixed PIN key device list
1383**
1384** Returns          TRUE   if the device is found in the fixed pin keyboard device list
1385**                  FALSE otherwise
1386**
1387*******************************************************************************/
1388BOOLEAN btif_storage_is_fixed_pin_zeros_keyboard(bt_bdaddr_t *remote_dev_addr)
1389{
1390    int ret;
1391    bdstr_t bdstr;
1392    char *dev_name_str;
1393    uint8_t i = 0;
1394    char linebuf[BTIF_STORAGE_MAX_LINE_SZ];
1395
1396    bd2str(remote_dev_addr, &bdstr);
1397
1398	/*consider on LAP part of BDA string*/
1399	bdstr[8] = '\0';
1400
1401    int line_size = sizeof(linebuf);
1402    if(btif_config_get_str("Local", BTIF_STORAGE_PATH_AUTOPAIR_BLACKLIST,
1403                            BTIF_STORAGE_KEY_AUTOPAIR_FIXPIN_KBLIST, linebuf, &line_size))
1404    {
1405        if (strcasestr(linebuf,bdstr) != NULL)
1406            return TRUE;
1407    }
1408    return FALSE;
1409
1410}
1411