Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 3 | #include <systemd/sd-event.h> |
| 4 | |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 5 | #include <chrono> |
| 6 | #include <functional> |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace hwmon |
| 11 | { |
| 12 | namespace timer |
| 13 | { |
| 14 | |
| 15 | enum Action |
| 16 | { |
| 17 | OFF = SD_EVENT_OFF, |
| 18 | ON = SD_EVENT_ON, |
| 19 | ONESHOT = SD_EVENT_ONESHOT |
| 20 | }; |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | /** @class Timer |
| 24 | * @brief Provides a timer source and a mechanism to callback when the timer |
| 25 | * expires. |
| 26 | * |
| 27 | * The timer is armed upon construction. The constructor requires a timeout |
| 28 | * handler function, the timer expiry duration, and the timer state (one-shot, |
| 29 | * repetitive, disabled). |
| 30 | * It's possible to change the state of the timer after it's been armed via the |
| 31 | * state() API. |
| 32 | */ |
| 33 | class Timer |
| 34 | { |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 35 | public: |
| 36 | Timer() = delete; |
| 37 | Timer(const Timer&) = delete; |
| 38 | Timer& operator=(const Timer&) = delete; |
| 39 | Timer(Timer&&) = delete; |
| 40 | Timer& operator=(Timer&&) = delete; |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 41 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 42 | /** @brief Constructs timer object |
| 43 | * |
| 44 | * @param[in] events - sd_event pointer |
| 45 | * @param[in] callback - function callback for timer expiry |
| 46 | * @param[in] usec - timer duration, in micro seconds |
| 47 | * @param[in] action - controls the timer's lifetime |
| 48 | */ |
| 49 | Timer(sd_event* event, std::function<void()> userCallback, |
| 50 | std::chrono::microseconds usec, timer::Action action); |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 51 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 52 | ~Timer() |
| 53 | { |
| 54 | if (eventSource) |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 55 | { |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 56 | eventSource = sd_event_source_unref(eventSource); |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 57 | } |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 58 | } |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 59 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 60 | /** @brief Timer expiry handler - invokes callback |
| 61 | * |
| 62 | * @param[in] eventSource - Source of the event |
| 63 | * @param[in] usec - time in micro seconds |
| 64 | * @param[in] userData - User data pointer |
| 65 | * |
| 66 | */ |
| 67 | static int timeoutHandler(sd_event_source* eventSource, uint64_t usec, |
| 68 | void* userData); |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 69 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 70 | /** @brief Enables / disables the timer |
| 71 | * @param[in] action - controls the timer's lifetime |
| 72 | */ |
| 73 | int state(timer::Action action) |
| 74 | { |
| 75 | return sd_event_source_set_enabled(eventSource, action); |
| 76 | } |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 77 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 78 | timer::Action getAction() const |
| 79 | { |
| 80 | return action; |
| 81 | } |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 82 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 83 | std::chrono::microseconds getDuration() const |
| 84 | { |
| 85 | return duration; |
| 86 | } |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 87 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 88 | private: |
| 89 | /** @brief the sd_event structure */ |
| 90 | sd_event* event = nullptr; |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 91 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 92 | /** @brief Source of events */ |
| 93 | sd_event_source* eventSource = nullptr; |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 94 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 95 | /** @brief Optional function to call on timer expiration */ |
| 96 | std::function<void()> callback{}; |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 97 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 98 | /** @brief Duration of the timer */ |
| 99 | std::chrono::microseconds duration{}; |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 100 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 101 | /** @brief whether the timer is enabled/disabled/one-shot */ |
| 102 | timer::Action action = timer::OFF; |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | } // namespace hwmon |
| 106 | } // namespace phosphor |