summaryrefslogtreecommitdiff
path: root/libmm-glib/mm-location-gps-nmea.c
blob: 396e24fc10fa8ba9ff5f328bd07b068c027aa40f (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
/* -*- 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 Lanedo GmbH <aleksander@lanedo.com>
 */

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

#include "mm-common-helpers.h"
#include "mm-errors-types.h"
#include "mm-location-gps-nmea.h"

/**
 * SECTION: mm-location-gps-nmea
 * @title: MMLocationGpsNmea
 * @short_description: Helper object to handle NMEA-based GPS location information.
 *
 * The #MMLocationGpsNmea is an object handling the location information of the
 * modem when this is reported by GPS.
 *
 * This object is retrieved with either mm_modem_location_get_gps_nmea(),
 * mm_modem_location_get_gps_nmea_sync(), mm_modem_location_get_full() or
 * mm_modem_location_get_full_sync().
 */

G_DEFINE_TYPE (MMLocationGpsNmea, mm_location_gps_nmea, G_TYPE_OBJECT)

struct _MMLocationGpsNmeaPrivate {
    GHashTable *traces;
    GRegex *sequence_regex;
};

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

static gboolean
check_append_or_replace (MMLocationGpsNmea *self,
                         const gchar *trace)
{
    /* By default, replace */
    gboolean append_or_replace = FALSE;
    GMatchInfo *match_info = NULL;

    if (G_UNLIKELY (!self->priv->sequence_regex))
        self->priv->sequence_regex = g_regex_new ("\\$..(?:ALM|GSV|RTE|SFI),(\\d),(\\d).*",
                                                  G_REGEX_RAW | G_REGEX_OPTIMIZE,
                                                  0,
                                                  NULL);

    if (g_regex_match (self->priv->sequence_regex, trace, 0, &match_info)) {
        guint index;

        /* If we don't have the first element of a sequence, append */
        if (mm_get_uint_from_match_info (match_info, 2, &index) && index != 1)
            append_or_replace = TRUE;
    }
    g_match_info_free (match_info);

    return append_or_replace;
}

static gboolean
location_gps_nmea_take_trace (MMLocationGpsNmea *self,
                              gchar *trace)
{
    gchar *i;
    gchar *trace_type;

    i = strchr (trace, ',');
    if (!i || i == trace) {
        g_free (trace);
        return FALSE;
    }

    trace_type = g_malloc (i - trace + 1);
    memcpy (trace_type, trace, i - trace);
    trace_type[i - trace] = '\0';

    /* Some traces are part of a SEQUENCE; so we need to decide whether we
     * completely replace the previous trace, or we append the new one to
     * the already existing list */
    if (check_append_or_replace (self, trace)) {
        /* Append */
        const gchar *previous;

        previous = g_hash_table_lookup (self->priv->traces, trace_type);
        if (previous) {
            gchar *sequence;

            /* Skip the trace if we already have it there */
            if (strstr (previous, trace)) {
                g_free (trace_type);
                g_free (trace);
                return TRUE;
            }

            sequence = g_strdup_printf ("%s%s%s",
                                        previous,
                                        g_str_has_suffix (previous, "\r\n") ? "" : "\r\n",
                                        trace);
            g_free (trace);
            trace = sequence;
        }
    }

    g_hash_table_replace (self->priv->traces,
                          trace_type,
                          trace);
    return TRUE;
}

/**
 * mm_location_gps_nmea_add_trace: (skip)
 */
gboolean
mm_location_gps_nmea_add_trace (MMLocationGpsNmea *self,
                                const gchar *trace)
{
    return location_gps_nmea_take_trace (self, g_strdup (trace));
}

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

/**
 * mm_location_gps_nmea_get_trace:
 * @self: a #MMLocationGpsNmea.
 * @trace_type: specific NMEA trace type to gather.
 *
 * Gets the last cached value of the specific @trace_type given.
 *
 * Returns: the NMEA trace, or %NULL if not available. Do not free the returned
 * value, it is owned by @self.
 *
 * Since: 1.0
 */
const gchar *
mm_location_gps_nmea_get_trace (MMLocationGpsNmea *self,
                                const gchar *trace_type)
{
    return (const gchar *)g_hash_table_lookup (self->priv->traces, trace_type);
}

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

static void
build_all_foreach (const gchar  *trace_type,
                   const gchar  *trace,
                   GPtrArray   **built)
{
    if (*built == NULL)
        *built = g_ptr_array_new ();
    g_ptr_array_add (*built, g_strdup (trace));
}

/**
 * mm_location_gps_nmea_get_traces:
 * @self: a #MMLocationGpsNmea.
 *
 * Gets all cached traces.
 *
 * Returns: (transfer full): The list of traces, or %NULL if none available. The returned value should be freed with g_strfreev().
 * Since: 1.14
 */
gchar **
mm_location_gps_nmea_get_traces (MMLocationGpsNmea *self)
{
    GPtrArray *built = NULL;

    g_return_val_if_fail (MM_IS_LOCATION_GPS_NMEA (self), NULL);

    g_hash_table_foreach (self->priv->traces,
                          (GHFunc)build_all_foreach,
                          &built);
    if (!built)
        return NULL;

    g_ptr_array_add (built, NULL);
    return (gchar **) g_ptr_array_free (built, FALSE);
}

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

/**
 * mm_location_gps_nmea_get_string_variant: (skip)
 */
GVariant *
mm_location_gps_nmea_get_string_variant (MMLocationGpsNmea *self)
{
    g_autofree gchar *built = NULL;
    g_auto (GStrv)    traces = NULL;

    g_return_val_if_fail (MM_IS_LOCATION_GPS_NMEA (self), NULL);

    traces = mm_location_gps_nmea_get_traces (self);
    if (traces)
        built = g_strjoinv ("\r\n", traces);
    return g_variant_ref_sink (g_variant_new_string (built ? built : ""));
}

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

/**
 * mm_location_gps_nmea_new_from_string_variant: (skip)
 */
MMLocationGpsNmea *
mm_location_gps_nmea_new_from_string_variant (GVariant *string,
                                              GError **error)
{
    MMLocationGpsNmea *self = NULL;
    gchar **split;
    guint i;

    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 GPS NMEA location from string: "
                     "invalid variant type received");
        return NULL;
    }

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

    /* Create new location object */
    self = mm_location_gps_nmea_new ();

    for (i = 0; split[i]; i++) {
        location_gps_nmea_take_trace (self, split[i]);
    }

    /* Note that the strings in the array of strings were already taken
     * or freed */
    g_free (split);

    return self;
}

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

/**
 * mm_location_gps_nmea_new: (skip)
 */
MMLocationGpsNmea *
mm_location_gps_nmea_new (void)
{
    return (MM_LOCATION_GPS_NMEA (
                g_object_new (MM_TYPE_LOCATION_GPS_NMEA, NULL)));
}

static void
mm_location_gps_nmea_init (MMLocationGpsNmea *self)
{
    self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
                                              MM_TYPE_LOCATION_GPS_NMEA,
                                              MMLocationGpsNmeaPrivate);

    self->priv->traces = g_hash_table_new_full (g_str_hash,
                                                g_str_equal,
                                                g_free,
                                                g_free);
}

static void
finalize (GObject *object)
{
    MMLocationGpsNmea *self = MM_LOCATION_GPS_NMEA (object);

    g_hash_table_destroy (self->priv->traces);
    if (self->priv->sequence_regex)
        g_regex_unref (self->priv->sequence_regex);

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

static void
mm_location_gps_nmea_class_init (MMLocationGpsNmeaClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    g_type_class_add_private (object_class, sizeof (MMLocationGpsNmeaPrivate));

    object_class->finalize = finalize;
}