main.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. int main(int argc, const char * argv[])
  13. {
  14. int fd;
  15. size_t len;
  16. char *outfile, *datadir;
  17. struct stat statbuf;
  18. unsigned char data;
  19. // Construct the output file path
  20. len = confstr(_CS_DARWIN_USER_TEMP_DIR, NULL, (size_t) 0);
  21. len += sizeof(DATA_DIR);
  22. datadir = malloc(len);
  23. outfile = malloc(len + sizeof(OUT_FILE));
  24. if (datadir == NULL || outfile == NULL) {
  25. fprintf(stderr, "Out of memory\n");
  26. exit(1);
  27. }
  28. confstr(_CS_DARWIN_USER_TEMP_DIR, datadir, len);
  29. strlcat(datadir, DATA_DIR, len);
  30. strlcpy(outfile, datadir, len);
  31. strlcat(outfile, OUT_FILE, len + sizeof(OUT_FILE));
  32. // Create the datadir and outfile as needed
  33. stat(datadir, &statbuf);
  34. if (!S_ISDIR(statbuf.st_mode)) {
  35. fprintf(stderr, "Creating data directory: %s\n", datadir);
  36. if (mkdir(datadir, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
  37. fprintf(stderr, "Error creating data directory (%s): %s\n", strerror(errno), outfile);
  38. exit(1);
  39. }
  40. }
  41. stat(outfile, &statbuf);
  42. if(!S_ISREG(statbuf.st_mode)) {
  43. fprintf(stderr, "Creating output file: %s\n", outfile);
  44. fd = open(outfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  45. if (fd <= 0 || close(fd) != 0) {
  46. fprintf(stderr, "Error creating output file (%s): %s\n", strerror(errno), outfile);
  47. exit(1);
  48. }
  49. }
  50. // Grab a USBmicroX context
  51. USBmX_ContextRef ctx = USBmX_Create();
  52. USBmX_Begin(ctx);
  53. // Find the specified device
  54. USBmX_DeviceRef device = USBmX_DeviceWithSerial(ctx, SERIAL);
  55. if (!device) {
  56. fprintf(stderr, "Device not found: %s\n", SERIAL);
  57. exit(1);
  58. }
  59. // Monitor for USB timeouts
  60. signal(SIGALRM, timeout);
  61. // Read forever
  62. while (1) {
  63. // Read from the device
  64. alarm(TIMEOUT);
  65. if (USBmX_ReadA(device, &data) != 0) {
  66. fprintf(stderr, "Device read error\n");
  67. exit(1);
  68. }
  69. alarm(0);
  70. // Pin A5 is high when we've got motion
  71. if (data & 0x20) {
  72. #ifdef DEBUG
  73. printf("Motion detected\n");
  74. #endif
  75. if (utimes(outfile, NULL) != 0) {
  76. if (errno == ENOENT) {
  77. } else {
  78. fprintf(stderr, "Error touching output file (%s): %s\n", strerror(errno), outfile);
  79. }
  80. }
  81. } else {
  82. #ifdef DEBUG
  83. printf("\n");
  84. #endif
  85. }
  86. // Sleep and loop
  87. usleep(SLEEP_DELAY);
  88. }
  89. // Cleanup
  90. USBmX_Destroy(ctx);
  91. return 0;
  92. }
  93. // Catch the timeout
  94. void timeout(int sig) {
  95. signal(sig, SIG_IGN);
  96. fprintf(stderr, "Timeout waiting for USB read\n");
  97. exit(1);
  98. }