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