summaryrefslogtreecommitdiff
path: root/libmm-glib/mm-pco.c
blob: ab3d088695aca948d6d29415bdbfd95c044d30e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * libmm-glib -- Access modem status & information from glib applications
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * Copyright 2018 Google LLC.
 */

#include <string.h>
#include <glib.h>

#include "mm-enums-types.h"
#include "mm-errors-types.h"
#include "mm-common-helpers.h"
#include "mm-pco.h"

/**
 * SECTION: mm-pco
 * @title: MMPco
 * @short_description: Helper object to handle 3GPP PCO.
 *
 * The #MMPco is an object handling the raw 3GPP Protocol Configuration Options
 * (PCO) that the modem has received from the network.
 *
 * This object is retrieved with mm_modem_3gpp_get_pco().
 */

G_DEFINE_TYPE (MMPco, mm_pco, G_TYPE_OBJECT)

struct _MMPcoPrivate {
    /* Session ID, signature 'u' */
    guint32 session_id;
    /* Flag indicating if the PCO data is complete or partial, signature 'b' */
    gboolean is_complete;
    /* Raw PCO data, signature 'ay' */
    GBytes *data;
};

/*****************************************************************************/

static GBytes *
_g_variant_get_bytes (GVariant *variant)
{
    GByteArray *byte_array;
    guint num_bytes;
    GVariantIter iter;
    guint8 byte;

    g_assert (g_variant_is_of_type (variant, G_VARIANT_TYPE ("ay")));

    num_bytes = g_variant_n_children (variant);
    if (num_bytes == 0)
        return NULL;

    byte_array = g_byte_array_sized_new (num_bytes);

    g_variant_iter_init (&iter, variant);
    while (g_variant_iter_loop (&iter, "y", &byte))
        g_byte_array_append (byte_array, &byte, sizeof (byte));

    return g_byte_array_free_to_bytes (byte_array);
}

/*****************************************************************************/

/**
 * mm_pco_get_session_id:
 * @self: a #MMPco.
 *
 * Gets the session ID associated with the PCO.
 *
 * Returns: the session ID.
 *
 * Since: 1.10
 */
guint32
mm_pco_get_session_id (MMPco *self)
{
    g_return_val_if_fail (MM_IS_PCO (self), G_MAXUINT32);

    return self->priv->session_id;
}

/**
 * mm_pco_set_session_id: (skip)
 */
void
mm_pco_set_session_id (MMPco *self,
                       guint32 session_id)
{
    g_return_if_fail (MM_IS_PCO (self));

    self->priv->session_id = session_id;
}

/*****************************************************************************/

/**
 * mm_pco_is_complete:
 * @self: a #MMPco.
 *
 * Gets the complete flag that indicates whether the PCO data contains the
 * complete PCO structure received from the network.
 *
 * Returns: %TRUE if the PCO data contains the complete PCO structure, %FALSE
 * otherwise.
 *
 * Since: 1.10
 */
gboolean
mm_pco_is_complete (MMPco *self)
{
    g_return_val_if_fail (MM_IS_PCO (self), FALSE);

    return self->priv->is_complete;
}

/**
 * mm_pco_set_complete: (skip)
 */
void
mm_pco_set_complete (MMPco *self,
                     gboolean is_complete)
{
    g_return_if_fail (MM_IS_PCO (self));

    self->priv->is_complete = is_complete;
}

/*****************************************************************************/

/**
 * mm_pco_get_data:
 * @self: a #MMPco.
 * @data_size: (out): Size of the PCO data, if any given.
 *
 * Gets the PCO data in raw bytes.
 *
 * Returns: (transfer none): the PCO data, or %NULL if it doesn't contain any.
 *
 * Since: 1.10
 */
const guint8 *
mm_pco_get_data (MMPco *self,
                 gsize *data_size)
{
    g_return_val_if_fail (MM_IS_PCO (self), NULL);

    return g_bytes_get_data (self->priv->data, data_size);
}

/**
 * mm_pco_set_data: (skip)
 */
void
mm_pco_set_data (MMPco *self,
                 const guint8 *data,
                 gsize data_size)
{
    g_return_if_fail (MM_IS_PCO (self));

    g_bytes_unref (self->priv->data);

    self->priv->data = (data && data_size) ? g_bytes_new (data, data_size)
                                           : NULL;
}

/*****************************************************************************/

/**
 * mm_pco_from_variant: (skip)
 */
MMPco *
mm_pco_from_variant (GVariant *variant,
                     GError **error)
{
    MMPco *pco;
    GVariant *pco_data = NULL;

    pco = mm_pco_new ();
    if (!variant)
        return pco;

    if (!g_variant_is_of_type (variant, G_VARIANT_TYPE ("(ubay)"))) {
        g_set_error (error,
                     MM_CORE_ERROR,
                     MM_CORE_ERROR_INVALID_ARGS,
                     "Cannot create PCO from variant: "
                     "invalid variant type received");
        g_object_unref (pco);
        return NULL;
    }

    g_variant_get (variant, "(ub@ay)",
                   &pco->priv->session_id,
                   &pco->priv->is_complete,
                   &pco_data);

    g_bytes_unref (pco->priv->data);
    pco->priv->data = _g_variant_get_bytes (pco_data);
    g_variant_unref (pco_data);

    return pco;
}

/*****************************************************************************/

/**
 * mm_pco_to_variant: (skip)
 */
GVariant *
mm_pco_to_variant (MMPco *self)
{
    GVariantBuilder builder;
    gsize i, pco_data_size;
    const guint8 *pco_data;

    /* Allow NULL */
    if (!self)
        return NULL;

    g_return_val_if_fail (MM_IS_PCO (self), NULL);

    g_variant_builder_init (&builder, G_VARIANT_TYPE ("(ubay)"));

    g_variant_builder_add (&builder, "u", self->priv->session_id);
    g_variant_builder_add (&builder, "b", self->priv->is_complete);

    g_variant_builder_open (&builder, G_VARIANT_TYPE ("ay"));
    if (self->priv->data) {
        pco_data = g_bytes_get_data (self->priv->data, &pco_data_size);
        for (i = 0; i < pco_data_size; ++i)
            g_variant_builder_add (&builder, "y", pco_data[i]);
    }
    g_variant_builder_close (&builder);

    return g_variant_ref_sink (g_variant_builder_end (&builder));
}

/*****************************************************************************/

/**
 * mm_pco_list_add: (skip)
 */
GList *
mm_pco_list_add (GList *pco_list,
                 MMPco *pco)
{
    GList *iter;
    guint32 session_id;

    g_return_val_if_fail (pco != NULL, pco_list);

    session_id = mm_pco_get_session_id (pco);

    for (iter = g_list_first (pco_list); iter; iter = g_list_next (iter)) {
        MMPco *iter_pco = iter->data;
        guint32 iter_session_id = mm_pco_get_session_id (iter_pco);

        if (iter_session_id < session_id)
            continue;
        else if (iter_session_id == session_id) {
            iter->data = g_object_ref (pco);
            g_object_unref (iter_pco);
            return pco_list;
        } else
            break;
    }

    return g_list_insert_before (pco_list, iter, g_object_ref (pco));
}

/*****************************************************************************/

/**
 * mm_pco_new: (skip)
 */
MMPco *
mm_pco_new (void)
{
    return (MM_PCO (g_object_new (MM_TYPE_PCO, NULL)));
}

static void
mm_pco_init (MMPco *self)
{
    self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, MM_TYPE_PCO, MMPcoPrivate);

    self->priv->session_id = G_MAXUINT32;
}

static void
finalize (GObject *object)
{
    MMPco *self = MM_PCO (object);

    g_bytes_unref (self->priv->data);

    G_OBJECT_CLASS (mm_pco_parent_class)->finalize (object);
}

static void
mm_pco_class_init (MMPcoClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    g_type_class_add_private (object_class, sizeof (MMPcoPrivate));

    object_class->finalize = finalize;
}