dummy.h
Go to the documentation of this file.
1 #ifndef SOCKETCAN_INTERFACE_DUMMY_H
2 #define SOCKETCAN_INTERFACE_DUMMY_H
3 
4 #include "interface.h"
5 #include "dispatcher.h"
6 #include "string.h"
7 #include <boost/algorithm/string.hpp>
8 
9 namespace can{
10 
14  typedef boost::unordered_multimap<std::string, Frame> Map;
15  FrameDispatcher frame_dispatcher_;
16  StateDispatcher state_dispatcher_;
18  Map map_;
19  bool loopback_;
20 
21  bool add_noconv(const std::string &k, const Frame &v, bool multi){
22  if(multi || map_.find(k) == map_.end()){
23  map_.insert( std::make_pair(boost::to_lower_copy(k), v));
24  return true;
25  }
26  return false;
27  }
28 public:
29  DummyInterface(bool loopback) : loopback_(loopback) {}
30 
31  bool add(const std::string &k, const Frame &v, bool multi){
32  return add_noconv(boost::to_lower_copy(k), v, multi);
33  }
34  bool add(const Frame &k, const Frame &v, bool multi){
35  return add_noconv(tostring(k,true), v, multi);
36  }
37  bool add(const std::string &k, const std::string &v, bool multi){
38  return add(k, toframe(v), multi);
39  }
40  bool add(const Frame &k, const std::string &v, bool multi){
41  return add(k, toframe(v), multi);
42  }
43  virtual bool send(const Frame & msg){
44  if(loopback_) frame_dispatcher_.dispatch(msg);
45  try{
46  std::pair <Map::iterator, Map::iterator> r = map_.equal_range(tostring(msg, true));
47  for (Map::iterator it=r.first; it!=r.second; ++it){
48  frame_dispatcher_.dispatch(it->second);
49  }
50  }
51  catch(const std::out_of_range &e){
52  }
53  return true;
54  }
55 
57  return frame_dispatcher_.createListener(delegate);
58  }
60  return frame_dispatcher_.createListener(h, delegate);
61  }
62 
63  // methods from StateInterface
64  virtual bool recover(){return false;};
65 
66  virtual State getState(){return state_;};
67 
68  virtual void shutdown(){};
69 
70  virtual bool translateError(unsigned int internal_error, std::string & str){
71  if (!internal_error) {
72  str = "OK";
73  return true;
74  }
75  return false;
76  };
77 
78  virtual bool doesLoopBack() const {return loopback_;};
79 
80  virtual void run(){};
81 
82  bool init(const std::string &device, bool loopback){
83  loopback_ = loopback;
84  state_.driver_state = State::ready;
85  state_.internal_error = 0;
86  state_dispatcher_.dispatch(state_);
87  return true;
88  };
89 
91  return state_dispatcher_.createListener(delegate);
92  };
93 
94 };
95 typedef boost::shared_ptr<DummyInterface> DummyInterfaceSharedPtr;
96 
97 
98 }
99 
100 #endif
virtual bool doesLoopBack() const
Definition: dummy.h:78
bool add_noconv(const std::string &k, const Frame &v, bool multi)
Definition: dummy.h:21
FilteredDispatcher< const unsigned int, CommInterface::FrameListener > FrameDispatcher
Definition: dummy.h:12
virtual FrameListenerConstSharedPtr createMsgListener(const FrameDelegate &delegate)
Definition: dummy.h:56
std::string tostring(const Header &h, bool lc)
Definition: string.cpp:77
virtual StateListenerConstSharedPtr createStateListener(const StateDelegate &delegate)
Definition: dummy.h:90
void dispatch(const typename BaseClass::Type &obj)
Definition: dispatcher.h:88
Definition: asio_base.h:11
DummyInterface(bool loopback)
Definition: dummy.h:29
StateDispatcher state_dispatcher_
Definition: dummy.h:16
Frame toframe(const std::string &s)
Definition: string.cpp:114
virtual void run()
Definition: dummy.h:80
virtual bool translateError(unsigned int internal_error, std::string &str)
Definition: dummy.h:70
StateListener::ListenerConstSharedPtr StateListenerConstSharedPtr
Definition: interface.h:111
virtual FrameListenerConstSharedPtr createMsgListener(const Frame::Header &h, const FrameDelegate &delegate)
Definition: dummy.h:59
void dispatch(const Type &obj)
Definition: dispatcher.h:67
bool add(const Frame &k, const std::string &v, bool multi)
Definition: dummy.h:40
bool add(const std::string &k, const Frame &v, bool multi)
Definition: dummy.h:31
enum can::State::DriverState driver_state
virtual State getState()
Definition: dummy.h:66
bool add(const Frame &k, const Frame &v, bool multi)
Definition: dummy.h:34
boost::shared_ptr< DummyInterface > DummyInterfaceSharedPtr
Definition: dummy.h:95
virtual bool recover()
Definition: dummy.h:64
bool add(const std::string &k, const std::string &v, bool multi)
Definition: dummy.h:37
virtual void shutdown()
Definition: dummy.h:68
SimpleDispatcher< StateInterface::StateListener > StateDispatcher
Definition: dummy.h:13
BaseClass::ListenerConstSharedPtr createListener(const K &key, const typename BaseClass::Callable &callable)
Definition: dispatcher.h:82
ListenerConstSharedPtr createListener(const Callable &callable)
Definition: dispatcher.h:63
unsigned int internal_error
driver specific error
Definition: interface.h:86
bool init(const std::string &device, bool loopback)
Definition: dummy.h:82
virtual bool send(const Frame &msg)
Definition: dummy.h:43
FrameListener::ListenerConstSharedPtr FrameListenerConstSharedPtr
Definition: interface.h:130
boost::unordered_multimap< std::string, Frame > Map
Definition: dummy.h:14
FrameDispatcher frame_dispatcher_
Definition: dummy.h:15


socketcan_interface
Author(s): Mathias Lüdtke
autogenerated on Fri May 14 2021 02:59:39