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