Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <chrono> |
| 4 | #include <memory> |
| 5 | #include <sdbusplus/bus.hpp> |
| 6 | #include <systemd/sd-event.h> |
| 7 | |
| 8 | // TODO: openbmc/openbmc#1720 - add error handling for sd_event API failures |
| 9 | |
| 10 | namespace sdevent |
| 11 | { |
| 12 | namespace event |
| 13 | { |
| 14 | namespace timer |
| 15 | { |
| 16 | class Timer; |
| 17 | } // namespace timer |
| 18 | } |
| 19 | namespace event |
| 20 | { |
| 21 | |
| 22 | using EventPtr = sd_event*; |
| 23 | class Event; |
| 24 | |
| 25 | /** @brief Get an instance of the 'default' event. */ |
| 26 | Event newDefault(); |
| 27 | |
| 28 | namespace details |
| 29 | { |
| 30 | |
| 31 | /** @brief unique_ptr functor to release an event reference. */ |
| 32 | struct EventDeleter |
| 33 | { |
| 34 | void operator()(sd_event* ptr) const |
| 35 | { |
| 36 | deleter(ptr); |
| 37 | } |
| 38 | |
| 39 | decltype(&sd_event_unref) deleter = sd_event_unref; |
| 40 | }; |
| 41 | |
| 42 | /* @brief Alias 'event' to a unique_ptr type for auto-release. */ |
| 43 | using Event = std::unique_ptr<sd_event, EventDeleter>; |
| 44 | |
| 45 | } // namespace details |
| 46 | |
| 47 | /** @class Event |
| 48 | * @brief Provides C++ bindings to the sd_event_* class functions. |
| 49 | */ |
| 50 | class Event |
| 51 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 52 | public: |
| 53 | /* Define all of the basic class operations: |
| 54 | * Not allowed: |
| 55 | * - Default constructor to avoid nullptrs. |
| 56 | * - Copy operations due to internal unique_ptr. |
| 57 | * Allowed: |
| 58 | * - Move operations. |
| 59 | * - Destructor. |
| 60 | */ |
| 61 | Event() = delete; |
| 62 | Event(const Event&) = delete; |
| 63 | Event& operator=(const Event&) = delete; |
| 64 | Event(Event&&) = default; |
| 65 | Event& operator=(Event&&) = default; |
| 66 | ~Event() = default; |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 67 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 68 | /** @brief Conversion constructor from 'EventPtr'. |
| 69 | * |
| 70 | * Increments ref-count of the event-pointer and releases it when |
| 71 | * done. |
| 72 | */ |
| 73 | explicit Event(EventPtr e); |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 74 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 75 | /** @brief Constructor for 'Event'. |
| 76 | * |
| 77 | * Takes ownership of the event-pointer and releases it when done. |
| 78 | */ |
| 79 | Event(EventPtr e, std::false_type); |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 80 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 81 | /** @brief Release ownership of the stored event-pointer. */ |
| 82 | EventPtr release() |
| 83 | { |
| 84 | return evt.release(); |
| 85 | } |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 86 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 87 | /** @brief Wait indefinitely for new event sources. */ |
| 88 | void loop() |
| 89 | { |
| 90 | sd_event_loop(evt.get()); |
| 91 | } |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 92 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 93 | /** @brief Attach to a DBus loop. */ |
| 94 | void attach(sdbusplus::bus::bus& bus) |
| 95 | { |
| 96 | bus.attach_event(evt.get(), SD_EVENT_PRIORITY_NORMAL); |
| 97 | } |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 98 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 99 | /** @brief C++ wrapper for sd_event_now. */ |
| 100 | auto now() |
| 101 | { |
| 102 | using namespace std::chrono; |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 103 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 104 | uint64_t usec; |
| 105 | sd_event_now(evt.get(), CLOCK_MONOTONIC, &usec); |
| 106 | microseconds d(usec); |
| 107 | return steady_clock::time_point(d); |
| 108 | } |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 109 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 110 | friend class timer::Timer; |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 111 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 112 | private: |
| 113 | EventPtr get() |
| 114 | { |
| 115 | return evt.get(); |
| 116 | } |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 117 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 118 | details::Event evt; |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | inline Event::Event(EventPtr l) : evt(sd_event_ref(l)) |
| 122 | { |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | inline Event::Event(EventPtr l, std::false_type) : evt(l) |
| 126 | { |
Brad Bishop | 605662d | 2017-05-30 12:39:09 -0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | inline Event newDefault() |
| 130 | { |
| 131 | sd_event* e = nullptr; |
| 132 | sd_event_default(&e); |
| 133 | return Event(e, std::false_type()); |
| 134 | } |
| 135 | |
| 136 | } // namespace event |
| 137 | } // namespace sdevent |