blob: 812f0a302c8ad9773104e01672ada28eed933e33 [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{
40 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;
55
56 /** @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 /** @brief Constructor for 'source'.
64 *
65 * Takes ownership of the source-pointer and releases it when done.
66 */
67 Source(SourcePtr s, std::false_type) : src(s) {}
68
69 /** @brief Check if source contains a real pointer. (non-nullptr). */
70 explicit operator bool() const
71 {
72 return bool(src);
73 }
74
75 /** @brief Test whether or not the source can generate events. */
76 auto enabled()
77 {
78 int enabled;
79 sd_event_source_get_enabled(src.get(), &enabled);
80 return enabled;
81 }
82
83 /** @brief Allow the source to generate events. */
84 void enable(int enable)
85 {
86 sd_event_source_set_enabled(src.get(), enable);
87 }
88
89 /** @brief Set the expiration on a timer source. */
90 void setTime(
91 const std::chrono::steady_clock::time_point& expires)
92 {
93 using namespace std::chrono;
94 auto epoch = expires.time_since_epoch();
95 auto time = duration_cast<microseconds>(epoch);
96 sd_event_source_set_time(src.get(), time.count());
97 }
98
99 /** @brief Get the expiration on a timer source. */
100 auto getTime()
101 {
102 using namespace std::chrono;
103 uint64_t usec;
104 sd_event_source_get_time(src.get(), &usec);
105 microseconds d(usec);
106 return steady_clock::time_point(d);
107 }
108
109 private:
110 details::source src;
111};
112} // namespace source
113} // namespace sdevent