Debian Codegraph @

modules/module-dbus-connection.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2022-2023 Collabora Ltd.
4 * @author Julian Bouzas <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include <wp/wp.h>
10 #include "dbus-connection-state.h"
11 #include "dbus-connection-enums.h"
12
13 WP_DEFINE_LOCAL_LOG_TOPIC ("m-dbus-connection")
14
15 enum
16 {
17 PROP_0,
18 PROP_BUS_TYPE,
19 PROP_STATE,
20 PROP_CONNECTION,
21 };
22
23 struct _WpDBusConnection
24 {
25 WpPlugin parent;
26
27 /* Props */
28 GBusType bus_type;
29 WpDBusConnectionState state;
30 GDBusConnection *connection;
31
32 GCancellable *cancellable;
33 };
34
35 static void on_connection_closed (GDBusConnection * connection,
36 gboolean remote_peer_vanished, GError * error, gpointer data);
37
38 G_DECLARE_FINAL_TYPE (WpDBusConnection, wp_dbus_connection,
39 WP, DBUS_CONNECTION, WpPlugin)
40 G_DEFINE_TYPE (WpDBusConnection, wp_dbus_connection, WP_TYPE_PLUGIN)
41
42 static void
43 wp_dbus_connection_init (WpDBusConnection * self)
44 {
45 }
46
47 static void
48 wp_dbus_connection_set_state (WpDBusConnection * self, WpDBusConnectionState new_state)
49 {
50 if (self->state != new_state) {
51 self->state = new_state;
52 g_object_notify (G_OBJECT (self), "state");
53 }
54 }
55
56 static void
57 on_got_bus (GObject * obj, GAsyncResult * res, gpointer data)
58 {
59 WpTransition *transition;
60 WpDBusConnection *self;
61 g_autoptr (GError) error = NULL;
62
63 if (WP_IS_TRANSITION (data)) {
64 // coming from wp_dbus_connection_enable
65 transition = WP_TRANSITION (data);
66 self = wp_transition_get_source_object (transition);
67 } else {
68 // coming from on_sync_reconnect
69 transition = NULL;
70 self = WP_DBUS_CONNECTION (data);
71 }
72
73 self->connection = g_dbus_connection_new_for_address_finish (res, &error);
74 if (!self->connection) {
75 if (transition) {
76 g_prefix_error (&error, "Failed to connect to bus: ");
77 wp_transition_return_error (transition, g_steal_pointer (&error));
78 }
79 return;
80 }
81
82 wp_debug_object (self, "Connected to bus");
83
84 /* set up connection */
85 g_signal_connect_object (self->connection, "closed",
86 G_CALLBACK (on_connection_closed), self, 0);
87 g_dbus_connection_set_exit_on_close (self->connection, FALSE);
88
89 wp_dbus_connection_set_state (self, WP_DBUS_CONNECTION_STATE_CONNECTED);
90
91 if (transition)
92 wp_object_update_features (WP_OBJECT (self), WP_PLUGIN_FEATURE_ENABLED, 0);
93 }
94
95 static gboolean
96 do_connect (WpDBusConnection * self, GAsyncReadyCallback callback,
97 gpointer data, GError ** error)
98 {
99 g_autofree gchar *address = NULL;
100
101 address = g_dbus_address_get_for_bus_sync (self->bus_type, NULL, error);
102 if (!address) {
103 g_prefix_error (error, "Error acquiring bus address: ");
104 return FALSE;
105 }
106
107 wp_dbus_connection_set_state (self, WP_DBUS_CONNECTION_STATE_CONNECTING);
108
109 wp_debug_object (self, "Connecting to bus: %s", address);
110 g_dbus_connection_new_for_address (address,
111 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
112 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
113 NULL, self->cancellable, callback, data);
114 return TRUE;
115 }
116
117 static void
118 on_sync_reconnect (WpCore * core, GAsyncResult * res, WpDBusConnection * self)
119 {
120 g_autoptr (GError) error = NULL;
121
122 if (!wp_core_sync_finish (core, res, &error)) {
123 wp_warning_object (self, "core sync error: %s", error->message);
124 return;
125 }
126
127 if (!do_connect (self, on_got_bus, self, &error))
128 wp_notice_object (self, "Cannot reconnect: %s", error->message);
129 }
130
131 static void
132 on_connection_closed (GDBusConnection * connection,
133 gboolean remote_peer_vanished, GError * error, gpointer data)
134 {
135 WpDBusConnection *self = WP_DBUS_CONNECTION (data);
136 g_autoptr (WpCore) core = NULL;
137
138 wp_notice_object (self, "DBus connection closed: %s", error->message);
139
140 g_clear_object (&self->connection);
141 wp_dbus_connection_set_state (self, WP_DBUS_CONNECTION_STATE_CLOSED);
142
143 /* try to reconnect on idle if connection was closed */
144 core = wp_object_get_core (WP_OBJECT (self));
145 if (core && wp_core_is_connected (core)) {
146 wp_notice_object (self, "Trying to reconnect after core sync");
147 wp_core_sync_closure (core, NULL, g_cclosure_new_object (
148 G_CALLBACK (on_sync_reconnect), G_OBJECT (self)));
149 }
150 }
151
152 static void
153 wp_dbus_connection_enable (WpPlugin * plugin, WpTransition * transition)
154 {
155 WpDBusConnection *self = WP_DBUS_CONNECTION (plugin);
156 g_autoptr (GError) error = NULL;
157 if (!do_connect (self, on_got_bus, transition, &error)) {
158 wp_transition_return_error (transition, g_steal_pointer (&error));
159 }
160 }
161
162 static void
163 wp_dbus_connection_disable (WpPlugin * plugin)
164 {
165 WpDBusConnection *self = WP_DBUS_CONNECTION (plugin);
166
167 g_cancellable_cancel (self->cancellable);
168
169 g_clear_object (&self->connection);
170 wp_dbus_connection_set_state (self, WP_DBUS_CONNECTION_STATE_CLOSED);
171
172 g_clear_object (&self->cancellable);
173 self->cancellable = g_cancellable_new ();
174 }
175
176 static void
177 wp_dbus_connection_get_property (GObject * object, guint property_id,
178 GValue * value, GParamSpec * pspec)
179 {
180 WpDBusConnection *self = WP_DBUS_CONNECTION (object);
181
182 switch (property_id) {
183 case PROP_BUS_TYPE:
184 g_value_set_enum (value, self->bus_type);
185 break;
186 case PROP_STATE:
187 g_value_set_enum (value, self->state);
188 break;
189 case PROP_CONNECTION:
190 g_value_set_object (value, self->connection);
191 break;
192 default:
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
194 break;
195 }
196 }
197
198 static void
199 wp_dbus_connection_set_property (GObject * object, guint property_id,
200 const GValue * value, GParamSpec * pspec)
201 {
202 WpDBusConnection *self = WP_DBUS_CONNECTION (object);
203
204 switch (property_id) {
205 case PROP_BUS_TYPE:
206 self->bus_type = g_value_get_enum (value);
207 break;
208 default:
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
210 break;
211 }
212 }
213
214 static void
215 wp_dbus_connection_finalize (GObject * object)
216 {
217 WpDBusConnection *self = WP_DBUS_CONNECTION (object);
218
219 g_cancellable_cancel (self->cancellable);
220
221 g_clear_object (&self->connection);
222 g_clear_object (&self->cancellable);
223
224 G_OBJECT_CLASS (wp_dbus_connection_parent_class)->finalize (object);
225 }
226
227 static void
228 wp_dbus_connection_class_init (WpDBusConnectionClass * klass)
229 {
230 GObjectClass *object_class = (GObjectClass *) klass;
231 WpPluginClass *plugin_class = (WpPluginClass *) klass;
232
233 object_class->get_property = wp_dbus_connection_get_property;
234 object_class->set_property = wp_dbus_connection_set_property;
235 object_class->finalize = wp_dbus_connection_finalize;
236
237 plugin_class->enable = wp_dbus_connection_enable;
238 plugin_class->disable = wp_dbus_connection_disable;
239
240 g_object_class_install_property (object_class, PROP_BUS_TYPE,
241 g_param_spec_enum ("bus-type", "bus-type", "The bus type",
242 G_TYPE_BUS_TYPE, G_BUS_TYPE_NONE,
243 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
244
245 g_object_class_install_property (object_class, PROP_STATE,
246 g_param_spec_enum ("state", "state", "The dbus connection state",
247 WP_TYPE_DBUS_CONNECTION_STATE, WP_DBUS_CONNECTION_STATE_CLOSED,
248 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
249
250 g_object_class_install_property (object_class, PROP_CONNECTION,
251 g_param_spec_object ("connection", "connection", "The dbus connection",
252 G_TYPE_DBUS_CONNECTION, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
253 }
254
255 WP_PLUGIN_EXPORT GObject *
256 wireplumber__module_init (WpCore * core, WpSpaJson * args, GError ** error)
257 {
258 g_autofree gchar *plugin_name = NULL;
259 gboolean is_system_bus = FALSE;
260
261 if (args)
262 wp_spa_json_object_get (args,
263 "plugin.name", "s", &plugin_name,
264 "bus.system", "b", &is_system_bus,
265 NULL);
266
267 return G_OBJECT (g_object_new (
268 wp_dbus_connection_get_type(),
269 "name", plugin_name ? plugin_name : "dbus-connection",
270 "core", core,
271 "bus-type", is_system_bus ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
272 NULL));
273 }