reader.h
Go to the documentation of this file.
1 #ifndef H_CAN_BUFFERED_READER
2 #define H_CAN_BUFFERED_READER
3 
5 #include <deque>
6 
7 #include <boost/thread/mutex.hpp>
8 #include <boost/thread/condition_variable.hpp>
9 #include <boost/chrono.hpp>
10 
11 namespace can{
12 
14  std::deque<can::Frame> buffer_;
15  boost::mutex mutex_;
16  boost::condition_variable cond_;
18  bool enabled_;
19  size_t max_len_;
20 
21  void trim(){
22  if(max_len_ > 0){
23  while(buffer_.size() > max_len_){
24  ROSCANOPEN_ERROR("socketcan_interface", "buffer overflow, discarded oldest message " /*<< tostring(buffer_.front())*/); // enable message printing
25  buffer_.pop_front();
26  }
27  }
28  }
29  void handleFrame(const can::Frame & msg){
30  boost::mutex::scoped_lock lock(mutex_);
31  if(enabled_){
32  buffer_.push_back(msg);
33  trim();
34  cond_.notify_one();
35  }else{
36  ROSCANOPEN_WARN("socketcan_interface", "discarded message " /*<< tostring(msg)*/); // enable message printing
37  }
38  }
39 public:
42  bool before_;
43  public:
46  };
47 
48  BufferedReader() : enabled_(true), max_len_(0) {}
49  BufferedReader(bool enable, size_t max_len = 0) : enabled_(enable), max_len_(max_len) {}
50 
51  void flush(){
52  boost::mutex::scoped_lock lock(mutex_);
53  buffer_.clear();
54  }
55  void setMaxLen(size_t max_len){
56  boost::mutex::scoped_lock lock(mutex_);
57  max_len_ = max_len;
58  trim();
59  }
60  bool isEnabled(){
61  boost::mutex::scoped_lock lock(mutex_);
62  return enabled_;
63  }
64  bool setEnabled(bool enabled){
65  boost::mutex::scoped_lock lock(mutex_);
66  bool before = enabled_;
67  enabled_ = enabled;
68  return before;
69  }
70  void enable(){
71  boost::mutex::scoped_lock lock(mutex_);
72  enabled_ = true;
73  }
74 
75  void disable(){
76  boost::mutex::scoped_lock lock(mutex_);
77  enabled_ = false;
78  }
79 
80  void listen(CommInterfaceSharedPtr interface){
81  boost::mutex::scoped_lock lock(mutex_);
82  listener_ = interface->createMsgListenerM(this, &BufferedReader::handleFrame);
83  buffer_.clear();
84  }
85  void listen(CommInterfaceSharedPtr interface, const Frame::Header& h){
86  boost::mutex::scoped_lock lock(mutex_);
87  listener_ = interface->createMsgListenerM(h, this, &BufferedReader::handleFrame);
88  buffer_.clear();
89  }
90 
91  template<typename DurationType> bool read(can::Frame * msg, const DurationType &duration){
92  return readUntil(msg, boost::chrono::high_resolution_clock::now() + duration);
93  }
94  bool readUntil(can::Frame * msg, boost::chrono::high_resolution_clock::time_point abs_time){
95  boost::mutex::scoped_lock lock(mutex_);
96 
97  while(buffer_.empty() && cond_.wait_until(lock,abs_time) != boost::cv_status::timeout)
98  {}
99 
100  if(buffer_.empty()){
101  return false;
102  }
103 
104  if(msg){
105  *msg = buffer_.front();
106  buffer_.pop_front();
107  }
108  return true;
109  }
110 
111 };
112 
113 } // namespace can
114 #endif
can::CommInterface::FrameListenerConstSharedPtr
FrameListener::ListenerConstSharedPtr FrameListenerConstSharedPtr
Definition: interface.h:140
can::BufferedReader::BufferedReader
BufferedReader(bool enable, size_t max_len=0)
Definition: reader.h:49
can::BufferedReader::handleFrame
void handleFrame(const can::Frame &msg)
Definition: reader.h:29
can::BufferedReader::ScopedEnabler::before_
bool before_
Definition: reader.h:42
can::Frame
Definition: interface.h:62
can::BufferedReader::listen
void listen(CommInterfaceSharedPtr interface)
Definition: reader.h:80
can::BufferedReader::setMaxLen
void setMaxLen(size_t max_len)
Definition: reader.h:55
can::BufferedReader::listener_
CommInterface::FrameListenerConstSharedPtr listener_
Definition: reader.h:17
can::BufferedReader::flush
void flush()
Definition: reader.h:51
can::BufferedReader::read
bool read(can::Frame *msg, const DurationType &duration)
Definition: reader.h:91
can::BufferedReader::disable
void disable()
Definition: reader.h:75
can::CommInterfaceSharedPtr
std::shared_ptr< CommInterface > CommInterfaceSharedPtr
Definition: interface.h:175
can::BufferedReader::buffer_
std::deque< can::Frame > buffer_
Definition: reader.h:14
can::BufferedReader::enabled_
bool enabled_
Definition: reader.h:18
can::BufferedReader::readUntil
bool readUntil(can::Frame *msg, boost::chrono::high_resolution_clock::time_point abs_time)
Definition: reader.h:94
can::BufferedReader::BufferedReader
BufferedReader()
Definition: reader.h:48
can::BufferedReader::cond_
boost::condition_variable cond_
Definition: reader.h:16
can::BufferedReader::mutex_
boost::mutex mutex_
Definition: reader.h:15
interface.h
ROSCANOPEN_ERROR
#define ROSCANOPEN_ERROR(name, args)
Definition: logging.h:9
can::BufferedReader::max_len_
size_t max_len_
Definition: reader.h:19
can
Definition: asio_base.h:11
can::BufferedReader::listen
void listen(CommInterfaceSharedPtr interface, const Frame::Header &h)
Definition: reader.h:85
can::BufferedReader::ScopedEnabler::~ScopedEnabler
~ScopedEnabler()
Definition: reader.h:45
can::BufferedReader
Definition: reader.h:13
can::BufferedReader::isEnabled
bool isEnabled()
Definition: reader.h:60
can::BufferedReader::ScopedEnabler::reader_
BufferedReader & reader_
Definition: reader.h:41
can::BufferedReader::enable
void enable()
Definition: reader.h:70
can::BufferedReader::ScopedEnabler::ScopedEnabler
ScopedEnabler(BufferedReader &reader)
Definition: reader.h:44
can::Header::Header
Header()
Definition: interface.h:41
can::BufferedReader::ScopedEnabler
Definition: reader.h:40
can::BufferedReader::setEnabled
bool setEnabled(bool enabled)
Definition: reader.h:64
can::BufferedReader::trim
void trim()
Definition: reader.h:21
ROSCANOPEN_WARN
#define ROSCANOPEN_WARN(name, args)
Definition: logging.h:11


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