2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
23#ifndef LIBSOCKCANPP_INCLUDE_CANDRIVER_HPP
24#define LIBSOCKCANPP_INCLUDE_CANDRIVER_HPP
33#include <unordered_map>
42
43
44
45
48 using namespace std::chrono_literals;
50 using std::chrono::microseconds;
51 using std::chrono::milliseconds;
52 using std::chrono::nanoseconds;
56 using std::unordered_map;
61
62
63
64
65
66
67
75 CanDriver(
const string& canInterface,
const int32_t canProtocol,
const CanId defaultSenderId = 0);
76 CanDriver(
const string& canInterface,
const int32_t canProtocol,
const int32_t filterMask,
const CanId defaultSenderId = 0);
77 CanDriver(
const string& canInterface,
const int32_t canProtocol,
const filtermap_t& filters,
const CanId defaultSenderId = 0);
110 virtual void allowCanXlFrames(
const bool enabled =
true)
const;
145
146
147
148
149
150
151
152
153
154
155 template<
typename... Args>
157 using std::unique_ptr;
158 auto stringSize = snprintf(
nullptr, 0, format.c_str(), args...) + 1;
159 unique_ptr<
char[]> buffer(
new char[stringSize]);
161 snprintf(buffer.get(), stringSize, format.c_str(), args...);
163 return string(buffer.get(), buffer.get() + stringSize - 1);
CanDriver class; handles communication via CAN.
virtual void allowCanFdFrames(const bool enabled=true) const
Sets the CAN FD frame option for the interface.
static constexpr int32_t CAN_SOCK_SEVEN
A separate CAN protocol, used by certain embedded device OEMs.
virtual void joinCanFilters() const
Configures the socket to join the CAN filters.
CanDriver(const string &canInterface, const int32_t canProtocol, const filtermap_t &filters, const CanId defaultSenderId=0)
int32_t getMessageQueueSize() const
Gets the amount of CAN messages found after last calling waitForMessages()
bool _canReadQueueSize
!< The size of the message queue read by waitForMessages()
virtual void setReceiveOwnMessages(const bool enabled=true) const
Sets the receive own messages option for the interface.
virtual CanMessage readMessage()
Attempts to read a single message from the bus.
filtermap_t getFilterMask() const
Gets the filter mask used by this instance.
CanDriver(const string &canInterface, const int32_t canProtocol, const CanId defaultSenderId=0)
Constructor.
virtual void setErrorFilter(const bool enabled=true) const
Sets the error filter for the interface.
CanDriver(const string &canInterface, const int32_t canProtocol, const int32_t filterMask, const CanId defaultSenderId=0)
virtual ssize_t sendMessageQueue(queue< CanMessage > &messages, milliseconds delay=20ms, bool forceExtended=false)
Attempts to send a queue of messages.
string getCanInterface() const
The CAN interface used by this instance.
virtual void setCanFilters(const filtermap_t &filters)
Sets the CAN filters for the interface.
string _canInterface
The CAN interface used for communication (e.g. can0, can1, ...)
CanId getDefaultSenderId() const
Gets the default sender ID.
CanId _defaultSenderId
The ID to send messages with if no other ID was set.
virtual queue< CanMessage > readQueuedMessages()
Attempts to read all queued messages from the bus.
int32_t _canProtocol
The protocol used when communicating via CAN.
virtual void setCanFilterMask(const int32_t mask, const CanId &filterId)
Attempts to set a new CAN filter mask to the interface.
virtual CanMessage readMessageLock(bool const lock=true)
readMessage deadlock guard
virtual ssize_t sendMessage(const CanMessage &message, bool forceExtended=false)
Attempts to send a single CAN message.
virtual ssize_t sendMessageQueue(queue< CanMessage > &messages, nanoseconds delay=20ns, bool forceExtended=false)
Attempts to send a queue of messages.
int32_t _socketFd
The CAN socket file descriptor.
static constexpr int32_t CAN_SOCK_RAW
The raw CAN protocol.
virtual bool waitForMessages(milliseconds timeout=3000ms)
Waits for CAN messages to appear.
mutex _lock
!< Is the queue size available
CanDriver & setDefaultSenderId(const CanId &id)
Sets the default sender ID.
filtermap_t _canFilterMask
The bit mask used to filter CAN messages.
int32_t getSocketFd() const
The socket file descriptor used by this instance.
virtual void initialiseSocketCan()
Initialises socketcan.
virtual ~CanDriver()
Destructor.
static constexpr int32_t CAN_MAX_DATA_LENGTH
The maximum amount of bytes allowed in a single CAN frame.
virtual bool waitForMessages(nanoseconds timeout=3000ns)
Waits for CAN messages to appear.
virtual bool waitForMessages(microseconds timeout=3000us)
Waits for CAN messages to appear.
virtual ssize_t sendMessageQueue(queue< CanMessage > &messages, microseconds delay=20us, bool forceExtended=false)
Attempts to send a queue of messages.
virtual void uninitialiseSocketCan()
Uninitialises socketcan.
Represents a CAN message that was received.
const string getFrameData() const
const can_frame getRawFrame() const
CanMessage(const struct can_frame frame)
const CanId getCanId() const
An exception that may be thrown when an error occurs while closing a CAN socket.
CanCloseException(string message)
An exception that may be thrown when an error occurs while closing a CAN socket.
CanException(const string &message, int32_t socket)
An exception that may be thrown when an error occurred while initialising a CAN socket.
CanInitException(string message)
An exception that may be thrown when an error occurs while closing a CAN socket.
InvalidSocketException(const string &message, int32_t socket)
string formatString(const string &format, Args... args)
Formats a std string object.
Implements a hash function for the CanId type.
Represents a CAN ID in a simple and easy-to-use manner.
constexpr canid_t operator*() const
Returns the raw CAN ID value.