-
Notifications
You must be signed in to change notification settings - Fork 3
/
PointcastSetup.cpp
393 lines (360 loc) · 10.4 KB
/
PointcastSetup.cpp
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
/*
Simple library to handle ngeigie configuration from file and EEPROM
*/
#include "PointcastSetup.h"
#include "PointcastDebug.h"
PointcastSetup::PointcastSetup(SoftwareSerial &openlog, ConfigType &config,DoseType &dose,
char * buffer, size_t buffer_size):
mOpenlog(openlog), mConfig(config), mDose(dose), mBuffer(buffer), mBufferSize(buffer_size) {
}
void PointcastSetup::initialize() {
// Configuration
memset(&mConfig, 0, sizeof(mConfig));
EEPROM_readAnything(BMRDD_EEPROM_SETUP, mConfig);
DEBUG_PRINTLN("Loading EEPROM configuration");
if (mConfig.marker != BMRDD_EEPROM_MARKER) {
DEBUG_PRINTLN(" - First time setup");
// First run, time to set default values
memset(&mConfig, 0, sizeof(mConfig));
mConfig.marker = BMRDD_EEPROM_MARKER;
EEPROM_writeAnything(BMRDD_EEPROM_SETUP, mConfig);
}
}
void PointcastSetup::loadFromFile(char const* setupFile) {
bool config_changed = false;
char *config_buffer, *key, *value;
unsigned int pos, line_length;
byte i, buffer_length;
mOpenlog.listen();
// Send read command to OpenLog
DEBUG_PRINT(" - read ");
DEBUG_PRINTLN(setupFile);
sprintf_P(mBuffer, PSTR("read %s 0 %d"), setupFile, mBufferSize);
mOpenlog.print(mBuffer);
mOpenlog.write(13); //This is \r
while(1) {
if(mOpenlog.available()) {
char ch = mOpenlog.read();
if (ch == '\r' || ch == '\n') break;
}
}
// Read config file in memory
pos = 0;
memset(mBuffer, 0, mBufferSize);
for(int timeOut = 0 ; timeOut < 1000 ; timeOut++) {
if(mOpenlog.available()) {
mBuffer[pos++] = mOpenlog.read();
timeOut = 0;
}
delay(1);
if(pos == mBufferSize) {
break;
}
}
line_length = pos;
pos = 0;
// Process each config file lines
while(pos < line_length){
// Get a complete line
i = 0;
config_buffer = mBuffer + pos;
while(true) {
char ch = mBuffer[pos++];
if (ch == '\n' || ch == '\r') break;
if(pos == mBufferSize) break;
i++;
}
buffer_length = i;
config_buffer[i] = '\0';
// Skip empty lines
if(config_buffer[0] == '\0' || config_buffer[0] == '#' || buffer_length < 3) continue;
// Search for keys
i = 0;
while(config_buffer[i] == ' ' || config_buffer[i] == '\t') {
if(++i == buffer_length) break; // skip white spaces
}
if(i == buffer_length) continue;
key = &config_buffer[i];
// Search for '=' ignoring white spaces
while(config_buffer[i] != '=') {
if(config_buffer[i] == ' ' || config_buffer[i] == '\t'|| config_buffer[i] == '\r') config_buffer[i] = '\0';
if(++i == buffer_length) {
break;
}
}
if(i == buffer_length) continue;
config_buffer[i++] = '\0';
// Search for value ignoring white spaces
while(config_buffer[i] == ' ' || config_buffer[i] == '\t') {
if(++i == buffer_length) {
break;
}
}
if(i == buffer_length) continue;
value = &config_buffer[i];
//
// Process matching keys
//
if(strcmp(key, "s1f") == 0) {
float factor = atoi(value);
if (mConfig.sensor1_cpm_factor != factor) {
mConfig.sensor1_cpm_factor = factor;
config_changed = true;
DEBUG_PRINTLN(" - Update sensor1_cpm_factor");
}
}
else if(strcmp(key, "s2f") == 0) {
float factor = atoi(value);
if (mConfig.sensor2_cpm_factor != factor) {
mConfig.sensor2_cpm_factor = factor;
config_changed = true;
DEBUG_PRINTLN(" - Update sensor2_cpm_factor");
}
}
else if(strcmp(key, "devid") == 0) {
if (mConfig.devid != (unsigned int)atoi(value)) {
mConfig.devid = atoi(value);
config_changed = true;
DEBUG_PRINTLN(" - Update dev_id");
}
}
else if(strcmp(key, "devt1") == 0) {
if (mConfig.devt1 != (unsigned int)atoi(value)) {
mConfig.devt1 = atoi(value);
config_changed = true;
DEBUG_PRINTLN(" - Update Device Type 1");
}
}
else if(strcmp(key, "devt2") == 0) {
if (mConfig.devt2 != (unsigned int)atoi(value)) {
mConfig.devt2 = atoi(value);
config_changed = true;
DEBUG_PRINTLN(" - Update Device Type 2");
}
}
else if(strcmp(key, "uid1") == 0) {
if (mConfig.user_id != (unsigned int)atoi(value)) {
mConfig.user_id = atoi(value);
config_changed = true;
DEBUG_PRINTLN(" - Update user_id");
}
}
else if(strcmp(key, "uid2") == 0) {
if (mConfig.user_id2 != (unsigned int)atoi(value)) {
mConfig.user_id2 = atoi(value);
config_changed = true;
DEBUG_PRINTLN(" - Update user_id2");
}
}
else if(strcmp(key, "api") == 0) {
if (strcmp(mConfig.api_key, value) != 0 ) {
strcpy(mConfig.api_key, value);
config_changed = true;
DEBUG_PRINTLN(" - Update api_key");
}
}
else if(strcmp(key, "s1e") == 0) {
if (mConfig.sensor1_enabled != atoi(value)) {
mConfig.sensor1_enabled = atoi(value);
config_changed = true;
DEBUG_PRINTLN(" - Update sensor1_enabled flag");
}
}
else if(strcmp(key, "s2e") == 0) {
if (mConfig.sensor2_enabled != atoi(value)) {
mConfig.sensor2_enabled = atoi(value);
config_changed = true;
DEBUG_PRINTLN(" - Update sensor2_enabled flag");
}
}
else if(strcmp(key, "lat") == 0) {
if (strcmp(mConfig.latitude, value) != 0 ) {
strcpy(mConfig.latitude, value);
config_changed = true;
DEBUG_PRINTLN(" - Update latitude");
}
}
else if(strcmp(key, "lon") == 0) {
if (strcmp(mConfig.longitude, value) != 0 ) {
strcpy(mConfig.longitude, value);
config_changed = true;
DEBUG_PRINTLN(" - Update longitude");
}
}
else if(strcmp(key, "dev") == 0) {
if (mConfig.dev != atoi(value)) {
mConfig.dev = atoi(value);
config_changed = true;
DEBUG_PRINTLN(" - Update API for development");
}
}
else if(strcmp(key, "gw1") == 0) {
if (strcmp(mConfig.gw1, value) != 0 ) {
strcpy(mConfig.gw1, value);
config_changed = true;
DEBUG_PRINTLN(" - Update Gateway1");
}
}
else if(strcmp(key, "gw2") == 0) {
if (strcmp(mConfig.gw2, value) != 0 ) {
strcpy(mConfig.gw2, value);
config_changed = true;
DEBUG_PRINTLN(" - Update Gateway2");
}
}
else if(strcmp(key, "intf") == 0) {
if (strcmp(mConfig.intf, value) != 0 ) {
strcpy(mConfig.intf, value);
config_changed = true;
DEBUG_PRINTLN(" - Update Interface");
}
}
else if(strcmp(key, "tws") == 0) {
if (mConfig.tws != (unsigned int)atoi(value)) {
mConfig.tws = atoi(value);
config_changed = true;
DEBUG_PRINTLN(" - Update Time measuring window");
}
}
else if(strcmp(key, "alt") == 0) {
if (mConfig.alt != (unsigned int)atoi(value)) {
mConfig.alt = atoi(value);
config_changed = true;
DEBUG_PRINTLN(" - Update altitude");
}
}
else if(strcmp(key, "autow") == 0) {
if (mConfig.autow != atoi(value)) {
mConfig.autow = atoi(value);
config_changed = true;
DEBUG_PRINTLN(" - Update auto adaptive window");
}
}
else if(strcmp(key, "alm") == 0) {
if (mConfig.alm != atoi(value)) {
mConfig.alm = atoi(value);
config_changed = true;
DEBUG_PRINTLN(" - Update Alarm");
}
}
else if(strcmp(key, "tz") == 0) {
if (strcmp(mConfig.tz, value) != 0 ) {
strcpy(mConfig.tz, value);
config_changed = true;
DEBUG_PRINTLN(" - Update timezone in EEPROM");
}
}
else if(strcmp(key, "ssid") == 0) {
if (strcmp(mConfig.ssid, value) != 0 ) {
strcpy(mConfig.ssid, value);
config_changed = true;
DEBUG_PRINTLN(" - Update SSID for WiFi");
}
}
else if(strcmp(key, "pwd") == 0) {
if (strcmp(mConfig.pwd, value) != 0 ) {
strcpy(mConfig.pwd, value);
config_changed = true;
DEBUG_PRINTLN(" - Update password for WiFi");
}
}
else if(strcmp(key, "gwn") == 0) {
if (strcmp(mConfig.gwn, value) != 0 ) {
strcpy(mConfig.gwn, value);
config_changed = true;
DEBUG_PRINTLN(" - Update name for GateWay");
}
}
else if(strcmp(key, "s1i") == 0) {
if (strcmp(mConfig.s1i, value) != 0 ) {
strcpy(mConfig.s1i, value);
config_changed = true;
DEBUG_PRINTLN(" - Update Sensor 1 Isotope");
}
}
else if(strcmp(key, "s2i") == 0) {
if (strcmp(mConfig.s2i, value) != 0 ) {
strcpy(mConfig.s2i, value);
config_changed = true;
DEBUG_PRINTLN(" - Update Sensor 2 Isotope");
}
}
else if(strcmp(key, "aux") == 0) {
if (mConfig.aux != atoi(value)) {
mConfig.aux = atoi(value);
config_changed = true;
DEBUG_PRINTLN(" - Update aux input is active");
}
}
else if(strcmp(key, "rst") == 0) {
if (mConfig.rst != atoi(value)) {
mConfig.rst = atoi(value);
config_changed = true;
DEBUG_PRINTLN(" - Days of reboot changed");
}
}
else if(strcmp(key, "trb") == 0) {
if (mConfig.trb != atoi(value)) {
mConfig.trb = atoi(value);
config_changed = true;
DEBUG_PRINTLN(" - Update trouble shooting active");
}
}
else if(strcmp(key, "apn") == 0) {
if (strcmp(mConfig.apn, value) != 0 ) {
strcpy(mConfig.apn, value);
config_changed = true;
DEBUG_PRINTLN(" - Update apn");
}
}
else if(strcmp(key, "macid") == 0) {
if (strcmp(mConfig.macid, value) != 0 ) {
strcpy(mConfig.macid, value);
config_changed = true;
DEBUG_PRINTLN(" - Update macid");
}
}
else if(strcmp(key, "tel") == 0) {
if (strcmp(mConfig.tel, value) != 0 ) {
strcpy(mConfig.tel, value);
config_changed = true;
DEBUG_PRINTLN(" - Update tel");
}
}
else if(strcmp(key, "last_failure") == 0) {
if (strcmp(mConfig.last_failure, value) != 0 ) {
strcpy(mConfig.last_failure, value);
config_changed = true;
DEBUG_PRINTLN(" - Update last_failure");
}
}
else if(strcmp(key, "ntp") == 0) {
if (strcmp(mConfig.ntp, value) != 0 ) {
strcpy(mConfig.ntp, value);
config_changed = true;
DEBUG_PRINTLN(" - Update NTP");
}
}
else if(strcmp(key, "cfm") == 0) {
if (strcmp(mConfig.cfm, value) != 0 ) {
strcpy(mConfig.cfm, value);
config_changed = true;
DEBUG_PRINTLN(" - Update Confirm API timing");
}
}
#if ENABLE_EEPROM_DOSE
else if(strcmp(key, "dose") == 0) {
// Reset total dose in EEPROM
memset(&mDose, 0, sizeof(mDose));
EEPROM_writeAnything(BMRDD_EEPROM_DOSE, mDose);
DEBUG_PRINTLN(" - Reset total dose in EEPROM");
}
#endif
}
DEBUG_PRINTLN(" - Done.");
if (config_changed) {
// Configuration is changed
DEBUG_PRINTLN("Update configuration in EEPROM");
EEPROM_writeAnything(BMRDD_EEPROM_SETUP, mConfig);
}
}