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