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