2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
23#ifndef LIBSOCKCANPP_INCLUDE_CANMESSAGE_HPP
24#define LIBSOCKCANPP_INCLUDE_CANMESSAGE_HPP
34#include <system_error>
44 using std::error_code;
45 using std::generic_category;
48 using std::system_error;
51
52
59 if (frameData.size() > CAN_MAX_DLEN) {
60 throw system_error(error_code(0xbadd1c, generic_category()),
"Payload too big!");
63 struct can_frame rawFrame{};
64 rawFrame.can_id = canId;
65 std::copy(frameData.begin(), frameData.end(), rawFrame.data);
66 rawFrame.can_dlc = frameData.size();
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)
CanMessage(const CanId canId, const string &frameData)
struct can_frame _rawFrame
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.
size_t operator()(const CanId &id) const
Represents a CAN ID in a simple and easy-to-use manner.
constexpr CanId()=default
constexpr bool operator!=(const CanId &x) const
Compares this ID to another.
constexpr bool operator==(const CanId &x) const
Compares this ID to another.
constexpr bool hasRtrFrameFlag() const
Indicates whether or not this ID is a remote transmission request.
constexpr bool operator!=(const T x) const
Compares this ID to another.
constexpr CanId operator&(const T x) const
Performs a bitwise AND operation on this ID and another.
constexpr CanId operator^=(const T x)
Performs a bitwise XOR operation on this ID and another.
constexpr CanId operator|(const CanId &x) const
Performs a bitwise OR operation on this ID and another.
constexpr CanId operator/=(const T x)
constexpr CanId operator^(const CanId &x) const
Performs a bitwise XOR operation on this ID and another.
constexpr bool operator<=(const T x) const
Compares this ID to another.
constexpr CanId operator-=(const T x)
constexpr CanId operator^(const T x) const
Performs a bitwise XOR operation on this ID and a 16-bit integer.
constexpr bool operator>(T x) const
Compares this ID to a 32-bit integer.
constexpr canid_t operator*() const
Returns the raw CAN ID value.
constexpr operator uint16_t() const
constexpr CanId operator/(const T x) const
constexpr CanId operator=(const int64_t val)
Assigns a 64-bit integer to this ID.
constexpr CanId operator+=(const T x)
constexpr CanId operator|=(const T x)
Performs a bitwise OR operation on this ID and another.
constexpr CanId operator+(const T x) const
constexpr CanId operator&(const CanId &x) const
Performs a bitwise AND operation on this ID and another.
CanId operator>>=(const CanId &x)
Shifts this ID to the right by another.
static constexpr bool isErrorFrame(T value)
Indicates whether or not a given integer contains the error frame flag or not.
CanId operator>>=(const T x)
Shifts this ID to the right by a 16-bit integer.
constexpr operator int16_t() const
constexpr CanId(const int32_t id)
constexpr bool isStandardFrameId() const
Indicates whether or not this ID is a standard frame ID.
constexpr operator int32_t() const
constexpr CanId operator|(const T x) const
Performs a bitwise OR operation on this ID and a 16-bit integer.
constexpr CanId operator%(const T x) const
constexpr operator canid_t() const
constexpr CanId operator>>(const CanId &x) const
Shifts this ID to the right by another.
static constexpr bool isRemoteTransmissionRequest(T value)
Indicates whether the received frame is a remote transmission request.
constexpr bool equals(const CanId &otherId) const
Compares this ID to another.
constexpr bool operator==(const T x) const
Compares this ID to another.
static constexpr bool isValidIdentifier(T value)
Indicates whether or not a given integer is a valid CAN identifier.
constexpr CanId operator*=(const T x)
constexpr bool operator<(T x) const
Compares this ID to another.
static constexpr bool isExtendedFrame(T value)
Indicates whether or not a given integer is an extended frame ID.
constexpr CanId operator=(const T val)
Assigns a new integer to this CanID.
constexpr CanId operator-(const T x) const
constexpr CanId(const canid_t id)
constexpr CanId operator~() const
Performs a bitwise NOT operation on this ID.
constexpr CanId operator>>(const T x) const
Shifts this ID to the right by a 16-bit integer.
constexpr bool hasErrorFrameFlag() const
Indicates whether or not this ID is an error frame.
constexpr CanId operator&=(const T x)
Performs a bitwise AND operation on this ID and another.
constexpr bool operator>=(const T x) const
Compares this ID to another.
constexpr CanId operator%=(const T x)
constexpr CanId operator*(const T x) const
constexpr bool isExtendedFrameId() const
Indicates whether or not this ID is an extended frame ID.