blob: ec432baae449809d1d23e1525ce42bdf9e788e0e [file] [log] [blame]
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +05301#pragma once
2
Patrick Venture0b02be92018-08-31 11:55:55 -07003#include <systemd/sd-event.h>
4
Vishwanatha Subbanna3eb117a2017-07-12 16:13:49 +05305#include <chrono>
Andrew Geissler8622f692017-04-02 18:19:00 -05006#include <functional>
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +05307namespace phosphor
8{
9namespace ipmi
10{
11
12/** @class Timer
13 * @brief Manages starting watchdog timers and handling timeouts
14 */
15class Timer
16{
Patrick Venture0b02be92018-08-31 11:55:55 -070017 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 Subbannabcb76882017-01-25 16:29:43 +053024
Patrick Venture0b02be92018-08-31 11:55:55 -070025 /** @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 Subbannabcb76882017-01-25 16:29:43 +053041 {
Patrick Venture0b02be92018-08-31 11:55:55 -070042 eventSource = sd_event_source_unref(eventSource);
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053043 }
Patrick Venture0b02be92018-08-31 11:55:55 -070044 }
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053045
Patrick Venture0b02be92018-08-31 11:55:55 -070046 inline auto isExpired() const
47 {
48 return expired;
49 }
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053050
Patrick Venture0b02be92018-08-31 11:55:55 -070051 /** @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 Subbannabcb76882017-01-25 16:29:43 +053055
Patrick Venture0b02be92018-08-31 11:55:55 -070056 /** @brief Enables / disables the timer */
57 int setTimer(int action);
Vishwanatha Subbannad27e71e2017-02-01 18:02:38 +053058
Patrick Venture0b02be92018-08-31 11:55:55 -070059 private:
60 /** @brief the sd_event structure */
61 sd_event* timeEvent = nullptr;
Vishwanatha Subbannad27e71e2017-02-01 18:02:38 +053062
Patrick Venture0b02be92018-08-31 11:55:55 -070063 /** @brief Source of events */
64 sd_event_source* eventSource = nullptr;
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053065
Patrick Venture0b02be92018-08-31 11:55:55 -070066 /** @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 Subbannabcb76882017-01-25 16:29:43 +053071
Patrick Venture0b02be92018-08-31 11:55:55 -070072 /** @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 Subbannabcb76882017-01-25 16:29:43 +053080
Patrick Venture0b02be92018-08-31 11:55:55 -070081 /** @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 Subbannabcb76882017-01-25 16:29:43 +053092
Patrick Venture0b02be92018-08-31 11:55:55 -070093 /** @brief Gets the current time from steady clock */
94 static std::chrono::microseconds getTime();
Vishwanatha Subbannad27e71e2017-02-01 18:02:38 +053095
Patrick Venture0b02be92018-08-31 11:55:55 -070096 /** @brief Optional function to call on timer expiration */
97 std::function<void()> userCallBack;
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053098};
99
100} // namespace ipmi
101} // namespace phosphor