| | |
| | | #include "logger.h" |
| | | #include "iniparser.h" |
| | | |
| | | int parser_conf(const char *conf_file, mqtt_ctx_t *ctx, int debug) |
| | | int parser_conf(const char *conf_file, iotd_ctx_t *ctx, int debug) |
| | | { |
| | | dictionary *ini; |
| | | const char *str; |
| | | int val; |
| | | int rv = 0; |
| | | logger_t *logger; |
| | | hwinfo_t *hwinfo; |
| | | mqtt_ctx_t *mqtt; |
| | | |
| | | if( !conf_file || !ctx ) |
| | | { |
| | |
| | | } |
| | | |
| | | memset(ctx, 0, sizeof(*ctx)); |
| | | logger = &ctx->logger; |
| | | hwinfo = &ctx->hwinfo; |
| | | mqtt = &ctx->mqtt; |
| | | |
| | | ini = iniparser_load(conf_file); |
| | | if( !ini ) |
| | |
| | | if( !debug ) |
| | | { |
| | | str = iniparser_getstring(ini, "logger:file", "/tmp/thingsboard.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); |
| | | strncpy(logger->logfile, str, sizeof(logger->logfile)); |
| | | logger->logsize = iniparser_getint(ini, "logger:size", 1024); |
| | | logger->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; |
| | | strncpy(logger->logfile, "console", sizeof(logger->logfile)); |
| | | logger->loglevel = LOG_LEVEL_DEBUG; |
| | | logger->logsize = 0; |
| | | } |
| | | |
| | | if( log_open(ctx->logfile, ctx->loglevel, ctx->logsize, LOG_LOCK_DISABLE) < 0 ) |
| | | if( log_open(logger->logfile, logger->loglevel, logger->logsize, LOG_LOCK_DISABLE) < 0 ) |
| | | { |
| | | fprintf(stderr, "Logger system initialise failure\n"); |
| | | return -2; |
| | |
| | | goto cleanup; |
| | | } |
| | | /* cJSON parser ID will get "" */ |
| | | snprintf(ctx->devid, sizeof(ctx->devid), "\"%s\"", str); |
| | | log_info("Parser device ID [%s]\n", ctx->devid); |
| | | snprintf(mqtt->devid, sizeof(mqtt->devid), "\"%s\"", str); |
| | | log_info("Parser device ID [%s]\n", mqtt->devid); |
| | | |
| | | |
| | | /*+------------------------------------------------------+ |
| | |
| | | *+------------------------------------------------------+*/ |
| | | |
| | | /* relay */ |
| | | ctx->hwconf.relay=iniparser_getint(ini, "hardware:relay", 0); |
| | | if( !ctx->hwconf.relay ) |
| | | hwinfo->relay=iniparser_getint(ini, "hardware:relay", 0); |
| | | if( !hwinfo->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 ) |
| | | hwinfo->led=iniparser_getint(ini, "hardware:rgbled", 0); |
| | | if( !hwinfo->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 ) |
| | | hwinfo->beeper=iniparser_getint(ini, "hardware:beep", 0); |
| | | if( !hwinfo->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 ) |
| | | hwinfo->ds18b20=iniparser_getint(ini, "hardware:ds18b20", 0); |
| | | if( !hwinfo->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 ) |
| | | hwinfo->sht2x=iniparser_getint(ini, "hardware:sht2x", 0); |
| | | if( !hwinfo->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 ) |
| | | hwinfo->tsl2561=iniparser_getint(ini, "hardware:tsl2561", 0); |
| | | if( !hwinfo->tsl2561 ) |
| | | log_warn("Parser TSL2561 light intensity sensor module disabled\n"); |
| | | else |
| | | log_info("Parser TSL2561 light intensity sensor module enabled\n"); |
| | |
| | | rv = -4; |
| | | goto cleanup; |
| | | } |
| | | strncpy(ctx->host, str, sizeof(ctx->host) ); |
| | | strncpy(mqtt->host, str, sizeof(mqtt->host) ); |
| | | |
| | | if( (val=iniparser_getint(ini, "broker:port", -1)) < 0 ) |
| | | { |
| | |
| | | rv = -5; |
| | | goto cleanup; |
| | | } |
| | | ctx->port = val; |
| | | log_info("Parser MQTT broker server [%s:%d]\n", ctx->host, ctx->port); |
| | | mqtt->port = val; |
| | | log_info("Parser MQTT broker server [%s:%d]\n", mqtt->host, mqtt->port); |
| | | |
| | | str=iniparser_getstring(ini, "broker:token", NULL); |
| | | strncpy(ctx->token, str, sizeof(ctx->uid) ); |
| | | strncpy(mqtt->token, str, sizeof(mqtt->uid) ); |
| | | log_info("Parser broker token [%s]\n", mqtt->token); |
| | | |
| | | str=iniparser_getstring(ini, "broker:username", NULL); |
| | | strncpy(ctx->uid, str, sizeof(ctx->uid) ); |
| | | strncpy(mqtt->uid, str, sizeof(mqtt->uid) ); |
| | | |
| | | str=iniparser_getstring(ini, "broker:password", NULL); |
| | | strncpy(ctx->pwd, str, sizeof(ctx->pwd) ); |
| | | strncpy(mqtt->pwd, str, sizeof(mqtt->pwd) ); |
| | | |
| | | if( ctx->uid && ctx->pwd ) |
| | | log_info("Parser broker author by [%s:%s]\n", ctx->uid, ctx->pwd); |
| | | if( mqtt->uid && mqtt->pwd ) |
| | | log_info("Parser broker account [%s:%s]\n", mqtt->uid, mqtt->pwd); |
| | | |
| | | ctx->keepalive = iniparser_getint(ini, "broker:keepalive", DEF_KEEPALIVE); |
| | | log_info("Parser broker keepalive timeout [%d] seconds\n", ctx->keepalive); |
| | | mqtt->keepalive = iniparser_getint(ini, "broker:keepalive", DEF_KEEPALIVE); |
| | | log_info("Parser broker keepalive timeout [%d] seconds\n", mqtt->keepalive); |
| | | |
| | | /*+------------------------------------------------------+ |
| | | *| parser publisher settings | |
| | |
| | | rv = -6; |
| | | goto cleanup; |
| | | } |
| | | strncpy(ctx->pubTopic, str, sizeof(ctx->pubTopic) ); |
| | | strncpy(mqtt->pubTopic, str, sizeof(mqtt->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); |
| | | mqtt->pubQos = iniparser_getint(ini, "publisher:pubQos", DEF_QOS); |
| | | mqtt->interval = iniparser_getint(ini, "publisher:interval", DEF_PUBINTERVAL); |
| | | log_info("Parser publisher topic \"%s\" with Qos[%d] interval[%d]\n", mqtt->pubTopic, mqtt->pubQos, mqtt->interval); |
| | | |
| | | /*+------------------------------------------------------+ |
| | | *| parser subscriber settings | |
| | |
| | | rv = -7; |
| | | goto cleanup; |
| | | } |
| | | strncpy(ctx->subTopic, str, sizeof(ctx->subTopic) ); |
| | | strncpy(mqtt->subTopic, str, sizeof(mqtt->subTopic) ); |
| | | |
| | | ctx->subQos = iniparser_getint(ini, "subsciber:subQos", DEF_QOS); |
| | | log_info("Parser subscriber topic \"%s\" with Qos[%d]\n", ctx->subTopic, ctx->subQos); |
| | | mqtt->subQos = iniparser_getint(ini, "subsciber:subQos", DEF_QOS); |
| | | log_info("Parser subscriber topic \"%s\" with Qos[%d]\n", mqtt->subTopic, mqtt->subQos); |
| | | |
| | | cleanup: |
| | | if( ini ) |