guowenxue
2019-06-26 6a924a29cefbea27a101790343137db819b361be
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
/*********************************************************************************
 *      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 )
    { 
        fprintf(stderr, "\nWARNNING: Use default MQTT configure\n");
 
        /* logger settings */
        strncpy(ctx->logfile, DBG_LOG_FILE, sizeof(ctx->logfile));
        ctx->loglevel = LOG_LEVEL_DEBUG;
        ctx->logsize = 1024;
 
        /* Broker settings */
        strncpy(ctx->host, DEF_BORKER_HOSTNAME, sizeof(ctx->host));
        ctx->port = DEF_BROKER_PORT;
        strncpy(ctx->uid, DEF_BROKER_USERNAME, sizeof(ctx->uid));
        strncpy(ctx->pwd, DEF_BROKER_PASSWD, sizeof(ctx->pwd));
        ctx->keepalive = DEF_BROKER_KEEPALIVE;
        
        /* Publisher settings */
        strncpy(ctx->pubTopic, DEF_PUBTOPIC, sizeof(ctx->pubTopic));
        ctx->pubQos = DEF_PUBQOS;
        ctx->interval = DEF_PUBINTERVAL;
 
        /* Subscriber settings */
        strncpy(ctx->subTopic, DEF_SUBTOPIC, sizeof(ctx->subTopic));
        ctx->subQos = DEF_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 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 and get 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 and get broker author by [%s:%s]\n", ctx->uid, ctx->pwd);
 
    ctx->keepalive = iniparser_getint(ini, "broker:keepalive", DEF_BROKER_KEEPALIVE);
    log_nrml("Parser and get 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) );
    log_nrml("Parser and get publisher topic \"%s\"\n", ctx->pubTopic);
 
    ctx->pubQos = iniparser_getint(ini, "publisher:pubQos", DEF_PUBQOS);
    ctx->interval = iniparser_getint(ini, "publisher:interval", DEF_PUBINTERVAL);
    log_nrml("Parser and get publisher Qos[%d] and interval [%d] seconds\n", ctx->pubQos, ctx->interval);
 
    /*+------------------------------------------------------+
     *|             parser subscriber settings               |
     *+------------------------------------------------------+*/
 
    str=iniparser_getstring(ini, "subsciber:subTopic", DEF_SUBTOPIC);
    strncpy(ctx->subTopic, str, sizeof(ctx->subTopic) );
    log_nrml("Parser and get subscriber topic \"%s\"\n", ctx->subTopic);
 
    ctx->subQos = iniparser_getint(ini, "subsciber:subQos", DEF_SUBQOS);
    log_nrml("Parser and get subscriber Qos[%d]\n", ctx->subQos);
 
    return 0;
}