incoming_transfer.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_listener.hpp>
  7. #include "../clock.hpp"
  8. #include "transfer_test_helpers.hpp"
  9. static uavcan::RxFrame makeFrame()
  10. {
  11. uavcan::RxFrame frame(uavcan::Frame(123, uavcan::TransferTypeMessageBroadcast, 1, uavcan::NodeID::Broadcast, 0),
  12. tsMono(123), tsUtc(456), 0);
  13. uint8_t data[8];
  14. for (uint8_t i = 0; i < sizeof(data); i++)
  15. {
  16. data[i] = i;
  17. }
  18. frame.setPayload(data, sizeof(data));
  19. frame.setEndOfTransfer(true);
  20. return frame;
  21. }
  22. static bool match(const uavcan::IncomingTransfer& it, const uavcan::RxFrame& frame,
  23. const uint8_t* payload, unsigned payload_len)
  24. {
  25. // Fields extracted from the frame struct
  26. EXPECT_EQ(it.getMonotonicTimestamp(), frame.getMonotonicTimestamp());
  27. EXPECT_EQ(it.getUtcTimestamp(), frame.getUtcTimestamp());
  28. EXPECT_EQ(it.getSrcNodeID(), frame.getSrcNodeID());
  29. EXPECT_EQ(it.getTransferID(), frame.getTransferID());
  30. EXPECT_EQ(it.getTransferType(), frame.getTransferType());
  31. // Payload comparison
  32. static const unsigned BUFLEN = 1024;
  33. uint8_t buf_reference[BUFLEN], buf_actual[BUFLEN];
  34. if (payload_len > BUFLEN)
  35. {
  36. std::cout << "match(): Payload is too long" << std::endl;
  37. exit(1);
  38. }
  39. std::fill(buf_reference, buf_reference + BUFLEN, 0);
  40. std::fill(buf_actual, buf_actual + BUFLEN, 0);
  41. std::copy(payload, payload + payload_len, buf_reference);
  42. EXPECT_EQ(payload_len, it.read(0, buf_actual, payload_len * 3));
  43. EXPECT_EQ(0, it.read(payload_len, buf_actual, payload_len * 3));
  44. return std::equal(buf_reference, buf_reference + BUFLEN, buf_actual);
  45. }
  46. TEST(SingleFrameIncomingTransfer, Basic)
  47. {
  48. using uavcan::RxFrame;
  49. using uavcan::SingleFrameIncomingTransfer;
  50. const RxFrame frame = makeFrame();
  51. SingleFrameIncomingTransfer it(frame);
  52. ASSERT_TRUE(match(it, frame, frame.getPayloadPtr(), frame.getPayloadLen()));
  53. }
  54. TEST(MultiFrameIncomingTransfer, Basic)
  55. {
  56. using uavcan::RxFrame;
  57. using uavcan::MultiFrameIncomingTransfer;
  58. uavcan::PoolAllocator<uavcan::MemPoolBlockSize * 100, uavcan::MemPoolBlockSize> poolmgr;
  59. uavcan::TransferBufferManager bufmgr(256, poolmgr);
  60. const RxFrame frame = makeFrame();
  61. uavcan::TransferBufferManagerKey bufmgr_key(frame.getSrcNodeID(), frame.getTransferType());
  62. uavcan::TransferBufferAccessor tba(bufmgr, bufmgr_key);
  63. MultiFrameIncomingTransfer it(frame.getMonotonicTimestamp(), frame.getUtcTimestamp(), frame, tba);
  64. /*
  65. * Empty read must fail
  66. */
  67. uint8_t data_byte = 0;
  68. ASSERT_GT(0, it.read(0, &data_byte, 1)); // Error - no such buffer
  69. /*
  70. * Filling the test data
  71. */
  72. const std::string data = "123Hello world";
  73. const uint8_t* const data_ptr = reinterpret_cast<const uint8_t*>(data.c_str());
  74. ASSERT_FALSE(bufmgr.access(bufmgr_key));
  75. ASSERT_TRUE(bufmgr.create(bufmgr_key));
  76. ASSERT_EQ(data.length(), bufmgr.access(bufmgr_key)->write(0, data_ptr, unsigned(data.length())));
  77. /*
  78. * Check
  79. */
  80. ASSERT_TRUE(match(it, frame, data_ptr, unsigned(data.length())));
  81. /*
  82. * Buffer release
  83. */
  84. ASSERT_TRUE(bufmgr.access(bufmgr_key));
  85. it.release();
  86. ASSERT_FALSE(bufmgr.access(bufmgr_key));
  87. }