blob: dc912c1383a4043dd7e357c4e208360609656f23 [file] [log] [blame]
Brad Bishopaabc5462017-05-30 12:39:22 -04001#pragma once
2
3#include "sdevent/event.hpp"
4#include "sdevent/timer.hpp"
5
Brad Bishopd9c1fab2017-06-04 22:32:12 -04006struct Loop;
7
Brad Bishopaabc5462017-05-30 12:39:22 -04008namespace phosphor
9{
10namespace dbus
11{
12namespace monitoring
13{
14
15/** @class SDEvent
16 * @brief SDEventType access delegate implementation for sdevent.
17 */
18class SDEvent
19{
20 protected:
21 /** @brief Share a single event loop amongst users. */
22 static auto& getEvent()
23 {
24 static auto event = sdevent::event::newDefault();
25 return event;
26 }
27
28 public:
29 /** @brief Wrapper for sd_event_now. */
30 static auto now()
31 {
32 return getEvent().now();
33 }
Brad Bishopd9c1fab2017-06-04 22:32:12 -040034
35 friend Loop;
Brad Bishopaabc5462017-05-30 12:39:22 -040036};
37
38/** @class SDEventTimer
39 * @brief TimerType access delegate implementation for sdevent.
40 */
41class SDEventTimer : public SDEvent
42{
43 public:
44 SDEventTimer() = delete;
45 SDEventTimer(const SDEventTimer&) = default;
46 SDEventTimer(SDEventTimer&&) = default;
47 SDEventTimer& operator=(const SDEventTimer&) = default;
48 SDEventTimer& operator=(SDEventTimer&&) = default;
49 ~SDEventTimer() = default;
50
51 explicit SDEventTimer(
52 const sdevent::event::timer::Timer::Callback& callback)
53 : timer(getEvent(), callback) {}
54
55 /** @brief Update a timer expiration. */
56 void update(
57 const std::chrono::steady_clock::time_point& expires)
58 {
59 timer.setTime(expires);
60 }
61
62 /** @brief Query timer state. */
63 auto enabled()
64 {
65 return timer.enabled() != SD_EVENT_OFF;
66 }
67
68 /** @brief Enable a timer. */
69 void enable()
70 {
71 timer.enable(SD_EVENT_ONESHOT);
72 }
73
74 /** @brief Disable a timer. */
75 void disable()
76 {
77 timer.enable(SD_EVENT_OFF);
78 }
79
80 private:
81 sdevent::event::timer::Timer timer;
82};
83
84} // namespace monitoring
85} // namespace dbus
86} // namespace phosphor