blob: 32b6f17de8976cd40e9946203cefe3038a26e920 [file] [log] [blame]
Ratan Gupta4f80c1a2017-09-03 18:01:22 +05301#pragma once
2
Patrick Venture189d44e2018-07-09 12:30:59 -07003#include <systemd/sd-event.h>
4
Ratan Gupta4f80c1a2017-09-03 18:01:22 +05305#include <chrono>
6#include <functional>
Ratan Gupta4f80c1a2017-09-03 18:01:22 +05307
8namespace phosphor
9{
10namespace network
11{
12
13/** @class Timer
14 * @brief Usage would be,instantiate the timer with the call back
15 * and start the timer for the given time.
16 */
17class Timer
18{
Gunnar Mills57d9c502018-09-14 14:42:34 -050019 public:
20 /** @brief Only need the default Timer */
21 Timer() = delete;
22 Timer(const Timer&) = delete;
23 Timer& operator=(const Timer&) = delete;
24 Timer(Timer&&) = delete;
25 Timer& operator=(Timer&&) = delete;
Ratan Gupta4f80c1a2017-09-03 18:01:22 +053026
Gunnar Mills57d9c502018-09-14 14:42:34 -050027 /** @brief Constructs timer object
28 *
29 * @param[in] funcCallBack - optional function callback for timer
30 * expirations
31 */
32 Timer(std::function<void()> userCallBack = nullptr) :
33 userCallBack(userCallBack)
34 {
35 // Initialize the timer
36 initialize();
37 }
38
39 ~Timer()
40 {
41 if (eventSource)
Ratan Gupta4f80c1a2017-09-03 18:01:22 +053042 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050043 eventSource = sd_event_source_unref(eventSource);
Ratan Gupta4f80c1a2017-09-03 18:01:22 +053044 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050045 if (timeEvent)
Ratan Gupta4f80c1a2017-09-03 18:01:22 +053046 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050047 timeEvent = sd_event_unref(timeEvent);
Ratan Gupta4f80c1a2017-09-03 18:01:22 +053048 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050049 }
Ratan Gupta4f80c1a2017-09-03 18:01:22 +053050
Gunnar Mills57d9c502018-09-14 14:42:34 -050051 inline auto isExpired() const
52 {
53 return expired;
54 }
Ratan Gupta4f80c1a2017-09-03 18:01:22 +053055
Gunnar Mills57d9c502018-09-14 14:42:34 -050056 /** @brief Starts the timer with specified expiration value.
57 * input is an offset from the current steady_clock
58 */
59 int startTimer(std::chrono::microseconds usec);
Ratan Gupta4f80c1a2017-09-03 18:01:22 +053060
Gunnar Mills57d9c502018-09-14 14:42:34 -050061 /** @brief Enables / disables the timer */
62 int setTimer(int action);
Ratan Gupta4f80c1a2017-09-03 18:01:22 +053063
Gunnar Mills57d9c502018-09-14 14:42:34 -050064 private:
65 /** @brief the sd_event structure */
66 sd_event* timeEvent = nullptr;
Ratan Gupta4f80c1a2017-09-03 18:01:22 +053067
Gunnar Mills57d9c502018-09-14 14:42:34 -050068 /** @brief Source of events */
69 sd_event_source* eventSource = nullptr;
Ratan Gupta4f80c1a2017-09-03 18:01:22 +053070
Gunnar Mills57d9c502018-09-14 14:42:34 -050071 bool expired = true;
Ratan Gupta4f80c1a2017-09-03 18:01:22 +053072
Gunnar Mills57d9c502018-09-14 14:42:34 -050073 /** @brief Initializes the timer object with infinite
74 * expiration time and sets up the callback handler
75 *
76 * @return None.
77 *
78 * @error std::runtime exception thrown
79 */
80 void initialize();
Ratan Gupta4f80c1a2017-09-03 18:01:22 +053081
Gunnar Mills57d9c502018-09-14 14:42:34 -050082 /** @brief Callback function when timer goes off
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, uint64_t usec,
90 void* userData);
Ratan Gupta4f80c1a2017-09-03 18:01:22 +053091
Gunnar Mills57d9c502018-09-14 14:42:34 -050092 /** @brief Gets the current time from steady clock */
93 static std::chrono::microseconds getTime();
Ratan Gupta4f80c1a2017-09-03 18:01:22 +053094
Gunnar Mills57d9c502018-09-14 14:42:34 -050095 /** @brief Optional function to call on timer expiration */
96 std::function<void()> userCallBack;
Ratan Gupta4f80c1a2017-09-03 18:01:22 +053097};
98
99} // namespace network
100} // namespace phosphor