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