interface.h
Go to the documentation of this file.
1 #ifndef H_CAN_INTERFACE
2 #define H_CAN_INTERFACE
3 
4 #include <boost/array.hpp>
5 #include <boost/system/error_code.hpp>
6 #include <boost/shared_ptr.hpp>
7 
8 #include "FastDelegate.h"
9 
10 namespace can{
11 
13 struct Header{
14  static const unsigned int ID_MASK = (1u << 29)-1;
15  static const unsigned int ERROR_MASK = (1u << 29);
16  static const unsigned int RTR_MASK = (1u << 30);
17  static const unsigned int EXTENDED_MASK = (1u << 31);
18 
19  unsigned int id:29;
20  unsigned int is_error:1;
21  unsigned int is_rtr:1;
22  unsigned int is_extended:1;
23 
24  bool isValid() const{
25  return id < (is_extended?(1<<29):(1<<11));
26  }
33  operator const unsigned int() const { return is_error ? (ERROR_MASK) : *(unsigned int*) this; }
34 
36  : id(0),is_error(0),is_rtr(0), is_extended(0) {}
37 
38  Header(unsigned int i, bool extended, bool rtr, bool error)
39  : id(i),is_error(error?1:0),is_rtr(rtr?1:0), is_extended(extended?1:0) {}
40 };
41 
42 
43 struct MsgHeader : public Header{
44  MsgHeader(unsigned int i=0, bool rtr = false) : Header(i, false, rtr, false) {}
45 };
46 struct ExtendedHeader : public Header{
47  ExtendedHeader(unsigned int i=0, bool rtr = false) : Header(i, true, rtr, false) {}
48 };
49 struct ErrorHeader : public Header{
50  ErrorHeader(unsigned int i=0) : Header(i, false, false, true) {}
51 };
52 
53 
54 
56 struct Frame: public Header{
57  typedef unsigned char value_type;
58  boost::array<value_type, 8> data;
59  unsigned char dlc;
60 
62  bool isValid() const{
63  return (dlc <= 8) && Header::isValid();
64  }
72  Frame() : Header(), dlc(0) {}
73  Frame(const Header &h, unsigned char l = 0) : Header(h), dlc(l) {}
74 
75  value_type * c_array() { return data.c_array(); }
76  const value_type * c_array() const { return data.data(); }
77 };
78 
80 class State{
81 public:
83  closed, open, ready
84  } driver_state;
85  boost::system::error_code error_code;
86  unsigned int internal_error;
87 
88  State() : driver_state(closed), internal_error(0) {}
89  virtual bool isReady() const { return driver_state == ready; }
90  virtual ~State() {}
91 };
92 
94 template <typename T,typename U> class Listener{
95  const T callable_;
96 public:
97  typedef U Type;
98  typedef T Callable;
99  typedef boost::shared_ptr<const Listener> ListenerConstSharedPtr;
100  typedef ListenerConstSharedPtr Ptr __attribute__((deprecated));
101 
102  Listener(const T &callable):callable_(callable){ }
103  void operator()(const U & u) const { if(callable_) callable_(u); }
104  virtual ~Listener() {}
105 };
106 
108 public:
112 
119  virtual StateListenerConstSharedPtr createStateListener(const StateDelegate &delegate) = 0;
120 
121  virtual ~StateInterface() {}
122 };
123 typedef boost::shared_ptr<StateInterface> StateInterfaceSharedPtr;
125 
127 public:
131 
138  virtual bool send(const Frame & msg) = 0;
139 
146  virtual FrameListenerConstSharedPtr createMsgListener(const FrameDelegate &delegate) = 0;
147 
155  virtual FrameListenerConstSharedPtr createMsgListener(const Frame::Header&, const FrameDelegate &delegate) = 0;
156 
157  virtual ~CommInterface() {}
158 };
159 typedef boost::shared_ptr<CommInterface> CommInterfaceSharedPtr;
161 
162 
164 public:
172  virtual bool init(const std::string &device, bool loopback) = 0;
173 
179  virtual bool recover() = 0;
180 
184  virtual State getState() = 0;
185 
191  virtual void shutdown() = 0;
192 
193  virtual bool translateError(unsigned int internal_error, std::string & str) = 0;
194 
195  virtual bool doesLoopBack() const = 0;
196 
197  virtual void run() = 0;
198 
199  virtual ~DriverInterface() {}
200 };
201 typedef boost::shared_ptr<DriverInterface> DriverInterfaceSharedPtr;
202 
203 
204 } // namespace can
205 
206 #include <iostream>
207 #include <boost/thread/mutex.hpp>
208 
210  static boost::mutex& get_cout_mutex(){
211  static boost::mutex mutex;
212  return mutex;
213  }
214 };
215 
216 #define LOG(log) { boost::mutex::scoped_lock _cout_lock(_cout_wrapper::get_cout_mutex()); std::cout << log << std::endl; }
217 
218 
219 #endif
static boost::mutex & get_cout_mutex()
Definition: interface.h:210
unsigned int is_error
marks an error frame (only used internally)
Definition: interface.h:20
virtual ~Listener()
Definition: interface.h:104
Definition: asio_base.h:11
static const unsigned int RTR_MASK
Definition: interface.h:16
fastdelegate::FastDelegate1< const State & > StateDelegate
Definition: interface.h:109
Frame(const Header &h, unsigned char l=0)
Definition: interface.h:73
const value_type * c_array() const
Definition: interface.h:76
const T callable_
Definition: interface.h:95
static const unsigned int ID_MASK
Definition: interface.h:14
static const unsigned int EXTENDED_MASK
Definition: interface.h:17
bool isValid() const
Definition: interface.h:24
boost::array< value_type, 8 > data
array for 8 data bytes with bounds checking
Definition: interface.h:58
Listener< const StateDelegate, const State & > StateListener
Definition: interface.h:110
boost::shared_ptr< CommInterface > CommInterfaceSharedPtr
Definition: interface.h:159
ErrorHeader(unsigned int i=0)
Definition: interface.h:50
unsigned int is_extended
frame uses 29 bit CAN identifier
Definition: interface.h:22
Listener(const T &callable)
Definition: interface.h:102
static const unsigned int ERROR_MASK
Definition: interface.h:15
void operator()(const U &u) const
Definition: interface.h:103
boost::shared_ptr< DriverInterface > DriverInterfaceSharedPtr
Definition: interface.h:201
MsgHeader(unsigned int i=0, bool rtr=false)
Definition: interface.h:44
fastdelegate::FastDelegate1< const Frame & > FrameDelegate
Definition: interface.h:128
virtual ~StateInterface()
Definition: interface.h:121
StateListener::ListenerConstSharedPtr StateListenerConstSharedPtr
Definition: interface.h:111
CommInterface::FrameListenerConstSharedPtr FrameListenerConstSharedPtr
Definition: interface.h:160
value_type * c_array()
Definition: interface.h:75
virtual ~CommInterface()
Definition: interface.h:157
ListenerConstSharedPtr Ptr __attribute__((deprecated))
Definition: interface.h:100
virtual bool isReady() const
Definition: interface.h:89
ExtendedHeader(unsigned int i=0, bool rtr=false)
Definition: interface.h:47
bool isValid() const
Definition: interface.h:62
virtual ~DriverInterface()
Definition: interface.h:199
Header(unsigned int i, bool extended, bool rtr, bool error)
Definition: interface.h:38
boost::shared_ptr< const Listener > ListenerConstSharedPtr
Definition: interface.h:99
unsigned char value_type
Definition: interface.h:57
unsigned int internal_error
driver specific error
Definition: interface.h:86
Listener< const FrameDelegate, const Frame & > FrameListener
Definition: interface.h:129
StateInterface::StateListenerConstSharedPtr StateListenerConstSharedPtr
Definition: interface.h:124
unsigned char dlc
len of data
Definition: interface.h:59
FrameListener::ListenerConstSharedPtr FrameListenerConstSharedPtr
Definition: interface.h:130
unsigned int id
CAN ID (11 or 29 bits valid, depending on is_extended member.
Definition: interface.h:19
boost::shared_ptr< StateInterface > StateInterfaceSharedPtr
Definition: interface.h:123
void run(ClassLoader *loader)
unsigned int is_rtr
frame is a remote transfer request
Definition: interface.h:21
virtual ~State()
Definition: interface.h:90
boost::system::error_code error_code
device access error
Definition: interface.h:85


socketcan_interface
Author(s): Mathias Lüdtke
autogenerated on Sat May 4 2019 02:40:41