Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 1 | #include <stdio.h>
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 2 | #include <stdlib.h>
|
| 3 | #include <errno.h>
|
| 4 | #include <string.h>
|
| 5 | #include <dirent.h>
|
| 6 | #include <systemd/sd-bus.h>
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 7 |
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 8 | /*
|
| 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 | */
|
| 13 | const char *power_ctrl = "brightness";
|
| 14 | const char *blink_ctrl = "trigger";
|
| 15 | const char *duty_on = "delay_on";
|
| 16 | const char *duty_off = "delay_off";
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 17 |
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 18 | /*
|
| 19 | * --------------------------------------------------
|
| 20 | * Given the dbus path, returns the name of the LED
|
| 21 | * --------------------------------------------------
|
| 22 | */
|
| 23 | char *get_led_name(const char *dbus_path)
|
| 24 | {
|
| 25 | char *led_name = NULL;
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 26 |
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 27 | /* 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 James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 33 |
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 34 | return led_name;
|
| 35 | }
|
| 36 |
|
| 37 | /*
|
| 38 | * -------------------------------------------------------------------------
|
| 39 | * Writes the 'on / off / blink' trigger to leds.
|
| 40 | * -------------------------------------------------------------------------
|
| 41 | */
|
| 42 | int 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 Kobylak | cc2be26 | 2016-02-09 16:50:14 -0600 | [diff] [blame] | 51 | len = snprintf(led_path, sizeof(led_path),
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 52 | "/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 Kobylak | cc2be26 | 2016-02-09 16:50:14 -0600 | [diff] [blame] | 62 | fprintf(stderr,"Error:[%s] opening:[%s]\n",strerror(errno),led_path);
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 63 | return rc;
|
| 64 | }
|
| 65 |
|
| 66 | rc = fwrite(value, strlen(value), 1, fp);
|
| 67 | if(rc != 1)
|
| 68 | {
|
Adriana Kobylak | cc2be26 | 2016-02-09 16:50:14 -0600 | [diff] [blame] | 69 | fprintf(stderr, "Error:[%s] writing to :[%s]\n",strerror(errno),led_path);
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 70 | }
|
| 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 | */
|
| 83 | static 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 | }
|
vishwa | 98730f0 | 2016-02-22 01:50:04 -0600 | [diff] [blame] | 125 | 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 Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 130 | else if(strcmp(led_function, "GetLedState") == 0)
|
| 131 | {
|
| 132 | char value_str[10] = {0};
|
| 133 | const char *led_state = NULL;
|
| 134 |
|
Adriana Kobylak | cc2be26 | 2016-02-09 16:50:14 -0600 | [diff] [blame] | 135 | rc = read_led(led_name, power_ctrl, value_str, sizeof(value_str)-1);
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 136 | if(rc >= 0)
|
| 137 | {
|
Adriana Kobylak | cc2be26 | 2016-02-09 16:50:14 -0600 | [diff] [blame] | 138 | /* LED is active HI */
|
| 139 | led_state = strtoul(value_str, NULL, 0) ? "On" : "Off";
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 140 | }
|
| 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 | */
|
| 156 | int 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 Kobylak | cc2be26 | 2016-02-09 16:50:14 -0600 | [diff] [blame] | 162 | if(strcmp(led_function, "setOff") == 0)
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 163 | {
|
| 164 | /* LED active low */
|
| 165 | value = "0";
|
| 166 | }
|
Adriana Kobylak | cc2be26 | 2016-02-09 16:50:14 -0600 | [diff] [blame] | 167 | else if(strcmp(led_function, "setOn") == 0)
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 168 | {
|
| 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 | //-----------------------------------------------------------------------------------
|
| 203 | int 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 | */
|
| 242 | int 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 | /*
|
vishwa | 98730f0 | 2016-02-22 01:50:04 -0600 | [diff] [blame] | 274 | * -------------------------------------------------
|
| 275 | * Blinks at user defined 'on' and 'off' intervals.
|
| 276 | * -------------------------------------------------
|
| 277 | */
|
| 278 | int 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 Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 330 | * ---------------------------------------------------------------
|
| 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 Kobylak | cc2be26 | 2016-02-09 16:50:14 -0600 | [diff] [blame] | 334 | * sufficient space for buffer. This will read upto user supplied
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 335 | * size -or- entire contents of file whichever is smaller
|
| 336 | * ----------------------------------------------------------------
|
| 337 | */
|
Adriana Kobylak | cc2be26 | 2016-02-09 16:50:14 -0600 | [diff] [blame] | 338 | int read_led(const char *name, const char *ctrl_file,
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 339 | 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 Kobylak | cc2be26 | 2016-02-09 16:50:14 -0600 | [diff] [blame] | 355 | led_len = snprintf(led_path, sizeof(led_path),
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 356 | "/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 | {
|
vishwa | 98730f0 | 2016-02-22 01:50:04 -0600 | [diff] [blame] | 366 | fprintf(stderr,"Error:[%s] opening:[%s]\n",strerror(errno),led_path);
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 367 | 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 Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 375 |
|
| 376 | fclose(fp);
|
| 377 | return 0;
|
| 378 | }
|
| 379 |
|
| 380 | /*
|
| 381 | * -----------------------------------------------
|
| 382 | * Dbus Services offered by this LED controller
|
| 383 | * -----------------------------------------------
|
| 384 | */
|
| 385 | static 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),
|
vishwa | 98730f0 | 2016-02-22 01:50:04 -0600 | [diff] [blame] | 393 | SD_BUS_METHOD("BlinkCustom", "uu", "i", &led_function_router, SD_BUS_VTABLE_UNPRIVILEGED),
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 394 | SD_BUS_VTABLE_END,
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 395 | };
|
| 396 |
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 397 | /*
|
| 398 | * ---------------------------------------------
|
| 399 | * Interested in all files except standard ones
|
| 400 | * ---------------------------------------------
|
| 401 | */
|
| 402 | int led_select(const struct dirent *entry)
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 403 | {
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 404 | if( (strcmp(entry->d_name, ".") == 0) ||
|
| 405 | (strcmp(entry->d_name, "..") == 0))
|
| 406 | {
|
| 407 | return 0;
|
| 408 | }
|
| 409 | return 1;
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 410 | }
|
| 411 |
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 412 | /*
|
| 413 | * ------------------------------------------------
|
| 414 | * Called as part of setting up skeleton services.
|
| 415 | * -----------------------------------------------
|
| 416 | */
|
| 417 | int start_led_services()
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 418 | {
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 419 | /* 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 Kobylak | cc2be26 | 2016-02-09 16:50:14 -0600 | [diff] [blame] | 439 | count_leds = num_leds = scandir("/sys/class/leds/",
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 440 | &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,
|
vishwa | 98730f0 | 2016-02-22 01:50:04 -0600 | [diff] [blame] | 472 | led_object, /* object path */
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 473 | "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 Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 488 | }
|
Adriana Kobylak | cc2be26 | 2016-02-09 16:50:14 -0600 | [diff] [blame] | 489 | free(led_list);
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 490 |
|
| 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 James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 529 | }
|
| 530 |
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 531 | int main(void)
|
Norman James | 877dd41 | 2015-10-31 17:33:25 -0500 | [diff] [blame] | 532 | {
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 533 | int rc = 0;
|
Norman James | 877dd41 | 2015-10-31 17:33:25 -0500 | [diff] [blame] | 534 |
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 535 | /* 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 James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 541 |
|
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 542 | return rc;
|
Norman James | 772232e | 2015-10-18 14:43:28 -0500 | [diff] [blame] | 543 | }
|