xref: /openbmc/sdbusplus/src/bus.cpp (revision 578b9733d30bb03015715932e9f64d11416655ca)
132ffb03dSPatrick Williams #include <sdbusplus/bus.hpp>
2*578b9733SPatrick Williams #include <sdbusplus/exception.hpp>
332ffb03dSPatrick Williams 
4fc0bb996SPatrick Williams namespace sdbusplus::bus
532ffb03dSPatrick Williams {
632ffb03dSPatrick Williams 
emit_interfaces_added(const char * path,const std::vector<std::string> & ifaces)732ffb03dSPatrick Williams void bus::emit_interfaces_added(const char* path,
832ffb03dSPatrick Williams                                 const std::vector<std::string>& ifaces)
932ffb03dSPatrick Williams {
1032ffb03dSPatrick Williams     details::Strv s{ifaces};
1132ffb03dSPatrick Williams     _intf->sd_bus_emit_interfaces_added_strv(_bus.get(), path,
1232ffb03dSPatrick Williams                                              static_cast<char**>(s));
1332ffb03dSPatrick Williams }
1432ffb03dSPatrick Williams 
emit_interfaces_removed(const char * path,const std::vector<std::string> & ifaces)1532ffb03dSPatrick Williams void bus::emit_interfaces_removed(const char* path,
1632ffb03dSPatrick Williams                                   const std::vector<std::string>& ifaces)
1732ffb03dSPatrick Williams {
1832ffb03dSPatrick Williams     details::Strv s{ifaces};
1932ffb03dSPatrick Williams     _intf->sd_bus_emit_interfaces_removed_strv(_bus.get(), path,
2032ffb03dSPatrick Williams                                                static_cast<char**>(s));
2132ffb03dSPatrick Williams }
2232ffb03dSPatrick Williams 
23fc0bb996SPatrick Williams /* Create a new default connection: system bus if root, session bus if user. */
new_default()24fc0bb996SPatrick Williams bus new_default()
25fc0bb996SPatrick Williams {
26fc0bb996SPatrick Williams     sd_bus* b = nullptr;
27*578b9733SPatrick Williams     auto rc = sd_bus_default(&b);
28*578b9733SPatrick Williams     if (rc < 0)
29*578b9733SPatrick Williams     {
30*578b9733SPatrick Williams         throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
31*578b9733SPatrick Williams     }
32fc0bb996SPatrick Williams     return bus(b, std::false_type());
33fc0bb996SPatrick Williams }
34fc0bb996SPatrick Williams 
35fc0bb996SPatrick Williams /* Create a new default connection to the session bus. */
new_default_user()36fc0bb996SPatrick Williams bus new_default_user()
37fc0bb996SPatrick Williams {
38fc0bb996SPatrick Williams     sd_bus* b = nullptr;
39*578b9733SPatrick Williams     auto rc = sd_bus_default_user(&b);
40*578b9733SPatrick Williams     if (rc < 0)
41*578b9733SPatrick Williams     {
42*578b9733SPatrick Williams         throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
43*578b9733SPatrick Williams     }
44fc0bb996SPatrick Williams     return bus(b, std::false_type());
45fc0bb996SPatrick Williams }
46fc0bb996SPatrick Williams 
47fc0bb996SPatrick Williams /* Create a new default connection to the system bus. */
new_default_system()48fc0bb996SPatrick Williams bus new_default_system()
49fc0bb996SPatrick Williams {
50fc0bb996SPatrick Williams     sd_bus* b = nullptr;
51*578b9733SPatrick Williams     auto rc = sd_bus_default_system(&b);
52*578b9733SPatrick Williams     if (rc < 0)
53*578b9733SPatrick Williams     {
54*578b9733SPatrick Williams         throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
55*578b9733SPatrick Williams     }
56fc0bb996SPatrick Williams     return bus(b, std::false_type());
57fc0bb996SPatrick Williams }
58fc0bb996SPatrick Williams 
59fc0bb996SPatrick Williams /* Create a new connection: system bus if root, session bus if user. */
new_bus()60fc0bb996SPatrick Williams bus new_bus()
61fc0bb996SPatrick Williams {
62fc0bb996SPatrick Williams     sd_bus* b = nullptr;
63*578b9733SPatrick Williams     auto rc = sd_bus_open(&b);
64*578b9733SPatrick Williams     if (rc < 0)
65*578b9733SPatrick Williams     {
66*578b9733SPatrick Williams         throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
67*578b9733SPatrick Williams     }
68fc0bb996SPatrick Williams     bus bus(b, std::false_type());
69fc0bb996SPatrick Williams     bus.set_should_close(true);
70fc0bb996SPatrick Williams     return bus;
71fc0bb996SPatrick Williams }
72fc0bb996SPatrick Williams 
73fc0bb996SPatrick Williams /* Create a new connection to the session bus. */
new_user()74fc0bb996SPatrick Williams bus new_user()
75fc0bb996SPatrick Williams {
76fc0bb996SPatrick Williams     sd_bus* b = nullptr;
77*578b9733SPatrick Williams     auto rc = sd_bus_open_user(&b);
78*578b9733SPatrick Williams     if (rc < 0)
79*578b9733SPatrick Williams     {
80*578b9733SPatrick Williams         throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
81*578b9733SPatrick Williams     }
82fc0bb996SPatrick Williams     bus bus(b, std::false_type());
83fc0bb996SPatrick Williams     bus.set_should_close(true);
84fc0bb996SPatrick Williams     return bus;
85fc0bb996SPatrick Williams }
86fc0bb996SPatrick Williams 
87fc0bb996SPatrick Williams /* Create a new connection to the system bus. */
new_system()88fc0bb996SPatrick Williams bus new_system()
89fc0bb996SPatrick Williams {
90fc0bb996SPatrick Williams     sd_bus* b = nullptr;
91*578b9733SPatrick Williams     auto rc = sd_bus_open_system(&b);
92*578b9733SPatrick Williams     if (rc < 0)
93*578b9733SPatrick Williams     {
94*578b9733SPatrick Williams         throw exception::SdBusError(-rc, __PRETTY_FUNCTION__);
95*578b9733SPatrick Williams     }
96fc0bb996SPatrick Williams     bus bus(b, std::false_type());
97fc0bb996SPatrick Williams     bus.set_should_close(true);
98fc0bb996SPatrick Williams     return bus;
99fc0bb996SPatrick Williams }
100fc0bb996SPatrick Williams 
bus(busp_t b,sdbusplus::SdBusInterface * intf)101fc0bb996SPatrick Williams bus::bus(busp_t b, sdbusplus::SdBusInterface* intf) :
102fc0bb996SPatrick Williams     _intf(intf), _bus(_intf->sd_bus_ref(b), details::BusDeleter(intf))
103fc0bb996SPatrick Williams {
104fc0bb996SPatrick Williams     // Emitting object added causes a message to get the properties
105fc0bb996SPatrick Williams     // which can trigger a 'transaction' in the server bindings.  If
106fc0bb996SPatrick Williams     // the bus isn't up far enough, this causes an assert deep in
107fc0bb996SPatrick Williams     // sd-bus code.  Get the 'unique_name' to ensure the bus is up far
108fc0bb996SPatrick Williams     // enough to avoid the assert.
109fc0bb996SPatrick Williams     if (b != nullptr)
110fc0bb996SPatrick Williams     {
111fc0bb996SPatrick Williams         get_unique_name();
112fc0bb996SPatrick Williams     }
113fc0bb996SPatrick Williams }
114fc0bb996SPatrick Williams 
bus(busp_t b)115fc0bb996SPatrick Williams bus::bus(busp_t b) :
116fc0bb996SPatrick Williams     _intf(&sdbus_impl),
117fc0bb996SPatrick Williams     _bus(_intf->sd_bus_ref(b), details::BusDeleter(&sdbus_impl))
118fc0bb996SPatrick Williams {
119fc0bb996SPatrick Williams     // Emitting object added causes a message to get the properties
120fc0bb996SPatrick Williams     // which can trigger a 'transaction' in the server bindings.  If
121fc0bb996SPatrick Williams     // the bus isn't up far enough, this causes an assert deep in
122fc0bb996SPatrick Williams     // sd-bus code.  Get the 'unique_name' to ensure the bus is up far
123fc0bb996SPatrick Williams     // enough to avoid the assert.
124fc0bb996SPatrick Williams     if (b != nullptr)
125fc0bb996SPatrick Williams     {
126fc0bb996SPatrick Williams         get_unique_name();
127fc0bb996SPatrick Williams     }
128fc0bb996SPatrick Williams }
129fc0bb996SPatrick Williams 
bus(busp_t b,std::false_type)130fc0bb996SPatrick Williams bus::bus(busp_t b, std::false_type) :
131fc0bb996SPatrick Williams     _intf(&sdbus_impl), _bus(b, details::BusDeleter(&sdbus_impl))
132fc0bb996SPatrick Williams {
133fc0bb996SPatrick Williams     // Emitting object added causes a message to get the properties
134fc0bb996SPatrick Williams     // which can trigger a 'transaction' in the server bindings.  If
135fc0bb996SPatrick Williams     // the bus isn't up far enough, this causes an assert deep in
136fc0bb996SPatrick Williams     // sd-bus code.  Get the 'unique_name' to ensure the bus is up far
137fc0bb996SPatrick Williams     // enough to avoid the assert.
138fc0bb996SPatrick Williams     if (b != nullptr)
139fc0bb996SPatrick Williams     {
140fc0bb996SPatrick Williams         get_unique_name();
141fc0bb996SPatrick Williams     }
142fc0bb996SPatrick Williams }
143fc0bb996SPatrick Williams 
144fc0bb996SPatrick Williams } // namespace sdbusplus::bus
145