1 /*
2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define LOG_TAG "bt_osi_config"
8 #include "esp_system.h"
9 #include "nvs_flash.h"
10 #include "nvs.h"
11
12 #include <ctype.h>
13 #include <errno.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "bt_common.h"
19 #include "osi/allocator.h"
20 #include "osi/config.h"
21 #include "osi/list.h"
22
23 #define CONFIG_FILE_MAX_SIZE (1536)//1.5k
24 #define CONFIG_FILE_DEFAULE_LENGTH (2048)
25 #define CONFIG_KEY "bt_cfg_key"
26 typedef struct {
27 char *key;
28 char *value;
29 } entry_t;
30
31 typedef struct {
32 char *name;
33 list_t *entries;
34 } section_t;
35
36 struct config_t {
37 list_t *sections;
38 };
39
40 // Empty definition; this type is aliased to list_node_t.
41 struct config_section_iter_t {};
42
43 static void config_parse(nvs_handle_t fp, config_t *config);
44
45 static section_t *section_new(const char *name);
46 static void section_free(void *ptr);
47 static section_t *section_find(const config_t *config, const char *section);
48
49 static entry_t *entry_new(const char *key, const char *value);
50 static void entry_free(void *ptr);
51 static entry_t *entry_find(const config_t *config, const char *section, const char *key);
52
config_new_empty(void)53 config_t *config_new_empty(void)
54 {
55 config_t *config = osi_calloc(sizeof(config_t));
56 if (!config) {
57 OSI_TRACE_ERROR("%s unable to allocate memory for config_t.\n", __func__);
58 goto error;
59 }
60
61 config->sections = list_new(section_free);
62 if (!config->sections) {
63 OSI_TRACE_ERROR("%s unable to allocate list for sections.\n", __func__);
64 goto error;
65 }
66
67 return config;
68
69 error:;
70 config_free(config);
71 return NULL;
72 }
73
config_new(const char * filename)74 config_t *config_new(const char *filename)
75 {
76 assert(filename != NULL);
77
78 config_t *config = config_new_empty();
79 if (!config) {
80 return NULL;
81 }
82
83 esp_err_t err;
84 nvs_handle_t fp;
85 err = nvs_open(filename, NVS_READWRITE, &fp);
86 if (err != ESP_OK) {
87 if (err == ESP_ERR_NVS_NOT_INITIALIZED) {
88 OSI_TRACE_ERROR("%s: NVS not initialized. "
89 "Call nvs_flash_init before initializing bluetooth.", __func__);
90 } else {
91 OSI_TRACE_ERROR("%s unable to open NVS namespace '%s'\n", __func__, filename);
92 }
93 config_free(config);
94 return NULL;
95 }
96
97 config_parse(fp, config);
98 nvs_close(fp);
99 return config;
100 }
101
config_free(config_t * config)102 void config_free(config_t *config)
103 {
104 if (!config) {
105 return;
106 }
107
108 list_free(config->sections);
109 osi_free(config);
110 }
111
config_has_section(const config_t * config,const char * section)112 bool config_has_section(const config_t *config, const char *section)
113 {
114 assert(config != NULL);
115 assert(section != NULL);
116
117 return (section_find(config, section) != NULL);
118 }
119
config_has_key(const config_t * config,const char * section,const char * key)120 bool config_has_key(const config_t *config, const char *section, const char *key)
121 {
122 assert(config != NULL);
123 assert(section != NULL);
124 assert(key != NULL);
125
126 return (entry_find(config, section, key) != NULL);
127 }
128
config_has_key_in_section(config_t * config,const char * key,char * key_value)129 bool config_has_key_in_section(config_t *config, const char *key, char *key_value)
130 {
131 OSI_TRACE_DEBUG("key = %s, value = %s", key, key_value);
132 for (const list_node_t *node = list_begin(config->sections); node != list_end(config->sections); node = list_next(node)) {
133 const section_t *section = (const section_t *)list_node(node);
134
135 for (const list_node_t *node = list_begin(section->entries); node != list_end(section->entries); node = list_next(node)) {
136 entry_t *entry = list_node(node);
137 OSI_TRACE_DEBUG("entry->key = %s, entry->value = %s", entry->key, entry->value);
138 if (!strcmp(entry->key, key) && !strcmp(entry->value, key_value)) {
139 OSI_TRACE_DEBUG("%s, the irk aready in the flash.", __func__);
140 return true;
141 }
142 }
143 }
144
145 return false;
146 }
147
config_get_int(const config_t * config,const char * section,const char * key,int def_value)148 int config_get_int(const config_t *config, const char *section, const char *key, int def_value)
149 {
150 assert(config != NULL);
151 assert(section != NULL);
152 assert(key != NULL);
153
154 entry_t *entry = entry_find(config, section, key);
155 if (!entry) {
156 return def_value;
157 }
158
159 char *endptr;
160 int ret = strtol(entry->value, &endptr, 0);
161 return (*endptr == '\0') ? ret : def_value;
162 }
163
config_get_bool(const config_t * config,const char * section,const char * key,bool def_value)164 bool config_get_bool(const config_t *config, const char *section, const char *key, bool def_value)
165 {
166 assert(config != NULL);
167 assert(section != NULL);
168 assert(key != NULL);
169
170 entry_t *entry = entry_find(config, section, key);
171 if (!entry) {
172 return def_value;
173 }
174
175 if (!strcmp(entry->value, "true")) {
176 return true;
177 }
178 if (!strcmp(entry->value, "false")) {
179 return false;
180 }
181
182 return def_value;
183 }
184
config_get_string(const config_t * config,const char * section,const char * key,const char * def_value)185 const char *config_get_string(const config_t *config, const char *section, const char *key, const char *def_value)
186 {
187 assert(config != NULL);
188 assert(section != NULL);
189 assert(key != NULL);
190
191 entry_t *entry = entry_find(config, section, key);
192 if (!entry) {
193 return def_value;
194 }
195
196 return entry->value;
197 }
198
config_set_int(config_t * config,const char * section,const char * key,int value)199 void config_set_int(config_t *config, const char *section, const char *key, int value)
200 {
201 assert(config != NULL);
202 assert(section != NULL);
203 assert(key != NULL);
204
205 char value_str[32] = { 0 };
206 sprintf(value_str, "%d", value);
207 config_set_string(config, section, key, value_str, false);
208 }
209
config_set_bool(config_t * config,const char * section,const char * key,bool value)210 void config_set_bool(config_t *config, const char *section, const char *key, bool value)
211 {
212 assert(config != NULL);
213 assert(section != NULL);
214 assert(key != NULL);
215
216 config_set_string(config, section, key, value ? "true" : "false", false);
217 }
218
config_set_string(config_t * config,const char * section,const char * key,const char * value,bool insert_back)219 void config_set_string(config_t *config, const char *section, const char *key, const char *value, bool insert_back)
220 {
221 section_t *sec = section_find(config, section);
222 if (!sec) {
223 sec = section_new(section);
224 if (insert_back) {
225 list_append(config->sections, sec);
226 } else {
227 list_prepend(config->sections, sec);
228 }
229 }
230
231 for (const list_node_t *node = list_begin(sec->entries); node != list_end(sec->entries); node = list_next(node)) {
232 entry_t *entry = list_node(node);
233 if (!strcmp(entry->key, key)) {
234 osi_free(entry->value);
235 entry->value = osi_strdup(value);
236 return;
237 }
238 }
239
240 entry_t *entry = entry_new(key, value);
241 list_append(sec->entries, entry);
242 }
243
config_remove_section(config_t * config,const char * section)244 bool config_remove_section(config_t *config, const char *section)
245 {
246 assert(config != NULL);
247 assert(section != NULL);
248
249 section_t *sec = section_find(config, section);
250 if (!sec) {
251 return false;
252 }
253
254 return list_remove(config->sections, sec);
255 }
256
config_remove_key(config_t * config,const char * section,const char * key)257 bool config_remove_key(config_t *config, const char *section, const char *key)
258 {
259 assert(config != NULL);
260 assert(section != NULL);
261 assert(key != NULL);
262 bool ret;
263
264 section_t *sec = section_find(config, section);
265 entry_t *entry = entry_find(config, section, key);
266 if (!sec || !entry) {
267 return false;
268 }
269
270 ret = list_remove(sec->entries, entry);
271 if (list_length(sec->entries) == 0) {
272 OSI_TRACE_DEBUG("%s remove section name:%s",__func__, section);
273 ret &= config_remove_section(config, section);
274 }
275 return ret;
276 }
277
config_section_begin(const config_t * config)278 const config_section_node_t *config_section_begin(const config_t *config)
279 {
280 assert(config != NULL);
281 return (const config_section_node_t *)list_begin(config->sections);
282 }
283
config_section_end(const config_t * config)284 const config_section_node_t *config_section_end(const config_t *config)
285 {
286 assert(config != NULL);
287 return (const config_section_node_t *)list_end(config->sections);
288 }
289
config_section_next(const config_section_node_t * node)290 const config_section_node_t *config_section_next(const config_section_node_t *node)
291 {
292 assert(node != NULL);
293 return (const config_section_node_t *)list_next((const list_node_t *)node);
294 }
295
config_section_name(const config_section_node_t * node)296 const char *config_section_name(const config_section_node_t *node)
297 {
298 assert(node != NULL);
299 const list_node_t *lnode = (const list_node_t *)node;
300 const section_t *section = (const section_t *)list_node(lnode);
301 return section->name;
302 }
303
get_config_size(const config_t * config)304 static int get_config_size(const config_t *config)
305 {
306 assert(config != NULL);
307
308 int w_len = 0, total_size = 0;
309
310 for (const list_node_t *node = list_begin(config->sections); node != list_end(config->sections); node = list_next(node)) {
311 const section_t *section = (const section_t *)list_node(node);
312 w_len = strlen(section->name) + strlen("[]\n");// format "[section->name]\n"
313 total_size += w_len;
314
315 for (const list_node_t *enode = list_begin(section->entries); enode != list_end(section->entries); enode = list_next(enode)) {
316 const entry_t *entry = (const entry_t *)list_node(enode);
317 w_len = strlen(entry->key) + strlen(entry->value) + strlen(" = \n");// format "entry->key = entry->value\n"
318 total_size += w_len;
319 }
320
321 // Only add a separating newline if there are more sections.
322 if (list_next(node) != list_end(config->sections)) {
323 total_size ++; //'\n'
324 } else {
325 break;
326 }
327 }
328 total_size ++; //'\0'
329 return total_size;
330 }
331
get_config_size_from_flash(nvs_handle_t fp)332 static int get_config_size_from_flash(nvs_handle_t fp)
333 {
334 assert(fp != 0);
335
336 esp_err_t err;
337 const size_t keyname_bufsz = sizeof(CONFIG_KEY) + 5 + 1; // including log10(sizeof(i))
338 char *keyname = osi_calloc(keyname_bufsz);
339 if (!keyname){
340 OSI_TRACE_ERROR("%s, malloc error\n", __func__);
341 return 0;
342 }
343 size_t length = CONFIG_FILE_DEFAULE_LENGTH;
344 size_t total_length = 0;
345 uint16_t i = 0;
346 snprintf(keyname, keyname_bufsz, "%s%d", CONFIG_KEY, 0);
347 err = nvs_get_blob(fp, keyname, NULL, &length);
348 if (err == ESP_ERR_NVS_NOT_FOUND) {
349 osi_free(keyname);
350 return 0;
351 }
352 if (err != ESP_OK) {
353 OSI_TRACE_ERROR("%s, error %d\n", __func__, err);
354 osi_free(keyname);
355 return 0;
356 }
357 total_length += length;
358 while (length == CONFIG_FILE_MAX_SIZE) {
359 length = CONFIG_FILE_DEFAULE_LENGTH;
360 snprintf(keyname, keyname_bufsz, "%s%d", CONFIG_KEY, ++i);
361 err = nvs_get_blob(fp, keyname, NULL, &length);
362
363 if (err == ESP_ERR_NVS_NOT_FOUND) {
364 break;
365 }
366 if (err != ESP_OK) {
367 OSI_TRACE_ERROR("%s, error %d\n", __func__, err);
368 osi_free(keyname);
369 return 0;
370 }
371 total_length += length;
372 }
373 osi_free(keyname);
374 return total_length;
375 }
376
config_save(const config_t * config,const char * filename)377 bool config_save(const config_t *config, const char *filename)
378 {
379 assert(config != NULL);
380 assert(filename != NULL);
381 assert(*filename != '\0');
382
383 esp_err_t err;
384 int err_code = 0;
385 nvs_handle_t fp;
386 char *line = osi_calloc(1024);
387 const size_t keyname_bufsz = sizeof(CONFIG_KEY) + 5 + 1; // including log10(sizeof(i))
388 char *keyname = osi_calloc(keyname_bufsz);
389 int config_size = get_config_size(config);
390 char *buf = osi_calloc(config_size);
391 if (!line || !buf || !keyname) {
392 err_code |= 0x01;
393 goto error;
394 }
395
396 err = nvs_open(filename, NVS_READWRITE, &fp);
397 if (err != ESP_OK) {
398 if (err == ESP_ERR_NVS_NOT_INITIALIZED) {
399 OSI_TRACE_ERROR("%s: NVS not initialized. "
400 "Call nvs_flash_init before initializing bluetooth.", __func__);
401 }
402 err_code |= 0x02;
403 goto error;
404 }
405
406 int w_cnt, w_cnt_total = 0;
407 for (const list_node_t *node = list_begin(config->sections); node != list_end(config->sections); node = list_next(node)) {
408 const section_t *section = (const section_t *)list_node(node);
409 w_cnt = snprintf(line, 1024, "[%s]\n", section->name);
410 if(w_cnt < 0) {
411 OSI_TRACE_ERROR("snprintf error w_cnt %d.",w_cnt);
412 err_code |= 0x10;
413 goto error;
414 }
415 if(w_cnt_total + w_cnt > config_size) {
416 OSI_TRACE_ERROR("%s, memcpy size (w_cnt + w_cnt_total = %d) is larger than buffer size (config_size = %d).", __func__, (w_cnt + w_cnt_total), config_size);
417 err_code |= 0x20;
418 goto error;
419 }
420 OSI_TRACE_DEBUG("section name: %s, w_cnt + w_cnt_total = %d\n", section->name, w_cnt + w_cnt_total);
421 memcpy(buf + w_cnt_total, line, w_cnt);
422 w_cnt_total += w_cnt;
423
424 for (const list_node_t *enode = list_begin(section->entries); enode != list_end(section->entries); enode = list_next(enode)) {
425 const entry_t *entry = (const entry_t *)list_node(enode);
426 OSI_TRACE_DEBUG("(key, val): (%s, %s)\n", entry->key, entry->value);
427 w_cnt = snprintf(line, 1024, "%s = %s\n", entry->key, entry->value);
428 if(w_cnt < 0) {
429 OSI_TRACE_ERROR("snprintf error w_cnt %d.",w_cnt);
430 err_code |= 0x10;
431 goto error;
432 }
433 if(w_cnt_total + w_cnt > config_size) {
434 OSI_TRACE_ERROR("%s, memcpy size (w_cnt + w_cnt_total = %d) is larger than buffer size.(config_size = %d)", __func__, (w_cnt + w_cnt_total), config_size);
435 err_code |= 0x20;
436 goto error;
437 }
438 OSI_TRACE_DEBUG("%s, w_cnt + w_cnt_total = %d", __func__, w_cnt + w_cnt_total);
439 memcpy(buf + w_cnt_total, line, w_cnt);
440 w_cnt_total += w_cnt;
441 }
442
443 // Only add a separating newline if there are more sections.
444 if (list_next(node) != list_end(config->sections)) {
445 buf[w_cnt_total] = '\n';
446 w_cnt_total += 1;
447 } else {
448 break;
449 }
450 }
451 buf[w_cnt_total] = '\0';
452 if (w_cnt_total < CONFIG_FILE_MAX_SIZE) {
453 snprintf(keyname, keyname_bufsz, "%s%d", CONFIG_KEY, 0);
454 err = nvs_set_blob(fp, keyname, buf, w_cnt_total);
455 if (err != ESP_OK) {
456 nvs_close(fp);
457 err_code |= 0x04;
458 goto error;
459 }
460 }else {
461 int count = (w_cnt_total / CONFIG_FILE_MAX_SIZE);
462 assert(count <= 0xFF);
463 for (uint8_t i = 0; i <= count; i++)
464 {
465 snprintf(keyname, keyname_bufsz, "%s%d", CONFIG_KEY, i);
466 if (i == count) {
467 err = nvs_set_blob(fp, keyname, buf + i*CONFIG_FILE_MAX_SIZE, w_cnt_total - i*CONFIG_FILE_MAX_SIZE);
468 OSI_TRACE_DEBUG("save keyname = %s, i = %d, %d\n", keyname, i, w_cnt_total - i*CONFIG_FILE_MAX_SIZE);
469 }else {
470 err = nvs_set_blob(fp, keyname, buf + i*CONFIG_FILE_MAX_SIZE, CONFIG_FILE_MAX_SIZE);
471 OSI_TRACE_DEBUG("save keyname = %s, i = %d, %d\n", keyname, i, CONFIG_FILE_MAX_SIZE);
472 }
473 if (err != ESP_OK) {
474 nvs_close(fp);
475 err_code |= 0x04;
476 goto error;
477 }
478 }
479 }
480
481 err = nvs_commit(fp);
482 if (err != ESP_OK) {
483 nvs_close(fp);
484 err_code |= 0x08;
485 goto error;
486 }
487
488 nvs_close(fp);
489 osi_free(line);
490 osi_free(buf);
491 osi_free(keyname);
492 return true;
493
494 error:
495 if (buf) {
496 osi_free(buf);
497 }
498 if (line) {
499 osi_free(line);
500 }
501 if (keyname) {
502 osi_free(keyname);
503 }
504 if (err_code) {
505 OSI_TRACE_ERROR("%s, err_code: 0x%x\n", __func__, err_code);
506 }
507 return false;
508 }
509
trim(char * str)510 static char *trim(char *str)
511 {
512 while (isspace((unsigned char)(*str))) {
513 ++str;
514 }
515
516 if (!*str) {
517 return str;
518 }
519
520 char *end_str = str + strlen(str) - 1;
521 while (end_str > str && isspace((unsigned char)(*end_str))) {
522 --end_str;
523 }
524
525 end_str[1] = '\0';
526 return str;
527 }
528
config_parse(nvs_handle_t fp,config_t * config)529 static void config_parse(nvs_handle_t fp, config_t *config)
530 {
531 assert(fp != 0);
532 assert(config != NULL);
533
534 esp_err_t err;
535 int line_num = 0;
536 int err_code = 0;
537 uint16_t i = 0;
538 size_t length = CONFIG_FILE_DEFAULE_LENGTH;
539 size_t total_length = 0;
540 char *line = osi_calloc(1024);
541 char *section = osi_calloc(1024);
542 const size_t keyname_bufsz = sizeof(CONFIG_KEY) + 5 + 1; // including log10(sizeof(i))
543 char *keyname = osi_calloc(keyname_bufsz);
544 int buf_size = get_config_size_from_flash(fp);
545 char *buf = NULL;
546
547 if(buf_size == 0) { //First use nvs
548 goto error;
549 }
550 buf = osi_calloc(buf_size);
551 if (!line || !section || !buf || !keyname) {
552 err_code |= 0x01;
553 goto error;
554 }
555 snprintf(keyname, keyname_bufsz, "%s%d", CONFIG_KEY, 0);
556 err = nvs_get_blob(fp, keyname, buf, &length);
557 if (err == ESP_ERR_NVS_NOT_FOUND) {
558 goto error;
559 }
560 if (err != ESP_OK) {
561 err_code |= 0x02;
562 goto error;
563 }
564 total_length += length;
565 while (length == CONFIG_FILE_MAX_SIZE) {
566 length = CONFIG_FILE_DEFAULE_LENGTH;
567 snprintf(keyname, keyname_bufsz, "%s%d", CONFIG_KEY, ++i);
568 err = nvs_get_blob(fp, keyname, buf + CONFIG_FILE_MAX_SIZE * i, &length);
569
570 if (err == ESP_ERR_NVS_NOT_FOUND) {
571 break;
572 }
573 if (err != ESP_OK) {
574 err_code |= 0x02;
575 goto error;
576 }
577 total_length += length;
578 }
579 char *p_line_end;
580 char *p_line_bgn = buf;
581 strcpy(section, CONFIG_DEFAULT_SECTION);
582
583 while ( (p_line_bgn < buf + total_length - 1) && (p_line_end = strchr(p_line_bgn, '\n'))) {
584
585 // get one line
586 int line_len = p_line_end - p_line_bgn;
587 if (line_len > 1023) {
588 OSI_TRACE_WARNING("%s exceed max line length on line %d.\n", __func__, line_num);
589 break;
590 }
591 memcpy(line, p_line_bgn, line_len);
592 line[line_len] = '\0';
593 p_line_bgn = p_line_end + 1;
594 char *line_ptr = trim(line);
595 ++line_num;
596
597 // Skip blank and comment lines.
598 if (*line_ptr == '\0' || *line_ptr == '#') {
599 continue;
600 }
601
602 if (*line_ptr == '[') {
603 size_t len = strlen(line_ptr);
604 if (line_ptr[len - 1] != ']') {
605 OSI_TRACE_WARNING("%s unterminated section name on line %d.\n", __func__, line_num);
606 continue;
607 }
608 strncpy(section, line_ptr + 1, len - 2);
609 section[len - 2] = '\0';
610 } else {
611 char *split = strchr(line_ptr, '=');
612 if (!split) {
613 OSI_TRACE_DEBUG("%s no key/value separator found on line %d.\n", __func__, line_num);
614 continue;
615 }
616 *split = '\0';
617 config_set_string(config, section, trim(line_ptr), trim(split + 1), true);
618 }
619 }
620
621 error:
622 if (buf) {
623 osi_free(buf);
624 }
625 if (line) {
626 osi_free(line);
627 }
628 if (section) {
629 osi_free(section);
630 }
631 if (keyname) {
632 osi_free(keyname);
633 }
634 if (err_code) {
635 OSI_TRACE_ERROR("%s returned with err code: %d\n", __func__, err_code);
636 }
637 }
638
section_new(const char * name)639 static section_t *section_new(const char *name)
640 {
641 section_t *section = osi_calloc(sizeof(section_t));
642 if (!section) {
643 return NULL;
644 }
645
646 section->name = osi_strdup(name);
647 section->entries = list_new(entry_free);
648 return section;
649 }
650
section_free(void * ptr)651 static void section_free(void *ptr)
652 {
653 if (!ptr) {
654 return;
655 }
656
657 section_t *section = ptr;
658 osi_free(section->name);
659 list_free(section->entries);
660 osi_free(section);
661 }
662
section_find(const config_t * config,const char * section)663 static section_t *section_find(const config_t *config, const char *section)
664 {
665 for (const list_node_t *node = list_begin(config->sections); node != list_end(config->sections); node = list_next(node)) {
666 section_t *sec = list_node(node);
667 if (!strcmp(sec->name, section)) {
668 return sec;
669 }
670 }
671
672 return NULL;
673 }
674
entry_new(const char * key,const char * value)675 static entry_t *entry_new(const char *key, const char *value)
676 {
677 entry_t *entry = osi_calloc(sizeof(entry_t));
678 if (!entry) {
679 return NULL;
680 }
681
682 entry->key = osi_strdup(key);
683 entry->value = osi_strdup(value);
684 return entry;
685 }
686
entry_free(void * ptr)687 static void entry_free(void *ptr)
688 {
689 if (!ptr) {
690 return;
691 }
692
693 entry_t *entry = ptr;
694 osi_free(entry->key);
695 osi_free(entry->value);
696 osi_free(entry);
697 }
698
entry_find(const config_t * config,const char * section,const char * key)699 static entry_t *entry_find(const config_t *config, const char *section, const char *key)
700 {
701 section_t *sec = section_find(config, section);
702 if (!sec) {
703 return NULL;
704 }
705
706 for (const list_node_t *node = list_begin(sec->entries); node != list_end(sec->entries); node = list_next(node)) {
707 entry_t *entry = list_node(node);
708 if (!strcmp(entry->key, key)) {
709 return entry;
710 }
711 }
712
713 return NULL;
714 }
715