1 // SPDX-License-Identifier: GPL-2.0
3 * led_hw_brightness_mon.c
5 * This program monitors LED brightness level changes having its origin
6 * in hardware/firmware, i.e. outside of kernel control.
7 * A timestamp and brightness value is printed each time the brightness changes.
9 * Usage: led_hw_brightness_mon <device-name>
11 * <device-name> is the name of the LED class device to be monitored. Pressing
24 #include <linux/uleds.h>
26 int main(int argc, char const *argv[])
29 char brightness_file_path[LED_MAX_NAME_SIZE + 11];
35 fprintf(stderr, "Requires <device-name> argument\n");
39 snprintf(brightness_file_path, LED_MAX_NAME_SIZE,
40 "/sys/class/leds/%s/brightness_hw_changed", argv[1]);
42 fd = open(brightness_file_path, O_RDONLY);
44 printf("Failed to open %s file\n", brightness_file_path);
49 * read may fail if no hw brightness change has occurred so far,
50 * but it is required to avoid spurious poll notifications in
53 read(fd, buf, sizeof(buf));
56 pollfd.events = POLLPRI;
59 ret = poll(&pollfd, 1, -1);
61 printf("Failed to poll %s file (%d)\n",
62 brightness_file_path, ret);
67 clock_gettime(CLOCK_MONOTONIC, &ts);
69 ret = read(fd, buf, sizeof(buf));
73 ret = lseek(pollfd.fd, 0, SEEK_SET);
75 printf("lseek failed (%d)\n", ret);
79 printf("[%ld.%09ld] %d\n", ts.tv_sec, ts.tv_nsec, atoi(buf));