publisher.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <gtest/gtest.h>
  5. #include <uavcan/node/publisher.hpp>
  6. #include <root_ns_a/MavlinkMessage.hpp>
  7. #include "../clock.hpp"
  8. #include "../transport/can/can.hpp"
  9. #include "test_node.hpp"
  10. TEST(Publisher, Basic)
  11. {
  12. SystemClockMock clock_mock(100);
  13. CanDriverMock can_driver(2, clock_mock);
  14. TestNode node(can_driver, clock_mock, 1);
  15. uavcan::Publisher<root_ns_a::MavlinkMessage> publisher(node);
  16. ASSERT_FALSE(publisher.getTransferSender().isInitialized());
  17. std::cout <<
  18. "sizeof(uavcan::Publisher<root_ns_a::MavlinkMessage>): " <<
  19. sizeof(uavcan::Publisher<root_ns_a::MavlinkMessage>) << std::endl;
  20. // Manual type registration - we can't rely on the GDTR state
  21. uavcan::GlobalDataTypeRegistry::instance().reset();
  22. uavcan::DefaultDataTypeRegistrator<root_ns_a::MavlinkMessage> _registrator;
  23. /*
  24. * Message layout:
  25. * uint8 seq
  26. * uint8 sysid
  27. * uint8 compid
  28. * uint8 msgid
  29. * uint8[<256] payload
  30. */
  31. root_ns_a::MavlinkMessage msg;
  32. msg.seq = 0x42;
  33. msg.sysid = 0x72;
  34. msg.compid = 0x08;
  35. msg.msgid = 0xa5;
  36. msg.payload = "Msg";
  37. const uint8_t expected_transfer_payload[] = {0x42, 0x72, 0x08, 0xa5, 'M', 's', 'g'};
  38. const uint64_t tx_timeout_usec = uint64_t(publisher.getDefaultTxTimeout().toUSec());
  39. /*
  40. * Broadcast
  41. */
  42. {
  43. ASSERT_LT(0, publisher.broadcast(msg));
  44. // uint_fast16_t data_type_id, TransferType transfer_type, NodeID src_node_id, NodeID dst_node_id,
  45. // uint_fast8_t frame_index, TransferID transfer_id, bool last_frame = false
  46. uavcan::Frame expected_frame(root_ns_a::MavlinkMessage::DefaultDataTypeID, uavcan::TransferTypeMessageBroadcast,
  47. node.getNodeID(), uavcan::NodeID::Broadcast, 0);
  48. expected_frame.setPayload(expected_transfer_payload, 7);
  49. expected_frame.setStartOfTransfer(true);
  50. expected_frame.setEndOfTransfer(true);
  51. uavcan::CanFrame expected_can_frame;
  52. ASSERT_TRUE(expected_frame.compile(expected_can_frame));
  53. ASSERT_TRUE(can_driver.ifaces[0].matchAndPopTx(expected_can_frame, tx_timeout_usec + 100));
  54. ASSERT_TRUE(can_driver.ifaces[1].matchAndPopTx(expected_can_frame, tx_timeout_usec + 100));
  55. ASSERT_TRUE(can_driver.ifaces[0].tx.empty());
  56. ASSERT_TRUE(can_driver.ifaces[1].tx.empty());
  57. // Second shot - checking the transfer ID
  58. publisher.setPriority(10);
  59. ASSERT_LT(0, publisher.broadcast(msg));
  60. expected_frame = uavcan::Frame(root_ns_a::MavlinkMessage::DefaultDataTypeID,
  61. uavcan::TransferTypeMessageBroadcast,
  62. node.getNodeID(), uavcan::NodeID::Broadcast, 1);
  63. expected_frame.setStartOfTransfer(true);
  64. expected_frame.setEndOfTransfer(true);
  65. expected_frame.setPayload(expected_transfer_payload, 7);
  66. expected_frame.setPriority(10);
  67. ASSERT_TRUE(expected_frame.compile(expected_can_frame));
  68. ASSERT_TRUE(can_driver.ifaces[0].matchAndPopTx(expected_can_frame, tx_timeout_usec + 100));
  69. ASSERT_TRUE(can_driver.ifaces[1].matchAndPopTx(expected_can_frame, tx_timeout_usec + 100));
  70. ASSERT_TRUE(can_driver.ifaces[0].tx.empty());
  71. ASSERT_TRUE(can_driver.ifaces[1].tx.empty());
  72. }
  73. clock_mock.advance(1000);
  74. /*
  75. * Misc
  76. */
  77. ASSERT_TRUE(uavcan::GlobalDataTypeRegistry::instance().isFrozen());
  78. ASSERT_TRUE(publisher.getTransferSender().isInitialized());
  79. }