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 | |
| 35 | /** @class Source |
| 36 | * @brief Provides C++ bindings to the sd_event_source* functions. |
| 37 | */ |
| 38 | class Source |
| 39 | { |
Brad Bishop | d516c61 | 2017-07-19 16:15:04 -0400 | [diff] [blame] | 40 | private: |
| 41 | using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: |
| 42 | Error::InternalFailure; |
| 43 | |
Brad Bishop | 14631dc | 2017-06-14 22:34:03 -0400 | [diff] [blame] | 44 | 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 Bishop | d516c61 | 2017-07-19 16:15:04 -0400 | [diff] [blame] | 83 | auto rc = sd_event_source_get_enabled(src.get(), &enabled); |
| 84 | if (rc < 0) |
| 85 | { |
| 86 | phosphor::logging::elog<InternalFailure>(); |
| 87 | } |
| 88 | |
Brad Bishop | 14631dc | 2017-06-14 22:34:03 -0400 | [diff] [blame] | 89 | return enabled; |
| 90 | } |
| 91 | |
| 92 | /** @brief Allow the source to generate events. */ |
| 93 | void enable(int enable) |
| 94 | { |
Brad Bishop | d516c61 | 2017-07-19 16:15:04 -0400 | [diff] [blame] | 95 | auto rc = sd_event_source_set_enabled(src.get(), enable); |
| 96 | if (rc < 0) |
| 97 | { |
| 98 | phosphor::logging::elog<InternalFailure>(); |
| 99 | } |
Brad Bishop | 14631dc | 2017-06-14 22:34:03 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | private: |
| 103 | details::source src; |
| 104 | }; |
| 105 | } // namespace source |
| 106 | } // namespace sdevent |