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