Libsockcanpp
A complete C++ wrapper around socketcan.
Loading...
Searching...
No Matches
CanMessage.hpp
Go to the documentation of this file.
1/**
2 * @file CanMessage.hpp
3 * @author Simon Cahill (contact@simonc.eu)
4 * @brief Contains the implementation of a CAN message representation in C++.
5 * @version 0.1
6 * @date 2020-07-01
7 *
8 * @copyright Copyright (c) 2020-2025 Simon Cahill
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23#ifndef LIBSOCKCANPP_INCLUDE_CANMESSAGE_HPP
24#define LIBSOCKCANPP_INCLUDE_CANMESSAGE_HPP
25
26//////////////////////////////
27// SYSTEM INCLUDES //
28//////////////////////////////
29#include <linux/can.h>
30
31#include <cstring>
32#include <exception>
33#include <string>
34#include <system_error>
35#include <thread>
36
37//////////////////////////////
38// LOCAL INCLUDES //
39//////////////////////////////
40#include "CanId.hpp"
41
42namespace sockcanpp {
43
44 using std::error_code;
45 using std::generic_category;
46 using std::memcpy;
47 using std::string;
48 using std::system_error;
49
50 /**
51 * @brief Represents a CAN message that was received.
52 */
53 class CanMessage {
54 public: // +++ Constructor / Destructor +++
55 CanMessage(const struct can_frame frame):
56 _canIdentifier(frame.can_id), _frameData((const char*)frame.data, frame.can_dlc), _rawFrame(frame) { }
57
58 CanMessage(const CanId canId, const string& frameData): _canIdentifier(canId), _frameData(frameData) {
59 if (frameData.size() > CAN_MAX_DLEN) {
60 throw system_error(error_code(0xbadd1c, generic_category()), "Payload too big!");
61 }
62
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();
67
68 _rawFrame = rawFrame;
69 }
70
71 virtual ~CanMessage() {}
72
73 public: // +++ Getters +++
74 const CanId getCanId() const { return this->_canIdentifier; }
75 const string getFrameData() const { return this->_frameData; }
76 const can_frame getRawFrame() const { return this->_rawFrame; }
77
78 private:
80
81 string _frameData;
82
83 struct can_frame _rawFrame;
84 };
85
86}
87
88#endif // LIBSOCKCANPP_INCLUDE_CANMESSAGE_HPP
CanDriver class; handles communication via CAN.
Definition CanDriver.hpp:68
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.
Definition CanDriver.hpp:72
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)
Definition CanDriver.cpp:85
int32_t getMessageQueueSize() const
Gets the amount of CAN messages found after last calling waitForMessages()
Definition CanDriver.hpp:88
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.
Definition CanDriver.hpp:86
CanDriver(const string &canInterface, const int32_t canProtocol, const CanId defaultSenderId=0)
Constructor.
Definition CanDriver.cpp:79
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)
Definition CanDriver.cpp:82
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.
Definition CanDriver.hpp:91
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.
Definition CanDriver.hpp:84
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.
Definition CanDriver.hpp:71
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.
Definition CanDriver.hpp:82
filtermap_t _canFilterMask
The bit mask used to filter CAN messages.
int32_t getSocketFd() const
The socket file descriptor used by this instance.
Definition CanDriver.hpp:89
virtual void initialiseSocketCan()
Initialises socketcan.
virtual ~CanDriver()
Destructor.
Definition CanDriver.hpp:79
static constexpr int32_t CAN_MAX_DATA_LENGTH
The maximum amount of bytes allowed in a single CAN frame.
Definition CanDriver.hpp:70
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.
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.
An exception that may be thrown when an error occurs while closing a CAN socket.
InvalidSocketException(const string &message, int32_t socket)
Main library namespace.
Definition CanDriver.cpp:56
string formatString(const string &format, Args... args)
Formats a std string object.
Implements a hash function for the CanId type.
Definition CanId.hpp:265
size_t operator()(const CanId &id) const
Definition CanId.hpp:267
Represents a CAN ID in a simple and easy-to-use manner.
Definition CanId.hpp:61
constexpr CanId()=default
constexpr bool operator!=(const CanId &x) const
Compares this ID to another.
Definition CanId.hpp:123
constexpr bool operator==(const CanId &x) const
Compares this ID to another.
Definition CanId.hpp:118
constexpr bool hasRtrFrameFlag() const
Indicates whether or not this ID is a remote transmission request.
Definition CanId.hpp:251
constexpr bool operator!=(const T x) const
Compares this ID to another.
Definition CanId.hpp:126
constexpr CanId operator&(const T x) const
Performs a bitwise AND operation on this ID and another.
Definition CanId.hpp:86
constexpr CanId operator^=(const T x)
Performs a bitwise XOR operation on this ID and another.
Definition CanId.hpp:161
constexpr CanId operator|(const CanId &x) const
Performs a bitwise OR operation on this ID and another.
Definition CanId.hpp:91
constexpr CanId operator/=(const T x)
Definition CanId.hpp:187
constexpr CanId operator^(const CanId &x) const
Performs a bitwise XOR operation on this ID and another.
Definition CanId.hpp:95
constexpr bool operator<=(const T x) const
Compares this ID to another.
Definition CanId.hpp:135
constexpr CanId operator-=(const T x)
Definition CanId.hpp:172
constexpr CanId operator^(const T x) const
Performs a bitwise XOR operation on this ID and a 16-bit integer.
Definition CanId.hpp:94
constexpr bool operator>(T x) const
Compares this ID to a 32-bit integer.
Definition CanId.hpp:132
constexpr canid_t operator*() const
Returns the raw CAN ID value.
Definition CanId.hpp:75
constexpr operator uint16_t() const
Definition CanId.hpp:79
constexpr CanId operator/(const T x) const
Definition CanId.hpp:184
constexpr CanId operator=(const int64_t val)
Assigns a 64-bit integer to this ID.
Definition CanId.hpp:152
constexpr CanId operator+=(const T x)
Definition CanId.hpp:169
constexpr CanId operator|=(const T x)
Performs a bitwise OR operation on this ID and another.
Definition CanId.hpp:155
constexpr CanId operator+(const T x) const
Definition CanId.hpp:166
constexpr CanId operator&(const CanId &x) const
Performs a bitwise AND operation on this ID and another.
Definition CanId.hpp:87
CanId operator>>=(const CanId &x)
Shifts this ID to the right by another.
Definition CanId.hpp:113
static constexpr bool isErrorFrame(T value)
Indicates whether or not a given integer contains the error frame flag or not.
Definition CanId.hpp:219
CanId operator>>=(const T x)
Shifts this ID to the right by a 16-bit integer.
Definition CanId.hpp:112
constexpr operator int16_t() const
Definition CanId.hpp:78
constexpr CanId(const int32_t id)
Definition CanId.hpp:65
CanId(const char *id)
Definition CanId.hpp:72
constexpr bool isStandardFrameId() const
Indicates whether or not this ID is a standard frame ID.
Definition CanId.hpp:252
constexpr operator int32_t() const
Definition CanId.hpp:80
constexpr CanId operator|(const T x) const
Performs a bitwise OR operation on this ID and a 16-bit integer.
Definition CanId.hpp:90
constexpr CanId operator%(const T x) const
Definition CanId.hpp:190
constexpr operator canid_t() const
Definition CanId.hpp:81
constexpr CanId operator>>(const CanId &x) const
Shifts this ID to the right by another.
Definition CanId.hpp:105
static constexpr bool isRemoteTransmissionRequest(T value)
Indicates whether the received frame is a remote transmission request.
Definition CanId.hpp:232
constexpr bool equals(const CanId &otherId) const
Compares this ID to another.
Definition CanId.hpp:256
constexpr bool operator==(const T x) const
Compares this ID to another.
Definition CanId.hpp:121
static constexpr bool isValidIdentifier(T value)
Indicates whether or not a given integer is a valid CAN identifier.
Definition CanId.hpp:206
constexpr CanId operator*=(const T x)
Definition CanId.hpp:181
constexpr bool operator<(T x) const
Compares this ID to another.
Definition CanId.hpp:129
static constexpr bool isExtendedFrame(T value)
Indicates whether or not a given integer is an extended frame ID.
Definition CanId.hpp:245
constexpr CanId operator=(const T val)
Assigns a new integer to this CanID.
Definition CanId.hpp:143
constexpr CanId operator-(const T x) const
Definition CanId.hpp:175
constexpr CanId(const canid_t id)
Definition CanId.hpp:64
constexpr CanId operator~() const
Performs a bitwise NOT operation on this ID.
Definition CanId.hpp:97
constexpr CanId operator>>(const T x) const
Shifts this ID to the right by a 16-bit integer.
Definition CanId.hpp:104
uint32_t m_identifier
Definition CanId.hpp:259
constexpr bool hasErrorFrameFlag() const
Indicates whether or not this ID is an error frame.
Definition CanId.hpp:250
constexpr CanId operator&=(const T x)
Performs a bitwise AND operation on this ID and another.
Definition CanId.hpp:158
constexpr bool operator>=(const T x) const
Compares this ID to another.
Definition CanId.hpp:138
constexpr CanId operator%=(const T x)
Definition CanId.hpp:193
constexpr CanId operator*(const T x) const
Definition CanId.hpp:178
constexpr bool isExtendedFrameId() const
Indicates whether or not this ID is an extended frame ID.
Definition CanId.hpp:253