summaryrefslogtreecommitdiff
path: root/libmm-glib/mm-location-3gpp.c
blob: 583069673c0fe627eb7f7a8b9abc90a30a322822 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* -*- 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 (C) 2012 Google, Inc.
 */

#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#include "mm-errors-types.h"
#include "mm-common-helpers.h"
#include "mm-location-3gpp.h"

/**
 * SECTION: mm-location-3gpp
 * @title: MMLocation3gpp
 * @short_description: Helper object to handle 3GPP location information.
 *
 * The #MMLocation3gpp is an object handling the location information of the
 * modem when this is reported by the 3GPP network.
 *
 * This object is retrieved with either mm_modem_location_get_3gpp(),
 * mm_modem_location_get_3gpp_sync(), mm_modem_location_get_full() or
 * mm_modem_location_get_full_sync().
 */

G_DEFINE_TYPE (MMLocation3gpp, mm_location_3gpp, G_TYPE_OBJECT);

struct _MMLocation3gppPrivate {
    gchar *operator_code;
    gulong location_area_code;
    gulong cell_id;
    gulong tracking_area_code;
};

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

static gboolean
validate_string_length (const gchar *display,
                        const gchar *str,
                        guint min_length,
                        guint max_length,
                        GError **error)
{
    /* Avoid empty strings */
    if (!str || !str[0]) {
        g_set_error (error,
                     MM_CORE_ERROR,
                     MM_CORE_ERROR_INVALID_ARGS,
                     "Invalid %s: none given",
                     display);
        return FALSE;
    }

    /* Check min length of the field */
    if (strlen (str) < min_length) {
        g_set_error (error,
                     MM_CORE_ERROR,
                     MM_CORE_ERROR_INVALID_ARGS,
                     "Invalid %s: shorter than the maximum expected (%u): '%s'",
                     display,
                     min_length,
                     str);
        return FALSE;
    }

    /* Check max length of the field */
    if (strlen (str) > max_length) {
        g_set_error (error,
                     MM_CORE_ERROR,
                     MM_CORE_ERROR_INVALID_ARGS,
                     "Invalid %s: longer than the maximum expected (%u): '%s'",
                     display,
                     max_length,
                     str);
        return FALSE;
    }

    return TRUE;
}

static gboolean
validate_numeric_string_content (const gchar *display,
                                 const gchar *str,
                                 gboolean hex,
                                 GError **error)
{
    guint i;

    for (i = 0; str[i]; i++) {
        if ((hex && !g_ascii_isxdigit (str[i])) ||
            (!hex && !g_ascii_isdigit (str[i]))) {
            g_set_error (error,
                         MM_CORE_ERROR,
                         MM_CORE_ERROR_INVALID_ARGS,
                         "Invalid %s: unexpected char (%c): '%s'",
                         display,
                         str[i],
                         str);
            return FALSE;
        }
    }

    return TRUE;
}

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

/**
 * mm_location_3gpp_get_mobile_country_code:
 * @self: a #MMLocation3gpp.
 *
 * Gets the Mobile Country Code of the 3GPP network.
 *
 * Returns: the MCC, or 0 if unknown.
 *
 * Since: 1.0
 */
guint
mm_location_3gpp_get_mobile_country_code (MMLocation3gpp *self)
{
    gchar mcc[4];

    g_return_val_if_fail (MM_IS_LOCATION_3GPP (self), 0);

    if (!self->priv->operator_code)
        return 0;
    memcpy (mcc, self->priv->operator_code, 3);
    mcc[3] = '\0';
    return strtol (mcc, NULL, 10);
}

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

/**
 * mm_location_3gpp_get_location_area_code:
 * @self: a #MMLocation3gpp.
 *
 * Gets the location area code of the 3GPP network.
 *
 * Returns: the location area code, or 0 if unknown.
 *
 * Since: 1.0
 */
gulong
mm_location_3gpp_get_location_area_code (MMLocation3gpp *self)
{
    g_return_val_if_fail (MM_IS_LOCATION_3GPP (self), 0);

    return self->priv->location_area_code;
}

/**
 * mm_location_3gpp_set_location_area_code: (skip)
 */
gboolean
mm_location_3gpp_set_location_area_code (MMLocation3gpp *self,
                                         gulong location_area_code)
{
    g_return_val_if_fail (MM_IS_LOCATION_3GPP (self), FALSE);

    /* If no change in the location info, don't do anything */
    if (self->priv->location_area_code == location_area_code)
        return FALSE;

    self->priv->location_area_code = location_area_code;
    return TRUE;
}

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

/**
 * mm_location_3gpp_get_cell_id:
 * @self: a #MMLocation3gpp.
 *
 * Gets the cell ID of the 3GPP network.
 *
 * Returns: the cell ID, or 0 if unknown.
 *
 * Since: 1.0
 */
gulong
mm_location_3gpp_get_cell_id (MMLocation3gpp *self)
{
    g_return_val_if_fail (MM_IS_LOCATION_3GPP (self), 0);

    return self->priv->cell_id;
}

/**
 * mm_location_3gpp_set_cell_id: (skip)
 */
gboolean
mm_location_3gpp_set_cell_id (MMLocation3gpp *self,
                              gulong cell_id)
{
    g_return_val_if_fail (MM_IS_LOCATION_3GPP (self), FALSE);

    /* If no change in the location info, don't do anything */
    if (self->priv->cell_id == cell_id)
        return FALSE;

    self->priv->cell_id = cell_id;
    return TRUE;
}

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

/**
 * mm_location_3gpp_get_tracking_area_code:
 * @self: a #MMLocation3gpp.
 *
 * Gets the location area code of the 3GPP network.
 *
 * Returns: the location area code, or 0 if unknown.
 *
 * Since: 1.10
 */
gulong
mm_location_3gpp_get_tracking_area_code (MMLocation3gpp *self)
{
    g_return_val_if_fail (MM_IS_LOCATION_3GPP (self), 0);

    return self->priv->tracking_area_code;
}

/**
 * mm_location_3gpp_set_tracking_area_code: (skip)
 */
gboolean
mm_location_3gpp_set_tracking_area_code (MMLocation3gpp *self,
                                         gulong tracking_area_code)
{
    g_return_val_if_fail (MM_IS_LOCATION_3GPP (self), FALSE);

    /* If no change in the location info, don't do anything */
    if (self->priv->tracking_area_code == tracking_area_code)
        return FALSE;

    self->priv->tracking_area_code = tracking_area_code;
    return TRUE;
}

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

/**
 * mm_location_3gpp_get_operator_code:
 * @self: A #MMLocation3gpp.
 *
 * Gets the 3GPP network Mobile Country Code and Mobile Network Code.
 *
 * Returned in the format <literal>"MCCMNC"</literal>, where
 * <literal>MCC</literal> is the three-digit ITU E.212 Mobile Country Code
 * and <literal>MNC</literal> is the two- or three-digit GSM Mobile Network
 * Code. e.g. e<literal>"31026"</literal> or <literal>"310260"</literal>.
 *
 * Returns: (transfer none): The operator code, or %NULL if none available.
 *
 * Since: 1.18
 */
const gchar *
mm_location_3gpp_get_operator_code (MMLocation3gpp *self)
{
    g_return_val_if_fail (MM_IS_LOCATION_3GPP (self), NULL);

    return self->priv->operator_code;
}

/**
 * mm_location_3gpp_set_operator_code: (skip)
 */
gboolean
mm_location_3gpp_set_operator_code (MMLocation3gpp *self,
                                    const gchar *operator_code)
{
    g_return_val_if_fail (MM_IS_LOCATION_3GPP (self), FALSE);

    /* If no change in operator code, don't do anything */
    if (!g_strcmp0 (operator_code, self->priv->operator_code))
        return FALSE;

    /* Check the validity here, all other functions expect it's valid. */
    if (operator_code &&
        (!validate_string_length ("MCCMNC", operator_code, 5, 6, NULL) ||
         !validate_numeric_string_content ("MCCMNC", operator_code, FALSE, NULL)))
         return FALSE;

    g_free (self->priv->operator_code);
    self->priv->operator_code = g_strdup (operator_code);
    return TRUE;
}

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

/**
 * mm_location_3gpp_reset: (skip)
 */
gboolean
mm_location_3gpp_reset (MMLocation3gpp *self)
{
    g_return_val_if_fail (MM_IS_LOCATION_3GPP (self), FALSE);

    if (self->priv->operator_code == NULL &&
        self->priv->location_area_code == 0 &&
        self->priv->tracking_area_code == 0 &&
        self->priv->cell_id == 0)
        return FALSE;

    g_free (self->priv->operator_code);
    self->priv->operator_code = NULL;
    self->priv->location_area_code = 0;
    self->priv->tracking_area_code = 0;
    self->priv->cell_id = 0;
    return TRUE;
}

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

/**
 * mm_location_3gpp_get_string_variant: (skip)
 */
GVariant *
mm_location_3gpp_get_string_variant (MMLocation3gpp *self)
{
    GVariant *variant = NULL;

    g_return_val_if_fail (MM_IS_LOCATION_3GPP (self), NULL);

    if (self->priv->operator_code &&
        (self->priv->location_area_code || self->priv->tracking_area_code) &&
        self->priv->cell_id) {
        gchar *str;

        str = g_strdup_printf ("%.3s,%s,%lX,%lX,%lX",
                               self->priv->operator_code,
                               self->priv->operator_code + 3,
                               self->priv->location_area_code,
                               self->priv->cell_id,
                               self->priv->tracking_area_code);

        variant = g_variant_ref_sink (g_variant_new_string (str));
        g_free (str);
    }

    return variant;
}

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

/**
 * mm_location_3gpp_new_from_string_variant: (skip)
 */
MMLocation3gpp *
mm_location_3gpp_new_from_string_variant (GVariant *string,
                                          GError **error)
{
    MMLocation3gpp *self = NULL;
    gchar **split;

    if (!g_variant_is_of_type (string, G_VARIANT_TYPE_STRING)) {
        g_set_error (error,
                     MM_CORE_ERROR,
                     MM_CORE_ERROR_INVALID_ARGS,
                     "Cannot create 3GPP location from string: "
                     "invalid variant type received");
        return NULL;
    }

    split = g_strsplit (g_variant_get_string (string, NULL), ",", -1);
    if (!split) {
        g_set_error (error,
                     MM_CORE_ERROR,
                     MM_CORE_ERROR_INVALID_ARGS,
                     "Invalid 3GPP location string: '%s'",
                     g_variant_get_string (string, NULL));
        return NULL;
    }

    /* Validate fields */
    if (validate_string_length ("MCC", split[0], 0, 3, error) &&
        validate_numeric_string_content ("MCC", split[0], FALSE, error) &&
        validate_string_length ("MNC", split[1], 0, 3, error) &&
        validate_numeric_string_content ("MNC", split[1], FALSE, error) &&
        validate_string_length ("Location area code", split[2], 0, 4, error) &&
        validate_numeric_string_content ("Location area code", split[2], TRUE, error) &&
        validate_string_length ("Cell ID", split[3], 0, 8, error) &&
        validate_numeric_string_content ("Cell ID", split[3], TRUE, error) &&
        validate_string_length ("Tracking area code", split[4], 0, 8, error) &&
        validate_numeric_string_content ("Tracking area code", split[4], TRUE, error)) {
        /* Create new location object */
        self = mm_location_3gpp_new ();
        /* Join MCC and MNC and ensure they are zero-padded to required widths */
        self->priv->operator_code = g_strdup_printf ("%03lu%0*lu",
                                                     strtoul (split[0], NULL, 10),
                                                     strlen (split[1]) == 3 ? 3 : 2,
                                                     strtoul (split[1], NULL, 10));
        self->priv->location_area_code = strtoul (split[2], NULL, 16);
        self->priv->cell_id = strtoul (split[3], NULL, 16);
        self->priv->tracking_area_code = strtoul (split[4], NULL, 16);
    }

    g_strfreev (split);
    return self;
}

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

/**
 * mm_location_3gpp_new: (skip)
 */
MMLocation3gpp *
mm_location_3gpp_new (void)
{
    return (MM_LOCATION_3GPP (
                g_object_new (MM_TYPE_LOCATION_3GPP, NULL)));
}

static void
mm_location_3gpp_init (MMLocation3gpp *self)
{
    self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
                                              MM_TYPE_LOCATION_3GPP,
                                              MMLocation3gppPrivate);
}

static void
finalize (GObject *object)
{
    MMLocation3gpp *self = MM_LOCATION_3GPP (object);

    g_free (self->priv->operator_code);

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

static void
mm_location_3gpp_class_init (MMLocation3gppClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    g_type_class_add_private (object_class, sizeof (MMLocation3gppPrivate));

    object_class->finalize = finalize;
}