subscriber.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <gtest/gtest.h>
  5. #include <uavcan/node/subscriber.hpp>
  6. #include <uavcan/util/method_binder.hpp>
  7. #include <root_ns_a/EmptyMessage.hpp>
  8. #include <root_ns_a/MavlinkMessage.hpp>
  9. #include "../clock.hpp"
  10. #include "../transport/can/can.hpp"
  11. #include "test_node.hpp"
  12. template <typename DataType>
  13. struct SubscriptionListener
  14. {
  15. typedef uavcan::ReceivedDataStructure<DataType> ReceivedDataStructure;
  16. struct ReceivedDataStructureCopy
  17. {
  18. uavcan::MonotonicTime ts_monotonic;
  19. uavcan::UtcTime ts_utc;
  20. uavcan::TransferType transfer_type;
  21. uavcan::TransferID transfer_id;
  22. uavcan::NodeID src_node_id;
  23. uavcan::uint8_t iface_index;
  24. DataType msg;
  25. ReceivedDataStructureCopy(const ReceivedDataStructure& s)
  26. : ts_monotonic(s.getMonotonicTimestamp())
  27. , ts_utc(s.getUtcTimestamp())
  28. , transfer_type(s.getTransferType())
  29. , transfer_id(s.getTransferID())
  30. , src_node_id(s.getSrcNodeID())
  31. , iface_index(s.getIfaceIndex())
  32. , msg(s)
  33. { }
  34. };
  35. std::vector<DataType> simple;
  36. std::vector<ReceivedDataStructureCopy> extended;
  37. void receiveExtended(ReceivedDataStructure& msg)
  38. {
  39. extended.push_back(msg);
  40. }
  41. void receiveSimple(DataType& msg)
  42. {
  43. simple.push_back(msg);
  44. }
  45. typedef SubscriptionListener<DataType> SelfType;
  46. typedef uavcan::MethodBinder<SelfType*, void (SelfType::*)(ReceivedDataStructure&)> ExtendedBinder;
  47. typedef uavcan::MethodBinder<SelfType*, void (SelfType::*)(DataType&)> SimpleBinder;
  48. ExtendedBinder bindExtended() { return ExtendedBinder(this, &SelfType::receiveExtended); }
  49. SimpleBinder bindSimple() { return SimpleBinder(this, &SelfType::receiveSimple); }
  50. };
  51. TEST(Subscriber, Basic)
  52. {
  53. // Manual type registration - we can't rely on the GDTR state
  54. uavcan::GlobalDataTypeRegistry::instance().reset();
  55. uavcan::DefaultDataTypeRegistrator<root_ns_a::MavlinkMessage> _registrator;
  56. SystemClockDriver clock_driver;
  57. CanDriverMock can_driver(2, clock_driver);
  58. TestNode node(can_driver, clock_driver, 1);
  59. typedef SubscriptionListener<root_ns_a::MavlinkMessage> Listener;
  60. uavcan::Subscriber<root_ns_a::MavlinkMessage, Listener::ExtendedBinder> sub_extended(node);
  61. uavcan::Subscriber<root_ns_a::MavlinkMessage, Listener::ExtendedBinder> sub_extended2(node); // Not used
  62. uavcan::Subscriber<root_ns_a::MavlinkMessage, Listener::SimpleBinder> sub_simple(node);
  63. uavcan::Subscriber<root_ns_a::MavlinkMessage, Listener::SimpleBinder> sub_simple2(node); // Not used
  64. std::cout <<
  65. "sizeof(uavcan::Subscriber<root_ns_a::MavlinkMessage, Listener::ExtendedBinder>): " <<
  66. sizeof(uavcan::Subscriber<root_ns_a::MavlinkMessage, Listener::ExtendedBinder>) << std::endl;
  67. // Null binder - will fail
  68. ASSERT_EQ(-uavcan::ErrInvalidParam, sub_extended.start(Listener::ExtendedBinder(UAVCAN_NULLPTR, UAVCAN_NULLPTR)));
  69. Listener listener;
  70. /*
  71. * Message layout:
  72. * uint8 seq
  73. * uint8 sysid
  74. * uint8 compid
  75. * uint8 msgid
  76. * uint8[<256] payload
  77. */
  78. root_ns_a::MavlinkMessage expected_msg;
  79. expected_msg.seq = 0x42;
  80. expected_msg.sysid = 0x72;
  81. expected_msg.compid = 0x08;
  82. expected_msg.msgid = 0xa5;
  83. expected_msg.payload = "Msg";
  84. const uint8_t transfer_payload[] = {0x42, 0x72, 0x08, 0xa5, 'M', 's', 'g'};
  85. /*
  86. * RxFrame generation
  87. */
  88. std::vector<uavcan::RxFrame> rx_frames;
  89. for (uint8_t i = 0; i < 4; i++)
  90. {
  91. uavcan::TransferType tt = uavcan::TransferTypeMessageBroadcast;
  92. uavcan::NodeID dni = (tt == uavcan::TransferTypeMessageBroadcast) ?
  93. uavcan::NodeID::Broadcast : node.getDispatcher().getNodeID();
  94. // uint_fast16_t data_type_id, TransferType transfer_type, NodeID src_node_id, NodeID dst_node_id,
  95. // uint_fast8_t frame_index, TransferID transfer_id, bool last_frame
  96. uavcan::Frame frame(root_ns_a::MavlinkMessage::DefaultDataTypeID, tt, uavcan::NodeID(uint8_t(i + 100)),
  97. dni, i);
  98. frame.setStartOfTransfer(true);
  99. frame.setEndOfTransfer(true);
  100. frame.setPayload(transfer_payload, 7);
  101. uavcan::RxFrame rx_frame(frame, clock_driver.getMonotonic(), clock_driver.getUtc(), 0);
  102. rx_frames.push_back(rx_frame);
  103. }
  104. /*
  105. * Reception
  106. */
  107. ASSERT_EQ(0, node.getDispatcher().getNumMessageListeners());
  108. ASSERT_EQ(0, sub_extended.start(listener.bindExtended()));
  109. ASSERT_EQ(0, sub_extended2.start(listener.bindExtended()));
  110. ASSERT_EQ(0, sub_simple.start(listener.bindSimple()));
  111. ASSERT_EQ(0, sub_simple2.start(listener.bindSimple()));
  112. ASSERT_EQ(4, node.getDispatcher().getNumMessageListeners());
  113. sub_extended2.stop(); // These are not used - making sure they aren't receiving anything
  114. sub_simple2.stop();
  115. ASSERT_EQ(2, node.getDispatcher().getNumMessageListeners());
  116. for (unsigned i = 0; i < rx_frames.size(); i++)
  117. {
  118. can_driver.ifaces[0].pushRx(rx_frames[i]);
  119. can_driver.ifaces[1].pushRx(rx_frames[i]);
  120. }
  121. ASSERT_LE(0, node.spin(clock_driver.getMonotonic() + durMono(10000)));
  122. /*
  123. * Validation
  124. */
  125. ASSERT_EQ(listener.extended.size(), rx_frames.size());
  126. for (unsigned i = 0; i < rx_frames.size(); i++)
  127. {
  128. const Listener::ReceivedDataStructureCopy s = listener.extended.at(i);
  129. ASSERT_TRUE(s.msg == expected_msg);
  130. ASSERT_EQ(rx_frames[i].getSrcNodeID(), s.src_node_id);
  131. ASSERT_EQ(rx_frames[i].getTransferID(), s.transfer_id);
  132. ASSERT_EQ(rx_frames[i].getTransferType(), s.transfer_type);
  133. ASSERT_EQ(rx_frames[i].getMonotonicTimestamp(), s.ts_monotonic);
  134. ASSERT_EQ(rx_frames[i].getIfaceIndex(), s.iface_index);
  135. }
  136. ASSERT_EQ(listener.simple.size(), rx_frames.size());
  137. for (unsigned i = 0; i < rx_frames.size(); i++)
  138. {
  139. ASSERT_TRUE(listener.simple.at(i) == expected_msg);
  140. }
  141. ASSERT_EQ(0, sub_extended.getFailureCount());
  142. ASSERT_EQ(0, sub_simple.getFailureCount());
  143. /*
  144. * Unregistration
  145. */
  146. ASSERT_EQ(2, node.getDispatcher().getNumMessageListeners());
  147. sub_extended.stop();
  148. sub_extended2.stop();
  149. sub_simple.stop();
  150. sub_simple2.stop();
  151. ASSERT_EQ(0, node.getDispatcher().getNumMessageListeners());
  152. }
  153. static void panickingSink(const uavcan::ReceivedDataStructure<root_ns_a::MavlinkMessage>&)
  154. {
  155. FAIL() << "I just went mad";
  156. }
  157. TEST(Subscriber, FailureCount)
  158. {
  159. // Manual type registration - we can't rely on the GDTR state
  160. uavcan::GlobalDataTypeRegistry::instance().reset();
  161. uavcan::DefaultDataTypeRegistrator<root_ns_a::MavlinkMessage> _registrator;
  162. SystemClockDriver clock_driver;
  163. CanDriverMock can_driver(2, clock_driver);
  164. TestNode node(can_driver, clock_driver, 1);
  165. {
  166. uavcan::Subscriber<root_ns_a::MavlinkMessage> sub(node);
  167. ASSERT_EQ(0, node.getDispatcher().getNumMessageListeners());
  168. sub.start(panickingSink);
  169. ASSERT_EQ(1, node.getDispatcher().getNumMessageListeners());
  170. ASSERT_EQ(0, sub.getFailureCount());
  171. for (uint8_t i = 0; i < 4; i++)
  172. {
  173. // uint_fast16_t data_type_id, TransferType transfer_type, NodeID src_node_id, NodeID dst_node_id,
  174. // uint_fast8_t frame_index, TransferID transfer_id, bool last_frame
  175. uavcan::Frame frame(root_ns_a::MavlinkMessage::DefaultDataTypeID, uavcan::TransferTypeMessageBroadcast,
  176. uavcan::NodeID(uint8_t(i + 100)), uavcan::NodeID::Broadcast, i);
  177. frame.setStartOfTransfer(true);
  178. frame.setEndOfTransfer(true);
  179. // No payload - broken transfer
  180. uavcan::RxFrame rx_frame(frame, clock_driver.getMonotonic(), clock_driver.getUtc(), 0);
  181. can_driver.ifaces[0].pushRx(rx_frame);
  182. can_driver.ifaces[1].pushRx(rx_frame);
  183. }
  184. ASSERT_LE(0, node.spin(clock_driver.getMonotonic() + durMono(10000)));
  185. ASSERT_EQ(4, sub.getFailureCount());
  186. ASSERT_EQ(1, node.getDispatcher().getNumMessageListeners()); // Still there
  187. }
  188. ASSERT_EQ(0, node.getDispatcher().getNumMessageListeners()); // Removed
  189. }
  190. TEST(Subscriber, SingleFrameTransfer)
  191. {
  192. // Manual type registration - we can't rely on the GDTR state
  193. uavcan::GlobalDataTypeRegistry::instance().reset();
  194. uavcan::DefaultDataTypeRegistrator<root_ns_a::EmptyMessage> _registrator;
  195. SystemClockDriver clock_driver;
  196. CanDriverMock can_driver(2, clock_driver);
  197. TestNode node(can_driver, clock_driver, 1);
  198. typedef SubscriptionListener<root_ns_a::EmptyMessage> Listener;
  199. uavcan::Subscriber<root_ns_a::EmptyMessage, Listener::SimpleBinder> sub(node);
  200. std::cout <<
  201. "sizeof(uavcan::Subscriber<root_ns_a::EmptyMessage, Listener::SimpleBinder>): " <<
  202. sizeof(uavcan::Subscriber<root_ns_a::EmptyMessage, Listener::SimpleBinder>) << std::endl;
  203. Listener listener;
  204. sub.start(listener.bindSimple());
  205. for (uint8_t i = 0; i < 4; i++)
  206. {
  207. // uint_fast16_t data_type_id, TransferType transfer_type, NodeID src_node_id, NodeID dst_node_id,
  208. // uint_fast8_t frame_index, TransferID transfer_id, bool last_frame
  209. uavcan::Frame frame(root_ns_a::EmptyMessage::DefaultDataTypeID, uavcan::TransferTypeMessageBroadcast,
  210. uavcan::NodeID(uint8_t(i + 100)), uavcan::NodeID::Broadcast, i);
  211. frame.setStartOfTransfer(true);
  212. frame.setEndOfTransfer(true);
  213. // No payload - message is empty
  214. uavcan::RxFrame rx_frame(frame, clock_driver.getMonotonic(), clock_driver.getUtc(), 0);
  215. can_driver.ifaces[0].pushRx(rx_frame);
  216. can_driver.ifaces[1].pushRx(rx_frame);
  217. }
  218. ASSERT_LE(0, node.spin(clock_driver.getMonotonic() + durMono(10000)));
  219. ASSERT_EQ(0, sub.getFailureCount());
  220. ASSERT_EQ(4, listener.simple.size());
  221. for (unsigned i = 0; i < 4; i++)
  222. {
  223. ASSERT_TRUE(listener.simple.at(i) == root_ns_a::EmptyMessage());
  224. }
  225. }