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 | |
| 6 | namespace phosphor |
| 7 | { |
| 8 | namespace dbus |
| 9 | { |
| 10 | namespace monitoring |
| 11 | { |
| 12 | |
| 13 | /** @class SDEvent |
| 14 | * @brief SDEventType access delegate implementation for sdevent. |
| 15 | */ |
| 16 | class SDEvent |
| 17 | { |
| 18 | protected: |
| 19 | /** @brief Share a single event loop amongst users. */ |
| 20 | static auto& getEvent() |
| 21 | { |
| 22 | static auto event = sdevent::event::newDefault(); |
| 23 | return event; |
| 24 | } |
| 25 | |
| 26 | public: |
| 27 | /** @brief Wrapper for sd_event_now. */ |
| 28 | static auto now() |
| 29 | { |
| 30 | return getEvent().now(); |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | /** @class SDEventTimer |
| 35 | * @brief TimerType access delegate implementation for sdevent. |
| 36 | */ |
| 37 | class SDEventTimer : public SDEvent |
| 38 | { |
| 39 | public: |
| 40 | SDEventTimer() = delete; |
| 41 | SDEventTimer(const SDEventTimer&) = default; |
| 42 | SDEventTimer(SDEventTimer&&) = default; |
| 43 | SDEventTimer& operator=(const SDEventTimer&) = default; |
| 44 | SDEventTimer& operator=(SDEventTimer&&) = default; |
| 45 | ~SDEventTimer() = default; |
| 46 | |
| 47 | explicit SDEventTimer( |
| 48 | const sdevent::event::timer::Timer::Callback& callback) |
| 49 | : timer(getEvent(), callback) {} |
| 50 | |
| 51 | /** @brief Update a timer expiration. */ |
| 52 | void update( |
| 53 | const std::chrono::steady_clock::time_point& expires) |
| 54 | { |
| 55 | timer.setTime(expires); |
| 56 | } |
| 57 | |
| 58 | /** @brief Query timer state. */ |
| 59 | auto enabled() |
| 60 | { |
| 61 | return timer.enabled() != SD_EVENT_OFF; |
| 62 | } |
| 63 | |
| 64 | /** @brief Enable a timer. */ |
| 65 | void enable() |
| 66 | { |
| 67 | timer.enable(SD_EVENT_ONESHOT); |
| 68 | } |
| 69 | |
| 70 | /** @brief Disable a timer. */ |
| 71 | void disable() |
| 72 | { |
| 73 | timer.enable(SD_EVENT_OFF); |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | sdevent::event::timer::Timer timer; |
| 78 | }; |
| 79 | |
| 80 | } // namespace monitoring |
| 81 | } // namespace dbus |
| 82 | } // namespace phosphor |