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