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