Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 3 | #include <systemd/sd-event.h> |
| 4 | |
| 5 | #include <chrono> |
Brad Bishop | be9bec1 | 2018-02-21 12:51:24 -0500 | [diff] [blame] | 6 | #include <functional> |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 7 | #include <memory> |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 8 | namespace phosphor |
| 9 | { |
| 10 | namespace watchdog |
| 11 | { |
| 12 | |
| 13 | /* Need a custom deleter for freeing up sd_event */ |
| 14 | struct EventDeleter |
| 15 | { |
| 16 | void operator()(sd_event* event) const |
| 17 | { |
| 18 | event = sd_event_unref(event); |
| 19 | } |
| 20 | }; |
| 21 | using EventPtr = std::unique_ptr<sd_event, EventDeleter>; |
| 22 | |
| 23 | /* Need a custom deleter for freeing up sd_event_source */ |
| 24 | struct EventSourceDeleter |
| 25 | { |
| 26 | void operator()(sd_event_source* eventSource) const |
| 27 | { |
| 28 | eventSource = sd_event_source_unref(eventSource); |
| 29 | } |
| 30 | }; |
| 31 | using EventSourcePtr = std::unique_ptr<sd_event_source, EventSourceDeleter>; |
| 32 | |
| 33 | /** @class Timer |
| 34 | * @brief Manages starting timers and handling timeouts |
| 35 | */ |
| 36 | class Timer |
| 37 | { |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 38 | public: |
| 39 | Timer() = delete; |
| 40 | ~Timer() = default; |
| 41 | Timer(const Timer&) = delete; |
| 42 | Timer& operator=(const Timer&) = delete; |
| 43 | Timer(Timer&&) = delete; |
| 44 | Timer& operator=(Timer&&) = delete; |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 45 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 46 | /** @brief Constructs timer object |
| 47 | * |
| 48 | * @param[in] event - sd_event unique pointer |
| 49 | * @param[in] userCallBack - Optional function callback |
| 50 | * for timer expiration |
| 51 | */ |
| 52 | Timer(EventPtr& event, std::function<void()> userCallBack = nullptr) : |
| 53 | event(event), userCallBack(userCallBack) |
| 54 | { |
| 55 | // Initialize the timer |
| 56 | initialize(); |
| 57 | } |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 58 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 59 | void clearExpired(void) |
| 60 | { |
| 61 | expire = false; |
| 62 | } |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 63 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 64 | /** @brief Tells whether the timer is expired or not */ |
| 65 | inline auto expired() const |
| 66 | { |
| 67 | return expire; |
| 68 | } |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 69 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 70 | /** @brief Returns the current Timer enablement type */ |
| 71 | int getEnabled() const; |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 72 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 73 | /** @brief Enables / disables the timer. |
| 74 | * <T> is an integral constant boolean |
| 75 | */ |
| 76 | template <typename T> |
| 77 | void setEnabled() |
| 78 | { |
| 79 | constexpr auto type = T::value ? SD_EVENT_ONESHOT : SD_EVENT_OFF; |
| 80 | return setEnabled(type); |
| 81 | } |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 82 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 83 | /** @brief Returns time remaining in usec before expiration |
| 84 | * which is an offset to current steady clock |
| 85 | */ |
| 86 | std::chrono::microseconds getRemaining() const; |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 87 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 88 | /** @brief Starts the timer with specified expiration value. |
| 89 | * std::steady_clock is used for base time. |
| 90 | * |
| 91 | * @param[in] usec - Microseconds from the current time |
| 92 | * before expiration. |
| 93 | * |
| 94 | * @return None. |
| 95 | * |
| 96 | * @error Throws exception |
| 97 | */ |
| 98 | void start(std::chrono::microseconds usec); |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 99 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 100 | /** @brief Gets the current time from steady clock */ |
| 101 | static std::chrono::microseconds getCurrentTime(); |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 102 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 103 | private: |
| 104 | /** @brief Reference to sd_event unique pointer */ |
| 105 | EventPtr& event; |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 106 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 107 | /** @brief event source */ |
| 108 | EventSourcePtr eventSource; |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 109 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 110 | /** @brief Set to true when the timeoutHandler is called into */ |
| 111 | bool expire = false; |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 112 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 113 | /** @brief Optional function to call on timer expiration |
| 114 | * This is called from timeout handler. |
| 115 | */ |
| 116 | std::function<void()> userCallBack; |
Vishwanatha Subbanna | 8c5a229 | 2017-05-30 15:34:23 +0530 | [diff] [blame] | 117 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 118 | /** @brief Initializes the timer object with infinite |
| 119 | * expiration time and sets up the callback handler |
| 120 | * |
| 121 | * @return None. |
| 122 | * |
| 123 | * @error Throws exception |
| 124 | */ |
| 125 | void initialize(); |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 126 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 127 | /** @brief Callback function when timer goes off |
| 128 | * |
| 129 | * @param[in] eventSource - Source of the event |
| 130 | * @param[in] usec - time in microseconds |
| 131 | * @param[in] userData - User data pointer |
| 132 | * |
| 133 | */ |
| 134 | static int timeoutHandler(sd_event_source* eventSource, uint64_t usec, |
| 135 | void* userData); |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 136 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 137 | /** @brief Enables / disables the timer |
| 138 | * |
| 139 | * @param[in] type - Timer type. |
| 140 | * This implementation uses only SD_EVENT_OFF |
| 141 | * and SD_EVENT_ONESHOT |
| 142 | */ |
| 143 | void setEnabled(int type); |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | } // namespace watchdog |
| 147 | } // namespace phosphor |