blob: 587d10b9b14c2766f4da0d587b4ffc822041fd9f [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
Brad Bishopd2945572017-02-15 23:40:15 -05006#include <algorithm>
Brad Bishop887ebf62016-10-12 15:40:04 -04007#include <climits>
Patrick Venture2ea15b72018-06-22 08:41:18 -07008#include <memory>
William A. Kennington III55d66862018-05-29 17:13:35 -07009#include <sdbusplus/exception.hpp>
Patrick Williams7802c072016-09-02 15:20:22 -050010#include <sdbusplus/message.hpp>
Patrick Ventureff095682018-04-13 20:54:32 -070011#include <sdbusplus/sdbus.hpp>
Patrick Venture2ea15b72018-06-22 08:41:18 -070012#include <string>
Patrick Venture2ea15b72018-06-22 08:41:18 -070013#include <vector>
Patrick Ventureff095682018-04-13 20:54:32 -070014
Patrick Williams5b485792016-08-02 07:35:14 -050015namespace sdbusplus
16{
Patrick Williams13f1ef72016-10-17 14:09:33 -050017
18// Forward declare.
Patrick Ventureff095682018-04-13 20:54:32 -070019namespace server
20{
21namespace interface
22{
23struct interface;
24}
25} // namespace server
26namespace server
27{
28namespace manager
29{
30struct manager;
31}
32} // namespace server
33namespace server
34{
35namespace object
36{
Brad Bishopb4cc7d72018-12-06 15:27:48 -050037template <class...>
38struct object;
Patrick Ventureff095682018-04-13 20:54:32 -070039}
40} // namespace server
41namespace bus
42{
43namespace match
44{
45struct match;
46}
47} // namespace bus
Patrick Williams13f1ef72016-10-17 14:09:33 -050048
Patrick Williams5b485792016-08-02 07:35:14 -050049namespace bus
50{
51
52using busp_t = sd_bus*;
53class bus;
54
55/** @brief Get an instance of the 'default' bus. */
56bus new_default();
57/** @brief Get an instance of the 'user' session bus. */
58bus new_user();
59/** @brief Get an instance of the 'system' bus. */
60bus new_system();
61
62namespace details
63{
64
65/** @brief unique_ptr functor to release a bus reference. */
66struct BusDeleter
67{
Brad Bishop75834602018-12-11 08:42:53 -050068 BusDeleter() = delete;
69 explicit BusDeleter(SdBusInterface* interface) : m_interface(interface)
Patrick Williams5b485792016-08-02 07:35:14 -050070 {
Patrick Williams5b485792016-08-02 07:35:14 -050071 }
Patrick Williamsba6f50c2017-04-18 11:38:30 -050072
Brad Bishop75834602018-12-11 08:42:53 -050073 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 Williams5b485792016-08-02 07:35:14 -050081};
82
Brad Bishopd2945572017-02-15 23:40:15 -050083/** @brief Convert a vector of strings to c-style char** array. */
84class Strv
85{
Brad Bishopb4cc7d72018-12-06 15:27:48 -050086 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 Bishopd2945572017-02-15 23:40:15 -050093
Brad Bishopb4cc7d72018-12-06 15:27:48 -050094 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 Bishopd2945572017-02-15 23:40:15 -0500100
Brad Bishopb4cc7d72018-12-06 15:27:48 -0500101 explicit operator char**()
102 {
103 return const_cast<char**>(&ptrs.front());
104 }
Brad Bishopd2945572017-02-15 23:40:15 -0500105
Brad Bishopb4cc7d72018-12-06 15:27:48 -0500106 private:
107 std::vector<const char*> ptrs;
Brad Bishopd2945572017-02-15 23:40:15 -0500108};
109
Patrick Williams5b485792016-08-02 07:35:14 -0500110/* @brief Alias 'bus' to a unique_ptr type for auto-release. */
111using 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 */
118struct bus
119{
Patrick Ventureff095682018-04-13 20:54:32 -0700120 /* 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 Williams5b485792016-08-02 07:35:14 -0500128 bus() = delete;
129 bus(const bus&) = delete;
130 bus& operator=(const bus&) = delete;
Patrick Ventureff095682018-04-13 20:54:32 -0700131
Patrick Williams5b485792016-08-02 07:35:14 -0500132 bus(bus&&) = default;
133 bus& operator=(bus&&) = default;
134 ~bus() = default;
135
Patrick Ventureff095682018-04-13 20:54:32 -0700136 bus(busp_t b, sdbusplus::SdBusInterface* intf);
137
Patrick Williams5b485792016-08-02 07:35:14 -0500138 /** @brief Conversion constructor from 'busp_t'.
139 *
Patrick Williamsba6f50c2017-04-18 11:38:30 -0500140 * Increments ref-count of the bus-pointer and releases it when done.
Patrick Williams5b485792016-08-02 07:35:14 -0500141 */
Patrick Williamsbee1a6a2017-02-16 16:06:51 -0600142 explicit bus(busp_t b);
Patrick Williams5b485792016-08-02 07:35:14 -0500143
Patrick Williamsba6f50c2017-04-18 11:38:30 -0500144 /** @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 Williams5b485792016-08-02 07:35:14 -0500152 /** @brief Release ownership of the stored bus-pointer. */
Patrick Ventureff095682018-04-13 20:54:32 -0700153 busp_t release()
154 {
155 return _bus.release();
156 }
Patrick Williams5b485792016-08-02 07:35:14 -0500157
William A. Kennington III42f6ad52018-12-19 17:32:30 -0800158 /** @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 Williams5b485792016-08-02 07:35:14 -0500174 /** @brief Wait for new dbus messages or signals.
175 *
176 * @param[in] timeout_us - Timeout in usec.
177 */
Brad Bishop887ebf62016-10-12 15:40:04 -0400178 void wait(uint64_t timeout_us = ULLONG_MAX)
Patrick Williams5b485792016-08-02 07:35:14 -0500179 {
Patrick Ventureff095682018-04-13 20:54:32 -0700180 _intf->sd_bus_wait(_bus.get(), timeout_us);
Patrick Williams5b485792016-08-02 07:35:14 -0500181 }
182
183 /** @brief Process waiting dbus messages or signals. */
184 auto process()
185 {
186 sd_bus_message* m = nullptr;
William A. Kennington III49e4bdc2018-06-05 23:28:02 -0700187 int r = _intf->sd_bus_process(_bus.get(), &m);
188 if (r < 0)
189 {
190 throw exception::SdBusError(-r, "sd_bus_process");
191 }
Patrick Williams5b485792016-08-02 07:35:14 -0500192
Patrick Ventureff095682018-04-13 20:54:32 -0700193 return message::message(m, _intf, std::false_type());
Patrick Williams5b485792016-08-02 07:35:14 -0500194 }
195
Patrick Williamsc7ba66f2016-10-18 11:30:07 -0500196 /** @brief Process waiting dbus messages or signals, discarding unhandled.
197 */
James Feistb5755182018-07-16 10:30:08 -0700198 auto process_discard()
Patrick Williamsc7ba66f2016-10-18 11:30:07 -0500199 {
William A. Kennington III49e4bdc2018-06-05 23:28:02 -0700200 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 Feistb5755182018-07-16 10:30:08 -0700205 return r > 0;
Patrick Williamsc7ba66f2016-10-18 11:30:07 -0500206 }
207
Patrick Williams5b485792016-08-02 07:35:14 -0500208 /** @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 Hameed841d8d32018-10-04 13:35:43 +0200214 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 Williams5b485792016-08-02 07:35:14 -0500219 }
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 IIIec9236d2018-06-05 23:30:15 -0700234 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 Williams5b485792016-08-02 07:35:14 -0500240
Patrick Ventureff095682018-04-13 20:54:32 -0700241 return message::message(m, _intf, std::false_type());
Patrick Williams5b485792016-08-02 07:35:14 -0500242 }
243
Patrick Williams20d82462016-10-18 08:01:33 -0500244 /** @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 IIIec9236d2018-06-05 23:30:15 -0700255 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 Williams20d82462016-10-18 08:01:33 -0500261
Patrick Ventureff095682018-04-13 20:54:32 -0700262 return message::message(m, _intf, std::false_type());
Patrick Williams20d82462016-10-18 08:01:33 -0500263 }
264
Patrick Williams5b485792016-08-02 07:35:14 -0500265 /** @brief Perform a message call.
William A. Kennington III079fb852018-10-12 13:36:31 -0700266 * 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 Williams5b485792016-08-02 07:35:14 -0500270 *
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 Williams7802c072016-09-02 15:20:22 -0500276 auto call(message::message& m, uint64_t timeout_us = 0)
Patrick Williams5b485792016-08-02 07:35:14 -0500277 {
William A. Kennington III55d66862018-05-29 17:13:35 -0700278 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Williams5b485792016-08-02 07:35:14 -0500279 sd_bus_message* reply = nullptr;
Brad Bishopb4cc7d72018-12-06 15:27:48 -0500280 int r =
281 _intf->sd_bus_call(_bus.get(), m.get(), timeout_us, &error, &reply);
William A. Kennington III55d66862018-05-29 17:13:35 -0700282 if (r < 0)
283 {
William A. Kennington III68cb1702018-06-22 19:35:48 -0700284 throw exception::SdBusError(&error, "sd_bus_call");
William A. Kennington III55d66862018-05-29 17:13:35 -0700285 }
Patrick Williams5b485792016-08-02 07:35:14 -0500286
Patrick Ventureff095682018-04-13 20:54:32 -0700287 return message::message(reply, _intf, std::false_type());
Patrick Williams5b485792016-08-02 07:35:14 -0500288 }
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 Williams7802c072016-09-02 15:20:22 -0500295 void call_noreply(message::message& m, uint64_t timeout_us = 0)
Patrick Williams5b485792016-08-02 07:35:14 -0500296 {
William A. Kennington III55d66862018-05-29 17:13:35 -0700297 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 III68cb1702018-06-22 19:35:48 -0700302 throw exception::SdBusError(&error, "sd_bus_call noreply");
William A. Kennington III55d66862018-05-29 17:13:35 -0700303 }
Patrick Williams5b485792016-08-02 07:35:14 -0500304 }
305
William A. Kennington III5b701c62018-09-11 00:36:30 -0700306 /** @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 Kobylakc5496772017-01-04 09:57:23 -0600324 /** @brief Get the bus unique name. Ex: ":1.11".
Patrick Ventureff095682018-04-13 20:54:32 -0700325 *
326 * @return The bus unique name.
327 */
Adriana Kobylakc5496772017-01-04 09:57:23 -0600328 auto get_unique_name()
329 {
330 const char* unique = nullptr;
Patrick Ventureff095682018-04-13 20:54:32 -0700331 _intf->sd_bus_get_unique_name(_bus.get(), &unique);
Adriana Kobylakc5496772017-01-04 09:57:23 -0600332 return std::string(unique);
333 }
334
Brad Bishopb4cc7d72018-12-06 15:27:48 -0500335 auto get_fd()
336 {
William A. Kennington IIIcb582e02018-06-28 18:01:01 -0700337 return _intf->sd_bus_get_fd(_bus.get());
James Feist284a0f92018-04-05 15:28:16 -0700338 }
339
Yi Li8ac39ee2017-01-16 16:21:51 +0800340 /** @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 Ventureff095682018-04-13 20:54:32 -0700347 _intf->sd_bus_attach_event(_bus.get(), event, priority);
Yi Li8ac39ee2017-01-16 16:21:51 +0800348 }
349
350 /** @brief Detach the bus from its sd-event event loop object */
351 void detach_event()
352 {
Patrick Ventureff095682018-04-13 20:54:32 -0700353 _intf->sd_bus_detach_event(_bus.get());
Yi Li8ac39ee2017-01-16 16:21:51 +0800354 }
355
356 /** @brief Get the sd-event event loop object of the bus */
357 auto get_event()
358 {
Patrick Ventureff095682018-04-13 20:54:32 -0700359 return _intf->sd_bus_get_event(_bus.get());
Yi Li8ac39ee2017-01-16 16:21:51 +0800360 }
361
Brad Bishopd2945572017-02-15 23:40:15 -0500362 /** @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 Ventureff095682018-04-13 20:54:32 -0700375 _intf->sd_bus_emit_interfaces_added_strv(_bus.get(), path,
376 static_cast<char**>(s));
Brad Bishopd2945572017-02-15 23:40:15 -0500377 }
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 Ventureff095682018-04-13 20:54:32 -0700392 _intf->sd_bus_emit_interfaces_removed_strv(_bus.get(), path,
393 static_cast<char**>(s));
Brad Bishopd2945572017-02-15 23:40:15 -0500394 }
395
Brad Bishop9a0baf52017-02-03 20:05:22 -0500396 /** @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 Ventureff095682018-04-13 20:54:32 -0700406 _intf->sd_bus_emit_object_added(_bus.get(), path);
Brad Bishop9a0baf52017-02-03 20:05:22 -0500407 }
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 Ventureff095682018-04-13 20:54:32 -0700419 _intf->sd_bus_emit_object_removed(_bus.get(), path);
Brad Bishop9a0baf52017-02-03 20:05:22 -0500420 }
421
Patrick Williamsb4041d42017-04-27 21:49:00 -0500422 /** @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 Ventureff095682018-04-13 20:54:32 -0700431 _intf->sd_bus_list_names(_bus.get(), &names, nullptr);
Patrick Williamsb4041d42017-04-27 21:49:00 -0500432
433 std::vector<std::string> result;
Patrick Ventureff095682018-04-13 20:54:32 -0700434 for (auto ptr = names; ptr && *ptr; ++ptr)
Patrick Williamsb4041d42017-04-27 21:49:00 -0500435 {
436 result.push_back(*ptr);
437 free(*ptr);
438 }
439 free(names);
440
441 return result;
442 }
443
Patrick Ventureb9a1e192018-04-16 09:40:21 -0700444 /** @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 Williams13f1ef72016-10-17 14:09:33 -0500453 friend struct server::interface::interface;
Patrick Williams05aab8b2016-10-17 14:12:12 -0500454 friend struct server::manager::manager;
Brad Bishopb4cc7d72018-12-06 15:27:48 -0500455 template <class... Args>
456 friend struct server::object::object;
Christian Andersenc69def62016-12-20 13:51:52 +0100457 friend struct match::match;
Patrick Williams13f1ef72016-10-17 14:09:33 -0500458
James Feist284a0f92018-04-05 15:28:16 -0700459 protected:
Patrick Ventureff095682018-04-13 20:54:32 -0700460 busp_t get()
461 {
462 return _bus.get();
463 }
464 sdbusplus::SdBusInterface* _intf;
465 details::bus _bus;
Patrick Williams5b485792016-08-02 07:35:14 -0500466};
467
Patrick Ventureff095682018-04-13 20:54:32 -0700468inline bus::bus(busp_t b, sdbusplus::SdBusInterface* intf) :
Brad Bishop75834602018-12-11 08:42:53 -0500469 _intf(intf), _bus(_intf->sd_bus_ref(b), details::BusDeleter(intf))
Patrick Williamsba6f50c2017-04-18 11:38:30 -0500470{
Brad Bishop75834602018-12-11 08:42:53 -0500471 _bus.get_deleter().deleter = &SdBusInterface::sd_bus_unref;
Patrick Williamsba6f50c2017-04-18 11:38:30 -0500472
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 Bishop75834602018-12-11 08:42:53 -0500486inline bus::bus(busp_t b) :
487 _intf(&sdbus_impl),
488 _bus(_intf->sd_bus_ref(b), details::BusDeleter(&sdbus_impl))
Patrick Williamsbee1a6a2017-02-16 16:06:51 -0600489{
Brad Bishop75834602018-12-11 08:42:53 -0500490 _bus.get_deleter().deleter = &SdBusInterface::sd_bus_unref;
Patrick Ventureff095682018-04-13 20:54:32 -0700491
Adriana Kobylakfd43ef72017-02-12 09:12:37 -0600492#if @WANT_TRANSACTION@
Patrick Williamsbee1a6a2017-02-16 16:06:51 -0600493 // 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 Bishop75834602018-12-11 08:42:53 -0500505inline bus::bus(busp_t b, std::false_type) :
506 _intf(&sdbus_impl), _bus(b, details::BusDeleter(&sdbus_impl))
Patrick Ventureff095682018-04-13 20:54:32 -0700507{
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 Williamsbee1a6a2017-02-16 16:06:51 -0600520
Vernon Mauery8ca60252018-11-08 14:55:34 -0800521/* Create a new default connection: system bus if root, session bus if user */
Brad Bishopaed81792016-10-12 08:40:34 -0400522inline bus new_default()
Patrick Williams5b485792016-08-02 07:35:14 -0500523{
524 sd_bus* b = nullptr;
Vernon Mauery8ca60252018-11-08 14:55:34 -0800525 sd_bus_default(&b);
Patrick Williamsba6f50c2017-04-18 11:38:30 -0500526 return bus(b, std::false_type());
Patrick Williams5b485792016-08-02 07:35:14 -0500527}
528
Vernon Mauery8ca60252018-11-08 14:55:34 -0800529/* Create a new default connection to the session bus */
530inline 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 */
538inline 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 Bishopaed81792016-10-12 08:40:34 -0400546inline bus new_user()
Patrick Williams5b485792016-08-02 07:35:14 -0500547{
548 sd_bus* b = nullptr;
549 sd_bus_open_user(&b);
Patrick Williamsba6f50c2017-04-18 11:38:30 -0500550 return bus(b, std::false_type());
Patrick Williams5b485792016-08-02 07:35:14 -0500551}
552
Brad Bishopaed81792016-10-12 08:40:34 -0400553inline bus new_system()
Patrick Williams5b485792016-08-02 07:35:14 -0500554{
555 sd_bus* b = nullptr;
556 sd_bus_open_system(&b);
Patrick Williamsba6f50c2017-04-18 11:38:30 -0500557 return bus(b, std::false_type());
Patrick Williams5b485792016-08-02 07:35:14 -0500558}
559
Patrick Williams5b485792016-08-02 07:35:14 -0500560} // namespace bus
561
Adriana Kobylak63122252017-01-26 15:09:00 -0600562/** @brief Get the dbus bus from the message.
563 *
564 * @return The dbus bus.
565 */
566inline auto message::message::get_bus()
567{
568 sd_bus* b = nullptr;
Patrick Venture4c3427c2018-04-13 17:09:55 -0700569 b = _intf->sd_bus_message_get_bus(_msg.get());
Patrick Ventureff095682018-04-13 20:54:32 -0700570 return bus::bus(b, _intf);
Adriana Kobylak63122252017-01-26 15:09:00 -0600571}
572
Patrick Williams5b485792016-08-02 07:35:14 -0500573} // namespace sdbusplus