uledmon.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #include <fcntl.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #include <unistd.h>
  17. #include <linux/uleds.h>
  18. int main(int argc, char const *argv[])
  19. {
  20. struct uleds_user_dev uleds_dev;
  21. int fd, ret;
  22. int brightness;
  23. struct timespec ts;
  24. if (argc != 2) {
  25. fprintf(stderr, "Requires <device-name> argument\n");
  26. return 1;
  27. }
  28. strncpy(uleds_dev.name, argv[1], LED_MAX_NAME_SIZE);
  29. uleds_dev.max_brightness = 100;
  30. fd = open("/dev/uleds", O_RDWR);
  31. if (fd == -1) {
  32. perror("Failed to open /dev/uleds");
  33. return 1;
  34. }
  35. ret = write(fd, &uleds_dev, sizeof(uleds_dev));
  36. if (ret == -1) {
  37. perror("Failed to write to /dev/uleds");
  38. close(fd);
  39. return 1;
  40. }
  41. while (1) {
  42. ret = read(fd, &brightness, sizeof(brightness));
  43. if (ret == -1) {
  44. perror("Failed to read from /dev/uleds");
  45. close(fd);
  46. return 1;
  47. }
  48. clock_gettime(CLOCK_MONOTONIC, &ts);
  49. printf("[%ld.%09ld] %u\n", ts.tv_sec, ts.tv_nsec, brightness);
  50. }
  51. close(fd);
  52. return 0;
  53. }