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