Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <chrono> |
| 4 | #include <functional> |
| 5 | #include <memory> |
| 6 | #include <systemd/sd-event.h> |
| 7 | |
| 8 | #include "sdevent/source.hpp" |
| 9 | #include "sdevent/event.hpp" |
| 10 | |
| 11 | // TODO: openbmc/openbmc#1720 - add error handling for sd_event API failures |
| 12 | |
| 13 | namespace sdevent |
| 14 | { |
| 15 | namespace event |
| 16 | { |
| 17 | namespace timer |
| 18 | { |
| 19 | |
| 20 | /** @class Timer |
| 21 | * @brief Provides C++ bindings to the sd_event_source* timer functions. |
| 22 | */ |
| 23 | class Timer |
| 24 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame^] | 25 | public: |
| 26 | /* Define all of the basic class operations: |
| 27 | * Not allowed: |
| 28 | * - Default constructor to avoid nullptrs. |
| 29 | * - Copy operations due to internal unique_ptr. |
| 30 | * Allowed: |
| 31 | * - Move operations. |
| 32 | * - Destructor. |
| 33 | */ |
| 34 | Timer() = delete; |
| 35 | Timer(const Timer&) = delete; |
| 36 | Timer& operator=(const Timer&) = delete; |
| 37 | Timer(Timer&&) = default; |
| 38 | Timer& operator=(Timer&&) = default; |
| 39 | ~Timer() = default; |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 40 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame^] | 41 | using Callback = std::function<void(source::Source&)>; |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 42 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame^] | 43 | /** @brief Register a timer callback. |
| 44 | * |
| 45 | * @param[in] event - The event to register on. |
| 46 | * @param[in] expires - The initial timer expiration time. |
| 47 | * @param[in] callback - The callback method. |
| 48 | */ |
| 49 | Timer(sdevent::event::Event& event, |
| 50 | const std::chrono::steady_clock::time_point& expires, |
| 51 | Callback callback) : |
| 52 | src(nullptr), |
| 53 | cb(std::make_unique<Callback>(std::move(callback))) |
| 54 | { |
| 55 | using namespace std::chrono; |
| 56 | auto epoch = expires.time_since_epoch(); |
| 57 | auto time = duration_cast<microseconds>(epoch); |
| 58 | sd_event_source* source = nullptr; |
| 59 | sd_event_add_time(event.get(), &source, CLOCK_MONOTONIC, time.count(), |
| 60 | 0, callCallback, cb.get()); |
| 61 | // **INDENT-OFF** |
| 62 | src = decltype(src){source, std::false_type()}; |
| 63 | // **INDENT-ON** |
| 64 | } |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 65 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame^] | 66 | /** @brief Register a disabled timer callback. |
| 67 | * |
| 68 | * @param[in] event - The event to register on. |
| 69 | * @param[in] callback - The callback method. |
| 70 | */ |
| 71 | Timer(sdevent::event::Event& event, Callback callback) : |
| 72 | src(nullptr), cb(std::make_unique<Callback>(std::move(callback))) |
| 73 | { |
| 74 | sd_event_source* source = nullptr; |
| 75 | sd_event_add_time(event.get(), &source, CLOCK_MONOTONIC, ULLONG_MAX, 0, |
| 76 | callCallback, cb.get()); |
| 77 | // **INDENT-OFF** |
| 78 | src = decltype(src){source, std::false_type()}; |
| 79 | // **INDENT-ON** |
| 80 | } |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 81 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame^] | 82 | /** @brief Set the timer expiration time. */ |
| 83 | void setTime(const std::chrono::steady_clock::time_point& expires) |
| 84 | { |
| 85 | src.setTime(expires); |
| 86 | } |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 87 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame^] | 88 | /** @brief Get the timer expiration time. */ |
| 89 | auto getTime() |
| 90 | { |
| 91 | return src.getTime(); |
| 92 | } |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 93 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame^] | 94 | /** @brief Set the timer source enable state. */ |
| 95 | void enable(int enable) |
| 96 | { |
| 97 | src.enable(enable); |
| 98 | } |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 99 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame^] | 100 | /** @brief Query timer state. */ |
| 101 | auto enabled() |
| 102 | { |
| 103 | return src.enabled(); |
| 104 | } |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 105 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame^] | 106 | private: |
| 107 | source::Source src; |
| 108 | std::unique_ptr<Callback> cb = nullptr; |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 109 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame^] | 110 | static int callCallback(sd_event_source* s, uint64_t usec, void* context) |
| 111 | { |
| 112 | source::Source source(s); |
| 113 | auto c = static_cast<Callback*>(context); |
| 114 | (*c)(source); |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 115 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame^] | 116 | return 0; |
| 117 | } |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 118 | }; |
| 119 | } // namespace timer |
| 120 | } // namespace event |
| 121 | } // namespace sdevent |