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:
89  closed, open, ready
90  } driver_state;
91  boost::system::error_code error_code;
92  unsigned int internal_error;
93 
94  State() : driver_state(closed), internal_error(0) {}
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
StateInterface::StateListenerConstSharedPtr StateListenerConstSharedPtr
Definition: interface.h:133
std::array< value_type, 8 > data
array for 8 data bytes with bounds checking
Definition: interface.h:64
bool isValid() const
Definition: interface.h:68
StateListener::ListenerConstSharedPtr StateListenerConstSharedPtr
Definition: interface.h:117
std::shared_ptr< DriverInterface > DriverInterfaceSharedPtr
Definition: interface.h:225
std::shared_ptr< const Listener > ListenerConstSharedPtr
Definition: interface.h:105
unsigned char value_type
Definition: interface.h:63
void run(class_loader::ClassLoader *loader)
unsigned int is_error
marks an error frame (only used internally)
Definition: interface.h:24
virtual ~Listener()
Definition: interface.h:109
Definition: asio_base.h:11
#define ROSCANOPEN_ERROR(name, args)
Definition: logging.h:9
std::function< void(const Frame &)> FrameFunc
Definition: interface.h:137
static const unsigned int RTR_MASK
Definition: interface.h:20
Frame(const Header &h, unsigned char l=0)
Definition: interface.h:79
unsigned int fullid() const
Definition: interface.h:31
const T callable_
Definition: interface.h:101
static const unsigned int ID_MASK
Definition: interface.h:18
StateListenerConstSharedPtr createStateListenerM(Instance inst, Callable callable)
Definition: interface.h:126
static const unsigned int EXTENDED_MASK
Definition: interface.h:21
bool isValid() const
Definition: interface.h:28
FrameListenerConstSharedPtr createMsgListenerM(const Frame::Header &header, Instance inst, Callable callable)
Definition: interface.h:169
ErrorHeader(unsigned int i=0)
Definition: interface.h:56
FrameListener::ListenerConstSharedPtr FrameListenerConstSharedPtr
Definition: interface.h:140
std::shared_ptr< CommInterface > CommInterfaceSharedPtr
Definition: interface.h:175
unsigned int is_extended
frame uses 29 bit CAN identifier
Definition: interface.h:26
Listener(const T &callable)
Definition: interface.h:107
static const unsigned int ERROR_MASK
Definition: interface.h:19
std::function< void(const State &)> StateFunc
Definition: interface.h:114
MsgHeader(unsigned int i=0, bool rtr=false)
Definition: interface.h:50
virtual bool isReady() const
Definition: interface.h:95
virtual ~StateInterface()
Definition: interface.h:130
CommInterface::FrameListenerConstSharedPtr FrameListenerConstSharedPtr
Definition: interface.h:176
value_type * c_array()
Definition: interface.h:81
virtual bool init(const std::string &device, bool loopback, SettingsConstSharedPtr settings)
Definition: interface.h:190
unsigned int key() const
Definition: interface.h:32
virtual ~CommInterface()
Definition: interface.h:173
std::shared_ptr< const Settings > SettingsConstSharedPtr
Definition: settings.h:31
void operator()(const U &u) const
Definition: interface.h:108
ExtendedHeader(unsigned int i=0, bool rtr=false)
Definition: interface.h:53
virtual ~DriverInterface()
Definition: interface.h:223
Header(unsigned int i, bool extended, bool rtr, bool error)
Definition: interface.h:44
const value_type * c_array() const
Definition: interface.h:82
unsigned int internal_error
driver specific error
Definition: interface.h:92
unsigned char dlc
len of data
Definition: interface.h:65
std::shared_ptr< StateInterface > StateInterfaceSharedPtr
Definition: interface.h:132
unsigned int id
CAN ID (11 or 29 bits valid, depending on is_extended member.
Definition: interface.h:23
FrameListenerConstSharedPtr createMsgListenerM(Instance inst, Callable callable)
Definition: interface.h:157
unsigned int is_rtr
frame is a remote transfer request
Definition: interface.h:25
virtual ~State()
Definition: interface.h:96
boost::system::error_code error_code
device access error
Definition: interface.h:91


socketcan_interface
Author(s): Mathias L├╝dtke
autogenerated on Tue May 4 2021 02:50:18