blob: 95dfc61d5c9cbbe1bb49a39934842de8c0585908 [file] [log] [blame]
Patrick Williams32ffb032020-10-12 12:17:48 -05001#include <sdbusplus/bus.hpp>
Patrick Williams578b9732024-10-11 09:50:23 -04002#include <sdbusplus/exception.hpp>
Patrick Williams32ffb032020-10-12 12:17:48 -05003
Patrick Williamsfc0bb992024-10-11 09:42:07 -04004namespace sdbusplus::bus
Patrick Williams32ffb032020-10-12 12:17:48 -05005{
6
7void bus::emit_interfaces_added(const char* path,
8 const std::vector<std::string>& ifaces)
9{
10 details::Strv s{ifaces};
11 _intf->sd_bus_emit_interfaces_added_strv(_bus.get(), path,
12 static_cast<char**>(s));
13}
14
15void bus::emit_interfaces_removed(const char* path,
16 const std::vector<std::string>& ifaces)
17{
18 details::Strv s{ifaces};
19 _intf->sd_bus_emit_interfaces_removed_strv(_bus.get(), path,
20 static_cast<char**>(s));
21}
22
Patrick Williamsfc0bb992024-10-11 09:42:07 -040023/* Create a new default connection: system bus if root, session bus if user. */
24bus new_default()
25{
26 sd_bus* b = nullptr;
Patrick Williams578b9732024-10-11 09:50:23 -040027 auto rc = sd_bus_default(&b);
28 if (rc < 0)
29 {
30 throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
31 }
Patrick Williamsfc0bb992024-10-11 09:42:07 -040032 return bus(b, std::false_type());
33}
34
35/* Create a new default connection to the session bus. */
36bus new_default_user()
37{
38 sd_bus* b = nullptr;
Patrick Williams578b9732024-10-11 09:50:23 -040039 auto rc = sd_bus_default_user(&b);
40 if (rc < 0)
41 {
42 throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
43 }
Patrick Williamsfc0bb992024-10-11 09:42:07 -040044 return bus(b, std::false_type());
45}
46
47/* Create a new default connection to the system bus. */
48bus new_default_system()
49{
50 sd_bus* b = nullptr;
Patrick Williams578b9732024-10-11 09:50:23 -040051 auto rc = sd_bus_default_system(&b);
52 if (rc < 0)
53 {
54 throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
55 }
Patrick Williamsfc0bb992024-10-11 09:42:07 -040056 return bus(b, std::false_type());
57}
58
59/* Create a new connection: system bus if root, session bus if user. */
60bus new_bus()
61{
62 sd_bus* b = nullptr;
Patrick Williams578b9732024-10-11 09:50:23 -040063 auto rc = sd_bus_open(&b);
64 if (rc < 0)
65 {
66 throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
67 }
Patrick Williamsfc0bb992024-10-11 09:42:07 -040068 bus bus(b, std::false_type());
69 bus.set_should_close(true);
70 return bus;
71}
72
73/* Create a new connection to the session bus. */
74bus new_user()
75{
76 sd_bus* b = nullptr;
Patrick Williams578b9732024-10-11 09:50:23 -040077 auto rc = sd_bus_open_user(&b);
78 if (rc < 0)
79 {
80 throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
81 }
Patrick Williamsfc0bb992024-10-11 09:42:07 -040082 bus bus(b, std::false_type());
83 bus.set_should_close(true);
84 return bus;
85}
86
87/* Create a new connection to the system bus. */
88bus new_system()
89{
90 sd_bus* b = nullptr;
Patrick Williams578b9732024-10-11 09:50:23 -040091 auto rc = sd_bus_open_system(&b);
92 if (rc < 0)
93 {
94 throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
95 }
Patrick Williamsfc0bb992024-10-11 09:42:07 -040096 bus bus(b, std::false_type());
97 bus.set_should_close(true);
98 return bus;
99}
100
101bus::bus(busp_t b, sdbusplus::SdBusInterface* intf) :
102 _intf(intf), _bus(_intf->sd_bus_ref(b), details::BusDeleter(intf))
103{
104 // Emitting object added causes a message to get the properties
105 // which can trigger a 'transaction' in the server bindings. If
106 // the bus isn't up far enough, this causes an assert deep in
107 // sd-bus code. Get the 'unique_name' to ensure the bus is up far
108 // enough to avoid the assert.
109 if (b != nullptr)
110 {
111 get_unique_name();
112 }
113}
114
115bus::bus(busp_t b) :
116 _intf(&sdbus_impl),
117 _bus(_intf->sd_bus_ref(b), details::BusDeleter(&sdbus_impl))
118{
119 // Emitting object added causes a message to get the properties
120 // which can trigger a 'transaction' in the server bindings. If
121 // the bus isn't up far enough, this causes an assert deep in
122 // sd-bus code. Get the 'unique_name' to ensure the bus is up far
123 // enough to avoid the assert.
124 if (b != nullptr)
125 {
126 get_unique_name();
127 }
128}
129
130bus::bus(busp_t b, std::false_type) :
131 _intf(&sdbus_impl), _bus(b, details::BusDeleter(&sdbus_impl))
132{
133 // Emitting object added causes a message to get the properties
134 // which can trigger a 'transaction' in the server bindings. If
135 // the bus isn't up far enough, this causes an assert deep in
136 // sd-bus code. Get the 'unique_name' to ensure the bus is up far
137 // enough to avoid the assert.
138 if (b != nullptr)
139 {
140 get_unique_name();
141 }
142}
143
144} // namespace sdbusplus::bus