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