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