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