filter.h
Go to the documentation of this file.
1 #ifndef SOCKETCAN_INTERFACE_FILTER_H
2 #define SOCKETCAN_INTERFACE_FILTER_H
3 
4 #include <vector>
5 
6 #include "interface.h"
7 
8 namespace can {
9 
10 class FrameFilter {
11 public:
12  virtual bool pass(const can::Frame &frame) const = 0;
13  virtual ~FrameFilter() {}
14 };
15 using FrameFilterSharedPtr = std::shared_ptr<FrameFilter>;
16 
17 class FrameMaskFilter : public FrameFilter {
18 public:
19  static const uint32_t MASK_ALL = 0xffffffff;
20  static const uint32_t MASK_RELAXED = ~Frame::EXTENDED_MASK;
21  FrameMaskFilter(uint32_t can_id, uint32_t mask = MASK_RELAXED, bool invert = false)
22  : mask_(mask), masked_id_(can_id & mask), invert_(invert)
23  {}
24  virtual bool pass(const can::Frame &frame) const{
25  const uint32_t k = frame.key();
26  return ((mask_ & k) == masked_id_) != invert_;
27  }
28 private:
29  const uint32_t mask_;
30  const uint32_t masked_id_;
31  const bool invert_;
32 };
33 
34 class FrameRangeFilter : public FrameFilter {
35 public:
36  FrameRangeFilter(uint32_t min_id, uint32_t max_id, bool invert = false)
37  : min_id_(min_id), max_id_(max_id), invert_(invert)
38  {}
39  virtual bool pass(const can::Frame &frame) const{
40  const uint32_t k = frame.key();
41  return (min_id_ <= k && k <= max_id_) != invert_;
42  }
43 private:
44  const uint32_t min_id_;
45  const uint32_t max_id_;
46  const bool invert_;
47 };
48 
50 public:
51  using FilterVector = std::vector<FrameFilterSharedPtr>;
52  FilteredFrameListener(CommInterfaceSharedPtr comm, const Callable &callable, const FilterVector &filters)
53  : CommInterface::FrameListener(callable),
54  filters_(filters),
55  listener_(comm->createMsgListener([this](const Frame &frame) {
56  for(FilterVector::const_iterator it=this->filters_.begin(); it != this->filters_.end(); ++it) {
57  if((*it)->pass(frame)){
58  (*this)(frame);
59  break;
60  }
61  }
62  }))
63  {}
64  const std::vector<FrameFilterSharedPtr> filters_;
66 };
67 
68 } // namespace can
69 
70 #endif /*SOCKETCAN_INTERFACE_FILTER_H*/
const uint32_t max_id_
Definition: filter.h:45
FilteredFrameListener(CommInterfaceSharedPtr comm, const Callable &callable, const FilterVector &filters)
Definition: filter.h:52
virtual bool pass(const can::Frame &frame) const
Definition: filter.h:39
virtual bool pass(const can::Frame &frame) const
Definition: filter.h:24
Definition: asio_base.h:11
const bool invert_
Definition: filter.h:46
const uint32_t min_id_
Definition: filter.h:44
const bool invert_
Definition: filter.h:31
FrameListener::ListenerConstSharedPtr FrameListenerConstSharedPtr
Definition: interface.h:140
std::shared_ptr< CommInterface > CommInterfaceSharedPtr
Definition: interface.h:175
const uint32_t masked_id_
Definition: filter.h:30
virtual ~FrameFilter()
Definition: filter.h:13
unsigned int key() const
Definition: interface.h:32
std::vector< FrameFilterSharedPtr > FilterVector
Definition: filter.h:51
const std::vector< FrameFilterSharedPtr > filters_
Definition: filter.h:64
virtual bool pass(const can::Frame &frame) const =0
FrameRangeFilter(uint32_t min_id, uint32_t max_id, bool invert=false)
Definition: filter.h:36
std::shared_ptr< FrameFilter > FrameFilterSharedPtr
Definition: filter.h:15
CommInterface::FrameListenerConstSharedPtr listener_
Definition: filter.h:65
const uint32_t mask_
Definition: filter.h:29
FrameMaskFilter(uint32_t can_id, uint32_t mask=MASK_RELAXED, bool invert=false)
Definition: filter.h:21


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