diff options
-rw-r--r-- | sdevent/event.hpp | 131 | ||||
-rw-r--r-- | sdevent/source.hpp | 92 |
2 files changed, 223 insertions, 0 deletions
diff --git a/sdevent/event.hpp b/sdevent/event.hpp new file mode 100644 index 0000000..ac9a13d --- /dev/null +++ b/sdevent/event.hpp @@ -0,0 +1,131 @@ +#pragma once + +#include <chrono> +#include <memory> +#include <sdbusplus/bus.hpp> +#include <systemd/sd-event.h> + +// TODO: openbmc/openbmc#1720 - add error handling for sd_event API failures + +namespace sdevent +{ +namespace event +{ + +using EventPtr = sd_event*; +class Event; + +/** @brief Get an instance of the 'default' event. */ +Event newDefault(); + +namespace details +{ + +/** @brief unique_ptr functor to release an event reference. */ +struct EventDeleter +{ + void operator()(sd_event* ptr) const + { + deleter(ptr); + } + + decltype(&sd_event_unref) deleter = sd_event_unref; +}; + +/* @brief Alias 'event' to a unique_ptr type for auto-release. */ +using Event = std::unique_ptr<sd_event, EventDeleter>; + +} // namespace details + +/** @class Event + * @brief Provides C++ bindings to the sd_event_* class functions. + */ +class Event +{ + 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. + */ + Event() = delete; + Event(const Event&) = delete; + Event& operator=(const Event&) = delete; + Event(Event&&) = default; + Event& operator=(Event&&) = default; + ~Event() = default; + + /** @brief Conversion constructor from 'EventPtr'. + * + * Increments ref-count of the event-pointer and releases it when + * done. + */ + explicit Event(EventPtr e); + + /** @brief Constructor for 'Event'. + * + * Takes ownership of the event-pointer and releases it when done. + */ + Event(EventPtr e, std::false_type); + + /** @brief Release ownership of the stored event-pointer. */ + EventPtr release() + { + return evt.release(); + } + + /** @brief Wait indefinitely for new event sources. */ + void loop() + { + sd_event_loop(evt.get()); + } + + /** @brief Attach to a DBus loop. */ + void attach(sdbusplus::bus::bus& bus) + { + bus.attach_event(evt.get(), SD_EVENT_PRIORITY_NORMAL); + } + + /** @brief C++ wrapper for sd_event_now. */ + auto now() + { + using namespace std::chrono; + + uint64_t usec; + sd_event_now(evt.get(), CLOCK_MONOTONIC, &usec); + microseconds d(usec); + return steady_clock::time_point(d); + } + + private: + + EventPtr get() + { + return evt.get(); + } + + details::Event evt; +}; + +inline Event::Event(EventPtr l) : evt(sd_event_ref(l)) +{ + +} + +inline Event::Event(EventPtr l, std::false_type) : evt(l) +{ + +} + +inline Event newDefault() +{ + sd_event* e = nullptr; + sd_event_default(&e); + return Event(e, std::false_type()); +} + +} // namespace event +} // namespace sdevent diff --git a/sdevent/source.hpp b/sdevent/source.hpp new file mode 100644 index 0000000..e9e2a0c --- /dev/null +++ b/sdevent/source.hpp @@ -0,0 +1,92 @@ +#pragma once + +#include <memory> +#include <systemd/sd-event.h> + +// TODO: openbmc/openbmc#1720 - add error handling for sd_event API failures + +namespace sdevent +{ +namespace source +{ + +using SourcePtr = sd_event_source*; + +namespace details +{ + +/** @brief unique_ptr functor to release a source reference. */ +struct SourceDeleter +{ + void operator()(sd_event_source* ptr) const + { + deleter(ptr); + } + + decltype(&sd_event_source_unref) deleter = sd_event_source_unref; +}; + +/* @brief Alias 'source' to a unique_ptr type for auto-release. */ +using source = std::unique_ptr<sd_event_source, SourceDeleter>; + +} // namespace details + +/** @class Source + * @brief Provides C++ bindings to the sd_event_source* functions. + */ +class Source +{ + 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. + */ + Source() = delete; + Source(const Source&) = delete; + Source& operator=(const Source&) = delete; + Source(Source&&) = default; + Source& operator=(Source&&) = default; + ~Source() = default; + + /** @brief Conversion constructor from 'SourcePtr'. + * + * Increments ref-count of the source-pointer and releases it + * when done. + */ + explicit Source(SourcePtr s) : src(sd_event_source_ref(s)) {} + + /** @brief Constructor for 'source'. + * + * Takes ownership of the source-pointer and releases it when done. + */ + Source(SourcePtr s, std::false_type) : src(s) {} + + /** @brief Check if source contains a real pointer. (non-nullptr). */ + explicit operator bool() const + { + return bool(src); + } + + /** @brief Test whether or not the source can generate events. */ + auto enabled() + { + int enabled; + sd_event_source_get_enabled(src.get(), &enabled); + return enabled; + } + + /** @brief Allow the source to generate events. */ + void enable(int enable) + { + sd_event_source_set_enabled(src.get(), enable); + } + + private: + details::source src; +}; +} // namespace source +} // namespace sdevent |