Files
ModemManager/libmm-glib/mm-location-gps-nmea.c
Andrey Skvortsov f7da97989b location-gps-nmea: add complete sequence to traces at once
Currently sequence messages are added to traces as they are
received. Therefore content of traces depends at the time, when
request is issued and data is reported. Sometimes not all sequence
traces are reported, because only first record was received yet and it
overwrote previous full sequence trace.

For example,

```
$GPGSV,3,1,12,01,35,282,,02,68,264,,03,02,243,,08,35,206,,1*68
$GPGSV,3,2,12,10,46,074,,14,16,312,,17,,,,22,16,327,,1*53
$GPGSV,3,3,12,23,07,068,,24,15,030,,27,14,181,,32,51,120,,1*60

$GLGSV,2,1,08,82,25,135,,76,11,029,,75,21,334,,69,11,201,,1*7A
```

one GSV sentence is missing and second later full sequence is
reported

```
$GPGSV,3,1,12,01,35,282,,02,68,264,,03,02,243,,08,35,206,,1*68
$GPGSV,3,2,12,10,46,074,,14,16,312,,17,,,,22,16,327,,1*53
$GPGSV,3,3,12,23,07,068,,24,15,030,,27,14,181,,32,51,120,,1*60

$GLGSV,2,1,08,82,25,135,,76,11,029,,75,21,334,,69,11,201,,1*7A
$GLGSV,2,2,08,84,40,306,,83,79,161,,67,56,056,,68,58,177,,1*7D
```

Therefore client software reports different number of visible
satellites.

Usually sequence sentences are reported consequently in NMEA
stream. Let's cache and join sequence sentences until new trace type
is received. In that case full sequence always overrides
previous full sequence.

Signed-off-by: Andrey Skvortsov <andrej.skvortzov@gmail.com>
2026-05-26 20:56:53 +00:00

339 lines
10 KiB
C

/* -*- 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 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;
gchar *sequence_last_trace;
gchar *sequence_last_trace_type;
GRegex *sequence_regex;
};
/*****************************************************************************/
static gboolean
is_sequence_trace_type (MMLocationGpsNmea *self,
const gchar *trace,
gboolean *first)
{
g_autoptr(GMatchInfo) match_info = NULL;
guint index;
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);
*first = FALSE;
if (g_regex_match (self->priv->sequence_regex, trace, 0, &match_info)) {
if (mm_get_uint_from_match_info (match_info, 2, &index)) {
*first = (index == 1);
return TRUE;
}
}
return FALSE;
}
static gboolean
location_gps_nmea_take_trace (MMLocationGpsNmea *self,
gchar *_trace)
{
gchar *i;
g_autofree gchar *trace_type = NULL;
g_autofree gchar *trace = _trace;
g_autofree gchar *sequence = NULL;
gboolean first;
gboolean sequence_trace;
i = strchr (trace, ',');
if (!i || i == 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 */
sequence_trace = is_sequence_trace_type (self, trace, &first);
/* Save previous sequence trace, if
* new trace type is different or this is the first sequence trace */
if (self->priv->sequence_last_trace_type &&
self->priv->sequence_last_trace &&
(g_strcmp0 (self->priv->sequence_last_trace_type, trace_type) ||
(sequence_trace && first))) {
g_hash_table_replace (self->priv->traces,
g_steal_pointer (&self->priv->sequence_last_trace_type),
g_steal_pointer (&self->priv->sequence_last_trace));
}
if (!sequence_trace) {
g_hash_table_replace (self->priv->traces,
g_steal_pointer (&trace_type),
g_steal_pointer (&trace));
return TRUE;
}
/* Process the trace as part of a sequence. */
if (first) {
/* Record first record in the sequence */
sequence = g_steal_pointer (&trace);
} else {
/* Append */
const gchar *previous = self->priv->sequence_last_trace;
if (!previous)
previous = g_hash_table_lookup (self->priv->traces, trace_type);
if (previous) {
/* Skip the trace if we already have it there */
if (strstr (previous, trace))
return TRUE;
sequence = g_strdup_printf ("%s%s%s",
previous,
g_str_has_suffix (previous, "\r\n") ? "" : "\r\n",
trace);
} else {
/* No previous records, record first record in the sequence */
sequence = g_steal_pointer (&trace);
}
}
g_clear_pointer (&self->priv->sequence_last_trace, g_free);
self->priv->sequence_last_trace = g_steal_pointer (&sequence);
g_clear_pointer (&self->priv->sequence_last_trace_type, g_free);
self->priv->sequence_last_trace_type = g_steal_pointer (&trace_type);
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_free (self->priv->sequence_last_trace);
g_free (self->priv->sequence_last_trace_type);
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;
}