summaryrefslogtreecommitdiff
path: root/src/devices/wwan/nm-modem-manager.c
blob: 2b7e87c80c33fae51028e35d2b584db6248ff542 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2009 - 2014 Red Hat, Inc.
 * Copyright (C) 2009 Novell, Inc.
 * Copyright (C) 2009 - 2013 Canonical Ltd.
 */

#include "nm-default.h"

#include "nm-modem-manager.h"

#include <string.h>
#include <libmm-glib.h>

#if HAVE_LIBSYSTEMD
#include <systemd/sd-daemon.h>
#else
#define sd_booted() FALSE
#endif

#include "nm-dbus-compat.h"
#include "nm-modem.h"
#include "nm-modem-broadband.h"

#if WITH_OFONO
#include "nm-modem-ofono.h"
#endif

#define MODEM_POKE_INTERVAL 120

G_DEFINE_TYPE (NMModemManager, nm_modem_manager, G_TYPE_OBJECT)

struct _NMModemManagerPrivate {
	GDBusConnection *dbus_connection;
	MMManager *modem_manager;
	guint mm_launch_id;
	gulong mm_name_owner_changed_id;
	gulong mm_object_added_id;
	gulong mm_object_removed_id;

#if WITH_OFONO
	GDBusProxy *ofono_proxy;

	gulong ofono_name_owner_changed_id;
#endif

	/* Common */
	GHashTable *modems;
};

enum {
	MODEM_ADDED,
	LAST_SIGNAL,
};
static guint signals[LAST_SIGNAL] = { 0 };

/************************************************************************/

static void
handle_new_modem (NMModemManager *self, NMModem *modem)
{
	const char *path;

	path = nm_modem_get_path (modem);
	if (g_hash_table_lookup (self->priv->modems, path)) {
		g_warn_if_reached ();
		return;
	}

	/* Track the new modem */
	g_hash_table_insert (self->priv->modems, g_strdup (path), modem);
	g_signal_emit (self, signals[MODEM_ADDED], 0, modem);
}

static gboolean
remove_one_modem (gpointer key, gpointer value, gpointer user_data)
{
	nm_modem_emit_removed (NM_MODEM (value));
	return TRUE;
}

static void
clear_modem_manager (NMModemManager *self)
{
	if (!self->priv->modem_manager)
		return;
	nm_clear_g_signal_handler (self->priv->modem_manager, &self->priv->mm_name_owner_changed_id);
	nm_clear_g_signal_handler (self->priv->modem_manager, &self->priv->mm_object_added_id);
	nm_clear_g_signal_handler (self->priv->modem_manager, &self->priv->mm_object_removed_id);
	g_clear_object (&self->priv->modem_manager);
}

static void
modem_object_added (MMManager *modem_manager,
                    MMObject  *modem_object,
                    NMModemManager *self)
{
	const gchar *path;
	MMModem *modem_iface;
	NMModem *modem;
	GError *error = NULL;

	/* Ensure we don't have the same modem already */
	path = mm_object_get_path (modem_object);
	if (g_hash_table_lookup (self->priv->modems, path)) {
		nm_log_warn (LOGD_MB, "modem with path %s already exists, ignoring", path);
		return;
	}

	/* Ensure we have the 'Modem' interface at least */
	modem_iface = mm_object_peek_modem (modem_object);
	if (!modem_iface) {
		nm_log_warn (LOGD_MB, "modem with path %s doesn't have the Modem interface, ignoring", path);
		return;
	}

	/* Ensure we have a primary port reported */
	if (!mm_modem_get_primary_port (modem_iface)) {
		nm_log_warn (LOGD_MB, "modem with path %s has unknown primary port, ignoring", path);
		return;
	}

	/* Create a new modem object */
	modem = nm_modem_broadband_new (G_OBJECT (modem_object), &error);
	if (modem)
		handle_new_modem (self, modem);
	else {
		nm_log_warn (LOGD_MB, "failed to create modem: %s",
		             error->message);
	}
	g_clear_error (&error);
}

static void
modem_object_removed (MMManager *manager,
                      MMObject  *modem_object,
                      NMModemManager *self)
{
	NMModem *modem;
	const gchar *path;

	path = mm_object_get_path (modem_object);
	modem = (NMModem *) g_hash_table_lookup (self->priv->modems, path);
	if (!modem)
		return;

	nm_modem_emit_removed (modem);
	g_hash_table_remove (self->priv->modems, path);
}

static void
modem_manager_available (NMModemManager *self)
{
	GList *modems, *l;

	nm_log_info (LOGD_MB, "ModemManager available in the bus");

	/* Update initial modems list */
	modems = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (self->priv->modem_manager));
	for (l = modems; l; l = g_list_next (l))
		modem_object_added (self->priv->modem_manager, MM_OBJECT (l->data), self);
	g_list_free_full (modems, (GDestroyNotify) g_object_unref);
}

static void schedule_modem_manager_relaunch (NMModemManager *self,
                                             guint n_seconds);
static void ensure_client                   (NMModemManager *self);

static void
modem_manager_name_owner_changed (MMManager *modem_manager,
                                  GParamSpec *pspec,
                                  NMModemManager *self)
{
	gchar *name_owner;

	/* Quit poking, if any */
	nm_clear_g_source (&self->priv->mm_launch_id);

	name_owner = g_dbus_object_manager_client_get_name_owner (G_DBUS_OBJECT_MANAGER_CLIENT (modem_manager));
	if (!name_owner) {
		nm_log_info (LOGD_MB, "ModemManager disappeared from bus");

		/* If not managed by systemd, schedule relaunch */
		if (!sd_booted ())
			schedule_modem_manager_relaunch (self, 0);

		return;
	}

	/* Available! */
	g_free (name_owner);

	/* Hack alert: GDBusObjectManagerClient won't signal neither 'object-added'
	 * nor 'object-removed' if it was created while there was no ModemManager in
	 * the bus. This hack avoids this issue until we get a GIO with the fix
	 * included... */
	clear_modem_manager (self);
	ensure_client (self);

	/* Whenever GDBusObjectManagerClient is fixed, we can just do the following:
	 * modem_manager_available (self);
	 */
}

#if WITH_OFONO
static void
clear_ofono_proxy (NMModemManager *self)
{
	if (!self->priv->ofono_proxy)
		return;

	nm_clear_g_signal_handler (self->priv->ofono_proxy, &self->priv->ofono_name_owner_changed_id);
	g_clear_object (&self->priv->ofono_proxy);
}

static void
ofono_create_modem (NMModemManager *self, const char *path)
{
	NMModem *modem = NULL;

	if (g_hash_table_lookup (self->priv->modems, path)) {
		nm_log_warn (LOGD_MB, "modem with path %s already exists, ignoring", path);
		return;
	}

	/* Create modem instance */
	modem = nm_modem_ofono_new (path);
	if (modem)
		handle_new_modem (self, modem);
	else
		nm_log_warn (LOGD_MB, "Failed to create oFono modem for %s", path);
}

static void
ofono_signal_cb (GDBusProxy *proxy,
                 gchar *sender_name,
                 gchar *signal_name,
                 GVariant *parameters,
                 gpointer user_data)
{
	NMModemManager *self = NM_MODEM_MANAGER (user_data);
	gchar *object_path;
	NMModem *modem;

	if (g_strcmp0 (signal_name, "ModemAdded") == 0) {
		g_variant_get (parameters, "(oa{sv})", &object_path, NULL);
		nm_log_info (LOGD_MB, "oFono modem appeared: %s", object_path);

		ofono_create_modem (NM_MODEM_MANAGER (user_data), object_path);
		g_free (object_path);
	} else if (g_strcmp0 (signal_name, "ModemRemoved") == 0) {
		g_variant_get (parameters, "(o)", &object_path);
		nm_log_info (LOGD_MB, "oFono modem removed: %s", object_path);

		modem = (NMModem *) g_hash_table_lookup (self->priv->modems, object_path);
		if (modem) {
			nm_modem_emit_removed (modem);
			g_hash_table_remove (self->priv->modems, object_path);
		} else {
			nm_log_warn (LOGD_MB, "could not remove modem %s, not found in table",
			             object_path);
		}
		g_free (object_path);
	}
}

#define OFONO_DBUS_MODEM_ENTRY (dbus_g_type_get_struct ("GValueArray", DBUS_TYPE_G_OBJECT_PATH, DBUS_TYPE_G_MAP_OF_VARIANT, G_TYPE_INVALID))
#define OFONO_DBUS_MODEM_ENTRIES (dbus_g_type_get_collection ("GPtrArray", OFONO_DBUS_MODEM_ENTRY))

static void
ofono_enumerate_devices_done (GDBusProxy *proxy, GAsyncResult *res, gpointer user_data)
{
	NMModemManager *manager = NM_MODEM_MANAGER (user_data);
	gs_free_error GError *error = NULL;
	GVariant *results;
	GVariantIter *iter;
	const char *path;

	results = g_dbus_proxy_call_finish (proxy, res, &error);
	if (results) {
		g_variant_get (results, "(a(oa{sv}))", &iter);
		while (g_variant_iter_loop (iter, "(&oa{sv})", &path, NULL))
			ofono_create_modem (manager, path);
		g_variant_iter_free (iter);
		g_variant_unref (results);
	}

	if (error) {
		nm_log_warn (LOGD_MB, "failed to enumerate oFono devices: %s",
		             error->message);
	}
}

static void ofono_appeared (NMModemManager *self);


static void
ofono_check_name_owner (NMModemManager *self)
{
	gs_free char *name_owner = NULL;

	name_owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (self->priv->ofono_proxy));
	if (name_owner) {
		/* Available! */
		ofono_appeared (self);
		return;
	}

	nm_log_info (LOGD_MB, "oFono disappeared from bus");

	clear_ofono_proxy (self);
	ensure_client (self);
}

static void
ofono_name_owner_changed (GDBusProxy *ofono_proxy,
                          GParamSpec *pspec,
                          NMModemManager *self)
{
	ofono_check_name_owner (self);
}

static void
ofono_appeared (NMModemManager *self)
{
	nm_log_info (LOGD_MB, "ofono is now available");

	self->priv->ofono_name_owner_changed_id =
	    g_signal_connect (self->priv->ofono_proxy,
	                      "notify::name-owner",
	                      G_CALLBACK (ofono_name_owner_changed),
	                      self);
	g_dbus_proxy_call (self->priv->ofono_proxy,
	                   "GetModems",
	                   NULL,
	                   G_DBUS_CALL_FLAGS_NONE,
	                   -1,
	                   NULL,
	                   (GAsyncReadyCallback) ofono_enumerate_devices_done,
	                   g_object_ref (self));

	g_signal_connect (self->priv->ofono_proxy,
	                  "g-signal",
	                  G_CALLBACK (ofono_signal_cb),
	                  self);
}

static void
ofono_proxy_new_cb (GObject *source_object, GAsyncResult *res, gpointer user_data)
{
	gs_unref_object NMModemManager *self = NM_MODEM_MANAGER (user_data);
	gs_free_error GError *error = NULL;

	self->priv->ofono_proxy = g_dbus_proxy_new_finish (res, &error);

	if (error) {
		//FIXME: do stuff if there's an error.
		return;
	}

	ofono_appeared (self);
}
#endif

static void
modem_manager_poke_cb (GDBusConnection *connection,
                       GAsyncResult *res,
                       NMModemManager *self)
{
	GError *error = NULL;
	GVariant *result;

	result = g_dbus_connection_call_finish (connection, res, &error);
	if (error) {
		nm_log_warn (LOGD_MB, "error poking ModemManager: %s",
					error ? error->message : "");

		/* Don't reschedule poke is MM service doesn't exist. */
		if (!g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN)
			&& !g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND)) {

			/* Setup timeout to relaunch */
			schedule_modem_manager_relaunch (self, MODEM_POKE_INTERVAL);
		}

		g_error_free (error);
	} else
		g_variant_unref (result);

	/* Balance refcount */
	g_object_unref (self);
}

static void
modem_manager_poke (NMModemManager *self)
{
	/* If there is no current owner right away, ensure we poke to get one */
	g_dbus_connection_call (self->priv->dbus_connection,
	                        "org.freedesktop.ModemManager1",
	                        "/org/freedesktop/ModemManager1",
	                        DBUS_INTERFACE_PEER,
	                        "Ping",
	                        NULL, /* inputs */
	                        NULL, /* outputs */
	                        G_DBUS_CALL_FLAGS_NONE,
	                        -1,
	                        NULL, /* cancellable */
	                        (GAsyncReadyCallback)modem_manager_poke_cb, /* callback */
	                        g_object_ref (self)); /* user_data */
}

static void
modem_manager_check_name_owner (NMModemManager *self)
{
	gchar *name_owner;

	name_owner = g_dbus_object_manager_client_get_name_owner (G_DBUS_OBJECT_MANAGER_CLIENT (self->priv->modem_manager));
	if (name_owner) {
		/* Available! */
		modem_manager_available (self);
		g_free (name_owner);
		return;
	}

	/* If the lifecycle is not managed by systemd, poke */
	if (!sd_booted ())
		modem_manager_poke (self);
}

static void
manager_new_ready (GObject *source,
                   GAsyncResult *res,
                   NMModemManager *self)
{
	/* Note we always get an extra reference to self here */

	GError *error = NULL;

	g_return_if_fail (!self->priv->modem_manager);

	self->priv->modem_manager = mm_manager_new_finish (res, &error);
	if (!self->priv->modem_manager) {
		/* We're not really supposed to get any error here. If we do get one,
		 * though, just re-schedule the MMManager creation after some time.
		 * During this period, name-owner changes won't be followed. */
		nm_log_warn (LOGD_MB, "error creating ModemManager client: %s", error->message);
		g_error_free (error);
		/* Setup timeout to relaunch */
		schedule_modem_manager_relaunch (self, MODEM_POKE_INTERVAL);
	} else {
		/* Setup signals in the GDBusObjectManagerClient */
		self->priv->mm_name_owner_changed_id =
		    g_signal_connect (self->priv->modem_manager,
		                      "notify::name-owner",
		                      G_CALLBACK (modem_manager_name_owner_changed),
		                      self);
		self->priv->mm_object_added_id =
		    g_signal_connect (self->priv->modem_manager,
		                      "object-added",
		                      G_CALLBACK (modem_object_added),
		                      self);
		self->priv->mm_object_removed_id =
		    g_signal_connect (self->priv->modem_manager,
		                      "object-removed",
		                      G_CALLBACK (modem_object_removed),
		                      self);

		modem_manager_check_name_owner (self);
	}

	/* Balance refcount */
	g_object_unref (self);
}

static void
ensure_client (NMModemManager *self)
{
	NMModemManagerPrivate *priv = self->priv;
	gboolean created = FALSE;

	g_assert (priv->dbus_connection);

	/* Create the GDBusObjectManagerClient. We do not request to autostart, as
	 * we don't really want the MMManager creation to fail. We can always poke
	 * later on if we want to request the autostart */
	if (!priv->modem_manager) {
		mm_manager_new (priv->dbus_connection,
		                G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_DO_NOT_AUTO_START,
		                NULL,
		                (GAsyncReadyCallback)manager_new_ready,
		                g_object_ref (self));
		created = TRUE;
	}

#if WITH_OFONO
	if (!priv->ofono_proxy) {
		g_dbus_proxy_new (priv->dbus_connection,
		                  G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
		                  NULL,
		                  OFONO_DBUS_SERVICE,
		                  OFONO_DBUS_PATH,
		                  OFONO_DBUS_INTERFACE,
		                  NULL,
		                  (GAsyncReadyCallback) ofono_proxy_new_cb,
		                  g_object_ref (self));
		created = TRUE;
	}
#endif /* WITH_OFONO */

	if (created)
		return;

	/* If already available, recheck name owner! */
	modem_manager_check_name_owner (self);
#if WITH_OFONO
	ofono_check_name_owner (self);
#endif
}

static void
bus_get_ready (GObject *source,
               GAsyncResult *res,
               NMModemManager *self)
{
	/* Note we always get an extra reference to self here */

	GError *error = NULL;

	self->priv->dbus_connection = g_bus_get_finish (res, &error);
	if (!self->priv->dbus_connection) {
		nm_log_warn (LOGD_CORE, "error getting bus connection: %s", error->message);
		g_error_free (error);
		/* Setup timeout to relaunch */
		schedule_modem_manager_relaunch (self, MODEM_POKE_INTERVAL);
	} else {
		/* Got the bus, ensure client */
		ensure_client (self);
	}

	/* Balance refcount */
	g_object_unref (self);
}

static gboolean
ensure_bus (NMModemManager *self)
{
	/* Clear launch ID */
	self->priv->mm_launch_id = 0;

	if (!self->priv->dbus_connection)
		g_bus_get (G_BUS_TYPE_SYSTEM,
		           NULL,
		           (GAsyncReadyCallback)bus_get_ready,
		           g_object_ref (self));
	else
		/* If bus is already available, ensure client */
		ensure_client (self);

	return FALSE;
}

static void
schedule_modem_manager_relaunch (NMModemManager *self,
                                 guint n_seconds)
{
	/* No need to pass an extra reference to self; timeout/idle will be
	 * cancelled if the object gets disposed. */

	if (n_seconds)
		self->priv->mm_launch_id = g_timeout_add_seconds (n_seconds, (GSourceFunc)ensure_bus, self);
	else
		self->priv->mm_launch_id = g_idle_add ((GSourceFunc)ensure_bus, self);
}

/************************************************************************/

static void
nm_modem_manager_init (NMModemManager *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, NM_TYPE_MODEM_MANAGER, NMModemManagerPrivate);

	self->priv->modems = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);

	schedule_modem_manager_relaunch (self, 0);
}

static void
dispose (GObject *object)
{
	NMModemManager *self = NM_MODEM_MANAGER (object);

	nm_clear_g_source (&self->priv->mm_launch_id);

	clear_modem_manager (self);

#if WITH_OFONO
	clear_ofono_proxy (self);
#endif

	g_clear_object (&self->priv->dbus_connection);

	if (self->priv->modems) {
		g_hash_table_foreach_remove (self->priv->modems, remove_one_modem, object);
		g_hash_table_destroy (self->priv->modems);
	}

	/* Chain up to the parent class */
	G_OBJECT_CLASS (nm_modem_manager_parent_class)->dispose (object);
}

static void
nm_modem_manager_class_init (NMModemManagerClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	g_type_class_add_private (object_class, sizeof (NMModemManagerPrivate));

	object_class->dispose = dispose;

	signals[MODEM_ADDED] =
		g_signal_new (NM_MODEM_MANAGER_MODEM_ADDED,
		              G_OBJECT_CLASS_TYPE (object_class),
		              G_SIGNAL_RUN_FIRST,
		              G_STRUCT_OFFSET (NMModemManagerClass, modem_added),
		              NULL, NULL, NULL,
		              G_TYPE_NONE, 1, NM_TYPE_MODEM);
}