Debian Codegraph @

src/osmo-bts-oc2g/misc/oc2gbts_led.c

raw
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 "oc2gbts_led.h"
20 #include "oc2gbts_bts.h"
21 #include <osmocom/core/talloc.h>
22 #include <osmocom/core/linuxlist.h>
23
24 static struct oc2gbts_led led_entries[] = {
25 {
26 .name = "led0",
27 .fullname = "led red",
28 .path = "/var/oc2g/leds/led0/brightness"
29 },
30 {
31 .name = "led1",
32 .fullname = "led green",
33 .path = "/var/oc2g/leds/led1/brightness"
34 }
35 };
36
37 static const struct value_string oc2gbts_led_strs[] = {
38 { OC2GBTS_LED_RED, "LED red" },
39 { OC2GBTS_LED_GREEN, "LED green" },
40 { OC2GBTS_LED_ORANGE, "LED orange" },
41 { OC2GBTS_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_EXT_LINK_MALFUNC,
54 BLINK_PATTERN_SUPPLY_VOLT_LOW,
55 BLINK_PATTERN_TEMP_HIGH,
56 BLINK_PATTERN_VSWR_HIGH,
57 BLINK_PATTERN_SUPPLY_PWR_HIGH,
58 BLINK_PATTERN_PA_PWR_HIGH,
59 BLINK_PATTERN_GPS_FIX_LOST,
60 BLINK_PATTERN_NORMAL
61 };
62
63
64 char *blink_pattern_command[] = BLINK_PATTERN_COMMAND;
65
66 static int oc2gbts_led_write(char *path, char *str)
67 {
68 int fd;
69
70 if ((fd = open(path, O_WRONLY)) == -1)
71 {
72 return 0;
73 }
74
75 write(fd, str, strlen(str)+1);
76 close(fd);
77 return 1;
78 }
79
80 static void led_set_red()
81 {
82 oc2gbts_led_write(led_entries[0].path, "1");
83 oc2gbts_led_write(led_entries[1].path, "0");
84 }
85
86 static void led_set_green()
87 {
88 oc2gbts_led_write(led_entries[0].path, "0");
89 oc2gbts_led_write(led_entries[1].path, "1");
90 }
91
92 static void led_set_orange()
93 {
94 oc2gbts_led_write(led_entries[0].path, "1");
95 oc2gbts_led_write(led_entries[1].path, "1");
96 }
97
98 static void led_set_off()
99 {
100 oc2gbts_led_write(led_entries[0].path, "0");
101 oc2gbts_led_write(led_entries[1].path, "0");
102 }
103
104 static void led_sleep( struct oc2gbts_mgr_instance *mgr, struct oc2gbts_led_timer *led_timer, void (*led_timer_cb)(void *_data)) {
105 /* Cancel any pending timer */
106 osmo_timer_del(&led_timer->timer);
107 /* Start LED timer */
108 led_timer->timer.cb = led_timer_cb;
109 led_timer->timer.data = mgr;
110 mgr->oc2gbts_leds.active_timer = led_timer->idx;
111 osmo_timer_schedule(&led_timer->timer, led_timer->param.sleep_sec, led_timer->param.sleep_usec);
112 LOGP(DTEMP, LOGL_DEBUG,"%s timer scheduled for %d sec + %d usec\n",
113 get_value_string(oc2gbts_led_strs, led_timer->idx),
114 led_timer->param.sleep_sec,
115 led_timer->param.sleep_usec);
116
117 switch (led_timer->idx) {
118 case OC2GBTS_LED_RED:
119 led_set_red();
120 break;
121 case OC2GBTS_LED_GREEN:
122 led_set_green();
123 break;
124 case OC2GBTS_LED_ORANGE:
125 led_set_orange();
126 break;
127 case OC2GBTS_LED_OFF:
128 led_set_off();
129 break;
130 default:
131 led_set_off();
132 }
133 }
134
135 static void led_sleep_cb(void *_data) {
136 struct oc2gbts_mgr_instance *mgr = _data;
137 struct oc2gbts_led_timer_list *led_list;
138
139 /* make sure the timer list is not empty */
140 if (llist_empty(&mgr->oc2gbts_leds.list))
141 return;
142
143 llist_for_each_entry(led_list, &mgr->oc2gbts_leds.list, list) {
144 if (led_list->led_timer.idx == mgr->oc2gbts_leds.active_timer) {
145 LOGP(DTEMP, LOGL_DEBUG,"Delete expired %s timer %d sec + %d usec\n",
146 get_value_string(oc2gbts_led_strs, led_list->led_timer.idx),
147 led_list->led_timer.param.sleep_sec,
148 led_list->led_timer.param.sleep_usec);
149
150 /* Delete current timer */
151 osmo_timer_del(&led_list->led_timer.timer);
152 /* Rotate the timer list */
153 llist_move_tail(&led_list->list, &mgr->oc2gbts_leds.list);
154 break;
155 }
156 }
157
158 /* Execute next timer */
159 led_list = llist_first_entry(&mgr->oc2gbts_leds.list, struct oc2gbts_led_timer_list, list);
160 if (led_list) {
161 LOGP(DTEMP, LOGL_DEBUG,"Execute %s timer %d sec + %d usec, total entries=%d\n",
162 get_value_string(oc2gbts_led_strs, led_list->led_timer.idx),
163 led_list->led_timer.param.sleep_sec,
164 led_list->led_timer.param.sleep_usec,
165 llist_count(&mgr->oc2gbts_leds.list));
166
167 led_sleep(mgr, &led_list->led_timer, led_sleep_cb);
168 }
169
170 }
171
172 static void delete_led_timer_entries(struct oc2gbts_mgr_instance *mgr)
173 {
174 struct oc2gbts_led_timer_list *led_list, *led_list2;
175
176 if (llist_empty(&mgr->oc2gbts_leds.list))
177 return;
178
179 llist_for_each_entry_safe(led_list, led_list2, &mgr->oc2gbts_leds.list, list) {
180 /* Delete the timer in list */
181 if (led_list) {
182 LOGP(DTEMP, LOGL_DEBUG,"Delete %s timer entry from list, %d sec + %d usec\n",
183 get_value_string(oc2gbts_led_strs, led_list->led_timer.idx),
184 led_list->led_timer.param.sleep_sec,
185 led_list->led_timer.param.sleep_usec);
186
187 /* Delete current timer */
188 osmo_timer_del(&led_list->led_timer.timer);
189 llist_del(&led_list->list);
190 talloc_free(led_list);
191 }
192 }
193 return;
194 }
195
196 static int add_led_timer_entry(struct oc2gbts_mgr_instance *mgr, char *cmdstr)
197 {
198 double sec, int_sec, frac_sec;
199 struct oc2gbts_sleep_time led_param;
200
201 led_param.sleep_sec = 0;
202 led_param.sleep_usec = 0;
203
204 if (strstr(cmdstr, "set red") != NULL)
205 mgr->oc2gbts_leds.led_idx = OC2GBTS_LED_RED;
206 else if (strstr(cmdstr, "set green") != NULL)
207 mgr->oc2gbts_leds.led_idx = OC2GBTS_LED_GREEN;
208 else if (strstr(cmdstr, "set orange") != NULL)
209 mgr->oc2gbts_leds.led_idx = OC2GBTS_LED_ORANGE;
210 else if (strstr(cmdstr, "set off") != NULL)
211 mgr->oc2gbts_leds.led_idx = OC2GBTS_LED_OFF;
212 else if (strstr(cmdstr, "sleep") != NULL) {
213 sec = atof(cmdstr + 6);
214 /* split time into integer and fractional of seconds */
215 frac_sec = modf(sec, &int_sec) * 1000000.0;
216 led_param.sleep_sec = (int)int_sec;
217 led_param.sleep_usec = (int)frac_sec;
218
219 if ((mgr->oc2gbts_leds.led_idx >= OC2GBTS_LED_RED) && (mgr->oc2gbts_leds.led_idx < _OC2GBTS_LED_MAX)) {
220 struct oc2gbts_led_timer_list *led_list;
221
222 /* allocate timer entry */
223 led_list = talloc_zero(tall_mgr_ctx, struct oc2gbts_led_timer_list);
224 if (led_list) {
225 led_list->led_timer.idx = mgr->oc2gbts_leds.led_idx;
226 led_list->led_timer.param.sleep_sec = led_param.sleep_sec;
227 led_list->led_timer.param.sleep_usec = led_param.sleep_usec;
228 llist_add_tail(&led_list->list, &mgr->oc2gbts_leds.list);
229
230 LOGP(DTEMP, LOGL_DEBUG,"Add %s timer to list, %d sec + %d usec, total entries=%d\n",
231 get_value_string(oc2gbts_led_strs, mgr->oc2gbts_leds.led_idx),
232 led_list->led_timer.param.sleep_sec,
233 led_list->led_timer.param.sleep_usec,
234 llist_count(&mgr->oc2gbts_leds.list));
235 }
236 }
237 } else
238 return -1;
239
240 return 0;
241 }
242
243 static int parse_led_pattern(char *pattern, struct oc2gbts_mgr_instance *mgr)
244 {
245 char str[1024];
246 char *pstr;
247 char *sep;
248 int rc = 0;
249
250 strcpy(str, pattern);
251 pstr = str;
252 while ((sep = strsep(&pstr, ";")) != NULL) {
253 rc = add_led_timer_entry(mgr, sep);
254 if (rc < 0) {
255 break;
256 }
257
258 }
259 return rc;
260 }
261
262 /*** led interface ***/
263
264 void led_set(struct oc2gbts_mgr_instance *mgr, int pattern_id)
265 {
266 int rc;
267 struct oc2gbts_led_timer_list *led_list;
268
269 if (pattern_id > BLINK_PATTERN_MAX_ITEM - 1) {
270 LOGP(DTEMP, LOGL_ERROR, "Invalid LED pattern : %d. LED pattern must be between %d..%d\n",
271 pattern_id,
272 BLINK_PATTERN_POWER_ON,
273 BLINK_PATTERN_MAX_ITEM - 1);
274 return;
275 }
276 if (pattern_id == mgr->oc2gbts_leds.last_pattern_id)
277 return;
278
279 mgr->oc2gbts_leds.last_pattern_id = pattern_id;
280
281 LOGP(DTEMP, LOGL_INFO, "blink pattern command : %d\n", pattern_id);
282 LOGP(DTEMP, LOGL_INFO, "%s\n", blink_pattern_command[pattern_id]);
283
284 /* Empty existing LED timer in the list */
285 delete_led_timer_entries(mgr);
286
287 /* parse LED pattern */
288 rc = parse_led_pattern(blink_pattern_command[pattern_id], mgr);
289 if (rc < 0) {
290 LOGP(DTEMP, LOGL_ERROR,"LED pattern not found or invalid LED pattern\n");
291 return;
292 }
293
294 /* make sure the timer list is not empty */
295 if (llist_empty(&mgr->oc2gbts_leds.list))
296 return;
297
298 /* Start the first LED timer in the list */
299 led_list = llist_first_entry(&mgr->oc2gbts_leds.list, struct oc2gbts_led_timer_list, list);
300 if (led_list) {
301 LOGP(DTEMP, LOGL_DEBUG,"Execute timer %s for %d sec + %d usec\n",
302 get_value_string(oc2gbts_led_strs, led_list->led_timer.idx),
303 led_list->led_timer.param.sleep_sec,
304 led_list->led_timer.param.sleep_usec);
305
306 led_sleep(mgr, &led_list->led_timer, led_sleep_cb);
307 }
308
309 }
310
311 void select_led_pattern(struct oc2gbts_mgr_instance *mgr)
312 {
313 int i;
314 uint8_t led[BLINK_PATTERN_MAX_ITEM] = {0};
315
316 /* set normal LED pattern at first */
317 led[BLINK_PATTERN_NORMAL] = 1;
318
319 /* check on-board sensors for new LED pattern */
320 check_sensor_led_pattern(mgr, led);
321
322 /* check BTS status for new LED pattern */
323 check_bts_led_pattern(led);
324
325 /* check by priority */
326 for (i = 0; i < sizeof(led_priority)/sizeof(uint8_t); i++) {
327 if(led[led_priority[i]] == 1) {
328 led_set(mgr, led_priority[i]);
329 break;
330 }
331 }
332 }