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