|
1 /***************************************************************************** |
|
2 * Copyright (C) 2015 |
|
3 * |
|
4 * Michiel Broek <mbroek at mbse dot eu> |
|
5 * |
|
6 * This file is part of the mbsePi-apps |
|
7 * |
|
8 * This is free software; you can redistribute it and/or modify it |
|
9 * under the terms of the GNU General Public License as published by the |
|
10 * Free Software Foundation; either version 2, or (at your option) any |
|
11 * later version. |
|
12 * |
|
13 * mbsePi-apps is distributed in the hope that it will be useful, but |
|
14 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 * General Public License for more details. |
|
17 * |
|
18 * You should have received a copy of the GNU General Public License |
|
19 * along with ThermFerm; see the file COPYING. If not, write to the Free |
|
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
|
21 *****************************************************************************/ |
|
22 |
|
23 #include "rdconfig.h" |
|
24 #include "brewco.h" |
|
25 #include "futil.h" |
|
26 #include "xutil.h" |
|
27 |
|
28 int debug = FALSE; |
|
29 sys_config Config; /* System configuration */ |
|
30 |
|
31 #define MY_ENCODING "utf-8" |
|
32 |
|
33 const char TEMPSTATE[3][8] = { "OK", "MISSING", "ERROR" }; |
|
34 //const char DEVTYPE[8][6] = { "NA", "W1", "GPIO", "RC433", "DHT", "I2C", "SPI", "SIM" }; |
|
35 //const char DEVPRESENT[4][6] = { "UNDEF", "NO", "YES", "ERROR" }; |
|
36 //const char DEVDIR[7][11] = { "UNDEF", "IN_BIN", "OUT_BIN", "IN_ANALOG", "OUT_ANALOG", "OUT_PWM", "INTERN" }; |
|
37 //const char PIDMODE[3][5] = { "NONE", "AUTO", "BOO" }; |
|
38 |
|
39 |
|
40 void killconfig(void) |
|
41 { |
|
42 |
|
43 if (Config.name) |
|
44 free(Config.name); |
|
45 Config.name = NULL; |
|
46 |
|
47 Config.tempFormat = 'C'; |
|
48 if (Config.hlt_sensor_address) |
|
49 free(Config.hlt_sensor_address); |
|
50 if (Config.mlt_sensor_address) |
|
51 free(Config.mlt_sensor_address); |
|
52 Config.hlt_sensor_address = Config.mlt_sensor_address = NULL; |
|
53 Config.hlt_sensor_value = Config.mlt_sensor_value = 20000; |
|
54 Config.hlt_sensor_state = Config.mlt_sensor_state = 1; // missing |
|
55 |
|
56 #ifdef HAVE_WIRINGPI_H |
|
57 Config.lcd_cols = 16; |
|
58 Config.lcd_rows = 2; |
|
59 #endif |
|
60 } |
|
61 |
|
62 |
|
63 |
|
64 int do_wrconfig(void); |
|
65 int do_wrconfig(void) |
|
66 { |
|
67 int rc = 0; |
|
68 FILE *fp; |
|
69 char *mypath = NULL; |
|
70 xmlTextWriterPtr writer; |
|
71 xmlBufferPtr buf; |
|
72 |
|
73 /* |
|
74 * Create a new XML buffer, to which the XML document will be written |
|
75 */ |
|
76 if ((buf = xmlBufferCreate()) == NULL) { |
|
77 syslog(LOG_NOTICE, "wrconfig: error creating the xml buffer"); |
|
78 return 1; |
|
79 } |
|
80 |
|
81 /* |
|
82 * Create a new XmlWriter for memory, with no compression. |
|
83 */ |
|
84 if ((writer = xmlNewTextWriterMemory(buf, 0)) == NULL) { |
|
85 syslog(LOG_NOTICE, "wrconfig: error creating the xml writer"); |
|
86 return 1; |
|
87 } |
|
88 |
|
89 /* |
|
90 * Use indentation instead of one long line |
|
91 */ |
|
92 if ((rc = xmlTextWriterSetIndent(writer, 2)) < 0) { |
|
93 syslog(LOG_NOTICE, "wrconfig: error setting Indent"); |
|
94 return 1; |
|
95 } |
|
96 |
|
97 /* |
|
98 * Start the document with the xml default for the version, |
|
99 * encoding ISO 8859-1 and the default for the standalone |
|
100 * declaration. |
|
101 */ |
|
102 if ((rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL)) < 0) { |
|
103 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartDocument"); |
|
104 return 1; |
|
105 } |
|
106 |
|
107 /* |
|
108 * Start an element named "BREWCO". Since thist is the first |
|
109 * element, this will be the root element of the document. |
|
110 */ |
|
111 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "BREWCO")) < 0) { |
|
112 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement"); |
|
113 return 1; |
|
114 } |
|
115 |
|
116 /* |
|
117 * Add an attribute with name "VERSION" and value "1" to BRWCO. |
|
118 */ |
|
119 if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) { |
|
120 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement"); |
|
121 return 1; |
|
122 } |
|
123 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", Config.name)) < 0) { |
|
124 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); |
|
125 return 1; |
|
126 } |
|
127 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TEMPFORMAT", "%c", Config.tempFormat)) < 0) { |
|
128 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); |
|
129 return 1; |
|
130 } |
|
131 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_SENSOR_ADDRESS", "%s", Config.hlt_sensor_address)) < 0) { |
|
132 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); |
|
133 return 1; |
|
134 } |
|
135 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_SENSOR_STATE", "%d", Config.hlt_sensor_state)) < 0) { |
|
136 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); |
|
137 return 1; |
|
138 } |
|
139 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HLT_SENSOR_VALUE", "%d", Config.hlt_sensor_value)) < 0) { |
|
140 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); |
|
141 return 1; |
|
142 } |
|
143 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_SENSOR_ADDRESS", "%s", Config.mlt_sensor_address)) < 0) { |
|
144 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); |
|
145 return 1; |
|
146 } |
|
147 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_SENSOR_STATE", "%d", Config.mlt_sensor_state)) < 0) { |
|
148 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); |
|
149 return 1; |
|
150 } |
|
151 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MLT_SENSOR_VALUE", "%d", Config.mlt_sensor_value)) < 0) { |
|
152 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); |
|
153 return 1; |
|
154 } |
|
155 |
|
156 #ifdef HAVE_WIRINGPI_H |
|
157 /* |
|
158 * Start an element named "LCDS" as child of BREWCO. |
|
159 */ |
|
160 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "LCDS")) < 0) { |
|
161 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement"); |
|
162 return 1; |
|
163 } |
|
164 /* |
|
165 * Start one LCD. It is possible to connect 7 LCD displays on the i2c bus. |
|
166 * However this program doesn't use more then one yet. |
|
167 */ |
|
168 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "LCD")) < 0) { |
|
169 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterStartElement"); |
|
170 return 1; |
|
171 } |
|
172 if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) { |
|
173 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteElement"); |
|
174 return 1; |
|
175 } |
|
176 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ADDRESS", "0x%x", Config.lcd_address)) < 0) { |
|
177 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); |
|
178 return 1; |
|
179 } |
|
180 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "COLUMNS", "%d", Config.lcd_cols)) < 0) { |
|
181 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); |
|
182 return 1; |
|
183 } |
|
184 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ROWS", "%d", Config.lcd_rows)) < 0) { |
|
185 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement"); |
|
186 return 1; |
|
187 } |
|
188 /* |
|
189 * Close the element named LCD. |
|
190 */ |
|
191 if ((rc = xmlTextWriterEndElement(writer)) < 0) { |
|
192 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement"); |
|
193 return 1; |
|
194 } |
|
195 /* |
|
196 * Close the element LCDS. |
|
197 */ |
|
198 if ((rc = xmlTextWriterEndElement(writer)) < 0) { |
|
199 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndElement"); |
|
200 return 1; |
|
201 } |
|
202 #endif |
|
203 |
|
204 /* |
|
205 * All done, close any open elements |
|
206 */ |
|
207 if ((rc = xmlTextWriterEndDocument(writer)) < 0) { |
|
208 syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterEndDocument"); |
|
209 return 1; |
|
210 } |
|
211 xmlFreeTextWriter(writer); |
|
212 |
|
213 /* |
|
214 * Now write the XML configuration |
|
215 */ |
|
216 if (getenv((char *)"USER") == NULL) { |
|
217 mypath = xstrcpy((char *)"/root"); |
|
218 } else { |
|
219 mypath = xstrcpy(getenv((char *)"HOME")); |
|
220 } |
|
221 mypath = xstrcat(mypath, (char *)"/.brewco/etc/"); |
|
222 mkdirs(mypath, 0755); |
|
223 mypath = xstrcat(mypath, (char *)"brewco.xml"); |
|
224 |
|
225 if (debug) |
|
226 fprintf(stdout, "Writing %s\n", mypath); |
|
227 |
|
228 if ((fp = fopen(mypath, "w")) == NULL) { |
|
229 syslog(LOG_NOTICE, "could not rewrite %s", mypath); |
|
230 free(mypath); |
|
231 return 1; |
|
232 } |
|
233 free(mypath); |
|
234 |
|
235 fprintf(fp, "%s", (const char *) buf->content); |
|
236 fclose(fp); |
|
237 xmlBufferFree(buf); |
|
238 |
|
239 return 0; |
|
240 } |
|
241 |
|
242 |
|
243 |
|
244 int wrconfig(void) |
|
245 { |
|
246 int rc; |
|
247 |
|
248 rc = do_wrconfig(); |
|
249 syslog(LOG_NOTICE, "Rewritten configuration, rc=%d", rc); |
|
250 return rc; |
|
251 } |
|
252 |
|
253 |
|
254 |
|
255 /* |
|
256 * Parse one LCD display |
|
257 */ |
|
258 #ifdef HAVE_WIRINGPI_H |
|
259 int parseLCD(xmlDocPtr doc, xmlNodePtr cur) |
|
260 { |
|
261 xmlChar *key; |
|
262 int ival; |
|
263 |
|
264 cur = cur->xmlChildrenNode; |
|
265 while (cur != NULL) { |
|
266 if ((!xmlStrcmp(cur->name, (const xmlChar *)"COLUMNS"))) { |
|
267 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
|
268 if (sscanf((const char *)key, "%d", &ival) == 1) |
|
269 Config.lcd_cols = ival; |
|
270 xmlFree(key); |
|
271 } |
|
272 if ((!xmlStrcmp(cur->name, (const xmlChar *)"ROWS"))) { |
|
273 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
|
274 if (sscanf((const char *)key, "%d", &ival) == 1) |
|
275 Config.lcd_rows = ival; |
|
276 xmlFree(key); |
|
277 } |
|
278 if ((!xmlStrcmp(cur->name, (const xmlChar *)"ADDRESS"))) { |
|
279 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
|
280 if (sscanf((const char *)key, "%x", &ival) == 1) |
|
281 Config.lcd_address = ival; |
|
282 xmlFree(key); |
|
283 } |
|
284 cur = cur->next; |
|
285 } |
|
286 |
|
287 return 0; |
|
288 } |
|
289 |
|
290 |
|
291 |
|
292 int parseLCDs(xmlDocPtr doc, xmlNodePtr cur) |
|
293 { |
|
294 cur = cur->xmlChildrenNode; |
|
295 while (cur != NULL) { |
|
296 if ((!xmlStrcmp(cur->name, (const xmlChar *)"LCD"))) { |
|
297 parseLCD(doc, cur); |
|
298 } |
|
299 cur = cur->next; |
|
300 } |
|
301 return 0; |
|
302 } |
|
303 #endif |
|
304 |
|
305 |
|
306 |
|
307 int rdconfig(void) |
|
308 { |
|
309 int rc = 0; |
|
310 char *mypath; |
|
311 xmlDocPtr doc; |
|
312 xmlNodePtr cur; |
|
313 xmlChar *key; |
|
314 |
|
315 killconfig(); |
|
316 syslog(LOG_NOTICE, "HOME='%s' USER='%s' LOGNAME='%s'", MBSE_SS(getenv((char *)"HOME")), MBSE_SS(getenv((char *)"USER")), MBSE_SS(getenv((char *)"LOGNAME"))); |
|
317 |
|
318 /* |
|
319 * Search config file |
|
320 */ |
|
321 if (getenv((char *)"USER") == NULL) { |
|
322 mypath = xstrcpy((char *)"/root"); |
|
323 } else { |
|
324 mypath = xstrcpy(getenv((char *)"HOME")); |
|
325 } |
|
326 mypath = xstrcat(mypath, (char *)"/.brewco/etc/"); |
|
327 mkdirs(mypath, 0755); |
|
328 mypath = xstrcat(mypath, (char *)"brewco.xml"); |
|
329 if ((doc = xmlParseFile(mypath)) == NULL) { |
|
330 /* |
|
331 * No config file, create a fresh one |
|
332 */ |
|
333 syslog(LOG_NOTICE, "rdconfig: %s not found, creating", mypath); |
|
334 wrconfig(); |
|
335 |
|
336 if ((doc = xmlParseFile(mypath)) == NULL) { |
|
337 syslog(LOG_NOTICE, "rdconfig: could not create %s", mypath); |
|
338 free(mypath); |
|
339 return 1; |
|
340 } |
|
341 } |
|
342 syslog(LOG_NOTICE, "rdconfig: using %s", mypath); |
|
343 |
|
344 if ((cur = xmlDocGetRootElement(doc)) == NULL) { |
|
345 syslog(LOG_NOTICE, "XML file %s empty.", mypath); |
|
346 xmlFreeDoc(doc); |
|
347 return 1; |
|
348 } |
|
349 if (xmlStrcmp(cur->name, (const xmlChar*)"BREWCO")) { |
|
350 syslog(LOG_NOTICE, "XML file %s is not a valid configuration file.", mypath); |
|
351 xmlFreeDoc(doc); |
|
352 return 1; |
|
353 } |
|
354 |
|
355 /* |
|
356 * Parse configuration |
|
357 */ |
|
358 cur = cur->xmlChildrenNode; |
|
359 while (cur != NULL) { |
|
360 if ((!xmlStrcmp(cur->name, (const xmlChar *)"VERSION"))) { |
|
361 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
|
362 if (xmlStrcmp(key, (const xmlChar *)"1")) { |
|
363 xmlFree(key); |
|
364 syslog(LOG_NOTICE, "XML file %s is not a valid version", mypath); |
|
365 return 1; |
|
366 } |
|
367 xmlFree(key); |
|
368 } |
|
369 if ((!xmlStrcmp(cur->name, (const xmlChar *)"NAME"))) { |
|
370 Config.name = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
|
371 } |
|
372 if ((!xmlStrcmp(cur->name, (const xmlChar *)"TEMPFORMAT"))) { |
|
373 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
|
374 Config.tempFormat = key[0]; |
|
375 xmlFree(key); |
|
376 } |
|
377 if ((!xmlStrcmp(cur->name, (const xmlChar *)"HLT_SENSOR_ADDRESS"))) { |
|
378 Config.hlt_sensor_address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
|
379 } |
|
380 if ((!xmlStrcmp(cur->name, (const xmlChar *)"MLT_SENSOR_ADDRESS"))) { |
|
381 Config.mlt_sensor_address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
|
382 } |
|
383 #ifdef HAVE_WIRINGPI_H |
|
384 if ((!xmlStrcmp(cur->name, (const xmlChar *)"LCDS"))) { |
|
385 parseLCDs(doc, cur); |
|
386 } |
|
387 #endif |
|
388 cur = cur->next; |
|
389 } |
|
390 xmlFreeDoc(doc); |
|
391 |
|
392 free(mypath); |
|
393 mypath = NULL; |
|
394 |
|
395 return rc; |
|
396 } |
|
397 |
|
398 |
|
399 |