From af30c84d820576c37e57de31bc47d18b6e4b89e1 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Wed, 21 Apr 2021 16:52:34 +0800
Subject: [PATCH] update iotd, test MQTT subscriber ok

---
 iotd/hal/gpio.c |   98 +++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 90 insertions(+), 8 deletions(-)

diff --git a/iotd/hal/gpio.c b/iotd/hal/gpio.c
index e50ae3b..a5532a5 100644
--- a/iotd/hal/gpio.c
+++ b/iotd/hal/gpio.c
@@ -21,21 +21,103 @@
 
 int gpio_init(gpio_t *gpio)
 {
+    int                 i;
+    gpio_info_t        *gpioinfo;
+
+
+    s_gpio = gpio;
+
+
     if( !gpio )
     {
         log_err("Invalid input arguments $gpio\n");
         return -1;
     }
 
-    s_gpio = gpio;
+
+    if( !gpio->incnt && !gpio->outcnt )
+    {
+        log_warn("WARNNING: No GPIO pins configured\n");
+        return 0;
+    }
+
+
+    /*  gpiod open chip */
+    s_chip = gpiod_chip_open_by_name(RPI_GPIONAME);
+    if( !s_chip )
+    {
+        log_err("gpiod open chip failure, maybe you need running as root\n");
+        return -2;
+    }
+    log_nrml("gpiod initialise open chip ok\n");
+
+
+    /*  gpiod request all output pins */
+    for(i=0; i<gpio->outcnt; i++)
+    {
+        gpio->output[i].line = gpiod_chip_get_line(s_chip, gpio->output[i].pin);
+        if( !gpio->output[i].line )
+        {
+            log_err("gpiod get line for '%s' pin[#%d] failure\n", gpio->output[i].name, gpio->output[i].pin );
+            return -2;
+        }
+
+        gpiod_line_request_output(gpio->output[i].line, gpio->output[i].name, !gpio->output[i].active_level);
+        log_nrml("gpiod request '%s' pin[#%d] output ok\n", gpio->output[i].name, gpio->output[i].pin);
+    }
+
+
+    /*  gpiod request all input pins */
+    for(i=0; i<gpio->incnt; i++)
+    {
+        gpio->input[i].line = gpiod_chip_get_line(s_chip, gpio->input[i].pin);
+        if( !gpio->input[i].line )
+        {
+            log_err("gpiod get line for '%s' pin[#%d] failure\n", gpio->input[i].name, gpio->input[i].pin );
+            return -2;
+        }
+
+        if( gpio->output[i].active_level )
+        {
+            gpiod_line_request_rising_edge_events(gpio->input[i].line, gpio->input[i].name);
+            log_nrml("gpiod request '%s' pin[#%d] for rising edge event ok\n", gpio->input[i].name, gpio->input[i].pin);
+        }
+        else
+        {
+            gpiod_line_request_falling_edge_events(gpio->input[i].line, gpio->input[i].name);
+            log_nrml("gpiod request '%s' pin[#%d] for falling edge event ok\n", gpio->input[i].name, gpio->input[i].pin);
+        }
+    }
 }
+
 
 void gpio_term(void)
 {
-    return ;
+    int              i;
+
+    log_nrml("start teriminated GPIO\n");
+
+    if( !s_gpio->incnt && !s_gpio->outcnt )
+    {
+        return ;
+    }
+
+
+    for(i=0; i<s_gpio->outcnt; i++)
+    {
+        gpiod_line_release(s_gpio->output[i].line);
+    }
+
+
+    for(i=0; i<s_gpio->incnt; i++)
+    {
+        gpiod_line_release(s_gpio->input[i].line);
+    }
+
+    gpiod_chip_close(s_chip);
 }
 
-void gpio_out(char *name, int cmd)
+void gpio_out(char *name, char *cmd)
 {
     int              i;
     int              found = 0;
@@ -51,17 +133,17 @@
 
     if( !found )
     {
-        log_err("GPIO output for [%s] pin not found\n", name);
+        log_err("GPIO output for '%s' pin not found\n", name);
         return ;
     }
 
-    if( OFF == cmd )
+    if( strstr(cmd, "on") )
     {
-        gpiod_line_set_value(s_gpio->output[i].lines, s_gpio->output[i].active_level);
+        gpiod_line_set_value(s_gpio->output[i].line, s_gpio->output[i].active_level);
     }
-    else
+    else if( strstr(cmd, "off") )
     {
-        gpiod_line_set_value(s_gpio->output[i].lines, !s_gpio->output[i].active_level);
+        gpiod_line_set_value(s_gpio->output[i].line, !s_gpio->output[i].active_level);
     }
 
     return ;

--
Gitblit v1.9.1