interface.h
Go to the documentation of this file.
1 #ifndef H_CAN_INTERFACE
2 #define H_CAN_INTERFACE
3 
4 #include <array>
5 #include <memory>
6 #include <functional>
7 
8 #include <boost/system/error_code.hpp>
9 
13 
14 namespace can{
15 
17 struct Header{
18  static const unsigned int ID_MASK = (1u << 29)-1;
19  static const unsigned int ERROR_MASK = (1u << 29);
20  static const unsigned int RTR_MASK = (1u << 30);
21  static const unsigned int EXTENDED_MASK = (1u << 31);
22 
23  unsigned int id:29;
24  unsigned int is_error:1;
25  unsigned int is_rtr:1;
26  unsigned int is_extended:1;
27 
28  bool isValid() const{
29  return id < (is_extended?(1<<29):(1<<11));
30  }
31  unsigned int fullid() const { return id | (is_error?ERROR_MASK:0) | (is_rtr?RTR_MASK:0) | (is_extended?EXTENDED_MASK:0); }
32  unsigned int key() const { return is_error ? (ERROR_MASK) : fullid(); }
33  [[deprecated("use key() instead")]] explicit operator unsigned int() const { return key(); }
34 
42  : id(0),is_error(0),is_rtr(0), is_extended(0) {}
43 
44  Header(unsigned int i, bool extended, bool rtr, bool error)
45  : id(i),is_error(error?1:0),is_rtr(rtr?1:0), is_extended(extended?1:0) {}
46 };
47 
48 
49 struct MsgHeader : public Header{
50  MsgHeader(unsigned int i=0, bool rtr = false) : Header(i, false, rtr, false) {}
51 };
52 struct ExtendedHeader : public Header{
53  ExtendedHeader(unsigned int i=0, bool rtr = false) : Header(i, true, rtr, false) {}
54 };
55 struct ErrorHeader : public Header{
56  ErrorHeader(unsigned int i=0) : Header(i, false, false, true) {}
57 };
58 
59 
60 
62 struct Frame: public Header{
63  using value_type = unsigned char;
64  std::array<value_type, 8> data;
65  unsigned char dlc;
66 
68  bool isValid() const{
69  return (dlc <= 8) && Header::isValid();
70  }
78  Frame() : Header(), dlc(0) {}
79  Frame(const Header &h, unsigned char l = 0) : Header(h), dlc(l) {}
80 
81  value_type * c_array() { return data.data(); }
82  const value_type * c_array() const { return data.data(); }
83 };
84 
86 class State{
87 public:
90  } driver_state;
91  boost::system::error_code error_code;
92  unsigned int internal_error;
93 
95  virtual bool isReady() const { return driver_state == ready; }
96  virtual ~State() {}
97 };
98 
100 template <typename T,typename U> class Listener{
101  const T callable_;
102 public:
103  using Type = U;
104  using Callable = T;
105  using ListenerConstSharedPtr = std::shared_ptr<const Listener>;
106 
107  Listener(const T &callable):callable_(callable){ }
108  void operator()(const U & u) const { if(callable_) callable_(u); }
109  virtual ~Listener() {}
110 };
111 
113 public:
114  using StateFunc = std::function<void(const State&)>;
115  using StateDelegate [[deprecated("use StateFunc instead")]] = DelegateHelper<StateFunc>;
118 
125  virtual StateListenerConstSharedPtr createStateListener(const StateFunc &delegate) = 0;
126  template <typename Instance, typename Callable> inline StateListenerConstSharedPtr createStateListenerM(Instance inst, Callable callable) {
127  return this->createStateListener(std::bind(callable, inst, std::placeholders::_1));
128  }
129 
130  virtual ~StateInterface() {}
131 };
132 using StateInterfaceSharedPtr = std::shared_ptr<StateInterface>;
134 
136 public:
137  using FrameFunc = std::function<void(const Frame&)>;
138  using FrameDelegate [[deprecated("use FrameFunc instead")]] = DelegateHelper<FrameFunc>;
141 
148  virtual bool send(const Frame & msg) = 0;
149 
156  virtual FrameListenerConstSharedPtr createMsgListener(const FrameFunc &delegate) = 0;
157  template <typename Instance, typename Callable> inline FrameListenerConstSharedPtr createMsgListenerM(Instance inst, Callable callable) {
158  return this->createMsgListener(std::bind(callable, inst, std::placeholders::_1));
159  }
160 
168  virtual FrameListenerConstSharedPtr createMsgListener(const Frame::Header&, const FrameFunc &delegate) = 0;
169  template <typename Instance, typename Callable> inline FrameListenerConstSharedPtr createMsgListenerM(const Frame::Header& header, Instance inst, Callable callable) {
170  return this->createMsgListener(header, std::bind(callable, inst, std::placeholders::_1));
171  }
172 
173  virtual ~CommInterface() {}
174 };
175 using CommInterfaceSharedPtr = std::shared_ptr<CommInterface>;
177 
179 public:
180  [[deprecated("provide settings explicitly")]] virtual bool init(const std::string &device, bool loopback) = 0;
181 
190  virtual bool init(const std::string &device, bool loopback, SettingsConstSharedPtr settings) {
191  ROSCANOPEN_ERROR("socketcan_interface", "Driver does not support custom settings");
192  #pragma GCC diagnostic push
193  #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
194  return init(device, loopback);
195  #pragma GCC diagnostic pop
196  }
197 
203  virtual bool recover() = 0;
204 
208  virtual State getState() = 0;
209 
215  virtual void shutdown() = 0;
216 
217  virtual bool translateError(unsigned int internal_error, std::string & str) = 0;
218 
219  virtual bool doesLoopBack() const = 0;
220 
221  virtual void run() = 0;
222 
223  virtual ~DriverInterface() {}
224 };
225 using DriverInterfaceSharedPtr = std::shared_ptr<DriverInterface>;
226 
227 
228 } // namespace can
229 
230 #endif
can::Header::RTR_MASK
static const unsigned int RTR_MASK
Definition: interface.h:20
can::CommInterface::FrameListenerConstSharedPtr
FrameListener::ListenerConstSharedPtr FrameListenerConstSharedPtr
Definition: interface.h:140
can::Header::EXTENDED_MASK
static const unsigned int EXTENDED_MASK
Definition: interface.h:21
delegates.h
can::ErrorHeader::ErrorHeader
ErrorHeader(unsigned int i=0)
Definition: interface.h:56
can::CommInterface
Definition: interface.h:135
can::StateListenerConstSharedPtr
StateInterface::StateListenerConstSharedPtr StateListenerConstSharedPtr
Definition: interface.h:133
can::DriverInterface::init
virtual bool init(const std::string &device, bool loopback, SettingsConstSharedPtr settings)
Definition: interface.h:190
can::Frame
Definition: interface.h:62
can::StateInterface::StateListenerConstSharedPtr
StateListener::ListenerConstSharedPtr StateListenerConstSharedPtr
Definition: interface.h:117
can::StateInterface::createStateListenerM
StateListenerConstSharedPtr createStateListenerM(Instance inst, Callable callable)
Definition: interface.h:126
can::State::open
@ open
Definition: interface.h:89
settings.h
can::Header::fullid
unsigned int fullid() const
Definition: interface.h:31
can::Header::is_extended
unsigned int is_extended
frame uses 29 bit CAN identifier
Definition: interface.h:26
can::DriverInterface::recover
virtual bool recover()=0
can::Listener::Type
U Type
Definition: interface.h:103
can::DriverInterface::init
virtual bool init(const std::string &device, bool loopback)=0
can::DriverInterface::getState
virtual State getState()=0
can::StateInterface::~StateInterface
virtual ~StateInterface()
Definition: interface.h:130
can::CommInterface::FrameFunc
std::function< void(const Frame &)> FrameFunc
Definition: interface.h:137
can::Header::Header
Header(unsigned int i, bool extended, bool rtr, bool error)
Definition: interface.h:44
can::DriverInterface::~DriverInterface
virtual ~DriverInterface()
Definition: interface.h:223
can::DriverInterface::doesLoopBack
virtual bool doesLoopBack() const =0
can::Frame::dlc
unsigned char dlc
len of data
Definition: interface.h:65
can::Frame::c_array
const value_type * c_array() const
Definition: interface.h:82
can::MsgHeader
Definition: interface.h:49
can::State::closed
@ closed
Definition: interface.h:89
can::DriverInterfaceSharedPtr
std::shared_ptr< DriverInterface > DriverInterfaceSharedPtr
Definition: interface.h:225
can::StateInterfaceSharedPtr
std::shared_ptr< StateInterface > StateInterfaceSharedPtr
Definition: interface.h:132
can::CommInterfaceSharedPtr
std::shared_ptr< CommInterface > CommInterfaceSharedPtr
Definition: interface.h:175
can::DelegateHelper
Definition: delegates.h:9
can::Frame::Frame
Frame()
Definition: interface.h:78
can::Listener::ListenerConstSharedPtr
std::shared_ptr< const Listener > ListenerConstSharedPtr
Definition: interface.h:105
can::ExtendedHeader
Definition: interface.h:52
can::Listener::~Listener
virtual ~Listener()
Definition: interface.h:109
can::State::~State
virtual ~State()
Definition: interface.h:96
can::DriverInterface::shutdown
virtual void shutdown()=0
can::StateInterface::createStateListener
virtual StateListenerConstSharedPtr createStateListener(const StateFunc &delegate)=0
can::MsgHeader::MsgHeader
MsgHeader(unsigned int i=0, bool rtr=false)
Definition: interface.h:50
can::Header::is_rtr
unsigned int is_rtr
frame is a remote transfer request
Definition: interface.h:25
ROSCANOPEN_ERROR
#define ROSCANOPEN_ERROR(name, args)
Definition: logging.h:9
can::Frame::value_type
unsigned char value_type
Definition: interface.h:63
can::Header::id
unsigned int id
CAN ID (11 or 29 bits valid, depending on is_extended member.
Definition: interface.h:23
can::State::error_code
boost::system::error_code error_code
device access error
Definition: interface.h:91
can::CommInterface::createMsgListenerM
FrameListenerConstSharedPtr createMsgListenerM(const Frame::Header &header, Instance inst, Callable callable)
Definition: interface.h:169
can::Listener::Listener
Listener(const T &callable)
Definition: interface.h:107
can::Frame::data
std::array< value_type, 8 > data
array for 8 data bytes with bounds checking
Definition: interface.h:64
can::StateInterface
Definition: interface.h:112
can::Header::key
unsigned int key() const
Definition: interface.h:32
can::SettingsConstSharedPtr
std::shared_ptr< const Settings > SettingsConstSharedPtr
Definition: settings.h:31
can::Listener::callable_
const T callable_
Definition: interface.h:101
can::Listener::operator()
void operator()(const U &u) const
Definition: interface.h:108
can::DriverInterface
Definition: interface.h:178
can::Frame::c_array
value_type * c_array()
Definition: interface.h:81
can::Listener
Definition: interface.h:100
can::State::isReady
virtual bool isReady() const
Definition: interface.h:95
can::DriverInterface::run
virtual void run()=0
can
Definition: asio_base.h:11
can::State::State
State()
Definition: interface.h:94
can::DriverInterface::translateError
virtual bool translateError(unsigned int internal_error, std::string &str)=0
can::Header
Definition: interface.h:17
can::Header::ERROR_MASK
static const unsigned int ERROR_MASK
Definition: interface.h:19
can::ErrorHeader
Definition: interface.h:55
can::State::DriverState
DriverState
Definition: interface.h:88
can::StateInterface::StateFunc
std::function< void(const State &)> StateFunc
Definition: interface.h:114
can::Header::Header
Header()
Definition: interface.h:41
logging.h
can::ExtendedHeader::ExtendedHeader
ExtendedHeader(unsigned int i=0, bool rtr=false)
Definition: interface.h:53
can::Header::isValid
bool isValid() const
Definition: interface.h:28
can::FrameListenerConstSharedPtr
CommInterface::FrameListenerConstSharedPtr FrameListenerConstSharedPtr
Definition: interface.h:176
can::Header::is_error
unsigned int is_error
marks an error frame (only used internally)
Definition: interface.h:24
can::State::ready
@ ready
Definition: interface.h:89
can::State
Definition: interface.h:86
can::CommInterface::send
virtual bool send(const Frame &msg)=0
can::State::driver_state
enum can::State::DriverState driver_state
can::Header::ID_MASK
static const unsigned int ID_MASK
Definition: interface.h:18
can::CommInterface::~CommInterface
virtual ~CommInterface()
Definition: interface.h:173
can::Frame::Frame
Frame(const Header &h, unsigned char l=0)
Definition: interface.h:79
can::CommInterface::createMsgListenerM
FrameListenerConstSharedPtr createMsgListenerM(Instance inst, Callable callable)
Definition: interface.h:157
can::Frame::isValid
bool isValid() const
Definition: interface.h:68
can::State::internal_error
unsigned int internal_error
driver specific error
Definition: interface.h:92
can::CommInterface::createMsgListener
virtual FrameListenerConstSharedPtr createMsgListener(const FrameFunc &delegate)=0
can::Listener::Callable
T Callable
Definition: interface.h:104


socketcan_interface
Author(s): Mathias Lüdtke
autogenerated on Wed Mar 2 2022 00:52:25