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