events.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #define _BSD_SOURCE
  2. #define _GNU_SOURCE
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <sys/time.h>
  8. #include <pthread.h>
  9. #include <assert.h>
  10. #include <ufodecode.h>
  11. #include <pcilib.h>
  12. #include <pcilib/tools.h>
  13. #include <pcilib/error.h>
  14. #include "ipecamera.h"
  15. #include "private.h"
  16. #include "events.h"
  17. int ipecamera_stream(pcilib_context_t *vctx, pcilib_event_callback_t callback, void *user) {
  18. int run_flag = 1;
  19. int res, err = 0;
  20. int do_stop = 0;
  21. ipecamera_event_info_t info;
  22. ipecamera_t *ctx = (ipecamera_t*)vctx;
  23. if (!ctx) {
  24. pcilib_error("IPECamera imaging is not initialized");
  25. return PCILIB_ERROR_NOTINITIALIZED;
  26. }
  27. ctx->streaming = 1;
  28. ctx->run_streamer = 1;
  29. if (!ctx->started) {
  30. err = ipecamera_start(vctx, PCILIB_EVENTS_ALL, PCILIB_EVENT_FLAGS_DEFAULT);
  31. if (err) {
  32. ctx->streaming = 0;
  33. return err;
  34. }
  35. do_stop = 1;
  36. }
  37. if (ctx->parse_data) {
  38. // This loop iterates while the generation
  39. while ((run_flag)&&((ctx->run_streamer)||(ctx->reported_id != ctx->event_id))) {
  40. #ifdef IPECAMERA_ANNOUNCE_READY
  41. while (((!ctx->preproc)&&(ctx->reported_id != ctx->event_id))||((ctx->preproc)&&(ctx->reported_id != ctx->preproc_id))) {
  42. #else /* IPECAMERA_ANNOUNCE_READY */
  43. while (ctx->reported_id != ctx->event_id) {
  44. #endif /* IPECAMERA_ANNOUNCE_READY */
  45. if ((ctx->event_id - ctx->reported_id) > (ctx->buffer_size - IPECAMERA_RESERVE_BUFFERS)) ctx->reported_id = ctx->event_id - (ctx->buffer_size - 1 - IPECAMERA_RESERVE_BUFFERS);
  46. else ++ctx->reported_id;
  47. memcpy(&info, ctx->frame + ((ctx->reported_id-1)%ctx->buffer_size), sizeof(ipecamera_event_info_t));
  48. if ((ctx->event_id - ctx->reported_id) < ctx->buffer_size) {
  49. res = callback(ctx->reported_id, (pcilib_event_info_t*)&info, user);
  50. if (res <= 0) {
  51. if (res < 0) err = -res;
  52. run_flag = 0;
  53. break;
  54. }
  55. }
  56. }
  57. usleep(IPECAMERA_NOFRAME_SLEEP);
  58. }
  59. } else {
  60. while ((run_flag)&&(ctx->run_streamer)) {
  61. usleep(IPECAMERA_NOFRAME_SLEEP);
  62. }
  63. }
  64. ctx->streaming = 0;
  65. if (do_stop) {
  66. ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
  67. }
  68. return err;
  69. }
  70. int ipecamera_next_event(pcilib_context_t *vctx, pcilib_timeout_t timeout, pcilib_event_id_t *evid, size_t info_size, pcilib_event_info_t *info) {
  71. struct timeval tv;
  72. ipecamera_t *ctx = (ipecamera_t*)vctx;
  73. if (!ctx) {
  74. pcilib_error("IPECamera imaging is not initialized");
  75. return PCILIB_ERROR_NOTINITIALIZED;
  76. }
  77. if (!ctx->started) {
  78. pcilib_error("IPECamera is not in grabbing mode");
  79. return PCILIB_ERROR_INVALID_REQUEST;
  80. }
  81. if (!ctx->parse_data) {
  82. pcilib_error("RAWData only mode is requested");
  83. return PCILIB_ERROR_INVALID_REQUEST;
  84. }
  85. #ifdef IPECAMERA_ANNOUNCE_READY
  86. if (((!ctx->preproc)&&(ctx->reported_id == ctx->event_id))||((ctx->preproc)&&(ctx->reported_id == ctx->preproc_id))) {
  87. #else /* IPECAMERA_ANNOUNCE_READY */
  88. if (ctx->reported_id == ctx->event_id) {
  89. #endif /* IPECAMERA_ANNOUNCE_READY */
  90. if (timeout) {
  91. if (timeout == PCILIB_TIMEOUT_INFINITE) {
  92. #ifdef IPECAMERA_ANNOUNCE_READY
  93. while ((ctx->started)&&(((!ctx->preproc)&&(ctx->reported_id == ctx->event_id))||((ctx->preproc)&&(ctx->reported_id == ctx->preproc_id)))) {
  94. #else /* IPECAMERA_ANNOUNCE_READY */
  95. while ((ctx->started)&&(ctx->reported_id == ctx->event_id)) {
  96. #endif /* IPECAMERA_ANNOUNCE_READY */
  97. usleep(IPECAMERA_NOFRAME_SLEEP);
  98. }
  99. } else {
  100. pcilib_calc_deadline(&tv, timeout);
  101. #ifdef IPECAMERA_ANNOUNCE_READY
  102. while ((ctx->started)&&(pcilib_calc_time_to_deadline(&tv) > 0)&&(((!ctx->preproc)&&(ctx->reported_id == ctx->event_id))||((ctx->preproc)&&(ctx->reported_id == ctx->preproc_id)))) {
  103. #else /* IPECAMERA_ANNOUNCE_READY */
  104. while ((ctx->started)&&(pcilib_calc_time_to_deadline(&tv) > 0)&&(ctx->reported_id == ctx->event_id)) {
  105. #endif /* IPECAMERA_ANNOUNCE_READY */
  106. usleep(IPECAMERA_NOFRAME_SLEEP);
  107. }
  108. }
  109. }
  110. if (ctx->reported_id == ctx->event_id) {
  111. return PCILIB_ERROR_TIMEOUT;
  112. }
  113. }
  114. retry:
  115. if ((ctx->event_id - ctx->reported_id) > (ctx->buffer_size - IPECAMERA_RESERVE_BUFFERS)) ctx->reported_id = ctx->event_id - (ctx->buffer_size - 1 - IPECAMERA_RESERVE_BUFFERS);
  116. else ++ctx->reported_id;
  117. if (evid) *evid = ctx->reported_id;
  118. if (info) {
  119. if (info_size >= sizeof(ipecamera_event_info_t))
  120. memcpy(info, ctx->frame + ((ctx->reported_id-1)%ctx->buffer_size), sizeof(ipecamera_event_info_t));
  121. else if (info_size >= sizeof(pcilib_event_info_t))
  122. memcpy(info, ctx->frame + ((ctx->reported_id-1)%ctx->buffer_size), sizeof(pcilib_event_info_t));
  123. else
  124. return PCILIB_ERROR_INVALID_ARGUMENT;
  125. }
  126. if ((ctx->event_id - ctx->reported_id) >= ctx->buffer_size) goto retry;
  127. return 0;
  128. }