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