blob: e90efd465cd9170e6c95dedfea79ea7b7e652ee2 [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
Marri Devender Rao60885582017-11-07 04:58:14 -060035using namespace phosphor::logging;
36
Brad Bishop14631dc2017-06-14 22:34:03 -040037/** @class Source
38 * @brief Provides C++ bindings to the sd_event_source* functions.
39 */
40class Source
41{
Brad Bishopd516c612017-07-19 16:15:04 -040042 private:
43 using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
44 Error::InternalFailure;
45
Brad Bishop14631dc2017-06-14 22:34:03 -040046 public:
47 /* Define all of the basic class operations:
48 * Not allowed:
49 * - Default constructor to avoid nullptrs.
50 * - Copy operations due to internal unique_ptr.
51 * Allowed:
52 * - Move operations.
53 * - Destructor.
54 */
55 Source() = delete;
56 Source(const Source&) = delete;
57 Source& operator=(const Source&) = delete;
58 Source(Source&&) = default;
59 Source& operator=(Source&&) = default;
60 ~Source() = default;
61
62 /** @brief Conversion constructor from 'SourcePtr'.
63 *
64 * Increments ref-count of the source-pointer and releases it
65 * when done.
66 */
67 explicit Source(SourcePtr s) : src(sd_event_source_ref(s)) {}
68
69 /** @brief Constructor for 'source'.
70 *
71 * Takes ownership of the source-pointer and releases it when done.
72 */
73 Source(SourcePtr s, std::false_type) : src(s) {}
74
75 /** @brief Check if source contains a real pointer. (non-nullptr). */
76 explicit operator bool() const
77 {
78 return bool(src);
79 }
80
81 /** @brief Test whether or not the source can generate events. */
82 auto enabled()
83 {
84 int enabled;
Brad Bishopd516c612017-07-19 16:15:04 -040085 auto rc = sd_event_source_get_enabled(src.get(), &enabled);
86 if (rc < 0)
87 {
Marri Devender Rao60885582017-11-07 04:58:14 -060088 log<level::ERR>("Error in call to sd_event_source_get_enabled",
89 entry("RC=%d", rc));
90 elog<InternalFailure>();
Brad Bishopd516c612017-07-19 16:15:04 -040091 }
92
Brad Bishop14631dc2017-06-14 22:34:03 -040093 return enabled;
94 }
95
96 /** @brief Allow the source to generate events. */
97 void enable(int enable)
98 {
Brad Bishopd516c612017-07-19 16:15:04 -040099 auto rc = sd_event_source_set_enabled(src.get(), enable);
100 if (rc < 0)
101 {
Marri Devender Rao60885582017-11-07 04:58:14 -0600102 log<level::ERR>("Error in call to sd_event_source_set_enabled",
103 entry("RC=%d", rc),
104 entry("ENABLE=%d", enable));
105 elog<InternalFailure>();
Brad Bishopd516c612017-07-19 16:15:04 -0400106 }
Brad Bishop14631dc2017-06-14 22:34:03 -0400107 }
108
109 private:
110 details::source src;
111};
112} // namespace source
113} // namespace sdevent