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