/*********************************************************************************
|
* 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 "logger.h"
|
#include "iniparser.h"
|
|
|
int mqttd_parser_conf(const char *conf_file, mqtt_ctx_t *ctx, int debug)
|
{
|
dictionary *ini;
|
const char *str;
|
int val;
|
int rv = 0;
|
|
if( !conf_file || !ctx )
|
{
|
fprintf(stderr, "%s() Invalid input arguments\n", __func__);
|
return -1;
|
}
|
|
memset(ctx, 0, sizeof(*ctx));
|
|
ini = iniparser_load(conf_file);
|
if( !ini )
|
{
|
fprintf(stderr, "ERROR: Configure file '%s' load failed\n", conf_file);
|
return -2;
|
}
|
|
/*+------------------------------------------------------+
|
*| 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_INFO);
|
}
|
else
|
{
|
strncpy(ctx->logfile, "console", sizeof(ctx->logfile));
|
ctx->loglevel = LOG_LEVEL_DEBUG;
|
ctx->logsize = 0;
|
}
|
|
if( log_open(ctx->logfile, ctx->loglevel, ctx->logsize, LOG_LOCK_DISABLE) < 0 )
|
{
|
fprintf(stderr, "Logger system initialise failure\n");
|
return -2;
|
}
|
|
log_info("Logger system initialise ok\n");
|
|
|
/*+------------------------------------------------------+
|
*| parser production ID |
|
*+------------------------------------------------------+*/
|
|
if( !(str=iniparser_getstring(ini, "common:devid", NULL)) )
|
{
|
log_error("ERROR: Parser device ID failure\n");
|
rv = -3;
|
goto cleanup;
|
}
|
/* cJSON parser ID will get "" */
|
snprintf(ctx->devid, sizeof(ctx->devid), "\"%s\"", str);
|
log_info("Parser device ID [%s]\n", ctx->devid);
|
|
|
/*+------------------------------------------------------+
|
*| parser hardware module configuration |
|
*+------------------------------------------------------+*/
|
|
/* relay */
|
ctx->hwconf.relay=iniparser_getint(ini, "hardware:relay", 0);
|
if( !ctx->hwconf.relay )
|
log_warn("Parser relay module disabled\n");
|
else
|
log_info("Parser relay module enabled\n");
|
|
/* RGB 3-colors LED */
|
ctx->hwconf.led=iniparser_getint(ini, "hardware:rgbled", 0);
|
if( !ctx->hwconf.led )
|
log_warn("Parser RGB 3-colors Led module disabled\n");
|
else
|
log_info("Parser RGB 3-colors Led module enabled\n");
|
|
/* beeper */
|
ctx->hwconf.beeper=iniparser_getint(ini, "hardware:beep", 0);
|
if( !ctx->hwconf.beeper )
|
log_warn("Parser beeper module disabled\n");
|
else
|
log_info("Parser beeper module enabled\n");
|
|
/* DS18B20 temperature module */
|
ctx->hwconf.ds18b20=iniparser_getint(ini, "hardware:ds18b20", 0);
|
if( !ctx->hwconf.ds18b20 )
|
log_warn("Parser DS18B20 temperature module disabled\n");
|
else
|
log_info("Parser DS18B20 temperature module enabled\n");
|
|
/* SHT20 temperature and hummidity module */
|
ctx->hwconf.sht2x=iniparser_getint(ini, "hardware:sht2x", 0);
|
if( !ctx->hwconf.sht2x )
|
log_warn("Parser SHT2X temperature and hummidity module disabled\n");
|
else
|
log_info("Parser SHT2X temperature and hummidity module enabled\n");
|
|
/* TSL2561 light intensity sensor module */
|
ctx->hwconf.tsl2561=iniparser_getint(ini, "hardware:tsl2561", 0);
|
if( !ctx->hwconf.tsl2561 )
|
log_warn("Parser TSL2561 light intensity sensor module disabled\n");
|
else
|
log_info("Parser TSL2561 light intensity sensor module enabled\n");
|
|
/*+------------------------------------------------------+
|
*| parser broker settings |
|
*+------------------------------------------------------+*/
|
|
if( !(str=iniparser_getstring(ini, "broker:hostname", NULL)) )
|
{
|
log_error("ERROR: Parser MQTT broker server hostname failure\n");
|
rv = -4;
|
goto cleanup;
|
}
|
strncpy(ctx->host, str, sizeof(ctx->host) );
|
|
if( (val=iniparser_getint(ini, "broker:port", -1)) < 0 )
|
{
|
log_error("ERROR: Parser MQTT broker server port failure\n");
|
rv = -5;
|
goto cleanup;
|
}
|
ctx->port = val;
|
log_info("Parser MQTT broker server [%s:%d]\n", ctx->host, ctx->port);
|
|
str=iniparser_getstring(ini, "broker:username", NULL);
|
strncpy(ctx->uid, str, sizeof(ctx->uid) );
|
|
str=iniparser_getstring(ini, "broker:password", NULL);
|
strncpy(ctx->pwd, str, sizeof(ctx->pwd) );
|
|
if( ctx->uid && ctx->pwd )
|
log_info("Parser broker author by [%s:%s]\n", ctx->uid, ctx->pwd);
|
|
ctx->keepalive = iniparser_getint(ini, "broker:keepalive", DEF_KEEPALIVE);
|
log_info("Parser broker keepalive timeout [%d] seconds\n", ctx->keepalive);
|
|
/*+------------------------------------------------------+
|
*| parser publisher settings |
|
*+------------------------------------------------------+*/
|
|
if( !(str=iniparser_getstring(ini, "publisher:pubTopic", NULL)) )
|
{
|
log_error("ERROR: Parser MQTT broker publisher topic failure\n");
|
rv = -6;
|
goto cleanup;
|
}
|
strncpy(ctx->pubTopic, str, sizeof(ctx->pubTopic) );
|
|
ctx->pubQos = iniparser_getint(ini, "publisher:pubQos", DEF_QOS);
|
ctx->interval = iniparser_getint(ini, "publisher:interval", DEF_PUBINTERVAL);
|
log_info("Parser publisher topic \"%s\" with Qos[%d] interval[%d]\n", ctx->pubTopic, ctx->pubQos, ctx->interval);
|
|
/*+------------------------------------------------------+
|
*| parser subscriber settings |
|
*+------------------------------------------------------+*/
|
|
if( !(str=iniparser_getstring(ini, "subsciber:subTopic", NULL)) )
|
{
|
log_error("ERROR: Parser MQTT broker publisher topic failure\n");
|
rv = -7;
|
goto cleanup;
|
}
|
strncpy(ctx->subTopic, str, sizeof(ctx->subTopic) );
|
|
ctx->subQos = iniparser_getint(ini, "subsciber:subQos", DEF_QOS);
|
log_info("Parser subscriber topic \"%s\" with Qos[%d]\n", ctx->subTopic, ctx->subQos);
|
|
cleanup:
|
if( ini )
|
iniparser_freedict(ini);
|
|
if( rv )
|
log_close();
|
|
return rv;
|
}
|