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

Provides a common queuing and thread layer for running the CAN stack and all CAN drivers. More...

#include <can_hardware_interface.hpp>

Collaboration diagram for isobus::CANHardwareInterface:
[legend]

Classes

class  CANHardware
 Stores the data for a single CAN channel. More...
 

Static Public Member Functions

static std::uint8_t get_number_of_can_channels ()
 Returns the number of configured CAN channels that the class is managing.
 
static bool set_number_of_can_channels (std::uint8_t value, std::size_t queueCapacity=40)
 Sets the number of CAN channels to manage.
 
static bool assign_can_channel_frame_handler (std::uint8_t channelIndex, std::shared_ptr< CANHardwarePlugin > canDriver)
 Assigns a CAN driver to a channel.
 
static bool unassign_can_channel_frame_handler (std::uint8_t channelIndex)
 Removes a CAN driver from a channel.
 
static std::shared_ptr< CANHardwarePluginget_assigned_can_channel_frame_handler (std::uint8_t channelIndex)
 Gets the CAN driver assigned to a channel.
 
static bool start ()
 Starts the threads for managing the CAN stack and CAN drivers.
 
static bool stop ()
 Stops all CAN management threads and discards all remaining messages in the Tx and Rx queues.
 
static bool is_running ()
 Checks if the CAN stack and CAN drivers are running.
 
static bool transmit_can_frame (const CANMessageFrame &frame)
 Called externally, adds a message to a CAN channel's Tx queue.
 
static EventDispatcher< const CANMessageFrame & > & get_can_frame_received_event_dispatcher ()
 Get the event dispatcher for when a CAN message frame is received from hardware event.
 
static EventDispatcher< const CANMessageFrame & > & get_can_frame_transmitted_event_dispatcher ()
 Get the event dispatcher for when a CAN message frame will be send to hardware event.
 
static EventDispatcher & get_periodic_update_event_dispatcher ()
 Get the event dispatcher for when a periodic update is called.
 
static void update ()
 Call this periodically if you have threads disabled.
 
static void set_periodic_update_interval (std::uint32_t value)
 Set the interval between periodic updates to the network manager.
 
static std::uint32_t get_periodic_update_interval ()
 Get the interval between periodic updates to the network manager.
 

Private Member Functions

virtual ~CANHardwareInterface ()
 Deconstructor for the CANHardwareInterface class for stopping threads.
 

Static Private Member Functions

static void update_thread_function ()
 The main thread loop for updating the stack.
 
static void start_threads ()
 Starts all threads related to the hardware interface.
 
static void stop_threads ()
 Stops all threads related to the hardware interface.
 

Static Private Attributes

static CANHardwareInterface SINGLETON
 Singleton instance of the CANHardwareInterface class.
 
static constexpr std::uint32_t PERIODIC_UPDATE_INTERVAL = 4
 The default update interval for the CAN stack. Mostly arbitrary.
 
static std::unique_ptr< std::thread > updateThread
 The main thread.
 
static std::condition_variable updateThreadWakeupCondition
 A condition variable to allow for signaling the updateThread to wakeup.
 
static std::uint32_t lastUpdateTimestamp
 The last time the network manager was updated.
 
static std::uint32_t periodicUpdateInterval = PERIODIC_UPDATE_INTERVAL
 The period between calls to the network manager update function in milliseconds.
 
static EventDispatcher< const CANMessageFrame & > frameReceivedEventDispatcher
 The event dispatcher for when a CAN message frame is received from hardware event.
 
static EventDispatcher< const CANMessageFrame & > frameTransmittedEventDispatcher
 The event dispatcher for when a CAN message has been transmitted via hardware.
 
static EventDispatcher periodicUpdateEventDispatcher
 The event dispatcher for when a periodic update is called.
 
static std::vector< std::unique_ptr< CANHardware > > hardwareChannels
 A list of all CAN channel's metadata.
 
static Mutex hardwareChannelsMutex
 Mutex to protect hardwareChannels
 
static Mutex updateMutex
 A mutex for the main thread.
 
static bool started = false
 Stores if the threads have been started.
 

Detailed Description

Provides a common queuing and thread layer for running the CAN stack and all CAN drivers.

The CANHardwareInterface class was created to provide a common queuing and thread layer for running the CAN stack and all CAN drivers to simplify integration and crucially to provide a consistent, safe order of operations for all the function calls needed to properly drive the stack.

Definition at line 36 of file can_hardware_interface.hpp.

Constructor & Destructor Documentation

◆ ~CANHardwareInterface()

isobus::CANHardwareInterface::~CANHardwareInterface ( )
privatevirtual

Deconstructor for the CANHardwareInterface class for stopping threads.

Definition at line 105 of file can_hardware_interface.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ assign_can_channel_frame_handler()

bool isobus::CANHardwareInterface::assign_can_channel_frame_handler ( std::uint8_t channelIndex,
std::shared_ptr< CANHardwarePlugin > canDriver )
static

Assigns a CAN driver to a channel.

Parameters
[in]channelIndexThe channel to assign to
[in]canDriverThe driver to assign to the channel
Note
The function will fail if the channel is already assigned to a driver or the interface is already started
Returns
true if the driver was assigned to the channel, otherwise false

Definition at line 182 of file can_hardware_interface.cpp.

◆ get_assigned_can_channel_frame_handler()

std::shared_ptr< CANHardwarePlugin > isobus::CANHardwareInterface::get_assigned_can_channel_frame_handler ( std::uint8_t channelIndex)
static

Gets the CAN driver assigned to a channel.

Parameters
[in]channelIndexThe channel to get the driver from
Returns
The driver assigned to the channel, or nullptr if the channel is not assigned

Definition at line 242 of file can_hardware_interface.cpp.

◆ get_can_frame_received_event_dispatcher()

EventDispatcher< const CANMessageFrame & > & isobus::CANHardwareInterface::get_can_frame_received_event_dispatcher ( )
static

Get the event dispatcher for when a CAN message frame is received from hardware event.

Returns
The event dispatcher which can be used to register callbacks/listeners to

Definition at line 328 of file can_hardware_interface.cpp.

◆ get_can_frame_transmitted_event_dispatcher()

EventDispatcher< const CANMessageFrame & > & isobus::CANHardwareInterface::get_can_frame_transmitted_event_dispatcher ( )
static

Get the event dispatcher for when a CAN message frame will be send to hardware event.

Returns
The event dispatcher which can be used to register callbacks/listeners to

Definition at line 333 of file can_hardware_interface.cpp.

◆ get_number_of_can_channels()

std::uint8_t isobus::CANHardwareInterface::get_number_of_can_channels ( )
static

Returns the number of configured CAN channels that the class is managing.

Returns
The number of configured CAN channels that the class is managing

Definition at line 210 of file can_hardware_interface.cpp.

◆ get_periodic_update_event_dispatcher()

EventDispatcher & isobus::CANHardwareInterface::get_periodic_update_event_dispatcher ( )
static

Get the event dispatcher for when a periodic update is called.

Returns
The event dispatcher which can be used to register callbacks/listeners to

Definition at line 338 of file can_hardware_interface.cpp.

◆ get_periodic_update_interval()

std::uint32_t isobus::CANHardwareInterface::get_periodic_update_interval ( )
static

Get the interval between periodic updates to the network manager.

Returns
The interval between update calls in milliseconds

Definition at line 348 of file can_hardware_interface.cpp.

◆ is_running()

bool isobus::CANHardwareInterface::is_running ( )
static

Checks if the CAN stack and CAN drivers are running.

Returns
true if the threads are running, otherwise false

Definition at line 290 of file can_hardware_interface.cpp.

◆ set_number_of_can_channels()

bool isobus::CANHardwareInterface::set_number_of_can_channels ( std::uint8_t value,
std::size_t queueCapacity = 40 )
static

Sets the number of CAN channels to manage.

Allocates the proper number of CanHardware objects to track each CAN channel's Tx and Rx message queues. If you pass in a smaller number than what was already configured, it will delete the unneeded CanHardware objects.

Note
The function will fail if the channel is already assigned to a driver or the interface is already started
Parameters
valueThe number of CAN channels to manage
queueCapacityThe capacity of the transmit and receive queues
Returns
true if the channel count was set, otherwise false.

Definition at line 161 of file can_hardware_interface.cpp.

◆ set_periodic_update_interval()

void isobus::CANHardwareInterface::set_periodic_update_interval ( std::uint32_t value)
static

Set the interval between periodic updates to the network manager.

Parameters
[in]valueThe interval between update calls in milliseconds

Definition at line 343 of file can_hardware_interface.cpp.

◆ start()

bool isobus::CANHardwareInterface::start ( )
static

Starts the threads for managing the CAN stack and CAN drivers.

Returns
true if the threads were started, otherwise false (perhaps they are already running)

Definition at line 253 of file can_hardware_interface.cpp.

Here is the call graph for this function:

◆ start_threads()

void isobus::CANHardwareInterface::start_threads ( )
staticprivate

Starts all threads related to the hardware interface.

Definition at line 424 of file can_hardware_interface.cpp.

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

◆ stop()

bool isobus::CANHardwareInterface::stop ( )
static

Stops all CAN management threads and discards all remaining messages in the Tx and Rx queues.

Returns
true if the threads were stopped, otherwise false

Definition at line 268 of file can_hardware_interface.cpp.

Here is the call graph for this function:

◆ stop_threads()

void isobus::CANHardwareInterface::stop_threads ( )
staticprivate

Stops all threads related to the hardware interface.

Definition at line 433 of file can_hardware_interface.cpp.

Here is the caller graph for this function:

◆ transmit_can_frame()

bool isobus::CANHardwareInterface::transmit_can_frame ( const CANMessageFrame & frame)
static

Called externally, adds a message to a CAN channel's Tx queue.

Parameters
[in]frameThe frame to add to the Tx queue
Returns
true if the frame was accepted, otherwise false (maybe wrong channel assigned)

Definition at line 295 of file can_hardware_interface.cpp.

Here is the caller graph for this function:

◆ unassign_can_channel_frame_handler()

bool isobus::CANHardwareInterface::unassign_can_channel_frame_handler ( std::uint8_t channelIndex)
static

Removes a CAN driver from a channel.

Parameters
[in]channelIndexThe channel to remove the driver from
Note
The function will fail if the channel is already assigned to a driver or the interface is already started
Returns
true if the driver was removed from the channel, otherwise false

Definition at line 215 of file can_hardware_interface.cpp.

◆ update()

void isobus::CANHardwareInterface::update ( )
static

Call this periodically if you have threads disabled.

Note
Try to call this very often, say at least every millisecond to ensure CAN messages are retrieved from the hardware

Definition at line 353 of file can_hardware_interface.cpp.

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

◆ update_thread_function()

void isobus::CANHardwareInterface::update_thread_function ( )
staticprivate

The main thread loop for updating the stack.

Definition at line 410 of file can_hardware_interface.cpp.

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

Member Data Documentation

◆ frameReceivedEventDispatcher

EventDispatcher< const CANMessageFrame & > isobus::CANHardwareInterface::frameReceivedEventDispatcher
staticprivate

The event dispatcher for when a CAN message frame is received from hardware event.

Definition at line 186 of file can_hardware_interface.hpp.

◆ frameTransmittedEventDispatcher

EventDispatcher< const CANMessageFrame & > isobus::CANHardwareInterface::frameTransmittedEventDispatcher
staticprivate

The event dispatcher for when a CAN message has been transmitted via hardware.

Definition at line 187 of file can_hardware_interface.hpp.

◆ hardwareChannels

std::vector< std::unique_ptr< CANHardwareInterface::CANHardware > > isobus::CANHardwareInterface::hardwareChannels
staticprivate

A list of all CAN channel's metadata.

Definition at line 190 of file can_hardware_interface.hpp.

◆ hardwareChannelsMutex

Mutex isobus::CANHardwareInterface::hardwareChannelsMutex
staticprivate

Mutex to protect hardwareChannels

Definition at line 191 of file can_hardware_interface.hpp.

◆ lastUpdateTimestamp

std::uint32_t isobus::CANHardwareInterface::lastUpdateTimestamp
staticprivate

The last time the network manager was updated.

Definition at line 184 of file can_hardware_interface.hpp.

◆ PERIODIC_UPDATE_INTERVAL

constexpr std::uint32_t isobus::CANHardwareInterface::PERIODIC_UPDATE_INTERVAL = 4
staticconstexprprivate

The default update interval for the CAN stack. Mostly arbitrary.

Definition at line 166 of file can_hardware_interface.hpp.

◆ periodicUpdateEventDispatcher

EventDispatcher isobus::CANHardwareInterface::periodicUpdateEventDispatcher
staticprivate

The event dispatcher for when a periodic update is called.

Definition at line 188 of file can_hardware_interface.hpp.

◆ periodicUpdateInterval

std::uint32_t isobus::CANHardwareInterface::periodicUpdateInterval = PERIODIC_UPDATE_INTERVAL
staticprivate

The period between calls to the network manager update function in milliseconds.

Definition at line 185 of file can_hardware_interface.hpp.

◆ SINGLETON

CANHardwareInterface isobus::CANHardwareInterface::SINGLETON
staticprivate

Singleton instance of the CANHardwareInterface class.

This is a little hack that allows to have the destructor called

Definition at line 163 of file can_hardware_interface.hpp.

◆ started

bool isobus::CANHardwareInterface::started = false
staticprivate

Stores if the threads have been started.

Definition at line 193 of file can_hardware_interface.hpp.

◆ updateMutex

Mutex isobus::CANHardwareInterface::updateMutex
staticprivate

A mutex for the main thread.

Definition at line 192 of file can_hardware_interface.hpp.

◆ updateThread

std::unique_ptr< std::thread > isobus::CANHardwareInterface::updateThread
staticprivate

The main thread.

Definition at line 181 of file can_hardware_interface.hpp.

◆ updateThreadWakeupCondition

std::condition_variable isobus::CANHardwareInterface::updateThreadWakeupCondition
staticprivate

A condition variable to allow for signaling the updateThread to wakeup.

Definition at line 182 of file can_hardware_interface.hpp.


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