2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
25#ifndef LIBSOCKPP_INCLUDE_CANID_HPP
26#define LIBSOCKPP_INCLUDE_CANID_HPP
35#include <system_error>
40 using std::error_code;
42 using std::generic_category;
43 using std::system_error;
46
47
58 if (((int32_t)log2(identifier) + 1) < 11) {
72#pragma region "Implicit Cast Operators"
79#pragma region "Bitwise Operators"
99#pragma region "Comparison Operators"
106 bool operator ==(
const int64_t x)
const {
return (x > UINT32_MAX || x < INT32_MIN) ?
false : x ==
_identifier; }
114 bool operator !=(
const int64_t x)
const {
return (x > UINT32_MAX || x < INT32_MIN) ?
false : x !=
_identifier; }
135#pragma region "Assignment Operators"
137 uint32_t tmpVal = val;
138 auto tmp = (
isValidIdentifier(tmpVal
) ?
CanId(val
) :
throw system_error(error_code(0x5421, generic_category()),
"INVALID CAST: ID is extended or invalid!"));
144 return (
isValidIdentifier(tmp
) ?
CanId(val
) :
throw system_error(error_code(0x5421, generic_category()),
"INVALID CAST: ID is extended or invalid!"));
150#pragma region "Arithmetic Operators"
172
173
174
175
176
177
178
180 int32_t tmpValue = ((int32_t)log2(value) + 2);
183 if (tmpValue >= 29) {
184 value = (value & CAN_EFF_FLAG) ? (value & CAN_EFF_MASK) : (value & CAN_SFF_MASK);
185 tmpValue = ((int32_t)log2(value) + 1);
188 return (value == 0) || ((tmpValue <= 29 && tmpValue > 0));
192
193
194
195
196
197
198
200 try {
return bitset<
sizeof(int32_t)>(value).test(29); }
201 catch (...) {
return false; }
205
206
207
208
209
210
211
213 try {
return bitset<
sizeof(int32_t)>(value).test(30); }
214 catch (...) {
return false; }
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.
CanId operator+(const uint32_t x) const
operator uint16_t() const
bool operator!=(const CanId &x) const
CanId operator|(const int64_t x) const
CanId operator-(const int32_t x) const
CanId operator-(CanId &x) const
bool operator<(const CanId &x) const
CanId operator|(const int16_t x) const
bool operator!=(const int32_t x) const
CanId operator|(const CanId x) const
bool operator>(uint32_t x) const
CanId operator&(const uint32_t x) const
CanId operator+(const uint64_t x) const
bool operator==(const int16_t x) const
bool operator==(const CanId &x) const
bool operator!=(const int64_t x) const
bool operator<(int16_t x) const
bool _isRemoteTransmissionRequest
CanId operator|(const uint16_t x) const
CanId operator&(const int32_t x) const
bool operator!=(const uint16_t x) const
bool operator>(const CanId &x) const
bool operator<=(const CanId &x) const
CanId operator&(const uint64_t x) const
CanId operator|(const uint64_t x) const
CanId operator+(const int64_t x) const
CanId(const uint32_t identifier)
bool operator==(CanId &x) const
CanId operator|(const uint32_t x) const
bool hasRtrFrameFlag() const
CanId operator-(const CanId &x) const
static bool isRemoteTransmissionRequest(uint32_t value)
Indicates whether the received frame is a remote transmission request.
static bool isErrorFrame(uint32_t value)
Indicates whether or not a given integer contains the error frame flag or not.
CanId operator&(CanId &x) const
CanId operator&(const int64_t x) const
CanId operator+(const int16_t x) const
bool operator<(CanId &x) const
CanId operator+(const int32_t x) const
operator uint32_t() const
CanId operator-(const uint32_t x) const
CanId operator&(const uint16_t x) const
bool operator==(const uint64_t x) const
bool equals(CanId otherId) const
CanId operator-(const uint16_t x) const
CanId operator+(CanId &x) const
CanId operator|(CanId &x) const
bool operator==(const uint16_t x) const
CanId operator|(const int32_t x) const
CanId operator+(const uint16_t x) const
bool operator>(CanId &x) const
bool operator>=(const CanId &x) const
bool operator==(const int32_t x) const
CanId operator+(const CanId &x) const
bool operator>(int32_t x) const
CanId operator&(const int16_t x) const
CanId operator=(const int32_t val)
bool operator<=(CanId &x) const
bool operator>=(CanId &x) const
CanId operator-(const uint64_t x) const
bool operator!=(const int16_t x) const
bool operator==(const int64_t x) const
CanId operator&(const CanId x) const
bool operator!=(const uint32_t x) const
bool operator>(uint16_t x) const
bool operator<(uint16_t x) const
bool hasErrorFrameFlag() const
CanId operator=(const uint32_t val)
bool operator==(const uint32_t x) const
CanId operator-(const int64_t x) const
bool operator<(int32_t x) const
bool operator<(uint32_t x) const
CanId operator-(const int16_t x) const
static bool isValidIdentifier(uint32_t value)
Indicates whether or not a given integer is a valid CAN identifier.
bool operator!=(CanId &x) const
bool isStandardFrameId() const
bool operator>(int16_t x) const
bool operator!=(const uint64_t x) const
CanId operator=(const int64_t val)
bool isExtendedFrameId() const