Testing helper class - InterlinkedTestNodes

This commit is contained in:
Pavel Kirienko
2014-03-15 12:58:39 +04:00
parent bb5f77857d
commit 206551bf78
2 changed files with 118 additions and 87 deletions
+104 -1
View File
@@ -5,7 +5,9 @@
#pragma once
#include <uavcan/node/abstract_node.hpp>
#include <uavcan/util/lazy_constructor.hpp>
#include <memory>
#include "../transport/can/can.hpp"
struct TestNode : public uavcan::INode
{
@@ -34,3 +36,104 @@ struct TestNode : public uavcan::INode
uavcan::IMarshalBufferProvider& getMarshalBufferProvider() { return buffer_provider; }
};
struct PairableCanDriver : public uavcan::ICanDriver, public uavcan::ICanIface
{
uavcan::ISystemClock& clock;
PairableCanDriver* other;
std::queue<uavcan::CanFrame> read_queue;
PairableCanDriver(uavcan::ISystemClock& clock)
: clock(clock)
, other(NULL)
{ }
void linkTogether(PairableCanDriver* with)
{
this->other = with;
with->other = this;
}
uavcan::ICanIface* getIface(int iface_index)
{
if (iface_index == 0)
return this;
return NULL;
}
int getNumIfaces() const { return 1; }
int select(int& inout_write_iface_mask, int& inout_read_iface_mask, uavcan::MonotonicTime blocking_deadline)
{
assert(other);
if (inout_read_iface_mask == 1)
inout_read_iface_mask = read_queue.size() ? 1 : 0;
if (inout_read_iface_mask || inout_write_iface_mask)
return 1;
while (clock.getMonotonic() < blocking_deadline)
usleep(1000);
return 0;
}
int send(const uavcan::CanFrame& frame, uavcan::MonotonicTime)
{
assert(other);
other->read_queue.push(frame);
return 1;
}
int receive(uavcan::CanFrame& out_frame, uavcan::MonotonicTime& out_ts_monotonic, uavcan::UtcTime& out_ts_utc)
{
assert(other);
assert(read_queue.size());
out_frame = read_queue.front();
read_queue.pop();
out_ts_monotonic = clock.getMonotonic();
out_ts_utc = clock.getUtc();
return 1;
}
int configureFilters(const uavcan::CanFilterConfig*, int) { return -1; }
int getNumFilters() const { return 0; }
uint64_t getNumErrors() const { return 0; }
};
struct InterlinkedTestNodes
{
SystemClockDriver clock;
PairableCanDriver can_a;
PairableCanDriver can_b;
TestNode a;
TestNode b;
InterlinkedTestNodes(uavcan::NodeID nid_first, uavcan::NodeID nid_second)
: can_a(clock)
, can_b(clock)
, a(can_a, clock, nid_first)
, b(can_b, clock, nid_second)
{
can_a.linkTogether(&can_b);
}
InterlinkedTestNodes()
: can_a(clock)
, can_b(clock)
, a(can_a, clock, 1)
, b(can_b, clock, 2)
{
can_a.linkTogether(&can_b);
}
int spinBoth(uavcan::MonotonicDuration duration)
{
const uavcan::MonotonicDuration duration_per_node = duration * 0.5;
const int ret = a.spin(duration_per_node);
if (ret < 0)
return ret;
return b.spin(duration_per_node);
}
};