AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
|
An OS and hardware independent virtual CAN interface driver for testing purposes. More...
#include <virtual_can_plugin.hpp>
Classes | |
struct | VirtualDevice |
A struct holding information about a virtual CAN device. More... | |
Public Member Functions | |
VirtualCANPlugin (const std::string channel="", const bool receiveOwnMessages=false) | |
Constructor for the virtual CAN driver. | |
virtual | ~VirtualCANPlugin () |
Destructor for the virtual CAN driver. | |
bool | get_is_valid () const override |
Returns if the socket connection is valid. | |
std::string | get_channel_name () const |
Returns the assigned virtual channel name. | |
void | close () override |
Closes the socket. | |
void | open () override |
Connects to the socket. | |
bool | read_frame (isobus::CANMessageFrame &canFrame) override |
Returns a frame from the hardware (synchronous), or false if no frame can be read. Times out after 1 second. | |
bool | read_frame (isobus::CANMessageFrame &canFrame, std::uint32_t timeout) const |
Returns a frame from the hardware (synchronous), or false if no frame can be read. | |
bool | write_frame (const isobus::CANMessageFrame &canFrame) override |
Writes a frame to the bus (synchronous) | |
void | write_frame_as_if_received (const isobus::CANMessageFrame &canFrame) const |
Allows us to write messages as if we received them from the bus. | |
bool | get_queue_empty () const |
Returns if the internal received message queue is empty or not. | |
void | clear_queue () const |
Clear the internal received message queue. | |
Private Attributes | |
const std::string | channel |
The virtual channel name. | |
const bool | receiveOwnMessages |
If true , the driver will receive its own messages. | |
std::shared_ptr< VirtualDevice > | ourDevice |
A pointer to the virtual device of this instance. | |
std::atomic_bool | running |
If true , the driver is running. | |
Static Private Attributes | |
static constexpr size_t | MAX_QUEUE_SIZE = 1000 |
The maximum size of the queue, mostly arbitrary. | |
static std::mutex | mutex |
Mutex to access channels and queues for thread safety. | |
static std::map< std::string, std::vector< std::shared_ptr< VirtualDevice > > > | channels |
A channel is a vector of devices. | |
An OS and hardware independent virtual CAN interface driver for testing purposes.
Any instance connecting to the same channel and in the same process can communicate. However, this plugin does not implement rate limiting or any other CAN bus specific features, like prioritization under heavy load.
Definition at line 35 of file virtual_can_plugin.hpp.
isobus::VirtualCANPlugin::VirtualCANPlugin | ( | const std::string | channel = "", |
const bool | receiveOwnMessages = false ) |
Constructor for the virtual CAN driver.
[in] | channel | The virtual channel name to use. Free to choose. |
[in] | receiveOwnMessages | If true , the driver will receive its own messages. |
Definition at line 16 of file virtual_can_plugin.cpp.
|
virtual |
Destructor for the virtual CAN driver.
Definition at line 25 of file virtual_can_plugin.cpp.
void isobus::VirtualCANPlugin::clear_queue | ( | ) | const |
Clear the internal received message queue.
Definition at line 103 of file virtual_can_plugin.cpp.
|
overridevirtual |
Closes the socket.
Implements isobus::CANHardwarePlugin.
Definition at line 47 of file virtual_can_plugin.cpp.
std::string isobus::VirtualCANPlugin::get_channel_name | ( | ) | const |
Returns the assigned virtual channel name.
Definition at line 37 of file virtual_can_plugin.cpp.
|
overridevirtual |
Returns if the socket connection is valid.
true
if connected, false
if not connected Implements isobus::CANHardwarePlugin.
Definition at line 32 of file virtual_can_plugin.cpp.
bool isobus::VirtualCANPlugin::get_queue_empty | ( | ) | const |
Returns if the internal received message queue is empty or not.
true
if the internal received message queue is empty, otherwise false Definition at line 97 of file virtual_can_plugin.cpp.
|
overridevirtual |
Connects to the socket.
Implements isobus::CANHardwarePlugin.
Definition at line 42 of file virtual_can_plugin.cpp.
|
overridevirtual |
Returns a frame from the hardware (synchronous), or false
if no frame can be read. Times out after 1 second.
[in,out] | canFrame | The CAN frame that was read |
true
if a CAN frame was read, otherwise false
Implements isobus::CANHardwarePlugin.
Definition at line 79 of file virtual_can_plugin.cpp.
bool isobus::VirtualCANPlugin::read_frame | ( | isobus::CANMessageFrame & | canFrame, |
std::uint32_t | timeout ) const |
Returns a frame from the hardware (synchronous), or false
if no frame can be read.
[in,out] | canFrame | The CAN frame that was read |
[in] | timeout | The timeout in milliseconds |
true
if a CAN frame was read, otherwise false
Definition at line 84 of file virtual_can_plugin.cpp.
|
overridevirtual |
Writes a frame to the bus (synchronous)
[in] | canFrame | The frame to write to the bus |
true
if the frame was written, otherwise false
Implements isobus::CANHardwarePlugin.
Definition at line 53 of file virtual_can_plugin.cpp.
void isobus::VirtualCANPlugin::write_frame_as_if_received | ( | const isobus::CANMessageFrame & | canFrame | ) | const |
Allows us to write messages as if we received them from the bus.
[in] | canFrame | The frame to write to the bus |
Definition at line 72 of file virtual_can_plugin.cpp.
|
private |
The virtual channel name.
Definition at line 100 of file virtual_can_plugin.hpp.
|
staticprivate |
A channel is a vector of devices.
Definition at line 98 of file virtual_can_plugin.hpp.
|
staticconstexprprivate |
The maximum size of the queue, mostly arbitrary.
Definition at line 95 of file virtual_can_plugin.hpp.
|
staticprivate |
Mutex to access channels and queues for thread safety.
Definition at line 97 of file virtual_can_plugin.hpp.
|
private |
A pointer to the virtual device of this instance.
Definition at line 103 of file virtual_can_plugin.hpp.
|
private |
If true
, the driver will receive its own messages.
Definition at line 101 of file virtual_can_plugin.hpp.
|
private |
If true
, the driver is running.
Definition at line 104 of file virtual_can_plugin.hpp.