1 /*
2  * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 
9 #include "bta/bta_api.h"
10 #include "btc/btc_config.h"
11 #include "device/bdaddr.h"
12 #include "btc/btc_ble_storage.h"
13 #include "bta/bta_gatts_co.h"
14 #include "btc/btc_util.h"
15 
16 #if (SMP_INCLUDED == TRUE)
17 
18 //the maximum number of bonded devices
19 #define BONED_DEVICES_MAX_COUNT (BTM_SEC_MAX_DEVICE_RECORDS)
20 
_btc_storage_save(void)21 static void _btc_storage_save(void)
22 {
23     uint16_t addr_section_count = 0;
24     bt_bdaddr_t bd_addr;
25 
26     const btc_config_section_iter_t *need_remove_iter = NULL;
27     const btc_config_section_iter_t *iter = btc_config_section_begin();
28 
29     while (iter != btc_config_section_end()) {
30         //store the next iter, if remove section, then will not loss the point
31 
32         const char *section = btc_config_section_name(iter);
33 
34         if (string_is_bdaddr(section) &&
35                 !btc_config_exist(section, BTC_BLE_STORAGE_DEV_TYPE_STR) &&
36                 !btc_config_exist(section, BTC_BLE_STORAGE_ADDR_TYPE_STR) &&
37                 !btc_config_exist(section, BTC_BLE_STORAGE_LINK_KEY_STR) &&
38                 !btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_PENC_STR) &&
39                 !btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_PID_STR) &&
40                 !btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_PCSRK_STR) &&
41                 !btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_LENC_STR) &&
42                 !btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_LCSRK_STR) &&
43                 !btc_config_exist(section, BTC_BLE_STORAGE_GATT_CL_SUPP_FEAT_STR) &&
44                 !btc_config_exist(section, BTC_BLE_STORAGE_GATT_DB_HASH_STR)) {
45             iter = btc_config_section_next(iter);
46             btc_config_remove_section(section);
47             continue;
48         }
49 
50         if (!string_is_bdaddr(section)) {
51             iter = btc_config_section_next(iter);
52             continue;
53         }
54 
55         if(addr_section_count == BONED_DEVICES_MAX_COUNT) {
56             need_remove_iter = iter;
57         }
58         addr_section_count ++;
59         iter = btc_config_section_next(iter);
60     }
61     /*exceeded the maximum number of bonded devices, delete them */
62     if (need_remove_iter) {
63         while(need_remove_iter != btc_config_section_end()) {
64             const char *need_remove_section = btc_config_section_name(need_remove_iter);
65             if (!string_is_bdaddr(need_remove_section)) {
66                 need_remove_iter = btc_config_section_next(need_remove_iter);
67                 continue;
68             }
69             need_remove_iter = btc_config_section_next(need_remove_iter);
70             //delete device info
71             string_to_bdaddr(need_remove_section, &bd_addr);
72             BTA_DmRemoveDevice(bd_addr.address, BT_TRANSPORT_LE);
73             BTA_DmRemoveDevice(bd_addr.address, BT_TRANSPORT_BR_EDR);
74             //delete config info
75             if (btc_config_remove_section(need_remove_section)) {
76                 // The need_remove_section has been freed
77                 BTIF_TRACE_WARNING("Exceeded the maximum number of bonded devices. Deleting the last device info: %02x:%02x:%02x:%02x:%02x:%02x",
78                                 bd_addr.address[0], bd_addr.address[1], bd_addr.address[2], bd_addr.address[3], bd_addr.address[4], bd_addr.address[5]);
79             }
80         }
81     }
82     btc_config_flush();
83 }
84 
btc_storage_save(void)85 void btc_storage_save(void)
86 {
87     btc_config_lock();
88     _btc_storage_save();
89     btc_config_unlock();
90 }
91 
92 #if (BLE_INCLUDED == TRUE)
_btc_storage_add_ble_bonding_key(bt_bdaddr_t * remote_bd_addr,char * key,uint8_t key_type,uint8_t key_length)93 static bt_status_t _btc_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
94                                             char *key,
95                                             uint8_t key_type,
96                                             uint8_t key_length)
97 {
98     bdstr_t bdstr;
99     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
100     const char* name;
101 
102     switch (key_type) {
103     case BTM_LE_KEY_PENC:
104         name = BTC_BLE_STORAGE_LE_KEY_PENC_STR;
105         break;
106     case BTM_LE_KEY_PID:
107         name = BTC_BLE_STORAGE_LE_KEY_PID_STR;
108         break;
109     case BTM_LE_KEY_PCSRK:
110         name = BTC_BLE_STORAGE_LE_KEY_PCSRK_STR;
111         break;
112     case BTM_LE_KEY_LENC:
113         name = BTC_BLE_STORAGE_LE_KEY_LENC_STR;
114         break;
115     case BTM_LE_KEY_LCSRK:
116         name = BTC_BLE_STORAGE_LE_KEY_LCSRK_STR;
117         break;
118     case BTM_LE_KEY_LID:
119         name = BTC_BLE_STORAGE_LE_KEY_LID_STR;
120         break;
121     default:
122         return BT_STATUS_FAIL;
123     }
124 
125     int ret = btc_config_set_bin(bdstr, name, (const uint8_t *)key, key_length);
126     _btc_storage_save();
127     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
128 }
129 
btc_storage_add_ble_bonding_key(bt_bdaddr_t * remote_bd_addr,char * key,uint8_t key_type,uint8_t key_length)130 bt_status_t btc_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
131                                             char *key,
132                                             uint8_t key_type,
133                                             uint8_t key_length)
134 {
135     bt_status_t ret;
136 
137     btc_config_lock();
138     ret = _btc_storage_add_ble_bonding_key(remote_bd_addr, key, key_type, key_length);
139     btc_config_unlock();
140 
141     return ret;
142 }
143 
144 /*******************************************************************************
145 **
146 ** Function         btc_storage_get_ble_bonding_key
147 **
148 ** Description
149 **
150 ** Returns       BT_STATUS_SUCCESS if the fetch was successful,
151 **                  BT_STATUS_FAIL otherwise
152 **
153 *******************************************************************************/
_btc_storage_get_ble_bonding_key(bt_bdaddr_t * remote_bd_addr,uint8_t key_type,char * key_value,int key_length)154 static bt_status_t _btc_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
155                                             uint8_t key_type,
156                                             char *key_value,
157                                             int key_length)
158 {
159     bdstr_t bdstr;
160     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
161     const char* name;
162     switch (key_type) {
163     case BTM_LE_KEY_PENC:
164         name = BTC_BLE_STORAGE_LE_KEY_PENC_STR;
165         break;
166     case BTM_LE_KEY_PID:
167         name = BTC_BLE_STORAGE_LE_KEY_PID_STR;
168         break;
169     case BTM_LE_KEY_PCSRK:
170         name = BTC_BLE_STORAGE_LE_KEY_PCSRK_STR;
171         break;
172     case BTM_LE_KEY_LENC:
173         name = BTC_BLE_STORAGE_LE_KEY_LENC_STR;
174         break;
175     case BTM_LE_KEY_LCSRK:
176         name = BTC_BLE_STORAGE_LE_KEY_LCSRK_STR;
177         break;
178     case BTM_LE_KEY_LID:
179         name =  BTC_BLE_STORAGE_LE_KEY_LID_STR;
180     default:
181         return BT_STATUS_FAIL;
182     }
183     size_t length = key_length;
184     int ret = btc_config_get_bin(bdstr, name, (uint8_t *)key_value, &length);
185     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
186 
187 }
188 
btc_storage_get_ble_bonding_key(bt_bdaddr_t * remote_bd_addr,uint8_t key_type,char * key_value,int key_length)189 bt_status_t btc_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
190                                             uint8_t key_type,
191                                             char *key_value,
192                                             int key_length)
193 {
194     bt_status_t ret;
195 
196     btc_config_lock();
197     ret = _btc_storage_get_ble_bonding_key(remote_bd_addr, key_type, key_value, key_length);
198     btc_config_unlock();
199 
200     return ret;
201 }
202 
_btc_storage_remove_all_ble_keys(const char * name)203 static bt_status_t _btc_storage_remove_all_ble_keys(const char *name)
204 {
205     int ret = 0;
206 
207     if (name == NULL) {
208         return BT_STATUS_FAIL;
209     }
210 
211     if (btc_config_exist(name, BTC_BLE_STORAGE_ADDR_TYPE_STR)) {
212         ret |= btc_config_remove(name, BTC_BLE_STORAGE_ADDR_TYPE_STR);
213     }
214     if (btc_config_exist(name, BTC_BLE_STORAGE_LE_KEY_PENC_STR)) {
215         ret |= btc_config_remove(name, BTC_BLE_STORAGE_LE_KEY_PENC_STR);
216     }
217     if (btc_config_exist(name, BTC_BLE_STORAGE_LE_KEY_PID_STR)) {
218         ret |= btc_config_remove(name, BTC_BLE_STORAGE_LE_KEY_PID_STR);
219     }
220     if (btc_config_exist(name, BTC_BLE_STORAGE_LE_KEY_PCSRK_STR)) {
221         ret |= btc_config_remove(name, BTC_BLE_STORAGE_LE_KEY_PCSRK_STR);
222     }
223     if (btc_config_exist(name, BTC_BLE_STORAGE_LE_KEY_LENC_STR)) {
224         ret |= btc_config_remove(name, BTC_BLE_STORAGE_LE_KEY_LENC_STR);
225     }
226     if (btc_config_exist(name, BTC_BLE_STORAGE_LE_KEY_LCSRK_STR)) {
227         ret |= btc_config_remove(name, BTC_BLE_STORAGE_LE_KEY_LCSRK_STR);
228     }
229     if (btc_config_exist(name, BTC_BLE_STORAGE_LE_KEY_LID_STR)) {
230         ret |= btc_config_remove(name, BTC_BLE_STORAGE_LE_KEY_LID_STR);
231     }
232 
233     return ret;
234 }
235 
btc_storage_remove_unused_sections(uint8_t * cur_addr,tBTM_LE_PID_KEYS * del_pid_key)236 void btc_storage_remove_unused_sections(uint8_t *cur_addr, tBTM_LE_PID_KEYS *del_pid_key)
237 {
238     bt_bdaddr_t bd_addr;
239     uint32_t device_type = 0;
240 
241     if (del_pid_key == NULL) {
242         return;
243     }
244 
245     btc_config_lock();
246 
247     const btc_config_section_iter_t *iter = btc_config_section_begin();
248 
249     while (iter != btc_config_section_end()) {
250         //store the next iter, if remove section, then will not loss the point
251 
252         const char *section = btc_config_section_name(iter);
253         if (string_is_bdaddr(section) &&
254                 !btc_config_exist(section, BTC_BLE_STORAGE_DEV_TYPE_STR) &&
255                 !btc_config_exist(section, BTC_BLE_STORAGE_ADDR_TYPE_STR) &&
256                 !btc_config_exist(section, BTC_BLE_STORAGE_LINK_KEY_STR) &&
257                 !btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_PENC_STR) &&
258                 !btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_PID_STR) &&
259                 !btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_PCSRK_STR) &&
260                 !btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_LENC_STR) &&
261                 !btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_LCSRK_STR)) {
262             iter = btc_config_section_next(iter);
263             btc_config_remove_section(section);
264             continue;
265         }
266 
267         if (!string_is_bdaddr(section) ||
268             !btc_config_get_int(section, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&device_type) ||
269             ((device_type & BT_DEVICE_TYPE_BLE) != BT_DEVICE_TYPE_BLE)) {
270             iter = btc_config_section_next(iter);
271             continue;
272         }
273 
274         string_to_bdaddr(section, &bd_addr);
275 
276         char buffer[sizeof(tBTM_LE_KEY_VALUE)] = {0};
277 
278         if (_btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PID, buffer, sizeof(tBTM_LE_PID_KEYS)) == BT_STATUS_SUCCESS) {
279 
280             tBTM_LE_PID_KEYS *pid_key = (tBTM_LE_PID_KEYS *) buffer;
281 
282             iter = btc_config_section_next(iter);
283 
284             if (memcmp(del_pid_key->static_addr, pid_key->static_addr, 6) == 0 && memcmp(cur_addr, bd_addr.address, 6) != 0 &&  del_pid_key->addr_type == pid_key->addr_type) {
285                 if (device_type == BT_DEVICE_TYPE_DUMO) {
286                     btc_config_set_int(section, BTC_BLE_STORAGE_DEV_TYPE_STR, BT_DEVICE_TYPE_BREDR);
287                     _btc_storage_remove_all_ble_keys(section);
288                 } else {
289                     //delete unused sections
290                     BTIF_TRACE_DEBUG("delete section %s\n", section);
291                     btc_config_remove_section(section);
292                 }
293             }
294         } else {
295             iter = btc_config_section_next(iter);
296         }
297     }
298 
299     btc_config_unlock();
300 
301 }
302 
btc_storage_delete_duplicate_ble_devices(void)303 void btc_storage_delete_duplicate_ble_devices(void)
304 {
305     bt_bdaddr_t bd_addr;
306     char buffer[sizeof(tBTM_LE_KEY_VALUE)] = {0};
307     char temp_buffer[sizeof(tBTM_LE_KEY_VALUE)] = {0};
308     tBTM_LE_PID_KEYS *pid_key;
309     tBTM_LE_PID_KEYS *temp_pid_key;
310     uint32_t device_type = 0;
311 
312     bt_bdaddr_t temp_bd_addr;
313 
314     btc_config_lock();
315     for (const btc_config_section_iter_t *iter = btc_config_section_begin(); iter != btc_config_section_end();
316             iter = btc_config_section_next(iter)) {
317         const char *name = btc_config_section_name(iter);
318 
319         if (!string_is_bdaddr(name) ||
320             !btc_config_get_int(name, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&device_type) ||
321             ((device_type & BT_DEVICE_TYPE_BLE) != BT_DEVICE_TYPE_BLE)) {
322             continue;
323         }
324 
325         string_to_bdaddr(name, &bd_addr);
326         if (_btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PID, buffer, sizeof(tBTM_LE_PID_KEYS)) == BT_STATUS_SUCCESS)
327         {
328             pid_key = (tBTM_LE_PID_KEYS *) buffer;
329 
330             const btc_config_section_iter_t *temp_iter = btc_config_section_next(iter);
331             while (temp_iter != NULL)
332             {
333                 const char *temp_name = btc_config_section_name(temp_iter);
334                 if (!string_is_bdaddr(temp_name) || !btc_config_get_int(temp_name, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&device_type) ||
335                     ((device_type & BT_DEVICE_TYPE_BLE) != BT_DEVICE_TYPE_BLE)) {
336                     temp_iter = btc_config_section_next(temp_iter);
337                     continue;
338                 }
339                 string_to_bdaddr(temp_name, &temp_bd_addr);
340                 if (_btc_storage_get_ble_bonding_key(&temp_bd_addr, BTM_LE_KEY_PID, temp_buffer, sizeof(tBTM_LE_PID_KEYS)) == BT_STATUS_SUCCESS)
341                 {
342                     temp_pid_key = (tBTM_LE_PID_KEYS *) temp_buffer;
343                     if (memcmp(pid_key->static_addr, temp_pid_key->static_addr, 6) == 0 && pid_key->addr_type == temp_pid_key->addr_type) {
344                         const char *temp_name = btc_config_section_name(temp_iter);
345                         temp_iter = btc_config_section_next(temp_iter);
346                         if (device_type == BT_DEVICE_TYPE_DUMO) {
347                             btc_config_set_int(temp_name, BTC_BLE_STORAGE_DEV_TYPE_STR, BT_DEVICE_TYPE_BREDR);
348                             _btc_storage_remove_all_ble_keys(temp_name);
349                         } else {
350                             BTC_TRACE_DEBUG("delete %s\n", temp_name);
351                             btc_config_remove_section(temp_name);
352                         }
353                     } else {
354                         temp_iter = btc_config_section_next(temp_iter);
355                     }
356                 } else {
357                     temp_iter = btc_config_section_next(temp_iter);
358                 }
359             }
360 
361         }
362     }
363     btc_config_unlock();
364 }
365 
366 
367 /*******************************************************************************
368 **
369 ** Function         btc_storage_remove_ble_bonding_keys
370 **
371 ** Description      btc storage API - Deletes the bonded device from NVRAM
372 **
373 ** Returns          BT_STATUS_SUCCESS if the deletion was successful,
374 **                  BT_STATUS_FAIL otherwise
375 **
376 *******************************************************************************/
_btc_storage_remove_ble_bonding_keys(bt_bdaddr_t * remote_bd_addr)377 static bt_status_t _btc_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr)
378 {
379     int ret = 0;
380     bdstr_t bdstr;
381     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
382 
383     BTIF_TRACE_DEBUG(" %s in bd addr:%s",__FUNCTION__, bdstr);
384 
385     ret = _btc_storage_remove_all_ble_keys(bdstr);
386 
387     //here don't remove section, because config_save will check it
388     _btc_storage_save();
389     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
390 }
391 
btc_storage_remove_ble_bonding_keys(bt_bdaddr_t * remote_bd_addr)392 bt_status_t btc_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr)
393 {
394     bt_status_t ret;
395 
396     btc_config_lock();
397     ret = _btc_storage_remove_ble_bonding_keys(remote_bd_addr);
398     btc_config_unlock();
399 
400     return ret;
401 }
402 
403 /*******************************************************************************
404 **
405 ** Function         btc_storage_add_ble_local_key
406 **
407 ** Description      BTIF storage API - Adds the ble key to NVRAM
408 **
409 ** Returns          BT_STATUS_SUCCESS if the store was successful,
410 **                  BT_STATUS_FAIL otherwise
411 **
412 *******************************************************************************/
_btc_storage_add_ble_local_key(char * key,uint8_t key_type,uint8_t key_length)413 static bt_status_t _btc_storage_add_ble_local_key(char *key,
414                                           uint8_t key_type,
415                                           uint8_t key_length)
416 {
417     const char* name;
418     switch (key_type) {
419     case BTC_LE_LOCAL_KEY_IR:
420         name = BTC_BLE_STORAGE_LE_LOCAL_KEY_IR_STR;
421         break;
422     case BTC_LE_LOCAL_KEY_IRK:
423         name = BTC_BLE_STORAGE_LE_LOCAL_KEY_IRK_STR;
424         break;
425     case BTC_LE_LOCAL_KEY_DHK:
426         name = BTC_BLE_STORAGE_LE_LOCAL_KEY_DHK_STR;
427         break;
428     case BTC_LE_LOCAL_KEY_ER:
429         name = BTC_BLE_STORAGE_LE_LOCAL_KEY_ER_STR;
430         break;
431     default:
432         return BT_STATUS_FAIL;
433     }
434 
435     int ret = btc_config_set_bin(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, name, (const uint8_t *)key, key_length);
436     _btc_storage_save();
437     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
438 }
439 
btc_storage_add_ble_local_key(char * key,uint8_t key_type,uint8_t key_length)440 bt_status_t btc_storage_add_ble_local_key(char *key,
441                                           uint8_t key_type,
442                                           uint8_t key_length)
443 {
444     bt_status_t ret;
445 
446     btc_config_lock();
447     ret = _btc_storage_add_ble_local_key(key, key_type, key_length);
448     btc_config_unlock();
449 
450     return ret;
451 }
452 
453 /*******************************************************************************
454 **
455 ** Function         btc_storage_get_ble_local_key
456 **
457 ** Description
458 **
459 ** Returns          BT_STATUS_SUCCESS if the fetch was successful,
460 **                  BT_STATUS_FAIL otherwise
461 **
462 *******************************************************************************/
_btc_storage_get_ble_local_key(uint8_t key_type,char * key_value,int key_length)463 static bt_status_t _btc_storage_get_ble_local_key(uint8_t key_type,
464                                           char *key_value,
465                                           int key_length)
466 {
467     const char* name;
468     switch (key_type) {
469     case BTC_LE_LOCAL_KEY_IR:
470         name = BTC_BLE_STORAGE_LE_LOCAL_KEY_IR_STR;
471         break;
472     case BTC_LE_LOCAL_KEY_IRK:
473         name = BTC_BLE_STORAGE_LE_LOCAL_KEY_IRK_STR;
474         break;
475     case BTC_LE_LOCAL_KEY_DHK:
476         name = BTC_BLE_STORAGE_LE_LOCAL_KEY_DHK_STR;
477         break;
478     case BTC_LE_LOCAL_KEY_ER:
479         name = BTC_BLE_STORAGE_LE_LOCAL_KEY_ER_STR;
480         break;
481     default:
482         return BT_STATUS_FAIL;
483     }
484     size_t length = key_length;
485 
486     int ret = btc_config_get_bin(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, name, (uint8_t *)key_value, &length);
487 
488     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
489 }
490 
btc_storage_get_ble_local_key(uint8_t key_type,char * key_value,int key_length)491 bt_status_t btc_storage_get_ble_local_key(uint8_t key_type,
492                                           char *key_value,
493                                           int key_length)
494 {
495     bt_status_t ret;
496 
497     btc_config_lock();
498     ret = _btc_storage_get_ble_local_key(key_type, key_value, key_length);
499     btc_config_unlock();
500 
501     return ret;
502 }
503 
504 /*******************************************************************************
505 **
506 ** Function         btc_storage_remove_ble_local_keys
507 **
508 ** Description      BTC storage API - Deletes the bonded device from NVRAM
509 **
510 ** Returns          BT_STATUS_SUCCESS if the deletion was successful,
511 **                  BT_STATUS_FAIL otherwise
512 **
513 *******************************************************************************/
_btc_storage_remove_ble_local_keys(void)514 static bt_status_t _btc_storage_remove_ble_local_keys(void)
515 {
516     int ret = 1;
517 
518     if (btc_config_exist(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, BTC_BLE_STORAGE_LE_LOCAL_KEY_IR_STR)) {
519         ret &= btc_config_remove(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, BTC_BLE_STORAGE_LE_LOCAL_KEY_IR_STR);
520     }
521     if (btc_config_exist(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, BTC_BLE_STORAGE_LE_LOCAL_KEY_IRK_STR)) {
522         ret &= btc_config_remove(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, BTC_BLE_STORAGE_LE_LOCAL_KEY_IRK_STR);
523     }
524     if (btc_config_exist(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, BTC_BLE_STORAGE_LE_LOCAL_KEY_DHK_STR)) {
525         ret &= btc_config_remove(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, BTC_BLE_STORAGE_LE_LOCAL_KEY_DHK_STR);
526     }
527     if (btc_config_exist(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, BTC_BLE_STORAGE_LE_LOCAL_KEY_ER_STR)) {
528         ret &= btc_config_remove(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, BTC_BLE_STORAGE_LE_LOCAL_KEY_ER_STR);
529     }
530     _btc_storage_save();
531 
532     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
533 }
534 
btc_storage_remove_ble_local_keys(void)535 bt_status_t btc_storage_remove_ble_local_keys(void)
536 {
537     bt_status_t ret;
538 
539     btc_config_lock();
540     ret = _btc_storage_remove_ble_local_keys();
541     btc_config_unlock();
542 
543     return ret;
544 }
545 
_btc_storage_compare_address_key_value(bt_bdaddr_t * remote_bd_addr,uint8_t key_type,void * key_value,int key_length)546 bool _btc_storage_compare_address_key_value(bt_bdaddr_t *remote_bd_addr,
547                                                    uint8_t key_type, void *key_value, int key_length)
548 {
549     bdstr_t bdstr;
550     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
551     const char *key_type_str;
552     switch (key_type) {
553     case BTM_LE_KEY_PENC:
554         key_type_str = BTC_BLE_STORAGE_LE_KEY_PENC_STR;
555         break;
556     case BTM_LE_KEY_PID:
557         key_type_str = BTC_BLE_STORAGE_LE_KEY_PID_STR;
558         break;
559     case BTM_LE_KEY_PCSRK:
560         key_type_str = BTC_BLE_STORAGE_LE_KEY_PCSRK_STR;
561         break;
562     case BTM_LE_KEY_LENC:
563         key_type_str = BTC_BLE_STORAGE_LE_KEY_LENC_STR;
564         break;
565     case BTM_LE_KEY_LCSRK:
566         key_type_str = BTC_BLE_STORAGE_LE_KEY_LCSRK_STR;
567         break;
568     case BTM_LE_KEY_LID:
569         key_type_str =  BTC_BLE_STORAGE_LE_KEY_LID_STR;
570     default:
571         return false;
572     }
573 
574     return btc_compare_address_key_value(bdstr, key_type_str, key_value, key_length);
575 }
576 
btc_storage_compare_address_key_value(bt_bdaddr_t * remote_bd_addr,uint8_t key_type,void * key_value,int key_length)577 bool btc_storage_compare_address_key_value(bt_bdaddr_t *remote_bd_addr,
578                                                    uint8_t key_type, void *key_value, int key_length)
579 {
580     bool ret;
581 
582     btc_config_lock();
583     ret = _btc_storage_compare_address_key_value(remote_bd_addr, key_type, key_value, key_length);
584     btc_config_unlock();
585 
586     return ret;
587 }
588 
_btc_storage_set_ble_dev_type(bt_bdaddr_t * bd_addr,bool flush)589 static bt_status_t _btc_storage_set_ble_dev_type(bt_bdaddr_t *bd_addr, bool flush)
590 {
591     bool ret = 1;
592     bdstr_t bdstr;
593     uint32_t dev_type = 0;
594 
595     bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr));
596 
597     btc_config_get_int(bdstr, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&dev_type);
598     ret = btc_config_set_int(bdstr, BTC_BLE_STORAGE_DEV_TYPE_STR, BT_DEVICE_TYPE_BLE|dev_type);
599     if (ret == false) {
600         return BT_STATUS_FAIL;
601     }
602 
603     if (flush) {
604         _btc_storage_save();
605     }
606 
607     return BT_STATUS_SUCCESS;
608 }
609 
btc_storage_set_ble_dev_type(bt_bdaddr_t * bd_addr,bool flush)610 bt_status_t btc_storage_set_ble_dev_type(bt_bdaddr_t *bd_addr, bool flush)
611 {
612     bt_status_t ret;
613 
614     btc_config_lock();
615     ret = _btc_storage_set_ble_dev_type(bd_addr, flush);
616     btc_config_unlock();
617 
618     return ret;
619 }
620 
_btc_storage_get_ble_dev_type(bt_bdaddr_t * bd_addr)621 static bool _btc_storage_get_ble_dev_type(bt_bdaddr_t *bd_addr)
622 {
623     bool ret = 1;
624     bdstr_t bdstr;
625     uint32_t dev_type = 0;
626 
627     bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr));
628 
629     BTIF_TRACE_DEBUG(" %s in bd addr:%s",__FUNCTION__, bdstr);
630 
631     ret = btc_config_get_int(bdstr, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&dev_type);
632     if (ret == false) {
633         return false;
634     }
635 
636     return (dev_type & BT_DEVICE_TYPE_BLE);
637 }
638 
btc_storage_get_ble_dev_type(bt_bdaddr_t * bd_addr)639 bool btc_storage_get_ble_dev_type(bt_bdaddr_t *bd_addr)
640 {
641     bt_status_t ret;
642 
643     btc_config_lock();
644     ret = _btc_storage_get_ble_dev_type(bd_addr);
645     btc_config_unlock();
646 
647     return ret;
648 }
649 
650 
_btc_storage_remove_ble_dev_type(bt_bdaddr_t * remote_bd_addr,bool flush)651 static bt_status_t _btc_storage_remove_ble_dev_type(bt_bdaddr_t *remote_bd_addr, bool flush)
652 {
653     bool ret = true;
654     bdstr_t bdstr;
655     uint32_t dev_type = 0;
656 
657     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
658 
659     BTIF_TRACE_DEBUG(" %s in bd addr:%s",__FUNCTION__, bdstr);
660 
661     ret = btc_config_get_int(bdstr, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&dev_type);
662     if (ret == false) {
663         //cannot find the key, just return SUCCESS, indicate already removed
664         return BT_STATUS_SUCCESS;
665     }
666 
667     if (dev_type == BT_DEVICE_TYPE_DUMO) {
668         ret = btc_config_set_int(bdstr, BTC_BLE_STORAGE_DEV_TYPE_STR, BT_DEVICE_TYPE_BREDR);
669     } else if (dev_type == BT_DEVICE_TYPE_BLE) {
670         ret = btc_config_remove(bdstr, BTC_BLE_STORAGE_DEV_TYPE_STR);
671     }
672 
673     if (ret == false) {
674         return BT_STATUS_FAIL;
675     }
676 
677     if (flush) {
678         _btc_storage_save();
679     }
680 
681     return BT_STATUS_SUCCESS;
682 }
683 
btc_storage_remove_ble_dev_type(bt_bdaddr_t * remote_bd_addr,bool flush)684 bt_status_t btc_storage_remove_ble_dev_type(bt_bdaddr_t *remote_bd_addr, bool flush)
685 {
686     bt_status_t ret;
687 
688     btc_config_lock();
689     ret = _btc_storage_remove_ble_dev_type(remote_bd_addr, flush);
690     btc_config_unlock();
691 
692     return ret;
693 }
694 #endif  ///BLE_INCLUDED == TRUE
695 
_btc_storage_set_ble_dev_auth_mode(bt_bdaddr_t * remote_bd_addr,uint8_t auth_mode,bool flush)696 static bt_status_t _btc_storage_set_ble_dev_auth_mode(bt_bdaddr_t *remote_bd_addr, uint8_t auth_mode, bool flush)
697 {
698     int ret;
699     bdstr_t bdstr;
700 
701     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr_t));
702     ret = btc_config_set_int(bdstr, BTC_BLE_STORAGE_LE_AUTH_MODE_STR, (int)auth_mode);
703     if (ret == false) {
704         return BT_STATUS_FAIL;
705     }
706 
707     if (flush) {
708         _btc_storage_save();
709     }
710 
711     return BT_STATUS_SUCCESS;
712 }
713 
btc_storage_set_ble_dev_auth_mode(bt_bdaddr_t * remote_bd_addr,uint8_t auth_mode,bool flush)714 bt_status_t btc_storage_set_ble_dev_auth_mode(bt_bdaddr_t *remote_bd_addr, uint8_t auth_mode, bool flush)
715 {
716     bt_status_t ret;
717 
718     btc_config_lock();
719     ret = _btc_storage_set_ble_dev_auth_mode(remote_bd_addr, auth_mode, flush);
720     btc_config_unlock();
721 
722     return ret;
723 }
724 
_btc_storage_get_ble_dev_auth_mode(bt_bdaddr_t * remote_bd_addr,int * auth_mode)725 static bt_status_t _btc_storage_get_ble_dev_auth_mode(bt_bdaddr_t *remote_bd_addr, int* auth_mode)
726 {
727     bdstr_t bdstr;
728     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
729     int ret = btc_config_get_int(bdstr, BTC_BLE_STORAGE_LE_AUTH_MODE_STR, auth_mode);
730     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
731 }
732 
btc_storage_get_ble_dev_auth_mode(bt_bdaddr_t * remote_bd_addr,int * auth_mode)733 bt_status_t btc_storage_get_ble_dev_auth_mode(bt_bdaddr_t *remote_bd_addr, int* auth_mode)
734 {
735     bt_status_t ret;
736 
737     btc_config_lock();
738     ret = _btc_storage_get_ble_dev_auth_mode(remote_bd_addr, auth_mode);
739     btc_config_unlock();
740 
741     return ret;
742 }
743 
_btc_storage_remove_ble_dev_auth_mode(bt_bdaddr_t * remote_bd_addr,bool flush)744 static bt_status_t _btc_storage_remove_ble_dev_auth_mode(bt_bdaddr_t *remote_bd_addr, bool flush)
745 {
746     bool ret = true;
747     bdstr_t bdstr;
748     uint32_t auth_mode = 0;
749 
750     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
751 
752     ret = btc_config_get_int(bdstr, BTC_BLE_STORAGE_LE_AUTH_MODE_STR, (int *)&auth_mode);
753     if (ret == false) {
754         //cannot find the key, just return SUCCESS, indicate already removed
755         return BT_STATUS_SUCCESS;
756     }
757 
758     ret = btc_config_remove(bdstr, BTC_BLE_STORAGE_LE_AUTH_MODE_STR);
759     if (ret == false) {
760         return BT_STATUS_FAIL;
761     }
762 
763     if (flush) {
764         _btc_storage_save();
765     }
766 
767     return  BT_STATUS_SUCCESS;
768 }
769 
btc_storage_remove_ble_dev_auth_mode(bt_bdaddr_t * remote_bd_addr,bool flush)770 bt_status_t btc_storage_remove_ble_dev_auth_mode(bt_bdaddr_t *remote_bd_addr, bool flush)
771 {
772     bt_status_t ret;
773 
774     btc_config_lock();
775     ret = _btc_storage_remove_ble_dev_auth_mode(remote_bd_addr, flush);
776     btc_config_unlock();
777 
778     return ret;
779 }
780 
_btc_storage_set_remote_addr_type(bt_bdaddr_t * remote_bd_addr,uint8_t addr_type,bool flush)781 static bt_status_t _btc_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr, uint8_t addr_type, bool flush)
782 {
783     int ret;
784     bdstr_t bdstr;
785 
786     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr_t));
787     ret = btc_config_set_int(bdstr, BTC_BLE_STORAGE_ADDR_TYPE_STR, (int)addr_type);
788     if (ret == false) {
789         return BT_STATUS_FAIL;
790     }
791 
792     if (flush) {
793         _btc_storage_save();
794     }
795 
796     return BT_STATUS_SUCCESS;
797 }
798 
btc_storage_set_remote_addr_type(bt_bdaddr_t * remote_bd_addr,uint8_t addr_type,bool flush)799 bt_status_t btc_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr, uint8_t addr_type, bool flush)
800 {
801     bt_status_t ret;
802 
803     btc_config_lock();
804     ret = _btc_storage_set_remote_addr_type(remote_bd_addr, addr_type, flush);
805     btc_config_unlock();
806 
807     return ret;
808 }
809 
_btc_storage_remove_remote_addr_type(bt_bdaddr_t * remote_bd_addr,bool flush)810 static bt_status_t _btc_storage_remove_remote_addr_type(bt_bdaddr_t *remote_bd_addr, bool flush)
811 {
812     bool ret = true;
813     bdstr_t bdstr;
814     uint32_t dev_type = 0;
815 
816     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
817 
818     ret = btc_config_get_int(bdstr, BTC_BLE_STORAGE_ADDR_TYPE_STR, (int *)&dev_type);
819     if (ret == false) {
820         //cannot find the key, just return SUCCESS, indicate already removed
821         return BT_STATUS_SUCCESS;
822     }
823 
824     ret = btc_config_remove(bdstr, BTC_BLE_STORAGE_ADDR_TYPE_STR);
825     if (ret == false) {
826         return BT_STATUS_FAIL;
827     }
828 
829     if (flush) {
830         _btc_storage_save();
831     }
832 
833     return  BT_STATUS_SUCCESS;
834 }
835 
btc_storage_remove_remote_addr_type(bt_bdaddr_t * remote_bd_addr,bool flush)836 bt_status_t btc_storage_remove_remote_addr_type(bt_bdaddr_t *remote_bd_addr, bool flush)
837 {
838     bt_status_t ret;
839 
840     btc_config_lock();
841     ret = _btc_storage_remove_remote_addr_type(remote_bd_addr, flush);
842     btc_config_unlock();
843 
844     return ret;
845 }
846 
_btc_storage_get_remote_addr_type(bt_bdaddr_t * remote_bd_addr,int * addr_type)847 static bt_status_t _btc_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
848                                              int*addr_type)
849 {
850     bdstr_t bdstr;
851     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
852     int ret = btc_config_get_int(bdstr, BTC_BLE_STORAGE_ADDR_TYPE_STR, addr_type);
853     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
854 }
855 
btc_storage_get_remote_addr_type(bt_bdaddr_t * remote_bd_addr,int * addr_type)856 bt_status_t btc_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
857                                              int*addr_type)
858 {
859     bt_status_t ret;
860 
861     btc_config_lock();
862     ret = _btc_storage_get_remote_addr_type(remote_bd_addr, addr_type);
863     btc_config_unlock();
864 
865     return ret;
866 }
867 
868 #if (BLE_INCLUDED == TRUE)
_btc_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)869 static void _btc_read_le_key(const uint8_t key_type, const size_t key_len, bt_bdaddr_t bd_addr,
870                  const uint8_t addr_type, const bool add_key, bool *device_added, bool *key_found)
871 {
872     assert(device_added);
873     assert(key_found);
874 
875     char buffer[100];
876     memset(buffer, 0, sizeof(buffer));
877 
878     bt_status_t ret = _btc_storage_get_ble_bonding_key(&bd_addr, key_type, buffer, key_len);
879 
880     if (ret == BT_STATUS_SUCCESS) {
881         if (add_key) {
882             BD_ADDR bta_bd_addr;
883             bdcpy(bta_bd_addr, bd_addr.address);
884 
885             if (!*device_added) {
886                 int auth_mode = 0;
887                 if(_btc_storage_get_ble_dev_auth_mode(&bd_addr, &auth_mode) != BT_STATUS_SUCCESS) {
888                     BTC_TRACE_WARNING("%s Failed to get auth mode from flash, please erase flash and download the firmware again", __func__);
889                 }
890                 BTA_DmAddBleDevice(bta_bd_addr, addr_type, auth_mode, BT_DEVICE_TYPE_BLE);
891                 *device_added = true;
892             }
893 
894 #if (!CONFIG_BT_STACK_NO_LOG)
895             char bd_str[20] = {0};
896 #endif
897             BTC_TRACE_DEBUG("%s() Adding key type %d for %s", __func__,
898                 key_type, bdaddr_to_string(&bd_addr, bd_str, sizeof(bd_str)));
899             BTA_DmAddBleKey(bta_bd_addr, (tBTA_LE_KEY_VALUE *)buffer, key_type);
900         }
901 
902         *key_found = true;
903     }
904 }
_btc_storage_in_fetch_bonded_ble_device(const char * remote_bd_addr,int add)905 bt_status_t _btc_storage_in_fetch_bonded_ble_device(const char *remote_bd_addr, int add)
906 {
907     uint32_t device_type;
908     int addr_type;
909     bt_bdaddr_t bd_addr;
910     BD_ADDR bta_bd_addr;
911     bool device_added = false;
912     bool key_found = false;
913 
914     if (!btc_config_get_int(remote_bd_addr, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&device_type)) {
915         BTC_TRACE_ERROR("%s, device_type = %x", __func__, device_type);
916         return BT_STATUS_FAIL;
917     }
918 
919     string_to_bdaddr(remote_bd_addr, &bd_addr);
920     bdcpy(bta_bd_addr, bd_addr.address);
921 
922     if (_btc_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS) {
923         addr_type = BLE_ADDR_PUBLIC;
924         _btc_storage_set_remote_addr_type(&bd_addr, BLE_ADDR_PUBLIC, true);
925     }
926 
927     _btc_read_le_key(BTM_LE_KEY_PENC, sizeof(tBTM_LE_PENC_KEYS),
928                     bd_addr, addr_type, add, &device_added, &key_found);
929 
930     _btc_read_le_key(BTM_LE_KEY_PID, sizeof(tBTM_LE_PID_KEYS),
931                     bd_addr, addr_type, add, &device_added, &key_found);
932 
933     _btc_read_le_key(BTM_LE_KEY_LID, sizeof(tBTM_LE_PID_KEYS),
934                     bd_addr, addr_type, add, &device_added, &key_found);
935 
936     _btc_read_le_key(BTM_LE_KEY_PCSRK, sizeof(tBTM_LE_PCSRK_KEYS),
937                     bd_addr, addr_type, add, &device_added, &key_found);
938 
939     _btc_read_le_key(BTM_LE_KEY_LENC, sizeof(tBTM_LE_LENC_KEYS),
940                     bd_addr, addr_type, add, &device_added, &key_found);
941 
942     _btc_read_le_key(BTM_LE_KEY_LCSRK, sizeof(tBTM_LE_LCSRK_KEYS),
943                     bd_addr, addr_type, add, &device_added, &key_found);
944 
945     if (key_found) {
946         return BT_STATUS_SUCCESS;
947     } else {
948         BTC_TRACE_DEBUG("Remote device:%s, no link key or ble key found", remote_bd_addr);
949     }
950 
951     return BT_STATUS_FAIL;
952 }
953 
btc_storage_get_bonded_ble_devices_list(esp_ble_bond_dev_t * bond_dev,int dev_num)954 bt_status_t btc_storage_get_bonded_ble_devices_list(esp_ble_bond_dev_t *bond_dev, int dev_num)
955 {
956     bt_bdaddr_t bd_addr;
957     int addr_t;
958     char buffer[sizeof(tBTM_LE_KEY_VALUE)] = {0};
959 
960     btc_config_lock();
961     for (const btc_config_section_iter_t *iter = btc_config_section_begin(); iter != btc_config_section_end();
962             iter = btc_config_section_next(iter)) {
963 
964         if (dev_num-- <= 0) {
965             break;
966         }
967         uint32_t device_type = 0;
968         const char *name = btc_config_section_name(iter);
969 
970         if (!string_is_bdaddr(name) ||
971                 !btc_config_get_int(name, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&device_type) ||
972                 !(device_type & BT_DEVICE_TYPE_BLE)) {
973             dev_num ++;
974             continue;
975         }
976 
977         string_to_bdaddr(name, &bd_addr);
978         memcpy(bond_dev->bd_addr, bd_addr.address, sizeof(bt_bdaddr_t));
979         //get address type
980         if (_btc_storage_get_remote_addr_type((bt_bdaddr_t *)bond_dev->bd_addr, &addr_t) == BT_STATUS_SUCCESS) {
981             bond_dev->bd_addr_type = (uint8_t) addr_t;
982         } else {
983             // Set an invalid address type
984             bond_dev->bd_addr_type = 0xFF;
985             BTC_TRACE_ERROR("%s, %s get address type fail", __func__, name);
986         }
987         //resolve the peer device long term key
988         if (_btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PENC, buffer, sizeof(tBTM_LE_PENC_KEYS)) == BT_STATUS_SUCCESS) {
989             bond_dev->bond_key.key_mask |= ESP_BLE_ENC_KEY_MASK;
990             memcpy(&bond_dev->bond_key.penc_key, buffer, sizeof(tBTM_LE_PENC_KEYS));
991         }
992         //resolve the peer device csrk
993         if (_btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PCSRK, buffer, sizeof(tBTM_LE_PCSRK_KEYS)) == BT_STATUS_SUCCESS) {
994             bond_dev->bond_key.key_mask |= ESP_BLE_CSR_KEY_MASK;
995             memcpy(&bond_dev->bond_key.pcsrk_key, buffer, sizeof(tBTM_LE_PCSRK_KEYS));
996         }
997         //resolve the peer device irk
998         if (_btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PID, buffer, sizeof(tBTM_LE_PID_KEYS)) == BT_STATUS_SUCCESS) {
999             bond_dev->bond_key.key_mask |= ESP_BLE_ID_KEY_MASK;
1000             tBTM_LE_PID_KEYS *pid_key = (tBTM_LE_PID_KEYS *) buffer;
1001             //Note: The memory size of the pid_key.addr_type in bond_key is different from that of (tBTM_LE_PID_KEYS) *pid_key.
1002             memcpy(&bond_dev->bond_key.pid_key.irk, pid_key->irk, ESP_BT_OCTET16_LEN);
1003             bond_dev->bond_key.pid_key.addr_type = pid_key->addr_type;
1004             memcpy(&bond_dev->bond_key.pid_key.static_addr, pid_key->static_addr, ESP_BD_ADDR_LEN);
1005         }
1006         //search for the next bond device
1007         bond_dev++;
1008     }
1009     btc_config_unlock();
1010 
1011     return BT_STATUS_SUCCESS;
1012 }
1013 
btc_storage_get_num_ble_bond_devices(void)1014 int btc_storage_get_num_ble_bond_devices(void)
1015 {
1016     int num_dev = 0;
1017     uint32_t device_type = 0;
1018 
1019     btc_config_lock();
1020     for (const btc_config_section_iter_t *iter = btc_config_section_begin(); iter != btc_config_section_end();
1021             iter = btc_config_section_next(iter)) {
1022         const char *name = btc_config_section_name(iter);
1023         if (!string_is_bdaddr(name) ||
1024                 !btc_config_get_int(name, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&device_type) ||
1025                 !(device_type & BT_DEVICE_TYPE_BLE)) {
1026             continue;
1027         }
1028 
1029         num_dev++;
1030     }
1031     btc_config_unlock();
1032 
1033     return num_dev;
1034 }
1035 
btc_storage_get_gatt_cl_supp_feat(bt_bdaddr_t * remote_bd_addr,uint8_t * value,int len)1036 bt_status_t btc_storage_get_gatt_cl_supp_feat(bt_bdaddr_t *remote_bd_addr, uint8_t *value, int len)
1037 {
1038     bdstr_t bdstr;
1039     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1040     int ret = btc_config_get_bin(bdstr, BTC_BLE_STORAGE_GATT_CL_SUPP_FEAT_STR, value, (size_t *)&len);
1041     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1042 }
1043 
btc_storage_set_gatt_cl_supp_feat(bt_bdaddr_t * remote_bd_addr,uint8_t * value,int len)1044 bt_status_t btc_storage_set_gatt_cl_supp_feat(bt_bdaddr_t *remote_bd_addr, uint8_t *value, int len)
1045 {
1046     int ret;
1047     bdstr_t bdstr;
1048 
1049     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr_t));
1050     ret = btc_config_set_bin(bdstr, BTC_BLE_STORAGE_GATT_CL_SUPP_FEAT_STR, value, (size_t)len);
1051     if (ret == false) {
1052         return BT_STATUS_FAIL;
1053     }
1054 
1055     return BT_STATUS_SUCCESS;
1056 }
1057 
btc_storage_get_gatt_db_hash(bt_bdaddr_t * remote_bd_addr,uint8_t * value,int len)1058 bt_status_t btc_storage_get_gatt_db_hash(bt_bdaddr_t *remote_bd_addr, uint8_t *value, int len)
1059 {
1060     bdstr_t bdstr;
1061     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1062     int ret = btc_config_get_bin(bdstr, BTC_BLE_STORAGE_GATT_DB_HASH_STR, value, (size_t *)&len);
1063     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
1064 }
1065 
btc_storage_set_gatt_db_hash(bt_bdaddr_t * remote_bd_addr,uint8_t * value,int len)1066 bt_status_t btc_storage_set_gatt_db_hash(bt_bdaddr_t *remote_bd_addr, uint8_t *value, int len)
1067 {
1068     int ret;
1069     bdstr_t bdstr;
1070 
1071     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr_t));
1072     ret = btc_config_set_bin(bdstr, BTC_BLE_STORAGE_GATT_DB_HASH_STR, value, (size_t)len);
1073     if (ret == false) {
1074         return BT_STATUS_FAIL;
1075     }
1076 
1077     return BT_STATUS_SUCCESS;
1078 }
1079 
btc_storage_remove_gatt_cl_supp_feat(bt_bdaddr_t * remote_bd_addr)1080 bt_status_t btc_storage_remove_gatt_cl_supp_feat(bt_bdaddr_t *remote_bd_addr)
1081 {
1082     bool ret = true;
1083     bdstr_t bdstr;
1084 
1085     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1086 
1087     ret = btc_config_remove(bdstr, BTC_BLE_STORAGE_GATT_CL_SUPP_FEAT_STR);
1088     if (ret == false) {
1089         return BT_STATUS_FAIL;
1090     }
1091 
1092     return  BT_STATUS_SUCCESS;
1093 }
1094 
btc_storage_remove_gatt_db_hash(bt_bdaddr_t * remote_bd_addr)1095 bt_status_t btc_storage_remove_gatt_db_hash(bt_bdaddr_t *remote_bd_addr)
1096 {
1097     bool ret = true;
1098     bdstr_t bdstr;
1099 
1100     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
1101 
1102     ret = btc_config_remove(bdstr, BTC_BLE_STORAGE_GATT_DB_HASH_STR);
1103     if (ret == false) {
1104         return BT_STATUS_FAIL;
1105     }
1106 
1107     return  BT_STATUS_SUCCESS;
1108 }
1109 #endif  ///BLE_INCLUDED == TRUE
1110 #endif  ///SMP_INCLUDED == TRUE
1111