blob: 6a59c0d1bb969f73c20b149c1019d1f2813e71af [file] [log] [blame]
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +05301#pragma once
2
Vishwanatha Subbanna3eb117a2017-07-12 16:13:49 +05303#include <chrono>
Andrew Geissler8622f692017-04-02 18:19:00 -05004#include <functional>
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +05305#include <systemd/sd-event.h>
6namespace phosphor
7{
8namespace ipmi
9{
10
11/** @class Timer
12 * @brief Manages starting watchdog timers and handling timeouts
13 */
14class Timer
15{
16 public:
17 /** @brief Only need the default Timer */
18 Timer() = delete;
19 Timer(const Timer&) = delete;
20 Timer& operator=(const Timer&) = delete;
21 Timer(Timer&&) = delete;
22 Timer& operator=(Timer&&) = delete;
23
24 /** @brief Constructs timer object
25 *
26 * @param[in] events - sd_event pointer
Andrew Geissler8622f692017-04-02 18:19:00 -050027 * @param[in] funcCallBack - optional function callback for timer
28 * expirations
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053029 */
Andrew Geissler8622f692017-04-02 18:19:00 -050030 Timer(sd_event* events,
31 std::function<void()> userCallBack = nullptr)
32 : timeEvent(events), userCallBack(userCallBack)
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053033 {
34 // Initialize the timer
35 initialize();
36 }
37
38 ~Timer()
39 {
40 if (eventSource)
41 {
42 eventSource = sd_event_source_unref(eventSource);
43 }
44 }
45
Vishwanatha Subbannad27e71e2017-02-01 18:02:38 +053046 inline auto isExpired() const
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053047 {
48 return expired;
49 }
50
Vishwanatha Subbannad27e71e2017-02-01 18:02:38 +053051 /** @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);
55
56 /** @brief Enables / disables the timer */
57 int setTimer(int action);
58
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053059 private:
60 /** @brief the sd_event structure */
61 sd_event* timeEvent = nullptr;
62
63 /** @brief Source of events */
64 sd_event_source* eventSource = nullptr;
65
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 = false;
71
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();
80
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,
91 uint64_t usec, void* userData);
Vishwanatha Subbannad27e71e2017-02-01 18:02:38 +053092
93 /** @brief Gets the current time from steady clock */
94 static std::chrono::microseconds getTime();
Andrew Geissler8622f692017-04-02 18:19:00 -050095
96 /** @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