-
Notifications
You must be signed in to change notification settings - Fork 0
/
housesprinkler_config.c
314 lines (260 loc) · 9.85 KB
/
housesprinkler_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
/* housesprinkler - A simple home web server for sprinkler control
*
* Copyright 2020, Pascal Martin
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*
* housesprinkler_config.c - Access the sprinkler config.
*
* SYNOPSYS:
*
* const char *housesprinkler_config_load (int argc, const char **argv);
*
* Load the configuration from the specified config option, or else
* from the default config file.
*
* const char *housesprinkler_config_latest (void);
*
* Return the JSON text corresponding to the latest config that was loaded,
* even if that config failed parsing..
*
* const char *housesprinkler_config_save (const char *text);
*
* Update both the live configuration and the configuration file with
* the provided text.
*
* int housesprinkler_config_exists (int parent, const char *path);
* const char *housesprinkler_config_string (int parent, const char *path);
* int housesprinkler_config_integer (int parent, const char *path);
* int housesprinkler_config_boolean (int parent, const char *path);
*
* Access individual items starting from the specified parent
* (the config root is index 0).
*
* int housesprinkler_config_array (int parent, const char *path);
* int housesprinkler_config_array_length (int array);
*
* Retrieve an array.
*
* int housesprinkler_config_enumerate (int parent, int *index);
*
* Retrieve the elements of an array or object.
*
* int housesprinkler_config_object (int parent, const char *path);
*
* Retrieve an object.
*
* const char *housesprinkler_config_name (void);
*
* Get the name of the current configuration file, for informational
* purpose (e.g. error messages).
*
* void housesprinkler_config_periodic (void);
*
* Background config activity (mostly: save data when changed).
*/
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <echttp_json.h>
#include "houselog.h"
#include "housedepositor.h"
#include "housesprinkler.h"
#include "housesprinkler_config.h"
#define DEBUG if (sprinkler_isdebug()) printf
static ParserToken *ConfigParsed = 0;
static int ConfigTokenAllocated = 0;
static int ConfigTokenCount = 0;
static char *ConfigText = 0;
static char *ConfigTextLatest = 0;
static int ConfigFileEnabled = 1;
static const char *ConfigFile = "/etc/house/sprinkler.json";
static const char FactoryDefaultsConfigFile[] =
"/usr/local/share/house/public/sprinkler/defaults.json";
static int UseFactoryDefaults = 0;
static void housesprinkler_config_clear (const char *reason) {
if (ConfigText) {
echttp_parser_free (ConfigText);
ConfigText = 0;
}
ConfigTokenCount = 0;
DEBUG ("Config cleared (%s).\n", reason);
}
static const char *housesprinkler_config_parse (char *text) {
int count = echttp_json_estimate(text);
if (count > ConfigTokenAllocated) {
if (ConfigParsed) free (ConfigParsed);
ConfigTokenAllocated = count;
ConfigParsed = calloc (ConfigTokenAllocated, sizeof(ParserToken));
}
if (ConfigTextLatest) free (ConfigTextLatest);
ConfigTextLatest = strdup (text);
DEBUG ("New configuration: %s\n", ConfigTextLatest);
ConfigTokenCount = ConfigTokenAllocated;
const char *error = echttp_json_parse (text, ConfigParsed, &ConfigTokenCount);
DEBUG ("Planned config for %d JSON tokens, got %d\n", ConfigTokenAllocated, ConfigTokenCount);
if (error) {
houselog_event ("SYSTEM", "CONFIG", "FAILED", "%s", error);
DEBUG ("Config error: %s\n", error);
}
return error;
}
static const char *housesprinkler_config_write (const char *text, int length) {
if (!ConfigFileEnabled) return 0; // No error.
DEBUG("Saving to %s: %s\n", ConfigFile, text);
int fd = open (ConfigFile, O_WRONLY|O_TRUNC|O_CREAT, 0777);
if (fd < 0) {
char *desc = strdup(strerror(errno));
houselog_trace (HOUSE_FAILURE, "CONFIG",
"Cannot save to %s: %s", ConfigFile, desc);
free (desc);
return "cannot save to file";
}
write (fd, text, length);
close (fd);
return 0;
}
static void housesprinkler_config_listener (const char *name, time_t timestamp,
const char *data, int length) {
houselog_event ("SYSTEM", "CONFIG", "LOAD", "FROM DEPOT %s", name);
housesprinkler_config_clear ("new depot config detected");
ConfigText = echttp_parser_string (data);
housesprinkler_config_write (data, length);
housesprinkler_config_parse (ConfigText);
UseFactoryDefaults = 0;
sprinkler_refresh ();
}
const char *housesprinkler_config_load (int argc, const char **argv) {
char *newconfig;
int i;
for (i = 1; i < argc; ++i) {
if (echttp_option_match ("-config=", argv[i], &ConfigFile)) continue;
if (echttp_option_present ("-no-local-storage", argv[i])) {
ConfigFileEnabled = 0;
continue;
}
}
housedepositor_subscribe ("config", "sprinkler.json",
housesprinkler_config_listener);
if (!ConfigFileEnabled) return 0; // No error.
DEBUG ("Loading config from %s\n", ConfigFile);
housesprinkler_config_clear ("loading..");
UseFactoryDefaults = 0;
newconfig = echttp_parser_load (ConfigFile);
if (!newconfig) {
DEBUG ("Loading config from %s\n", FactoryDefaultsConfigFile);
UseFactoryDefaults = 1;
newconfig = echttp_parser_load (FactoryDefaultsConfigFile);
if (!newconfig) return "not accessible";
houselog_event ("SYSTEM", "CONFIG", "LOAD",
"FILE %s", FactoryDefaultsConfigFile);
} else {
houselog_event ("SYSTEM", "CONFIG", "LOAD", "FILE %s", ConfigFile);
}
ConfigText = newconfig;
return housesprinkler_config_parse (ConfigText);
}
const char *housesprinkler_config_save (const char *text) {
int length = strlen(text);
char *newconfig;
const char *error;
// Protect against bugs leading to the wrong string being used.
//
if (length < 10 || text[0] != '{') {
houselog_trace (HOUSE_FAILURE, "CONFIG",
"Invalid config string: %-0.60s (length %d)",
text, length);
return "invalid string";
}
housesprinkler_config_clear ("new configuration");
newconfig = echttp_parser_string(text);
error = housesprinkler_config_parse (newconfig);
if (error) {
houselog_trace (HOUSE_FAILURE, "CONFIG",
"JSON error %s on %-0.60s", error, text);
free (newconfig);
return error;
}
ConfigText = newconfig;
houselog_event ("SYSTEM", "CONFIG", "SAVE", "TO DEPOT sprinkler.json");
housedepositor_put ("config", "sprinkler.json", text, length);
error = housesprinkler_config_write (text, length);
if (error) return error;
UseFactoryDefaults = 0;
houselog_event ("SYSTEM", "CONFIG", "UPDATED", "FILE %s", ConfigFile);
return 0;
}
const char *housesprinkler_config_latest (void) {
return ConfigTextLatest;
}
int housesprinkler_config_find (int parent, const char *path, int type) {
int i;
if (parent < 0 || parent >= ConfigTokenCount) return -1;
i = echttp_json_search(ConfigParsed+parent, path);
if (i >= 0 && ConfigParsed[parent+i].type == type) return parent+i;
return -1;
}
int housesprinkler_config_exists (int parent, const char *path) {
if (parent < 0 || parent >= ConfigTokenCount) return 0;
return (echttp_json_search(ConfigParsed+parent, path) >= 0);
}
const char *housesprinkler_config_string (int parent, const char *path) {
int i = housesprinkler_config_find(parent, path, PARSER_STRING);
return (i >= 0) ? ConfigParsed[i].value.string : 0;
}
int housesprinkler_config_integer (int parent, const char *path) {
int i = housesprinkler_config_find(parent, path, PARSER_INTEGER);
return (i >= 0) ? ConfigParsed[i].value.integer : 0;
}
int housesprinkler_config_boolean (int parent, const char *path) {
int i = housesprinkler_config_find(parent, path, PARSER_BOOL);
return (i >= 0) ? ConfigParsed[i].value.bool : 0;
}
int housesprinkler_config_array (int parent, const char *path) {
return housesprinkler_config_find(parent, path, PARSER_ARRAY);
}
int housesprinkler_config_array_length (int array) {
if (array < 0
|| array >= ConfigTokenCount
|| ConfigParsed[array].type != PARSER_ARRAY) return 0;
return ConfigParsed[array].length;
}
int housesprinkler_config_enumerate (int parent, int *index) {
int i, length;
if (parent < 0 || parent >= ConfigTokenCount) return 0;
const char *error = echttp_json_enumerate (ConfigParsed+parent, index);
if (error) {
fprintf (stderr, "Cannot enumerate %s: %s\n",
ConfigParsed[parent].key, error);
return -1;
}
length = ConfigParsed[parent].length;
for (i = 0; i < length; ++i) index[i] += parent;
return length;
}
int housesprinkler_config_object (int parent, const char *path) {
return housesprinkler_config_find(parent, path, PARSER_OBJECT);
}
const char *housesprinkler_config_name (void) {
return ConfigFile;
}
void housesprinkler_config_periodic (void) {
}