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