Skip to content

Commit

Permalink
[WIP] Introduce a fake CAN sender delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBowman committed Dec 30, 2023
1 parent c1301d5 commit 7241551
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
10 changes: 10 additions & 0 deletions libraries/YarpPlugins/CanBusBroker/DeviceDriverImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ bool CanBusBroker::open(yarp::os::Searchable & config)
iCanBusSharer->registerSender(broker->getWriter()->getDelegate());
}
}
else
{
auto * broker = new FakeBusBroker();
brokers.push_back(broker);

for (auto * iCanBusSharer : busSharers)
{
iCanBusSharer->registerSender(broker->getDelegate());
}
}
}

for (auto * broker : brokers)
Expand Down
14 changes: 14 additions & 0 deletions libraries/YarpPlugins/CanBusBroker/SingleBusBroker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "CanUtils.hpp"
#include "LogComponent.hpp"
#include "YarpCanSenderDelegate.hpp"

using namespace roboticslab;

Expand Down Expand Up @@ -323,3 +324,16 @@ void SingleBusBroker::onRead(yarp::os::Bottle & b)
}

// -----------------------------------------------------------------------------

FakeBusBroker::FakeBusBroker()
: sender(new FakeCanSenderDelegate())
{ }

// -----------------------------------------------------------------------------

FakeBusBroker::~FakeBusBroker()
{
delete sender;
}

// -----------------------------------------------------------------------------
22 changes: 22 additions & 0 deletions libraries/YarpPlugins/CanBusBroker/SingleBusBroker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ class SingleBusBroker final : public yarp::os::TypedReaderCallback<yarp::os::Bot
BusLoadMonitor * busLoadMonitor;
};

/**
* @ingroup CanBusBroker
* @brief Fake message broker of a CAN bus.
*
* This class is used to instantiate a CAN bus broker without a real CAN bus.
*/
class FakeBusBroker final
{
public:
//! Constructor.
FakeBusBroker();

//! Destructor.
~FakeBusBroker();

//! Retrieve a handle to the fake CAN sender delegate.
ICanSenderDelegate * getDelegate();

private:
ICanSenderDelegate * sender;
};

} // namespace roboticslab

#endif // __SINGLE_BUS_BROKER_HPP__
14 changes: 14 additions & 0 deletions libraries/YarpPlugins/CanBusBroker/YarpCanSenderDelegate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ class YarpCanSenderDelegate : public ICanSenderDelegate
std::unordered_map<unsigned int, bool> nodeAvailability;
};

/**
* @ingroup CanBusBroker
* @brief A sender delegate that does nothing.
*/
class FakeCanSenderDelegate : public ICanSenderDelegate
{
public:
bool prepareMessage(const can_message & msg) override
{ return true; }

void reportAvailability(bool available, unsigned int id) override
{ }
};

} // namespace roboticslab

#endif // __YARP_CAN_SENDER_DELEGATE_HPP__

0 comments on commit 7241551

Please sign in to comment.