Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 3 | #include <chrono> |
Andrew Geissler | 8622f69 | 2017-04-02 18:19:00 -0500 | [diff] [blame] | 4 | #include <functional> |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 5 | #include <systemd/sd-event.h> |
| 6 | namespace phosphor |
| 7 | { |
| 8 | namespace ipmi |
| 9 | { |
| 10 | |
| 11 | /** @class Timer |
| 12 | * @brief Manages starting watchdog timers and handling timeouts |
| 13 | */ |
| 14 | class Timer |
| 15 | { |
| 16 | public: |
| 17 | /** @brief Only need the default Timer */ |
| 18 | Timer() = delete; |
| 19 | Timer(const Timer&) = delete; |
| 20 | Timer& operator=(const Timer&) = delete; |
| 21 | Timer(Timer&&) = delete; |
| 22 | Timer& operator=(Timer&&) = delete; |
| 23 | |
| 24 | /** @brief Constructs timer object |
| 25 | * |
| 26 | * @param[in] events - sd_event pointer |
Andrew Geissler | 8622f69 | 2017-04-02 18:19:00 -0500 | [diff] [blame] | 27 | * @param[in] funcCallBack - optional function callback for timer |
| 28 | * expirations |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 29 | */ |
Andrew Geissler | 8622f69 | 2017-04-02 18:19:00 -0500 | [diff] [blame] | 30 | Timer(sd_event* events, |
| 31 | std::function<void()> userCallBack = nullptr) |
| 32 | : timeEvent(events), userCallBack(userCallBack) |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 33 | { |
| 34 | // Initialize the timer |
| 35 | initialize(); |
| 36 | } |
| 37 | |
| 38 | ~Timer() |
| 39 | { |
| 40 | if (eventSource) |
| 41 | { |
| 42 | eventSource = sd_event_source_unref(eventSource); |
| 43 | } |
| 44 | } |
| 45 | |
Vishwanatha Subbanna | d27e71e | 2017-02-01 18:02:38 +0530 | [diff] [blame] | 46 | inline auto isExpired() const |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 47 | { |
| 48 | return expired; |
| 49 | } |
| 50 | |
Vishwanatha Subbanna | d27e71e | 2017-02-01 18:02:38 +0530 | [diff] [blame] | 51 | /** @brief Starts the timer with specified expiration value. |
| 52 | * input is an offset from the current steady_clock |
| 53 | */ |
| 54 | int startTimer(std::chrono::microseconds usec); |
| 55 | |
| 56 | /** @brief Enables / disables the timer */ |
| 57 | int setTimer(int action); |
| 58 | |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 59 | private: |
| 60 | /** @brief the sd_event structure */ |
| 61 | sd_event* timeEvent = nullptr; |
| 62 | |
| 63 | /** @brief Source of events */ |
| 64 | sd_event_source* eventSource = nullptr; |
| 65 | |
| 66 | /** @brief Returns if the associated timer is expired |
| 67 | * |
| 68 | * This is set to true when the timeoutHandler is called into |
| 69 | */ |
Ratan Gupta | 32cbd65 | 2018-03-07 16:03:53 +0530 | [diff] [blame] | 70 | bool expired = true; |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 71 | |
| 72 | /** @brief Initializes the timer object with infinite |
| 73 | * expiration time and sets up the callback handler |
| 74 | * |
| 75 | * @return None. |
| 76 | * |
| 77 | * @error std::runtime exception thrown |
| 78 | */ |
| 79 | void initialize(); |
| 80 | |
| 81 | /** @brief Callback function when timer goes off |
| 82 | * |
| 83 | * On getting the signal, initiate the hard power off request |
| 84 | * |
| 85 | * @param[in] eventSource - Source of the event |
| 86 | * @param[in] usec - time in micro seconds |
| 87 | * @param[in] userData - User data pointer |
| 88 | * |
| 89 | */ |
| 90 | static int timeoutHandler(sd_event_source* eventSource, |
| 91 | uint64_t usec, void* userData); |
Vishwanatha Subbanna | d27e71e | 2017-02-01 18:02:38 +0530 | [diff] [blame] | 92 | |
| 93 | /** @brief Gets the current time from steady clock */ |
| 94 | static std::chrono::microseconds getTime(); |
Andrew Geissler | 8622f69 | 2017-04-02 18:19:00 -0500 | [diff] [blame] | 95 | |
| 96 | /** @brief Optional function to call on timer expiration */ |
| 97 | std::function<void()> userCallBack; |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | } // namespace ipmi |
| 101 | } // namespace phosphor |