uledmon.c 1.3 KB

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