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) | |
!< A separate CAN protocol, used by certain embedded device OEMs. More... | |
CanDriver (const string canInterface, const int32_t canProtocol, const int32_t filterMask, const CanId defaultSenderId=0) | |
!< Constructor More... | |
CanDriver () | |
virtual | ~CanDriver () |
CanDriver & | setDefaultSenderId (const CanId id) |
!< Destructor More... | |
const CanId | getDefaultSenderId () const |
!< Sets the default sender ID More... | |
const int32_t | getFilterMask () const |
!< Gets the default sender ID More... | |
const int32_t | getMessageQueueSize () const |
!< Gets the filter mask used by this instance More... | |
const int32_t | getSocketFd () const |
!< Gets the amount of CAN messages found after last calling waitForMessages() More... | |
virtual bool | waitForMessages (milliseconds timeout=milliseconds(3000)) |
!< The socket file descriptor used by this instance. More... | |
virtual CanMessage | readMessage () |
!< Waits for CAN messages to appear More... | |
virtual int32_t | sendMessage (const CanMessage message, bool forceExtended=false) |
!< Attempts to read a single message from the bus More... | |
virtual int32_t | sendMessageQueue (queue< CanMessage > messages, milliseconds delay=milliseconds(20), bool forceExtended=false) |
!< Attempts to send a single CAN message More... | |
virtual queue< CanMessage > | readQueuedMessages () |
!< Attempts to send a queue of messages More... | |
virtual void | setCanFilterMask (const int32_t mask) |
!< Attempts to read all queued messages from the bus More... | |
Static Public Attributes | |
static const int32_t | CAN_MAX_DATA_LENGTH = 8 |
static const int32_t | CAN_SOCK_RAW = CAN_RAW |
!< The maximum amount of bytes allowed in a single CAN frame More... | |
static const int32_t | CAN_SOCK_SEVEN = 7 |
!< The raw CAN protocol More... | |
Protected Member Functions | |
virtual void | initialiseSocketCan () |
!< Attempts to set a new CAN filter mask to the BIOS More... | |
virtual void | uninitialiseSocketCan () |
!< Initialises socketcan More... | |
Private Member Functions | |
virtual CanMessage | readMessageLock (bool const lock=true) |
!< Uninitialises socketcan More... | |
Private Attributes | |
CanId | _defaultSenderId |
!< readMessage deadlock guard More... | |
int32_t | _canFilterMask |
!< The ID to send messages with if no other ID was set. More... | |
int32_t | _canProtocol |
!< The bit mask used to filter CAN messages More... | |
int32_t | _socketFd |
!< The protocol used when communicating via CAN More... | |
int32_t | _queueSize |
!< The CAN socket file descriptor More... | |
mutex | _lock |
!< Mutex for thread-safety. More... | |
mutex | _lockSend |
string | _canInterface |
CanDriver class; handles communication via CAN.
This class provides the means of easily communicating with other devices via CAN in C++.
Definition at line 62 of file CanDriver.hpp.
sockcanpp::CanDriver::CanDriver | ( | const string | canInterface, |
const int32_t | canProtocol, | ||
const CanId | defaultSenderId = 0 |
||
) |
!< A separate CAN protocol, used by certain embedded device OEMs.
Definition at line 81 of file CanDriver.cpp.
References CanDriver().
sockcanpp::CanDriver::CanDriver | ( | const string | canInterface, |
const int32_t | canProtocol, | ||
const int32_t | filterMask, | ||
const CanId | defaultSenderId = 0 |
||
) |
!< Constructor
Definition at line 84 of file CanDriver.cpp.
References _canFilterMask, _canInterface, _canProtocol, _defaultSenderId, _socketFd, sockcanpp::CanId::CanId(), and initialiseSocketCan().
Referenced by CanDriver().
|
inline |
Definition at line 71 of file CanDriver.hpp.
|
inlinevirtual |
Definition at line 72 of file CanDriver.hpp.
References uninitialiseSocketCan().
|
inline |
!< Sets the default sender ID
Definition at line 77 of file CanDriver.hpp.
References _defaultSenderId.
|
inline |
!< Gets the default sender ID
Definition at line 79 of file CanDriver.hpp.
References _canFilterMask.
|
inline |
!< Gets the filter mask used by this instance
Definition at line 80 of file CanDriver.hpp.
References _queueSize.
|
inline |
!< Gets the amount of CAN messages found after last calling waitForMessages()
Definition at line 81 of file CanDriver.hpp.
References _socketFd.
|
protectedvirtual |
!< Attempts to set a new CAN filter mask to the BIOS
Initialises the underlying CAN socket.
Definition at line 243 of file CanDriver.cpp.
References _canFilterMask, _canInterface, _canProtocol, _socketFd, sockcanpp::exceptions::CanInitException::CanInitException(), sockcanpp::formatString(), and setCanFilterMask().
Referenced by CanDriver().
|
virtual |
!< Waits for CAN messages to appear
Attempts to read a message from the associated CAN bus.
Definition at line 121 of file CanDriver.cpp.
References readMessageLock().
|
privatevirtual |
!< Uninitialises socketcan
readMessage deadlock guard, attempts to read a message from the associated CAN bus.
Definition at line 130 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 send a queue of messages
Attempts to read all messages stored in the buffer for the associated CAN bus.
Definition at line 202 of file CanDriver.cpp.
References _lock, _queueSize, _socketFd, sockcanpp::exceptions::InvalidSocketException::InvalidSocketException(), and readMessageLock().
|
virtual |
!< Attempts to read a single message from the bus
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 153 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 single CAN message
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 184 of file CanDriver.cpp.
References _socketFd, sockcanpp::exceptions::InvalidSocketException::InvalidSocketException(), and sendMessage().
|
virtual |
!< Attempts to read all queued messages from the bus
Attempts to set the filter mask for the associated CAN bus.
mask | The bit mask to apply. |
Definition at line 217 of file CanDriver.cpp.
References _canFilterMask, _defaultSenderId, _lock, _socketFd, sockcanpp::exceptions::CanInitException::CanInitException(), sockcanpp::formatString(), and sockcanpp::exceptions::InvalidSocketException::InvalidSocketException().
Referenced by initialiseSocketCan().
|
protectedvirtual |
!< Initialises socketcan
Closes the underlying CAN socket.
Definition at line 284 of file CanDriver.cpp.
References _lock, _socketFd, sockcanpp::exceptions::CanCloseException::CanCloseException(), and sockcanpp::formatString().
Referenced by ~CanDriver().
|
virtual |
!< The socket file descriptor used by this instance.
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 99 of file CanDriver.cpp.
References _lock, _queueSize, _socketFd, and sockcanpp::exceptions::InvalidSocketException::InvalidSocketException().
|
private |
!< The ID to send messages with if no other ID was set.
Definition at line 104 of file CanDriver.hpp.
Referenced by CanDriver(), getFilterMask(), initialiseSocketCan(), and setCanFilterMask().
|
private |
Definition at line 113 of file CanDriver.hpp.
Referenced by CanDriver(), and initialiseSocketCan().
|
private |
!< The bit mask used to filter CAN messages
Definition at line 105 of file CanDriver.hpp.
Referenced by CanDriver(), and initialiseSocketCan().
|
private |
!< readMessage deadlock guard
Definition at line 102 of file CanDriver.hpp.
Referenced by CanDriver(), getDefaultSenderId(), setCanFilterMask(), and setDefaultSenderId().
|
private |
!< Mutex for thread-safety.
Definition at line 110 of file CanDriver.hpp.
Referenced by readMessageLock(), readQueuedMessages(), setCanFilterMask(), uninitialiseSocketCan(), and waitForMessages().
|
private |
Definition at line 111 of file CanDriver.hpp.
Referenced by sendMessage().
|
private |
!< The CAN socket file descriptor
Definition at line 107 of file CanDriver.hpp.
Referenced by getMessageQueueSize(), readQueuedMessages(), and waitForMessages().
|
private |
!< The protocol used when communicating via CAN
Definition at line 106 of file CanDriver.hpp.
Referenced by CanDriver(), getSocketFd(), initialiseSocketCan(), readMessageLock(), readQueuedMessages(), sendMessage(), sendMessageQueue(), setCanFilterMask(), uninitialiseSocketCan(), and waitForMessages().
|
static |
Definition at line 64 of file CanDriver.hpp.
Referenced by sendMessage().
|
static |
!< The maximum amount of bytes allowed in a single CAN frame
Definition at line 65 of file CanDriver.hpp.
|
static |
!< The raw CAN protocol
Definition at line 66 of file CanDriver.hpp.