Fri, 02 May 2014 23:14:50 +0200
Tests for wiringPi are now not fatal
7 | 1 | /***************************************************************************** |
2 | * Copyright (C) 2008-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 | ||
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
27 | bool debug = FALSE; |
7 | 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 **); | |
9
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
37 | static int getw1(char **); |
7 | 38 | //static int getbyt(char **); |
39 | //static int gethex(char **); | |
40 | ||
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
41 | #define XSTR(x) #x |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
42 | #define STR(x) XSTR(x) |
7 | 43 | |
44 | /* | |
45 | * System configuration table | |
46 | */ | |
47 | key_list keytab[] = { | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
48 | {(char *)"mosq_host", getstr, &Config.mosq_host}, |
7 | 49 | {(char *)"mosq_port", getint, (char **)&Config.mosq_port}, |
9
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
50 | {(char *)"w1therm", getw1, (char **)&Config.w1therms}, |
17
b802305046dc
Tests for wiringPi are now not fatal
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
51 | #ifdef HAVE_WIRINGPI_H |
b802305046dc
Tests for wiringPi are now not fatal
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
52 | {(char *)"lcd_cols", getint, (char **)&Config.lcd_cols}, |
b802305046dc
Tests for wiringPi are now not fatal
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
53 | {(char *)"lcd_rows", getint, (char **)&Config.lcd_rows}, |
b802305046dc
Tests for wiringPi are now not fatal
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
54 | #endif |
7 | 55 | {NULL, NULL, NULL} |
56 | }; | |
57 | ||
58 | ||
59 | ||
60 | void killconfig(void) | |
61 | { | |
9
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
62 | w1_therm *tmp1, *old1; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
63 | |
7 | 64 | if (Config.name) |
65 | free(Config.name); | |
66 | Config.name = NULL; | |
67 | ||
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
68 | if (Config.mosq_host) |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
69 | free(Config.mosq_host); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
70 | Config.mosq_host= (char *)"localhost"; |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
71 | Config.mosq_port = 1883; |
9
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
72 | |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
73 | for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
74 | old1 = tmp1->next; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
75 | if (tmp1->master) |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
76 | free(tmp1->master); |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
77 | if (tmp1->name) |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
78 | free(tmp1->name); |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
79 | if (tmp1->alias) |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
80 | free(tmp1->alias); |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
81 | free(tmp1); |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
82 | } |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
83 | Config.w1therms = NULL; |
17
b802305046dc
Tests for wiringPi are now not fatal
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
84 | |
b802305046dc
Tests for wiringPi are now not fatal
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
85 | #ifdef HAVE_WIRINGPI_H |
b802305046dc
Tests for wiringPi are now not fatal
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
86 | Config.lcd_cols = 16; |
b802305046dc
Tests for wiringPi are now not fatal
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
87 | Config.lcd_rows = 2; |
b802305046dc
Tests for wiringPi are now not fatal
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
88 | #endif |
7 | 89 | } |
90 | ||
91 | ||
92 | ||
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
93 | int rdconfig(void) |
7 | 94 | { |
95 | char buf[256], *p; | |
96 | FILE *fp; | |
97 | int i, rc = 0; | |
98 | ||
99 | killconfig(); | |
100 | ||
101 | /* | |
102 | * Get config from the system path | |
103 | */ | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
104 | mypath = xstrcpy((char *)STR(ETCDIR)); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
105 | mypath = xstrcat(mypath, (char *)"/mbsepi-apps.conf"); |
7 | 106 | |
107 | if ((fp = fopen(mypath, "r")) == NULL) { | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
108 | syslog(LOG_NOTICE, "rdconfig: could not open %s", mypath); |
7 | 109 | return 1; |
110 | } | |
111 | ||
112 | linecnt = 0; | |
113 | while (fgets(buf, sizeof(buf) -1, fp)) { | |
114 | linecnt++; | |
115 | if (*(p = buf + strlen(buf) -1) != '\n') { | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
116 | syslog(LOG_NOTICE, "rdconfig: %s(%d): \"%s\" - line too long", mypath, linecnt, buf); |
7 | 117 | rc = 1; |
118 | break; | |
119 | } | |
120 | *p-- = '\0'; | |
121 | while ((p >= buf) && isspace(*p)) | |
122 | *p-- = '\0'; | |
123 | k = buf; | |
124 | while (*k && isspace(*k)) | |
125 | k++; | |
126 | p = k; | |
127 | while (*p && !isspace(*p)) | |
128 | p++; | |
129 | *p++='\0'; | |
130 | v = p; | |
131 | while (*v && isspace(*v)) | |
132 | v++; | |
133 | ||
134 | if ((*k == '\0') || (*k == '#')) { | |
135 | continue; | |
136 | } | |
137 | ||
138 | for (i = 0; keytab[i].key; i++) | |
139 | if (strcasecmp(k,keytab[i].key) == 0) | |
140 | break; | |
141 | ||
142 | if (keytab[i].key == NULL) { | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
143 | syslog(LOG_NOTICE, "rdconfig: %s(%d): %s %s - unknown keyword", mypath, linecnt, MBSE_SS(k), MBSE_SS(v)); |
7 | 144 | rc = 1; |
145 | break; | |
146 | } else if ((keytab[i].prc(keytab[i].dest))) { | |
147 | rc = 1; | |
148 | break; | |
149 | } | |
150 | ||
151 | } | |
152 | fclose(fp); | |
153 | ||
154 | free(mypath); | |
155 | mypath = NULL; | |
156 | ||
157 | return rc; | |
158 | } | |
159 | ||
160 | ||
161 | ||
162 | static int getstr(char **dest) | |
163 | { | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
164 | if (debug) |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
165 | syslog(LOG_NOTICE, "rdconfig: getstr: %s(%d): %s %s", mypath, linecnt, MBSE_SS(k), MBSE_SS(v)); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
166 | |
7 | 167 | *dest = xstrcpy(v); |
168 | return 0; | |
169 | } | |
170 | ||
171 | ||
172 | ||
173 | static int getint(char **dest) | |
174 | { | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
175 | if (debug) |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
176 | syslog(LOG_NOTICE, "rdconfig: getint: %s(%d): %s %s", mypath, linecnt, MBSE_SS(k), MBSE_SS(v)); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
177 | |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
178 | if (strspn(v,"0123456789") != strlen(v)) |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
179 | syslog(LOG_NOTICE, "rdconfig: %s(%d): %s %s - bad numeric", mypath, linecnt, MBSE_SS(k), MBSE_SS(v)); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
180 | else |
7 | 181 | *((int*)dest)=atoi(v); |
182 | return 0; | |
183 | } | |
184 | ||
185 | ||
9
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
186 | |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
187 | static int getw1(char **dest) |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
188 | { |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
189 | char *p, *q = NULL, *r = NULL; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
190 | w1_therm **tmpm; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
191 | int rc = 0, tmpp; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
192 | |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
193 | for (p = v; *p && !isspace(*p); p++); |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
194 | if (*p) |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
195 | *p++ = '\0'; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
196 | while (*p && isspace(*p)) |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
197 | p++; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
198 | if (*p == '\0') { |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
199 | syslog(LOG_NOTICE, "rdconfig: %s(%d): less then two tokens", mypath, linecnt); |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
200 | return 1; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
201 | } |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
202 | |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
203 | for (q = p; *q && !isspace(*q); q++); |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
204 | if (*q && isspace(*q)) { |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
205 | if (*q) |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
206 | *q++ = '\0'; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
207 | while (*q && isspace(*q)) |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
208 | q++; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
209 | |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
210 | for (r = q; *r && !isspace(*r); r++); |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
211 | if (*r) |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
212 | *r++ = '\0'; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
213 | rc = sscanf(p, "%d", &tmpp); |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
214 | if (rc != 1) { |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
215 | syslog(LOG_NOTICE, "rdconfig: getw1: %s(%d): %s is not a integer value", mypath, linecnt, p); |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
216 | return 1; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
217 | } |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
218 | if (debug) |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
219 | syslog(LOG_NOTICE, "rdconfig: getw1: %s(%d): %s %d %s %s", mypath, linecnt, v, tmpp, q, r); |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
220 | } |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
221 | |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
222 | for (tmpm = (w1_therm**)dest; *tmpm; tmpm=&((*tmpm)->next)); |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
223 | (*tmpm) = (w1_therm *) xmalloc(sizeof(w1_therm)); |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
224 | (*tmpm)->next = NULL; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
225 | (*tmpm)->master = xstrcpy(v); |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
226 | (*tmpm)->bus = tmpp; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
227 | (*tmpm)->name = xstrcpy(q); |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
228 | (*tmpm)->alias = xstrcpy(r); |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
229 | (*tmpm)->present = 0; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
230 | (*tmpm)->lastval = 0; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
231 | |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
232 | return 0; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
233 | } |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
234 | |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
235 | |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
236 | |
7 | 237 | /* |
238 | static int getbyt(char **dest) | |
239 | { | |
240 | Log_Msg("[rdconfig] getbyt: %s(%d): %s %s", mypath, linecnt, k, v); | |
241 | if (strspn(v,"0123456789") != strlen(v)) | |
242 | Log_Msg("[rdconfig] %s(%d): %s %s - bad numeric", mypath, linecnt, S(k), S(v)); | |
243 | else | |
244 | *((Uint8*)dest)=atoi(v); | |
245 | return 0; | |
246 | } | |
247 | */ | |
248 | ||
249 | ||
250 | /* | |
251 | static int gethex(char **dest) | |
252 | { | |
253 | unsigned int val = 0; | |
254 | int rc; | |
255 | ||
256 | Log_Msg("[rdconfig] gethex: %s(%d): %s %s", mypath, linecnt, k, v); | |
257 | rc = sscanf(v, "%08x", &val); | |
258 | if (rc != 1) { | |
259 | Log_Msg("[rdconfig] %s(%d): %s %s - bad hex value", mypath, linecnt, S(k), S(v)); | |
260 | return 1; | |
261 | } | |
262 | *((int*)dest) = val; | |
263 | ||
264 | return 0; | |
265 | } | |
266 | */ | |
267 | ||
268 |