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