Browse Source

reformat sockcanpp

master
zhaohe 2 years ago
parent
commit
5199d0a9ee
  1. 0
      core/driver/sockcanpp/.clang-format
  2. 95
      core/driver/sockcanpp/CanDriver.cpp
  3. 129
      core/driver/sockcanpp/CanDriver.hpp
  4. 79
      core/driver/sockcanpp/CanId.hpp
  5. 24
      core/driver/sockcanpp/CanMessage.hpp
  6. 6
      core/driver/sockcanpp/README.md
  7. 12
      core/driver/sockcanpp/exceptions/CanCloseException.hpp
  8. 17
      core/driver/sockcanpp/exceptions/CanException.hpp
  9. 12
      core/driver/sockcanpp/exceptions/CanInitException.hpp
  10. 17
      core/driver/sockcanpp/exceptions/InvalidSocketException.hpp

0
core/driver/sockcanpp/.clang-format

95
core/driver/sockcanpp/CanDriver.cpp

@ -74,18 +74,14 @@ using std::this_thread::sleep_for;
//////////////////////////////////////
const int32_t CanDriver::CAN_MAX_DATA_LENGTH = 8;
const int32_t CanDriver::CAN_SOCK_RAW = CAN_RAW;
const int32_t CanDriver::CAN_SOCK_SEVEN = 7;
const int32_t CanDriver::CAN_SOCK_RAW = CAN_RAW;
const int32_t CanDriver::CAN_SOCK_SEVEN = 7;
CanDriver::CanDriver(string canInterface, int32_t canProtocol,
const CanId defaultSenderId)
: CanDriver(canInterface, canProtocol, 0 /* match all */, defaultSenderId) {
}
CanDriver::CanDriver(string canInterface, int32_t canProtocol, const CanId defaultSenderId)
: CanDriver(canInterface, canProtocol, 0 /* match all */, defaultSenderId) {}
CanDriver::CanDriver(const string canInterface, const int32_t canProtocol,
const int32_t filterMask, const CanId defaultSenderId)
: _defaultSenderId(defaultSenderId), _canProtocol(canProtocol),
_canInterface(canInterface), _canFilterMask(filterMask), _socketFd(-1) {
CanDriver::CanDriver(const string canInterface, const int32_t canProtocol, const int32_t filterMask, const CanId defaultSenderId)
: _defaultSenderId(defaultSenderId), _canProtocol(canProtocol), _canInterface(canInterface), _canFilterMask(filterMask), _socketFd(-1) {
initialiseSocketCan();
}
@ -105,9 +101,9 @@ bool CanDriver::waitForMessages(milliseconds timeout) {
unique_lock<mutex> locky(_lock);
fd_set readFileDescriptors;
fd_set readFileDescriptors;
timeval waitTime;
waitTime.tv_sec = timeout.count() / 1000;
waitTime.tv_sec = timeout.count() / 1000;
waitTime.tv_usec = timeout.count() * 1000;
FD_ZERO(&readFileDescriptors);
@ -132,19 +128,13 @@ CanMessage CanDriver::readMessage() { return readMessageLock(); }
*/
CanMessage CanDriver::readMessageLock(bool const lock) {
std::unique_ptr<std::unique_lock<std::mutex>> _lockLck{nullptr};
if (lock)
_lockLck = std::unique_ptr<std::unique_lock<std::mutex>>{
new std::unique_lock<std::mutex>{_lock}};
if (0 > _socketFd)
throw InvalidSocketException("Invalid socket!", _socketFd);
int32_t readBytes{0};
if (lock) _lockLck = std::unique_ptr<std::unique_lock<std::mutex>>{new std::unique_lock<std::mutex>{_lock}};
if (0 > _socketFd) throw InvalidSocketException("Invalid socket!", _socketFd);
int32_t readBytes{0};
can_frame canFrame;
memset(&canFrame, 0, sizeof(can_frame));
readBytes = read(_socketFd, &canFrame, sizeof(can_frame));
if (0 > readBytes)
throw CanException(formatString("FAILED to read from CAN! Error: %d => %s",
errno, strerror(errno)),
_socketFd);
if (0 > readBytes) throw CanException(formatString("FAILED to read from CAN! Error: %d => %s", errno, strerror(errno)), _socketFd);
return CanMessage{canFrame};
}
@ -166,11 +156,7 @@ int32_t CanDriver::sendMessage(const CanMessage message, bool forceExtended) {
int32_t bytesWritten = 0;
if (message.getFrameData().size() > CAN_MAX_DATA_LENGTH) {
throw CanException(
formatString(
"INVALID data length! Message must be smaller than %d bytes!",
CAN_MAX_DATA_LENGTH),
_socketFd);
throw CanException(formatString("INVALID data length! Message must be smaller than %d bytes!", CAN_MAX_DATA_LENGTH), _socketFd);
}
auto canFrame = message.getRawFrame();
@ -182,10 +168,7 @@ int32_t CanDriver::sendMessage(const CanMessage message, bool forceExtended) {
bytesWritten = write(_socketFd, (const void *)&canFrame, sizeof(canFrame));
if (bytesWritten == -1) {
throw CanException(
formatString("FAILED to write data to socket! Error: %d => %s", errno,
strerror(errno)),
_socketFd);
throw CanException(formatString("FAILED to write data to socket! Error: %d => %s", errno, strerror(errno)), _socketFd);
}
return bytesWritten;
@ -200,8 +183,7 @@ int32_t CanDriver::sendMessage(const CanMessage message, bool forceExtended) {
*
* @return int32_t The total amount of bytes sent.
*/
int32_t CanDriver::sendMessageQueue(queue<CanMessage> messages,
milliseconds delay, bool forceExtended) {
int32_t CanDriver::sendMessageQueue(queue<CanMessage> messages, milliseconds delay, bool forceExtended) {
if (_socketFd < 0) {
throw InvalidSocketException("Invalid socket!", _socketFd);
}
@ -224,12 +206,10 @@ int32_t CanDriver::sendMessageQueue(queue<CanMessage> messages,
* buffer.
*/
queue<CanMessage> CanDriver::readQueuedMessages() {
if (_socketFd < 0)
throw InvalidSocketException("Invalid socket!", _socketFd);
if (_socketFd < 0) throw InvalidSocketException("Invalid socket!", _socketFd);
unique_lock<mutex> locky(_lock);
queue<CanMessage> messages;
for (int32_t i = _queueSize; 0 < i; --i)
messages.emplace(readMessageLock(false));
queue<CanMessage> messages;
for (int32_t i = _queueSize; 0 < i; --i) messages.emplace(readMessageLock(false));
return messages;
}
@ -244,16 +224,13 @@ void CanDriver::setCanFilterMask(const int32_t mask) {
}
unique_lock<mutex> locky(_lock);
can_filter canFilter;
can_filter canFilter;
canFilter.can_id = _defaultSenderId;
canFilter.can_id = _defaultSenderId;
canFilter.can_mask = mask;
if (setsockopt(_socketFd, SOL_CAN_RAW, CAN_RAW_FILTER, &canFilter,
sizeof(canFilter)) == -1) {
throw CanInitException(formatString(
"FAILED to set CAN filter mask %x on socket %d! Error: %d => %s", mask,
_socketFd, errno, strerror(errno)));
if (setsockopt(_socketFd, SOL_CAN_RAW, CAN_RAW_FILTER, &canFilter, sizeof(canFilter)) == -1) {
throw CanInitException(formatString("FAILED to set CAN filter mask %x on socket %d! Error: %d => %s", mask, _socketFd, errno, strerror(errno)));
}
_canFilterMask = mask;
@ -270,9 +247,9 @@ void CanDriver::initialiseSocketCan() {
// unique_lock<mutex> locky(_lock);
struct sockaddr_can address;
struct ifreq ifaceRequest;
int64_t fdOptions = 0;
int32_t tmpReturn;
struct ifreq ifaceRequest;
int64_t fdOptions = 0;
int32_t tmpReturn;
memset(&address, 0, sizeof(sizeof(struct sockaddr_can)));
memset(&ifaceRequest, 0, sizeof(sizeof(struct ifreq)));
@ -280,33 +257,26 @@ void CanDriver::initialiseSocketCan() {
_socketFd = socket(PF_CAN, SOCK_RAW, _canProtocol);
if (_socketFd == -1) {
throw CanInitException(
formatString("FAILED to initialise socketcan! Error: %d => %s", errno,
strerror(errno)));
throw CanInitException(formatString("FAILED to initialise socketcan! Error: %d => %s", errno, strerror(errno)));
}
strcpy(ifaceRequest.ifr_name, _canInterface.c_str());
if ((tmpReturn = ioctl(_socketFd, SIOCGIFINDEX, &ifaceRequest)) == -1) {
throw CanInitException(formatString(
"FAILED to perform IO control operation on socket %s! Error: %d => %s",
_canInterface.c_str(), errno, strerror(errno)));
throw CanInitException(formatString("FAILED to perform IO control operation on socket %s! Error: %d => %s", _canInterface.c_str(), errno, strerror(errno)));
}
fdOptions = fcntl(_socketFd, F_GETFL);
fdOptions |= O_NONBLOCK;
tmpReturn = fcntl(_socketFd, F_SETFL, fdOptions);
address.can_family = AF_CAN;
address.can_family = AF_CAN;
address.can_ifindex = ifaceRequest.ifr_ifindex;
setCanFilterMask(_canFilterMask);
if ((tmpReturn = bind(_socketFd, (struct sockaddr *)&address,
sizeof(address))) == -1) {
throw CanInitException(
formatString("FAILED to bind to socket CAN! Error: %d => %s", errno,
strerror(errno)));
if ((tmpReturn = bind(_socketFd, (struct sockaddr *)&address, sizeof(address))) == -1) {
throw CanInitException(formatString("FAILED to bind to socket CAN! Error: %d => %s", errno, strerror(errno)));
}
}
@ -321,11 +291,10 @@ void CanDriver::uninitialiseSocketCan() {
}
if (close(_socketFd) == -1) {
throw CanCloseException(formatString(
"FAILED to close CAN socket! Error: %d => %s", errno, strerror(errno)));
throw CanCloseException(formatString("FAILED to close CAN socket! Error: %d => %s", errno, strerror(errno)));
}
_socketFd = -1;
}
} // namespace sockcanpp
} // namespace sockcanpp

129
core/driver/sockcanpp/CanDriver.hpp

@ -62,84 +62,68 @@ using std::chrono::milliseconds;
* needs.
*/
class CanDriver {
public: // +++ Static +++
static const int32_t CAN_MAX_DATA_LENGTH; ///!< The maximum amount of bytes
/// allowed in a single CAN frame
static const int32_t CAN_SOCK_RAW; ///!< The raw CAN protocol
static const int32_t CAN_SOCK_SEVEN; ///!< A separate CAN protocol, used by
/// certain embedded device OEMs.
public: // +++ Constructor / Destructor +++
public: // +++ Static +++
static const int32_t CAN_MAX_DATA_LENGTH; ///!< The maximum amount of bytes
/// allowed in a single CAN frame
static const int32_t CAN_SOCK_RAW; ///!< The raw CAN protocol
static const int32_t CAN_SOCK_SEVEN; ///!< A separate CAN protocol, used by
/// certain embedded device OEMs.
public: // +++ Constructor / Destructor +++
CanDriver(const string canInterface, const int32_t canProtocol,
const CanId defaultSenderId = 0); ///!< Constructor
CanDriver(const string canInterface, const int32_t canProtocol,
const int32_t filterMask, const CanId defaultSenderId = 0);
const CanId defaultSenderId = 0); ///!< Constructor
CanDriver(const string canInterface, const int32_t canProtocol, const int32_t filterMask, const CanId defaultSenderId = 0);
CanDriver() {}
virtual ~CanDriver() { uninitialiseSocketCan(); } ///!< Destructor
virtual ~CanDriver() { uninitialiseSocketCan(); } ///!< Destructor
public: // +++ Getter / Setter +++
public: // +++ Getter / Setter +++
CanDriver &setDefaultSenderId(const CanId id) {
this->_defaultSenderId = id;
return *this;
} ///!< Sets the default sender ID
} ///!< Sets the default sender ID
const CanId getDefaultSenderId() const {
return this->_defaultSenderId;
} ///!< Gets the default sender ID
const CanId getDefaultSenderId() const { return this->_defaultSenderId; } ///!< Gets the default sender ID
const int32_t getFilterMask() const {
return this->_canFilterMask;
} ///!< Gets the filter mask used by this instance
const int32_t getFilterMask() const { return this->_canFilterMask; } ///!< Gets the filter mask used by this instance
const int32_t getMessageQueueSize() const {
return this->_queueSize;
} ///!< Gets the amount of CAN messages found after last calling
/// waitForMessages()
const int32_t getSocketFd() const {
return this->_socketFd;
} ///!< The socket file descriptor used by this instance.
public: // +++ I/O +++
virtual bool
waitForMessages(milliseconds timeout = milliseconds(
3000)); ///!< Waits for CAN messages to appear
virtual CanMessage
readMessage(); ///!< Attempts to read a single message from the bus
virtual int32_t sendMessage(
const CanMessage message,
bool forceExtended = false); ///!< Attempts to send a single CAN message
virtual int32_t sendMessageQueue(
queue<CanMessage> messages, milliseconds delay = milliseconds(20),
bool forceExtended = false); ///!< Attempts to send a queue of messages
virtual queue<CanMessage>
readQueuedMessages(); ///!< Attempts to read all queued messages from the bus
virtual void
setCanFilterMask(const int32_t mask); ///!< Attempts to set a new CAN filter
/// mask to the BIOS
protected: // +++ Socket Management +++
virtual void initialiseSocketCan(); ///!< Initialises socketcan
virtual void uninitialiseSocketCan(); ///!< Uninitialises socketcan
private:
virtual CanMessage
readMessageLock(bool const lock = true); ///!< readMessage deadlock guard
CanId _defaultSenderId; ///!< The ID to send messages with if no other ID was
/// set.
int32_t _canFilterMask; ///!< The bit mask used to filter CAN messages
int32_t _canProtocol; ///!< The protocol used when communicating via CAN
int32_t _socketFd; ///!< The CAN socket file descriptor
int32_t
_queueSize; ////!< The size of the message queue read by waitForMessages()
mutex _lock; ///!< Mutex for thread-safety.
string _canInterface; ///!< The CAN interface used for communication (e.g.
/// can0, can1, ...)
} ///!< Gets the amount of CAN messages found after last calling
/// waitForMessages()
const int32_t getSocketFd() const { return this->_socketFd; } ///!< The socket file descriptor used by this instance.
public: // +++ I/O +++
virtual bool waitForMessages(milliseconds timeout = milliseconds(3000)); ///!< Waits for CAN messages to appear
virtual CanMessage readMessage(); ///!< Attempts to read a single message from the bus
virtual int32_t sendMessage(const CanMessage message,
bool forceExtended = false); ///!< Attempts to send a single CAN message
virtual int32_t sendMessageQueue(queue<CanMessage> messages, milliseconds delay = milliseconds(20),
bool forceExtended = false); ///!< Attempts to send a queue of messages
virtual queue<CanMessage> readQueuedMessages(); ///!< Attempts to read all queued messages from the bus
virtual void setCanFilterMask(const int32_t mask); ///!< Attempts to set a new CAN filter
/// mask to the BIOS
protected: // +++ Socket Management +++
virtual void initialiseSocketCan(); ///!< Initialises socketcan
virtual void uninitialiseSocketCan(); ///!< Uninitialises socketcan
private:
virtual CanMessage readMessageLock(bool const lock = true); ///!< readMessage deadlock guard
CanId _defaultSenderId; ///!< The ID to send messages with if no other ID was
/// set.
int32_t _canFilterMask; ///!< The bit mask used to filter CAN messages
int32_t _canProtocol; ///!< The protocol used when communicating via CAN
int32_t _socketFd; ///!< The CAN socket file descriptor
int32_t _queueSize; ////!< The size of the message queue read by waitForMessages()
mutex _lock; ///!< Mutex for thread-safety.
string _canInterface; ///!< The CAN interface used for communication (e.g.
/// can0, can1, ...)
};
/**
@ -157,15 +141,14 @@ private:
template <typename... Args>
string formatString(const string &format, Args... args) {
using std::unique_ptr;
auto stringSize = snprintf(NULL, 0, format.c_str(), args...) + 1; // +1 for \0
auto stringSize = snprintf(NULL, 0, format.c_str(), args...) + 1; // +1 for \0
unique_ptr<char[]> buffer(new char[stringSize]);
snprintf(buffer.get(), stringSize, format.c_str(), args...);
return string(buffer.get(), buffer.get() + stringSize -
1); // std::string handles termination for us.
return string(buffer.get(), buffer.get() + stringSize - 1); // std::string handles termination for us.
}
} // namespace sockcanpp
} // namespace sockcanpp
#endif // LIBSOCKCANPP_INCLUDE_CANDRIVER_HPP
#endif // LIBSOCKCANPP_INCLUDE_CANDRIVER_HPP

79
core/driver/sockcanpp/CanId.hpp

@ -29,10 +29,11 @@
//////////////////////////////
// SYSTEM INCLUDES //
//////////////////////////////
#include <linux/can.h>
#include <bitset>
#include <cmath>
#include <exception>
#include <linux/can.h>
namespace sockcanpp {
@ -46,9 +47,10 @@ using std::system_error;
* @brief Represents a CAN ID in a simple and easy-to-use manner.
*/
struct CanId {
public: // +++ Constructors +++
public: // +++ Constructors +++
CanId(const CanId &orig)
: _identifier(orig._identifier), _isErrorFrame(orig._isErrorFrame),
: _identifier(orig._identifier),
_isErrorFrame(orig._isErrorFrame),
_isRemoteTransmissionRequest(orig._isRemoteTransmissionRequest),
_isStandardFrameId(orig._isStandardFrameId),
_isExtendedFrameId(orig._isExtendedFrameId) { /* copy */
@ -72,18 +74,13 @@ public: // +++ Constructors +++
CanId() : _identifier(0), _isStandardFrameId(true) {}
public: // +++ Operators +++
public: // +++ Operators +++
operator int16_t() const {
return isStandardFrameId()
? (int16_t)_identifier
: throw system_error(error_code(0xbad1d, generic_category()),
"INVALID CAST: ID is extended or invalid!");
return isStandardFrameId() ? (int16_t)_identifier : throw system_error(error_code(0xbad1d, generic_category()), "INVALID CAST: ID is extended or invalid!");
}
operator uint16_t() const {
return isStandardFrameId()
? (uint16_t)_identifier
: throw system_error(error_code(0xbad1d, generic_category()),
"INVALID CAST: ID is extended or invalid!");
return isStandardFrameId() ? (uint16_t)_identifier
: throw system_error(error_code(0xbad1d, generic_category()), "INVALID CAST: ID is extended or invalid!");
}
operator int32_t() const { return _identifier; }
operator uint32_t() const { return _identifier; }
@ -112,25 +109,16 @@ public: // +++ Operators +++
bool operator==(const uint16_t x) const { return uint16_t(_identifier) == x; }
bool operator==(const int32_t x) const { return int32_t(_identifier) == x; }
bool operator==(const uint32_t x) const { return uint32_t(_identifier) == x; }
bool operator==(const int64_t x) const {
return (x > UINT32_MAX || x < INT32_MIN) ? false
: x == int64_t(_identifier);
}
bool operator==(const uint64_t x) const {
return x > UINT32_MAX ? false : x == uint64_t(_identifier);
}
bool operator==(const int64_t x) const { return (x > UINT32_MAX || x < INT32_MIN) ? false : x == int64_t(_identifier); }
bool operator==(const uint64_t x) const { return x > UINT32_MAX ? false : x == uint64_t(_identifier); }
bool operator!=(CanId &x) const { return _identifier != x._identifier; }
bool operator!=(const CanId &x) const { return _identifier != x._identifier; }
bool operator!=(const int16_t x) const { return int16_t(_identifier) != x; }
bool operator!=(const uint16_t x) const { return uint16_t(_identifier) != x; }
bool operator!=(const int32_t x) const { return int32_t(_identifier) != x; }
bool operator!=(const uint32_t x) const { return uint32_t(_identifier) != x; }
bool operator!=(const int64_t x) const {
return (x > UINT32_MAX || x < INT32_MIN) ? false : x != _identifier;
}
bool operator!=(const uint64_t x) const {
return x > UINT32_MAX ? false : x != _identifier;
}
bool operator!=(const int64_t x) const { return (x > UINT32_MAX || x < INT32_MIN) ? false : x != _identifier; }
bool operator!=(const uint64_t x) const { return x > UINT32_MAX ? false : x != _identifier; }
bool operator<(CanId &x) const { return x._identifier < _identifier; }
bool operator<(int32_t x) const { return x < int32_t(_identifier); }
@ -151,21 +139,14 @@ public: // +++ Operators +++
CanId operator=(const int32_t val) {
uint32_t tmpVal = val;
auto tmp =
(isValidIdentifier(tmpVal)
? CanId(val)
: throw system_error(error_code(0x5421, generic_category()),
"INVALID CAST: ID is extended or invalid!"));
auto tmp =
(isValidIdentifier(tmpVal) ? CanId(val) : throw system_error(error_code(0x5421, generic_category()), "INVALID CAST: ID is extended or invalid!"));
return tmp;
}
CanId operator=(const uint32_t val) {
uint32_t tmp = val;
return (
isValidIdentifier(tmp)
? CanId(val)
: throw system_error(error_code(0x5421, generic_category()),
"INVALID CAST: ID is extended or invalid!"));
return (isValidIdentifier(tmp) ? CanId(val) : throw system_error(error_code(0x5421, generic_category()), "INVALID CAST: ID is extended or invalid!"));
}
CanId operator=(const int64_t val) { return operator=((int32_t)val); }
@ -188,7 +169,7 @@ public: // +++ Operators +++
CanId operator-(const int64_t x) const { return _identifier - x; }
CanId operator-(const uint64_t x) const { return _identifier - x; }
public: // +++ Validity Checks +++
public: // +++ Validity Checks +++
/**
* @brief Indicates whether or not a given integer is a valid CAN identifier.
*
@ -198,17 +179,15 @@ public: // +++ Validity Checks +++
* @return false Otherwise.
*/
static bool isValidIdentifier(uint32_t value) {
int32_t tmpValue = ((int32_t)log2(value) + 2); // Get bit count
int32_t tmpValue = ((int32_t)log2(value) + 2); // Get bit count
// Check for extended frame flag
if (tmpValue >= 29) {
value = (value & CAN_EFF_FLAG) ? (value & CAN_EFF_MASK)
: (value & CAN_SFF_MASK);
tmpValue = ((int32_t)log2(value) + 1); // Get bit count again
value = (value & CAN_EFF_FLAG) ? (value & CAN_EFF_MASK) : (value & CAN_SFF_MASK);
tmpValue = ((int32_t)log2(value) + 1); // Get bit count again
}
return (value == 0) /* Default value, also valid ID */ ||
((tmpValue <= 29 && tmpValue > 0));
return (value == 0) /* Default value, also valid ID */ || ((tmpValue <= 29 && tmpValue > 0));
}
/**
@ -245,24 +224,24 @@ public: // +++ Validity Checks +++
}
}
public: // +++ Getters +++
public: // +++ Getters +++
bool hasErrorFrameFlag() const { return _isErrorFrame; }
bool hasRtrFrameFlag() const { return _isRemoteTransmissionRequest; }
bool isStandardFrameId() const { return _isStandardFrameId; }
bool isExtendedFrameId() const { return _isExtendedFrameId; }
public: // +++ Equality Checks +++
public: // +++ Equality Checks +++
bool equals(CanId otherId) const { return *this == otherId; }
private: // +++ Variables +++
bool _isErrorFrame = false;
private: // +++ Variables +++
bool _isErrorFrame = false;
bool _isRemoteTransmissionRequest = false;
bool _isStandardFrameId = false;
bool _isExtendedFrameId = false;
bool _isStandardFrameId = false;
bool _isExtendedFrameId = false;
uint32_t _identifier = 0;
};
} // namespace sockcanpp
} // namespace sockcanpp
#endif // LIBSOCKPP_INCLUDE_CANID_HPP
#endif // LIBSOCKPP_INCLUDE_CANID_HPP

24
core/driver/sockcanpp/CanMessage.hpp

@ -52,16 +52,12 @@ using std::system_error;
* @brief Represents a CAN message that was received.
*/
class CanMessage {
public: // +++ Constructor / Destructor +++
CanMessage(const struct can_frame frame)
: _canIdentifier(frame.can_id),
_frameData((const char *)frame.data, frame.can_dlc), _rawFrame(frame) {}
public: // +++ Constructor / Destructor +++
CanMessage(const struct can_frame frame) : _canIdentifier(frame.can_id), _frameData((const char *)frame.data, frame.can_dlc), _rawFrame(frame) {}
CanMessage(const CanId canId, const string frameData)
: _canIdentifier(canId), _frameData(frameData) {
CanMessage(const CanId canId, const string frameData) : _canIdentifier(canId), _frameData(frameData) {
if (frameData.size() > 8) {
throw system_error(error_code(0xbadd1c, generic_category()),
"Payload too big!");
throw system_error(error_code(0xbadd1c, generic_category()), "Payload too big!");
}
struct can_frame rawFrame;
@ -74,12 +70,12 @@ public: // +++ Constructor / Destructor +++
virtual ~CanMessage() {}
public: // +++ Getters +++
const CanId getCanId() const { return this->_canIdentifier; }
const string getFrameData() const { return this->_frameData; }
public: // +++ Getters +++
const CanId getCanId() const { return this->_canIdentifier; }
const string getFrameData() const { return this->_frameData; }
const can_frame getRawFrame() const { return this->_rawFrame; }
private:
private:
CanId _canIdentifier;
string _frameData;
@ -87,6 +83,6 @@ private:
struct can_frame _rawFrame;
};
} // namespace sockcanpp
} // namespace sockcanpp
#endif // LIBSOCKCANPP_INCLUDE_CANMESSAGE_HPP
#endif // LIBSOCKCANPP_INCLUDE_CANMESSAGE_HPP

6
core/driver/sockcanpp/README.md

@ -3,6 +3,12 @@
BaseOn:
https://github.com/SimonCahill/libsockcanpp
4d0e6a3e679a34dee4449a9d22b7f794a3cf9871
localGit
http://192.168.1.3:3000/z3rd_lib/libsockcanpp
Demo:
http://192.168.1.3:3000/z3rd_lib/libsockcanpp/src/branch/master/test/src/Main.cpp
```

12
core/driver/sockcanpp/exceptions/CanCloseException.hpp

@ -40,18 +40,18 @@ using std::string;
* CAN socket.
*/
class CanCloseException : public exception {
public: // +++ Constructor / Destructor +++
public: // +++ Constructor / Destructor +++
CanCloseException(string message) : _message(message) {}
~CanCloseException() {}
public: // +++ Overrides +++
public: // +++ Overrides +++
const char *what() { return _message.c_str(); }
private:
private:
string _message;
};
} // namespace exceptions
} // namespace sockcanpp
} // namespace exceptions
} // namespace sockcanpp
#endif // LIBSOCKCANPP_INCLUDE_EXCEPTIONS_CANCLOSEEXCEPTION_HPP
#endif // LIBSOCKCANPP_INCLUDE_EXCEPTIONS_CANCLOSEEXCEPTION_HPP

17
core/driver/sockcanpp/exceptions/CanException.hpp

@ -40,24 +40,23 @@ using std::string;
* CAN socket.
*/
class CanException : public exception {
public: // +++ Constructor / Destructor +++
CanException(string message, int32_t socket)
: _message(message), _socket(socket) {}
public: // +++ Constructor / Destructor +++
CanException(string message, int32_t socket) : _message(message), _socket(socket) {}
~CanException() {}
public: // +++ Overrides +++
public: // +++ Overrides +++
const char *what() { return _message.c_str(); }
public: // +++ Getter +++
public: // +++ Getter +++
const int32_t getSocket() const { return _socket; }
private:
private:
int32_t _socket;
string _message;
};
} // namespace exceptions
} // namespace sockcanpp
} // namespace exceptions
} // namespace sockcanpp
#endif // LIBSOCKCANPP_INCLUDE_EXCEPTIONS_CANEXCEPTION_HPP
#endif // LIBSOCKCANPP_INCLUDE_EXCEPTIONS_CANEXCEPTION_HPP

12
core/driver/sockcanpp/exceptions/CanInitException.hpp

@ -40,18 +40,18 @@ using std::string;
* initialising a CAN socket.
*/
class CanInitException : public exception {
public: // +++ Constructor / Destructor +++
public: // +++ Constructor / Destructor +++
CanInitException(string message) : _message(message) {}
virtual ~CanInitException() {}
public: // +++ Override +++
public: // +++ Override +++
const char *what() { return _message.c_str(); }
private:
private:
string _message;
};
} // namespace exceptions
} // namespace sockcanpp
} // namespace exceptions
} // namespace sockcanpp
#endif // LIBSOCKCANPP_INCLUDE_EXCEPTIONS_CANINITEXCEPTION_HPP
#endif // LIBSOCKCANPP_INCLUDE_EXCEPTIONS_CANINITEXCEPTION_HPP

17
core/driver/sockcanpp/exceptions/InvalidSocketException.hpp

@ -40,24 +40,23 @@ using std::string;
* CAN socket.
*/
class InvalidSocketException : public exception {
public: // +++ Constructor / Destructor +++
InvalidSocketException(string message, int32_t socket)
: _message(message), _socket(socket) {}
public: // +++ Constructor / Destructor +++
InvalidSocketException(string message, int32_t socket) : _message(message), _socket(socket) {}
~InvalidSocketException() {}
public: // +++ Overrides +++
public: // +++ Overrides +++
const char *what() { return _message.c_str(); }
public: // +++ Getter +++
public: // +++ Getter +++
const int32_t getSocket() const { return _socket; }
private:
private:
int32_t _socket;
string _message;
};
} // namespace exceptions
} // namespace sockcanpp
} // namespace exceptions
} // namespace sockcanpp
#endif // LIBSOCKCANPP_INCLUDE_EXCEPTIONS_INVALIDSOCKETEXCEPTION_HPP
#endif // LIBSOCKCANPP_INCLUDE_EXCEPTIONS_INVALIDSOCKETEXCEPTION_HPP
Loading…
Cancel
Save