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


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