main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. i = NUM_DEVICES;
  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. if (errno == ENOENT) {
  51. } else {
  52. fprintf(stderr, "Error touching output file (%s): %s\n", strerror(errno), outfile);
  53. }
  54. }
  55. } else {
  56. #ifdef DEBUG
  57. printf("\n");
  58. #endif
  59. }
  60. // Delay and loop
  61. usleep(SLEEP_DELAY);
  62. }
  63. // Cleanup (we never get here)
  64. USBmX_Destroy(ctx);
  65. return 0;
  66. }
  67. // Catch the timeout
  68. void timeout(int sig) {
  69. signal(sig, SIG_IGN);
  70. fprintf(stderr, "Timeout waiting for USB read\n");
  71. exit(1);
  72. }
  73. // Setup the USBMicro context and build refs to our specified devices
  74. USBmX_DeviceRef init(const char *serial) {
  75. USBmX_DeviceRef device;
  76. // Create a new USBmicroX context as needed
  77. if (!ctx) {
  78. ctx = USBmX_Create();
  79. USBmX_Begin(ctx);
  80. }
  81. // Find the specified device
  82. device = USBmX_DeviceWithSerial(ctx, serial);
  83. if (!device) {
  84. fprintf(stderr, "Device not found: %s\n", serial);
  85. exit(1);
  86. }
  87. // Return device handle
  88. return device;
  89. }
  90. // Read the specified USBMicro device
  91. u8 readDev(USBmX_DeviceRef dev) {
  92. unsigned char data = 0x00;
  93. // Enable the timeout
  94. alarm(TIMEOUT);
  95. // Read from the device
  96. if (USBmX_ReadA(dev, &data) != 0) {
  97. fprintf(stderr, "Device read error\n");
  98. exit(1);
  99. }
  100. // Clear the timeout
  101. alarm(0);
  102. // Return pin A5
  103. return (data & 0x20);
  104. }
  105. // Setup the data directory and output file
  106. char * initPaths() {
  107. int fd;
  108. size_t len;
  109. char *datadir, *outfile;
  110. struct stat statbuf;
  111. // Construct the data directory and output file paths
  112. len = confstr(_CS_DARWIN_USER_TEMP_DIR, NULL, (size_t) 0);
  113. len += sizeof(DATA_DIR);
  114. datadir = malloc(len);
  115. outfile = malloc(len + sizeof(OUT_FILE));
  116. if (datadir == NULL || outfile == NULL) {
  117. fprintf(stderr, "Out of memory\n");
  118. exit(1);
  119. }
  120. confstr(_CS_DARWIN_USER_TEMP_DIR, datadir, len);
  121. strlcat(datadir, DATA_DIR, len);
  122. strlcpy(outfile, datadir, len);
  123. strlcat(outfile, OUT_FILE, len + sizeof(OUT_FILE));
  124. // Create the datadir as needed
  125. stat(datadir, &statbuf);
  126. if (!S_ISDIR(statbuf.st_mode)) {
  127. fprintf(stderr, "Creating data directory: %s\n", datadir);
  128. if (mkdir(datadir, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
  129. fprintf(stderr, "Error creating data directory (%s): %s\n", strerror(errno), outfile);
  130. exit(1);
  131. }
  132. }
  133. // Create the output file as needed
  134. stat(outfile, &statbuf);
  135. if(!S_ISREG(statbuf.st_mode)) {
  136. fprintf(stderr, "Creating output file: %s\n", outfile);
  137. fd = open(outfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  138. if (fd <= 0 || close(fd) != 0) {
  139. fprintf(stderr, "Error creating output file (%s): %s\n", strerror(errno), outfile);
  140. exit(1);
  141. }
  142. }
  143. // Return the output file path
  144. return outfile;
  145. }