Debian Codegraph @

src/osmo-bts-lc15/misc/lc15bts_led.c

raw symbols SCIP
1 /* Copyright (C) 2016 by NuRAN Wireless <[email protected]>
2 *
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 #include "lc15bts_led.h"
20 #include "lc15bts_bts.h"
21 #include <osmocom/core/talloc.h>
22 #include <osmocom/core/linuxlist.h>
23
24 static struct lc15bts_led led_entries[] = {
25 {
26 .name = "led0",
27 .fullname = "led red",
28 .path = "/var/lc15/leds/led0/brightness"
29 },
30 {
31 .name = "led1",
32 .fullname = "led green",
33 .path = "/var/lc15/leds/led1/brightness"
34 }
35 };
36
37 static const struct value_string lc15bts_led_strs[] = {
38 { LC15BTS_LED_RED, "LED red" },
39 { LC15BTS_LED_GREEN, "LED green" },
40 { LC15BTS_LED_ORANGE, "LED orange" },
41 { LC15BTS_LED_OFF, "LED off" },
42 { 0, NULL }
43 };
44
45 static uint8_t led_priority[] = {
46 BLINK_PATTERN_INIT,
47 BLINK_PATTERN_INT_PROC_MALFUNC,
48 BLINK_PATTERN_SUPPLY_PWR_MAX,
49 BLINK_PATTERN_PA_PWR_MAX,
50 BLINK_PATTERN_VSWR_MAX,
51 BLINK_PATTERN_SUPPLY_VOLT_MIN,
52 BLINK_PATTERN_TEMP_MAX,
53 BLINK_PATTERN_SUPPLY_PWR_MAX2,
54 BLINK_PATTERN_EXT_LINK_MALFUNC,
55 BLINK_PATTERN_SUPPLY_VOLT_LOW,
56 BLINK_PATTERN_TEMP_HIGH,
57 BLINK_PATTERN_VSWR_HIGH,
58 BLINK_PATTERN_SUPPLY_PWR_HIGH,
59 BLINK_PATTERN_PA_PWR_HIGH,
60 BLINK_PATTERN_GPS_FIX_LOST,
61 BLINK_PATTERN_NORMAL
62 };
63
64
65 char *blink_pattern_command[] = BLINK_PATTERN_COMMAND;
66
67 static int lc15bts_led_write(char *path, char *str)
68 {
69 int fd;
70
71 if ((fd = open(path, O_WRONLY)) == -1)
72 {
73 return 0;
74 }
75
76 write(fd, str, strlen(str)+1);
77 close(fd);
78 return 1;
79 }
80
81 static void led_set_red()
82 {
83 lc15bts_led_write(led_entries[0].path, "1");
84 lc15bts_led_write(led_entries[1].path, "0");
85 }
86
87 static void led_set_green()
88 {
89 lc15bts_led_write(led_entries[0].path, "0");
90 lc15bts_led_write(led_entries[1].path, "1");
91 }
92
93 static void led_set_orange()
94 {
95 lc15bts_led_write(led_entries[0].path, "1");
96 lc15bts_led_write(led_entries[1].path, "1");
97 }
98
99 static void led_set_off()
100 {
101 lc15bts_led_write(led_entries[0].path, "0");
102 lc15bts_led_write(led_entries[1].path, "0");
103 }
104
105 static void led_sleep( struct lc15bts_mgr_instance *mgr, struct lc15bts_led_timer *led_timer, void (*led_timer_cb)(void *_data)) {
106 /* Cancel any pending timer */
107 osmo_timer_del(&led_timer->timer);
108 /* Start LED timer */
109 led_timer->timer.cb = led_timer_cb;
110 led_timer->timer.data = mgr;
111 mgr->lc15bts_leds.active_timer = led_timer->idx;
112 osmo_timer_schedule(&led_timer->timer, led_timer->param.sleep_sec, led_timer->param.sleep_usec);
113 LOGP(DTEMP, LOGL_DEBUG,"%s timer scheduled for %d sec + %d usec\n",
114 get_value_string(lc15bts_led_strs, led_timer->idx),
115 led_timer->param.sleep_sec,
116 led_timer->param.sleep_usec);
117
118 switch (led_timer->idx) {
119 case LC15BTS_LED_RED:
120 led_set_red();
121 break;
122 case LC15BTS_LED_GREEN:
123 led_set_green();
124 break;
125 case LC15BTS_LED_ORANGE:
126 led_set_orange();
127 break;
128 case LC15BTS_LED_OFF:
129 led_set_off();
130 break;
131 default:
132 led_set_off();
133 }
134 }
135
136 static void led_sleep_cb(void *_data) {
137 struct lc15bts_mgr_instance *mgr = _data;
138 struct lc15bts_led_timer_list *led_list;
139
140 /* make sure the timer list is not empty */
141 if (llist_empty(&mgr->lc15bts_leds.list))
142 return;
143
144 llist_for_each_entry(led_list, &mgr->lc15bts_leds.list, list) {
145 if (led_list->led_timer.idx == mgr->lc15bts_leds.active_timer) {
146 LOGP(DTEMP, LOGL_DEBUG,"Delete expired %s timer %d sec + %d usec\n",
147 get_value_string(lc15bts_led_strs, led_list->led_timer.idx),
148 led_list->led_timer.param.sleep_sec,
149 led_list->led_timer.param.sleep_usec);
150
151 /* Delete current timer */
152 osmo_timer_del(&led_list->led_timer.timer);
153 /* Rotate the timer list */
154 llist_move_tail(&led_list->list, &mgr->lc15bts_leds.list);
155 break;
156 }
157 }
158
159 /* Execute next timer */
160 led_list = llist_first_entry(&mgr->lc15bts_leds.list, struct lc15bts_led_timer_list, list);
161 if (led_list) {
162 LOGP(DTEMP, LOGL_DEBUG,"Execute %s timer %d sec + %d usec, total entries=%d\n",
163 get_value_string(lc15bts_led_strs, led_list->led_timer.idx),
164 led_list->led_timer.param.sleep_sec,
165 led_list->led_timer.param.sleep_usec,
166 llist_count(&mgr->lc15bts_leds.list));
167
168 led_sleep(mgr, &led_list->led_timer, led_sleep_cb);
169 }
170
171 }
172
173 static void delete_led_timer_entries(struct lc15bts_mgr_instance *mgr)
174 {
175 struct lc15bts_led_timer_list *led_list, *led_list2;
176
177 if (llist_empty(&mgr->lc15bts_leds.list))
178 return;
179
180 llist_for_each_entry_safe(led_list, led_list2, &mgr->lc15bts_leds.list, list) {
181 /* Delete the timer in list */
182 if (led_list) {
183 LOGP(DTEMP, LOGL_DEBUG,"Delete %s timer entry from list, %d sec + %d usec\n",
184 get_value_string(lc15bts_led_strs, led_list->led_timer.idx),
185 led_list->led_timer.param.sleep_sec,
186 led_list->led_timer.param.sleep_usec);
187
188 /* Delete current timer */
189 osmo_timer_del(&led_list->led_timer.timer);
190 llist_del(&led_list->list);
191 talloc_free(led_list);
192 }
193 }
194 return;
195 }
196
197 static int add_led_timer_entry(struct lc15bts_mgr_instance *mgr, char *cmdstr)
198 {
199 double sec, int_sec, frac_sec;
200 struct lc15bts_sleep_time led_param;
201
202 led_param.sleep_sec = 0;
203 led_param.sleep_usec = 0;
204
205 if (strstr(cmdstr, "set red") != NULL)
206 mgr->lc15bts_leds.led_idx = LC15BTS_LED_RED;
207 else if (strstr(cmdstr, "set green") != NULL)
208 mgr->lc15bts_leds.led_idx = LC15BTS_LED_GREEN;
209 else if (strstr(cmdstr, "set orange") != NULL)
210 mgr->lc15bts_leds.led_idx = LC15BTS_LED_ORANGE;
211 else if (strstr(cmdstr, "set off") != NULL)
212 mgr->lc15bts_leds.led_idx = LC15BTS_LED_OFF;
213 else if (strstr(cmdstr, "sleep") != NULL) {
214 sec = atof(cmdstr + 6);
215 /* split time into integer and fractional of seconds */
216 frac_sec = modf(sec, &int_sec) * 1000000.0;
217 led_param.sleep_sec = (int)int_sec;
218 led_param.sleep_usec = (int)frac_sec;
219
220 if ((mgr->lc15bts_leds.led_idx >= LC15BTS_LED_RED) && (mgr->lc15bts_leds.led_idx < _LC15BTS_LED_MAX)) {
221 struct lc15bts_led_timer_list *led_list;
222
223 /* allocate timer entry */
224 led_list = talloc_zero(tall_mgr_ctx, struct lc15bts_led_timer_list);
225 if (led_list) {
226 led_list->led_timer.idx = mgr->lc15bts_leds.led_idx;
227 led_list->led_timer.param.sleep_sec = led_param.sleep_sec;
228 led_list->led_timer.param.sleep_usec = led_param.sleep_usec;
229 llist_add_tail(&led_list->list, &mgr->lc15bts_leds.list);
230
231 LOGP(DTEMP, LOGL_DEBUG,"Add %s timer to list, %d sec + %d usec, total entries=%d\n",
232 get_value_string(lc15bts_led_strs, mgr->lc15bts_leds.led_idx),
233 led_list->led_timer.param.sleep_sec,
234 led_list->led_timer.param.sleep_usec,
235 llist_count(&mgr->lc15bts_leds.list));
236 }
237 }
238 } else
239 return -1;
240
241 return 0;
242 }
243
244 static int parse_led_pattern(char *pattern, struct lc15bts_mgr_instance *mgr)
245 {
246 char str[1024];
247 char *pstr;
248 char *sep;
249 int rc = 0;
250
251 strcpy(str, pattern);
252 pstr = str;
253 while ((sep = strsep(&pstr, ";")) != NULL) {
254 rc = add_led_timer_entry(mgr, sep);
255 if (rc < 0) {
256 break;
257 }
258
259 }
260 return rc;
261 }
262
263 /*** led interface ***/
264
265 void led_set(struct lc15bts_mgr_instance *mgr, int pattern_id)
266 {
267 int rc;
268 struct lc15bts_led_timer_list *led_list;
269
270 if (pattern_id > BLINK_PATTERN_MAX_ITEM - 1) {
271 LOGP(DTEMP, LOGL_ERROR, "Invalid LED pattern : %d. LED pattern must be between %d..%d\n",
272 pattern_id,
273 BLINK_PATTERN_POWER_ON,
274 BLINK_PATTERN_MAX_ITEM - 1);
275 return;
276 }
277 if (pattern_id == mgr->lc15bts_leds.last_pattern_id)
278 return;
279
280 mgr->lc15bts_leds.last_pattern_id = pattern_id;
281
282 LOGP(DTEMP, LOGL_NOTICE, "blink pattern command : %d\n", pattern_id);
283 LOGP(DTEMP, LOGL_NOTICE, "%s\n", blink_pattern_command[pattern_id]);
284
285 /* Empty existing LED timer in the list */
286 delete_led_timer_entries(mgr);
287
288 /* parse LED pattern */
289 rc = parse_led_pattern(blink_pattern_command[pattern_id], mgr);
290 if (rc < 0) {
291 LOGP(DTEMP, LOGL_ERROR,"LED pattern not found or invalid LED pattern\n");
292 return;
293 }
294
295 /* make sure the timer list is not empty */
296 if (llist_empty(&mgr->lc15bts_leds.list))
297 return;
298
299 /* Start the first LED timer in the list */
300 led_list = llist_first_entry(&mgr->lc15bts_leds.list, struct lc15bts_led_timer_list, list);
301 if (led_list) {
302 LOGP(DTEMP, LOGL_DEBUG,"Execute timer %s for %d sec + %d usec\n",
303 get_value_string(lc15bts_led_strs, led_list->led_timer.idx),
304 led_list->led_timer.param.sleep_sec,
305 led_list->led_timer.param.sleep_usec);
306
307 led_sleep(mgr, &led_list->led_timer, led_sleep_cb);
308 }
309
310 }
311
312 void select_led_pattern(struct lc15bts_mgr_instance *mgr)
313 {
314 int i;
315 uint8_t led[BLINK_PATTERN_MAX_ITEM] = {0};
316
317 /* set normal LED pattern at first */
318 led[BLINK_PATTERN_NORMAL] = 1;
319
320 /* check on-board sensors for new LED pattern */
321 check_sensor_led_pattern(mgr, led);
322
323 /* check BTS status for new LED pattern */
324 check_bts_led_pattern(led);
325
326 /* check by priority */
327 for (i = 0; i < sizeof(led_priority)/sizeof(uint8_t); i++) {
328 if(led[led_priority[i]] == 1) {
329 led_set(mgr, led_priority[i]);
330 break;
331 }
332 }
333 }