1 /*
2 * ser2net - A program for allowing telnet connection to serial ports
3 * Copyright (C) 2001-2016 Corey Minyard <minyard@acm.org>
4 * Copyright (C) 2016 Michael Heimpold <mhei@heimpold.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /* This file holds common LED code required to read the configuration file and
22 to dispatch calls from dataxfer to the actual LED driver. */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <stdio.h>
28 #include <syslog.h>
29
30 #include "led.h"
31 #include "led_sysfs.h"
32
33 /* list of all registered LED drivers */
34 static struct led_driver_s *led_drivers = NULL;
35
36 /* all LEDs in the system. */
37 static struct led_s *leds = NULL;
38
39 static struct led_driver_s *
led_driver_by_name(const char * name)40 led_driver_by_name(const char *name)
41 {
42 struct led_driver_s *drv = led_drivers;
43
44 while (drv) {
45 if (strcmp(name, drv->name) == 0)
46 return drv;
47 drv = drv->next;
48 }
49
50 return NULL;
51 }
52
53 int
led_driver_init(void)54 led_driver_init(void)
55 {
56 int rv = 0;
57
58 rv |= led_sysfs_register();
59
60 return rv;
61 }
62
63 int
led_driver_register(struct led_driver_s * led_driver)64 led_driver_register(struct led_driver_s *led_driver)
65 {
66 led_driver->next = led_drivers;
67 led_drivers = led_driver;
68 return 0;
69 }
70
71 void
handle_led(const char * name,char * cfg,int lineno)72 handle_led(const char *name, char *cfg, int lineno)
73 {
74 struct led_driver_s *driver;
75 struct led_s *new_led;
76 char *delim;
77
78 delim = strchr(cfg, ':');
79 if (!delim) {
80 syslog(LOG_ERR, "Couldn't parse LED definition for '%s' on %d",
81 name, lineno);
82 return;
83 }
84 *delim++ = '\0';
85
86 driver = led_driver_by_name(cfg);
87 if (!driver) {
88 syslog(LOG_ERR, "Unknown LED driver '%s' for LED '%s' on %d",
89 cfg, name, lineno);
90 return;
91 }
92
93 new_led = calloc(1, sizeof(*new_led));
94 if (!new_led) {
95 syslog(LOG_ERR, "Out of memory handling LED '%s' on %d", name, lineno);
96 return;
97 }
98
99 new_led->name = strdup(name);
100 if (!new_led->name) {
101 syslog(LOG_ERR, "Out of memory handling LED '%s' on %d", name, lineno);
102 free(new_led);
103 return;
104 }
105
106 new_led->driver = driver;
107
108 if (new_led->driver->init(new_led, delim, lineno) < 0) {
109 /* errors should be reported by driver itself */
110 free(new_led->name);
111 free(new_led);
112 return;
113 }
114
115 if (new_led->driver->configure) {
116 if (new_led->driver->configure(new_led->drv_data) < 0) {
117 /*
118 * errors should be reported by driver itself; however, we
119 * cleanup here
120 */
121 if (new_led->driver->free)
122 new_led->driver->free(new_led);
123
124 free(new_led->name);
125 free(new_led);
126 return;
127 }
128 }
129
130 new_led->next = leds;
131 leds = new_led;
132 }
133
134 struct led_s *
find_led(const char * name)135 find_led(const char *name)
136 {
137 struct led_s *led = leds;
138
139 while (led) {
140 if (strcmp(name, led->name) == 0)
141 return led;
142 led = led->next;
143 }
144
145 syslog(LOG_ERR, "LED '%s' not found, it will be ignored", name);
146 return NULL;
147 }
148
149 void
free_leds(void)150 free_leds(void)
151 {
152 while (leds) {
153 struct led_s *led = leds;
154 leds = leds->next;
155
156 /* let driver deconfigure the LED */
157 if (led->driver->deconfigure)
158 led->driver->deconfigure(led);
159
160 /* let driver free its own data when it registered a cleanup function */
161 if (led->driver->free)
162 led->driver->free(led);
163
164 free(led->name);
165 free(led);
166 }
167 }
168
169 int
led_flash(struct led_s * led)170 led_flash(struct led_s *led)
171 {
172 return led->driver->flash(led->drv_data);
173 }
174