diff options
author | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2017-06-14 23:10:37 -0400 |
---|---|---|
committer | Patrick Williams <patrick@stwcx.xyz> | 2017-08-02 20:18:18 +0000 |
commit | e54a98f42006d3298fcfae34c52b70fb70245431 (patch) | |
tree | c48a745c655a8b639b57b551d508363dcd6daa52 /sdevent | |
parent | 4253fcaa5af69241ddc06ae72d3f94fd3b8818c9 (diff) | |
download | phosphor-fan-presence-e54a98f42006d3298fcfae34c52b70fb70245431.tar.gz phosphor-fan-presence-e54a98f42006d3298fcfae34c52b70fb70245431.zip |
Add sdevent io source C++ wrappers
Change-Id: Ifadee2bb8788be87e759d1f0be875f7047a2f37e
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
Diffstat (limited to 'sdevent')
-rw-r--r-- | sdevent/event.hpp | 6 | ||||
-rw-r--r-- | sdevent/io.hpp | 104 |
2 files changed, 110 insertions, 0 deletions
diff --git a/sdevent/event.hpp b/sdevent/event.hpp index ac9a13d..3e97ce7 100644 --- a/sdevent/event.hpp +++ b/sdevent/event.hpp @@ -11,6 +11,10 @@ namespace sdevent { namespace event { +namespace io +{ +class IO; +} // namespace io using EventPtr = sd_event*; class Event; @@ -100,6 +104,8 @@ class Event return steady_clock::time_point(d); } + friend class io::IO; + private: EventPtr get() diff --git a/sdevent/io.hpp b/sdevent/io.hpp new file mode 100644 index 0000000..93625a0 --- /dev/null +++ b/sdevent/io.hpp @@ -0,0 +1,104 @@ +#pragma once + +#include <functional> +#include <memory> +#include <phosphor-logging/elog.hpp> +#include <phosphor-logging/elog-errors.hpp> +#include <systemd/sd-event.h> +#include <xyz/openbmc_project/Common/error.hpp> + +#include "sdevent/source.hpp" +#include "sdevent/event.hpp" + +namespace sdevent +{ +namespace event +{ +namespace io +{ + +/** @class IO + * @brief Provides C++ bindings to the sd_event_source* io functions. + */ +class IO +{ + private: + using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: + Error::InternalFailure; + + public: + /* Define all of the basic class operations: + * Not allowed: + * - Default constructor to avoid nullptrs. + * - Copy operations due to internal unique_ptr. + * Allowed: + * - Move operations. + * - Destructor. + */ + IO() = delete; + IO(const IO&) = delete; + IO& operator=(const IO&) = delete; + IO(IO&&) = default; + IO& operator=(IO&&) = default; + ~IO() = default; + + using Callback = std::function<void(source::Source&)>; + + /** @brief Register an io callback. + * + * @param[in] event - The event to register on. + * @param[in] fd - The file descriptor to poll. + * @param[in] callback - The callback method. + */ + IO( + sdevent::event::Event& event, + int fd, + Callback callback) + : src(nullptr), + cb(std::make_unique<Callback>(std::move(callback))) + { + sd_event_source* source = nullptr; + auto rc = sd_event_add_io( + event.get(), + &source, + fd, + EPOLLIN, + callCallback, + cb.get()); + if (rc < 0) + { + phosphor::logging::elog<InternalFailure>(); + } + + src = decltype(src){source, std::false_type()}; + } + + /** @brief Set the IO source enable state. */ + void enable(int enable) + { + src.enable(enable); + } + + /** @brief Query the IO enable state. */ + auto enabled() + { + return src.enabled(); + } + + private: + source::Source src; + std::unique_ptr<Callback> cb = nullptr; + + static int callCallback(sd_event_source* s, int fd, uint32_t events, + void* context) + { + source::Source source(s); + auto c = static_cast<Callback*>(context); + (*c)(source); + + return 0; + } +}; +} // namespace io +} // namespace event +} // namespace sdevent |