AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
virtual_can_plugin.hpp
Go to the documentation of this file.
1//================================================================================================
9//================================================================================================
10#ifndef VIRTUAL_CAN_PLUGIN_HPP
11#define VIRTUAL_CAN_PLUGIN_HPP
12
16
17#include <atomic>
18#include <condition_variable>
19#include <deque>
20#include <map>
21#include <mutex>
22#include <string>
23#include <vector>
24
25namespace isobus
26{
27 //================================================================================================
34 //================================================================================================
36 {
37 public:
41 VirtualCANPlugin(const std::string channel = "", const bool receiveOwnMessages = false);
42
44 virtual ~VirtualCANPlugin();
45
48 bool get_is_valid() const override;
49
52 std::string get_channel_name() const;
53
55 void close() override;
56
58 void open() override;
59
63 bool read_frame(isobus::CANMessageFrame &canFrame) override;
64
69 bool read_frame(isobus::CANMessageFrame &canFrame, std::uint32_t timeout) const;
70
74 bool write_frame(const isobus::CANMessageFrame &canFrame) override;
75
78 void write_frame_as_if_received(const isobus::CANMessageFrame &canFrame) const;
79
82 bool get_queue_empty() const;
83
85 void clear_queue() const;
86
87 private:
90 {
91 std::deque<isobus::CANMessageFrame> queue;
92 std::condition_variable condition;
93 };
94
95 static constexpr size_t MAX_QUEUE_SIZE = 1000;
96
97 static std::mutex mutex;
98 static std::map<std::string, std::vector<std::shared_ptr<VirtualDevice>>> channels;
99
100 const std::string channel;
102
103 std::shared_ptr<VirtualDevice> ourDevice;
104 std::atomic_bool running;
105 };
106}
107#endif // VIRTUAL_CAN_PLUGIN_HPP
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.
An abstract base class for a CAN driver.
A CAN frame for interfacing with a hardware layer, like socket CAN or other interface.
An OS and hardware independent virtual CAN interface driver for testing purposes.
std::string get_channel_name() const
Returns the assigned virtual channel name.
void open() override
Connects to the socket.
const bool receiveOwnMessages
If true, the driver will receive its own messages.
void clear_queue() const
Clear the internal received message queue.
std::atomic_bool running
If true, the driver is running.
static constexpr size_t MAX_QUEUE_SIZE
The maximum size of the queue, mostly arbitrary.
const std::string channel
The virtual channel name.
bool write_frame(const isobus::CANMessageFrame &canFrame) override
Writes a frame to the bus (synchronous)
VirtualCANPlugin(const std::string channel="", const bool receiveOwnMessages=false)
Constructor for the virtual CAN driver.
bool read_frame(isobus::CANMessageFrame &canFrame) override
Returns a frame from the hardware (synchronous), or false if no frame can be read....
std::shared_ptr< VirtualDevice > ourDevice
A pointer to the virtual device of this instance.
bool get_queue_empty() const
Returns if the internal received message queue is empty or not.
bool get_is_valid() const override
Returns if the socket connection is valid.
static std::mutex mutex
Mutex to access channels and queues for thread safety.
void close() override
Closes the socket.
static std::map< std::string, std::vector< std::shared_ptr< VirtualDevice > > > channels
A channel is a vector of devices.
void write_frame_as_if_received(const isobus::CANMessageFrame &canFrame) const
Allows us to write messages as if we received them from the bus.
virtual ~VirtualCANPlugin()
Destructor for the virtual CAN driver.
This namespace encompasses all of the ISO11783 stack's functionality to reduce global namespace pollu...
A struct holding information about a virtual CAN device.
std::condition_variable condition
A condition variable to wake us up when a frame is received.
std::deque< isobus::CANMessageFrame > queue
A queue of CAN frames.