transfer_receiver.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <algorithm>
  5. #include <gtest/gtest.h>
  6. #include <uavcan/transport/transfer_receiver.hpp>
  7. #include "../clock.hpp"
  8. #include "transfer_test_helpers.hpp"
  9. /*
  10. * Beware!
  11. * The code you're about to look at desperately needs some cleaning.
  12. */
  13. enum SotEotToggle
  14. {
  15. SET000 = 0,
  16. SET001 = 1,
  17. SET010 = 2,
  18. SET011 = 3,
  19. SET100 = 4,
  20. SET101 = 5, // Illegal
  21. SET110 = 6,
  22. SET111 = 7 // Illegal
  23. };
  24. struct RxFrameGenerator
  25. {
  26. static const uavcan::TransferBufferManagerKey DEFAULT_KEY;
  27. enum { TARGET_NODE_ID = 126 };
  28. uint16_t data_type_id;
  29. uavcan::TransferBufferManagerKey bufmgr_key;
  30. RxFrameGenerator(uint16_t data_type_id, const uavcan::TransferBufferManagerKey& bufmgr_key = DEFAULT_KEY)
  31. : data_type_id(data_type_id)
  32. , bufmgr_key(bufmgr_key)
  33. { }
  34. /// iface_index, data, set, transfer_id, ts_monotonic [, ts_utc]
  35. uavcan::RxFrame operator()(uint8_t iface_index, const std::string& data, SotEotToggle set,
  36. uint8_t transfer_id, uint64_t ts_monotonic, uint64_t ts_utc = 0)
  37. {
  38. const uavcan::NodeID dst_nid =
  39. (bufmgr_key.getTransferType() == uavcan::TransferTypeMessageBroadcast) ?
  40. uavcan::NodeID::Broadcast : TARGET_NODE_ID;
  41. uavcan::Frame frame(data_type_id, bufmgr_key.getTransferType(), bufmgr_key.getNodeID(),
  42. dst_nid, transfer_id);
  43. frame.setStartOfTransfer((set & (1 << 2)) != 0);
  44. frame.setEndOfTransfer((set & (1 << 1)) != 0);
  45. if ((set & (1 << 0)) != 0)
  46. {
  47. frame.flipToggle();
  48. }
  49. EXPECT_EQ(data.length(),
  50. frame.setPayload(reinterpret_cast<const uint8_t*>(data.c_str()), unsigned(data.length())));
  51. uavcan::RxFrame output(frame, uavcan::MonotonicTime::fromUSec(ts_monotonic),
  52. uavcan::UtcTime::fromUSec(ts_utc), iface_index);
  53. //std::cout << "Generated frame: " << output.toString() << std::endl;
  54. return output;
  55. }
  56. };
  57. const uavcan::TransferBufferManagerKey RxFrameGenerator::DEFAULT_KEY(42, uavcan::TransferTypeMessageBroadcast);
  58. template <unsigned BufSize>
  59. struct Context
  60. {
  61. uavcan::PoolAllocator<uavcan::MemPoolBlockSize * 100, uavcan::MemPoolBlockSize> pool;
  62. uavcan::TransferReceiver receiver; // Must be default constructible and copyable
  63. uavcan::TransferBufferManager bufmgr;
  64. Context() :
  65. bufmgr(BufSize, pool)
  66. { }
  67. ~Context()
  68. {
  69. // We need to destroy the receiver before its buffer manager
  70. receiver = uavcan::TransferReceiver();
  71. }
  72. };
  73. static bool matchBufferContent(const uavcan::ITransferBuffer* tbb, const std::string& content)
  74. {
  75. uint8_t data[1024];
  76. std::fill(data, data + sizeof(data), 0);
  77. if (content.length() > sizeof(data))
  78. {
  79. std::cerr << "matchBufferContent(): Content is too long" << std::endl;
  80. std::exit(1);
  81. }
  82. tbb->read(0, data, unsigned(content.length()));
  83. if (std::equal(content.begin(), content.end(), data))
  84. {
  85. return true;
  86. }
  87. std::cout << "Buffer content mismatch:"
  88. << "\n\tExpected: " << content
  89. << "\n\tActually: " << reinterpret_cast<const char*>(data)
  90. << std::endl;
  91. return false;
  92. }
  93. #define CHECK_NOT_COMPLETE(x) ASSERT_EQ(uavcan::TransferReceiver::ResultNotComplete, (x))
  94. #define CHECK_COMPLETE(x) ASSERT_EQ(uavcan::TransferReceiver::ResultComplete, (x))
  95. #define CHECK_SINGLE_FRAME(x) ASSERT_EQ(uavcan::TransferReceiver::ResultSingleFrame, (x))
  96. TEST(TransferReceiver, Basic)
  97. {
  98. using uavcan::TransferReceiver;
  99. Context<32> context;
  100. RxFrameGenerator gen(789);
  101. uavcan::TransferReceiver& rcv = context.receiver;
  102. uavcan::TransferBufferManager& bufmgr = context.bufmgr;
  103. uavcan::TransferBufferAccessor bk(context.bufmgr, RxFrameGenerator::DEFAULT_KEY);
  104. std::cout << "sizeof(TransferReceiver): " << sizeof(TransferReceiver) << std::endl;
  105. /*
  106. * Empty
  107. */
  108. ASSERT_EQ(TransferReceiver::getDefaultTransferInterval(), rcv.getInterval());
  109. ASSERT_EQ(0, rcv.getLastTransferTimestampMonotonic().toUSec());
  110. /*
  111. * Single frame transfer with zero ts, must be ignored
  112. */
  113. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "Foo", SET110, 0, 0), bk));
  114. ASSERT_EQ(TransferReceiver::getDefaultTransferInterval(), rcv.getInterval());
  115. ASSERT_EQ(0, rcv.getLastTransferTimestampMonotonic().toUSec());
  116. /*
  117. * Valid compound transfer
  118. * Args: iface_index, data, set, transfer_id, ts_monotonic
  119. */
  120. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "\x34\x12" "34567", SET100, 0, 100), bk));
  121. CHECK_COMPLETE( rcv.addFrame(gen(0, "foo", SET011, 0, 200), bk));
  122. ASSERT_TRUE(matchBufferContent(bufmgr.access(gen.bufmgr_key), "34567foo"));
  123. ASSERT_EQ(0x1234, rcv.getLastTransferCrc());
  124. ASSERT_EQ(TransferReceiver::getDefaultTransferInterval(), rcv.getInterval()); // Not initialized yet
  125. ASSERT_EQ(100, rcv.getLastTransferTimestampMonotonic().toUSec());
  126. /*
  127. * Compound transfer mixed with invalid frames; buffer was not released explicitly
  128. * Args: iface_index, data, set, transfer_id, ts_monotonic
  129. */
  130. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "qwe", SET100, 0, 300), bk)); // Previous TID, rejected
  131. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "rty", SET100, 0, 300), bk)); // Previous TID, wrong iface
  132. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "\x9a\x78" "34567", SET100, 1, 1000), bk));
  133. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "qwertyu", SET100, 1, 1100), bk)); // Old toggle
  134. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "qwertyu", SET000, 1, 1100), bk)); // Old toggle
  135. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "abcdefg", SET001, 1, 1200), bk));
  136. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "4567891", SET001, 2, 1300), bk)); // Next TID, but not SOT
  137. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "", SET010, 1, 1300), bk)); // Wrong iface
  138. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "", SET001, 1, 1300), bk)); // Unexpected toggle
  139. CHECK_COMPLETE( rcv.addFrame(gen(0, "", SET010, 1, 1300), bk));
  140. ASSERT_TRUE(matchBufferContent(bufmgr.access(gen.bufmgr_key), "34567abcdefg"));
  141. ASSERT_EQ(0x789A, rcv.getLastTransferCrc());
  142. ASSERT_GE(TransferReceiver::getDefaultTransferInterval(), rcv.getInterval());
  143. ASSERT_LE(TransferReceiver::getMinTransferInterval(), rcv.getInterval());
  144. ASSERT_EQ(1000, rcv.getLastTransferTimestampMonotonic().toUSec());
  145. ASSERT_FALSE(rcv.isTimedOut(tsMono(1000)));
  146. ASSERT_FALSE(rcv.isTimedOut(tsMono(5000)));
  147. ASSERT_TRUE(rcv.isTimedOut(tsMono(60000000)));
  148. /*
  149. * Single-frame transfers
  150. * Args: iface_index, data, set, transfer_id, ts_monotonic
  151. */
  152. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "qwe", SET110, 1, 2000), bk)); // Previous TID
  153. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "qwe", SET110, 2, 2100), bk)); // Wrong iface
  154. CHECK_SINGLE_FRAME(rcv.addFrame(gen(0, "qwe", SET110, 2, 2200), bk));
  155. ASSERT_FALSE(bufmgr.access(gen.bufmgr_key)); // Buffer must be removed
  156. ASSERT_GT(TransferReceiver::getDefaultTransferInterval(), rcv.getInterval());
  157. ASSERT_EQ(2200, rcv.getLastTransferTimestampMonotonic().toUSec());
  158. CHECK_SINGLE_FRAME(rcv.addFrame(gen(0, "", SET110, 3, 2500), bk));
  159. ASSERT_EQ(2500, rcv.getLastTransferTimestampMonotonic().toUSec());
  160. CHECK_SINGLE_FRAME(rcv.addFrame(gen(0, "", SET110, 0, 3000), bk));
  161. CHECK_SINGLE_FRAME(rcv.addFrame(gen(0, "", SET110, 1, 3100), bk));
  162. CHECK_SINGLE_FRAME(rcv.addFrame(gen(0, "", SET110, 3, 3200), bk));
  163. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "", SET110, 0, 3300), bk)); // Old TID, wrong iface
  164. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "", SET110, 2, 3400), bk)); // Old TID, wrong iface
  165. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "", SET110, 3, 3500), bk)); // Old TID, wrong iface
  166. CHECK_SINGLE_FRAME(rcv.addFrame(gen(0, "", SET110, 4, 3600), bk));
  167. ASSERT_EQ(3600, rcv.getLastTransferTimestampMonotonic().toUSec());
  168. std::cout << "Interval: " << rcv.getInterval().toString() << std::endl;
  169. /*
  170. * Timeouts
  171. */
  172. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "qwe", SET110, 1, 5000), bk)); // Wrong iface - ignored
  173. CHECK_SINGLE_FRAME(rcv.addFrame(gen(1, "qwe", SET110, 6, 1500000), bk)); // Accepted due to iface timeout
  174. ASSERT_EQ(1500000, rcv.getLastTransferTimestampMonotonic().toUSec());
  175. std::cout << "Interval: " << rcv.getInterval().toString() << std::endl;
  176. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "qwe", SET110, 7, 1500100), bk)); // Ignored - old iface 0
  177. CHECK_SINGLE_FRAME(rcv.addFrame(gen(1, "qwe", SET110, 7, 1500100), bk));
  178. ASSERT_EQ(1500100, rcv.getLastTransferTimestampMonotonic().toUSec());
  179. std::cout << "Interval: " << rcv.getInterval().toString() << std::endl;
  180. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "qwe", SET110, 7, 1500100), bk)); // Old TID
  181. CHECK_SINGLE_FRAME(rcv.addFrame(gen(0, "qwe", SET110, 7, 100000000), bk)); // Accepted - global timeout
  182. ASSERT_EQ(100000000, rcv.getLastTransferTimestampMonotonic().toUSec());
  183. std::cout << "Interval: " << rcv.getInterval().toString() << std::endl;
  184. CHECK_SINGLE_FRAME(rcv.addFrame(gen(0, "qwe", SET110, 8, 100000100), bk));
  185. ASSERT_EQ(100000100, rcv.getLastTransferTimestampMonotonic().toUSec());
  186. std::cout << "Interval: " << rcv.getInterval().toString() << std::endl;
  187. ASSERT_TRUE(rcv.isTimedOut(tsMono(900000000)));
  188. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "\x78\x56" "34567", SET100, 0, 900000000), bk)); // Global timeout
  189. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "1234567", SET100, 0, 900000100), bk)); // Wrong iface
  190. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "qwe", SET011, 0, 900000200), bk)); // Wrong iface
  191. CHECK_COMPLETE( rcv.addFrame(gen(1, "qwe", SET011, 0, 900000200), bk));
  192. ASSERT_EQ(900000000, rcv.getLastTransferTimestampMonotonic().toUSec());
  193. std::cout << "Interval: " << rcv.getInterval().toString() << std::endl;
  194. ASSERT_FALSE(rcv.isTimedOut(tsMono(1000)));
  195. ASSERT_FALSE(rcv.isTimedOut(tsMono(900000300)));
  196. ASSERT_TRUE(rcv.isTimedOut(tsMono(9990000000)));
  197. ASSERT_LT(TransferReceiver::getDefaultTransferInterval(), rcv.getInterval());
  198. ASSERT_LE(TransferReceiver::getMinTransferInterval(), rcv.getInterval());
  199. ASSERT_GE(TransferReceiver::getMaxTransferInterval(), rcv.getInterval());
  200. ASSERT_TRUE(matchBufferContent(bufmgr.access(gen.bufmgr_key), "34567qwe"));
  201. ASSERT_EQ(0x5678, rcv.getLastTransferCrc());
  202. /*
  203. * Destruction
  204. */
  205. ASSERT_TRUE(bufmgr.access(gen.bufmgr_key));
  206. context.receiver.~TransferReceiver(); // TransferReceiver does not own the buffer, it must not be released!
  207. ASSERT_TRUE(bufmgr.access(gen.bufmgr_key)); // Making sure that the buffer is still there
  208. }
  209. TEST(TransferReceiver, OutOfBufferSpace_32bytes)
  210. {
  211. Context<32> context;
  212. RxFrameGenerator gen(789);
  213. uavcan::TransferReceiver& rcv = context.receiver;
  214. uavcan::TransferBufferManager& bufmgr = context.bufmgr;
  215. uavcan::TransferBufferAccessor bk(context.bufmgr, RxFrameGenerator::DEFAULT_KEY);
  216. /*
  217. * Simple transfer, maximum buffer length
  218. */
  219. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "1234567", SET100, 1, 100000000), bk)); // 5
  220. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "1234567", SET001, 1, 100000100), bk)); // 12
  221. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "1234567", SET000, 1, 100000200), bk)); // 19
  222. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "1234567", SET001, 1, 100000300), bk)); // 26
  223. CHECK_COMPLETE( rcv.addFrame(gen(1, "123456", SET010, 1, 100000400), bk)); // 32
  224. ASSERT_EQ(100000000, rcv.getLastTransferTimestampMonotonic().toUSec());
  225. ASSERT_TRUE(matchBufferContent(bufmgr.access(gen.bufmgr_key), "34567123456712345671234567123456"));
  226. ASSERT_EQ(0x3231, rcv.getLastTransferCrc()); // CRC from "12", which is 0x3231 in little endian
  227. /*
  228. * Transfer longer than available buffer space
  229. */
  230. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "1234567", SET100, 2, 100001000), bk)); // 5
  231. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "1234567", SET001, 2, 100001100), bk)); // 12
  232. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "1234567", SET000, 2, 100001200), bk)); // 19
  233. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "1234567", SET001, 2, 100001200), bk)); // 26
  234. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "1234567", SET010, 2, 100001300), bk)); // 33 // EOT, ignored - lost sync
  235. ASSERT_EQ(100000000, rcv.getLastTransferTimestampMonotonic().toUSec()); // Timestamp will not be overriden
  236. ASSERT_FALSE(bufmgr.access(gen.bufmgr_key)); // Buffer should be removed
  237. ASSERT_EQ(1, rcv.yieldErrorCount());
  238. ASSERT_EQ(0, rcv.yieldErrorCount());
  239. }
  240. TEST(TransferReceiver, OutOfOrderFrames)
  241. {
  242. Context<32> context;
  243. RxFrameGenerator gen(789);
  244. uavcan::TransferReceiver& rcv = context.receiver;
  245. uavcan::TransferBufferManager& bufmgr = context.bufmgr;
  246. uavcan::TransferBufferAccessor bk(context.bufmgr, RxFrameGenerator::DEFAULT_KEY);
  247. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "1234567", SET100, 7, 100000000), bk));
  248. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "-------", SET000, 7, 100000100), bk)); // Out of order
  249. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "-------", SET010, 7, 100000200), bk)); // Out of order
  250. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "qwertyu", SET001, 7, 100000300), bk));
  251. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "-------", SET011, 7, 100000200), bk)); // Out of order
  252. CHECK_COMPLETE( rcv.addFrame(gen(1, "abcd", SET010, 7, 100000400), bk));
  253. ASSERT_EQ(100000000, rcv.getLastTransferTimestampMonotonic().toUSec());
  254. ASSERT_TRUE(matchBufferContent(bufmgr.access(gen.bufmgr_key), "34567qwertyuabcd"));
  255. ASSERT_EQ(0x3231, rcv.getLastTransferCrc());
  256. ASSERT_EQ(3, rcv.yieldErrorCount());
  257. ASSERT_EQ(0, rcv.yieldErrorCount());
  258. }
  259. TEST(TransferReceiver, IntervalMeasurement)
  260. {
  261. Context<32> context;
  262. RxFrameGenerator gen(789);
  263. uavcan::TransferReceiver& rcv = context.receiver;
  264. uavcan::TransferBufferManager& bufmgr = context.bufmgr;
  265. uavcan::TransferBufferAccessor bk(context.bufmgr, RxFrameGenerator::DEFAULT_KEY);
  266. static const int INTERVAL = 1000;
  267. uavcan::TransferID tid;
  268. uint64_t timestamp = 100000000;
  269. for (int i = 0; i < 1000; i++)
  270. {
  271. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "1234567", SET100, tid.get(), timestamp), bk));
  272. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "qwertyu", SET001, tid.get(), timestamp), bk));
  273. CHECK_COMPLETE( rcv.addFrame(gen(1, "abcd", SET010, tid.get(), timestamp), bk));
  274. ASSERT_TRUE(matchBufferContent(bufmgr.access(gen.bufmgr_key), "34567qwertyuabcd"));
  275. ASSERT_EQ(0x3231, rcv.getLastTransferCrc());
  276. ASSERT_EQ(timestamp, rcv.getLastTransferTimestampMonotonic().toUSec());
  277. timestamp += uint64_t(INTERVAL);
  278. tid.increment();
  279. }
  280. ASSERT_EQ(INTERVAL, rcv.getInterval().toUSec());
  281. }
  282. TEST(TransferReceiver, Restart)
  283. {
  284. Context<32> context;
  285. RxFrameGenerator gen(789);
  286. uavcan::TransferReceiver& rcv = context.receiver;
  287. uavcan::TransferBufferManager& bufmgr = context.bufmgr;
  288. uavcan::TransferBufferAccessor bk(context.bufmgr, RxFrameGenerator::DEFAULT_KEY);
  289. /*
  290. * This transfer looks complete, but must be ignored because of large delay after the first frame
  291. * Args: iface_index, data, set, transfer_id, ts_monotonic [, ts_utc]
  292. */
  293. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "-------", SET100, 0, 100), bk)); // Begin
  294. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "-------", SET001, 0, 100000100), bk)); // Continue 100 sec later, expired
  295. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "-------", SET010, 0, 100000200), bk)); // Ignored
  296. /*
  297. * Begins immediately after, encounters a delay 0.9 sec but completes OK
  298. */
  299. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "1234567", SET100, 0, 100000300), bk)); // Begin
  300. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "1234567", SET001, 0, 100900300), bk)); // 0.9 sec later
  301. CHECK_COMPLETE( rcv.addFrame(gen(1, "1234567", SET010, 0, 100900400), bk)); // OK nevertheless
  302. ASSERT_TRUE(matchBufferContent(bufmgr.access(gen.bufmgr_key), "3456712345671234567"));
  303. ASSERT_EQ(0x3231, rcv.getLastTransferCrc());
  304. std::cout << "Interval: " << rcv.getInterval().toString() << std::endl;
  305. /*
  306. * Begins OK, gets a timeout, switches to another iface
  307. */
  308. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "-------", SET100, 1, 103000500), bk)); // Begin
  309. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "-------", SET001, 1, 105000500), bk)); // 2 sec later, timeout
  310. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "-------", SET001, 1, 105000600), bk)); // Same TID, another iface - ignore
  311. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "-------", SET001, 2, 105000700), bk)); // Not first frame - ignore
  312. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "1234567", SET100, 2, 105000800), bk)); // First, another iface - restart
  313. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "-------", SET010, 1, 105000600), bk)); // Old iface - ignore
  314. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "1234567", SET001, 2, 105000900), bk)); // Continuing
  315. CHECK_COMPLETE( rcv.addFrame(gen(0, "1234567", SET010, 2, 105000910), bk)); // Done
  316. ASSERT_TRUE(matchBufferContent(bufmgr.access(gen.bufmgr_key), "3456712345671234567"));
  317. ASSERT_EQ(0x3231, rcv.getLastTransferCrc());
  318. ASSERT_EQ(4, rcv.yieldErrorCount());
  319. ASSERT_EQ(0, rcv.yieldErrorCount());
  320. }
  321. TEST(TransferReceiver, UtcTransferTimestamping)
  322. {
  323. Context<32> context;
  324. RxFrameGenerator gen(789);
  325. uavcan::TransferReceiver& rcv = context.receiver;
  326. uavcan::TransferBufferManager& bufmgr = context.bufmgr;
  327. uavcan::TransferBufferAccessor bk(context.bufmgr, RxFrameGenerator::DEFAULT_KEY);
  328. /*
  329. * Zero UTC timestamp must be preserved
  330. */
  331. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "1234567", SET100, 0, 1, 0), bk));
  332. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "qwertyu", SET001, 0, 2, 0), bk));
  333. CHECK_COMPLETE( rcv.addFrame(gen(1, "abcd", SET010, 0, 3, 0), bk));
  334. ASSERT_TRUE(matchBufferContent(bufmgr.access(gen.bufmgr_key), "34567qwertyuabcd"));
  335. ASSERT_EQ(1, rcv.getLastTransferTimestampMonotonic().toUSec());
  336. ASSERT_EQ(0, rcv.getLastTransferTimestampUtc().toUSec());
  337. /*
  338. * Non-zero UTC timestamp
  339. */
  340. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "1234567", SET100, 1, 4, 123), bk)); // This UTC is going to be preserved
  341. CHECK_NOT_COMPLETE(rcv.addFrame(gen(1, "qwertyu", SET001, 1, 5, 0), bk)); // Following are ignored
  342. CHECK_COMPLETE( rcv.addFrame(gen(1, "abcd", SET010, 1, 6, 42), bk));
  343. ASSERT_TRUE(matchBufferContent(bufmgr.access(gen.bufmgr_key), "34567qwertyuabcd"));
  344. ASSERT_EQ(4, rcv.getLastTransferTimestampMonotonic().toUSec());
  345. ASSERT_EQ(123, rcv.getLastTransferTimestampUtc().toUSec());
  346. /*
  347. * Single-frame transfers
  348. * iface_index, data, set, transfer_id, ts_monotonic
  349. */
  350. CHECK_SINGLE_FRAME(rcv.addFrame(gen(1, "abc", SET110, 2, 10, 100000000), bk)); // Exact value is irrelevant
  351. ASSERT_EQ(10, rcv.getLastTransferTimestampMonotonic().toUSec());
  352. ASSERT_EQ(100000000, rcv.getLastTransferTimestampUtc().toUSec());
  353. /*
  354. * Restart recovery
  355. */
  356. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "1234567", SET100, 1, 100000000, 800000000), bk));
  357. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "qwertyu", SET001, 1, 100000001, 300000000), bk));
  358. CHECK_COMPLETE( rcv.addFrame(gen(0, "abcd", SET010, 1, 100000002, 900000000), bk));
  359. ASSERT_TRUE(matchBufferContent(bufmgr.access(gen.bufmgr_key), "34567qwertyuabcd"));
  360. ASSERT_EQ(100000000, rcv.getLastTransferTimestampMonotonic().toUSec());
  361. ASSERT_EQ(800000000, rcv.getLastTransferTimestampUtc().toUSec());
  362. }
  363. TEST(TransferReceiver, HeaderParsing)
  364. {
  365. static const std::string SFT_PAYLOAD = "1234567";
  366. uavcan::TransferID tid;
  367. /*
  368. * Broadcast
  369. */
  370. {
  371. Context<32> context;
  372. RxFrameGenerator gen(123);
  373. uavcan::TransferReceiver& rcv = context.receiver;
  374. uavcan::TransferBufferManager& bufmgr = context.bufmgr;
  375. /*
  376. * MFT, message broadcasting
  377. */
  378. {
  379. gen.bufmgr_key =
  380. uavcan::TransferBufferManagerKey(gen.bufmgr_key.getNodeID(), uavcan::TransferTypeMessageBroadcast);
  381. uavcan::TransferBufferAccessor bk1(context.bufmgr, gen.bufmgr_key);
  382. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "1234567", SET100, tid.get(), 1), bk1));
  383. CHECK_COMPLETE( rcv.addFrame(gen(0, "abcd", SET011, tid.get(), 2), bk1));
  384. ASSERT_TRUE(matchBufferContent(bufmgr.access(gen.bufmgr_key), "34567abcd"));
  385. ASSERT_EQ(0x3231, rcv.getLastTransferCrc());
  386. tid.increment();
  387. bk1.remove();
  388. }
  389. /*
  390. * SFT, message broadcasting
  391. */
  392. {
  393. gen.bufmgr_key =
  394. uavcan::TransferBufferManagerKey(gen.bufmgr_key.getNodeID(), uavcan::TransferTypeMessageBroadcast);
  395. uavcan::TransferBufferAccessor bk(context.bufmgr, gen.bufmgr_key);
  396. const uavcan::RxFrame frame = gen(0, SFT_PAYLOAD, SET110, tid.get(), 1000);
  397. CHECK_SINGLE_FRAME(rcv.addFrame(frame, bk));
  398. ASSERT_EQ(0x0000, rcv.getLastTransferCrc()); // Default value - zero
  399. // All bytes are payload, zero overhead
  400. ASSERT_TRUE(std::equal(SFT_PAYLOAD.begin(), SFT_PAYLOAD.end(), frame.getPayloadPtr()));
  401. tid.increment();
  402. }
  403. }
  404. /*
  405. * Unicast
  406. */
  407. {
  408. Context<32> context;
  409. RxFrameGenerator gen(123);
  410. uavcan::TransferReceiver& rcv = context.receiver;
  411. uavcan::TransferBufferManager& bufmgr = context.bufmgr;
  412. static const uavcan::TransferType ADDRESSED_TRANSFER_TYPES[2] =
  413. {
  414. uavcan::TransferTypeServiceRequest,
  415. uavcan::TransferTypeServiceResponse
  416. };
  417. /*
  418. * MFT, service request/response
  419. */
  420. for (unsigned i = 0; i < (sizeof(ADDRESSED_TRANSFER_TYPES) / sizeof(ADDRESSED_TRANSFER_TYPES[0])); i++)
  421. {
  422. gen.bufmgr_key =
  423. uavcan::TransferBufferManagerKey(gen.bufmgr_key.getNodeID(), ADDRESSED_TRANSFER_TYPES[i]);
  424. uavcan::TransferBufferAccessor bk2(context.bufmgr, gen.bufmgr_key);
  425. const uint64_t ts_monotonic = i + 10;
  426. CHECK_NOT_COMPLETE(rcv.addFrame(gen(0, "1234567", SET100, tid.get(), ts_monotonic), bk2));
  427. CHECK_COMPLETE( rcv.addFrame(gen(0, "abcd", SET011, tid.get(), ts_monotonic), bk2));
  428. ASSERT_TRUE(matchBufferContent(bufmgr.access(gen.bufmgr_key), "34567abcd"));
  429. ASSERT_EQ(0x3231, rcv.getLastTransferCrc());
  430. tid.increment();
  431. bk2.remove();
  432. }
  433. /*
  434. * SFT, message unicast, service request/response
  435. */
  436. for (unsigned i = 0; i < int(sizeof(ADDRESSED_TRANSFER_TYPES) / sizeof(ADDRESSED_TRANSFER_TYPES[0])); i++)
  437. {
  438. gen.bufmgr_key =
  439. uavcan::TransferBufferManagerKey(gen.bufmgr_key.getNodeID(), ADDRESSED_TRANSFER_TYPES[i]);
  440. uavcan::TransferBufferAccessor bk(context.bufmgr, gen.bufmgr_key);
  441. const uavcan::RxFrame frame = gen(0, SFT_PAYLOAD, SET110, tid.get(), i + 10000U);
  442. CHECK_SINGLE_FRAME(rcv.addFrame(frame, bk));
  443. ASSERT_EQ(0x0000, rcv.getLastTransferCrc()); // Default value - zero
  444. // First byte must be ignored
  445. ASSERT_TRUE(std::equal(SFT_PAYLOAD.begin(), SFT_PAYLOAD.end(), frame.getPayloadPtr()));
  446. tid.increment();
  447. }
  448. }
  449. }