blob: 4fed1593359766b3de60316227bfc7cc0d25a52e [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{
Brad Bishopd1eac882018-03-29 10:34:05 -040020 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 }
Brad Bishopaabc5462017-05-30 12:39:22 -040027
Brad Bishopd1eac882018-03-29 10:34:05 -040028 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
Brad Bishopd1eac882018-03-29 10:34:05 -040035 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{
Brad Bishopd1eac882018-03-29 10:34:05 -040043 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;
Brad Bishopaabc5462017-05-30 12:39:22 -040050
Brad Bishopd1eac882018-03-29 10:34:05 -040051 explicit SDEventTimer(
52 const sdevent::event::timer::Timer::Callback& callback) :
53 timer(getEvent(), callback)
54 {
55 }
Brad Bishopaabc5462017-05-30 12:39:22 -040056
Brad Bishopd1eac882018-03-29 10:34:05 -040057 /** @brief Update a timer expiration. */
58 void update(const std::chrono::steady_clock::time_point& expires)
59 {
60 timer.setTime(expires);
61 }
Brad Bishopaabc5462017-05-30 12:39:22 -040062
Brad Bishopd1eac882018-03-29 10:34:05 -040063 /** @brief Query timer state. */
64 auto enabled()
65 {
66 return timer.enabled() != SD_EVENT_OFF;
67 }
Brad Bishopaabc5462017-05-30 12:39:22 -040068
Brad Bishopd1eac882018-03-29 10:34:05 -040069 /** @brief Enable a timer. */
70 void enable()
71 {
72 timer.enable(SD_EVENT_ONESHOT);
73 }
Brad Bishopaabc5462017-05-30 12:39:22 -040074
Brad Bishopd1eac882018-03-29 10:34:05 -040075 /** @brief Disable a timer. */
76 void disable()
77 {
78 timer.enable(SD_EVENT_OFF);
79 }
Brad Bishopaabc5462017-05-30 12:39:22 -040080
Brad Bishopd1eac882018-03-29 10:34:05 -040081 private:
82 sdevent::event::timer::Timer timer;
Brad Bishopaabc5462017-05-30 12:39:22 -040083};
84
85} // namespace monitoring
86} // namespace dbus
87} // namespace phosphor