summaryrefslogtreecommitdiffstats
path: root/timer.hpp
blob: 408ded4ec54d2353d4370ab8979031ba74776ef1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#pragma once

#include <chrono>
#include <functional>
#include <systemd/sd-event.h>

namespace phosphor
{
namespace network
{

/** @class Timer
 *  @brief Usage would be,instantiate the timer with the call back
 *         and start the timer for the given time.
 */
class Timer
{
    public:
        /** @brief Only need the default Timer */
        Timer() = delete;
        Timer(const Timer&) = delete;
        Timer& operator=(const Timer&) = delete;
        Timer(Timer&&) = delete;
        Timer& operator=(Timer&&) = delete;

        /** @brief Constructs timer object
         *
         *  @param[in] funcCallBack - optional function callback for timer
         *                            expirations
         */
        Timer(std::function<void()> userCallBack = nullptr):
                userCallBack(userCallBack)
        {
            // Initialize the timer
            initialize();
        }

        ~Timer()
        {
            if (eventSource)
            {
                eventSource = sd_event_source_unref(eventSource);
            }
            if(timeEvent)
            {
                timeEvent = sd_event_unref(timeEvent);
            }
        }

        inline auto isExpired() const
        {
            return expired;
        }

        /** @brief Starts the timer with specified expiration value.
         *  input is an offset from the current steady_clock
         */
        int startTimer(std::chrono::microseconds usec);

        /** @brief Enables / disables the timer */
        int setTimer(int action);

    private:
        /** @brief the sd_event structure */
        sd_event* timeEvent = nullptr;

        /** @brief Source of events */
        sd_event_source* eventSource = nullptr;

        bool expired = true;

        /** @brief Initializes the timer object with infinite
         *         expiration time and sets up the callback handler
         *
         *  @return None.
         *
         *  @error std::runtime exception thrown
         */
        void initialize();

        /** @brief Callback function when timer goes off
         *
         *  @param[in] eventSource - Source of the event
         *  @param[in] usec        - time in micro seconds
         *  @param[in] userData    - User data pointer
         *
         */
        static int timeoutHandler(sd_event_source* eventSource,
                                  uint64_t usec, void* userData);

        /** @brief Gets the current time from steady clock */
        static std::chrono::microseconds getTime();

        /** @brief Optional function to call on timer expiration */
        std::function<void()> userCallBack;
};

} // namespace network
} // namespace phosphor
OpenPOWER on IntegriCloud