APM:Libraries
PollerThread.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Intel Corporation. All rights reserved.
3  *
4  * This file is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This file is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #pragma once
18 
19 #include <inttypes.h>
20 #include <vector>
21 
22 #include <AP_HAL/Device.h>
23 
24 #include "Poller.h"
25 #include "Thread.h"
26 
27 namespace Linux {
28 
29 class TimerPollable : public Pollable {
30  friend class PollerThread;
31 
32 public:
33  class WrapperCb {
34  public:
35  virtual ~WrapperCb() { }
36 
37  virtual void start_cb() { }
38  virtual void end_cb() { }
39  };
40 
41  using PeriodicCb = AP_HAL::Device::PeriodicCb;
42 
43  virtual ~TimerPollable() { }
44 
45  void on_can_read() override;
46 
47  bool setup_timer(uint32_t timeout_usec);
48  bool adjust_timer(uint32_t timeout_usec);
49 
50 protected:
52  : _cb(cb)
53  , _wrapper(wrapper)
54  {
55  }
56 
59  bool _removeme = false;
60 };
61 
62 
63 class PollerThread : public Thread {
64 public:
66  virtual ~PollerThread() { }
67 
69  TimerPollable::WrapperCb *wrapper,
70  uint32_t timeout_usec);
71  bool adjust_timer(TimerPollable *p, uint32_t timeout_usec);
72 
73  void mainloop();
74 
75  bool stop() override;
76 
77 protected:
78  void _cleanup_timers();
79 
80  Poller _poller{};
81  std::vector<TimerPollable*> _timers{};
82 };
83 
84 }
bool adjust_timer(uint32_t timeout_usec)
AP_HAL::Device::PeriodicCb PeriodicCb
Definition: PollerThread.h:41
WrapperCb * _wrapper
Definition: PollerThread.h:58
void on_can_read() override
bool setup_timer(uint32_t timeout_usec)
virtual ~TimerPollable()
Definition: PollerThread.h:43
TimerPollable(PeriodicCb cb, WrapperCb *wrapper)
Definition: PollerThread.h:51
#define FUNCTOR_BIND_MEMBER(func, rettype,...)
Definition: functor.h:31
virtual ~PollerThread()
Definition: PollerThread.h:66