Skip to content

Commit ae34732

Browse files
tools/leds: Add led_hw_brightness_mon program
LED subsystem supports POLLPRI on "brightness_hw_changed" sysfs file of LED class devices. This tool demonstrates how to use the feature. Signed-off-by: Jacek Anaszewski <jacek.anaszewski@gmail.com> Acked-by: Hans de Goede <hdegoede@redhat.com> Acked-by: Pavel Machek <pavel@ucw.cz>
1 parent 0cb8eb3 commit ae34732

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

‎tools/leds/Makefile‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
CC = $(CROSS_COMPILE)gcc
44
CFLAGS = -Wall -Wextra -g -I../../include/uapi
55

6-
all: uledmon
6+
all: uledmon led_hw_brightness_mon
77
%: %.c
88
$(CC) $(CFLAGS) -o $@ $^
99

1010
clean:
11-
$(RM) uledmon
11+
$(RM) uledmon led_hw_brightness_mon
1212

1313
.PHONY: all clean

‎tools/leds/led_hw_brightness_mon.c‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* led_hw_brightness_mon.c
3+
*
4+
* This program monitors LED brightness level changes having its origin
5+
* in hardware/firmware, i.e. outside of kernel control.
6+
* A timestamp and brightness value is printed each time the brightness changes.
7+
*
8+
* Usage: led_hw_brightness_mon <device-name>
9+
*
10+
* <device-name> is the name of the LED class device to be monitored. Pressing
11+
* CTRL+C will exit.
12+
*/
13+
14+
#include <errno.h>
15+
#include <fcntl.h>
16+
#include <poll.h>
17+
#include <stdio.h>
18+
#include <stdlib.h>
19+
#include <string.h>
20+
#include <time.h>
21+
#include <unistd.h>
22+
23+
#include <linux/uleds.h>
24+
25+
int main(int argc, char const *argv[])
26+
{
27+
int fd, ret;
28+
char brightness_file_path[LED_MAX_NAME_SIZE + 11];
29+
struct pollfd pollfd;
30+
struct timespec ts;
31+
char buf[11];
32+
33+
if (argc != 2) {
34+
fprintf(stderr, "Requires <device-name> argument\n");
35+
return 1;
36+
}
37+
38+
snprintf(brightness_file_path, LED_MAX_NAME_SIZE,
39+
"/sys/class/leds/%s/brightness_hw_changed", argv[1]);
40+
41+
fd = open(brightness_file_path, O_RDONLY);
42+
if (fd == -1) {
43+
printf("Failed to open %s file\n", brightness_file_path);
44+
return 1;
45+
}
46+
47+
/*
48+
* read may fail if no hw brightness change has occurred so far,
49+
* but it is required to avoid spurious poll notifications in
50+
* the opposite case.
51+
*/
52+
read(fd, buf, sizeof(buf));
53+
54+
pollfd.fd = fd;
55+
pollfd.events = POLLPRI;
56+
57+
while (1) {
58+
ret = poll(&pollfd, 1, -1);
59+
if (ret == -1) {
60+
printf("Failed to poll %s file (%d)\n",
61+
brightness_file_path, ret);
62+
ret = 1;
63+
break;
64+
}
65+
66+
clock_gettime(CLOCK_MONOTONIC, &ts);
67+
68+
ret = read(fd, buf, sizeof(buf));
69+
if (ret < 0)
70+
break;
71+
72+
ret = lseek(pollfd.fd, 0, SEEK_SET);
73+
if (ret < 0) {
74+
printf("lseek failed (%d)\n", ret);
75+
break;
76+
}
77+
78+
printf("[%ld.%09ld] %d\n", ts.tv_sec, ts.tv_nsec, atoi(buf));
79+
}
80+
81+
close(fd);
82+
83+
return ret;
84+
}

0 commit comments

Comments
 (0)