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;
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
can::CommInterface::FrameListenerConstSharedPtr
FrameListener::ListenerConstSharedPtr FrameListenerConstSharedPtr
Definition: interface.h:140
can::Frame
Definition: interface.h:62
can::tostring
std::string tostring(const Header &h, bool lc)
Definition: string.cpp:78
can::StateInterface::StateListenerConstSharedPtr
StateListener::ListenerConstSharedPtr StateListenerConstSharedPtr
Definition: interface.h:117
can::DummyInterface
Definition: dummy.h:13
can::DummyInterface::add
bool add(const std::string &k, const Frame &v, bool multi)
Definition: dummy.h:33
string.h
dispatcher.h
can::CommInterface::FrameFunc
std::function< void(const Frame &)> FrameFunc
Definition: interface.h:137
can::DummyInterface::map_
Map map_
Definition: dummy.h:20
can::DummyInterfaceSharedPtr
std::shared_ptr< DummyInterface > DummyInterfaceSharedPtr
Definition: dummy.h:97
can::DummyInterface::shutdown
virtual void shutdown()
Definition: dummy.h:70
can::DummyInterface::createMsgListener
virtual FrameListenerConstSharedPtr createMsgListener(const FrameFunc &delegate)
Definition: dummy.h:58
can::FilteredDispatcher::createListener
BaseClass::ListenerConstSharedPtr createListener(const K &key, const typename BaseClass::Callable &callable)
Definition: dispatcher.h:85
can::DummyInterface::add
bool add(const Frame &k, const std::string &v, bool multi)
Definition: dummy.h:42
can::DummyInterface::run
virtual void run()
Definition: dummy.h:82
can::DummyInterface::getState
virtual State getState()
Definition: dummy.h:68
can::DummyInterface::send
virtual bool send(const Frame &msg)
Definition: dummy.h:45
can::DummyInterface::DummyInterface
DummyInterface(bool loopback)
Definition: dummy.h:31
can::DummyInterface::createStateListener
virtual StateListenerConstSharedPtr createStateListener(const StateFunc &delegate)
Definition: dummy.h:92
can::DummyInterface::loopback_
bool loopback_
Definition: dummy.h:21
can::SimpleDispatcher< StateInterface::StateListener >
interface.h
can::DummyInterface::add_noconv
bool add_noconv(const std::string &k, const Frame &v, bool multi)
Definition: dummy.h:23
can::Header::key
unsigned int key() const
Definition: interface.h:32
can::DummyInterface::state_dispatcher_
StateDispatcher state_dispatcher_
Definition: dummy.h:18
can::DummyInterface::add
bool add(const Frame &k, const Frame &v, bool multi)
Definition: dummy.h:36
can::SimpleDispatcher::dispatch
void dispatch(const Type &obj)
Definition: dispatcher.h:70
can::DriverInterface
Definition: interface.h:178
can
Definition: asio_base.h:11
can::DummyInterface::createMsgListener
virtual FrameListenerConstSharedPtr createMsgListener(const Frame::Header &h, const FrameFunc &delegate)
Definition: dummy.h:61
can::DummyInterface::state_
State state_
Definition: dummy.h:19
can::toframe
Frame toframe(const std::string &s)
Definition: string.cpp:119
can::StateInterface::StateFunc
std::function< void(const State &)> StateFunc
Definition: interface.h:114
can::Header::Header
Header()
Definition: interface.h:41
can::DummyInterface::Map
std::unordered_map< std::string, Frame > Map
Definition: dummy.h:16
can::SimpleDispatcher::createListener
ListenerConstSharedPtr createListener(const Callable &callable)
Definition: dispatcher.h:66
can::DummyInterface::recover
virtual bool recover()
Definition: dummy.h:66
can::State::ready
@ ready
Definition: interface.h:89
can::State
Definition: interface.h:86
can::DummyInterface::add
bool add(const std::string &k, const std::string &v, bool multi)
Definition: dummy.h:39
can::FilteredDispatcher< unsigned int, CommInterface::FrameListener >
can::DummyInterface::doesLoopBack
virtual bool doesLoopBack() const
Definition: dummy.h:80
can::State::driver_state
enum can::State::DriverState driver_state
can::DummyInterface::frame_dispatcher_
FrameDispatcher frame_dispatcher_
Definition: dummy.h:17
can::FilteredDispatcher::dispatch
void dispatch(const K &key, const typename BaseClass::Type &obj)
Definition: dispatcher.h:98
can::State::internal_error
unsigned int internal_error
driver specific error
Definition: interface.h:92
can::DummyInterface::translateError
virtual bool translateError(unsigned int internal_error, std::string &str)
Definition: dummy.h:72
can::DummyInterface::init
bool init(const std::string &device, bool loopback)
Definition: dummy.h:84


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