main.c 3.6 KB

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