Libsockcanpp
A complete C++ wrapper around socketcan.
InvalidSocketException.hpp
Go to the documentation of this file.
1/**
2 * @file InvalidSocketException.hpp
3 * @author Simon Cahill (simonc@online.de)
4 * @brief Contains the implementation of an exception that may be thrown when an invalid CAN socket is detected.
5 * @version 0.1
6 * @date 2020-07-02
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_EXCEPTIONS_INVALIDSOCKETEXCEPTION_HPP
26#define LIBSOCKCANPP_INCLUDE_EXCEPTIONS_INVALIDSOCKETEXCEPTION_HPP
27
28#include <exception>
29#include <string>
30
31namespace sockcanpp { namespace exceptions {
32
33 using std::exception;
34 using std::string;
35
36 /**
37 * @brief An exception that may be thrown when an error occurs while closing a CAN socket.
38 */
39 class InvalidSocketException: public exception {
40 public: // +++ Constructor / Destructor +++
41 InvalidSocketException(string message, int32_t socket): _message(message), _socket(socket) {}
43
44 public: // +++ Overrides +++
45 const char* what() { return _message.c_str(); }
46
47 public: // +++ Getter +++
48 const int32_t getSocket() const { return _socket; }
49
50 private:
51 int32_t _socket;
52
54 };
55
56} /* exceptions */ } /* sockcanpp */
57
58#endif // LIBSOCKCANPP_INCLUDE_EXCEPTIONS_INVALIDSOCKETEXCEPTION_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
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(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(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