| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright (C) 2025 Richard Acayan and contributors |
| 4 |
* @author: Richard Acayan |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <gio/gio.h> |
| 10 |
#include <wp/wp.h> |
| 11 |
|
| 12 |
#include "dbus-connection-state.h" |
| 13 |
|
| 14 |
#define WP_TYPE_MODEM_MANAGER wp_modem_manager_get_type () |
| 15 |
|
| 16 |
WP_DEFINE_LOCAL_LOG_TOPIC ("m-modem-manager") |
| 17 |
|
| 18 |
struct _WpModemManager { |
| 19 |
WpPlugin parent; |
| 20 |
WpPlugin *dbus; |
| 21 |
GDBusObjectManager *manager; |
| 22 |
GList *voice; |
| 23 |
GList *calls; |
| 24 |
guint n_calls; |
| 25 |
}; |
| 26 |
|
| 27 |
enum { |
| 28 |
SIGNAL_CALL_START, |
| 29 |
SIGNAL_CALL_STOP, |
| 30 |
N_SIGNALS, |
| 31 |
}; |
| 32 |
|
| 33 |
/* see ModemManager-enums.h */ |
| 34 |
enum MMCallState { |
| 35 |
MM_CALL_STATE_DIALING = 1, |
| 36 |
MM_CALL_STATE_RINGING_OUT = 2, |
| 37 |
MM_CALL_STATE_ACTIVE = 4, |
| 38 |
}; |
| 39 |
|
| 40 |
static guint signals[N_SIGNALS]; |
| 41 |
|
| 42 |
G_DECLARE_FINAL_TYPE (WpModemManager, wp_modem_manager, |
| 43 |
WP, MODEM_MANAGER, WpPlugin) |
| 44 |
|
| 45 |
G_DEFINE_TYPE (WpModemManager, wp_modem_manager, WP_TYPE_PLUGIN) |
| 46 |
|
| 47 |
static void |
| 48 |
wp_modem_manager_init (WpModemManager * self) |
| 49 |
{ |
| 50 |
} |
| 51 |
|
| 52 |
static gboolean |
| 53 |
is_active_state (gint32 state) |
| 54 |
{ |
| 55 |
return state == MM_CALL_STATE_DIALING |
| 56 |
|| state == MM_CALL_STATE_RINGING_OUT |
| 57 |
|| state == MM_CALL_STATE_ACTIVE; |
| 58 |
} |
| 59 |
|
| 60 |
static void |
| 61 |
active_calls_inc (WpModemManager * wpmm) |
| 62 |
{ |
| 63 |
wpmm->n_calls++; |
| 64 |
if (wpmm->n_calls == 1) { |
| 65 |
wp_info_object (wpmm, "modem call started"); |
| 66 |
g_signal_emit (wpmm, signals[SIGNAL_CALL_START], 0); |
| 67 |
} |
| 68 |
} |
| 69 |
static void |
| 70 |
active_calls_dec (WpModemManager * wpmm) |
| 71 |
{ |
| 72 |
wpmm->n_calls--; |
| 73 |
if (wpmm->n_calls == 0) { |
| 74 |
wp_info_object (wpmm, "modem call stopped"); |
| 75 |
g_signal_emit (wpmm, signals[SIGNAL_CALL_STOP], 0); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
static void |
| 80 |
on_call_state_change (GDBusProxy * iface, |
| 81 |
gchar * sender, |
| 82 |
gchar * signal, |
| 83 |
GVariant * params, |
| 84 |
gpointer data) |
| 85 |
{ |
| 86 |
WpModemManager *wpmm = WP_MODEM_MANAGER (data); |
| 87 |
gint32 old, new; |
| 88 |
guint32 reason; |
| 89 |
|
| 90 |
if (g_strcmp0 (signal, "StateChanged")) |
| 91 |
return; |
| 92 |
|
| 93 |
g_variant_get (params, "(iiu)", &old, &new, &reason); |
| 94 |
|
| 95 |
if (!is_active_state (old) && is_active_state (new)) { |
| 96 |
active_calls_inc (wpmm); |
| 97 |
} else if (is_active_state (old) && !is_active_state (new)) { |
| 98 |
active_calls_dec (wpmm); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
static void |
| 103 |
bind_call (GObject * obj, GAsyncResult * res, gpointer data) |
| 104 |
{ |
| 105 |
WpModemManager *wpmm = WP_MODEM_MANAGER (data); |
| 106 |
g_autoptr (GError) err = NULL; |
| 107 |
GDBusProxy *call; |
| 108 |
g_autoptr (GVariant) prop = NULL; |
| 109 |
gint init_state; |
| 110 |
|
| 111 |
call = g_dbus_proxy_new_finish (res, &err); |
| 112 |
if (call == NULL) { |
| 113 |
g_prefix_error (&err, "Failed to get call: "); |
| 114 |
wp_warning_object (wpmm, "%s", err->message); |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
prop = g_dbus_proxy_get_cached_property (call, "State"); |
| 119 |
if (prop == NULL) { |
| 120 |
wp_warning_object (wpmm, "Failed to get initial call state"); |
| 121 |
} else { |
| 122 |
g_variant_get (prop, "i", &init_state); |
| 123 |
|
| 124 |
if (is_active_state (init_state)) |
| 125 |
active_calls_inc (wpmm); |
| 126 |
} |
| 127 |
|
| 128 |
wpmm->calls = g_list_prepend (wpmm->calls, call); |
| 129 |
g_signal_connect (call, "g-signal", G_CALLBACK (on_call_state_change), wpmm); |
| 130 |
} |
| 131 |
|
| 132 |
static gint |
| 133 |
match_call_path (gconstpointer a, gconstpointer b) |
| 134 |
{ |
| 135 |
GDBusProxy *call; |
| 136 |
const gchar *path, *call_path; |
| 137 |
|
| 138 |
if (G_IS_DBUS_PROXY (a)) { |
| 139 |
call = G_DBUS_PROXY (a); |
| 140 |
path = b; |
| 141 |
} else if (G_IS_DBUS_PROXY (b)) { |
| 142 |
call = G_DBUS_PROXY (b); |
| 143 |
path = a; |
| 144 |
} else { |
| 145 |
return 1; |
| 146 |
} |
| 147 |
|
| 148 |
call_path = g_dbus_proxy_get_object_path (call); |
| 149 |
return g_strcmp0 (path, call_path); |
| 150 |
} |
| 151 |
|
| 152 |
static void |
| 153 |
on_voice_signal (GDBusProxy * iface, |
| 154 |
gchar * sender, |
| 155 |
gchar * signal, |
| 156 |
GVariant * params, |
| 157 |
gpointer data) |
| 158 |
{ |
| 159 |
WpModemManager *wpmm = WP_MODEM_MANAGER (data); |
| 160 |
GList *deleted; |
| 161 |
gchar *path; |
| 162 |
g_autoptr (GDBusConnection) conn = NULL; |
| 163 |
|
| 164 |
g_object_get (wpmm->dbus, "connection", &conn, NULL); |
| 165 |
|
| 166 |
if (!g_strcmp0 (signal, "CallAdded")) { |
| 167 |
g_variant_get (params, "(&o)", &path); |
| 168 |
g_dbus_proxy_new (conn, |
| 169 |
G_DBUS_PROXY_FLAGS_NONE, |
| 170 |
NULL, |
| 171 |
"org.freedesktop.ModemManager1", |
| 172 |
path, |
| 173 |
"org.freedesktop.ModemManager1.Call", |
| 174 |
NULL, |
| 175 |
bind_call, |
| 176 |
wpmm); |
| 177 |
} else if (!g_strcmp0 (signal, "CallDeleted")) { |
| 178 |
g_variant_get (params, "(&o)", &path); |
| 179 |
|
| 180 |
// The user shouldn't have hundreds of calls, so just linear search. |
| 181 |
deleted = g_list_find_custom (wpmm->calls, path, match_call_path); |
| 182 |
if (deleted) { |
| 183 |
g_object_unref (deleted->data); |
| 184 |
wpmm->calls = g_list_delete_link (wpmm->calls, deleted); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
static void |
| 190 |
list_calls_done (GObject * obj, |
| 191 |
GAsyncResult *res, |
| 192 |
gpointer data) |
| 193 |
{ |
| 194 |
WpModemManager *wpmm = WP_MODEM_MANAGER (data); |
| 195 |
g_autoptr (GVariant) params = NULL; |
| 196 |
g_autoptr (GVariantIter) calls = NULL; |
| 197 |
gchar *path; |
| 198 |
g_autoptr (GError) err = NULL; |
| 199 |
g_autoptr (GDBusConnection) conn = NULL; |
| 200 |
|
| 201 |
params = g_dbus_proxy_call_finish (G_DBUS_PROXY (obj), res, &err); |
| 202 |
if (params == NULL) { |
| 203 |
g_prefix_error (&err, "Failed to list active calls on startup: "); |
| 204 |
wp_warning_object (wpmm, "%s", err->message); |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
g_object_get (wpmm->dbus, "connection", &conn, NULL); |
| 209 |
|
| 210 |
g_variant_get (params, "(ao)", &calls); |
| 211 |
while (g_variant_iter_loop (calls, "&o", &path)) { |
| 212 |
g_dbus_proxy_new (conn, |
| 213 |
G_DBUS_PROXY_FLAGS_NONE, |
| 214 |
NULL, |
| 215 |
"org.freedesktop.ModemManager1", |
| 216 |
path, |
| 217 |
"org.freedesktop.ModemManager1.Call", |
| 218 |
NULL, |
| 219 |
bind_call, |
| 220 |
wpmm); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
static void |
| 225 |
hotplug_modem (GDBusObjectManager * mm, |
| 226 |
GDBusObject * obj, |
| 227 |
gpointer data) |
| 228 |
{ |
| 229 |
WpModemManager *wpmm = WP_MODEM_MANAGER (data); |
| 230 |
GDBusInterface *iface; |
| 231 |
|
| 232 |
iface = g_dbus_object_get_interface (obj, "org.freedesktop.ModemManager1.Modem.Voice"); |
| 233 |
if (iface == NULL) |
| 234 |
return; |
| 235 |
|
| 236 |
g_dbus_proxy_call (G_DBUS_PROXY (iface), |
| 237 |
"ListCalls", |
| 238 |
g_variant_new ("()"), |
| 239 |
G_DBUS_CALL_FLAGS_NONE, |
| 240 |
-1, |
| 241 |
NULL, |
| 242 |
list_calls_done, |
| 243 |
wpmm); |
| 244 |
|
| 245 |
wpmm->voice = g_list_prepend (wpmm->voice, iface); |
| 246 |
g_signal_connect (iface, "g-signal", G_CALLBACK (on_voice_signal), wpmm); |
| 247 |
} |
| 248 |
|
| 249 |
static void |
| 250 |
coldplug_modem (gpointer elt, gpointer data) |
| 251 |
{ |
| 252 |
GDBusObject *obj = elt; |
| 253 |
WpModemManager *wpmm = data; |
| 254 |
GDBusInterface *iface; |
| 255 |
|
| 256 |
iface = g_dbus_object_get_interface (obj, "org.freedesktop.ModemManager1.Modem.Voice"); |
| 257 |
if (iface == NULL) |
| 258 |
return; |
| 259 |
|
| 260 |
g_dbus_proxy_call (G_DBUS_PROXY (iface), |
| 261 |
"ListCalls", |
| 262 |
g_variant_new ("()"), |
| 263 |
G_DBUS_CALL_FLAGS_NONE, |
| 264 |
-1, |
| 265 |
NULL, |
| 266 |
list_calls_done, |
| 267 |
wpmm); |
| 268 |
|
| 269 |
wpmm->voice = g_list_prepend (wpmm->voice, iface); |
| 270 |
g_signal_connect (iface, "g-signal", G_CALLBACK (on_voice_signal), wpmm); |
| 271 |
} |
| 272 |
|
| 273 |
static void |
| 274 |
on_modemmanager_get (GObject * obj, GAsyncResult * res, gpointer data) |
| 275 |
{ |
| 276 |
WpTransition *transition = WP_TRANSITION (data); |
| 277 |
GError *err = NULL; |
| 278 |
WpModemManager *wpmm; |
| 279 |
GList *modems; |
| 280 |
|
| 281 |
if (WP_IS_TRANSITION (data)) { |
| 282 |
// from wp_modem_manager_enable |
| 283 |
transition = WP_TRANSITION (data); |
| 284 |
wpmm = wp_transition_get_source_object (transition); |
| 285 |
} else { |
| 286 |
// from on_dbus_state_changed |
| 287 |
transition = NULL; |
| 288 |
wpmm = WP_MODEM_MANAGER (data); |
| 289 |
} |
| 290 |
|
| 291 |
wpmm->manager = g_dbus_object_manager_client_new_finish (res, &err); |
| 292 |
if (wpmm->manager == NULL) { |
| 293 |
g_prefix_error (&err, "Failed to connect to ModemManager: "); |
| 294 |
wp_warning_object (wpmm, "%s", err->message); |
| 295 |
|
| 296 |
if (transition) |
| 297 |
wp_transition_return_error (transition, g_steal_pointer (&err)); |
| 298 |
|
| 299 |
g_clear_object (&wpmm->dbus); |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
modems = g_dbus_object_manager_get_objects (wpmm->manager); |
| 304 |
g_list_foreach (modems, coldplug_modem, wpmm); |
| 305 |
g_list_free_full (g_steal_pointer (&modems), g_object_unref); |
| 306 |
|
| 307 |
g_signal_connect (wpmm->manager, "object-added", G_CALLBACK (hotplug_modem), wpmm); |
| 308 |
|
| 309 |
if (WP_IS_TRANSITION (data)) |
| 310 |
wp_object_update_features (WP_OBJECT (wpmm), WP_PLUGIN_FEATURE_ENABLED, 0); |
| 311 |
} |
| 312 |
|
| 313 |
static void |
| 314 |
on_dbus_state_changed (GObject * dbus, GParamSpec * spec, gpointer data) |
| 315 |
{ |
| 316 |
WpDBusConnectionState state; |
| 317 |
WpModemManager *wpmm = WP_MODEM_MANAGER (data); |
| 318 |
g_autoptr (GDBusConnection) conn = NULL; |
| 319 |
|
| 320 |
g_object_get (wpmm->dbus, "state", &state, NULL); |
| 321 |
|
| 322 |
switch (state) { |
| 323 |
case WP_DBUS_CONNECTION_STATE_CONNECTED: |
| 324 |
g_object_get (wpmm->dbus, "connection", &conn, NULL); |
| 325 |
g_dbus_object_manager_client_new (conn, |
| 326 |
G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_DO_NOT_AUTO_START, |
| 327 |
"org.freedesktop.ModemManager1", |
| 328 |
"/org/freedesktop/ModemManager1", |
| 329 |
NULL, NULL, NULL, NULL, |
| 330 |
on_modemmanager_get, wpmm); |
| 331 |
break; |
| 332 |
|
| 333 |
case WP_DBUS_CONNECTION_STATE_CONNECTING: |
| 334 |
case WP_DBUS_CONNECTION_STATE_CLOSED: |
| 335 |
g_list_free_full (g_steal_pointer (&wpmm->calls), g_object_unref); |
| 336 |
g_list_free_full (g_steal_pointer (&wpmm->voice), g_object_unref); |
| 337 |
g_clear_object (&wpmm->manager); |
| 338 |
break; |
| 339 |
|
| 340 |
default: |
| 341 |
g_assert_not_reached (); |
| 342 |
break; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
static void |
| 347 |
wp_modem_manager_enable (WpPlugin * self, WpTransition * transition) |
| 348 |
{ |
| 349 |
WpModemManager *wpmm = WP_MODEM_MANAGER (self); |
| 350 |
g_autoptr (WpCore) core = NULL; |
| 351 |
GError *err; |
| 352 |
g_autoptr (GDBusConnection) conn = NULL; |
| 353 |
|
| 354 |
g_object_get (self, "core", &core, NULL); |
| 355 |
|
| 356 |
wpmm->dbus = wp_plugin_find (core, "system-dbus-connection"); |
| 357 |
if (!wpmm->dbus) { |
| 358 |
err = g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT, |
| 359 |
"dbus-connection module must be loaded before modem-manager"); |
| 360 |
wp_transition_return_error (transition, err); |
| 361 |
return; |
| 362 |
} |
| 363 |
|
| 364 |
g_signal_connect_object (wpmm->dbus, "notify::state", |
| 365 |
G_CALLBACK (on_dbus_state_changed), wpmm, 0); |
| 366 |
|
| 367 |
g_object_get (wpmm->dbus, "connection", &conn, NULL); |
| 368 |
g_dbus_object_manager_client_new (conn, |
| 369 |
G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_DO_NOT_AUTO_START, |
| 370 |
"org.freedesktop.ModemManager1", |
| 371 |
"/org/freedesktop/ModemManager1", |
| 372 |
NULL, NULL, NULL, NULL, |
| 373 |
on_modemmanager_get, transition); |
| 374 |
} |
| 375 |
|
| 376 |
static void |
| 377 |
wp_modem_manager_disable (WpPlugin * self) |
| 378 |
{ |
| 379 |
WpModemManager *wpmm = WP_MODEM_MANAGER (self); |
| 380 |
|
| 381 |
g_list_free_full (g_steal_pointer (&wpmm->calls), g_object_unref); |
| 382 |
g_list_free_full (g_steal_pointer (&wpmm->voice), g_object_unref); |
| 383 |
g_clear_object (&wpmm->manager); |
| 384 |
g_clear_object (&wpmm->dbus); |
| 385 |
} |
| 386 |
|
| 387 |
static void |
| 388 |
wp_modem_manager_class_init (WpModemManagerClass * klass) |
| 389 |
{ |
| 390 |
WpPluginClass *wpplugin_class = (WpPluginClass *) klass; |
| 391 |
|
| 392 |
wpplugin_class->enable = wp_modem_manager_enable; |
| 393 |
wpplugin_class->disable = wp_modem_manager_disable; |
| 394 |
|
| 395 |
signals[SIGNAL_CALL_START] = g_signal_new ("voice-call-start", |
| 396 |
WP_TYPE_MODEM_MANAGER, |
| 397 |
G_SIGNAL_RUN_LAST, |
| 398 |
0, NULL, NULL, NULL, |
| 399 |
G_TYPE_NONE, |
| 400 |
0); |
| 401 |
signals[SIGNAL_CALL_STOP] = g_signal_new ("voice-call-stop", |
| 402 |
WP_TYPE_MODEM_MANAGER, |
| 403 |
G_SIGNAL_RUN_LAST, |
| 404 |
0, NULL, NULL, NULL, |
| 405 |
G_TYPE_NONE, |
| 406 |
0); |
| 407 |
} |
| 408 |
|
| 409 |
WP_PLUGIN_EXPORT GObject * |
| 410 |
wireplumber__module_init (WpCore * core, WpSpaJson * json, GError ** err) |
| 411 |
{ |
| 412 |
return g_object_new (wp_modem_manager_get_type (), |
| 413 |
"name", "modem-manager", |
| 414 |
"core", core, |
| 415 |
NULL); |
| 416 |
} |