blob: 4f837bee0cc7d939726b9c56c70ea8172e58eb9a [file] [log] [blame]
Brad Bishop605662d2017-05-30 12:39:09 -04001#pragma once
2
3#include <chrono>
4#include <memory>
5#include <systemd/sd-event.h>
6
7// TODO: openbmc/openbmc#1720 - add error handling for sd_event API failures
8
9namespace sdevent
10{
11namespace source
12{
13
14using SourcePtr = sd_event_source*;
15
16namespace details
17{
18
19/** @brief unique_ptr functor to release a source reference. */
20struct SourceDeleter
21{
22 void operator()(sd_event_source* ptr) const
23 {
24 deleter(ptr);
25 }
26
27 decltype(&sd_event_source_unref) deleter = sd_event_source_unref;
28};
29
30/* @brief Alias 'source' to a unique_ptr type for auto-release. */
31using source = std::unique_ptr<sd_event_source, SourceDeleter>;
32
33} // namespace details
34
35/** @class Source
36 * @brief Provides C++ bindings to the sd_event_source* functions.
37 */
38class Source
39{
Brad Bishopd1eac882018-03-29 10:34:05 -040040 public:
41 /* Define all of the basic class operations:
42 * Not allowed:
43 * - Default constructor to avoid nullptrs.
44 * - Copy operations due to internal unique_ptr.
45 * Allowed:
46 * - Move operations.
47 * - Destructor.
48 */
49 Source() = delete;
50 Source(const Source&) = delete;
51 Source& operator=(const Source&) = delete;
52 Source(Source&&) = default;
53 Source& operator=(Source&&) = default;
54 ~Source() = default;
Brad Bishop605662d2017-05-30 12:39:09 -040055
Brad Bishopd1eac882018-03-29 10:34:05 -040056 /** @brief Conversion constructor from 'SourcePtr'.
57 *
58 * Increments ref-count of the source-pointer and releases it
59 * when done.
60 */
61 explicit Source(SourcePtr s) : src(sd_event_source_ref(s))
62 {
63 }
Brad Bishop605662d2017-05-30 12:39:09 -040064
Brad Bishopd1eac882018-03-29 10:34:05 -040065 /** @brief Constructor for 'source'.
66 *
67 * Takes ownership of the source-pointer and releases it when done.
68 */
69 Source(SourcePtr s, std::false_type) : src(s)
70 {
71 }
Brad Bishop605662d2017-05-30 12:39:09 -040072
Brad Bishopd1eac882018-03-29 10:34:05 -040073 /** @brief Check if source contains a real pointer. (non-nullptr). */
74 explicit operator bool() const
75 {
76 return bool(src);
77 }
Brad Bishop605662d2017-05-30 12:39:09 -040078
Brad Bishopd1eac882018-03-29 10:34:05 -040079 /** @brief Test whether or not the source can generate events. */
80 auto enabled()
81 {
82 int enabled;
83 sd_event_source_get_enabled(src.get(), &enabled);
84 return enabled;
85 }
Brad Bishop605662d2017-05-30 12:39:09 -040086
Brad Bishopd1eac882018-03-29 10:34:05 -040087 /** @brief Allow the source to generate events. */
88 void enable(int enable)
89 {
90 sd_event_source_set_enabled(src.get(), enable);
91 }
Brad Bishop605662d2017-05-30 12:39:09 -040092
Brad Bishopd1eac882018-03-29 10:34:05 -040093 /** @brief Set the expiration on a timer source. */
94 void setTime(const std::chrono::steady_clock::time_point& expires)
95 {
96 using namespace std::chrono;
97 auto epoch = expires.time_since_epoch();
98 auto time = duration_cast<microseconds>(epoch);
99 sd_event_source_set_time(src.get(), time.count());
100 }
Brad Bishop605662d2017-05-30 12:39:09 -0400101
Brad Bishopd1eac882018-03-29 10:34:05 -0400102 /** @brief Get the expiration on a timer source. */
103 auto getTime()
104 {
105 using namespace std::chrono;
106 uint64_t usec;
107 sd_event_source_get_time(src.get(), &usec);
108 microseconds d(usec);
109 return steady_clock::time_point(d);
110 }
Brad Bishop605662d2017-05-30 12:39:09 -0400111
Brad Bishopd1eac882018-03-29 10:34:05 -0400112 private:
113 details::source src;
Brad Bishop605662d2017-05-30 12:39:09 -0400114};
115} // namespace source
116} // namespace sdevent