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