blob: 4fe0aa4934a5b8be1762c9dd42e1bffbb7a3c8f4 [file] [log] [blame]
Norman James772232e2015-10-18 14:43:28 -05001#include <stdio.h>
Adriana Kobylak9c751042016-02-09 13:44:32 -06002#include <stdlib.h>
3#include <errno.h>
4#include <string.h>
5#include <dirent.h>
6#include <systemd/sd-bus.h>
Norman James772232e2015-10-18 14:43:28 -05007
Adriana Kobylak9c751042016-02-09 13:44:32 -06008/*
9 * These are control files that are present for each led under
10 *'/sys/class/leds/<led_name>/' which are used to trigger action
11 * on the respective leds by writing predefined data.
12 */
13const char *power_ctrl = "brightness";
14const char *blink_ctrl = "trigger";
15const char *duty_on = "delay_on";
16const char *duty_off = "delay_off";
Norman James772232e2015-10-18 14:43:28 -050017
Adriana Kobylak9c751042016-02-09 13:44:32 -060018/*
19 * --------------------------------------------------
20 * Given the dbus path, returns the name of the LED
21 * --------------------------------------------------
22 */
23char *get_led_name(const char *dbus_path)
24{
25 char *led_name = NULL;
Norman James772232e2015-10-18 14:43:28 -050026
Adriana Kobylak9c751042016-02-09 13:44:32 -060027 /* Get the led name from /org/openbmc/control/led/<name> */
28 led_name = strrchr(dbus_path, '/');
29 if(led_name)
30 {
31 led_name++;
32 }
Norman James772232e2015-10-18 14:43:28 -050033
Adriana Kobylak9c751042016-02-09 13:44:32 -060034 return led_name;
35}
36
37/*
38 * -------------------------------------------------------------------------
39 * Writes the 'on / off / blink' trigger to leds.
40 * -------------------------------------------------------------------------
41 */
42int write_to_led(const char *name, const char *ctrl_file, const char *value)
43{
44 /* Generic error reporter. */
45 int rc = -1;
46
47 /* To get /sys/class/leds/<name>/<control file> */
48 char led_path[128] = {0};
49
50 int len = 0;
Adriana Kobylakcc2be262016-02-09 16:50:14 -060051 len = snprintf(led_path, sizeof(led_path),
Adriana Kobylak9c751042016-02-09 13:44:32 -060052 "/sys/class/leds/%s/%s",name, ctrl_file);
53 if(len >= sizeof(led_path))
54 {
55 fprintf(stderr, "Error. LED path is too long. :[%d]\n",len);
56 return rc;
57 }
58
59 FILE *fp = fopen(led_path,"w");
60 if(fp == NULL)
61 {
Adriana Kobylakcc2be262016-02-09 16:50:14 -060062 fprintf(stderr,"Error:[%s] opening:[%s]\n",strerror(errno),led_path);
Adriana Kobylak9c751042016-02-09 13:44:32 -060063 return rc;
64 }
65
66 rc = fwrite(value, strlen(value), 1, fp);
67 if(rc != 1)
68 {
Adriana Kobylakcc2be262016-02-09 16:50:14 -060069 fprintf(stderr, "Error:[%s] writing to :[%s]\n",strerror(errno),led_path);
Adriana Kobylak9c751042016-02-09 13:44:32 -060070 }
71
72 fclose(fp);
73
74 /* When we get here, rc would be what it was from writing to the file */
75 return (rc == 1) ? 0 : -1;
76}
77
78/*
79 * ----------------------------------------------------------------
80 * Router function for any LED operations that come via dbus
81 *----------------------------------------------------------------
82 */
83static int led_function_router(sd_bus_message *msg, void *user_data,
84 sd_bus_error *ret_error)
85{
86 /* Generic error reporter. */
87 int rc = -1;
88
89 /* Extract the led name from the full dbus path */
90 const char *led_path = sd_bus_message_get_path(msg);
91 if(led_path == NULL)
92 {
93 fprintf(stderr, "Error. LED path is empty");
94 return sd_bus_reply_method_return(msg, "i", rc);
95 }
96
97 char *led_name = get_led_name(led_path);
98 if(led_name == NULL)
99 {
100 fprintf(stderr, "Invalid LED name for path :[%s]\n",led_path);
101 return sd_bus_reply_method_return(msg, "i", rc);
102 }
103
104 /* Now that we have the LED name, get the Operation. */
105 const char *led_function = sd_bus_message_get_member(msg);
106 if(led_function == NULL)
107 {
108 fprintf(stderr, "Null LED function specificed for : [%s]\n",led_name);
109 return sd_bus_reply_method_return(msg, "i", rc);
110 }
111
112 /* Route the user action to appropriate handlers. */
113 if( (strcmp(led_function, "setOn") == 0) ||
114 (strcmp(led_function, "setOff") == 0))
115 {
116 rc = led_stable_state_function(led_name, led_function);
117 return sd_bus_reply_method_return(msg, "i", rc);
118 }
119 else if( (strcmp(led_function, "setBlinkFast") == 0) ||
120 (strcmp(led_function, "setBlinkSlow") == 0))
121 {
122 rc = led_default_blink(led_name, led_function);
123 return sd_bus_reply_method_return(msg, "i", rc);
124 }
vishwa98730f02016-02-22 01:50:04 -0600125 else if(strcmp(led_function, "BlinkCustom") == 0)
126 {
127 rc = led_custom_blink(led_name, msg);
128 return sd_bus_reply_method_return(msg, "i", rc);
129 }
Adriana Kobylak9c751042016-02-09 13:44:32 -0600130 else if(strcmp(led_function, "GetLedState") == 0)
131 {
132 char value_str[10] = {0};
133 const char *led_state = NULL;
134
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600135 rc = read_led(led_name, power_ctrl, value_str, sizeof(value_str)-1);
Adriana Kobylak9c751042016-02-09 13:44:32 -0600136 if(rc >= 0)
137 {
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600138 /* LED is active HI */
139 led_state = strtoul(value_str, NULL, 0) ? "On" : "Off";
Adriana Kobylak9c751042016-02-09 13:44:32 -0600140 }
141 return sd_bus_reply_method_return(msg, "is", rc, led_state);
142 }
143 else
144 {
145 fprintf(stderr,"Invalid LED function:[%s]\n",led_function);
146 }
147
148 return sd_bus_reply_method_return(msg, "i", rc);
149}
150
151/*
152 * --------------------------------------------------------------
153 * Turn On or Turn Off the LED
154 * --------------------------------------------------------------
155 */
156int led_stable_state_function(char *led_name, char *led_function)
157{
158 /* Generic error reporter. */
159 int rc = -1;
160
161 const char *value = NULL;
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600162 if(strcmp(led_function, "setOff") == 0)
Adriana Kobylak9c751042016-02-09 13:44:32 -0600163 {
164 /* LED active low */
165 value = "0";
166 }
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600167 else if(strcmp(led_function, "setOn") == 0)
Adriana Kobylak9c751042016-02-09 13:44:32 -0600168 {
169 value = "255";
170 }
171 else
172 {
173 fprintf(stderr,"Invalid LED stable state operation:[%s] \n",led_function);
174 return rc;
175 }
176
177 /*
178 * Before doing anything, need to turn off the blinking
179 * if there is one in progress by writing 'none' to trigger
180 */
181 rc = write_to_led(led_name, blink_ctrl, "none");
182 if(rc < 0)
183 {
184 fprintf(stderr,"Error disabling blink. Function:[%s]\n", led_function);
185 return rc;
186 }
187
188 /*
189 * Open the brightness file and write corresponding values.
190 */
191 rc = write_to_led(led_name, power_ctrl, value);
192 if(rc < 0)
193 {
194 fprintf(stderr,"Error driving LED. Function:[%s]\n", led_function);
195 }
196
197 return rc;
198}
199
200//-----------------------------------------------------------------------------------
201// Given the on and off duration, applies the action on the specified LED.
202//-----------------------------------------------------------------------------------
203int blink_led(const char *led_name, const char *on_duration, const char *off_duration)
204{
205 /* Generic error reporter */
206 int rc = -1;
207
208 /* Protocol demands that 'timer' be echoed to 'trigger' */
209 rc = write_to_led(led_name, blink_ctrl, "timer");
210 if(rc < 0)
211 {
212 fprintf(stderr,"Error writing timer to Led:[%s]\n", led_name);
213 return rc;
214 }
215
216 /*
217 * After writing 'timer to 'trigger', 2 new files get generated namely
218 *'delay_on' and 'delay_off' which are telling the time duration for a
219 * particular LED on and off.
220 */
221 rc = write_to_led(led_name, duty_on, on_duration);
222 if(rc < 0)
223 {
224 fprintf(stderr,"Error writing [%s] to delay_on:[%s]\n",on_duration,led_name);
225 return rc;
226 }
227
228 rc = write_to_led(led_name, duty_off, off_duration);
229 if(rc < 0)
230 {
231 fprintf(stderr,"Error writing [%s] to delay_off:[%s]\n",off_duration,led_name);
232 }
233
234 return rc;
235}
236
237/*
238 * ----------------------------------------------------
239 * Default blink action on the LED.
240 * ----------------------------------------------------
241 */
242int led_default_blink(char *led_name, char *blink_type)
243{
244 /* Generic error reporter */
245 int rc = -1;
246
247 /* How long the LED needs to be in on and off state while blinking */
248 const char *on_duration = NULL;
249 const char *off_duration = NULL;
250 if(strcmp(blink_type, "setBlinkSlow") == 0)
251 {
252 //*Delay 900 millisec before 'on' and delay 900 millisec before off */
253 on_duration = "900";
254 off_duration = "900";
255 }
256 else if(strcmp(blink_type, "setBlinkFast") == 0)
257 {
258 /* Delay 200 millisec before 'on' and delay 200 millisec before off */
259 on_duration = "200";
260 off_duration = "200";
261 }
262 else
263 {
264 fprintf(stderr,"Invalid blink operation:[%s]\n",blink_type);
265 return rc;
266 }
267
268 rc = blink_led(led_name, on_duration, off_duration);
269
270 return rc;
271}
272
273/*
vishwa98730f02016-02-22 01:50:04 -0600274 * -------------------------------------------------
275 * Blinks at user defined 'on' and 'off' intervals.
276 * -------------------------------------------------
277 */
278int led_custom_blink(const char *led_name, sd_bus_message *msg)
279{
280 /* Generic error reporter. */
281 int rc = -1;
282 int led_len = 0;
283
284 /* User supplied 'on' and 'off' duration converted into string */
285 char on_duration[32] = {0};
286 char off_duration[32] = {0};
287
288 /* User supplied 'on' and 'off' duration */
289 uint32_t user_input_on = 0;
290 uint32_t user_input_off = 0;
291
292 /* Extract values into 'ss' ( string, string) */
293 rc = sd_bus_message_read(msg, "uu", &user_input_on, &user_input_off);
294 if(rc < 0)
295 {
296 fprintf(stderr, "Failed to read 'on' and 'off' duration.[%s]\n", strerror(-rc));
297 }
298 else
299 {
300 /*
301 * Converting user supplied integer arguments into string as required by
302 * sys interface. The top level REST will make sure that an error is
303 * thrown right away on invalid inputs. However, REST is allowing the
304 * unsigned decimal and floating numbers but when its received here, its
305 * received as decimal so no input validation needed.
306 */
307 led_len = snprintf(on_duration, sizeof(on_duration),
308 "%d",user_input_on);
309 if(led_len >= sizeof(on_duration))
310 {
311 fprintf(stderr, "Error. Blink ON duration is too long. :[%d]\n",led_len);
312 return rc;
313 }
314
315 led_len = snprintf(off_duration, sizeof(off_duration),
316 "%d",user_input_off);
317 if(led_len >= sizeof(off_duration))
318 {
319 fprintf(stderr, "Error. Blink OFF duration is too long. :[%d]\n",led_len);
320 return rc;
321 }
322
323 /* We are good here.*/
324 rc = blink_led(led_name, on_duration, off_duration);
325 }
326 return rc;
327}
328
329/*
Adriana Kobylak9c751042016-02-09 13:44:32 -0600330 * ---------------------------------------------------------------
331 * Gets the current value of passed in LED file
332 * Mainly used for reading 'brightness'
333 * NOTE : It is the responsibility of the caller to allocate
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600334 * sufficient space for buffer. This will read upto user supplied
Adriana Kobylak9c751042016-02-09 13:44:32 -0600335 * size -or- entire contents of file whichever is smaller
336 * ----------------------------------------------------------------
337 */
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600338int read_led(const char *name, const char *ctrl_file,
Adriana Kobylak9c751042016-02-09 13:44:32 -0600339 void *value, const size_t len)
340{
341 /* Generic error reporter. */
342 int rc = -1;
343 int count = 0;
344
345 if(value == NULL || len <= 0)
346 {
347 fprintf(stderr, "Invalid buffer passed to LED read\n");
348 return rc;
349 }
350
351 /* To get /sys/class/leds/<name>/<control file> */
352 char led_path[128] = {0};
353
354 int led_len = 0;
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600355 led_len = snprintf(led_path, sizeof(led_path),
Adriana Kobylak9c751042016-02-09 13:44:32 -0600356 "/sys/class/leds/%s/%s",name, ctrl_file);
357 if(led_len >= sizeof(led_path))
358 {
359 fprintf(stderr, "Error. LED path is too long. :[%d]\n",led_len);
360 return rc;
361 }
362
363 FILE *fp = fopen(led_path,"rb");
364 if(fp == NULL)
365 {
vishwa98730f02016-02-22 01:50:04 -0600366 fprintf(stderr,"Error:[%s] opening:[%s]\n",strerror(errno),led_path);
Adriana Kobylak9c751042016-02-09 13:44:32 -0600367 return rc;
368 }
369
370 char *sysfs_value = (char *)value;
371 while(!feof(fp) && (count < len))
372 {
373 sysfs_value[count++] = fgetc(fp);
374 }
Adriana Kobylak9c751042016-02-09 13:44:32 -0600375
376 fclose(fp);
377 return 0;
378}
379
380/*
381 * -----------------------------------------------
382 * Dbus Services offered by this LED controller
383 * -----------------------------------------------
384 */
385static const sd_bus_vtable led_control_vtable[] =
386{
387 SD_BUS_VTABLE_START(0),
388 SD_BUS_METHOD("setOn", "", "i", &led_function_router, SD_BUS_VTABLE_UNPRIVILEGED),
389 SD_BUS_METHOD("setOff", "", "i", &led_function_router, SD_BUS_VTABLE_UNPRIVILEGED),
390 SD_BUS_METHOD("setBlinkFast", "", "i", &led_function_router, SD_BUS_VTABLE_UNPRIVILEGED),
391 SD_BUS_METHOD("setBlinkSlow", "", "i", &led_function_router, SD_BUS_VTABLE_UNPRIVILEGED),
392 SD_BUS_METHOD("GetLedState", "", "is", &led_function_router, SD_BUS_VTABLE_UNPRIVILEGED),
vishwa98730f02016-02-22 01:50:04 -0600393 SD_BUS_METHOD("BlinkCustom", "uu", "i", &led_function_router, SD_BUS_VTABLE_UNPRIVILEGED),
Adriana Kobylak9c751042016-02-09 13:44:32 -0600394 SD_BUS_VTABLE_END,
Norman James772232e2015-10-18 14:43:28 -0500395};
396
Adriana Kobylak9c751042016-02-09 13:44:32 -0600397/*
398 * ---------------------------------------------
399 * Interested in all files except standard ones
400 * ---------------------------------------------
401 */
402int led_select(const struct dirent *entry)
Norman James772232e2015-10-18 14:43:28 -0500403{
Adriana Kobylak9c751042016-02-09 13:44:32 -0600404 if( (strcmp(entry->d_name, ".") == 0) ||
405 (strcmp(entry->d_name, "..") == 0))
406 {
407 return 0;
408 }
409 return 1;
Norman James772232e2015-10-18 14:43:28 -0500410}
411
Adriana Kobylak9c751042016-02-09 13:44:32 -0600412/*
413 * ------------------------------------------------
414 * Called as part of setting up skeleton services.
415 * -----------------------------------------------
416 */
417int start_led_services()
Norman James772232e2015-10-18 14:43:28 -0500418{
Adriana Kobylak9c751042016-02-09 13:44:32 -0600419 /* Generic error reporter. */
420 int rc = -1;
421 int num_leds = 0;
422 int count_leds = 0;
423
424 /* Bus and slot where we are offering the LED dbus service. */
425 sd_bus *bus_type = NULL;
426 sd_bus_slot *led_slot = NULL;
427
428 /* For walking '/sys/class/leds/' looking for names of LED.*/
429 struct dirent **led_list;
430
431 /* Get a hook onto system bus. */
432 rc = sd_bus_open_system(&bus_type);
433 if(rc < 0)
434 {
435 fprintf(stderr,"Error opening system bus.\n");
436 return rc;
437 }
438
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600439 count_leds = num_leds = scandir("/sys/class/leds/",
Adriana Kobylak9c751042016-02-09 13:44:32 -0600440 &led_list, led_select, alphasort);
441 if(num_leds <= 0)
442 {
443 fprintf(stderr,"No LEDs present in the system\n");
444
445 sd_bus_slot_unref(led_slot);
446 sd_bus_unref(bus_type);
447 return rc;
448 }
449
450 /* Fully qualified Dbus object for a particular LED */
451 char led_object[128] = {0};
452 int len = 0;
453
454 /* For each led present, announce the service on dbus. */
455 while(num_leds--)
456 {
457 memset(led_object, 0x0, sizeof(led_object));
458
459 len = snprintf(led_object, sizeof(led_object), "%s%s",
460 "/org/openbmc/control/led/", led_list[num_leds]->d_name);
461
462 if(len >= sizeof(led_object))
463 {
464 fprintf(stderr, "Error. LED object is too long:[%d]\n",len);
465 rc = -1;
466 break;
467 }
468
469 /* Install the object */
470 rc = sd_bus_add_object_vtable(bus_type,
471 &led_slot,
vishwa98730f02016-02-22 01:50:04 -0600472 led_object, /* object path */
Adriana Kobylak9c751042016-02-09 13:44:32 -0600473 "org.openbmc.Led", /* interface name */
474 led_control_vtable,
475 NULL);
476
477 if (rc < 0)
478 {
479 fprintf(stderr, "Failed to add object to dbus: %s\n", strerror(-rc));
480 break;
481 }
482 }
483
484 /* Done with all registration. */
485 while (count_leds > 0)
486 {
487 free(led_list[--count_leds]);
Adriana Kobylak9c751042016-02-09 13:44:32 -0600488 }
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600489 free(led_list);
Adriana Kobylak9c751042016-02-09 13:44:32 -0600490
491 /* If we had success in adding the providers, request for a bus name. */
492 if(rc == 0)
493 {
494 /* Take one in OpenBmc */
495 rc = sd_bus_request_name(bus_type, "org.openbmc.control.led", 0);
496 if (rc < 0)
497 {
498 fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-rc));
499 }
500 else
501 {
502 for (;;)
503 {
504 /* Process requests */
505 rc = sd_bus_process(bus_type, NULL);
506 if (rc < 0)
507 {
508 fprintf(stderr, "Failed to process bus: %s\n", strerror(-rc));
509 break;
510 }
511 if (rc > 0)
512 {
513 continue;
514 }
515
516 rc = sd_bus_wait(bus_type, (uint64_t) - 1);
517 if (rc < 0)
518 {
519 fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-rc));
520 break;
521 }
522 }
523 }
524 }
525 sd_bus_slot_unref(led_slot);
526 sd_bus_unref(bus_type);
527
528 return rc;
Norman James772232e2015-10-18 14:43:28 -0500529}
530
Adriana Kobylak9c751042016-02-09 13:44:32 -0600531int main(void)
Norman James877dd412015-10-31 17:33:25 -0500532{
Adriana Kobylak9c751042016-02-09 13:44:32 -0600533 int rc = 0;
Norman James877dd412015-10-31 17:33:25 -0500534
Adriana Kobylak9c751042016-02-09 13:44:32 -0600535 /* This call is not supposed to return. If it does, then an error */
536 rc = start_led_services();
537 if(rc < 0)
538 {
539 fprintf(stderr, "Error starting LED Services. Exiting");
540 }
Norman James772232e2015-10-18 14:43:28 -0500541
Adriana Kobylak9c751042016-02-09 13:44:32 -0600542 return rc;
Norman James772232e2015-10-18 14:43:28 -0500543}