Libsockcanpp
A complete C++ wrapper around socketcan.
|
CanDriver class; handles communication via CAN. More...
#include <CanDriver.hpp>
Public Member Functions | |
CanDriver (const string &canInterface, const int32_t canProtocol, const CanId defaultSenderId=0) | |
Constructor. | |
CanDriver (const string &canInterface, const int32_t canProtocol, const int32_t filterMask, const CanId defaultSenderId=0) | |
CanDriver (const string &canInterface, const int32_t canProtocol, const filtermap_t &filters, const CanId defaultSenderId=0) | |
CanDriver ()=default | |
virtual | ~CanDriver () |
Destructor. | |
CanDriver & | setDefaultSenderId (const CanId &id) |
Sets the default sender ID. | |
CanId | getDefaultSenderId () const |
Gets the default sender ID. | |
filtermap_t | getFilterMask () const |
Gets the filter mask used by this instance. | |
int32_t | getMessageQueueSize () const |
Gets the amount of CAN messages found after last calling waitForMessages() | |
int32_t | getSocketFd () const |
The socket file descriptor used by this instance. | |
string | getCanInterface () const |
The CAN interface used by this instance. | |
virtual bool | waitForMessages (microseconds timeout=3000us) |
Waits for CAN messages to appear. | |
virtual bool | waitForMessages (milliseconds timeout=3000ms) |
Waits for CAN messages to appear. | |
virtual bool | waitForMessages (nanoseconds timeout=3000ns) |
Waits for CAN messages to appear. | |
virtual CanMessage | readMessage () |
Attempts to read a single message from the bus. | |
virtual ssize_t | sendMessage (const CanMessage &message, bool forceExtended=false) |
Attempts to send a single CAN message. | |
virtual ssize_t | sendMessageQueue (queue< CanMessage > &messages, microseconds delay=20us, bool forceExtended=false) |
Attempts to send a queue of messages. | |
virtual ssize_t | sendMessageQueue (queue< CanMessage > &messages, milliseconds delay=20ms, bool forceExtended=false) |
Attempts to send a queue of messages. | |
virtual ssize_t | sendMessageQueue (queue< CanMessage > &messages, nanoseconds delay=20ns, bool forceExtended=false) |
Attempts to send a queue of messages. | |
virtual queue< CanMessage > | readQueuedMessages () |
Attempts to read all queued messages from the bus. | |
virtual void | allowCanFdFrames (const bool enabled=true) const |
Sets the CAN FD frame option for the interface. | |
virtual void | joinCanFilters () const |
Configures the socket to join the CAN filters. | |
virtual void | setCanFilterMask (const int32_t mask, const CanId &filterId) |
Attempts to set a new CAN filter mask to the interface. | |
virtual void | setCanFilters (const filtermap_t &filters) |
Sets the CAN filters for the interface. | |
virtual void | setErrorFilter (const bool enabled=true) const |
Sets the error filter for the interface. | |
virtual void | setReceiveOwnMessages (const bool enabled=true) const |
Sets the receive own messages option for the interface. | |
Static Public Attributes | |
static constexpr int32_t | CAN_MAX_DATA_LENGTH = 8 |
The maximum amount of bytes allowed in a single CAN frame. | |
static constexpr int32_t | CAN_SOCK_RAW = CAN_RAW |
The raw CAN protocol. | |
static constexpr int32_t | CAN_SOCK_SEVEN = 7 |
A separate CAN protocol, used by certain embedded device OEMs. | |
Protected Member Functions | |
virtual void | initialiseSocketCan () |
Initialises socketcan. | |
virtual void | uninitialiseSocketCan () |
Uninitialises socketcan. | |
Private Member Functions | |
virtual CanMessage | readMessageLock (bool const lock=true) |
readMessage deadlock guard | |
Private Attributes | |
CanId | _defaultSenderId |
The ID to send messages with if no other ID was set. | |
filtermap_t | _canFilterMask |
The bit mask used to filter CAN messages. | |
int32_t | _canProtocol |
The protocol used when communicating via CAN. | |
int32_t | _socketFd {-1} |
The CAN socket file descriptor. | |
int32_t | _queueSize {0} |
bool | _canReadQueueSize {true} |
!< The size of the message queue read by waitForMessages() | |
mutex | _lock {} |
!< Is the queue size available | |
mutex | _lockSend {} |
string | _canInterface |
The CAN interface used for communication (e.g. can0, can1, ...) | |
CanDriver class; handles communication via CAN.
This class provides the means of easily communicating with other devices via CAN in C++.
Definition at line 68 of file CanDriver.hpp.
sockcanpp::CanDriver::CanDriver | ( | const string & | canInterface, |
const int32_t | canProtocol, | ||
const CanId | defaultSenderId = 0 |
||
) |
Constructor.
Definition at line 79 of file CanDriver.cpp.
References CanDriver().
sockcanpp::CanDriver::CanDriver | ( | const string & | canInterface, |
const int32_t | canProtocol, | ||
const int32_t | filterMask, | ||
const CanId | defaultSenderId = 0 |
||
) |
Definition at line 82 of file CanDriver.cpp.
References CanDriver().
Referenced by CanDriver().
sockcanpp::CanDriver::CanDriver | ( | const string & | canInterface, |
const int32_t | canProtocol, | ||
const filtermap_t & | filters, | ||
const CanId | defaultSenderId = 0 |
||
) |
Definition at line 85 of file CanDriver.cpp.
References _canFilterMask, _canInterface, _canProtocol, _defaultSenderId, and initialiseSocketCan().
Referenced by CanDriver().
|
default |
|
inlinevirtual |
|
virtual |
Sets the CAN FD frame option for the interface.
This option allows the current driver instance to receive CAN FD frames.
enabled | Whether or not to enable the CAN FD frame option. |
Definition at line 297 of file CanDriver.cpp.
References _socketFd, sockcanpp::exceptions::CanInitException::CanInitException(), sockcanpp::formatString(), and sockcanpp::exceptions::InvalidSocketException::InvalidSocketException().
|
inline |
The CAN interface used by this instance.
Definition at line 91 of file CanDriver.hpp.
References _canInterface.
|
inline |
Gets the default sender ID.
Definition at line 84 of file CanDriver.hpp.
References _defaultSenderId.
|
inline |
Gets the filter mask used by this instance.
Definition at line 86 of file CanDriver.hpp.
References _canFilterMask.
|
inline |
Gets the amount of CAN messages found after last calling waitForMessages()
Definition at line 88 of file CanDriver.hpp.
References _queueSize.
|
inline |
The socket file descriptor used by this instance.
Definition at line 89 of file CanDriver.hpp.
References _socketFd.
|
protectedvirtual |
Initialises socketcan.
Initialises the underlying CAN socket.
Definition at line 420 of file CanDriver.cpp.
References _canFilterMask, _canInterface, _canProtocol, _socketFd, sockcanpp::exceptions::CanInitException::CanInitException(), sockcanpp::formatString(), and setCanFilters().
Referenced by CanDriver().
|
virtual |
Configures the socket to join the CAN filters.
Configures the socket to join the CAN filters. This is especially required, when using inverted CAN filters.
Source: https://stackoverflow.com/a/57680496/2921426
Definition at line 333 of file CanDriver.cpp.
References _socketFd, sockcanpp::exceptions::CanInitException::CanInitException(), sockcanpp::formatString(), and sockcanpp::exceptions::InvalidSocketException::InvalidSocketException().
|
virtual |
Attempts to read a single message from the bus.
Attempts to read a message from the associated CAN bus.
Definition at line 151 of file CanDriver.cpp.
References readMessageLock().
|
privatevirtual |
readMessage deadlock guard
readMessage deadlock guard, attempts to read a message from the associated CAN bus.
Definition at line 158 of file CanDriver.cpp.
References _lock, _socketFd, sockcanpp::exceptions::CanException::CanException(), sockcanpp::CanMessage::CanMessage(), sockcanpp::formatString(), and sockcanpp::exceptions::InvalidSocketException::InvalidSocketException().
Referenced by readMessage(), and readQueuedMessages().
|
virtual |
Attempts to read all queued messages from the bus.
Attempts to read all messages stored in the buffer for the associated CAN bus.
Definition at line 258 of file CanDriver.cpp.
References _canReadQueueSize, _lock, _queueSize, _socketFd, sockcanpp::exceptions::CanException::CanException(), sockcanpp::formatString(), sockcanpp::exceptions::InvalidSocketException::InvalidSocketException(), and readMessageLock().
|
virtual |
Attempts to send a single CAN message.
Attempts to send a CAN message on the associated bus.
message | The message to be sent. |
forceExtended | Whether or not to force use of an extended ID. |
Definition at line 183 of file CanDriver.cpp.
References _lockSend, _socketFd, CAN_MAX_DATA_LENGTH, sockcanpp::exceptions::CanException::CanException(), sockcanpp::formatString(), sockcanpp::CanMessage::getCanId(), sockcanpp::CanMessage::getFrameData(), sockcanpp::CanMessage::getRawFrame(), and sockcanpp::exceptions::InvalidSocketException::InvalidSocketException().
Referenced by sendMessageQueue().
|
virtual |
Attempts to send a queue of messages.
Attempts to send a queue of messages on the associated CAN bus.
messages | A queue containing the messages to be sent. |
delay | If greater than 0, will delay the sending of the next message. |
forceExtended | Whether or not to force use of an extended ID. |
Definition at line 214 of file CanDriver.cpp.
References sendMessageQueue().
|
virtual |
Attempts to send a queue of messages.
Attempts to send a queue of messages on the associated CAN bus.
messages | A queue containing the messages to be sent. |
delay | If greater than 0, will delay the sending of the next message. |
forceExtended | Whether or not to force use of an extended ID. |
Definition at line 225 of file CanDriver.cpp.
References sendMessageQueue().
|
virtual |
Attempts to send a queue of messages.
Attempts to send a queue of messages on the associated CAN bus.
messages | A queue containing the messages to be sent. |
delay | If greater than 0, will delay the sending of the next message. |
forceExtended | Whether or not to force use of an extended ID. |
Definition at line 236 of file CanDriver.cpp.
References _socketFd, sockcanpp::exceptions::InvalidSocketException::InvalidSocketException(), and sendMessage().
Referenced by sendMessageQueue(), and sendMessageQueue().
|
virtual |
Attempts to set a new CAN filter mask to the interface.
Attempts to set the filter mask for the associated CAN bus.
mask | The bit mask to apply. |
filterId | The ID to filter on. |
Definition at line 349 of file CanDriver.cpp.
References setCanFilters().
|
virtual |
Sets the CAN filters for the interface.
Sets multiple CAN filters for the associated CAN bus.
filters | A map containing the filters to apply. |
Definition at line 356 of file CanDriver.cpp.
References _lock, _socketFd, sockcanpp::exceptions::CanInitException::CanInitException(), sockcanpp::formatString(), sockcanpp::exceptions::InvalidSocketException::InvalidSocketException(), and sockcanpp::CanId::operator*().
Referenced by initialiseSocketCan(), and setCanFilterMask().
Sets the default sender ID.
Definition at line 82 of file CanDriver.hpp.
References _defaultSenderId.
|
virtual |
Sets the error filter for the interface.
Sets the error filter for the associated CAN bus.
enabled | Whether or not to enable the error filter. |
Definition at line 383 of file CanDriver.cpp.
References _socketFd, sockcanpp::exceptions::CanInitException::CanInitException(), sockcanpp::formatString(), and sockcanpp::exceptions::InvalidSocketException::InvalidSocketException().
|
virtual |
Sets the receive own messages option for the interface.
Sets the receive own messages option for the associated CAN bus.
This option allows the socket to receive its own messages.
enabled | Whether or not to enable the receive own messages option. |
Definition at line 400 of file CanDriver.cpp.
References _socketFd, sockcanpp::exceptions::CanInitException::CanInitException(), sockcanpp::formatString(), and sockcanpp::exceptions::InvalidSocketException::InvalidSocketException().
|
protectedvirtual |
Uninitialises socketcan.
Closes the underlying CAN socket.
Definition at line 456 of file CanDriver.cpp.
References _lock, _socketFd, sockcanpp::exceptions::CanCloseException::CanCloseException(), and sockcanpp::formatString().
Referenced by ~CanDriver().
|
virtual |
Waits for CAN messages to appear.
Blocks until one or more CAN messages appear on the bus, or until the timeout runs out.
timeout | The time (in µsec) to wait before timing out. |
Definition at line 100 of file CanDriver.cpp.
References _canReadQueueSize, _lock, _queueSize, _socketFd, and sockcanpp::exceptions::InvalidSocketException::InvalidSocketException().
Referenced by waitForMessages(), and waitForMessages().
|
virtual |
Waits for CAN messages to appear.
Blocks until one or more CAN messages appear on the bus, or until the timeout runs out.
timeout | The time (in millis) to wait before timing out. |
Definition at line 134 of file CanDriver.cpp.
References waitForMessages().
|
virtual |
Waits for CAN messages to appear.
Blocks until one or more CAN messages appear on the bus, or until the timeout runs out.
timeout | The time (in nanoseconds) to wait before timing out. |
Definition at line 144 of file CanDriver.cpp.
References waitForMessages().
|
private |
The bit mask used to filter CAN messages.
Definition at line 129 of file CanDriver.hpp.
Referenced by CanDriver(), getFilterMask(), and initialiseSocketCan().
|
private |
The CAN interface used for communication (e.g. can0, can1, ...)
Definition at line 140 of file CanDriver.hpp.
Referenced by CanDriver(), getCanInterface(), and initialiseSocketCan().
|
private |
The protocol used when communicating via CAN.
Definition at line 131 of file CanDriver.hpp.
Referenced by CanDriver(), and initialiseSocketCan().
|
private |
!< The size of the message queue read by waitForMessages()
Definition at line 134 of file CanDriver.hpp.
Referenced by readQueuedMessages(), and waitForMessages().
|
private |
The ID to send messages with if no other ID was set.
Definition at line 127 of file CanDriver.hpp.
Referenced by CanDriver(), getDefaultSenderId(), and setDefaultSenderId().
|
private |
!< Is the queue size available
< Mutex for thread-safety.
Definition at line 137 of file CanDriver.hpp.
Referenced by readMessageLock(), readQueuedMessages(), setCanFilters(), uninitialiseSocketCan(), and waitForMessages().
|
private |
|
private |
Definition at line 133 of file CanDriver.hpp.
Referenced by getMessageQueueSize(), readQueuedMessages(), and waitForMessages().
|
private |
The CAN socket file descriptor.
Definition at line 132 of file CanDriver.hpp.
Referenced by allowCanFdFrames(), getSocketFd(), initialiseSocketCan(), joinCanFilters(), readMessageLock(), readQueuedMessages(), sendMessage(), sendMessageQueue(), setCanFilters(), setErrorFilter(), setReceiveOwnMessages(), uninitialiseSocketCan(), and waitForMessages().
|
staticconstexpr |
The maximum amount of bytes allowed in a single CAN frame.
Definition at line 70 of file CanDriver.hpp.
Referenced by sendMessage().
|
staticconstexpr |
The raw CAN protocol.
Definition at line 71 of file CanDriver.hpp.
|
staticconstexpr |
A separate CAN protocol, used by certain embedded device OEMs.
Definition at line 72 of file CanDriver.hpp.