41438f44ffce0a6d69c2e48733335e532250457d
[platform/upstream/connman.git] / plugins / bluetooth_legacy.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 <stdio.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <netinet/ether.h>
31
32 #include <gdbus.h>
33
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/plugin.h>
36 #include <connman/technology.h>
37 #include <connman/device.h>
38 #include <connman/inet.h>
39 #include <connman/dbus.h>
40 #include <connman/log.h>
41
42 #define BLUEZ_SERVICE                   "org.bluez"
43 #define BLUEZ_MANAGER_INTERFACE         BLUEZ_SERVICE ".Manager"
44 #define BLUEZ_ADAPTER_INTERFACE         BLUEZ_SERVICE ".Adapter"
45 #define BLUEZ_DEVICE_INTERFACE          BLUEZ_SERVICE ".Device"
46 #define BLUEZ_NETWORK_INTERFACE         BLUEZ_SERVICE ".Network"
47 #define BLUEZ_NETWORK_SERVER            BLUEZ_SERVICE ".NetworkServer"
48
49 #define LIST_ADAPTERS                   "ListAdapters"
50 #define ADAPTER_ADDED                   "AdapterAdded"
51 #define ADAPTER_REMOVED                 "AdapterRemoved"
52 #define DEVICE_REMOVED                  "DeviceRemoved"
53
54 #define PROPERTY_CHANGED                "PropertyChanged"
55 #define GET_PROPERTIES                  "GetProperties"
56 #define SET_PROPERTY                    "SetProperty"
57
58 #define CONNECT                         "Connect"
59 #define DISCONNECT                      "Disconnect"
60
61 #define REGISTER                        "Register"
62 #define UNREGISTER                      "Unregister"
63
64 #define UUID_NAP        "00001116-0000-1000-8000-00805f9b34fb"
65
66 #define TIMEOUT 60000
67
68 static DBusConnection *connection;
69
70 static GHashTable *bluetooth_devices = NULL;
71 static GHashTable *bluetooth_networks = NULL;
72 static GHashTable *pending_networks = NULL;
73
74 static int pan_probe(struct connman_network *network)
75 {
76         GHashTableIter iter;
77         gpointer key, val;
78
79         g_hash_table_iter_init(&iter, bluetooth_networks);
80         while (g_hash_table_iter_next(&iter, &key, &val) == TRUE) {
81                 struct connman_network *known = val;
82
83                 if (network != known)
84                         continue;
85
86                 DBG("network %p", network);
87
88                 return 0;
89         }
90
91         return -EOPNOTSUPP;
92 }
93
94 static void pan_remove(struct connman_network *network)
95 {
96         DBG("network %p", network);
97 }
98
99 static void connect_reply(DBusPendingCall *call, void *user_data)
100 {
101         struct connman_network *network = user_data;
102         DBusMessage *reply;
103         DBusError error;
104         const char *interface = NULL;
105         int index;
106
107         DBG("network %p", network);
108
109         reply = dbus_pending_call_steal_reply(call);
110
111         dbus_error_init(&error);
112
113         if (dbus_set_error_from_message(&error, reply) == TRUE) {
114                 connman_error("%s", error.message);
115                 dbus_error_free(&error);
116
117                 goto err;
118         }
119
120         if (dbus_message_get_args(reply, &error,
121                                         DBUS_TYPE_STRING, &interface,
122                                                 DBUS_TYPE_INVALID) == FALSE) {
123                 if (dbus_error_is_set(&error) == TRUE) {
124                         connman_error("%s", error.message);
125                         dbus_error_free(&error);
126                 } else
127                         connman_error("Wrong arguments for connect");
128                 goto err;
129         }
130
131         if (interface == NULL)
132                 goto err;
133
134         DBG("interface %s", interface);
135
136         index = connman_inet_ifindex(interface);
137
138         connman_network_set_index(network, index);
139
140         connman_network_set_connected(network, TRUE);
141
142         dbus_message_unref(reply);
143
144         dbus_pending_call_unref(call);
145
146         return;
147 err:
148
149         connman_network_set_connected(network, FALSE);
150
151         dbus_message_unref(reply);
152
153         dbus_pending_call_unref(call);
154 }
155
156 static int pan_connect(struct connman_network *network)
157 {
158         const char *path = connman_network_get_string(network, "Path");
159         const char *uuid = "nap";
160         DBusMessage *message;
161         DBusPendingCall *call;
162
163         DBG("network %p", network);
164
165         if (path == NULL)
166                 return -EINVAL;
167
168         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
169                                         BLUEZ_NETWORK_INTERFACE, CONNECT);
170         if (message == NULL)
171                 return -ENOMEM;
172
173         dbus_message_set_auto_start(message, FALSE);
174
175         dbus_message_append_args(message, DBUS_TYPE_STRING, &uuid,
176                                                         DBUS_TYPE_INVALID);
177
178         if (dbus_connection_send_with_reply(connection, message,
179                                         &call, TIMEOUT * 10) == FALSE) {
180                 connman_error("Failed to connect service");
181                 dbus_message_unref(message);
182                 return -EINVAL;
183         }
184
185         if (call == NULL) {
186                 connman_error("D-Bus connection not available");
187                 dbus_message_unref(message);
188                 return -EINVAL;
189         }
190
191         dbus_pending_call_set_notify(call, connect_reply, network, NULL);
192
193         dbus_message_unref(message);
194
195         return -EINPROGRESS;
196 }
197
198 static void disconnect_reply(DBusPendingCall *call, void *user_data)
199 {
200         struct connman_network *network = user_data;
201         DBusMessage *reply;
202         DBusError error;
203
204         DBG("network %p", network);
205
206         reply = dbus_pending_call_steal_reply(call);
207
208         dbus_error_init(&error);
209
210         if (dbus_set_error_from_message(&error, reply) == TRUE) {
211                 connman_error("%s", error.message);
212                 dbus_error_free(&error);
213                 goto done;
214         }
215
216         if (dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) == FALSE) {
217                 if (dbus_error_is_set(&error) == TRUE) {
218                         connman_error("%s", error.message);
219                         dbus_error_free(&error);
220                 } else
221                         connman_error("Wrong arguments for disconnect");
222                 goto done;
223         }
224
225         connman_network_set_connected(network, FALSE);
226
227 done:
228         dbus_message_unref(reply);
229
230         dbus_pending_call_unref(call);
231
232         connman_network_unref(network);
233 }
234
235 static int pan_disconnect(struct connman_network *network)
236 {
237         const char *path = connman_network_get_string(network, "Path");
238         DBusMessage *message;
239         DBusPendingCall *call;
240
241         DBG("network %p", network);
242
243         if (path == NULL)
244                 return -EINVAL;
245
246         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
247                                         BLUEZ_NETWORK_INTERFACE, DISCONNECT);
248         if (message == NULL)
249                 return -ENOMEM;
250
251         dbus_message_set_auto_start(message, FALSE);
252
253         dbus_message_append_args(message, DBUS_TYPE_INVALID);
254
255         if (dbus_connection_send_with_reply(connection, message,
256                                                 &call, TIMEOUT) == FALSE) {
257                 connman_error("Failed to disconnect service");
258                 dbus_message_unref(message);
259                 return -EINVAL;
260         }
261
262         if (call == NULL) {
263                 connman_error("D-Bus connection not available");
264                 dbus_message_unref(message);
265                 return -EINVAL;
266         }
267
268         connman_network_ref(network);
269
270         connman_network_set_associating(network, FALSE);
271
272         dbus_pending_call_set_notify(call, disconnect_reply, network, NULL);
273
274         dbus_message_unref(message);
275
276         return 0;
277 }
278
279 static struct connman_network_driver pan_driver = {
280         .name           = "bluetooth_legacy-pan",
281         .type           = CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN,
282         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
283         .probe          = pan_probe,
284         .remove         = pan_remove,
285         .connect        = pan_connect,
286         .disconnect     = pan_disconnect,
287 };
288
289 static gboolean network_changed(DBusConnection *conn,
290                                 DBusMessage *message, void *user_data)
291 {
292         const char *path = dbus_message_get_path(message);
293         struct connman_network *network;
294         DBusMessageIter iter, value;
295         const char *key;
296
297         DBG("path %s", path);
298
299         network = g_hash_table_lookup(bluetooth_networks, path);
300         if (network == NULL)
301                 return TRUE;
302
303         if (dbus_message_iter_init(message, &iter) == FALSE)
304                 return TRUE;
305
306         dbus_message_iter_get_basic(&iter, &key);
307
308         dbus_message_iter_next(&iter);
309         dbus_message_iter_recurse(&iter, &value);
310
311         if (g_str_equal(key, "Connected") == TRUE) {
312                 dbus_bool_t connected;
313
314                 dbus_message_iter_get_basic(&value, &connected);
315
316                 if (connected == TRUE)
317                         return TRUE;
318
319                 connman_network_set_associating(network, FALSE);
320                 connman_network_set_connected(network, FALSE);
321         }
322
323         return TRUE;
324 }
325
326 static void extract_properties(DBusMessage *reply, const char **parent,
327                                                 const char **address,
328                                                 const char **name,
329                                                 const char **alias,
330                                                 dbus_bool_t *powered,
331                                                 dbus_bool_t *scanning,
332                                                 DBusMessageIter *uuids,
333                                                 DBusMessageIter *networks)
334 {
335         DBusMessageIter array, dict;
336
337         if (dbus_message_iter_init(reply, &array) == FALSE)
338                 return;
339
340         if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
341                 return;
342
343         dbus_message_iter_recurse(&array, &dict);
344
345         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
346                 DBusMessageIter entry, value;
347                 const char *key;
348
349                 dbus_message_iter_recurse(&dict, &entry);
350                 dbus_message_iter_get_basic(&entry, &key);
351
352                 dbus_message_iter_next(&entry);
353                 dbus_message_iter_recurse(&entry, &value);
354
355                 if (g_str_equal(key, "Adapter") == TRUE) {
356                         if (parent != NULL)
357                                 dbus_message_iter_get_basic(&value, parent);
358                 } else if (g_str_equal(key, "Address") == TRUE) {
359                         if (address != NULL)
360                                 dbus_message_iter_get_basic(&value, address);
361                 } else if (g_str_equal(key, "Name") == TRUE) {
362                         if (name != NULL)
363                                 dbus_message_iter_get_basic(&value, name);
364                 } else if (g_str_equal(key, "Alias") == TRUE) {
365                         if (alias != NULL)
366                                 dbus_message_iter_get_basic(&value, alias);
367                 } else if (g_str_equal(key, "Powered") == TRUE) {
368                         if (powered != NULL)
369                                 dbus_message_iter_get_basic(&value, powered);
370                 } else if (g_str_equal(key, "Discovering") == TRUE) {
371                         if (scanning != NULL)
372                                 dbus_message_iter_get_basic(&value, scanning);
373                 } else if (g_str_equal(key, "Devices") == TRUE) {
374                         if (networks != NULL)
375                                 memcpy(networks, &value, sizeof(value));
376                 } else if (g_str_equal(key, "UUIDs") == TRUE) {
377                         if (uuids != NULL)
378                                 memcpy(uuids, &value, sizeof(value));
379                 }
380
381                 dbus_message_iter_next(&dict);
382         }
383 }
384
385 static dbus_bool_t has_pan(DBusMessageIter *array)
386 {
387         DBusMessageIter value;
388
389         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
390                 return FALSE;
391
392         dbus_message_iter_recurse(array, &value);
393
394         while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
395                 const char *uuid;
396
397                 dbus_message_iter_get_basic(&value, &uuid);
398
399                 if (g_strcmp0(uuid, UUID_NAP) == 0)
400                         return TRUE;
401
402                 dbus_message_iter_next(&value);
403         }
404
405         return FALSE;
406 }
407
408 static void network_properties_reply(DBusPendingCall *call, void *user_data)
409 {
410         char *path = user_data;
411         struct connman_device *device;
412         struct connman_network *network;
413         DBusMessage *reply;
414         DBusMessageIter uuids;
415         const char *parent = NULL, *address = NULL, *name = NULL;
416         struct ether_addr addr;
417         char ident[13];
418
419         reply = dbus_pending_call_steal_reply(call);
420
421         extract_properties(reply, &parent, &address, NULL, &name,
422                                                 NULL, NULL, &uuids, NULL);
423
424         if (parent == NULL)
425                 goto done;
426
427         device = g_hash_table_lookup(bluetooth_devices, parent);
428         if (device == NULL)
429                 goto done;
430
431         if (address == NULL)
432                 goto done;
433
434         ether_aton_r(address, &addr);
435
436         snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
437                                                 addr.ether_addr_octet[0],
438                                                 addr.ether_addr_octet[1],
439                                                 addr.ether_addr_octet[2],
440                                                 addr.ether_addr_octet[3],
441                                                 addr.ether_addr_octet[4],
442                                                 addr.ether_addr_octet[5]);
443
444         if (has_pan(&uuids) == FALSE)
445                 goto done;
446
447         network = connman_device_get_network(device, ident);
448         if (network != NULL)
449                 goto done;
450
451         network = connman_network_create(ident,
452                                         CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
453         if (network == NULL)
454                 goto done;
455
456         connman_network_set_string(network, "Path", path);
457
458         connman_network_set_name(network, name);
459
460         g_hash_table_replace(bluetooth_networks, g_strdup(path), network);
461
462         connman_device_add_network(device, network);
463
464         connman_network_set_group(network, ident);
465
466 done:
467         dbus_message_unref(reply);
468
469         dbus_pending_call_unref(call);
470 }
471
472 static void add_network(const char *path)
473 {
474         DBusMessage *message;
475         DBusPendingCall *call;
476
477         DBG("path %s", path);
478
479         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
480                                 BLUEZ_DEVICE_INTERFACE, GET_PROPERTIES);
481         if (message == NULL)
482                 return;
483
484         dbus_message_set_auto_start(message, FALSE);
485
486         if (dbus_connection_send_with_reply(connection, message,
487                                                 &call, TIMEOUT) == FALSE) {
488                 connman_error("Failed to get network properties for %s", path);
489                 goto done;
490         }
491
492         if (call == NULL) {
493                 connman_error("D-Bus connection not available");
494                 goto done;
495         }
496
497         dbus_pending_call_set_notify(call, network_properties_reply,
498                                                 g_strdup(path), g_free);
499
500 done:
501         dbus_message_unref(message);
502 }
503
504 static void check_networks(DBusMessageIter *array)
505 {
506         DBusMessageIter value;
507
508         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
509                 return;
510
511         dbus_message_iter_recurse(array, &value);
512
513         while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
514                 const char *path;
515
516                 dbus_message_iter_get_basic(&value, &path);
517
518                 add_network(path);
519
520                 dbus_message_iter_next(&value);
521         }
522 }
523
524 static void check_pending_networks(const char *adapter)
525 {
526         GSList *networks, *list;
527
528         networks = g_hash_table_lookup(pending_networks, adapter);
529         if (networks == NULL)
530                 return;
531
532         for (list = networks; list != NULL; list = list->next) {
533                 char *path = list->data;
534
535                 add_network(path);
536         }
537
538         g_hash_table_remove(pending_networks, adapter);
539 }
540
541 static gboolean adapter_changed(DBusConnection *conn,
542                                 DBusMessage *message, void *user_data)
543 {
544         const char *path = dbus_message_get_path(message);
545         struct connman_device *device;
546         DBusMessageIter iter, value;
547         const char *key;
548
549         DBG("path %s", path);
550
551         device = g_hash_table_lookup(bluetooth_devices, path);
552         if (device == NULL)
553                 return TRUE;
554
555         if (dbus_message_iter_init(message, &iter) == FALSE)
556                 return TRUE;
557
558         dbus_message_iter_get_basic(&iter, &key);
559
560         dbus_message_iter_next(&iter);
561         dbus_message_iter_recurse(&iter, &value);
562
563         if (g_str_equal(key, "Powered") == TRUE) {
564                 dbus_bool_t val;
565
566                 dbus_message_iter_get_basic(&value, &val);
567                 connman_device_set_powered(device, val);
568                 if (val == TRUE)
569                         check_pending_networks(path);
570         } else if (g_str_equal(key, "Discovering") == TRUE) {
571                 dbus_bool_t val;
572
573                 dbus_message_iter_get_basic(&value, &val);
574                 connman_device_set_scanning(device, val);
575         } else if (g_str_equal(key, "Devices") == TRUE) {
576                 check_networks(&value);
577         }
578
579         return TRUE;
580 }
581
582 static gboolean device_removed(DBusConnection *conn,
583                                 DBusMessage *message, void *user_data)
584 {
585         const char *network_path;
586         struct connman_network *network;
587         struct connman_device *device;
588         DBusMessageIter iter;
589
590         DBG("");
591
592         if (dbus_message_iter_init(message, &iter) == FALSE)
593                 return TRUE;
594
595         dbus_message_iter_get_basic(&iter, &network_path);
596
597         network = g_hash_table_lookup(bluetooth_networks, network_path);
598         if (network == NULL)
599                 return TRUE;
600
601         device = connman_network_get_device(network);
602         if (device == NULL)
603                 return TRUE;
604
605         g_hash_table_remove(bluetooth_networks, network_path);
606
607         return TRUE;
608 }
609
610 static gboolean device_changed(DBusConnection *conn,
611                                 DBusMessage *message, void *user_data)
612 {
613         const char *path = dbus_message_get_path(message);
614         DBusMessageIter iter, value;
615         const char *key;
616
617         DBG("path %s", path);
618
619         if (dbus_message_iter_init(message, &iter) == FALSE)
620                 return TRUE;
621
622         dbus_message_iter_get_basic(&iter, &key);
623
624         dbus_message_iter_next(&iter);
625         dbus_message_iter_recurse(&iter, &value);
626
627         DBG("key %s", key);
628
629         if (g_str_equal(key, "UUIDs") == TRUE)
630                 add_network(path);
631
632         return TRUE;
633 }
634
635 static void remove_device_networks(struct connman_device *device)
636 {
637         GHashTableIter iter;
638         gpointer key, value;
639         GSList *key_list = NULL;
640         GSList *list;
641
642         if (bluetooth_networks == NULL)
643                 return;
644
645         g_hash_table_iter_init(&iter, bluetooth_networks);
646
647         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
648                 struct connman_network *network = value;
649
650                 if (connman_network_get_device(network) != device)
651                         continue;
652
653                 key_list = g_slist_prepend(key_list, key);
654         }
655
656         for (list = key_list; list != NULL; list = list->next) {
657                 const char *network_path = list->data;
658
659                 g_hash_table_remove(bluetooth_networks, network_path);
660         }
661
662         g_slist_free(key_list);
663 }
664
665 static void add_pending_networks(const char *adapter, DBusMessageIter *array)
666 {
667         DBusMessageIter value;
668         GSList *list = NULL;
669
670         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
671                 return;
672
673         dbus_message_iter_recurse(array, &value);
674
675         while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
676                 const char *path;
677
678                 dbus_message_iter_get_basic(&value, &path);
679
680                 list = g_slist_prepend(list, g_strdup(path));
681
682                 dbus_message_iter_next(&value);
683         }
684
685         if (list == NULL)
686                 return;
687
688         g_hash_table_replace(pending_networks, g_strdup(adapter), list);
689 }
690
691 static void adapter_properties_reply(DBusPendingCall *call, void *user_data)
692 {
693         char *path = user_data;
694         struct connman_device *device;
695         DBusMessage *reply;
696         DBusMessageIter networks;
697         const char *address = NULL, *name = NULL;
698         dbus_bool_t powered = FALSE, scanning = FALSE;
699         struct ether_addr addr;
700         char ident[13];
701
702         DBG("path %s", path);
703
704         reply = dbus_pending_call_steal_reply(call);
705
706         if (path == NULL)
707                 goto done;
708
709         extract_properties(reply, NULL, &address, &name, NULL,
710                                         &powered, &scanning, NULL, &networks);
711
712         if (address == NULL)
713                 goto done;
714
715         if (g_strcmp0(address, "00:00:00:00:00:00") == 0)
716                 goto done;
717
718         device = g_hash_table_lookup(bluetooth_devices, path);
719         if (device != NULL)
720                 goto update;
721
722         ether_aton_r(address, &addr);
723
724         snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
725                                                 addr.ether_addr_octet[0],
726                                                 addr.ether_addr_octet[1],
727                                                 addr.ether_addr_octet[2],
728                                                 addr.ether_addr_octet[3],
729                                                 addr.ether_addr_octet[4],
730                                                 addr.ether_addr_octet[5]);
731
732         device = connman_device_create("bluetooth_legacy",
733                         CONNMAN_DEVICE_TYPE_BLUETOOTH);
734         if (device == NULL)
735                 goto done;
736
737         g_hash_table_insert(bluetooth_devices, g_strdup(path), device);
738
739         connman_device_set_ident(device, ident);
740
741         connman_device_set_string(device, "Path", path);
742
743         if (connman_device_register(device) < 0) {
744                 connman_device_unref(device);
745                 g_hash_table_remove(bluetooth_devices, path);
746                 goto done;
747         }
748
749 update:
750         connman_device_set_string(device, "Address", address);
751         connman_device_set_string(device, "Name", name);
752         connman_device_set_string(device, "Path", path);
753
754         connman_device_set_powered(device, powered);
755         connman_device_set_scanning(device, scanning);
756
757         if (powered == FALSE) {
758                 remove_device_networks(device);
759                 add_pending_networks(path, &networks);
760         } else
761                 check_networks(&networks);
762
763 done:
764         dbus_message_unref(reply);
765
766         dbus_pending_call_unref(call);
767 }
768
769 static void add_adapter(DBusConnection *conn, const char *path)
770 {
771         DBusMessage *message;
772         DBusPendingCall *call;
773
774         DBG("path %s", path);
775
776         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
777                                 BLUEZ_ADAPTER_INTERFACE, GET_PROPERTIES);
778         if (message == NULL)
779                 return;
780
781         dbus_message_set_auto_start(message, FALSE);
782
783         if (dbus_connection_send_with_reply(conn, message,
784                                                 &call, TIMEOUT) == FALSE) {
785                 connman_error("Failed to get adapter properties for %s", path);
786                 goto done;
787         }
788
789         if (call == NULL) {
790                 connman_error("D-Bus connection not available");
791                 goto done;
792         }
793
794         dbus_pending_call_set_notify(call, adapter_properties_reply,
795                                                 g_strdup(path), g_free);
796
797 done:
798         dbus_message_unref(message);
799 }
800
801 static gboolean adapter_added(DBusConnection *conn, DBusMessage *message,
802                                 void *user_data)
803 {
804         const char *path;
805
806         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
807                                 DBUS_TYPE_INVALID);
808         add_adapter(conn, path);
809         return TRUE;
810 }
811
812 static void remove_adapter(DBusConnection *conn, const char *path)
813 {
814         DBG("path %s", path);
815
816         g_hash_table_remove(bluetooth_devices, path);
817         g_hash_table_remove(pending_networks, path);
818 }
819
820 static gboolean adapter_removed(DBusConnection *conn, DBusMessage *message,
821                                 void *user_data)
822 {
823         const char *path;
824
825         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
826                                 DBUS_TYPE_INVALID);
827         remove_adapter(conn, path);
828         return TRUE;
829 }
830
831 static void list_adapters_reply(DBusPendingCall *call, void *user_data)
832 {
833         DBusMessage *reply;
834         DBusError error;
835         char **adapters;
836         int i, num_adapters;
837
838         DBG("");
839
840         reply = dbus_pending_call_steal_reply(call);
841
842         dbus_error_init(&error);
843
844         if (dbus_set_error_from_message(&error, reply) == TRUE) {
845                 connman_error("%s", error.message);
846                 dbus_error_free(&error);
847                 goto done;
848         }
849
850         if (dbus_message_get_args(reply, &error,
851                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
852                                                 &adapters, &num_adapters,
853                                                 DBUS_TYPE_INVALID) == FALSE) {
854                 if (dbus_error_is_set(&error) == TRUE) {
855                         connman_error("%s", error.message);
856                         dbus_error_free(&error);
857                 } else
858                         connman_error("Wrong arguments for adapter list");
859                 goto done;
860         }
861
862         for (i = 0; i < num_adapters; i++)
863                 add_adapter(connection, adapters[i]);
864
865         g_strfreev(adapters);
866
867 done:
868         dbus_message_unref(reply);
869
870         dbus_pending_call_unref(call);
871 }
872
873 static void unregister_device(gpointer data)
874 {
875         struct connman_device *device = data;
876
877         DBG("");
878
879         remove_device_networks(device);
880
881         connman_device_unregister(device);
882         connman_device_unref(device);
883 }
884
885 static void remove_network(gpointer data)
886 {
887         struct connman_network *network = data;
888         struct connman_device *device;
889
890         DBG("network %p", network);
891
892         device = connman_network_get_device(network);
893         if (device != NULL)
894                 connman_device_remove_network(device, network);
895
896         connman_network_unref(network);
897 }
898
899 static void remove_pending_networks(gpointer data)
900 {
901         GSList *list = data;
902
903         g_slist_free_full(list, g_free);
904 }
905
906 static void bluetooth_connect(DBusConnection *conn, void *user_data)
907 {
908         DBusMessage *message;
909         DBusPendingCall *call;
910
911         DBG("connection %p", conn);
912
913         bluetooth_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
914                                                 g_free, unregister_device);
915
916         bluetooth_networks = g_hash_table_new_full(g_str_hash, g_str_equal,
917                                                 g_free, remove_network);
918
919         pending_networks = g_hash_table_new_full(g_str_hash, g_str_equal,
920                                         g_free, remove_pending_networks);
921
922         message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
923                                 BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS);
924         if (message == NULL)
925                 return;
926
927         dbus_message_set_auto_start(message, FALSE);
928
929         if (dbus_connection_send_with_reply(conn, message,
930                                                 &call, TIMEOUT) == FALSE) {
931                 connman_error("Failed to get Bluetooth adapters");
932                 goto done;
933         }
934
935         if (call == NULL) {
936                 connman_error("D-Bus connection not available");
937                 goto done;
938         }
939
940         dbus_pending_call_set_notify(call, list_adapters_reply, NULL, NULL);
941
942 done:
943         dbus_message_unref(message);
944 }
945
946 static void bluetooth_disconnect(DBusConnection *conn, void *user_data)
947 {
948         DBG("connection %p", conn);
949
950         if (bluetooth_devices == NULL)
951                 return;
952
953         g_hash_table_destroy(bluetooth_networks);
954         bluetooth_networks = NULL;
955         g_hash_table_destroy(bluetooth_devices);
956         bluetooth_devices = NULL;
957         g_hash_table_destroy(pending_networks);
958         pending_networks = NULL;
959 }
960
961 static int bluetooth_probe(struct connman_device *device)
962 {
963         GHashTableIter iter;
964         gpointer key, value;
965
966         DBG("device %p", device);
967
968         if (bluetooth_devices == NULL)
969                 return -ENOTSUP;
970
971         g_hash_table_iter_init(&iter, bluetooth_devices);
972
973         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
974                 struct connman_device *device_pan = value;
975
976                 if (device == device_pan)
977                         return 0;
978         }
979
980         return -ENOTSUP;
981 }
982
983 static void bluetooth_remove(struct connman_device *device)
984 {
985         DBG("device %p", device);
986 }
987
988 static void powered_reply(DBusPendingCall *call, void *user_data)
989 {
990         DBusError error;
991         DBusMessage *reply;
992
993         DBG("");
994
995         reply = dbus_pending_call_steal_reply(call);
996
997         dbus_error_init(&error);
998
999         if (dbus_set_error_from_message(&error, reply) == TRUE) {
1000                 connman_error("%s", error.message);
1001                 dbus_error_free(&error);
1002                 dbus_message_unref(reply);
1003                 dbus_pending_call_unref(call);
1004                 return;
1005         }
1006
1007         dbus_message_unref(reply);
1008         dbus_pending_call_unref(call);
1009
1010         add_adapter(connection, user_data);
1011 }
1012
1013 static int change_powered(DBusConnection *conn, const char *path,
1014                                                         dbus_bool_t powered)
1015 {
1016         DBusMessage *message;
1017         DBusMessageIter iter;
1018         DBusPendingCall *call;
1019
1020         DBG("");
1021
1022         if (path == NULL)
1023                 return -EINVAL;
1024
1025         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
1026                                         BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY);
1027         if (message == NULL)
1028                 return -ENOMEM;
1029
1030         dbus_message_set_auto_start(message, FALSE);
1031
1032         dbus_message_iter_init_append(message, &iter);
1033         connman_dbus_property_append_basic(&iter, "Powered",
1034                                                 DBUS_TYPE_BOOLEAN, &powered);
1035
1036         if (dbus_connection_send_with_reply(conn, message,
1037                                                 &call, TIMEOUT) == FALSE) {
1038                 connman_error("Failed to change Powered property");
1039                 dbus_message_unref(message);
1040                 return -EINVAL;
1041         }
1042
1043         if (call == NULL) {
1044                 connman_error("D-Bus connection not available");
1045                 dbus_message_unref(message);
1046                 return -EINVAL;
1047         }
1048
1049         dbus_pending_call_set_notify(call, powered_reply,
1050                                         g_strdup(path), g_free);
1051
1052         dbus_message_unref(message);
1053
1054         return -EINPROGRESS;
1055 }
1056
1057 static int bluetooth_enable(struct connman_device *device)
1058 {
1059         const char *path = connman_device_get_string(device, "Path");
1060
1061         DBG("device %p", device);
1062
1063         return change_powered(connection, path, TRUE);
1064 }
1065
1066 static int bluetooth_disable(struct connman_device *device)
1067 {
1068         const char *path = connman_device_get_string(device, "Path");
1069
1070         DBG("device %p", device);
1071
1072         return change_powered(connection, path, FALSE);
1073 }
1074
1075 static struct connman_device_driver bluetooth_driver = {
1076         .name           = "bluetooth_legacy",
1077         .type           = CONNMAN_DEVICE_TYPE_BLUETOOTH,
1078         .probe          = bluetooth_probe,
1079         .remove         = bluetooth_remove,
1080         .enable         = bluetooth_enable,
1081         .disable        = bluetooth_disable,
1082 };
1083
1084 static int tech_probe(struct connman_technology *technology)
1085 {
1086         return 0;
1087 }
1088
1089 static void tech_remove(struct connman_technology *technology)
1090 {
1091 }
1092
1093 static void server_register_reply(DBusPendingCall *call, void *user_data)
1094 {
1095         struct connman_technology *technology = user_data;
1096         DBusError error;
1097         DBusMessage *reply;
1098
1099         DBG("");
1100
1101         reply = dbus_pending_call_steal_reply(call);
1102
1103         dbus_error_init(&error);
1104
1105         if (dbus_set_error_from_message(&error, reply) == TRUE) {
1106                 connman_error("%s", error.message);
1107                 dbus_error_free(&error);
1108                 dbus_message_unref(reply);
1109                 dbus_pending_call_unref(call);
1110                 return;
1111         }
1112
1113         dbus_message_unref(reply);
1114         dbus_pending_call_unref(call);
1115
1116         connman_technology_tethering_notify(technology, TRUE);
1117 }
1118
1119 static void server_unregister_reply(DBusPendingCall *call, void *user_data)
1120 {
1121         struct connman_technology *technology = user_data;
1122         DBusError error;
1123         DBusMessage *reply;
1124
1125         DBG("");
1126
1127         reply = dbus_pending_call_steal_reply(call);
1128
1129         dbus_error_init(&error);
1130
1131         if (dbus_set_error_from_message(&error, reply) == TRUE) {
1132                 connman_error("%s", error.message);
1133                 dbus_error_free(&error);
1134                 dbus_message_unref(reply);
1135                 dbus_pending_call_unref(call);
1136                 return;
1137         }
1138
1139         dbus_message_unref(reply);
1140         dbus_pending_call_unref(call);
1141
1142         connman_technology_tethering_notify(technology, FALSE);
1143 }
1144
1145
1146 static void server_register(const char *path, const char *uuid,
1147                                 struct connman_technology *technology,
1148                                 const char *bridge, connman_bool_t enabled)
1149 {
1150         DBusMessage *message;
1151         DBusPendingCall *call;
1152         char *command;
1153
1154         DBG("path %s enabled %d", path, enabled);
1155
1156         command = enabled ? REGISTER : UNREGISTER;
1157
1158         message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
1159                                         BLUEZ_NETWORK_SERVER, command);
1160         if (message == NULL)
1161                 return;
1162
1163         dbus_message_set_auto_start(message, FALSE);
1164
1165         dbus_message_append_args(message, DBUS_TYPE_STRING, &uuid,
1166                                                         DBUS_TYPE_INVALID);
1167
1168         if (enabled == TRUE)
1169                 dbus_message_append_args(message, DBUS_TYPE_STRING, &bridge,
1170                                                         DBUS_TYPE_INVALID);
1171
1172         if (dbus_connection_send_with_reply(connection, message,
1173                                                 &call, TIMEOUT) == FALSE) {
1174                 connman_error("Failed to enable PAN server");
1175                 dbus_message_unref(message);
1176                 return;
1177         }
1178
1179         if (call == NULL) {
1180                 connman_error("D-Bus connection not available");
1181                 dbus_message_unref(message);
1182                 return;
1183         }
1184
1185         if (enabled == TRUE)
1186                 dbus_pending_call_set_notify(call, server_register_reply,
1187                                                 technology, NULL);
1188         else
1189                 dbus_pending_call_set_notify(call, server_unregister_reply,
1190                                                 technology, NULL);
1191
1192         dbus_message_unref(message);
1193 }
1194
1195 struct tethering_info {
1196         struct connman_technology *technology;
1197         const char *bridge;
1198 };
1199
1200 static void enable_nap(gpointer key, gpointer value, gpointer user_data)
1201 {
1202         struct tethering_info *info = user_data;
1203         struct connman_device *device = value;
1204         const char *path;
1205
1206         DBG("");
1207
1208         path = connman_device_get_string(device, "Path");
1209
1210         server_register(path, "nap", info->technology, info->bridge, TRUE);
1211 }
1212
1213 static void disable_nap(gpointer key, gpointer value, gpointer user_data)
1214 {
1215         struct tethering_info *info = user_data;
1216         struct connman_device *device = value;
1217         const char *path;
1218
1219         DBG("");
1220
1221         path = connman_device_get_string(device, "Path");
1222
1223         server_register(path, "nap", info->technology, info->bridge, FALSE);
1224 }
1225
1226 static int tech_set_tethering(struct connman_technology *technology,
1227                                 const char *identifier, const char *passphrase,
1228                                 const char *bridge, connman_bool_t enabled)
1229 {
1230         struct tethering_info info = {
1231                 .technology     = technology,
1232                 .bridge         = bridge,
1233         };
1234
1235         DBG("bridge %s", bridge);
1236
1237         if (enabled)
1238                 g_hash_table_foreach(bluetooth_devices, enable_nap, &info);
1239         else
1240                 g_hash_table_foreach(bluetooth_devices, disable_nap, &info);
1241
1242         return 0;
1243 }
1244
1245 static struct connman_technology_driver tech_driver = {
1246         .name           = "bluetooth_legacy",
1247         .type           = CONNMAN_SERVICE_TYPE_BLUETOOTH,
1248         .priority       = -10,
1249         .probe          = tech_probe,
1250         .remove         = tech_remove,
1251         .set_tethering  = tech_set_tethering,
1252 };
1253
1254 static guint watch;
1255 static guint added_watch;
1256 static guint removed_watch;
1257 static guint adapter_watch;
1258 static guint device_watch;
1259 static guint device_removed_watch;
1260 static guint network_watch;
1261
1262 static int bluetooth_init(void)
1263 {
1264         int err;
1265
1266         connection = connman_dbus_get_connection();
1267         if (connection == NULL)
1268                 return -EIO;
1269
1270         watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
1271                         bluetooth_connect, bluetooth_disconnect, NULL, NULL);
1272
1273         added_watch = g_dbus_add_signal_watch(connection, BLUEZ_SERVICE, NULL,
1274                                                 BLUEZ_MANAGER_INTERFACE,
1275                                                 ADAPTER_ADDED, adapter_added,
1276                                                 NULL, NULL);
1277
1278         removed_watch = g_dbus_add_signal_watch(connection, BLUEZ_SERVICE, NULL,
1279                                                 BLUEZ_MANAGER_INTERFACE,
1280                                                 ADAPTER_REMOVED, adapter_removed,
1281                                                 NULL, NULL);
1282
1283         adapter_watch = g_dbus_add_signal_watch(connection, BLUEZ_SERVICE,
1284                                                 NULL, BLUEZ_ADAPTER_INTERFACE,
1285                                                 PROPERTY_CHANGED, adapter_changed,
1286                                                 NULL, NULL);
1287
1288         device_removed_watch = g_dbus_add_signal_watch(connection,
1289                                                 BLUEZ_SERVICE, NULL,
1290                                                 BLUEZ_ADAPTER_INTERFACE,
1291                                                 DEVICE_REMOVED, device_removed,
1292                                                 NULL, NULL);
1293
1294         device_watch = g_dbus_add_signal_watch(connection, BLUEZ_SERVICE, NULL,
1295                                                 BLUEZ_DEVICE_INTERFACE,
1296                                                 PROPERTY_CHANGED, device_changed,
1297                                                 NULL, NULL);
1298
1299         network_watch = g_dbus_add_signal_watch(connection, BLUEZ_SERVICE,
1300                                                 NULL, BLUEZ_NETWORK_INTERFACE,
1301                                                 PROPERTY_CHANGED, network_changed,
1302                                                 NULL, NULL);
1303
1304         if (watch == 0 || added_watch == 0 || removed_watch == 0
1305                         || adapter_watch == 0 || network_watch == 0
1306                                 || device_watch == 0
1307                                         || device_removed_watch == 0) {
1308                 err = -EIO;
1309                 goto remove;
1310         }
1311
1312         err = connman_network_driver_register(&pan_driver);
1313         if (err < 0)
1314                 goto remove;
1315
1316         err = connman_device_driver_register(&bluetooth_driver);
1317         if (err < 0) {
1318                 connman_network_driver_unregister(&pan_driver);
1319                 goto remove;
1320         }
1321
1322         err = connman_technology_driver_register(&tech_driver);
1323         if (err < 0) {
1324                 connman_device_driver_unregister(&bluetooth_driver);
1325                 connman_network_driver_unregister(&pan_driver);
1326                 goto remove;
1327         }
1328
1329         return 0;
1330
1331 remove:
1332         g_dbus_remove_watch(connection, watch);
1333         g_dbus_remove_watch(connection, added_watch);
1334         g_dbus_remove_watch(connection, removed_watch);
1335         g_dbus_remove_watch(connection, adapter_watch);
1336         g_dbus_remove_watch(connection, device_removed_watch);
1337         g_dbus_remove_watch(connection, device_watch);
1338         g_dbus_remove_watch(connection, network_watch);
1339
1340         dbus_connection_unref(connection);
1341
1342         return err;
1343 }
1344
1345 static void bluetooth_exit(void)
1346 {
1347         g_dbus_remove_watch(connection, watch);
1348         g_dbus_remove_watch(connection, added_watch);
1349         g_dbus_remove_watch(connection, removed_watch);
1350         g_dbus_remove_watch(connection, adapter_watch);
1351         g_dbus_remove_watch(connection, device_removed_watch);
1352         g_dbus_remove_watch(connection, device_watch);
1353         g_dbus_remove_watch(connection, network_watch);
1354
1355         bluetooth_disconnect(connection, NULL);
1356
1357         connman_technology_driver_unregister(&tech_driver);
1358
1359         connman_device_driver_unregister(&bluetooth_driver);
1360         connman_network_driver_unregister(&pan_driver);
1361
1362         dbus_connection_unref(connection);
1363 }
1364
1365 CONNMAN_PLUGIN_DEFINE(bluetooth_legacy, "Bluetooth technology plugin (legacy)",
1366                 VERSION, CONNMAN_PLUGIN_PRIORITY_LOW,
1367                 bluetooth_init, bluetooth_exit)