• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved.
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 
16 /*****************************************************************************
17 **
18 **  Name:           wm_bt_storage.c
19 **
20 **  Description:    This file contains the  functions for bluetooth parameters storage
21 **
22 *****************************************************************************/
23 #include <string.h>
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include <assert.h>
27 #include "securec.h"
28 #include "wm_bt_config.h"
29 
30 #if (WM_NIMBLE_INCLUDED == CFG_ON)
31 
32 #include "wm_params.h"
33 #include "wm_param.h"
34 #include "wm_bt_def.h"
35 #include "syscfg/syscfg.h"
36 #include "host/ble_hs.h"
37 #include "ble_store_config_priv.h"
38 #include "store/config/wm_bt_storage.h"
39 
40 #define WM_BT_STORAGE_DEBUG_TAG 0
41 // our param area support max bond device count is 5
42 #define BTM_SEC_MAX_BLE_DEVICE_RECORDS MYNEWT_VAL(BLE_STORE_MAX_BONDS)
43 
44 uint8_t btstorage_trace_level = 0;
45 
46 #if WM_BT_STORAGE_DEBUG_TAG == 1
47 static char *nv_tag_2_str(uint8_t tag);
48 
49 #define BTSTORAGE_TRACE_ERROR(...) printf("%s(L%d): " fmt, __FUNCTION__, __LINE__,  ## __VA_ARGS__);
50 #define BTSTORAGE_TRACE_WARNING(...)
51 #define BTSTORAGE_TRACE_API(...)
52 #define BTSTORAGE_TRACE_EVENT(...)
53 #define BTSTORAGE_TRACE_DEBUG(...) printf("%s(L%d): " fmt, __FUNCTION__, __LINE__,  ## __VA_ARGS__);
54 #define BTSTORAGE_TRACE_VERBOSE(...)
55 
56 #else
57 
58 #define BTSTORAGE_TRACE_ERROR(...)
59 #define BTSTORAGE_TRACE_WARNING(...)
60 #define BTSTORAGE_TRACE_API(...)
61 #define BTSTORAGE_TRACE_EVENT(...)
62 #define BTSTORAGE_TRACE_DEBUG(...)
63 #define BTSTORAGE_TRACE_VERBOSE(...)
64 #endif
65 
string_to_bdaddr(const char * string,tls_bt_addr_t * addr)66 static uint8_t string_to_bdaddr(const char *string, tls_bt_addr_t *addr)
67 {
68     assert(string != NULL);
69     assert(addr != NULL);
70     tls_bt_addr_t new_addr;
71     uint8_t *ptr = new_addr.address;
72     uint8_t ret = sscanf(string, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
73                          &ptr[0], &ptr[1], &ptr[2], &ptr[3], &ptr[4], &ptr[5]) == 6; // 6:len
74     if (ret) {
75         memcpy_s(addr, sizeof(*addr), &new_addr, sizeof(tls_bt_addr_t));
76     }
77 
78     return ret;
79 }
80 
bd_to_string(const uint8_t * addr,char * string,size_t size)81 static const char *bd_to_string(const uint8_t *addr, char *string, size_t size)
82 {
83     assert(addr != NULL);
84     assert(string != NULL);
85 
86     if (size < 18) { // 18:Analyzing conditions
87         return NULL;
88     }
89 
90     const uint8_t *ptr = addr;
91     int eok = sprintf_s(string, sizeof(*string), "%02x:%02x:%02x:%02x:%02x:%02x",
92             ptr[0], ptr[1], ptr[2], // 2:array element
93             ptr[3], ptr[4], ptr[5]); // 3:array element, 4:array element, 5:array element
94     if (eok != EOK) {
95         return NULL;
96     }
97     return string;
98 }
99 
100 /* Only 16 bytes, the spec definition is 248 bytes */
101 #define WM_BD_NAME_LEN 16
102 
btif_wm_config_get_remote_device(int index,void * ptr,int from_flash)103 int btif_wm_config_get_remote_device(int index, void *ptr, int from_flash)
104 {
105     assert(ptr != NULL);
106     /* always from sram */
107     return tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + index, ptr, 0);
108 }
109 
btif_wm_config_set_local(const nv_tag_t section,const nv_tag_t key,const nv_tag_t name,const char * value,int bytes,int type,bool dummy_wr)110 int btif_wm_config_set_local(const nv_tag_t section, const nv_tag_t key, const nv_tag_t name,
111                              const char  *value, int bytes, int type, bool dummy_wr)
112 {
113     int ret = TRUE;
114 
115     bt_adapter_t adapter;
116     tls_bt_addr_t address;
117     BTSTORAGE_TRACE_DEBUG("btif_wm_config_set_local [%s->%s->%s]\r\n", nv_tag_2_str(section),
118                           nv_tag_2_str(key), nv_tag_2_str(name));
119     if (section == NV_LOCAL) {
120         tls_param_get(TLS_PARAM_ID_BT_ADAPTER, &adapter, 0);
121 
122         if (!dummy_wr) {
123             if (adapter.valid_tag != 0xdeadbeaf) {
124                 adapter.valid_tag = 0xdeadbeaf;
125             }
126         } else {
127             if (adapter.valid_tag != 0xdeadbeaf) {
128                 BTSTORAGE_TRACE_DEBUG("Clear local adapter information, but it is an empty pos!!!")
129                 return ret;
130             }
131         }
132 
133         switch ((uint8_t)name) {
134             case NV_LOCAL_ADAPTER_ADDRESS:
135                 BTSTORAGE_TRACE_DEBUG("Save local address:%s\r\n", value);
136 
137                 if (!dummy_wr) {
138                     adapter.valid_bit |= ADAPTER_BD_ADDRESS_VALID_BIT;
139                     string_to_bdaddr(value, &address);
140                     memcpy_s(adapter.bd_addr, sizeof(adapter.bd_addr), address.address, 6); // 6:bytes
141                 } else {
142                     adapter.valid_bit &= ~ADAPTER_BD_ADDRESS_VALID_BIT;
143                 }
144 
145                 break;
146 
147             case NV_LOCAL_ADAPTER_NAME:
148                 BTSTORAGE_TRACE_DEBUG("Save local name:%s\r\n", value);
149 
150                 if ((bytes <= WM_BD_NAME_LEN) && (!dummy_wr)) {
151                     if (bytes > WM_BD_NAME_LEN) {
152                         bytes = WM_BD_NAME_LEN;
153                     }
154 
155                     adapter.valid_bit |= ADAPTER_NAME_VALID_BIT;
156                     memcpy_s(adapter.name, sizeof(adapter.name), value, bytes);
157                     adapter.name_len = bytes;
158                 } else {
159                     /* Invalid name parameter */
160                     adapter.valid_bit &= ~ADAPTER_NAME_VALID_BIT;
161                     adapter.name_len = 0;
162 
163                     if (!dummy_wr) {
164                         ret = FALSE;
165                     }
166                 }
167 
168                 break;
169 
170             case NV_LOCAL_ADAPTER_CLASS:
171                 if (!dummy_wr) {
172                     adapter.class_of_device = *((uint32_t *)value);
173                     adapter.valid_bit |= ADAPTER_CLASS_VALID_BIT;
174                 } else {
175                     adapter.valid_bit &= ~ADAPTER_CLASS_VALID_BIT;
176                 }
177 
178                 BTSTORAGE_TRACE_DEBUG("Save local device class:%04x\r\n", adapter.class_of_device);
179                 break;
180 
181             case NV_LOCAL_ADAPTER_IO_CAP:
182                 if (!dummy_wr) {
183                     adapter.io_cap = *((uint8_t *)value);
184                     adapter.valid_bit |= ADAPTER_IOCAP_VALID_BIT;
185                 } else {
186                     adapter.valid_bit &= ~ADAPTER_IOCAP_VALID_BIT;
187                 }
188 
189                 BTSTORAGE_TRACE_DEBUG("Save local iocap:%d\r\n", adapter.io_cap);
190                 break;
191 
192             case NV_LOCAL_ADAPTER_DISCOVERABLE:
193                 if (!dummy_wr) {
194                     adapter.discoverable = *((uint8_t *)value);
195                     adapter.valid_bit |= ADAPTER_DISCOVER_VALID_BIT;
196                 } else {
197                     adapter.valid_bit &= ~ADAPTER_DISCOVER_VALID_BIT;
198                 }
199 
200                 BTSTORAGE_TRACE_DEBUG("Save local discoverable:%d\r\n", adapter.discoverable);
201                 break;
202 
203             case NV_LOCAL_ADAPTER_CONNECTABLE:
204                 if (!dummy_wr) {
205                     adapter.connectable = *((uint8_t *)value) ;
206                     adapter.valid_bit |= ADAPTER_CONNECT_VALID_BIT;
207                 } else {
208                     adapter.valid_bit &= ~ADAPTER_CONNECT_VALID_BIT;
209                 }
210 
211                 BTSTORAGE_TRACE_DEBUG("Save local connectable:%d\r\n", adapter.connectable);
212                 break;
213 
214             case NV_LOCAL_ADAPTER_AUTH_REQ:
215                 if (!dummy_wr) {
216                     adapter.bt_auth_req = *((uint8_t *)value);
217                     adapter.valid_bit |= ADAPTER_BT_AUTH_REQ_VALID_BIT;
218                 } else {
219                     adapter.valid_bit &= ~ADAPTER_BT_AUTH_REQ_VALID_BIT;
220                 }
221 
222                 BTSTORAGE_TRACE_DEBUG("Save local auth:0x%02x\r\n", adapter.bt_auth_req);
223                 break;
224 
225             case NV_LOCAL_ADAPTER_MODE:
226                 if (!dummy_wr) {
227                     adapter.mode = *((uint8_t *)value) ;
228                     adapter.valid_bit |= ADAPTER_BLE_WORK_MODE_VALID_BIT;
229                 } else {
230                     adapter.valid_bit &= ~ADAPTER_BLE_WORK_MODE_VALID_BIT;
231                 }
232 
233                 BTSTORAGE_TRACE_DEBUG("Save local adapter mode:0x%02x\r\n", adapter.mode);
234                 break;
235 #if ( BTA_GATT_INCLUDED == TRUE && BLE_INCLUDED == TRUE)
236 
237             case NV_LOCAL_ADAPTER_BLE_AUTH_REQ:
238                 if (!dummy_wr) {
239                     adapter.ble_auth_req = *((uint8_t *)value) ;
240                     adapter.valid_bit |= ADAPTER_BLE_AUTH_REQ_VALID_BIT;
241                 } else {
242                     adapter.valid_bit &= ~ADAPTER_BLE_AUTH_REQ_VALID_BIT;
243                 }
244 
245                 break;
246 
247             case NV_LOCAL_ADAPTER_BLE_IR:
248                 if (!dummy_wr) {
249                     adapter.valid_bit |= ADAPTER_BLE_IR_VALID_BIT;
250                     memcpy_s(adapter.ir, sizeof(adapter.ir), value, 16); // 16:bytes
251                 } else {
252                     adapter.valid_bit &= ~ADAPTER_BLE_IR_VALID_BIT;
253                 }
254 
255                 BTSTORAGE_TRACE_DEBUG("Save local ble ir type=%d", type);
256                 break;
257 
258             case NV_LOCAL_ADAPTER_BLE_ER:
259                 if (!dummy_wr) {
260                     adapter.valid_bit |= ADAPTER_BLE_ER_VALID_BIT;
261                     memcpy_s(adapter.er, sizeof(adapter.er), value, 16); // 16:bytes
262                 } else {
263                     adapter.valid_bit &= ~ADAPTER_BLE_ER_VALID_BIT;
264                 }
265 
266                 BTSTORAGE_TRACE_DEBUG("Save local ble er type=%d", type);
267                 break;
268 
269             case NV_LOCAL_ADAPTER_BLE_IRK:
270                 if (!dummy_wr) {
271                     adapter.valid_bit |= ADAPTER_BLE_IRK_VALID_BIT;
272                     memcpy_s(adapter.irk, sizeof(adapter.irk), value, 16); // 16:bytes
273                 } else {
274                     adapter.valid_bit &= ~ADAPTER_BLE_IRK_VALID_BIT;
275                 }
276 
277                 BTSTORAGE_TRACE_DEBUG("Save local ble irk type=%d", type);
278                 break;
279 
280             case NV_LOCAL_ADAPTER_BLE_DHK:
281                 if (!dummy_wr) {
282                     adapter.valid_bit |= ADAPTER_BLE_DHK_VALID_BIT;
283                     memcpy_s(adapter.dhk, sizeof(adapter.dhk), value, 16); // 16:bytes
284                 } else {
285                     adapter.valid_bit &= ~ADAPTER_BLE_DHK_VALID_BIT;
286                 }
287 
288                 BTSTORAGE_TRACE_DEBUG("Save local ble dhk type=%d", type);
289                 break;
290 #endif
291 
292             default:
293                 BTSTORAGE_TRACE_WARNING("ERROR ERROR ERROR, unknown tag index to save local parameter\r\n");
294                 ret = FALSE;
295                 break;
296         }
297 
298         int err = tls_param_set(TLS_PARAM_ID_BT_ADAPTER, &adapter, 0);
299         if (err == TLS_PARAM_STATUS_OK) {
300             ret = TRUE;
301         } else {
302             ret = FALSE;
303         }
304     } else {
305         ret = FALSE;
306     }
307 
308     return ret;
309 }
btif_wm_config_get_local(const nv_tag_t section,const nv_tag_t key,const nv_tag_t name,char * value,int * bytes,int * type,bool dummy_rd)310 int btif_wm_config_get_local(const nv_tag_t section, const nv_tag_t key, const nv_tag_t name,
311                              char *value, int *bytes, int *type, bool dummy_rd)
312 {
313     int ret = TRUE;
314     bt_adapter_t adapter;
315 
316     if (section == NV_LOCAL) {
317         if (key == NV_LOCAL_ADAPTER) {
318             tls_param_get(TLS_PARAM_ID_BT_ADAPTER, &adapter, 0);
319 
320             if (adapter.valid_tag != 0xdeadbeaf) {
321                 /* Flash content invalid, do nothing */
322                 ret = FALSE;
323                 return ret;
324             }
325 
326             switch ((uint8_t)name) {
327                 case NV_LOCAL_ADAPTER_ADDRESS:
328                     if (adapter.valid_bit & ADAPTER_BD_ADDRESS_VALID_BIT) {
329                         if (!dummy_rd) {
330                             bd_to_string(adapter.bd_addr, value, 18); // 18:size
331                             *bytes = 6; // 6:bytes
332                             *type = BTIF_CFG_TYPE_STR;
333                         }
334                     } else {
335                         ret = FALSE;
336                     }
337 
338                     break;
339 
340                 case NV_LOCAL_ADAPTER_NAME:
341                     if (adapter.valid_bit & ADAPTER_NAME_VALID_BIT) {
342                         if (!dummy_rd) {
343                             memcpy_s(value, sizeof(*value), adapter.name, adapter.name_len);
344                             *bytes = adapter.name_len;
345                             *type = BTIF_CFG_TYPE_STR;
346                         }
347                     } else {
348                         /* Invalid name parameter */
349                         ret = FALSE;
350                     }
351 
352                     break;
353 
354                 case NV_LOCAL_ADAPTER_CLASS:
355                     if (adapter.valid_bit & ADAPTER_CLASS_VALID_BIT) {
356                         if (!dummy_rd) {
357                             *((uint32_t *)value) = adapter.class_of_device;
358                         }
359                     } else {
360                         ret = FALSE;
361                     }
362 
363                     break;
364 
365                 case NV_LOCAL_ADAPTER_IO_CAP:
366                     if (adapter.valid_bit & ADAPTER_IOCAP_VALID_BIT) {
367                         if (!dummy_rd) {
368                             *((uint8_t *)value) = adapter.io_cap;
369                         }
370                     } else {
371                         ret = FALSE;
372                     }
373 
374                     break;
375 
376                 case NV_LOCAL_ADAPTER_DISCOVERABLE:
377                     if (adapter.valid_bit & ADAPTER_DISCOVER_VALID_BIT) {
378                         if (!dummy_rd) {
379                             *((uint8_t *)value) = adapter.discoverable;
380                         }
381                     } else {
382                         ret = FALSE;
383                     }
384 
385                     break;
386 
387                 case NV_LOCAL_ADAPTER_CONNECTABLE:
388                     if (adapter.valid_bit & ADAPTER_CONNECT_VALID_BIT) {
389                         if (!dummy_rd) {
390                             *((uint8_t *)value) = adapter.connectable;
391                         }
392                     } else {
393                         ret = FALSE;
394                     }
395 
396                     break;
397 
398                 case NV_LOCAL_ADAPTER_AUTH_REQ:
399                     if (adapter.valid_bit & ADAPTER_BT_AUTH_REQ_VALID_BIT) {
400                         if (!dummy_rd) {
401                             *((uint8_t *)value) = adapter.bt_auth_req;
402                         }
403                     } else {
404                         ret = FALSE;
405                     }
406 
407                     break;
408 
409                 case NV_LOCAL_ADAPTER_MODE:
410                     if (adapter.valid_bit & ADAPTER_BLE_WORK_MODE_VALID_BIT) {
411                         if (!dummy_rd) {
412                             *((uint8_t *)value) = adapter.mode;
413                         }
414                     } else {
415                         ret = FALSE;
416                     }
417 
418                     break;
419 #if ( BTA_GATT_INCLUDED == TRUE && BLE_INCLUDED == TRUE)
420 
421                 case NV_LOCAL_ADAPTER_BLE_AUTH_REQ:
422                     if (adapter.valid_bit & ADAPTER_BLE_AUTH_REQ_VALID_BIT) {
423                         if (!dummy_rd) {
424                             *((uint8_t *)value) = adapter.ble_auth_req;
425                         }
426                     } else {
427                         ret = FALSE;
428                     }
429 
430                     break;
431 
432                 case NV_LOCAL_ADAPTER_BLE_IR:
433                     assert(*type == BTIF_CFG_TYPE_BIN);
434 
435                     if (adapter.valid_bit & ADAPTER_BLE_IR_VALID_BIT) {
436                         if (!dummy_rd) {
437                             memcpy_s(value, sizeof(*value), adapter.ir, 16); // 16:bytes
438                             *bytes = 16;
439                         }
440                     } else {
441                         ret = FALSE;
442                     }
443 
444                     break;
445 
446                 case NV_LOCAL_ADAPTER_BLE_ER:
447                     assert(*type == BTIF_CFG_TYPE_BIN);
448 
449                     if (adapter.valid_bit & ADAPTER_BLE_ER_VALID_BIT) {
450                         if (!dummy_rd) {
451                             memcpy_s(value, sizeof(*value), adapter.er, 16); // 16:bytes
452                             *bytes = 16; // 16:bytes
453                         }
454                     } else {
455                         ret = FALSE;
456                     }
457 
458                     break;
459 
460                 case NV_LOCAL_ADAPTER_BLE_IRK:
461                     assert(*type == BTIF_CFG_TYPE_BIN);
462 
463                     if (adapter.valid_bit & ADAPTER_BLE_IRK_VALID_BIT) {
464                         if (!dummy_rd) {
465                             memcpy_s(value, sizeof(*value), adapter.irk, 16); // 16:bytes
466                             *bytes = 16; // 16:bytes
467                         }
468                     } else {
469                         ret = FALSE;
470                     }
471 
472                     break;
473 
474                 case NV_LOCAL_ADAPTER_BLE_DHK:
475                     assert(*type == BTIF_CFG_TYPE_BIN);
476 
477                     if (adapter.valid_bit & ADAPTER_BLE_DHK_VALID_BIT) {
478                         if (!dummy_rd) {
479                             memcpy_s(value, sizeof(*value), adapter.dhk, 16); // 16:bytes
480                             *bytes = 16; // 16:bytes
481                         }
482                     } else {
483                         ret = FALSE;
484                     }
485 
486                     break;
487 #endif
488 
489                 default:
490                     ret = FALSE;
491                     break;
492             }
493         } else {
494             ret = FALSE;
495         }
496     } else {
497         ret = FALSE;
498     }
499 
500     if (ret) {
501         BTSTORAGE_TRACE_DEBUG("btif_wm_config_got_local [%s->%s->%s]\r\n", nv_tag_2_str(section),
502                               nv_tag_2_str(key), nv_tag_2_str(name));
503     } else {
504         BTSTORAGE_TRACE_DEBUG("There is no local adapter information:[%s->%s->%s]\r\n",
505                               nv_tag_2_str(section), nv_tag_2_str(key), nv_tag_2_str(name));
506     }
507 
508     return ret;
509 }
btif_wm_config_get_local_int(const nv_tag_t section,const nv_tag_t key,const nv_tag_t name,int * value)510 int btif_wm_config_get_local_int(const nv_tag_t section, const nv_tag_t key, const nv_tag_t name,
511                                  int *value)
512 {
513     int size = sizeof(*value);
514     int type = BTIF_CFG_TYPE_INT;
515     return btif_wm_config_get_local(section, key, name, (char *)value, &size, &type, false);
516 }
btif_wm_config_set_local_int(const nv_tag_t section,const nv_tag_t key,const nv_tag_t name,int value)517 int btif_wm_config_set_local_int(const nv_tag_t section, const nv_tag_t key, const nv_tag_t name,
518                                  int value)
519 {
520     return btif_wm_config_set_local(section, key, name, (char *)&value, sizeof(value),
521                                     BTIF_CFG_TYPE_INT, false);
522 }
btif_wm_config_get_local_str(const nv_tag_t section,const nv_tag_t key,const nv_tag_t name,char * value,int * size)523 int btif_wm_config_get_local_str(const nv_tag_t section, const nv_tag_t key, const nv_tag_t name,
524                                  char *value, int *size)
525 {
526     int type = BTIF_CFG_TYPE_STR;
527 
528     if (value) {
529         *value = 0;
530     }
531 
532     return btif_wm_config_get_local(section, key, name, value, size, &type, false);
533 }
btif_wm_config_set_local_str(const nv_tag_t section,const nv_tag_t key,const nv_tag_t name,const char * value)534 int btif_wm_config_set_local_str(const nv_tag_t section, const nv_tag_t key, const nv_tag_t name,
535                                  const char *value)
536 {
537     const char *value_tmp = value;
538     value_tmp = value_tmp ? value_tmp : "";
539     return btif_wm_config_set_local(section, key, name, value_tmp, strlen(value_tmp) + 1, BTIF_CFG_TYPE_STR,
540                                     false);
541 }
btif_wm_config_remove_local(const nv_tag_t section,const nv_tag_t key,const nv_tag_t name)542 int btif_wm_config_remove_local(const nv_tag_t section, const nv_tag_t key, const nv_tag_t name)
543 {
544     return btif_wm_config_set_local(section, key, name, NULL, 0, 0, true);
545 }
btif_wm_config_exist_local(const nv_tag_t section,const nv_tag_t key,const nv_tag_t name)546 int btif_wm_config_exist_local(const nv_tag_t section, const nv_tag_t key, const nv_tag_t name)
547 {
548     return btif_wm_config_get_local(section, key, name, NULL, NULL, NULL, true);
549 }
btif_wm_config_filter_remove_local(const char * section,const char * filter[],int filter_count,int max_allowed)550 int btif_wm_config_filter_remove_local(const char *section, const char *filter[], int filter_count,
551                                        int max_allowed)
552 {
553     return FALSE;
554 }
555 
btif_wm_config_find_by_key(uint8_t * bd_addr,int * index,int add)556 bool btif_wm_config_find_by_key(uint8_t *bd_addr, int *index, int add)
557 {
558     uint8_t i = 0;
559     bool found = false;
560     bt_remote_device_t device;
561 
562     for (i = 0; i < BTM_SEC_MAX_BLE_DEVICE_RECORDS; i++) {
563         tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + i, (void *)&device, 0);
564 
565         if ((device.valid_tag == 0xdeadbeaf) && device.in_use &&
566             (memcmp(device.bd_addr, bd_addr, 6) == 0)) { // 6:bytes
567             found = true;
568             *index = i;
569             return found;
570         }
571     }
572 
573     /* find an empty pos and insert one */
574     if ((found == false) && add) {
575         /* Try to find an empty pos */
576         for (i = 0; i < BTM_SEC_MAX_BLE_DEVICE_RECORDS; i++) {
577             tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + i, (void *)&device, 0);
578 
579             /* For a fresh flash, valid tag must be not equal to 0xdeadbeaf */
580             if ((device.valid_tag != 0xdeadbeaf) || (device.in_use == 0)) {
581                 found = true;
582                 *index = i;
583                 memcpy_s(device.bd_addr, sizeof(device.bd_addr), bd_addr, 6); // 6:bytes
584                 device.in_use = 1;
585                 device.valid_tag = 0xdeadbeaf;
586                 tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + i, (void *)&device, 0);
587                 break;
588             }
589         }
590     }
591 
592     if ((found == false) && (add)) {
593         BTSTORAGE_TRACE_WARNING("No position to fit the bonding key, we try to erase an old one !!!\r\n");
594         // find the old one position;
595         // the last pos must be the oldest one
596         // the pos[0~(BTM_SEC_MAX_BLE_DEVICE_RECORDS-1)] will be updated once is paried with phone;
597         found = true;
598         *index = (BTM_SEC_MAX_BLE_DEVICE_RECORDS - 1);
599         memcpy_s(device.bd_addr, sizeof(device.bd_addr), bd_addr, 6); // 6:bytes
600         device.in_use = 1;
601         device.valid_tag = 0xdeadbeaf;
602         // the last pos will be overwitten;
603         tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + (BTM_SEC_MAX_BLE_DEVICE_RECORDS - 1),
604                       (void *)&device, 0);
605     }
606 
607     return found;
608 }
609 
btif_wm_config_update_remote_device(const char * key)610 int btif_wm_config_update_remote_device(const char *key)
611 {
612     int ret = FALSE;
613     tls_bt_addr_t addr;
614     int index = -1;
615     bt_remote_device_t update_device;
616     bt_remote_device_t tmp_device;
617     string_to_bdaddr(key, &addr);
618 
619     // scan the total records
620     if (btif_wm_config_find_by_key(addr.address, &index, 0)) {
621         /* if index == 0, already the first pos, do nothing */
622         if (index != 0) {
623             // for there is only five devices, so we sort it by hand;
624             tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + index, (void *)&update_device, 0);
625 
626             if (index == 1) {
627                 tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 0, (void *)&tmp_device, 0);      // read 0
628                 tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + index, (void *)&tmp_device, 0);  // 0 write to 1
629                 tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 0, (void *)&update_device, 0);
630                 ret = TRUE;
631             } else if (index == 2) { // 2:value of index
632                 tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 1, (void *)&tmp_device, 0); // read 1
633                 tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + index, (void *)&tmp_device, 0);  // 1->2
634                 tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 0, (void *)&tmp_device, 0);      // read 0
635                 tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 1, (void *)&tmp_device, 0);  // 0 write to 1
636                 tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 0, (void *)&update_device, 0);
637                 ret = TRUE;
638             } else if (index == 3) { // 3:value of index
639                 tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 2, (void *)&tmp_device, 0); // read 2
640                 tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + index, (void *)&tmp_device, 0);  // 2->3
641                 tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 1, (void *)&tmp_device, 0); // read 1
642                 tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 2, (void *)&tmp_device, 0);  // 1->2
643                 tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 0, (void *)&tmp_device, 0);      // read 0
644                 tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 1, (void *)&tmp_device, 0);  // 0 write to 1
645                 tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 0, (void *)&update_device, 0);
646                 ret = TRUE;
647             } else if (index == 4) { // 4:value of index
648                 tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 3, (void *)&tmp_device, 0); // read 3
649                 tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + index, (void *)&tmp_device, 0);  // 3->4
650                 tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 2, (void *)&tmp_device, 0); // read 2
651                 tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 3, (void *)&tmp_device, 0);  // 2->3
652                 tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 1, (void *)&tmp_device, 0); // read 1
653                 tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 2, (void *)&tmp_device, 0);  // 1->2
654                 tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 0, (void *)&tmp_device, 0);      // read 0
655                 tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 1, (void *)&tmp_device, 0);  // 0 write to 1
656                 tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + 0, (void *)&update_device, 0);
657                 ret = TRUE;
658             } else {
659                 BTSTORAGE_TRACE_WARNING("We support only 5 remote devices by now!!!\r\n");
660             }
661         } else {
662             BTSTORAGE_TRACE_DEBUG("Already the first pos, do nothing\r\n");
663         }
664     } else {
665         BTSTORAGE_TRACE_WARNING("Warning, device[%s] is not found when updating\r\n", key);
666     }
667 
668     if (ret) {
669         btif_config_save();
670     }
671 
672     return ret;
673 }
674 
675 /********************************************************************************************************
676 *  FUNCTION: btif_wm_config_set_remote
677 *  DESCRIPTION:  dummy_wr 1 clear, 0 write the parameters
678 *
679 *
680 *********************************************************************************************************/
681 
btif_wm_config_set_remote(const nv_tag_t section,const char * key,const nv_tag_t name,const char * value,int bytes,int type,bool dummy_wr)682 int btif_wm_config_set_remote(const nv_tag_t section, const char *key, const nv_tag_t name,
683                               const char  *value, int bytes, int type, bool dummy_wr)
684 {
685     return FALSE;
686 }
btif_wm_config_get_remote(const nv_tag_t section,const char * key,const nv_tag_t name,char * value,int * bytes,int * type,bool dummy_rd)687 int btif_wm_config_get_remote(const nv_tag_t section, const char *key, const nv_tag_t name,
688                               char *value, int *bytes, int *type, bool dummy_rd)
689 {
690     return FALSE;
691 }
692 
btif_wm_config_filter_remove_remote(const char * section,const char * filter[],int filter_count,int max_allowed)693 int btif_wm_config_filter_remove_remote(const char *section, const char *filter[], int filter_count,
694                                         int max_allowed)
695 {
696     return FALSE;
697 }
698 
local_name_str_to_index(const char * tag_name)699 int local_name_str_to_index(const char *tag_name)
700 {
701     const char *name[] = {"Address", "Name", "DevClass", "IOCAP", "Discoverable", "Connectable",
702                           "DiscoveryTimeout", "AuthReq", "ScanMode", "BleAuthReq",  "LE_LOCAL_KEY_IR",
703                           "LE_LOCAL_KEY_IRK", "LE_LOCAL_KEY_DHK", "LE_LOCAL_KEY_ER", NULL};
704     int i = 0;
705 
706     do {
707         if (strncmp(name[i], tag_name, strlen(tag_name)) == 0) {
708             return (i + (int)NV_LOCAL_ADAPTER_ADDRESS);
709         }
710 
711         i++;
712     } while (name[i] != NULL);
713 
714     BTSTORAGE_TRACE_ERROR("ERROR ERROR ERROR , unindexed local tag string(%s)\r\n", tag_name);
715     assert(0);
716     return -1;
717 }
718 
remote_name_str_to_index(const char * tag_name)719 int remote_name_str_to_index(const char *tag_name)
720 {
721     const char *name[] = {"Address", "Name", "DevClass", "Service",
722         "LinkKey", "LinkKeyType", "IOCAP", "PinLength", "DevType",
723         "AddrType", "LE_KEY_PENC", "LE_KEY_LENC", "LE_KEY_PID",
724         "LE_KEY_LID", "LE_KEY_PCSRK", "LE_KEY_LCSRK", "Reconnect",
725         "Manufacturer", "LmpVer", "LmpSubVer", NULL};
726     int i = 0;
727 
728     do {
729         if (strncmp(name[i], tag_name, strlen(tag_name)) == 0) {
730             return (i + (int)NV_REMOTE_ADDRESS);
731         }
732 
733         i++;
734     } while (name[i] != NULL);
735 
736     BTSTORAGE_TRACE_ERROR("ERROR ERROR ERROR , unindexed remote tag string(%s)\r\n", tag_name);
737     assert(0);
738     return -1;
739 }
740 
btif_config_get_int(const char * section,const char * key,const char * name,int * value)741 int btif_config_get_int(const char *section, const char *key, const char *name, int *value)
742 {
743     BTSTORAGE_TRACE_EVENT("btif_config_get_int section:%s, key:%s, name:%s\r\n", section, key, name);
744 
745     if ((strncmp(section, "Local", 5) == 0) && (strncmp(key, "Adapter", 7) == 0)) { // 5:size, 7:size
746         int index = local_name_str_to_index(name);
747         if (index > 0) {
748             return btif_wm_config_get_local_int(NV_LOCAL, NV_LOCAL_ADAPTER, index, value);
749         }
750     } else {
751     }
752 
753     return FALSE;
754 }
755 
btif_config_set_int(const char * section,const char * key,const char * name,int value)756 int btif_config_set_int(const char *section, const char *key, const char *name, int value)
757 {
758     BTSTORAGE_TRACE_EVENT("btif_config_set_int section:%s, key:%s, name:%s\r\n", section, key, name);
759 
760     if ((strncmp(section, "Local", 5) == 0) && (strncmp(key, "Adapter", 7) == 0)) { // 5:size, 7:size
761         int index = local_name_str_to_index(name);
762         if (index > 0) {
763             return btif_wm_config_set_local_int(NV_LOCAL, NV_LOCAL_ADAPTER, index, value);
764         }
765     } else {
766     }
767 
768     return FALSE;
769 }
btif_config_get_str(const char * section,const char * key,const char * name,char * value,int * size)770 int btif_config_get_str(const char *section, const char *key, const char *name, char *value,
771                         int *size)
772 {
773     BTSTORAGE_TRACE_EVENT("btif_config_get_str section:%s, key:%s, name:%s\r\n", section, key, name);
774 
775     if ((strncmp(section, "Local", 5) == 0) && (strncmp(key, "Adapter", 7) == 0)) { // 5:size, 7:size
776         int index = local_name_str_to_index(name);
777         if (index > 0) {
778             return btif_wm_config_get_local_str(NV_LOCAL, NV_LOCAL_ADAPTER, index, value, size);
779         }
780     } else {
781     }
782 
783     return FALSE;
784 }
btif_config_set_str(const char * section,const char * key,const char * name,const char * value)785 int btif_config_set_str(const char *section, const char *key, const char *name, const char *value)
786 {
787     BTSTORAGE_TRACE_EVENT("btif_config_set_str section:%s, key:%s, name:%s\r\n", section, key, name);
788 
789     if ((strncmp(section, "Local", 5) == 0) && (strncmp(key, "Adapter", 7) == 0)) { // 5:size, 7:size
790         int index = local_name_str_to_index(name);
791         if (index > 0) {
792             return btif_wm_config_set_local_str(NV_LOCAL, NV_LOCAL_ADAPTER, index, value);
793         }
794     } else {
795     }
796 
797     return FALSE;
798 }
799 
btif_config_get(const char * section,const char * key,const char * name,char * value,int * bytes,int * type)800 int btif_config_get(const char *section, const char *key, const char *name, char *value, int *bytes,
801                     int *type)
802 {
803     BTSTORAGE_TRACE_EVENT("btif_config_get section:%s, key:%s, name:%s\r\n", section, key, name);
804 
805     if ((strncmp(section, "Local", 5) == 0) && (strncmp(key, "Adapter", 7) == 0)) { // 5:size, 7:size
806         int index = local_name_str_to_index(name);
807         if (index > 0) {
808             return btif_wm_config_get_local(NV_LOCAL, NV_LOCAL_ADAPTER, index, value, bytes, type, false);
809         }
810     } else {
811     }
812 
813     return FALSE;
814 }
btif_config_set(const char * section,const char * key,const char * name,const char * value,int bytes,int type)815 int btif_config_set(const char *section, const char *key, const char *name, const char  *value,
816                     int bytes, int type)
817 {
818     BTSTORAGE_TRACE_EVENT("btif_config_set section:%s, key:%s, name:%s\r\n", section, key, name);
819 
820     if ((strncmp(section, "Local", 5) == 0) && (strncmp(key, "Adapter", 7) == 0)) { // 5:size, 7:size
821         int index = local_name_str_to_index(name);
822         if (index > 0) {
823             return btif_wm_config_set_local(NV_LOCAL, NV_LOCAL_ADAPTER, index, value, bytes, type, false);
824         }
825     } else {
826     }
827 
828     return FALSE;
829 }
830 
btif_config_remove(const char * section,const char * key,const char * name)831 int btif_config_remove(const char *section, const char *key, const char *name)
832 {
833     BTSTORAGE_TRACE_EVENT("btif_config_remove section:%s, key:%s, name:%s\r\n", section, key, name);
834 
835     if ((strncmp(section, "Local", 5) == 0) && (strncmp(key, "Adapter", 7) == 0)) { // 5:size, 7:size
836         int index = local_name_str_to_index(name);
837         if (index > 0) {
838             return btif_wm_config_remove_local(NV_LOCAL, NV_LOCAL_ADAPTER, index);
839         }
840     } else {
841     }
842 
843     return FALSE;
844 }
btif_config_filter_remove(const char * section,const char * filter[],int filter_count,int max_allowed)845 int btif_config_filter_remove(const char *section, const char *filter[], int filter_count,
846                               int max_allowed)
847 {
848     BTSTORAGE_TRACE_EVENT("btif_config_filter_remove ...section:[%s]filter_count:[%d]\r\n", section,
849                           filter_count);
850     return FALSE;
851 }
btif_config_exist(const char * section,const char * key,const char * name)852 int btif_config_exist(const char *section, const char *key, const char *name)
853 {
854     BTSTORAGE_TRACE_DEBUG("btif_config_exist ...section:[%s] key:[%s] name:[%s]\r\n", section, key,
855                           name);
856 
857     if ((strncmp(section, "Local", 5) == 0) && (strncmp(key, "Adapter", 7) == 0)) { // 5:size, 7:size
858         int index = local_name_str_to_index(name);
859         if (index > 0) {
860             return btif_wm_config_exist_local(NV_LOCAL, NV_LOCAL_ADAPTER, index);
861         }
862     } else {
863     }
864 
865     return FALSE;
866 }
867 
btif_config_remove_remote(const char * key)868 int btif_config_remove_remote(const char *key)
869 {
870     int ret = TRUE;
871     return ret;
872 }
873 
btif_config_save(void)874 int btif_config_save(void)
875 {
876     return TRUE;
877 }
btif_config_flush(int force)878 int btif_config_flush(int force)
879 {
880     if (force) { tls_param_to_flash(TLS_PARAM_ID_ALL); }
881 
882     return TRUE;
883 }
884 /* debug function */
btif_clear_remote_all(void)885 void btif_clear_remote_all(void)
886 {
887     tls_param_to_flash(TLS_PARAM_ID_ALL);
888 }
889 
890 #define NVRAM_ADDR_PAYLOAD_OFFSET 12
891 #define NVRAM_OUR_SEC_PAYLOAD_OFFSET   (NVRAM_ADDR_PAYLOAD_OFFSET+7)
892 #define NVRAM_PEER_SEC_PAYLOAD_OFFSET  (NVRAM_OUR_SEC_PAYLOAD_OFFSET+72)
893 #define NVRAM_CCCD_SEC_PAYLOAD_OFFSET  (NVRAM_PEER_SEC_PAYLOAD_OFFSET+72)
894 
btif_config_get_sec_index(void * addr,uint8_t * found)895 int btif_config_get_sec_index(void *addr, uint8_t *found)
896 {
897     int i = 0;
898     bt_remote_device_t device;
899     ble_addr_t *addr_offset;
900     uint8_t *ptr_offset = (uint8_t *)&device;
901 
902     for (i = 0; i < BTM_SEC_MAX_BLE_DEVICE_RECORDS; i++) {
903         tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + i, (void *)&device, 0);
904 
905         if ((device.valid_tag == 0xdeadbeae)) {
906             addr_offset = (ble_addr_t *)(ptr_offset + NVRAM_ADDR_PAYLOAD_OFFSET);
907 
908             if (ble_addr_cmp((ble_addr_t *)addr, addr_offset) == 0) {
909                 *found = 1;
910                 return i;
911             }
912         }
913     }
914 
915     for (i = 0; i < BTM_SEC_MAX_BLE_DEVICE_RECORDS; i++) {
916         tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + i, (void *)&device, 0);
917 
918         if (device.valid_tag != 0xdeadbeae) {
919             return i;
920         }
921     }
922 
923     return -1;
924 }
925 
dump_bt_device_info(bt_remote_device_t * device)926 void dump_bt_device_info(bt_remote_device_t *device)
927 {
928     uint8_t *ptr = (uint8_t *)device;
929     int i = 0, j = 0;
930     printf("dump device info valid_tag:%08x,valid_bit:0x%08x\r\n", device->valid_tag,
931            device->valid_bit);
932 
933     for (i = 0; i < sizeof(bt_remote_device_t); i++) {
934         printf("%02x ", ptr[i]);
935         j++;
936 
937         if (j == 16) { // 16:bytes
938             j = 0;
939             printf("\r\n");
940         }
941     }
942 
943     printf("\r\n");
944 }
945 
btif_config_store_cccd(int idx,void * addr,int count,void * payload,int length)946 int btif_config_store_cccd(int idx, void *addr, int count, void *payload, int length)
947 {
948     bt_remote_device_t device;
949     uint8_t *ptr_offset = (uint8_t *)&device;
950     assert(idx < BTM_SEC_MAX_BLE_DEVICE_RECORDS);
951     memset_s(&device, sizeof(device), 0, sizeof(device));
952     tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + idx, (void *)&device, 0);
953 
954     if ((device.valid_tag == 0xdeadbeae)) {
955         device.valid_bit = ((count) << 2) | (device.valid_bit & 0x03); // 2:bytes alignment
956         ptr_offset += NVRAM_CCCD_SEC_PAYLOAD_OFFSET;
957         memcpy_s(ptr_offset, sizeof(*ptr_offset), payload, length);
958     } else {
959         device.valid_tag = 0xdeadbeae;
960         device.valid_bit = (count) << 2; // 2:bytes alignment
961         ptr_offset += NVRAM_ADDR_PAYLOAD_OFFSET;
962         memcpy_s(ptr_offset, sizeof(*ptr_offset), addr, sizeof(ble_addr_t));
963         ptr_offset = (uint8_t *)NVRAM_CCCD_SEC_PAYLOAD_OFFSET;
964         memcpy_s(ptr_offset, sizeof(*ptr_offset), payload, length);
965     }
966 
967     tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + idx, (void *)&device, 0);
968     return TRUE;
969 }
btif_config_store_our_sec(int idx,void * addr,uint8_t * payload,int length)970 int btif_config_store_our_sec(int idx, void *addr, uint8_t *payload, int length)
971 {
972     bt_remote_device_t device;
973     uint8_t *ptr_offset = (uint8_t *)&device;
974     assert(idx < BTM_SEC_MAX_BLE_DEVICE_RECORDS);
975     memset_s(&device, sizeof(device), 0, sizeof(device));
976     tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + idx, (void *)&device, 0);
977 
978     if ((device.valid_tag == 0xdeadbeae)) {
979         device.valid_bit |= 0x01;
980         ptr_offset += NVRAM_OUR_SEC_PAYLOAD_OFFSET;
981         memcpy_s(ptr_offset, sizeof(*ptr_offset), payload, length);
982     } else {
983         device.valid_tag = 0xdeadbeae;
984         device.valid_bit = 0x01;
985         ptr_offset += NVRAM_ADDR_PAYLOAD_OFFSET;
986         memcpy_s(ptr_offset, sizeof(*ptr_offset), addr, sizeof(ble_addr_t));
987         ptr_offset += sizeof(ble_addr_t);
988         memcpy_s(ptr_offset, sizeof(*ptr_offset), payload, length);
989     }
990 
991     tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + idx, (void *)&device, 0);
992     return 0;
993 }
994 
btif_config_store_peer_sec(int idx,void * addr,uint8_t * payload,int length)995 int btif_config_store_peer_sec(int idx, void *addr, uint8_t *payload, int length)
996 {
997     bt_remote_device_t device;
998     uint8_t *ptr_offset = (uint8_t *)&device;
999     assert(idx < BTM_SEC_MAX_BLE_DEVICE_RECORDS);
1000     memset_s(&device, sizeof(device), 0, sizeof(device));
1001     tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + idx, (void *)&device, 0);
1002 
1003     if ((device.valid_tag == 0xdeadbeae)) {
1004         device.valid_bit |= 0x02;
1005         ptr_offset += NVRAM_PEER_SEC_PAYLOAD_OFFSET;
1006         memcpy_s(ptr_offset, sizeof(*ptr_offset), payload, length);
1007     } else {
1008         device.valid_tag = 0xdeadbeae;
1009         device.valid_bit = 0x02;
1010         ptr_offset += NVRAM_ADDR_PAYLOAD_OFFSET;
1011         memcpy_s(ptr_offset, sizeof(*ptr_offset), addr, sizeof(ble_addr_t));
1012         ptr_offset = (uint8_t *)NVRAM_PEER_SEC_PAYLOAD_OFFSET;
1013         memcpy_s(ptr_offset, sizeof(*ptr_offset), payload, length);
1014     }
1015 
1016     tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + idx, (void *)&device, 0);
1017     return 0;
1018 }
1019 
btif_config_get_sec_cccd_item(int idx,void * addr,void * our_sec,int our_sec_size,void * peer_sec,int peer_sec_size,void * cccd_info,int cccd_info_size)1020 uint32_t btif_config_get_sec_cccd_item(int idx, void *addr, void *our_sec, int our_sec_size,
1021                                        void *peer_sec, int peer_sec_size, void *cccd_info, int cccd_info_size)
1022 {
1023     bt_remote_device_t device;
1024     uint8_t *ptr_offset = (uint8_t *)&device;
1025     uint32_t valid_bit = 0;
1026     assert(idx < BTM_SEC_MAX_BLE_DEVICE_RECORDS);
1027     memset_s(&device, sizeof(device), 0, sizeof(device));
1028     tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + idx, (void *)&device, 0);
1029 
1030     if ((device.valid_tag == 0xdeadbeae)) {
1031         valid_bit = device.valid_bit;
1032         memcpy_s(addr, sizeof(*addr), ptr_offset + NVRAM_ADDR_PAYLOAD_OFFSET, 7); // 7:bytes
1033         memcpy_s(our_sec, sizeof(*our_sec), ptr_offset + NVRAM_OUR_SEC_PAYLOAD_OFFSET, our_sec_size);
1034         memcpy_s(peer_sec, sizeof(*peer_sec), ptr_offset + NVRAM_PEER_SEC_PAYLOAD_OFFSET, peer_sec_size);
1035         memcpy_s(cccd_info, sizeof(*cccd_info), ptr_offset + NVRAM_CCCD_SEC_PAYLOAD_OFFSET, cccd_info_size);
1036     }
1037 
1038     return valid_bit;
1039 }
1040 
btif_config_delete_our_sec(int idx)1041 int btif_config_delete_our_sec(int idx)
1042 {
1043     bt_remote_device_t device;
1044     memset_s(&device, sizeof(device), 0, sizeof(device));
1045     assert(idx < BTM_SEC_MAX_BLE_DEVICE_RECORDS);
1046     tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + idx, (void *)&device, 0);
1047 
1048     if ((device.valid_tag == 0xdeadbeae)) {
1049         if (device.valid_bit & 0x01) {
1050             device.valid_bit &= (~0x01);
1051 
1052             if ((device.valid_bit & 0x03) == 0x00) {
1053                 device.valid_tag = 0x88888888; // invalid tag!!!
1054             }
1055 
1056             tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + idx, (void *)&device, 0);
1057         }
1058     }
1059 
1060     return 0;
1061 }
btif_config_delete_cccd(int idx)1062 int btif_config_delete_cccd(int idx)
1063 {
1064     bt_remote_device_t device;
1065     memset_s(&device, sizeof(device), 0, sizeof(device));
1066     assert(idx < BTM_SEC_MAX_BLE_DEVICE_RECORDS);
1067     tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + idx, (void *)&device, 0);
1068 
1069     if ((device.valid_tag == 0xdeadbeae)) {
1070         if (device.valid_bit & 0xFFFFFFFC) {
1071             device.valid_bit &= (0x03);
1072 
1073             if ((device.valid_bit & 0x03) == 0x00) {
1074                 device.valid_tag = 0x88888888; // invalid tag!!!
1075             }
1076 
1077             tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + idx, (void *)&device, 0);
1078         }
1079     }
1080 
1081     return 0;
1082 }
1083 
btif_config_delete_peer_sec(int idx)1084 int btif_config_delete_peer_sec(int idx)
1085 {
1086     bt_remote_device_t device;
1087     memset_s(&device, sizeof(device), 0, sizeof(device));
1088     assert(idx < BTM_SEC_MAX_BLE_DEVICE_RECORDS);
1089     tls_param_get(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + idx, (void *)&device, 0);
1090 
1091     if ((device.valid_tag == 0xdeadbeae)) {
1092         if (device.valid_bit & 0x02) {
1093             device.valid_bit &= (~0x02);
1094 
1095             if ((device.valid_bit & 0x03) == 0x00) { // omit the cccd counter;
1096                 device.valid_tag = 0x88888888; // invalid tag!!!
1097             }
1098 
1099             tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + idx, (void *)&device, 0);
1100         }
1101     }
1102 
1103     return 0;
1104 }
btif_config_delete_all(void)1105 int btif_config_delete_all(void)
1106 {
1107     int idx = 0;
1108     bt_remote_device_t device;
1109 
1110     device.valid_tag = 0x88888888;
1111     device.valid_bit = 0;
1112 
1113     for (idx = 0; idx < BTM_SEC_MAX_BLE_DEVICE_RECORDS; idx++) {
1114         tls_param_set(TLS_PARAM_ID_BT_REMOTE_DEVICE_1 + idx, (void *)&device, 0);
1115     }
1116 
1117     return 0;
1118 }
1119 
btif_config_store_key_map(const uint8_t * map_info,int length,bool force_flush)1120 int btif_config_store_key_map(const uint8_t *map_info, int length, bool force_flush)
1121 {
1122     bt_adapter_t adapter;
1123     tls_param_get(TLS_PARAM_ID_BT_ADAPTER, &adapter, 0);
1124 
1125     if (adapter.valid_tag != 0xdeadbeaf) {
1126         adapter.valid_tag = 0xdeadbeaf;
1127     }
1128 
1129     adapter.valid_bit |= ADAPTER_BLE_IR_VALID_BIT; // reuse the ADAPTER_BLE_IR_VALID_BIT to mark the map key value;
1130     memcpy_s(&adapter.ir[0], sizeof(adapter.ir[0]), map_info, length);
1131     tls_param_set(TLS_PARAM_ID_BT_ADAPTER, (void *)&adapter, 0);
1132 
1133     if (force_flush) {
1134         btif_config_flush(1);
1135     } else {
1136         ble_store_config_persist_flush();
1137     }
1138 
1139     return 0;
1140 }
1141 
btif_config_load_key_map(uint8_t * map_info,int length)1142 int btif_config_load_key_map(uint8_t *map_info, int length)
1143 {
1144     bt_adapter_t adapter;
1145     memset_s(&adapter, sizeof(adapter), 0, sizeof(adapter));
1146     tls_param_get(TLS_PARAM_ID_BT_ADAPTER, &adapter, 0);
1147 
1148     if ((adapter.valid_tag == 0xdeadbeaf) && (adapter.valid_bit & ADAPTER_BLE_IR_VALID_BIT)) {
1149         memcpy_s(map_info, sizeof(*map_info), &adapter.ir[0], length);
1150         return 0;
1151     } else {
1152         return -1;
1153     }
1154 }
1155 
1156 #if WM_BT_STORAGE_DEBUG_TAG == 1
1157 
1158 #ifndef CASE_RETURN_STR
1159 #define CASE_RETURN_STR(const) case const: return #const;
1160 #endif
1161 
nv_tag_2_str(uint8_t state)1162 static char *nv_tag_2_str(uint8_t state)
1163 {
1164     switch (state) {
1165         CASE_RETURN_STR(NV_LOCAL)
1166         CASE_RETURN_STR(NV_LOCAL_ADAPTER)
1167         CASE_RETURN_STR(NV_LOCAL_ADAPTER_ADDRESS)
1168         CASE_RETURN_STR(NV_LOCAL_ADAPTER_NAME)
1169         CASE_RETURN_STR(NV_LOCAL_ADAPTER_CLASS)
1170         CASE_RETURN_STR(NV_LOCAL_ADAPTER_IO_CAP)
1171         CASE_RETURN_STR(NV_LOCAL_ADAPTER_DISCOVERABLE)
1172         CASE_RETURN_STR(NV_LOCAL_ADAPTER_CONNECTABLE)
1173         CASE_RETURN_STR(NV_LOCAL_ADAPTER_DISCOVERY_TIMEOUT)
1174         CASE_RETURN_STR(NV_LOCAL_ADAPTER_AUTH_REQ)
1175         CASE_RETURN_STR(NV_LOCAL_ADAPTER_MODE)
1176         CASE_RETURN_STR(NV_LOCAL_ADAPTER_BLE_AUTH_REQ)
1177         CASE_RETURN_STR(NV_LOCAL_ADAPTER_BLE_IR)
1178         CASE_RETURN_STR(NV_LOCAL_ADAPTER_BLE_IRK)
1179         CASE_RETURN_STR(NV_LOCAL_ADAPTER_BLE_DHK)
1180         CASE_RETURN_STR(NV_LOCAL_ADAPTER_BLE_ER)
1181         CASE_RETURN_STR(NV_LOCAL_ADAPTER_MAX_TAG)
1182         CASE_RETURN_STR(NV_REMOTE)
1183         CASE_RETURN_STR(NV_REMOTE_ADDRESS)
1184         CASE_RETURN_STR(NV_REMOTE_NAME)
1185         CASE_RETURN_STR(NV_REMOTE_CLASS)
1186         CASE_RETURN_STR(NV_REMOTE_SERVICE)
1187         CASE_RETURN_STR(NV_REMOTE_LINK_KEY)
1188         CASE_RETURN_STR(NV_REMOTE_KEY_TYPE)
1189         CASE_RETURN_STR(NV_REMOTE_IO_CAP)
1190         CASE_RETURN_STR(NV_REMOTE_PIN_LENGTH)
1191         CASE_RETURN_STR(NV_REMOTE_DEVICE_TYPE)
1192         CASE_RETURN_STR(NV_REMOTE_BLE_ADDRESS_TYPE)
1193         CASE_RETURN_STR(NV_REMOTE_BLE_KEY_PENC)
1194         CASE_RETURN_STR(NV_REMOTE_BLE_KEY_LENC)
1195         CASE_RETURN_STR(NV_REMOTE_BLE_KEY_PID)
1196         CASE_RETURN_STR(NV_REMOTE_BLE_KEY_LID)
1197         CASE_RETURN_STR(NV_REMOTE_BLE_KEY_PCSRK)
1198         CASE_RETURN_STR(NV_REMOTE_BLE_KEY_LCSRK)
1199         CASE_RETURN_STR(NV_REMOTE_MANU)
1200         CASE_RETURN_STR(NV_REMOTE_LMPVER)
1201         CASE_RETURN_STR(NV_REMOTE_LMPSUBVER)
1202         CASE_RETURN_STR(NV_REMOTE_MAX_TAG)
1203 
1204         default:
1205             return "~~~~~~~~~~~~~~~~!!!Unknown nv tag ID!!!~~~~~~~~~~~~~~~~~";
1206     }
1207 }
1208 #endif
1209 
1210 #endif