Patrick Williams | 32ffb03 | 2020-10-12 12:17:48 -0500 | [diff] [blame] | 1 | #include <sdbusplus/bus.hpp> |
Patrick Williams | 578b973 | 2024-10-11 09:50:23 -0400 | [diff] [blame] | 2 | #include <sdbusplus/exception.hpp> |
Patrick Williams | 32ffb03 | 2020-10-12 12:17:48 -0500 | [diff] [blame] | 3 | |
Patrick Williams | fc0bb99 | 2024-10-11 09:42:07 -0400 | [diff] [blame] | 4 | namespace sdbusplus::bus |
Patrick Williams | 32ffb03 | 2020-10-12 12:17:48 -0500 | [diff] [blame] | 5 | { |
| 6 | |
| 7 | void 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 | |
| 15 | void 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 Williams | fc0bb99 | 2024-10-11 09:42:07 -0400 | [diff] [blame] | 23 | /* Create a new default connection: system bus if root, session bus if user. */ |
| 24 | bus new_default() |
| 25 | { |
| 26 | sd_bus* b = nullptr; |
Patrick Williams | 578b973 | 2024-10-11 09:50:23 -0400 | [diff] [blame] | 27 | auto rc = sd_bus_default(&b); |
| 28 | if (rc < 0) |
| 29 | { |
| 30 | throw exception::SdBusError(-rc, __PRETTY_FUNCTION__); |
| 31 | } |
Patrick Williams | fc0bb99 | 2024-10-11 09:42:07 -0400 | [diff] [blame] | 32 | return bus(b, std::false_type()); |
| 33 | } |
| 34 | |
| 35 | /* Create a new default connection to the session bus. */ |
| 36 | bus new_default_user() |
| 37 | { |
| 38 | sd_bus* b = nullptr; |
Patrick Williams | 578b973 | 2024-10-11 09:50:23 -0400 | [diff] [blame] | 39 | auto rc = sd_bus_default_user(&b); |
| 40 | if (rc < 0) |
| 41 | { |
| 42 | throw exception::SdBusError(-rc, __PRETTY_FUNCTION__); |
| 43 | } |
Patrick Williams | fc0bb99 | 2024-10-11 09:42:07 -0400 | [diff] [blame] | 44 | return bus(b, std::false_type()); |
| 45 | } |
| 46 | |
| 47 | /* Create a new default connection to the system bus. */ |
| 48 | bus new_default_system() |
| 49 | { |
| 50 | sd_bus* b = nullptr; |
Patrick Williams | 578b973 | 2024-10-11 09:50:23 -0400 | [diff] [blame] | 51 | auto rc = sd_bus_default_system(&b); |
| 52 | if (rc < 0) |
| 53 | { |
| 54 | throw exception::SdBusError(-rc, __PRETTY_FUNCTION__); |
| 55 | } |
Patrick Williams | fc0bb99 | 2024-10-11 09:42:07 -0400 | [diff] [blame] | 56 | return bus(b, std::false_type()); |
| 57 | } |
| 58 | |
| 59 | /* Create a new connection: system bus if root, session bus if user. */ |
| 60 | bus new_bus() |
| 61 | { |
| 62 | sd_bus* b = nullptr; |
Patrick Williams | 578b973 | 2024-10-11 09:50:23 -0400 | [diff] [blame] | 63 | auto rc = sd_bus_open(&b); |
| 64 | if (rc < 0) |
| 65 | { |
| 66 | throw exception::SdBusError(-rc, __PRETTY_FUNCTION__); |
| 67 | } |
Patrick Williams | fc0bb99 | 2024-10-11 09:42:07 -0400 | [diff] [blame] | 68 | 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. */ |
| 74 | bus new_user() |
| 75 | { |
| 76 | sd_bus* b = nullptr; |
Patrick Williams | 578b973 | 2024-10-11 09:50:23 -0400 | [diff] [blame] | 77 | auto rc = sd_bus_open_user(&b); |
| 78 | if (rc < 0) |
| 79 | { |
| 80 | throw exception::SdBusError(-rc, __PRETTY_FUNCTION__); |
| 81 | } |
Patrick Williams | fc0bb99 | 2024-10-11 09:42:07 -0400 | [diff] [blame] | 82 | 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. */ |
| 88 | bus new_system() |
| 89 | { |
| 90 | sd_bus* b = nullptr; |
Patrick Williams | 578b973 | 2024-10-11 09:50:23 -0400 | [diff] [blame] | 91 | auto rc = sd_bus_open_system(&b); |
| 92 | if (rc < 0) |
| 93 | { |
| 94 | throw exception::SdBusError(-rc, __PRETTY_FUNCTION__); |
| 95 | } |
Patrick Williams | fc0bb99 | 2024-10-11 09:42:07 -0400 | [diff] [blame] | 96 | bus bus(b, std::false_type()); |
| 97 | bus.set_should_close(true); |
| 98 | return bus; |
| 99 | } |
| 100 | |
| 101 | bus::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 | |
| 115 | bus::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 | |
| 130 | bus::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 |