Nagaraju Goruganti | 41a774e | 2018-04-06 06:15:27 -0500 | [diff] [blame^] | 1 | #pragma once |
| 2 | |
| 3 | #include <memory> |
| 4 | #include <chrono> |
| 5 | #include <functional> |
| 6 | #include <systemd/sd-event.h> |
| 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace state |
| 11 | { |
| 12 | namespace manager |
| 13 | { |
| 14 | namespace timer |
| 15 | { |
| 16 | |
| 17 | enum Action |
| 18 | { |
| 19 | OFF = SD_EVENT_OFF, |
| 20 | ON = SD_EVENT_ON, |
| 21 | ONESHOT = SD_EVENT_ONESHOT |
| 22 | }; |
| 23 | } // namespace timer |
| 24 | |
| 25 | /* Need a custom deleter for freeing up sd_event */ |
| 26 | struct EventDeleter |
| 27 | { |
| 28 | void operator()(sd_event* event) const |
| 29 | { |
| 30 | event = sd_event_unref(event); |
| 31 | } |
| 32 | }; |
| 33 | using EventPtr = std::unique_ptr<sd_event, EventDeleter>; |
| 34 | |
| 35 | /* Need a custom deleter for freeing up sd_event_source */ |
| 36 | struct EventSourceDeleter |
| 37 | { |
| 38 | void operator()(sd_event_source* eventSource) const |
| 39 | { |
| 40 | eventSource = sd_event_source_unref(eventSource); |
| 41 | } |
| 42 | }; |
| 43 | using EventSourcePtr = std::unique_ptr<sd_event_source, EventSourceDeleter>; |
| 44 | |
| 45 | /** @class Timer |
| 46 | * @brief Provides a timer source and a mechanism to callback when the timer |
| 47 | * expires. |
| 48 | * |
| 49 | * The timer is armed upon construction. The constructor requires a timeout |
| 50 | * handler function, the timer expiry duration, and the timer state (one-shot, |
| 51 | * reptitive, disabled). |
| 52 | * It's possible to change the state of the timer after it's been armed via the |
| 53 | * state() API. |
| 54 | */ |
| 55 | class Timer |
| 56 | { |
| 57 | public: |
| 58 | Timer() = delete; |
| 59 | Timer(const Timer&) = delete; |
| 60 | Timer& operator=(const Timer&) = delete; |
| 61 | Timer(Timer&&) = delete; |
| 62 | Timer& operator=(Timer&&) = delete; |
| 63 | |
| 64 | /** @brief Constructs timer object |
| 65 | * |
| 66 | * @param[in] events - sd_event pointer |
| 67 | * @param[in] callback - function callback for timer expiry |
| 68 | * @param[in] usec - timer duration, in micro seconds |
| 69 | * @param[in] action - controls the timer's lifetime |
| 70 | */ |
| 71 | Timer(EventPtr& event, std::function<void()> userCallback, |
| 72 | std::chrono::microseconds usec, timer::Action action); |
| 73 | |
| 74 | /** @brief Enables / disables the timer |
| 75 | * @param[in] action - controls the timer's lifetime |
| 76 | */ |
| 77 | int state(timer::Action value); |
| 78 | |
| 79 | timer::Action getAction() const; |
| 80 | |
| 81 | std::chrono::microseconds getDuration() const; |
| 82 | |
| 83 | private: |
| 84 | /** @brief Reference to sd_event unique pointer */ |
| 85 | EventPtr& event_; |
| 86 | |
| 87 | /** @brief Source of events */ |
| 88 | EventSourcePtr eventSource_; |
| 89 | |
| 90 | /** @brief Optional function to call on timer expiration */ |
| 91 | std::function<void()> callback_{}; |
| 92 | |
| 93 | /** @brief Duration of the timer */ |
| 94 | std::chrono::microseconds duration_{}; |
| 95 | |
| 96 | /** @brief whether the timer is enabled/disabled/one-shot */ |
| 97 | timer::Action action_ = timer::OFF; |
| 98 | |
| 99 | /** @brief Timer expiry handler - invokes callback |
| 100 | * |
| 101 | * @param[in] eventSource - Source of the event |
| 102 | * @param[in] usec - time in micro seconds |
| 103 | * @param[in] userData - User data pointer |
| 104 | * |
| 105 | * @return zero on success, non-zero otherwise |
| 106 | */ |
| 107 | static int timeoutHandler(sd_event_source* eventSource, uint64_t usec, |
| 108 | void* userData); |
| 109 | }; |
| 110 | } // namespace manager |
| 111 | } // namespace state |
| 112 | } // namespace phosphor |