aboutsummaryrefslogtreecommitdiff
path: root/plugins/cinterion/mm-common-cinterion.c
blob: 6e4da70c6675629e1f1f828b149c456cf8511ce5 (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
462
463
464
465
466
/* -*- 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) 2014 Ammonit Measurement GmbH
 * Author: Aleksander Morgado <aleksander@aleksander.es>
 */

#include "mm-common-cinterion.h"
#include "mm-base-modem-at.h"

static MMIfaceModemLocation *iface_modem_location_parent;

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

#define CINTERION_LOCATION_CONTEXT_TAG "cinterion-location-tag"
static GQuark cinterion_location_context_quark;

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

typedef struct {
    MMModemLocationSource enabled_sources;
} LocationContext;

static void
location_context_free (LocationContext *ctx)
{
    g_slice_free (LocationContext, ctx);
}

static LocationContext *
get_location_context (MMBaseModem *self)
{
    LocationContext *ctx;

    if (G_UNLIKELY (!cinterion_location_context_quark))
        cinterion_location_context_quark =  (g_quark_from_static_string (
                                                 CINTERION_LOCATION_CONTEXT_TAG));

    ctx = g_object_get_qdata (G_OBJECT (self), cinterion_location_context_quark);
    if (!ctx) {
        /* Create context and keep it as object data */
        ctx = g_slice_new (LocationContext);
        ctx->enabled_sources = MM_MODEM_LOCATION_SOURCE_NONE;

        g_object_set_qdata_full (
            G_OBJECT (self),
            cinterion_location_context_quark,
            ctx,
            (GDestroyNotify)location_context_free);
    }

    return ctx;
}


/*****************************************************************************/
/* Location capabilities loading (Location interface) */

MMModemLocationSource
mm_common_cinterion_location_load_capabilities_finish (MMIfaceModemLocation *self,
                                                       GAsyncResult *res,
                                                       GError **error)
{
    if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error))
        return MM_MODEM_LOCATION_SOURCE_NONE;

    return (MMModemLocationSource) GPOINTER_TO_UINT (g_simple_async_result_get_op_res_gpointer (
                                                         G_SIMPLE_ASYNC_RESULT (res)));
}

static void
parent_load_capabilities_ready (MMIfaceModemLocation *self,
                                GAsyncResult *res,
                                GSimpleAsyncResult *simple)
{
    MMModemLocationSource sources;
    GError *error = NULL;

    sources = iface_modem_location_parent->load_capabilities_finish (self, res, &error);
    if (error) {
        g_simple_async_result_take_error (simple, error);
        g_simple_async_result_complete (simple);
        g_object_unref (simple);
        return;
    }

    /* Now our own check. */
    if (mm_base_modem_peek_port_gps (MM_BASE_MODEM (self)))
        sources |= (MM_MODEM_LOCATION_SOURCE_GPS_NMEA |
                    MM_MODEM_LOCATION_SOURCE_GPS_RAW  |
                    MM_MODEM_LOCATION_SOURCE_GPS_UNMANAGED);

    /* So we're done, complete */
    g_simple_async_result_set_op_res_gpointer (simple,
                                               GUINT_TO_POINTER (sources),
                                               NULL);
    g_simple_async_result_complete (simple);
    g_object_unref (simple);
}

void
mm_common_cinterion_location_load_capabilities (MMIfaceModemLocation *self,
                                                GAsyncReadyCallback callback,
                                                gpointer user_data)
{
    GSimpleAsyncResult *result;

    result = g_simple_async_result_new (G_OBJECT (self),
                                        callback,
                                        user_data,
                                        mm_common_cinterion_location_load_capabilities);

    /* Chain up parent's setup */
    iface_modem_location_parent->load_capabilities (
        self,
        (GAsyncReadyCallback)parent_load_capabilities_ready,
        result);
}

/*****************************************************************************/
/* Enable/Disable location gathering (Location interface) */

typedef struct {
    MMBaseModem *self;
    GSimpleAsyncResult *result;
    MMModemLocationSource source;
} LocationGatheringContext;

static void
location_gathering_context_complete_and_free (LocationGatheringContext *ctx)
{
    g_simple_async_result_complete_in_idle (ctx->result);
    g_object_unref (ctx->result);
    g_object_unref (ctx->self);
    g_slice_free (LocationGatheringContext, ctx);
}

/******************************/
/* Disable location gathering */

gboolean
mm_common_cinterion_disable_location_gathering_finish (MMIfaceModemLocation *self,
                                                       GAsyncResult *res,
                                                       GError **error)
{
    return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error);
}

static void
gps_disabled_ready (MMBaseModem *self,
                    GAsyncResult *res,
                    LocationGatheringContext *ctx)
{
    GError *error = NULL;

    if (!mm_base_modem_at_command_full_finish (self, res, &error))
        g_simple_async_result_take_error (ctx->result, error);
    else
        g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);

    /* Only use the GPS port in NMEA/RAW setups */
    if (ctx->source & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA |
                       MM_MODEM_LOCATION_SOURCE_GPS_RAW)) {
        MMPortSerialGps *gps_port;

        /* Even if we get an error here, we try to close the GPS port */
        gps_port = mm_base_modem_peek_port_gps (self);
        if (gps_port)
            mm_port_serial_close (MM_PORT_SERIAL (gps_port));
    }

    location_gathering_context_complete_and_free (ctx);
}

static void
internal_disable_location_gathering (LocationGatheringContext *ctx)
{
    LocationContext *location_ctx;
    gboolean stop_gps = FALSE;

    location_ctx = get_location_context (MM_BASE_MODEM (ctx->self));

    /* Only stop GPS engine if no GPS-related sources enabled */
    if (ctx->source & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA |
                       MM_MODEM_LOCATION_SOURCE_GPS_RAW |
                       MM_MODEM_LOCATION_SOURCE_GPS_UNMANAGED)) {
        location_ctx->enabled_sources &= ~ctx->source;

        if (!(location_ctx->enabled_sources & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA |
                                               MM_MODEM_LOCATION_SOURCE_GPS_RAW |
                                               MM_MODEM_LOCATION_SOURCE_GPS_UNMANAGED)))
            stop_gps = TRUE;
    }

    if (stop_gps) {
        /* We disable continuous GPS fixes */
        mm_base_modem_at_command_full (MM_BASE_MODEM (ctx->self),
                                       mm_base_modem_peek_best_at_port (MM_BASE_MODEM (ctx->self), NULL),
                                       "AT^SGPSS=0",
                                       3,
                                       FALSE,
                                       FALSE, /* raw */
                                       NULL, /* cancellable */
                                       (GAsyncReadyCallback)gps_disabled_ready,
                                       ctx);
        return;
    }

    /* For any other location (e.g. 3GPP), or if still some GPS needed, just return */
    g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
    location_gathering_context_complete_and_free (ctx);
}

static void
parent_disable_location_gathering_ready (MMIfaceModemLocation *self,
                                         GAsyncResult *res,
                                         LocationGatheringContext *ctx)
{
    GError *error = NULL;

    if (!iface_modem_location_parent->disable_location_gathering_finish (self, res, &error)) {
        if (ctx->source & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA |
                           MM_MODEM_LOCATION_SOURCE_GPS_RAW  |
                           MM_MODEM_LOCATION_SOURCE_GPS_UNMANAGED)) {
            /* Ignore errors when disabling GPS, we can try with AT commands */
            g_error_free (error);
        } else {
            /* Fatal */
            g_simple_async_result_take_error (ctx->result, error);
            location_gathering_context_complete_and_free (ctx);
            return;
        }
    }

    internal_disable_location_gathering (ctx);
}

void
mm_common_cinterion_disable_location_gathering (MMIfaceModemLocation *self,
                                                MMModemLocationSource source,
                                                GAsyncReadyCallback callback,
                                                gpointer user_data)
{
    LocationGatheringContext *ctx;

    ctx = g_slice_new (LocationGatheringContext);
    ctx->self = g_object_ref (self);
    ctx->result = g_simple_async_result_new (G_OBJECT (self),
                                             callback,
                                             user_data,
                                             mm_common_cinterion_disable_location_gathering);
    ctx->source = source;

    /* Chain up parent's gathering enable */
    if (iface_modem_location_parent->disable_location_gathering) {
        iface_modem_location_parent->disable_location_gathering (
            self,
            source,
            (GAsyncReadyCallback)parent_disable_location_gathering_ready,
            ctx);
        return;
    }

    internal_disable_location_gathering (ctx);
}

/*****************************************************************************/
/* Enable location gathering (Location interface) */

gboolean
mm_common_cinterion_enable_location_gathering_finish (MMIfaceModemLocation *self,
                                                      GAsyncResult *res,
                                                      GError **error)
{
    return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error);
}

static void
gps_enabled_ready (MMBaseModem *self,
                   GAsyncResult *res,
                   LocationGatheringContext *ctx)
{
    GError *error = NULL;

    if (!mm_base_modem_at_command_full_finish (self, res, &error)) {
        g_simple_async_result_take_error (ctx->result, error);
        location_gathering_context_complete_and_free (ctx);
        return;
    }

    /* Only use the GPS port in NMEA/RAW setups */
    if (ctx->source & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA |
                       MM_MODEM_LOCATION_SOURCE_GPS_RAW)) {
        MMPortSerialGps *gps_port;

        gps_port = mm_base_modem_peek_port_gps (self);
        if (!gps_port ||
            !mm_port_serial_open (MM_PORT_SERIAL (gps_port), &error)) {
            if (error)
                g_simple_async_result_take_error (ctx->result, error);
            else
                g_simple_async_result_set_error (ctx->result,
                                                 MM_CORE_ERROR,
                                                 MM_CORE_ERROR_FAILED,
                                                 "Couldn't open raw GPS serial port");
        } else
            g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
    } else
        g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);

    location_gathering_context_complete_and_free (ctx);
}

static void
parent_enable_location_gathering_ready (MMIfaceModemLocation *self,
                                        GAsyncResult *res,
                                        LocationGatheringContext *ctx)
{
    gboolean start_gps = FALSE;
    GError *error = NULL;
    LocationContext *location_ctx;

    if (!iface_modem_location_parent->enable_location_gathering_finish (self, res, &error)) {
        if (ctx->source & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA |
                           MM_MODEM_LOCATION_SOURCE_GPS_RAW  |
                           MM_MODEM_LOCATION_SOURCE_GPS_UNMANAGED)) {
            /* Ignore errors when enabling GPS, we can try with AT commands */
            g_error_free (error);
        } else {
            /* Fatal */
            g_simple_async_result_take_error (ctx->result, error);
            location_gathering_context_complete_and_free (ctx);
            return;
        }
    }

    /* Now our own enabling */

    location_ctx = get_location_context (MM_BASE_MODEM (self));

    /* NMEA and RAW are both enabled in the same way */
    if (ctx->source & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA |
                       MM_MODEM_LOCATION_SOURCE_GPS_RAW  |
                       MM_MODEM_LOCATION_SOURCE_GPS_UNMANAGED)) {
        /* Only start GPS engine if not done already */
        if (!(location_ctx->enabled_sources & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA |
                                               MM_MODEM_LOCATION_SOURCE_GPS_RAW  |
                                               MM_MODEM_LOCATION_SOURCE_GPS_UNMANAGED)))
            start_gps = TRUE;
        location_ctx->enabled_sources |= ctx->source;
    }

    if (start_gps) {
        /* We enable continuous GPS fixes */
        mm_base_modem_at_command_full (MM_BASE_MODEM (self),
                                       mm_base_modem_peek_best_at_port (MM_BASE_MODEM (self), NULL),
                                       "AT^SGPSS=4",
                                       3,
                                       FALSE,
                                       FALSE, /* raw */
                                       NULL, /* cancellable */
                                       (GAsyncReadyCallback)gps_enabled_ready,
                                       ctx);
        return;
    }

    /* For any other location (e.g. 3GPP), or if GPS already running just return */
    g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
    location_gathering_context_complete_and_free (ctx);
}

void
mm_common_cinterion_enable_location_gathering (MMIfaceModemLocation *self,
                                               MMModemLocationSource source,
                                               GAsyncReadyCallback callback,
                                               gpointer user_data)
{
    LocationGatheringContext *ctx;

    ctx = g_slice_new (LocationGatheringContext);
    ctx->self = g_object_ref (self);
    ctx->result = g_simple_async_result_new (G_OBJECT (self),
                                             callback,
                                             user_data,
                                             mm_common_cinterion_enable_location_gathering);
    ctx->source = source;

    /* Chain up parent's gathering enable */
    iface_modem_location_parent->enable_location_gathering (
        self,
        source,
        (GAsyncReadyCallback)parent_enable_location_gathering_ready,
        ctx);
}

/*****************************************************************************/
/* Setup ports (Broadband modem class) */

static void
trace_received (MMPortSerialGps *port,
                const gchar *trace,
                MMIfaceModemLocation *self)
{
    /* Helper to debug GPS location related issues. Don't depend on a real GPS
     * fix for debugging, just use some random values to update */
#if 0
    if (g_str_has_prefix (trace, "$GPGGA")) {
        GString *str;
        GDateTime *now;

        now = g_date_time_new_now_utc ();
        str = g_string_new ("");
        g_string_append_printf (str,
                                "$GPGGA,%02u%02u%02u,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47",
                                g_date_time_get_hour (now),
                                g_date_time_get_minute (now),
                                g_date_time_get_second (now));
        mm_iface_modem_location_gps_update (self, str->str);
        g_string_free (str, TRUE);
        g_date_time_unref (now);
        return;
    }
#endif

    mm_iface_modem_location_gps_update (self, trace);
}

void
mm_common_cinterion_setup_gps_port (MMBroadbandModem *self)
{
    MMPortSerialGps *gps_data_port;

    gps_data_port = mm_base_modem_peek_port_gps (MM_BASE_MODEM (self));
    if (gps_data_port) {
        /* It may happen that the modem was started with GPS already enabled, or
         * maybe ModemManager got rebooted and it was left enabled before. We'll make
         * sure that it is disabled when we initialize the modem */
        mm_base_modem_at_command_full (MM_BASE_MODEM (self),
                                       mm_base_modem_peek_best_at_port (MM_BASE_MODEM (self), NULL),
                                       "AT^SGPSS=0",
                                       3, FALSE, FALSE, NULL, NULL, NULL);

        /* Add handler for the NMEA traces */
        mm_port_serial_gps_add_trace_handler (gps_data_port,
                                              (MMPortSerialGpsTraceFn)trace_received,
                                              self,
                                              NULL);
    }
}

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

void
mm_common_cinterion_peek_parent_location_interface (MMIfaceModemLocation *iface)
{
    iface_modem_location_parent = g_type_interface_peek_parent (iface);
}