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