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