diff options
-rw-r--r-- | libmm-common/Makefile.am | 4 | ||||
-rw-r--r-- | libmm-common/libmm-common.h | 1 | ||||
-rw-r--r-- | libmm-common/mm-unlock-retries.c | 218 | ||||
-rw-r--r-- | libmm-common/mm-unlock-retries.h | 78 |
4 files changed, 301 insertions, 0 deletions
diff --git a/libmm-common/Makefile.am b/libmm-common/Makefile.am index 2743a9e1..9e817a9c 100644 --- a/libmm-common/Makefile.am +++ b/libmm-common/Makefile.am @@ -156,6 +156,7 @@ mm-common-sms-properties.c: mm-errors-types.h mm-common-bearer-ip-config.c: mm-errors-types.h mm-common-location-3gpp.c: mm-errors-types.h mm-errors-quarks.c: mm-errors-types.h +mm-unlock-retries.c: mm-enums-types.h includedir = @includedir@/libmm-common include_HEADERS = \ @@ -168,6 +169,7 @@ include_HEADERS = \ mm-common-sms-properties.h \ mm-common-bearer-ip-config.h \ mm-common-location-3gpp.h \ + mm-unlock-retries.h \ mm-gdbus-manager.h \ mm-gdbus-modem.h \ mm-gdbus-bearer.h \ @@ -194,6 +196,8 @@ libmm_common_la_SOURCES = \ mm-common-bearer-ip-config.c \ mm-common-location-3gpp.h \ mm-common-location-3gpp.c \ + mm-unlock-retries.h \ + mm-unlock-retries.c \ libmm-common.h libmm_common_la_CPPFLAGS = \ diff --git a/libmm-common/libmm-common.h b/libmm-common/libmm-common.h index 4e0500ea..819c2b73 100644 --- a/libmm-common/libmm-common.h +++ b/libmm-common/libmm-common.h @@ -32,6 +32,7 @@ #include "mm-common-bearer-properties.h" #include "mm-common-bearer-ip-config.h" #include "mm-common-location-3gpp.h" +#include "mm-unlock-retries.h" #include "mm-gdbus-manager.h" #include "mm-gdbus-modem.h" #include "mm-gdbus-bearer.h" diff --git a/libmm-common/mm-unlock-retries.c b/libmm-common/mm-unlock-retries.c new file mode 100644 index 00000000..a329c411 --- /dev/null +++ b/libmm-common/mm-unlock-retries.c @@ -0,0 +1,218 @@ +/* -*- 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) 2012 Google, Inc. + */ + +#include <string.h> +#include <errno.h> +#include <stdlib.h> + +#include "mm-enums-types.h" +#include "mm-unlock-retries.h" + +G_DEFINE_TYPE (MMUnlockRetries, mm_unlock_retries, G_TYPE_OBJECT); + +struct _MMUnlockRetriesPrivate { + GHashTable *ht; +}; + +/*****************************************************************************/ + +void +mm_unlock_retries_set (MMUnlockRetries *self, + MMModemLock lock, + guint retries) +{ + g_hash_table_replace (self->priv->ht, + GUINT_TO_POINTER (lock), + GUINT_TO_POINTER (retries)); +} + +void +mm_unlock_retries_unset (MMUnlockRetries *self, + MMModemLock lock) +{ + g_hash_table_remove (self->priv->ht, + GUINT_TO_POINTER (lock)); +} + +guint +mm_unlock_retries_get (MMUnlockRetries *self, + MMModemLock lock) +{ + gpointer value = NULL; + + return (g_hash_table_lookup_extended (self->priv->ht, + GUINT_TO_POINTER (lock), + NULL, /* original key not needed */ + &value) ? + GPOINTER_TO_UINT (value) : + MM_UNLOCK_RETRIES_UNKNOWN); +} + +/*****************************************************************************/ + +gboolean +mm_unlock_retries_cmp (MMUnlockRetries *a, + MMUnlockRetries *b) +{ + GHashTableIter iter; + gpointer key, value; + + if (g_hash_table_size (a->priv->ht) != g_hash_table_size (b->priv->ht)) + return FALSE; + + g_hash_table_iter_init (&iter, a->priv->ht); + while (g_hash_table_iter_next (&iter, &key, &value)) { + g_assert (GPOINTER_TO_UINT (value) != MM_UNLOCK_RETRIES_UNKNOWN); + + if (GPOINTER_TO_UINT (value) != mm_unlock_retries_get (b, GPOINTER_TO_UINT (key))) + return FALSE; + } + + /* All equal! */ + return TRUE; +} + +/*****************************************************************************/ + +void +mm_unlock_retries_foreach (MMUnlockRetries *self, + MMUnlockRetriesForeachCb callback, + gpointer user_data) +{ + GHashTableIter iter; + gpointer key, value; + + g_hash_table_iter_init (&iter, self->priv->ht); + while (g_hash_table_iter_next (&iter, &key, &value)) { + callback (GPOINTER_TO_UINT (key), + GPOINTER_TO_UINT (value), + user_data); + } +} + +/*****************************************************************************/ + +GVariant * +mm_unlock_retries_get_dictionary (MMUnlockRetries *self) +{ + GVariantBuilder builder; + GHashTableIter iter; + gpointer key, value; + + /* We do allow NULL */ + if (!self) + return NULL; + + g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{uu}")); + + g_hash_table_iter_init (&iter, self->priv->ht); + while (g_hash_table_iter_next (&iter, &key, &value)) { + g_variant_builder_add (&builder, + "{uu}", + GPOINTER_TO_UINT (key), + GPOINTER_TO_UINT (value)); + } + + return g_variant_ref_sink (g_variant_builder_end (&builder)); +} + +/*****************************************************************************/ + +MMUnlockRetries * +mm_unlock_retries_new_from_dictionary (GVariant *dictionary) +{ + GVariantIter iter; + guint key, value; + MMUnlockRetries *self; + + self = mm_unlock_retries_new (); + if (!dictionary) + return self; + + g_variant_iter_init (&iter, dictionary); + while (g_variant_iter_next (&iter, "{uu}", &key, &value)) { + mm_unlock_retries_set (self, + (MMModemLock)key, + value); + } + + return self; +} + +/*****************************************************************************/ + +gchar * +mm_unlock_retries_build_string (MMUnlockRetries *self) +{ + GString *str = NULL; + GHashTableIter iter; + gpointer key, value; + + g_hash_table_iter_init (&iter, self->priv->ht); + while (g_hash_table_iter_next (&iter, &key, &value)) { + const gchar *lock_name; + guint retries; + + lock_name = mm_modem_lock_get_string ((MMModemLock)GPOINTER_TO_UINT (key)); + retries = GPOINTER_TO_UINT (value); + + if (!str) { + str = g_string_new (""); + g_string_append_printf (str, "%s (%u)", lock_name, retries); + } else + g_string_append_printf (str, ", %s (%u)", lock_name, retries); + } + + return (str ? g_string_free (str, FALSE) : NULL); +} + +/*****************************************************************************/ + +MMUnlockRetries * +mm_unlock_retries_new (void) +{ + return (MM_UNLOCK_RETRIES ( + g_object_new (MM_TYPE_UNLOCK_RETRIES, NULL))); +} + +static void +mm_unlock_retries_init (MMUnlockRetries *self) +{ + self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self), + MM_TYPE_UNLOCK_RETRIES, + MMUnlockRetriesPrivate); + self->priv->ht = g_hash_table_new (g_direct_hash, + g_direct_equal); +} + +static void +finalize (GObject *object) +{ + MMUnlockRetries *self = MM_UNLOCK_RETRIES (object); + + g_hash_table_destroy (self->priv->ht); + + G_OBJECT_CLASS (mm_unlock_retries_parent_class)->finalize (object); +} + +static void +mm_unlock_retries_class_init (MMUnlockRetriesClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (MMUnlockRetriesPrivate)); + + object_class->finalize = finalize; +} diff --git a/libmm-common/mm-unlock-retries.h b/libmm-common/mm-unlock-retries.h new file mode 100644 index 00000000..48851b21 --- /dev/null +++ b/libmm-common/mm-unlock-retries.h @@ -0,0 +1,78 @@ +/* -*- 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) 2012 Google, Inc. + */ + +#ifndef MM_UNLOCK_RETRIES_H +#define MM_UNLOCK_RETRIES_H + +#include <ModemManager.h> +#include <glib-object.h> + +G_BEGIN_DECLS + +#define MM_TYPE_UNLOCK_RETRIES (mm_unlock_retries_get_type ()) +#define MM_UNLOCK_RETRIES(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MM_TYPE_UNLOCK_RETRIES, MMUnlockRetries)) +#define MM_UNLOCK_RETRIES_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MM_TYPE_UNLOCK_RETRIES, MMUnlockRetriesClass)) +#define MM_IS_UNLOCK_RETRIES(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MM_TYPE_UNLOCK_RETRIES)) +#define MM_IS_UNLOCK_RETRIES_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MM_TYPE_UNLOCK_RETRIES)) +#define MM_UNLOCK_RETRIES_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_UNLOCK_RETRIES, MMUnlockRetriesClass)) + +#define MM_UNLOCK_RETRIES_UNKNOWN 999 + +typedef struct _MMUnlockRetries MMUnlockRetries; +typedef struct _MMUnlockRetriesClass MMUnlockRetriesClass; +typedef struct _MMUnlockRetriesPrivate MMUnlockRetriesPrivate; + +struct _MMUnlockRetries { + GObject parent; + MMUnlockRetriesPrivate *priv; +}; + +struct _MMUnlockRetriesClass { + GObjectClass parent; +}; + +GType mm_unlock_retries_get_type (void); + +MMUnlockRetries *mm_unlock_retries_new (void); +MMUnlockRetries *mm_unlock_retries_new_from_dictionary (GVariant *dictionary); + +void mm_unlock_retries_set (MMUnlockRetries *self, + MMModemLock lock, + guint retries); + +void mm_unlock_retries_unset (MMUnlockRetries *self, + MMModemLock lock); + +guint mm_unlock_retries_get (MMUnlockRetries *self, + MMModemLock lock); + +typedef void (* MMUnlockRetriesForeachCb) (MMModemLock lock, + guint count, + gpointer user_data); + +void mm_unlock_retries_foreach (MMUnlockRetries *self, + MMUnlockRetriesForeachCb callback, + gpointer user_data); + +gboolean mm_unlock_retries_cmp (MMUnlockRetries *a, + MMUnlockRetries *b); + +GVariant *mm_unlock_retries_get_dictionary (MMUnlockRetries *self); + +gchar *mm_unlock_retries_build_string (MMUnlockRetries *self); + +G_END_DECLS + +#endif /* MM_UNLOCK_RETRIES_H */ |