-
Notifications
You must be signed in to change notification settings - Fork 1
/
configstore.c
469 lines (369 loc) · 10.8 KB
/
configstore.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
/*
* mµOS - my micro OS
*
* Copyright (C)
* 2018 Christian Thäter <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef MUOS_CONFIGSTORE
#include <muos/configstore.h>
#include <muos/eeprom.h>
#include <muos/io.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#ifdef MUOS_EEPROM_CRC16_INCLUDE
#include MUOS_EEPROM_CRC16_INCLUDE
#else
#error MUOS_EEPROM_CRC16_INCLUDE must be configured
#endif
#ifndef MUOS_EEPROM_CRC16_FN
#error MUOS_EEPROM_CRC16_FN must be configured
#endif
static
muos_configstore_status status = CONFIGSTORE_UNKNOWN;
#define ENTRY(type, ary, name, default, min, max, attr, descr) static const char __flash configstore_##name##_str[] = #name;
CONFIGSTORE_DATA_IMPL
#undef ENTRY
static
const char __flash * const __flash muos_configstore_names[] =
{
#define ENTRY(type, ary, name, default, min, max, attr, descr) configstore_##name##_str,
CONFIGSTORE_DATA_IMPL
#undef ENTRY
};
//PLANNED: do we need an array of type names?
static const uint8_t __flash type_size[] =
{
#define string char
#define TYPE(type) sizeof(type),
MUOS_CONFIGSTORE_TYPES
#undef TYPE
#undef string
};
struct muos_configstore_schema
{
enum muos_configstore_type type;
size_t offset;
uint8_t ary;
#ifdef MUOS_CONFIGSTORE_ATTRS
uint8_t attrs;
#endif
};
static const struct muos_configstore_schema __flash schema[] =
{
#define ARRAY(len) len
#ifdef MUOS_CONFIGSTORE_ATTRS
#define ENTRY(type, ary, name, default, min, max, attr, descr) \
{ \
MUOS_CONFIGSTORE_TYPE_##type, \
offsetof(struct muos_configstore_data, name), \
ary, \
attr, \
},
#else
#define ENTRY(type, ary, name, default, min, max, attr, descr) \
{ \
MUOS_CONFIGSTORE_TYPE_##type, \
offsetof(struct muos_configstore_data, name), \
ary \
},
#endif
CONFIGSTORE_DATA_IMPL
#undef ENTRY
#undef ARRAY
};
#ifdef MUOS_CONFIGSTORE_DEFAULTS
static const struct muos_configstore_data __flash configstore_defaults =
{
#define ENTRY(type, ary, name, default, min, max, attr, descr) default,
CONFIGSTORE_DATA_IMPL
#undef ENTRY
};
#endif
#ifdef MUOS_CONFIGSTORE_LIMITS
static const struct
{
int32_t min;
int32_t max;
} __flash limits[] =
{
#define ENTRY(type, ary, name, default, min, max, attr, descr) {min, max},
CONFIGSTORE_DATA_IMPL
#undef ENTRY
};
#endif
#ifdef MUOS_CONFIGSTORE_ATTRS
bool muos_configstore_authenticated;
#endif
struct muos_configstore_frame
{
struct muos_configstore_data userdata;
// uint32_t generation; only used for journaled
// uint8_t xor;
//PLANNED: bitmap instead generation
uint16_t crc; //PLANNED: on the fly
};
//TODO: calculate type from array capacity
//static uint16_t pos;
static struct muos_configstore_frame the_configstore;
static void*
muos_configstore_value (enum muos_configstore_id id, uint8_t index)
{
return (void*) &the_configstore + schema[id].offset + index * type_size[schema[id].type];
}
enum muos_configstore_type
muos_configstore_type (enum muos_configstore_id id)
{
return schema[id].type;
}
uint8_t
muos_configstore_ary (enum muos_configstore_id id)
{
return schema[id].ary;
}
muos_error
muos_configstore_output_value MUOS_IO_HWPARAM(enum muos_configstore_id id, uint8_t index)
{
if (status <= CONFIGSTORE_VALID)
return muos_error_configstore;
//TODO: not hidden
//TODO: not RPROT || auth
void* value = muos_configstore_value (id, index);
switch (schema[id].type)
{
case MUOS_CONFIGSTORE_TYPE_int8_t:
return muos_output_int8 MUOS_IO_HWARG(*(int8_t*) value);
case MUOS_CONFIGSTORE_TYPE_uint8_t:
return muos_output_uint8 MUOS_IO_HWARG(*(uint8_t*) value);
case MUOS_CONFIGSTORE_TYPE_int16_t:
return muos_output_int16 MUOS_IO_HWARG(*(int16_t*) value);
case MUOS_CONFIGSTORE_TYPE_uint16_t:
return muos_output_uint16 MUOS_IO_HWARG(*(uint16_t*) value);
case MUOS_CONFIGSTORE_TYPE_int32_t:
return muos_output_int32 MUOS_IO_HWARG(*(int32_t*) value);
case MUOS_CONFIGSTORE_TYPE_string:
return muos_output_cstr MUOS_IO_HWARG((const char*) value);
default:
return muos_fatal_error; /*will never happen*/
}
}
muos_error
muos_configstore_output_name MUOS_IO_HWPARAM(enum muos_configstore_id id)
{
//TODO: not hidden
return muos_output_fstr MUOS_IO_HWARG(muos_configstore_names[id]);
}
muos_error
muos_configstore_set (char* var, uint8_t index, char* val)
{
//TODO: not WPROT || auth
enum muos_configstore_id id;
for (id = 0; id < CONFIGSTORE_MAX_ID; ++id)
{
if (strcmp_P (var, muos_configstore_names[id]) == 0)
break;
}
if (id == CONFIGSTORE_MAX_ID)
return muos_error_configstore;
if (status != CONFIGSTORE_WLOCK
#ifdef MUOS_CONFIGSTORE_ATTRS
&& !(schema[id].attrs & CONFIGSTORE_MUTABLE)
#endif
)
return muos_error_configstore;
if (index > schema[id].ary)
return muos_error_configstore;
void* dest = muos_configstore_value (id, index);
long number = strtol (val, NULL, 0);
#ifdef MUOS_CONFIGSTORE_LIMITS
if (schema[id].type != MUOS_CONFIGSTORE_TYPE_string)
{
if (number < limits[id].min || number > limits[id].max)
return muos_error_configstore;
}
#endif
switch (schema[id].type)
{
case MUOS_CONFIGSTORE_TYPE_int8_t:
(*(int8_t*)dest) = number;
break;
case MUOS_CONFIGSTORE_TYPE_uint8_t:
(*(uint8_t*)dest) = number;
break;
case MUOS_CONFIGSTORE_TYPE_int16_t:
(*(int16_t*)dest) = number;
break;
case MUOS_CONFIGSTORE_TYPE_uint16_t:
(*(uint16_t*)dest) = number;
break;
case MUOS_CONFIGSTORE_TYPE_int32_t:
(*(int32_t*)dest) = number;
break;
case MUOS_CONFIGSTORE_TYPE_string:
if (index != 0 || strlen(val) >= schema[id].ary)
return muos_error_configstore;
strcpy (dest, val);
break;
default:
return muos_fatal_error; /*will never happen*/
}
return muos_success;
}
muos_configstore_status
muos_configstore_get_status (void)
{
return status;
};
const struct muos_configstore_data*
muos_configstore_lock (void)
{
if (status < CONFIGSTORE_RLOCK_MAX && status >= CONFIGSTORE_VALID)
{
++status;
return &the_configstore.userdata;
}
return NULL;
}
struct muos_configstore_data*
muos_configstore_wlock (void)
{
if (status == CONFIGSTORE_VALID)
{
status = CONFIGSTORE_WLOCK;
return &the_configstore.userdata;
}
return NULL;
}
void
muos_configstore_unlock (const struct muos_configstore_data** lock)
{
if (*lock)
{
if (status == CONFIGSTORE_WLOCK)
status = CONFIGSTORE_VALID;
else
--status;
*lock = NULL;
}
}
#ifdef MUOS_CONFIGSTORE_DEFAULTS
muos_error
muos_configstore_defaults (void)
{
if (status == CONFIGSTORE_WLOCK)
{
memcpy_P (&the_configstore.userdata, &configstore_defaults, sizeof(configstore_defaults));
the_configstore.userdata.config_size = sizeof (struct muos_configstore_data);
return muos_success;
}
return muos_error_configstore;
}
#endif
struct muos_configstore_data*
muos_configstore_initial (void)
{
if (status == CONFIGSTORE_INVALID)
{
status = CONFIGSTORE_WLOCK;
#ifdef MUOS_CONFIGSTORE_DEFAULTS
memcpy_P (&the_configstore.userdata, &configstore_defaults, sizeof(configstore_defaults));
#endif
the_configstore.userdata.config_size = sizeof (struct muos_configstore_data);
return &the_configstore.userdata;
}
return NULL;
}
static muos_configstore_callback callback;
//PLANNED: configstore_revert revert to an earlier config
static void
eeprom_read_done (void)
{
uint16_t crc = MUOS_EEPROM_CRC16_INIT;
for (size_t i = 0; i < sizeof(struct muos_configstore_frame); ++i)
{
crc = MUOS_EEPROM_CRC16_FN (crc, ((uint8_t*)&the_configstore)[i]);
}
if (crc == 0 && the_configstore.userdata.config_size == sizeof (struct muos_configstore_data))
{
status = CONFIGSTORE_VALID;
}
else
{
status = CONFIGSTORE_INVALID;
muos_error_set (muos_error_configstore);
}
if (callback)
callback ();
}
muos_error
muos_configstore_load (muos_configstore_callback cb)
{
if (status < CONFIGSTORE_RLOCK)
status = CONFIGSTORE_WLOCK;
else
return muos_error_configstore;
callback = cb;
//FIXME: simple non redundant version for now
muos_eeprom_read (&the_configstore,
MUOS_CONFIGSTORE_OFFSET,
sizeof(struct muos_configstore_frame),
eeprom_read_done);
//PLANNED: later, journaled
//pos = 0;
//scan for valid crc
//remember highest generations position
//finally load it
return muos_success;
}
static void
eeprom_write_done (void)
{
if (muos_error_peek (muos_error_eeprom_verify))
{
//FIXME: DEAD not as state but error, let states only reflect the state in memory
status = CONFIGSTORE_DEAD;
muos_error_set (muos_error_configstore);
}
else
{
status = CONFIGSTORE_VALID;
}
if (callback)
callback ();
}
muos_error
muos_configstore_save (muos_configstore_callback cb)
{
if (status != CONFIGSTORE_VALID)
return muos_error_configstore;
//PLANNED: call user defined verification function, checking the whole config
status = CONFIGSTORE_WLOCK; // needs wlock because the eeprom may turn out DEAD
callback = cb;
uint16_t crc = MUOS_EEPROM_CRC16_INIT;
for (size_t i = 0; i < sizeof(struct muos_configstore_data); ++i)
{
crc = MUOS_EEPROM_CRC16_FN (crc, ((uint8_t*)&the_configstore.userdata)[i]);
}
the_configstore.crc = crc;
//FIXME: smart write fails
muos_eeprom_write (&the_configstore,
MUOS_CONFIGSTORE_OFFSET,
sizeof(struct muos_configstore_frame),
eeprom_write_done);
//PLANNED: later, journaled
return muos_success;
}
#endif