Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 3 | #include <systemd/sd-event.h> |
| 4 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 5 | #include <chrono> |
Andrew Geissler | 8622f69 | 2017-04-02 18:19:00 -0500 | [diff] [blame] | 6 | #include <functional> |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 7 | namespace phosphor |
| 8 | { |
| 9 | namespace ipmi |
| 10 | { |
| 11 | |
| 12 | /** @class Timer |
| 13 | * @brief Manages starting watchdog timers and handling timeouts |
| 14 | */ |
| 15 | class Timer |
| 16 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 17 | public: |
| 18 | /** @brief Only need the default Timer */ |
| 19 | Timer() = delete; |
| 20 | Timer(const Timer&) = delete; |
| 21 | Timer& operator=(const Timer&) = delete; |
| 22 | Timer(Timer&&) = delete; |
| 23 | Timer& operator=(Timer&&) = delete; |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 24 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 25 | /** @brief Constructs timer object |
| 26 | * |
| 27 | * @param[in] events - sd_event pointer |
| 28 | * @param[in] funcCallBack - optional function callback for timer |
| 29 | * expirations |
| 30 | */ |
| 31 | Timer(sd_event* events, std::function<void()> userCallBack = nullptr) : |
| 32 | timeEvent(events), userCallBack(userCallBack) |
| 33 | { |
| 34 | // Initialize the timer |
| 35 | initialize(); |
| 36 | } |
| 37 | |
| 38 | ~Timer() |
| 39 | { |
| 40 | if (eventSource) |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 41 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 42 | eventSource = sd_event_source_unref(eventSource); |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 43 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 44 | } |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 45 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 46 | inline auto isExpired() const |
| 47 | { |
| 48 | return expired; |
| 49 | } |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 50 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [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); |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 55 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 56 | /** @brief Enables / disables the timer */ |
| 57 | int setTimer(int action); |
Vishwanatha Subbanna | d27e71e | 2017-02-01 18:02:38 +0530 | [diff] [blame] | 58 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 59 | private: |
| 60 | /** @brief the sd_event structure */ |
| 61 | sd_event* timeEvent = nullptr; |
Vishwanatha Subbanna | d27e71e | 2017-02-01 18:02:38 +0530 | [diff] [blame] | 62 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 63 | /** @brief Source of events */ |
| 64 | sd_event_source* eventSource = nullptr; |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 65 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 66 | /** @brief Returns if the associated timer is expired |
| 67 | * |
| 68 | * This is set to true when the timeoutHandler is called into |
| 69 | */ |
| 70 | bool expired = true; |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 71 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 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(); |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 80 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 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, uint64_t usec, |
| 91 | void* userData); |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 92 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 93 | /** @brief Gets the current time from steady clock */ |
| 94 | static std::chrono::microseconds getTime(); |
Vishwanatha Subbanna | d27e71e | 2017-02-01 18:02:38 +0530 | [diff] [blame] | 95 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 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 |