| Brad Bishop | 14631dc | 2017-06-14 22:34:03 -0400 | [diff] [blame] | 1 | #pragma once | 
|  | 2 |  | 
|  | 3 | #include <memory> | 
| Brad Bishop | d516c61 | 2017-07-19 16:15:04 -0400 | [diff] [blame] | 4 | #include <phosphor-logging/elog.hpp> | 
|  | 5 | #include <phosphor-logging/elog-errors.hpp> | 
| Brad Bishop | 14631dc | 2017-06-14 22:34:03 -0400 | [diff] [blame] | 6 | #include <systemd/sd-event.h> | 
| Brad Bishop | d516c61 | 2017-07-19 16:15:04 -0400 | [diff] [blame] | 7 | #include <xyz/openbmc_project/Common/error.hpp> | 
| Brad Bishop | 14631dc | 2017-06-14 22:34:03 -0400 | [diff] [blame] | 8 |  | 
|  | 9 | namespace sdevent | 
|  | 10 | { | 
|  | 11 | namespace source | 
|  | 12 | { | 
|  | 13 |  | 
|  | 14 | using SourcePtr = sd_event_source*; | 
|  | 15 |  | 
|  | 16 | namespace details | 
|  | 17 | { | 
|  | 18 |  | 
|  | 19 | /** @brief unique_ptr functor to release a source reference. */ | 
|  | 20 | struct 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. */ | 
|  | 31 | using source = std::unique_ptr<sd_event_source, SourceDeleter>; | 
|  | 32 |  | 
|  | 33 | } // namespace details | 
|  | 34 |  | 
| Marri Devender Rao | 6088558 | 2017-11-07 04:58:14 -0600 | [diff] [blame] | 35 | using namespace phosphor::logging; | 
|  | 36 |  | 
| Brad Bishop | 14631dc | 2017-06-14 22:34:03 -0400 | [diff] [blame] | 37 | /** @class Source | 
|  | 38 | *  @brief Provides C++ bindings to the sd_event_source* functions. | 
|  | 39 | */ | 
|  | 40 | class Source | 
|  | 41 | { | 
| Brad Bishop | d516c61 | 2017-07-19 16:15:04 -0400 | [diff] [blame] | 42 | private: | 
|  | 43 | using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: | 
|  | 44 | Error::InternalFailure; | 
|  | 45 |  | 
| Brad Bishop | 14631dc | 2017-06-14 22:34:03 -0400 | [diff] [blame] | 46 | 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 Bishop | d516c61 | 2017-07-19 16:15:04 -0400 | [diff] [blame] | 85 | auto rc = sd_event_source_get_enabled(src.get(), &enabled); | 
|  | 86 | if (rc < 0) | 
|  | 87 | { | 
| Marri Devender Rao | 6088558 | 2017-11-07 04:58:14 -0600 | [diff] [blame] | 88 | log<level::ERR>("Error in call to sd_event_source_get_enabled", | 
|  | 89 | entry("RC=%d", rc)); | 
|  | 90 | elog<InternalFailure>(); | 
| Brad Bishop | d516c61 | 2017-07-19 16:15:04 -0400 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
| Brad Bishop | 14631dc | 2017-06-14 22:34:03 -0400 | [diff] [blame] | 93 | return enabled; | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | /** @brief Allow the source to generate events. */ | 
|  | 97 | void enable(int enable) | 
|  | 98 | { | 
| Brad Bishop | d516c61 | 2017-07-19 16:15:04 -0400 | [diff] [blame] | 99 | auto rc = sd_event_source_set_enabled(src.get(), enable); | 
|  | 100 | if (rc < 0) | 
|  | 101 | { | 
| Marri Devender Rao | 6088558 | 2017-11-07 04:58:14 -0600 | [diff] [blame] | 102 | 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 Bishop | d516c61 | 2017-07-19 16:15:04 -0400 | [diff] [blame] | 106 | } | 
| Brad Bishop | 14631dc | 2017-06-14 22:34:03 -0400 | [diff] [blame] | 107 | } | 
|  | 108 |  | 
|  | 109 | private: | 
|  | 110 | details::source src; | 
|  | 111 | }; | 
|  | 112 | } // namespace source | 
|  | 113 | } // namespace sdevent |