Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Brad Bishop | b4cc7d7 | 2018-12-06 15:27:48 -0500 | [diff] [blame] | 3 | #include <systemd/sd-bus.h> |
| 4 | #include <systemd/sd-event.h> |
| 5 | |
Patrick Williams | 127b8ab | 2020-05-21 15:24:19 -0500 | [diff] [blame] | 6 | #include <sdbusplus/exception.hpp> |
| 7 | #include <sdbusplus/message.hpp> |
| 8 | #include <sdbusplus/sdbus.hpp> |
| 9 | |
Brad Bishop | d294557 | 2017-02-15 23:40:15 -0500 | [diff] [blame] | 10 | #include <algorithm> |
Brad Bishop | 887ebf6 | 2016-10-12 15:40:04 -0400 | [diff] [blame] | 11 | #include <climits> |
Patrick Venture | 2ea15b7 | 2018-06-22 08:41:18 -0700 | [diff] [blame] | 12 | #include <memory> |
William A. Kennington III | b5a8446 | 2019-03-06 16:46:57 -0800 | [diff] [blame] | 13 | #include <optional> |
Patrick Venture | 2ea15b7 | 2018-06-22 08:41:18 -0700 | [diff] [blame] | 14 | #include <string> |
Patrick Venture | 2ea15b7 | 2018-06-22 08:41:18 -0700 | [diff] [blame] | 15 | #include <vector> |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 16 | |
Patrick Williams | 0c8136a | 2021-08-28 14:42:31 -0500 | [diff] [blame] | 17 | #ifdef __clang__ |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wc99-extensions" |
| 20 | #endif |
| 21 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 22 | namespace sdbusplus |
| 23 | { |
Patrick Williams | 13f1ef7 | 2016-10-17 14:09:33 -0500 | [diff] [blame] | 24 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 25 | namespace bus |
| 26 | { |
| 27 | |
| 28 | using busp_t = sd_bus*; |
Ed Tanous | 8cd7a4a | 2019-03-13 16:41:49 -0700 | [diff] [blame] | 29 | struct bus; |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 30 | |
| 31 | /** @brief Get an instance of the 'default' bus. */ |
| 32 | bus new_default(); |
| 33 | /** @brief Get an instance of the 'user' session bus. */ |
| 34 | bus new_user(); |
| 35 | /** @brief Get an instance of the 'system' bus. */ |
| 36 | bus new_system(); |
| 37 | |
| 38 | namespace details |
| 39 | { |
| 40 | |
| 41 | /** @brief unique_ptr functor to release a bus reference. */ |
| 42 | struct BusDeleter |
| 43 | { |
Brad Bishop | 7583460 | 2018-12-11 08:42:53 -0500 | [diff] [blame] | 44 | BusDeleter() = delete; |
Patrick Williams | f289c65 | 2022-08-26 10:43:10 -0500 | [diff] [blame] | 45 | explicit BusDeleter(SdBusInterface* interface) : m_interface(interface) {} |
Patrick Williams | ba6f50c | 2017-04-18 11:38:30 -0500 | [diff] [blame] | 46 | |
Brad Bishop | 7583460 | 2018-12-11 08:42:53 -0500 | [diff] [blame] | 47 | void operator()(sd_bus* ptr) const |
| 48 | { |
| 49 | (m_interface->*deleter)(ptr); |
| 50 | } |
| 51 | |
| 52 | SdBusInterface* m_interface; |
William A. Kennington III | 062205d | 2018-12-19 17:34:13 -0800 | [diff] [blame] | 53 | decltype(&SdBusInterface::sd_bus_unref) deleter = |
| 54 | &SdBusInterface::sd_bus_unref; |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 55 | }; |
| 56 | |
Brad Bishop | d294557 | 2017-02-15 23:40:15 -0500 | [diff] [blame] | 57 | /** @brief Convert a vector of strings to c-style char** array. */ |
| 58 | class Strv |
| 59 | { |
Brad Bishop | b4cc7d7 | 2018-12-06 15:27:48 -0500 | [diff] [blame] | 60 | public: |
| 61 | ~Strv() = default; |
| 62 | Strv() = delete; |
| 63 | Strv(const Strv&) = delete; |
| 64 | Strv& operator=(const Strv&) = delete; |
| 65 | Strv(Strv&&) = default; |
| 66 | Strv& operator=(Strv&&) = default; |
Brad Bishop | d294557 | 2017-02-15 23:40:15 -0500 | [diff] [blame] | 67 | |
Brad Bishop | b4cc7d7 | 2018-12-06 15:27:48 -0500 | [diff] [blame] | 68 | explicit Strv(const std::vector<std::string>& v) |
| 69 | { |
| 70 | std::transform(v.begin(), v.end(), std::back_inserter(ptrs), |
| 71 | [](const auto& i) { return i.c_str(); }); |
| 72 | ptrs.push_back(nullptr); |
| 73 | } |
Brad Bishop | d294557 | 2017-02-15 23:40:15 -0500 | [diff] [blame] | 74 | |
Patrick Williams | 4b64623 | 2021-02-22 13:50:48 -0600 | [diff] [blame] | 75 | explicit operator char**() |
Brad Bishop | b4cc7d7 | 2018-12-06 15:27:48 -0500 | [diff] [blame] | 76 | { |
| 77 | return const_cast<char**>(&ptrs.front()); |
| 78 | } |
Brad Bishop | d294557 | 2017-02-15 23:40:15 -0500 | [diff] [blame] | 79 | |
Brad Bishop | b4cc7d7 | 2018-12-06 15:27:48 -0500 | [diff] [blame] | 80 | private: |
| 81 | std::vector<const char*> ptrs; |
Brad Bishop | d294557 | 2017-02-15 23:40:15 -0500 | [diff] [blame] | 82 | }; |
| 83 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 84 | /* @brief Alias 'bus' to a unique_ptr type for auto-release. */ |
| 85 | using bus = std::unique_ptr<sd_bus, BusDeleter>; |
| 86 | |
Patrick Williams | 6649221 | 2021-11-19 16:49:28 -0600 | [diff] [blame] | 87 | struct bus_friend; |
| 88 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 89 | } // namespace details |
| 90 | |
| 91 | /** @class bus |
| 92 | * @brief Provides C++ bindings to the sd_bus_* class functions. |
| 93 | */ |
| 94 | struct bus |
| 95 | { |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 96 | /* Define all of the basic class operations: |
| 97 | * Not allowed: |
| 98 | * - Default constructor to avoid nullptrs. |
| 99 | * - Copy operations due to internal unique_ptr. |
| 100 | * Allowed: |
| 101 | * - Move operations. |
| 102 | * - Destructor. |
| 103 | */ |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 104 | bus() = delete; |
| 105 | bus(const bus&) = delete; |
| 106 | bus& operator=(const bus&) = delete; |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 107 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 108 | bus(bus&&) = default; |
| 109 | bus& operator=(bus&&) = default; |
| 110 | ~bus() = default; |
| 111 | |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 112 | bus(busp_t b, sdbusplus::SdBusInterface* intf); |
| 113 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 114 | /** @brief Conversion constructor from 'busp_t'. |
| 115 | * |
Patrick Williams | ba6f50c | 2017-04-18 11:38:30 -0500 | [diff] [blame] | 116 | * Increments ref-count of the bus-pointer and releases it when done. |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 117 | */ |
Patrick Williams | bee1a6a | 2017-02-16 16:06:51 -0600 | [diff] [blame] | 118 | explicit bus(busp_t b); |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 119 | |
Patrick Williams | ba6f50c | 2017-04-18 11:38:30 -0500 | [diff] [blame] | 120 | /** @brief Constructor for 'bus'. |
| 121 | * |
| 122 | * Takes ownership of the bus-pointer and releases it when done. |
Patrick Williams | ba6f50c | 2017-04-18 11:38:30 -0500 | [diff] [blame] | 123 | */ |
| 124 | bus(busp_t b, std::false_type); |
| 125 | |
William A. Kennington III | 062205d | 2018-12-19 17:34:13 -0800 | [diff] [blame] | 126 | /** @brief Sets the bus to be closed when this handle is destroyed. */ |
| 127 | void set_should_close(bool shouldClose) |
| 128 | { |
| 129 | if (shouldClose) |
| 130 | { |
| 131 | _bus.get_deleter().deleter = |
| 132 | &SdBusInterface::sd_bus_flush_close_unref; |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | _bus.get_deleter().deleter = &SdBusInterface::sd_bus_unref; |
| 137 | } |
| 138 | } |
| 139 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 140 | /** @brief Release ownership of the stored bus-pointer. */ |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 141 | busp_t release() |
| 142 | { |
| 143 | return _bus.release(); |
| 144 | } |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 145 | |
William A. Kennington III | 42f6ad5 | 2018-12-19 17:32:30 -0800 | [diff] [blame] | 146 | /** @brief Flush all of the outstanding work on the bus. */ |
| 147 | void flush() |
| 148 | { |
| 149 | int r = _intf->sd_bus_flush(_bus.get()); |
| 150 | if (r < 0) |
| 151 | { |
| 152 | throw exception::SdBusError(-r, "sd_bus_flush"); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** @brief Close the connection to the dbus daemon. */ |
| 157 | void close() |
| 158 | { |
| 159 | _intf->sd_bus_close(_bus.get()); |
| 160 | } |
| 161 | |
William A. Kennington III | 5d81ca2 | 2022-11-22 02:28:17 -0800 | [diff] [blame] | 162 | /** @brief Determine if the bus is open to the broker */ |
| 163 | bool is_open() |
| 164 | { |
| 165 | int r = _intf->sd_bus_is_open(_bus.get()); |
| 166 | if (r < 0) |
| 167 | { |
| 168 | throw exception::SdBusError(-r, "sd_bus_is_open"); |
| 169 | } |
| 170 | return r; |
| 171 | } |
| 172 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 173 | /** @brief Wait for new dbus messages or signals. |
| 174 | * |
| 175 | * @param[in] timeout_us - Timeout in usec. |
| 176 | */ |
William A. Kennington III | 94865b3 | 2022-11-21 18:02:09 -0800 | [diff] [blame] | 177 | int wait(uint64_t timeout_us) |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 178 | { |
William A. Kennington III | 94865b3 | 2022-11-21 18:02:09 -0800 | [diff] [blame] | 179 | int r = _intf->sd_bus_wait(_bus.get(), timeout_us); |
| 180 | if (r < 0) |
| 181 | { |
| 182 | throw exception::SdBusError(-r, "sd_bus_wait"); |
| 183 | } |
| 184 | return r; |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 185 | } |
William A. Kennington III | 94865b3 | 2022-11-21 18:02:09 -0800 | [diff] [blame] | 186 | |
| 187 | int wait(std::optional<SdBusDuration> timeout = std::nullopt) |
William A. Kennington III | b5a8446 | 2019-03-06 16:46:57 -0800 | [diff] [blame] | 188 | { |
William A. Kennington III | 94865b3 | 2022-11-21 18:02:09 -0800 | [diff] [blame] | 189 | return wait(timeout ? timeout->count() : UINT64_MAX); |
William A. Kennington III | b5a8446 | 2019-03-06 16:46:57 -0800 | [diff] [blame] | 190 | } |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 191 | |
| 192 | /** @brief Process waiting dbus messages or signals. */ |
| 193 | auto process() |
| 194 | { |
| 195 | sd_bus_message* m = nullptr; |
William A. Kennington III | 49e4bdc | 2018-06-05 23:28:02 -0700 | [diff] [blame] | 196 | int r = _intf->sd_bus_process(_bus.get(), &m); |
| 197 | if (r < 0) |
| 198 | { |
| 199 | throw exception::SdBusError(-r, "sd_bus_process"); |
| 200 | } |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 201 | |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 202 | return message_t(m, _intf, std::false_type()); |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 203 | } |
| 204 | |
Patrick Williams | c7ba66f | 2016-10-18 11:30:07 -0500 | [diff] [blame] | 205 | /** @brief Process waiting dbus messages or signals, discarding unhandled. |
| 206 | */ |
James Feist | b575518 | 2018-07-16 10:30:08 -0700 | [diff] [blame] | 207 | auto process_discard() |
Patrick Williams | c7ba66f | 2016-10-18 11:30:07 -0500 | [diff] [blame] | 208 | { |
William A. Kennington III | 49e4bdc | 2018-06-05 23:28:02 -0700 | [diff] [blame] | 209 | int r = _intf->sd_bus_process(_bus.get(), nullptr); |
| 210 | if (r < 0) |
| 211 | { |
| 212 | throw exception::SdBusError(-r, "sd_bus_process discard"); |
| 213 | } |
James Feist | b575518 | 2018-07-16 10:30:08 -0700 | [diff] [blame] | 214 | return r > 0; |
Patrick Williams | c7ba66f | 2016-10-18 11:30:07 -0500 | [diff] [blame] | 215 | } |
| 216 | |
Nan Zhou | 25e2a09 | 2022-09-01 03:47:19 +0000 | [diff] [blame] | 217 | /** @brief Process waiting dbus messages or signals forever, discarding |
| 218 | * unhandled. |
| 219 | */ |
Brad Bishop | e8c7614 | 2022-09-29 11:07:00 -0400 | [diff] [blame] | 220 | [[noreturn]] void process_loop() |
Nan Zhou | 25e2a09 | 2022-09-01 03:47:19 +0000 | [diff] [blame] | 221 | { |
| 222 | while (true) |
| 223 | { |
| 224 | process_discard(); |
| 225 | wait(); |
| 226 | } |
| 227 | } |
| 228 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 229 | /** @brief Claim a service name on the dbus. |
| 230 | * |
| 231 | * @param[in] service - The service name to claim. |
| 232 | */ |
| 233 | void request_name(const char* service) |
| 234 | { |
Waqar Hameed | 841d8d3 | 2018-10-04 13:35:43 +0200 | [diff] [blame] | 235 | int r = _intf->sd_bus_request_name(_bus.get(), service, 0); |
| 236 | if (r < 0) |
| 237 | { |
| 238 | throw exception::SdBusError(-r, "sd_bus_request_name"); |
| 239 | } |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /** @brief Create a method_call message. |
| 243 | * |
| 244 | * @param[in] service - The service to call. |
| 245 | * @param[in] objpath - The object's path for the call. |
| 246 | * @param[in] interf - The object's interface to call. |
| 247 | * @param[in] method - The object's method to call. |
| 248 | * |
| 249 | * @return A newly constructed message. |
| 250 | */ |
| 251 | auto new_method_call(const char* service, const char* objpath, |
| 252 | const char* interf, const char* method) |
| 253 | { |
| 254 | sd_bus_message* m = nullptr; |
William A. Kennington III | ec9236d | 2018-06-05 23:30:15 -0700 | [diff] [blame] | 255 | int r = _intf->sd_bus_message_new_method_call(_bus.get(), &m, service, |
| 256 | objpath, interf, method); |
| 257 | if (r < 0) |
| 258 | { |
| 259 | throw exception::SdBusError(-r, "sd_bus_message_new_method_call"); |
| 260 | } |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 261 | |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 262 | return message_t(m, _intf, std::false_type()); |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 263 | } |
| 264 | |
Patrick Williams | 20d8246 | 2016-10-18 08:01:33 -0500 | [diff] [blame] | 265 | /** @brief Create a signal message. |
| 266 | * |
| 267 | * @param[in] objpath - The object's path for the signal. |
| 268 | * @param[in] interf - The object's interface for the signal. |
| 269 | * @param[in] member - The signal name. |
| 270 | * |
| 271 | * @return A newly constructed message. |
| 272 | */ |
| 273 | auto new_signal(const char* objpath, const char* interf, const char* member) |
| 274 | { |
| 275 | sd_bus_message* m = nullptr; |
William A. Kennington III | ec9236d | 2018-06-05 23:30:15 -0700 | [diff] [blame] | 276 | int r = _intf->sd_bus_message_new_signal(_bus.get(), &m, objpath, |
| 277 | interf, member); |
| 278 | if (r < 0) |
| 279 | { |
| 280 | throw exception::SdBusError(-r, "sd_bus_message_new_signal"); |
| 281 | } |
Patrick Williams | 20d8246 | 2016-10-18 08:01:33 -0500 | [diff] [blame] | 282 | |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 283 | return message_t(m, _intf, std::false_type()); |
Patrick Williams | 20d8246 | 2016-10-18 08:01:33 -0500 | [diff] [blame] | 284 | } |
| 285 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 286 | /** @brief Perform a message call. |
William A. Kennington III | 079fb85 | 2018-10-12 13:36:31 -0700 | [diff] [blame] | 287 | * Errors generated by this call come from underlying dbus |
| 288 | * related errors *AND* from any method call that results |
| 289 | * in a METHOD_ERROR. This means you do not need to check |
| 290 | * is_method_error() on the returned message. |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 291 | * |
| 292 | * @param[in] m - The method_call message. |
| 293 | * @param[in] timeout_us - The timeout for the method call. |
| 294 | * |
| 295 | * @return The response message. |
| 296 | */ |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 297 | auto call(message_t& m, uint64_t timeout_us) |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 298 | { |
William A. Kennington III | 55d6686 | 2018-05-29 17:13:35 -0700 | [diff] [blame] | 299 | sd_bus_error error = SD_BUS_ERROR_NULL; |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 300 | sd_bus_message* reply = nullptr; |
Patrick Williams | 1a25a10 | 2022-09-29 17:18:26 -0500 | [diff] [blame] | 301 | int r = _intf->sd_bus_call(_bus.get(), m.get(), timeout_us, &error, |
| 302 | &reply); |
William A. Kennington III | 55d6686 | 2018-05-29 17:13:35 -0700 | [diff] [blame] | 303 | if (r < 0) |
| 304 | { |
William A. Kennington III | 68cb170 | 2018-06-22 19:35:48 -0700 | [diff] [blame] | 305 | throw exception::SdBusError(&error, "sd_bus_call"); |
William A. Kennington III | 55d6686 | 2018-05-29 17:13:35 -0700 | [diff] [blame] | 306 | } |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 307 | |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 308 | return message_t(reply, _intf, std::false_type()); |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 309 | } |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 310 | auto call(message_t& m, std::optional<SdBusDuration> timeout = std::nullopt) |
William A. Kennington III | b5a8446 | 2019-03-06 16:46:57 -0800 | [diff] [blame] | 311 | { |
| 312 | return call(m, timeout ? timeout->count() : 0); |
| 313 | } |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 314 | |
| 315 | /** @brief Perform a message call, ignoring the reply. |
| 316 | * |
| 317 | * @param[in] m - The method_call message. |
| 318 | * @param[in] timeout_us - The timeout for the method call. |
| 319 | */ |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 320 | void call_noreply(message_t& m, uint64_t timeout_us) |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 321 | { |
William A. Kennington III | 55d6686 | 2018-05-29 17:13:35 -0700 | [diff] [blame] | 322 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 323 | int r = _intf->sd_bus_call(_bus.get(), m.get(), timeout_us, &error, |
| 324 | nullptr); |
| 325 | if (r < 0) |
| 326 | { |
William A. Kennington III | 68cb170 | 2018-06-22 19:35:48 -0700 | [diff] [blame] | 327 | throw exception::SdBusError(&error, "sd_bus_call noreply"); |
William A. Kennington III | 55d6686 | 2018-05-29 17:13:35 -0700 | [diff] [blame] | 328 | } |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 329 | } |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 330 | auto call_noreply(message_t& m, |
William A. Kennington III | b5a8446 | 2019-03-06 16:46:57 -0800 | [diff] [blame] | 331 | std::optional<SdBusDuration> timeout = std::nullopt) |
| 332 | { |
| 333 | return call_noreply(m, timeout ? timeout->count() : 0); |
| 334 | } |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 335 | |
William A. Kennington III | 5b701c6 | 2018-09-11 00:36:30 -0700 | [diff] [blame] | 336 | /** @brief Perform a message call, ignoring the reply and any errors |
| 337 | * in the dbus stack. |
| 338 | * |
| 339 | * @param[in] m - The method_call message. |
| 340 | * @param[in] timeout_us - The timeout for the method call. |
| 341 | */ |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 342 | void call_noreply_noerror(message_t& m, uint64_t timeout_us) |
William A. Kennington III | 5b701c6 | 2018-09-11 00:36:30 -0700 | [diff] [blame] | 343 | { |
| 344 | try |
| 345 | { |
| 346 | call_noreply(m, timeout_us); |
| 347 | } |
Patrick Williams | b466765 | 2021-10-06 12:16:17 -0500 | [diff] [blame] | 348 | catch (const exception::SdBusError&) |
William A. Kennington III | 5b701c6 | 2018-09-11 00:36:30 -0700 | [diff] [blame] | 349 | { |
| 350 | // Intentionally ignoring these sd_bus errors |
| 351 | } |
| 352 | } |
Patrick Williams | 6805284 | 2020-05-15 11:29:10 -0500 | [diff] [blame] | 353 | auto call_noreply_noerror( |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 354 | message_t& m, std::optional<SdBusDuration> timeout = std::nullopt) |
William A. Kennington III | b5a8446 | 2019-03-06 16:46:57 -0800 | [diff] [blame] | 355 | { |
| 356 | return call_noreply_noerror(m, timeout ? timeout->count() : 0); |
| 357 | } |
William A. Kennington III | 5b701c6 | 2018-09-11 00:36:30 -0700 | [diff] [blame] | 358 | |
Adriana Kobylak | c549677 | 2017-01-04 09:57:23 -0600 | [diff] [blame] | 359 | /** @brief Get the bus unique name. Ex: ":1.11". |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 360 | * |
| 361 | * @return The bus unique name. |
| 362 | */ |
Adriana Kobylak | c549677 | 2017-01-04 09:57:23 -0600 | [diff] [blame] | 363 | auto get_unique_name() |
| 364 | { |
| 365 | const char* unique = nullptr; |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 366 | _intf->sd_bus_get_unique_name(_bus.get(), &unique); |
Adriana Kobylak | c549677 | 2017-01-04 09:57:23 -0600 | [diff] [blame] | 367 | return std::string(unique); |
| 368 | } |
| 369 | |
Brad Bishop | b4cc7d7 | 2018-12-06 15:27:48 -0500 | [diff] [blame] | 370 | auto get_fd() |
| 371 | { |
William A. Kennington III | cb582e0 | 2018-06-28 18:01:01 -0700 | [diff] [blame] | 372 | return _intf->sd_bus_get_fd(_bus.get()); |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Yi Li | 8ac39ee | 2017-01-16 16:21:51 +0800 | [diff] [blame] | 375 | /** @brief Attach the bus with a sd-event event loop object. |
| 376 | * |
| 377 | * @param[in] event - sd_event object. |
| 378 | * @param[in] priority - priority of bus event source. |
| 379 | */ |
| 380 | void attach_event(sd_event* event, int priority) |
| 381 | { |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 382 | _intf->sd_bus_attach_event(_bus.get(), event, priority); |
Yi Li | 8ac39ee | 2017-01-16 16:21:51 +0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /** @brief Detach the bus from its sd-event event loop object */ |
| 386 | void detach_event() |
| 387 | { |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 388 | _intf->sd_bus_detach_event(_bus.get()); |
Yi Li | 8ac39ee | 2017-01-16 16:21:51 +0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /** @brief Get the sd-event event loop object of the bus */ |
| 392 | auto get_event() |
| 393 | { |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 394 | return _intf->sd_bus_get_event(_bus.get()); |
Yi Li | 8ac39ee | 2017-01-16 16:21:51 +0800 | [diff] [blame] | 395 | } |
| 396 | |
Brad Bishop | d294557 | 2017-02-15 23:40:15 -0500 | [diff] [blame] | 397 | /** @brief Wrapper for sd_bus_emit_interfaces_added_strv |
| 398 | * |
| 399 | * In general the similarly named server::object::object API should |
| 400 | * be used to manage emission of ObjectManager signals in favor |
| 401 | * of this one. Provided here for complex usage scenarios. |
| 402 | * |
| 403 | * @param[in] path - The path to forward. |
| 404 | * @param[in] ifaces - The interfaces to forward. |
| 405 | */ |
| 406 | void emit_interfaces_added(const char* path, |
Patrick Williams | 32ffb03 | 2020-10-12 12:17:48 -0500 | [diff] [blame] | 407 | const std::vector<std::string>& ifaces); |
Brad Bishop | d294557 | 2017-02-15 23:40:15 -0500 | [diff] [blame] | 408 | |
| 409 | /** @brief Wrapper for sd_bus_emit_interfaces_removed_strv |
| 410 | * |
| 411 | * In general the similarly named server::object::object API should |
| 412 | * be used to manage emission of ObjectManager signals in favor |
| 413 | * of this one. Provided here for complex usage scenarios. |
| 414 | * |
| 415 | * @param[in] path - The path to forward. |
| 416 | * @param[in] ifaces - The interfaces to forward. |
| 417 | */ |
| 418 | void emit_interfaces_removed(const char* path, |
Patrick Williams | 32ffb03 | 2020-10-12 12:17:48 -0500 | [diff] [blame] | 419 | const std::vector<std::string>& ifaces); |
Brad Bishop | d294557 | 2017-02-15 23:40:15 -0500 | [diff] [blame] | 420 | |
Brad Bishop | 9a0baf5 | 2017-02-03 20:05:22 -0500 | [diff] [blame] | 421 | /** @brief Wrapper for sd_bus_emit_object_added |
| 422 | * |
| 423 | * In general the similarly named server::object::object API should |
| 424 | * be used to manage emission of ObjectManager signals in favor |
| 425 | * of this one. Provided here for complex usage scenarios. |
| 426 | * |
| 427 | * @param[in] path - The path to forward to sd_bus_emit_object_added |
| 428 | */ |
| 429 | void emit_object_added(const char* path) |
| 430 | { |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 431 | _intf->sd_bus_emit_object_added(_bus.get(), path); |
Brad Bishop | 9a0baf5 | 2017-02-03 20:05:22 -0500 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /** @brief Wrapper for sd_bus_emit_object_removed |
| 435 | * |
| 436 | * In general the similarly named server::object::object API should |
| 437 | * be used to manage emission of ObjectManager signals in favor |
| 438 | * of this one. Provided here for complex usage scenarios. |
| 439 | * |
| 440 | * @param[in] path - The path to forward to sd_bus_emit_object_removed |
| 441 | */ |
| 442 | void emit_object_removed(const char* path) |
| 443 | { |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 444 | _intf->sd_bus_emit_object_removed(_bus.get(), path); |
Brad Bishop | 9a0baf5 | 2017-02-03 20:05:22 -0500 | [diff] [blame] | 445 | } |
| 446 | |
Patrick Williams | b4041d4 | 2017-04-27 21:49:00 -0500 | [diff] [blame] | 447 | /** @brief Wrapper for sd_bus_list_names. |
| 448 | * |
| 449 | * @return A vector of strings containing the 'acquired' names from |
| 450 | * sd_bus_list_names. |
| 451 | */ |
| 452 | auto list_names_acquired() |
| 453 | { |
| 454 | char** names = nullptr; |
| 455 | |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 456 | _intf->sd_bus_list_names(_bus.get(), &names, nullptr); |
Patrick Williams | b4041d4 | 2017-04-27 21:49:00 -0500 | [diff] [blame] | 457 | |
| 458 | std::vector<std::string> result; |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 459 | for (auto ptr = names; ptr && *ptr; ++ptr) |
Patrick Williams | b4041d4 | 2017-04-27 21:49:00 -0500 | [diff] [blame] | 460 | { |
| 461 | result.push_back(*ptr); |
| 462 | free(*ptr); |
| 463 | } |
| 464 | free(names); |
| 465 | |
| 466 | return result; |
| 467 | } |
| 468 | |
Patrick Venture | b9a1e19 | 2018-04-16 09:40:21 -0700 | [diff] [blame] | 469 | /** @brief Get the SdBusInterface used by this bus. |
| 470 | * |
| 471 | * @return A pointer to the SdBusInterface used by this bus. |
| 472 | */ |
| 473 | sdbusplus::SdBusInterface* getInterface() |
| 474 | { |
| 475 | return _intf; |
| 476 | } |
| 477 | |
Patrick Williams | 6649221 | 2021-11-19 16:49:28 -0600 | [diff] [blame] | 478 | friend struct details::bus_friend; |
Patrick Williams | 13f1ef7 | 2016-10-17 14:09:33 -0500 | [diff] [blame] | 479 | |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 480 | protected: |
Patrick Williams | 6649221 | 2021-11-19 16:49:28 -0600 | [diff] [blame] | 481 | busp_t get() noexcept |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 482 | { |
| 483 | return _bus.get(); |
| 484 | } |
| 485 | sdbusplus::SdBusInterface* _intf; |
| 486 | details::bus _bus; |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 487 | }; |
| 488 | |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 489 | inline bus::bus(busp_t b, sdbusplus::SdBusInterface* intf) : |
Brad Bishop | 7583460 | 2018-12-11 08:42:53 -0500 | [diff] [blame] | 490 | _intf(intf), _bus(_intf->sd_bus_ref(b), details::BusDeleter(intf)) |
Patrick Williams | ba6f50c | 2017-04-18 11:38:30 -0500 | [diff] [blame] | 491 | { |
Patrick Williams | ba6f50c | 2017-04-18 11:38:30 -0500 | [diff] [blame] | 492 | // Emitting object added causes a message to get the properties |
| 493 | // which can trigger a 'transaction' in the server bindings. If |
| 494 | // the bus isn't up far enough, this causes an assert deep in |
| 495 | // sd-bus code. Get the 'unique_name' to ensure the bus is up far |
| 496 | // enough to avoid the assert. |
| 497 | if (b != nullptr) |
| 498 | { |
| 499 | get_unique_name(); |
| 500 | } |
Patrick Williams | ba6f50c | 2017-04-18 11:38:30 -0500 | [diff] [blame] | 501 | } |
| 502 | |
Brad Bishop | 7583460 | 2018-12-11 08:42:53 -0500 | [diff] [blame] | 503 | inline bus::bus(busp_t b) : |
| 504 | _intf(&sdbus_impl), |
| 505 | _bus(_intf->sd_bus_ref(b), details::BusDeleter(&sdbus_impl)) |
Patrick Williams | bee1a6a | 2017-02-16 16:06:51 -0600 | [diff] [blame] | 506 | { |
Patrick Williams | bee1a6a | 2017-02-16 16:06:51 -0600 | [diff] [blame] | 507 | // Emitting object added causes a message to get the properties |
| 508 | // which can trigger a 'transaction' in the server bindings. If |
| 509 | // the bus isn't up far enough, this causes an assert deep in |
| 510 | // sd-bus code. Get the 'unique_name' to ensure the bus is up far |
| 511 | // enough to avoid the assert. |
| 512 | if (b != nullptr) |
| 513 | { |
| 514 | get_unique_name(); |
| 515 | } |
Patrick Williams | bee1a6a | 2017-02-16 16:06:51 -0600 | [diff] [blame] | 516 | } |
| 517 | |
Brad Bishop | 7583460 | 2018-12-11 08:42:53 -0500 | [diff] [blame] | 518 | inline bus::bus(busp_t b, std::false_type) : |
| 519 | _intf(&sdbus_impl), _bus(b, details::BusDeleter(&sdbus_impl)) |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 520 | { |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 521 | // Emitting object added causes a message to get the properties |
| 522 | // which can trigger a 'transaction' in the server bindings. If |
| 523 | // the bus isn't up far enough, this causes an assert deep in |
| 524 | // sd-bus code. Get the 'unique_name' to ensure the bus is up far |
| 525 | // enough to avoid the assert. |
| 526 | if (b != nullptr) |
| 527 | { |
| 528 | get_unique_name(); |
| 529 | } |
Patrick Venture | ff09568 | 2018-04-13 20:54:32 -0700 | [diff] [blame] | 530 | } |
Patrick Williams | bee1a6a | 2017-02-16 16:06:51 -0600 | [diff] [blame] | 531 | |
Vernon Mauery | 8ca6025 | 2018-11-08 14:55:34 -0800 | [diff] [blame] | 532 | /* Create a new default connection: system bus if root, session bus if user */ |
Brad Bishop | aed8179 | 2016-10-12 08:40:34 -0400 | [diff] [blame] | 533 | inline bus new_default() |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 534 | { |
| 535 | sd_bus* b = nullptr; |
Vernon Mauery | 8ca6025 | 2018-11-08 14:55:34 -0800 | [diff] [blame] | 536 | sd_bus_default(&b); |
Patrick Williams | ba6f50c | 2017-04-18 11:38:30 -0500 | [diff] [blame] | 537 | return bus(b, std::false_type()); |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 538 | } |
| 539 | |
Vernon Mauery | 8ca6025 | 2018-11-08 14:55:34 -0800 | [diff] [blame] | 540 | /* Create a new default connection to the session bus */ |
| 541 | inline bus new_default_user() |
| 542 | { |
| 543 | sd_bus* b = nullptr; |
| 544 | sd_bus_default_user(&b); |
| 545 | return bus(b, std::false_type()); |
| 546 | } |
| 547 | |
| 548 | /* Create a new default connection to the system bus */ |
| 549 | inline bus new_default_system() |
| 550 | { |
| 551 | sd_bus* b = nullptr; |
| 552 | sd_bus_default_system(&b); |
| 553 | return bus(b, std::false_type()); |
| 554 | } |
| 555 | |
| 556 | /* WARNING: THESE ARE NOT THE FUNCTIONS YOU ARE LOOKING FOR! */ |
William A. Kennington III | 34fdbf3 | 2018-12-19 17:33:42 -0800 | [diff] [blame] | 557 | inline bus new_bus() |
| 558 | { |
| 559 | sd_bus* b = nullptr; |
| 560 | sd_bus_open(&b); |
William A. Kennington III | 062205d | 2018-12-19 17:34:13 -0800 | [diff] [blame] | 561 | bus bus(b, std::false_type()); |
| 562 | bus.set_should_close(true); |
| 563 | return bus; |
William A. Kennington III | 34fdbf3 | 2018-12-19 17:33:42 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Brad Bishop | aed8179 | 2016-10-12 08:40:34 -0400 | [diff] [blame] | 566 | inline bus new_user() |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 567 | { |
| 568 | sd_bus* b = nullptr; |
| 569 | sd_bus_open_user(&b); |
William A. Kennington III | 062205d | 2018-12-19 17:34:13 -0800 | [diff] [blame] | 570 | bus bus(b, std::false_type()); |
| 571 | bus.set_should_close(true); |
| 572 | return bus; |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 573 | } |
| 574 | |
Brad Bishop | aed8179 | 2016-10-12 08:40:34 -0400 | [diff] [blame] | 575 | inline bus new_system() |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 576 | { |
| 577 | sd_bus* b = nullptr; |
| 578 | sd_bus_open_system(&b); |
William A. Kennington III | 062205d | 2018-12-19 17:34:13 -0800 | [diff] [blame] | 579 | bus bus(b, std::false_type()); |
| 580 | bus.set_should_close(true); |
| 581 | return bus; |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 582 | } |
| 583 | |
Patrick Williams | 6649221 | 2021-11-19 16:49:28 -0600 | [diff] [blame] | 584 | namespace details |
| 585 | { |
| 586 | |
| 587 | // Some sdbusplus classes need to be able to pass the underlying bus pointer |
| 588 | // along to sd_bus calls, but we don't want to make it available for everyone. |
| 589 | // Define a class which can be inherited explicitly (intended for internal users |
| 590 | // only) to get the underlying bus pointer. |
| 591 | struct bus_friend |
| 592 | { |
| 593 | static busp_t get_busp(sdbusplus::bus::bus& b) noexcept |
| 594 | { |
| 595 | return b.get(); |
| 596 | } |
| 597 | }; |
| 598 | |
| 599 | } // namespace details |
| 600 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 601 | } // namespace bus |
| 602 | |
Patrick Williams | 0f282c4 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 603 | using bus_t = bus::bus; |
| 604 | |
Adriana Kobylak | 6312225 | 2017-01-26 15:09:00 -0600 | [diff] [blame] | 605 | /** @brief Get the dbus bus from the message. |
| 606 | * |
| 607 | * @return The dbus bus. |
| 608 | */ |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 609 | inline auto message_t::get_bus() const |
Adriana Kobylak | 6312225 | 2017-01-26 15:09:00 -0600 | [diff] [blame] | 610 | { |
| 611 | sd_bus* b = nullptr; |
Patrick Venture | 4c3427c | 2018-04-13 17:09:55 -0700 | [diff] [blame] | 612 | b = _intf->sd_bus_message_get_bus(_msg.get()); |
Patrick Williams | 0f282c4 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 613 | return bus_t(b, _intf); |
Adriana Kobylak | 6312225 | 2017-01-26 15:09:00 -0600 | [diff] [blame] | 614 | } |
| 615 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 616 | } // namespace sdbusplus |
Patrick Williams | 0c8136a | 2021-08-28 14:42:31 -0500 | [diff] [blame] | 617 | |
| 618 | #ifdef __clang__ |
| 619 | #pragma clang diagnostic pop |
| 620 | #endif |