10#ifndef CAN_HARDWARE_INTERFACE_HPP
11#define CAN_HARDWARE_INTERFACE_HPP
16#include "isobus/utility/event_dispatcher.hpp"
24#if !defined CAN_STACK_DISABLE_THREADS && !defined ARDUINO
25#include <condition_variable>
141#if !defined CAN_STACK_DISABLE_THREADS && !defined ARDUINO
168#if !defined CAN_STACK_DISABLE_THREADS && !defined ARDUINO
An abstraction between this CAN stack and any hardware layer.
A base class for a CAN driver. Can be derived into your platform's required interface.
A classical CAN frame, with 8 data bytes.
Stores the data for a single CAN channel.
std::shared_ptr< CANHardwarePlugin > frameHandler
The CAN driver to use for a CAN channel.
CANHardware(std::size_t queueCapacity)
Constructor for the CANHardware.
virtual ~CANHardware()
Destructor for the CANHardware.
LockFreeQueue< CANMessageFrame > messagesToBeTransmittedQueue
Transmit message queue for a CAN channel.
void receive_thread_function()
The receiving thread loop for this CAN channel.
bool receive_can_frame()
Receives a frame from the hardware and adds it to the receive queue.
void start_threads()
Starts the receiving thread for this CAN channel.
bool stop()
Stops this hardware channel.
bool receiveThreadRunning
Flag to indicate if the receive thread is running.
LockFreeQueue< CANMessageFrame > receivedMessagesQueue
Receive message queue for a CAN channel.
bool start()
Starts this hardware channel.
void stop_threads()
Stops the receiving thread for this CAN channel.
bool transmit_can_frame(const CANMessageFrame &frame) const
Try to transmit the frame to the hardware.
std::unique_ptr< std::thread > receiveMessageThread
Thread to manage getting messages from a CAN channel.
Provides a common queuing and thread layer for running the CAN stack and all CAN drivers.
static bool stop()
Stops all CAN management threads and discards all remaining messages in the Tx and Rx queues.
static std::vector< std::unique_ptr< CANHardware > > hardwareChannels
A list of all CAN channel's metadata.
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 std::uint8_t get_number_of_can_channels()
Returns the number of configured CAN channels that the class is managing.
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 periodicUpdateEventDispatcher
The event dispatcher for when a periodic update is called.
static Mutex updateMutex
A mutex for the main thread.
static std::uint32_t periodicUpdateInterval
The period between calls to the network manager update function in milliseconds.
static EventDispatcher & get_periodic_update_event_dispatcher()
Get the event dispatcher for when a periodic update is called.
static void stop_threads()
Stops all threads related to the hardware interface.
static bool started
Stores if the threads have been started.
static bool unassign_can_channel_frame_handler(std::uint8_t channelIndex)
Removes a CAN driver from a channel.
static void update()
Call this periodically if you have threads disabled.
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 void start_threads()
Starts all threads related to the hardware interface.
static std::shared_ptr< CANHardwarePlugin > get_assigned_can_channel_frame_handler(std::uint8_t channelIndex)
Gets the CAN driver assigned to a channel.
static bool transmit_can_frame(const CANMessageFrame &frame)
Called externally, adds a message to a CAN channel's Tx queue.
static Mutex hardwareChannelsMutex
Mutex to protect hardwareChannels
static std::uint32_t lastUpdateTimestamp
The last time the network manager was updated.
static CANHardwareInterface SINGLETON
Singleton instance of the CANHardwareInterface class.
virtual ~CANHardwareInterface()
Deconstructor for the CANHardwareInterface class for stopping threads.
static std::uint32_t get_periodic_update_interval()
Get the interval between periodic updates to the network manager.
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 bool is_running()
Checks if the CAN stack and CAN drivers are running.
static bool assign_can_channel_frame_handler(std::uint8_t channelIndex, std::shared_ptr< CANHardwarePlugin > canDriver)
Assigns a CAN driver to a channel.
static constexpr std::uint32_t PERIODIC_UPDATE_INTERVAL
The default update interval for the CAN stack. Mostly arbitrary.
static void update_thread_function()
The main thread loop for updating the stack.
static std::condition_variable updateThreadWakeupCondition
A condition variable to allow for signaling the updateThread to wakeup.
static std::unique_ptr< std::thread > updateThread
The main thread.
static void set_periodic_update_interval(std::uint32_t value)
Set the interval between periodic updates to the network manager.
static bool start()
Starts the threads for managing the CAN stack and CAN drivers.
A CAN frame for interfacing with a hardware layer, like socket CAN or other interface.
This namespace encompasses all of the ISO11783 stack's functionality to reduce global namespace pollu...