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