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
|
/* -*- 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-raw.h"
/**
* SECTION: mm-location-gps-raw
* @title: MMLocationGpsRaw
* @short_description: Helper object to handle generic GPS location information.
*
* The #MMLocationGpsRaw 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_raw(),
* mm_modem_location_get_gps_raw_sync(), mm_modem_location_get_full() or
* mm_modem_location_get_full_sync().
*/
G_DEFINE_TYPE (MMLocationGpsRaw, mm_location_gps_raw, G_TYPE_OBJECT)
#define PROPERTY_UTC_TIME "utc-time"
#define PROPERTY_LATITUDE "latitude"
#define PROPERTY_LONGITUDE "longitude"
#define PROPERTY_ALTITUDE "altitude"
struct _MMLocationGpsRawPrivate {
GRegex *gga_regex;
gboolean prefer_gngga;
gchar *utc_time;
gdouble latitude;
gdouble longitude;
gdouble altitude;
};
/*****************************************************************************/
/**
* mm_location_gps_raw_get_utc_time:
* @self: a #MMLocationGpsRaw.
*
* Gets the UTC time of the location being reported.
*
* Returns: a string with the UTC time, or #NULL if unknown. Do not free the
* returned value, it is owned by @self.
*
* Since: 1.0
*/
const gchar *
mm_location_gps_raw_get_utc_time (MMLocationGpsRaw *self)
{
g_return_val_if_fail (MM_IS_LOCATION_GPS_RAW (self), NULL);
return self->priv->utc_time;
}
/*****************************************************************************/
/**
* mm_location_gps_raw_get_longitude:
* @self: a #MMLocationGpsRaw.
*
* Gets the longitude, in the [-180,180] range.
*
* Returns: the longitude, or %MM_LOCATION_LONGITUDE_UNKNOWN if unknown.
*
* Since: 1.0
*/
gdouble
mm_location_gps_raw_get_longitude (MMLocationGpsRaw *self)
{
g_return_val_if_fail (MM_IS_LOCATION_GPS_RAW (self),
MM_LOCATION_LONGITUDE_UNKNOWN);
return self->priv->longitude;
}
/*****************************************************************************/
/**
* mm_location_gps_raw_get_latitude:
* @self: a #MMLocationGpsRaw.
*
* Gets the latitude, in the [-90,90] range.
*
* Returns: the latitude, or %MM_LOCATION_LATITUDE_UNKNOWN if unknown.
*
* Since: 1.0
*/
gdouble
mm_location_gps_raw_get_latitude (MMLocationGpsRaw *self)
{
g_return_val_if_fail (MM_IS_LOCATION_GPS_RAW (self),
MM_LOCATION_LATITUDE_UNKNOWN);
return self->priv->latitude;
}
/*****************************************************************************/
/**
* mm_location_gps_raw_get_altitude:
* @self: a #MMLocationGpsRaw.
*
* Gets the altitude, in the [-90,90] range.
*
* Returns: the altitude, or %MM_LOCATION_ALTITUDE_UNKNOWN if unknown.
*
* Since: 1.0
*/
gdouble
mm_location_gps_raw_get_altitude (MMLocationGpsRaw *self)
{
g_return_val_if_fail (MM_IS_LOCATION_GPS_RAW (self),
MM_LOCATION_ALTITUDE_UNKNOWN);
return self->priv->altitude;
}
/*****************************************************************************/
static gboolean
get_longitude_or_latitude_from_match_info (GMatchInfo *match_info,
guint32 match_index,
gdouble *out)
{
gchar *aux;
gchar *s;
gboolean ret = FALSE;
gdouble minutes;
gdouble degrees;
s = g_match_info_fetch (match_info, match_index);
if (!s)
goto out;
/* 4533.35 is 45 degrees and 33.35 minutes */
aux = strchr (s, '.');
if (!aux || ((aux - s) < 3))
goto out;
aux -= 2;
if (!mm_get_double_from_str (aux, &minutes))
goto out;
aux[0] = '\0';
if (!mm_get_double_from_str (s, °rees))
goto out;
/* Include the minutes as part of the degrees */
*out = degrees + (minutes / 60.0);
ret = TRUE;
out:
g_free (s);
return ret;
}
/**
* mm_location_gps_raw_add_trace: (skip)
*/
gboolean
mm_location_gps_raw_add_trace (MMLocationGpsRaw *self,
const gchar *trace)
{
g_autoptr(GMatchInfo) match_info = NULL;
/* Current implementation works only with $GPGGA and $GNGGA traces */
do {
if (g_str_has_prefix (trace, "$GPGGA")) {
if (self->priv->prefer_gngga)
/* Ignore GPGGA, prefer GNGGA */
return FALSE;
break;
}
if (g_str_has_prefix (trace, "$GNGGA")) {
if (!self->priv->prefer_gngga)
self->priv->prefer_gngga = TRUE;
break;
}
/* Otherwise, ignore trace */
return FALSE;
} while (0);
/*
* $GPGGA,hhmmss.ss,llll.ll,a,yyyyy.yy,a,x,xx,x.x,x.x,M,x.x,M,x.x,xxxx*hh
* 1 = UTC of Position
* 2 = Latitude
* 3 = N or S
* 4 = Longitude
* 5 = E or W
* 6 = GPS quality indicator (0=invalid; 1=GPS fix; 2=Diff. GPS fix)
* 7 = Number of satellites in use [not those in view]
* 8 = Horizontal dilution of position
* 9 = Antenna altitude above/below mean sea level (geoid)
* 10 = Meters (Antenna height unit)
* 11 = Geoidal separation (Diff. between WGS-84 earth ellipsoid and
* mean sea level. -=geoid is below WGS-84 ellipsoid)
* 12 = Meters (Units of geoidal separation)
* 13 = Age in seconds since last update from diff. reference station
* 14 = Diff. reference station ID#
* 15 = Checksum
*/
if (G_UNLIKELY (!self->priv->gga_regex))
self->priv->gga_regex = g_regex_new ("\\$G(?:P|N)GGA,(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*)\\*(.*).*",
G_REGEX_RAW | G_REGEX_OPTIMIZE,
0,
NULL);
if (g_regex_match (self->priv->gga_regex, trace, 0, &match_info)) {
/* UTC time */
g_free (self->priv->utc_time);
self->priv->utc_time = g_match_info_fetch (match_info, 1);
/* Latitude */
self->priv->latitude = MM_LOCATION_LATITUDE_UNKNOWN;
if (get_longitude_or_latitude_from_match_info (match_info, 2, &self->priv->latitude)) {
gchar *str;
/* N/S */
str = g_match_info_fetch (match_info, 3);
if (str && str[0] == 'S')
self->priv->latitude *= -1;
g_free (str);
}
/* Longitude */
self->priv->longitude = MM_LOCATION_LONGITUDE_UNKNOWN;
if (get_longitude_or_latitude_from_match_info (match_info, 4, &self->priv->longitude)) {
gchar *str;
/* N/S */
str = g_match_info_fetch (match_info, 5);
if (str && str[0] == 'W')
self->priv->longitude *= -1;
g_free (str);
}
/* Altitude */
self->priv->altitude = MM_LOCATION_ALTITUDE_UNKNOWN;
mm_get_double_from_match_info (match_info, 9, &self->priv->altitude);
}
return TRUE;
}
/*****************************************************************************/
/**
* mm_location_gps_raw_get_dictionary: (skip)
*/
GVariant *
mm_location_gps_raw_get_dictionary (MMLocationGpsRaw *self)
{
GVariantBuilder builder;
/* We do allow NULL */
if (!self)
return NULL;
g_return_val_if_fail (MM_IS_LOCATION_GPS_RAW (self), NULL);
/* If mandatory parameters are not found, return NULL */
if (!self->priv->utc_time ||
self->priv->longitude == MM_LOCATION_LONGITUDE_UNKNOWN ||
self->priv->latitude == MM_LOCATION_LATITUDE_UNKNOWN)
return NULL;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
g_variant_builder_add (&builder,
"{sv}",
PROPERTY_UTC_TIME,
g_variant_new_string (self->priv->utc_time));
g_variant_builder_add (&builder,
"{sv}",
PROPERTY_LONGITUDE,
g_variant_new_double (self->priv->longitude));
g_variant_builder_add (&builder,
"{sv}",
PROPERTY_LATITUDE,
g_variant_new_double (self->priv->latitude));
/* Altitude is optional */
if (self->priv->altitude != MM_LOCATION_ALTITUDE_UNKNOWN)
g_variant_builder_add (&builder,
"{sv}",
PROPERTY_ALTITUDE,
g_variant_new_double (self->priv->altitude));
return g_variant_ref_sink (g_variant_builder_end (&builder));
}
/*****************************************************************************/
/**
* mm_location_gps_raw_new_from_dictionary: (skip)
*/
MMLocationGpsRaw *
mm_location_gps_raw_new_from_dictionary (GVariant *dictionary,
GError **error)
{
GError *inner_error = NULL;
MMLocationGpsRaw *self;
GVariantIter iter;
gchar *key;
GVariant *value;
self = mm_location_gps_raw_new ();
if (!dictionary)
return self;
if (!g_variant_is_of_type (dictionary, G_VARIANT_TYPE ("a{sv}"))) {
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Cannot create GPS RAW location from dictionary: "
"invalid variant type received");
g_object_unref (self);
return NULL;
}
g_variant_iter_init (&iter, dictionary);
while (!inner_error &&
g_variant_iter_next (&iter, "{sv}", &key, &value)) {
if (g_str_equal (key, PROPERTY_UTC_TIME))
self->priv->utc_time = g_variant_dup_string (value, NULL);
else if (g_str_equal (key, PROPERTY_LONGITUDE))
self->priv->longitude = g_variant_get_double (value);
else if (g_str_equal (key, PROPERTY_LATITUDE))
self->priv->latitude = g_variant_get_double (value);
else if (g_str_equal (key, PROPERTY_ALTITUDE))
self->priv->altitude = g_variant_get_double (value);
g_free (key);
g_variant_unref (value);
}
/* If any of the mandatory parameters is missing, cleanup */
if (!self->priv->utc_time ||
self->priv->longitude == MM_LOCATION_LONGITUDE_UNKNOWN ||
self->priv->latitude == MM_LOCATION_LATITUDE_UNKNOWN) {
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Cannot create GPS RAW location from dictionary: "
"mandatory parameters missing "
"(utc-time: %s, longitude: %s, latitude: %s)",
self->priv->utc_time ? "yes" : "missing",
(self->priv->longitude != MM_LOCATION_LONGITUDE_UNKNOWN) ? "yes" : "missing",
(self->priv->latitude != MM_LOCATION_LATITUDE_UNKNOWN) ? "yes" : "missing");
g_clear_object (&self);
}
return self;
}
/*****************************************************************************/
/**
* mm_location_gps_raw_new: (skip)
*/
MMLocationGpsRaw *
mm_location_gps_raw_new (void)
{
return (MM_LOCATION_GPS_RAW (
g_object_new (MM_TYPE_LOCATION_GPS_RAW, NULL)));
}
static void
mm_location_gps_raw_init (MMLocationGpsRaw *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
MM_TYPE_LOCATION_GPS_RAW,
MMLocationGpsRawPrivate);
self->priv->utc_time = NULL;
self->priv->latitude = MM_LOCATION_LATITUDE_UNKNOWN;
self->priv->longitude = MM_LOCATION_LONGITUDE_UNKNOWN;
self->priv->altitude = MM_LOCATION_ALTITUDE_UNKNOWN;
}
static void
finalize (GObject *object)
{
MMLocationGpsRaw *self = MM_LOCATION_GPS_RAW (object);
if (self->priv->gga_regex)
g_regex_unref (self->priv->gga_regex);
g_free (self->priv->utc_time);
G_OBJECT_CLASS (mm_location_gps_raw_parent_class)->finalize (object);
}
static void
mm_location_gps_raw_class_init (MMLocationGpsRawClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (object_class, sizeof (MMLocationGpsRawPrivate));
object_class->finalize = finalize;
}
|