blob: cbd3444fb9b1626a398778cdf56af4a58a4db66e [file] [log] [blame]
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +05301#pragma once
2
Andrew Geissler8622f692017-04-02 18:19:00 -05003#include <functional>
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +05304#include <systemd/sd-event.h>
5namespace phosphor
6{
7namespace ipmi
8{
9
10/** @class Timer
11 * @brief Manages starting watchdog timers and handling timeouts
12 */
13class 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 Geissler8622f692017-04-02 18:19:00 -050026 * @param[in] funcCallBack - optional function callback for timer
27 * expirations
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053028 */
Andrew Geissler8622f692017-04-02 18:19:00 -050029 Timer(sd_event* events,
30 std::function<void()> userCallBack = nullptr)
31 : timeEvent(events), userCallBack(userCallBack)
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053032 {
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 Subbannad27e71e2017-02-01 18:02:38 +053045 inline auto isExpired() const
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053046 {
47 return expired;
48 }
49
Vishwanatha Subbannad27e71e2017-02-01 18:02:38 +053050 /** @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 Subbannabcb76882017-01-25 16:29:43 +053058 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 Subbannad27e71e2017-02-01 18:02:38 +053091
92 /** @brief Gets the current time from steady clock */
93 static std::chrono::microseconds getTime();
Andrew Geissler8622f692017-04-02 18:19:00 -050094
95 /** @brief Optional function to call on timer expiration */
96 std::function<void()> userCallBack;
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053097};
98
99} // namespace ipmi
100} // namespace phosphor