diff options
Diffstat (limited to 'src/modem-manager/nm-modem-manager.c')
| -rw-r--r-- | src/modem-manager/nm-modem-manager.c | 451 |
1 files changed, 377 insertions, 74 deletions
diff --git a/src/modem-manager/nm-modem-manager.c b/src/modem-manager/nm-modem-manager.c index 051940f5..d896b468 100644 --- a/src/modem-manager/nm-modem-manager.c +++ b/src/modem-manager/nm-modem-manager.c @@ -21,6 +21,7 @@ */ #include <string.h> +#include "config.h" #include "nm-modem-manager.h" #include "nm-logging.h" #include "nm-modem.h" @@ -31,19 +32,33 @@ #include "nm-marshal.h" #include "nm-dbus-glib-types.h" +#if WITH_MODEM_MANAGER_1 +#include <libmm-glib.h> +#include "nm-modem-broadband.h" +#endif + #define MODEM_POKE_INTERVAL 120 G_DEFINE_TYPE (NMModemManager, nm_modem_manager, G_TYPE_OBJECT) -#define NM_MODEM_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_MODEM_MANAGER, NMModemManagerPrivate)) - -typedef struct { +struct _NMModemManagerPrivate { + /* ModemManager < 0.7 */ NMDBusManager *dbus_mgr; DBusGProxy *proxy; - GHashTable *modems; - gboolean disposed; guint poke_id; -} NMModemManagerPrivate; + +#if WITH_MODEM_MANAGER_1 + /* ModemManager >= 0.7 */ + GDBusConnection *dbus_connection; + MMManager *modem_manager_1; + guint modem_manager_1_poke_id; + gboolean old_modem_manager_found; + gboolean new_modem_manager_found; +#endif + + /* Common */ + GHashTable *modems; +}; enum { MODEM_ADDED, @@ -69,6 +84,28 @@ nm_modem_manager_get (void) return singleton; } +/************************************************************************/ +/* Support for ModemManager < 0.7 */ + +static void +clear_modem_manager_support (NMModemManager *self) +{ + if (self->priv->poke_id) { + g_source_remove (self->priv->poke_id); + self->priv->poke_id = 0; + } + + if (self->priv->proxy) { + g_object_unref (self->priv->proxy); + self->priv->proxy = NULL; + } + + if (self->priv->dbus_mgr) { + g_object_unref (self->priv->dbus_mgr); + self->priv->dbus_mgr = NULL; + } +} + static gboolean get_modem_properties (DBusGConnection *connection, const char *path, @@ -77,7 +114,8 @@ get_modem_properties (DBusGConnection *connection, char **driver, guint32 *type, guint32 *ip_method, - guint32 *ip_timeout) + guint32 *ip_timeout, + NMModemState *state) { DBusGProxy *proxy; GError *err = NULL; @@ -87,12 +125,12 @@ get_modem_properties (DBusGConnection *connection, GValue *value; proxy = dbus_g_proxy_new_for_name (connection, - MM_DBUS_SERVICE, + MM_OLD_DBUS_SERVICE, path, "org.freedesktop.DBus.Properties"); if (!dbus_g_proxy_call_with_timeout (proxy, "GetAll", 15000, &err, - G_TYPE_STRING, MM_DBUS_INTERFACE_MODEM, + G_TYPE_STRING, MM_OLD_DBUS_INTERFACE_MODEM, G_TYPE_INVALID, DBUS_TYPE_G_MAP_OF_VARIANT, &props, G_TYPE_INVALID)) { @@ -122,6 +160,8 @@ get_modem_properties (DBusGConnection *connection, *driver = g_value_dup_string (value); else if (g_strcmp0 (prop, "IpTimeout") == 0) *ip_timeout = g_value_get_uint (value); + else if (g_strcmp0 (prop, "State") == 0) + *state = g_value_get_uint (value); } g_hash_table_unref (props); @@ -132,23 +172,23 @@ get_modem_properties (DBusGConnection *connection, } static void -create_modem (NMModemManager *manager, const char *path) +create_modem (NMModemManager *self, const char *path) { - NMModemManagerPrivate *priv = NM_MODEM_MANAGER_GET_PRIVATE (manager); NMModem *modem = NULL; char *data_device = NULL, *driver = NULL, *master_device = NULL; uint modem_type = MM_MODEM_TYPE_UNKNOWN; uint ip_method = MM_MODEM_IP_METHOD_PPP; uint ip_timeout = 0; + NMModemState state = NM_MODEM_STATE_UNKNOWN; - if (g_hash_table_lookup (priv->modems, path)) { + if (g_hash_table_lookup (self->priv->modems, path)) { nm_log_warn (LOGD_MB, "modem with path %s already exists, ignoring", path); return; } - if (!get_modem_properties (nm_dbus_manager_get_connection (priv->dbus_mgr), + if (!get_modem_properties (nm_dbus_manager_get_connection (self->priv->dbus_mgr), path, &master_device, &data_device, &driver, - &modem_type, &ip_method, &ip_timeout)) + &modem_type, &ip_method, &ip_timeout, &state)) return; if (modem_type == MM_MODEM_TYPE_UNKNOWN) { @@ -172,9 +212,9 @@ create_modem (NMModemManager *manager, const char *path) } if (modem_type == MM_MODEM_TYPE_GSM) - modem = nm_modem_gsm_new (path, master_device, data_device, ip_method); + modem = nm_modem_gsm_new (path, data_device, ip_method, state); else if (modem_type == MM_MODEM_TYPE_CDMA) - modem = nm_modem_cdma_new (path, master_device, data_device, ip_method); + modem = nm_modem_cdma_new (path, data_device, ip_method, state); else nm_log_warn (LOGD_MB, "unknown modem type '%d'", modem_type); @@ -182,8 +222,8 @@ create_modem (NMModemManager *manager, const char *path) if (modem) { g_object_set (G_OBJECT (modem), NM_MODEM_IP_TIMEOUT, ip_timeout, NULL); - g_hash_table_insert (priv->modems, g_strdup (path), modem); - g_signal_emit (manager, signals[MODEM_ADDED], 0, modem, driver); + g_hash_table_insert (self->priv->modems, g_strdup (path), modem); + g_signal_emit (self, signals[MODEM_ADDED], 0, modem, driver); } g_free (driver); @@ -198,13 +238,13 @@ modem_added (DBusGProxy *proxy, const char *path, gpointer user_data) static void modem_removed (DBusGProxy *proxy, const char *path, gpointer user_data) { - NMModemManagerPrivate *priv = NM_MODEM_MANAGER_GET_PRIVATE (user_data); + NMModemManager *self = NM_MODEM_MANAGER (user_data); NMModem *modem; - modem = (NMModem *) g_hash_table_lookup (priv->modems, path); + modem = (NMModem *) g_hash_table_lookup (self->priv->modems, path); if (modem) { - g_signal_emit (user_data, signals[MODEM_REMOVED], 0, modem); - g_hash_table_remove (priv->modems, path); + g_signal_emit (self, signals[MODEM_REMOVED], 0, modem); + g_hash_table_remove (self->priv->modems, path); } } @@ -229,16 +269,17 @@ static gboolean poke_modem_cb (gpointer user_data) { NMModemManager *self = NM_MODEM_MANAGER (user_data); - NMModemManagerPrivate *priv = NM_MODEM_MANAGER_GET_PRIVATE (self); DBusGConnection *g_connection; DBusGProxy *proxy; DBusGProxyCall *call; - g_connection = nm_dbus_manager_get_connection (priv->dbus_mgr); + g_connection = nm_dbus_manager_get_connection (self->priv->dbus_mgr); proxy = dbus_g_proxy_new_for_name (g_connection, - MM_DBUS_SERVICE, - MM_DBUS_PATH, - MM_DBUS_INTERFACE); + MM_OLD_DBUS_SERVICE, + MM_OLD_DBUS_PATH, + MM_OLD_DBUS_INTERFACE); + + nm_log_dbg (LOGD_MB, "Requesting to (re)launch modem-manager..."); call = dbus_g_proxy_begin_call_with_timeout (proxy, "EnumerateDevices", @@ -276,33 +317,43 @@ enumerate_devices_done (DBusGProxy *proxy, DBusGProxyCall *call_id, gpointer dat } } +#if WITH_MODEM_MANAGER_1 +static void clear_modem_manager_1_support (NMModemManager *self); +#endif + static void modem_manager_appeared (NMModemManager *self, gboolean enumerate_devices) { - NMModemManagerPrivate *priv = NM_MODEM_MANAGER_GET_PRIVATE (self); - - if (priv->poke_id) { - g_source_remove (priv->poke_id); - priv->poke_id = 0; + if (self->priv->poke_id) { + g_source_remove (self->priv->poke_id); + self->priv->poke_id = 0; } nm_log_info (LOGD_MB, "modem-manager is now available"); - priv->proxy = dbus_g_proxy_new_for_name (nm_dbus_manager_get_connection (priv->dbus_mgr), - MM_DBUS_SERVICE, MM_DBUS_PATH, MM_DBUS_INTERFACE); +#if WITH_MODEM_MANAGER_1 + self->priv->old_modem_manager_found = TRUE; + if (self->priv->new_modem_manager_found) + nm_log_warn (LOGD_MB, "Both the old and the new ModemManager were found"); + else + clear_modem_manager_1_support (self); +#endif + + self->priv->proxy = dbus_g_proxy_new_for_name (nm_dbus_manager_get_connection (self->priv->dbus_mgr), + MM_OLD_DBUS_SERVICE, MM_OLD_DBUS_PATH, MM_OLD_DBUS_INTERFACE); - dbus_g_proxy_add_signal (priv->proxy, "DeviceAdded", DBUS_TYPE_G_OBJECT_PATH, G_TYPE_INVALID); - dbus_g_proxy_connect_signal (priv->proxy, "DeviceAdded", + dbus_g_proxy_add_signal (self->priv->proxy, "DeviceAdded", DBUS_TYPE_G_OBJECT_PATH, G_TYPE_INVALID); + dbus_g_proxy_connect_signal (self->priv->proxy, "DeviceAdded", G_CALLBACK (modem_added), self, NULL); - dbus_g_proxy_add_signal (priv->proxy, "DeviceRemoved", DBUS_TYPE_G_OBJECT_PATH, G_TYPE_INVALID); - dbus_g_proxy_connect_signal (priv->proxy, "DeviceRemoved", + dbus_g_proxy_add_signal (self->priv->proxy, "DeviceRemoved", DBUS_TYPE_G_OBJECT_PATH, G_TYPE_INVALID); + dbus_g_proxy_connect_signal (self->priv->proxy, "DeviceRemoved", G_CALLBACK (modem_removed), self, NULL); if (enumerate_devices) - dbus_g_proxy_begin_call (priv->proxy, "EnumerateDevices", enumerate_devices_done, self, NULL, G_TYPE_INVALID); + dbus_g_proxy_begin_call (self->priv->proxy, "EnumerateDevices", enumerate_devices_done, self, NULL, G_TYPE_INVALID); } static gboolean @@ -316,19 +367,17 @@ remove_one_modem (gpointer key, gpointer value, gpointer user_data) static void modem_manager_disappeared (NMModemManager *self) { - NMModemManagerPrivate *priv = NM_MODEM_MANAGER_GET_PRIVATE (self); - - g_hash_table_foreach_remove (priv->modems, remove_one_modem, self); + g_hash_table_foreach_remove (self->priv->modems, remove_one_modem, self); - if (priv->proxy) { - g_object_unref (priv->proxy); - priv->proxy = NULL; + if (self->priv->proxy) { + g_object_unref (self->priv->proxy); + self->priv->proxy = NULL; } /* Try to activate the modem-manager */ - nm_log_info (LOGD_MB, "trying to start the modem manager..."); + nm_log_dbg (LOGD_MB, "trying to start the modem manager..."); poke_modem_cb (self); - priv->poke_id = g_timeout_add_seconds (MODEM_POKE_INTERVAL, poke_modem_cb, self); + self->priv->poke_id = g_timeout_add_seconds (MODEM_POKE_INTERVAL, poke_modem_cb, self); } static void @@ -342,7 +391,7 @@ nm_modem_manager_name_owner_changed (NMDBusManager *dbus_mgr, gboolean new_owner_good; /* Can't handle the signal if its not from the modem service */ - if (strcmp (MM_DBUS_SERVICE, name) != 0) + if (strcmp (MM_OLD_DBUS_SERVICE, name) != 0) return; old_owner_good = (old_owner && strlen (old_owner)); @@ -356,52 +405,306 @@ nm_modem_manager_name_owner_changed (NMDBusManager *dbus_mgr, } } -/*******************************************************/ +/************************************************************************/ +/* Support for ModemManager >= 0.7 */ + +#if WITH_MODEM_MANAGER_1 static void -nm_modem_manager_init (NMModemManager *self) +clear_modem_manager_1_support (NMModemManager *self) { - NMModemManagerPrivate *priv = NM_MODEM_MANAGER_GET_PRIVATE (self); + if (self->priv->modem_manager_1_poke_id) { + g_source_remove (self->priv->modem_manager_1_poke_id); + self->priv->modem_manager_1_poke_id = 0; + } - priv->modems = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref); - priv->dbus_mgr = nm_dbus_manager_get (); + g_clear_object (&self->priv->modem_manager_1); + g_clear_object (&self->priv->dbus_connection); +} - g_signal_connect (priv->dbus_mgr, NM_DBUS_MANAGER_NAME_OWNER_CHANGED, - G_CALLBACK (nm_modem_manager_name_owner_changed), - self); +static void +modem_object_added (MMManager *modem_manager, + MMObject *modem_object, + NMModemManager *self) +{ + const gchar *path; + gchar *drivers; + MMModem *modem_iface; + NMModem *modem; - if (nm_dbus_manager_name_has_owner (priv->dbus_mgr, MM_DBUS_SERVICE)) - modem_manager_appeared (self, TRUE); + /* 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)); + if (!modem) + return; + + /* Build a single string with all drivers listed */ + drivers = g_strjoinv (", ", (gchar **)mm_modem_get_drivers (modem_iface)); + + /* Keep track of the new modem and notify about it */ + g_hash_table_insert (self->priv->modems, g_strdup (path), modem); + g_signal_emit (self, signals[MODEM_ADDED], 0, modem, drivers); + g_free (drivers); +} + +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; + + g_signal_emit (self, signals[MODEM_REMOVED], 0, modem); + g_hash_table_remove (self->priv->modems, path); +} + +static void +modem_manager_1_available (NMModemManager *self) +{ + GList *modems, *l; + + nm_log_info (LOGD_MB, "ModemManager available in the bus"); + + self->priv->new_modem_manager_found = TRUE; + if (self->priv->old_modem_manager_found) + nm_log_warn (LOGD_MB, "Both the old and the new ModemManager were found"); else - modem_manager_disappeared (self); + clear_modem_manager_support (self); + + /* Update initial modems list */ + modems = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (self->priv->modem_manager_1)); + for (l = modems; l; l = g_list_next (l)) + modem_object_added (self->priv->modem_manager_1, MM_OBJECT (l->data), self); + g_list_free_full (modems, (GDestroyNotify) g_object_unref); } +static void schedule_modem_manager_1_relaunch (NMModemManager *self, + guint n_seconds); + static void -dispose (GObject *object) +modem_manager_1_name_owner_changed (MMManager *modem_manager_1, + GParamSpec *pspec, + NMModemManager *self) { - NMModemManagerPrivate *priv = NM_MODEM_MANAGER_GET_PRIVATE (object); + /* Quit poking, if any */ + if (self->priv->modem_manager_1_poke_id) { + g_source_remove (self->priv->modem_manager_1_poke_id); + self->priv->modem_manager_1_poke_id = 0; + } - if (priv->disposed) + if (!g_dbus_object_manager_client_get_name_owner (G_DBUS_OBJECT_MANAGER_CLIENT (modem_manager_1))) { + nm_log_info (LOGD_MB, "ModemManager disappeared from bus"); + schedule_modem_manager_1_relaunch (self, 0); return; + } + + /* Available! */ + modem_manager_1_available (self); +} + +static void +manager_new_ready (GObject *source, + GAsyncResult *res, + NMModemManager *self) +{ + /* Note we always get an extra reference to self here */ - priv->disposed = TRUE; + GError *error = NULL; - if (priv->poke_id) { - g_source_remove (priv->poke_id); - priv->poke_id = 0; + g_assert (!self->priv->modem_manager_1); + self->priv->modem_manager_1 = mm_manager_new_finish (res, &error); + if (!self->priv->modem_manager_1) { + if ( !g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN) + && !g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_SPAWN_EXEC_FAILED) + && !g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_SPAWN_FORK_FAILED) + && !g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_SPAWN_FAILED) + && !g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_TIMEOUT) + && !g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND)) { + nm_log_warn (LOGD_MB, "error creating ModemManager client: %s", error->message); + } + g_error_free (error); + /* Setup timeout to relaunch */ + schedule_modem_manager_1_relaunch (self, MODEM_POKE_INTERVAL); + } else if (self->priv->old_modem_manager_found) { + /* If we found the old MM, abort */ + clear_modem_manager_1_support (self); + } else { + gchar *name_owner; + + g_signal_connect (self->priv->modem_manager_1, + "notify::name-owner", + G_CALLBACK (modem_manager_1_name_owner_changed), + self); + g_signal_connect (self->priv->modem_manager_1, + "object-added", + G_CALLBACK (modem_object_added), + self); + g_signal_connect (self->priv->modem_manager_1, + "object-removed", + G_CALLBACK (modem_object_removed), + self); + + /* If there is no current owner right away, ensure we poke until we get + * one */ + name_owner = g_dbus_object_manager_client_get_name_owner (G_DBUS_OBJECT_MANAGER_CLIENT (self->priv->modem_manager_1)); + if (!name_owner) { + /* Setup timeout to wait for an owner */ + schedule_modem_manager_1_relaunch (self, MODEM_POKE_INTERVAL); + } else { + /* Available! */ + modem_manager_1_available (self); + g_free (name_owner); + } } - g_hash_table_foreach_remove (priv->modems, remove_one_modem, object); - g_hash_table_destroy (priv->modems); + /* Balance refcount */ + g_object_unref (self); +} + +static void +recreate_client (NMModemManager *self) +{ + g_assert (self->priv->dbus_connection); + + /* Re-create the GDBusObjectManagerClient so that we request again the owner + * for the well-known name. + * Note that we pass an extra reference always */ + g_clear_object (&self->priv->modem_manager_1); + mm_manager_new (self->priv->dbus_connection, + G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE, + NULL, + (GAsyncReadyCallback)manager_new_ready, + g_object_ref (self)); +} + +static void +bus_get_ready (GObject *source, + GAsyncResult *res, + NMModemManager *self) +{ + /* Note we always get an extra reference to self here */ + + GError *error = NULL; - if (priv->proxy) { - g_object_unref (priv->proxy); - priv->proxy = 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_1_relaunch (self, MODEM_POKE_INTERVAL); + } else if (self->priv->old_modem_manager_found) { + /* If we found the old MM, abort */ + clear_modem_manager_1_support (self); + } else { + /* Got the bus, create new ModemManager client. */ + recreate_client (self); } - if (priv->dbus_mgr) { - g_object_unref (priv->dbus_mgr); - priv->dbus_mgr = NULL; + /* Balance refcount */ + g_object_unref (self); +} + +static gboolean +ensure_bus (NMModemManager *self) +{ + nm_log_dbg (LOGD_MB, "Requesting to (re)launch ModemManager..."); + + /* Clear poke ID */ + self->priv->modem_manager_1_poke_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, launch client re-creation */ + recreate_client (self); + + return FALSE; +} + +static void +schedule_modem_manager_1_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->modem_manager_1_poke_id = g_timeout_add_seconds (n_seconds, (GSourceFunc)ensure_bus, self); + else + self->priv->modem_manager_1_poke_id = g_idle_add ((GSourceFunc)ensure_bus, self); +} + +#endif /* WITH_MODEM_MANAGER_1 */ + +/************************************************************************/ + +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); + + /* ModemManager < 0.7 */ + self->priv->dbus_mgr = nm_dbus_manager_get (); + g_signal_connect (self->priv->dbus_mgr, NM_DBUS_MANAGER_NAME_OWNER_CHANGED, + G_CALLBACK (nm_modem_manager_name_owner_changed), + self); + if (nm_dbus_manager_name_has_owner (self->priv->dbus_mgr, MM_OLD_DBUS_SERVICE)) + modem_manager_appeared (self, TRUE); + else + modem_manager_disappeared (self); + +#if WITH_MODEM_MANAGER_1 + /* ModemManager >= 0.7 */ + schedule_modem_manager_1_relaunch (self, 0); +#endif +} + +static void +dispose (GObject *object) +{ + NMModemManager *self = NM_MODEM_MANAGER (object); + + /* ModemManager < 0.7 */ + clear_modem_manager_support (self); + +#if WITH_MODEM_MANAGER_1 + /* ModemManager >= 0.7 */ + clear_modem_manager_1_support (self); +#endif + + 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 */ |