guowenxue
2021-01-28 a903b916f09419e7324a2352232e601acc598d67
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
/*********************************************************************************
 *      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/ini_parser.h"
 
 
int parser_conf(const char *conf_file, lightd_ctx_t *ctx, int debug)
{
    dictionary          *ini;
    char                *str;
    int                  val; 
    log_ctx_t           *log_ctx;
    hal_ctx_t           *hal_ctx;
    mqtt_ctx_t          *mqtt_ctx;
 
    static logger_t      logger;
 
 
    if( !conf_file || !ctx )
    { 
        fprintf(stderr, "ERROR: parser configure file or ctx is NULL\n");
        return 0;
    }
 
    memset(ctx, 0, sizeof(*ctx));
 
    log_ctx = &ctx->log_ctx;
    hal_ctx = &ctx->hal_ctx;
    mqtt_ctx = &ctx->mqtt_ctx;
 
 
    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(log_ctx->logfile, str, sizeof(log_ctx->logfile));
        log_ctx->logsize = iniparser_getint(ini, "logger:size", 1024);
        log_ctx->loglevel = iniparser_getint(ini, "logger:level", LOG_LEVEL_DEBUG);
    }
    else
    {
        strncpy(log_ctx->logfile, DBG_LOG_FILE, sizeof(log_ctx->logfile));
        log_ctx->loglevel = LOG_LEVEL_DEBUG;
        log_ctx->logsize = 1024;
    }
 
    if( logger_init(&logger, log_ctx->logfile, log_ctx->loglevel, log_ctx->logsize) < 0 || logger_open()<0 ) 
    {
        fprintf(stderr, "Logger system initialise failure\n");
        return -2;
    }
 
    log_nrml("Logger system initialise ok\n");
 
 
    /*+------------------------------------------------------+
     *|               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(mqtt_ctx->id, sizeof(mqtt_ctx->id), "\"%s\"", str);
    log_nrml("Parser production ID [%s]\n", mqtt_ctx->id);
 
 
    /*+------------------------------------------------------+
     *|      parser hardware module configuration ID         |
     *+------------------------------------------------------+*/
 
    /* relay control light  */
    if( !(str=iniparser_getstring(ini, "hardware:relay_pin", NULL)) )
    {
        log_err("ERROR: Parser Relay pins failure\n");
        return -2;
    }
 
    log_nrml("Parser relay pins [%s]\n", str);
 
    {
        char             *ptr;
        int               i;
 
        ptr = strtok(str, ",");
        if( NULL == ptr )
        {
        log_err("relay pins format failure, can not find ',' in it.\n");
        return -2;
        }
 
        hal_ctx->light_cnt = 1;
        hal_ctx->light_pins[0] = atoi(ptr);
 
        for(i=0; i<LIGHT_MAX; i++)
        {
        ptr = strtok(NULL, ",");
        if( NULL != ptr)
        {
           hal_ctx->light_pins[i+1] = atoi(ptr); 
           hal_ctx->light_cnt ++;
        }
        }
    }
    
 
    hal_ctx->infrared_pin=iniparser_getint(ini, "hardware:infrared_pin", 0);
    log_nrml("Parser infrared pin connected BCM #pin number [%d]\n", hal_ctx->infrared_pin);
 
    hal_ctx->light_intval = iniparser_getint(ini, "hardware:light_intval", 20);
    log_nrml("Parser inbreak fill-in light interval time [%d]\n", hal_ctx->light_intval);
 
    hal_ctx->lux_threshold = iniparser_getdouble(ini, "hardware:lux_threshold", 0.1);
    log_nrml("Parser fill-in light threshold lux [%.03f]\n", hal_ctx->lux_threshold);
 
    /*+------------------------------------------------------+
     *|              parser broker settings                  |
     *+------------------------------------------------------+*/
 
    if( !(str=iniparser_getstring(ini, "broker:hostname", NULL)) )
    {
        log_err("ERROR: Parser broker server hostname failure\n");
        return -2;
    }
    strncpy(mqtt_ctx->host, str, sizeof(mqtt_ctx->host) );
 
    if( (val=iniparser_getint(ini, "broker:port", -1)) < 0 ) 
    {
        log_err("ERROR: Parser broker server port failure\n");
        return -2;
    }
    mqtt_ctx->port = val;
    log_nrml("Parser broker server [%s:%d]\n", mqtt_ctx->host, mqtt_ctx->port);
 
    str=iniparser_getstring(ini, "broker:username", NULL);
    strncpy(mqtt_ctx->uid, str, sizeof(mqtt_ctx->uid) );
 
    str=iniparser_getstring(ini, "broker:password", NULL);
    strncpy(mqtt_ctx->pwd, str, sizeof(mqtt_ctx->pwd) );
 
    log_nrml("Parser broker author by [%s:%s]\n", mqtt_ctx->uid, mqtt_ctx->pwd);
 
    mqtt_ctx->keepalive = iniparser_getint(ini, "broker:keepalive", 30);
    log_nrml("Parser broker keepalive timeout [%d] seconds\n", mqtt_ctx->keepalive);
 
    /*+------------------------------------------------------+
     *|             parser subscriber settings               |
     *+------------------------------------------------------+*/
 
    if( !(str=iniparser_getstring(ini, "subsciber:subTopic", NULL)) )
    {
        log_err("ERROR: Parser MQTT subscribe topic failure\n");
        return -2;
    }
    strncpy(mqtt_ctx->subTopic, str, sizeof(mqtt_ctx->subTopic) );
 
    mqtt_ctx->subQos = iniparser_getint(ini, "subsciber:subQos", 0);
    log_nrml("Parser subscriber topic \"%s\" with Qos[%d]\n", mqtt_ctx->subTopic, mqtt_ctx->subQos);
 
    return 0;
}