AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
Classes | Public Member Functions | Private Attributes | Static Private Attributes | List of all members
isobus::VirtualCANPlugin Class Reference

An OS and hardware independent virtual CAN interface driver for testing purposes. More...

#include <virtual_can_plugin.hpp>

Inheritance diagram for isobus::VirtualCANPlugin:
[legend]
Collaboration diagram for isobus::VirtualCANPlugin:
[legend]

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< VirtualDeviceourDevice
 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.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ VirtualCANPlugin()

isobus::VirtualCANPlugin::VirtualCANPlugin ( const std::string channel = "",
const bool receiveOwnMessages = false )

Constructor for the virtual CAN driver.

Parameters
[in]channelThe virtual channel name to use. Free to choose.
[in]receiveOwnMessagesIf true, the driver will receive its own messages.

Definition at line 16 of file virtual_can_plugin.cpp.

◆ ~VirtualCANPlugin()

isobus::VirtualCANPlugin::~VirtualCANPlugin ( )
virtual

Destructor for the virtual CAN driver.

Definition at line 25 of file virtual_can_plugin.cpp.

Member Function Documentation

◆ clear_queue()

void isobus::VirtualCANPlugin::clear_queue ( ) const

Clear the internal received message queue.

Definition at line 103 of file virtual_can_plugin.cpp.

◆ close()

void isobus::VirtualCANPlugin::close ( )
overridevirtual

Closes the socket.

Implements isobus::CANHardwarePlugin.

Definition at line 47 of file virtual_can_plugin.cpp.

◆ get_channel_name()

std::string isobus::VirtualCANPlugin::get_channel_name ( ) const

Returns the assigned virtual channel name.

Returns
The virtual channel name the bus is assigned to

Definition at line 37 of file virtual_can_plugin.cpp.

◆ get_is_valid()

bool isobus::VirtualCANPlugin::get_is_valid ( ) const
overridevirtual

Returns if the socket connection is valid.

Returns
true if connected, false if not connected

Implements isobus::CANHardwarePlugin.

Definition at line 32 of file virtual_can_plugin.cpp.

◆ get_queue_empty()

bool isobus::VirtualCANPlugin::get_queue_empty ( ) const

Returns if the internal received message queue is empty or not.

Returns
true if the internal received message queue is empty, otherwise false

Definition at line 97 of file virtual_can_plugin.cpp.

◆ open()

void isobus::VirtualCANPlugin::open ( )
overridevirtual

Connects to the socket.

Implements isobus::CANHardwarePlugin.

Definition at line 42 of file virtual_can_plugin.cpp.

◆ read_frame() [1/2]

bool isobus::VirtualCANPlugin::read_frame ( isobus::CANMessageFrame & canFrame)
overridevirtual

Returns a frame from the hardware (synchronous), or false if no frame can be read. Times out after 1 second.

Parameters
[in,out]canFrameThe CAN frame that was read
Returns
true if a CAN frame was read, otherwise false

Implements isobus::CANHardwarePlugin.

Definition at line 79 of file virtual_can_plugin.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_frame() [2/2]

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.

Parameters
[in,out]canFrameThe CAN frame that was read
[in]timeoutThe timeout in milliseconds
Returns
true if a CAN frame was read, otherwise false

Definition at line 84 of file virtual_can_plugin.cpp.

◆ write_frame()

bool isobus::VirtualCANPlugin::write_frame ( const isobus::CANMessageFrame & canFrame)
overridevirtual

Writes a frame to the bus (synchronous)

Parameters
[in]canFrameThe frame to write to the bus
Returns
true if the frame was written, otherwise false

Implements isobus::CANHardwarePlugin.

Definition at line 53 of file virtual_can_plugin.cpp.

◆ write_frame_as_if_received()

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.

Parameters
[in]canFrameThe frame to write to the bus

Definition at line 72 of file virtual_can_plugin.cpp.

Member Data Documentation

◆ channel

const std::string isobus::VirtualCANPlugin::channel
private

The virtual channel name.

Definition at line 100 of file virtual_can_plugin.hpp.

◆ channels

std::map< std::string, std::vector< std::shared_ptr< VirtualCANPlugin::VirtualDevice > > > isobus::VirtualCANPlugin::channels
staticprivate

A channel is a vector of devices.

Definition at line 98 of file virtual_can_plugin.hpp.

◆ MAX_QUEUE_SIZE

constexpr size_t isobus::VirtualCANPlugin::MAX_QUEUE_SIZE = 1000
staticconstexprprivate

The maximum size of the queue, mostly arbitrary.

Definition at line 95 of file virtual_can_plugin.hpp.

◆ mutex

std::mutex isobus::VirtualCANPlugin::mutex
staticprivate

Mutex to access channels and queues for thread safety.

Definition at line 97 of file virtual_can_plugin.hpp.

◆ ourDevice

std::shared_ptr<VirtualDevice> isobus::VirtualCANPlugin::ourDevice
private

A pointer to the virtual device of this instance.

Definition at line 103 of file virtual_can_plugin.hpp.

◆ receiveOwnMessages

const bool isobus::VirtualCANPlugin::receiveOwnMessages
private

If true, the driver will receive its own messages.

Definition at line 101 of file virtual_can_plugin.hpp.

◆ running

std::atomic_bool isobus::VirtualCANPlugin::running
private

If true, the driver is running.

Definition at line 104 of file virtual_can_plugin.hpp.


The documentation for this class was generated from the following files: