d112f8764c289d3e09c1804785ac15a1ebe5481e
[framework/connectivity/connman.git] / plugins / bluetooth.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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/technology.h>
37 #include <connman/device.h>
38 #include <connman/inet.h>
39 #include <connman/dbus.h>
40 #include <connman/log.h>
41
42 #define BLUEZ_SERVICE                   "org.bluez"
43 #define BLUEZ_MANAGER_INTERFACE         BLUEZ_SERVICE ".Manager"
44 #define BLUEZ_ADAPTER_INTERFACE         BLUEZ_SERVICE ".Adapter"
45 #define BLUEZ_DEVICE_INTERFACE          BLUEZ_SERVICE ".Device"
46 #define BLUEZ_NETWORK_INTERFACE         BLUEZ_SERVICE ".Network"
47 #define BLUEZ_NETWORK_SERVER            BLUEZ_SERVICE ".NetworkServer"
48
49 #define LIST_ADAPTERS                   "ListAdapters"
50 #define ADAPTER_ADDED                   "AdapterAdded"
51 #define ADAPTER_REMOVED                 "AdapterRemoved"
52 #define DEVICE_REMOVED                  "DeviceRemoved"
53
54 #define PROPERTY_CHANGED                "PropertyChanged"
55 #define GET_PROPERTIES                  "GetProperties"
56 #define SET_PROPERTY                    "SetProperty"
57
58 #define CONNECT                         "Connect"
59 #define DISCONNECT                      "Disconnect"
60
61 #define REGISTER                        "Register"
62 #define UNREGISTER                      "Unregister"
63
64 #define UUID_NAP        "00001116-0000-1000-8000-00805f9b34fb"
65
66 #define TIMEOUT 5000
67
68 static DBusConnection *connection;
69
70 static GHashTable *bluetooth_devices = NULL;
71 static GHashTable *bluetooth_networks = NULL;
72
73 static int pan_probe(struct connman_network *network)
74 {
75         DBG("network %p", network);
76
77         return 0;
78 }
79
80 static void pan_remove(struct connman_network *network)
81 {
82         DBG("network %p", network);
83 }
84
85 static void connect_reply(DBusPendingCall *call, void *user_data)
86 {
87         struct connman_network *network = user_data;
88         DBusMessage *reply;
89         DBusError error;
90         const char *interface = NULL;
91         int index;
92
93         DBG("network %p", network);
94
95         reply = dbus_pending_call_steal_reply(call);
96
97         dbus_error_init(&error);
98
99         if (dbus_set_error_from_message(&error, reply) == TRUE) {
100                 connman_error("%s", error.message);
101                 dbus_error_free(&error);
102
103                 goto err;
104         }
105
106         if (dbus_message_get_args(reply, &error,
107                                         DBUS_TYPE_STRING, &interface,
108                                                 DBUS_TYPE_INVALID) == FALSE) {
109                 if (dbus_error_is_set(&error) == TRUE) {
110                         connman_error("%s", error.message);
111                         dbus_error_free(&error);
112                 } else
113                         connman_error("Wrong arguments for connect");
114                 goto err;
115         }
116
117         if (interface == NULL)
118                 goto err;
119
120         DBG("interface %s", interface);
121
122         index = connman_inet_ifindex(interface);
123
124         connman_network_set_index(network, index);
125
126         connman_network_set_connected(network, TRUE);
127
128         dbus_message_unref(reply);
129
130         dbus_pending_call_unref(call);
131
132         return;
133 err:
134
135         connman_network_set_connected(network, FALSE);
136
137         dbus_message_unref(reply);
138
139         dbus_pending_call_unref(call);
140 }
141
142 static int pan_connect(struct connman_network *network)
143 {
144         const char *path = connman_network_get_string(network, "Path");
145         const char *uuid = "nap";
146         DBusMessage *message;
147         DBusPendingCall *call;
148
149         DBG("network %p", network);
150
151         if (path == NULL)
152                 return -EINVAL;
153
154         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
155                                         BLUEZ_NETWORK_INTERFACE, CONNECT);
156         if (message == NULL)
157                 return -ENOMEM;
158
159         dbus_message_set_auto_start(message, FALSE);
160
161         dbus_message_append_args(message, DBUS_TYPE_STRING, &uuid,
162                                                         DBUS_TYPE_INVALID);
163
164         if (dbus_connection_send_with_reply(connection, message,
165                                         &call, TIMEOUT * 10) == FALSE) {
166                 connman_error("Failed to connect service");
167                 dbus_message_unref(message);
168                 return -EINVAL;
169         }
170
171         if (call == NULL) {
172                 connman_error("D-Bus connection not available");
173                 dbus_message_unref(message);
174                 return -EINVAL;
175         }
176
177         dbus_pending_call_set_notify(call, connect_reply, network, NULL);
178
179         dbus_message_unref(message);
180
181         return -EINPROGRESS;
182 }
183
184 static void disconnect_reply(DBusPendingCall *call, void *user_data)
185 {
186         struct connman_network *network = user_data;
187         DBusMessage *reply;
188         DBusError error;
189
190         DBG("network %p", network);
191
192         reply = dbus_pending_call_steal_reply(call);
193
194         dbus_error_init(&error);
195
196         if (dbus_set_error_from_message(&error, reply) == TRUE) {
197                 connman_error("%s", error.message);
198                 dbus_error_free(&error);
199                 goto done;
200         }
201
202         if (dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) == FALSE) {
203                 if (dbus_error_is_set(&error) == TRUE) {
204                         connman_error("%s", error.message);
205                         dbus_error_free(&error);
206                 } else
207                         connman_error("Wrong arguments for disconnect");
208                 goto done;
209         }
210
211         connman_network_set_connected(network, FALSE);
212
213 done:
214         dbus_message_unref(reply);
215
216         dbus_pending_call_unref(call);
217
218         connman_network_unregister(network);
219         connman_network_unref(network);
220 }
221
222 static int pan_disconnect(struct connman_network *network)
223 {
224         const char *path = connman_network_get_string(network, "Path");
225         DBusMessage *message;
226         DBusPendingCall *call;
227
228         DBG("network %p", network);
229
230         if (path == NULL)
231                 return -EINVAL;
232
233         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
234                                         BLUEZ_NETWORK_INTERFACE, DISCONNECT);
235         if (message == NULL)
236                 return -ENOMEM;
237
238         dbus_message_set_auto_start(message, FALSE);
239
240         dbus_message_append_args(message, DBUS_TYPE_INVALID);
241
242         if (dbus_connection_send_with_reply(connection, message,
243                                                 &call, TIMEOUT) == FALSE) {
244                 connman_error("Failed to disconnect service");
245                 dbus_message_unref(message);
246                 return -EINVAL;
247         }
248
249         if (call == NULL) {
250                 connman_error("D-Bus connection not available");
251                 dbus_message_unref(message);
252                 return -EINVAL;
253         }
254
255         connman_network_ref(network);
256
257         connman_network_set_associating(network, FALSE);
258
259         dbus_pending_call_set_notify(call, disconnect_reply, network, NULL);
260
261         dbus_message_unref(message);
262
263         return 0;
264 }
265
266 static struct connman_network_driver pan_driver = {
267         .name           = "bluetooth-pan",
268         .type           = CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN,
269         .probe          = pan_probe,
270         .remove         = pan_remove,
271         .connect        = pan_connect,
272         .disconnect     = pan_disconnect,
273 };
274
275 static gboolean network_changed(DBusConnection *connection,
276                                 DBusMessage *message, void *user_data)
277 {
278         const char *path = dbus_message_get_path(message);
279         struct connman_network *network;
280         DBusMessageIter iter, value;
281         const char *key;
282
283         DBG("path %s", path);
284
285         network = g_hash_table_lookup(bluetooth_networks, path);
286         if (network == NULL)
287                 return TRUE;
288
289         if (dbus_message_iter_init(message, &iter) == FALSE)
290                 return TRUE;
291
292         dbus_message_iter_get_basic(&iter, &key);
293
294         dbus_message_iter_next(&iter);
295         dbus_message_iter_recurse(&iter, &value);
296
297         if (g_str_equal(key, "Connected") == TRUE) {
298                 dbus_bool_t connected;
299
300                 dbus_message_iter_get_basic(&value, &connected);
301
302                 if (connected == TRUE)
303                         return TRUE;
304
305                 connman_network_set_associating(network, FALSE);
306                 connman_network_set_connected(network, FALSE);
307         }
308
309         return TRUE;
310 }
311
312 static void extract_properties(DBusMessage *reply, const char **parent,
313                                                 const char **address,
314                                                 const char **name,
315                                                 const char **alias,
316                                                 dbus_bool_t *powered,
317                                                 dbus_bool_t *scanning,
318                                                 DBusMessageIter *uuids,
319                                                 DBusMessageIter *networks)
320 {
321         DBusMessageIter array, dict;
322
323         if (dbus_message_iter_init(reply, &array) == FALSE)
324                 return;
325
326         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
327                 return;
328
329         dbus_message_iter_recurse(&array, &dict);
330
331         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
332                 DBusMessageIter entry, value;
333                 const char *key;
334
335                 dbus_message_iter_recurse(&dict, &entry);
336                 dbus_message_iter_get_basic(&entry, &key);
337
338                 dbus_message_iter_next(&entry);
339                 dbus_message_iter_recurse(&entry, &value);
340
341                 if (g_str_equal(key, "Adapter") == TRUE) {
342                         if (parent != NULL)
343                                 dbus_message_iter_get_basic(&value, parent);
344                 } else if (g_str_equal(key, "Address") == TRUE) {
345                         if (address != NULL)
346                                 dbus_message_iter_get_basic(&value, address);
347                 } else if (g_str_equal(key, "Name") == TRUE) {
348                         if (name != NULL)
349                                 dbus_message_iter_get_basic(&value, name);
350                 } else if (g_str_equal(key, "Alias") == TRUE) {
351                         if (alias != NULL)
352                                 dbus_message_iter_get_basic(&value, alias);
353                 } else if (g_str_equal(key, "Powered") == TRUE) {
354                         if (powered != NULL)
355                                 dbus_message_iter_get_basic(&value, powered);
356                 } else if (g_str_equal(key, "Discovering") == TRUE) {
357                         if (scanning != NULL)
358                                 dbus_message_iter_get_basic(&value, scanning);
359                 } else if (g_str_equal(key, "Devices") == TRUE) {
360                         if (networks != NULL)
361                                 memcpy(networks, &value, sizeof(value));
362                 } else if (g_str_equal(key, "UUIDs") == TRUE) {
363                         if (uuids != NULL)
364                                 memcpy(uuids, &value, sizeof(value));
365                 }
366
367                 dbus_message_iter_next(&dict);
368         }
369 }
370
371 static dbus_bool_t has_pan(DBusMessageIter *array)
372 {
373         DBusMessageIter value;
374
375         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
376                 return FALSE;
377
378         dbus_message_iter_recurse(array, &value);
379
380         while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
381                 const char *uuid;
382
383                 dbus_message_iter_get_basic(&value, &uuid);
384
385                 if (g_strcmp0(uuid, UUID_NAP) == 0)
386                         return TRUE;
387
388                 dbus_message_iter_next(&value);
389         }
390
391         return FALSE;
392 }
393
394 static void network_properties_reply(DBusPendingCall *call, void *user_data)
395 {
396         char *path = user_data;
397         struct connman_device *device;
398         struct connman_network *network;
399         DBusMessage *reply;
400         DBusMessageIter uuids;
401         const char *parent = NULL, *address = NULL, *name = NULL;
402         struct ether_addr addr;
403         char ident[13];
404
405         reply = dbus_pending_call_steal_reply(call);
406
407         extract_properties(reply, &parent, &address, NULL, &name,
408                                                 NULL, NULL, &uuids, NULL);
409
410         if (parent == NULL)
411                 goto done;
412
413         device = g_hash_table_lookup(bluetooth_devices, parent);
414         if (device == NULL)
415                 goto done;
416
417         if (address == NULL)
418                 goto done;
419
420         ether_aton_r(address, &addr);
421
422         snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
423                                                 addr.ether_addr_octet[0],
424                                                 addr.ether_addr_octet[1],
425                                                 addr.ether_addr_octet[2],
426                                                 addr.ether_addr_octet[3],
427                                                 addr.ether_addr_octet[4],
428                                                 addr.ether_addr_octet[5]);
429
430         if (has_pan(&uuids) == FALSE)
431                 goto done;
432
433         network = connman_device_get_network(device, ident);
434         if (network != NULL)
435                 goto done;
436
437         network = connman_network_create(ident,
438                                         CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
439         if (network == NULL)
440                 goto done;
441
442         connman_network_register(network);
443
444         connman_network_set_string(network, "Path", path);
445
446         connman_network_set_name(network, name);
447
448         connman_device_add_network(device, network);
449
450         connman_network_set_group(network, ident);
451
452         g_hash_table_insert(bluetooth_networks, g_strdup(path), network);
453
454 done:
455         dbus_message_unref(reply);
456
457         dbus_pending_call_unref(call);
458 }
459
460 static void add_network(struct connman_device *device, const char *path)
461 {
462         DBusMessage *message;
463         DBusPendingCall *call;
464
465         DBG("path %s", path);
466
467         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
468                                 BLUEZ_DEVICE_INTERFACE, GET_PROPERTIES);
469         if (message == NULL)
470                 return;
471
472         dbus_message_set_auto_start(message, FALSE);
473
474         if (dbus_connection_send_with_reply(connection, message,
475                                                 &call, TIMEOUT) == FALSE) {
476                 connman_error("Failed to get network properties for %s", path);
477                 goto done;
478         }
479
480         if (call == NULL) {
481                 connman_error("D-Bus connection not available");
482                 goto done;
483         }
484
485         dbus_pending_call_set_notify(call, network_properties_reply,
486                                                 g_strdup(path), g_free);
487
488 done:
489         dbus_message_unref(message);
490 }
491
492 static void check_networks(struct connman_device *device,
493                                                 DBusMessageIter *array)
494 {
495         DBusMessageIter value;
496
497         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
498                 return;
499
500         dbus_message_iter_recurse(array, &value);
501
502         while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
503                 const char *path;
504
505                 dbus_message_iter_get_basic(&value, &path);
506
507                 add_network(device, path);
508
509                 dbus_message_iter_next(&value);
510         }
511 }
512
513 static gboolean adapter_changed(DBusConnection *connection,
514                                 DBusMessage *message, void *user_data)
515 {
516         const char *path = dbus_message_get_path(message);
517         struct connman_device *device;
518         DBusMessageIter iter, value;
519         const char *key;
520
521         DBG("path %s", path);
522
523         device = g_hash_table_lookup(bluetooth_devices, path);
524         if (device == NULL)
525                 return TRUE;
526
527         if (dbus_message_iter_init(message, &iter) == FALSE)
528                 return TRUE;
529
530         dbus_message_iter_get_basic(&iter, &key);
531
532         dbus_message_iter_next(&iter);
533         dbus_message_iter_recurse(&iter, &value);
534
535         if (g_str_equal(key, "Powered") == TRUE) {
536                 dbus_bool_t val;
537
538                 dbus_message_iter_get_basic(&value, &val);
539                 connman_device_set_powered(device, val);
540         } else if (g_str_equal(key, "Discovering") == TRUE) {
541                 dbus_bool_t val;
542
543                 dbus_message_iter_get_basic(&value, &val);
544                 connman_device_set_scanning(device, val);
545         } else if (g_str_equal(key, "Devices") == TRUE) {
546                 check_networks(device, &value);
547         }
548
549         return TRUE;
550 }
551
552 static gboolean device_removed(DBusConnection *connection,
553                                 DBusMessage *message, void *user_data)
554 {
555         const char *network_path, *identifier;
556         struct connman_network *network;
557         struct connman_device *device;
558         DBusMessageIter iter;
559
560         DBG("");
561
562         if (dbus_message_iter_init(message, &iter) == FALSE)
563                 return TRUE;
564
565         dbus_message_iter_get_basic(&iter, &network_path);
566
567         network = g_hash_table_lookup(bluetooth_networks, network_path);
568         if (network == NULL)
569                 return TRUE;
570
571         device = connman_network_get_device(network);
572         if (device == NULL)
573                 return TRUE;
574
575         identifier = connman_network_get_identifier(network);
576
577         g_hash_table_remove(bluetooth_networks, network_path);
578
579         connman_device_remove_network(device, identifier);
580
581         return TRUE;
582 }
583
584 static gboolean device_changed(DBusConnection *connection,
585                                 DBusMessage *message, void *user_data)
586 {
587         const char *path = dbus_message_get_path(message);
588         DBusMessageIter iter, value;
589         const char *key;
590
591         DBG("path %s", path);
592
593         if (dbus_message_iter_init(message, &iter) == FALSE)
594                 return TRUE;
595
596         dbus_message_iter_get_basic(&iter, &key);
597
598         dbus_message_iter_next(&iter);
599         dbus_message_iter_recurse(&iter, &value);
600
601         DBG("key %s", key);
602
603         if (g_str_equal(key, "UUIDs") == TRUE)
604                 add_network(NULL, path);
605
606         return TRUE;
607 }
608
609 static void adapter_properties_reply(DBusPendingCall *call, void *user_data)
610 {
611         char *path = user_data;
612         struct connman_device *device;
613         DBusMessage *reply;
614         DBusMessageIter networks;
615         const char *address = NULL, *name = NULL;
616         dbus_bool_t powered = FALSE, scanning = FALSE;
617         struct ether_addr addr;
618         char ident[13];
619
620         DBG("path %s", path);
621
622         reply = dbus_pending_call_steal_reply(call);
623
624         if (path == NULL)
625                 goto done;
626
627         extract_properties(reply, NULL, &address, &name, NULL,
628                                         &powered, &scanning, NULL, &networks);
629
630         if (address == NULL)
631                 goto done;
632
633         if (g_strcmp0(address, "00:00:00:00:00:00") == 0)
634                 goto done;
635
636         device = g_hash_table_lookup(bluetooth_devices, path);
637         if (device != NULL)
638                 goto update;
639
640         ether_aton_r(address, &addr);
641
642         snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
643                                                 addr.ether_addr_octet[0],
644                                                 addr.ether_addr_octet[1],
645                                                 addr.ether_addr_octet[2],
646                                                 addr.ether_addr_octet[3],
647                                                 addr.ether_addr_octet[4],
648                                                 addr.ether_addr_octet[5]);
649
650         device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_BLUETOOTH);
651         if (device == NULL)
652                 goto done;
653
654         connman_device_set_ident(device, ident);
655
656         connman_device_set_string(device, "Path", path);
657
658         if (connman_device_register(device) < 0) {
659                 connman_device_unref(device);
660                 goto done;
661         }
662
663         g_hash_table_insert(bluetooth_devices, g_strdup(path), device);
664
665 update:
666         connman_device_set_string(device, "Address", address);
667         connman_device_set_string(device, "Name", name);
668         connman_device_set_string(device, "Path", path);
669
670         connman_device_set_powered(device, powered);
671         connman_device_set_scanning(device, scanning);
672
673         if (powered == TRUE)
674                 check_networks(device, &networks);
675
676 done:
677         dbus_message_unref(reply);
678
679         dbus_pending_call_unref(call);
680 }
681
682 static void add_adapter(DBusConnection *connection, const char *path)
683 {
684         DBusMessage *message;
685         DBusPendingCall *call;
686
687         DBG("path %s", path);
688
689         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
690                                 BLUEZ_ADAPTER_INTERFACE, GET_PROPERTIES);
691         if (message == NULL)
692                 return;
693
694         dbus_message_set_auto_start(message, FALSE);
695
696         if (dbus_connection_send_with_reply(connection, message,
697                                                 &call, TIMEOUT) == FALSE) {
698                 connman_error("Failed to get adapter properties for %s", path);
699                 goto done;
700         }
701
702         if (call == NULL) {
703                 connman_error("D-Bus connection not available");
704                 goto done;
705         }
706
707         dbus_pending_call_set_notify(call, adapter_properties_reply,
708                                                 g_strdup(path), g_free);
709
710 done:
711         dbus_message_unref(message);
712 }
713
714 static gboolean adapter_added(DBusConnection *connection, DBusMessage *message,
715                                 void *user_data)
716 {
717         const char *path;
718
719         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
720                                 DBUS_TYPE_INVALID);
721         add_adapter(connection, path);
722         return TRUE;
723 }
724
725 static void remove_adapter(DBusConnection *connection, const char *path)
726 {
727         DBG("path %s", path);
728
729         g_hash_table_remove(bluetooth_devices, path);
730 }
731
732 static gboolean adapter_removed(DBusConnection *connection, DBusMessage *message,
733                                 void *user_data)
734 {
735         const char *path;
736
737         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
738                                 DBUS_TYPE_INVALID);
739         remove_adapter(connection, path);
740         return TRUE;
741 }
742
743 static void list_adapters_reply(DBusPendingCall *call, void *user_data)
744 {
745         DBusMessage *reply;
746         DBusError error;
747         char **adapters;
748         int i, num_adapters;
749
750         DBG("");
751
752         reply = dbus_pending_call_steal_reply(call);
753
754         dbus_error_init(&error);
755
756         if (dbus_set_error_from_message(&error, reply) == TRUE) {
757                 connman_error("%s", error.message);
758                 dbus_error_free(&error);
759                 goto done;
760         }
761
762         if (dbus_message_get_args(reply, &error,
763                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
764                                                 &adapters, &num_adapters,
765                                                 DBUS_TYPE_INVALID) == FALSE) {
766                 if (dbus_error_is_set(&error) == TRUE) {
767                         connman_error("%s", error.message);
768                         dbus_error_free(&error);
769                 } else
770                         connman_error("Wrong arguments for adapter list");
771                 goto done;
772         }
773
774         for (i = 0; i < num_adapters; i++)
775                 add_adapter(connection, adapters[i]);
776
777         g_strfreev(adapters);
778
779 done:
780         dbus_message_unref(reply);
781
782         dbus_pending_call_unref(call);
783 }
784
785 static void unregister_device(gpointer data)
786 {
787         struct connman_device *device = data;
788
789         DBG("");
790
791         connman_device_unregister(device);
792         connman_device_unref(device);
793 }
794
795 static void bluetooth_connect(DBusConnection *connection, void *user_data)
796 {
797         DBusMessage *message;
798         DBusPendingCall *call;
799
800         DBG("connection %p", connection);
801
802         bluetooth_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
803                                                 g_free, unregister_device);
804
805         bluetooth_networks = g_hash_table_new_full(g_str_hash, g_str_equal,
806                                                 g_free, NULL);
807
808         message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
809                                 BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS);
810         if (message == NULL)
811                 return;
812
813         dbus_message_set_auto_start(message, FALSE);
814
815         if (dbus_connection_send_with_reply(connection, message,
816                                                 &call, TIMEOUT) == FALSE) {
817                 connman_error("Failed to get Bluetooth adapters");
818                 goto done;
819         }
820
821         if (call == NULL) {
822                 connman_error("D-Bus connection not available");
823                 goto done;
824         }
825
826         dbus_pending_call_set_notify(call, list_adapters_reply, NULL, NULL);
827
828 done:
829         dbus_message_unref(message);
830 }
831
832 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
833 {
834         DBG("connection %p", connection);
835
836         if (bluetooth_devices == NULL)
837                 return;
838
839         g_hash_table_destroy(bluetooth_networks);
840         g_hash_table_destroy(bluetooth_devices);
841         bluetooth_devices = NULL;
842 }
843
844 static int bluetooth_probe(struct connman_device *device)
845 {
846         DBG("device %p", device);
847
848         return 0;
849 }
850
851 static void bluetooth_remove(struct connman_device *device)
852 {
853         DBG("device %p", device);
854 }
855
856 static void powered_reply(DBusPendingCall *call, void *user_data)
857 {
858         DBusError error;
859         DBusMessage *reply;
860
861         DBG("");
862
863         reply = dbus_pending_call_steal_reply(call);
864
865         dbus_error_init(&error);
866
867         if (dbus_set_error_from_message(&error, reply) == TRUE) {
868                 connman_error("%s", error.message);
869                 dbus_error_free(&error);
870                 dbus_message_unref(reply);
871                 dbus_pending_call_unref(call);
872                 return;
873         }
874
875         dbus_message_unref(reply);
876         dbus_pending_call_unref(call);
877
878         add_adapter(connection, user_data);
879 }
880
881 static int change_powered(DBusConnection *connection, const char *path,
882                                                         dbus_bool_t powered)
883 {
884         DBusMessage *message;
885         DBusMessageIter iter;
886         DBusPendingCall *call;
887
888         DBG("");
889
890         if (path == NULL)
891                 return -EINVAL;
892
893         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
894                                         BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY);
895         if (message == NULL)
896                 return -ENOMEM;
897
898         dbus_message_set_auto_start(message, FALSE);
899
900         dbus_message_iter_init_append(message, &iter);
901         connman_dbus_property_append_basic(&iter, "Powered",
902                                                 DBUS_TYPE_BOOLEAN, &powered);
903
904         if (dbus_connection_send_with_reply(connection, message,
905                                                 &call, TIMEOUT) == FALSE) {
906                 connman_error("Failed to change Powered property");
907                 dbus_message_unref(message);
908                 return -EINVAL;
909         }
910
911         if (call == NULL) {
912                 connman_error("D-Bus connection not available");
913                 dbus_message_unref(message);
914                 return -EINVAL;
915         }
916
917         dbus_pending_call_set_notify(call, powered_reply,
918                                         g_strdup(path), g_free);
919
920         dbus_message_unref(message);
921
922         return -EINPROGRESS;
923 }
924
925 static int bluetooth_enable(struct connman_device *device)
926 {
927         const char *path = connman_device_get_string(device, "Path");
928
929         DBG("device %p", device);
930
931         return change_powered(connection, path, TRUE);
932 }
933
934 static int bluetooth_disable(struct connman_device *device)
935 {
936         const char *path = connman_device_get_string(device, "Path");
937
938         DBG("device %p", device);
939
940         return change_powered(connection, path, FALSE);
941 }
942
943 static struct connman_device_driver bluetooth_driver = {
944         .name           = "bluetooth",
945         .type           = CONNMAN_DEVICE_TYPE_BLUETOOTH,
946         .probe          = bluetooth_probe,
947         .remove         = bluetooth_remove,
948         .enable         = bluetooth_enable,
949         .disable        = bluetooth_disable,
950 };
951
952 static int tech_probe(struct connman_technology *technology)
953 {
954         return 0;
955 }
956
957 static void tech_remove(struct connman_technology *technology)
958 {
959 }
960
961 static void server_register_reply(DBusPendingCall *call, void *user_data)
962 {
963         struct connman_technology *technology = user_data;
964         DBusError error;
965         DBusMessage *reply;
966
967         DBG("");
968
969         reply = dbus_pending_call_steal_reply(call);
970
971         dbus_error_init(&error);
972
973         if (dbus_set_error_from_message(&error, reply) == TRUE) {
974                 connman_error("%s", error.message);
975                 dbus_error_free(&error);
976                 dbus_message_unref(reply);
977                 dbus_pending_call_unref(call);
978                 return;
979         }
980
981         dbus_message_unref(reply);
982         dbus_pending_call_unref(call);
983
984         connman_technology_tethering_notify(technology, TRUE);
985 }
986
987 static void server_unregister_reply(DBusPendingCall *call, void *user_data)
988 {
989         struct connman_technology *technology = user_data;
990         DBusError error;
991         DBusMessage *reply;
992
993         DBG("");
994
995         reply = dbus_pending_call_steal_reply(call);
996
997         dbus_error_init(&error);
998
999         if (dbus_set_error_from_message(&error, reply) == TRUE) {
1000                 connman_error("%s", error.message);
1001                 dbus_error_free(&error);
1002                 dbus_message_unref(reply);
1003                 dbus_pending_call_unref(call);
1004                 return;
1005         }
1006
1007         dbus_message_unref(reply);
1008         dbus_pending_call_unref(call);
1009
1010         connman_technology_tethering_notify(technology, FALSE);
1011 }
1012
1013
1014 static void server_register(const char *path, const char *uuid,
1015                                 struct connman_technology *technology,
1016                                 const char *bridge, connman_bool_t enabled)
1017 {
1018         DBusMessage *message;
1019         DBusPendingCall *call;
1020         char *command;
1021
1022         DBG("path %s enabled %d", path, enabled);
1023
1024         command = enabled ? REGISTER : UNREGISTER;
1025
1026         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
1027                                         BLUEZ_NETWORK_SERVER, command);
1028         if (message == NULL)
1029                 return;
1030
1031         dbus_message_set_auto_start(message, FALSE);
1032
1033         dbus_message_append_args(message, DBUS_TYPE_STRING, &uuid,
1034                                                         DBUS_TYPE_INVALID);
1035
1036         if (enabled == TRUE)
1037                 dbus_message_append_args(message, DBUS_TYPE_STRING, &bridge,
1038                                                         DBUS_TYPE_INVALID);
1039
1040         if (dbus_connection_send_with_reply(connection, message,
1041                                                 &call, TIMEOUT) == FALSE) {
1042                 connman_error("Failed to enable PAN server");
1043                 dbus_message_unref(message);
1044                 return;
1045         }
1046
1047         if (call == NULL) {
1048                 connman_error("D-Bus connection not available");
1049                 dbus_message_unref(message);
1050                 return;
1051         }
1052
1053         if (enabled == TRUE)
1054                 dbus_pending_call_set_notify(call, server_register_reply,
1055                                                 technology, NULL);
1056         else
1057                 dbus_pending_call_set_notify(call, server_unregister_reply,
1058                                                 technology, NULL);
1059
1060         dbus_message_unref(message);
1061 }
1062
1063 struct tethering_info {
1064         struct connman_technology *technology;
1065         const char *bridge;
1066 };
1067
1068 static void enable_nap(gpointer key, gpointer value, gpointer user_data)
1069 {
1070         struct tethering_info *info = user_data;
1071         struct connman_device *device = value;
1072         const char *path;
1073
1074         DBG("");
1075
1076         path = connman_device_get_string(device, "Path");
1077
1078         server_register(path, "nap", info->technology, info->bridge, TRUE);
1079 }
1080
1081 static void disable_nap(gpointer key, gpointer value, gpointer user_data)
1082 {
1083         struct tethering_info *info = user_data;
1084         struct connman_device *device = value;
1085         const char *path;
1086
1087         DBG("");
1088
1089         path = connman_device_get_string(device, "Path");
1090
1091         server_register(path, "nap", info->technology, info->bridge, FALSE);
1092 }
1093
1094 static int tech_set_tethering(struct connman_technology *technology,
1095                                 const char *identifier, const char *passphrase,
1096                                 const char *bridge, connman_bool_t enabled)
1097 {
1098         struct tethering_info info = {
1099                 .technology     = technology,
1100                 .bridge         = bridge,
1101         };
1102
1103         DBG("bridge %s", bridge);
1104
1105         if (enabled)
1106                 g_hash_table_foreach(bluetooth_devices, enable_nap, &info);
1107         else
1108                 g_hash_table_foreach(bluetooth_devices, disable_nap, &info);
1109
1110         return 0;
1111 }
1112
1113 static struct connman_technology_driver tech_driver = {
1114         .name           = "bluetooth",
1115         .type           = CONNMAN_SERVICE_TYPE_BLUETOOTH,
1116         .probe          = tech_probe,
1117         .remove         = tech_remove,
1118         .set_tethering  = tech_set_tethering,
1119 };
1120
1121 static guint watch;
1122 static guint added_watch;
1123 static guint removed_watch;
1124 static guint adapter_watch;
1125 static guint device_watch;
1126 static guint device_removed_watch;
1127 static guint network_watch;
1128
1129 static int bluetooth_init(void)
1130 {
1131         int err;
1132
1133         connection = connman_dbus_get_connection();
1134         if (connection == NULL)
1135                 return -EIO;
1136
1137         watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
1138                         bluetooth_connect, bluetooth_disconnect, NULL, NULL);
1139
1140         added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1141                                                 BLUEZ_MANAGER_INTERFACE,
1142                                                 ADAPTER_ADDED, adapter_added,
1143                                                 NULL, NULL);
1144
1145         removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1146                                                 BLUEZ_MANAGER_INTERFACE,
1147                                                 ADAPTER_REMOVED, adapter_removed,
1148                                                 NULL, NULL);
1149
1150         adapter_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1151                                                 BLUEZ_ADAPTER_INTERFACE,
1152                                                 PROPERTY_CHANGED, adapter_changed,
1153                                                 NULL, NULL);
1154
1155         device_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1156                                                 BLUEZ_ADAPTER_INTERFACE,
1157                                                 DEVICE_REMOVED, device_removed,
1158                                                 NULL, NULL);
1159
1160         device_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1161                                                 BLUEZ_DEVICE_INTERFACE,
1162                                                 PROPERTY_CHANGED, device_changed,
1163                                                 NULL, NULL);
1164
1165         network_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1166                                                 BLUEZ_NETWORK_INTERFACE,
1167                                                 PROPERTY_CHANGED, network_changed,
1168                                                 NULL, NULL);
1169
1170         if (watch == 0 || added_watch == 0 || removed_watch == 0
1171                         || adapter_watch == 0 || network_watch == 0
1172                                 || device_watch == 0
1173                                         || device_removed_watch == 0) {
1174                 err = -EIO;
1175                 goto remove;
1176         }
1177
1178         err = connman_network_driver_register(&pan_driver);
1179         if (err < 0)
1180                 goto remove;
1181
1182         err = connman_device_driver_register(&bluetooth_driver);
1183         if (err < 0) {
1184                 connman_network_driver_unregister(&pan_driver);
1185                 goto remove;
1186         }
1187
1188         err = connman_technology_driver_register(&tech_driver);
1189         if (err < 0) {
1190                 connman_device_driver_unregister(&bluetooth_driver);
1191                 connman_network_driver_unregister(&pan_driver);
1192                 return err;
1193         }
1194
1195         return 0;
1196
1197 remove:
1198         g_dbus_remove_watch(connection, watch);
1199         g_dbus_remove_watch(connection, added_watch);
1200         g_dbus_remove_watch(connection, removed_watch);
1201         g_dbus_remove_watch(connection, adapter_watch);
1202         g_dbus_remove_watch(connection, device_removed_watch);
1203         g_dbus_remove_watch(connection, device_watch);
1204         g_dbus_remove_watch(connection, network_watch);
1205
1206         dbus_connection_unref(connection);
1207
1208         return err;
1209 }
1210
1211 static void bluetooth_exit(void)
1212 {
1213         g_dbus_remove_watch(connection, watch);
1214         g_dbus_remove_watch(connection, added_watch);
1215         g_dbus_remove_watch(connection, removed_watch);
1216         g_dbus_remove_watch(connection, adapter_watch);
1217         g_dbus_remove_watch(connection, device_removed_watch);
1218         g_dbus_remove_watch(connection, device_watch);
1219         g_dbus_remove_watch(connection, network_watch);
1220
1221         bluetooth_disconnect(connection, NULL);
1222
1223         connman_technology_driver_unregister(&tech_driver);
1224
1225         connman_device_driver_unregister(&bluetooth_driver);
1226         connman_network_driver_unregister(&pan_driver);
1227
1228         dbus_connection_unref(connection);
1229 }
1230
1231 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
1232                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, bluetooth_init, bluetooth_exit)