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