thermometers/rdconfig.c

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

mercurial