blob: be3a8925fd54dbe2b6c21019fe3690fe1ab27c69 [file] [log] [blame]
Brad Bishop14631dc2017-06-14 22:34:03 -04001#pragma once
2
3#include <memory>
Brad Bishopd516c612017-07-19 16:15:04 -04004#include <phosphor-logging/elog.hpp>
5#include <phosphor-logging/elog-errors.hpp>
Brad Bishop14631dc2017-06-14 22:34:03 -04006#include <systemd/sd-event.h>
Brad Bishopd516c612017-07-19 16:15:04 -04007#include <xyz/openbmc_project/Common/error.hpp>
Brad Bishop14631dc2017-06-14 22:34:03 -04008
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 Bishopd516c612017-07-19 16:15:04 -040040 private:
41 using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
42 Error::InternalFailure;
43
Brad Bishop14631dc2017-06-14 22:34:03 -040044 public:
45 /* Define all of the basic class operations:
46 * Not allowed:
47 * - Default constructor to avoid nullptrs.
48 * - Copy operations due to internal unique_ptr.
49 * Allowed:
50 * - Move operations.
51 * - Destructor.
52 */
53 Source() = delete;
54 Source(const Source&) = delete;
55 Source& operator=(const Source&) = delete;
56 Source(Source&&) = default;
57 Source& operator=(Source&&) = default;
58 ~Source() = default;
59
60 /** @brief Conversion constructor from 'SourcePtr'.
61 *
62 * Increments ref-count of the source-pointer and releases it
63 * when done.
64 */
65 explicit Source(SourcePtr s) : src(sd_event_source_ref(s)) {}
66
67 /** @brief Constructor for 'source'.
68 *
69 * Takes ownership of the source-pointer and releases it when done.
70 */
71 Source(SourcePtr s, std::false_type) : src(s) {}
72
73 /** @brief Check if source contains a real pointer. (non-nullptr). */
74 explicit operator bool() const
75 {
76 return bool(src);
77 }
78
79 /** @brief Test whether or not the source can generate events. */
80 auto enabled()
81 {
82 int enabled;
Brad Bishopd516c612017-07-19 16:15:04 -040083 auto rc = sd_event_source_get_enabled(src.get(), &enabled);
84 if (rc < 0)
85 {
86 phosphor::logging::elog<InternalFailure>();
87 }
88
Brad Bishop14631dc2017-06-14 22:34:03 -040089 return enabled;
90 }
91
92 /** @brief Allow the source to generate events. */
93 void enable(int enable)
94 {
Brad Bishopd516c612017-07-19 16:15:04 -040095 auto rc = sd_event_source_set_enabled(src.get(), enable);
96 if (rc < 0)
97 {
98 phosphor::logging::elog<InternalFailure>();
99 }
Brad Bishop14631dc2017-06-14 22:34:03 -0400100 }
101
102 private:
103 details::source src;
104};
105} // namespace source
106} // namespace sdevent