blob: 5e45b9452987651d87b97ae45ec873b656ee2d78 [file] [log] [blame]
Brad Bishop605662d2017-05-30 12:39:09 -04001#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
13namespace sdevent
14{
15namespace event
16{
17namespace timer
18{
19
20/** @class Timer
21 * @brief Provides C++ bindings to the sd_event_source* timer functions.
22 */
23class Timer
24{
Brad Bishopd1eac882018-03-29 10:34:05 -040025 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 Bishop605662d2017-05-30 12:39:09 -040040
Brad Bishopd1eac882018-03-29 10:34:05 -040041 using Callback = std::function<void(source::Source&)>;
Brad Bishop605662d2017-05-30 12:39:09 -040042
Brad Bishopd1eac882018-03-29 10:34:05 -040043 /** @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 Bishop605662d2017-05-30 12:39:09 -040065
Brad Bishopd1eac882018-03-29 10:34:05 -040066 /** @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 Bishop605662d2017-05-30 12:39:09 -040081
Brad Bishopd1eac882018-03-29 10:34:05 -040082 /** @brief Set the timer expiration time. */
83 void setTime(const std::chrono::steady_clock::time_point& expires)
84 {
85 src.setTime(expires);
86 }
Brad Bishop605662d2017-05-30 12:39:09 -040087
Brad Bishopd1eac882018-03-29 10:34:05 -040088 /** @brief Get the timer expiration time. */
89 auto getTime()
90 {
91 return src.getTime();
92 }
Brad Bishop605662d2017-05-30 12:39:09 -040093
Brad Bishopd1eac882018-03-29 10:34:05 -040094 /** @brief Set the timer source enable state. */
95 void enable(int enable)
96 {
97 src.enable(enable);
98 }
Brad Bishop605662d2017-05-30 12:39:09 -040099
Brad Bishopd1eac882018-03-29 10:34:05 -0400100 /** @brief Query timer state. */
101 auto enabled()
102 {
103 return src.enabled();
104 }
Brad Bishop605662d2017-05-30 12:39:09 -0400105
Brad Bishopd1eac882018-03-29 10:34:05 -0400106 private:
107 source::Source src;
108 std::unique_ptr<Callback> cb = nullptr;
Brad Bishop605662d2017-05-30 12:39:09 -0400109
Brad Bishopd1eac882018-03-29 10:34:05 -0400110 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 Bishop605662d2017-05-30 12:39:09 -0400115
Brad Bishopd1eac882018-03-29 10:34:05 -0400116 return 0;
117 }
Brad Bishop605662d2017-05-30 12:39:09 -0400118};
119} // namespace timer
120} // namespace event
121} // namespace sdevent