summaryrefslogtreecommitdiffstats
path: root/src/sdevent/timer.hpp
diff options
context:
space:
mode:
authorBrad Bishop <bradleyb@fuzziesquirrel.com>2017-05-30 12:39:09 -0400
committerBrad Bishop <bradleyb@fuzziesquirrel.com>2017-06-19 16:26:48 -0400
commit605662db616fd1b7c8874e86186f7a39c3e63a39 (patch)
tree10ddff6af04a746371f8be100c76f84e4f9596f8 /src/sdevent/timer.hpp
parentf67884e44b3b0a9cb9ed109d0bc754df6d00b032 (diff)
downloadphosphor-dbus-monitor-605662db616fd1b7c8874e86186f7a39c3e63a39.tar.gz
phosphor-dbus-monitor-605662db616fd1b7c8874e86186f7a39c3e63a39.zip
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
Diffstat (limited to 'src/sdevent/timer.hpp')
-rw-r--r--src/sdevent/timer.hpp139
1 files changed, 139 insertions, 0 deletions
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
OpenPOWER on IntegriCloud