-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.c
451 lines (383 loc) · 15.6 KB
/
config.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/mount.h>
#include <sys/statvfs.h>
#ifdef USE_WIRINGPI
#include <wiringPi.h>
#define PIN_CFG_NAME "WPI_PIN"
#else
//#include "sd_GPIO.h"
#define PIN_CFG_NAME "GPIO_PIN"
#endif
#include "GPIO_Pi.h"
#include "minIni.h"
#include "utils.h"
#include "config.h"
#define sizearray(a) (sizeof(a) / sizeof((a)[0]))
#define MAXCFGLINE 256
struct sprinklerdcfg _sdconfig_;
bool remount_root_ro(bool readonly) {
// NSF Check if config is RO_ROOT set
if (readonly) {
logMessage(LOG_INFO, "reMounting root RO\n");
mount (NULL, "/", NULL, MS_REMOUNT | MS_RDONLY, NULL);
return true;
} else {
struct statvfs fsinfo;
statvfs("/", &fsinfo);
if ((fsinfo.f_flag & ST_RDONLY) == 0) // We are readwrite, ignore
return false;
logMessage(LOG_INFO, "reMounting root RW\n");
mount (NULL, "/", NULL, MS_REMOUNT, NULL);
return true;
}
}
void write_cache() {
bool state = remount_root_ro(false);
FILE *fp;
int zone;
fp = fopen(_sdconfig_.cache_file, "w");
if (fp == NULL) {
logMessage(LOG_ERR, "Open file failed '%s'\n", _sdconfig_.cache_file);
remount_root_ro(true);
return;
}
fprintf(fp, "%d\n", _sdconfig_.calendar);
fprintf(fp, "%d\n", _sdconfig_.delay24h);
fprintf(fp, "%li\n", _sdconfig_.delay24h_time);
fprintf(fp, "%d\n", _sdconfig_.precipChanceDelay);
fprintf(fp, "%.1f\n", _sdconfig_.precipInchDelay1day);
fprintf(fp, "%.1f\n", _sdconfig_.precipInchDelay2day);
fprintf(fp, "-1\n");
for (zone=1; zone <= _sdconfig_.zones; zone ++) {
fprintf(fp, "%d\n", _sdconfig_.zonecfg[zone].default_runtime);
}
fclose(fp);
remount_root_ro(state);
logMessage(LOG_INFO, "Wrote cache\n");
}
void read_cache() {
FILE *fp;
int i;
float f;
int c = 0;
bool z = false;
int zs = 0;
fp = fopen(_sdconfig_.cache_file, "r");
if (fp == NULL) {
logMessage(LOG_ERR, "Open file failed '%s'\n", _sdconfig_.cache_file);
return;
}
//while (EOF != fscanf (fp, "%d", &i)) {
while (EOF != fscanf (fp, "%f", &f)) {
if (f == -1) {
z = true;
zs = c;
c++;
continue;
}
i = (int)f;
if (z == true) {
_sdconfig_.zonecfg[ (c - zs) ].default_runtime = i;
logMessage(LOG_DEBUG, "Read default_runtime '%d' for zone %d\n", i, (c - zs) );
} else if (c == 0){
_sdconfig_.calendar = i;
logMessage(LOG_DEBUG, "Read Calendar '%s' from cache\n", _sdconfig_.calendar?"ON":"OFF");
} else if (c == 1){
_sdconfig_.delay24h = i;
logMessage(LOG_DEBUG, "Read delay24h '%s' from cache\n", _sdconfig_.delay24h?"ON":"OFF");
} else if (c == 2){
_sdconfig_.delay24h_time = i;
logMessage(LOG_DEBUG, "Read delay24h endtime '%li' from cache\n",_sdconfig_.delay24h_time);
} else if (c == 3){
_sdconfig_.precipChanceDelay = i;
logMessage(LOG_DEBUG, "Read Chance of Rain delay '%d' from cache\n",_sdconfig_.precipChanceDelay);
} else if (c == 4){
_sdconfig_.precipInchDelay1day = f;
logMessage(LOG_DEBUG, "Read Rain 1d delay '%f'in from cache\n",_sdconfig_.precipInchDelay1day);
} else if (c == 5){
_sdconfig_.precipInchDelay2day = f;
logMessage(LOG_DEBUG, "Read Rain 2d delay '%f'in from cache\n",_sdconfig_.precipInchDelay2day);
}/* else if ( c-1 <= _sdconfig_.zones) {
_sdconfig_.gpiocfg[c-1].default_runtime = i;
logMessage(LOG_DEBUG, "Read default_runtime '%d' for zone %d\n", i, c-1);
}*/
c++;
}
fclose (fp);
}
char *trimwhitespace(char *str)
{
char *end;
// Trim leading space
while(isspace(*str)) str++;
if(*str == 0) // All spaces?
return str;
// Trim trailing space
end = str + strlen(str) - 1;
while(end > str && isspace(*end)) end--;
// Write new null terminator
*(end+1) = 0;
return str;
}
char *cleanalloc(char*str)
{
char *result;
str = trimwhitespace(str);
result = (char*)malloc(strlen(str)+1);
strcpy ( result, str );
//printf("Result=%s\n",result);
return result;
}
char *cleanallocindex(char*str, int index)
{
char *result;
int i;
int found = 1;
int loc1=0;
int loc2=strlen(str);
for(i=0;i<loc2;i++) {
if ( str[i] == ';' ) {
found++;
if (found == index)
loc1 = i;
else if (found == (index+1))
loc2 = i;
}
}
if (found < index)
return NULL;
// Trim leading & trailing spaces
loc1++;
while(isspace(str[loc1])) loc1++;
loc2--;
while(isspace(str[loc2])) loc2--;
// Allocate and copy
result = (char*)malloc(loc2-loc1+2*sizeof(char));
strncpy ( result, &str[loc1], loc2-loc1+1 );
result[loc2-loc1+1] = '\0';
return result;
}
int cleanint(char*str)
{
if (str == NULL)
return 0;
str = trimwhitespace(str);
return atoi(str);
}
bool text2bool(char *str)
{
str = trimwhitespace(str);
if (strcasecmp (str, "YES") == 0 || strcasecmp (str, "ON") == 0)
return true;
else
return false;
}
void readCfg(char *inifile)
{
char str[100];
//long n;
int i;
int idx=0;
ini_gets("SPRINKLERD", "LOG_LEVEL", "WARNING", str, sizearray(str), inifile);
if (_sdconfig_.log_level != LOG_DEBUG)
_sdconfig_.log_level = text2elevel(str);
ini_gets("SPRINKLERD", "NAME", "None", _sdconfig_.name, sizearray(_sdconfig_.name), inifile);
ini_gets("SPRINKLERD", "PORT", "8888", _sdconfig_.socket_port, sizearray(_sdconfig_.socket_port), inifile);
ini_gets("SPRINKLERD", "DOCUMENTROOT", "./", _sdconfig_.docroot, sizearray(_sdconfig_.docroot), inifile);
//ini_gets("SPRINKLERD", "CACHE", "/tmp/sprinklerd.cache", _sdconfig_.cache_file, sizearray(_sdconfig_.cache_file), inifile);
ini_gets("SPRINKLERD", "CACHE", "/tmp/sprinklerd.cache", _sdconfig_.cache_file, 512, inifile);
ini_gets("SPRINKLERD", "MQTT_ADDRESS", NULL, _sdconfig_.mqtt_address, sizearray(_sdconfig_.mqtt_address), inifile);
ini_gets("SPRINKLERD", "MQTT_USER", NULL, _sdconfig_.mqtt_user, sizearray(_sdconfig_.mqtt_user), inifile);
ini_gets("SPRINKLERD", "MQTT_PASSWD", NULL, _sdconfig_.mqtt_passwd, sizearray(_sdconfig_.mqtt_passwd), inifile);
if ( ini_gets("SPRINKLERD", "MQT_TOPIC", NULL, _sdconfig_.mqtt_topic, sizearray(_sdconfig_.mqtt_topic), inifile) > 0 )
_sdconfig_.enableMQTTaq = true;
else
_sdconfig_.enableMQTTaq = false;
if ( ini_gets("SPRINKLERD", "MQTT_DZ_PUB_TOPIC", NULL, _sdconfig_.mqtt_dz_pub_topic, sizearray(_sdconfig_.mqtt_dz_pub_topic), inifile) > 0 &&
ini_gets("SPRINKLERD", "MQTT_DZ_SUB_TOPIC", NULL, _sdconfig_.mqtt_dz_sub_topic, sizearray(_sdconfig_.mqtt_dz_sub_topic), inifile) > 0)
_sdconfig_.enableMQTTdz = true;
else
_sdconfig_.enableMQTTdz = false;
if (_sdconfig_.enableMQTTdz == true || _sdconfig_.enableMQTTaq == true) {
logMessage (LOG_DEBUG,"Config mqtt_topic '%s'\n",_sdconfig_.mqtt_topic);
logMessage (LOG_DEBUG,"Config mqtt_dz_pub_topic '%s'\n",_sdconfig_.mqtt_dz_pub_topic);
logMessage (LOG_DEBUG,"Config mqtt_dz_sub_topic '%s'\n",_sdconfig_.mqtt_dz_sub_topic);
} else {
logMessage (LOG_DEBUG,"Config mqtt 'disabeled'\n");
}
_sdconfig_.dzidx_calendar = ini_getl("SPRINKLERD", "DZIDX_CALENDAR", 0, inifile);
_sdconfig_.dzidx_24hdelay = ini_getl("SPRINKLERD", "DZIDX_24HDELAY", 0, inifile);
_sdconfig_.dzidx_allzones = ini_getl("SPRINKLERD", "DZIDX_ALL_ZONES", 0, inifile);
_sdconfig_.dzidx_status = ini_getl("SPRINKLERD", "DZIDX_STATUS", 0, inifile);
_sdconfig_.dzidx_rainsensor = ini_getl("SPRINKLERD", "DZIDX_RAINSENSOR", 0, inifile);
logMessage (LOG_INFO, "Name = %s\n", _sdconfig_.name);
logMessage (LOG_INFO, "Port = %s\n", _sdconfig_.socket_port);
logMessage (LOG_INFO, "Docroot = %s\n", _sdconfig_.docroot);
idx=0;
int pin=-1;
_sdconfig_.zones = 0;
// Caculate how many zones we have
for (i=1; i <= 24; i++) // 24 = Just some arbutary number (max GPIO without expansion board)
{
sprintf(str, "ZONE:%d", i);
pin = ini_getl(str, PIN_CFG_NAME, -1, inifile);
if (pin == -1)
break;
else
_sdconfig_.zones = i;
}
logMessage (LOG_DEBUG, "Found %d ZONES\n", _sdconfig_.zones);
_sdconfig_.master_valve = false;
//_sdconfig_.dz_cache = malloc((_sdconfig_.zones + NON_ZONE_DZIDS + 1) * sizeof(struct DZcache));
_sdconfig_.dz_cache = calloc((_sdconfig_.zones + NON_ZONE_DZIDS + 1), sizeof(struct DZcache));
if ( _sdconfig_.zones != 0) {
// n= _sdconfig_.zones+1;
_sdconfig_.zonecfg = malloc((_sdconfig_.zones + 1) * sizeof(struct GPIOcfg));
for (i=0; i <= _sdconfig_.zones; i++)
{
sprintf(str, "ZONE:%d", i);
pin = ini_getl(str, PIN_CFG_NAME, -1, inifile);
if (pin != -1) {
logMessage (LOG_DEBUG, "ZONE = %d\n", i);
if (i==0) {
_sdconfig_.master_valve = true;
}
_sdconfig_.zonecfg[i].input_output = OUTPUT; // Zone is always output
_sdconfig_.zonecfg[i].receive_mode = BOTH; // Zone always needs trigger on both (high or low)
_sdconfig_.zonecfg[i].zone = i;
_sdconfig_.zonecfg[i].pin = pin;
_sdconfig_.zonecfg[i].on_state = ini_getl(str, "GPIO_ON_STATE", NO, inifile);
_sdconfig_.zonecfg[i].startup_state = !_sdconfig_.zonecfg[i].on_state;
_sdconfig_.zonecfg[i].shutdown_state = !_sdconfig_.zonecfg[i].on_state;
_sdconfig_.zonecfg[i].set_pull_updown = ini_getl(str, "GPIO_PULL_UPDN", -1, inifile);
_sdconfig_.zonecfg[i].dz_idx = ini_getl(str, "DOMOTICZ_IDX", -1, inifile);
//_sdconfig_.zonecfg[i].master_valve = ini_getl(str, "MASTER_VALVE", NO, inifile);
_sdconfig_.zonecfg[i].default_runtime = ini_getl(str, "DEFAULT_RUNTIME", 10, inifile);
//ini_gets(str, "NAME", NULL, _sdconfig_.zonecfg[idx].name, sizearray(_sdconfig_.zonecfg[idx].name), inifile);
ini_gets(str, "NAME", NULL, _sdconfig_.zonecfg[i].name, sizearray(_sdconfig_.zonecfg[i].name), inifile);
//#ifndef USE_WIRINGPI
if ( ! validGPIO(pin) ) {
logMessage (LOG_ERR, "GPIO pin %d is not valid, found in ZONE:%d of configuration file %s \n",pin, i, inifile);
pin = -1;
sprintf(_sdconfig_.zonecfg[i].name, "ERROR");
}
//#endif
/*
logMessage (LOG_DEBUG,"Zone Config : %s\n%25s : %d\n%25s : %d\n%25s : %d\n%25s : %d\n%25s : %d\n",
_sdconfig_.zonecfg[i].name,
"PIN",_sdconfig_.zonecfg[i].pin,
"Set pull up/down", _sdconfig_.zonecfg[i].set_pull_updown,
"ON state", _sdconfig_.zonecfg[i].on_state,
"Master valve", _sdconfig_.zonecfg[i].master_valve,
"Domoticz IDX", _sdconfig_.zonecfg[i].dz_idx);
*/
logMessage (LOG_DEBUG,"Zone Config : %s\n%25s : %d\n%25s : %d\n%25s : %d\n%25s : %d\n",
_sdconfig_.zonecfg[i].name,
"PIN",_sdconfig_.zonecfg[i].pin,
"Set pull up/down", _sdconfig_.zonecfg[i].set_pull_updown,
"ON state", _sdconfig_.zonecfg[i].on_state,
"Domoticz IDX", _sdconfig_.zonecfg[i].dz_idx);
idx++;
} else {
logMessage (LOG_DEBUG, "No Information for ZONE = %d\n", i);
if (i > 0) // We may not have zone 0, but much have zone 1 and all zones in sequence
break;
}
}
//_sdconfig_.pinscfgs = idx;
//logMessage (LOG_DEBUG,"Config: total GPIO pins = %d\n",_sdconfig_.pinscfgs);
_sdconfig_.cron[0].zruntimes = malloc(_sdconfig_.zones * sizeof(int));
_sdconfig_.cron[1].zruntimes = malloc(_sdconfig_.zones * sizeof(int));
_sdconfig_.cron[2].zruntimes = malloc(_sdconfig_.zones * sizeof(int));
_sdconfig_.cron[3].zruntimes = malloc(_sdconfig_.zones * sizeof(int));
_sdconfig_.cron[4].zruntimes = malloc(_sdconfig_.zones * sizeof(int));
_sdconfig_.cron[5].zruntimes = malloc(_sdconfig_.zones * sizeof(int));
_sdconfig_.cron[6].zruntimes = malloc(_sdconfig_.zones * sizeof(int));
} else {
printf("ERROR no config zones set\n");
logMessage (LOG_ERR," no config zones set\n");
exit (EXIT_FAILURE);
}
// Caculate how many inputs we have
for (i=1; i <= 24; i++) // 24 = Just some arbutary number (max GPIO without expansion board)
{
sprintf(str, "INPUT:%d", i);
pin = ini_getl(str, PIN_CFG_NAME, -1, inifile);
if (pin == -1)
break;
else
_sdconfig_.inputs = i;
}
logMessage (LOG_DEBUG, "Found %d INPUTS\n", _sdconfig_.inputs);
if ( _sdconfig_.inputs != 0) {
// n= _sdconfig_.zones+1;
_sdconfig_.inputcfg = malloc((_sdconfig_.inputs + 1) * sizeof(struct GPIOcfg));
for (i=0; i < _sdconfig_.inputs; i++)
{
sprintf(str, "INPUT:%d", i+1);
pin = ini_getl(str, PIN_CFG_NAME, -1, inifile);
if (! validGPIO(pin) ) {
logMessage (LOG_ERR, "GPIO pin %d is not valid, found in INPUT:%d of configuration file %s \n",pin, i+1, inifile);
continue;
}
logMessage (LOG_DEBUG, "INPUT = %d\n", i+1);
_sdconfig_.inputcfg[i].input_output = INPUT; // Zone is always input
_sdconfig_.inputcfg[i].receive_mode = BOTH; // Zone always needs trigger on both (high or low)
_sdconfig_.inputcfg[i].zone = i+1;
_sdconfig_.inputcfg[i].pin = pin;
_sdconfig_.inputcfg[i].on_state = ini_getl(str, "GPIO_ON_STATE", NO, inifile);
//_sdconfig_.inputcfg[i].startup_state = !_sdconfig_.inputcfg[i].on_state;
//_sdconfig_.inputcfg[i].shutdown_state = !_sdconfig_.inputcfg[i].on_state;
_sdconfig_.inputcfg[i].set_pull_updown = ini_getl(str, "GPIO_PULL_UPDN", -1, inifile);
_sdconfig_.inputcfg[i].dz_idx = ini_getl(str, "DOMOTICZ_IDX", -1, inifile); // Not used at the moment.
ini_gets(str, "NAME", NULL, _sdconfig_.inputcfg[i].name, sizearray(_sdconfig_.inputcfg[i].name), inifile);
_sdconfig_.inputcfg[i].command_on = malloc(COMMAND_SIZE * sizeof(char));
_sdconfig_.inputcfg[i].command_off = malloc(COMMAND_SIZE * sizeof(char));
ini_gets(str, "COMMAND_ON", NULL, _sdconfig_.inputcfg[i].command_on, COMMAND_SIZE, inifile);
ini_gets(str, "COMMAND_OFF", NULL, _sdconfig_.inputcfg[i].command_off, COMMAND_SIZE, inifile);
logMessage (LOG_DEBUG,"Input Config : %s\n%25s : %d\n%25s : %d\n%25s : %d\n%25s : %d\n",
_sdconfig_.inputcfg[i].name,
"PIN",_sdconfig_.inputcfg[i].pin,
"Set pull up/down", _sdconfig_.inputcfg[i].set_pull_updown,
"ON state", _sdconfig_.inputcfg[i].on_state,
"Domoticz IDX", _sdconfig_.inputcfg[i].dz_idx);
}
}
/*
idx=0;
pin=-1;
// Caculate how many gpio cfg we have
for (i=1; i <= 24; i++) // 24 = Just some arbutary number (max GPIO without expansion board)
{
sprintf(str, "GPIO:%d", i);
pin = ini_getl(str, PIN_CFG_NAME, -1, inifile);
if (pin == -1)
break;
else
_sdconfig_.pincfgs = i;
}
logMessage (LOG_DEBUG, "Found %d GPIO PINS\n", _sdconfig_.pincfgs);
if ( _sdconfig_.pincfgs != 0) {
// n= _sdconfig_.zones+1;
_sdconfig_.gpiocfg = malloc((_sdconfig_.pincfgs + 1) * sizeof(struct GPIOcfg));
for (i=0; i <= _sdconfig_.pincfgs; i++)
{
sprintf(str, "GPIO:%d", i);
int pin = ini_getl(str, PIN_CFG_NAME, -1, inifile);
if (pin != -1) {
logMessage (LOG_DEBUG, "ZONE = %d\n", i);
_sdconfig_.pincfgs[i].input_output = OUTPUT; // Zone is always output
_sdconfig_.pincfgs[i].receive_mode = BOTH; // Zone always needs trigger on both (high or low)
ini_gets(str, "NAME", NULL, _sdconfig_.pincfgs[idx].name, sizearray(_sdconfig_.pincfgs[idx].name), inifile);
_sdconfig_.pincfgs[i].pin = pin;
_sdconfig_.pincfgs[i].on_state = ini_getl(str, "GPIO_ON_STATE", NO, inifile);
_sdconfig_.pincfgs[i].startup_state = !_sdconfig_.pincfgs[i].on_state;
_sdconfig_.pincfgs[i].shutdown_state = !_sdconfig_.pincfgs[i].on_state;
_sdconfig_.pincfgs[i].set_pull_updown = ini_getl(str, "GPIO_PULL_UPDN", -1, inifile);
}
}
*/
}