diff options
author | Aleksander Morgado <aleksander@lanedo.com> | 2011-12-26 21:31:07 +0100 |
---|---|---|
committer | Aleksander Morgado <aleksander@lanedo.com> | 2012-03-15 14:14:37 +0100 |
commit | dcecb94631935584919c2cce6a814b78772ac0aa (patch) | |
tree | 20426d55f445a56f0a8a46a55509ad32d789f511 | |
parent | 034381250277751741f134123fc61c0c5405adea (diff) |
libmm-common,libmm-glib: new object to handle properties in simple Connect()
Instead of using type unsafe properties passed to the connect() call, we build
a new object which does the handling of the properties to be passed.
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | libmm-common/Makefile.am | 3 | ||||
-rw-r--r-- | libmm-common/libmm-common.h | 1 | ||||
-rw-r--r-- | libmm-common/mm-common-connect-properties.c | 580 | ||||
-rw-r--r-- | libmm-common/mm-common-connect-properties.h | 116 | ||||
-rw-r--r-- | libmm-common/mm-common-helpers.c | 74 | ||||
-rw-r--r-- | libmm-common/mm-common-helpers.h | 8 | ||||
-rw-r--r-- | libmm-glib/Makefile.am | 2 | ||||
-rw-r--r-- | libmm-glib/mm-modem-simple-connect-properties.c | 123 | ||||
-rw-r--r-- | libmm-glib/mm-modem-simple-connect-properties.h | 71 | ||||
-rw-r--r-- | libmm-glib/mm-modem-simple.c | 86 | ||||
-rw-r--r-- | libmm-glib/mm-modem-simple.h | 21 |
12 files changed, 983 insertions, 106 deletions
@@ -50,7 +50,9 @@ data/org.freedesktop.ModemManager1.policy include/ModemManager-names.h -libmm-common/*.[ch] +libmm-common/mm-gdbus-*.[ch] +libmm-common/mm-enums-types.[ch] +libmm-common/mm-errors-types.[ch] libmm-common/*.xml po/Makefile.in.in diff --git a/libmm-common/Makefile.am b/libmm-common/Makefile.am index 437fb1cf..14766be1 100644 --- a/libmm-common/Makefile.am +++ b/libmm-common/Makefile.am @@ -151,6 +151,7 @@ include_HEADERS = \ mm-errors-types.h \ mm-enums-types.h \ mm-common-helpers.h \ + mm-common-connect-properties.h \ mm-gdbus-manager.h \ mm-gdbus-modem.h \ mm-gdbus-bearer.h \ @@ -163,6 +164,8 @@ libmm_common_la_SOURCES = \ mm-errors-quarks.c \ mm-common-helpers.h \ mm-common-helpers.c \ + mm-common-connect-properties.h \ + mm-common-connect-properties.c \ libmm-common.h libmm_common_la_CPPFLAGS = \ diff --git a/libmm-common/libmm-common.h b/libmm-common/libmm-common.h index 10ac5871..75bbc364 100644 --- a/libmm-common/libmm-common.h +++ b/libmm-common/libmm-common.h @@ -26,6 +26,7 @@ #include "mm-errors-types.h" #include "mm-enums-types.h" #include "mm-common-helpers.h" +#include "mm-common-connect-properties.h" #include "mm-gdbus-manager.h" #include "mm-gdbus-modem.h" #include "mm-gdbus-bearer.h" diff --git a/libmm-common/mm-common-connect-properties.c b/libmm-common/mm-common-connect-properties.c new file mode 100644 index 00000000..35381ab0 --- /dev/null +++ b/libmm-common/mm-common-connect-properties.c @@ -0,0 +1,580 @@ +/* -*- 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) 2011 Aleksander Morgado <aleksander@gnu.org> + */ + +#include <string.h> + +#include <libmm-common.h> + +#include "mm-common-connect-properties.h" + +G_DEFINE_TYPE (MMCommonConnectProperties, mm_common_connect_properties, G_TYPE_OBJECT); + +#define PROPERTY_PIN "pin" +#define PROPERTY_OPERATOR_ID "operator-id" +#define PROPERTY_ALLOWED_BANDS "allowed-bands" +#define PROPERTY_ALLOWED_MODES "allowed-modes" +#define PROPERTY_PREFERRED_MODE "preferred-mode" +#define PROPERTY_APN "apn" +#define PROPERTY_USER "user" +#define PROPERTY_PASSWORD "password" +#define PROPERTY_IP_TYPE "ip-type" +#define PROPERTY_NUMBER "number" +#define PROPERTY_ALLOW_ROAMING "allow-roaming" + +struct _MMCommonConnectPropertiesPrivate { + /* PIN */ + gchar *pin; + /* Operator ID */ + gchar *operator_id; + /* Bands */ + MMModemBand *allowed_bands; + guint n_allowed_bands; + /* Modes */ + gboolean allowed_modes_set; + MMModemMode allowed_modes; + MMModemMode preferred_mode; + /* APN */ + gchar *apn; + /* User */ + gchar *user; + /* Password */ + gchar *password; + /* IP type */ + gchar *ip_type; + /* Number */ + gchar *number; + /* Roaming allowance */ + gboolean allow_roaming_set; + gboolean allow_roaming; +}; + +/*****************************************************************************/ + +void +mm_common_connect_properties_set_pin (MMCommonConnectProperties *self, + const gchar *pin) +{ + g_free (self->priv->pin); + self->priv->pin = g_strdup (pin); +} + +void +mm_common_connect_properties_set_operator_id (MMCommonConnectProperties *self, + const gchar *operator_id) +{ + g_free (self->priv->operator_id); + self->priv->operator_id = g_strdup (operator_id); +} + +void +mm_common_connect_properties_set_allowed_bands (MMCommonConnectProperties *self, + const MMModemBand *bands, + guint n_bands) +{ + g_free (self->priv->allowed_bands); + self->priv->n_allowed_bands = n_bands; + self->priv->allowed_bands = g_new (MMModemBand, self->priv->n_allowed_bands); + memcpy (self->priv->allowed_bands, + bands, + sizeof (MMModemBand) * self->priv->n_allowed_bands); +} + +void +mm_common_connect_properties_set_allowed_modes (MMCommonConnectProperties *self, + MMModemMode allowed, + MMModemMode preferred) +{ + self->priv->allowed_modes = allowed; + self->priv->preferred_mode = preferred; + self->priv->allowed_modes_set = TRUE; +} + +void +mm_common_connect_properties_set_apn (MMCommonConnectProperties *self, + const gchar *apn) +{ + g_free (self->priv->apn); + self->priv->apn = g_strdup (apn); +} + +void +mm_common_connect_properties_set_user (MMCommonConnectProperties *self, + const gchar *user) +{ + g_free (self->priv->user); + self->priv->user = g_strdup (user); +} + +void +mm_common_connect_properties_set_password (MMCommonConnectProperties *self, + const gchar *password) +{ + g_free (self->priv->password); + self->priv->password = g_strdup (password); +} + +void +mm_common_connect_properties_set_ip_type (MMCommonConnectProperties *self, + const gchar *ip_type) +{ + g_free (self->priv->ip_type); + self->priv->ip_type = g_strdup (ip_type); +} + +void +mm_common_connect_properties_set_allow_roaming (MMCommonConnectProperties *self, + gboolean allow_roaming) +{ + self->priv->allow_roaming = allow_roaming; + self->priv->allow_roaming_set = TRUE; +} + +void +mm_common_connect_properties_set_number (MMCommonConnectProperties *self, + const gchar *number) +{ + g_free (self->priv->number); + self->priv->number = g_strdup (number); +} + +/*****************************************************************************/ + +const gchar * +mm_common_connect_properties_get_pin (MMCommonConnectProperties *self) +{ + return self->priv->apn; +} + +const gchar * +mm_common_connect_properties_get_operator_id (MMCommonConnectProperties *self) +{ + return self->priv->operator_id; +} + +void +mm_common_connect_properties_get_allowed_bands (MMCommonConnectProperties *self, + const MMModemBand **bands, + guint *n_bands) +{ + *bands = self->priv->allowed_bands; + *n_bands = self->priv->n_allowed_bands; +} + +void +mm_common_connect_properties_get_allowed_modes (MMCommonConnectProperties *self, + MMModemMode *allowed, + MMModemMode *preferred) +{ + *allowed = self->priv->allowed_modes; + *preferred = self->priv->preferred_mode; +} + +const gchar * +mm_common_connect_properties_get_apn (MMCommonConnectProperties *self) +{ + return self->priv->apn; +} + +const gchar * +mm_common_connect_properties_get_user (MMCommonConnectProperties *self) +{ + return self->priv->user; +} + +const gchar * +mm_common_connect_properties_get_password (MMCommonConnectProperties *self) +{ + return self->priv->password; +} + +const gchar * +mm_common_connect_properties_get_ip_type (MMCommonConnectProperties *self) +{ + return self->priv->ip_type; +} + +gboolean +mm_common_connect_properties_get_allow_roaming (MMCommonConnectProperties *self) +{ + return self->priv->allow_roaming; +} + +const gchar * +mm_common_connect_properties_get_number (MMCommonConnectProperties *self) +{ + return self->priv->number; +} + +/*****************************************************************************/ + +GVariant * +mm_common_connect_properties_get_dictionary (MMCommonConnectProperties *self) +{ + GVariantBuilder builder; + + /* We do allow NULL */ + if (!self) + return NULL; + + g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}")); + + if (self->priv->pin) + g_variant_builder_add (&builder, + "{sv}", + PROPERTY_PIN, + g_variant_new_string (self->priv->pin)); + + if (self->priv->operator_id) + g_variant_builder_add (&builder, + "{sv}", + PROPERTY_OPERATOR_ID, + g_variant_new_string (self->priv->operator_id)); + + if (self->priv->allowed_bands) + g_variant_builder_add (&builder, + "{sv}", + PROPERTY_ALLOWED_BANDS, + mm_common_bands_array_to_variant (self->priv->allowed_bands, + self->priv->n_allowed_bands)); + + if (self->priv->allowed_modes_set) { + g_variant_builder_add (&builder, + "{sv}", + PROPERTY_ALLOWED_MODES, + g_variant_new_uint32 (self->priv->allowed_modes)); + g_variant_builder_add (&builder, + "{sv}", + PROPERTY_PREFERRED_MODE, + g_variant_new_uint32 (self->priv->preferred_mode)); + } + + if (self->priv->apn) + g_variant_builder_add (&builder, + "{sv}", + PROPERTY_APN, + g_variant_new_string (self->priv->apn)); + + if (self->priv->user) + g_variant_builder_add (&builder, + "{sv}", + PROPERTY_USER, + g_variant_new_string (self->priv->user)); + + if (self->priv->password) + g_variant_builder_add (&builder, + "{sv}", + PROPERTY_PASSWORD, + g_variant_new_string (self->priv->password)); + + if (self->priv->ip_type) + g_variant_builder_add (&builder, + "{sv}", + PROPERTY_IP_TYPE, + g_variant_new_string (self->priv->ip_type)); + + if (self->priv->number) + g_variant_builder_add (&builder, + "{sv}", + PROPERTY_NUMBER, + g_variant_new_string (self->priv->number)); + + if (self->priv->allow_roaming_set) + g_variant_builder_add (&builder, + "{sv}", + PROPERTY_ALLOW_ROAMING, + g_variant_new_boolean (self->priv->allow_roaming)); + + return g_variant_ref_sink (g_variant_builder_end (&builder)); +} + +/*****************************************************************************/ + +MMCommonConnectProperties * +mm_common_connect_properties_new_from_string (const gchar *str, + GError **error) +{ + GError *inner_error = NULL; + MMCommonConnectProperties *properties; + gchar **words; + gchar *key; + gchar *value; + guint i; + const gchar *allowed_modes_str = NULL; + const gchar *preferred_mode_str = NULL; + + properties = mm_common_connect_properties_new (); + + /* Expecting input as: + * key1=string,key2=true,key3=false... + * */ + + words = g_strsplit_set (str, ",= ", -1); + if (!words) + return properties; + + i = 0; + key = words[i]; + while (key) { + value = words[++i]; + + if (!value) { + inner_error = g_error_new (MM_CORE_ERROR, + MM_CORE_ERROR_INVALID_ARGS, + "Invalid properties string, no value for key '%s'", + key); + break; + } + + if (g_str_equal (key, PROPERTY_PIN)) + mm_common_connect_properties_set_pin (properties, value); + else if (g_str_equal (key, PROPERTY_OPERATOR_ID)) + mm_common_connect_properties_set_operator_id (properties, value); + else if (g_str_equal (key, PROPERTY_APN)) + mm_common_connect_properties_set_apn (properties, value); + else if (g_str_equal (key, PROPERTY_USER)) + mm_common_connect_properties_set_user (properties, value); + else if (g_str_equal (key, PROPERTY_PASSWORD)) + mm_common_connect_properties_set_password (properties, value); + else if (g_str_equal (key, PROPERTY_IP_TYPE)) + mm_common_connect_properties_set_ip_type (properties, value); + else if (g_str_equal (key, PROPERTY_ALLOW_ROAMING)) { + gboolean allow_roaming; + + allow_roaming = mm_common_get_boolean_from_string (value, &inner_error); + if (!inner_error) + mm_common_connect_properties_set_allow_roaming (properties, allow_roaming); + } else if (g_str_equal (key, PROPERTY_NUMBER)) + mm_common_connect_properties_set_number (properties, value); + else if (g_str_equal (key, PROPERTY_ALLOWED_BANDS)) { + MMModemBand *bands = NULL; + guint n_bands = 0; + + mm_common_get_bands_from_string (value, &bands, &n_bands, &inner_error); + if (!inner_error) + mm_common_connect_properties_set_allowed_bands (properties, bands, n_bands); + g_free (bands); + } else if (g_str_equal (key, PROPERTY_ALLOWED_MODES)) { + allowed_modes_str = value; + } else if (g_str_equal (key, PROPERTY_PREFERRED_MODE)) { + preferred_mode_str = value; + } else { + inner_error = g_error_new (MM_CORE_ERROR, + MM_CORE_ERROR_INVALID_ARGS, + "Invalid properties string, unexpected key '%s'", + key); + break; + } + + key = words[++i]; + } + + /* If error, destroy the object */ + if (inner_error) { + g_propagate_error (error, inner_error); + g_object_unref (properties); + properties = NULL; + } + else if (allowed_modes_str || preferred_mode_str) { + MMModemMode allowed_modes; + MMModemMode preferred_mode; + + allowed_modes = (allowed_modes_str ? + mm_common_get_modes_from_string (allowed_modes_str, + &inner_error) : + MM_MODEM_MODE_ANY); + if (!inner_error) { + preferred_mode = (preferred_mode_str ? + mm_common_get_modes_from_string (preferred_mode_str, + &inner_error) : + MM_MODEM_MODE_NONE); + } + + if (inner_error) { + g_propagate_error (error, inner_error); + g_object_unref (properties); + properties = NULL; + } else { + mm_common_connect_properties_set_allowed_modes ( + properties, + allowed_modes, + preferred_mode); + } + } + + g_strfreev (words); + return properties; +} + +/*****************************************************************************/ + +MMCommonConnectProperties * +mm_common_connect_properties_new_from_dictionary (GVariant *dictionary, + GError **error) +{ + GError *inner_error = NULL; + GVariantIter iter; + gchar *key; + GVariant *value; + MMCommonConnectProperties *properties; + GVariant *allowed_modes_variant = NULL; + GVariant *preferred_mode_variant = NULL; + + properties = mm_common_connect_properties_new (); + if (!dictionary) + return properties; + + g_variant_iter_init (&iter, dictionary); + while (!inner_error && + g_variant_iter_next (&iter, "{sv}", &key, &value)) { + if (g_str_equal (key, PROPERTY_PIN)) + mm_common_connect_properties_set_pin ( + properties, + g_variant_get_string (value, NULL)); + else if (g_str_equal (key, PROPERTY_OPERATOR_ID)) + mm_common_connect_properties_set_operator_id ( + properties, + g_variant_get_string (value, NULL)); + else if (g_str_equal (key, PROPERTY_ALLOWED_BANDS)) { + GArray *array; + + array = mm_common_bands_variant_to_garray (value); + mm_common_connect_properties_set_allowed_bands ( + properties, + (MMModemBand *)array->data, + array->len); + g_array_unref (array); + } else if (g_str_equal (key, PROPERTY_ALLOWED_MODES)) + allowed_modes_variant = g_variant_ref (value); + else if (g_str_equal (key, PROPERTY_PREFERRED_MODE)) + preferred_mode_variant = g_variant_ref (value); + else if (g_str_equal (key, PROPERTY_APN)) + mm_common_connect_properties_set_apn ( + properties, + g_variant_get_string (value, NULL)); + else if (g_str_equal (key, PROPERTY_USER)) + mm_common_connect_properties_set_user ( + properties, + g_variant_get_string (value, NULL)); + else if (g_str_equal (key, PROPERTY_PASSWORD)) + mm_common_connect_properties_set_password ( + properties, + g_variant_get_string (value, NULL)); + else if (g_str_equal (key, PROPERTY_IP_TYPE)) + mm_common_connect_properties_set_ip_type ( + properties, + g_variant_get_string (value, NULL)); + else if (g_str_equal (key, PROPERTY_NUMBER)) + mm_common_connect_properties_set_number ( + properties, + g_variant_get_string (value, NULL)); + else if (g_str_equal (key, PROPERTY_ALLOW_ROAMING)) + mm_common_connect_properties_set_allow_roaming ( + properties, + g_variant_get_boolean (value)); + else { + /* Set inner error, will stop the loop */ + inner_error = g_error_new (MM_CORE_ERROR, + MM_CORE_ERROR_INVALID_ARGS, + "Invalid properties dictionary, unexpected key '%s'", + key); + } + + g_free (key); + g_variant_unref (value); + } + + /* If error, destroy the object */ + if (inner_error) { + g_propagate_error (error, inner_error); + g_object_unref (properties); + properties = NULL; + } + /* If we got allowed modes variant, check if we got preferred mode */ + else if (allowed_modes_variant) { + mm_common_connect_properties_set_allowed_modes ( + properties, + g_variant_get_uint32 (allowed_modes_variant), + (preferred_mode_variant ? + g_variant_get_uint32 (preferred_mode_variant) : + MM_MODEM_MODE_NONE)); + } + /* If we only got preferred mode, assume allowed is ANY */ + else if (preferred_mode_variant) { + mm_common_connect_properties_set_allowed_modes ( + properties, + MM_MODEM_MODE_ANY, + g_variant_get_uint32 (preferred_mode_variant)); + } + + /* Cleanup last things before exiting */ + if (allowed_modes_variant) + g_variant_unref (allowed_modes_variant); + if (preferred_mode_variant) + g_variant_unref (preferred_mode_variant); + + return properties; +} + +/*****************************************************************************/ + +MMCommonConnectProperties * +mm_common_connect_properties_new (void) +{ + return (MM_COMMON_CONNECT_PROPERTIES ( + g_object_new (MM_TYPE_COMMON_CONNECT_PROPERTIES, NULL))); +} + +static void +mm_common_connect_properties_init (MMCommonConnectProperties *self) +{ + self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self), + MM_TYPE_COMMON_CONNECT_PROPERTIES, + MMCommonConnectPropertiesPrivate); + + /* Some defaults */ + self->priv->allow_roaming = TRUE; + self->priv->allowed_modes = MM_MODEM_MODE_ANY; + self->priv->preferred_mode = MM_MODEM_MODE_NONE; + self->priv->allowed_bands = g_new (MMModemBand, 1); + self->priv->allowed_bands[0] = MM_MODEM_BAND_ANY; + self->priv->n_allowed_bands = 1; +} + +static void +finalize (GObject *object) +{ + MMCommonConnectProperties *self = MM_COMMON_CONNECT_PROPERTIES (object); + + g_free (self->priv->pin); + g_free (self->priv->operator_id); + g_free (self->priv->allowed_bands); + g_free (self->priv->apn); + g_free (self->priv->user); + g_free (self->priv->password); + g_free (self->priv->ip_type); + g_free (self->priv->number); + + G_OBJECT_CLASS (mm_common_connect_properties_parent_class)->finalize (object); +} + +static void +mm_common_connect_properties_class_init (MMCommonConnectPropertiesClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (MMCommonConnectPropertiesPrivate)); + + object_class->finalize = finalize; +} diff --git a/libmm-common/mm-common-connect-properties.h b/libmm-common/mm-common-connect-properties.h new file mode 100644 index 00000000..a4de48bc --- /dev/null +++ b/libmm-common/mm-common-connect-properties.h @@ -0,0 +1,116 @@ +/* -*- 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) 2011 Aleksander Morgado <aleksander@gnu.org> + */ + +#ifndef MM_COMMON_CONNECT_PROPERTIES_H +#define MM_COMMON_CONNECT_PROPERTIES_H + +#include <ModemManager.h> +#include <glib-object.h> + +G_BEGIN_DECLS + +#define MM_TYPE_COMMON_CONNECT_PROPERTIES (mm_common_connect_properties_get_type ()) +#define MM_COMMON_CONNECT_PROPERTIES(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MM_TYPE_COMMON_CONNECT_PROPERTIES, MMCommonConnectProperties)) +#define MM_COMMON_CONNECT_PROPERTIES_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MM_TYPE_COMMON_CONNECT_PROPERTIES, MMCommonConnectPropertiesClass)) +#define MM_IS_COMMON_CONNECT_PROPERTIES(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MM_TYPE_COMMON_CONNECT_PROPERTIES)) +#define MM_IS_COMMON_CONNECT_PROPERTIES_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MM_TYPE_COMMON_CONNECT_PROPERTIES)) +#define MM_COMMON_CONNECT_PROPERTIES_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_COMMON_CONNECT_PROPERTIES, MMCommonConnectPropertiesClass)) + +typedef struct _MMCommonConnectProperties MMCommonConnectProperties; +typedef struct _MMCommonConnectPropertiesClass MMCommonConnectPropertiesClass; +typedef struct _MMCommonConnectPropertiesPrivate MMCommonConnectPropertiesPrivate; + +struct _MMCommonConnectProperties { + GObject parent; + MMCommonConnectPropertiesPrivate *priv; +}; + +struct _MMCommonConnectPropertiesClass { + GObjectClass parent; +}; + +GType mm_common_connect_properties_get_type (void); + +MMCommonConnectProperties *mm_common_connect_properties_new (void); +MMCommonConnectProperties *mm_common_connect_properties_new_from_string ( + const gchar *str, + GError **error); +MMCommonConnectProperties *mm_common_connect_properties_new_from_dictionary ( + GVariant *dictionary, + GError **error); + +void mm_common_connect_properties_set_pin ( + MMCommonConnectProperties *properties, + const gchar *pin); +void mm_common_connect_properties_set_operator_id ( + MMCommonConnectProperties *properties, + const gchar *operator_id); +void mm_common_connect_properties_set_allowed_bands ( + MMCommonConnectProperties *properties, + const MMModemBand *bands, + guint n_bands); +void mm_common_connect_properties_set_allowed_modes ( + MMCommonConnectProperties *properties, + MMModemMode allowed, + MMModemMode preferred); +void mm_common_connect_properties_set_apn ( + MMCommonConnectProperties *properties, + const gchar *apn); +void mm_common_connect_properties_set_user ( + MMCommonConnectProperties *properties, + const gchar *user); +void mm_common_connect_properties_set_password ( + MMCommonConnectProperties *properties, + const gchar *password); +void mm_common_connect_properties_set_ip_type ( + MMCommonConnectProperties *properties, + const gchar *ip_type); +void mm_common_connect_properties_set_allow_roaming ( + MMCommonConnectProperties *properties, + gboolean allow_roaming); +void mm_common_connect_properties_set_number ( + MMCommonConnectProperties *properties, + const gchar *number); + +const gchar *mm_common_connect_properties_get_pin ( + MMCommonConnectProperties *properties); +const gchar *mm_common_connect_properties_get_operator_id ( + MMCommonConnectProperties *properties); +void mm_common_connect_properties_get_allowed_bands ( + MMCommonConnectProperties *properties, + const MMModemBand **bands, + guint *n_bands); +void mm_common_connect_properties_get_allowed_modes ( + MMCommonConnectProperties *properties, + MMModemMode *allowed, + MMModemMode *preferred); +const gchar *mm_common_connect_properties_get_apn ( + MMCommonConnectProperties *properties); +const gchar *mm_common_connect_properties_get_user ( + MMCommonConnectProperties *properties); +const gchar *mm_common_connect_properties_get_password ( + MMCommonConnectProperties *properties); +const gchar *mm_common_connect_properties_get_ip_type ( + MMCommonConnectProperties *properties); +gboolean mm_common_connect_properties_get_allow_roaming ( + MMCommonConnectProperties *properties); +const gchar *mm_common_connect_properties_get_number ( + MMCommonConnectProperties *properties); + +GVariant *mm_common_connect_properties_get_dictionary (MMCommonConnectProperties *self); + +G_END_DECLS + +#endif /* MM_COMMON_CONNECT_PROPERTIES_H */ diff --git a/libmm-common/mm-common-helpers.c b/libmm-common/mm-common-helpers.c index c7eb4821..b117c3f9 100644 --- a/libmm-common/mm-common-helpers.c +++ b/libmm-common/mm-common-helpers.c @@ -15,7 +15,10 @@ #include <gio/gio.h> +#include <ModemManager.h> + #include "mm-enums-types.h" +#include "mm-errors-types.h" #include "mm-common-helpers.h" gchar * @@ -169,8 +172,10 @@ mm_common_get_bands_string (const MMModemBand *bands, } MMModemMode -mm_common_get_modes_from_string (const gchar *str) +mm_common_get_modes_from_string (const gchar *str, + GError **error) { + GError *inner_error = NULL; MMModemMode modes; gchar **mode_strings; GFlagsClass *flags_class; @@ -195,12 +200,22 @@ mm_common_get_modes_from_string (const gchar *str) } } - if (!found) - g_warning ("Couldn't match '%s' with a valid MMModemMode value", - mode_strings[i]); + if (!found) { + inner_error = g_error_new ( + MM_CORE_ERROR, + MM_CORE_ERROR_INVALID_ARGS, + "Couldn't match '%s' with a valid MMModemMode value", + mode_strings[i]); + break; + } } } + if (inner_error) { + g_propagate_error (error, inner_error); + modes = MM_MODEM_MODE_NONE; + } + g_type_class_unref (flags_class); g_strfreev (mode_strings); return modes; @@ -209,8 +224,10 @@ mm_common_get_modes_from_string (const gchar *str) void mm_common_get_bands_from_string (const gchar *str, MMModemBand **bands, - guint *n_bands) + guint *n_bands, + GError **error) { + GError *inner_error = NULL; GArray *array; gchar **band_strings; GEnumClass *enum_class; @@ -235,24 +252,35 @@ mm_common_get_bands_from_string (const gchar *str, } } - if (!found) - g_warning ("Couldn't match '%s' with a valid MMModemBand value", - band_strings[i]); + if (!found) { + inner_error = g_error_new (MM_CORE_ERROR, + MM_CORE_ERROR_INVALID_ARGS, + "Couldn't match '%s' with a valid MMModemBand value", + band_strings[i]); + break; + } } } - if (!array->len) { - GEnumValue *value; + if (inner_error) { + g_propagate_error (error, inner_error); + g_array_free (array, TRUE); + *n_bands = 0; + *bands = NULL; + } else { + if (!array->len) { + GEnumValue *value; - value = g_enum_get_value (enum_class, MM_MODEM_BAND_UNKNOWN); - g_array_append_val (array, value->value); + value = g_enum_get_value (enum_class, MM_MODEM_BAND_UNKNOWN); + g_array_append_val (array, value->value); + } + + *n_bands = array->len; + *bands = (MMModemBand *)g_array_free (array, FALSE); } g_type_class_unref (enum_class); g_strfreev (band_strings); - - *n_bands = array->len; - *bands = (MMModemBand *)g_array_free (array, FALSE); } GArray * @@ -295,3 +323,19 @@ mm_common_bands_garray_to_variant (GArray *array) return mm_common_bands_array_to_variant ((const MMModemBand *)array->data, array->len); } + +gboolean +mm_common_get_boolean_from_string (const gchar *value, + GError **error) +{ + if (!g_ascii_strcasecmp (value, "true") || g_str_equal (value, "1")) + return TRUE; + + if (g_ascii_strcasecmp (value, "false") && g_str_equal (value, "0")) + g_set_error (error, + MM_CORE_ERROR, + MM_CORE_ERROR_INVALID_ARGS, + "Cannot get boolean from string '%s'", value); + + return FALSE; +} diff --git a/libmm-common/mm-common-helpers.h b/libmm-common/mm-common-helpers.h index 1c3dcc28..077828e1 100644 --- a/libmm-common/mm-common-helpers.h +++ b/libmm-common/mm-common-helpers.h @@ -25,10 +25,14 @@ gchar *mm_common_get_modes_string (MMModemMode mode); gchar *mm_common_get_bands_string (const MMModemBand *bands, guint n_bands); -MMModemMode mm_common_get_modes_from_string (const gchar *str); +MMModemMode mm_common_get_modes_from_string (const gchar *str, + GError **error); void mm_common_get_bands_from_string (const gchar *str, MMModemBand **bands, - guint *n_bands); + guint *n_bands, + GError **error); +gboolean mm_common_get_boolean_from_string (const gchar *value, + GError **error); GArray *mm_common_bands_variant_to_garray (GVariant *variant); GVariant *mm_common_bands_array_to_variant (const MMModemBand *bands, diff --git a/libmm-glib/Makefile.am b/libmm-glib/Makefile.am index d7740306..2b58d8cb 100644 --- a/libmm-glib/Makefile.am +++ b/libmm-glib/Makefile.am @@ -20,6 +20,8 @@ libmm_glib_la_SOURCES = \ mm-modem.c \ mm-modem-3gpp.h \ mm-modem-3gpp.c \ + mm-modem-simple-connect-properties.h \ + mm-modem-simple-connect-properties.c \ mm-modem-simple.h \ mm-modem-simple.c \ mm-sim.h \ diff --git a/libmm-glib/mm-modem-simple-connect-properties.c b/libmm-glib/mm-modem-simple-connect-properties.c new file mode 100644 index 00000000..5a48c958 --- /dev/null +++ b/libmm-glib/mm-modem-simple-connect-properties.c @@ -0,0 +1,123 @@ +/* -*- 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) 2011 Aleksander Morgado <aleksander@gnu.org> + */ + +#include "mm-modem-simple-connect-properties.h" + +void +mm_modem_simple_connect_properties_set_pin (MMModemSimpleConnectProperties *self, + const gchar *pin) +{ + g_return_if_fail (MM_IS_MODEM_SIMPLE_CONNECT_PROPERTIES (self)); + + mm_common_connect_properties_set_pin (self, pin); +} + +void +mm_modem_simple_connect_properties_set_operator_id (MMModemSimpleConnectProperties *self, + const gchar *operator_id) +{ + g_return_if_fail (MM_IS_MODEM_SIMPLE_CONNECT_PROPERTIES (self)); + + mm_common_connect_properties_set_operator_id (self, operator_id); +} + +void +mm_modem_simple_connect_properties_set_allowed_bands (MMModemSimpleConnectProperties *self, + const MMModemBand *bands, + guint n_bands) +{ + g_return_if_fail (MM_IS_MODEM_SIMPLE_CONNECT_PROPERTIES (self)); + + mm_common_connect_properties_set_allowed_bands (self, bands, n_bands); +} + +void +mm_modem_simple_connect_properties_set_allowed_modes (MMModemSimpleConnectProperties *self, + MMModemMode allowed, + MMModemMode preferred) +{ + g_return_if_fail (MM_IS_MODEM_SIMPLE_CONNECT_PROPERTIES (self)); + + mm_common_connect_properties_set_allowed_modes (self, allowed, preferred); +} + +void +mm_modem_simple_connect_properties_set_apn (MMModemSimpleConnectProperties *self, + const gchar *apn) +{ + g_return_if_fail (MM_IS_MODEM_SIMPLE_CONNECT_PROPERTIES (self)); + + mm_common_connect_properties_set_apn (self, apn); +} + +void +mm_modem_simple_connect_properties_set_user (MMModemSimpleConnectProperties *self, + const gchar *user) +{ + g_return_if_fail (MM_IS_MODEM_SIMPLE_CONNECT_PROPERTIES (self)); + + mm_common_connect_properties_set_user (self, user); +} + +void +mm_modem_simple_connect_properties_set_password (MMModemSimpleConnectProperties *self, + const gchar *password) +{ + g_return_if_fail (MM_IS_MODEM_SIMPLE_CONNECT_PROPERTIES (self)); + + mm_common_connect_properties_set_password (self, password); +} + +void +mm_modem_simple_connect_properties_set_ip_type (MMModemSimpleConnectProperties *self, + const gchar *ip_type) +{ + g_return_if_fail (MM_IS_MODEM_SIMPLE_CONNECT_PROPERTIES (self)); + + mm_common_connect_properties_set_ip_type (self, ip_type); +} + +void +mm_modem_simple_connect_properties_set_allow_roaming (MMModemSimpleConnectProperties *self, + gboolean allow_roaming) +{ + g_return_if_fail (MM_IS_MODEM_SIMPLE_CONNECT_PROPERTIES (self)); + + mm_common_connect_properties_set_allow_roaming (self, allow_roaming); +} + +void +mm_modem_simple_connect_properties_set_number (MMModemSimpleConnectProperties *self, + const gchar *number) +{ + g_return_if_fail (MM_IS_MODEM_SIMPLE_CONNECT_PROPERTIES (self)); + + mm_common_connect_properties_set_number (self, number); +} + +/*****************************************************************************/ + +MMModemSimpleConnectProperties * +mm_modem_simple_connect_properties_new_from_string (const gchar *str, + GError **error) +{ + return mm_common_connect_properties_new_from_string (str, error); +} + +MMModemSimpleConnectProperties * +mm_modem_simple_connect_properties_new (void) +{ + return mm_common_connect_properties_new (); +} diff --git a/libmm-glib/mm-modem-simple-connect-properties.h b/libmm-glib/mm-modem-simple-connect-properties.h new file mode 100644 index 00000000..80d88f1a --- /dev/null +++ b/libmm-glib/mm-modem-simple-connect-properties.h @@ -0,0 +1,71 @@ +/* -*- 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) 2011 Aleksander Morgado <aleksander@gnu.org> + */ + +#ifndef MM_MODEM_SIMPLE_CONNECT_PROPERTIES_H +#define MM_MODEM_SIMPLE_CONNECT_PROPERTIES_H + +#include <ModemManager.h> +#include <glib-object.h> + +#include <libmm-common.h> + +G_BEGIN_DECLS + +typedef MMCommonConnectProperties MMModemSimpleConnectProperties; +#define MM_TYPE_MODEM_SIMPLE_CONNECT_PROPERTIES(o) MM_TYPE_COMMON_CONNECT_PROPERTIES (o) +#define MM_MODEM_SIMPLE_CONNECT_PROPERTIES(o) MM_COMMON_CONNECT_PROPERTIES(o) +#define MM_IS_MODEM_SIMPLE_CONNECT_PROPERTIES(o) MM_IS_COMMON_CONNECT_PROPERTIES(o) + +MMModemSimpleConnectProperties *mm_modem_simple_connect_properties_new (void); +MMModemSimpleConnectProperties *mm_modem_simple_connect_properties_new_from_string ( + const gchar *str, + GError **error); + +void mm_modem_simple_connect_properties_set_pin ( + MMModemSimpleConnectProperties *properties, + const gchar *pin); +void mm_modem_simple_connect_properties_set_operator_id ( + MMModemSimpleConnectProperties *properties, + const gchar *operator_id); +void mm_modem_simple_connect_properties_set_allowed_bands ( + MMModemSimpleConnectProperties *properties, + const MMModemBand *bands, + guint n_bands); +void mm_modem_simple_connect_properties_set_allowed_modes ( + MMModemSimpleConnectProperties *properties, + MMModemMode allowed, + MMModemMode preferred); +void mm_modem_simple_connect_properties_set_apn ( + MMModemSimpleConnectProperties *properties, + const gchar *apn); +void mm_modem_simple_connect_properties_set_user ( + MMModemSimpleConnectProperties *properties, + const gchar *user); +void mm_modem_simple_connect_properties_set_password ( + MMModemSimpleConnectProperties *properties, + const gchar *password); +void mm_modem_simple_connect_properties_set_ip_type ( + MMModemSimpleConnectProperties *properties, + const gchar *ip_type); +void mm_modem_simple_connect_properties_set_allow_roaming ( + MMModemSimpleConnectProperties *properties, + gboolean allow_roaming); +void mm_modem_simple_connect_properties_set_number ( + MMModemSimpleConnectProperties *properties, + const gchar *number); + +G_END_DECLS + +#endif /* MM_MODEM_SIMPLE_CONNECT_PROPERTIES_H */ diff --git a/libmm-glib/mm-modem-simple.c b/libmm-glib/mm-modem-simple.c index 2aa592ac..61cc4304 100644 --- a/libmm-glib/mm-modem-simple.c +++ b/libmm-glib/mm-modem-simple.c @@ -78,56 +78,6 @@ connect_context_complete_and_free (ConnectContext *ctx) g_slice_free (ConnectContext, ctx); } -static GVariant * -connect_build_properties (const gchar *first_property_name, - va_list var_args) -{ - const gchar *key; - GVariantBuilder builder; - - g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}")); - - key = first_property_name; - while (key) { - if (g_str_equal (key, MM_SIMPLE_PROPERTY_PIN) || - g_str_equal (key, MM_SIMPLE_PROPERTY_OPERATOR_ID) || - g_str_equal (key, MM_SIMPLE_PROPERTY_APN) || - g_str_equal (key, MM_SIMPLE_PROPERTY_IP_TYPE) || - g_str_equal (key, MM_SIMPLE_PROPERTY_NUMBER)) { - const gchar *value; - - /* If a key with NULL value is given, just ignore it. */ - value = va_arg (var_args, gchar *); - if (value) - g_variant_builder_add (&builder, "{sv}", key, g_variant_new_string (value)); - } else if (g_str_equal (key, MM_SIMPLE_PROPERTY_ALLOW_ROAMING)) { - gboolean value; - - value = va_arg (var_args, gboolean); - g_variant_builder_add (&builder, "{sv}", key, g_variant_new_boolean (value)); - } else if (g_str_equal (key, MM_SIMPLE_PROPERTY_ALLOWED_BANDS)) { - guint64 value; - - value = va_arg (var_args, guint64); - g_variant_builder_add (&builder, "{sv}", key, g_variant_new_uint64 (value)); - } else if (g_str_equal (key, MM_SIMPLE_PROPERTY_ALLOWED_MODES) || - g_str_equal (key, MM_SIMPLE_PROPERTY_PREFERRED_MODE)) { - guint32 value; - - value = va_arg (var_args, guint32); - g_variant_builder_add (&builder, "{sv}", key, g_variant_new_uint32 (value)); - } else { - g_warning ("Unexpected key '%s'", key); - /* ignore value */ - va_arg (var_args, gpointer); - } - - key = va_arg (var_args, gchar *); - } - - return g_variant_ref_sink (g_variant_builder_end (&builder)); -} - MMBearer * mm_modem_simple_connect_finish (MMModemSimple *self, GAsyncResult *res, @@ -190,15 +140,13 @@ simple_connect_ready (MMModemSimple *self, void mm_modem_simple_connect (MMModemSimple *self, + MMModemSimpleConnectProperties *properties, GCancellable *cancellable, GAsyncReadyCallback callback, - gpointer user_data, - const gchar *first_property_name, - ...) + gpointer user_data) { ConnectContext *ctx; - va_list va_args; - GVariant *properties; + GVariant *variant; g_return_if_fail (MM_GDBUS_IS_MODEM_SIMPLE (self)); @@ -210,39 +158,34 @@ mm_modem_simple_connect (MMModemSimple *self, if (cancellable) ctx->cancellable = g_object_ref (cancellable); - - va_start (va_args, first_property_name); - properties = connect_build_properties (first_property_name, va_args); + variant = mm_common_connect_properties_get_dictionary ( + MM_COMMON_CONNECT_PROPERTIES (properties)); mm_gdbus_modem_simple_call_connect ( self, - properties, + variant, cancellable, (GAsyncReadyCallback)simple_connect_ready, ctx); - g_variant_unref (properties); - va_end (va_args); + g_variant_unref (variant); } MMBearer * mm_modem_simple_connect_sync (MMModemSimple *self, + MMModemSimpleConnectProperties *properties, GCancellable *cancellable, - GError **error, - const gchar *first_property_name, - ...) + GError **error) { MMBearer *bearer = NULL; gchar *bearer_path = NULL; - va_list va_args; - GVariant *properties; + GVariant *variant; g_return_val_if_fail (MM_GDBUS_IS_MODEM (self), NULL); - va_start (va_args, first_property_name); - properties = connect_build_properties (first_property_name, va_args); - + variant = mm_common_connect_properties_get_dictionary ( + MM_COMMON_CONNECT_PROPERTIES (properties)); mm_gdbus_modem_simple_call_connect_sync (self, - properties, + variant, &bearer_path, cancellable, error); @@ -258,8 +201,7 @@ mm_modem_simple_connect_sync (MMModemSimple *self, g_free (bearer_path); } - g_variant_unref (properties); - va_end (va_args); + g_variant_unref (variant); return bearer; } diff --git a/libmm-glib/mm-modem-simple.h b/libmm-glib/mm-modem-simple.h index 95306ab7..4eba5f0b 100644 --- a/libmm-glib/mm-modem-simple.h +++ b/libmm-glib/mm-modem-simple.h @@ -27,6 +27,7 @@ #include <mm-gdbus-modem.h> #include "mm-bearer.h" +#include "mm-modem-simple-connect-properties.h" G_BEGIN_DECLS @@ -38,30 +39,18 @@ typedef MmGdbusModemSimple MMModemSimple; const gchar *mm_modem_simple_get_path (MMModemSimple *self); gchar *mm_modem_simple_dup_path (MMModemSimple *self); -#define MM_SIMPLE_PROPERTY_PIN "pin" /* string */ -#define MM_SIMPLE_PROPERTY_OPERATOR_ID "operator-id" /* string */ -#define MM_SIMPLE_PROPERTY_ALLOWED_BANDS "allowed-bands" /* GArray of MMModemBand */ -#define MM_SIMPLE_PROPERTY_ALLOWED_MODES "allowed-modes" /* MMModemMode */ -#define MM_SIMPLE_PROPERTY_PREFERRED_MODE "preferred-mode" /* MMModemMode */ -#define MM_SIMPLE_PROPERTY_APN "apn" /* string */ -#define MM_SIMPLE_PROPERTY_IP_TYPE "ip-type" /* string */ -#define MM_SIMPLE_PROPERTY_NUMBER "number" /* string */ -#define MM_SIMPLE_PROPERTY_ALLOW_ROAMING "allow-roaming" /* boolean */ - void mm_modem_simple_connect (MMModemSimple *self, + MMModemSimpleConnectProperties *properties, GCancellable *cancellable, GAsyncReadyCallback callback, - gpointer user_data, - const gchar *first_property_name, - ...); + gpointer user_data); MMBearer *mm_modem_simple_connect_finish (MMModemSimple *self, GAsyncResult *res, GError **error); MMBearer *mm_modem_simple_connect_sync (MMModemSimple *self, + MMModemSimpleConnectProperties *properties, GCancellable *cancellable, - GError **error, - const gchar *first_property_name, - ...); + GError **error); /* void mm_modem_simple_disconnect (MMModemSimple *self, */ /* GCancellable *cancellable, */ |