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