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