Fix memory leaks caused by missing dbus_pending_call_unref()
[framework/connectivity/connman.git] / plugins / bluetooth.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
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 <stdio.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <netinet/ether.h>
31
32 #include <gdbus.h>
33
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/plugin.h>
36 #include <connman/device.h>
37 #include <connman/inet.h>
38 #include <connman/dbus.h>
39 #include <connman/log.h>
40
41 #define BLUEZ_SERVICE                   "org.bluez"
42 #define BLUEZ_MANAGER_INTERFACE         BLUEZ_SERVICE ".Manager"
43 #define BLUEZ_ADAPTER_INTERFACE         BLUEZ_SERVICE ".Adapter"
44 #define BLUEZ_DEVICE_INTERFACE          BLUEZ_SERVICE ".Device"
45 #define BLUEZ_NETWORK_INTERFACE         BLUEZ_SERVICE ".Network"
46
47 #define LIST_ADAPTERS                   "ListAdapters"
48 #define ADAPTER_ADDED                   "AdapterAdded"
49 #define ADAPTER_REMOVED                 "AdapterRemoved"
50
51 #define PROPERTY_CHANGED                "PropertyChanged"
52 #define GET_PROPERTIES                  "GetProperties"
53 #define SET_PROPERTY                    "SetProperty"
54
55 #define CONNECT                         "Connect"
56 #define DISCONNECT                      "Disconnect"
57
58 #define UUID_NAP        "00001116-0000-1000-8000-00805f9b34fb"
59
60 #define TIMEOUT 5000
61
62 static DBusConnection *connection;
63
64 static GHashTable *bluetooth_devices = NULL;
65
66 static int pan_probe(struct connman_network *network)
67 {
68         DBG("network %p", network);
69
70         return 0;
71 }
72
73 static void pan_remove(struct connman_network *network)
74 {
75         DBG("network %p", network);
76 }
77
78 static void connect_reply(DBusPendingCall *call, void *user_data)
79 {
80         struct connman_network *network = user_data;
81         DBusMessage *reply;
82         DBusError error;
83         const char *interface = NULL;
84         int index;
85
86         DBG("network %p", network);
87
88         reply = dbus_pending_call_steal_reply(call);
89
90         dbus_error_init(&error);
91
92         if (dbus_message_get_args(reply, &error,
93                                         DBUS_TYPE_STRING, &interface,
94                                                 DBUS_TYPE_INVALID) == FALSE) {
95                 if (dbus_error_is_set(&error) == TRUE) {
96                         connman_error("%s", error.message);
97                         dbus_error_free(&error);
98                 } else
99                         connman_error("Wrong arguments for connect");
100                 goto done;
101         }
102
103         if (interface == NULL)
104                 goto done;
105
106         DBG("interface %s", interface);
107
108         index = connman_inet_ifindex(interface);
109
110         connman_network_set_index(network, index);
111
112         connman_network_set_method(network, CONNMAN_IPCONFIG_METHOD_DHCP);
113
114         connman_network_set_connected(network, TRUE);
115
116 done:
117         dbus_message_unref(reply);
118
119         dbus_pending_call_unref(call);
120 }
121
122 static int pan_connect(struct connman_network *network)
123 {
124         const char *path = connman_network_get_string(network, "Path");
125         const char *uuid = "nap";
126         DBusMessage *message;
127         DBusPendingCall *call;
128
129         DBG("network %p", network);
130
131         if (path == NULL)
132                 return -EINVAL;
133
134         if (connman_network_get_index(network) >= 0)
135                 return -EISCONN;
136
137         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
138                                         BLUEZ_NETWORK_INTERFACE, CONNECT);
139         if (message == NULL)
140                 return -ENOMEM;
141
142         dbus_message_set_auto_start(message, FALSE);
143
144         dbus_message_append_args(message, DBUS_TYPE_STRING, &uuid,
145                                                         DBUS_TYPE_INVALID);
146
147         if (dbus_connection_send_with_reply(connection, message,
148                                         &call, TIMEOUT * 10) == FALSE) {
149                 connman_error("Failed to connect service");
150                 dbus_message_unref(message);
151                 return -EINVAL;
152         }
153
154         if (call == NULL) {
155                 connman_error("D-Bus connection not available");
156                 dbus_message_unref(message);
157                 return -EINVAL;
158         }
159
160         dbus_pending_call_set_notify(call, connect_reply, network, NULL);
161
162         dbus_message_unref(message);
163
164         return -EINPROGRESS;
165 }
166
167 static void disconnect_reply(DBusPendingCall *call, void *user_data)
168 {
169         struct connman_network *network = user_data;
170         DBusMessage *reply;
171         DBusError error;
172
173         DBG("network %p", network);
174
175         reply = dbus_pending_call_steal_reply(call);
176
177         dbus_error_init(&error);
178
179         if (dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) == FALSE) {
180                 if (dbus_error_is_set(&error) == TRUE) {
181                         connman_error("%s", error.message);
182                         dbus_error_free(&error);
183                 } else
184                         connman_error("Wrong arguments for disconnect");
185                 goto done;
186         }
187
188         connman_network_set_connected(network, FALSE);
189         connman_network_set_index(network, -1);
190
191 done:
192         dbus_message_unref(reply);
193
194         dbus_pending_call_unref(call);
195 }
196
197 static int pan_disconnect(struct connman_network *network)
198 {
199         const char *path = connman_network_get_string(network, "Path");
200         DBusMessage *message;
201         DBusPendingCall *call;
202
203         DBG("network %p", network);
204
205         if (path == NULL)
206                 return -EINVAL;
207
208         if (connman_network_get_index(network) < 0)
209                 return -ENOTCONN;
210
211         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
212                                         BLUEZ_NETWORK_INTERFACE, DISCONNECT);
213         if (message == NULL)
214                 return -ENOMEM;
215
216         dbus_message_set_auto_start(message, FALSE);
217
218         dbus_message_append_args(message, DBUS_TYPE_INVALID);
219
220         if (dbus_connection_send_with_reply(connection, message,
221                                                 &call, TIMEOUT) == FALSE) {
222                 connman_error("Failed to disconnect service");
223                 dbus_message_unref(message);
224                 return -EINVAL;
225         }
226
227         if (call == NULL) {
228                 connman_error("D-Bus connection not available");
229                 dbus_message_unref(message);
230                 return -EINVAL;
231         }
232
233         dbus_pending_call_set_notify(call, disconnect_reply, network, NULL);
234
235         dbus_message_unref(message);
236
237         return 0;
238 }
239
240 static struct connman_network_driver pan_driver = {
241         .name           = "bluetooth-pan",
242         .type           = CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN,
243         .probe          = pan_probe,
244         .remove         = pan_remove,
245         .connect        = pan_connect,
246         .disconnect     = pan_disconnect,
247 };
248
249 static int bluetooth_probe(struct connman_device *device)
250 {
251         DBG("device %p", device);
252
253         return 0;
254 }
255
256 static void bluetooth_remove(struct connman_device *device)
257 {
258         DBG("device %p", device);
259 }
260
261 static void powered_reply(DBusPendingCall *call, void *user_data)
262 {
263         DBusMessage *reply;
264
265         DBG("");
266
267         reply = dbus_pending_call_steal_reply(call);
268
269         dbus_message_unref(reply);
270
271         dbus_pending_call_unref(call);
272 }
273
274 static int change_powered(DBusConnection *connection, const char *path,
275                                                         dbus_bool_t powered)
276 {
277         DBusMessage *message;
278         DBusMessageIter iter;
279         DBusPendingCall *call;
280
281         DBG("");
282
283         if (path == NULL)
284                 return -EINVAL;
285
286         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
287                                         BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY);
288         if (message == NULL)
289                 return -ENOMEM;
290
291         dbus_message_set_auto_start(message, FALSE);
292
293         dbus_message_iter_init_append(message, &iter);
294         connman_dbus_property_append_basic(&iter, "Powered",
295                                                 DBUS_TYPE_BOOLEAN, &powered);
296
297         if (dbus_connection_send_with_reply(connection, message,
298                                                 &call, TIMEOUT) == FALSE) {
299                 connman_error("Failed to change Powered property");
300                 dbus_message_unref(message);
301                 return -EINVAL;
302         }
303
304         if (call == NULL) {
305                 connman_error("D-Bus connection not available");
306                 dbus_message_unref(message);
307                 return -EINVAL;
308         }
309
310         dbus_pending_call_set_notify(call, powered_reply, NULL, NULL);
311
312         dbus_message_unref(message);
313
314         return -EINPROGRESS;
315 }
316
317 static int bluetooth_enable(struct connman_device *device)
318 {
319         const char *path = connman_device_get_string(device, "Path");
320
321         DBG("device %p", device);
322
323         return change_powered(connection, path, TRUE);
324 }
325
326 static int bluetooth_disable(struct connman_device *device)
327 {
328         const char *path = connman_device_get_string(device, "Path");
329
330         DBG("device %p", device);
331
332         return change_powered(connection, path, FALSE);
333 }
334
335 static struct connman_device_driver bluetooth_driver = {
336         .name           = "bluetooth",
337         .type           = CONNMAN_DEVICE_TYPE_BLUETOOTH,
338         .probe          = bluetooth_probe,
339         .remove         = bluetooth_remove,
340         .enable         = bluetooth_enable,
341         .disable        = bluetooth_disable,
342 };
343
344 static void extract_properties(DBusMessage *reply, const char **parent,
345                                                 const char **address,
346                                                 const char **name,
347                                                 const char **alias,
348                                                 dbus_bool_t *powered,
349                                                 dbus_bool_t *scanning,
350                                                 DBusMessageIter *uuids,
351                                                 DBusMessageIter *networks)
352 {
353         DBusMessageIter array, dict;
354
355         if (dbus_message_iter_init(reply, &array) == FALSE)
356                 return;
357
358         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
359                 return;
360
361         dbus_message_iter_recurse(&array, &dict);
362
363         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
364                 DBusMessageIter entry, value;
365                 const char *key;
366
367                 dbus_message_iter_recurse(&dict, &entry);
368                 dbus_message_iter_get_basic(&entry, &key);
369
370                 dbus_message_iter_next(&entry);
371                 dbus_message_iter_recurse(&entry, &value);
372
373                 if (g_str_equal(key, "Adapter") == TRUE) {
374                         if (parent != NULL)
375                                 dbus_message_iter_get_basic(&value, parent);
376                 } else if (g_str_equal(key, "Address") == TRUE) {
377                         if (address != NULL)
378                                 dbus_message_iter_get_basic(&value, address);
379                 } else if (g_str_equal(key, "Name") == TRUE) {
380                         if (name != NULL)
381                                 dbus_message_iter_get_basic(&value, name);
382                 } else if (g_str_equal(key, "Alias") == TRUE) {
383                         if (alias != NULL)
384                                 dbus_message_iter_get_basic(&value, alias);
385                 } else if (g_str_equal(key, "Powered") == TRUE) {
386                         if (powered != NULL)
387                                 dbus_message_iter_get_basic(&value, powered);
388                 } else if (g_str_equal(key, "Discovering") == TRUE) {
389                         if (scanning != NULL)
390                                 dbus_message_iter_get_basic(&value, scanning);
391                 } else if (g_str_equal(key, "Devices") == TRUE) {
392                         if (networks != NULL)
393                                 memcpy(networks, &value, sizeof(value));
394                 } else if (g_str_equal(key, "UUIDs") == TRUE) {
395                         if (uuids != NULL)
396                                 memcpy(uuids, &value, sizeof(value));
397                 }
398
399                 dbus_message_iter_next(&dict);
400         }
401 }
402
403 static dbus_bool_t has_pan(DBusMessageIter *array)
404 {
405         DBusMessageIter value;
406
407         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
408                 return FALSE;
409
410         dbus_message_iter_recurse(array, &value);
411
412         while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
413                 const char *uuid;
414
415                 dbus_message_iter_get_basic(&value, &uuid);
416
417                 if (g_strcmp0(uuid, UUID_NAP) == 0)
418                         return TRUE;
419
420                 dbus_message_iter_next(&value);
421         }
422
423         return FALSE;
424 }
425
426 static void network_properties_reply(DBusPendingCall *call, void *user_data)
427 {
428         char *path = user_data;
429         struct connman_device *device;
430         struct connman_network *network;
431         DBusMessage *reply;
432         DBusMessageIter uuids;
433         const char *parent = NULL, *address = NULL, *name = NULL;
434         struct ether_addr addr;
435         char ident[13];
436
437         reply = dbus_pending_call_steal_reply(call);
438
439         extract_properties(reply, &parent, &address, NULL, &name,
440                                                 NULL, NULL, &uuids, NULL);
441
442         if (parent == NULL)
443                 goto done;
444
445         device = g_hash_table_lookup(bluetooth_devices, parent);
446         if (device == NULL)
447                 goto done;
448
449         if (address == NULL)
450                 goto done;
451
452         ether_aton_r(address, &addr);
453
454         snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
455                                                 addr.ether_addr_octet[0],
456                                                 addr.ether_addr_octet[1],
457                                                 addr.ether_addr_octet[2],
458                                                 addr.ether_addr_octet[3],
459                                                 addr.ether_addr_octet[4],
460                                                 addr.ether_addr_octet[5]);
461
462         if (has_pan(&uuids) == FALSE)
463                 goto done;
464
465         network = connman_device_get_network(device, ident);
466         if (network != NULL)
467                 goto done;
468
469         network = connman_network_create(ident,
470                                         CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
471         if (network == NULL)
472                 goto done;
473
474         connman_network_set_string(network, "Path", path);
475
476         connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
477
478         connman_network_set_name(network, name);
479
480         connman_device_add_network(device, network);
481
482         connman_network_set_group(network, ident);
483
484 done:
485         dbus_message_unref(reply);
486
487         dbus_pending_call_unref(call);
488 }
489
490 static void add_network(struct connman_device *device, const char *path)
491 {
492         DBusMessage *message;
493         DBusPendingCall *call;
494
495         DBG("path %s", path);
496
497         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
498                                 BLUEZ_DEVICE_INTERFACE, GET_PROPERTIES);
499         if (message == NULL)
500                 return;
501
502         dbus_message_set_auto_start(message, FALSE);
503
504         if (dbus_connection_send_with_reply(connection, message,
505                                                 &call, TIMEOUT) == FALSE) {
506                 connman_error("Failed to get network properties for %s", path);
507                 goto done;
508         }
509
510         if (call == NULL) {
511                 connman_error("D-Bus connection not available");
512                 goto done;
513         }
514
515         dbus_pending_call_set_notify(call, network_properties_reply,
516                                                 g_strdup(path), g_free);
517
518 done:
519         dbus_message_unref(message);
520 }
521
522 static void check_networks(struct connman_device *device,
523                                                 DBusMessageIter *array)
524 {
525         DBusMessageIter value;
526
527         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
528                 return;
529
530         dbus_message_iter_recurse(array, &value);
531
532         while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
533                 const char *path;
534
535                 dbus_message_iter_get_basic(&value, &path);
536
537                 add_network(device, path);
538
539                 dbus_message_iter_next(&value);
540         }
541 }
542
543 static gboolean adapter_changed(DBusConnection *connection,
544                                 DBusMessage *message, void *user_data)
545 {
546         const char *path = dbus_message_get_path(message);
547         struct connman_device *device;
548         DBusMessageIter iter, value;
549         const char *key;
550
551         DBG("path %s", path);
552
553         device = g_hash_table_lookup(bluetooth_devices, path);
554         if (device == NULL)
555                 return TRUE;
556
557         if (dbus_message_iter_init(message, &iter) == FALSE)
558                 return TRUE;
559
560         dbus_message_iter_get_basic(&iter, &key);
561
562         dbus_message_iter_next(&iter);
563         dbus_message_iter_recurse(&iter, &value);
564
565         if (g_str_equal(key, "Powered") == TRUE) {
566                 dbus_bool_t val;
567
568                 dbus_message_iter_get_basic(&value, &val);
569                 connman_device_set_powered(device, val);
570         } else if (g_str_equal(key, "Discovering") == TRUE) {
571                 dbus_bool_t val;
572
573                 dbus_message_iter_get_basic(&value, &val);
574                 connman_device_set_scanning(device, val);
575         } else if (g_str_equal(key, "Devices") == TRUE) {
576                 check_networks(device, &value);
577         }
578
579         return TRUE;
580 }
581
582 static void adapter_properties_reply(DBusPendingCall *call, void *user_data)
583 {
584         char *path = user_data;
585         struct connman_device *device;
586         DBusMessage *reply;
587         DBusMessageIter networks;
588         const char *address = NULL, *name = NULL;
589         dbus_bool_t powered = FALSE, scanning = FALSE;
590         struct ether_addr addr;
591         char ident[13];
592
593         DBG("path %s", path);
594
595         reply = dbus_pending_call_steal_reply(call);
596
597         if (path == NULL)
598                 goto done;
599
600         extract_properties(reply, NULL, &address, &name, NULL,
601                                         &powered, &scanning, NULL, &networks);
602
603         if (address == NULL)
604                 goto done;
605
606         device = g_hash_table_lookup(bluetooth_devices, path);
607         if (device != NULL)
608                 goto update;
609
610         ether_aton_r(address, &addr);
611
612         snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
613                                                 addr.ether_addr_octet[0],
614                                                 addr.ether_addr_octet[1],
615                                                 addr.ether_addr_octet[2],
616                                                 addr.ether_addr_octet[3],
617                                                 addr.ether_addr_octet[4],
618                                                 addr.ether_addr_octet[5]);
619
620         device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_BLUETOOTH);
621         if (device == NULL)
622                 goto done;
623
624         connman_device_set_ident(device, ident);
625
626         connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE);
627
628         if (connman_device_register(device) < 0) {
629                 connman_device_unref(device);
630                 goto done;
631         }
632
633         g_hash_table_insert(bluetooth_devices, g_strdup(path), device);
634
635 update:
636         connman_device_set_string(device, "Address", address);
637         connman_device_set_string(device, "Name", name);
638         connman_device_set_string(device, "Path", path);
639
640         connman_device_set_powered(device, powered);
641         connman_device_set_scanning(device, scanning);
642
643         if (powered == TRUE)
644                 check_networks(device, &networks);
645
646 done:
647         dbus_message_unref(reply);
648
649         dbus_pending_call_unref(call);
650 }
651
652 static void add_adapter(DBusConnection *connection, const char *path)
653 {
654         DBusMessage *message;
655         DBusPendingCall *call;
656
657         DBG("path %s", path);
658
659         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
660                                 BLUEZ_ADAPTER_INTERFACE, GET_PROPERTIES);
661         if (message == NULL)
662                 return;
663
664         dbus_message_set_auto_start(message, FALSE);
665
666         if (dbus_connection_send_with_reply(connection, message,
667                                                 &call, TIMEOUT) == FALSE) {
668                 connman_error("Failed to get adapter properties for %s", path);
669                 goto done;
670         }
671
672         if (call == NULL) {
673                 connman_error("D-Bus connection not available");
674                 goto done;
675         }
676
677         dbus_pending_call_set_notify(call, adapter_properties_reply,
678                                                 g_strdup(path), g_free);
679
680 done:
681         dbus_message_unref(message);
682 }
683
684 static gboolean adapter_added(DBusConnection *connection, DBusMessage *message,
685                                 void *user_data)
686 {
687         const char *path;
688
689         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
690                                 DBUS_TYPE_INVALID);
691         add_adapter(connection, path);
692         return TRUE;
693 }
694
695 static void remove_adapter(DBusConnection *connection, const char *path)
696 {
697         DBG("path %s", path);
698
699         g_hash_table_remove(bluetooth_devices, path);
700 }
701
702 static gboolean adapter_removed(DBusConnection *connection, DBusMessage *message,
703                                 void *user_data)
704 {
705         const char *path;
706
707         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
708                                 DBUS_TYPE_INVALID);
709         remove_adapter(connection, path);
710         return TRUE;
711 }
712
713 static void list_adapters_reply(DBusPendingCall *call, void *user_data)
714 {
715         DBusMessage *reply;
716         DBusError error;
717         char **adapters;
718         int i, num_adapters;
719
720         DBG("");
721
722         reply = dbus_pending_call_steal_reply(call);
723
724         dbus_error_init(&error);
725
726         if (dbus_message_get_args(reply, &error,
727                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
728                                                 &adapters, &num_adapters,
729                                                 DBUS_TYPE_INVALID) == FALSE) {
730                 if (dbus_error_is_set(&error) == TRUE) {
731                         connman_error("%s", error.message);
732                         dbus_error_free(&error);
733                 } else
734                         connman_error("Wrong arguments for adapter list");
735                 goto done;
736         }
737
738         for (i = 0; i < num_adapters; i++)
739                 add_adapter(connection, adapters[i]);
740
741         g_strfreev(adapters);
742
743 done:
744         dbus_message_unref(reply);
745
746         dbus_pending_call_unref(call);
747 }
748
749 static void unregister_device(gpointer data)
750 {
751         struct connman_device *device = data;
752
753         DBG("");
754
755         connman_device_unregister(device);
756         connman_device_unref(device);
757 }
758
759 static void bluetooth_connect(DBusConnection *connection, void *user_data)
760 {
761         DBusMessage *message;
762         DBusPendingCall *call;
763
764         DBG("connection %p", connection);
765
766         bluetooth_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
767                                                 g_free, unregister_device);
768
769         message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
770                                 BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS);
771         if (message == NULL)
772                 return;
773
774         dbus_message_set_auto_start(message, FALSE);
775
776         if (dbus_connection_send_with_reply(connection, message,
777                                                 &call, TIMEOUT) == FALSE) {
778                 connman_error("Failed to get Bluetooth adapters");
779                 goto done;
780         }
781
782         if (call == NULL) {
783                 connman_error("D-Bus connection not available");
784                 goto done;
785         }
786
787         dbus_pending_call_set_notify(call, list_adapters_reply, NULL, NULL);
788
789 done:
790         dbus_message_unref(message);
791 }
792
793 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
794 {
795         DBG("connection %p", connection);
796
797         if (bluetooth_devices == NULL)
798                 return;
799
800         g_hash_table_destroy(bluetooth_devices);
801         bluetooth_devices = NULL;
802 }
803
804 static guint watch;
805 static guint added_watch;
806 static guint removed_watch;
807 static guint adapter_watch;
808
809 static int bluetooth_init(void)
810 {
811         int err;
812
813         connection = connman_dbus_get_connection();
814         if (connection == NULL)
815                 return -EIO;
816
817         watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
818                         bluetooth_connect, bluetooth_disconnect, NULL, NULL);
819
820         added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
821                                                 BLUEZ_MANAGER_INTERFACE,
822                                                 ADAPTER_ADDED, adapter_added,
823                                                 NULL, NULL);
824
825         removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
826                                                 BLUEZ_MANAGER_INTERFACE,
827                                                 ADAPTER_REMOVED, adapter_removed,
828                                                 NULL, NULL);
829
830         adapter_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
831                                                 BLUEZ_MANAGER_INTERFACE,
832                                                 PROPERTY_CHANGED, adapter_changed,
833                                                 NULL, NULL);
834
835         if (watch == 0 || added_watch == 0 || removed_watch == 0
836                         || adapter_watch == 0) {
837                 err = -EIO;
838                 goto remove;
839         }
840
841         err = connman_network_driver_register(&pan_driver);
842         if (err < 0)
843                 goto remove;
844
845         err = connman_device_driver_register(&bluetooth_driver);
846         if (err < 0) {
847                 connman_network_driver_unregister(&pan_driver);
848                 goto remove;
849         }
850
851         return 0;
852
853 remove:
854         g_dbus_remove_watch(connection, watch);
855         g_dbus_remove_watch(connection, added_watch);
856         g_dbus_remove_watch(connection, removed_watch);
857         g_dbus_remove_watch(connection, adapter_watch);
858
859         dbus_connection_unref(connection);
860
861         return err;
862 }
863
864 static void bluetooth_exit(void)
865 {
866         g_dbus_remove_watch(connection, watch);
867         g_dbus_remove_watch(connection, added_watch);
868         g_dbus_remove_watch(connection, removed_watch);
869         g_dbus_remove_watch(connection, adapter_watch);
870
871         bluetooth_disconnect(connection, NULL);
872
873         connman_device_driver_unregister(&bluetooth_driver);
874         connman_network_driver_unregister(&pan_driver);
875
876         dbus_connection_unref(connection);
877 }
878
879 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
880                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, bluetooth_init, bluetooth_exit)