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