Brad Bishop | aabc546 | 2017-05-30 12:39:22 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "sdevent/event.hpp" |
| 4 | #include "sdevent/timer.hpp" |
| 5 | |
Brad Bishop | d9c1fab | 2017-06-04 22:32:12 -0400 | [diff] [blame] | 6 | struct Loop; |
| 7 | |
Brad Bishop | aabc546 | 2017-05-30 12:39:22 -0400 | [diff] [blame] | 8 | namespace phosphor |
| 9 | { |
| 10 | namespace dbus |
| 11 | { |
| 12 | namespace monitoring |
| 13 | { |
| 14 | |
| 15 | /** @class SDEvent |
| 16 | * @brief SDEventType access delegate implementation for sdevent. |
| 17 | */ |
| 18 | class 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 Bishop | d9c1fab | 2017-06-04 22:32:12 -0400 | [diff] [blame] | 34 | |
| 35 | friend Loop; |
Brad Bishop | aabc546 | 2017-05-30 12:39:22 -0400 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | /** @class SDEventTimer |
| 39 | * @brief TimerType access delegate implementation for sdevent. |
| 40 | */ |
| 41 | class 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 |