summaryrefslogtreecommitdiff
path: root/src/gtlm-nfc.c
blob: c1a20e59d10fa70f0ca9332deebf2a203c214db8 (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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
/* vi: set et sw=4 ts=4 cino=t0,(0: */
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of gtlm-nfc
 *
 * Copyright (C) 2013 Intel Corporation.
 *
 * Contact: Alexander Kanavin <alex.kanavin@gmail.com>
 *
 * 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.1 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 St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include "gtlm-nfc.h"
#include <gio/gio.h>

GQuark
gtlm_nfc_error_quark (void)
{
    return g_quark_from_static_string ("gtlm-nfc");
}

#define GTLM_NFC_ERROR   (gtlm_nfc_error_quark())

/**
 * SECTION:gtlm-nfc
 * @short_description: a helper object that provides NFC functionality to user management code
 * @include: gtlm-nfc.h
 *
 * #GTlmNfc is used by Tizen user and login management to access NFC tags
 * and authenticate users through them. It provides signals to listen for tags
 * appearing and disappearing, and for reading and writing username/password pairs 
 * to tags.
 */
/**
 * GTlmNfcError:
 * @GTLM_NFC_ERROR_NONE: No error
 * @GTLM_NFC_ERROR_NO_TAG: Issued when attempting to write to an absent tag
 * 
 * This enum provides a list of errors that libtlm-nfc returns.
 * 
 */

/**
 * GTlmNfc:
 *
 * Opaque #GTlmNfc data structure.
 */
/**
 * GTlmNfcClass:
 * @parent_class: the parent class structure
 *
 * Opaque #GTlmNfcClass data structure.
 */

G_DEFINE_TYPE_WITH_CODE (GTlmNfc, gtlm_nfc, 
                         G_TYPE_OBJECT,
                         );

enum
{
    PROP_0
    
};

enum {
    SIG_TAG_FOUND,
    SIG_TAG_LOST,
    SIG_RECORD_FOUND,
    SIG_NO_RECORD_FOUND,
 
    SIG_MAX
};

static guint signals[SIG_MAX];


static gchar* _encode_username_password(const gchar* username, const gchar* password)
{
    GVariant* v = g_variant_new("(msms)", username, password);
    
    gchar* out = g_base64_encode(g_variant_get_data(v), g_variant_get_size(v));
    g_variant_unref(v);
    return out;
}

/**
 * gtlm_nfc_write_username_password:
 * @tlm_nfc: an instance of GTlmNfc object
 * @nfc_tag_path: an identificator of the nfc tag (returned by #GTlmNfc::tag-found)
 * @username: username to write
 * @password: password to write
 * @error: if non-NULL, set to an error, if one occurs
 * 
 * This function is used to write a username and password to a tag. The tag path
 * can be obtained by listening to #GTlmNfc::tag-found signals). @error is set to
 * @GTLM_NFC_ERROR_NO_TAG if no such tag exists.
 */
void gtlm_nfc_write_username_password(GTlmNfc* tlm_nfc,
                                      const gchar* nfc_tag_path,
                                      const gchar* username, 
                                      const gchar* password,
                                      GError** error)
{
    if (nfc_tag_path == NULL) {
        *error = g_error_new(GTLM_NFC_ERROR, GTLM_NFC_ERROR_NO_TAG, "No tag is present");
        return;
    }
    
    GDBusProxy* tag = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                         G_DBUS_PROXY_FLAGS_NONE,
                                         NULL, /* GDBusInterfaceInfo */
                                         "org.neard",
                                         nfc_tag_path,
                                         "org.neard.Tag",
                                         NULL, /* GCancellable */
                                         error);
    if (tag == NULL) {
        g_debug ("Error creating tag proxy: %s", (*error)->message);
        return;
    }    

    //gchar binary_data[] = {0x41, 0x42, 0x43, 0x44};
    gchar* binary_data = _encode_username_password(username, password);
    GVariant* payload =  g_variant_new_bytestring(binary_data);
    g_free(binary_data);
    
    GVariant* arguments = g_variant_new_parsed ("({'Type': <'MIME'>, 'MIME': <'application/gtlm-nfc'>, 'Payload' : %v },)", payload);
    
    GVariant *result = g_dbus_proxy_call_sync (tag,
                                        "Write",
                                        arguments,
                                        G_DBUS_CALL_FLAGS_NONE,
                                        -1,
                                        NULL,
                                        error);
    
   if (result == NULL) {
        g_debug ("Error writing to tag: %s", (*error)->message);
        g_object_unref(tag);
        return;
    }    

    g_variant_unref(result);
    g_object_unref(tag);
}

static void _decode_username_password(GTlmNfc* self, const gchar* data)
{
    gsize variant_s_size = 0;
    guchar* variant_s = g_base64_decode(data, &variant_s_size);
    
    GVariantType* v_t = g_variant_type_new("(msms)");
    GVariant* v = g_variant_new_from_data(v_t, variant_s, variant_s_size, FALSE, NULL, NULL);
    
    if (v == NULL) {
        g_debug("Couldn't decode Payload data to variant");
        g_variant_type_free(v_t);
        g_free(variant_s);
        g_signal_emit(self, signals[SIG_NO_RECORD_FOUND], 0);
        return;
    }
    
    gchar* username = NULL;
    gchar* password = NULL;
    g_variant_get(v, "(msms)", &username, &password);
    
    g_signal_emit(self, signals[SIG_RECORD_FOUND], 0, username, password);
    
    g_free(username);
    g_free(password);
    g_variant_unref(v);
    g_variant_type_free(v_t);
    g_free(variant_s);
}

static void
_handle_agent_method_call (GDBusConnection       *connection,
                    const gchar           *sender,
                    const gchar           *object_path,
                    const gchar           *interface_name,
                    const gchar           *method_name,
                    GVariant              *parameters,
                    GDBusMethodInvocation *invocation,
                    gpointer               user_data)
{
    GTlmNfc* self = GTLM_NFC(user_data);
    gchar *parameters_str;

    parameters_str = g_variant_print (parameters, TRUE);
    g_debug ("Agent received method call: %s\n\tParameters: %s\n\tSender: %s\n\tObject path: %s\n\tInteface name: %s",
           method_name, parameters_str, sender, object_path, interface_name);
    g_free (parameters_str);
    
    if (g_strcmp0(method_name, "GetNDEF") != 0) {
        goto out;
    }

    GVariant* parameters_dict = g_variant_get_child_value(parameters, 0);
    if (parameters_dict == NULL)
    {
        g_debug ("Error getting parameters dict");
        g_signal_emit(self, signals[SIG_NO_RECORD_FOUND], 0);
        goto out;
    }

    gchar* payload_data;
    
    if (g_variant_lookup(parameters_dict, "Payload", "^ay", &payload_data) == FALSE) {
        g_debug ("Error getting raw Payload data");
        g_signal_emit(self, signals[SIG_NO_RECORD_FOUND], 0);        
        g_variant_unref(parameters_dict);
        goto out;
    }
    g_variant_unref(parameters_dict);
    
    _decode_username_password(self, payload_data);
    g_free(payload_data);
    
out:    
    g_dbus_method_invocation_return_value (invocation, NULL);
}

static GVariant *
_handle_agent_get_property (GDBusConnection  *connection,
                     const gchar      *sender,
                     const gchar      *object_path,
                     const gchar      *interface_name,
                     const gchar      *property_name,
                     GError          **error,
                     gpointer          user_data)
{
    return NULL;
}

static gboolean
_handle_agent_set_property (GDBusConnection  *connection,
                     const gchar      *sender,
                     const gchar      *object_path,
                     const gchar      *interface_name,
                     const gchar      *property_name,
                     GVariant         *value,
                     GError          **error,
                     gpointer          user_data)
{
    return FALSE;
}

static void
_setup_nfc_adapter(GTlmNfc *self, GDBusProxy *nfc_adapter)
{
    GError* error = NULL;
    gboolean powered;
    gboolean polling;

    // for some reason the cached properties aren't updated, so we request them directly
    //GVariant* powered_v = g_dbus_proxy_get_cached_property(nfc_adapter, "Powered");
    GVariant* powered_resp = g_dbus_connection_call_sync (self->system_bus,
                                        "org.neard",
                                        g_dbus_proxy_get_object_path(nfc_adapter) ,
                                        "org.freedesktop.DBus.Properties",
                                        "Get",
                                        g_variant_new("(ss)", 
                                                      g_dbus_proxy_get_interface_name(nfc_adapter), 
                                                      "Powered"
                                                     ),
                                        NULL,              
                                        G_DBUS_CALL_FLAGS_NONE,
                                        -1,
                                        NULL,
                                        &error);
    
    if (powered_resp == NULL) {
        g_debug("Powered property is absent on an adapter: %s", error->message);
        g_error_free (error);
        return;
    }
    
    GVariant* powered_v;
    g_variant_get_child(powered_resp, 0, "v", &powered_v);
    
    if (powered_v == NULL || !g_variant_is_of_type(powered_v, G_VARIANT_TYPE_BOOLEAN)) {
        g_debug("Error retrieving powered property");
        return;
    }
    
    powered = g_variant_get_boolean(powered_v);
    g_variant_unref(powered_resp);
    
    //GVariant* polling_v = g_dbus_proxy_get_cached_property(nfc_adapter, "Polling");
    GVariant* polling_resp = g_dbus_connection_call_sync (self->system_bus,
                                        "org.neard",
                                        g_dbus_proxy_get_object_path(nfc_adapter) ,
                                        "org.freedesktop.DBus.Properties",
                                        "Get",
                                        g_variant_new("(ss)", 
                                                      g_dbus_proxy_get_interface_name(nfc_adapter), 
                                                      "Polling"
                                                     ),
                                        NULL,              
                                        G_DBUS_CALL_FLAGS_NONE,
                                        -1,
                                        NULL,
                                        &error);
    if (polling_resp == NULL) {
        g_debug("Polling property is absent on an adapter: %s", error->message);
        g_error_free (error);
        return;
    }
    
    GVariant* polling_v;
    g_variant_get_child(polling_resp, 0, "v", &polling_v);
    
    if (polling_v == NULL || !g_variant_is_of_type(polling_v, G_VARIANT_TYPE_BOOLEAN)) {
        g_debug("Error retrieving polling property");
        return;
    }
    
    polling = g_variant_get_boolean(polling_v);
    g_variant_unref(polling_resp);
    
    
    GVariant* response;
    if (powered == FALSE) {
        // switch power on

        response = g_dbus_connection_call_sync (self->system_bus,
                                        "org.neard",
                                        g_dbus_proxy_get_object_path(nfc_adapter) ,
                                        "org.freedesktop.DBus.Properties",
                                        "Set",
                                        g_variant_new("(ssv)", 
                                                      g_dbus_proxy_get_interface_name(nfc_adapter), 
                                                      "Powered",
                                                      g_variant_new_boolean(TRUE)
                                                     ),
                                        NULL,              
                                        G_DBUS_CALL_FLAGS_NONE,
                                        -1,
                                        NULL,
                                        &error);
        
        if (response == NULL ) {
            g_debug("Error swithing NFC adapter on: %s", error->message);
            g_error_free (error);
            return;
        }
        g_variant_unref(response);
        g_debug("Switched NFC adapter on");
    } else
        g_debug("Adapter already switched on");

    if (polling == FALSE) {
        // start polling
        response = g_dbus_proxy_call_sync (nfc_adapter,
                                        "StartPollLoop",
                                        g_variant_new("(s)", "Initiator"),
                                        G_DBUS_CALL_FLAGS_NONE,
                                        -1,
                                        NULL,
                                        &error);
        if (response == NULL)
        {
            g_debug("Error starting NFC poll loop: %s", error->message);
            g_error_free (error);
            return;
        }
        g_variant_unref(response);
        g_debug("Started NFC poll loop");

    } else
        g_debug("Adapter already in polling mode");


}


void _on_interface_added(GDBusObjectManager *manager,
                         GDBusObject        *object,
                         GDBusInterface     *interface,
                         gpointer            user_data)
{
    GTlmNfc* self = GTLM_NFC(user_data);
    GDBusProxy* proxy = G_DBUS_PROXY(interface);
    g_debug("Object %s added interface %s", 
                    g_dbus_object_get_object_path (object),
                    g_dbus_proxy_get_interface_name (proxy));
    
    if (g_strcmp0(g_dbus_proxy_get_interface_name (proxy),
                "org.neard.Adapter") == 0) {
        _setup_nfc_adapter(self, proxy);
        return;
    }
    if (g_strcmp0(g_dbus_proxy_get_interface_name (proxy),
                "org.neard.Tag") == 0) {
        g_signal_emit(self, signals[SIG_TAG_FOUND], 0, g_dbus_object_get_object_path (object));
        return;
    }

    if (g_strcmp0(g_dbus_proxy_get_interface_name (proxy),
                "org.neard.Record") == 0) {
        GVariant* type_v = g_dbus_proxy_get_cached_property(proxy, "Type");
        if (type_v == NULL || !g_variant_is_of_type(type_v, G_VARIANT_TYPE_STRING)) {
            g_debug("Type property is absent on a record");
            g_signal_emit(self, signals[SIG_NO_RECORD_FOUND], 0);
            return;
        }
        const gchar* type = g_variant_get_string(type_v, NULL);
        g_debug("Record has type %s", type);
        if (g_strcmp0(type, "MIME") != 0) {
            g_variant_unref(type_v);
            g_signal_emit(self, signals[SIG_NO_RECORD_FOUND], 0);
            return;
        }
        g_variant_unref(type_v);

        GVariant* mimetype_v = g_dbus_proxy_get_cached_property(proxy, "MIME");
        if (mimetype_v == NULL || !g_variant_is_of_type(type_v, G_VARIANT_TYPE_STRING)) {
            g_debug("MIME property is absent on a record");
            g_signal_emit(self, signals[SIG_NO_RECORD_FOUND], 0);
            return;
        }
        const gchar* mimetype = g_variant_get_string(mimetype_v, NULL);
        g_debug("Record has MIME type %s", mimetype);
        if (g_strcmp0(mimetype, "application/gtlm-nfc") != 0) {
            g_variant_unref(mimetype_v);
            g_signal_emit(self, signals[SIG_NO_RECORD_FOUND], 0);
            return;
        }
        g_variant_unref(mimetype_v);
    }
}

static void
_setup_nfc_adapters(GTlmNfc *self)
{
    GList* objects = g_dbus_object_manager_get_objects(self->neard_manager);
    GList* objects_iter = objects;
    
    while (objects_iter != NULL) {
        GList* interfaces = g_dbus_object_get_interfaces(objects_iter->data);
        GList* interfaces_iter = interfaces;
        while (interfaces_iter != NULL) {
            g_debug("Checking managed object %s, interface %s", 
                    g_dbus_object_get_object_path (objects_iter->data),
                    g_dbus_proxy_get_interface_name (interfaces_iter->data));
            if (g_strcmp0(g_dbus_proxy_get_interface_name (interfaces_iter->data),
                "org.neard.Adapter") == 0) {
                _setup_nfc_adapter(self, interfaces_iter->data);
            }
            g_object_unref(interfaces_iter->data);
            interfaces_iter = interfaces_iter->next;
        }
        g_list_free(interfaces);
        g_object_unref(objects_iter->data);
        objects_iter = objects_iter->next;
    }
    g_list_free(objects);
    
}



void _on_interface_removed(GDBusObjectManager *manager,
                         GDBusObject        *object,
                         GDBusInterface     *interface,
                         gpointer            user_data)
{
    GTlmNfc* self = GTLM_NFC(user_data);
    GDBusProxy* proxy = G_DBUS_PROXY(interface);
    g_debug("Object %s removed interface %s", 
                    g_dbus_object_get_object_path (object),
                    g_dbus_proxy_get_interface_name (proxy));    

    if (g_strcmp0(g_dbus_proxy_get_interface_name (proxy),
                "org.neard.Tag") == 0) {
        g_signal_emit(self, signals[SIG_TAG_LOST], 0, g_dbus_object_get_object_path (object));
    
        GVariant* adapter_v = g_dbus_proxy_get_cached_property(proxy, "Adapter");
        if (adapter_v == NULL || !g_variant_is_of_type(adapter_v, G_VARIANT_TYPE_OBJECT_PATH)) {
            g_debug("Adapter property is absent on a tag");
            return;
        }
        const gchar* adapter_path = g_variant_get_string(adapter_v, NULL);
        g_debug("Tag belongs to adapter %s", adapter_path);
        GError* error = NULL;
        GDBusProxy* adapter = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
                                         G_DBUS_PROXY_FLAGS_NONE,
                                         NULL, /* GDBusInterfaceInfo */
                                         "org.neard",
                                         adapter_path,
                                         "org.neard.Adapter",
                                         NULL, /* GCancellable */
                                         &error);
        if (adapter == NULL) {
            g_debug ("Error creating adapter proxy: %s", error->message);
            g_error_free(error);
            g_variant_unref(adapter_v);
            return;
        }        
        // start polling on an adapter
        _setup_nfc_adapter(self, adapter);
        g_object_unref(adapter);
        g_variant_unref(adapter_v);
        return;
    }

    
}


void _on_object_added(GDBusObjectManager *manager,
                         GDBusObject        *object,
                         gpointer            user_data)
{
    //GTlmNfc* self = GTLM_NFC(user_data);
    g_debug("Object %s added", 
                    g_dbus_object_get_object_path (object));
    
    GList* interfaces = g_dbus_object_get_interfaces(object);
    GList* interfaces_iter = interfaces;
    while (interfaces_iter != NULL) {
        _on_interface_added(manager, object, interfaces_iter->data, user_data);
        g_object_unref(interfaces_iter->data);
        interfaces_iter = interfaces_iter->next;
    }
    g_list_free(interfaces);
    g_debug("Finished checking interfaces");   
}

void _on_object_removed(GDBusObjectManager *manager,
                         GDBusObject        *object,
                         gpointer            user_data)
{
    //GTlmNfc* self = GTLM_NFC(user_data);
    g_debug("Object %s removed", 
                    g_dbus_object_get_object_path (object));

    GList* interfaces = g_dbus_object_get_interfaces(object);
    GList* interfaces_iter = interfaces;
    while (interfaces_iter != NULL) {
        _on_interface_removed(manager, object, interfaces_iter->data, user_data);
        g_object_unref(interfaces_iter->data);
        interfaces_iter = interfaces_iter->next;
    }
    g_list_free(interfaces);
    g_debug("Finished checking interfaces");       
}

void _on_property_changed (GDBusObjectManagerClient *manager,
                                                        GDBusObjectProxy         *object_proxy,
                                                        GDBusProxy               *interface_proxy,
                                                        GVariant                 *changed_properties,
                                                        GStrv                     invalidated_properties,
                                                        gpointer                  user_data)
{
    gchar *parameters_str;

    parameters_str = g_variant_print (changed_properties, TRUE);
    g_debug("Property of object %s changed:\n%s",
            g_dbus_object_get_object_path(G_DBUS_OBJECT(object_proxy)), parameters_str);
    g_debug("Invalidated properties:");
    while (*invalidated_properties != NULL) {
        g_print(*invalidated_properties);
        invalidated_properties++;
    }
    g_free(parameters_str);
}

static void _setup_agent_and_adapters(GTlmNfc* self)
{
    GError *error = NULL;

    /* Introspection data for the agent object we are exporting */
    const gchar introspection_xml[] =
        "<node>"
        "  <interface name='org.neard.NDEFAgent'>"
        "    <method name='GetNDEF'>"
        "      <arg type='a{sv}' name='values' direction='in'/>"
        "    </method>"
        "    <method name='Release'>"
        "    </method>"
        "  </interface>"
        "</node>";
    
    const GDBusInterfaceVTable interface_vtable =
    {
        _handle_agent_method_call,
        _handle_agent_get_property,
        _handle_agent_set_property
    };

    self->system_bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
    
    if (self->system_bus == NULL) {
        g_debug ("Error getting a system bus: %s", error->message);
        g_error_free (error);
        return;
    }        
    
    GDBusNodeInfo *introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
      
    self->agent_registration_id = g_dbus_connection_register_object (self->system_bus,
                                                       "/org/tlmnfc/agent",
                                                       introspection_data->interfaces[0],
                                                       &interface_vtable,
                                                       self,
                                                       NULL,
                                                       &error);

    if (self->agent_registration_id <= 0) {
        g_debug ("Error registering an agent object: %s", error->message);
        g_dbus_node_info_unref (introspection_data);
        g_error_free (error);
        return;
    }
    g_dbus_node_info_unref (introspection_data);
    
    GVariant* agent_register_response = g_dbus_connection_call_sync (self->system_bus,
                                         "org.neard",
                                         "/org/neard",
                                         "org.neard.AgentManager",
                                        "RegisterNDEFAgent",
                                        g_variant_new("(os)", 
                                                      "/org/tlmnfc/agent", 
                                                      "application/gtlm-nfc"),
                                        NULL,              
                                        G_DBUS_CALL_FLAGS_NONE,
                                        -1,
                                        NULL,
                                        &error);
    if (agent_register_response == NULL) {
        g_debug ("Error registering an agent with neard: %s", error->message);
        g_error_free (error);
        return;
    }
    g_variant_unref(agent_register_response);

    
    self->neard_manager =  g_dbus_object_manager_client_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                         G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
                                         "org.neard",
                                         "/",
                                         NULL, NULL, NULL, NULL,
                                         &error);
    if (self->neard_manager == NULL)
    {
        g_debug ("Error creating neard object manager: %s", error->message);
        g_error_free (error);
        return ;
    }
    
    // subscribe to interface added/removed signals
    g_signal_connect (G_DBUS_OBJECT_MANAGER(self->neard_manager),
                    "interface-added",
                    G_CALLBACK (_on_interface_added),
                    self);
    g_signal_connect (G_DBUS_OBJECT_MANAGER(self->neard_manager),
                    "interface-removed",
                    G_CALLBACK (_on_interface_removed),
                    self);
    g_signal_connect (G_DBUS_OBJECT_MANAGER(self->neard_manager),
                    "object-added",
                    G_CALLBACK (_on_object_added),
                    self);
    g_signal_connect (G_DBUS_OBJECT_MANAGER(self->neard_manager),
                    "object-removed",
                    G_CALLBACK (_on_object_removed),
                    self);
    
    
    g_signal_connect (G_DBUS_OBJECT_MANAGER(self->neard_manager),
                    "interface-proxy-properties-changed",
                    G_CALLBACK (_on_property_changed),
                    self);
    

    
    _setup_nfc_adapters(self);    
    return;
}


static void
gtlm_nfc_init (GTlmNfc *self)
{
    
    _setup_agent_and_adapters(self);
}

static void
gtlm_nfc_set_property (GObject      *object,
                                       guint         property_id,
                                       const GValue *value,
                                       GParamSpec   *pspec)
{
    switch (property_id)
    {
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
            break;
    }
}

static void
gtlm_nfc_get_property (GObject    *object,
                                       guint       prop_id,
                                       GValue     *value,
                                       GParamSpec *pspec)
{
    GTlmNfc *tlm_nfc = GTLM_NFC (object);
    (void) tlm_nfc;
    switch (prop_id)
    {
            
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
            break;
    }
}

static void gtlm_nfc_dispose(GObject *object)
{
    GTlmNfc* self = GTLM_NFC (object);

    if (self->system_bus) {
        GError* error = NULL;
        GVariant* agent_register_response = g_dbus_connection_call_sync (self->system_bus,
                                         "org.neard",
                                         "/org/neard",
                                         "org.neard.AgentManager",
                                        "UnregisterNDEFAgent",
                                        g_variant_new("(os)",
                                                      "/org/tlmnfc/agent",
                                                      "application/gtlm-nfc"),
                                        NULL,
                                        G_DBUS_CALL_FLAGS_NONE,
                                        -1,
                                        NULL,
                                        &error);
        if (agent_register_response == NULL) {
            g_debug ("Error unregistering an agent with neard: %s", error->message);
            g_error_free (error);
        }
        g_variant_unref(agent_register_response);
    }
    if (self->agent_registration_id > 0)
        if (g_dbus_connection_unregister_object(self->system_bus, self->agent_registration_id) == FALSE)
            g_debug("Error unregistering agent object");


    if (self->neard_manager) {
        g_object_unref(self->neard_manager);
        self->neard_manager = NULL;
    }
    if (self->system_bus) {
        g_object_unref(self->system_bus);
        self->system_bus = NULL;
    }
    
    G_OBJECT_CLASS (gtlm_nfc_parent_class)->dispose (object);
}

static void gtlm_nfc_finalize(GObject *object)
{
    //GTlmNfc* self = GTLM_NFC (object);
    

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

static void
gtlm_nfc_class_init (GTlmNfcClass *klass)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
    
    gobject_class->set_property = gtlm_nfc_set_property;
    gobject_class->get_property = gtlm_nfc_get_property;
    gobject_class->dispose = gtlm_nfc_dispose;
    gobject_class->finalize = gtlm_nfc_finalize;
    
    
    /**
     * GTlmNfc::tag-found:
     * @tlm_nfc: the object which emitted the signal
     * @nfc_tag_path: an identifier of the tag (use in gtlm_nfc_write_username_password())
     * 
     * This signal is issued by #GTlmNfc object when a tag has been found.
     */
    signals[SIG_TAG_FOUND] = g_signal_new ("tag-found", 
        G_TYPE_TLM_NFC,
        G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL, G_TYPE_NONE,
        1, G_TYPE_STRING);
        //2, G_TYPE_STRING, G_TYPE_STRING);    

    /**
     * GTlmNfc::tag-lost:
     * @tlm_nfc: the object which emitted the signal
     * @nfc_tag_path: an identifier of the tag
     * 
     * This signal is issued by #GTlmNfc object when a tag has been lost.
     */
    signals[SIG_TAG_LOST] = g_signal_new ("tag-lost", 
        G_TYPE_TLM_NFC,
        G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL, G_TYPE_NONE,
        1, G_TYPE_STRING);

    /**
     * GTlmNfc::record-found:
     * @tlm_nfc: the object which emitted the signal
     * @username: the username on the tag
     * @password: the password on the tag
     * 
     * This signal is issued by #GTlmNfc object when a username and password pair
     * has been found on a tag.
     */
    signals[SIG_RECORD_FOUND] = g_signal_new ("record-found", 
        G_TYPE_TLM_NFC,
        G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL, G_TYPE_NONE,
        2, G_TYPE_STRING, G_TYPE_STRING);    

    /**
     * GTlmNfc::no-record-found:
     * @tlm_nfc: the object which emitted the signal
     * 
     * This signal is issued by #GTlmNfc object when a username and password pair
     * has not been found on a tag
     */
    signals[SIG_NO_RECORD_FOUND] = g_signal_new ("no-record-found", 
        G_TYPE_TLM_NFC,
        G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL, G_TYPE_NONE,
        0);     
    
}