lib/rdconfig.c

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

mercurial