summaryrefslogtreecommitdiff
path: root/src/gclue-nmea-source.c
blob: c1be17232df7c0789ef6ea890f303430226c822b (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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/* vim: set et ts=8 sw=8: */
/*
 * Copyright 2014 Red Hat, Inc.
 * Copyright 2015 Ankit (Verma)
 *
 * Geoclue 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.
 *
 * Geoclue 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with Geoclue; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *          Ankit (Verma) <ankitstarski@gmail.com>
 */

#include <stdlib.h>
#include <glib.h>
#include "gclue-config.h"
#include "gclue-nmea-source.h"
#include "gclue-location.h"
#include "config.h"
#include "gclue-enum-types.h"

#include <avahi-client/lookup.h>
#include <avahi-common/simple-watch.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>
#include <avahi-glib/glib-watch.h>
#include <gio/gunixsocketaddress.h>

typedef struct AvahiServiceInfo AvahiServiceInfo;

struct _GClueNMEASourcePrivate {
        GSocketConnection *connection;

        GSocketClient *client;

        GCancellable *cancellable;

        AvahiClient *avahi_client;

        AvahiServiceInfo *active_service;

        /* List of all services but only the most accurate one is used. */
        GList *all_services;
};

G_DEFINE_TYPE_WITH_CODE (GClueNMEASource,
                         gclue_nmea_source,
                         GCLUE_TYPE_LOCATION_SOURCE,
                         G_ADD_PRIVATE (GClueNMEASource))

static GClueLocationSourceStartResult
gclue_nmea_source_start (GClueLocationSource *source);
static GClueLocationSourceStopResult
gclue_nmea_source_stop (GClueLocationSource *source);

static void
connect_to_service (GClueNMEASource *source);
static void
disconnect_from_service (GClueNMEASource *source);

struct AvahiServiceInfo {
    char *identifier;
    char *host_name;
    guint16 port;
    GClueAccuracyLevel accuracy;
    guint64 timestamp;
};

static void
avahi_service_free (gpointer data)
{
        AvahiServiceInfo *service = (AvahiServiceInfo *) data;

        g_free (service->identifier);
        g_free (service->host_name);
        g_slice_free(AvahiServiceInfo, service);
}

static AvahiServiceInfo *
avahi_service_new (const char        *identifier,
                   const char        *host_name,
                   guint16            port,
                   GClueAccuracyLevel accuracy)
{
        GTimeVal tv;

        AvahiServiceInfo *service = g_slice_new0 (AvahiServiceInfo);

        service->identifier = g_strdup (identifier);
        service->host_name = g_strdup (host_name);
        service->port = port;
        service->accuracy = accuracy;
        g_get_current_time (&tv);
        service->timestamp = tv.tv_sec;

        return service;
}

static gint
compare_avahi_service_by_identifier (gconstpointer a,
                                     gconstpointer b)
{
        AvahiServiceInfo *first, *second;

        first = (AvahiServiceInfo *) a;
        second = (AvahiServiceInfo *) b;

        return g_strcmp0 (first->identifier, second->identifier);
}

static gint
compare_avahi_service_by_accuracy_n_time (gconstpointer a,
                                          gconstpointer b)
{
        AvahiServiceInfo *first, *second;
        gint diff;

        first = (AvahiServiceInfo *) a;
        second = (AvahiServiceInfo *) b;

        diff = second->accuracy - first->accuracy;

        if (diff == 0)
                return first->timestamp - second->timestamp;

        return diff;
}

static gboolean
reconnection_required (GClueNMEASource *source)
{
        GClueNMEASourcePrivate *priv = source->priv;

        /* Basically, reconnection is required if either
         *
         * 1. service in use went down.
         * 2. a more accurate service than one currently in use, is now
         *    available.
         */
        return (priv->active_service != NULL &&
                (priv->all_services == NULL ||
                 priv->active_service != priv->all_services->data));
}

static void
reconnect_service (GClueNMEASource *source)
{
        if (!reconnection_required (source))
                return;

        disconnect_from_service (source);
        connect_to_service (source);
}

static void
refresh_accuracy_level (GClueNMEASource *source)
{
        GClueAccuracyLevel new, existing;

        existing = gclue_location_source_get_available_accuracy_level
                        (GCLUE_LOCATION_SOURCE (source));

        if (source->priv->all_services != NULL) {
                AvahiServiceInfo *service;

                service = (AvahiServiceInfo *) source->priv->all_services->data;
                new = service->accuracy;
        } else {
                new = GCLUE_ACCURACY_LEVEL_NONE;
        }

        if (new != existing) {
                g_debug ("Available accuracy level from %s: %u",
                         G_OBJECT_TYPE_NAME (source), new);
                g_object_set (G_OBJECT (source),
                              "available-accuracy-level", new,
                              NULL);
        }
}

static void
add_new_service (GClueNMEASource *source,
                 const char *name,
                 const char *host_name,
                 uint16_t port,
                 AvahiStringList *txt)
{
        GClueAccuracyLevel accuracy = GCLUE_ACCURACY_LEVEL_NONE;
        AvahiServiceInfo *service;
        AvahiStringList *node;
        guint n_services;
        char *key, *value;
        GEnumClass *enum_class;
        GEnumValue *enum_value;

        if (port == 0) {
	        accuracy = GCLUE_ACCURACY_LEVEL_EXACT;

	        goto CREATE_SERVICE;
        }

        node = avahi_string_list_find (txt, "accuracy");

        if (node == NULL) {
                g_warning ("No `accuracy` key inside TXT record");
                accuracy = GCLUE_ACCURACY_LEVEL_EXACT;

                goto CREATE_SERVICE;
        }

        avahi_string_list_get_pair (node, &key, &value, NULL);

        if (value == NULL) {
                g_warning ("There is no value for `accuracy` inside TXT "
                           "record");
                accuracy = GCLUE_ACCURACY_LEVEL_EXACT;

                goto CREATE_SERVICE;
        }

        enum_class = g_type_class_ref (GCLUE_TYPE_ACCURACY_LEVEL);
        enum_value = g_enum_get_value_by_nick (enum_class, value);
        g_type_class_unref (enum_class);

        if (enum_value == NULL) {
                g_warning ("Invalid `accuracy` value `%s` inside TXT records.",
                           value);
                accuracy = GCLUE_ACCURACY_LEVEL_EXACT;

                goto CREATE_SERVICE;
        }

        accuracy = enum_value->value;

CREATE_SERVICE:
        service = avahi_service_new (name, host_name, port, accuracy);

        source->priv->all_services = g_list_insert_sorted
                (source->priv->all_services,
                 service,
                 compare_avahi_service_by_accuracy_n_time);

        refresh_accuracy_level (source);
        reconnect_service (source);

        n_services = g_list_length (source->priv->all_services);

        g_debug ("No. of _nmea-0183._tcp services %u", n_services);
}

static void
remove_service (GClueNMEASource *source,
                AvahiServiceInfo *service)
{
        guint n_services = 0;

        avahi_service_free (service);
        source->priv->all_services = g_list_remove
                (source->priv->all_services, service);

        n_services = g_list_length (source->priv->all_services);

        g_debug ("No. of _nmea-0183._tcp services %u",
                 n_services);

        refresh_accuracy_level (source);
        reconnect_service (source);
}

static void
remove_service_by_name (GClueNMEASource *source,
                        const char      *name)
{
        AvahiServiceInfo *service;
        GList *item;

        /* only `name` is required here */
        service = avahi_service_new (name,
                                     NULL,
                                     0,
                                     GCLUE_ACCURACY_LEVEL_NONE);

        item = g_list_find_custom (source->priv->all_services,
                                   service,
                                   compare_avahi_service_by_identifier);
        avahi_service_free (service);

        if (item == NULL)
                return;

        remove_service (source, item->data);
}

static void
resolve_callback (AvahiServiceResolver  *service_resolver,
                  AvahiIfIndex           interface G_GNUC_UNUSED,
                  AvahiProtocol          protocol G_GNUC_UNUSED,
                  AvahiResolverEvent     event,
                  const char            *name,
                  const char            *type,
                  const char            *domain,
                  const char            *host_name,
                  const AvahiAddress    *address,
                  uint16_t               port,
                  AvahiStringList       *txt,
                  AvahiLookupResultFlags flags,
                  void                  *user_data)
{
        const char *errorstr;

        /* FIXME: check with Avahi devs whether this is really needed. */
        g_return_if_fail (service_resolver != NULL);

        switch (event) {
        case AVAHI_RESOLVER_FAILURE: {
                AvahiClient *avahi_client = avahi_service_resolver_get_client
                        (service_resolver);

                errorstr = avahi_strerror (avahi_client_errno (avahi_client));

                g_warning ("(Resolver) Failed to resolve service '%s' "
                           "of type '%s' in domain '%s': %s",
                           name,
                           type,
                           domain,
                           errorstr);

                break;
        }

        case AVAHI_RESOLVER_FOUND:
                g_debug ("Service %s:%u resolved",
                         host_name,
                         port);

                add_new_service (GCLUE_NMEA_SOURCE (user_data),
                                 name,
                                 host_name,
                                 port,
                                 txt);

                break;
        }

    avahi_service_resolver_free (service_resolver);
}

static void
client_callback (AvahiClient     *avahi_client,
                 AvahiClientState state,
                 void            *user_data)
{
        GClueNMEASourcePrivate *priv = GCLUE_NMEA_SOURCE (user_data)->priv;

        g_return_if_fail (avahi_client != NULL);

        priv->avahi_client = avahi_client;

        if (state == AVAHI_CLIENT_FAILURE) {
                const char *errorstr = avahi_strerror
                        (avahi_client_errno (avahi_client));
                g_warning ("Avahi client failure: %s",
                           errorstr);
        }
}

static void
browse_callback (AvahiServiceBrowser   *service_browser,
                 AvahiIfIndex           interface,
                 AvahiProtocol          protocol,
                 AvahiBrowserEvent      event,
                 const char            *name,
                 const char            *type,
                 const char            *domain,
                 AvahiLookupResultFlags flags G_GNUC_UNUSED,
                 void                  *user_data)
{
        GClueNMEASourcePrivate *priv = GCLUE_NMEA_SOURCE (user_data)->priv;
        const char *errorstr;

        /* FIXME: check with Avahi devs whether this is really needed. */
        g_return_if_fail (service_browser != NULL);

        switch (event) {
        case AVAHI_BROWSER_FAILURE:
                errorstr = avahi_strerror (avahi_client_errno
                        (avahi_service_browser_get_client (service_browser)));

                g_warning ("Avahi service browser Error %s", errorstr);

                return;

        case AVAHI_BROWSER_NEW: {
                AvahiServiceResolver *service_resolver;

                g_debug ("Service '%s' of type '%s' found in domain '%s'",
                         name, type, domain);

                service_resolver = avahi_service_resolver_new
                        (priv->avahi_client,
                         interface, protocol,
                         name, type,
                         domain,
                         AVAHI_PROTO_UNSPEC,
                         0,
                         resolve_callback,
                         user_data);

                if (service_resolver == NULL) {
                        errorstr = avahi_strerror
                                (avahi_client_errno (priv->avahi_client));

                        g_warning ("Failed to resolve service '%s': %s",
                                   name,
                                   errorstr);
                }

                break;
        }

        case AVAHI_BROWSER_REMOVE:
                g_debug ("Service '%s' of type '%s' in domain '%s' removed "
                         "from the list of available NMEA services",
                         name,
                         type,
                         domain);

                remove_service_by_name (GCLUE_NMEA_SOURCE (user_data), name);

                break;

        case AVAHI_BROWSER_ALL_FOR_NOW:
        case AVAHI_BROWSER_CACHE_EXHAUSTED:
                g_debug ("Avahi Service Browser's %s event occurred",
                         event == AVAHI_BROWSER_CACHE_EXHAUSTED ?
                         "CACHE_EXHAUSTED" :
                         "ALL_FOR_NOW");

                break;
        }
}

/**
 * gclue_nmea_is_gga:
 * @msg: NMEA sentence
 *
 * Returns: whether given NMEA sentence is a GGA
 **/
gboolean
gclue_nmea_is_gga (const char *msg)
{
        return g_str_has_prefix (msg, "$GA" "GGA") ||  /* Galieo */
               g_str_has_prefix (msg, "$GB" "GGA") ||  /* BeiDou */
               g_str_has_prefix (msg, "$BD" "GGA") ||  /* BeiDou */
               g_str_has_prefix (msg, "$GL" "GGA") ||  /* GLONASS */
               g_str_has_prefix (msg, "$GN" "GGA") ||  /* GNSS (combined) */
               g_str_has_prefix (msg, "$GP" "GGA") ||  /* GPS, SBAS, QZSS */
               g_str_has_prefix (msg, "$QZ" "GGA");    /* QZSS */
}

/**
 * gclue_nmea_is_rmc:
 * @msg: NMEA sentence
 *
 * Returns: whether given NMEA sentence is a RMC
 **/
gboolean
gclue_nmea_is_rmc (const char *msg)
{
        return g_str_has_prefix (msg, "$GA" "RMC") ||  /* Galieo */
               g_str_has_prefix (msg, "$GB" "RMC") ||  /* BeiDou */
               g_str_has_prefix (msg, "$BD" "RMC") ||  /* BeiDou */
               g_str_has_prefix (msg, "$GL" "RMC") ||  /* GLONASS */
               g_str_has_prefix (msg, "$GN" "RMC") ||  /* GNSS (combined) */
               g_str_has_prefix (msg, "$GP" "RMC") ||  /* GPS, SBAS, QZSS */
               g_str_has_prefix (msg, "$QZ" "RMC");    /* QZSS */

}

/**
 * gclue_nmea_is_nmea:
 * @msg: sentence
 *
 * Returns: whether given sentence is valid NMEA
 **/
gboolean
gclue_nmea_is_nmea(const char *msg)
{
        return gclue_nmea_is_gga(msg) || gclue_nmea_is_rmc(msg);
}

static void
on_read_nmea_sentence (GObject      *object,
                       GAsyncResult *result,
                       gpointer      user_data)
{
        GClueNMEASource *source = GCLUE_NMEA_SOURCE (user_data);
        GDataInputStream *data_input_stream = G_DATA_INPUT_STREAM (object);
        GError *error = NULL;
        GClueLocation *prev_location;
        GClueLocation *location;
        gsize data_size = 0 ;
        char *message;

        message = g_data_input_stream_read_line_finish (data_input_stream,
                                                        result,
                                                        &data_size,
                                                        &error);

        if (message == NULL) {
                if (error != NULL) {
                        if (error->code == G_IO_ERROR_CLOSED)
                                g_debug ("Socket closed.");
                        else if (error->code != G_IO_ERROR_CANCELLED)
                                g_warning ("Error when receiving message: %s",
                                           error->message);
                        g_error_free (error);
                } else {
                        g_debug ("Nothing to read");
                }
                g_object_unref (data_input_stream);

                if (source->priv->active_service != NULL)
                        /* In case service did not advertise it exiting
                         * or we failed to receive it's notification.
                         */
                        remove_service (source, source->priv->active_service);

                return;
        }
        g_debug ("Network source sent: \"%s\"", message);

        if (!gclue_nmea_is_nmea (message)) {
                g_debug ("Ignoring NMEA sentence, as it's niether GGA or RMC: %s", message);
                goto READ_NEXT_LINE;
         }

        prev_location = gclue_location_source_get_location (GCLUE_LOCATION_SOURCE (source));
        location = gclue_location_create_from_nmea (message,
                                                    prev_location,
                                                    &error);

        if (error != NULL) {
                g_warning ("Error: %s", error->message);
                g_clear_error (&error);
        } else {
                gclue_location_source_set_location
                        (GCLUE_LOCATION_SOURCE (source), location);
        }

READ_NEXT_LINE:
        g_data_input_stream_read_line_async (data_input_stream,
                                             G_PRIORITY_DEFAULT,
                                             source->priv->cancellable,
                                             on_read_nmea_sentence,
                                             source);
}

static void
on_connection_to_location_server (GObject      *object,
                                  GAsyncResult *result,
                                  gpointer      user_data)
{
        GClueNMEASource *source = GCLUE_NMEA_SOURCE (user_data);
        GSocketClient *client = G_SOCKET_CLIENT (object);
        GError *error = NULL;
        GDataInputStream *data_input_stream;
        GInputStream *input_stream;

        source->priv->connection = g_socket_client_connect_to_host_finish
                (client,
                 result,
                 &error);

        if (error != NULL) {
                if (error->code != G_IO_ERROR_CANCELLED)
                        g_warning ("Failed to connect to NMEA service: %s", error->message);
                g_clear_error (&error);

                return;
        }

        input_stream = g_io_stream_get_input_stream
                (G_IO_STREAM (source->priv->connection));
        data_input_stream = g_data_input_stream_new (input_stream);

        g_data_input_stream_read_line_async (data_input_stream,
                                             G_PRIORITY_DEFAULT,
                                             source->priv->cancellable,
                                             on_read_nmea_sentence,
                                             source);
}

static void
connect_to_service (GClueNMEASource *source)
{
        GClueNMEASourcePrivate *priv = source->priv;
        GSocketAddress *addr;
        GSocketConnectable *connectable;

        if (priv->all_services == NULL)
                return;

        priv->client = g_socket_client_new ();
        g_cancellable_reset (priv->cancellable);

        /* The service with the highest accuracy will be stored in the beginning
         * of the list.
         */
        priv->active_service = (AvahiServiceInfo *) priv->all_services->data;

        if ( priv->active_service->port != 0 )
		g_socket_client_connect_to_host_async
			(priv->client,
			 priv->active_service->host_name,
			 priv->active_service->port,
			 priv->cancellable,
			 on_connection_to_location_server,
			 source);
        else {
		addr = g_unix_socket_address_new(priv->active_service->host_name);
		connectable = G_SOCKET_CONNECTABLE (addr);
		g_socket_client_connect_async (priv->client,
                               connectable,
                               priv->cancellable,
                               on_connection_to_location_server,
                               source);
        }
}

static void
disconnect_from_service (GClueNMEASource *source)
{
        GClueNMEASourcePrivate *priv = source->priv;

        g_cancellable_cancel (priv->cancellable);

        if (priv->connection != NULL) {
                GError *error = NULL;

                g_io_stream_close (G_IO_STREAM (priv->connection),
                                   NULL,
                                   &error);
                if (error != NULL)
                        g_warning ("Error in closing socket connection: %s", error->message);
        }

        g_clear_object (&priv->connection);
        g_clear_object (&priv->client);
        priv->active_service = NULL;
}

static void
gclue_nmea_source_finalize (GObject *gnmea)
{
        GClueNMEASourcePrivate *priv = GCLUE_NMEA_SOURCE (gnmea)->priv;

        G_OBJECT_CLASS (gclue_nmea_source_parent_class)->finalize (gnmea);

        g_clear_object (&priv->connection);
        g_clear_object (&priv->client);
        g_clear_object (&priv->cancellable);
        if (priv->avahi_client)
                avahi_client_free (priv->avahi_client);
        g_list_free_full (priv->all_services,
                          avahi_service_free);
}

static void
gclue_nmea_source_class_init (GClueNMEASourceClass *klass)
{
        GClueLocationSourceClass *source_class = GCLUE_LOCATION_SOURCE_CLASS (klass);
        GObjectClass *gnmea_class = G_OBJECT_CLASS (klass);

        gnmea_class->finalize = gclue_nmea_source_finalize;

        source_class->start = gclue_nmea_source_start;
        source_class->stop = gclue_nmea_source_stop;
}

static void
gclue_nmea_source_init (GClueNMEASource *source)
{
        GClueNMEASourcePrivate *priv;
        AvahiServiceBrowser *service_browser;
        const AvahiPoll *poll_api;
        AvahiGLibPoll *glib_poll;
        const char *nmea_socket;
        GClueConfig *config;
        int error;

        source->priv = G_TYPE_INSTANCE_GET_PRIVATE ((source),
                                                    GCLUE_TYPE_NMEA_SOURCE,
                                                    GClueNMEASourcePrivate);
        priv = source->priv;

        glib_poll = avahi_glib_poll_new (NULL, G_PRIORITY_DEFAULT);
        poll_api = avahi_glib_poll_get (glib_poll);

        priv->cancellable = g_cancellable_new ();

        config = gclue_config_get_singleton ();

        nmea_socket = gclue_config_get_nmea_socket (config);
        if (nmea_socket != NULL) {
                add_new_service (source,
                                 "nmea-socket",
                                 nmea_socket,
                                 0,
                                 NULL);
        }

        avahi_client_new (poll_api,
                          0,
                          client_callback,
                          source,
                          &error);

        if (priv->avahi_client == NULL) {
                g_warning ("Failed to connect to avahi service: %s",
                           avahi_strerror (error));
                return;
        }

        service_browser = avahi_service_browser_new
                (priv->avahi_client,
                 AVAHI_IF_UNSPEC,
                 AVAHI_PROTO_UNSPEC,
                 "_nmea-0183._tcp",
                 NULL,
                 0,
                 browse_callback,
                 source);


        if (service_browser == NULL) {
                const char *errorstr;

                error = avahi_client_errno (priv->avahi_client);
                errorstr = avahi_strerror (error);
                g_warning ("Failed to browse avahi services: %s", errorstr);
        }
}

/**
 * gclue_nmea_source_get_singleton:
 *
 * Get the #GClueNMEASource singleton.
 *
 * Returns: (transfer full): a new ref to #GClueNMEASource. Use g_object_unref()
 * when done.
 **/
GClueNMEASource *
gclue_nmea_source_get_singleton (void)
{
        static GClueNMEASource *source = NULL;

        if (source == NULL) {
                source = g_object_new (GCLUE_TYPE_NMEA_SOURCE, NULL);
                g_object_add_weak_pointer (G_OBJECT (source),
                                           (gpointer) &source);
        } else
                g_object_ref (source);

        return source;
}

static GClueLocationSourceStartResult
gclue_nmea_source_start (GClueLocationSource *source)
{
        GClueLocationSourceClass *base_class;
        GClueLocationSourceStartResult base_result;

        g_return_val_if_fail (GCLUE_IS_NMEA_SOURCE (source), FALSE);

        base_class = GCLUE_LOCATION_SOURCE_CLASS (gclue_nmea_source_parent_class);
        base_result = base_class->start (source);
        if (base_result != GCLUE_LOCATION_SOURCE_START_RESULT_OK)
                return base_result;

        connect_to_service (GCLUE_NMEA_SOURCE (source));

        return base_result;
}

static GClueLocationSourceStopResult
gclue_nmea_source_stop (GClueLocationSource *source)
{
        GClueLocationSourceClass *base_class;
        GClueLocationSourceStopResult base_result;

        g_return_val_if_fail (GCLUE_IS_NMEA_SOURCE (source), FALSE);

        base_class = GCLUE_LOCATION_SOURCE_CLASS (gclue_nmea_source_parent_class);
        base_result = base_class->stop (source);
        if (base_result == GCLUE_LOCATION_SOURCE_STOP_RESULT_STILL_USED)
                return base_result;

        disconnect_from_service (GCLUE_NMEA_SOURCE (source));

        return base_result;
}