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