From 3ef6cf39749c00366b12aef9f023aac3bda16f2d Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Mon, 17 Nov 2025 23:39:25 +0800
Subject: [PATCH] update modules for tsl2561 and ds18b20
---
project/lightd/config.c | 306 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 306 insertions(+), 0 deletions(-)
diff --git a/project/lightd/config.c b/project/lightd/config.c
new file mode 100644
index 0000000..4f2b261
--- /dev/null
+++ b/project/lightd/config.c
@@ -0,0 +1,306 @@
+/*********************************************************************************
+ * Copyright: (C) 2019 LingYun IoT System Studio
+ * All rights reserved.
+ *
+ * Filename: config.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 "config.h"
+#include "logger.h"
+#include "iniparser.h"
+
+enum
+{
+ _TYPE_INPUT,
+ _TYPE_OUTPUT,
+};
+int parser_gpio_info(int type, gpio_t *gpio, char *conf);
+
+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 )
+ {
+ fprintf(stderr, "%s() Invalid input arguments\n", __func__);
+ return -1;
+ }
+
+ memset(ctx, 0, sizeof(*ctx));
+ logger = &ctx->logger;
+ hwinfo = &ctx->hwinfo;
+ mqtt = &ctx->mqtt;
+ mqtt->userdata = (void *)hwinfo;
+
+ 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/thingsboard.log");
+ 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(logger->logfile, "console", sizeof(logger->logfile));
+ logger->loglevel = LOG_LEVEL_DEBUG;
+ logger->logsize = 0;
+ }
+
+ if( log_open(logger->logfile, logger->loglevel, logger->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(mqtt->devid, sizeof(mqtt->devid), "\"%s\"", str);
+ log_info("Parser device ID [%s]\n", mqtt->devid);
+
+
+ /*+------------------------------------------------------+
+ *| parser hardware module configuration |
+ *+------------------------------------------------------+*/
+
+ /* DS18B20 temperature sensor status */
+ 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");
+
+ /* TSL2561 lux sensor status */
+ hwinfo->tsl2561=iniparser_getint(ini, "hardware:tsl2561", 0);
+ if( !hwinfo->tsl2561)
+ log_warn("Parser TSL2561 lux sensor module disabled\n");
+ else
+ log_info("Parser TSL2561 lux sensor module enabled\n");
+
+ /* TSL2561 lux sensor threshold value */
+ hwinfo->lux_threshold = iniparser_getdouble(ini, "hardware:lux_threshold", 0.1);
+ log_info("parser LUX enable and threshold value set be [%.03f]\n", hwinfo->lux_threshold);
+
+ /* parser GPIO output pins for relay */
+ if( !(str=iniparser_getstring(ini, "hardware:gpio_outpin", NULL)) )
+ {
+ log_warn("parser no GPIO output pins\n");
+ }
+ else
+ {
+ parser_gpio_info(_TYPE_OUTPUT, &hwinfo->gpio, (char *)str);
+ log_info("parser [%d] GPIO output pins configured\n", hwinfo->gpio.outcnt);
+ }
+
+ hwinfo->light_intval = iniparser_getint(ini, "hardware:light_intval", 20);
+ log_info("parser relay controled light interval time [%d]\n", hwinfo->light_intval);
+
+ /* parser GPIO input pins for infrared */
+ if( !(str=iniparser_getstring(ini, "hardware:gpio_inpin", NULL)) )
+ {
+ log_warn("parser no GPIO input pins\n");
+ }
+ else
+ {
+ parser_gpio_info(_TYPE_INPUT, &hwinfo->gpio, (char *)str);
+ log_info("parser [%d] GPIO input pins\n", hwinfo->gpio.incnt);
+ }
+
+ /*+------------------------------------------------------+
+ *| 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(mqtt->host, str, sizeof(mqtt->host) );
+
+ if( (val=iniparser_getint(ini, "broker:port", -1)) < 0 )
+ {
+ log_error("ERROR: Parser MQTT broker server port failure\n");
+ rv = -5;
+ goto cleanup;
+ }
+ mqtt->port = val;
+ log_info("Parser MQTT broker server [%s:%d]\n", mqtt->host, mqtt->port);
+
+ str=iniparser_getstring(ini, "broker:token", NULL);
+ strncpy(mqtt->token, str, sizeof(mqtt->uid) );
+
+ str=iniparser_getstring(ini, "broker:username", NULL);
+ strncpy(mqtt->uid, str, sizeof(mqtt->uid) );
+
+ str=iniparser_getstring(ini, "broker:password", NULL);
+ strncpy(mqtt->pwd, str, sizeof(mqtt->pwd) );
+
+ if( mqtt->uid && mqtt->pwd )
+ log_info("Parser broker author by [%s:%s]\n", mqtt->uid, mqtt->pwd);
+
+ mqtt->keepalive = iniparser_getint(ini, "broker:keepalive", DEF_KEEPALIVE);
+ log_info("Parser broker keepalive timeout [%d] seconds\n", mqtt->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(mqtt->pubTopic, str, sizeof(mqtt->pubTopic) );
+
+ 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 |
+ *+------------------------------------------------------+*/
+
+ if( !(str=iniparser_getstring(ini, "subsciber:subTopic", NULL)) )
+ {
+ log_error("ERROR: Parser MQTT broker publisher topic failure\n");
+ rv = -7;
+ goto cleanup;
+ }
+ strncpy(mqtt->subTopic, str, sizeof(mqtt->subTopic) );
+
+ 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 )
+ iniparser_freedict(ini);
+
+ if( rv )
+ log_close();
+
+ return rv;
+}
+
+/* conf format: "{light_indoor:6:0},{light_hallway:26:0}" */
+int parser_gpio_info(int type, gpio_t *gpio, char *conf)
+{
+ char *ptr;
+ char *pstart;
+ char *pend;
+ char buf[64];
+ int cnt = 0;
+ int rv = 0;
+
+ if( !gpio || !conf || strlen(conf)<3 )
+ {
+ log_error("Invalid input arguments.\n");
+ return -1;
+ }
+
+ pstart = strchr(conf, '{');
+ if( !pstart )
+ return 0;
+
+ pend = strchr(pstart, '}');
+
+ while( pstart && pend )
+ {
+ memset(buf, 0, sizeof(buf));
+
+ strncpy(buf, pstart+1, pend-pstart-1);
+
+ /* parser and get the GPIO name, BCM pin number, active power level */
+
+ {
+ /* check GPIO configure name too long or not */
+ ptr = strchr(buf, ':');
+ if( !ptr )
+ {
+ log_error("Found invalid GPIO configure: %s\n", buf);
+ goto NEXT_LOOP;
+ }
+
+ if( ptr-buf > sizeof(gpio->input[cnt].name) )
+ {
+ log_error("Found GPIO name too long\n", buf);
+ goto NEXT_LOOP;
+ }
+
+ /* use sscanf() to parser GPIO configured values */
+ if(_TYPE_INPUT == type )
+ {
+ rv = sscanf(buf, "%[^:]:%d:%d", gpio->input[cnt].name, &gpio->input[cnt].pin, &gpio->input[cnt].active_level);
+ if( 3 == rv)
+ {
+ log_info("parser GPIO input[%s] BCM[%d] active[%d]\n", gpio->input[cnt].name, gpio->input[cnt].pin, gpio->input[cnt].active_level);
+ if( strstr(gpio->input[cnt].name, "infrared") )
+ {
+ log_info("parser GPIO enable infrared detect\n");
+ }
+ cnt++;
+ gpio->incnt = cnt;
+ }
+ else
+ {
+ log_error("Found invalid GPIO configure: %s\n", buf);
+ }
+ }
+ else
+ {
+ rv = sscanf(buf, "%[^:]:%d:%d", gpio->output[cnt].name, &gpio->output[cnt].pin, &gpio->output[cnt].active_level);
+ if( 3 == rv)
+ {
+ log_info("parser GPIO output[%s] BCM[%d] active[%d]\n", gpio->output[cnt].name, gpio->output[cnt].pin, gpio->output[cnt].active_level);
+ cnt++;
+ gpio->outcnt = cnt;
+ }
+ else
+ {
+ log_error("Found invalid GPIO configure: %s\n", buf);
+ }
+ }
+ }
+
+NEXT_LOOP:
+ pstart = strchr(pend, '{');
+ if( pstart )
+ pend = strchr(pstart, '}');
+ }
+
+
+ return gpio->outcnt;
+}
--
Gitblit v1.9.1