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