blob: 7c3bac2eea1554ab204efe3637e823107549a527 [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
41 inline auto isExpired()
42 {
43 return expired;
44 }
45
46 private:
47 /** @brief the sd_event structure */
48 sd_event* timeEvent = nullptr;
49
50 /** @brief Source of events */
51 sd_event_source* eventSource = nullptr;
52
53 /** @brief Returns if the associated timer is expired
54 *
55 * This is set to true when the timeoutHandler is called into
56 */
57 bool expired = false;
58
59 /** @brief Initializes the timer object with infinite
60 * expiration time and sets up the callback handler
61 *
62 * @return None.
63 *
64 * @error std::runtime exception thrown
65 */
66 void initialize();
67
68 /** @brief Callback function when timer goes off
69 *
70 * On getting the signal, initiate the hard power off request
71 *
72 * @param[in] eventSource - Source of the event
73 * @param[in] usec - time in micro seconds
74 * @param[in] userData - User data pointer
75 *
76 */
77 static int timeoutHandler(sd_event_source* eventSource,
78 uint64_t usec, void* userData);
79};
80
81} // namespace ipmi
82} // namespace phosphor