From 68826376ee5f47783c644c6604f4411ec747cd7e Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Fri, 14 Nov 2025 23:52:16 +0800
Subject: [PATCH] Add UDP DNS client source code

---
 project/4.mqttd/booster/leds.c |  162 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 162 insertions(+), 0 deletions(-)

diff --git a/project/4.mqttd/booster/leds.c b/project/4.mqttd/booster/leds.c
new file mode 100644
index 0000000..2e46182
--- /dev/null
+++ b/project/4.mqttd/booster/leds.c
@@ -0,0 +1,162 @@
+/*********************************************************************************
+ *      Copyright:  (C) 2021 LingYun IoT System Studio
+ *                  All rights reserved.
+ *
+ *       Filename:  led.c
+ *    Description:  This file is used to control RGB 3-colors LED
+ *                  compatible with libgpiod-1.x.x, not compatible with 2.x.x
+ *
+ *
+ * Pin connection:
+ *               RGB Led Module           Raspberry Pi Board
+ *                   R        <----->      #Pin33(BCM GPIO13)
+ *                   G        <----->      #Pin35(BCM GPIO19)
+ *                   B        <----->      #Pin37(BCM GPIO26)
+ *                  GND       <----->      GND
+ *
+ * System install:
+ *                  sudo apt install -y libgpiod-dev gpiod
+ *
+ *
+ ********************************************************************************/
+
+#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 <signal.h>
+
+#include <gpiod.h>
+#include "logger.h"
+#include "leds.h"
+
+#define DELAY     500
+
+static led_info_t    leds_info[LED_CNT] =
+{
+    {"red",   13, 1, NULL },
+    {"green", 19, 1, NULL },
+    {"blue",  26, 1, NULL },
+};
+
+int init_led(led_ctx_t *ctx, int which)
+{
+    int            i, rv;
+    led_info_t    *led;
+
+    if( !ctx )
+    {
+        log_error("Invalid input arguments\n");
+        return -1;
+    }
+
+    ctx->leds = leds_info;
+    ctx->count = LED_CNT;
+
+    ctx->chip = gpiod_chip_open_by_name("gpiochip0");
+    if( !ctx->chip )
+    {
+        log_error("open gpiochip failure, maybe you need running as root\n");
+        return -2;
+    }
+
+	for(i=0; i<ctx->count; i++)
+	{
+		if( which == i )
+		{
+			led = &ctx->leds[i];
+
+			led->line = gpiod_chip_get_line(ctx->chip, led->gpio);
+			if( !led->line )
+			{
+				log_error("open gpioline for %s[%d] failed\n", led->name, led->gpio);
+				rv = -3;
+				goto failed;
+			}
+
+			rv  = gpiod_line_request_output(led->line, led->name, !led->active);
+			if( rv )
+			{
+				log_error("request gpio output for %5s[%d] failed: %s\n", led->name, led->gpio, strerror(errno));
+				rv = -4;
+				goto failed;
+			}
+
+			log_debug("request %5s led[%d] for gpio output okay\n", led->name, led->gpio);
+		}
+	}
+
+    return 0;
+
+failed:
+    term_led(ctx, which);
+    return rv;
+}
+
+int term_led(led_ctx_t *ctx, int which)
+{
+    int            i;
+    led_info_t    *led;
+
+    log_warn("terminate RGB Led gpios\n");
+
+    if( !ctx )
+    {
+        log_error("Invalid input arguments\n");
+        return -1;
+    }
+
+    if( !ctx->chip )
+        return 0;
+
+    for(i=0; i<ctx->count; i++)
+	{
+		if( which == i )
+		{
+			led = &ctx->leds[i];
+
+			if( led->line )
+				gpiod_line_release(led->line);
+		}
+	}
+
+    gpiod_chip_close(ctx->chip);
+    return 0;
+}
+
+int turn_led(int which, int cmd)
+{
+    int            rv = 0;
+    led_ctx_t      ctx;
+    led_info_t    *led;
+
+    if( which<0 || which>=LED_CNT )
+    {
+        log_error("Invalid input arguments\n");
+        return -1;
+    }
+
+    if( (rv=init_led(&ctx, which)) < 0 )
+    {
+        log_error("Initial RGB leds failure, rv=%d\n", rv);
+        return -2;
+    }
+
+    led = &ctx.leds[which];
+
+    if( OFF == cmd )
+    {
+        gpiod_line_set_value(led->line, !led->active);
+    }
+    else
+    {
+        gpiod_line_set_value(led->line, led->active);
+    }
+
+    term_led(&ctx, which);
+    return 0;
+}

--
Gitblit v1.9.1