Matt Spinler | 2de67cf | 2017-04-27 11:07:53 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <chrono> |
| 4 | #include <functional> |
| 5 | #include <memory> |
Matt Spinler | e824f98 | 2017-05-11 10:07:55 -0500 | [diff] [blame] | 6 | #include "event.hpp" |
Matt Spinler | 2de67cf | 2017-04-27 11:07:53 -0500 | [diff] [blame] | 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace fan |
| 11 | { |
| 12 | namespace util |
| 13 | { |
| 14 | |
Matt Spinler | 2de67cf | 2017-04-27 11:07:53 -0500 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * @class Timer |
| 18 | * |
| 19 | * This class implements a simple timer that runs an arbitrary |
| 20 | * function on expiration. The timeout value is set in microseconds. |
| 21 | * It can be stopped while it is running, and queried to see if it is |
| 22 | * running. |
| 23 | * |
| 24 | * If started with the 'repeating' argument, it will keep calling the |
| 25 | * callback function every <timeout> microseconds. If started with the |
| 26 | * 'oneshot' argument, it will just call the callback function one time. |
| 27 | * |
| 28 | * It needs an sd_event loop to function. |
| 29 | */ |
| 30 | class Timer |
| 31 | { |
| 32 | public: |
| 33 | |
| 34 | enum class TimerType |
| 35 | { |
| 36 | oneshot, |
| 37 | repeating |
| 38 | }; |
| 39 | |
| 40 | Timer() = delete; |
| 41 | Timer(const Timer&) = delete; |
| 42 | Timer& operator=(const Timer&) = delete; |
| 43 | Timer(Timer&&) = default; |
| 44 | Timer& operator=(Timer&&) = default; |
| 45 | |
| 46 | /** |
| 47 | * @brief Constructs timer object |
| 48 | * |
| 49 | * @param[in] events - sd_event pointer, previously created |
| 50 | * @param[in] callbackFunc - The function to call on timer expiration |
| 51 | */ |
Matt Spinler | e824f98 | 2017-05-11 10:07:55 -0500 | [diff] [blame] | 52 | Timer(phosphor::fan::event::EventPtr& events, |
Matt Spinler | 2de67cf | 2017-04-27 11:07:53 -0500 | [diff] [blame] | 53 | std::function<void()> callbackFunc); |
| 54 | |
| 55 | /** |
| 56 | * @brief Destructor |
| 57 | */ |
| 58 | ~Timer(); |
| 59 | |
| 60 | /** |
| 61 | * @brief Starts the timer |
| 62 | * |
| 63 | * The input is an offset from the current steady clock. |
| 64 | * |
| 65 | * @param[in] usec - the timeout value in microseconds |
| 66 | * @param[in] type - either a oneshot, or repeating |
| 67 | */ |
| 68 | void start(std::chrono::microseconds usec, |
| 69 | TimerType type = TimerType::oneshot); |
| 70 | |
| 71 | /** |
| 72 | * @brief Stop the timer |
| 73 | */ |
| 74 | void stop(); |
| 75 | |
| 76 | /** |
| 77 | * @brief Returns true if the timer is running |
| 78 | */ |
| 79 | bool running(); |
| 80 | |
| 81 | /** |
| 82 | * @brief Returns the timeout value |
| 83 | * |
| 84 | * @return - the last value sent in via start(). |
| 85 | * |
| 86 | * Could be used to restart the timer with the same |
| 87 | * timeout. i.e. start(getTimeout()); |
| 88 | */ |
| 89 | inline auto getTimeout() const |
| 90 | { |
| 91 | return timeout; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @brief Returns the timer type |
| 96 | */ |
| 97 | inline auto getType() const |
| 98 | { |
| 99 | return type; |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | |
| 104 | /** |
| 105 | * @brief Callback function when timer goes off |
| 106 | * |
| 107 | * Calls the callback function passed in by the user. |
| 108 | * |
| 109 | * @param[in] eventSource - Source of the event |
| 110 | * @param[in] usec - time in micro seconds |
| 111 | * @param[in] userData - User data pointer |
| 112 | */ |
| 113 | static int timeoutHandler(sd_event_source* eventSource, |
| 114 | uint64_t usec, void* userData); |
| 115 | |
| 116 | /** |
| 117 | * @brief Gets the current time from the steady clock |
| 118 | */ |
| 119 | std::chrono::microseconds getTime(); |
| 120 | |
| 121 | /** |
| 122 | * @brief Wrapper around sd_event_source_set_enabled |
| 123 | * |
| 124 | * @param[in] action - either SD_EVENT_OFF, SD_EVENT_ON, |
| 125 | * or SD_EVENT_ONESHOT |
| 126 | */ |
| 127 | void setTimer(int action); |
| 128 | |
| 129 | |
| 130 | /** |
| 131 | * @brief Sets the expiration time for the timer |
| 132 | * |
| 133 | * Sets it to timeout microseconds in the future |
| 134 | */ |
| 135 | void setTimeout(); |
| 136 | |
| 137 | /** |
| 138 | * @brief The sd_event structure |
| 139 | */ |
Matt Spinler | e824f98 | 2017-05-11 10:07:55 -0500 | [diff] [blame] | 140 | phosphor::fan::event::EventPtr& timeEvent; |
Matt Spinler | 2de67cf | 2017-04-27 11:07:53 -0500 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * @brief Source of events |
| 144 | */ |
Matt Spinler | e824f98 | 2017-05-11 10:07:55 -0500 | [diff] [blame] | 145 | phosphor::fan::event::EventSourcePtr eventSource; |
Matt Spinler | 2de67cf | 2017-04-27 11:07:53 -0500 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * @brief Either 'repeating' or 'oneshot' |
| 149 | */ |
| 150 | TimerType type = TimerType::oneshot; |
| 151 | |
| 152 | /** |
| 153 | * @brief The function to call when the timer expires |
| 154 | */ |
| 155 | std::function<void()> callback; |
| 156 | |
| 157 | /** |
| 158 | * @brief What the timer was set to run for |
| 159 | * |
| 160 | * Not cleared on timer expiration |
| 161 | */ |
| 162 | std::chrono::microseconds timeout; |
| 163 | }; |
| 164 | |
| 165 | } |
| 166 | } |
| 167 | } |