-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
settings.c
550 lines (426 loc) · 13.4 KB
/
settings.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
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2018, Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is part of the Simba project.
*/
#include "simba.h"
struct module_t {
int8_t initialized;
#if CONFIG_SETTINGS_FS_COMMAND_LIST == 1
struct fs_command_t cmd_list;
#endif
#if CONFIG_SETTINGS_FS_COMMAND_RESET == 1
struct fs_command_t cmd_reset;
#endif
#if CONFIG_SETTINGS_FS_COMMAND_READ == 1
struct fs_command_t cmd_read;
#endif
#if CONFIG_SETTINGS_FS_COMMAND_WRITE == 1
struct fs_command_t cmd_write;
#endif
};
static struct module_t module;
extern const FAR struct setting_t settings[];
const FAR uint8_t settings_default[CONFIG_SETTINGS_AREA_SIZE]
__attribute__ ((weak)) = { 0xff, };
static const FAR struct setting_t *get_setting_by_name(
const char *name_p)
{
const FAR struct setting_t *setting_p;
setting_p = &settings[0];
while (setting_p->name_p != NULL) {
if (std_strcmp(name_p, setting_p->name_p) == 0) {
return (setting_p);
}
setting_p++;
}
return (NULL);
}
#if CONFIG_SETTINGS_FS_COMMAND_LIST == 1
static int cmd_list_cb(int argc,
const char *argv[],
void *chout_p,
void *chin_p,
void *arg_p,
void *call_arg_p)
{
const FAR struct setting_t *setting_p;
int i;
int32_t int32;
char buf[41];
size_t size;
/* Print the header. */
std_fprintf(chout_p,
OSTR("NAME TYPE SIZE VALUE\r\n"));
/* Print all settings. */
setting_p = &settings[0];
while (setting_p->name_p != NULL) {
std_strcpy(&buf[0], setting_p->name_p);
std_fprintf(chout_p, OSTR("%-40s "), &buf[0]);
size = setting_p->size;
switch (setting_p->type) {
case setting_type_int32_t:
int32 = 0;
(void)settings_read(&int32, setting_p->address, size);
std_fprintf(chout_p,
OSTR("int32_t 4 %ld\r\n"),
(long)int32);
break;
case setting_type_string_t:
std_fprintf(chout_p, OSTR("string_t %4u "), (int)size);
for (i = 0; i < size; i++) {
(void)settings_read(&buf[0], setting_p->address + i, 1);
if (buf[0] == '\0') {
break;
}
std_fprintf(chout_p, OSTR("%c"), buf[0]);
}
std_fprintf(chout_p, OSTR("\r\n"));
break;
#if CONFIG_SETTINGS_BLOB == 1
case setting_type_blob_t:
std_fprintf(chout_p, OSTR("blob_t %4u "), (int)size);
for (i = 0; i < size; i++) {
buf[0] = 0x00;
(void)settings_read(&buf[0], setting_p->address + i, 1);
std_fprintf(chout_p, OSTR("%02x"), buf[0] & 0xff);
}
std_fprintf(chout_p, OSTR("\r\n"));
break;
#endif
default:
std_fprintf(chout_p,
OSTR("bad setting type %d\r\n"),
setting_p->type);
}
setting_p++;
}
return (0);
}
#endif
#if CONFIG_SETTINGS_FS_COMMAND_RESET == 1
static int cmd_reset_cb(int argc,
const char *argv[],
void *chout_p,
void *chin_p,
void *arg_p,
void *call_arg_p)
{
const FAR struct setting_t *setting_p;
if (argc > 2) {
std_fprintf(chout_p, OSTR("Usage: reset [<name>]\r\n"));
return (-EINVAL);
}
if (argc == 1) {
return (settings_reset_all());
} else {
setting_p = get_setting_by_name(argv[1]);
if (setting_p == NULL) {
std_fprintf(chout_p, OSTR("%s: setting not found\r\n"), argv[1]);
return (-EINVAL);
}
return (settings_reset(setting_p->address, setting_p->size));
}
return (-EINVAL);
}
#endif
#if CONFIG_SETTINGS_FS_COMMAND_READ == 1
static int cmd_read_cb(int argc,
const char *argv[],
void *chout_p,
void *chin_p,
void *arg_p,
void *call_arg_p)
{
const FAR struct setting_t *setting_p;
int i;
int32_t int32;
char buf[1];
if (argc != 2) {
std_fprintf(chout_p, OSTR("Usage: read <name>\r\n"));
return (-EINVAL);
}
setting_p = get_setting_by_name(argv[1]);
if (setting_p == NULL) {
std_fprintf(chout_p, OSTR("%s: setting not found\r\n"), argv[1]);
return (-EINVAL);
}
switch (setting_p->type) {
case setting_type_int32_t:
int32 = 0;
settings_read(&int32, setting_p->address, setting_p->size);
std_fprintf(chout_p, OSTR("%ld\r\n"), (long)int32);
break;
case setting_type_string_t:
for (i = 0; i < setting_p->size; i++) {
buf[0] = '\0';
settings_read(&buf[0], setting_p->address + i, 1);
if (buf[0] == '\0') {
break;
}
std_fprintf(chout_p, OSTR("%c"), buf[0]);
}
std_fprintf(chout_p, OSTR("\r\n"));
break;
#if CONFIG_SETTINGS_BLOB == 1
case setting_type_blob_t:
for (i = 0; i < setting_p->size; i++) {
buf[0] = 0;
settings_read(&buf[0], setting_p->address + i, 1);
std_fprintf(chout_p, OSTR("%02x"), buf[0] & 0xff);
}
std_fprintf(chout_p, OSTR("\r\n"));
break;
#endif
default:
std_fprintf(chout_p,
OSTR("bad setting type %d\r\n"),
setting_p->type);
}
return (0);
}
#endif
#if CONFIG_SETTINGS_FS_COMMAND_WRITE == 1
static int cmd_write_cb(int argc,
const char *argv[],
void *chout_p,
void *chin_p,
void *arg_p,
void *call_arg_p)
{
const FAR struct setting_t *setting_p;
size_t size;
long value;
int32_t int32;
char buf[5];
char *buf_p;
int i;
if (argc != 3) {
std_fprintf(chout_p, OSTR("Usage: write <name> <value>\r\n"));
return (-EINVAL);
}
setting_p = get_setting_by_name(argv[1]);
if (setting_p == NULL) {
std_fprintf(chout_p, OSTR("%s: setting not found\r\n"), argv[1]);
return (-EINVAL);
}
switch (setting_p->type) {
case setting_type_int32_t:
if (std_strtol(argv[2], &value) == NULL) {
return (-EINVAL);
}
/* Range check. */
if ((value > 2147483647) || (value < -2147483648)) {
std_fprintf(chout_p,
OSTR("%ld: value out of range\r\n"),
value);
return (-EINVAL);
}
int32 = (int32_t)value;
settings_write(setting_p->address, &int32, setting_p->size);
break;
case setting_type_string_t:
/* Range check. */
if (strlen(argv[2]) >= setting_p->size) {
std_fprintf(chout_p,
OSTR("%s: string too long\r\n"),
argv[2]);
return (-EINVAL);
}
settings_write(setting_p->address, argv[2], setting_p->size);
break;
#if CONFIG_SETTINGS_BLOB == 1
case setting_type_blob_t:
/* Range check. */
size = DIV_CEIL(strlen(argv[2]), 2);
if (size != setting_p->size) {
std_fprintf(chout_p,
OSTR("%u: bad blob data length\r\n"),
size);
return (-EINVAL);
}
/* For odd number of bytes the check will fail since
the null termination is not a hexadecimal digit. */
for (i = 0; i < 2 * size; i += 2) {
if (!(isxdigit((int)argv[2][i])
&& isxdigit((int)argv[2][i + 1]))) {
std_fprintf(chout_p,
OSTR("%s: bad blob data\r\n"),
argv[2]);
return (-EINVAL);
}
}
/* For std_strtol(). */
buf[0] = '0';
buf[1] = 'x';
buf[4] = '\0';
/* argv pointers shall not be const in the furute. */
buf_p = (char *)argv[2];
for (i = 0; i < size; i++) {
memcpy(&buf[2], &argv[2][2 * i], 2);
(void)std_strtol(&buf[0], &value);
*buf_p++ = value;
}
settings_write(setting_p->address, argv[2], size);
break;
#endif
default:
std_fprintf(chout_p,
OSTR("bad setting type %d\r\n"),
setting_p->type);
}
return (0);
}
#endif
static int reset(size_t address, size_t size)
{
size_t i;
size_t left;
uint8_t buf[128];
left = size;
while (left > 0) {
if (left < sizeof(buf)) {
size = left;
} else {
size = sizeof(buf);
}
for (i = 0; i < size; i++) {
buf[i] = settings_default[address + i];
}
if (nvm_write(address, &buf[0], size) != size) {
return (-1);
}
address += size;
left -= size;
}
return (0);
}
int settings_module_init(void)
{
/* Return immediately if the module is already initialized. */
if (module.initialized == 1) {
return (0);
}
module.initialized = 1;
#if CONFIG_SETTINGS_FS_COMMAND_LIST == 1
fs_command_init(&module.cmd_list,
CSTR("/oam/settings/list"),
cmd_list_cb,
NULL);
fs_command_register(&module.cmd_list);
#endif
#if CONFIG_SETTINGS_FS_COMMAND_RESET == 1
fs_command_init(&module.cmd_reset,
CSTR("/oam/settings/reset"),
cmd_reset_cb,
NULL);
fs_command_register(&module.cmd_reset);
#endif
#if CONFIG_SETTINGS_FS_COMMAND_READ == 1
fs_command_init(&module.cmd_read,
CSTR("/oam/settings/read"),
cmd_read_cb,
NULL);
fs_command_register(&module.cmd_read);
#endif
#if CONFIG_SETTINGS_FS_COMMAND_WRITE == 1
fs_command_init(&module.cmd_write,
CSTR("/oam/settings/write"),
cmd_write_cb,
NULL);
fs_command_register(&module.cmd_write);
#endif
return (nvm_module_init());
}
ssize_t settings_read(void *dst_p, size_t src, size_t size)
{
ASSERTN(dst_p != NULL, EINVAL);
ASSERTN(size > 0, EINVAL);
return (nvm_read(dst_p, src, size));
}
ssize_t settings_write(size_t dst, const void *src_p, size_t size)
{
ASSERTN(src_p != NULL, EINVAL);
ASSERTN(size > 0, EINVAL);
return (nvm_write(dst, src_p, size));
}
int settings_reset(size_t address, size_t size)
{
ASSERTN(address < sizeof(settings_default), EINVAL);
ASSERTN((address + size) <= sizeof(settings_default), EINVAL);
return (reset(address, size));
}
ssize_t settings_read_by_name(const char *name_p,
void *dst_p,
size_t size)
{
ASSERTN(name_p != NULL, EINVAL);
ASSERTN(dst_p != NULL, EINVAL);
ASSERTN(size > 0, EINVAL);
const FAR struct setting_t *setting_p;
setting_p = get_setting_by_name(name_p);
if (setting_p == NULL) {
return (-EINVAL);
}
if (size > setting_p->size) {
return (-EINVAL);
}
return (settings_read(dst_p, setting_p->address, size));
}
ssize_t settings_write_by_name(const char *name_p,
const void *src_p,
size_t size)
{
ASSERTN(name_p != NULL, EINVAL);
ASSERTN(src_p != NULL, EINVAL);
ASSERTN(size > 0, EINVAL);
const FAR struct setting_t *setting_p;
setting_p = get_setting_by_name(name_p);
if (setting_p == NULL) {
return (-EINVAL);
}
if (size > setting_p->size) {
return (-EINVAL);
}
return (settings_write(setting_p->address, src_p, size));
}
int settings_reset_by_name(const char *name_p,
size_t size)
{
ASSERTN(name_p != NULL, EINVAL);
const FAR struct setting_t *setting_p;
setting_p = get_setting_by_name(name_p);
if (setting_p == NULL) {
return (-EINVAL);
}
if (size > setting_p->size) {
return (-EINVAL);
}
return (reset(setting_p->address, size));
}
int settings_reset_all()
{
return (reset(0, sizeof(settings_default)));
}