reader.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #define _IPECAMERA_IMAGE_C
  2. #define _BSD_SOURCE
  3. #define _GNU_SOURCE
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <sys/time.h>
  9. #include <pthread.h>
  10. #include <assert.h>
  11. #include <ufodecode.h>
  12. #include "../tools.h"
  13. #include "../error.h"
  14. #include "pcilib.h"
  15. #include "private.h"
  16. #include "reader.h"
  17. static inline int ipecamera_new_frame(ipecamera_t *ctx) {
  18. ctx->frame[ctx->buffer_pos].event.raw_size = ctx->cur_size;
  19. if (ctx->cur_size < ctx->raw_size) ctx->frame[ctx->buffer_pos].event.info.flags |= PCILIB_EVENT_INFO_FLAG_BROKEN;
  20. ctx->buffer_pos = (++ctx->event_id) % ctx->buffer_size;
  21. ctx->cur_size = 0;
  22. ctx->frame[ctx->buffer_pos].event.info.type = PCILIB_EVENT0;
  23. ctx->frame[ctx->buffer_pos].event.info.flags = 0;
  24. ctx->frame[ctx->buffer_pos].event.image_ready = 0;
  25. if ((ctx->event_id == ctx->autostop.evid)&&(ctx->event_id)) {
  26. ctx->run_reader = 0;
  27. return 1;
  28. }
  29. if (pcilib_check_deadline(&ctx->autostop.timestamp, PCILIB_DMA_TIMEOUT)) {
  30. ctx->run_reader = 0;
  31. return 1;
  32. }
  33. return 0;
  34. }
  35. static uint32_t frame_magic[6] = { 0x51111111, 0x52222222, 0x53333333, 0x54444444, 0x55555555, 0x56666666 };
  36. static int ipecamera_data_callback(void *user, pcilib_dma_flags_t flags, size_t bufsize, void *buf) {
  37. int res;
  38. int eof = 0;
  39. #ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
  40. size_t extra_data = 0;
  41. #endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
  42. ipecamera_t *ctx = (ipecamera_t*)user;
  43. if (!ctx->cur_size) {
  44. #if defined(IPECAMERA_BUG_INCOMPLETE_PACKETS)||defined(IPECAMERA_BUG_MULTIFRAME_PACKETS)
  45. size_t startpos;
  46. for (startpos = 0; (startpos + 8) < bufsize; startpos++) {
  47. if (!memcmp(buf + startpos, frame_magic, sizeof(frame_magic))) break;
  48. }
  49. if (startpos) {
  50. buf += startpos;
  51. bufsize -= startpos;
  52. }
  53. #endif /* IPECAMERA_BUG_INCOMPLETE_PACKETS */
  54. if ((bufsize >= 8)&&(!memcmp(buf, frame_magic, sizeof(frame_magic)))) {
  55. /*
  56. // Not implemented in hardware yet
  57. ctx->frame[ctx->buffer_pos].event.info.seqnum = ((uint32_t*)buf)[6] & 0xF0000000;
  58. ctx->frame[ctx->buffer_pos].event.info.offset = ((uint32_t*)buf)[7] & 0xF0000000;
  59. */
  60. gettimeofday(&ctx->frame[ctx->buffer_pos].event.info.timestamp, NULL);
  61. } else {
  62. // pcilib_warning("Frame magic is not found, ignoring broken data...");
  63. return PCILIB_STREAMING_CONTINUE;
  64. }
  65. }
  66. #ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
  67. if (ctx->cur_size + bufsize > ctx->raw_size) {
  68. size_t need;
  69. for (need = ctx->raw_size - ctx->cur_size; need < bufsize; need += sizeof(uint32_t)) {
  70. if (*(uint32_t*)(buf + need) == frame_magic[0]) break;
  71. }
  72. if (need < bufsize) {
  73. extra_data = bufsize - need;
  74. //bufsize = need;
  75. eof = 1;
  76. }
  77. // just rip of padding
  78. bufsize = ctx->raw_size - ctx->cur_size;
  79. }
  80. #endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
  81. if (ctx->parse_data) {
  82. if (ctx->cur_size + bufsize > ctx->full_size) {
  83. pcilib_error("Unexpected event data, we are expecting at maximum (%zu) bytes, but (%zu) already read", ctx->full_size, ctx->cur_size + bufsize);
  84. return -PCILIB_ERROR_TOOBIG;
  85. }
  86. memcpy(ctx->buffer + ctx->buffer_pos * ctx->padded_size + ctx->cur_size, buf, bufsize);
  87. }
  88. ctx->cur_size += bufsize;
  89. // printf("%i: %i %i\n", ctx->buffer_pos, ctx->cur_size, bufsize);
  90. if (ctx->cur_size >= ctx->full_size) eof = 1;
  91. if (ctx->event.params.rawdata.callback) {
  92. res = ctx->event.params.rawdata.callback(ctx->event_id, (pcilib_event_info_t*)(ctx->frame + ctx->buffer_pos), (eof?PCILIB_EVENT_FLAG_EOF:PCILIB_EVENT_FLAGS_DEFAULT), bufsize, buf, ctx->event.params.rawdata.user);
  93. if (res <= 0) {
  94. if (res < 0) return res;
  95. ctx->run_reader = 0;
  96. }
  97. }
  98. if (eof) {
  99. if (ipecamera_new_frame(ctx)) {
  100. return PCILIB_STREAMING_STOP;
  101. }
  102. #ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
  103. if (extra_data) {
  104. return ipecamera_data_callback(user, flags, extra_data, buf + bufsize);
  105. }
  106. #endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
  107. }
  108. return PCILIB_STREAMING_REQ_FRAGMENT;
  109. }
  110. void *ipecamera_reader_thread(void *user) {
  111. int err;
  112. ipecamera_t *ctx = (ipecamera_t*)user;
  113. while (ctx->run_reader) {
  114. err = pcilib_stream_dma(ctx->event.pcilib, ctx->rdma, 0, 0, PCILIB_DMA_FLAG_MULTIPACKET, PCILIB_DMA_TIMEOUT, &ipecamera_data_callback, user);
  115. if (err) {
  116. if (err == PCILIB_ERROR_TIMEOUT) {
  117. if (ctx->cur_size > ctx->raw_size) ipecamera_new_frame(ctx);
  118. #ifdef IPECAMERA_BUG_INCOMPLETE_PACKETS
  119. else if (ctx->cur_size > 0) ipecamera_new_frame(ctx);
  120. #endif /* IPECAMERA_BUG_INCOMPLETE_PACKETS */
  121. if (pcilib_check_deadline(&ctx->autostop.timestamp, PCILIB_DMA_TIMEOUT)) {
  122. ctx->run_reader = 0;
  123. break;
  124. }
  125. usleep(IPECAMERA_NOFRAME_SLEEP);
  126. } else pcilib_error("DMA error while reading IPECamera frames, error: %i", err);
  127. } else printf("no error\n");
  128. //usleep(1000);
  129. }
  130. ctx->run_streamer = 0;
  131. if (ctx->cur_size)
  132. pcilib_error("partialy read frame after stop signal, %zu bytes in the buffer", ctx->cur_size);
  133. return NULL;
  134. }