2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
25#ifndef LIBSOCKCANPP_INCLUDE_CANDRIVER_HPP
26#define LIBSOCKCANPP_INCLUDE_CANDRIVER_HPP
43
44
45
46
49 using std::chrono::milliseconds;
55
56
57
58
59
60
61
69 CanDriver(
const string canInterface,
const int32_t canProtocol,
const CanId defaultSenderId = 0);
70 CanDriver(
const string canInterface,
const int32_t canProtocol,
const int32_t filterMask,
const CanId defaultSenderId = 0);
84 virtual bool waitForMessages(milliseconds timeout = milliseconds(3000));
90 milliseconds delay = milliseconds(20),
bool forceExtended =
false);
120
121
122
123
124
125
126
127
128
129
130 template<
typename... Args>
132 using std::unique_ptr;
133 auto stringSize = snprintf(NULL, 0, format.c_str(), args...) + 1;
134 unique_ptr<
char[]> buffer(
new char[stringSize]);
136 snprintf(buffer.get(), stringSize, format.c_str(), args...);
138 return string(buffer.get(), buffer.get() + stringSize - 1);
CanDriver class; handles communication via CAN.
CanDriver(const string canInterface, const int32_t canProtocol, const CanId defaultSenderId=0)
!< A separate CAN protocol, used by certain embedded device OEMs.
virtual CanMessage readMessage()
!< Waits for CAN messages to appear
const int32_t getFilterMask() const
!< Gets the default sender ID
static const int32_t CAN_SOCK_RAW
!< The maximum amount of bytes allowed in a single CAN frame
static const int32_t CAN_SOCK_SEVEN
!< The raw CAN protocol
const int32_t getMessageQueueSize() const
!< Gets the filter mask used by this instance
CanId _defaultSenderId
!< readMessage deadlock guard
int32_t _queueSize
!< The CAN socket file descriptor
virtual queue< CanMessage > readQueuedMessages()
!< Attempts to send a queue of messages
int32_t _canProtocol
!< The bit mask used to filter CAN messages
virtual int32_t sendMessageQueue(queue< CanMessage > messages, milliseconds delay=milliseconds(20), bool forceExtended=false)
!< Attempts to send a single CAN message
virtual CanMessage readMessageLock(bool const lock=true)
!< Uninitialises socketcan
const int32_t getSocketFd() const
!< Gets the amount of CAN messages found after last calling waitForMessages()
CanDriver(const string canInterface, const int32_t canProtocol, const int32_t filterMask, const CanId defaultSenderId=0)
!< Constructor
int32_t _socketFd
!< The protocol used when communicating via CAN
int32_t _canFilterMask
!< The ID to send messages with if no other ID was set.
CanDriver & setDefaultSenderId(const CanId id)
!< Destructor
virtual bool waitForMessages(milliseconds timeout=milliseconds(3000))
!< The socket file descriptor used by this instance.
mutex _lock
!< Mutex for thread-safety.
virtual void initialiseSocketCan()
!< Attempts to set a new CAN filter mask to the BIOS
virtual int32_t sendMessage(const CanMessage message, bool forceExtended=false)
!< Attempts to read a single message from the bus
const CanId getDefaultSenderId() const
!< Sets the default sender ID
virtual void setCanFilterMask(const int32_t mask)
!< Attempts to read all queued messages from the bus
virtual void uninitialiseSocketCan()
!< Initialises socketcan
static const int32_t CAN_MAX_DATA_LENGTH
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
CanCloseException(string message)
CanException(string message, int32_t socket)
CanInitException(string message)
InvalidSocketException(string message, int32_t socket)
string formatString(const string &format, Args... args)
Formats a std string object.
Represents a CAN ID in a simple and easy-to-use manner.