commit | author | age
|
d6b4a7
|
1 |
/********************************************************************************* |
G |
2 |
* Copyright: (C) 2019 LingYun IoT System Studio |
|
3 |
* All rights reserved. |
|
4 |
* |
|
5 |
* Filename: conf.c |
|
6 |
* Description: This file is mqttd 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 |
|
|
18 |
int mqttd_parser_conf(const char *conf_file, mqtt_ctx_t *ctx, int debug) |
|
19 |
{ |
|
20 |
dictionary *ini; |
|
21 |
const char *str; |
|
22 |
int val; |
|
23 |
int rv = 0; |
|
24 |
|
|
25 |
if( !conf_file || !ctx ) |
|
26 |
{ |
|
27 |
fprintf(stderr, "%s() Invalid input arguments\n", __func__); |
|
28 |
return -1; |
|
29 |
} |
|
30 |
|
|
31 |
memset(ctx, 0, sizeof(*ctx)); |
|
32 |
|
|
33 |
ini = iniparser_load(conf_file); |
|
34 |
if( !ini ) |
|
35 |
{ |
|
36 |
fprintf(stderr, "ERROR: Configure file '%s' load failed\n", conf_file); |
|
37 |
return -2; |
|
38 |
} |
|
39 |
|
|
40 |
/*+------------------------------------------------------+ |
|
41 |
*| parser logger settings and start logger system | |
|
42 |
*+------------------------------------------------------+*/ |
|
43 |
if( !debug ) |
|
44 |
{ |
|
45 |
str = iniparser_getstring(ini, "logger:file", "/tmp/mqttd.log"); |
|
46 |
strncpy(ctx->logfile, str, sizeof(ctx->logfile)); |
|
47 |
ctx->logsize = iniparser_getint(ini, "logger:size", 1024); |
|
48 |
ctx->loglevel = iniparser_getint(ini, "logger:level", LOG_LEVEL_INFO); |
|
49 |
} |
|
50 |
else |
|
51 |
{ |
|
52 |
strncpy(ctx->logfile, "console", sizeof(ctx->logfile)); |
|
53 |
ctx->loglevel = LOG_LEVEL_DEBUG; |
|
54 |
ctx->logsize = 0; |
|
55 |
} |
|
56 |
|
|
57 |
if( log_open(ctx->logfile, ctx->loglevel, ctx->logsize, LOG_LOCK_DISABLE) < 0 ) |
|
58 |
{ |
|
59 |
fprintf(stderr, "Logger system initialise failure\n"); |
|
60 |
return -2; |
|
61 |
} |
|
62 |
|
|
63 |
log_info("Logger system initialise ok\n"); |
|
64 |
|
|
65 |
|
|
66 |
/*+------------------------------------------------------+ |
|
67 |
*| parser production ID | |
|
68 |
*+------------------------------------------------------+*/ |
|
69 |
|
|
70 |
if( !(str=iniparser_getstring(ini, "common:devid", NULL)) ) |
|
71 |
{ |
|
72 |
log_error("ERROR: Parser device ID failure\n"); |
|
73 |
rv = -3; |
|
74 |
goto cleanup; |
|
75 |
} |
|
76 |
/* cJSON parser ID will get "" */ |
|
77 |
snprintf(ctx->devid, sizeof(ctx->devid), "\"%s\"", str); |
|
78 |
log_info("Parser device ID [%s]\n", ctx->devid); |
|
79 |
|
|
80 |
|
|
81 |
/*+------------------------------------------------------+ |
|
82 |
*| parser hardware module configuration | |
|
83 |
*+------------------------------------------------------+*/ |
|
84 |
|
|
85 |
/* relay */ |
|
86 |
ctx->hwconf.relay=iniparser_getint(ini, "hardware:relay", 0); |
|
87 |
if( !ctx->hwconf.relay ) |
|
88 |
log_warn("Parser relay module disabled\n"); |
|
89 |
else |
|
90 |
log_info("Parser relay module enabled\n"); |
|
91 |
|
|
92 |
/* RGB 3-colors LED */ |
|
93 |
ctx->hwconf.led=iniparser_getint(ini, "hardware:rgbled", 0); |
|
94 |
if( !ctx->hwconf.led ) |
|
95 |
log_warn("Parser RGB 3-colors Led module disabled\n"); |
|
96 |
else |
|
97 |
log_info("Parser RGB 3-colors Led module enabled\n"); |
|
98 |
|
|
99 |
/* beeper */ |
|
100 |
ctx->hwconf.beeper=iniparser_getint(ini, "hardware:beep", 0); |
|
101 |
if( !ctx->hwconf.beeper ) |
|
102 |
log_warn("Parser beeper module disabled\n"); |
|
103 |
else |
|
104 |
log_info("Parser beeper module enabled\n"); |
|
105 |
|
|
106 |
/* DS18B20 temperature module */ |
|
107 |
ctx->hwconf.ds18b20=iniparser_getint(ini, "hardware:ds18b20", 0); |
|
108 |
if( !ctx->hwconf.ds18b20 ) |
|
109 |
log_warn("Parser DS18B20 temperature module disabled\n"); |
|
110 |
else |
|
111 |
log_info("Parser DS18B20 temperature module enabled\n"); |
|
112 |
|
|
113 |
/* SHT20 temperature and hummidity module */ |
|
114 |
ctx->hwconf.sht2x=iniparser_getint(ini, "hardware:sht2x", 0); |
|
115 |
if( !ctx->hwconf.sht2x ) |
|
116 |
log_warn("Parser SHT2X temperature and hummidity module disabled\n"); |
|
117 |
else |
|
118 |
log_info("Parser SHT2X temperature and hummidity module enabled\n"); |
|
119 |
|
|
120 |
/* TSL2561 light intensity sensor module */ |
|
121 |
ctx->hwconf.tsl2561=iniparser_getint(ini, "hardware:tsl2561", 0); |
|
122 |
if( !ctx->hwconf.tsl2561 ) |
|
123 |
log_warn("Parser TSL2561 light intensity sensor module disabled\n"); |
|
124 |
else |
|
125 |
log_info("Parser TSL2561 light intensity sensor module enabled\n"); |
|
126 |
|
|
127 |
/*+------------------------------------------------------+ |
|
128 |
*| parser broker settings | |
|
129 |
*+------------------------------------------------------+*/ |
|
130 |
|
|
131 |
if( !(str=iniparser_getstring(ini, "broker:hostname", NULL)) ) |
|
132 |
{ |
|
133 |
log_error("ERROR: Parser MQTT broker server hostname failure\n"); |
|
134 |
rv = -4; |
|
135 |
goto cleanup; |
|
136 |
} |
|
137 |
strncpy(ctx->host, str, sizeof(ctx->host) ); |
|
138 |
|
|
139 |
if( (val=iniparser_getint(ini, "broker:port", -1)) < 0 ) |
|
140 |
{ |
|
141 |
log_error("ERROR: Parser MQTT broker server port failure\n"); |
|
142 |
rv = -5; |
|
143 |
goto cleanup; |
|
144 |
} |
|
145 |
ctx->port = val; |
|
146 |
log_info("Parser MQTT broker server [%s:%d]\n", ctx->host, ctx->port); |
|
147 |
|
|
148 |
str=iniparser_getstring(ini, "broker:username", NULL); |
|
149 |
strncpy(ctx->uid, str, sizeof(ctx->uid) ); |
|
150 |
|
|
151 |
str=iniparser_getstring(ini, "broker:password", NULL); |
|
152 |
strncpy(ctx->pwd, str, sizeof(ctx->pwd) ); |
|
153 |
|
|
154 |
if( ctx->uid && ctx->pwd ) |
|
155 |
log_info("Parser broker author by [%s:%s]\n", ctx->uid, ctx->pwd); |
|
156 |
|
|
157 |
ctx->keepalive = iniparser_getint(ini, "broker:keepalive", DEF_KEEPALIVE); |
|
158 |
log_info("Parser broker keepalive timeout [%d] seconds\n", ctx->keepalive); |
|
159 |
|
|
160 |
/*+------------------------------------------------------+ |
|
161 |
*| parser publisher settings | |
|
162 |
*+------------------------------------------------------+*/ |
|
163 |
|
|
164 |
if( !(str=iniparser_getstring(ini, "publisher:pubTopic", NULL)) ) |
|
165 |
{ |
|
166 |
log_error("ERROR: Parser MQTT broker publisher topic failure\n"); |
|
167 |
rv = -6; |
|
168 |
goto cleanup; |
|
169 |
} |
|
170 |
strncpy(ctx->pubTopic, str, sizeof(ctx->pubTopic) ); |
|
171 |
|
|
172 |
ctx->pubQos = iniparser_getint(ini, "publisher:pubQos", DEF_QOS); |
|
173 |
ctx->interval = iniparser_getint(ini, "publisher:interval", DEF_PUBINTERVAL); |
|
174 |
log_info("Parser publisher topic \"%s\" with Qos[%d] interval[%d]\n", ctx->pubTopic, ctx->pubQos, ctx->interval); |
|
175 |
|
|
176 |
/*+------------------------------------------------------+ |
|
177 |
*| parser subscriber settings | |
|
178 |
*+------------------------------------------------------+*/ |
|
179 |
|
|
180 |
if( !(str=iniparser_getstring(ini, "subsciber:subTopic", NULL)) ) |
|
181 |
{ |
|
182 |
log_error("ERROR: Parser MQTT broker publisher topic failure\n"); |
|
183 |
rv = -7; |
|
184 |
goto cleanup; |
|
185 |
} |
|
186 |
strncpy(ctx->subTopic, str, sizeof(ctx->subTopic) ); |
|
187 |
|
|
188 |
ctx->subQos = iniparser_getint(ini, "subsciber:subQos", DEF_QOS); |
|
189 |
log_info("Parser subscriber topic \"%s\" with Qos[%d]\n", ctx->subTopic, ctx->subQos); |
|
190 |
|
|
191 |
cleanup: |
|
192 |
if( ini ) |
|
193 |
iniparser_freedict(ini); |
|
194 |
|
|
195 |
if( rv ) |
|
196 |
log_close(); |
|
197 |
|
|
198 |
return rv; |
|
199 |
} |
|
200 |
|