guowenxue
2023-07-10 5e9d03d507aad324a803eb8795e0eed6fb671761
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*********************************************************************************
 *      Copyright:  (C) 2019 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  conf.c
 *    Description:  This file is mqttd configure file parser function
 *                 
 *        Version:  1.0.0(2019年06月25日)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "2019年06月25日 22时23分55秒"
 *                 
 ********************************************************************************/
#include "conf.h"
#include "lylib/logger.h"
#include "lylib/iniparser.h"
 
 
int mqttd_parser_conf(const char *conf_file, mqtt_ctx_t *ctx, int debug)
{
    dictionary          *ini;
    char                *str;
    int                  val; 
 
    memset(ctx, 0, sizeof(*ctx));
 
    if( !conf_file )
    { 
        strncpy(ctx->id, "\"rpi3b001\"", sizeof(ctx->id));
 
        /* logger settings */
        strncpy(ctx->logfile, DBG_LOG_FILE, sizeof(ctx->logfile));
        ctx->loglevel = LOG_LEVEL_DEBUG;
        ctx->logsize = 1024;
 
        if( logger_init(ctx->logfile, ctx->loglevel, ctx->logsize) < 0 ) 
        {
            fprintf(stderr, "Logger system initialise failure\n");
            return -2;
        } 
        log_nrml("Logger system initialise ok\n");
        log_warn("WARNNING: Use default MQTT configure\n");
 
        /* Broker settings */
        strncpy(ctx->host, DEF_BORKER_HOSTNAME, sizeof(ctx->host));
        ctx->port = DEF_BROKER_PORT;
        log_nrml("Use default broker server [%s:%d]\n", ctx->host, ctx->port);
 
        strncpy(ctx->uid, DEF_BROKER_USERNAME, sizeof(ctx->uid));
        strncpy(ctx->pwd, DEF_BROKER_PASSWD, sizeof(ctx->pwd));
        log_nrml("Use default broker author by [%s:%s]\n", ctx->uid, ctx->pwd);
 
        ctx->keepalive = DEF_BROKER_KEEPALIVE;
        log_nrml("Use default broker keepalive timeout [%d] seconds\n", ctx->keepalive);
        
        /* Publisher settings */
        strncpy(ctx->pubTopic, DEF_PUBTOPIC, sizeof(ctx->pubTopic));
        ctx->pubQos = DEF_PUBQOS;
        ctx->interval = DEF_PUBINTERVAL;
        log_nrml("Use default publisher topic \"%s\" with Qos[%d] interval[%d]\n", ctx->pubTopic, ctx->pubQos, ctx->interval);
 
        /* Subscriber settings */
        strncpy(ctx->subTopic, DEF_SUBTOPIC, sizeof(ctx->subTopic));
        ctx->subQos = DEF_SUBQOS;
        log_nrml("Use default subscriber topic \"%s\" with Qos[%d]\n", ctx->subTopic, ctx->subQos);
 
        return 0;
    }
 
 
    ini = iniparser_load(conf_file);
    if( !ini )
    {
        fprintf(stderr, "ERROR: cannot parse file: '%s'\n", conf_file);
        return -1;
    }
 
    /*+------------------------------------------------------+
     *|    parser logger settings and start logger system    |
     *+------------------------------------------------------+*/
    if( !debug ) 
    {
        str = iniparser_getstring(ini, "logger:file", "/tmp/mqttd.log");
        strncpy(ctx->logfile, str, sizeof(ctx->logfile));
        ctx->logsize = iniparser_getint(ini, "logger:size", 1024);
        ctx->loglevel = iniparser_getint(ini, "logger:level", LOG_LEVEL_DEBUG);
    }
    else
    {
        strncpy(ctx->logfile, DBG_LOG_FILE, sizeof(ctx->logfile));
        ctx->loglevel = LOG_LEVEL_DEBUG;
        ctx->logsize = 1024;
    }
 
    if( logger_init(ctx->logfile, ctx->loglevel, ctx->logsize) < 0 ) 
    {
        fprintf(stderr, "Logger system initialise failure\n");
        return -2;
    }
 
    log_nrml("Logger system initialise ok\n");
 
 
    /*+------------------------------------------------------+
     *|      parser hardware module configuration ID         |
     *+------------------------------------------------------+*/
 
    /* hardware module enable or not */
    ctx->hwconf.enable=iniparser_getint(ini, "hardware:enable", 0);
    log_nrml("Parser hardware modules [%s]\n", ctx->hwconf.enable ? "enable" : "disable");
 
    /* relay control light  */
    ctx->hwconf.relay_pin=iniparser_getint(ini, "hardware:relay_pin", 0);
    if( !ctx->hwconf.relay_pin )
        log_nrml("Parser relay module disabled\n");
    else
        log_nrml("Parser relay connected wPI #pin number [%d]\n", ctx->hwconf.relay_pin); 
 
    /* buzzer */
    ctx->hwconf.beep_pin=iniparser_getint(ini, "hardware:beep_pin", 0); 
    log_nrml("Parser buzzer connected wPI #pin number [%d]\n", ctx->hwconf.beep_pin); 
 
    /* Beep max. times set beep 10 */ 
    ctx->hwconf.beep_times = iniparser_getint(ini, "hardware:beep_times", 0); 
    log_nrml("Parser inbreak buzzer beep times [%d]\n", ctx->hwconf.beep_times);
 
 
    /* RGB 3-colors LED  */
    ctx->hwconf.red_pin=iniparser_getint(ini, "hardware:red_pin", 0);
    ctx->hwconf.green_pin=iniparser_getint(ini, "hardware:green_pin", 0);
    ctx->hwconf.blue_pin=iniparser_getint(ini, "hardware:blue_pin", 0);
    if( !ctx->hwconf.red_pin && !ctx->hwconf.green_pin && !ctx->hwconf.blue_pin )
    {
        ctx->hwconf.leds = 0;
        log_nrml("Parser RGB leds module disabled\n");
    }
    else
    {
        ctx->hwconf.leds = 1;
        log_nrml("Parser RGB led connected wPI #pin number [%d,%d,%d]\n", 
            ctx->hwconf.red_pin, ctx->hwconf.green_pin, ctx->hwconf.blue_pin);
    }
 
    /* temperature sensor ds18b20  */
    ctx->hwconf.ds18b20=iniparser_getint(ini, "hardware:ds18b20", 0);
    log_nrml("Parser temperature sensor DS18B20 modules [%s]\n", ctx->hwconf.ds18b20 ? "enable" : "disable");
 
    /* temperature and hummidity sensor sht2x  */
    ctx->hwconf.sht2x=iniparser_getint(ini, "hardware:sht2x", 0);
    log_nrml("Parser temperature and hummidity sensor sht2x modules [%s]\n", ctx->hwconf.sht2x ? "enable" : "disable");
 
    /* light intensity sensor TSL2561 */
    ctx->hwconf.lux=iniparser_getint(ini, "hardware:lux", 0);
    log_nrml("Parser light intensity sensor TSL2561 modules [%s]\n", ctx->hwconf.ds18b20 ? "enable" : "disable");
 
    ctx->hwconf.infrared_pin=iniparser_getint(ini, "hardware:infrared_pin", 0);
    log_nrml("Parser infrared pin connected wPI #pin number [%d]\n", ctx->hwconf.infrared_pin);
 
    if( ctx->hwconf.infrared_pin )
    {
        ctx->hwconf.inbreak_beep = iniparser_getint(ini, "hardware:inbreak_beep", 0); 
        log_nrml("Parser %s inbreak beep\n", ctx->hwconf.inbreak_beep? "enable" : "disable");
        if( !ctx->hwconf.beep_pin ) 
        {
            log_warn("WARNNING: beep hardware not enable and will disable inbreak beep\n");
            ctx->hwconf.inbreak_beep = 0;
        }
 
        ctx->hwconf.fillin_light = iniparser_getint(ini, "hardware:fillin_light", 0);
        log_nrml("Parser inbreak fill-in light mode [%d]\n", ctx->hwconf.fillin_light);
 
        if( !ctx->hwconf.relay_pin )
        {
            log_warn("WARNNING: Relay hardware not enable and change fill-in light mode to OFF\n");
            ctx->hwconf.fillin_light = FILLIN_LIGHT_OFF;
        }
 
        if( !ctx->hwconf.lux )
        {
            log_warn("WARNNING: LUX sensor hardware not enable and change fill-in light mode to ON\n");
            ctx->hwconf.fillin_light = FILLIN_LIGHT_ON;
        }
 
        ctx->hwconf.light_intval = iniparser_getint(ini, "hardware:light_intval", 20);
        log_nrml("Parser inbreak fill-in light interval time [%d]\n", ctx->hwconf.light_intval);
 
        ctx->hwconf.lux_threshold = iniparser_getdouble(ini, "hardware:lux_threshold", 0.1);
        log_nrml("Parser fill-in light threshold lux [%.03f]\n", ctx->hwconf.lux_threshold);
    }
 
 
    /*+------------------------------------------------------+
     *|               parser production ID                   |
     *+------------------------------------------------------+*/
 
    if( !(str=iniparser_getstring(ini, "common:id", NULL)) )
    {
        log_err("ERROR: Parser production ID failure\n");
        return -2;
    }
    /* cJSON parser ID will get ""  */
    snprintf(ctx->id, sizeof(ctx->id), "\"%s\"", str);
    log_nrml("Parser production ID [%s]\n", ctx->id);
 
 
    /*+------------------------------------------------------+
     *|              parser broker settings                  |
     *+------------------------------------------------------+*/
 
    if( !(str=iniparser_getstring(ini, "broker:hostname", NULL)) )
    {
        log_err("ERROR: Parser broker server hostname failure\n");
        return -2;
    }
    strncpy(ctx->host, str, sizeof(ctx->host) );
 
    if( (val=iniparser_getint(ini, "broker:port", -1)) < 0 ) 
    {
        log_err("ERROR: Parser broker server port failure\n");
        return -2;
    }
    ctx->port = val;
    log_nrml("Parser broker server [%s:%d]\n", ctx->host, ctx->port);
 
    str=iniparser_getstring(ini, "broker:username", DEF_BROKER_USERNAME);
    strncpy(ctx->uid, str, sizeof(ctx->uid) );
 
    str=iniparser_getstring(ini, "broker:password", DEF_BROKER_PASSWD);
    strncpy(ctx->pwd, str, sizeof(ctx->pwd) );
 
    log_nrml("Parser broker author by [%s:%s]\n", ctx->uid, ctx->pwd);
 
    ctx->keepalive = iniparser_getint(ini, "broker:keepalive", DEF_BROKER_KEEPALIVE);
    log_nrml("Parser broker keepalive timeout [%d] seconds\n", ctx->keepalive);
 
    /*+------------------------------------------------------+
     *|             parser publisher settings                |
     *+------------------------------------------------------+*/
 
    str=iniparser_getstring(ini, "publisher:pubTopic", DEF_PUBTOPIC);
    strncpy(ctx->pubTopic, str, sizeof(ctx->pubTopic) );
 
    ctx->pubQos = iniparser_getint(ini, "publisher:pubQos", DEF_PUBQOS);
    ctx->interval = iniparser_getint(ini, "publisher:interval", DEF_PUBINTERVAL);
    log_nrml("Parser publisher topic \"%s\" with Qos[%d] interval[%d]\n", ctx->pubTopic, ctx->pubQos, ctx->interval);
 
    /*+------------------------------------------------------+
     *|             parser subscriber settings               |
     *+------------------------------------------------------+*/
 
    str=iniparser_getstring(ini, "subsciber:subTopic", DEF_SUBTOPIC);
    strncpy(ctx->subTopic, str, sizeof(ctx->subTopic) );
 
    ctx->subQos = iniparser_getint(ini, "subsciber:subQos", DEF_SUBQOS);
    log_nrml("Parser subscriber topic \"%s\" with Qos[%d]\n", ctx->subTopic, ctx->subQos);
 
    return 0;
}