commit | author | age
|
e30a4c
|
1 |
/********************************************************************************* |
GW |
2 |
* Copyright: (C) 2019 LingYun IoT System Studio |
|
3 |
* All rights reserved. |
|
4 |
* |
|
5 |
* Filename: conf.c |
|
6 |
* Description: This file is iotd configure file parser function |
|
7 |
* |
|
8 |
* Version: 1.0.0(2019年06月25日) |
|
9 |
* Author: Guo Wenxue <guowenxue@gmail.com> |
|
10 |
* ChangeLog: 1, Release initial version on "2019年06月25日 22时23分55秒" |
|
11 |
* |
|
12 |
********************************************************************************/ |
|
13 |
#include "conf.h" |
|
14 |
#include "logger.h" |
|
15 |
#include "iniparser.h" |
|
16 |
|
|
17 |
enum |
|
18 |
{ |
|
19 |
_TYPE_INPUT, |
|
20 |
_TYPE_OUTPUT, |
|
21 |
}; |
|
22 |
|
|
23 |
/* conf format: "{light_indoor:6:0},{light_livroomL:13:0},{light_livroomR:19:0},{light_hallway:26:0}" */ |
|
24 |
int parser_gpio_info(int type, gpio_t *gpio, char *conf) |
|
25 |
{ |
|
26 |
char *ptr; |
|
27 |
char *pstart; |
|
28 |
char *pend; |
|
29 |
char buf[64]; |
|
30 |
int cnt = 0; |
|
31 |
int rv = 0; |
|
32 |
|
|
33 |
if( !gpio || !conf || strlen(conf)<3 ) |
|
34 |
{ |
|
35 |
log_error("Invalid input arguments.\n"); |
|
36 |
return -1; |
|
37 |
} |
|
38 |
|
|
39 |
pstart = strchr(conf, '{'); |
|
40 |
if( !pstart ) |
|
41 |
return 0; |
|
42 |
|
|
43 |
pend = strchr(pstart, '}'); |
|
44 |
|
|
45 |
while( pstart && pend ) |
|
46 |
{ |
|
47 |
memset(buf, 0, sizeof(buf)); |
|
48 |
|
|
49 |
strncpy(buf, pstart+1, pend-pstart-1); |
|
50 |
|
|
51 |
/* parser and get the GPIO name, BCM pin number, active power level */ |
|
52 |
|
|
53 |
{ |
|
54 |
/* check GPIO configure name too long or not */ |
|
55 |
ptr = strchr(buf, ':'); |
|
56 |
if( !ptr ) |
|
57 |
{ |
|
58 |
log_error("Found invalid GPIO configure: %s\n", buf); |
|
59 |
goto NEXT_LOOP; |
|
60 |
} |
|
61 |
|
|
62 |
if( ptr-buf > sizeof(gpio->input[cnt].name) ) |
|
63 |
{ |
|
64 |
log_error("Found GPIO name too long\n", buf); |
|
65 |
goto NEXT_LOOP; |
|
66 |
} |
|
67 |
|
|
68 |
/* use sscanf() to parser GPIO configured values */ |
|
69 |
if(_TYPE_INPUT == type ) |
|
70 |
{ |
|
71 |
rv = sscanf(buf, "%[^:]:%d:%d", gpio->input[cnt].name, &gpio->input[cnt].pin, &gpio->input[cnt].active_level); |
|
72 |
if( 3 == rv) |
|
73 |
{ |
|
74 |
log_info("parser GPIO input[%s] BCM[%d] active[%d]\n", gpio->input[cnt].name, gpio->input[cnt].pin, gpio->input[cnt].active_level); |
|
75 |
if( strstr(gpio->input[cnt].name, "infrared") ) |
|
76 |
{ |
|
77 |
log_info("parser GPIO enable infrared detect\n"); |
|
78 |
gpio->infrared_enable = 1; |
|
79 |
} |
|
80 |
cnt++; |
|
81 |
gpio->incnt = cnt; |
|
82 |
} |
|
83 |
else |
|
84 |
{ |
|
85 |
log_error("Found invalid GPIO configure: %s\n", buf); |
|
86 |
} |
|
87 |
} |
|
88 |
else |
|
89 |
{ |
|
90 |
rv = sscanf(buf, "%[^:]:%d:%d", gpio->output[cnt].name, &gpio->output[cnt].pin, &gpio->output[cnt].active_level); |
|
91 |
if( 3 == rv) |
|
92 |
{ |
|
93 |
log_info("parser GPIO output[%s] BCM[%d] active[%d]\n", gpio->output[cnt].name, gpio->output[cnt].pin, gpio->output[cnt].active_level); |
|
94 |
cnt++; |
|
95 |
gpio->outcnt = cnt; |
|
96 |
} |
|
97 |
else |
|
98 |
{ |
|
99 |
log_error("Found invalid GPIO configure: %s\n", buf); |
|
100 |
} |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
NEXT_LOOP: |
|
105 |
pstart = strchr(pend, '{'); |
|
106 |
if( pstart ) |
|
107 |
pend = strchr(pstart, '}'); |
|
108 |
} |
|
109 |
|
|
110 |
|
|
111 |
return gpio->outcnt; |
|
112 |
} |
|
113 |
|
|
114 |
int parser_conf(const char *conf_file, iotd_ctx_t *ctx, int debug) |
|
115 |
{ |
|
116 |
dictionary *ini; |
|
117 |
const char *str; |
|
118 |
int val; |
|
119 |
log_ctx_t *log_ctx; |
|
120 |
hal_ctx_t *hal_ctx; |
|
121 |
mqtt_ctx_t *mqtt_ctx; |
|
122 |
gpio_t *gpio; |
|
123 |
|
|
124 |
|
|
125 |
if( !conf_file || !ctx ) |
|
126 |
{ |
|
127 |
fprintf(stderr, "ERROR: parser configure file or ctx is NULL\n"); |
|
128 |
return 0; |
|
129 |
} |
|
130 |
|
|
131 |
memset(ctx, 0, sizeof(*ctx)); |
|
132 |
|
|
133 |
log_ctx = &ctx->log_ctx; |
|
134 |
hal_ctx = &ctx->hal_ctx; |
|
135 |
mqtt_ctx = &ctx->mqtt_ctx; |
|
136 |
|
|
137 |
|
|
138 |
ini = iniparser_load(conf_file); |
|
139 |
if( !ini ) |
|
140 |
{ |
|
141 |
fprintf(stderr, "ERROR: cannot parse file: '%s'\n", conf_file); |
|
142 |
return -1; |
|
143 |
} |
|
144 |
|
|
145 |
|
|
146 |
/*+------------------------------------------------------+ |
|
147 |
*| parser logger settings and start logger system | |
|
148 |
*+------------------------------------------------------+*/ |
|
149 |
if( !debug ) |
|
150 |
{ |
|
151 |
str = iniparser_getstring(ini, "logger:file", "/tmp/iotd.log"); |
|
152 |
strncpy(log_ctx->logfile, str, sizeof(log_ctx->logfile)); |
|
153 |
log_ctx->logsize = iniparser_getint(ini, "logger:size", 1024); |
|
154 |
log_ctx->loglevel = iniparser_getint(ini, "logger:level", LOG_LEVEL_DEBUG); |
|
155 |
} |
|
156 |
else |
|
157 |
{ |
|
158 |
strncpy(log_ctx->logfile, "console", sizeof(log_ctx->logfile)); |
|
159 |
log_ctx->loglevel = LOG_LEVEL_DEBUG; |
|
160 |
} |
|
161 |
|
|
162 |
if( log_open(log_ctx->logfile, log_ctx->loglevel, log_ctx->logsize, LOG_LOCK_DISABLE) < 0 ) |
|
163 |
{ |
|
164 |
fprintf(stderr, "Logger system initialise failure\n"); |
|
165 |
return -2; |
|
166 |
} |
|
167 |
|
|
168 |
log_info("Logger system initialise ok\n"); |
|
169 |
|
|
170 |
|
|
171 |
/*+------------------------------------------------------+ |
|
172 |
*| parser production ID | |
|
173 |
*+------------------------------------------------------+*/ |
|
174 |
|
|
175 |
if( !(str=iniparser_getstring(ini, "common:id", NULL)) ) |
|
176 |
{ |
|
177 |
log_error("ERROR: parser production ID failure\n"); |
|
178 |
return -2; |
|
179 |
} |
|
180 |
/* cJSON parser ID will get "" */ |
|
181 |
snprintf(mqtt_ctx->id, sizeof(mqtt_ctx->id), "\"%s\"", str); |
|
182 |
log_info("parser production ID [%s]\n", mqtt_ctx->id); |
|
183 |
|
|
184 |
|
|
185 |
/*+------------------------------------------------------+ |
|
186 |
*| parser hardware module configuration ID | |
|
187 |
*+------------------------------------------------------+*/ |
|
188 |
|
|
189 |
gpio = &hal_ctx->gpio; |
|
190 |
|
|
191 |
/* parser GPIO output pins */ |
|
192 |
if( !(str=iniparser_getstring(ini, "hardware:gpio_outpin", NULL)) ) |
|
193 |
{ |
|
194 |
log_warn("parser no GPIO output pins\n"); |
|
195 |
} |
|
196 |
else |
|
197 |
{ |
|
198 |
parser_gpio_info(_TYPE_OUTPUT, gpio, (char *)str); |
|
199 |
log_info("parser [%d] GPIO output pins configured\n", gpio->outcnt); |
|
200 |
} |
|
201 |
|
|
202 |
gpio->light_intval = iniparser_getint(ini, "hardware:light_intval", 20); |
|
203 |
log_info("parser relay controled light interval time [%d]\n", gpio->light_intval); |
|
204 |
|
|
205 |
/* parser GPIO input pins */ |
|
206 |
if( !(str=iniparser_getstring(ini, "hardware:gpio_inpin", NULL)) ) |
|
207 |
{ |
|
208 |
log_warn("parser no GPIO input pins\n"); |
|
209 |
} |
|
210 |
else |
|
211 |
{ |
|
212 |
parser_gpio_info(_TYPE_INPUT, gpio, (char *)str); |
|
213 |
log_info("parser [%d] GPIO input pins\n", gpio->incnt); |
|
214 |
} |
|
215 |
|
|
216 |
hal_ctx->lux_enable = iniparser_getint(ini, "hardware:lux", 0); |
|
217 |
if( hal_ctx->lux_enable ) |
|
218 |
{ |
|
219 |
hal_ctx->lux_threshold = iniparser_getdouble(ini, "hardware:lux_threshold", 0.1); |
|
220 |
log_info("parser LUX enable and threshold value set be [%.03f]\n", hal_ctx->lux_threshold); |
|
221 |
} |
|
222 |
|
|
223 |
hal_ctx->sht2x_enable = iniparser_getint(ini, "hardware:sht2x", 0); |
|
224 |
if( hal_ctx->sht2x_enable ) |
|
225 |
{ |
|
226 |
log_info("parser SHT2x sensor enabled\n"); |
|
227 |
} |
|
228 |
|
|
229 |
hal_ctx->ds18b20_enable = iniparser_getint(ini, "hardware:ds18b20", 0); |
|
230 |
if( hal_ctx->ds18b20_enable ) |
|
231 |
{ |
|
232 |
log_info("parser DS18B20 sensor enabled\n"); |
|
233 |
} |
|
234 |
|
|
235 |
|
|
236 |
/*+------------------------------------------------------+ |
|
237 |
*| parser broker settings | |
|
238 |
*+------------------------------------------------------+*/ |
|
239 |
|
|
240 |
if( !(str=iniparser_getstring(ini, "broker:hostname", NULL)) ) |
|
241 |
{ |
|
242 |
log_error("ERROR: Parser broker server hostname failure\n"); |
|
243 |
return -2; |
|
244 |
} |
|
245 |
strncpy(mqtt_ctx->host, str, sizeof(mqtt_ctx->host) ); |
|
246 |
|
|
247 |
if( (val=iniparser_getint(ini, "broker:port", -1)) < 0 ) |
|
248 |
{ |
|
249 |
log_error("ERROR: Parser broker server port failure\n"); |
|
250 |
return -2; |
|
251 |
} |
|
252 |
mqtt_ctx->port = val; |
|
253 |
log_info("Parser broker server [%s:%d]\n", mqtt_ctx->host, mqtt_ctx->port); |
|
254 |
|
|
255 |
str=iniparser_getstring(ini, "broker:username", NULL); |
|
256 |
strncpy(mqtt_ctx->uid, str, sizeof(mqtt_ctx->uid) ); |
|
257 |
|
|
258 |
str=iniparser_getstring(ini, "broker:password", NULL); |
|
259 |
strncpy(mqtt_ctx->pwd, str, sizeof(mqtt_ctx->pwd) ); |
|
260 |
|
|
261 |
log_info("Parser broker author by [%s:%s]\n", mqtt_ctx->uid, mqtt_ctx->pwd); |
|
262 |
|
|
263 |
mqtt_ctx->keepalive = iniparser_getint(ini, "broker:keepalive", 30); |
|
264 |
log_info("Parser broker keepalive timeout [%d] seconds\n", mqtt_ctx->keepalive); |
|
265 |
|
|
266 |
/*+------------------------------------------------------+ |
|
267 |
*| parser subscriber settings | |
|
268 |
*+------------------------------------------------------+*/ |
|
269 |
|
|
270 |
if( !(str=iniparser_getstring(ini, "subsciber:subTopic", NULL)) ) |
|
271 |
{ |
|
272 |
log_warn("WARNNING: Parser MQTT subscribe topic failure\n"); |
|
273 |
} |
|
274 |
else |
|
275 |
{ |
|
276 |
strncpy(mqtt_ctx->subTopic, str, sizeof(mqtt_ctx->subTopic) ); |
|
277 |
mqtt_ctx->subQos = iniparser_getint(ini, "subsciber:subQos", 0); |
|
278 |
mqtt_ctx->sub_enable = 1; |
|
279 |
|
|
280 |
log_info("Parser subscriber topic \"%s\" with Qos[%d]\n", mqtt_ctx->subTopic, mqtt_ctx->subQos); |
|
281 |
} |
|
282 |
|
|
283 |
/*+------------------------------------------------------+ |
|
284 |
*| parser publisher settings | |
|
285 |
*+------------------------------------------------------+*/ |
|
286 |
|
|
287 |
if( !(str=iniparser_getstring(ini, "publisher:pubTopic", NULL)) ) |
|
288 |
{ |
|
289 |
log_warn("WARNNING: Parser MQTT publisher topic failure\n"); |
|
290 |
} |
|
291 |
else |
|
292 |
{ |
|
293 |
strncpy(mqtt_ctx->pubTopic, str, sizeof(mqtt_ctx->pubTopic) ); |
|
294 |
mqtt_ctx->pubQos = iniparser_getint(ini, "publisher:pubQos", 0); |
|
295 |
mqtt_ctx->interval = iniparser_getint(ini, "publisher:interval", 60); |
|
296 |
mqtt_ctx->pub_enable = 1; |
|
297 |
|
|
298 |
log_info("Parser publisher topic \"%s\" with Qos[%d]\n", mqtt_ctx->pubTopic, mqtt_ctx->pubQos); |
|
299 |
} |
|
300 |
|
|
301 |
|
|
302 |
return 0; |
|
303 |
} |
|
304 |
|