diff options
Diffstat (limited to 'plugins/linktop')
-rw-r--r-- | plugins/linktop/mm-broadband-modem-linktop.c | 238 | ||||
-rw-r--r-- | plugins/linktop/mm-broadband-modem-linktop.h | 49 | ||||
-rw-r--r-- | plugins/linktop/mm-plugin-linktop.c | 122 | ||||
-rw-r--r-- | plugins/linktop/mm-plugin-linktop.h | 42 |
4 files changed, 451 insertions, 0 deletions
diff --git a/plugins/linktop/mm-broadband-modem-linktop.c b/plugins/linktop/mm-broadband-modem-linktop.c new file mode 100644 index 00000000..2c6aef57 --- /dev/null +++ b/plugins/linktop/mm-broadband-modem-linktop.c @@ -0,0 +1,238 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * 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: + * + * Copyright (C) 2008 - 2009 Novell, Inc. + * Copyright (C) 2009 - 2012 Red Hat, Inc. + * Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org> + */ + +#include <config.h> + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +#include <libmm-common.h> + +#include "ModemManager.h" +#include "mm-serial-parsers.h" +#include "mm-log.h" +#include "mm-modem-helpers.h" +#include "mm-iface-modem.h" +#include "mm-base-modem-at.h" +#include "mm-broadband-modem-linktop.h" + +#define LINKTOP_MODE_ANY 1 +#define LINKTOP_MODE_2G 5 +#define LINKTOP_MODE_3G 6 + +static void iface_modem_init (MMIfaceModem *iface); + +G_DEFINE_TYPE_EXTENDED (MMBroadbandModemLinktop, mm_broadband_modem_linktop, MM_TYPE_BROADBAND_MODEM, 0, + G_IMPLEMENT_INTERFACE (MM_TYPE_IFACE_MODEM, iface_modem_init)); + +/*****************************************************************************/ +/* Load initial allowed/preferred modes (Modem interface) */ + +static gboolean +load_allowed_modes_finish (MMIfaceModem *self, + GAsyncResult *res, + MMModemMode *allowed, + MMModemMode *preferred, + GError **error) +{ + const gchar *response; + const gchar *str; + guint aux; + + response = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, error); + if (!response) + return FALSE; + + str = mm_strip_tag (response, "CFUN:"); + if (!mm_get_uint_from_str (str, &aux)) { + g_set_error (error, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Couldn't parse CFUN? response: '%s'", + response); + return FALSE; + } + + switch (aux) { + case LINKTOP_MODE_2G: + *allowed = MM_MODEM_MODE_2G; + *preferred = MM_MODEM_MODE_NONE; + break; + + case LINKTOP_MODE_3G: + *allowed = MM_MODEM_MODE_3G; + *preferred = MM_MODEM_MODE_NONE; + break; + + case LINKTOP_MODE_ANY: + *allowed = (MM_MODEM_MODE_2G | MM_MODEM_MODE_3G); + *preferred = MM_MODEM_MODE_NONE; + break; + + default: + *allowed = MM_MODEM_MODE_ANY; + *preferred = MM_MODEM_MODE_NONE; + break; + } + + return TRUE; +} + +static void +load_allowed_modes (MMIfaceModem *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + mm_base_modem_at_command (MM_BASE_MODEM (self), + "+CFUN?", + 3, + FALSE, + callback, + user_data); +} + +/*****************************************************************************/ +/* Set allowed modes (Modem interface) */ + +static gboolean +set_allowed_modes_finish (MMIfaceModem *self, + GAsyncResult *res, + GError **error) +{ + return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error); +} + +static void +allowed_mode_update_ready (MMBroadbandModemLinktop *self, + GAsyncResult *res, + GSimpleAsyncResult *operation_result) +{ + GError *error = NULL; + + mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error); + if (error) + /* Let the error be critical. */ + g_simple_async_result_take_error (operation_result, error); + else + g_simple_async_result_set_op_res_gboolean (operation_result, TRUE); + g_simple_async_result_complete (operation_result); + g_object_unref (operation_result); +} + +static void +set_allowed_modes (MMIfaceModem *self, + MMModemMode allowed, + MMModemMode preferred, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *result; + gchar *command; + gint linktop_mode = -1; + + result = g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + set_allowed_modes); + + /* There is no explicit config for CS connections, we just assume we may + * have them as part of 2G when no GPRS is available */ + if (allowed & MM_MODEM_MODE_CS) { + allowed |= MM_MODEM_MODE_2G; + allowed &= ~MM_MODEM_MODE_CS; + } + + if (allowed == MM_MODEM_MODE_2G) + linktop_mode = LINKTOP_MODE_2G; + else if (allowed == MM_MODEM_MODE_3G) + linktop_mode = LINKTOP_MODE_3G; + else if ((allowed == (MM_MODEM_MODE_2G | MM_MODEM_MODE_3G)) && + (preferred == MM_MODEM_MODE_NONE)) { + linktop_mode = LINKTOP_MODE_ANY; + } + + if (linktop_mode < 0) { + gchar *allowed_str; + gchar *preferred_str; + + allowed_str = mm_modem_mode_build_string_from_mask (allowed); + preferred_str = mm_modem_mode_build_string_from_mask (preferred); + g_simple_async_result_set_error (result, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Requested mode (allowed: '%s', preferred: '%s') not " + "supported by the modem.", + allowed_str, + preferred_str); + g_free (allowed_str); + g_free (preferred_str); + + g_simple_async_result_complete_in_idle (result); + g_object_unref (result); + return; + } + + command = g_strdup_printf ("AT+CFUN=%d", linktop_mode); + mm_base_modem_at_command ( + MM_BASE_MODEM (self), + command, + 3, + FALSE, + (GAsyncReadyCallback)allowed_mode_update_ready, + result); + g_free (command); +} + +/*****************************************************************************/ + +MMBroadbandModemLinktop * +mm_broadband_modem_linktop_new (const gchar *device, + const gchar *driver, + const gchar *plugin, + guint16 vendor_id, + guint16 product_id) +{ + return g_object_new (MM_TYPE_BROADBAND_MODEM_LINKTOP, + MM_BASE_MODEM_DEVICE, device, + MM_BASE_MODEM_DRIVER, driver, + MM_BASE_MODEM_PLUGIN, plugin, + MM_BASE_MODEM_VENDOR_ID, vendor_id, + MM_BASE_MODEM_PRODUCT_ID, product_id, + NULL); +} + +static void +mm_broadband_modem_linktop_init (MMBroadbandModemLinktop *self) +{ +} + +static void +iface_modem_init (MMIfaceModem *iface) +{ + iface->load_allowed_modes = load_allowed_modes; + iface->load_allowed_modes_finish = load_allowed_modes_finish; + iface->set_allowed_modes = set_allowed_modes; + iface->set_allowed_modes_finish = set_allowed_modes_finish; +} + +static void +mm_broadband_modem_linktop_class_init (MMBroadbandModemLinktopClass *klass) +{ +} diff --git a/plugins/linktop/mm-broadband-modem-linktop.h b/plugins/linktop/mm-broadband-modem-linktop.h new file mode 100644 index 00000000..dbc8a10d --- /dev/null +++ b/plugins/linktop/mm-broadband-modem-linktop.h @@ -0,0 +1,49 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * 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: + * + * Copyright (C) 2008 - 2009 Novell, Inc. + * Copyright (C) 2009 - 2012 Red Hat, Inc. + * Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org> + */ + +#ifndef MM_BROADBAND_MODEM_LINKTOP_H +#define MM_BROADBAND_MODEM_LINKTOP_H + +#include "mm-broadband-modem.h" + +#define MM_TYPE_BROADBAND_MODEM_LINKTOP (mm_broadband_modem_linktop_get_type ()) +#define MM_BROADBAND_MODEM_LINKTOP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MM_TYPE_BROADBAND_MODEM_LINKTOP, MMBroadbandModemLinktop)) +#define MM_BROADBAND_MODEM_LINKTOP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MM_TYPE_BROADBAND_MODEM_LINKTOP, MMBroadbandModemLinktopClass)) +#define MM_IS_BROADBAND_MODEM_LINKTOP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MM_TYPE_BROADBAND_MODEM_LINKTOP)) +#define MM_IS_BROADBAND_MODEM_LINKTOP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MM_TYPE_BROADBAND_MODEM_LINKTOP)) +#define MM_BROADBAND_MODEM_LINKTOP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_BROADBAND_MODEM_LINKTOP, MMBroadbandModemLinktopClass)) + +typedef struct _MMBroadbandModemLinktop MMBroadbandModemLinktop; +typedef struct _MMBroadbandModemLinktopClass MMBroadbandModemLinktopClass; + +struct _MMBroadbandModemLinktop { + MMBroadbandModem parent; +}; + +struct _MMBroadbandModemLinktopClass{ + MMBroadbandModemClass parent; +}; + +GType mm_broadband_modem_linktop_get_type (void); + +MMBroadbandModemLinktop *mm_broadband_modem_linktop_new (const gchar *device, + const gchar *driver, + const gchar *plugin, + guint16 vendor_id, + guint16 product_id); + +#endif /* MM_BROADBAND_MODEM_LINKTOP_H */ diff --git a/plugins/linktop/mm-plugin-linktop.c b/plugins/linktop/mm-plugin-linktop.c new file mode 100644 index 00000000..69a8e881 --- /dev/null +++ b/plugins/linktop/mm-plugin-linktop.c @@ -0,0 +1,122 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * 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: + * + * Copyright (C) 2008 - 2009 Novell, Inc. + * Copyright (C) 2009 - 2012 Red Hat, Inc. + * Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org> + */ + +#include <string.h> +#include <gmodule.h> +#include <libmm-common.h> + +#include "mm-plugin-linktop.h" +#include "mm-broadband-modem-linktop.h" + +G_DEFINE_TYPE (MMPluginLinktop, mm_plugin_linktop, MM_TYPE_PLUGIN_BASE) + +int mm_plugin_major_version = MM_PLUGIN_MAJOR_VERSION; +int mm_plugin_minor_version = MM_PLUGIN_MINOR_VERSION; + +/*****************************************************************************/ + +static MMBaseModem * +grab_port (MMPluginBase *base, + MMBaseModem *existing, + MMPortProbe *probe, + GError **error) +{ + MMBaseModem *modem = NULL; + GUdevDevice *port; + const gchar *name, *subsys, *devfile; + guint16 vendor = 0, product = 0; + + /* The Linktop plugin cannot do anything with non-AT ports */ + if (!mm_port_probe_is_at (probe)) { + g_set_error_literal (error, + MM_CORE_ERROR, + MM_CORE_ERROR_UNSUPPORTED, + "Ignoring non-AT port"); + return NULL; + } + + port = mm_port_probe_get_port (probe); /* transfer none */ + + /* TODO: Why do we check for device file? */ + devfile = g_udev_device_get_device_file (port); + if (!devfile) { + g_set_error (error, 0, 0, "Could not get port's sysfs file."); + return NULL; + } + + subsys = mm_port_probe_get_port_subsys (probe); + name = mm_port_probe_get_port_name (probe); + + if (!mm_plugin_base_get_device_ids (base, subsys, name, &vendor, &product)) { + g_set_error_literal (error, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Could not get modem product ID"); + return NULL; + } + + /* If this is the first port being grabbed, create a new modem object */ + if (!existing) + modem = MM_BASE_MODEM (mm_broadband_modem_linktop_new (mm_port_probe_get_port_physdev (probe), + mm_port_probe_get_port_driver (probe), + mm_plugin_get_name (MM_PLUGIN (base)), + vendor, + product)); + + if (!mm_base_modem_grab_port (existing ? existing : modem, + subsys, + name, + MM_PORT_TYPE_AT, /* we only allow AT ports here */ + MM_AT_PORT_FLAG_NONE, + error)) { + if (modem) + g_object_unref (modem); + return NULL; + } + + return existing ? existing : modem; +} + +/*****************************************************************************/ + +G_MODULE_EXPORT MMPlugin * +mm_plugin_create (void) +{ + static const gchar *subsystems[] = { "tty", NULL }; + static const guint16 vendor_ids[] = { 0x230d, 0 }; + + return MM_PLUGIN ( + g_object_new (MM_TYPE_PLUGIN_LINKTOP, + MM_PLUGIN_BASE_NAME, "Linktop", + MM_PLUGIN_BASE_ALLOWED_SUBSYSTEMS, subsystems, + MM_PLUGIN_BASE_ALLOWED_VENDOR_IDS, vendor_ids, + MM_PLUGIN_BASE_ALLOWED_AT, TRUE, + NULL)); +} + +static void +mm_plugin_linktop_init (MMPluginLinktop *self) +{ +} + +static void +mm_plugin_linktop_class_init (MMPluginLinktopClass *klass) +{ + MMPluginBaseClass *pb_class = MM_PLUGIN_BASE_CLASS (klass); + + pb_class->grab_port = grab_port; +} diff --git a/plugins/linktop/mm-plugin-linktop.h b/plugins/linktop/mm-plugin-linktop.h new file mode 100644 index 00000000..e4b68c41 --- /dev/null +++ b/plugins/linktop/mm-plugin-linktop.h @@ -0,0 +1,42 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * 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: + * + * Copyright (C) 2008 - 2009 Novell, Inc. + * Copyright (C) 2009 - 2012 Red Hat, Inc. + * Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org> + */ + +#ifndef MM_PLUGIN_LINKTOP_H +#define MM_PLUGIN_LINKTOP_H + +#include "mm-plugin-base.h" + +#define MM_TYPE_PLUGIN_LINKTOP (mm_plugin_linktop_get_type ()) +#define MM_PLUGIN_LINKTOP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MM_TYPE_PLUGIN_LINKTOP, MMPluginLinktop)) +#define MM_PLUGIN_LINKTOP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MM_TYPE_PLUGIN_LINKTOP, MMPluginLinktopClass)) +#define MM_IS_PLUGIN_LINKTOP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MM_TYPE_PLUGIN_LINKTOP)) +#define MM_IS_PLUGIN_LINKTOP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MM_TYPE_PLUGIN_LINKTOP)) +#define MM_PLUGIN_LINKTOP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_PLUGIN_LINKTOP, MMPluginLinktopClass)) + +typedef struct { + MMPluginBase parent; +} MMPluginLinktop; + +typedef struct { + MMPluginBaseClass parent; +} MMPluginLinktopClass; + +GType mm_plugin_linktop_get_type (void); + +G_MODULE_EXPORT MMPlugin *mm_plugin_create (void); + +#endif /* MM_PLUGIN_LINKTOP_H */ |