Use dbus_free for deallocation, when allocated with dbus_malloc0
[platform/upstream/connman.git] / plugins / dundee.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012-2014  BMW Car IT GmbH.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <errno.h>
28
29 #include <gdbus.h>
30
31 #define CONNMAN_API_SUBJECT_TO_CHANGE
32 #include <connman/plugin.h>
33 #include <connman/device.h>
34 #include <connman/network.h>
35 #include <connman/service.h>
36 #include <connman/inet.h>
37 #include <connman/dbus.h>
38
39 #define DUNDEE_SERVICE                  "org.ofono.dundee"
40 #define DUNDEE_MANAGER_INTERFACE        DUNDEE_SERVICE ".Manager"
41 #define DUNDEE_DEVICE_INTERFACE         DUNDEE_SERVICE ".Device"
42
43 #define DEVICE_ADDED                    "DeviceAdded"
44 #define DEVICE_REMOVED                  "DeviceRemoved"
45 #define PROPERTY_CHANGED                "PropertyChanged"
46
47 #define GET_PROPERTIES                  "GetProperties"
48 #define SET_PROPERTY                    "SetProperty"
49 #define GET_DEVICES                     "GetDevices"
50
51 #define TIMEOUT 60000
52
53 static DBusConnection *connection;
54
55 static GHashTable *dundee_devices = NULL;
56
57 struct dundee_data {
58         char *path;
59         char *name;
60
61         struct connman_device *device;
62         struct connman_network *network;
63
64         bool active;
65
66         int index;
67
68         /* IPv4 Settings */
69         enum connman_ipconfig_method method;
70         struct connman_ipaddress *address;
71         char *nameservers;
72
73         DBusPendingCall *call;
74 };
75
76 static char *get_ident(const char *path)
77 {
78         char *pos;
79
80         if (*path != '/')
81                 return NULL;
82
83         pos = strrchr(path, '/');
84         if (!pos)
85                 return NULL;
86
87         return pos + 1;
88 }
89
90 static int create_device(struct dundee_data *info)
91 {
92         struct connman_device *device;
93         char *ident;
94         int err;
95
96         DBG("%s", info->path);
97
98         ident = g_strdup(get_ident(info->path));
99         device = connman_device_create("dundee", CONNMAN_DEVICE_TYPE_BLUETOOTH);
100         if (!device) {
101                 err = -ENOMEM;
102                 goto out;
103         }
104
105         DBG("device %p", device);
106
107         connman_device_set_ident(device, ident);
108
109         connman_device_set_string(device, "Path", info->path);
110
111         connman_device_set_data(device, info);
112
113         err = connman_device_register(device);
114         if (err < 0) {
115                 connman_error("Failed to register DUN device");
116                 connman_device_unref(device);
117                 goto out;
118         }
119
120         info->device = device;
121
122 out:
123         g_free(ident);
124         return err;
125 }
126
127 static void destroy_device(struct dundee_data *info)
128 {
129         connman_device_set_powered(info->device, false);
130
131         if (info->call)
132                 dbus_pending_call_cancel(info->call);
133
134         if (info->network) {
135                 connman_device_remove_network(info->device, info->network);
136                 connman_network_unref(info->network);
137                 info->network = NULL;
138         }
139
140         connman_device_unregister(info->device);
141         connman_device_unref(info->device);
142
143         info->device = NULL;
144 }
145
146 static void device_destroy(gpointer data)
147 {
148         struct dundee_data *info = data;
149
150         if (info->device)
151                 destroy_device(info);
152
153         g_free(info->path);
154         g_free(info->name);
155
156         g_free(info);
157 }
158
159 static int create_network(struct dundee_data *info)
160 {
161         struct connman_network *network;
162         const char *group;
163         int err;
164
165         DBG("%s", info->path);
166
167         network = connman_network_create(info->path,
168                                 CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN);
169         if (!network)
170                 return -ENOMEM;
171
172         DBG("network %p", network);
173
174         connman_network_set_data(network, info);
175
176         connman_network_set_string(network, "Path",
177                                 info->path);
178
179         connman_network_set_name(network, info->name);
180
181         group = get_ident(info->path);
182         connman_network_set_group(network, group);
183
184         err = connman_device_add_network(info->device, network);
185         if (err < 0) {
186                 connman_network_unref(network);
187                 return err;
188         }
189
190         info->network = network;
191
192         return 0;
193 }
194
195 static void set_connected(struct dundee_data *info)
196 {
197         struct connman_service *service;
198
199         DBG("%s", info->path);
200
201         connman_inet_ifup(info->index);
202
203         service = connman_service_lookup_from_network(info->network);
204         if (!service)
205                 return;
206
207         connman_service_create_ip4config(service, info->index);
208         connman_network_set_index(info->network, info->index);
209         connman_network_set_ipv4_method(info->network,
210                                         CONNMAN_IPCONFIG_METHOD_FIXED);
211         connman_network_set_ipaddress(info->network, info->address);
212         connman_network_set_nameservers(info->network, info->nameservers);
213
214         connman_network_set_connected(info->network, true);
215 }
216
217 static void set_disconnected(struct dundee_data *info)
218 {
219         DBG("%s", info->path);
220
221         connman_network_set_connected(info->network, false);
222         connman_inet_ifdown(info->index);
223 }
224
225 static void set_property_reply(DBusPendingCall *call, void *user_data)
226 {
227         struct dundee_data *info = user_data;
228         DBusMessage *reply;
229         DBusError error;
230
231         DBG("%s", info->path);
232
233         info->call = NULL;
234
235         dbus_error_init(&error);
236
237         reply = dbus_pending_call_steal_reply(call);
238
239         if (dbus_set_error_from_message(&error, reply)) {
240                 connman_error("Failed to change property: %s %s %s",
241                                 info->path, error.name, error.message);
242                 dbus_error_free(&error);
243
244                 connman_network_set_error(info->network,
245                                         CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
246         }
247
248         dbus_message_unref(reply);
249
250         dbus_pending_call_unref(call);
251 }
252
253 static int set_property(struct dundee_data *info,
254                         const char *property, int type, void *value)
255 {
256         DBusMessage *message;
257         DBusMessageIter iter;
258
259         DBG("%s %s", info->path, property);
260
261         message = dbus_message_new_method_call(DUNDEE_SERVICE, info->path,
262                                         DUNDEE_DEVICE_INTERFACE, SET_PROPERTY);
263         if (!message)
264                 return -ENOMEM;
265
266         dbus_message_iter_init_append(message, &iter);
267         connman_dbus_property_append_basic(&iter, property, type, value);
268
269         if (!dbus_connection_send_with_reply(connection, message,
270                                                 &info->call, TIMEOUT)) {
271                 connman_error("Failed to change property: %s %s",
272                                 info->path, property);
273                 dbus_message_unref(message);
274                 return -EINVAL;
275         }
276
277         if (!info->call) {
278                 connman_error("D-Bus connection not available");
279                 dbus_message_unref(message);
280                 return -EINVAL;
281         }
282
283         dbus_pending_call_set_notify(info->call, set_property_reply,
284                                         info, NULL);
285
286         dbus_message_unref(message);
287
288         return -EINPROGRESS;
289 }
290
291 static int device_set_active(struct dundee_data *info)
292 {
293         dbus_bool_t active = TRUE;
294
295         DBG("%s", info->path);
296
297         return set_property(info, "Active", DBUS_TYPE_BOOLEAN,
298                                 &active);
299 }
300
301 static int device_set_inactive(struct dundee_data *info)
302 {
303         dbus_bool_t active = FALSE;
304         int err;
305
306         DBG("%s", info->path);
307
308         err = set_property(info, "Active", DBUS_TYPE_BOOLEAN,
309                                 &active);
310         if (err == -EINPROGRESS)
311                 return 0;
312
313         return err;
314 }
315
316 static int network_probe(struct connman_network *network)
317 {
318         DBG("network %p", network);
319
320         return 0;
321 }
322
323 static void network_remove(struct connman_network *network)
324 {
325         DBG("network %p", network);
326 }
327
328 static int network_connect(struct connman_network *network)
329 {
330         struct dundee_data *info = connman_network_get_data(network);
331
332         DBG("network %p", network);
333
334         return device_set_active(info);
335 }
336
337 static int network_disconnect(struct connman_network *network)
338 {
339         struct dundee_data *info = connman_network_get_data(network);
340
341         DBG("network %p", network);
342
343         return device_set_inactive(info);
344 }
345
346 static struct connman_network_driver network_driver = {
347         .name           = "network",
348         .type           = CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN,
349         .probe          = network_probe,
350         .remove         = network_remove,
351         .connect        = network_connect,
352         .disconnect     = network_disconnect,
353 };
354
355 static int dundee_probe(struct connman_device *device)
356 {
357         GHashTableIter iter;
358         gpointer key, value;
359
360         DBG("device %p", device);
361
362         if (!dundee_devices)
363                 return -ENOTSUP;
364
365         g_hash_table_iter_init(&iter, dundee_devices);
366
367         while (g_hash_table_iter_next(&iter, &key, &value)) {
368                 struct dundee_data *info = value;
369
370                 if (device == info->device)
371                         return 0;
372         }
373
374         return -ENOTSUP;
375 }
376
377 static void dundee_remove(struct connman_device *device)
378 {
379         DBG("device %p", device);
380 }
381
382 static int dundee_enable(struct connman_device *device)
383 {
384         DBG("device %p", device);
385
386         return 0;
387 }
388
389 static int dundee_disable(struct connman_device *device)
390 {
391         DBG("device %p", device);
392
393         return 0;
394 }
395
396 static struct connman_device_driver dundee_driver = {
397         .name           = "dundee",
398         .type           = CONNMAN_DEVICE_TYPE_BLUETOOTH,
399         .probe          = dundee_probe,
400         .remove         = dundee_remove,
401         .enable         = dundee_enable,
402         .disable        = dundee_disable,
403 };
404
405 static char *extract_nameservers(DBusMessageIter *array)
406 {
407         DBusMessageIter entry;
408         char *nameservers = NULL;
409         char *tmp;
410
411         dbus_message_iter_recurse(array, &entry);
412
413         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
414                 const char *nameserver;
415
416                 dbus_message_iter_get_basic(&entry, &nameserver);
417
418                 if (!nameservers) {
419                         nameservers = g_strdup(nameserver);
420                 } else {
421                         tmp = nameservers;
422                         nameservers = g_strdup_printf("%s %s", tmp, nameserver);
423                         g_free(tmp);
424                 }
425
426                 dbus_message_iter_next(&entry);
427         }
428
429         return nameservers;
430 }
431
432 static void extract_settings(DBusMessageIter *array,
433                                 struct dundee_data *info)
434 {
435         DBusMessageIter dict;
436         char *address = NULL, *gateway = NULL;
437         char *nameservers = NULL;
438         const char *interface = NULL;
439         int index = -1;
440
441         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
442                 return;
443
444         dbus_message_iter_recurse(array, &dict);
445
446         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
447                 DBusMessageIter entry, value;
448                 const char *key, *val;
449
450                 dbus_message_iter_recurse(&dict, &entry);
451                 dbus_message_iter_get_basic(&entry, &key);
452
453                 dbus_message_iter_next(&entry);
454                 dbus_message_iter_recurse(&entry, &value);
455
456                 if (g_str_equal(key, "Interface")) {
457                         dbus_message_iter_get_basic(&value, &interface);
458
459                         DBG("Interface %s", interface);
460
461                         index = connman_inet_ifindex(interface);
462
463                         DBG("index %d", index);
464
465                         if (index < 0)
466                                 break;
467                 } else if (g_str_equal(key, "Address")) {
468                         dbus_message_iter_get_basic(&value, &val);
469
470                         address = g_strdup(val);
471
472                         DBG("Address %s", address);
473                 } else if (g_str_equal(key, "DomainNameServers")) {
474                         nameservers = extract_nameservers(&value);
475
476                         DBG("Nameservers %s", nameservers);
477                 } else if (g_str_equal(key, "Gateway")) {
478                         dbus_message_iter_get_basic(&value, &val);
479
480                         gateway = g_strdup(val);
481
482                         DBG("Gateway %s", gateway);
483                 }
484
485                 dbus_message_iter_next(&dict);
486         }
487
488         if (index < 0)
489                 goto out;
490
491         info->address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
492         if (!info->address)
493                 goto out;
494
495         info->index = index;
496         connman_ipaddress_set_ipv4(info->address, address, NULL, gateway);
497
498         info->nameservers = nameservers;
499
500 out:
501         if (info->nameservers != nameservers)
502                 g_free(nameservers);
503
504         g_free(address);
505         g_free(gateway);
506 }
507
508 static gboolean device_changed(DBusConnection *conn,
509                                 DBusMessage *message,
510                                 void *user_data)
511 {
512         const char *path = dbus_message_get_path(message);
513         struct dundee_data *info = NULL;
514         DBusMessageIter iter, value;
515         const char *key;
516         const char *signature = DBUS_TYPE_STRING_AS_STRING
517                 DBUS_TYPE_VARIANT_AS_STRING;
518
519         if (!dbus_message_has_signature(message, signature)) {
520                 connman_error("dundee signature does not match");
521                 return TRUE;
522         }
523
524         info = g_hash_table_lookup(dundee_devices, path);
525         if (!info)
526                 return TRUE;
527
528         if (!dbus_message_iter_init(message, &iter))
529                 return TRUE;
530
531         dbus_message_iter_get_basic(&iter, &key);
532
533         dbus_message_iter_next(&iter);
534         dbus_message_iter_recurse(&iter, &value);
535
536         /*
537          * Dundee guarantees the ordering of Settings and
538          * Active. Settings will always be send before Active = True.
539          * That means we don't have to order here.
540          */
541         if (g_str_equal(key, "Active")) {
542                 dbus_bool_t active;
543
544                 dbus_message_iter_get_basic(&value, &active);
545                 info->active = active;
546
547                 DBG("%s Active %d", info->path, info->active);
548
549                 if (info->active)
550                         set_connected(info);
551                 else
552                         set_disconnected(info);
553         } else if (g_str_equal(key, "Settings")) {
554                 DBG("%s Settings", info->path);
555
556                 extract_settings(&value, info);
557         } else if (g_str_equal(key, "Name")) {
558                 char *name;
559
560                 dbus_message_iter_get_basic(&value, &name);
561
562                 g_free(info->name);
563                 info->name = g_strdup(name);
564
565                 DBG("%s Name %s", info->path, info->name);
566
567                 connman_network_set_name(info->network, info->name);
568                 connman_network_update(info->network);
569         }
570
571         return TRUE;
572 }
573
574 static void add_device(const char *path, DBusMessageIter *properties)
575 {
576         struct dundee_data *info;
577         int err;
578
579         info = g_hash_table_lookup(dundee_devices, path);
580         if (info)
581                 return;
582
583         info = g_try_new0(struct dundee_data, 1);
584         if (!info)
585                 return;
586
587         info->path = g_strdup(path);
588
589         while (dbus_message_iter_get_arg_type(properties) ==
590                         DBUS_TYPE_DICT_ENTRY) {
591                 DBusMessageIter entry, value;
592                 const char *key;
593
594                 dbus_message_iter_recurse(properties, &entry);
595                 dbus_message_iter_get_basic(&entry, &key);
596
597                 dbus_message_iter_next(&entry);
598                 dbus_message_iter_recurse(&entry, &value);
599
600                 if (g_str_equal(key, "Active")) {
601                         dbus_bool_t active;
602
603                         dbus_message_iter_get_basic(&value, &active);
604                         info->active = active;
605
606                         DBG("%s Active %d", info->path, info->active);
607                 } else if (g_str_equal(key, "Settings")) {
608                         DBG("%s Settings", info->path);
609
610                         extract_settings(&value, info);
611                 } else if (g_str_equal(key, "Name")) {
612                         char *name;
613
614                         dbus_message_iter_get_basic(&value, &name);
615
616                         info->name = g_strdup(name);
617
618                         DBG("%s Name %s", info->path, info->name);
619                 }
620
621                 dbus_message_iter_next(properties);
622         }
623
624         g_hash_table_insert(dundee_devices, g_strdup(path), info);
625
626         err = create_device(info);
627         if (err < 0)
628                 goto out;
629
630         err = create_network(info);
631         if (err < 0) {
632                 destroy_device(info);
633                 goto out;
634         }
635
636         if (info->active)
637                 set_connected(info);
638
639         return;
640
641 out:
642         g_hash_table_remove(dundee_devices, path);
643 }
644
645 static gboolean device_added(DBusConnection *conn, DBusMessage *message,
646                                 void *user_data)
647 {
648         DBusMessageIter iter, properties;
649         const char *path;
650         const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING
651                 DBUS_TYPE_ARRAY_AS_STRING
652                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
653                 DBUS_TYPE_STRING_AS_STRING
654                 DBUS_TYPE_VARIANT_AS_STRING
655                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
656
657         if (!dbus_message_has_signature(message, signature)) {
658                 connman_error("dundee signature does not match");
659                 return TRUE;
660         }
661
662         DBG("");
663
664         if (!dbus_message_iter_init(message, &iter))
665                 return TRUE;
666
667         dbus_message_iter_get_basic(&iter, &path);
668
669         dbus_message_iter_next(&iter);
670         dbus_message_iter_recurse(&iter, &properties);
671
672         add_device(path, &properties);
673
674         return TRUE;
675 }
676
677 static void remove_device(DBusConnection *conn, const char *path)
678 {
679         DBG("path %s", path);
680
681         g_hash_table_remove(dundee_devices, path);
682 }
683
684 static gboolean device_removed(DBusConnection *conn, DBusMessage *message,
685                                 void *user_data)
686 {
687         const char *path;
688         const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
689
690         if (!dbus_message_has_signature(message, signature)) {
691                 connman_error("dundee signature does not match");
692                 return TRUE;
693         }
694
695         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
696                                 DBUS_TYPE_INVALID);
697         remove_device(conn, path);
698         return TRUE;
699 }
700
701 static void manager_get_devices_reply(DBusPendingCall *call, void *user_data)
702 {
703         DBusMessage *reply;
704         DBusError error;
705         DBusMessageIter array, dict;
706         const char *signature = DBUS_TYPE_ARRAY_AS_STRING
707                 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
708                 DBUS_TYPE_OBJECT_PATH_AS_STRING
709                 DBUS_TYPE_ARRAY_AS_STRING
710                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
711                 DBUS_TYPE_STRING_AS_STRING
712                 DBUS_TYPE_VARIANT_AS_STRING
713                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
714                 DBUS_STRUCT_END_CHAR_AS_STRING;
715
716         DBG("");
717
718         reply = dbus_pending_call_steal_reply(call);
719
720         if (!dbus_message_has_signature(reply, signature)) {
721                 connman_error("dundee signature does not match");
722                 goto done;
723         }
724
725         dbus_error_init(&error);
726
727         if (dbus_set_error_from_message(&error, reply)) {
728                 connman_error("%s", error.message);
729                 dbus_error_free(&error);
730                 goto done;
731         }
732
733         if (!dbus_message_iter_init(reply, &array))
734                 goto done;
735
736         dbus_message_iter_recurse(&array, &dict);
737
738         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
739                 DBusMessageIter value, properties;
740                 const char *path;
741
742                 dbus_message_iter_recurse(&dict, &value);
743                 dbus_message_iter_get_basic(&value, &path);
744
745                 dbus_message_iter_next(&value);
746                 dbus_message_iter_recurse(&value, &properties);
747
748                 add_device(path, &properties);
749
750                 dbus_message_iter_next(&dict);
751         }
752
753 done:
754         dbus_message_unref(reply);
755
756         dbus_pending_call_unref(call);
757 }
758
759 static int manager_get_devices(void)
760 {
761         DBusMessage *message;
762         DBusPendingCall *call;
763
764         DBG("");
765
766         message = dbus_message_new_method_call(DUNDEE_SERVICE, "/",
767                                         DUNDEE_MANAGER_INTERFACE, GET_DEVICES);
768         if (!message)
769                 return -ENOMEM;
770
771         if (!dbus_connection_send_with_reply(connection, message,
772                                                 &call, TIMEOUT)) {
773                 connman_error("Failed to call GetDevices()");
774                 dbus_message_unref(message);
775                 return -EINVAL;
776         }
777
778         if (!call) {
779                 connman_error("D-Bus connection not available");
780                 dbus_message_unref(message);
781                 return -EINVAL;
782         }
783
784         dbus_pending_call_set_notify(call, manager_get_devices_reply,
785                                         NULL, NULL);
786
787         dbus_message_unref(message);
788
789         return -EINPROGRESS;
790 }
791
792 static void dundee_connect(DBusConnection *conn, void *user_data)
793 {
794         DBG("connection %p", conn);
795
796         dundee_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
797                                         g_free, device_destroy);
798
799         manager_get_devices();
800 }
801
802 static void dundee_disconnect(DBusConnection *conn, void *user_data)
803 {
804         DBG("connection %p", conn);
805
806         g_hash_table_destroy(dundee_devices);
807         dundee_devices = NULL;
808 }
809
810 static guint watch;
811 static guint added_watch;
812 static guint removed_watch;
813 static guint device_watch;
814
815 static int dundee_init(void)
816 {
817         int err;
818
819         connection = connman_dbus_get_connection();
820         if (!connection)
821                 return -EIO;
822
823         watch = g_dbus_add_service_watch(connection, DUNDEE_SERVICE,
824                         dundee_connect, dundee_disconnect, NULL, NULL);
825
826         added_watch = g_dbus_add_signal_watch(connection, DUNDEE_SERVICE, NULL,
827                                                 DUNDEE_MANAGER_INTERFACE,
828                                                 DEVICE_ADDED, device_added,
829                                                 NULL, NULL);
830
831         removed_watch = g_dbus_add_signal_watch(connection, DUNDEE_SERVICE,
832                                                 NULL, DUNDEE_MANAGER_INTERFACE,
833                                                 DEVICE_REMOVED, device_removed,
834                                                 NULL, NULL);
835
836         device_watch = g_dbus_add_signal_watch(connection, DUNDEE_SERVICE,
837                                                 NULL, DUNDEE_DEVICE_INTERFACE,
838                                                 PROPERTY_CHANGED,
839                                                 device_changed,
840                                                 NULL, NULL);
841
842
843         if (watch == 0 || added_watch == 0 || removed_watch == 0 ||
844                         device_watch == 0) {
845                 err = -EIO;
846                 goto remove;
847         }
848
849         err = connman_network_driver_register(&network_driver);
850         if (err < 0)
851                 goto remove;
852
853         err = connman_device_driver_register(&dundee_driver);
854         if (err < 0) {
855                 connman_network_driver_unregister(&network_driver);
856                 goto remove;
857         }
858
859         return 0;
860
861 remove:
862         g_dbus_remove_watch(connection, watch);
863         g_dbus_remove_watch(connection, added_watch);
864         g_dbus_remove_watch(connection, removed_watch);
865         g_dbus_remove_watch(connection, device_watch);
866
867         dbus_connection_unref(connection);
868
869         return err;
870 }
871
872 static void dundee_exit(void)
873 {
874         g_dbus_remove_watch(connection, watch);
875         g_dbus_remove_watch(connection, added_watch);
876         g_dbus_remove_watch(connection, removed_watch);
877         g_dbus_remove_watch(connection, device_watch);
878
879         connman_device_driver_unregister(&dundee_driver);
880         connman_network_driver_unregister(&network_driver);
881
882         dbus_connection_unref(connection);
883 }
884
885 CONNMAN_PLUGIN_DEFINE(dundee, "Dundee plugin", VERSION,
886                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, dundee_init, dundee_exit)