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