Imported Upstream version 1.29
[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         connman_network_set_index(pan->network, index);
196         connman_network_set_connected(pan->network, true);
197
198         return true;
199 }
200
201 static void pan_connect_cb(DBusMessage *message, void *user_data)
202 {
203         const char *path = user_data;
204         const char *iface = NULL;
205         struct bluetooth_pan *pan;
206         DBusMessageIter iter;
207
208         pan = g_hash_table_lookup(networks, path);
209         if (!pan) {
210                 DBG("network already removed");
211                 return;
212         }
213
214         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
215                 const char *dbus_error = dbus_message_get_error_name(message);
216
217                 DBG("network %p %s", pan->network, dbus_error);
218
219                 if (strcmp(dbus_error,
220                                 "org.bluez.Error.AlreadyConnected") != 0) {
221                         connman_network_set_error(pan->network,
222                                 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
223                         return;
224                 }
225         } else {
226                 if (dbus_message_iter_init(message, &iter) &&
227                                 dbus_message_iter_get_arg_type(&iter) ==
228                                 DBUS_TYPE_STRING)
229                         dbus_message_iter_get_basic(&iter, &iface);
230         }
231
232         DBG("network %p interface %s", pan->network, iface);
233
234         pan_connect(pan, iface);
235 }
236
237 static void pan_connect_append(DBusMessageIter *iter,
238                 void *user_data)
239 {
240         const char *path = user_data;
241         struct bluetooth_pan *pan;
242
243         pan = g_hash_table_lookup(networks, path);
244         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &pan->pan_role);
245 }
246
247 static int bluetooth_pan_connect(struct connman_network *network)
248 {
249         struct bluetooth_pan *pan = connman_network_get_data(network);
250         const char *path;
251
252         DBG("network %p", network);
253
254         if (!pan)
255                 return -EINVAL;
256
257         path = g_dbus_proxy_get_path(pan->btnetwork_proxy);
258
259         if (!g_dbus_proxy_method_call(pan->btnetwork_proxy, "Connect",
260                         pan_connect_append, pan_connect_cb,
261                         g_strdup(path), g_free))
262                 return -EIO;
263
264         connman_network_set_associating(pan->network, true);
265
266         return -EINPROGRESS;
267 }
268
269 static void pan_disconnect_cb(DBusMessage *message, void *user_data)
270 {
271         const char *path = user_data;
272         struct bluetooth_pan *pan;
273
274         pan = g_hash_table_lookup(networks, path);
275         if (!pan) {
276                 DBG("network already removed");
277                 return;
278         }
279
280         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
281                 const char *dbus_error = dbus_message_get_error_name(message);
282
283                 DBG("network %p %s", pan->network, dbus_error);
284         }
285
286         DBG("network %p", pan->network);
287
288         connman_network_set_connected(pan->network, false);
289 }
290
291 static int bluetooth_pan_disconnect(struct connman_network *network)
292 {
293         struct bluetooth_pan *pan = connman_network_get_data(network);
294         const char *path;
295
296         DBG("network %p", network);
297
298         if (!pan)
299                 return -EINVAL;
300
301         path = g_dbus_proxy_get_path(pan->btnetwork_proxy);
302
303         if (!g_dbus_proxy_method_call(pan->btnetwork_proxy, "Disconnect",
304                         NULL, pan_disconnect_cb, g_strdup(path), g_free))
305                 return -EIO;
306
307        return -EINPROGRESS;
308 }
309
310 static void btnetwork_property_change(GDBusProxy *proxy, const char *name,
311                 DBusMessageIter *iter, void *user_data)
312 {
313         struct bluetooth_pan *pan;
314         dbus_bool_t connected;
315         bool proxy_connected, network_connected;
316
317         if (strcmp(name, "Connected") != 0)
318                 return;
319
320         pan = g_hash_table_lookup(networks, g_dbus_proxy_get_path(proxy));
321         if (!pan || !pan->network)
322                 return;
323
324         dbus_message_iter_get_basic(iter, &connected);
325         proxy_connected = connected;
326
327         network_connected = connman_network_get_connected(pan->network);
328
329         DBG("network %p network connected %d proxy connected %d",
330                         pan->network, network_connected, proxy_connected);
331
332        if (network_connected != proxy_connected)
333                connman_network_set_connected(pan->network, proxy_connected);
334 }
335
336 static void pan_create_nap(struct bluetooth_pan *pan)
337 {
338         struct connman_device *device;
339         const char* role;
340
341         role = proxy_get_role(pan->btdevice_proxy);
342         if (!role) {
343                 pan_remove_nap(pan);
344                 return;
345         }
346
347         device = g_hash_table_lookup(devices,
348                         proxy_get_string(pan->btdevice_proxy, "Adapter"));
349
350         if (!device || !connman_device_get_powered(device))
351                 return;
352
353         if (!pan->network) {
354                 const char *address;
355                 char ident[BLUETOOTH_ADDR_LEN * 2 + 1];
356                 const char *name, *path;
357
358                 address = proxy_get_string(pan->btdevice_proxy, "Address");
359                 if (!address) {
360                         connman_warn("Bluetooth device address missing");
361                         return;
362                 }
363
364                 address2ident(address, ident);
365
366                 pan->network = connman_network_create(ident,
367                                 CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
368
369                 name = proxy_get_string(pan->btdevice_proxy, "Alias");
370                 path = g_dbus_proxy_get_path(pan->btnetwork_proxy);
371
372                 DBG("network %p %s %s", pan->network, path, name);
373
374                 if (!pan->network) {
375                         connman_warn("Bluetooth network %s creation failed",
376                                         path);
377                         return;
378                 }
379
380                 connman_network_set_data(pan->network, pan);
381                 connman_network_set_name(pan->network, name);
382                 connman_network_set_group(pan->network, ident);
383         }
384
385         pan->pan_role = role;
386         connman_device_add_network(device, pan->network);
387
388         if (pan_connect(pan, NULL))
389                 DBG("network %p already connected", pan->network);
390 }
391
392 static void btdevice_property_change(GDBusProxy *proxy, const char *name,
393                 DBusMessageIter *iter, void *user_data)
394 {
395         struct bluetooth_pan *pan;
396         const char *old_role = NULL;
397         const char *new_role;
398
399         if (strcmp(name, "UUIDs"))
400                 return;
401
402         pan = g_hash_table_lookup(networks, g_dbus_proxy_get_path(proxy));
403         if (!pan)
404                 return;
405
406         if (pan->network &&
407                         connman_network_get_device(pan->network))
408                 old_role = pan->pan_role;
409         new_role = proxy_get_role(pan->btdevice_proxy);
410
411         DBG("network %p network role %s proxy role %s", pan->network, old_role,
412                         new_role);
413
414         if (old_role && new_role && !strcmp(old_role, new_role))
415                 return;
416
417         pan_create_nap(pan);
418 }
419
420 static void pan_free(gpointer data)
421 {
422         struct bluetooth_pan *pan = data;
423
424         if (pan->btnetwork_proxy) {
425                 g_dbus_proxy_unref(pan->btnetwork_proxy);
426                 pan->btnetwork_proxy = NULL;
427         }
428
429         if (pan->btdevice_proxy) {
430                 g_dbus_proxy_unref(pan->btdevice_proxy);
431                 pan->btdevice_proxy = NULL;
432         }
433
434         pan_remove_nap(pan);
435
436         g_free(pan);
437 }
438
439 static void pan_create(GDBusProxy *network_proxy)
440 {
441         const char *path = g_dbus_proxy_get_path(network_proxy);
442         struct bluetooth_pan *pan;
443
444         pan = g_try_new0(struct bluetooth_pan, 1);
445
446         if (!pan) {
447                 connman_error("Out of memory creating PAN NAP");
448                 return;
449         }
450
451         g_hash_table_replace(networks, g_strdup(path), pan);
452
453         pan->btnetwork_proxy = g_dbus_proxy_ref(network_proxy);
454         pan->btdevice_proxy = g_dbus_proxy_new(client, path,
455                         "org.bluez.Device1");
456
457         if (!pan->btdevice_proxy) {
458                 connman_error("Cannot create BT PAN watcher %s", path);
459                 g_hash_table_remove(networks, path);
460                 return;
461         }
462
463         g_dbus_proxy_set_property_watch(pan->btnetwork_proxy,
464                         btnetwork_property_change, NULL);
465
466         g_dbus_proxy_set_property_watch(pan->btdevice_proxy,
467                         btdevice_property_change, NULL);
468
469         DBG("pan %p %s role %s", pan, path, proxy_get_role(pan->btdevice_proxy));
470
471         pan_create_nap(pan);
472 }
473
474 static struct connman_network_driver network_driver = {
475         .name           = "bluetooth",
476         .type           = CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN,
477         .probe          = bluetooth_pan_probe,
478         .remove         = bluetooth_pan_remove,
479         .connect        = bluetooth_pan_connect,
480         .disconnect     = bluetooth_pan_disconnect,
481 };
482
483 static void enable_device(struct connman_device *device, const char *path)
484 {
485         GHashTableIter iter;
486         gpointer key, value;
487
488         DBG("device %p %s", device, path);
489         connman_device_set_powered(device, true);
490
491         g_hash_table_iter_init(&iter, networks);
492         while (g_hash_table_iter_next(&iter, &key, &value)) {
493                 struct bluetooth_pan *pan = value;
494
495                 if (g_strcmp0(proxy_get_string(pan->btdevice_proxy, "Adapter"),
496                                                 path) == 0) {
497
498                         DBG("enable network %p", pan->network);
499                         pan_create_nap(pan);
500                 }
501         }
502 }
503
504 static void device_enable_cb(const DBusError *error, void *user_data)
505 {
506         char *path = user_data;
507         struct connman_device *device;
508
509         device = g_hash_table_lookup(devices, path);
510         if (!device) {
511                 DBG("device already removed");
512                 goto out;
513         }
514
515         if (dbus_error_is_set(error)) {
516                 connman_warn("Bluetooth device %s not enabled %s",
517                                 path, error->message);
518                 goto out;
519         }
520
521         enable_device(device, path);
522 out:
523         g_free(path);
524 }
525
526 static int bluetooth_device_enable(struct connman_device *device)
527 {
528         GDBusProxy *proxy = connman_device_get_data(device);
529         dbus_bool_t device_powered = TRUE;
530         const char *path;
531
532         if (!proxy)
533                 return 0;
534
535         path = g_dbus_proxy_get_path(proxy);
536
537         if (proxy_get_bool(proxy, "Powered")) {
538                 DBG("already enabled %p %s", device, path);
539                 return -EALREADY;
540         }
541
542         DBG("device %p %s", device, path);
543
544         g_dbus_proxy_set_property_basic(proxy, "Powered",
545                         DBUS_TYPE_BOOLEAN, &device_powered,
546                         device_enable_cb, g_strdup(path), NULL);
547
548         return -EINPROGRESS;
549 }
550
551 static void disable_device(struct connman_device *device, const char *path)
552 {
553         GHashTableIter iter;
554         gpointer key, value;
555
556         DBG("device %p %s", device, path);
557         connman_device_set_powered(device, false);
558
559         g_hash_table_iter_init(&iter, networks);
560         while (g_hash_table_iter_next(&iter, &key, &value)) {
561                 struct bluetooth_pan *pan = value;
562
563                 if (pan->network && connman_network_get_device(pan->network)
564                                 == device) {
565                         DBG("disable network %p", pan->network);
566                         connman_device_remove_network(device, pan->network);
567                 }
568         }
569 }
570
571 static void device_disable_cb(const DBusError *error, void *user_data)
572 {
573         char *path = user_data;
574         struct connman_device *device;
575
576         device = g_hash_table_lookup(devices, path);
577         if (!device) {
578                 DBG("device already removed");
579                 goto out;
580         }
581
582         if (dbus_error_is_set(error)) {
583                 connman_warn("Bluetooth device %s not disabled: %s",
584                                 path, error->message);
585                 goto out;
586         }
587
588         disable_device(device, path);
589
590 out:
591         g_free(path);
592 }
593
594 static int bluetooth_device_disable(struct connman_device *device)
595 {
596         GDBusProxy *proxy = connman_device_get_data(device);
597         dbus_bool_t device_powered = FALSE;
598         const char *path;
599
600         if (!proxy)
601                 return 0;
602
603         path = g_dbus_proxy_get_path(proxy);
604
605         if (!proxy_get_bool(proxy, "Powered")) {
606                 DBG("already disabled %p %s", device, path);
607                 return -EALREADY;
608         }
609
610         DBG("device %p %s", device, path);
611
612         g_dbus_proxy_set_property_basic(proxy, "Powered",
613                         DBUS_TYPE_BOOLEAN, &device_powered,
614                         device_disable_cb, g_strdup(path), NULL);
615
616         return -EINPROGRESS;
617 }
618
619 static void adapter_property_change(GDBusProxy *proxy, const char *name,
620                 DBusMessageIter *iter, void *user_data)
621 {
622         struct connman_device *device;
623         const char *path;
624         bool adapter_powered, device_powered;
625
626         if (strcmp(name, "Powered") != 0)
627                 return;
628
629         path = g_dbus_proxy_get_path(proxy);
630         device = g_hash_table_lookup(devices, path);
631
632         adapter_powered = proxy_get_bool(proxy, "Powered");
633         device_powered = connman_device_get_powered(device);
634
635         DBG("device %p %s device powered %d adapter powered %d", device, path,
636                         device_powered, adapter_powered);
637
638         if (device_powered != adapter_powered) {
639                 if (adapter_powered)
640                         enable_device(device, path);
641                 else
642                         disable_device(device, path);
643         }
644 }
645
646 static void device_free(gpointer data)
647 {
648         struct connman_device *device = data;
649         GDBusProxy *proxy = connman_device_get_data(device);
650
651         connman_device_set_data(device, NULL);
652         if (proxy)
653                 g_dbus_proxy_unref(proxy);
654
655         connman_device_unregister(device);
656         connman_device_unref(device);
657 }
658
659 struct tethering_info {
660         struct connman_technology *technology;
661         char *bridge;
662         bool enable;
663 };
664
665 static void tethering_free(void *user_data)
666 {
667         struct tethering_info *tethering = user_data;
668
669         g_free(tethering->bridge);
670         g_free(tethering);
671 }
672
673 static void tethering_create_cb(DBusMessage *message, void *user_data)
674 {
675         struct tethering_info *tethering = user_data;
676
677         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
678                 const char *dbus_error = dbus_message_get_error_name(message);
679
680                 DBG("%s tethering failed: %s",
681                                 tethering->enable ? "enable" : "disable",
682                                 dbus_error);
683                 return;
684         }
685
686         DBG("bridge %s %s", tethering->bridge, tethering->enable ?
687                         "enabled": "disabled");
688
689         if (tethering->technology)
690                 connman_technology_tethering_notify(tethering->technology,
691                                 tethering->enable);
692 }
693
694 static void tethering_append(DBusMessageIter *iter, void *user_data)
695 {
696         struct tethering_info *tethering = user_data;
697         const char *nap = "nap";
698
699         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &nap);
700         if (tethering->enable)
701                 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
702                                 &tethering->bridge);
703 }
704
705 static bool tethering_create(const char *path,
706                 struct connman_technology *technology, const char *bridge,
707                 bool enabled)
708 {
709         struct tethering_info *tethering = g_new0(struct tethering_info, 1);
710         GDBusProxy *proxy;
711         const char *method;
712         bool result;
713
714         DBG("path %s bridge %s", path, bridge);
715
716         if (!bridge)
717                 return -EINVAL;
718
719         proxy = g_dbus_proxy_new(client, path, "org.bluez.NetworkServer1");
720         if (!proxy)
721                 return false;
722
723         tethering->technology = technology;
724         tethering->bridge = g_strdup(bridge);
725         tethering->enable = enabled;
726
727         if (tethering->enable)
728                 method = "Register";
729         else
730                 method = "Unregister";
731
732         result = g_dbus_proxy_method_call(proxy, method, tethering_append,
733                         tethering_create_cb, tethering, tethering_free);
734
735         g_dbus_proxy_unref(proxy);
736
737         return result;
738 }
739
740 static void device_create(GDBusProxy *proxy)
741 {
742         struct connman_device *device = NULL;
743         const char *path = g_dbus_proxy_get_path(proxy);
744         const char *address;
745         char ident[BLUETOOTH_ADDR_LEN * 2 + 1];
746         bool powered;
747
748         address = proxy_get_string(proxy, "Address");
749         if (!address)
750                 return;
751
752         address2ident(address, ident);
753
754         device = connman_device_create("bluetooth",
755                         CONNMAN_DEVICE_TYPE_BLUETOOTH);
756         if (!device)
757                 return;
758
759         connman_device_set_data(device, g_dbus_proxy_ref(proxy));
760         connman_device_set_ident(device, ident);
761
762         g_hash_table_replace(devices, g_strdup(path), device);
763
764         DBG("device %p %s device powered %d adapter powered %d", device,
765                         path, connman_device_get_powered(device),
766                         proxy_get_bool(proxy, "Powered"));
767
768         if (connman_device_register(device) < 0) {
769                 g_hash_table_remove(devices, device);
770                 return;
771         }
772
773         g_dbus_proxy_set_property_watch(proxy, adapter_property_change, NULL);
774
775         powered = proxy_get_bool(proxy, "Powered");
776         connman_device_set_powered(device, powered);
777
778         if (proxy_get_role(proxy) && !bluetooth_tethering)
779                 tethering_create(path, NULL, NULL, false);
780 }
781
782 static void object_added(GDBusProxy *proxy, void *user_data)
783 {
784         const char *interface;
785
786         interface = g_dbus_proxy_get_interface(proxy);
787         if (!interface) {
788                 connman_warn("Interface or proxy missing when adding "
789                                                         "bluetooth object");
790                 return;
791         }
792
793         if (strcmp(interface, "org.bluez.Adapter1") == 0) {
794                 DBG("%s %s", interface, g_dbus_proxy_get_path(proxy));
795                 device_create(proxy);
796                 return;
797         }
798
799         if (strcmp(interface, "org.bluez.Network1") == 0) {
800                 DBG("%s %s", interface, g_dbus_proxy_get_path(proxy));
801                 pan_create(proxy);
802                 return;
803         }
804 }
805
806 static void object_removed(GDBusProxy *proxy, void *user_data)
807 {
808         const char *interface, *path;
809
810         interface = g_dbus_proxy_get_interface(proxy);
811         if (!interface) {
812                 connman_warn("Interface or proxy missing when removing "
813                                                         "bluetooth object");
814                 return;
815         }
816
817         if (strcmp(interface, "org.bluez.Adapter1") == 0) {
818                 path = g_dbus_proxy_get_path(proxy);
819                 DBG("%s %s", interface, path);
820
821                 g_hash_table_remove(devices, path);
822         }
823
824         if (strcmp(interface, "org.bluez.Network1") == 0) {
825                 path = g_dbus_proxy_get_path(proxy);
826                 DBG("%s %s", interface, path);
827
828                 g_hash_table_remove(networks, path);
829         }
830
831 }
832
833 static int bluetooth_device_probe(struct connman_device *device)
834 {
835         GHashTableIter iter;
836         gpointer key, value;
837
838         g_hash_table_iter_init(&iter, devices);
839
840         while (g_hash_table_iter_next(&iter, &key, &value)) {
841                 struct connman_device *known = value;
842
843                 if (device == known)
844                         return 0;
845         }
846
847         return -EOPNOTSUPP;
848 }
849
850 static void bluetooth_device_remove(struct connman_device *device)
851 {
852         DBG("%p", device);
853 }
854
855 static struct connman_device_driver device_driver = {
856         .name           = "bluetooth",
857         .type           = CONNMAN_DEVICE_TYPE_BLUETOOTH,
858         .probe          = bluetooth_device_probe,
859         .remove         = bluetooth_device_remove,
860         .enable         = bluetooth_device_enable,
861         .disable        = bluetooth_device_disable,
862 };
863
864 static int bluetooth_tech_probe(struct connman_technology *technology)
865 {
866         return 0;
867 }
868
869 static void bluetooth_tech_remove(struct connman_technology *technology)
870 {
871
872 }
873
874 static int bluetooth_tech_set_tethering(struct connman_technology *technology,
875                 const char *identifier, const char *passphrase,
876                 const char *bridge, bool enabled)
877 {
878         GHashTableIter hash_iter;
879         gpointer key, value;
880         int i = 0;
881
882         bluetooth_tethering = enabled;
883
884         g_hash_table_iter_init(&hash_iter, devices);
885
886         while (g_hash_table_iter_next(&hash_iter, &key, &value)) {
887                 const char *path = key;
888                 struct connman_device *device = value;
889
890                 DBG("device %p", device);
891
892                 if (tethering_create(path, technology, bridge, enabled)
893                                 )
894                         i++;
895         }
896
897         DBG("%s %d device(s)", enabled ? "enabled" : "disabled", i);
898
899         if (i == 0)
900                 return -ENODEV;
901
902         return 0;
903 }
904
905 static struct connman_technology_driver tech_driver = {
906         .name           = "bluetooth",
907         .type           = CONNMAN_SERVICE_TYPE_BLUETOOTH,
908         .probe          = bluetooth_tech_probe,
909         .remove         = bluetooth_tech_remove,
910         .set_tethering  = bluetooth_tech_set_tethering,
911 };
912
913 static int bluetooth_init(void)
914 {
915         connection = connman_dbus_get_connection();
916         if (!connection)
917                 goto out;
918
919         if (connman_technology_driver_register(&tech_driver) < 0) {
920                 connman_warn("Failed to initialize technology for Bluez 5");
921                 goto out;
922         }
923
924         devices = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
925                         device_free);
926
927         if (connman_device_driver_register(&device_driver) < 0) {
928                 connman_warn("Failed to initialize device driver for "
929                                 BLUEZ_SERVICE);
930                 connman_technology_driver_unregister(&tech_driver);
931                 goto out;
932         }
933
934         if (connman_network_driver_register(&network_driver) < 0) {
935                 connman_technology_driver_unregister(&tech_driver);
936                 connman_device_driver_unregister(&device_driver);
937                 goto out;
938         }
939
940         networks = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
941                         pan_free);
942
943         client = g_dbus_client_new(connection, BLUEZ_SERVICE, BLUEZ_PATH);
944         if (!client) {
945                 connman_warn("Failed to initialize D-Bus client for "
946                                 BLUEZ_SERVICE);
947                 goto out;
948         }
949
950         g_dbus_client_set_proxy_handlers(client, object_added, object_removed,
951                         NULL, NULL);
952
953         return 0;
954
955 out:
956         if (networks)
957                 g_hash_table_destroy(networks);
958
959         if (devices)
960                 g_hash_table_destroy(devices);
961
962         if (client)
963                 g_dbus_client_unref(client);
964
965         if (connection)
966                 dbus_connection_unref(connection);
967
968         return -EIO;
969 }
970
971 static void bluetooth_exit(void)
972 {
973         /*
974          * We unset the disabling of the Bluetooth device when shutting down
975          * so that non-PAN BT connections are not affected.
976          */
977         device_driver.disable = NULL;
978
979         g_dbus_client_unref(client);
980
981         connman_network_driver_unregister(&network_driver);
982         g_hash_table_destroy(networks);
983
984         connman_device_driver_unregister(&device_driver);
985         g_hash_table_destroy(devices);
986
987         connman_technology_driver_unregister(&tech_driver);
988         dbus_connection_unref(connection);
989 }
990
991 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
992                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, bluetooth_init, bluetooth_exit)