blob: 9d597f8a9212e8c4a5b4f4040f53fc9bdba4e39d [file] [log] [blame]
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +05301#pragma once
2
3#include <systemd/sd-event.h>
4namespace phosphor
5{
6namespace ipmi
7{
8
9/** @class Timer
10 * @brief Manages starting watchdog timers and handling timeouts
11 */
12class 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 Subbannad27e71e2017-02-01 18:02:38 +053041 inline auto isExpired() const
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053042 {
43 return expired;
44 }
45
Vishwanatha Subbannad27e71e2017-02-01 18:02:38 +053046 /** @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 Subbannabcb76882017-01-25 16:29:43 +053054 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 Subbannad27e71e2017-02-01 18:02:38 +053087
88 /** @brief Gets the current time from steady clock */
89 static std::chrono::microseconds getTime();
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053090};
91
92} // namespace ipmi
93} // namespace phosphor