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