blob: 03f642d4e875ea5d6b64fe668018fbf90679718a [file] [log] [blame]
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -06001#pragma once
2
Patrick Venture043d3232018-08-31 10:10:53 -07003#include <systemd/sd-event.h>
4
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -06005#include <chrono>
6#include <functional>
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -06007
8namespace phosphor
9{
10namespace hwmon
11{
12namespace timer
13{
14
15enum Action
16{
17 OFF = SD_EVENT_OFF,
18 ON = SD_EVENT_ON,
19 ONESHOT = SD_EVENT_ONESHOT
20};
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -060021}
22
23/** @class Timer
24 * @brief Provides a timer source and a mechanism to callback when the timer
25 * expires.
26 *
27 * The timer is armed upon construction. The constructor requires a timeout
28 * handler function, the timer expiry duration, and the timer state (one-shot,
29 * repetitive, disabled).
30 * It's possible to change the state of the timer after it's been armed via the
31 * state() API.
32 */
33class Timer
34{
Patrick Venture043d3232018-08-31 10:10:53 -070035 public:
36 Timer() = delete;
37 Timer(const Timer&) = delete;
38 Timer& operator=(const Timer&) = delete;
39 Timer(Timer&&) = delete;
40 Timer& operator=(Timer&&) = delete;
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -060041
Patrick Venture043d3232018-08-31 10:10:53 -070042 /** @brief Constructs timer object
43 *
44 * @param[in] events - sd_event pointer
45 * @param[in] callback - function callback for timer expiry
46 * @param[in] usec - timer duration, in micro seconds
47 * @param[in] action - controls the timer's lifetime
48 */
49 Timer(sd_event* event, std::function<void()> userCallback,
50 std::chrono::microseconds usec, timer::Action action);
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -060051
Patrick Venture043d3232018-08-31 10:10:53 -070052 ~Timer()
53 {
54 if (eventSource)
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -060055 {
Patrick Venture043d3232018-08-31 10:10:53 -070056 eventSource = sd_event_source_unref(eventSource);
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -060057 }
Patrick Venture043d3232018-08-31 10:10:53 -070058 }
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -060059
Patrick Venture043d3232018-08-31 10:10:53 -070060 /** @brief Timer expiry handler - invokes callback
61 *
62 * @param[in] eventSource - Source of the event
63 * @param[in] usec - time in micro seconds
64 * @param[in] userData - User data pointer
65 *
66 */
67 static int timeoutHandler(sd_event_source* eventSource, uint64_t usec,
68 void* userData);
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -060069
Patrick Venture043d3232018-08-31 10:10:53 -070070 /** @brief Enables / disables the timer
71 * @param[in] action - controls the timer's lifetime
72 */
73 int state(timer::Action action)
74 {
75 return sd_event_source_set_enabled(eventSource, action);
76 }
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -060077
Patrick Venture043d3232018-08-31 10:10:53 -070078 timer::Action getAction() const
79 {
80 return action;
81 }
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -060082
Patrick Venture043d3232018-08-31 10:10:53 -070083 std::chrono::microseconds getDuration() const
84 {
85 return duration;
86 }
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -060087
Patrick Venture043d3232018-08-31 10:10:53 -070088 private:
89 /** @brief the sd_event structure */
90 sd_event* event = nullptr;
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -060091
Patrick Venture043d3232018-08-31 10:10:53 -070092 /** @brief Source of events */
93 sd_event_source* eventSource = nullptr;
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -060094
Patrick Venture043d3232018-08-31 10:10:53 -070095 /** @brief Optional function to call on timer expiration */
96 std::function<void()> callback{};
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -060097
Patrick Venture043d3232018-08-31 10:10:53 -070098 /** @brief Duration of the timer */
99 std::chrono::microseconds duration{};
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -0600100
Patrick Venture043d3232018-08-31 10:10:53 -0700101 /** @brief whether the timer is enabled/disabled/one-shot */
102 timer::Action action = timer::OFF;
Deepak Kodihalli6aa24d42018-03-07 01:10:03 -0600103};
104
105} // namespace hwmon
106} // namespace phosphor