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