5 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
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.
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.
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
30 #include <netinet/ether.h>
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>
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"
49 #define LIST_ADAPTERS "ListAdapters"
50 #define ADAPTER_ADDED "AdapterAdded"
51 #define ADAPTER_REMOVED "AdapterRemoved"
52 #define DEVICE_REMOVED "DeviceRemoved"
54 #define PEER_CONNECTED "PeerConnected"
55 #define PEER_DISCONNECTED "PeerDisconnected"
57 #define PROPERTY_CHANGED "PropertyChanged"
58 #define GET_PROPERTIES "GetProperties"
59 #define SET_PROPERTY "SetProperty"
61 #define CONNECT "Connect"
62 #define DISCONNECT "Disconnect"
64 #define REGISTER "Register"
65 #define UNREGISTER "Unregister"
67 #define UUID_NAP "00001116-0000-1000-8000-00805f9b34fb"
71 static DBusConnection *connection;
73 static GHashTable *bluetooth_devices = NULL;
74 static GHashTable *bluetooth_networks = NULL;
75 static GHashTable *pending_networks = NULL;
77 static int pan_probe(struct connman_network *network)
82 g_hash_table_iter_init(&iter, bluetooth_networks);
83 while (g_hash_table_iter_next(&iter, &key, &val)) {
84 struct connman_network *known = val;
89 DBG("network %p", network);
97 static void pan_remove(struct connman_network *network)
99 DBG("network %p", network);
102 static void connect_reply(DBusPendingCall *call, void *user_data)
104 char *path = user_data;
105 struct connman_network *network;
108 const char *interface = NULL;
111 network = g_hash_table_lookup(bluetooth_networks, path);
115 DBG("network %p", network);
117 reply = dbus_pending_call_steal_reply(call);
119 dbus_error_init(&error);
121 if (dbus_set_error_from_message(&error, reply)) {
122 connman_error("%s", error.message);
123 dbus_error_free(&error);
128 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_STRING,
129 &interface, DBUS_TYPE_INVALID)) {
130 if (dbus_error_is_set(&error)) {
131 connman_error("%s", error.message);
132 dbus_error_free(&error);
134 connman_error("Wrong arguments for connect");
141 DBG("interface %s", interface);
143 index = connman_inet_ifindex(interface);
145 connman_network_set_index(network, index);
147 connman_network_set_connected(network, true);
149 dbus_message_unref(reply);
151 dbus_pending_call_unref(call);
156 connman_network_set_connected(network, false);
158 dbus_message_unref(reply);
160 dbus_pending_call_unref(call);
163 static int pan_connect(struct connman_network *network)
165 const char *path = connman_network_get_string(network, "Path");
166 const char *uuid = "nap";
167 DBusMessage *message;
168 DBusPendingCall *call;
170 DBG("network %p", network);
175 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
176 BLUEZ_NETWORK_INTERFACE, CONNECT);
180 dbus_message_set_auto_start(message, FALSE);
182 dbus_message_append_args(message, DBUS_TYPE_STRING, &uuid,
185 if (!dbus_connection_send_with_reply(connection, message,
186 &call, TIMEOUT * 10)) {
187 connman_error("Failed to connect service");
188 dbus_message_unref(message);
193 connman_error("D-Bus connection not available");
194 dbus_message_unref(message);
198 dbus_pending_call_set_notify(call, connect_reply, g_strdup(path),
201 dbus_message_unref(message);
206 static void disconnect_reply(DBusPendingCall *call, void *user_data)
208 char *path = user_data;
209 struct connman_network *network;
213 network = g_hash_table_lookup(bluetooth_networks, path);
217 DBG("network %p", network);
219 reply = dbus_pending_call_steal_reply(call);
221 dbus_error_init(&error);
223 if (dbus_set_error_from_message(&error, reply)) {
224 connman_error("%s", error.message);
225 dbus_error_free(&error);
229 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID)) {
230 if (dbus_error_is_set(&error)) {
231 connman_error("%s", error.message);
232 dbus_error_free(&error);
234 connman_error("Wrong arguments for disconnect");
238 connman_network_set_connected(network, false);
241 dbus_message_unref(reply);
243 dbus_pending_call_unref(call);
245 connman_network_unref(network);
248 static int pan_disconnect(struct connman_network *network)
250 const char *path = connman_network_get_string(network, "Path");
251 DBusMessage *message;
252 DBusPendingCall *call;
254 DBG("network %p", network);
259 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
260 BLUEZ_NETWORK_INTERFACE, DISCONNECT);
264 dbus_message_set_auto_start(message, FALSE);
266 dbus_message_append_args(message, DBUS_TYPE_INVALID);
268 if (!dbus_connection_send_with_reply(connection, message,
270 connman_error("Failed to disconnect service");
271 dbus_message_unref(message);
276 connman_error("D-Bus connection not available");
277 dbus_message_unref(message);
281 connman_network_ref(network);
283 connman_network_set_associating(network, false);
285 dbus_pending_call_set_notify(call, disconnect_reply, g_strdup(path),
288 dbus_message_unref(message);
293 static struct connman_network_driver pan_driver = {
294 .name = "bluetooth_legacy-pan",
295 .type = CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN,
296 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
298 .remove = pan_remove,
299 .connect = pan_connect,
300 .disconnect = pan_disconnect,
303 static gboolean network_changed(DBusConnection *conn,
304 DBusMessage *message, void *user_data)
306 const char *path = dbus_message_get_path(message);
307 struct connman_network *network;
308 DBusMessageIter iter, value;
311 DBG("path %s", path);
313 network = g_hash_table_lookup(bluetooth_networks, path);
317 if (!dbus_message_iter_init(message, &iter))
320 dbus_message_iter_get_basic(&iter, &key);
322 dbus_message_iter_next(&iter);
323 dbus_message_iter_recurse(&iter, &value);
325 if (g_str_equal(key, "Connected")) {
326 dbus_bool_t connected;
328 dbus_message_iter_get_basic(&value, &connected);
333 connman_network_set_associating(network, false);
334 connman_network_set_connected(network, false);
340 static void parse_peer_device(DBusMessage *message, char **dev,
343 const char *path = dbus_message_get_path(message);
344 DBusMessageIter iter;
346 DBG("path %s", path);
348 if (dbus_message_iter_init(message, &iter) == FALSE)
351 dbus_message_iter_get_basic(&iter, dev);
352 dbus_message_iter_next(&iter);
353 dbus_message_iter_get_basic(&iter, address);
356 static gboolean peer_connected(DBusConnection *connection,
357 DBusMessage *message, void *user_data)
361 parse_peer_device(message, &dev, &address);
363 DBG("connection device is %s", dev);
364 DBG("connection address is %s", address);
366 connman_technology_tethering_add_station(
367 CONNMAN_SERVICE_TYPE_BLUETOOTH, address);
372 static gboolean peer_disconnected(DBusConnection *connection,
373 DBusMessage *message, void *user_data)
377 parse_peer_device(message, &dev, &address);
379 DBG("disconnection device is %s", dev);
380 DBG("disconnection address is %s", address);
382 connman_technology_tethering_remove_station(address);
387 static void extract_properties(DBusMessage *reply, const char **parent,
388 const char **address,
391 dbus_bool_t *powered,
392 dbus_bool_t *scanning,
393 DBusMessageIter *uuids,
394 DBusMessageIter *networks)
396 DBusMessageIter array, dict;
398 if (!dbus_message_iter_init(reply, &array))
401 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
404 dbus_message_iter_recurse(&array, &dict);
406 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
407 DBusMessageIter entry, value;
410 dbus_message_iter_recurse(&dict, &entry);
411 dbus_message_iter_get_basic(&entry, &key);
413 dbus_message_iter_next(&entry);
414 dbus_message_iter_recurse(&entry, &value);
416 if (g_str_equal(key, "Adapter")) {
418 dbus_message_iter_get_basic(&value, parent);
419 } else if (g_str_equal(key, "Address")) {
421 dbus_message_iter_get_basic(&value, address);
422 } else if (g_str_equal(key, "Name")) {
424 dbus_message_iter_get_basic(&value, name);
425 } else if (g_str_equal(key, "Alias")) {
427 dbus_message_iter_get_basic(&value, alias);
428 } else if (g_str_equal(key, "Powered")) {
430 dbus_message_iter_get_basic(&value, powered);
431 } else if (g_str_equal(key, "Discovering")) {
433 dbus_message_iter_get_basic(&value, scanning);
434 } else if (g_str_equal(key, "Devices")) {
436 memcpy(networks, &value, sizeof(value));
437 } else if (g_str_equal(key, "UUIDs")) {
439 memcpy(uuids, &value, sizeof(value));
442 dbus_message_iter_next(&dict);
446 static dbus_bool_t has_pan(DBusMessageIter *array)
448 DBusMessageIter value;
450 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
453 dbus_message_iter_recurse(array, &value);
455 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
458 dbus_message_iter_get_basic(&value, &uuid);
460 if (g_strcmp0(uuid, UUID_NAP) == 0)
463 dbus_message_iter_next(&value);
469 static void network_properties_reply(DBusPendingCall *call, void *user_data)
471 char *path = user_data;
472 struct connman_device *device;
473 struct connman_network *network;
475 DBusMessageIter uuids;
476 const char *parent = NULL, *address = NULL, *name = NULL;
477 struct ether_addr addr;
480 reply = dbus_pending_call_steal_reply(call);
482 extract_properties(reply, &parent, &address, NULL, &name,
483 NULL, NULL, &uuids, NULL);
488 device = g_hash_table_lookup(bluetooth_devices, parent);
495 ether_aton_r(address, &addr);
497 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
498 addr.ether_addr_octet[0],
499 addr.ether_addr_octet[1],
500 addr.ether_addr_octet[2],
501 addr.ether_addr_octet[3],
502 addr.ether_addr_octet[4],
503 addr.ether_addr_octet[5]);
505 if (!has_pan(&uuids))
508 network = connman_device_get_network(device, ident);
512 network = connman_network_create(ident,
513 CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
517 connman_network_set_string(network, "Path", path);
519 connman_network_set_name(network, name);
521 g_hash_table_replace(bluetooth_networks, g_strdup(path), network);
523 connman_device_add_network(device, network);
525 connman_network_set_group(network, ident);
528 dbus_message_unref(reply);
530 dbus_pending_call_unref(call);
533 static void add_network(const char *path)
535 DBusMessage *message;
536 DBusPendingCall *call;
538 DBG("path %s", path);
540 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
541 BLUEZ_DEVICE_INTERFACE, GET_PROPERTIES);
545 dbus_message_set_auto_start(message, FALSE);
547 if (!dbus_connection_send_with_reply(connection, message,
549 connman_error("Failed to get network properties for %s", path);
554 connman_error("D-Bus connection not available");
558 dbus_pending_call_set_notify(call, network_properties_reply,
559 g_strdup(path), g_free);
562 dbus_message_unref(message);
565 static void check_networks(DBusMessageIter *array)
567 DBusMessageIter value;
569 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
572 dbus_message_iter_recurse(array, &value);
574 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
577 dbus_message_iter_get_basic(&value, &path);
581 dbus_message_iter_next(&value);
585 static void check_pending_networks(const char *adapter)
587 GSList *networks, *list;
589 networks = g_hash_table_lookup(pending_networks, adapter);
593 for (list = networks; list; list = list->next) {
594 char *path = list->data;
599 g_hash_table_remove(pending_networks, adapter);
602 static gboolean adapter_changed(DBusConnection *conn,
603 DBusMessage *message, void *user_data)
605 const char *path = dbus_message_get_path(message);
606 struct connman_device *device;
607 DBusMessageIter iter, value;
610 DBG("path %s", path);
612 device = g_hash_table_lookup(bluetooth_devices, path);
616 if (!dbus_message_iter_init(message, &iter))
619 dbus_message_iter_get_basic(&iter, &key);
621 dbus_message_iter_next(&iter);
622 dbus_message_iter_recurse(&iter, &value);
624 if (g_str_equal(key, "Powered")) {
627 dbus_message_iter_get_basic(&value, &val);
628 connman_device_set_powered(device, val);
630 check_pending_networks(path);
631 } else if (g_str_equal(key, "Discovering")) {
634 dbus_message_iter_get_basic(&value, &val);
635 connman_device_set_scanning(device,
636 CONNMAN_SERVICE_TYPE_BLUETOOTH, val);
637 } else if (g_str_equal(key, "Devices")) {
638 check_networks(&value);
644 static gboolean device_removed(DBusConnection *conn,
645 DBusMessage *message, void *user_data)
647 const char *network_path;
648 struct connman_network *network;
649 struct connman_device *device;
650 DBusMessageIter iter;
654 if (!dbus_message_iter_init(message, &iter))
657 dbus_message_iter_get_basic(&iter, &network_path);
659 network = g_hash_table_lookup(bluetooth_networks, network_path);
663 device = connman_network_get_device(network);
667 g_hash_table_remove(bluetooth_networks, network_path);
672 static gboolean device_changed(DBusConnection *conn,
673 DBusMessage *message, void *user_data)
675 const char *path = dbus_message_get_path(message);
676 DBusMessageIter iter, value;
679 DBG("path %s", path);
681 if (!dbus_message_iter_init(message, &iter))
684 dbus_message_iter_get_basic(&iter, &key);
686 dbus_message_iter_next(&iter);
687 dbus_message_iter_recurse(&iter, &value);
691 if (g_str_equal(key, "UUIDs"))
697 static void remove_device_networks(struct connman_device *device)
701 GSList *key_list = NULL;
704 if (!bluetooth_networks)
707 g_hash_table_iter_init(&iter, bluetooth_networks);
709 while (g_hash_table_iter_next(&iter, &key, &value)) {
710 struct connman_network *network = value;
712 if (connman_network_get_device(network) != device)
715 key_list = g_slist_prepend(key_list, key);
718 for (list = key_list; list; list = list->next) {
719 const char *network_path = list->data;
721 g_hash_table_remove(bluetooth_networks, network_path);
724 g_slist_free(key_list);
727 static void add_pending_networks(const char *adapter, DBusMessageIter *array)
729 DBusMessageIter value;
732 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
735 dbus_message_iter_recurse(array, &value);
737 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
740 dbus_message_iter_get_basic(&value, &path);
742 list = g_slist_prepend(list, g_strdup(path));
744 dbus_message_iter_next(&value);
750 g_hash_table_replace(pending_networks, g_strdup(adapter), list);
753 static void adapter_properties_reply(DBusPendingCall *call, void *user_data)
755 char *path = user_data;
756 struct connman_device *device;
758 DBusMessageIter networks;
759 const char *address = NULL, *name = NULL;
760 dbus_bool_t powered = FALSE, scanning = FALSE;
761 struct ether_addr addr;
764 DBG("path %s", path);
766 reply = dbus_pending_call_steal_reply(call);
771 extract_properties(reply, NULL, &address, &name, NULL,
772 &powered, &scanning, NULL, &networks);
777 if (g_strcmp0(address, "00:00:00:00:00:00") == 0)
780 device = g_hash_table_lookup(bluetooth_devices, path);
784 ether_aton_r(address, &addr);
786 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
787 addr.ether_addr_octet[0],
788 addr.ether_addr_octet[1],
789 addr.ether_addr_octet[2],
790 addr.ether_addr_octet[3],
791 addr.ether_addr_octet[4],
792 addr.ether_addr_octet[5]);
794 device = connman_device_create("bluetooth_legacy",
795 CONNMAN_DEVICE_TYPE_BLUETOOTH);
799 g_hash_table_insert(bluetooth_devices, g_strdup(path), device);
801 connman_device_set_ident(device, ident);
803 connman_device_set_string(device, "Path", path);
805 if (connman_device_register(device) < 0) {
806 connman_device_unref(device);
807 g_hash_table_remove(bluetooth_devices, path);
812 connman_device_set_string(device, "Address", address);
813 connman_device_set_string(device, "Name", name);
814 connman_device_set_string(device, "Path", path);
816 connman_device_set_powered(device, powered);
817 connman_device_set_scanning(device,
818 CONNMAN_SERVICE_TYPE_BLUETOOTH, scanning);
821 remove_device_networks(device);
822 add_pending_networks(path, &networks);
824 check_networks(&networks);
827 dbus_message_unref(reply);
829 dbus_pending_call_unref(call);
832 static void add_adapter(DBusConnection *conn, const char *path)
834 DBusMessage *message;
835 DBusPendingCall *call;
837 DBG("path %s", path);
839 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
840 BLUEZ_ADAPTER_INTERFACE, GET_PROPERTIES);
844 dbus_message_set_auto_start(message, FALSE);
846 if (!dbus_connection_send_with_reply(conn, message, &call, TIMEOUT)) {
847 connman_error("Failed to get adapter properties for %s", path);
852 connman_error("D-Bus connection not available");
856 dbus_pending_call_set_notify(call, adapter_properties_reply,
857 g_strdup(path), g_free);
860 dbus_message_unref(message);
863 static gboolean adapter_added(DBusConnection *conn, DBusMessage *message,
868 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
870 add_adapter(conn, path);
874 static void remove_adapter(DBusConnection *conn, const char *path)
876 DBG("path %s", path);
878 g_hash_table_remove(bluetooth_devices, path);
879 g_hash_table_remove(pending_networks, path);
882 static gboolean adapter_removed(DBusConnection *conn, DBusMessage *message,
887 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
889 remove_adapter(conn, path);
893 static void list_adapters_reply(DBusPendingCall *call, void *user_data)
902 reply = dbus_pending_call_steal_reply(call);
904 dbus_error_init(&error);
906 if (dbus_set_error_from_message(&error, reply)) {
907 connman_error("%s", error.message);
908 dbus_error_free(&error);
912 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_ARRAY,
913 DBUS_TYPE_OBJECT_PATH, &adapters,
914 &num_adapters, DBUS_TYPE_INVALID)) {
915 if (dbus_error_is_set(&error)) {
916 connman_error("%s", error.message);
917 dbus_error_free(&error);
919 connman_error("Wrong arguments for adapter list");
923 for (i = 0; i < num_adapters; i++)
924 add_adapter(connection, adapters[i]);
926 g_strfreev(adapters);
929 dbus_message_unref(reply);
931 dbus_pending_call_unref(call);
934 static void unregister_device(gpointer data)
936 struct connman_device *device = data;
940 remove_device_networks(device);
942 connman_device_unregister(device);
943 connman_device_unref(device);
946 static void remove_network(gpointer data)
948 struct connman_network *network = data;
949 struct connman_device *device;
951 DBG("network %p", network);
953 device = connman_network_get_device(network);
955 connman_device_remove_network(device, network);
957 connman_network_unref(network);
960 static void remove_pending_networks(gpointer data)
964 g_slist_free_full(list, g_free);
967 static void bluetooth_connect(DBusConnection *conn, void *user_data)
969 DBusMessage *message;
970 DBusPendingCall *call;
972 DBG("connection %p", conn);
974 bluetooth_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
975 g_free, unregister_device);
977 bluetooth_networks = g_hash_table_new_full(g_str_hash, g_str_equal,
978 g_free, remove_network);
980 pending_networks = g_hash_table_new_full(g_str_hash, g_str_equal,
981 g_free, remove_pending_networks);
983 message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
984 BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS);
988 dbus_message_set_auto_start(message, FALSE);
990 if (!dbus_connection_send_with_reply(conn, message, &call, TIMEOUT)) {
991 connman_error("Failed to get Bluetooth adapters");
996 connman_error("D-Bus connection not available");
1000 dbus_pending_call_set_notify(call, list_adapters_reply, NULL, NULL);
1003 dbus_message_unref(message);
1006 static void bluetooth_disconnect(DBusConnection *conn, void *user_data)
1008 DBG("connection %p", conn);
1010 if (!bluetooth_devices)
1013 g_hash_table_destroy(bluetooth_networks);
1014 bluetooth_networks = NULL;
1015 g_hash_table_destroy(bluetooth_devices);
1016 bluetooth_devices = NULL;
1017 g_hash_table_destroy(pending_networks);
1018 pending_networks = NULL;
1021 static int bluetooth_probe(struct connman_device *device)
1023 GHashTableIter iter;
1024 gpointer key, value;
1026 DBG("device %p", device);
1028 if (!bluetooth_devices)
1031 g_hash_table_iter_init(&iter, bluetooth_devices);
1033 while (g_hash_table_iter_next(&iter, &key, &value)) {
1034 struct connman_device *device_pan = value;
1036 if (device == device_pan)
1043 static void bluetooth_remove(struct connman_device *device)
1045 DBG("device %p", device);
1048 static void powered_reply(DBusPendingCall *call, void *user_data)
1055 reply = dbus_pending_call_steal_reply(call);
1057 dbus_error_init(&error);
1059 if (dbus_set_error_from_message(&error, reply)) {
1060 connman_error("%s", error.message);
1061 dbus_error_free(&error);
1062 dbus_message_unref(reply);
1063 dbus_pending_call_unref(call);
1067 dbus_message_unref(reply);
1068 dbus_pending_call_unref(call);
1070 add_adapter(connection, user_data);
1073 static int change_powered(DBusConnection *conn, const char *path,
1074 dbus_bool_t powered)
1076 DBusMessage *message;
1077 DBusMessageIter iter;
1078 DBusPendingCall *call;
1085 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
1086 BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY);
1090 dbus_message_set_auto_start(message, FALSE);
1092 dbus_message_iter_init_append(message, &iter);
1093 connman_dbus_property_append_basic(&iter, "Powered",
1094 DBUS_TYPE_BOOLEAN, &powered);
1096 if (!dbus_connection_send_with_reply(conn, message, &call, TIMEOUT)) {
1097 connman_error("Failed to change Powered property");
1098 dbus_message_unref(message);
1103 connman_error("D-Bus connection not available");
1104 dbus_message_unref(message);
1108 dbus_pending_call_set_notify(call, powered_reply,
1109 g_strdup(path), g_free);
1111 dbus_message_unref(message);
1113 return -EINPROGRESS;
1116 static int bluetooth_enable(struct connman_device *device)
1118 const char *path = connman_device_get_string(device, "Path");
1120 DBG("device %p", device);
1122 return change_powered(connection, path, TRUE);
1125 static int bluetooth_disable(struct connman_device *device)
1127 const char *path = connman_device_get_string(device, "Path");
1129 DBG("device %p", device);
1131 return change_powered(connection, path, FALSE);
1134 static struct connman_device_driver bluetooth_driver = {
1135 .name = "bluetooth_legacy",
1136 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
1137 .probe = bluetooth_probe,
1138 .remove = bluetooth_remove,
1139 .enable = bluetooth_enable,
1140 .disable = bluetooth_disable,
1143 static int tech_probe(struct connman_technology *technology)
1148 static void tech_remove(struct connman_technology *technology)
1152 static void server_register_reply(DBusPendingCall *call, void *user_data)
1154 struct connman_technology *technology = user_data;
1160 reply = dbus_pending_call_steal_reply(call);
1162 dbus_error_init(&error);
1164 if (dbus_set_error_from_message(&error, reply)) {
1165 connman_error("%s", error.message);
1166 dbus_error_free(&error);
1167 dbus_message_unref(reply);
1168 dbus_pending_call_unref(call);
1172 dbus_message_unref(reply);
1173 dbus_pending_call_unref(call);
1175 connman_technology_tethering_notify(technology, true);
1178 static void server_unregister_reply(DBusPendingCall *call, void *user_data)
1180 struct connman_technology *technology = user_data;
1186 reply = dbus_pending_call_steal_reply(call);
1188 dbus_error_init(&error);
1190 if (dbus_set_error_from_message(&error, reply)) {
1191 connman_error("%s", error.message);
1192 dbus_error_free(&error);
1193 dbus_message_unref(reply);
1194 dbus_pending_call_unref(call);
1198 dbus_message_unref(reply);
1199 dbus_pending_call_unref(call);
1201 connman_technology_tethering_notify(technology, false);
1205 static void server_register(const char *path, const char *uuid,
1206 struct connman_technology *technology,
1207 const char *bridge, bool enabled)
1209 DBusMessage *message;
1210 DBusPendingCall *call;
1213 DBG("path %s enabled %d", path, enabled);
1215 command = enabled ? REGISTER : UNREGISTER;
1217 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
1218 BLUEZ_NETWORK_SERVER, command);
1222 dbus_message_set_auto_start(message, FALSE);
1224 dbus_message_append_args(message, DBUS_TYPE_STRING, &uuid,
1228 dbus_message_append_args(message, DBUS_TYPE_STRING, &bridge,
1231 if (!dbus_connection_send_with_reply(connection, message,
1233 connman_error("Failed to enable PAN server");
1234 dbus_message_unref(message);
1239 connman_error("D-Bus connection not available");
1240 dbus_message_unref(message);
1245 dbus_pending_call_set_notify(call, server_register_reply,
1248 dbus_pending_call_set_notify(call, server_unregister_reply,
1251 dbus_message_unref(message);
1254 struct tethering_info {
1255 struct connman_technology *technology;
1259 static void enable_nap(gpointer key, gpointer value, gpointer user_data)
1261 struct tethering_info *info = user_data;
1262 struct connman_device *device = value;
1267 path = connman_device_get_string(device, "Path");
1269 server_register(path, "nap", info->technology, info->bridge, true);
1272 static void disable_nap(gpointer key, gpointer value, gpointer user_data)
1274 struct tethering_info *info = user_data;
1275 struct connman_device *device = value;
1280 path = connman_device_get_string(device, "Path");
1282 server_register(path, "nap", info->technology, info->bridge, false);
1285 static int tech_set_tethering(struct connman_technology *technology,
1286 const char *identifier, const char *passphrase,
1287 const char *bridge, bool enabled, bool hidden)
1289 struct tethering_info info = {
1290 .technology = technology,
1294 DBG("bridge %s", bridge);
1296 if (!bluetooth_devices)
1300 g_hash_table_foreach(bluetooth_devices, enable_nap, &info);
1302 g_hash_table_foreach(bluetooth_devices, disable_nap, &info);
1307 static struct connman_technology_driver tech_driver = {
1308 .name = "bluetooth_legacy",
1309 .type = CONNMAN_SERVICE_TYPE_BLUETOOTH,
1311 .probe = tech_probe,
1312 .remove = tech_remove,
1313 .set_tethering = tech_set_tethering,
1317 static guint added_watch;
1318 static guint removed_watch;
1319 static guint adapter_watch;
1320 static guint device_watch;
1321 static guint device_removed_watch;
1322 static guint network_watch;
1323 static guint peerconnected_watch;
1324 static guint peerdisconnected_watch;
1326 static int bluetooth_init(void)
1330 connection = connman_dbus_get_connection();
1334 watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
1335 bluetooth_connect, bluetooth_disconnect, NULL, NULL);
1337 added_watch = g_dbus_add_signal_watch(connection, BLUEZ_SERVICE, NULL,
1338 BLUEZ_MANAGER_INTERFACE,
1339 ADAPTER_ADDED, adapter_added,
1342 removed_watch = g_dbus_add_signal_watch(connection, BLUEZ_SERVICE, NULL,
1343 BLUEZ_MANAGER_INTERFACE,
1344 ADAPTER_REMOVED, adapter_removed,
1347 adapter_watch = g_dbus_add_signal_watch(connection, BLUEZ_SERVICE,
1348 NULL, BLUEZ_ADAPTER_INTERFACE,
1349 PROPERTY_CHANGED, adapter_changed,
1352 device_removed_watch = g_dbus_add_signal_watch(connection,
1353 BLUEZ_SERVICE, NULL,
1354 BLUEZ_ADAPTER_INTERFACE,
1355 DEVICE_REMOVED, device_removed,
1358 device_watch = g_dbus_add_signal_watch(connection, BLUEZ_SERVICE, NULL,
1359 BLUEZ_DEVICE_INTERFACE,
1360 PROPERTY_CHANGED, device_changed,
1363 network_watch = g_dbus_add_signal_watch(connection, BLUEZ_SERVICE,
1364 NULL, BLUEZ_NETWORK_INTERFACE,
1365 PROPERTY_CHANGED, network_changed,
1368 peerconnected_watch = g_dbus_add_signal_watch(connection,
1370 NULL, BLUEZ_NETWORK_SERVER,
1371 PEER_CONNECTED, peer_connected,
1374 peerdisconnected_watch = g_dbus_add_signal_watch(connection,
1376 NULL, BLUEZ_NETWORK_SERVER,
1381 if (watch == 0 || added_watch == 0 || removed_watch == 0
1382 || adapter_watch == 0 || network_watch == 0 || device_watch == 0
1383 || peerconnected_watch == 0 || peerdisconnected_watch == 0
1384 || device_removed_watch == 0) {
1389 err = connman_network_driver_register(&pan_driver);
1393 err = connman_device_driver_register(&bluetooth_driver);
1395 connman_network_driver_unregister(&pan_driver);
1399 err = connman_technology_driver_register(&tech_driver);
1401 connman_device_driver_unregister(&bluetooth_driver);
1402 connman_network_driver_unregister(&pan_driver);
1409 g_dbus_remove_watch(connection, watch);
1410 g_dbus_remove_watch(connection, added_watch);
1411 g_dbus_remove_watch(connection, removed_watch);
1412 g_dbus_remove_watch(connection, adapter_watch);
1413 g_dbus_remove_watch(connection, device_removed_watch);
1414 g_dbus_remove_watch(connection, device_watch);
1415 g_dbus_remove_watch(connection, network_watch);
1416 g_dbus_remove_watch(connection, peerconnected_watch);
1417 g_dbus_remove_watch(connection, peerdisconnected_watch);
1419 dbus_connection_unref(connection);
1424 static void bluetooth_exit(void)
1426 g_dbus_remove_watch(connection, watch);
1427 g_dbus_remove_watch(connection, added_watch);
1428 g_dbus_remove_watch(connection, removed_watch);
1429 g_dbus_remove_watch(connection, adapter_watch);
1430 g_dbus_remove_watch(connection, device_removed_watch);
1431 g_dbus_remove_watch(connection, device_watch);
1432 g_dbus_remove_watch(connection, network_watch);
1433 g_dbus_remove_watch(connection, peerconnected_watch);
1434 g_dbus_remove_watch(connection, peerdisconnected_watch);
1437 * We unset the disabling of the Bluetooth device when shutting down
1438 * so that non-PAN BT connections are not affected.
1440 bluetooth_driver.disable = NULL;
1442 bluetooth_disconnect(connection, NULL);
1444 connman_technology_driver_unregister(&tech_driver);
1446 connman_device_driver_unregister(&bluetooth_driver);
1447 connman_network_driver_unregister(&pan_driver);
1449 dbus_connection_unref(connection);
1452 CONNMAN_PLUGIN_DEFINE(bluetooth_legacy, "Bluetooth technology plugin (legacy)",
1453 VERSION, CONNMAN_PLUGIN_PRIORITY_LOW,
1454 bluetooth_init, bluetooth_exit)