forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationKeyValue.cpp
More file actions
298 lines (244 loc) · 6.23 KB
/
ConfigurationKeyValue.cpp
File metadata and controls
298 lines (244 loc) · 6.23 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
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Core/ConfigurationKeyValue.h>
#include <MicroOcpp/Core/Memory.h>
#include <MicroOcpp/Debug.h>
#include <string.h>
#include <ArduinoJson.h>
#define KEY_MAXLEN 60
#define STRING_VAL_MAXLEN 512
namespace MicroOcpp {
template<> TConfig convertType<int>() {return TConfig::Int;}
template<> TConfig convertType<bool>() {return TConfig::Bool;}
template<> TConfig convertType<const char*>() {return TConfig::String;}
Configuration::~Configuration() {
}
void Configuration::setInt(int) {
#if MO_CONFIG_TYPECHECK
MO_DBG_ERR("type err");
#endif
}
void Configuration::setBool(bool) {
#if MO_CONFIG_TYPECHECK
MO_DBG_ERR("type err");
#endif
}
bool Configuration::setString(const char*) {
#if MO_CONFIG_TYPECHECK
MO_DBG_ERR("type err");
#endif
return false;
}
int Configuration::getInt() {
#if MO_CONFIG_TYPECHECK
MO_DBG_ERR("type err");
#endif
return 0;
}
bool Configuration::getBool() {
#if MO_CONFIG_TYPECHECK
MO_DBG_ERR("type err");
#endif
return false;
}
const char *Configuration::getString() {
#if MO_CONFIG_TYPECHECK
MO_DBG_ERR("type err");
#endif
return "";
}
revision_t Configuration::getValueRevision() {
return value_revision;
}
void Configuration::setRebootRequired() {
rebootRequired = true;
}
bool Configuration::isRebootRequired() {
return rebootRequired;
}
void Configuration::setReadOnly() {
if (mutability == Mutability::ReadWrite) {
mutability = Mutability::ReadOnly;
} else {
mutability = Mutability::None;
}
}
bool Configuration::isReadOnly() {
return mutability == Mutability::ReadOnly;
}
bool Configuration::isReadable() {
return mutability == Mutability::ReadWrite || mutability == Mutability::ReadOnly;
}
void Configuration::setWriteOnly() {
if (mutability == Mutability::ReadWrite) {
mutability = Mutability::WriteOnly;
} else {
mutability = Mutability::None;
}
}
/*
* Default implementations of the Configuration interface.
*
* How to use custom implementations: for each OCPP config, pass a config instance to the OCPP lib
* before its initialization stage. Then the library won't create new config objects but
*/
class ConfigInt : public Configuration, public MemoryManaged {
private:
const char *key = nullptr;
int val = 0;
public:
~ConfigInt() = default;
bool setKey(const char *key) override {
this->key = key;
updateMemoryTag("v16.Configuration.", key);
return true;
}
const char *getKey() override {
return key;
}
TConfig getType() override {
return TConfig::Int;
}
void setInt(int val) override {
this->val = val;
value_revision++;
}
int getInt() override {
return val;
}
};
class ConfigBool : public Configuration, public MemoryManaged {
private:
const char *key = nullptr;
bool val = false;
public:
~ConfigBool() = default;
bool setKey(const char *key) override {
this->key = key;
updateMemoryTag("v16.Configuration.", key);
return true;
}
const char *getKey() override {
return key;
}
TConfig getType() override {
return TConfig::Bool;
}
void setBool(bool val) override {
this->val = val;
value_revision++;
}
bool getBool() override {
return val;
}
};
class ConfigString : public Configuration, public MemoryManaged {
private:
const char *key = nullptr;
char *val = nullptr;
public:
ConfigString() = default;
ConfigString(const ConfigString&) = delete;
ConfigString(ConfigString&&) = delete;
ConfigString& operator=(const ConfigString&) = delete;
~ConfigString() {
MO_FREE(val);
}
bool setKey(const char *key) override {
this->key = key;
updateMemoryTag("v16.Configuration.", key);
if (val) {
MO_MEM_SET_TAG(val, getMemoryTag());
}
return true;
}
const char *getKey() override {
return key;
}
TConfig getType() override {
return TConfig::String;
}
bool setString(const char *src) override {
bool src_empty = !src || !*src;
if (!val && src_empty) {
return true;
}
if (this->val && src && !strcmp(this->val, src)) {
return true;
}
size_t size = 0;
if (!src_empty) {
size = strlen(src) + 1;
}
if (size > MO_CONFIG_MAX_VALSTRSIZE) {
return false;
}
value_revision++;
if (this->val) {
MO_FREE(this->val);
this->val = nullptr;
}
if (!src_empty) {
this->val = (char*) MO_MALLOC(getMemoryTag(), size);
if (!this->val) {
return false;
}
strcpy(this->val, src);
}
return true;
}
const char *getString() override {
if (!val) {
return "";
}
return val;
}
};
std::unique_ptr<Configuration> makeConfiguration(TConfig type, const char *key) {
std::unique_ptr<Configuration> res;
switch (type) {
case TConfig::Int:
res.reset(new ConfigInt());
break;
case TConfig::Bool:
res.reset(new ConfigBool());
break;
case TConfig::String:
res.reset(new ConfigString());
break;
}
if (!res) {
MO_DBG_ERR("OOM");
return nullptr;
}
res->setKey(key);
return res;
}
bool deserializeTConfig(const char *serialized, TConfig& out) {
if (!strcmp(serialized, "int")) {
out = TConfig::Int;
return true;
} else if (!strcmp(serialized, "bool")) {
out = TConfig::Bool;
return true;
} else if (!strcmp(serialized, "string")) {
out = TConfig::String;
return true;
} else {
MO_DBG_WARN("config type error");
return false;
}
}
const char *serializeTConfig(TConfig type) {
switch (type) {
case TConfig::Int:
return "int";
case TConfig::Bool:
return "bool";
case TConfig::String:
return "string";
}
return "_Undefined";
}
} //end namespace MicroOcpp