blob: a0b0b11c7cdf5f1ef8d4c477626afae6cf4b586d [file] [log] [blame]
Patrick Williams5b485792016-08-02 07:35:14 -05001#pragma once
2
Brad Bishopb4cc7d72018-12-06 15:27:48 -05003#include <systemd/sd-bus.h>
4#include <systemd/sd-event.h>
5
Patrick Williams127b8ab2020-05-21 15:24:19 -05006#include <sdbusplus/exception.hpp>
7#include <sdbusplus/message.hpp>
8#include <sdbusplus/sdbus.hpp>
9
Brad Bishopd2945572017-02-15 23:40:15 -050010#include <algorithm>
Brad Bishop887ebf62016-10-12 15:40:04 -040011#include <climits>
Patrick Venture2ea15b72018-06-22 08:41:18 -070012#include <memory>
William A. Kennington IIIb5a84462019-03-06 16:46:57 -080013#include <optional>
Patrick Venture2ea15b72018-06-22 08:41:18 -070014#include <string>
Patrick Venture2ea15b72018-06-22 08:41:18 -070015#include <vector>
Patrick Ventureff095682018-04-13 20:54:32 -070016
Patrick Williams5b485792016-08-02 07:35:14 -050017namespace sdbusplus
18{
Patrick Williams13f1ef72016-10-17 14:09:33 -050019
20// Forward declare.
Patrick Ventureff095682018-04-13 20:54:32 -070021namespace server
22{
23namespace interface
24{
25struct interface;
26}
27} // namespace server
28namespace server
29{
30namespace manager
31{
32struct manager;
33}
34} // namespace server
35namespace server
36{
37namespace object
38{
Brad Bishopb4cc7d72018-12-06 15:27:48 -050039template <class...>
40struct object;
Patrick Ventureff095682018-04-13 20:54:32 -070041}
42} // namespace server
43namespace bus
44{
45namespace match
46{
47struct match;
48}
49} // namespace bus
Patrick Williams13f1ef72016-10-17 14:09:33 -050050
Patrick Williams5b485792016-08-02 07:35:14 -050051namespace bus
52{
53
54using busp_t = sd_bus*;
Ed Tanous8cd7a4a2019-03-13 16:41:49 -070055struct bus;
Patrick Williams5b485792016-08-02 07:35:14 -050056
57/** @brief Get an instance of the 'default' bus. */
58bus new_default();
59/** @brief Get an instance of the 'user' session bus. */
60bus new_user();
61/** @brief Get an instance of the 'system' bus. */
62bus new_system();
63
64namespace details
65{
66
67/** @brief unique_ptr functor to release a bus reference. */
68struct BusDeleter
69{
Brad Bishop75834602018-12-11 08:42:53 -050070 BusDeleter() = delete;
71 explicit BusDeleter(SdBusInterface* interface) : m_interface(interface)
Patrick Williams127b8ab2020-05-21 15:24:19 -050072 {}
Patrick Williamsba6f50c2017-04-18 11:38:30 -050073
Brad Bishop75834602018-12-11 08:42:53 -050074 void operator()(sd_bus* ptr) const
75 {
76 (m_interface->*deleter)(ptr);
77 }
78
79 SdBusInterface* m_interface;
William A. Kennington III062205d2018-12-19 17:34:13 -080080 decltype(&SdBusInterface::sd_bus_unref) deleter =
81 &SdBusInterface::sd_bus_unref;
Patrick Williams5b485792016-08-02 07:35:14 -050082};
83
Brad Bishopd2945572017-02-15 23:40:15 -050084/** @brief Convert a vector of strings to c-style char** array. */
85class Strv
86{
Brad Bishopb4cc7d72018-12-06 15:27:48 -050087 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 Bishopd2945572017-02-15 23:40:15 -050094
Brad Bishopb4cc7d72018-12-06 15:27:48 -050095 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 Bishopd2945572017-02-15 23:40:15 -0500101
Patrick Williams4b646232021-02-22 13:50:48 -0600102 explicit operator char**()
Brad Bishopb4cc7d72018-12-06 15:27:48 -0500103 {
104 return const_cast<char**>(&ptrs.front());
105 }
Brad Bishopd2945572017-02-15 23:40:15 -0500106
Brad Bishopb4cc7d72018-12-06 15:27:48 -0500107 private:
108 std::vector<const char*> ptrs;
Brad Bishopd2945572017-02-15 23:40:15 -0500109};
110
Patrick Williams5b485792016-08-02 07:35:14 -0500111/* @brief Alias 'bus' to a unique_ptr type for auto-release. */
112using 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 */
119struct bus
120{
Patrick Ventureff095682018-04-13 20:54:32 -0700121 /* 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 Williams5b485792016-08-02 07:35:14 -0500129 bus() = delete;
130 bus(const bus&) = delete;
131 bus& operator=(const bus&) = delete;
Patrick Ventureff095682018-04-13 20:54:32 -0700132
Patrick Williams5b485792016-08-02 07:35:14 -0500133 bus(bus&&) = default;
134 bus& operator=(bus&&) = default;
135 ~bus() = default;
136
Patrick Ventureff095682018-04-13 20:54:32 -0700137 bus(busp_t b, sdbusplus::SdBusInterface* intf);
138
Patrick Williams5b485792016-08-02 07:35:14 -0500139 /** @brief Conversion constructor from 'busp_t'.
140 *
Patrick Williamsba6f50c2017-04-18 11:38:30 -0500141 * Increments ref-count of the bus-pointer and releases it when done.
Patrick Williams5b485792016-08-02 07:35:14 -0500142 */
Patrick Williamsbee1a6a2017-02-16 16:06:51 -0600143 explicit bus(busp_t b);
Patrick Williams5b485792016-08-02 07:35:14 -0500144
Patrick Williamsba6f50c2017-04-18 11:38:30 -0500145 /** @brief Constructor for 'bus'.
146 *
147 * Takes ownership of the bus-pointer and releases it when done.
Patrick Williamsba6f50c2017-04-18 11:38:30 -0500148 */
149 bus(busp_t b, std::false_type);
150
William A. Kennington III062205d2018-12-19 17:34:13 -0800151 /** @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 Williams5b485792016-08-02 07:35:14 -0500165 /** @brief Release ownership of the stored bus-pointer. */
Patrick Ventureff095682018-04-13 20:54:32 -0700166 busp_t release()
167 {
168 return _bus.release();
169 }
Patrick Williams5b485792016-08-02 07:35:14 -0500170
William A. Kennington III42f6ad52018-12-19 17:32:30 -0800171 /** @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 Williams5b485792016-08-02 07:35:14 -0500187 /** @brief Wait for new dbus messages or signals.
188 *
189 * @param[in] timeout_us - Timeout in usec.
190 */
William A. Kennington IIIb5a84462019-03-06 16:46:57 -0800191 void wait(uint64_t timeout_us)
Patrick Williams5b485792016-08-02 07:35:14 -0500192 {
Patrick Ventureff095682018-04-13 20:54:32 -0700193 _intf->sd_bus_wait(_bus.get(), timeout_us);
Patrick Williams5b485792016-08-02 07:35:14 -0500194 }
William A. Kennington IIIb5a84462019-03-06 16:46:57 -0800195 void wait(std::optional<SdBusDuration> timeout = std::nullopt)
196 {
197 wait(timeout ? timeout->count() : UINT64_MAX);
198 }
Patrick Williams5b485792016-08-02 07:35:14 -0500199
200 /** @brief Process waiting dbus messages or signals. */
201 auto process()
202 {
203 sd_bus_message* m = nullptr;
William A. Kennington III49e4bdc2018-06-05 23:28:02 -0700204 int r = _intf->sd_bus_process(_bus.get(), &m);
205 if (r < 0)
206 {
207 throw exception::SdBusError(-r, "sd_bus_process");
208 }
Patrick Williams5b485792016-08-02 07:35:14 -0500209
Patrick Ventureff095682018-04-13 20:54:32 -0700210 return message::message(m, _intf, std::false_type());
Patrick Williams5b485792016-08-02 07:35:14 -0500211 }
212
Patrick Williamsc7ba66f2016-10-18 11:30:07 -0500213 /** @brief Process waiting dbus messages or signals, discarding unhandled.
214 */
James Feistb5755182018-07-16 10:30:08 -0700215 auto process_discard()
Patrick Williamsc7ba66f2016-10-18 11:30:07 -0500216 {
William A. Kennington III49e4bdc2018-06-05 23:28:02 -0700217 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 Feistb5755182018-07-16 10:30:08 -0700222 return r > 0;
Patrick Williamsc7ba66f2016-10-18 11:30:07 -0500223 }
224
Patrick Williams5b485792016-08-02 07:35:14 -0500225 /** @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 Hameed841d8d32018-10-04 13:35:43 +0200231 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 Williams5b485792016-08-02 07:35:14 -0500236 }
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 IIIec9236d2018-06-05 23:30:15 -0700251 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 Williams5b485792016-08-02 07:35:14 -0500257
Patrick Ventureff095682018-04-13 20:54:32 -0700258 return message::message(m, _intf, std::false_type());
Patrick Williams5b485792016-08-02 07:35:14 -0500259 }
260
Patrick Williams20d82462016-10-18 08:01:33 -0500261 /** @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 IIIec9236d2018-06-05 23:30:15 -0700272 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 Williams20d82462016-10-18 08:01:33 -0500278
Patrick Ventureff095682018-04-13 20:54:32 -0700279 return message::message(m, _intf, std::false_type());
Patrick Williams20d82462016-10-18 08:01:33 -0500280 }
281
Patrick Williams5b485792016-08-02 07:35:14 -0500282 /** @brief Perform a message call.
William A. Kennington III079fb852018-10-12 13:36:31 -0700283 * 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 Williams5b485792016-08-02 07:35:14 -0500287 *
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 IIIb5a84462019-03-06 16:46:57 -0800293 auto call(message::message& m, uint64_t timeout_us)
Patrick Williams5b485792016-08-02 07:35:14 -0500294 {
William A. Kennington III55d66862018-05-29 17:13:35 -0700295 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Williams5b485792016-08-02 07:35:14 -0500296 sd_bus_message* reply = nullptr;
Brad Bishopb4cc7d72018-12-06 15:27:48 -0500297 int r =
298 _intf->sd_bus_call(_bus.get(), m.get(), timeout_us, &error, &reply);
William A. Kennington III55d66862018-05-29 17:13:35 -0700299 if (r < 0)
300 {
William A. Kennington III68cb1702018-06-22 19:35:48 -0700301 throw exception::SdBusError(&error, "sd_bus_call");
William A. Kennington III55d66862018-05-29 17:13:35 -0700302 }
Patrick Williams5b485792016-08-02 07:35:14 -0500303
Patrick Ventureff095682018-04-13 20:54:32 -0700304 return message::message(reply, _intf, std::false_type());
Patrick Williams5b485792016-08-02 07:35:14 -0500305 }
William A. Kennington IIIb5a84462019-03-06 16:46:57 -0800306 auto call(message::message& m,
307 std::optional<SdBusDuration> timeout = std::nullopt)
308 {
309 return call(m, timeout ? timeout->count() : 0);
310 }
Patrick Williams5b485792016-08-02 07:35:14 -0500311
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 IIIb5a84462019-03-06 16:46:57 -0800317 void call_noreply(message::message& m, uint64_t timeout_us)
Patrick Williams5b485792016-08-02 07:35:14 -0500318 {
William A. Kennington III55d66862018-05-29 17:13:35 -0700319 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 III68cb1702018-06-22 19:35:48 -0700324 throw exception::SdBusError(&error, "sd_bus_call noreply");
William A. Kennington III55d66862018-05-29 17:13:35 -0700325 }
Patrick Williams5b485792016-08-02 07:35:14 -0500326 }
William A. Kennington IIIb5a84462019-03-06 16:46:57 -0800327 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 Williams5b485792016-08-02 07:35:14 -0500332
William A. Kennington III5b701c62018-09-11 00:36:30 -0700333 /** @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 IIIb5a84462019-03-06 16:46:57 -0800339 void call_noreply_noerror(message::message& m, uint64_t timeout_us)
William A. Kennington III5b701c62018-09-11 00:36:30 -0700340 {
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 Williams68052842020-05-15 11:29:10 -0500350 auto call_noreply_noerror(
351 message::message& m,
352 std::optional<SdBusDuration> timeout = std::nullopt)
William A. Kennington IIIb5a84462019-03-06 16:46:57 -0800353 {
354 return call_noreply_noerror(m, timeout ? timeout->count() : 0);
355 }
William A. Kennington III5b701c62018-09-11 00:36:30 -0700356
Adriana Kobylakc5496772017-01-04 09:57:23 -0600357 /** @brief Get the bus unique name. Ex: ":1.11".
Patrick Ventureff095682018-04-13 20:54:32 -0700358 *
359 * @return The bus unique name.
360 */
Adriana Kobylakc5496772017-01-04 09:57:23 -0600361 auto get_unique_name()
362 {
363 const char* unique = nullptr;
Patrick Ventureff095682018-04-13 20:54:32 -0700364 _intf->sd_bus_get_unique_name(_bus.get(), &unique);
Adriana Kobylakc5496772017-01-04 09:57:23 -0600365 return std::string(unique);
366 }
367
Brad Bishopb4cc7d72018-12-06 15:27:48 -0500368 auto get_fd()
369 {
William A. Kennington IIIcb582e02018-06-28 18:01:01 -0700370 return _intf->sd_bus_get_fd(_bus.get());
James Feist284a0f92018-04-05 15:28:16 -0700371 }
372
Yi Li8ac39ee2017-01-16 16:21:51 +0800373 /** @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 Ventureff095682018-04-13 20:54:32 -0700380 _intf->sd_bus_attach_event(_bus.get(), event, priority);
Yi Li8ac39ee2017-01-16 16:21:51 +0800381 }
382
383 /** @brief Detach the bus from its sd-event event loop object */
384 void detach_event()
385 {
Patrick Ventureff095682018-04-13 20:54:32 -0700386 _intf->sd_bus_detach_event(_bus.get());
Yi Li8ac39ee2017-01-16 16:21:51 +0800387 }
388
389 /** @brief Get the sd-event event loop object of the bus */
390 auto get_event()
391 {
Patrick Ventureff095682018-04-13 20:54:32 -0700392 return _intf->sd_bus_get_event(_bus.get());
Yi Li8ac39ee2017-01-16 16:21:51 +0800393 }
394
Brad Bishopd2945572017-02-15 23:40:15 -0500395 /** @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 Williams32ffb032020-10-12 12:17:48 -0500405 const std::vector<std::string>& ifaces);
Brad Bishopd2945572017-02-15 23:40:15 -0500406
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 Williams32ffb032020-10-12 12:17:48 -0500417 const std::vector<std::string>& ifaces);
Brad Bishopd2945572017-02-15 23:40:15 -0500418
Brad Bishop9a0baf52017-02-03 20:05:22 -0500419 /** @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 Ventureff095682018-04-13 20:54:32 -0700429 _intf->sd_bus_emit_object_added(_bus.get(), path);
Brad Bishop9a0baf52017-02-03 20:05:22 -0500430 }
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 Ventureff095682018-04-13 20:54:32 -0700442 _intf->sd_bus_emit_object_removed(_bus.get(), path);
Brad Bishop9a0baf52017-02-03 20:05:22 -0500443 }
444
Patrick Williamsb4041d42017-04-27 21:49:00 -0500445 /** @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 Ventureff095682018-04-13 20:54:32 -0700454 _intf->sd_bus_list_names(_bus.get(), &names, nullptr);
Patrick Williamsb4041d42017-04-27 21:49:00 -0500455
456 std::vector<std::string> result;
Patrick Ventureff095682018-04-13 20:54:32 -0700457 for (auto ptr = names; ptr && *ptr; ++ptr)
Patrick Williamsb4041d42017-04-27 21:49:00 -0500458 {
459 result.push_back(*ptr);
460 free(*ptr);
461 }
462 free(names);
463
464 return result;
465 }
466
Patrick Ventureb9a1e192018-04-16 09:40:21 -0700467 /** @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 Williams13f1ef72016-10-17 14:09:33 -0500476 friend struct server::interface::interface;
Patrick Williams05aab8b2016-10-17 14:12:12 -0500477 friend struct server::manager::manager;
Brad Bishopb4cc7d72018-12-06 15:27:48 -0500478 template <class... Args>
479 friend struct server::object::object;
Christian Andersenc69def62016-12-20 13:51:52 +0100480 friend struct match::match;
Patrick Williams13f1ef72016-10-17 14:09:33 -0500481
James Feist284a0f92018-04-05 15:28:16 -0700482 protected:
Patrick Ventureff095682018-04-13 20:54:32 -0700483 busp_t get()
484 {
485 return _bus.get();
486 }
487 sdbusplus::SdBusInterface* _intf;
488 details::bus _bus;
Patrick Williams5b485792016-08-02 07:35:14 -0500489};
490
Patrick Ventureff095682018-04-13 20:54:32 -0700491inline bus::bus(busp_t b, sdbusplus::SdBusInterface* intf) :
Brad Bishop75834602018-12-11 08:42:53 -0500492 _intf(intf), _bus(_intf->sd_bus_ref(b), details::BusDeleter(intf))
Patrick Williamsba6f50c2017-04-18 11:38:30 -0500493{
Patrick Williamsba6f50c2017-04-18 11:38:30 -0500494 // 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 Williamsba6f50c2017-04-18 11:38:30 -0500503}
504
Brad Bishop75834602018-12-11 08:42:53 -0500505inline bus::bus(busp_t b) :
506 _intf(&sdbus_impl),
507 _bus(_intf->sd_bus_ref(b), details::BusDeleter(&sdbus_impl))
Patrick Williamsbee1a6a2017-02-16 16:06:51 -0600508{
Patrick Williamsbee1a6a2017-02-16 16:06:51 -0600509 // 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 Williamsbee1a6a2017-02-16 16:06:51 -0600518}
519
Brad Bishop75834602018-12-11 08:42:53 -0500520inline bus::bus(busp_t b, std::false_type) :
521 _intf(&sdbus_impl), _bus(b, details::BusDeleter(&sdbus_impl))
Patrick Ventureff095682018-04-13 20:54:32 -0700522{
Patrick Ventureff095682018-04-13 20:54:32 -0700523 // 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 Ventureff095682018-04-13 20:54:32 -0700532}
Patrick Williamsbee1a6a2017-02-16 16:06:51 -0600533
Vernon Mauery8ca60252018-11-08 14:55:34 -0800534/* Create a new default connection: system bus if root, session bus if user */
Brad Bishopaed81792016-10-12 08:40:34 -0400535inline bus new_default()
Patrick Williams5b485792016-08-02 07:35:14 -0500536{
537 sd_bus* b = nullptr;
Vernon Mauery8ca60252018-11-08 14:55:34 -0800538 sd_bus_default(&b);
Patrick Williamsba6f50c2017-04-18 11:38:30 -0500539 return bus(b, std::false_type());
Patrick Williams5b485792016-08-02 07:35:14 -0500540}
541
Vernon Mauery8ca60252018-11-08 14:55:34 -0800542/* Create a new default connection to the session bus */
543inline 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 */
551inline 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 III34fdbf32018-12-19 17:33:42 -0800559inline bus new_bus()
560{
561 sd_bus* b = nullptr;
562 sd_bus_open(&b);
William A. Kennington III062205d2018-12-19 17:34:13 -0800563 bus bus(b, std::false_type());
564 bus.set_should_close(true);
565 return bus;
William A. Kennington III34fdbf32018-12-19 17:33:42 -0800566}
567
Brad Bishopaed81792016-10-12 08:40:34 -0400568inline bus new_user()
Patrick Williams5b485792016-08-02 07:35:14 -0500569{
570 sd_bus* b = nullptr;
571 sd_bus_open_user(&b);
William A. Kennington III062205d2018-12-19 17:34:13 -0800572 bus bus(b, std::false_type());
573 bus.set_should_close(true);
574 return bus;
Patrick Williams5b485792016-08-02 07:35:14 -0500575}
576
Brad Bishopaed81792016-10-12 08:40:34 -0400577inline bus new_system()
Patrick Williams5b485792016-08-02 07:35:14 -0500578{
579 sd_bus* b = nullptr;
580 sd_bus_open_system(&b);
William A. Kennington III062205d2018-12-19 17:34:13 -0800581 bus bus(b, std::false_type());
582 bus.set_should_close(true);
583 return bus;
Patrick Williams5b485792016-08-02 07:35:14 -0500584}
585
Patrick Williams5b485792016-08-02 07:35:14 -0500586} // namespace bus
587
Adriana Kobylak63122252017-01-26 15:09:00 -0600588/** @brief Get the dbus bus from the message.
589 *
590 * @return The dbus bus.
591 */
Waqar Hameede4f21df2020-11-05 15:49:21 +0100592inline auto message::message::get_bus() const
Adriana Kobylak63122252017-01-26 15:09:00 -0600593{
594 sd_bus* b = nullptr;
Patrick Venture4c3427c2018-04-13 17:09:55 -0700595 b = _intf->sd_bus_message_get_bus(_msg.get());
Patrick Ventureff095682018-04-13 20:54:32 -0700596 return bus::bus(b, _intf);
Adriana Kobylak63122252017-01-26 15:09:00 -0600597}
598
Patrick Williams5b485792016-08-02 07:35:14 -0500599} // namespace sdbusplus