|
Packit Service |
8101fe |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
Packit Service |
8101fe |
/*
|
|
Packit Service |
8101fe |
* This program is free software; you can redistribute it and/or modify
|
|
Packit Service |
8101fe |
* it under the terms of the GNU General Public License as published by
|
|
Packit Service |
8101fe |
* the Free Software Foundation; either version 2 of the License, or
|
|
Packit Service |
8101fe |
* (at your option) any later version.
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
8101fe |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
8101fe |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
8101fe |
* GNU General Public License for more details:
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* Copyright 2018 Google LLC.
|
|
Packit Service |
8101fe |
*/
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
#include <string.h>
|
|
Packit Service |
8101fe |
#include <glib.h>
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
#include "mm-enums-types.h"
|
|
Packit Service |
8101fe |
#include "mm-errors-types.h"
|
|
Packit Service |
8101fe |
#include "mm-common-helpers.h"
|
|
Packit Service |
8101fe |
#include "mm-pco.h"
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/**
|
|
Packit Service |
8101fe |
* SECTION: mm-pco
|
|
Packit Service |
8101fe |
* @title: MMPco
|
|
Packit Service |
8101fe |
* @short_description: Helper object to handle 3GPP PCO.
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* The #MMPco is an object handling the raw 3GPP Protocol Configuration Options
|
|
Packit Service |
8101fe |
* (PCO) that the modem has received from the network.
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* This object is retrieved with mm_modem_3gpp_get_pco().
|
|
Packit Service |
8101fe |
*/
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
G_DEFINE_TYPE (MMPco, mm_pco, G_TYPE_OBJECT);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
struct _MMPcoPrivate {
|
|
Packit Service |
8101fe |
/* Session ID, signature 'u' */
|
|
Packit Service |
8101fe |
guint32 session_id;
|
|
Packit Service |
8101fe |
/* Flag indicating if the PCO data is complete or partial, signature 'b' */
|
|
Packit Service |
8101fe |
gboolean is_complete;
|
|
Packit Service |
8101fe |
/* Raw PCO data, signature 'ay' */
|
|
Packit Service |
8101fe |
GBytes *data;
|
|
Packit Service |
8101fe |
};
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/*****************************************************************************/
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
static GBytes *
|
|
Packit Service |
8101fe |
_g_variant_get_bytes (GVariant *variant)
|
|
Packit Service |
8101fe |
{
|
|
Packit Service |
8101fe |
GByteArray *byte_array;
|
|
Packit Service |
8101fe |
guint num_bytes;
|
|
Packit Service |
8101fe |
GVariantIter iter;
|
|
Packit Service |
8101fe |
guint8 byte;
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
g_assert (g_variant_is_of_type (variant, G_VARIANT_TYPE ("ay")));
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
num_bytes = g_variant_n_children (variant);
|
|
Packit Service |
8101fe |
if (num_bytes == 0)
|
|
Packit Service |
8101fe |
return NULL;
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
byte_array = g_byte_array_sized_new (num_bytes);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
g_variant_iter_init (&iter, variant);
|
|
Packit Service |
8101fe |
while (g_variant_iter_loop (&iter, "y", &byte))
|
|
Packit Service |
8101fe |
g_byte_array_append (byte_array, &byte, sizeof (byte));
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
return g_byte_array_free_to_bytes (byte_array);
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/*****************************************************************************/
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/**
|
|
Packit Service |
8101fe |
* mm_pco_get_session_id:
|
|
Packit Service |
8101fe |
* @self: a #MMPco.
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* Gets the session ID associated with the PCO.
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* Returns: the session ID.
|
|
Packit Service |
8101fe |
*/
|
|
Packit Service |
8101fe |
guint32
|
|
Packit Service |
8101fe |
mm_pco_get_session_id (MMPco *self)
|
|
Packit Service |
8101fe |
{
|
|
Packit Service |
8101fe |
g_return_val_if_fail (MM_IS_PCO (self), G_MAXUINT32);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
return self->priv->session_id;
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
void
|
|
Packit Service |
8101fe |
mm_pco_set_session_id (MMPco *self,
|
|
Packit Service |
8101fe |
guint32 session_id)
|
|
Packit Service |
8101fe |
{
|
|
Packit Service |
8101fe |
g_return_if_fail (MM_IS_PCO (self));
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
self->priv->session_id = session_id;
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/*****************************************************************************/
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/**
|
|
Packit Service |
8101fe |
* mm_pco_is_complete:
|
|
Packit Service |
8101fe |
* @self: a #MMPco.
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* Gets the complete flag that indicates whether the PCO data contains the
|
|
Packit Service |
8101fe |
* complete PCO structure received from the network.
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* Returns: %TRUE if the PCO data contains the complete PCO structure, %FALSE otherwise.
|
|
Packit Service |
8101fe |
*/
|
|
Packit Service |
8101fe |
gboolean
|
|
Packit Service |
8101fe |
mm_pco_is_complete (MMPco *self)
|
|
Packit Service |
8101fe |
{
|
|
Packit Service |
8101fe |
g_return_val_if_fail (MM_IS_PCO (self), FALSE);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
return self->priv->is_complete;
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
void
|
|
Packit Service |
8101fe |
mm_pco_set_complete (MMPco *self,
|
|
Packit Service |
8101fe |
gboolean is_complete)
|
|
Packit Service |
8101fe |
{
|
|
Packit Service |
8101fe |
g_return_if_fail (MM_IS_PCO (self));
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
self->priv->is_complete = is_complete;
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/*****************************************************************************/
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/**
|
|
Packit Service |
8101fe |
* mm_pco_get_data:
|
|
Packit Service |
8101fe |
* @self: a #MMPco.
|
|
Packit Service |
8101fe |
* @data_size: (out): Size of the PCO data, if any given.
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* Gets the PCO data in raw bytes.
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* Returns: (transfer none): the PCO data, or %NULL if it doesn't contain any.
|
|
Packit Service |
8101fe |
*/
|
|
Packit Service |
8101fe |
const guint8 *
|
|
Packit Service |
8101fe |
mm_pco_get_data (MMPco *self,
|
|
Packit Service |
8101fe |
gsize *data_size)
|
|
Packit Service |
8101fe |
{
|
|
Packit Service |
8101fe |
g_return_val_if_fail (MM_IS_PCO (self), NULL);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
return g_bytes_get_data (self->priv->data, data_size);
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
void
|
|
Packit Service |
8101fe |
mm_pco_set_data (MMPco *self,
|
|
Packit Service |
8101fe |
const guint8 *data,
|
|
Packit Service |
8101fe |
gsize data_size)
|
|
Packit Service |
8101fe |
{
|
|
Packit Service |
8101fe |
g_return_if_fail (MM_IS_PCO (self));
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
g_bytes_unref (self->priv->data);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
self->priv->data = (data && data_size) ? g_bytes_new (data, data_size)
|
|
Packit Service |
8101fe |
: NULL;
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/*****************************************************************************/
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/**
|
|
Packit Service |
8101fe |
* mm_pco_from_variant:
|
|
Packit Service |
8101fe |
* @variant: A variant with the PCO information.
|
|
Packit Service |
8101fe |
* @error: Return location for error or %NULL.
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* Creates a new #MMPco object with the values exposed in
|
|
Packit Service |
8101fe |
* the variant.
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* Returns: (transfer full): A #MMPco or %NULL if @error is set. The returned value should be freed with g_object_unref().
|
|
Packit Service |
8101fe |
*/
|
|
Packit Service |
8101fe |
MMPco *
|
|
Packit Service |
8101fe |
mm_pco_from_variant (GVariant *variant,
|
|
Packit Service |
8101fe |
GError **error)
|
|
Packit Service |
8101fe |
{
|
|
Packit Service |
8101fe |
MMPco *pco;
|
|
Packit Service |
8101fe |
GVariant *pco_data = NULL;
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
pco = mm_pco_new ();
|
|
Packit Service |
8101fe |
if (!variant)
|
|
Packit Service |
8101fe |
return pco;
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
if (!g_variant_is_of_type (variant, G_VARIANT_TYPE ("(ubay)"))) {
|
|
Packit Service |
8101fe |
g_set_error (error,
|
|
Packit Service |
8101fe |
MM_CORE_ERROR,
|
|
Packit Service |
8101fe |
MM_CORE_ERROR_INVALID_ARGS,
|
|
Packit Service |
8101fe |
"Cannot create PCO from variant: "
|
|
Packit Service |
8101fe |
"invalid variant type received");
|
|
Packit Service |
8101fe |
g_object_unref (pco);
|
|
Packit Service |
8101fe |
return NULL;
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
g_variant_get (variant, "(ub@ay)",
|
|
Packit Service |
8101fe |
&pco->priv->session_id,
|
|
Packit Service |
8101fe |
&pco->priv->is_complete,
|
|
Packit Service |
8101fe |
&pco_data);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
g_bytes_unref (pco->priv->data);
|
|
Packit Service |
8101fe |
pco->priv->data = _g_variant_get_bytes (pco_data);
|
|
Packit Service |
8101fe |
g_variant_unref (pco_data);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
return pco;
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/*****************************************************************************/
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/**
|
|
Packit Service |
8101fe |
* mm_pco_to_variant:
|
|
Packit Service |
8101fe |
* @self: a #MMPco.
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* Gets a GVariant representation with signature "(ubay)" of @self.
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* Returns: (transfer full): A #GVariant representation of the #MMPco object. The returned value should be freed with g_variant_unref().
|
|
Packit Service |
8101fe |
*/
|
|
Packit Service |
8101fe |
GVariant *
|
|
Packit Service |
8101fe |
mm_pco_to_variant (MMPco *self)
|
|
Packit Service |
8101fe |
{
|
|
Packit Service |
8101fe |
GVariantBuilder builder;
|
|
Packit Service |
8101fe |
gsize i, pco_data_size;
|
|
Packit Service |
8101fe |
const guint8 *pco_data;
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/* Allow NULL */
|
|
Packit Service |
8101fe |
if (!self)
|
|
Packit Service |
8101fe |
return NULL;
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
g_return_val_if_fail (MM_IS_PCO (self), NULL);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("(ubay)"));
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
g_variant_builder_add (&builder, "u", self->priv->session_id);
|
|
Packit Service |
8101fe |
g_variant_builder_add (&builder, "b", self->priv->is_complete);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
g_variant_builder_open (&builder, G_VARIANT_TYPE ("ay"));
|
|
Packit Service |
8101fe |
if (self->priv->data) {
|
|
Packit Service |
8101fe |
pco_data = g_bytes_get_data (self->priv->data, &pco_data_size);
|
|
Packit Service |
8101fe |
for (i = 0; i < pco_data_size; ++i)
|
|
Packit Service |
8101fe |
g_variant_builder_add (&builder, "y", pco_data[i]);
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
g_variant_builder_close (&builder);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
return g_variant_ref_sink (g_variant_builder_end (&builder));
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/*****************************************************************************/
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/**
|
|
Packit Service |
8101fe |
* mm_pco_list_free:
|
|
Packit Service |
8101fe |
* @pco_list: (transfer full)(element-type ModemManager.Pco): a #GList of #MMPco.
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* Frees all of the memory used by a #GList of #MMPco.
|
|
Packit Service |
8101fe |
*/
|
|
Packit Service |
8101fe |
void
|
|
Packit Service |
8101fe |
mm_pco_list_free (GList *pco_list)
|
|
Packit Service |
8101fe |
{
|
|
Packit Service |
8101fe |
g_list_free_full (pco_list, g_object_unref);
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/**
|
|
Packit Service |
8101fe |
* mm_pco_list_add:
|
|
Packit Service |
8101fe |
* @pco_list: (transfer full)(element-type ModemManager.Pco): a #GList of #MMPco.
|
|
Packit Service |
8101fe |
* @pco: (transfer none): a #MMPco to add to the given list.
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* Adds a #MMPco to a given PCO list. #MMPco objects stored in the order of
|
|
Packit Service |
8101fe |
* their session ID. An existing PCO with the same session ID is overwritten
|
|
Packit Service |
8101fe |
* with the new value.
|
|
Packit Service |
8101fe |
*
|
|
Packit Service |
8101fe |
* Returns: (transfer full)(element-type ModemManager.Pco): the new start of an updated or newly allocated #GList of #MMPco.
|
|
Packit Service |
8101fe |
*/
|
|
Packit Service |
8101fe |
GList *
|
|
Packit Service |
8101fe |
mm_pco_list_add (GList *pco_list,
|
|
Packit Service |
8101fe |
MMPco *pco)
|
|
Packit Service |
8101fe |
{
|
|
Packit Service |
8101fe |
GList *iter;
|
|
Packit Service |
8101fe |
guint32 session_id;
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
g_return_val_if_fail (pco != NULL, pco_list);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
session_id = mm_pco_get_session_id (pco);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
for (iter = g_list_first (pco_list); iter; iter = g_list_next (iter)) {
|
|
Packit Service |
8101fe |
MMPco *iter_pco = iter->data;
|
|
Packit Service |
8101fe |
guint32 iter_session_id = mm_pco_get_session_id (iter_pco);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
if (iter_session_id < session_id)
|
|
Packit Service |
8101fe |
continue;
|
|
Packit Service |
8101fe |
else if (iter_session_id == session_id) {
|
|
Packit Service |
8101fe |
iter->data = g_object_ref (pco);
|
|
Packit Service |
8101fe |
g_object_unref (iter_pco);
|
|
Packit Service |
8101fe |
return pco_list;
|
|
Packit Service |
8101fe |
} else
|
|
Packit Service |
8101fe |
break;
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
return g_list_insert_before (pco_list, iter, g_object_ref (pco));
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
/*****************************************************************************/
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
MMPco *
|
|
Packit Service |
8101fe |
mm_pco_new (void)
|
|
Packit Service |
8101fe |
{
|
|
Packit Service |
8101fe |
return (MM_PCO (g_object_new (MM_TYPE_PCO, NULL)));
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
static void
|
|
Packit Service |
8101fe |
mm_pco_init (MMPco *self)
|
|
Packit Service |
8101fe |
{
|
|
Packit Service |
8101fe |
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, MM_TYPE_PCO, MMPcoPrivate);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
self->priv->session_id = G_MAXUINT32;
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
static void
|
|
Packit Service |
8101fe |
finalize (GObject *object)
|
|
Packit Service |
8101fe |
{
|
|
Packit Service |
8101fe |
MMPco *self = MM_PCO (object);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
g_bytes_unref (self->priv->data);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
G_OBJECT_CLASS (mm_pco_parent_class)->finalize (object);
|
|
Packit Service |
8101fe |
}
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
static void
|
|
Packit Service |
8101fe |
mm_pco_class_init (MMPcoClass *klass)
|
|
Packit Service |
8101fe |
{
|
|
Packit Service |
8101fe |
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
g_type_class_add_private (object_class, sizeof (MMPcoPrivate));
|
|
Packit Service |
8101fe |
|
|
Packit Service |
8101fe |
object_class->finalize = finalize;
|
|
Packit Service |
8101fe |
}
|