dummy.h
Go to the documentation of this file.
00001 #ifndef SOCKETCAN_INTERFACE_DUMMY_H
00002 #define SOCKETCAN_INTERFACE_DUMMY_H
00003 
00004 #include "interface.h"
00005 #include "dispatcher.h"
00006 #include "string.h"
00007 #include <boost/algorithm/string.hpp>
00008 
00009 namespace can{
00010 
00011 class DummyInterface : public DriverInterface{
00012     typedef FilteredDispatcher<const unsigned int, CommInterface::FrameListener> FrameDispatcher;
00013     typedef SimpleDispatcher<StateInterface::StateListener> StateDispatcher;
00014     typedef boost::unordered_multimap<std::string, Frame> Map;
00015     FrameDispatcher frame_dispatcher_;
00016     StateDispatcher state_dispatcher_;
00017     State state_;
00018     Map map_;
00019     bool loopback_;
00020 
00021     bool add_noconv(const std::string &k, const Frame &v, bool multi){
00022         if(multi || map_.find(k) == map_.end()){
00023               map_.insert( std::make_pair(boost::to_lower_copy(k), v));
00024               return true;
00025         }
00026         return false;
00027     }
00028 public:
00029     DummyInterface(bool loopback) : loopback_(loopback) {}
00030 
00031     bool add(const std::string &k, const Frame &v, bool multi){
00032         return add_noconv(boost::to_lower_copy(k), v, multi);
00033     }
00034     bool add(const Frame &k, const Frame &v, bool multi){
00035         return add_noconv(tostring(k,true), v, multi);
00036     }
00037     bool add(const std::string &k, const std::string &v, bool multi){
00038         return add(k, toframe(v), multi);
00039     }
00040     bool add(const Frame &k, const std::string &v, bool multi){
00041         return add(k, toframe(v), multi);
00042     }
00043     virtual bool send(const Frame & msg){
00044         if(loopback_) frame_dispatcher_.dispatch(msg);
00045         try{
00046             std::pair <Map::iterator, Map::iterator> r = map_.equal_range(tostring(msg, true));
00047             for (Map::iterator it=r.first; it!=r.second; ++it){
00048                 frame_dispatcher_.dispatch(it->second);
00049             }
00050         }
00051         catch(const std::out_of_range &e){
00052         }
00053         return true;
00054     }
00055 
00056     virtual FrameListener::Ptr createMsgListener(const FrameDelegate &delegate){
00057         return frame_dispatcher_.createListener(delegate);
00058     }
00059     virtual FrameListener::Ptr createMsgListener(const Frame::Header&h , const FrameDelegate &delegate){
00060         return frame_dispatcher_.createListener(h, delegate);
00061     }
00062 
00063     // methods from StateInterface
00064     virtual bool recover(){return false;};
00065 
00066     virtual State getState(){return state_;};
00067 
00068     virtual void shutdown(){};
00069 
00070     virtual bool translateError(unsigned int internal_error, std::string & str){
00071         if (!internal_error) {
00072             str = "OK";
00073             return true;
00074         }
00075         return false;
00076     };
00077 
00078     virtual bool doesLoopBack() const {return loopback_;};
00079 
00080     virtual void run(){};
00081 
00082     bool init(const std::string &device, bool loopback){
00083         loopback_ = loopback;
00084         state_.driver_state = State::ready;
00085         state_.internal_error = 0;
00086         state_dispatcher_.dispatch(state_);
00087         return true;
00088     };
00089 
00090     virtual StateListener::Ptr createStateListener(const StateDelegate &delegate){
00091       return state_dispatcher_.createListener(delegate);
00092     };
00093 
00094 };
00095 
00096 
00097 }
00098 
00099 #endif


socketcan_interface
Author(s): Mathias Lüdtke
autogenerated on Thu Jun 6 2019 20:43:53