LingYun IoT Studio NB-IoT research project
guowenxue
2018-11-20 e361b1654fefd1a563a82d335795f94b794b4873
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
/*********************************************************************************
 *      Copyright:  (C) 2018 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  iotd_conf.c
 *    Description:  This file is for iotd configure file parser API
 *                 
 *        Version:  1.0.0(2018年11月20日)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "2018年11月20日 12时11分46秒"
 *                 
 ********************************************************************************/
#include "iotd_conf.h"
#include "cp_iniparser.h"
 
int parser_iotd_conf(char *conf_file, iotd_conf_t *iotd_conf)
{
    dictionary          *ini;
    char                *str;
    int                  data;
    logger_t            *logger;
    nbiot_conf_t        *nbiot;
    lora_conf_t         *lora;
    mqtt_conf_t         *mqtt;
 
 
    if(!conf_file || !iotd_conf )
    {
        log_err("Invalid input arguments\n");
        return -1;
    }
 
    ini = iniparser_load(conf_file);
    if (ini==NULL) 
    {   
        log_err("cannot parse file: %s\n", conf_file);
        return -2 ;
    }   
 
    log_nrml("Start parser iotd configure file \"%s\"\n", conf_file);
 
    logger = &iotd_conf->logger;
    nbiot = &iotd_conf->nbiot_conf;
    lora = &iotd_conf->lora_conf;
    mqtt = &iotd_conf->mqtt_conf;
 
    /*+----------------------------------+
      |  Program  logger configuration   |
      +----------------------------------+*/
 
    str = iniparser_getstring(ini, "log:file", NULL);
    strncpy(logger->file, (strlen(str)>0 ? str : DEF_LOG_FILE), FILENAME_LEN);
    log_nrml("Set log file [%s] from configuration.\n", logger->file);
 
    /* If not set the log level from command line, then use the configure one*/
    if( !(logger->flag&CP_LOGGER_LEVEL_OPT) )
    {
        logger->level = iniparser_getint(ini, "log:level", LOG_LEVEL_NRML);
        log_nrml("Set log level[%d] from configuration.\n", logger->level);
    }
 
    /*  Set the log maximum file size in the configure file */
    logger->size = iniparser_getint(ini, "log:size", DEF_LOG_MAXSIZE);
    logger->size = logger->size>DEF_LOG_MAXSIZE ? DEF_LOG_MAXSIZE : logger->size;
    log_nrml("Set log size [%dKiB] from configuration\n", logger->size);
 
 
    /*+----------------------------------+
      |    NB-IoT Module configuration   |
      +----------------------------------+*/
 
    data = iniparser_getint(ini, "nbiot:enable", 0);
    nbiot->enable = data? ENABLE : DISABLE;
    log_nrml("Confiugre NB-IoT thread [%s]\n", nbiot->enable ? "enable" : "disable");
 
    str = iniparser_getstring(ini, "nbiot:comport", NULL);
    if( NULL!=str && strlen(str) > 0 )
    {
        strncpy(nbiot->comport, str, sizeof(nbiot->comport));
    }
    else
    {
        log_err("Configure without NB-IoT comport device, thread will not start\n");
        nbiot->enable = DISABLE;
    }
    log_nrml("Configure NB-IoT comport device as '%s'\n", nbiot->comport);
 
    nbiot->baudrate = iniparser_getint(ini, "nbiot:baudrate", 9600);
    log_nrml("Configure NB-IoT comport baudrate as '%d'\n", nbiot->baudrate);
 
    str = iniparser_getstring(ini, "nbiot:settings", "8N1N");
    if( NULL!=str && strlen(str) >= 4 )
    {
        strncpy(nbiot->settings, str, 4);
        log_nrml("Configure NB-IoT comport settings as '%s'\n", nbiot->settings);
    }
    else
    {
        log_err("Configure Invalid NB-IoT comport settings '%s', thread will not start.\n", str);
        nbiot->enable = DISABLE;
    }
 
 
    /*+----------------------------------+
      |     LoRa Module configuration    |
      +----------------------------------+*/
 
    data = iniparser_getint(ini, "lora:enable", 0);
    lora->enable = data? ENABLE : DISABLE;
    log_nrml("Confiugre LoRa thread [%s]\n", lora->enable ? "enable" : "disable");
 
    str = iniparser_getstring(ini, "lora:comport", NULL);
    if( NULL!=str && strlen(str) > 0 )
    {
        strncpy(lora->comport, str, sizeof(lora->comport));
    }
    else
    {
        log_err("Configure without LoRa comport device, thread will not start\n");
        lora->enable = DISABLE;
    }
    log_nrml("Configure LoRa comport device as '%s'\n", lora->comport);
 
    lora->baudrate = iniparser_getint(ini, "lora:baudrate", 115200);
    log_nrml("Configure LoRa comport baudrate as '%d'\n", lora->baudrate);
 
    str = iniparser_getstring(ini, "lora:settings", "8N1N");
    if( NULL!=str && strlen(str) >= 4 )
    {
        strncpy(lora->settings, str, 4);
        log_nrml("Configure LoRa comport settings as '%s'\n", lora->settings);
    }
    else
    {
        log_err("Configure Invalid LoRa comport settings '%s', thread will not start.\n", str);
        lora->enable = DISABLE;
    }
 
 
    /*+----------------------------------+
      |     MQTT Server configuration    |
      +----------------------------------+*/
 
    data = iniparser_getint(ini, "mqtt:enable", 0);
    mqtt->enable = data? ENABLE : DISABLE;
    log_nrml("Confiugre MQTT thread [%s]\n", mqtt->enable ? "enable" : "disable");
 
    str = iniparser_getstring(ini, "mqtt:hostname", NULL);
    if( !str || strlen(str) <= 0 )
    {
        log_err("Configure MQTT Server hostname invalid, thread will not start\n");
        mqtt->enable = DISABLE;
    }
    strncpy(mqtt->hostname, str, HOSTN_LEN);
    log_nrml("Configure MQTT Server hostname: \"%s\" \n", mqtt->hostname);
 
    data = iniparser_getint(ini, "mqtt:port", 0);
    if( !data )
    {
        log_err("Configure MQTT Server port invalid, thread will not start\n");
        mqtt->enable = DISABLE;
    }
    mqtt->port = data;
    log_nrml("Configure MQTT Server port: [%d]\n", mqtt->port);
 
 
 
    iniparser_freedict(ini);
 
    return 0;
}