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-firmware-properties.c | 300 | ||||
-rw-r--r-- | libmm-common/mm-firmware-properties.h | 68 |
4 files changed, 373 insertions, 0 deletions
diff --git a/libmm-common/Makefile.am b/libmm-common/Makefile.am index 3e0f21f3..2285137e 100644 --- a/libmm-common/Makefile.am +++ b/libmm-common/Makefile.am @@ -169,6 +169,7 @@ mm-location-gps-raw.c: mm-errors-types.h mm-location-gps-nmea.c: mm-errors-types.h mm-unlock-retries.c: mm-enums-types.h mm-network-timezone.c: mm-errors-types.h +mm-firmware-properties.c: mm-errors-types.h includedir = @includedir@/libmm-common include_HEADERS = \ @@ -185,6 +186,7 @@ include_HEADERS = \ mm-location-gps-raw.h \ mm-unlock-retries.h \ mm-network-timezone.h \ + mm-firmware-properties.h \ mm-gdbus-manager.h \ mm-gdbus-modem.h \ mm-gdbus-bearer.h \ @@ -219,6 +221,8 @@ libmm_common_la_SOURCES = \ mm-unlock-retries.c \ mm-network-timezone.h \ mm-network-timezone.c \ + mm-firmware-properties.h \ + mm-firmware-properties.c \ libmm-common.h libmm_common_la_CPPFLAGS = \ diff --git a/libmm-common/libmm-common.h b/libmm-common/libmm-common.h index 3f3a8f29..0404c892 100644 --- a/libmm-common/libmm-common.h +++ b/libmm-common/libmm-common.h @@ -36,6 +36,7 @@ #include "mm-location-gps-nmea.h" #include "mm-unlock-retries.h" #include "mm-network-timezone.h" +#include "mm-firmware-properties.h" #include "mm-gdbus-manager.h" #include "mm-gdbus-modem.h" #include "mm-gdbus-bearer.h" diff --git a/libmm-common/mm-firmware-properties.c b/libmm-common/mm-firmware-properties.c new file mode 100644 index 00000000..8ef8347b --- /dev/null +++ b/libmm-common/mm-firmware-properties.c @@ -0,0 +1,300 @@ +/* -*- 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 "mm-errors-types.h" +#include "mm-common-helpers.h" +#include "mm-firmware-properties.h" + +G_DEFINE_TYPE (MMFirmwareProperties, mm_firmware_properties, G_TYPE_OBJECT); + +#define PROPERTY_NAME "name" +#define PROPERTY_VERSION "version" +#define PROPERTY_IMAGE_TYPE "image-type" + +struct _MMFirmwarePropertiesPrivate { + /* Mandatory parameters */ + MMFirmwareImageType image_type; + gchar *name; + gchar *version; +}; + +static MMFirmwareProperties *firmware_properties_new_empty (void); + +/*****************************************************************************/ + +/** + * mm_firmware_properties_get_name: + * @self: A #MMFirmwareProperties. + * + * Gets the unique name of the firmare image. + * + * Returns: (transfer none): The name of the image. Do not free the returned value, it is owned by @self. + */ +const gchar * +mm_firmware_properties_get_name (MMFirmwareProperties *self) +{ + g_return_val_if_fail (MM_IS_FIRMWARE_PROPERTIES (self), NULL); + + return self->priv->name; +} + +/** + * mm_firmware_properties_get_version: + * @self: A #MMFirmwareProperties. + * + * Gets the version string of the firmare image. + * + * Returns: (transfer none): The version of the image. Do not free the returned value, it is owned by @self. + */ +const gchar * +mm_firmware_properties_get_version (MMFirmwareProperties *self) +{ + g_return_val_if_fail (MM_IS_FIRMWARE_PROPERTIES (self), NULL); + + return self->priv->version; +} + +/** + * mm_firmware_properties_get_image_type: + * @self: A #MMFirmwareProperties. + * + * Gets the type of the firmare image. + * + * Returns: A #MMFirmwareImageType specifying The type of the image. + */ +MMFirmwareImageType +mm_firmware_properties_get_image_type (MMFirmwareProperties *self) +{ + g_return_val_if_fail (MM_IS_FIRMWARE_PROPERTIES (self), MM_FIRMWARE_IMAGE_TYPE_UNKNOWN); + + return self->priv->image_type; +} + +/*****************************************************************************/ + +/** + * mm_firmware_properties_get_dictionary: + * @self: A #MMFirmwareProperties. + * + * Gets a variant dictionary with the contents of @self. + * + * Returns: (transfer full): A dictionary with the image properties. The returned value should be freed with g_variant_unref(). + */ +GVariant * +mm_firmware_properties_get_dictionary (MMFirmwareProperties *self) +{ + GVariantBuilder builder; + + g_return_val_if_fail (MM_IS_FIRMWARE_PROPERTIES (self), NULL); + + /* We do allow NULL */ + if (!self) + return NULL; + + g_return_val_if_fail (MM_IS_FIRMWARE_PROPERTIES (self), NULL); + + g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}")); + + g_variant_builder_add (&builder, + "{sv}", + PROPERTY_NAME, + g_variant_new_string (self->priv->name)); + + g_variant_builder_add (&builder, + "{sv}", + PROPERTY_VERSION, + g_variant_new_string (self->priv->version)); + + g_variant_builder_add (&builder, + "{sv}", + PROPERTY_IMAGE_TYPE, + g_variant_new_uint32 (self->priv->image_type)); + + return g_variant_ref_sink (g_variant_builder_end (&builder)); +} + +/*****************************************************************************/ + +static gboolean +consume_variant (MMFirmwareProperties *self, + const gchar *key, + GVariant *value, + GError **error) +{ + if (g_str_equal (key, PROPERTY_NAME)) { + g_free (self->priv->name); + self->priv->name = g_variant_dup_string (value, NULL); + } else if (g_str_equal (key, PROPERTY_VERSION)) { + g_free (self->priv->version); + self->priv->version = g_variant_dup_string (value, NULL); + } else if (g_str_equal (key, PROPERTY_IMAGE_TYPE)) + self->priv->image_type = g_variant_get_uint32 (value); + else { + /* Set error */ + g_set_error (error, + MM_CORE_ERROR, + MM_CORE_ERROR_INVALID_ARGS, + "Invalid properties dictionary, unexpected key '%s'", + key); + return FALSE; + } + + return TRUE; +} + +/** + * mm_firmware_properties_new_from_dictionary: + * @dictionary: A variant dictionary with the properties of the image. + * @error: Return location for error or %NULL. + * + * Creates a new #MMFirmwareProperties object with the properties exposed in + * the dictionary. + * + * Returns: (transfer full): A #MMFirmwareProperties or %NULL if @error is set. The returned value should be freed with g_object_unref(). + */ +MMFirmwareProperties * +mm_firmware_properties_new_from_dictionary (GVariant *dictionary, + GError **error) +{ + GError *inner_error = NULL; + GVariantIter iter; + gchar *key; + GVariant *value; + MMFirmwareProperties *self; + + if (!dictionary) { + g_set_error (error, + MM_CORE_ERROR, + MM_CORE_ERROR_INVALID_ARGS, + "Cannot create Firmware properties from empty dictionary"); + return NULL; + } + + if (!g_variant_is_of_type (dictionary, G_VARIANT_TYPE ("a{sv}"))) { + g_set_error (error, + MM_CORE_ERROR, + MM_CORE_ERROR_INVALID_ARGS, + "Cannot create Firmware properties from dictionary: " + "invalid variant type received"); + return NULL; + } + + self = firmware_properties_new_empty (); + + g_variant_iter_init (&iter, dictionary); + while (!inner_error && + g_variant_iter_next (&iter, "{sv}", &key, &value)) { + consume_variant (self, + key, + value, + &inner_error); + g_free (key); + g_variant_unref (value); + } + + /* If error, destroy the object */ + if (inner_error) { + g_propagate_error (error, inner_error); + g_object_unref (self); + return NULL; + } + + /* If mandatory properties missing, destroy the object */ + if (!self->priv->name || + !self->priv->version || + self->priv->image_type == MM_FIRMWARE_IMAGE_TYPE_UNKNOWN) { + g_set_error (error, + MM_CORE_ERROR, + MM_CORE_ERROR_INVALID_ARGS, + "Cannot create Firmware properties from dictionary: " + "mandatory parameter missing"); + g_object_unref (self); + return NULL; + } + + return self; +} + +/*****************************************************************************/ + +/** + * mm_firmware_properties_new: + * @image_type: A #MMFirmwareImageType specifying the type of the image. + * @name: The unique name of the image. + * @version: The version of the image. + * + * Creates a new #MMFirmwareProperties object with the properties specified. + * + * Returns: (transfer full): A #MMFirmwareProperties or %NULL if @error is set. The returned value should be freed with g_object_unref(). + */ +MMFirmwareProperties * +mm_firmware_properties_new (MMFirmwareImageType image_type, + const gchar *name, + const gchar *version) +{ + MMFirmwareProperties *self; + + g_return_val_if_fail (image_type != MM_FIRMWARE_IMAGE_TYPE_UNKNOWN, NULL); + g_return_val_if_fail (name != NULL, NULL); + g_return_val_if_fail (version != NULL, NULL); + + self = firmware_properties_new_empty (); + self->priv->image_type = image_type; + self->priv->name = g_strdup (name); + self->priv->version = g_strdup (version); + + return self; +} + +static MMFirmwareProperties * +firmware_properties_new_empty (void) +{ + return (MM_FIRMWARE_PROPERTIES ( + g_object_new (MM_TYPE_FIRMWARE_PROPERTIES, NULL))); +} + +static void +mm_firmware_properties_init (MMFirmwareProperties *self) +{ + self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, + MM_TYPE_FIRMWARE_PROPERTIES, + MMFirmwarePropertiesPrivate); + + /* Some defaults */ + self->priv->image_type = MM_FIRMWARE_IMAGE_TYPE_UNKNOWN; +} + +static void +finalize (GObject *object) +{ + MMFirmwareProperties *self = MM_FIRMWARE_PROPERTIES (object); + + g_free (self->priv->name); + g_free (self->priv->version); + + G_OBJECT_CLASS (mm_firmware_properties_parent_class)->finalize (object); +} + +static void +mm_firmware_properties_class_init (MMFirmwarePropertiesClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (MMFirmwarePropertiesPrivate)); + + object_class->finalize = finalize; +} diff --git a/libmm-common/mm-firmware-properties.h b/libmm-common/mm-firmware-properties.h new file mode 100644 index 00000000..83973443 --- /dev/null +++ b/libmm-common/mm-firmware-properties.h @@ -0,0 +1,68 @@ +/* -*- 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_FIRMWARE_PROPERTIES_H +#define MM_FIRMWARE_PROPERTIES_H + +#include <ModemManager.h> +#include <glib-object.h> + +G_BEGIN_DECLS + +#define MM_TYPE_FIRMWARE_PROPERTIES (mm_firmware_properties_get_type ()) +#define MM_FIRMWARE_PROPERTIES(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MM_TYPE_FIRMWARE_PROPERTIES, MMFirmwareProperties)) +#define MM_FIRMWARE_PROPERTIES_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MM_TYPE_FIRMWARE_PROPERTIES, MMFirmwarePropertiesClass)) +#define MM_IS_FIRMWARE_PROPERTIES(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MM_TYPE_FIRMWARE_PROPERTIES)) +#define MM_IS_FIRMWARE_PROPERTIES_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MM_TYPE_FIRMWARE_PROPERTIES)) +#define MM_FIRMWARE_PROPERTIES_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_FIRMWARE_PROPERTIES, MMFirmwarePropertiesClass)) + +typedef struct _MMFirmwareProperties MMFirmwareProperties; +typedef struct _MMFirmwarePropertiesClass MMFirmwarePropertiesClass; +typedef struct _MMFirmwarePropertiesPrivate MMFirmwarePropertiesPrivate; + +/** + * MMFirmwareProperties: + * + * The #MMFirmwareProperties structure contains private data and should only be accessed + * using the provided API. + */ +struct _MMFirmwareProperties { + /*< private >*/ + GObject parent; + MMFirmwarePropertiesPrivate *priv; +}; + +struct _MMFirmwarePropertiesClass { + /*< private >*/ + GObjectClass parent; +}; + +GType mm_firmware_properties_get_type (void); + +MMFirmwareProperties *mm_firmware_properties_new (MMFirmwareImageType image_type, + const gchar *name, + const gchar *version); +MMFirmwareProperties *mm_firmware_properties_new_from_dictionary (GVariant *dictionary, + GError **error); + +const gchar *mm_firmware_properties_get_name (MMFirmwareProperties *properties); +const gchar *mm_firmware_properties_get_version (MMFirmwareProperties *properties); +MMFirmwareImageType mm_firmware_properties_get_image_type (MMFirmwareProperties *properties); + +GVariant *mm_firmware_properties_get_dictionary (MMFirmwareProperties *self); + +G_END_DECLS + +#endif /* MM_FIRMWARE_PROPERTIES_H */ |