blob: b4536aa0130eaa6cfaab784d0f60e8f68ea99ff8 [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 }
125 else if(strcmp(led_function, "GetLedState") == 0)
126 {
127 char value_str[10] = {0};
128 const char *led_state = NULL;
129
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600130 rc = read_led(led_name, power_ctrl, value_str, sizeof(value_str)-1);
Adriana Kobylak9c751042016-02-09 13:44:32 -0600131 if(rc >= 0)
132 {
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600133 /* LED is active HI */
134 led_state = strtoul(value_str, NULL, 0) ? "On" : "Off";
Adriana Kobylak9c751042016-02-09 13:44:32 -0600135 }
136 return sd_bus_reply_method_return(msg, "is", rc, led_state);
137 }
138 else
139 {
140 fprintf(stderr,"Invalid LED function:[%s]\n",led_function);
141 }
142
143 return sd_bus_reply_method_return(msg, "i", rc);
144}
145
146/*
147 * --------------------------------------------------------------
148 * Turn On or Turn Off the LED
149 * --------------------------------------------------------------
150 */
151int led_stable_state_function(char *led_name, char *led_function)
152{
153 /* Generic error reporter. */
154 int rc = -1;
155
156 const char *value = NULL;
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600157 if(strcmp(led_function, "setOff") == 0)
Adriana Kobylak9c751042016-02-09 13:44:32 -0600158 {
159 /* LED active low */
160 value = "0";
161 }
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600162 else if(strcmp(led_function, "setOn") == 0)
Adriana Kobylak9c751042016-02-09 13:44:32 -0600163 {
164 value = "255";
165 }
166 else
167 {
168 fprintf(stderr,"Invalid LED stable state operation:[%s] \n",led_function);
169 return rc;
170 }
171
172 /*
173 * Before doing anything, need to turn off the blinking
174 * if there is one in progress by writing 'none' to trigger
175 */
176 rc = write_to_led(led_name, blink_ctrl, "none");
177 if(rc < 0)
178 {
179 fprintf(stderr,"Error disabling blink. Function:[%s]\n", led_function);
180 return rc;
181 }
182
183 /*
184 * Open the brightness file and write corresponding values.
185 */
186 rc = write_to_led(led_name, power_ctrl, value);
187 if(rc < 0)
188 {
189 fprintf(stderr,"Error driving LED. Function:[%s]\n", led_function);
190 }
191
192 return rc;
193}
194
195//-----------------------------------------------------------------------------------
196// Given the on and off duration, applies the action on the specified LED.
197//-----------------------------------------------------------------------------------
198int blink_led(const char *led_name, const char *on_duration, const char *off_duration)
199{
200 /* Generic error reporter */
201 int rc = -1;
202
203 /* Protocol demands that 'timer' be echoed to 'trigger' */
204 rc = write_to_led(led_name, blink_ctrl, "timer");
205 if(rc < 0)
206 {
207 fprintf(stderr,"Error writing timer to Led:[%s]\n", led_name);
208 return rc;
209 }
210
211 /*
212 * After writing 'timer to 'trigger', 2 new files get generated namely
213 *'delay_on' and 'delay_off' which are telling the time duration for a
214 * particular LED on and off.
215 */
216 rc = write_to_led(led_name, duty_on, on_duration);
217 if(rc < 0)
218 {
219 fprintf(stderr,"Error writing [%s] to delay_on:[%s]\n",on_duration,led_name);
220 return rc;
221 }
222
223 rc = write_to_led(led_name, duty_off, off_duration);
224 if(rc < 0)
225 {
226 fprintf(stderr,"Error writing [%s] to delay_off:[%s]\n",off_duration,led_name);
227 }
228
229 return rc;
230}
231
232/*
233 * ----------------------------------------------------
234 * Default blink action on the LED.
235 * ----------------------------------------------------
236 */
237int led_default_blink(char *led_name, char *blink_type)
238{
239 /* Generic error reporter */
240 int rc = -1;
241
242 /* How long the LED needs to be in on and off state while blinking */
243 const char *on_duration = NULL;
244 const char *off_duration = NULL;
245 if(strcmp(blink_type, "setBlinkSlow") == 0)
246 {
247 //*Delay 900 millisec before 'on' and delay 900 millisec before off */
248 on_duration = "900";
249 off_duration = "900";
250 }
251 else if(strcmp(blink_type, "setBlinkFast") == 0)
252 {
253 /* Delay 200 millisec before 'on' and delay 200 millisec before off */
254 on_duration = "200";
255 off_duration = "200";
256 }
257 else
258 {
259 fprintf(stderr,"Invalid blink operation:[%s]\n",blink_type);
260 return rc;
261 }
262
263 rc = blink_led(led_name, on_duration, off_duration);
264
265 return rc;
266}
267
268/*
269 * ---------------------------------------------------------------
270 * Gets the current value of passed in LED file
271 * Mainly used for reading 'brightness'
272 * NOTE : It is the responsibility of the caller to allocate
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600273 * sufficient space for buffer. This will read upto user supplied
Adriana Kobylak9c751042016-02-09 13:44:32 -0600274 * size -or- entire contents of file whichever is smaller
275 * ----------------------------------------------------------------
276 */
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600277int read_led(const char *name, const char *ctrl_file,
Adriana Kobylak9c751042016-02-09 13:44:32 -0600278 void *value, const size_t len)
279{
280 /* Generic error reporter. */
281 int rc = -1;
282 int count = 0;
283
284 if(value == NULL || len <= 0)
285 {
286 fprintf(stderr, "Invalid buffer passed to LED read\n");
287 return rc;
288 }
289
290 /* To get /sys/class/leds/<name>/<control file> */
291 char led_path[128] = {0};
292
293 int led_len = 0;
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600294 led_len = snprintf(led_path, sizeof(led_path),
Adriana Kobylak9c751042016-02-09 13:44:32 -0600295 "/sys/class/leds/%s/%s",name, ctrl_file);
296 if(led_len >= sizeof(led_path))
297 {
298 fprintf(stderr, "Error. LED path is too long. :[%d]\n",led_len);
299 return rc;
300 }
301
302 FILE *fp = fopen(led_path,"rb");
303 if(fp == NULL)
304 {
305 perror("Error:");
306 fprintf(stderr,"Error opening:[%s]\n",led_path);
307 return rc;
308 }
309
310 char *sysfs_value = (char *)value;
311 while(!feof(fp) && (count < len))
312 {
313 sysfs_value[count++] = fgetc(fp);
314 }
Adriana Kobylak9c751042016-02-09 13:44:32 -0600315
316 fclose(fp);
317 return 0;
318}
319
320/*
321 * -----------------------------------------------
322 * Dbus Services offered by this LED controller
323 * -----------------------------------------------
324 */
325static const sd_bus_vtable led_control_vtable[] =
326{
327 SD_BUS_VTABLE_START(0),
328 SD_BUS_METHOD("setOn", "", "i", &led_function_router, SD_BUS_VTABLE_UNPRIVILEGED),
329 SD_BUS_METHOD("setOff", "", "i", &led_function_router, SD_BUS_VTABLE_UNPRIVILEGED),
330 SD_BUS_METHOD("setBlinkFast", "", "i", &led_function_router, SD_BUS_VTABLE_UNPRIVILEGED),
331 SD_BUS_METHOD("setBlinkSlow", "", "i", &led_function_router, SD_BUS_VTABLE_UNPRIVILEGED),
332 SD_BUS_METHOD("GetLedState", "", "is", &led_function_router, SD_BUS_VTABLE_UNPRIVILEGED),
333 SD_BUS_VTABLE_END,
Norman James772232e2015-10-18 14:43:28 -0500334};
335
Adriana Kobylak9c751042016-02-09 13:44:32 -0600336/*
337 * ---------------------------------------------
338 * Interested in all files except standard ones
339 * ---------------------------------------------
340 */
341int led_select(const struct dirent *entry)
Norman James772232e2015-10-18 14:43:28 -0500342{
Adriana Kobylak9c751042016-02-09 13:44:32 -0600343 if( (strcmp(entry->d_name, ".") == 0) ||
344 (strcmp(entry->d_name, "..") == 0))
345 {
346 return 0;
347 }
348 return 1;
Norman James772232e2015-10-18 14:43:28 -0500349}
350
Adriana Kobylak9c751042016-02-09 13:44:32 -0600351/*
352 * ------------------------------------------------
353 * Called as part of setting up skeleton services.
354 * -----------------------------------------------
355 */
356int start_led_services()
Norman James772232e2015-10-18 14:43:28 -0500357{
Adriana Kobylak9c751042016-02-09 13:44:32 -0600358 /* Generic error reporter. */
359 int rc = -1;
360 int num_leds = 0;
361 int count_leds = 0;
362
363 /* Bus and slot where we are offering the LED dbus service. */
364 sd_bus *bus_type = NULL;
365 sd_bus_slot *led_slot = NULL;
366
367 /* For walking '/sys/class/leds/' looking for names of LED.*/
368 struct dirent **led_list;
369
370 /* Get a hook onto system bus. */
371 rc = sd_bus_open_system(&bus_type);
372 if(rc < 0)
373 {
374 fprintf(stderr,"Error opening system bus.\n");
375 return rc;
376 }
377
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600378 count_leds = num_leds = scandir("/sys/class/leds/",
Adriana Kobylak9c751042016-02-09 13:44:32 -0600379 &led_list, led_select, alphasort);
380 if(num_leds <= 0)
381 {
382 fprintf(stderr,"No LEDs present in the system\n");
383
384 sd_bus_slot_unref(led_slot);
385 sd_bus_unref(bus_type);
386 return rc;
387 }
388
389 /* Fully qualified Dbus object for a particular LED */
390 char led_object[128] = {0};
391 int len = 0;
392
393 /* For each led present, announce the service on dbus. */
394 while(num_leds--)
395 {
396 memset(led_object, 0x0, sizeof(led_object));
397
398 len = snprintf(led_object, sizeof(led_object), "%s%s",
399 "/org/openbmc/control/led/", led_list[num_leds]->d_name);
400
401 if(len >= sizeof(led_object))
402 {
403 fprintf(stderr, "Error. LED object is too long:[%d]\n",len);
404 rc = -1;
405 break;
406 }
407
408 /* Install the object */
409 rc = sd_bus_add_object_vtable(bus_type,
410 &led_slot,
411 led_object, /* object path */
412 "org.openbmc.Led", /* interface name */
413 led_control_vtable,
414 NULL);
415
416 if (rc < 0)
417 {
418 fprintf(stderr, "Failed to add object to dbus: %s\n", strerror(-rc));
419 break;
420 }
421 }
422
423 /* Done with all registration. */
424 while (count_leds > 0)
425 {
426 free(led_list[--count_leds]);
Adriana Kobylak9c751042016-02-09 13:44:32 -0600427 }
Adriana Kobylakcc2be262016-02-09 16:50:14 -0600428 free(led_list);
Adriana Kobylak9c751042016-02-09 13:44:32 -0600429
430 /* If we had success in adding the providers, request for a bus name. */
431 if(rc == 0)
432 {
433 /* Take one in OpenBmc */
434 rc = sd_bus_request_name(bus_type, "org.openbmc.control.led", 0);
435 if (rc < 0)
436 {
437 fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-rc));
438 }
439 else
440 {
441 for (;;)
442 {
443 /* Process requests */
444 rc = sd_bus_process(bus_type, NULL);
445 if (rc < 0)
446 {
447 fprintf(stderr, "Failed to process bus: %s\n", strerror(-rc));
448 break;
449 }
450 if (rc > 0)
451 {
452 continue;
453 }
454
455 rc = sd_bus_wait(bus_type, (uint64_t) - 1);
456 if (rc < 0)
457 {
458 fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-rc));
459 break;
460 }
461 }
462 }
463 }
464 sd_bus_slot_unref(led_slot);
465 sd_bus_unref(bus_type);
466
467 return rc;
Norman James772232e2015-10-18 14:43:28 -0500468}
469
Adriana Kobylak9c751042016-02-09 13:44:32 -0600470int main(void)
Norman James877dd412015-10-31 17:33:25 -0500471{
Adriana Kobylak9c751042016-02-09 13:44:32 -0600472 int rc = 0;
Norman James877dd412015-10-31 17:33:25 -0500473
Adriana Kobylak9c751042016-02-09 13:44:32 -0600474 /* This call is not supposed to return. If it does, then an error */
475 rc = start_led_services();
476 if(rc < 0)
477 {
478 fprintf(stderr, "Error starting LED Services. Exiting");
479 }
Norman James772232e2015-10-18 14:43:28 -0500480
Adriana Kobylak9c751042016-02-09 13:44:32 -0600481 return rc;
Norman James772232e2015-10-18 14:43:28 -0500482}