-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathConfiguration.cpp
More file actions
352 lines (288 loc) · 10.1 KB
/
Configuration.cpp
File metadata and controls
352 lines (288 loc) · 10.1 KB
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
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Core/Configuration.h>
#include <MicroOcpp/Core/ConfigurationContainerFlash.h>
#include <MicroOcpp/Core/Memory.h>
#include <MicroOcpp/Debug.h>
#include <string.h>
#include <algorithm>
#include <climits>
#include <ArduinoJson.h>
namespace MicroOcpp {
struct Validator {
const char *key = nullptr;
std::function<bool(const char*)> checkValue;
Validator(const char *key, std::function<bool(const char*)> checkValue) : key(key), checkValue(checkValue) {
}
};
namespace ConfigurationLocal {
std::shared_ptr<FilesystemAdapter> filesystem;
auto configurationContainers = makeVector<std::shared_ptr<ConfigurationContainer>>("v16.Configuration.Containers");
auto validators = makeVector<Validator>("v16.Configuration.Validators");
}
using namespace ConfigurationLocal;
std::unique_ptr<ConfigurationContainer> createConfigurationContainer(const char *filename, bool accessible) {
//create non-persistent Configuration store (i.e. lives only in RAM) if
// - Flash FS usage is switched off OR
// - Filename starts with "/volatile"
if (!filesystem ||
!strncmp(filename, CONFIGURATION_VOLATILE, strlen(CONFIGURATION_VOLATILE))) {
return makeConfigurationContainerVolatile(filename, accessible);
} else {
//create persistent Configuration store. This is the normal case
return makeConfigurationContainerFlash(filesystem, filename, accessible);
}
}
void addConfigurationContainer(std::shared_ptr<ConfigurationContainer> container) {
configurationContainers.push_back(container);
}
std::shared_ptr<ConfigurationContainer> getContainer(const char *filename) {
auto container = std::find_if(configurationContainers.begin(), configurationContainers.end(),
[filename](decltype(configurationContainers)::value_type &elem) {
return !strcmp(elem->getFilename(), filename);
});
if (container != configurationContainers.end()) {
return *container;
} else {
return nullptr;
}
}
ConfigurationContainer *declareContainer(const char *filename, bool accessible) {
auto container = getContainer(filename);
if (!container) {
MO_DBG_DEBUG("init new configurations container: %s", filename);
container = createConfigurationContainer(filename, accessible);
if (!container) {
MO_DBG_ERR("OOM");
return nullptr;
}
configurationContainers.push_back(container);
}
if (container->isAccessible() != accessible) {
MO_DBG_ERR("%s: conflicting accessibility declarations (expect %s)", filename, container->isAccessible() ? "accessible" : "inaccessible");
}
return container.get();
}
std::shared_ptr<Configuration> loadConfiguration(TConfig type, const char *key, bool accessible) {
for (auto& container : configurationContainers) {
if (auto config = container->getConfiguration(key)) {
if (config->getType() != type) {
MO_DBG_ERR("conflicting type for %s - remove old config", key);
container->remove(config.get());
continue;
}
if (container->isAccessible() != accessible) {
MO_DBG_ERR("conflicting accessibility for %s", key);
}
container->loadStaticKey(*config.get(), key);
return config;
}
}
return nullptr;
}
template<class T>
bool loadFactoryDefault(Configuration& config, T loadFactoryDefault);
template<>
bool loadFactoryDefault<int>(Configuration& config, int factoryDef) {
config.setInt(factoryDef);
return true;
}
template<>
bool loadFactoryDefault<bool>(Configuration& config, bool factoryDef) {
config.setBool(factoryDef);
return true;
}
template<>
bool loadFactoryDefault<const char*>(Configuration& config, const char *factoryDef) {
return config.setString(factoryDef);
}
void loadPermissions(Configuration& config, bool readonly, bool rebootRequired) {
if (readonly) {
config.setReadOnly();
}
if (rebootRequired) {
config.setRebootRequired();
}
}
template<class T>
std::shared_ptr<Configuration> declareConfiguration(const char *key, T factoryDef, const char *filename, bool readonly, bool rebootRequired, bool accessible) {
std::shared_ptr<Configuration> res = loadConfiguration(convertType<T>(), key, accessible);
if (!res) {
auto container = declareContainer(filename, accessible);
if (!container) {
return nullptr;
}
res = container->createConfiguration(convertType<T>(), key);
if (!res) {
return nullptr;
}
if (!loadFactoryDefault(*res.get(), factoryDef)) {
container->remove(res.get());
return nullptr;
}
}
loadPermissions(*res.get(), readonly, rebootRequired);
return res;
}
template std::shared_ptr<Configuration> declareConfiguration<int>(const char *key, int factoryDef, const char *filename, bool readonly, bool rebootRequired, bool accessible);
template std::shared_ptr<Configuration> declareConfiguration<bool>(const char *key, bool factoryDef, const char *filename, bool readonly, bool rebootRequired, bool accessible);
template std::shared_ptr<Configuration> declareConfiguration<const char*>(const char *key, const char *factoryDef, const char *filename, bool readonly, bool rebootRequired, bool accessible);
std::function<bool(const char*)> *getConfigurationValidator(const char *key) {
for (auto& v : validators) {
if (!strcmp(v.key, key)) {
return &v.checkValue;
}
}
return nullptr;
}
void registerConfigurationValidator(const char *key, std::function<bool(const char*)> validator) {
for (auto& v : validators) {
if (!strcmp(v.key, key)) {
v.checkValue = validator;
return;
}
}
validators.push_back(Validator{key, validator});
}
Configuration *getConfigurationPublic(const char *key) {
for (auto& container : configurationContainers) {
if (container->isAccessible()) {
if (auto res = container->getConfiguration(key)) {
return res.get();
}
}
}
return nullptr;
}
Vector<ConfigurationContainer*> getConfigurationContainersPublic() {
auto res = makeVector<ConfigurationContainer*>("v16.Configuration.Containers");
for (auto& container : configurationContainers) {
if (container->isAccessible()) {
res.push_back(container.get());
}
}
return res;
}
bool configuration_init(std::shared_ptr<FilesystemAdapter> _filesystem) {
filesystem = _filesystem;
return true;
}
void configuration_deinit() {
makeVector<decltype(configurationContainers)::value_type>("v16.Configuration.Containers").swap(configurationContainers); //release allocated memory (see https://cplusplus.com/reference/vector/vector/clear/)
makeVector<decltype(validators)::value_type>("v16.Configuration.Validators").swap(validators);
filesystem.reset();
}
bool configuration_load(const char *filename) {
bool success = true;
for (auto& container : configurationContainers) {
if ((!filename || !strcmp(filename, container->getFilename())) && !container->load()) {
success = false;
}
}
return success;
}
bool configuration_save() {
bool success = true;
for (auto& container : configurationContainers) {
if (!container->save()) {
success = false;
}
}
return success;
}
bool configuration_clean_unused() {
for (auto& container : configurationContainers) {
container->removeUnused();
}
return configuration_save();
}
bool VALIDATE_UNSIGNED_INT(const char *value) {
for(size_t i = 0; value[i] != '\0'; i++) {
if (value[i] < '0' || value[i] > '9') {
return false;
}
}
return true;
}
bool safeStringToInt(const char* str, int* result) {
if (!str || !result) {
return false;
}
// Skip leading whitespace
while (*str == ' ' || *str == '\t') {
str++;
}
// Check for empty string
if (*str == '\0') {
return false;
}
// Handle sign
bool negative = false;
if (*str == '-') {
negative = true;
str++;
} else if (*str == '+') {
str++;
}
// Check if there are digits after sign
if (*str < '0' || *str > '9') {
return false;
}
// Count digits and validate characters BEFORE parsing
int nDigits = 0;
const char* p = str;
while (*p >= '0' && *p <= '9') {
nDigits++;
p++;
}
// Check for trailing non-digit characters (allow decimal point for compatibility)
if (*p != '\0' && *p != '.') {
return false;
}
// Determine maximum safe digit count based on int size
int INT_MAXDIGITS;
if (sizeof(int) >= 4UL) {
INT_MAXDIGITS = 10; // Safe range: allows valid 10-digit integers within INT_MAX
} else {
INT_MAXDIGITS = 4; // Safe range: -9,999 to 9,999
}
// Reject if too many digits (prevents overflow)
if (nDigits > INT_MAXDIGITS) {
return false;
}
// Now perform the actual conversion with overflow checking
int value = 0;
const int INT_MAX_DIV_10 = INT_MAX / 10;
const int INT_MIN_DIV_10 = INT_MIN / 10;
while (*str >= '0' && *str <= '9') {
int digit = *str - '0';
if (negative) {
// Check for overflow before multiplication
if (value < INT_MIN_DIV_10) {
return false;
}
value *= 10;
// Check for overflow before subtraction
if (value < INT_MIN + digit) {
return false;
}
value -= digit;
} else {
// Check for overflow before multiplication
if (value > INT_MAX_DIV_10) {
return false;
}
value *= 10;
// Check for overflow before addition
if (value > INT_MAX - digit) {
return false;
}
value += digit;
}
str++;
}
*result = value;
return true;
}
} //end namespace MicroOcpp