Line data Source code
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 0 : virtual ~WrapperCb() { } 36 : 37 0 : virtual void start_cb() { } 38 0 : virtual void end_cb() { } 39 : }; 40 : 41 : using PeriodicCb = AP_HAL::Device::PeriodicCb; 42 : 43 0 : 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: 51 0 : TimerPollable(PeriodicCb cb, WrapperCb *wrapper) 52 0 : : _cb(cb) 53 0 : , _wrapper(wrapper) 54 : { 55 0 : } 56 : 57 : PeriodicCb _cb; 58 : WrapperCb *_wrapper; 59 : bool _removeme = false; 60 : }; 61 : 62 : 63 : class PollerThread : public Thread { 64 : public: 65 1 : PollerThread() : Thread{FUNCTOR_BIND_MEMBER(&PollerThread::mainloop, void)} { } 66 1 : virtual ~PollerThread() { } 67 : 68 : TimerPollable *add_timer(TimerPollable::PeriodicCb cb, 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 : }