summaryrefslogtreecommitdiffstats
path: root/timer.hpp
blob: bb1d6e94dad23735d5549aa3a5a92862400e7300 (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
100
101
102
103
104
105
106
107
108
#pragma once

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

namespace phosphor
{
namespace hwmon
{
namespace timer
{

enum Action
{
    OFF = SD_EVENT_OFF,
    ON = SD_EVENT_ON,
    ONESHOT = SD_EVENT_ONESHOT
};

}

/** @class Timer
 *  @brief Provides a timer source and a mechanism to callback when the timer
 *         expires.
 *
 *  The timer is armed upon construction. The constructor requires a timeout
 *  handler function, the timer expiry duration, and the timer state (one-shot,
 *  repetitive, disabled).
 *  It's possible to change the state of the timer after it's been armed via the
 *  state() API.
 */
class Timer
{
    public:
        Timer() = delete;
        Timer(const Timer&) = delete;
        Timer& operator=(const Timer&) = delete;
        Timer(Timer&&) = delete;
        Timer& operator=(Timer&&) = delete;

        /** @brief Constructs timer object
         *
         *  @param[in] events - sd_event pointer
         *  @param[in] callback - function callback for timer expiry
         *  @param[in] usec - timer duration, in micro seconds
         *  @param[in] action - controls the timer's lifetime
         */
        Timer(sd_event* event,
              std::function<void()> userCallback,
              std::chrono::microseconds usec,
              timer::Action action);

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

        /** @brief Timer expiry handler - invokes callback
         *
         *  @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 Enables / disables the timer
         *  @param[in] action - controls the timer's lifetime
         */
        int state(timer::Action action)
        {
            return sd_event_source_set_enabled(eventSource, action);
        }

        timer::Action getAction() const
        {
            return action;
        }

        std::chrono::microseconds getDuration() const
        {
            return duration;
        }

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

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

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

        /** @brief Duration of the timer */
        std::chrono::microseconds duration{};

        /** @brief whether the timer is enabled/disabled/one-shot */
        timer::Action action = timer::OFF;
};

} // namespace hwmon
} // namespace phosphor
OpenPOWER on IntegriCloud