main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "USBMicroX.h"
  2. #include <fcntl.h>
  3. #include <sys/stat.h>
  4. #define DATA_DIR "plexMonitor/"
  5. #define OUT_FILE "MOTION"
  6. #define SERIAL "101128160542"
  7. #define SLEEP_DELAY 250000
  8. #define TIMEOUT 2
  9. //#define DEBUG
  10. // Prototypes
  11. void timeout(int sig);
  12. USBmX_DeviceRef init();
  13. unsigned char readDev(USBmX_DeviceRef dev);
  14. // Globals
  15. USBmX_ContextRef ctx;
  16. int main(int argc, const char * argv[])
  17. {
  18. USBmX_DeviceRef device;
  19. int fd;
  20. size_t len;
  21. char *datadir, *outfile;
  22. struct stat statbuf;
  23. // Construct the data directory and output file paths
  24. len = confstr(_CS_DARWIN_USER_TEMP_DIR, NULL, (size_t) 0);
  25. len += sizeof(DATA_DIR);
  26. datadir = malloc(len);
  27. outfile = malloc(len + sizeof(OUT_FILE));
  28. if (datadir == NULL || outfile == NULL) {
  29. fprintf(stderr, "Out of memory\n");
  30. exit(1);
  31. }
  32. confstr(_CS_DARWIN_USER_TEMP_DIR, datadir, len);
  33. strlcat(datadir, DATA_DIR, len);
  34. strlcpy(outfile, datadir, len);
  35. strlcat(outfile, OUT_FILE, len + sizeof(OUT_FILE));
  36. // Create the datadir as needed
  37. stat(datadir, &statbuf);
  38. if (!S_ISDIR(statbuf.st_mode)) {
  39. fprintf(stderr, "Creating data directory: %s\n", datadir);
  40. if (mkdir(datadir, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
  41. fprintf(stderr, "Error creating data directory (%s): %s\n", strerror(errno), outfile);
  42. exit(1);
  43. }
  44. }
  45. // Create the output file as needed
  46. stat(outfile, &statbuf);
  47. if(!S_ISREG(statbuf.st_mode)) {
  48. fprintf(stderr, "Creating output file: %s\n", outfile);
  49. fd = open(outfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  50. if (fd <= 0 || close(fd) != 0) {
  51. fprintf(stderr, "Error creating output file (%s): %s\n", strerror(errno), outfile);
  52. exit(1);
  53. }
  54. }
  55. // Monitor for alarms
  56. signal(SIGALRM, timeout);
  57. // Setup the USB device
  58. device = init(SERIAL);
  59. // Read forever
  60. while (1) {
  61. // If we detected motion
  62. if (readDev(device)) {
  63. #ifdef DEBUG
  64. printf("Motion detected\n");
  65. #endif
  66. if (utimes(outfile, NULL) != 0) {
  67. if (errno == ENOENT) {
  68. } else {
  69. fprintf(stderr, "Error touching output file (%s): %s\n", strerror(errno), outfile);
  70. }
  71. }
  72. } else {
  73. #ifdef DEBUG
  74. printf("\n");
  75. #endif
  76. }
  77. // Delay and loop
  78. usleep(SLEEP_DELAY);
  79. }
  80. // Cleanup (we never get here)
  81. USBmX_Destroy(ctx);
  82. return 0;
  83. }
  84. // Catch the timeout
  85. void timeout(int sig) {
  86. signal(sig, SIG_IGN);
  87. fprintf(stderr, "Timeout waiting for USB read\n");
  88. exit(1);
  89. }
  90. // Setup the USBMicro context and build refs to our specified devices
  91. USBmX_DeviceRef init(const char *serial) {
  92. USBmX_DeviceRef device;
  93. // Create a new USBmicroX context as needed
  94. if (!ctx) {
  95. ctx = USBmX_Create();
  96. USBmX_Begin(ctx);
  97. }
  98. // Find the specified device
  99. device = USBmX_DeviceWithSerial(ctx, serial);
  100. if (!device) {
  101. fprintf(stderr, "Device not found: %s\n", serial);
  102. exit(1);
  103. }
  104. // Return device handles
  105. return device;
  106. }
  107. // Read the specified USBMicro device
  108. unsigned char readDev(USBmX_DeviceRef dev) {
  109. unsigned char data = 0x00;
  110. // Enable the timeout
  111. alarm(TIMEOUT);
  112. // Read from the device
  113. if (USBmX_ReadA(dev, &data) != 0) {
  114. fprintf(stderr, "Device read error\n");
  115. exit(1);
  116. }
  117. // Clear the timeout
  118. alarm(0);
  119. // Return pin A5
  120. return (data & 0x20);
  121. }