Libsockcanpp
A complete C++ wrapper around socketcan.
CanMessage.hpp
Go to the documentation of this file.
1/**
2 * @file CanMessage.hpp
3 * @author Simon Cahill (simonc@online.de)
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 Simon Cahill
9 *
10 * Copyright 2020 Simon Cahill
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 */
24
25#ifndef LIBSOCKCANPP_INCLUDE_CANMESSAGE_HPP
26#define LIBSOCKCANPP_INCLUDE_CANMESSAGE_HPP
27
28//////////////////////////////
29// SYSTEM INCLUDES //
30//////////////////////////////
31#include <linux/can.h>
32
33#include <cstring>
34#include <exception>
35#include <string>
36#include <system_error>
37#include <thread>
38
39//////////////////////////////
40// LOCAL INCLUDES //
41//////////////////////////////
42#include "CanId.hpp"
43
44namespace sockcanpp {
45
46 using std::error_code;
47 using std::generic_category;
48 using std::memcpy;
49 using std::string;
50 using std::system_error;
51
52 /**
53 * @brief Represents a CAN message that was received.
54 */
55 class CanMessage {
56 public: // +++ Constructor / Destructor +++
57 CanMessage(const struct can_frame frame):
58 _canIdentifier(frame.can_id), _frameData((const char*)frame.data, frame.can_dlc), _rawFrame(frame) { }
59
60 CanMessage(const CanId canId, const string frameData): _canIdentifier(canId), _frameData(frameData) {
61 if (frameData.size() > 8) {
62 throw system_error(error_code(0xbadd1c, generic_category()), "Payload too big!");
63 }
64
65 struct can_frame rawFrame;
66 rawFrame.can_id = canId;
67 memcpy(rawFrame.data, frameData.data(), frameData.size());
68 rawFrame.can_dlc = frameData.size();
69
70 _rawFrame = rawFrame;
71 }
72
73 virtual ~CanMessage() {}
74
75 public: // +++ Getters +++
76 const CanId getCanId() const { return this->_canIdentifier; }
77 const string getFrameData() const { return this->_frameData; }
78 const can_frame getRawFrame() const { return this->_rawFrame; }
79
80 private:
82
84
85 struct can_frame _rawFrame;
86 };
87
88}
89
90#endif // LIBSOCKCANPP_INCLUDE_CANMESSAGE_HPP
CanDriver class; handles communication via CAN.
Definition: CanDriver.hpp:62
CanDriver(const string canInterface, const int32_t canProtocol, const CanId defaultSenderId=0)
!< A separate CAN protocol, used by certain embedded device OEMs.
Definition: CanDriver.cpp:81
virtual CanMessage readMessage()
!< Waits for CAN messages to appear
Definition: CanDriver.cpp:121
const int32_t getFilterMask() const
!< Gets the default sender ID
Definition: CanDriver.hpp:79
static const int32_t CAN_SOCK_RAW
!< The maximum amount of bytes allowed in a single CAN frame
Definition: CanDriver.hpp:65
static const int32_t CAN_SOCK_SEVEN
!< The raw CAN protocol
Definition: CanDriver.hpp:66
const int32_t getMessageQueueSize() const
!< Gets the filter mask used by this instance
Definition: CanDriver.hpp:80
CanId _defaultSenderId
!< readMessage deadlock guard
Definition: CanDriver.hpp:102
int32_t _queueSize
!< The CAN socket file descriptor
Definition: CanDriver.hpp:107
virtual queue< CanMessage > readQueuedMessages()
!< Attempts to send a queue of messages
Definition: CanDriver.cpp:202
int32_t _canProtocol
!< The bit mask used to filter CAN messages
Definition: CanDriver.hpp:105
virtual int32_t sendMessageQueue(queue< CanMessage > messages, milliseconds delay=milliseconds(20), bool forceExtended=false)
!< Attempts to send a single CAN message
Definition: CanDriver.cpp:184
virtual CanMessage readMessageLock(bool const lock=true)
!< Uninitialises socketcan
Definition: CanDriver.cpp:130
const int32_t getSocketFd() const
!< Gets the amount of CAN messages found after last calling waitForMessages()
Definition: CanDriver.hpp:81
CanDriver(const string canInterface, const int32_t canProtocol, const int32_t filterMask, const CanId defaultSenderId=0)
!< Constructor
Definition: CanDriver.cpp:84
int32_t _socketFd
!< The protocol used when communicating via CAN
Definition: CanDriver.hpp:106
int32_t _canFilterMask
!< The ID to send messages with if no other ID was set.
Definition: CanDriver.hpp:104
CanDriver & setDefaultSenderId(const CanId id)
!< Destructor
Definition: CanDriver.hpp:75
virtual bool waitForMessages(milliseconds timeout=milliseconds(3000))
!< The socket file descriptor used by this instance.
Definition: CanDriver.cpp:99
mutex _lock
!< Mutex for thread-safety.
Definition: CanDriver.hpp:110
virtual void initialiseSocketCan()
!< Attempts to set a new CAN filter mask to the BIOS
Definition: CanDriver.cpp:243
virtual ~CanDriver()
Definition: CanDriver.hpp:72
virtual int32_t sendMessage(const CanMessage message, bool forceExtended=false)
!< Attempts to read a single message from the bus
Definition: CanDriver.cpp:153
const CanId getDefaultSenderId() const
!< Sets the default sender ID
Definition: CanDriver.hpp:77
virtual void setCanFilterMask(const int32_t mask)
!< Attempts to read all queued messages from the bus
Definition: CanDriver.cpp:217
virtual void uninitialiseSocketCan()
!< Initialises socketcan
Definition: CanDriver.cpp:284
static const int32_t CAN_MAX_DATA_LENGTH
Definition: CanDriver.hpp:64
Represents a CAN message that was received.
Definition: CanMessage.hpp:55
CanMessage(const CanId canId, const string frameData)
Definition: CanMessage.hpp:60
const string getFrameData() const
Definition: CanMessage.hpp:77
const can_frame getRawFrame() const
Definition: CanMessage.hpp:78
CanMessage(const struct can_frame frame)
Definition: CanMessage.hpp:57
struct can_frame _rawFrame
Definition: CanMessage.hpp:85
const CanId getCanId() const
Definition: CanMessage.hpp:76
CanException(string message, int32_t socket)
InvalidSocketException(string message, int32_t socket)
Main library namespace.
Definition: CanDriver.cpp:57
string formatString(const string &format, Args... args)
Formats a std string object.
Definition: CanDriver.hpp:131
Represents a CAN ID in a simple and easy-to-use manner.
Definition: CanId.hpp:48
CanId operator+(const uint32_t x) const
Definition: CanId.hpp:156
operator uint16_t() const
Definition: CanId.hpp:74
bool operator!=(const CanId &x) const
Definition: CanId.hpp:109
CanId operator|(const int64_t x) const
Definition: CanId.hpp:95
CanId operator-(const int32_t x) const
Definition: CanId.hpp:164
CanId operator-(CanId &x) const
Definition: CanId.hpp:160
bool operator<(const CanId &x) const
Definition: CanId.hpp:129
CanId operator|(const int16_t x) const
Definition: CanId.hpp:91
bool operator!=(const int32_t x) const
Definition: CanId.hpp:112
bool _isExtendedFrameId
Definition: CanId.hpp:230
CanId operator|(const CanId x) const
Definition: CanId.hpp:90
bool operator>(uint32_t x) const
Definition: CanId.hpp:125
CanId operator&(const uint32_t x) const
Definition: CanId.hpp:85
CanId operator+(const uint64_t x) const
Definition: CanId.hpp:158
bool operator==(const int16_t x) const
Definition: CanId.hpp:102
bool operator==(const CanId &x) const
Definition: CanId.hpp:101
CanId(const CanId &orig)
Definition: CanId.hpp:50
bool operator!=(const int64_t x) const
Definition: CanId.hpp:114
bool operator<(int16_t x) const
Definition: CanId.hpp:120
bool _isRemoteTransmissionRequest
Definition: CanId.hpp:228
CanId operator|(const uint16_t x) const
Definition: CanId.hpp:92
CanId operator&(const int32_t x) const
Definition: CanId.hpp:84
bool operator!=(const uint16_t x) const
Definition: CanId.hpp:111
bool operator>(const CanId &x) const
Definition: CanId.hpp:131
uint32_t _identifier
Definition: CanId.hpp:232
bool operator<=(const CanId &x) const
Definition: CanId.hpp:130
CanId operator&(const uint64_t x) const
Definition: CanId.hpp:87
CanId operator|(const uint64_t x) const
Definition: CanId.hpp:96
CanId operator+(const int64_t x) const
Definition: CanId.hpp:157
CanId(const uint32_t identifier)
Definition: CanId.hpp:54
bool operator==(CanId &x) const
Definition: CanId.hpp:100
CanId operator|(const uint32_t x) const
Definition: CanId.hpp:94
bool hasRtrFrameFlag() const
Definition: CanId.hpp:219
CanId operator-(const CanId &x) const
Definition: CanId.hpp:161
static bool isRemoteTransmissionRequest(uint32_t value)
Indicates whether the received frame is a remote transmission request.
Definition: CanId.hpp:212
static bool isErrorFrame(uint32_t value)
Indicates whether or not a given integer contains the error frame flag or not.
Definition: CanId.hpp:199
CanId operator&(CanId &x) const
Definition: CanId.hpp:80
CanId operator&(const int64_t x) const
Definition: CanId.hpp:86
CanId operator+(const int16_t x) const
Definition: CanId.hpp:153
bool operator<(CanId &x) const
Definition: CanId.hpp:117
CanId operator+(const int32_t x) const
Definition: CanId.hpp:155
operator uint32_t() const
Definition: CanId.hpp:76
CanId operator-(const uint32_t x) const
Definition: CanId.hpp:165
CanId operator&(const uint16_t x) const
Definition: CanId.hpp:83
bool operator==(const uint64_t x) const
Definition: CanId.hpp:107
bool equals(CanId otherId) const
Definition: CanId.hpp:224
CanId operator-(const uint16_t x) const
Definition: CanId.hpp:163
CanId operator+(CanId &x) const
Definition: CanId.hpp:151
CanId operator|(CanId &x) const
Definition: CanId.hpp:89
bool operator==(const uint16_t x) const
Definition: CanId.hpp:103
CanId operator|(const int32_t x) const
Definition: CanId.hpp:93
CanId operator+(const uint16_t x) const
Definition: CanId.hpp:154
bool operator>(CanId &x) const
Definition: CanId.hpp:123
bool operator>=(const CanId &x) const
Definition: CanId.hpp:132
bool operator==(const int32_t x) const
Definition: CanId.hpp:104
bool _isErrorFrame
Definition: CanId.hpp:227
CanId operator+(const CanId &x) const
Definition: CanId.hpp:152
bool _isStandardFrameId
Definition: CanId.hpp:229
bool operator>(int32_t x) const
Definition: CanId.hpp:124
CanId operator&(const int16_t x) const
Definition: CanId.hpp:82
operator int16_t() const
Definition: CanId.hpp:73
CanId operator=(const int32_t val)
Definition: CanId.hpp:136
bool operator<=(CanId &x) const
Definition: CanId.hpp:122
bool operator>=(CanId &x) const
Definition: CanId.hpp:128
CanId operator-(const uint64_t x) const
Definition: CanId.hpp:167
bool operator!=(const int16_t x) const
Definition: CanId.hpp:110
bool operator==(const int64_t x) const
Definition: CanId.hpp:106
CanId operator&(const CanId x) const
Definition: CanId.hpp:81
bool operator!=(const uint32_t x) const
Definition: CanId.hpp:113
bool operator>(uint16_t x) const
Definition: CanId.hpp:127
bool operator<(uint16_t x) const
Definition: CanId.hpp:121
bool hasErrorFrameFlag() const
Definition: CanId.hpp:218
CanId operator=(const uint32_t val)
Definition: CanId.hpp:142
bool operator==(const uint32_t x) const
Definition: CanId.hpp:105
operator int32_t() const
Definition: CanId.hpp:75
CanId operator-(const int64_t x) const
Definition: CanId.hpp:166
bool operator<(int32_t x) const
Definition: CanId.hpp:118
bool operator<(uint32_t x) const
Definition: CanId.hpp:119
CanId operator-(const int16_t x) const
Definition: CanId.hpp:162
static bool isValidIdentifier(uint32_t value)
Indicates whether or not a given integer is a valid CAN identifier.
Definition: CanId.hpp:179
bool operator!=(CanId &x) const
Definition: CanId.hpp:108
bool isStandardFrameId() const
Definition: CanId.hpp:220
bool operator>(int16_t x) const
Definition: CanId.hpp:126
bool operator!=(const uint64_t x) const
Definition: CanId.hpp:115
CanId operator=(const int64_t val)
Definition: CanId.hpp:147
bool isExtendedFrameId() const
Definition: CanId.hpp:221