xref: /linux/tools/leds/uledmon.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1 /*
2  * uledmon.c
3  *
4  * This program creates a new userspace LED class device and monitors it. A
5  * timestamp and brightness value is printed each time the brightness changes.
6  *
7  * Usage: uledmon <device-name>
8  *
9  * <device-name> is the name of the LED class device to be created. Pressing
10  * CTRL+C will exit.
11  */
12 
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <time.h>
17 #include <unistd.h>
18 
19 #include <linux/uleds.h>
20 
21 int main(int argc, char const *argv[])
22 {
23 	struct uleds_user_dev uleds_dev;
24 	int fd, ret;
25 	int brightness;
26 	struct timespec ts;
27 
28 	if (argc != 2) {
29 		fprintf(stderr, "Requires <device-name> argument\n");
30 		return 1;
31 	}
32 
33 	strncpy(uleds_dev.name, argv[1], LED_MAX_NAME_SIZE);
34 	uleds_dev.max_brightness = 100;
35 
36 	fd = open("/dev/uleds", O_RDWR);
37 	if (fd == -1) {
38 		perror("Failed to open /dev/uleds");
39 		return 1;
40 	}
41 
42 	ret = write(fd, &uleds_dev, sizeof(uleds_dev));
43 	if (ret == -1) {
44 		perror("Failed to write to /dev/uleds");
45 		close(fd);
46 		return 1;
47 	}
48 
49 	while (1) {
50 		ret = read(fd, &brightness, sizeof(brightness));
51 		if (ret == -1) {
52 			perror("Failed to read from /dev/uleds");
53 			close(fd);
54 			return 1;
55 		}
56 		clock_gettime(CLOCK_MONOTONIC, &ts);
57 		printf("[%ld.%09ld] %u\n", ts.tv_sec, ts.tv_nsec, brightness);
58 	}
59 
60 	close(fd);
61 
62 	return 0;
63 }
64