|
1 /***************************************************************************** |
|
2 * Copyright (C) 2014 |
|
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 EC-65K; 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 "thermferm.h" |
|
24 |
|
25 |
|
26 bool debug = FALSE; |
|
27 static char *mypath; |
|
28 static char *k, *v; |
|
29 static int linecnt = 0; |
|
30 sys_config Config; /* System configuration */ |
|
31 |
|
32 |
|
33 |
|
34 static int getstr(char **); |
|
35 static int getint(char **); |
|
36 static int getw1(char **); |
|
37 #ifdef HAVE_WIRINGPI_H |
|
38 static int getrcs(char **); |
|
39 #endif |
|
40 //static int getbyt(char **); |
|
41 //static int gethex(char **); |
|
42 |
|
43 #define XSTR(x) #x |
|
44 #define STR(x) XSTR(x) |
|
45 |
|
46 /* |
|
47 * System configuration table |
|
48 */ |
|
49 key_list keytab[] = { |
|
50 {(char *)"mosq_host", getstr, &Config.mosq_host}, |
|
51 {(char *)"mosq_port", getint, (char **)&Config.mosq_port}, |
|
52 {(char *)"w1therm", getw1, (char **)&Config.w1therms}, |
|
53 #ifdef HAVE_WIRINGPI_H |
|
54 {(char *)"lcd_cols", getint, (char **)&Config.lcd_cols}, |
|
55 {(char *)"lcd_rows", getint, (char **)&Config.lcd_rows}, |
|
56 {(char *)"rx433", getint, (char **)&Config.rx433}, |
|
57 {(char *)"tx433", getint, (char **)&Config.tx433}, |
|
58 {(char *)"rcswitch", getrcs, (char **)&Config.rcswitch}, |
|
59 #endif |
|
60 {NULL, NULL, NULL} |
|
61 }; |
|
62 |
|
63 |
|
64 |
|
65 void killconfig(void) |
|
66 { |
|
67 w1_therm *tmp1, *old1; |
|
68 #ifdef HAVE_WIRINGPI_H |
|
69 rc_switch *tmp2, *old2; |
|
70 #endif |
|
71 |
|
72 if (Config.name) |
|
73 free(Config.name); |
|
74 Config.name = NULL; |
|
75 |
|
76 if (Config.mosq_host) |
|
77 free(Config.mosq_host); |
|
78 Config.mosq_host= (char *)"localhost"; |
|
79 Config.mosq_port = 1883; |
|
80 |
|
81 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { |
|
82 old1 = tmp1->next; |
|
83 if (tmp1->master) |
|
84 free(tmp1->master); |
|
85 if (tmp1->name) |
|
86 free(tmp1->name); |
|
87 if (tmp1->alias) |
|
88 free(tmp1->alias); |
|
89 free(tmp1); |
|
90 } |
|
91 Config.w1therms = NULL; |
|
92 Config.my_port = 6554; |
|
93 |
|
94 #ifdef HAVE_WIRINGPI_H |
|
95 Config.lcd_cols = 16; |
|
96 Config.lcd_rows = 2; |
|
97 Config.rx433 = -1; |
|
98 Config.tx433 = -1; |
|
99 |
|
100 for (tmp2 = Config.rcswitch; tmp2; tmp2 = old2) { |
|
101 old2 = tmp2->next; |
|
102 if (tmp2->address) |
|
103 free(tmp2->address); |
|
104 tmp2->address = NULL; |
|
105 if (tmp2->alias) |
|
106 free(tmp2->alias); |
|
107 tmp2->alias = NULL; |
|
108 free(tmp2); |
|
109 } |
|
110 Config.rcswitch = NULL; |
|
111 |
|
112 #endif |
|
113 } |
|
114 |
|
115 |
|
116 |
|
117 int rdconfig(char *config) |
|
118 { |
|
119 char buf[256], *p; |
|
120 FILE *fp; |
|
121 int i, rc = 0; |
|
122 |
|
123 killconfig(); |
|
124 |
|
125 /* |
|
126 * Search config file |
|
127 */ |
|
128 mypath = xstrcpy(getenv((char *)"HOME")); |
|
129 mypath = xstrcat(mypath, (char *)"/mbsepi-apps/"); |
|
130 mypath = xstrcat(mypath, config); |
|
131 if ((fp = fopen(mypath, "r")) == NULL) { |
|
132 /* |
|
133 * Not in the users home directory |
|
134 */ |
|
135 free(mypath); |
|
136 mypath = xstrcpy((char *)"/etc/mbsepi-apps/"); |
|
137 mypath = xstrcat(mypath, config); |
|
138 if ((fp = fopen(mypath, "r")) == NULL) { |
|
139 /* |
|
140 * Try /usr/local/etc |
|
141 */ |
|
142 free(mypath); |
|
143 mypath = xstrcpy((char *)"/usr/local/etc/mbsepi-apps/"); |
|
144 mypath = xstrcat(mypath, config); |
|
145 if ((fp = fopen(mypath, "r")) == NULL) { |
|
146 syslog(LOG_NOTICE, "rdconfig: could not open %s", mypath); |
|
147 return 1; |
|
148 } |
|
149 } |
|
150 } |
|
151 |
|
152 linecnt = 0; |
|
153 while (fgets(buf, sizeof(buf) -1, fp)) { |
|
154 linecnt++; |
|
155 if (*(p = buf + strlen(buf) -1) != '\n') { |
|
156 syslog(LOG_NOTICE, "rdconfig: %s(%d): \"%s\" - line too long", mypath, linecnt, buf); |
|
157 rc = 1; |
|
158 break; |
|
159 } |
|
160 *p-- = '\0'; |
|
161 while ((p >= buf) && isspace(*p)) |
|
162 *p-- = '\0'; |
|
163 k = buf; |
|
164 while (*k && isspace(*k)) |
|
165 k++; |
|
166 p = k; |
|
167 while (*p && !isspace(*p)) |
|
168 p++; |
|
169 *p++='\0'; |
|
170 v = p; |
|
171 while (*v && isspace(*v)) |
|
172 v++; |
|
173 |
|
174 if ((*k == '\0') || (*k == '#')) { |
|
175 continue; |
|
176 } |
|
177 |
|
178 for (i = 0; keytab[i].key; i++) |
|
179 if (strcasecmp(k,keytab[i].key) == 0) |
|
180 break; |
|
181 |
|
182 if (keytab[i].key == NULL) { |
|
183 syslog(LOG_NOTICE, "rdconfig: %s(%d): %s %s - unknown keyword", mypath, linecnt, MBSE_SS(k), MBSE_SS(v)); |
|
184 rc = 1; |
|
185 break; |
|
186 } else if ((keytab[i].prc(keytab[i].dest))) { |
|
187 rc = 1; |
|
188 break; |
|
189 } |
|
190 |
|
191 } |
|
192 fclose(fp); |
|
193 |
|
194 free(mypath); |
|
195 mypath = NULL; |
|
196 |
|
197 return rc; |
|
198 } |
|
199 |
|
200 |
|
201 |
|
202 static int getstr(char **dest) |
|
203 { |
|
204 if (debug) |
|
205 syslog(LOG_NOTICE, "rdconfig: getstr: %s(%d): %s %s", mypath, linecnt, MBSE_SS(k), MBSE_SS(v)); |
|
206 |
|
207 *dest = xstrcpy(v); |
|
208 return 0; |
|
209 } |
|
210 |
|
211 |
|
212 |
|
213 static int getint(char **dest) |
|
214 { |
|
215 if (debug) |
|
216 syslog(LOG_NOTICE, "rdconfig: getint: %s(%d): %s %s", mypath, linecnt, MBSE_SS(k), MBSE_SS(v)); |
|
217 |
|
218 if (strspn(v,"0123456789") != strlen(v)) |
|
219 syslog(LOG_NOTICE, "rdconfig: %s(%d): %s %s - bad numeric", mypath, linecnt, MBSE_SS(k), MBSE_SS(v)); |
|
220 else |
|
221 *((int*)dest)=atoi(v); |
|
222 return 0; |
|
223 } |
|
224 |
|
225 |
|
226 |
|
227 static int getw1(char **dest) |
|
228 { |
|
229 char *p, *q = NULL, *r = NULL; |
|
230 w1_therm **tmpm; |
|
231 int rc = 0, tmpp; |
|
232 |
|
233 for (p = v; *p && !isspace(*p); p++); |
|
234 if (*p) |
|
235 *p++ = '\0'; |
|
236 while (*p && isspace(*p)) |
|
237 p++; |
|
238 if (*p == '\0') { |
|
239 syslog(LOG_NOTICE, "rdconfig: %s(%d): less then two tokens", mypath, linecnt); |
|
240 return 1; |
|
241 } |
|
242 |
|
243 for (q = p; *q && !isspace(*q); q++); |
|
244 if (*q && isspace(*q)) { |
|
245 if (*q) |
|
246 *q++ = '\0'; |
|
247 while (*q && isspace(*q)) |
|
248 q++; |
|
249 |
|
250 for (r = q; *r && !isspace(*r); r++); |
|
251 if (*r) |
|
252 *r++ = '\0'; |
|
253 rc = sscanf(p, "%d", &tmpp); |
|
254 if (rc != 1) { |
|
255 syslog(LOG_NOTICE, "rdconfig: getw1: %s(%d): %s is not a integer value", mypath, linecnt, p); |
|
256 return 1; |
|
257 } |
|
258 if (debug) |
|
259 syslog(LOG_NOTICE, "rdconfig: getw1: %s(%d): %s %d %s %s", mypath, linecnt, v, tmpp, q, r); |
|
260 } |
|
261 |
|
262 for (tmpm = (w1_therm**)dest; *tmpm; tmpm=&((*tmpm)->next)); |
|
263 (*tmpm) = (w1_therm *) xmalloc(sizeof(w1_therm)); |
|
264 (*tmpm)->next = NULL; |
|
265 (*tmpm)->master = xstrcpy(v); |
|
266 (*tmpm)->bus = tmpp; |
|
267 (*tmpm)->name = xstrcpy(q); |
|
268 (*tmpm)->alias = xstrcpy(r); |
|
269 (*tmpm)->present = 0; |
|
270 (*tmpm)->lastval = 0; |
|
271 (*tmpm)->update = 0; |
|
272 |
|
273 return 0; |
|
274 } |
|
275 |
|
276 |
|
277 |
|
278 #ifdef HAVE_WIRINGPI_H |
|
279 static int getrcs(char **dest) |
|
280 { |
|
281 char *p, *q = NULL, *r = NULL; |
|
282 rc_switch **tmpm; |
|
283 |
|
284 for (p = v; *p && !isspace(*p); p++); |
|
285 if (*p) |
|
286 *p++ = '\0'; |
|
287 while (*p && isspace(*p)) |
|
288 p++; |
|
289 if (*p == '\0') { |
|
290 syslog(LOG_NOTICE, "rdconfig: %s(%d): less then two tokens", mypath, linecnt); |
|
291 return 1; |
|
292 } |
|
293 |
|
294 for (q = p; *q && !isspace(*q); q++); |
|
295 if (*q && isspace(*q)) { |
|
296 if (*q) |
|
297 *q++ = '\0'; |
|
298 while (*q && isspace(*q)) |
|
299 q++; |
|
300 |
|
301 for (r = q; *r && !isspace(*r); r++); |
|
302 if (*r) |
|
303 *r++ = '\0'; |
|
304 if (debug) |
|
305 syslog(LOG_NOTICE, "rdconfig: getrcs: %s(%d): %s %s", mypath, linecnt, v, p); |
|
306 } |
|
307 |
|
308 for (tmpm = (rc_switch**)dest; *tmpm; tmpm=&((*tmpm)->next)); |
|
309 (*tmpm) = (rc_switch *) xmalloc(sizeof(rc_switch)); |
|
310 (*tmpm)->next = NULL; |
|
311 (*tmpm)->address = xstrcpy(v); |
|
312 (*tmpm)->alias = xstrcpy(p); |
|
313 |
|
314 return 0; |
|
315 } |
|
316 #endif |
|
317 |
|
318 |
|
319 /* |
|
320 static int getbyt(char **dest) |
|
321 { |
|
322 Log_Msg("[rdconfig] getbyt: %s(%d): %s %s", mypath, linecnt, k, v); |
|
323 if (strspn(v,"0123456789") != strlen(v)) |
|
324 Log_Msg("[rdconfig] %s(%d): %s %s - bad numeric", mypath, linecnt, S(k), S(v)); |
|
325 else |
|
326 *((Uint8*)dest)=atoi(v); |
|
327 return 0; |
|
328 } |
|
329 */ |
|
330 |
|
331 |
|
332 /* |
|
333 static int gethex(char **dest) |
|
334 { |
|
335 unsigned int val = 0; |
|
336 int rc; |
|
337 |
|
338 Log_Msg("[rdconfig] gethex: %s(%d): %s %s", mypath, linecnt, k, v); |
|
339 rc = sscanf(v, "%08x", &val); |
|
340 if (rc != 1) { |
|
341 Log_Msg("[rdconfig] %s(%d): %s %s - bad hex value", mypath, linecnt, S(k), S(v)); |
|
342 return 1; |
|
343 } |
|
344 *((int*)dest) = val; |
|
345 |
|
346 return 0; |
|
347 } |
|
348 */ |
|
349 |
|
350 |