reader.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #define _DEFAULT_SOURCE
  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 <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <pthread.h>
  12. #include <assert.h>
  13. #include <ufodecode.h>
  14. #include <pcilib.h>
  15. #include <pcilib/tools.h>
  16. #include <pcilib/error.h>
  17. #include <pcilib/timing.h>
  18. #include "model.h"
  19. #include "private.h"
  20. #include "reader.h"
  21. #define GET_REG(reg, var) \
  22. if (!err) { \
  23. err = pcilib_read_register_by_id(pcilib, ctx->reg, &var); \
  24. if (err) { \
  25. pcilib_error("Error reading %s register", model_info->registers[ctx->reg].name); \
  26. } \
  27. }
  28. #define SET_REG(reg, val) \
  29. if (!err) { \
  30. err = pcilib_write_register_by_id(pcilib, ctx->reg, val); \
  31. if (err) { \
  32. pcilib_error("Error writting %s register", model_info->registers[ctx->reg].name); \
  33. } \
  34. }
  35. /*
  36. #define CHECK_FRAME_MAGIC(buf) \
  37. memcmp(buf, ((void*)frame_magic) + 1, sizeof(frame_magic) - 1)
  38. */
  39. #define CHECK_FRAME_MAGIC(buf) \
  40. memcmp(((ipecamera_payload_t*)(buf)) + 1, &frame_magic[1], sizeof(frame_magic) - sizeof(ipecamera_payload_t))
  41. static ipecamera_payload_t frame_magic[3] = { 0x51111111, 0x52222222, 0x53333333 };
  42. int ipecamera_compute_buffer_size(ipecamera_t *ctx, ipecamera_format_t format, size_t header_size, size_t lines) {
  43. // const size_t header_size = 8 * sizeof(ipecamera_payload_t);
  44. const size_t footer_size = CMOSIS_FRAME_TAIL_SIZE;
  45. size_t max_channels;
  46. size_t line_size, raw_size, padded_blocks;
  47. switch (format) {
  48. case IPECAMERA_FORMAT_CMOSIS:
  49. case IPECAMERA_FORMAT_CMOSIS20:
  50. max_channels = CMOSIS_MAX_CHANNELS;
  51. line_size = ctx->data_line_size;
  52. break;
  53. default:
  54. pcilib_warning("Unsupported version (%u) of frame format...", format);
  55. return PCILIB_ERROR_NOTSUPPORTED;
  56. }
  57. raw_size = lines * line_size;
  58. raw_size *= max_channels / ctx->cmosis_outputs;
  59. raw_size += header_size + footer_size;
  60. #ifdef IPECAMERA_BUG_MISSING_PAYLOAD
  61. // As I understand, the first 32-byte packet is missing, so we need to substract 32 (both CMOSIS and CMOSIS20)
  62. raw_size -= 32;
  63. #endif /* IPECAMERA_BUG_MISSING_PAYLOAD */
  64. padded_blocks = raw_size / IPECAMERA_DMA_PACKET_LENGTH + ((raw_size % IPECAMERA_DMA_PACKET_LENGTH)?1:0);
  65. ctx->roi_raw_size = raw_size;
  66. ctx->roi_padded_size = padded_blocks * IPECAMERA_DMA_PACKET_LENGTH;
  67. return 0;
  68. }
  69. static int ipecamera_parse_header(ipecamera_t *ctx, ipecamera_payload_t *buf, size_t buf_size) {
  70. int err;
  71. int last = buf[0] & 1;
  72. int version = (buf[0] >> 1) & 7;
  73. size_t size = 0, n_lines;
  74. ipecamera_format_t format = IPECAMERA_FORMAT_CMOSIS;
  75. switch (version) {
  76. case 0:
  77. n_lines = ((uint32_t*)buf)[5] & 0x7FF;
  78. ctx->frame[ctx->buffer_pos].event.info.seqnum = buf[6] & 0xFFFFFF;
  79. ctx->frame[ctx->buffer_pos].event.info.offset = (buf[7] & 0xFFFFFF) * 80;
  80. break;
  81. case 1:
  82. n_lines = ((uint32_t*)buf)[5] & 0xFFFF;
  83. if (!n_lines) {
  84. pcilib_error("The frame header claims 0 lines in the data");
  85. return 0;
  86. }
  87. ctx->frame[ctx->buffer_pos].event.info.seqnum = buf[6] & 0xFFFFFF;
  88. ctx->frame[ctx->buffer_pos].event.info.offset = (buf[7] & 0xFFFFFF) * 80;
  89. format = (buf[6] >> 24)&0x0F;
  90. break;
  91. default:
  92. ipecamera_debug(HARDWARE, "Incorrect version of the frame header, ignoring broken data...");
  93. return 0;
  94. }
  95. gettimeofday(&ctx->frame[ctx->buffer_pos].event.info.timestamp, NULL);
  96. ipecamera_debug(FRAME_HEADERS, "frame %lu: %x %x %x %x", ctx->frame[ctx->buffer_pos].event.info.seqnum, buf[0], buf[1], buf[2], buf[3]);
  97. ipecamera_debug(FRAME_HEADERS, "frame %lu: %x %x %x %x", ctx->frame[ctx->buffer_pos].event.info.seqnum, buf[4], buf[5], buf[6], buf[7]);
  98. while ((!last)&&((size + CMOSIS_FRAME_HEADER_SIZE) <= buf_size)) {
  99. size += CMOSIS_FRAME_HEADER_SIZE;
  100. last = buf[size] & 1;
  101. }
  102. size += CMOSIS_FRAME_HEADER_SIZE;
  103. err = ipecamera_compute_buffer_size(ctx, format, size, n_lines);
  104. if (err) return 0;
  105. // Returns total size of found headers or 0 on the error
  106. return size;
  107. }
  108. static inline int ipecamera_new_frame(ipecamera_t *ctx) {
  109. ctx->frame[ctx->buffer_pos].event.raw_size = ctx->cur_size;
  110. if (ctx->cur_size < ctx->roi_raw_size) {
  111. ctx->frame[ctx->buffer_pos].event.info.flags |= PCILIB_EVENT_INFO_FLAG_BROKEN;
  112. }
  113. ctx->buffer_pos = (++ctx->event_id) % ctx->buffer_size;
  114. ctx->cur_size = 0;
  115. ctx->frame[ctx->buffer_pos].event.info.type = PCILIB_EVENT0;
  116. ctx->frame[ctx->buffer_pos].event.info.flags = 0;
  117. ctx->frame[ctx->buffer_pos].event.image_ready = 0;
  118. if ((ctx->event_id == ctx->autostop.evid)&&(ctx->event_id)) {
  119. ctx->run_reader = 0;
  120. return 1;
  121. }
  122. if (pcilib_check_deadline(&ctx->autostop.timestamp, 0)) {
  123. ctx->run_reader = 0;
  124. return 1;
  125. }
  126. return 0;
  127. }
  128. static int ipecamera_data_callback(void *user, pcilib_dma_flags_t flags, size_t bufsize, void *buf) {
  129. int res;
  130. int eof = 0;
  131. static unsigned long packet_id = 0;
  132. #ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
  133. size_t real_size;
  134. size_t extra_data = 0;
  135. #endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
  136. ipecamera_t *ctx = (ipecamera_t*)user;
  137. #if defined(IPECAMERA_BUG_INCOMPLETE_PACKETS)||defined(IPECAMERA_BUG_MULTIFRAME_PACKETS)
  138. static pcilib_event_id_t invalid_frame_id = (pcilib_event_id_t)-1;
  139. #endif
  140. packet_id++;
  141. ipecamera_debug_buffer(RAW_PACKETS, bufsize, buf, PCILIB_DEBUG_BUFFER_MKDIR, "frame%4lu/frame%9lu", ctx->event_id, packet_id);
  142. if (!ctx->cur_size) {
  143. #ifdef IPECAMERA_BUG_MULTIFRAME_HEADERS
  144. if (ctx->saved_header_size) {
  145. void *buf2 = alloca(ctx->saved_header_size + bufsize);
  146. if (!buf2) {
  147. pcilib_error("Error allocating %zu bytes of memory in stack", ctx->saved_header_size + bufsize);
  148. return -PCILIB_ERROR_MEMORY;
  149. }
  150. memcpy(buf2, ctx->saved_header, ctx->saved_header_size);
  151. memcpy(buf2 + ctx->saved_header_size, buf, bufsize);
  152. buf = buf2;
  153. bufsize += ctx->saved_header_size;
  154. ctx->saved_header_size = 0;
  155. }
  156. #endif /* IPECAMERA_BUG_MULTIFRAME_HEADERS */
  157. #if defined(IPECAMERA_BUG_INCOMPLETE_PACKETS)||defined(IPECAMERA_BUG_MULTIFRAME_PACKETS)
  158. size_t startpos;
  159. for (startpos = 0; (startpos + CMOSIS_ENTITY_SIZE) <= bufsize; startpos += sizeof(ipecamera_payload_t)) {
  160. if (!CHECK_FRAME_MAGIC(buf + startpos)) break;
  161. }
  162. if ((startpos + CMOSIS_ENTITY_SIZE) > bufsize) {
  163. ipecamera_debug_buffer(RAW_PACKETS, bufsize, NULL, 0, "frame%4lu/frame%9lu.invalid", ctx->event_id, packet_id);
  164. if (invalid_frame_id != ctx->event_id) {
  165. ipecamera_debug(HARDWARE, "No frame magic in DMA packet of %u bytes, current event %lu", bufsize, ctx->event_id);
  166. invalid_frame_id = ctx->event_id;
  167. }
  168. return PCILIB_STREAMING_CONTINUE;
  169. }
  170. if (startpos) {
  171. // pass padding to rawdata callback
  172. if (ctx->event.params.rawdata.callback) {
  173. res = ctx->event.params.rawdata.callback(0, NULL, PCILIB_EVENT_FLAG_RAW_DATA_ONLY, startpos, buf, ctx->event.params.rawdata.user);
  174. if (res <= 0) {
  175. if (res < 0) return res;
  176. ctx->run_reader = 0;
  177. }
  178. }
  179. buf += startpos;
  180. bufsize -= startpos;
  181. }
  182. #endif /* IPECAMERA_BUG_INCOMPLETE_PACKETS */
  183. if ((bufsize >= CMOSIS_FRAME_HEADER_SIZE)&&(!CHECK_FRAME_MAGIC(buf))) {
  184. // We should handle the case when multi-header is split between multiple DMA packets
  185. if (!ipecamera_parse_header(ctx, buf, bufsize))
  186. return PCILIB_STREAMING_CONTINUE;
  187. #ifdef IPECAMERA_BUG_MULTIFRAME_HEADERS
  188. } else if ((bufsize >= CMOSIS_ENTITY_SIZE)&&(!CHECK_FRAME_MAGIC(buf))) {
  189. memcpy(ctx->saved_header, buf, bufsize);
  190. ctx->saved_header_size = bufsize;
  191. return PCILIB_STREAMING_REQ_FRAGMENT;
  192. #endif /* IPECAMERA_BUG_MULTIFRAME_HEADERS */
  193. } else {
  194. ipecamera_debug(HARDWARE, "Frame magic is not found in the remaining DMA packet consisting of %u bytes, ignoring broken data...", bufsize);
  195. return PCILIB_STREAMING_CONTINUE;
  196. }
  197. }
  198. #ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
  199. // for rawdata_callback with complete padding
  200. real_size = bufsize;
  201. if (ctx->cur_size + bufsize > ctx->roi_raw_size) {
  202. size_t need;
  203. for (need = ctx->roi_raw_size - ctx->cur_size; (need + CMOSIS_ENTITY_SIZE) <= bufsize; need += sizeof(uint32_t)) {
  204. if (!CHECK_FRAME_MAGIC(buf + need)) break;
  205. }
  206. if ((need + CMOSIS_ENTITY_SIZE) <= bufsize) {
  207. extra_data = bufsize - need;
  208. eof = 1;
  209. }
  210. // just rip of padding
  211. bufsize = ctx->roi_raw_size - ctx->cur_size;
  212. ipecamera_debug_buffer(RAW_PACKETS, bufsize, buf, 0, "frame%4lu/frame%9lu.partial", ctx->event_id, packet_id);
  213. }
  214. #endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
  215. if (ctx->parse_data) {
  216. if (ctx->cur_size + bufsize > ctx->padded_size) {
  217. pcilib_error("Unexpected event data, we are expecting at maximum (%zu) bytes, but (%zu) already read", ctx->padded_size, ctx->cur_size + bufsize);
  218. return -PCILIB_ERROR_TOOBIG;
  219. }
  220. if (bufsize) {
  221. #ifdef IPECAMERA_BUG_REPEATING_DATA
  222. if ((bufsize > 16)&&(ctx->cur_size > 16)) {
  223. if (!memcmp(ctx->buffer + ctx->buffer_pos * ctx->padded_size + ctx->cur_size - 16, buf, 16)) {
  224. pcilib_warning("Skipping repeating bytes at offset %zu of frame %zu", ctx->cur_size, ctx->event_id);
  225. buf += 16;
  226. bufsize -=16;
  227. }
  228. }
  229. #endif /* IPECAMERA_BUG_REPEATING_DATA */
  230. memcpy(ctx->buffer + ctx->buffer_pos * ctx->padded_size + ctx->cur_size, buf, bufsize);
  231. }
  232. }
  233. ctx->cur_size += bufsize;
  234. if (ctx->cur_size >= ctx->roi_raw_size) {
  235. eof = 1;
  236. }
  237. if (ctx->event.params.rawdata.callback) {
  238. 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);
  239. if (res <= 0) {
  240. if (res < 0) return res;
  241. ctx->run_reader = 0;
  242. }
  243. }
  244. if (eof) {
  245. if ((ipecamera_new_frame(ctx))||(!ctx->run_reader)) {
  246. return PCILIB_STREAMING_STOP;
  247. }
  248. #ifdef IPECAMERA_BUG_MULTIFRAME_PACKETS
  249. if (extra_data) {
  250. return ipecamera_data_callback(user, flags, extra_data, buf + (real_size - extra_data));
  251. }
  252. #endif /* IPECAMERA_BUG_MULTIFRAME_PACKETS */
  253. }
  254. return PCILIB_STREAMING_REQ_FRAGMENT;
  255. }
  256. void *ipecamera_reader_thread(void *user) {
  257. int err;
  258. ipecamera_t *ctx = (ipecamera_t*)user;
  259. #ifdef IPECAMERA_BUG_STUCKED_BUSY
  260. pcilib_register_value_t saved, value;
  261. pcilib_t *pcilib = ctx->event.pcilib;
  262. const pcilib_model_description_t *model_info = pcilib_get_model_description(pcilib);
  263. #endif /* IPECAMERA_BUG_STUCKED_BUSY */
  264. while (ctx->run_reader) {
  265. err = pcilib_stream_dma(ctx->event.pcilib, ctx->rdma, 0, 0, PCILIB_DMA_FLAG_MULTIPACKET, IPECAMERA_DMA_TIMEOUT, &ipecamera_data_callback, user);
  266. if (err) {
  267. if (err == PCILIB_ERROR_TIMEOUT) {
  268. if (ctx->cur_size >= ctx->roi_raw_size) ipecamera_new_frame(ctx);
  269. #ifdef IPECAMERA_BUG_INCOMPLETE_PACKETS
  270. else if (ctx->cur_size > 0) ipecamera_new_frame(ctx);
  271. #endif /* IPECAMERA_BUG_INCOMPLETE_PACKETS */
  272. if (pcilib_check_deadline(&ctx->autostop.timestamp, 0)) {
  273. ctx->run_reader = 0;
  274. break;
  275. }
  276. #ifdef IPECAMERA_BUG_STUCKED_BUSY
  277. GET_REG(status2_reg, value);
  278. if ((!err)&&(value&0x2FFFFFFF)) {
  279. pcilib_warning("Camera stuck in busy, trying to recover...");
  280. GET_REG(control_reg, saved);
  281. SET_REG(control_reg, IPECAMERA_IDLE|(saved&0xFFFF0000));
  282. while ((value&0x2FFFFFFF)&&(ctx->run_reader)) {
  283. usleep(IPECAMERA_NOFRAME_SLEEP);
  284. }
  285. }
  286. #endif /* IPECAMERA_BUG_STUCKED_BUSY */
  287. usleep(IPECAMERA_NOFRAME_SLEEP);
  288. } else pcilib_error("DMA error while reading IPECamera frames, error: %i", err);
  289. }
  290. }
  291. ctx->run_streamer = 0;
  292. if (ctx->cur_size)
  293. pcilib_info("partialy read frame after stop signal, %zu bytes in the buffer", ctx->cur_size);
  294. return NULL;
  295. }