From f032e478d8fa73b0c259fb0b88d315eaecb0779a Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Thu, 25 Mar 2010 13:15:03 -0700 Subject: [PATCH] modem: consolidate common modem code into NMDeviceModem --- src/nm-device-cdma.c | 369 ++------------------------------------ src/nm-device-cdma.h | 1 - src/nm-device-gsm.c | 369 ++------------------------------------ src/nm-device-gsm.h | 1 - src/nm-device-modem.c | 402 +++++++++++++++++++++++++++++++++++++++++- src/nm-device-modem.h | 7 +- 6 files changed, 422 insertions(+), 727 deletions(-) diff --git a/src/nm-device-cdma.c b/src/nm-device-cdma.c index 7acf79014..45771f429 100644 --- a/src/nm-device-cdma.c +++ b/src/nm-device-cdma.c @@ -20,342 +20,38 @@ #include -#include "nm-dbus-glib-types.h" -#include "nm-modem.h" #include "nm-modem-cdma.h" #include "nm-device-interface.h" -#include "nm-device-private.h" #include "nm-device-cdma.h" -#include "nm-utils.h" -#include "NetworkManagerUtils.h" -#include "nm-marshal.h" #include "nm-properties-changed-signal.h" #include "nm-rfkill.h" #include "nm-device-cdma-glue.h" -static void device_interface_init (NMDeviceInterface *iface_class); - -G_DEFINE_TYPE_EXTENDED (NMDeviceCdma, nm_device_cdma, NM_TYPE_DEVICE_MODEM, 0, - G_IMPLEMENT_INTERFACE (NM_TYPE_DEVICE_INTERFACE, device_interface_init)) - -#define NM_DEVICE_CDMA_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_CDMA, NMDeviceCdmaPrivate)) - -typedef struct { - NMModem *modem; -} NMDeviceCdmaPrivate; +G_DEFINE_TYPE (NMDeviceCdma, nm_device_cdma, NM_TYPE_DEVICE_MODEM) enum { - PPP_STATS, PROPERTIES_CHANGED, - LAST_SIGNAL }; - static guint signals[LAST_SIGNAL] = { 0 }; -static void -ppp_stats (NMModem *modem, - guint32 in_bytes, - guint32 out_bytes, - gpointer user_data) -{ - g_signal_emit (NM_DEVICE_CDMA (user_data), signals[PPP_STATS], 0, in_bytes, out_bytes); -} - -static void -ppp_failed (NMModem *modem, NMDeviceStateReason reason, gpointer user_data) -{ - NMDevice *device = NM_DEVICE (user_data); - - switch (nm_device_interface_get_state (NM_DEVICE_INTERFACE (device))) { - case NM_DEVICE_STATE_PREPARE: - case NM_DEVICE_STATE_CONFIG: - case NM_DEVICE_STATE_NEED_AUTH: - case NM_DEVICE_STATE_IP_CONFIG: - case NM_DEVICE_STATE_ACTIVATED: - nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, reason); - break; - default: - break; - } -} - -static void -device_state_changed (NMDevice *device, - NMDeviceState new_state, - NMDeviceState old_state, - NMDeviceStateReason reason, - gpointer user_data) -{ - NMDeviceCdmaPrivate *priv = NM_DEVICE_CDMA_GET_PRIVATE (device); - - nm_modem_device_state_changed (priv->modem, new_state, old_state, reason); -} - -static gboolean -real_hw_is_up (NMDevice *device) -{ - return nm_modem_hw_is_up (NM_DEVICE_CDMA_GET_PRIVATE (device)->modem, device); -} - -static gboolean -real_hw_bring_up (NMDevice *device, gboolean *no_firmware) -{ - return nm_modem_hw_bring_up (NM_DEVICE_CDMA_GET_PRIVATE (device)->modem, device, no_firmware); -} - -static NMConnection * -real_get_best_auto_connection (NMDevice *device, - GSList *connections, - char **specific_object) -{ - NMDeviceCdmaPrivate *priv = NM_DEVICE_CDMA_GET_PRIVATE (device); - - return nm_modem_get_best_auto_connection (priv->modem, connections, specific_object); -} - -static void -real_connection_secrets_updated (NMDevice *device, - NMConnection *connection, - GSList *updated_settings, - RequestSecretsCaller caller) -{ - NMDeviceCdmaPrivate *priv = NM_DEVICE_CDMA_GET_PRIVATE (device); - NMActRequest *req; - - g_return_if_fail (IS_ACTIVATING_STATE (nm_device_get_state (device))); - - req = nm_device_get_act_request (device); - g_assert (req); - - if (!nm_modem_connection_secrets_updated (priv->modem, - req, - connection, - updated_settings, - caller)) { - nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_NO_SECRETS); - return; - } - - /* PPP handles stuff itself... */ - if (caller == SECRETS_CALLER_PPP) - return; - - /* Otherwise, on success for CDMA secrets we need to schedule stage1 again */ - g_return_if_fail (nm_device_get_state (device) == NM_DEVICE_STATE_NEED_AUTH); - nm_device_activate_schedule_stage1_device_prepare (device); -} - -static gboolean -real_check_connection_compatible (NMDevice *device, - NMConnection *connection, - GError **error) -{ - NMDeviceCdmaPrivate *priv = NM_DEVICE_CDMA_GET_PRIVATE (device); - - return nm_modem_check_connection_compatible (priv->modem, connection, error); -} - - -static void -modem_need_auth (NMModem *modem, - const char *setting_name, - gboolean retry, - RequestSecretsCaller caller, - const char *hint1, - const char *hint2, - gpointer user_data) -{ - NMDeviceCdma *self = NM_DEVICE_CDMA (user_data); - NMActRequest *req; - - req = nm_device_get_act_request (NM_DEVICE (self)); - g_assert (req); - - nm_device_state_changed (NM_DEVICE (self), NM_DEVICE_STATE_NEED_AUTH, NM_DEVICE_STATE_REASON_NONE); - nm_act_request_get_secrets (req, setting_name, retry, caller, hint1, hint2); -} - -static void -modem_prepare_result (NMModem *modem, - gboolean success, - NMDeviceStateReason reason, - gpointer user_data) -{ - NMDevice *device = NM_DEVICE (user_data); - NMDeviceState state; - - state = nm_device_interface_get_state (NM_DEVICE_INTERFACE (device)); - g_return_if_fail (state == NM_DEVICE_STATE_PREPARE); - - if (success) - nm_device_activate_schedule_stage2_device_config (device); - else - nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, reason); -} - -static NMActStageReturn -real_act_stage1_prepare (NMDevice *device, NMDeviceStateReason *reason) -{ - NMActRequest *req; - - req = nm_device_get_act_request (device); - g_assert (req); - - return nm_modem_act_stage1_prepare (NM_DEVICE_CDMA_GET_PRIVATE (device)->modem, req, reason); -} - -static NMActStageReturn -real_act_stage2_config (NMDevice *device, NMDeviceStateReason *reason) -{ - NMActRequest *req; - - req = nm_device_get_act_request (device); - g_assert (req); - - return nm_modem_act_stage2_config (NM_DEVICE_CDMA_GET_PRIVATE (device)->modem, req, reason); -} - -static void -modem_ip4_config_result (NMModem *self, - const char *iface, - NMIP4Config *config, - GError *error, - gpointer user_data) -{ - NMDevice *device = NM_DEVICE (user_data); - NMDeviceState state; - - state = nm_device_interface_get_state (NM_DEVICE_INTERFACE (device)); - g_return_if_fail (state == NM_DEVICE_STATE_IP_CONFIG); - - if (error) { - nm_warning ("%s: retrieving IP4 configuration failed: (%d) %s", - __func__, - error ? error->code : -1, - error && error->message ? error->message : "(unknown)"); - - nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_IP_CONFIG_UNAVAILABLE); - } else { - if (iface) - nm_device_set_ip_iface (device, iface); - - nm_device_activate_schedule_stage4_ip4_config_get (device); - } -} - -static NMActStageReturn -real_act_stage3_ip4_config_start (NMDevice *device, NMDeviceStateReason *reason) -{ - return nm_modem_stage3_ip4_config_start (NM_DEVICE_CDMA_GET_PRIVATE (device)->modem, - device, - NM_DEVICE_CLASS (nm_device_cdma_parent_class), - reason); -} - -static NMActStageReturn -real_act_stage4_get_ip4_config (NMDevice *device, - NMIP4Config **config, - NMDeviceStateReason *reason) -{ - return nm_modem_stage4_get_ip4_config (NM_DEVICE_CDMA_GET_PRIVATE (device)->modem, - device, - NM_DEVICE_CLASS (nm_device_cdma_parent_class), - config, - reason); -} - -static void -real_deactivate_quickly (NMDevice *device) -{ - NMDeviceCdmaPrivate *priv = NM_DEVICE_CDMA_GET_PRIVATE (device); - - nm_modem_deactivate_quickly (priv->modem, device); -} - -static guint32 -real_get_generic_capabilities (NMDevice *device) -{ - return NM_DEVICE_CAP_NM_SUPPORTED; -} - -static void -real_set_enabled (NMDeviceInterface *device, gboolean enabled) -{ - NMDeviceCdma *self = NM_DEVICE_CDMA (device); - NMDeviceCdmaPrivate *priv = NM_DEVICE_CDMA_GET_PRIVATE (self); - NMDeviceState state; - - if (priv->modem) { - nm_modem_set_mm_enabled (priv->modem, enabled); - - if (enabled == FALSE) { - state = nm_device_interface_get_state (device); - if (state == NM_DEVICE_STATE_ACTIVATED) { - nm_device_state_changed (NM_DEVICE (device), - NM_DEVICE_STATE_DISCONNECTED, - NM_DEVICE_STATE_REASON_NONE); - } - } - } -} - -static void -modem_enabled_cb (NMModem *modem, GParamSpec *pspec, gpointer user_data) -{ - NMDeviceCdma *self = NM_DEVICE_CDMA (user_data); - NMDeviceCdmaPrivate *priv = NM_DEVICE_CDMA_GET_PRIVATE (self); - - real_set_enabled (NM_DEVICE_INTERFACE (self), nm_modem_get_mm_enabled (priv->modem)); -} - -/*****************************************************************************/ - -static NMModem * -real_get_modem (NMDeviceModem *device) -{ - return NM_DEVICE_CDMA_GET_PRIVATE (NM_DEVICE_CDMA (device))->modem; -} - -/*****************************************************************************/ - NMDevice * nm_device_cdma_new (NMModemCdma *modem, const char *driver) { - NMDevice *device; - g_return_val_if_fail (modem != NULL, NULL); g_return_val_if_fail (NM_IS_MODEM_CDMA (modem), NULL); g_return_val_if_fail (driver != NULL, NULL); - device = (NMDevice *) g_object_new (NM_TYPE_DEVICE_CDMA, - NM_DEVICE_INTERFACE_UDI, nm_modem_get_path (NM_MODEM (modem)), - NM_DEVICE_INTERFACE_IFACE, nm_modem_get_iface (NM_MODEM (modem)), - NM_DEVICE_INTERFACE_DRIVER, driver, - NM_DEVICE_INTERFACE_TYPE_DESC, "CDMA", - NM_DEVICE_INTERFACE_DEVICE_TYPE, NM_DEVICE_TYPE_CDMA, - NM_DEVICE_INTERFACE_RFKILL_TYPE, RFKILL_TYPE_WWAN, - NULL); - if (device) { - g_signal_connect (device, "state-changed", G_CALLBACK (device_state_changed), device); - - NM_DEVICE_CDMA_GET_PRIVATE (device)->modem = g_object_ref (modem); - g_signal_connect (modem, NM_MODEM_PPP_STATS, G_CALLBACK (ppp_stats), device); - g_signal_connect (modem, NM_MODEM_PPP_FAILED, G_CALLBACK (ppp_failed), device); - g_signal_connect (modem, NM_MODEM_PREPARE_RESULT, G_CALLBACK (modem_prepare_result), device); - g_signal_connect (modem, NM_MODEM_IP4_CONFIG_RESULT, G_CALLBACK (modem_ip4_config_result), device); - g_signal_connect (modem, NM_MODEM_NEED_AUTH, G_CALLBACK (modem_need_auth), device); - g_signal_connect (modem, "notify::" NM_MODEM_ENABLED, G_CALLBACK (modem_enabled_cb), device); - } - - return device; -} - -static void -device_interface_init (NMDeviceInterface *iface_class) -{ - iface_class->set_enabled = real_set_enabled; + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_CDMA, + NM_DEVICE_INTERFACE_UDI, nm_modem_get_path (NM_MODEM (modem)), + NM_DEVICE_INTERFACE_IFACE, nm_modem_get_iface (NM_MODEM (modem)), + NM_DEVICE_INTERFACE_DRIVER, driver, + NM_DEVICE_INTERFACE_TYPE_DESC, "CDMA", + NM_DEVICE_INTERFACE_DEVICE_TYPE, NM_DEVICE_TYPE_CDMA, + NM_DEVICE_INTERFACE_RFKILL_TYPE, RFKILL_TYPE_WWAN, + NM_DEVICE_MODEM_MODEM, modem, + NULL); } static void @@ -363,61 +59,16 @@ nm_device_cdma_init (NMDeviceCdma *self) { } -static void -finalize (GObject *object) -{ - NMDeviceCdmaPrivate *priv = NM_DEVICE_CDMA_GET_PRIVATE (object); - - g_object_unref (priv->modem); - priv->modem = NULL; - - G_OBJECT_CLASS (nm_device_cdma_parent_class)->finalize (object); -} - static void nm_device_cdma_class_init (NMDeviceCdmaClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); - NMDeviceClass *device_class = NM_DEVICE_CLASS (klass); - NMDeviceModemClass *modem_class = NM_DEVICE_MODEM_CLASS (klass); - - g_type_class_add_private (object_class, sizeof (NMDeviceCdmaPrivate)); - - /* Virtual methods */ - object_class->finalize = finalize; - - device_class->get_best_auto_connection = real_get_best_auto_connection; - device_class->connection_secrets_updated = real_connection_secrets_updated; - device_class->check_connection_compatible = real_check_connection_compatible; - device_class->hw_is_up = real_hw_is_up; - device_class->hw_bring_up = real_hw_bring_up; - device_class->get_generic_capabilities = real_get_generic_capabilities; - device_class->act_stage1_prepare = real_act_stage1_prepare; - device_class->act_stage2_config = real_act_stage2_config; - device_class->act_stage3_ip4_config_start = real_act_stage3_ip4_config_start; - device_class->act_stage4_get_ip4_config = real_act_stage4_get_ip4_config; - device_class->deactivate_quickly = real_deactivate_quickly; - - modem_class->get_modem = real_get_modem; /* Signals */ - signals[PPP_STATS] = - g_signal_new ("ppp-stats", - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_FIRST, - G_STRUCT_OFFSET (NMDeviceCdmaClass, ppp_stats), - NULL, NULL, - _nm_marshal_VOID__UINT_UINT, - G_TYPE_NONE, 2, - G_TYPE_UINT, G_TYPE_UINT); - signals[PROPERTIES_CHANGED] = nm_properties_changed_signal_new (object_class, G_STRUCT_OFFSET (NMDeviceCdmaClass, properties_changed)); - dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (klass), - nm_modem_get_serial_dbus_info ()); - dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (klass), &dbus_glib_nm_device_cdma_object_info); } diff --git a/src/nm-device-cdma.h b/src/nm-device-cdma.h index f840a66fc..f0bf34567 100644 --- a/src/nm-device-cdma.h +++ b/src/nm-device-cdma.h @@ -43,7 +43,6 @@ typedef struct { /* Signals */ void (*signal_quality) (NMDeviceCdma *self, guint32 quality); - void (*ppp_stats) (NMDeviceCdma *self, guint32 in_bytes, guint32 out_bytes); void (*properties_changed) (NMDeviceCdma *self, GHashTable *properties); } NMDeviceCdmaClass; diff --git a/src/nm-device-gsm.c b/src/nm-device-gsm.c index 5d795d43e..2a98a41f4 100644 --- a/src/nm-device-gsm.c +++ b/src/nm-device-gsm.c @@ -20,342 +20,38 @@ #include -#include "nm-dbus-glib-types.h" -#include "nm-modem.h" #include "nm-modem-gsm.h" #include "nm-device-interface.h" -#include "nm-device-private.h" #include "nm-device-gsm.h" -#include "nm-utils.h" -#include "NetworkManagerUtils.h" -#include "nm-marshal.h" #include "nm-properties-changed-signal.h" #include "nm-rfkill.h" #include "nm-device-gsm-glue.h" -static void device_interface_init (NMDeviceInterface *iface_class); - -G_DEFINE_TYPE_EXTENDED (NMDeviceGsm, nm_device_gsm, NM_TYPE_DEVICE_MODEM, 0, - G_IMPLEMENT_INTERFACE (NM_TYPE_DEVICE_INTERFACE, device_interface_init)) - -#define NM_DEVICE_GSM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_GSM, NMDeviceGsmPrivate)) - -typedef struct { - NMModem *modem; -} NMDeviceGsmPrivate; +G_DEFINE_TYPE (NMDeviceGsm, nm_device_gsm, NM_TYPE_DEVICE_MODEM) enum { - PPP_STATS, PROPERTIES_CHANGED, - LAST_SIGNAL }; - static guint signals[LAST_SIGNAL] = { 0 }; -static void -ppp_stats (NMModem *modem, - guint32 in_bytes, - guint32 out_bytes, - gpointer user_data) -{ - g_signal_emit (NM_DEVICE_GSM (user_data), signals[PPP_STATS], 0, in_bytes, out_bytes); -} - -static void -ppp_failed (NMModem *modem, NMDeviceStateReason reason, gpointer user_data) -{ - NMDevice *device = NM_DEVICE (user_data); - - switch (nm_device_interface_get_state (NM_DEVICE_INTERFACE (device))) { - case NM_DEVICE_STATE_PREPARE: - case NM_DEVICE_STATE_CONFIG: - case NM_DEVICE_STATE_NEED_AUTH: - case NM_DEVICE_STATE_IP_CONFIG: - case NM_DEVICE_STATE_ACTIVATED: - nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, reason); - break; - default: - break; - } -} - -static void -device_state_changed (NMDevice *device, - NMDeviceState new_state, - NMDeviceState old_state, - NMDeviceStateReason reason, - gpointer user_data) -{ - NMDeviceGsmPrivate *priv = NM_DEVICE_GSM_GET_PRIVATE (device); - - nm_modem_device_state_changed (priv->modem, new_state, old_state, reason); -} - -static gboolean -real_hw_is_up (NMDevice *device) -{ - return nm_modem_hw_is_up (NM_DEVICE_GSM_GET_PRIVATE (device)->modem, device); -} - -static gboolean -real_hw_bring_up (NMDevice *device, gboolean *no_firmware) -{ - return nm_modem_hw_bring_up (NM_DEVICE_GSM_GET_PRIVATE (device)->modem, device, no_firmware); -} - -static NMConnection * -real_get_best_auto_connection (NMDevice *device, - GSList *connections, - char **specific_object) -{ - NMDeviceGsmPrivate *priv = NM_DEVICE_GSM_GET_PRIVATE (device); - - return nm_modem_get_best_auto_connection (priv->modem, connections, specific_object); -} - -static void -real_connection_secrets_updated (NMDevice *device, - NMConnection *connection, - GSList *updated_settings, - RequestSecretsCaller caller) -{ - NMDeviceGsmPrivate *priv = NM_DEVICE_GSM_GET_PRIVATE (device); - NMActRequest *req; - - g_return_if_fail (IS_ACTIVATING_STATE (nm_device_get_state (device))); - - req = nm_device_get_act_request (device); - g_assert (req); - - if (!nm_modem_connection_secrets_updated (priv->modem, - req, - connection, - updated_settings, - caller)) { - nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_NO_SECRETS); - return; - } - - /* PPP handles stuff itself... */ - if (caller == SECRETS_CALLER_PPP) - return; - - /* Otherwise, on success for GSM secrets we need to schedule stage1 again */ - g_return_if_fail (nm_device_get_state (device) == NM_DEVICE_STATE_NEED_AUTH); - nm_device_activate_schedule_stage1_device_prepare (device); -} - -static gboolean -real_check_connection_compatible (NMDevice *device, - NMConnection *connection, - GError **error) -{ - NMDeviceGsmPrivate *priv = NM_DEVICE_GSM_GET_PRIVATE (device); - - return nm_modem_check_connection_compatible (priv->modem, connection, error); -} - - -static void -modem_need_auth (NMModem *modem, - const char *setting_name, - gboolean retry, - RequestSecretsCaller caller, - const char *hint1, - const char *hint2, - gpointer user_data) -{ - NMDeviceGsm *self = NM_DEVICE_GSM (user_data); - NMActRequest *req; - - req = nm_device_get_act_request (NM_DEVICE (self)); - g_assert (req); - - nm_device_state_changed (NM_DEVICE (self), NM_DEVICE_STATE_NEED_AUTH, NM_DEVICE_STATE_REASON_NONE); - nm_act_request_get_secrets (req, setting_name, retry, caller, hint1, hint2); -} - -static void -modem_prepare_result (NMModem *modem, - gboolean success, - NMDeviceStateReason reason, - gpointer user_data) -{ - NMDevice *device = NM_DEVICE (user_data); - NMDeviceState state; - - state = nm_device_interface_get_state (NM_DEVICE_INTERFACE (device)); - g_return_if_fail (state == NM_DEVICE_STATE_PREPARE); - - if (success) - nm_device_activate_schedule_stage2_device_config (device); - else - nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, reason); -} - -static NMActStageReturn -real_act_stage1_prepare (NMDevice *device, NMDeviceStateReason *reason) -{ - NMActRequest *req; - - req = nm_device_get_act_request (device); - g_assert (req); - - return nm_modem_act_stage1_prepare (NM_DEVICE_GSM_GET_PRIVATE (device)->modem, req, reason); -} - -static NMActStageReturn -real_act_stage2_config (NMDevice *device, NMDeviceStateReason *reason) -{ - NMActRequest *req; - - req = nm_device_get_act_request (device); - g_assert (req); - - return nm_modem_act_stage2_config (NM_DEVICE_GSM_GET_PRIVATE (device)->modem, req, reason); -} - -static void -modem_ip4_config_result (NMModem *self, - const char *iface, - NMIP4Config *config, - GError *error, - gpointer user_data) -{ - NMDevice *device = NM_DEVICE (user_data); - NMDeviceState state; - - state = nm_device_interface_get_state (NM_DEVICE_INTERFACE (device)); - g_return_if_fail (state == NM_DEVICE_STATE_IP_CONFIG); - - if (error) { - nm_warning ("%s: retrieving IP4 configuration failed: (%d) %s", - __func__, - error ? error->code : -1, - error && error->message ? error->message : "(unknown)"); - - nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_IP_CONFIG_UNAVAILABLE); - } else { - if (iface) - nm_device_set_ip_iface (device, iface); - - nm_device_activate_schedule_stage4_ip4_config_get (device); - } -} - -static NMActStageReturn -real_act_stage3_ip4_config_start (NMDevice *device, NMDeviceStateReason *reason) -{ - return nm_modem_stage3_ip4_config_start (NM_DEVICE_GSM_GET_PRIVATE (device)->modem, - device, - NM_DEVICE_CLASS (nm_device_gsm_parent_class), - reason); -} - -static NMActStageReturn -real_act_stage4_get_ip4_config (NMDevice *device, - NMIP4Config **config, - NMDeviceStateReason *reason) -{ - return nm_modem_stage4_get_ip4_config (NM_DEVICE_GSM_GET_PRIVATE (device)->modem, - device, - NM_DEVICE_CLASS (nm_device_gsm_parent_class), - config, - reason); -} - -static void -real_deactivate_quickly (NMDevice *device) -{ - NMDeviceGsmPrivate *priv = NM_DEVICE_GSM_GET_PRIVATE (device); - - nm_modem_deactivate_quickly (priv->modem, device); -} - -static guint32 -real_get_generic_capabilities (NMDevice *device) -{ - return NM_DEVICE_CAP_NM_SUPPORTED; -} - -static void -real_set_enabled (NMDeviceInterface *device, gboolean enabled) -{ - NMDeviceGsm *self = NM_DEVICE_GSM (device); - NMDeviceGsmPrivate *priv = NM_DEVICE_GSM_GET_PRIVATE (self); - NMDeviceState state; - - if (priv->modem) { - nm_modem_set_mm_enabled (priv->modem, enabled); - - if (enabled == FALSE) { - state = nm_device_interface_get_state (device); - if (state == NM_DEVICE_STATE_ACTIVATED) { - nm_device_state_changed (NM_DEVICE (device), - NM_DEVICE_STATE_DISCONNECTED, - NM_DEVICE_STATE_REASON_NONE); - } - } - } -} - -static void -modem_enabled_cb (NMModem *modem, GParamSpec *pspec, gpointer user_data) -{ - NMDeviceGsm *self = NM_DEVICE_GSM (user_data); - NMDeviceGsmPrivate *priv = NM_DEVICE_GSM_GET_PRIVATE (self); - - real_set_enabled (NM_DEVICE_INTERFACE (self), nm_modem_get_mm_enabled (priv->modem)); -} - -/*****************************************************************************/ - -static NMModem * -real_get_modem (NMDeviceModem *device) -{ - return NM_DEVICE_GSM_GET_PRIVATE (NM_DEVICE_GSM (device))->modem; -} - -/*****************************************************************************/ - NMDevice * nm_device_gsm_new (NMModemGsm *modem, const char *driver) { - NMDevice *device; - g_return_val_if_fail (modem != NULL, NULL); g_return_val_if_fail (NM_IS_MODEM_GSM (modem), NULL); g_return_val_if_fail (driver != NULL, NULL); - device = (NMDevice *) g_object_new (NM_TYPE_DEVICE_GSM, - NM_DEVICE_INTERFACE_UDI, nm_modem_get_path (NM_MODEM (modem)), - NM_DEVICE_INTERFACE_IFACE, nm_modem_get_iface (NM_MODEM (modem)), - NM_DEVICE_INTERFACE_DRIVER, driver, - NM_DEVICE_INTERFACE_TYPE_DESC, "GSM", - NM_DEVICE_INTERFACE_DEVICE_TYPE, NM_DEVICE_TYPE_GSM, - NM_DEVICE_INTERFACE_RFKILL_TYPE, RFKILL_TYPE_WWAN, - NULL); - if (device) { - g_signal_connect (device, "state-changed", G_CALLBACK (device_state_changed), device); - - NM_DEVICE_GSM_GET_PRIVATE (device)->modem = g_object_ref (modem); - g_signal_connect (modem, NM_MODEM_PPP_STATS, G_CALLBACK (ppp_stats), device); - g_signal_connect (modem, NM_MODEM_PPP_FAILED, G_CALLBACK (ppp_failed), device); - g_signal_connect (modem, NM_MODEM_PREPARE_RESULT, G_CALLBACK (modem_prepare_result), device); - g_signal_connect (modem, NM_MODEM_IP4_CONFIG_RESULT, G_CALLBACK (modem_ip4_config_result), device); - g_signal_connect (modem, NM_MODEM_NEED_AUTH, G_CALLBACK (modem_need_auth), device); - g_signal_connect (modem, "notify::" NM_MODEM_ENABLED, G_CALLBACK (modem_enabled_cb), device); - } - - return device; -} - -static void -device_interface_init (NMDeviceInterface *iface_class) -{ - iface_class->set_enabled = real_set_enabled; + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_GSM, + NM_DEVICE_INTERFACE_UDI, nm_modem_get_path (NM_MODEM (modem)), + NM_DEVICE_INTERFACE_IFACE, nm_modem_get_iface (NM_MODEM (modem)), + NM_DEVICE_INTERFACE_DRIVER, driver, + NM_DEVICE_INTERFACE_TYPE_DESC, "GSM", + NM_DEVICE_INTERFACE_DEVICE_TYPE, NM_DEVICE_TYPE_GSM, + NM_DEVICE_INTERFACE_RFKILL_TYPE, RFKILL_TYPE_WWAN, + NM_DEVICE_MODEM_MODEM, modem, + NULL); } static void @@ -363,61 +59,16 @@ nm_device_gsm_init (NMDeviceGsm *self) { } -static void -finalize (GObject *object) -{ - NMDeviceGsmPrivate *priv = NM_DEVICE_GSM_GET_PRIVATE (object); - - g_object_unref (priv->modem); - priv->modem = NULL; - - G_OBJECT_CLASS (nm_device_gsm_parent_class)->finalize (object); -} - static void nm_device_gsm_class_init (NMDeviceGsmClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); - NMDeviceClass *device_class = NM_DEVICE_CLASS (klass); - NMDeviceModemClass *modem_class = NM_DEVICE_MODEM_CLASS (klass); - - g_type_class_add_private (object_class, sizeof (NMDeviceGsmPrivate)); - - /* Virtual methods */ - object_class->finalize = finalize; - - device_class->get_best_auto_connection = real_get_best_auto_connection; - device_class->connection_secrets_updated = real_connection_secrets_updated; - device_class->check_connection_compatible = real_check_connection_compatible; - device_class->hw_is_up = real_hw_is_up; - device_class->hw_bring_up = real_hw_bring_up; - device_class->get_generic_capabilities = real_get_generic_capabilities; - device_class->act_stage1_prepare = real_act_stage1_prepare; - device_class->act_stage2_config = real_act_stage2_config; - device_class->act_stage3_ip4_config_start = real_act_stage3_ip4_config_start; - device_class->act_stage4_get_ip4_config = real_act_stage4_get_ip4_config; - device_class->deactivate_quickly = real_deactivate_quickly; - - modem_class->get_modem = real_get_modem; /* Signals */ - signals[PPP_STATS] = - g_signal_new ("ppp-stats", - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_FIRST, - G_STRUCT_OFFSET (NMDeviceGsmClass, ppp_stats), - NULL, NULL, - _nm_marshal_VOID__UINT_UINT, - G_TYPE_NONE, 2, - G_TYPE_UINT, G_TYPE_UINT); - signals[PROPERTIES_CHANGED] = nm_properties_changed_signal_new (object_class, G_STRUCT_OFFSET (NMDeviceGsmClass, properties_changed)); - dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (klass), - nm_modem_get_serial_dbus_info ()); - dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (klass), &dbus_glib_nm_device_gsm_object_info); } diff --git a/src/nm-device-gsm.h b/src/nm-device-gsm.h index 1981c8287..9b403c63a 100644 --- a/src/nm-device-gsm.h +++ b/src/nm-device-gsm.h @@ -43,7 +43,6 @@ typedef struct { /* Signals */ void (*signal_quality) (NMDeviceGsm *self, guint32 quality); - void (*ppp_stats) (NMDeviceGsm *self, guint32 in_bytes, guint32 out_bytes); void (*properties_changed) (NMDeviceGsm *self, GHashTable *properties); } NMDeviceGsmClass; diff --git a/src/nm-device-modem.c b/src/nm-device-modem.c index 1f74f39d5..fd6b044c3 100644 --- a/src/nm-device-modem.c +++ b/src/nm-device-modem.c @@ -15,7 +15,7 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * - * Copyright (C) 2008 Red Hat, Inc. + * Copyright (C) 2009 - 2010 Red Hat, Inc. */ #include @@ -23,24 +23,309 @@ #include "nm-device-modem.h" #include "nm-device-interface.h" #include "nm-modem.h" +#include "nm-device-private.h" +#include "nm-properties-changed-signal.h" +#include "nm-marshal.h" static void device_interface_init (NMDeviceInterface *iface_class); G_DEFINE_TYPE_EXTENDED (NMDeviceModem, nm_device_modem, NM_TYPE_DEVICE, G_TYPE_FLAG_ABSTRACT, G_IMPLEMENT_INTERFACE (NM_TYPE_DEVICE_INTERFACE, device_interface_init)) +#define NM_DEVICE_MODEM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_MODEM, NMDeviceModemPrivate)) + +typedef struct { + NMModem *modem; +} NMDeviceModemPrivate; + +enum { + PROP_0, + PROP_MODEM +}; + +enum { + PPP_STATS, + LAST_SIGNAL +}; +static guint signals[LAST_SIGNAL] = { 0 }; + +static void real_set_enabled (NMDeviceInterface *device, gboolean enabled); + +/*****************************************************************************/ + +static void +ppp_stats (NMModem *modem, + guint32 in_bytes, + guint32 out_bytes, + gpointer user_data) +{ + g_signal_emit (G_OBJECT (user_data), signals[PPP_STATS], 0, in_bytes, out_bytes); +} + +static void +ppp_failed (NMModem *modem, NMDeviceStateReason reason, gpointer user_data) +{ + NMDevice *device = NM_DEVICE (user_data); + + switch (nm_device_interface_get_state (NM_DEVICE_INTERFACE (device))) { + case NM_DEVICE_STATE_PREPARE: + case NM_DEVICE_STATE_CONFIG: + case NM_DEVICE_STATE_NEED_AUTH: + case NM_DEVICE_STATE_IP_CONFIG: + case NM_DEVICE_STATE_ACTIVATED: + nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, reason); + break; + default: + break; + } +} + +static void +modem_prepare_result (NMModem *modem, + gboolean success, + NMDeviceStateReason reason, + gpointer user_data) +{ + NMDevice *device = NM_DEVICE (user_data); + NMDeviceState state; + + state = nm_device_interface_get_state (NM_DEVICE_INTERFACE (device)); + g_return_if_fail (state == NM_DEVICE_STATE_PREPARE); + + if (success) + nm_device_activate_schedule_stage2_device_config (device); + else + nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, reason); +} + +static void +modem_need_auth (NMModem *modem, + const char *setting_name, + gboolean retry, + RequestSecretsCaller caller, + const char *hint1, + const char *hint2, + gpointer user_data) +{ + NMDeviceModem *self = NM_DEVICE_MODEM (user_data); + NMActRequest *req; + + req = nm_device_get_act_request (NM_DEVICE (self)); + g_assert (req); + + nm_device_state_changed (NM_DEVICE (self), NM_DEVICE_STATE_NEED_AUTH, NM_DEVICE_STATE_REASON_NONE); + nm_act_request_get_secrets (req, setting_name, retry, caller, hint1, hint2); +} + +static void +modem_ip4_config_result (NMModem *self, + const char *iface, + NMIP4Config *config, + GError *error, + gpointer user_data) +{ + NMDevice *device = NM_DEVICE (user_data); + NMDeviceState state; + + state = nm_device_interface_get_state (NM_DEVICE_INTERFACE (device)); + g_return_if_fail (state == NM_DEVICE_STATE_IP_CONFIG); + + if (error) { + g_warning ("%s: retrieving IP4 configuration failed: (%d) %s", + __func__, + error ? error->code : -1, + error && error->message ? error->message : "(unknown)"); + + nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_IP_CONFIG_UNAVAILABLE); + } else { + if (iface) + nm_device_set_ip_iface (device, iface); + + nm_device_activate_schedule_stage4_ip4_config_get (device); + } +} + +static void +modem_enabled_cb (NMModem *modem, GParamSpec *pspec, gpointer user_data) +{ + NMDeviceModem *self = NM_DEVICE_MODEM (user_data); + NMDeviceModemPrivate *priv = NM_DEVICE_MODEM_GET_PRIVATE (self); + + real_set_enabled (NM_DEVICE_INTERFACE (self), nm_modem_get_mm_enabled (priv->modem)); +} + +/*****************************************************************************/ + +NMModem * +nm_device_modem_get_modem (NMDeviceModem *self) +{ + g_return_val_if_fail (self != NULL, NULL); + g_return_val_if_fail (NM_IS_DEVICE_MODEM (self), NULL); + + return NM_DEVICE_MODEM_GET_PRIVATE (self)->modem; +} + +/*****************************************************************************/ + +static void +device_state_changed (NMDevice *device, + NMDeviceState new_state, + NMDeviceState old_state, + NMDeviceStateReason reason, + gpointer user_data) +{ + nm_modem_device_state_changed (NM_DEVICE_MODEM_GET_PRIVATE (device)->modem, + new_state, + old_state, + reason); +} + +static guint32 +real_get_generic_capabilities (NMDevice *device) +{ + return NM_DEVICE_CAP_NM_SUPPORTED; +} + +static NMConnection * +real_get_best_auto_connection (NMDevice *device, + GSList *connections, + char **specific_object) +{ + NMDeviceModemPrivate *priv = NM_DEVICE_MODEM_GET_PRIVATE (device); + + return nm_modem_get_best_auto_connection (priv->modem, connections, specific_object); +} + +static void +real_connection_secrets_updated (NMDevice *device, + NMConnection *connection, + GSList *updated_settings, + RequestSecretsCaller caller) +{ + NMDeviceModemPrivate *priv = NM_DEVICE_MODEM_GET_PRIVATE (device); + NMActRequest *req; + + g_return_if_fail (IS_ACTIVATING_STATE (nm_device_get_state (device))); + + req = nm_device_get_act_request (device); + g_assert (req); + + if (!nm_modem_connection_secrets_updated (priv->modem, + req, + connection, + updated_settings, + caller)) { + nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_NO_SECRETS); + return; + } + + /* PPP handles stuff itself... */ + if (caller == SECRETS_CALLER_PPP) + return; + + /* Otherwise, on success for modem secrets we need to schedule stage1 again */ + g_return_if_fail (nm_device_get_state (device) == NM_DEVICE_STATE_NEED_AUTH); + nm_device_activate_schedule_stage1_device_prepare (device); +} + +static gboolean +real_check_connection_compatible (NMDevice *device, + NMConnection *connection, + GError **error) +{ + NMDeviceModemPrivate *priv = NM_DEVICE_MODEM_GET_PRIVATE (device); + + return nm_modem_check_connection_compatible (priv->modem, connection, error); +} + +static gboolean +real_hw_is_up (NMDevice *device) +{ + return nm_modem_hw_is_up (NM_DEVICE_MODEM_GET_PRIVATE (device)->modem, device); +} + +static gboolean +real_hw_bring_up (NMDevice *device, gboolean *no_firmware) +{ + return nm_modem_hw_bring_up (NM_DEVICE_MODEM_GET_PRIVATE (device)->modem, device, no_firmware); +} + +static void +real_deactivate_quickly (NMDevice *device) +{ + nm_modem_deactivate_quickly (NM_DEVICE_MODEM_GET_PRIVATE (device)->modem, device); +} + +static NMActStageReturn +real_act_stage1_prepare (NMDevice *device, NMDeviceStateReason *reason) +{ + NMActRequest *req; + + req = nm_device_get_act_request (device); + g_assert (req); + + return nm_modem_act_stage1_prepare (NM_DEVICE_MODEM_GET_PRIVATE (device)->modem, req, reason); +} + +static NMActStageReturn +real_act_stage2_config (NMDevice *device, NMDeviceStateReason *reason) +{ + NMActRequest *req; + + req = nm_device_get_act_request (device); + g_assert (req); + + return nm_modem_act_stage2_config (NM_DEVICE_MODEM_GET_PRIVATE (device)->modem, req, reason); +} + +static NMActStageReturn +real_act_stage3_ip4_config_start (NMDevice *device, NMDeviceStateReason *reason) +{ + return nm_modem_stage3_ip4_config_start (NM_DEVICE_MODEM_GET_PRIVATE (device)->modem, + device, + NM_DEVICE_CLASS (nm_device_modem_parent_class), + reason); +} + +static NMActStageReturn +real_act_stage4_get_ip4_config (NMDevice *device, + NMIP4Config **config, + NMDeviceStateReason *reason) +{ + return nm_modem_stage4_get_ip4_config (NM_DEVICE_MODEM_GET_PRIVATE (device)->modem, + device, + NM_DEVICE_CLASS (nm_device_modem_parent_class), + config, + reason); +} + /*****************************************************************************/ static gboolean real_get_enabled (NMDeviceInterface *device) +{ + return nm_modem_get_mm_enabled (NM_DEVICE_MODEM_GET_PRIVATE (device)->modem); +} + +static void +real_set_enabled (NMDeviceInterface *device, gboolean enabled) { NMDeviceModem *self = NM_DEVICE_MODEM (device); - NMModem *modem; + NMDeviceModemPrivate *priv = NM_DEVICE_MODEM_GET_PRIVATE (self); + NMDeviceState state; - g_assert (NM_DEVICE_MODEM_GET_CLASS (self)->get_modem); - modem = NM_DEVICE_MODEM_GET_CLASS (self)->get_modem (self); + if (priv->modem) { + nm_modem_set_mm_enabled (priv->modem, enabled); - return modem ? nm_modem_get_mm_enabled (modem) : TRUE; + if (enabled == FALSE) { + state = nm_device_interface_get_state (device); + if (state == NM_DEVICE_STATE_ACTIVATED) { + nm_device_state_changed (NM_DEVICE (device), + NM_DEVICE_STATE_DISCONNECTED, + NM_DEVICE_STATE_REASON_NONE); + } + } + } } /*****************************************************************************/ @@ -49,15 +334,120 @@ static void device_interface_init (NMDeviceInterface *iface_class) { iface_class->get_enabled = real_get_enabled; + iface_class->set_enabled = real_set_enabled; } static void nm_device_modem_init (NMDeviceModem *self) { + g_signal_connect (self, "state-changed", G_CALLBACK (device_state_changed), self); } static void -nm_device_modem_class_init (NMDeviceModemClass *config_class) +set_modem (NMDeviceModem *self, NMModem *modem) { + NMDeviceModemPrivate *priv = NM_DEVICE_MODEM_GET_PRIVATE (self); + + g_return_if_fail (modem != NULL); + + priv->modem = g_object_ref (modem); + + g_signal_connect (modem, NM_MODEM_PPP_STATS, G_CALLBACK (ppp_stats), self); + g_signal_connect (modem, NM_MODEM_PPP_FAILED, G_CALLBACK (ppp_failed), self); + g_signal_connect (modem, NM_MODEM_PREPARE_RESULT, G_CALLBACK (modem_prepare_result), self); + g_signal_connect (modem, NM_MODEM_IP4_CONFIG_RESULT, G_CALLBACK (modem_ip4_config_result), self); + g_signal_connect (modem, NM_MODEM_NEED_AUTH, G_CALLBACK (modem_need_auth), self); + g_signal_connect (modem, "notify::" NM_MODEM_ENABLED, G_CALLBACK (modem_enabled_cb), self); +} + +static void +set_property (GObject *object, guint prop_id, + const GValue *value, GParamSpec *pspec) +{ + switch (prop_id) { + case PROP_MODEM: + /* construct-only */ + set_modem (NM_DEVICE_MODEM (object), g_value_get_object (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +get_property (GObject *object, guint prop_id, + GValue *value, GParamSpec *pspec) +{ + NMDeviceModemPrivate *priv = NM_DEVICE_MODEM_GET_PRIVATE (object); + + switch (prop_id) { + case PROP_MODEM: + g_value_set_object (value, priv->modem); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +finalize (GObject *object) +{ + NMDeviceModemPrivate *priv = NM_DEVICE_MODEM_GET_PRIVATE (object); + + g_object_unref (priv->modem); + priv->modem = NULL; + + G_OBJECT_CLASS (nm_device_modem_parent_class)->finalize (object); +} + +static void +nm_device_modem_class_init (NMDeviceModemClass *mclass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (mclass); + NMDeviceClass *device_class = NM_DEVICE_CLASS (mclass); + + g_type_class_add_private (object_class, sizeof (NMDeviceModemPrivate)); + + /* Virtual methods */ + object_class->finalize = finalize; + object_class->get_property = get_property; + object_class->set_property = set_property; + + device_class->get_generic_capabilities = real_get_generic_capabilities; + device_class->get_best_auto_connection = real_get_best_auto_connection; + device_class->connection_secrets_updated = real_connection_secrets_updated; + device_class->check_connection_compatible = real_check_connection_compatible; + device_class->hw_is_up = real_hw_is_up; + device_class->hw_bring_up = real_hw_bring_up; + device_class->deactivate_quickly = real_deactivate_quickly; + device_class->act_stage1_prepare = real_act_stage1_prepare; + device_class->act_stage2_config = real_act_stage2_config; + device_class->act_stage3_ip4_config_start = real_act_stage3_ip4_config_start; + device_class->act_stage4_get_ip4_config = real_act_stage4_get_ip4_config; + + /* Properties */ + g_object_class_install_property + (object_class, PROP_MODEM, + g_param_spec_object (NM_DEVICE_MODEM_MODEM, + "Modem", + "Modem", + NM_TYPE_MODEM, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | NM_PROPERTY_PARAM_NO_EXPORT)); + + /* Signals */ + signals[PPP_STATS] = + g_signal_new ("ppp-stats", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (NMDeviceModemClass, ppp_stats), + NULL, NULL, + _nm_marshal_VOID__UINT_UINT, + G_TYPE_NONE, 2, + G_TYPE_UINT, G_TYPE_UINT); + + dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (mclass), + nm_modem_get_serial_dbus_info ()); } diff --git a/src/nm-device-modem.h b/src/nm-device-modem.h index 05c66ae77..40e89d4b7 100644 --- a/src/nm-device-modem.h +++ b/src/nm-device-modem.h @@ -34,6 +34,8 @@ #define NM_IS_DEVICE_MODEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), NM_TYPE_DEVICE_MODEM)) #define NM_DEVICE_MODEM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DEVICE_MODEM, NMDeviceModemClass)) +#define NM_DEVICE_MODEM_MODEM "modem" + typedef struct { NMDevice parent; } NMDeviceModem; @@ -41,9 +43,12 @@ typedef struct { typedef struct { NMDeviceClass parent; - NMModem * (*get_modem) (NMDeviceModem *modem); + void (*ppp_stats) (NMDeviceModem *self, guint32 in_bytes, guint32 out_bytes); } NMDeviceModemClass; GType nm_device_modem_get_type (void); +/* Private for subclases */ +NMModem *nm_device_modem_get_modem (NMDeviceModem *self); + #endif /* NM_DEVICE_MODEM_H */