/*********************************************************************************
|
* Copyright: (C) 2019 LingYun IoT System Studio
|
* All rights reserved.
|
*
|
* Filename: conf.c
|
* Description: This file is iotd 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"
|
|
enum
|
{
|
_TYPE_INPUT,
|
_TYPE_OUTPUT,
|
};
|
|
int parser_gpio_info(int type, gpio_t *gpio, char *conf)
|
{
|
char *pstart;
|
char *pend;
|
|
if( !gpio || !conf || strlen(conf)<3 )
|
{
|
log_err("Invalid input arguments.\n");
|
return -1;
|
}
|
|
printf("conf: %s\n", conf);
|
|
pstart = strchr(conf, '{');
|
pend = strchr(conf, '}');
|
|
|
|
|
}
|
|
int parser_conf(const char *conf_file, iotd_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;
|
gpio_t *gpio;
|
|
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/iotd.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;
|
}
|
|
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 |
|
*+------------------------------------------------------+*/
|
|
gpio = &hal_ctx->gpio;
|
|
/* parser GPIO output pins */
|
if( !(str=iniparser_getstring(ini, "hardware:gpio_outpin", NULL)) )
|
{
|
log_warn("parser no GPIO output pins\n");
|
}
|
else
|
{
|
parser_gpio_info(_TYPE_OUTPUT, gpio, str);
|
log_nrml("parser [%d] GPIO output pins configured\n", gpio->outcnt);
|
}
|
|
gpio->light_intval = iniparser_getint(ini, "hardware:light_intval", 20);
|
log_nrml("parser relay controled light interval time [%d]\n", gpio->light_intval);
|
|
/* parser GPIO input pins */
|
if( !(str=iniparser_getstring(ini, "hardware:gpio_inpin", NULL)) )
|
{
|
log_warn("parser no GPIO input pins\n");
|
}
|
else
|
{
|
parser_gpio_info(_TYPE_INPUT, gpio, str);
|
log_nrml("parser [%d] GPIO input pins\n", gpio->incnt);
|
}
|
|
hal_ctx->lux_enable = iniparser_getint(ini, "hardware:lux", 0);
|
if( hal_ctx->lux_enable )
|
{
|
hal_ctx->lux_threshold = iniparser_getdouble(ini, "hardware:lux_threshold", 0.1);
|
log_nrml("parser LUX enable and threshold value set be [%.03f]\n", hal_ctx->lux_threshold);
|
}
|
|
hal_ctx->sht2x_enable = iniparser_getint(ini, "hardware:sht2x", 0);
|
if( hal_ctx->sht2x_enable )
|
{
|
log_nrml("parser SHT2x sensor enabled\n");
|
}
|
|
hal_ctx->ds18b20_enable = iniparser_getint(ini, "hardware:sht2x", 0);
|
if( hal_ctx->ds18b20_enable )
|
{
|
log_nrml("parser DS18B20 sensor enabled\n");
|
}
|
|
|
/*+------------------------------------------------------+
|
*| 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);
|
|
/*+------------------------------------------------------+
|
*| parser publisher settings |
|
*+------------------------------------------------------+*/
|
|
if( !(str=iniparser_getstring(ini, "publisher:pubTopic", NULL)) )
|
{
|
log_err("ERROR: Parser MQTT publisher topic failure\n");
|
return -2;
|
}
|
strncpy(mqtt_ctx->pubTopic, str, sizeof(mqtt_ctx->pubTopic) );
|
|
mqtt_ctx->pubQos = iniparser_getint(ini, "publisher:pubQos", 0);
|
log_nrml("Parser publisher topic \"%s\" with Qos[%d]\n", mqtt_ctx->pubTopic, mqtt_ctx->pubQos);
|
|
|
return 0;
|
}
|