Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <memory> |
Brad Bishop | 887ebf6 | 2016-10-12 15:40:04 -0400 | [diff] [blame] | 4 | #include <climits> |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 5 | #include <systemd/sd-bus.h> |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame] | 6 | #include <sdbusplus/message.hpp> |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 7 | |
| 8 | namespace sdbusplus |
| 9 | { |
Patrick Williams | 13f1ef7 | 2016-10-17 14:09:33 -0500 | [diff] [blame] | 10 | |
| 11 | // Forward declare. |
| 12 | namespace server { namespace interface { struct interface; } } |
Patrick Williams | 05aab8b | 2016-10-17 14:12:12 -0500 | [diff] [blame] | 13 | namespace server { namespace manager { struct manager; } } |
Patrick Williams | d1102f4 | 2016-10-17 21:42:38 -0500 | [diff] [blame] | 14 | namespace server { namespace object { template<class...> struct object; } } |
Patrick Williams | 13f1ef7 | 2016-10-17 14:09:33 -0500 | [diff] [blame] | 15 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 16 | namespace bus |
| 17 | { |
| 18 | |
| 19 | using busp_t = sd_bus*; |
| 20 | class bus; |
| 21 | |
| 22 | /** @brief Get an instance of the 'default' bus. */ |
| 23 | bus new_default(); |
| 24 | /** @brief Get an instance of the 'user' session bus. */ |
| 25 | bus new_user(); |
| 26 | /** @brief Get an instance of the 'system' bus. */ |
| 27 | bus new_system(); |
| 28 | |
| 29 | namespace details |
| 30 | { |
| 31 | |
| 32 | /** @brief unique_ptr functor to release a bus reference. */ |
| 33 | struct BusDeleter |
| 34 | { |
| 35 | void operator()(sd_bus* ptr) const |
| 36 | { |
Patrick Williams | 67df0f2 | 2016-10-16 17:58:03 -0500 | [diff] [blame] | 37 | sd_bus_unref(ptr); |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 38 | } |
| 39 | }; |
| 40 | |
| 41 | /* @brief Alias 'bus' to a unique_ptr type for auto-release. */ |
| 42 | using bus = std::unique_ptr<sd_bus, BusDeleter>; |
| 43 | |
| 44 | } // namespace details |
| 45 | |
| 46 | /** @class bus |
| 47 | * @brief Provides C++ bindings to the sd_bus_* class functions. |
| 48 | */ |
| 49 | struct bus |
| 50 | { |
| 51 | /* Define all of the basic class operations: |
| 52 | * Not allowed: |
| 53 | * - Default constructor to avoid nullptrs. |
| 54 | * - Copy operations due to internal unique_ptr. |
| 55 | * Allowed: |
| 56 | * - Move operations. |
| 57 | * - Destructor. |
| 58 | */ |
| 59 | bus() = delete; |
| 60 | bus(const bus&) = delete; |
| 61 | bus& operator=(const bus&) = delete; |
| 62 | bus(bus&&) = default; |
| 63 | bus& operator=(bus&&) = default; |
| 64 | ~bus() = default; |
| 65 | |
| 66 | /** @brief Conversion constructor from 'busp_t'. |
| 67 | * |
| 68 | * Takes ownership of the bus-pointer and releases it when done. |
| 69 | */ |
| 70 | explicit bus(busp_t b) : _bus(b) {} |
| 71 | |
| 72 | /** @brief Release ownership of the stored bus-pointer. */ |
| 73 | busp_t release() { return _bus.release(); } |
| 74 | |
| 75 | /** @brief Wait for new dbus messages or signals. |
| 76 | * |
| 77 | * @param[in] timeout_us - Timeout in usec. |
| 78 | */ |
Brad Bishop | 887ebf6 | 2016-10-12 15:40:04 -0400 | [diff] [blame] | 79 | void wait(uint64_t timeout_us = ULLONG_MAX) |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 80 | { |
| 81 | sd_bus_wait(_bus.get(), timeout_us); |
| 82 | } |
| 83 | |
| 84 | /** @brief Process waiting dbus messages or signals. */ |
| 85 | auto process() |
| 86 | { |
| 87 | sd_bus_message* m = nullptr; |
| 88 | sd_bus_process(_bus.get(), &m); |
| 89 | |
Patrick Williams | 13b97c5 | 2016-09-09 09:36:55 -0500 | [diff] [blame] | 90 | return message::message(m); |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 91 | } |
| 92 | |
Patrick Williams | c7ba66f | 2016-10-18 11:30:07 -0500 | [diff] [blame^] | 93 | /** @brief Process waiting dbus messages or signals, discarding unhandled. |
| 94 | */ |
| 95 | void process_discard() |
| 96 | { |
| 97 | sd_bus_process(_bus.get(), nullptr); |
| 98 | } |
| 99 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 100 | /** @brief Claim a service name on the dbus. |
| 101 | * |
| 102 | * @param[in] service - The service name to claim. |
| 103 | */ |
| 104 | void request_name(const char* service) |
| 105 | { |
| 106 | sd_bus_request_name(_bus.get(), service, 0); |
| 107 | } |
| 108 | |
| 109 | /** @brief Create a method_call message. |
| 110 | * |
| 111 | * @param[in] service - The service to call. |
| 112 | * @param[in] objpath - The object's path for the call. |
| 113 | * @param[in] interf - The object's interface to call. |
| 114 | * @param[in] method - The object's method to call. |
| 115 | * |
| 116 | * @return A newly constructed message. |
| 117 | */ |
| 118 | auto new_method_call(const char* service, const char* objpath, |
| 119 | const char* interf, const char* method) |
| 120 | { |
| 121 | sd_bus_message* m = nullptr; |
| 122 | sd_bus_message_new_method_call(_bus.get(), &m, service, objpath, |
| 123 | interf, method); |
| 124 | |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame] | 125 | return message::message(m); |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 126 | } |
| 127 | |
Patrick Williams | 20d8246 | 2016-10-18 08:01:33 -0500 | [diff] [blame] | 128 | /** @brief Create a signal message. |
| 129 | * |
| 130 | * @param[in] objpath - The object's path for the signal. |
| 131 | * @param[in] interf - The object's interface for the signal. |
| 132 | * @param[in] member - The signal name. |
| 133 | * |
| 134 | * @return A newly constructed message. |
| 135 | */ |
| 136 | auto new_signal(const char* objpath, const char* interf, const char* member) |
| 137 | { |
| 138 | sd_bus_message* m = nullptr; |
| 139 | sd_bus_message_new_signal(_bus.get(), &m, objpath, interf, member); |
| 140 | |
| 141 | return message::message(m); |
| 142 | } |
| 143 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 144 | /** @brief Perform a message call. |
| 145 | * |
| 146 | * @param[in] m - The method_call message. |
| 147 | * @param[in] timeout_us - The timeout for the method call. |
| 148 | * |
| 149 | * @return The response message. |
| 150 | */ |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame] | 151 | auto call(message::message& m, uint64_t timeout_us = 0) |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 152 | { |
| 153 | sd_bus_message* reply = nullptr; |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame] | 154 | sd_bus_call(_bus.get(), m.get(), timeout_us, nullptr, &reply); |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 155 | |
| 156 | return reply; |
| 157 | } |
| 158 | |
| 159 | /** @brief Perform a message call, ignoring the reply. |
| 160 | * |
| 161 | * @param[in] m - The method_call message. |
| 162 | * @param[in] timeout_us - The timeout for the method call. |
| 163 | */ |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame] | 164 | void call_noreply(message::message& m, uint64_t timeout_us = 0) |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 165 | { |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame] | 166 | sd_bus_call(_bus.get(), m.get(), timeout_us, nullptr, nullptr); |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 167 | } |
| 168 | |
Patrick Williams | 13f1ef7 | 2016-10-17 14:09:33 -0500 | [diff] [blame] | 169 | friend struct server::interface::interface; |
Patrick Williams | 05aab8b | 2016-10-17 14:12:12 -0500 | [diff] [blame] | 170 | friend struct server::manager::manager; |
Patrick Williams | d1102f4 | 2016-10-17 21:42:38 -0500 | [diff] [blame] | 171 | template<class... Args> friend struct server::object::object; |
Patrick Williams | 13f1ef7 | 2016-10-17 14:09:33 -0500 | [diff] [blame] | 172 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 173 | private: |
Patrick Williams | 13f1ef7 | 2016-10-17 14:09:33 -0500 | [diff] [blame] | 174 | busp_t get() { return _bus.get(); } |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 175 | details::bus _bus; |
| 176 | }; |
| 177 | |
Brad Bishop | aed8179 | 2016-10-12 08:40:34 -0400 | [diff] [blame] | 178 | inline bus new_default() |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 179 | { |
| 180 | sd_bus* b = nullptr; |
| 181 | sd_bus_open(&b); |
| 182 | return bus(b); |
| 183 | } |
| 184 | |
Brad Bishop | aed8179 | 2016-10-12 08:40:34 -0400 | [diff] [blame] | 185 | inline bus new_user() |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 186 | { |
| 187 | sd_bus* b = nullptr; |
| 188 | sd_bus_open_user(&b); |
| 189 | return bus(b); |
| 190 | } |
| 191 | |
Brad Bishop | aed8179 | 2016-10-12 08:40:34 -0400 | [diff] [blame] | 192 | inline bus new_system() |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 193 | { |
| 194 | sd_bus* b = nullptr; |
| 195 | sd_bus_open_system(&b); |
| 196 | return bus(b); |
| 197 | } |
| 198 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 199 | } // namespace bus |
| 200 | |
| 201 | } // namespace sdbusplus |