From 45e62dbd9e964c74b24f7fcadad9b418f117e008 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Thu, 27 Jun 2019 15:11:42 +0800
Subject: [PATCH] update mqttd, add hardware configured in configure files

---
 mqttd/etc/conf.h     |   23 ++++
 mqttd/hal/hal.c      |   83 +++++++++++++++-
 /dev/null            |   31 ------
 mqttd/lylib/proc.c   |    4 
 mqttd/etc/conf.c     |   47 +++++++++
 mqttd/hal/hal.h      |   17 ++
 mqttd/etc/mqttd.conf |   27 +++++
 mqttd/main.c         |   59 ++++++-----
 8 files changed, 223 insertions(+), 68 deletions(-)

diff --git a/mqttd/etc/conf.c b/mqttd/etc/conf.c
index a32683d..f998ce4 100644
--- a/mqttd/etc/conf.c
+++ b/mqttd/etc/conf.c
@@ -101,6 +101,53 @@
 
 
     /*+------------------------------------------------------+
+     *|      parser hardware module configuration ID         |
+     *+------------------------------------------------------+*/
+
+    /* hardware module enable or not */
+    ctx->hwconf.enable=iniparser_getint(ini, "hardware:enable", 0);
+    log_nrml("Parser hardware modules [%s]\n", ctx->hwconf.enable ? "enable" : "disable");
+
+    /* relay control light  */
+    ctx->hwconf.relay=iniparser_getint(ini, "hardware:relay_pin", 0);
+    if( !ctx->hwconf.relay )
+        log_nrml("Parser relay module disabled\n");
+    else
+        log_nrml("Parser relay connected wPI #pin nubmer [%d]\n", ctx->hwconf.relay); 
+
+    /* buzzer */
+    ctx->hwconf.beep=iniparser_getint(ini, "hardware:beep_pin", 0);
+
+    /* RGB 3-colors LED  */
+    ctx->hwconf.redled=iniparser_getint(ini, "hardware:red_pin", 0);
+    ctx->hwconf.greenled=iniparser_getint(ini, "hardware:green_pin", 0);
+    ctx->hwconf.blueled=iniparser_getint(ini, "hardware:blue_pin", 0);
+    if( !ctx->hwconf.redled && !ctx->hwconf.greenled && !ctx->hwconf.blueled )
+    {
+        ctx->hwconf.leds = 0;
+        log_nrml("Parser RGB leds module disabled\n");
+    }
+    else
+    {
+        ctx->hwconf.leds = 1;
+        log_nrml("Parser RGB led connected wPI #pin nubmer [%d,%d,%d]\n", 
+            ctx->hwconf.redled, ctx->hwconf.greenled, ctx->hwconf.blueled);
+    }
+
+    /* temperature sensor ds18b20  */
+    ctx->hwconf.ds18b20=iniparser_getint(ini, "hardware:ds18b20", 0);
+    log_nrml("Parser temperature sensor DS18B20 modules [%s]\n", ctx->hwconf.ds18b20 ? "enable" : "disable");
+
+    /* temperature and hummidity sensor sht2x  */
+    ctx->hwconf.sht2x=iniparser_getint(ini, "hardware:sht2x", 0);
+    log_nrml("Parser temperature and hummidity sensor sht2x modules [%s]\n", ctx->hwconf.sht2x ? "enable" : "disable");
+
+    /* light intensity sensor TSL2561 */
+    ctx->hwconf.lux=iniparser_getint(ini, "hardware:lux", 0);
+    log_nrml("Parser light intensity sensor TSL2561 modules [%s]\n", ctx->hwconf.ds18b20 ? "enable" : "disable");
+
+
+    /*+------------------------------------------------------+
      *|               parser production ID                   |
      *+------------------------------------------------------+*/
 
diff --git a/mqttd/etc/conf.h b/mqttd/etc/conf.h
index 1c6216c..49abfbc 100644
--- a/mqttd/etc/conf.h
+++ b/mqttd/etc/conf.h
@@ -35,10 +35,33 @@
     Qos2, /* Qos2使用两阶段确认来保证消息的不丢失和不重复。在Qos2情况下,Broker肯定会收到消息,且只收到一次  */
 };
 
+
+typedef struct hwconf_s 
+{
+    unsigned char  enable;      /* Hardware enable or not, help to running on X86 */
+
+    int            relay;       /* relay connected wPI #pin number, if set to 0 means disable */
+    int            beep;        /* beep connected wPI #pin number, if set to 0 means disable */
+
+    unsigned char  leds;
+    int            redled;     /*  redled connect wPI #pin number, if set to 0 means disable */
+    int            greenled;   /*greenled connect wPI #pin number, if set to 0 means disable */
+    int            blueled;    /* blueled connect wPI #pin number, if set to 0 means disable */
+
+    unsigned char  ds18b20;    /* enable or disable temperature sensor ds18b20 */
+    unsigned char  sht2x;      /* enable or disable temperature and hummidity sensor sht2x */
+    unsigned char  lux;        /* enable or disable light intensity sensor TSL2561 */
+} hwconf_t;  
+
+
+
 typedef struct mqtt_ctx_s
 {
     char          id[32];       /*  production ID */
 
+    /* hardware configuration */
+    hwconf_t      hwconf;
+
     /* logger settings */
     char          logfile[128]; /* logger record file */
     int           loglevel;     /* logger level  */
diff --git a/mqttd/etc/mqttd.conf b/mqttd/etc/mqttd.conf
index 3747c4f..773f954 100644
--- a/mqttd/etc/mqttd.conf
+++ b/mqttd/etc/mqttd.conf
@@ -1,6 +1,33 @@
 [common]
 id="rpi3b001"
 
+[hardware]
+# Settings 0:sisable  others: enable or connected #PIN nubmer(wPI)
+
+# Enablue hardware support or not, help to running on X86
+enable=1
+
+# relay connected wPI #pin number, if set to 0 means disable
+relay_pin=29
+
+# beep connected wPI #pin number, if set to 0 means disable
+beep_pin=0
+
+# RGB 3-colors LED connect wPI #pin number, if all set to 0 means disable
+red_pin=0
+green_pin=0
+blue_pin=0
+
+# temperature sensor ds18b20
+ds18b20=1
+
+# temperature and hummidity sensor sht2x
+sht2x=0
+
+# light intensity sensor TSL2561
+lux=0
+
+
 [logger]
 
 # 日志记录文件
diff --git a/mqttd/hal/hal.c b/mqttd/hal/hal.c
index 44f4638..94dd8dc 100644
--- a/mqttd/hal/hal.c
+++ b/mqttd/hal/hal.c
@@ -13,21 +13,90 @@
 
 #include "hal.h"
 
-int hal_init(void)
+                              /* LED_R  LED_G   LED_B */
+static int led_pin[LED_MAX] = { 0,0,0 };
+static int relay_pin = 0;
+
+void init_relay(int pin);
+void init_led(int redpin, int greenpin, int bluepin);
+
+int hal_init(hwconf_t *hwconf)
 {
+    if( !hwconf->enable )
+    {
+        log_err("All hardware modules are disabled, skip initialise HAL.\n");
+        return 0;
+    }
+
     wiringPiSetup();
 
-    init_led(); 
-    init_relay();
+    if( hwconf->leds )
+        init_led(hwconf->redled, hwconf->greenled, hwconf->blueled); 
 
-#if 0
-    if( sht2x_init() < 0 )
+    if(hwconf->relay)
+        init_relay(hwconf->relay);
+
+    if( hwconf->sht2x && sht2x_init() < 0 )
     {
         log_err("Initialise SHT20 failure\n");
-        return -2;
+        return -1;
     } 
-#endif
     
     return 0;
 }
 
+void init_led(int redpin, int greenpin, int bluepin)
+{
+	int         i; 
+
+    led_pin[LED_R]=redpin; 
+    led_pin[LED_G]=greenpin; 
+    led_pin[LED_B]=bluepin;
+    
+	for(i=0; i<LED_MAX; i++)
+	{
+        if(led_pin[i])
+            pinMode(led_pin[i], OUTPUT );
+	}
+}
+
+int turn_led(int which, int cmd)
+{
+	if( which<0 || which>=LED_MAX )
+		return -1;
+
+	if( OFF == cmd )
+    {
+        if(led_pin[which])
+            digitalWrite (led_pin[which], LOW); 
+    }
+    else 
+    {
+        if(led_pin[which])
+            digitalWrite (led_pin[which], HIGH);
+    }
+
+	return 0;
+}
+
+
+void init_relay(int pin)
+{
+    relay_pin = pin;
+    pinMode(relay_pin, OUTPUT);
+}
+
+
+void turn_relay(int cmd)
+{
+    if( OFF == cmd )
+    {
+        digitalWrite ( relay_pin, LOW );
+    }
+    else
+    {
+        digitalWrite ( relay_pin, HIGH );
+    }
+}
+
+
diff --git a/mqttd/hal/hal.h b/mqttd/hal/hal.h
index 7855cdd..5788ff8 100644
--- a/mqttd/hal/hal.h
+++ b/mqttd/hal/hal.h
@@ -17,12 +17,23 @@
 #include <wiringPi.h>
 
 #include "lylib/logger.h"
+#include "etc/conf.h"
 
-#include "led.h"
-#include "relay.h"
 #include "ds18b20.h"
 #include "sht20.h"
 
-int hal_init(void);
+#define OFF   0
+#define ON    1
+
+/* Three LEDs code */
+enum
+{
+	LED_R = 0,
+	LED_G,
+	LED_B,
+	LED_MAX,
+};
+
+int hal_init(hwconf_t *conf);
 
 #endif   /* ----- #ifndef _HAL_H_  ----- */
diff --git a/mqttd/hal/led.c b/mqttd/hal/led.c
deleted file mode 100644
index 0025089..0000000
--- a/mqttd/hal/led.c
+++ /dev/null
@@ -1,57 +0,0 @@
-/*********************************************************************************
- *      Copyright:  (C) 2018 LingYun IoT System Studio
- *                  All rights reserved.
- *
- *       Filename:  led.c
- *    Description:  This file is used to control RGB 3-colours LED
- *
- *     pi@raspberrypi:~ $ gpio readall show BCM and wPi pinmap  
- *
- *                 LED      BCM           wPi           Phy Pin
- *                  G ---- GPIO.13  ---- GPIO.23  ----   #Pin33
- *                  R ---- GPIO.19  ---- GPIO.24  ----   #Pin35  
- *                  B ---- GPIO.26  ---- GPIO.25  ----   #Pin37
- *                  I ---- GND      ----
- *                 
- *        Version:  1.0.0(2018/10/14)
- *         Author:  Guo Wenxue <guowenxue@gmail.com>
- *      ChangeLog:  1, Release initial version on "2018/10/14 12:13:26"
- *                 
- ********************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <dirent.h>
-#include <string.h>
-#include <time.h>
-#include <errno.h>
-
-#include <wiringPi.h>
-#include "led.h"
-
-void init_led(void)
-{
-	int         i; 
-    
-	for(i=0; i<LED_MAX; i++)
-	{
-        pinMode( led_gpio[i], OUTPUT );
-	}
-}
-
-int turn_led(int which, int cmd)
-{
-	if( which<0 || which>=LED_MAX )
-		return -1;
-
-	if( OFF == cmd )
-        digitalWrite (led_gpio[which], LOW); 
-    else 
-        digitalWrite (led_gpio[which], HIGH);
-
-	return 0;
-}
-
-
diff --git a/mqttd/hal/led.h b/mqttd/hal/led.h
deleted file mode 100644
index da6cbcc..0000000
--- a/mqttd/hal/led.h
+++ /dev/null
@@ -1,48 +0,0 @@
-/*********************************************************************************
- *      Copyright:  (C) 2018 LingYun IoT System Studio
- *                  All rights reserved.
- *
- *       Filename:  led.h
- *    Description:  This file is used to control RGB 3-colours LED
- *
- *     pi@raspberrypi:~ $ gpio readall show BCM and wPi pinmap  
- *
- *                 LED      BCM           wPi           #PIN
- *                  G ---- GPIO.13  ---- GPIO.23  ----   33
- *                  R ---- GPIO.19  ---- GPIO.24  ----   35  
- *                  B ---- GPIO.26  ---- GPIO.25  ----   37
- *                  I ---- GND      ----
- *                 
- *        Version:  1.0.0(2018/10/14)
- *         Author:  Guo Wenxue <guowenxue@gmail.com>
- *      ChangeLog:  1, Release initial version on "2018/10/14 12:13:26"
- *                 
- ********************************************************************************/
-
-#ifndef __LED_H
-#define __LED_H
-
-#define OFF   0
-#define ON    1
-
-/* Three LEDs code */
-enum
-{
-	LED_R = 0,
-	LED_G,
-	LED_B,
-	LED_MAX,
-};
-
-/* 3 LEDs WiringPi GPIO port */
-                               
-                              /* LED_R  LED_G   LED_B */
-static int led_gpio[LED_MAX]= {   24,    23,     25  };
-
-
-void init_led(void);
-int turn_led(int which, int cmd);
-
-
-#endif
-
diff --git a/mqttd/hal/relay.c b/mqttd/hal/relay.c
deleted file mode 100644
index a35eca0..0000000
--- a/mqttd/hal/relay.c
+++ /dev/null
@@ -1,36 +0,0 @@
-/*********************************************************************************
- *      Copyright:  (C) 2019 LingYun IoT System Studio
- *                  All rights reserved.
- *
- *       Filename:  relay.c
- *    Description:  This file 
- *                 
- *        Version:  1.0.0(30/01/19)
- *         Author:  Guo Wenxue <guowenxue@gmail.com>
- *      ChangeLog:  1, Release initial version on "30/01/19 08:44:30"
- *                 
- ********************************************************************************/
-
-
-#include <stdio.h>
-#include <wiringPi.h>
-#include "relay.h"
-
-void init_relay(void)
-{
-    pinMode(RELAY_PIN, OUTPUT);
-}
-
-
-void turn_relay(int cmd)
-{
-    if( OFF == cmd )
-    {
-        digitalWrite ( RELAY_PIN, LOW );
-    }
-    else
-    {
-        digitalWrite ( RELAY_PIN, HIGH );
-    }
-}
-
diff --git a/mqttd/hal/relay.h b/mqttd/hal/relay.h
deleted file mode 100644
index dd4754d..0000000
--- a/mqttd/hal/relay.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/********************************************************************************
- *      Copyright:  (C) 2019 LingYun IoT System Studio
- *                  All rights reserved.
- *
- *       Filename:  relay.h
- *    Description:  This head file 
- *
- *        Version:  1.0.0(30/01/19)
- *         Author:  Guo Wenxue <guowenxue@gmail.com>
- *      ChangeLog:  1, Release initial version on "30/01/19 08:49:31"
- *                 
- ********************************************************************************/
-
-#ifndef  _RELAY_H_
-#define  _RELAY_H_
-
-#ifndef ON
-#define ON                  1
-#define OFF                 0
-#endif
-
-
-/* I/O Pin connected to PIN#40, BCM code pin number is 21 and wPi pin number is 29, 
- * can check by linux command <gpio readall> on RPi */
-#define RELAY_PIN           29
-
-void init_relay(void);
-void turn_relay(int cmd);
-
-#endif   /* ----- #ifndef _RELAY_H_  ----- */
-
diff --git a/mqttd/lylib/proc.c b/mqttd/lylib/proc.c
index 62e9b5a..42db67b 100644
--- a/mqttd/lylib/proc.c
+++ b/mqttd/lylib/proc.c
@@ -234,7 +234,7 @@
     if (0 == retVal) 
     { 
         pid_t pid = -1; 
-        printf("PID record file \"%s\" exist.\n", pid_file);
+        //printf("PID record file \"%s\" exist.\n", pid_file);
 
         pid = get_daemon_pid(pid_file);
         if (pid > 0)  /*  Process pid exist */
@@ -246,7 +246,7 @@
             } 
             else   /* Send signal to the old process get no reply. */ 
             { 
-                printf("Program with PID[%d] seems exit.\n", pid); 
+                printf("Program with PID[%d] already exit.\n", pid); 
                 remove(pid_file); 
                 return 0; 
             } 
diff --git a/mqttd/main.c b/mqttd/main.c
index aa88835..90c6803 100644
--- a/mqttd/main.c
+++ b/mqttd/main.c
@@ -109,19 +109,23 @@
         return -2;
     }
 
-    if( hal_init() < 0 )
+    if( ctx.hwconf.enable  )
     {
-        log_err("Initialise hardware failure\n");
+        if( hal_init(&ctx.hwconf) < 0 )
+            log_err("Initialise hardware failure\n");
+        else 
+            log_nrml("HAL initialise ok\n");
+
         return -1;
     }
-    log_nrml("HAL initialise ok\n");
+    else
+    {
+    }
 
     install_proc_signal();
 
     if( check_set_program_running(daemon) < 0 ) 
         goto OUT;
-
-    if( !debug )
 
     mosquitto_lib_init();
 
@@ -200,22 +204,26 @@
     
     log_nrml("Publisher connect to broker server[%s:%d] successfully\n", ctx->host, ctx->port); 
 
-    memset(msg, 0, sizeof(msg));
-    if( ds18b20_get_temperature(&temp) < 0 )
-        snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"error\" }", ctx->id);
-    else 
-        snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"%.2f\" }", ctx->id, temp);
-
-    rv = mosquitto_publish(mosq, NULL, ctx->pubTopic, strlen(msg)+1, msg, ctx->pubQos, retain);
-    if( rv )
+    if( ctx->hwconf.ds18b20 )
     {
-        log_err("Publisher broadcast message '%s' failure: %d\n", msg, rv);
-    }
-    else 
-    {
-        log_nrml("Publisher broadcast message '%s' ok\n", msg);
-    }
+        memset(msg, 0, sizeof(msg));
+        
+        if( ds18b20_get_temperature(&temp) < 0 )
+            snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"error\" }", ctx->id);
+        else 
+            snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"%.2f\" }", ctx->id, temp);
 
+        rv = mosquitto_publish(mosq, NULL, ctx->pubTopic, strlen(msg)+1, msg, ctx->pubQos, retain);
+        if( rv )
+        {
+            log_err("Publisher broadcast message '%s' failure: %d\n", msg, rv);
+        }
+        else 
+        {
+            log_nrml("Publisher broadcast message '%s' ok\n", msg);
+        }
+    }
+    
     log_nrml("Publisher broadcast over and disconnect broker now\n", ctx->host, ctx->port); 
     mosquitto_disconnect(mosq);
 
@@ -294,12 +302,13 @@
         turn_led(which, OFF);
 }
 
-void proc_json_items(cJSON *root)
+void proc_json_items(cJSON *root, mqtt_ctx_t *ctx)
 {
     int                    i;
     char                  *value;
     cJSON                 *item; 
     cJSON                 *array;
+    hwconf_t              *hwconf = &ctx->hwconf;
 
     if( !root )
     {
@@ -316,12 +325,12 @@
         /* if item is cJSON_Object, then recursive call proc_json */
         if( cJSON_Object == item->type )
         {
-            proc_json_items(item);
+            proc_json_items(item, ctx);
         }
         else if( cJSON_Array == item->type )
         {
             /* RGB colors led control  */
-            if( !strcasecmp(item->string, "led") )
+            if( hwconf->leds && !strcasecmp(item->string, "led") )
             {
                 array = cJSON_GetArrayItem(item, 0);
                 if( NULL != array )
@@ -353,7 +362,7 @@
             value = cJSON_Print(item);
 
             /* light controled by relay */
-            if( !strcasecmp(item->string, "light") )
+            if( hwconf->relay && !strcasecmp(item->string, "light") )
             {
                 if( strcasestr(value, "on") )
                 {
@@ -368,7 +377,7 @@
             }
 
             /* buzzer */
-            if( !strcasecmp(item->string, "buzzer") )
+            if( hwconf->beep && !strcasecmp(item->string, "buzzer") )
             {
                 if( strcasestr(value, "on") )
                 {
@@ -427,7 +436,7 @@
     free(value); 
     log_nrml("Subscriber receive message: '%s'\n", message->payload);
 
-    proc_json_items(root); 
+    proc_json_items(root, ctx); 
 
 OUT:
     cJSON_Delete(root); /* must delete it, or it will result memory leak */

--
Gitblit v1.9.1