events.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. ipecamera_debug(API, "ipecamera: start streaming");
  28. ctx->streaming = 1;
  29. ctx->run_streamer = 1;
  30. if (!ctx->started) {
  31. err = ipecamera_start(vctx, PCILIB_EVENTS_ALL, PCILIB_EVENT_FLAGS_DEFAULT);
  32. if (err) {
  33. ctx->streaming = 0;
  34. return err;
  35. }
  36. do_stop = 1;
  37. }
  38. if (ctx->parse_data) {
  39. // This loop iterates while the generation
  40. while ((run_flag)&&((ctx->run_streamer)||(ctx->reported_id != ctx->event_id))) {
  41. #ifdef IPECAMERA_ANNOUNCE_READY
  42. while (((!ctx->preproc)&&(ctx->reported_id != ctx->event_id))||((ctx->preproc)&&(ctx->reported_id != ctx->preproc_id))) {
  43. #else /* IPECAMERA_ANNOUNCE_READY */
  44. while (ctx->reported_id != ctx->event_id) {
  45. #endif /* IPECAMERA_ANNOUNCE_READY */
  46. 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);
  47. else ++ctx->reported_id;
  48. memcpy(&info, ctx->frame + ((ctx->reported_id-1)%ctx->buffer_size), sizeof(ipecamera_event_info_t));
  49. if ((ctx->event_id - ctx->reported_id) < ctx->buffer_size) {
  50. res = callback(ctx->reported_id, (pcilib_event_info_t*)&info, user);
  51. if (res <= 0) {
  52. if (res < 0) err = -res;
  53. run_flag = 0;
  54. break;
  55. }
  56. }
  57. }
  58. usleep(IPECAMERA_NOFRAME_SLEEP);
  59. }
  60. } else {
  61. while ((run_flag)&&(ctx->run_streamer)) {
  62. usleep(IPECAMERA_NOFRAME_SLEEP);
  63. }
  64. }
  65. ctx->streaming = 0;
  66. ipecamera_debug(API, "ipecamera: streaming finished");
  67. if (do_stop) {
  68. ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
  69. }
  70. return err;
  71. }
  72. 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) {
  73. struct timeval tv;
  74. ipecamera_t *ctx = (ipecamera_t*)vctx;
  75. if (!ctx) {
  76. pcilib_error("IPECamera imaging is not initialized");
  77. return PCILIB_ERROR_NOTINITIALIZED;
  78. }
  79. if (!ctx->started) {
  80. pcilib_error("IPECamera is not in grabbing mode");
  81. return PCILIB_ERROR_INVALID_REQUEST;
  82. }
  83. if (!ctx->parse_data) {
  84. pcilib_error("RAWData only mode is requested");
  85. return PCILIB_ERROR_INVALID_REQUEST;
  86. }
  87. ipecamera_debug(API, "ipecamera: next_event");
  88. #ifdef IPECAMERA_ANNOUNCE_READY
  89. if (((!ctx->preproc)&&(ctx->reported_id == ctx->event_id))||((ctx->preproc)&&(ctx->reported_id == ctx->preproc_id))) {
  90. #else /* IPECAMERA_ANNOUNCE_READY */
  91. if (ctx->reported_id == ctx->event_id) {
  92. #endif /* IPECAMERA_ANNOUNCE_READY */
  93. if (timeout) {
  94. if (timeout == PCILIB_TIMEOUT_INFINITE) {
  95. #ifdef IPECAMERA_ANNOUNCE_READY
  96. while ((ctx->started)&&(((!ctx->preproc)&&(ctx->reported_id == ctx->event_id))||((ctx->preproc)&&(ctx->reported_id == ctx->preproc_id)))) {
  97. #else /* IPECAMERA_ANNOUNCE_READY */
  98. while ((ctx->started)&&(ctx->reported_id == ctx->event_id)) {
  99. #endif /* IPECAMERA_ANNOUNCE_READY */
  100. usleep(IPECAMERA_NOFRAME_SLEEP);
  101. }
  102. } else {
  103. pcilib_calc_deadline(&tv, timeout);
  104. #ifdef IPECAMERA_ANNOUNCE_READY
  105. 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)))) {
  106. #else /* IPECAMERA_ANNOUNCE_READY */
  107. while ((ctx->started)&&(pcilib_calc_time_to_deadline(&tv) > 0)&&(ctx->reported_id == ctx->event_id)) {
  108. #endif /* IPECAMERA_ANNOUNCE_READY */
  109. usleep(IPECAMERA_NOFRAME_SLEEP);
  110. }
  111. }
  112. }
  113. if (ctx->reported_id == ctx->event_id) {
  114. ipecamera_debug(API, "ipecamera: next_event timed out");
  115. return PCILIB_ERROR_TIMEOUT;
  116. }
  117. }
  118. retry:
  119. 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);
  120. else ++ctx->reported_id;
  121. if (evid) *evid = ctx->reported_id;
  122. if (info) {
  123. if (info_size >= sizeof(ipecamera_event_info_t))
  124. memcpy(info, ctx->frame + ((ctx->reported_id-1)%ctx->buffer_size), sizeof(ipecamera_event_info_t));
  125. else if (info_size >= sizeof(pcilib_event_info_t))
  126. memcpy(info, ctx->frame + ((ctx->reported_id-1)%ctx->buffer_size), sizeof(pcilib_event_info_t));
  127. else {
  128. ipecamera_debug(API, "ipecamera: next_event returned a error");
  129. return PCILIB_ERROR_INVALID_ARGUMENT;
  130. }
  131. }
  132. if ((ctx->event_id - ctx->reported_id) >= ctx->buffer_size) goto retry;
  133. ipecamera_debug(API, "ipecamera: next_event returned");
  134. return 0;
  135. }