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