Add sdevent C++ wrappers

Add C++ wrappers around select sd-event APIs, for a more C++ like
programming API.

Includes support for core sd_event and timer related sd_event_source
APIs.

Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
Change-Id: I10ac5406c9f8831b4efb294d3aef36026469d4a5
diff --git a/src/sdevent/event.hpp b/src/sdevent/event.hpp
new file mode 100644
index 0000000..5462202
--- /dev/null
+++ b/src/sdevent/event.hpp
@@ -0,0 +1,140 @@
+#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
+{
+namespace timer
+{
+class Timer;
+} // namespace timer
+}
+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);
+        }
+
+        friend class timer::Timer;
+
+    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/src/sdevent/source.hpp b/src/sdevent/source.hpp
new file mode 100644
index 0000000..812f0a3
--- /dev/null
+++ b/src/sdevent/source.hpp
@@ -0,0 +1,113 @@
+#pragma once
+
+#include <chrono>
+#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);
+        }
+
+        /** @brief Set the expiration on a timer source. */
+        void setTime(
+            const std::chrono::steady_clock::time_point& expires)
+        {
+            using namespace std::chrono;
+            auto epoch = expires.time_since_epoch();
+            auto time = duration_cast<microseconds>(epoch);
+            sd_event_source_set_time(src.get(), time.count());
+        }
+
+        /** @brief Get the expiration on a timer source. */
+        auto getTime()
+        {
+            using namespace std::chrono;
+            uint64_t usec;
+            sd_event_source_get_time(src.get(), &usec);
+            microseconds d(usec);
+            return steady_clock::time_point(d);
+        }
+
+    private:
+        details::source src;
+};
+} // namespace source
+} // namespace sdevent
diff --git a/src/sdevent/timer.hpp b/src/sdevent/timer.hpp
new file mode 100644
index 0000000..863e725
--- /dev/null
+++ b/src/sdevent/timer.hpp
@@ -0,0 +1,139 @@
+#pragma once
+
+#include <chrono>
+#include <functional>
+#include <memory>
+#include <systemd/sd-event.h>
+
+#include "sdevent/source.hpp"
+#include "sdevent/event.hpp"
+
+// TODO: openbmc/openbmc#1720 - add error handling for sd_event API failures
+
+namespace sdevent
+{
+namespace event
+{
+namespace timer
+{
+
+/** @class Timer
+ *  @brief Provides C++ bindings to the sd_event_source* timer functions.
+ */
+class Timer
+{
+    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.
+         */
+        Timer() = delete;
+        Timer(const Timer&) = delete;
+        Timer& operator=(const Timer&) = delete;
+        Timer(Timer&&) = default;
+        Timer& operator=(Timer&&) = default;
+        ~Timer() = default;
+
+        using Callback = std::function<void(source::Source&)>;
+
+        /** @brief Register a timer callback.
+         *
+         *  @param[in] event - The event to register on.
+         *  @param[in] expires - The initial timer expiration time.
+         *  @param[in] callback - The callback method.
+         */
+        Timer(
+            sdevent::event::Event& event,
+            const std::chrono::steady_clock::time_point& expires,
+            Callback callback)
+            : src(nullptr),
+              cb(std::make_unique<Callback>(std::move(callback)))
+        {
+            using namespace std::chrono;
+            auto epoch = expires.time_since_epoch();
+            auto time = duration_cast<microseconds>(epoch);
+            sd_event_source* source = nullptr;
+            sd_event_add_time(
+                event.get(),
+                &source,
+                CLOCK_MONOTONIC,
+                time.count(),
+                0,
+                callCallback,
+                cb.get());
+// **INDENT-OFF**
+            src = decltype(src){source, std::false_type()};
+// **INDENT-ON**
+        }
+
+        /** @brief Register a disabled timer callback.
+         *
+         *  @param[in] event - The event to register on.
+         *  @param[in] callback - The callback method.
+         */
+        Timer(
+            sdevent::event::Event& event,
+            Callback callback)
+            : src(nullptr),
+              cb(std::make_unique<Callback>(std::move(callback)))
+        {
+            sd_event_source* source = nullptr;
+            sd_event_add_time(
+                event.get(),
+                &source,
+                CLOCK_MONOTONIC,
+                ULLONG_MAX,
+                0,
+                callCallback,
+                cb.get());
+// **INDENT-OFF**
+            src = decltype(src){source, std::false_type()};
+// **INDENT-ON**
+        }
+
+        /** @brief Set the timer expiration time. */
+        void setTime(
+            const std::chrono::steady_clock::time_point& expires)
+        {
+            src.setTime(expires);
+        }
+
+        /** @brief Get the timer expiration time. */
+        auto getTime()
+        {
+            return src.getTime();
+        }
+
+        /** @brief Set the timer source enable state. */
+        void enable(int enable)
+        {
+            src.enable(enable);
+        }
+
+        /** @brief Query timer state. */
+        auto enabled()
+        {
+            return src.enabled();
+        }
+
+    private:
+        source::Source src;
+        std::unique_ptr<Callback> cb = nullptr;
+
+        static int callCallback(sd_event_source* s, uint64_t usec,
+                                void* context)
+        {
+            source::Source source(s);
+            auto c = static_cast<Callback*>(context);
+            (*c)(source);
+
+            return 0;
+        }
+};
+} // namespace timer
+} // namespace event
+} // namespace sdevent