7 #include <gtest/gtest.h> 41 std::cout <<
"RX frame [flags=" << flags <<
"]: " << frame.toString() << std::endl;
44 rx_frames.push_back(frame);
61 ASSERT_TRUE(dispatcher.
setNodeID(SELF_NODE_ID));
62 ASSERT_FALSE(dispatcher.
setNodeID(SELF_NODE_ID));
63 ASSERT_EQ(SELF_NODE_ID, dispatcher.
getNodeID());
74 ASSERT_TRUE(rx_listener.
rx_frames.empty());
87 typedef std::unique_ptr<TestListener> TestListenerPtr;
88 static const int MaxBufSize = 512;
89 static const int NumSubscribers = 6;
90 TestListenerPtr subscribers[NumSubscribers] =
100 static const std::string DATA[6] =
102 "Yes, man is mortal, but that would be only half the trouble. " 103 "The worst of it is that he's sometimes unexpectedly mortal - there's the trick!",
105 "In fact, I'm beginning to fear that this confusion will go on for a long time. " 106 "And all because he writes down what I said incorrectly.",
108 "I had the pleasure of meeting that young man at the Patriarch's Ponds. " 109 "He almost drove me mad myself, proving to me that I don't exist. But you do believe that it is really I?",
111 "He was a dreamer, a thinker, a speculative philosopher... or, as his wife would have it, an idiot.",
113 "The only way to get ideas for stories is to drink way too much coffee and buy a desk that doesn't " 114 "collapse when you beat your head against it",
119 for (
unsigned i = 0; i <
sizeof(DATA) /
sizeof(DATA[0]); i++)
121 std::cout <<
"Size of test data chunk " << i <<
": " << DATA[i].length() << std::endl;
140 for (
int i = 0; i < NumSubscribers; i++)
142 ASSERT_FALSE(dispatcher.
hasSubscriber(subscribers[i]->getDataTypeDescriptor().getID()));
143 ASSERT_FALSE(dispatcher.
hasPublisher(subscribers[i]->getDataTypeDescriptor().getID()));
144 ASSERT_FALSE(dispatcher.
hasServer(subscribers[i]->getDataTypeDescriptor().getID()));
154 for (
int i = 0; i < NumSubscribers; i++)
156 ASSERT_FALSE(dispatcher.
hasPublisher(subscribers[i]->getDataTypeDescriptor().getID()));
160 ASSERT_TRUE(dispatcher.
hasSubscriber(subscribers[0]->getDataTypeDescriptor().getID()));
161 ASSERT_TRUE(dispatcher.
hasSubscriber(subscribers[1]->getDataTypeDescriptor().getID()));
162 ASSERT_TRUE(dispatcher.
hasSubscriber(subscribers[2]->getDataTypeDescriptor().getID()));
165 ASSERT_TRUE(dispatcher.
hasServer(subscribers[3]->getDataTypeDescriptor().getID()));
178 for (
int i = 0; i < NumSubscribers; i++)
180 ASSERT_TRUE(subscribers[i]->isEmpty());
183 emulator.send(transfers);
184 emulator.send(transfers);
188 const int res = dispatcher.
spinOnce();
200 ASSERT_TRUE(subscribers[0]->matchAndPop(transfers[4]));
201 ASSERT_TRUE(subscribers[0]->matchAndPop(transfers[0]));
203 ASSERT_TRUE(subscribers[1]->matchAndPop(transfers[4]));
204 ASSERT_TRUE(subscribers[1]->matchAndPop(transfers[0]));
206 ASSERT_TRUE(subscribers[2]->matchAndPop(transfers[5]));
207 ASSERT_TRUE(subscribers[2]->matchAndPop(transfers[1]));
209 ASSERT_TRUE(subscribers[3]->matchAndPop(transfers[2]));
211 ASSERT_TRUE(subscribers[4]->matchAndPop(transfers[3]));
213 ASSERT_TRUE(subscribers[5]->matchAndPop(transfers[3]));
215 for (
int i = 0; i < NumSubscribers; i++)
217 ASSERT_TRUE(subscribers[i]->isEmpty());
244 std::cout <<
"Num received frames: " << rx_listener.
rx_frames.size() << std::endl;
245 ASSERT_EQ(292, rx_listener.
rx_frames.size());
257 ASSERT_TRUE(dispatcher.
setNodeID(SELF_NODE_ID));
258 ASSERT_FALSE(dispatcher.
setNodeID(SELF_NODE_ID));
274 frame.
setPayload(reinterpret_cast<const uint8_t*>(
"123"), 3);
284 ASSERT_EQ(2, dispatcher.
send(frame, TX_DEADLINE,
tsMono(0), 0, 0xFF));
290 ASSERT_TRUE(frame.
compile(expected_can_frame));
292 ASSERT_TRUE(driver.
ifaces.at(0).matchAndPopTx(expected_can_frame, TX_DEADLINE));
293 ASSERT_TRUE(driver.
ifaces.at(1).matchAndPopTx(expected_can_frame, TX_DEADLINE));
295 ASSERT_TRUE(driver.
ifaces.at(0).tx.empty());
296 ASSERT_TRUE(driver.
ifaces.at(1).tx.empty());
308 ASSERT_TRUE(rx_listener.rx_frames.empty());
320 ASSERT_TRUE(dispatcher.
setNodeID(SELF_NODE_ID));
321 ASSERT_FALSE(dispatcher.
setNodeID(SELF_NODE_ID));
328 ASSERT_EQ(0, dispatcher.
spinOnce());
341 :
uavcan::LoopbackFrameListenerBase(dispatcher)
350 std::cout <<
"DispatcherTestLoopbackFrameListener: " << frame.toString() << std::endl;
364 ASSERT_TRUE(dispatcher.
setNodeID(SELF_NODE_ID));
375 frame.setPayload(reinterpret_cast<const uint8_t*>(
"123"), 3);
387 ASSERT_EQ(2, listener.
count);
LoopbackFrameListenerRegistry & getLoopbackFrameListenerRegistry()
uint64_t monotonic_auto_advance
const uint64_t & getErrorCount() const
void sendOneFrame(const uavcan::RxFrame &frame)
std::vector< uavcan::CanRxFrame > rx_frames
int send(const Frame &frame, MonotonicTime tx_deadline, MonotonicTime blocking_deadline, CanIOFlags flags, uint8_t iface_mask)
bool registerServiceRequestListener(TransferListener *listener)
bool hasSubscriber(DataTypeID dtid) const
IRxFrameListener * getRxFrameListener() const
int spin(MonotonicTime deadline)
void unregisterServiceRequestListener(TransferListener *listener)
virtual uavcan::ICanIface * getIface(uavcan::uint8_t iface_index)
bool hasPublisher(DataTypeID dtid) const
unsigned getNumServiceResponseListeners() const
uint8_t setPayload(const uint8_t *data, unsigned len)
void installRxFrameListener(IRxFrameListener *listener)
void handleLoopbackFrame(const uavcan::RxFrame &frame)
void advance(uint64_t usec) const
static MonotonicTime fromMSec(uint64_t ms)
bool hasServer(DataTypeID dtid) const
void unregisterServiceResponseListener(TransferListener *listener)
const uint64_t & getTxTransferCount() const
unsigned getNumServiceRequestListeners() const
void unregisterMessageListener(TransferListener *listener)
unsigned getNumListeners() const
const TransferPerfCounter & getTransferPerfCounter() const
uint8_t getIfaceIndex() const
const uint64_t & getRxTransferCount() const
bool registerMessageListener(TransferListener *listener)
bool compile(CanFrame &can_frame) const
bool registerServiceResponseListener(TransferListener *listener)
uavcan::MonotonicTime tsMono(uint64_t usec)
uavcan::RxFrame last_frame
TEST(Dispatcher, Reception)
bool setNodeID(NodeID nid)
class UAVCAN_EXPORT Dispatcher
DispatcherTransferEmulator(CanDriverMock &target, uavcan::NodeID dst_node_id=127)
TransferID * accessOrCreate(const OutgoingTransferRegistryKey &key, MonotonicTime new_deadline)
unsigned getNumMessageListeners() const
DispatcherTestLoopbackFrameListener(uavcan::Dispatcher &dispatcher)
static const CanIOFlags CanIOFlagLoopback
virtual void handleRxFrame(const uavcan::CanRxFrame &frame, uavcan::CanIOFlags flags)
static const uavcan::NodeID SELF_NODE_ID(64)
void pushRx(const uavcan::CanFrame &frame)
std::vector< CanIfaceMock > ifaces
OutgoingTransferRegistry & getOutgoingTransferRegistry()