Updated connman to version 1.35
[platform/upstream/connman.git] / plugins / bluetooth.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2013  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 <errno.h>
27 #include <string.h>
28
29 #define CONNMAN_API_SUBJECT_TO_CHANGE
30 #include <connman/plugin.h>
31 #include <connman/dbus.h>
32 #include <connman/technology.h>
33 #include <connman/device.h>
34 #include <connman/inet.h>
35 #include <gdbus.h>
36
37 #define BLUEZ_SERVICE                   "org.bluez"
38 #define BLUEZ_PATH                      "/org/bluez"
39 #define BLUETOOTH_PAN_PANU              "00001115-0000-1000-8000-00805f9b34fb"
40 #define BLUETOOTH_PAN_NAP               "00001116-0000-1000-8000-00805f9b34fb"
41 #define BLUETOOTH_PAN_GN                "00001117-0000-1000-8000-00805f9b34fb"
42
43 #define BLUETOOTH_ADDR_LEN              6
44
45 static DBusConnection *connection;
46 static GDBusClient *client;
47 static GHashTable *devices;
48 static GHashTable *networks;
49 static bool bluetooth_tethering;
50
51 struct bluetooth_pan {
52         struct connman_network *network;
53         GDBusProxy *btdevice_proxy;
54         GDBusProxy *btnetwork_proxy;
55         const char *pan_role;
56 };
57
58 static void address2ident(const char *address, char *ident)
59 {
60         int i;
61
62         for (i = 0; i < BLUETOOTH_ADDR_LEN; i++) {
63                 ident[i * 2] = address[i * 3];
64                 ident[i * 2 + 1] = address[i * 3 + 1];
65         }
66         ident[BLUETOOTH_ADDR_LEN * 2] = '\0';
67 }
68
69 static const char *proxy_get_string(GDBusProxy *proxy, const char *property)
70 {
71         DBusMessageIter iter;
72         const char *str;
73
74         if (!g_dbus_proxy_get_property(proxy, property, &iter))
75                 return NULL;
76         dbus_message_iter_get_basic(&iter, &str);
77         return str;
78 }
79
80 static bool proxy_get_bool(GDBusProxy *proxy, const char *property)
81 {
82         DBusMessageIter iter;
83         dbus_bool_t value;
84
85         if (!g_dbus_proxy_get_property(proxy, property, &iter))
86                 return false;
87         dbus_message_iter_get_basic(&iter, &value);
88         return value;
89 }
90
91 static const char *proxy_get_role(GDBusProxy *proxy)
92 {
93         DBusMessageIter iter, value;
94         const char *pref = NULL;
95
96         if (!proxy)
97                 return NULL;
98
99         if (!g_dbus_proxy_get_property(proxy, "UUIDs", &iter))
100                 return NULL;
101
102         dbus_message_iter_recurse(&iter, &value);
103         while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
104                 const char *uuid;
105
106                 dbus_message_iter_get_basic(&value, &uuid);
107                 /*
108                  * If a device offers more than one role, we prefer NAP,
109                  * then GN, then PANU.
110                  */
111                 if (!strcmp(uuid, BLUETOOTH_PAN_NAP))
112                         return "nap";
113                 if (!strcmp(uuid, BLUETOOTH_PAN_GN))
114                         pref = "gn";
115                 if (!strcmp(uuid, BLUETOOTH_PAN_PANU) && !pref)
116                         pref = "panu";
117
118                 dbus_message_iter_next(&value);
119         }
120
121         return pref;
122 }
123
124 static int bluetooth_pan_probe(struct connman_network *network)
125 {
126         GHashTableIter iter;
127         gpointer key, value;
128
129         DBG("network %p", network);
130
131         g_hash_table_iter_init(&iter, networks);
132
133         while (g_hash_table_iter_next(&iter, &key, &value)) {
134                 struct bluetooth_pan *pan = value;
135
136                 if (network == pan->network)
137                         return 0;
138         }
139
140         return -EOPNOTSUPP;
141 }
142
143 static void pan_remove_nap(struct bluetooth_pan *pan)
144 {
145         struct connman_device *device;
146         struct connman_network *network = pan->network;
147
148         DBG("network %p pan %p", pan->network, pan);
149
150         if (!network)
151                 return;
152
153         pan->network = NULL;
154         connman_network_set_data(network, NULL);
155
156         device = connman_network_get_device(network);
157         if (device)
158                 connman_device_remove_network(device, network);
159
160         connman_network_unref(network);
161 }
162
163 static void bluetooth_pan_remove(struct connman_network *network)
164 {
165         struct bluetooth_pan *pan = connman_network_get_data(network);
166
167         DBG("network %p pan %p", network, pan);
168
169         connman_network_set_data(network, NULL);
170
171         if (pan)
172                 pan_remove_nap(pan);
173 }
174
175 static bool pan_connect(struct bluetooth_pan *pan,
176                 const char *iface)
177 {
178         int index;
179
180         if (!iface) {
181                 if (!proxy_get_bool(pan->btnetwork_proxy, "Connected"))
182                         return false;
183                 iface = proxy_get_string(pan->btnetwork_proxy, "Interface");
184         }
185
186         if (!iface)
187                 return false;
188
189         index = connman_inet_ifindex(iface);
190         if (index < 0) {
191                 DBG("network %p invalid index %d", pan->network, index);
192                 return false;
193         }
194
195 #if defined TIZEN_EXT
196         if (pan->network) {
197 #endif
198         connman_network_set_index(pan->network, index);
199         connman_network_set_connected(pan->network, true);
200 #if defined TIZEN_EXT
201         }
202 #endif
203
204         return true;
205 }
206
207 static void pan_connect_cb(DBusMessage *message, void *user_data)
208 {
209         const char *path = user_data;
210         const char *iface = NULL;
211         struct bluetooth_pan *pan;
212         DBusMessageIter iter;
213
214         pan = g_hash_table_lookup(networks, path);
215         if (!pan || !pan->network) {
216                 DBG("network already removed");
217                 return;
218         }
219
220 #ifdef TIZEN_EXT
221         /*
222          * Network could be removed because of BT adapter power off
223          * This is to handle the scenario where network is removed
224          * before the connect_cb is called
225          */
226         if (!pan->network) {
227                 DBG("network already removed");
228                 return;
229         }
230 #endif
231
232         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
233                 const char *dbus_error = dbus_message_get_error_name(message);
234
235                 DBG("network %p %s", pan->network, dbus_error);
236
237                 if (strcmp(dbus_error,
238                                 "org.bluez.Error.AlreadyConnected") != 0) {
239                         connman_network_set_associating(pan->network, false);
240                         connman_network_set_error(pan->network,
241                                 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
242                         return;
243                 }
244         } else {
245                 if (dbus_message_iter_init(message, &iter) &&
246                                 dbus_message_iter_get_arg_type(&iter) ==
247                                 DBUS_TYPE_STRING)
248                         dbus_message_iter_get_basic(&iter, &iface);
249         }
250
251         DBG("network %p interface %s", pan->network, iface);
252
253         pan_connect(pan, iface);
254 }
255
256 static void pan_connect_append(DBusMessageIter *iter,
257                 void *user_data)
258 {
259         const char *path = user_data;
260         struct bluetooth_pan *pan;
261
262         pan = g_hash_table_lookup(networks, path);
263         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &pan->pan_role);
264 }
265
266 static int bluetooth_pan_connect(struct connman_network *network)
267 {
268         struct bluetooth_pan *pan = connman_network_get_data(network);
269         const char *path;
270
271         DBG("network %p", network);
272
273         if (!pan)
274                 return -EINVAL;
275
276         path = g_dbus_proxy_get_path(pan->btnetwork_proxy);
277
278         if (!g_dbus_proxy_method_call(pan->btnetwork_proxy, "Connect",
279                         pan_connect_append, pan_connect_cb,
280                         g_strdup(path), g_free))
281                 return -EIO;
282
283 #if defined TIZEN_EXT
284         if (pan->network)
285 #endif
286         connman_network_set_associating(pan->network, true);
287
288         return -EINPROGRESS;
289 }
290
291 static void pan_disconnect_cb(DBusMessage *message, void *user_data)
292 {
293         const char *path = user_data;
294         struct bluetooth_pan *pan;
295
296         pan = g_hash_table_lookup(networks, path);
297         if (!pan || !pan->network) {
298                 DBG("network already removed");
299                 return;
300         }
301
302         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
303                 const char *dbus_error = dbus_message_get_error_name(message);
304
305                 DBG("network %p %s", pan->network, dbus_error);
306         }
307
308         DBG("network %p", pan->network);
309
310 #if defined TIZEN_EXT
311         if (pan->network)
312 #endif
313         connman_network_set_connected(pan->network, false);
314 }
315
316 static int bluetooth_pan_disconnect(struct connman_network *network)
317 {
318         struct bluetooth_pan *pan = connman_network_get_data(network);
319         const char *path;
320
321         DBG("network %p", network);
322
323         if (!pan)
324                 return -EINVAL;
325
326 #if defined TIZEN_EXT
327         if (connman_network_get_associating(network) == TRUE)
328                 connman_network_clear_associating(network);
329 #endif
330
331         path = g_dbus_proxy_get_path(pan->btnetwork_proxy);
332
333         if (!g_dbus_proxy_method_call(pan->btnetwork_proxy, "Disconnect",
334                         NULL, pan_disconnect_cb, g_strdup(path), g_free))
335                 return -EIO;
336
337        return -EINPROGRESS;
338 }
339
340 static void btnetwork_property_change(GDBusProxy *proxy, const char *name,
341                 DBusMessageIter *iter, void *user_data)
342 {
343         struct bluetooth_pan *pan;
344         dbus_bool_t connected;
345         bool proxy_connected, network_connected;
346
347         if (strcmp(name, "Connected") != 0)
348                 return;
349
350         pan = g_hash_table_lookup(networks, g_dbus_proxy_get_path(proxy));
351         if (!pan || !pan->network)
352                 return;
353
354         dbus_message_iter_get_basic(iter, &connected);
355         proxy_connected = connected;
356
357         network_connected = connman_network_get_connected(pan->network);
358
359         DBG("network %p network connected %d proxy connected %d",
360                         pan->network, network_connected, proxy_connected);
361
362        if (network_connected != proxy_connected)
363                connman_network_set_connected(pan->network, proxy_connected);
364 }
365
366 static void pan_create_nap(struct bluetooth_pan *pan)
367 {
368         struct connman_device *device;
369         const char* role;
370         const char *adapter;
371
372         role = proxy_get_role(pan->btdevice_proxy);
373         if (!role) {
374                 pan_remove_nap(pan);
375                 return;
376         }
377
378         adapter = proxy_get_string(pan->btdevice_proxy, "Adapter");
379
380         if (!adapter)
381                 return;
382
383         device = g_hash_table_lookup(devices, adapter);
384
385         if (!device || !connman_device_get_powered(device))
386                 return;
387
388         if (!pan->network) {
389                 const char *address;
390                 char ident[BLUETOOTH_ADDR_LEN * 2 + 1];
391                 const char *name, *path;
392
393                 address = proxy_get_string(pan->btdevice_proxy, "Address");
394                 if (!address) {
395                         connman_warn("Bluetooth device address missing");
396                         return;
397                 }
398
399                 address2ident(address, ident);
400
401                 pan->network = connman_network_create(ident,
402                                 CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
403
404                 name = proxy_get_string(pan->btdevice_proxy, "Alias");
405                 path = g_dbus_proxy_get_path(pan->btnetwork_proxy);
406
407                 DBG("network %p %s %s", pan->network, path, name);
408
409                 if (!pan->network) {
410                         connman_warn("Bluetooth network %s creation failed",
411                                         path);
412                         return;
413                 }
414
415                 connman_network_set_data(pan->network, pan);
416                 connman_network_set_name(pan->network, name);
417                 connman_network_set_group(pan->network, ident);
418         }
419
420         pan->pan_role = role;
421         connman_device_add_network(device, pan->network);
422
423         if (pan_connect(pan, NULL))
424                 DBG("network %p already connected", pan->network);
425 }
426
427 static void btdevice_property_change(GDBusProxy *proxy, const char *name,
428                 DBusMessageIter *iter, void *user_data)
429 {
430         struct bluetooth_pan *pan;
431         const char *old_role = NULL;
432         const char *new_role;
433
434         if (strcmp(name, "UUIDs"))
435                 return;
436
437         pan = g_hash_table_lookup(networks, g_dbus_proxy_get_path(proxy));
438         if (!pan)
439                 return;
440
441         if (pan->network &&
442                         connman_network_get_device(pan->network))
443                 old_role = pan->pan_role;
444         new_role = proxy_get_role(pan->btdevice_proxy);
445
446         DBG("network %p network role %s proxy role %s", pan->network, old_role,
447                         new_role);
448
449         if (old_role && new_role && !strcmp(old_role, new_role))
450                 return;
451
452         pan_create_nap(pan);
453 }
454
455 static void pan_free(gpointer data)
456 {
457         struct bluetooth_pan *pan = data;
458
459         if (pan->btnetwork_proxy) {
460                 g_dbus_proxy_unref(pan->btnetwork_proxy);
461                 pan->btnetwork_proxy = NULL;
462         }
463
464         if (pan->btdevice_proxy) {
465                 g_dbus_proxy_unref(pan->btdevice_proxy);
466                 pan->btdevice_proxy = NULL;
467         }
468
469         pan_remove_nap(pan);
470
471         g_free(pan);
472 }
473
474 static void pan_create(GDBusProxy *network_proxy)
475 {
476         const char *path = g_dbus_proxy_get_path(network_proxy);
477         struct bluetooth_pan *pan;
478
479         pan = g_try_new0(struct bluetooth_pan, 1);
480
481         if (!pan) {
482                 connman_error("Out of memory creating PAN NAP");
483                 return;
484         }
485
486         g_hash_table_replace(networks, g_strdup(path), pan);
487
488         pan->btnetwork_proxy = g_dbus_proxy_ref(network_proxy);
489         pan->btdevice_proxy = g_dbus_proxy_new(client, path,
490                         "org.bluez.Device1");
491
492         if (!pan->btdevice_proxy) {
493                 connman_error("Cannot create BT PAN watcher %s", path);
494                 g_hash_table_remove(networks, path);
495                 return;
496         }
497
498         g_dbus_proxy_set_property_watch(pan->btnetwork_proxy,
499                         btnetwork_property_change, NULL);
500
501         g_dbus_proxy_set_property_watch(pan->btdevice_proxy,
502                         btdevice_property_change, NULL);
503
504         DBG("pan %p %s role %s", pan, path, proxy_get_role(pan->btdevice_proxy));
505
506         pan_create_nap(pan);
507 }
508
509 static struct connman_network_driver network_driver = {
510         .name           = "bluetooth",
511         .type           = CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN,
512         .probe          = bluetooth_pan_probe,
513         .remove         = bluetooth_pan_remove,
514         .connect        = bluetooth_pan_connect,
515         .disconnect     = bluetooth_pan_disconnect,
516 };
517
518 static void enable_device(struct connman_device *device, const char *path)
519 {
520         GHashTableIter iter;
521         gpointer key, value;
522
523         DBG("device %p %s", device, path);
524         connman_device_set_powered(device, true);
525
526         g_hash_table_iter_init(&iter, networks);
527         while (g_hash_table_iter_next(&iter, &key, &value)) {
528                 struct bluetooth_pan *pan = value;
529
530                 if (g_strcmp0(proxy_get_string(pan->btdevice_proxy, "Adapter"),
531                                                 path) == 0) {
532
533                         DBG("enable network %p", pan->network);
534                         pan_create_nap(pan);
535                 }
536         }
537 }
538
539 static void device_enable_cb(const DBusError *error, void *user_data)
540 {
541         char *path = user_data;
542         struct connman_device *device;
543
544         device = g_hash_table_lookup(devices, path);
545         if (!device) {
546                 DBG("device already removed");
547                 goto out;
548         }
549
550         if (dbus_error_is_set(error)) {
551                 connman_warn("Bluetooth device %s not enabled %s",
552                                 path, error->message);
553                 goto out;
554         }
555
556 #if !defined TIZEN_EXT
557         enable_device(device, path);
558 #endif
559
560 out:
561         g_free(path);
562 }
563
564 static int bluetooth_device_enable(struct connman_device *device)
565 {
566         GDBusProxy *proxy = connman_device_get_data(device);
567         dbus_bool_t device_powered = TRUE;
568         const char *path;
569
570         if (!proxy)
571                 return 0;
572
573         path = g_dbus_proxy_get_path(proxy);
574
575         if (proxy_get_bool(proxy, "Powered")) {
576                 DBG("already enabled %p %s", device, path);
577                 return -EALREADY;
578         }
579
580         DBG("device %p %s", device, path);
581
582         g_dbus_proxy_set_property_basic(proxy, "Powered",
583                         DBUS_TYPE_BOOLEAN, &device_powered,
584                         device_enable_cb, g_strdup(path), NULL);
585
586         return -EINPROGRESS;
587 }
588
589 static void disable_device(struct connman_device *device, const char *path)
590 {
591         GHashTableIter iter;
592         gpointer key, value;
593
594         DBG("device %p %s", device, path);
595         connman_device_set_powered(device, false);
596
597         g_hash_table_iter_init(&iter, networks);
598         while (g_hash_table_iter_next(&iter, &key, &value)) {
599                 struct bluetooth_pan *pan = value;
600
601                 if (pan->network && connman_network_get_device(pan->network)
602                                 == device) {
603                         DBG("disable network %p", pan->network);
604                         connman_device_remove_network(device, pan->network);
605                 }
606         }
607 }
608
609 static void device_disable_cb(const DBusError *error, void *user_data)
610 {
611         char *path = user_data;
612         struct connman_device *device;
613
614         device = g_hash_table_lookup(devices, path);
615         if (!device) {
616                 DBG("device already removed");
617                 goto out;
618         }
619
620         if (dbus_error_is_set(error)) {
621                 connman_warn("Bluetooth device %s not disabled: %s",
622                                 path, error->message);
623                 goto out;
624         }
625
626 #if !defined TIZEN_EXT
627         disable_device(device, path);
628 #endif
629
630 out:
631         g_free(path);
632 }
633
634 static int bluetooth_device_disable(struct connman_device *device)
635 {
636         GDBusProxy *proxy = connman_device_get_data(device);
637         dbus_bool_t device_powered = FALSE;
638         const char *path;
639
640         if (!proxy)
641                 return 0;
642
643         path = g_dbus_proxy_get_path(proxy);
644
645         if (!proxy_get_bool(proxy, "Powered")) {
646                 DBG("already disabled %p %s", device, path);
647                 return -EALREADY;
648         }
649
650         DBG("device %p %s", device, path);
651
652         g_dbus_proxy_set_property_basic(proxy, "Powered",
653                         DBUS_TYPE_BOOLEAN, &device_powered,
654                         device_disable_cb, g_strdup(path), NULL);
655
656         return -EINPROGRESS;
657 }
658
659 static void adapter_property_change(GDBusProxy *proxy, const char *name,
660                 DBusMessageIter *iter, void *user_data)
661 {
662         struct connman_device *device;
663         const char *path;
664         bool adapter_powered, device_powered;
665
666         if (strcmp(name, "Powered") != 0)
667                 return;
668
669         path = g_dbus_proxy_get_path(proxy);
670         device = g_hash_table_lookup(devices, path);
671
672         adapter_powered = proxy_get_bool(proxy, "Powered");
673         device_powered = connman_device_get_powered(device);
674
675         DBG("device %p %s device powered %d adapter powered %d", device, path,
676                         device_powered, adapter_powered);
677
678         if (device_powered != adapter_powered) {
679                 if (adapter_powered)
680                         enable_device(device, path);
681                 else
682                         disable_device(device, path);
683         }
684 }
685
686 static void device_free(gpointer data)
687 {
688         struct connman_device *device = data;
689         GDBusProxy *proxy = connman_device_get_data(device);
690
691         connman_device_set_data(device, NULL);
692         if (proxy)
693                 g_dbus_proxy_unref(proxy);
694
695         connman_device_unregister(device);
696         connman_device_unref(device);
697 }
698
699 struct tethering_info {
700         struct connman_technology *technology;
701         char *bridge;
702         bool enable;
703 };
704
705 static void tethering_free(void *user_data)
706 {
707         struct tethering_info *tethering = user_data;
708
709         g_free(tethering->bridge);
710         g_free(tethering);
711 }
712
713 static void tethering_create_cb(DBusMessage *message, void *user_data)
714 {
715         struct tethering_info *tethering = user_data;
716
717         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
718                 const char *dbus_error = dbus_message_get_error_name(message);
719
720                 DBG("%s tethering failed: %s",
721                                 tethering->enable ? "enable" : "disable",
722                                 dbus_error);
723                 return;
724         }
725
726         DBG("bridge %s %s", tethering->bridge, tethering->enable ?
727                         "enabled": "disabled");
728
729         if (tethering->technology)
730                 connman_technology_tethering_notify(tethering->technology,
731                                 tethering->enable);
732 }
733
734 static void tethering_append(DBusMessageIter *iter, void *user_data)
735 {
736         struct tethering_info *tethering = user_data;
737         const char *nap = "nap";
738
739         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &nap);
740         if (tethering->enable)
741                 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
742                                 &tethering->bridge);
743 }
744
745 static bool tethering_create(const char *path,
746                 struct connman_technology *technology, const char *bridge,
747                 bool enabled)
748 {
749         struct tethering_info *tethering = g_new0(struct tethering_info, 1);
750         GDBusProxy *proxy;
751         const char *method;
752         bool result;
753
754         DBG("path %s bridge %s", path, bridge);
755
756         if (!bridge) {
757                 g_free(tethering);
758                 return false;
759         }
760
761         proxy = g_dbus_proxy_new(client, path, "org.bluez.NetworkServer1");
762         if (!proxy) {
763                 g_free(tethering);
764                 return false;
765         }
766
767         tethering->technology = technology;
768         tethering->bridge = g_strdup(bridge);
769         tethering->enable = enabled;
770
771         if (tethering->enable)
772                 method = "Register";
773         else
774                 method = "Unregister";
775
776         result = g_dbus_proxy_method_call(proxy, method, tethering_append,
777                         tethering_create_cb, tethering, tethering_free);
778
779         g_dbus_proxy_unref(proxy);
780
781         return result;
782 }
783
784 static void device_create(GDBusProxy *proxy)
785 {
786         struct connman_device *device = NULL;
787         const char *path = g_dbus_proxy_get_path(proxy);
788         const char *address;
789         char ident[BLUETOOTH_ADDR_LEN * 2 + 1];
790         bool powered;
791
792         address = proxy_get_string(proxy, "Address");
793         if (!address)
794                 return;
795
796         address2ident(address, ident);
797
798         device = connman_device_create("bluetooth",
799                         CONNMAN_DEVICE_TYPE_BLUETOOTH);
800         if (!device)
801                 return;
802
803         connman_device_set_data(device, g_dbus_proxy_ref(proxy));
804         connman_device_set_ident(device, ident);
805
806         g_hash_table_replace(devices, g_strdup(path), device);
807
808         DBG("device %p %s device powered %d adapter powered %d", device,
809                         path, connman_device_get_powered(device),
810                         proxy_get_bool(proxy, "Powered"));
811
812         if (connman_device_register(device) < 0) {
813                 g_hash_table_remove(devices, device);
814                 return;
815         }
816
817         g_dbus_proxy_set_property_watch(proxy, adapter_property_change, NULL);
818
819         powered = proxy_get_bool(proxy, "Powered");
820         connman_device_set_powered(device, powered);
821
822         if (proxy_get_role(proxy) && !bluetooth_tethering)
823                 tethering_create(path, NULL, NULL, false);
824 }
825
826 static void object_added(GDBusProxy *proxy, void *user_data)
827 {
828         const char *interface;
829
830         interface = g_dbus_proxy_get_interface(proxy);
831         if (!interface) {
832                 connman_warn("Interface or proxy missing when adding "
833                                                         "bluetooth object");
834                 return;
835         }
836
837         if (strcmp(interface, "org.bluez.Adapter1") == 0) {
838                 DBG("%s %s", interface, g_dbus_proxy_get_path(proxy));
839                 device_create(proxy);
840                 return;
841         }
842
843         if (strcmp(interface, "org.bluez.Network1") == 0) {
844                 DBG("%s %s", interface, g_dbus_proxy_get_path(proxy));
845                 pan_create(proxy);
846                 return;
847         }
848 }
849
850 static void object_removed(GDBusProxy *proxy, void *user_data)
851 {
852         const char *interface, *path;
853
854         interface = g_dbus_proxy_get_interface(proxy);
855         if (!interface) {
856                 connman_warn("Interface or proxy missing when removing "
857                                                         "bluetooth object");
858                 return;
859         }
860
861         if (strcmp(interface, "org.bluez.Adapter1") == 0) {
862                 path = g_dbus_proxy_get_path(proxy);
863                 DBG("%s %s", interface, path);
864
865                 g_hash_table_remove(devices, path);
866         }
867
868         if (strcmp(interface, "org.bluez.Network1") == 0) {
869                 path = g_dbus_proxy_get_path(proxy);
870                 DBG("%s %s", interface, path);
871
872                 g_hash_table_remove(networks, path);
873         }
874
875 }
876
877 static int bluetooth_device_probe(struct connman_device *device)
878 {
879         GHashTableIter iter;
880         gpointer key, value;
881
882         g_hash_table_iter_init(&iter, devices);
883
884         while (g_hash_table_iter_next(&iter, &key, &value)) {
885                 struct connman_device *known = value;
886
887                 if (device == known)
888                         return 0;
889         }
890
891         return -EOPNOTSUPP;
892 }
893
894 static void bluetooth_device_remove(struct connman_device *device)
895 {
896         DBG("%p", device);
897 }
898
899 static struct connman_device_driver device_driver = {
900         .name           = "bluetooth",
901         .type           = CONNMAN_DEVICE_TYPE_BLUETOOTH,
902         .probe          = bluetooth_device_probe,
903         .remove         = bluetooth_device_remove,
904         .enable         = bluetooth_device_enable,
905         .disable        = bluetooth_device_disable,
906 };
907
908 static int bluetooth_tech_probe(struct connman_technology *technology)
909 {
910         return 0;
911 }
912
913 static void bluetooth_tech_remove(struct connman_technology *technology)
914 {
915
916 }
917
918 static int bluetooth_tech_set_tethering(struct connman_technology *technology,
919                 const char *identifier, const char *passphrase,
920                 const char *bridge, bool enabled, bool hidden)
921 {
922         GHashTableIter hash_iter;
923         gpointer key, value;
924         int i = 0;
925
926         bluetooth_tethering = enabled;
927
928         g_hash_table_iter_init(&hash_iter, devices);
929
930         while (g_hash_table_iter_next(&hash_iter, &key, &value)) {
931                 const char *path = key;
932                 struct connman_device *device = value;
933
934                 DBG("device %p", device);
935
936                 if (tethering_create(path, technology, bridge, enabled)
937                                 )
938                         i++;
939         }
940
941         DBG("%s %d device(s)", enabled ? "enabled" : "disabled", i);
942
943         if (i == 0)
944                 return -ENODEV;
945
946         return 0;
947 }
948
949 static struct connman_technology_driver tech_driver = {
950         .name           = "bluetooth",
951         .type           = CONNMAN_SERVICE_TYPE_BLUETOOTH,
952         .probe          = bluetooth_tech_probe,
953         .remove         = bluetooth_tech_remove,
954         .set_tethering  = bluetooth_tech_set_tethering,
955 };
956
957 static int bluetooth_init(void)
958 {
959         connection = connman_dbus_get_connection();
960         if (!connection)
961                 goto out;
962
963         if (connman_technology_driver_register(&tech_driver) < 0) {
964                 connman_warn("Failed to initialize technology for Bluez 5");
965                 goto out;
966         }
967
968         devices = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
969                         device_free);
970
971         if (connman_device_driver_register(&device_driver) < 0) {
972                 connman_warn("Failed to initialize device driver for "
973                                 BLUEZ_SERVICE);
974                 connman_technology_driver_unregister(&tech_driver);
975                 goto out;
976         }
977
978         if (connman_network_driver_register(&network_driver) < 0) {
979                 connman_technology_driver_unregister(&tech_driver);
980                 connman_device_driver_unregister(&device_driver);
981                 goto out;
982         }
983
984         networks = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
985                         pan_free);
986
987         client = g_dbus_client_new(connection, BLUEZ_SERVICE, BLUEZ_PATH);
988         if (!client) {
989                 connman_warn("Failed to initialize D-Bus client for "
990                                 BLUEZ_SERVICE);
991                 goto out;
992         }
993
994         g_dbus_client_set_proxy_handlers(client, object_added, object_removed,
995                         NULL, NULL);
996
997         return 0;
998
999 out:
1000         if (networks)
1001                 g_hash_table_destroy(networks);
1002
1003         if (devices)
1004                 g_hash_table_destroy(devices);
1005
1006         if (client)
1007                 g_dbus_client_unref(client);
1008
1009         if (connection)
1010                 dbus_connection_unref(connection);
1011
1012         return -EIO;
1013 }
1014
1015 static void bluetooth_exit(void)
1016 {
1017         /*
1018          * We unset the disabling of the Bluetooth device when shutting down
1019          * so that non-PAN BT connections are not affected.
1020          */
1021         device_driver.disable = NULL;
1022
1023         g_dbus_client_unref(client);
1024
1025         connman_network_driver_unregister(&network_driver);
1026         g_hash_table_destroy(networks);
1027
1028         connman_device_driver_unregister(&device_driver);
1029         g_hash_table_destroy(devices);
1030
1031         connman_technology_driver_unregister(&tech_driver);
1032         dbus_connection_unref(connection);
1033 }
1034
1035 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
1036                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, bluetooth_init, bluetooth_exit)