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