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*/
can::CommInterface::FrameListenerConstSharedPtr
FrameListener::ListenerConstSharedPtr FrameListenerConstSharedPtr
Definition: interface.h:140
can::CommInterface
Definition: interface.h:135
can::Frame
Definition: interface.h:62
can::FilteredFrameListener
Definition: filter.h:49
can::FrameRangeFilter::invert_
const bool invert_
Definition: filter.h:46
can::FrameMaskFilter::MASK_RELAXED
static const uint32_t MASK_RELAXED
Definition: filter.h:20
can::FrameRangeFilter::max_id_
const uint32_t max_id_
Definition: filter.h:45
can::FrameMaskFilter::MASK_ALL
static const uint32_t MASK_ALL
Definition: filter.h:19
can::FrameMaskFilter
Definition: filter.h:17
can::FrameFilter
Definition: filter.h:10
can::FrameRangeFilter::pass
virtual bool pass(const can::Frame &frame) const
Definition: filter.h:39
can::FilteredFrameListener::listener_
CommInterface::FrameListenerConstSharedPtr listener_
Definition: filter.h:65
can::CommInterfaceSharedPtr
std::shared_ptr< CommInterface > CommInterfaceSharedPtr
Definition: interface.h:175
can::FrameRangeFilter::min_id_
const uint32_t min_id_
Definition: filter.h:44
can::FilteredFrameListener::FilterVector
std::vector< FrameFilterSharedPtr > FilterVector
Definition: filter.h:51
can::FrameMaskFilter::mask_
const uint32_t mask_
Definition: filter.h:29
can::FilteredFrameListener::FilteredFrameListener
FilteredFrameListener(CommInterfaceSharedPtr comm, const Callable &callable, const FilterVector &filters)
Definition: filter.h:52
can::FrameFilter::~FrameFilter
virtual ~FrameFilter()
Definition: filter.h:13
interface.h
can::FrameFilter::pass
virtual bool pass(const can::Frame &frame) const =0
can::FrameMaskFilter::FrameMaskFilter
FrameMaskFilter(uint32_t can_id, uint32_t mask=MASK_RELAXED, bool invert=false)
Definition: filter.h:21
can::FrameMaskFilter::pass
virtual bool pass(const can::Frame &frame) const
Definition: filter.h:24
can::Header::key
unsigned int key() const
Definition: interface.h:32
can::Listener
Definition: interface.h:100
can
Definition: asio_base.h:11
can::FrameFilterSharedPtr
std::shared_ptr< FrameFilter > FrameFilterSharedPtr
Definition: filter.h:15
can::FrameRangeFilter::FrameRangeFilter
FrameRangeFilter(uint32_t min_id, uint32_t max_id, bool invert=false)
Definition: filter.h:36
can::FrameMaskFilter::masked_id_
const uint32_t masked_id_
Definition: filter.h:30
can::FrameRangeFilter
Definition: filter.h:34
can::FrameMaskFilter::invert_
const bool invert_
Definition: filter.h:31
can::FilteredFrameListener::filters_
const std::vector< FrameFilterSharedPtr > filters_
Definition: filter.h:64
can::Listener::Callable
T Callable
Definition: interface.h:104


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