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 #if defined TIZEN_EXT
260 if (connman_network_get_associating(network) == TRUE) {
261 connman_network_set_error(network,
262 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
266 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
267 BLUEZ_NETWORK_INTERFACE, DISCONNECT);
271 dbus_message_set_auto_start(message, FALSE);
273 dbus_message_append_args(message, DBUS_TYPE_INVALID);
275 if (!dbus_connection_send_with_reply(connection, message,
277 connman_error("Failed to disconnect service");
278 dbus_message_unref(message);
283 connman_error("D-Bus connection not available");
284 dbus_message_unref(message);
288 connman_network_ref(network);
290 connman_network_set_associating(network, false);
292 dbus_pending_call_set_notify(call, disconnect_reply, g_strdup(path),
295 dbus_message_unref(message);
300 static struct connman_network_driver pan_driver = {
301 .name = "bluetooth_legacy-pan",
302 .type = CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN,
303 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
305 .remove = pan_remove,
306 .connect = pan_connect,
307 .disconnect = pan_disconnect,
310 static gboolean network_changed(DBusConnection *conn,
311 DBusMessage *message, void *user_data)
313 const char *path = dbus_message_get_path(message);
314 struct connman_network *network;
315 DBusMessageIter iter, value;
318 DBG("path %s", path);
320 network = g_hash_table_lookup(bluetooth_networks, path);
324 if (!dbus_message_iter_init(message, &iter))
327 dbus_message_iter_get_basic(&iter, &key);
329 dbus_message_iter_next(&iter);
330 dbus_message_iter_recurse(&iter, &value);
332 if (g_str_equal(key, "Connected")) {
333 dbus_bool_t connected;
335 dbus_message_iter_get_basic(&value, &connected);
340 connman_network_set_associating(network, false);
341 connman_network_set_connected(network, false);
347 static void parse_peer_device(DBusMessage *message, char **dev,
350 const char *path = dbus_message_get_path(message);
351 DBusMessageIter iter;
353 DBG("path %s", path);
355 if (dbus_message_iter_init(message, &iter) == FALSE)
358 dbus_message_iter_get_basic(&iter, dev);
359 dbus_message_iter_next(&iter);
360 dbus_message_iter_get_basic(&iter, address);
363 static gboolean peer_connected(DBusConnection *connection,
364 DBusMessage *message, void *user_data)
368 parse_peer_device(message, &dev, &address);
370 DBG("connection device is %s", dev);
371 DBG("connection address is %s", address);
373 connman_technology_tethering_add_station(
374 CONNMAN_SERVICE_TYPE_BLUETOOTH, address);
379 static gboolean peer_disconnected(DBusConnection *connection,
380 DBusMessage *message, void *user_data)
384 parse_peer_device(message, &dev, &address);
386 DBG("disconnection device is %s", dev);
387 DBG("disconnection address is %s", address);
389 connman_technology_tethering_remove_station(address);
394 static void extract_properties(DBusMessage *reply, const char **parent,
395 const char **address,
398 dbus_bool_t *powered,
399 dbus_bool_t *scanning,
400 DBusMessageIter *uuids,
401 DBusMessageIter *networks)
403 DBusMessageIter array, dict;
405 if (!dbus_message_iter_init(reply, &array))
408 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
411 dbus_message_iter_recurse(&array, &dict);
413 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
414 DBusMessageIter entry, value;
417 dbus_message_iter_recurse(&dict, &entry);
418 dbus_message_iter_get_basic(&entry, &key);
420 dbus_message_iter_next(&entry);
421 dbus_message_iter_recurse(&entry, &value);
423 if (g_str_equal(key, "Adapter")) {
425 dbus_message_iter_get_basic(&value, parent);
426 } else if (g_str_equal(key, "Address")) {
428 dbus_message_iter_get_basic(&value, address);
429 } else if (g_str_equal(key, "Name")) {
431 dbus_message_iter_get_basic(&value, name);
432 } else if (g_str_equal(key, "Alias")) {
434 dbus_message_iter_get_basic(&value, alias);
435 } else if (g_str_equal(key, "Powered")) {
437 dbus_message_iter_get_basic(&value, powered);
438 } else if (g_str_equal(key, "Discovering")) {
440 dbus_message_iter_get_basic(&value, scanning);
441 } else if (g_str_equal(key, "Devices")) {
443 memcpy(networks, &value, sizeof(value));
444 } else if (g_str_equal(key, "UUIDs")) {
446 memcpy(uuids, &value, sizeof(value));
449 dbus_message_iter_next(&dict);
453 static dbus_bool_t has_pan(DBusMessageIter *array)
455 DBusMessageIter value;
457 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
460 dbus_message_iter_recurse(array, &value);
462 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
465 dbus_message_iter_get_basic(&value, &uuid);
467 if (g_strcmp0(uuid, UUID_NAP) == 0)
470 dbus_message_iter_next(&value);
476 static void network_properties_reply(DBusPendingCall *call, void *user_data)
478 char *path = user_data;
479 struct connman_device *device;
480 struct connman_network *network;
482 DBusMessageIter uuids;
483 const char *parent = NULL, *address = NULL, *name = NULL;
484 struct ether_addr addr;
487 reply = dbus_pending_call_steal_reply(call);
489 extract_properties(reply, &parent, &address, NULL, &name,
490 NULL, NULL, &uuids, NULL);
495 device = g_hash_table_lookup(bluetooth_devices, parent);
502 ether_aton_r(address, &addr);
504 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
505 addr.ether_addr_octet[0],
506 addr.ether_addr_octet[1],
507 addr.ether_addr_octet[2],
508 addr.ether_addr_octet[3],
509 addr.ether_addr_octet[4],
510 addr.ether_addr_octet[5]);
512 if (!has_pan(&uuids))
515 network = connman_device_get_network(device, ident);
519 network = connman_network_create(ident,
520 CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
524 connman_network_set_string(network, "Path", path);
526 connman_network_set_name(network, name);
528 g_hash_table_replace(bluetooth_networks, g_strdup(path), network);
530 connman_device_add_network(device, network);
532 connman_network_set_group(network, ident);
535 dbus_message_unref(reply);
537 dbus_pending_call_unref(call);
540 static void add_network(const char *path)
542 DBusMessage *message;
543 DBusPendingCall *call;
545 DBG("path %s", path);
547 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
548 BLUEZ_DEVICE_INTERFACE, GET_PROPERTIES);
552 dbus_message_set_auto_start(message, FALSE);
554 if (!dbus_connection_send_with_reply(connection, message,
556 connman_error("Failed to get network properties for %s", path);
561 connman_error("D-Bus connection not available");
565 dbus_pending_call_set_notify(call, network_properties_reply,
566 g_strdup(path), g_free);
569 dbus_message_unref(message);
572 static void check_networks(DBusMessageIter *array)
574 DBusMessageIter value;
576 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
579 dbus_message_iter_recurse(array, &value);
581 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
584 dbus_message_iter_get_basic(&value, &path);
588 dbus_message_iter_next(&value);
592 static void check_pending_networks(const char *adapter)
594 GSList *networks, *list;
596 networks = g_hash_table_lookup(pending_networks, adapter);
600 for (list = networks; list; list = list->next) {
601 char *path = list->data;
606 g_hash_table_remove(pending_networks, adapter);
609 static gboolean adapter_changed(DBusConnection *conn,
610 DBusMessage *message, void *user_data)
612 const char *path = dbus_message_get_path(message);
613 struct connman_device *device;
614 DBusMessageIter iter, value;
617 DBG("path %s", path);
619 device = g_hash_table_lookup(bluetooth_devices, path);
623 if (!dbus_message_iter_init(message, &iter))
626 dbus_message_iter_get_basic(&iter, &key);
628 dbus_message_iter_next(&iter);
629 dbus_message_iter_recurse(&iter, &value);
631 if (g_str_equal(key, "Powered")) {
634 dbus_message_iter_get_basic(&value, &val);
635 connman_device_set_powered(device, val);
637 check_pending_networks(path);
638 } else if (g_str_equal(key, "Discovering")) {
641 dbus_message_iter_get_basic(&value, &val);
642 connman_device_set_scanning(device,
643 CONNMAN_SERVICE_TYPE_BLUETOOTH, val);
644 } else if (g_str_equal(key, "Devices")) {
645 check_networks(&value);
651 static gboolean device_removed(DBusConnection *conn,
652 DBusMessage *message, void *user_data)
654 const char *network_path;
655 struct connman_network *network;
656 struct connman_device *device;
657 DBusMessageIter iter;
661 if (!dbus_message_iter_init(message, &iter))
664 dbus_message_iter_get_basic(&iter, &network_path);
666 network = g_hash_table_lookup(bluetooth_networks, network_path);
670 device = connman_network_get_device(network);
674 g_hash_table_remove(bluetooth_networks, network_path);
679 static gboolean device_changed(DBusConnection *conn,
680 DBusMessage *message, void *user_data)
682 const char *path = dbus_message_get_path(message);
683 DBusMessageIter iter, value;
686 DBG("path %s", path);
688 if (!dbus_message_iter_init(message, &iter))
691 dbus_message_iter_get_basic(&iter, &key);
693 dbus_message_iter_next(&iter);
694 dbus_message_iter_recurse(&iter, &value);
698 if (g_str_equal(key, "UUIDs"))
704 static void remove_device_networks(struct connman_device *device)
708 GSList *key_list = NULL;
711 if (!bluetooth_networks)
714 g_hash_table_iter_init(&iter, bluetooth_networks);
716 while (g_hash_table_iter_next(&iter, &key, &value)) {
717 struct connman_network *network = value;
719 if (connman_network_get_device(network) != device)
722 key_list = g_slist_prepend(key_list, key);
725 for (list = key_list; list; list = list->next) {
726 const char *network_path = list->data;
728 g_hash_table_remove(bluetooth_networks, network_path);
731 g_slist_free(key_list);
734 static void add_pending_networks(const char *adapter, DBusMessageIter *array)
736 DBusMessageIter value;
739 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
742 dbus_message_iter_recurse(array, &value);
744 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
747 dbus_message_iter_get_basic(&value, &path);
749 list = g_slist_prepend(list, g_strdup(path));
751 dbus_message_iter_next(&value);
757 g_hash_table_replace(pending_networks, g_strdup(adapter), list);
760 static void adapter_properties_reply(DBusPendingCall *call, void *user_data)
762 char *path = user_data;
763 struct connman_device *device;
765 DBusMessageIter networks;
766 const char *address = NULL, *name = NULL;
767 dbus_bool_t powered = FALSE, scanning = FALSE;
768 struct ether_addr addr;
771 DBG("path %s", path);
773 reply = dbus_pending_call_steal_reply(call);
778 extract_properties(reply, NULL, &address, &name, NULL,
779 &powered, &scanning, NULL, &networks);
784 if (g_strcmp0(address, "00:00:00:00:00:00") == 0)
787 device = g_hash_table_lookup(bluetooth_devices, path);
791 ether_aton_r(address, &addr);
793 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
794 addr.ether_addr_octet[0],
795 addr.ether_addr_octet[1],
796 addr.ether_addr_octet[2],
797 addr.ether_addr_octet[3],
798 addr.ether_addr_octet[4],
799 addr.ether_addr_octet[5]);
801 device = connman_device_create("bluetooth_legacy",
802 CONNMAN_DEVICE_TYPE_BLUETOOTH);
806 g_hash_table_insert(bluetooth_devices, g_strdup(path), device);
808 connman_device_set_ident(device, ident);
810 connman_device_set_string(device, "Path", path);
812 if (connman_device_register(device) < 0) {
813 connman_device_unref(device);
814 g_hash_table_remove(bluetooth_devices, path);
819 connman_device_set_string(device, "Address", address);
820 connman_device_set_string(device, "Name", name);
821 connman_device_set_string(device, "Path", path);
823 connman_device_set_powered(device, powered);
824 connman_device_set_scanning(device,
825 CONNMAN_SERVICE_TYPE_BLUETOOTH, scanning);
828 remove_device_networks(device);
829 add_pending_networks(path, &networks);
831 check_networks(&networks);
834 dbus_message_unref(reply);
836 dbus_pending_call_unref(call);
839 static void add_adapter(DBusConnection *conn, const char *path)
841 DBusMessage *message;
842 DBusPendingCall *call;
844 DBG("path %s", path);
846 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
847 BLUEZ_ADAPTER_INTERFACE, GET_PROPERTIES);
851 dbus_message_set_auto_start(message, FALSE);
853 if (!dbus_connection_send_with_reply(conn, message, &call, TIMEOUT)) {
854 connman_error("Failed to get adapter properties for %s", path);
859 connman_error("D-Bus connection not available");
863 dbus_pending_call_set_notify(call, adapter_properties_reply,
864 g_strdup(path), g_free);
867 dbus_message_unref(message);
870 static gboolean adapter_added(DBusConnection *conn, DBusMessage *message,
875 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
877 add_adapter(conn, path);
881 static void remove_adapter(DBusConnection *conn, const char *path)
883 DBG("path %s", path);
885 g_hash_table_remove(bluetooth_devices, path);
886 g_hash_table_remove(pending_networks, path);
889 static gboolean adapter_removed(DBusConnection *conn, DBusMessage *message,
894 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
896 remove_adapter(conn, path);
900 static void list_adapters_reply(DBusPendingCall *call, void *user_data)
909 reply = dbus_pending_call_steal_reply(call);
911 dbus_error_init(&error);
913 if (dbus_set_error_from_message(&error, reply)) {
914 connman_error("%s", error.message);
915 dbus_error_free(&error);
919 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_ARRAY,
920 DBUS_TYPE_OBJECT_PATH, &adapters,
921 &num_adapters, DBUS_TYPE_INVALID)) {
922 if (dbus_error_is_set(&error)) {
923 connman_error("%s", error.message);
924 dbus_error_free(&error);
926 connman_error("Wrong arguments for adapter list");
930 for (i = 0; i < num_adapters; i++)
931 add_adapter(connection, adapters[i]);
933 g_strfreev(adapters);
936 dbus_message_unref(reply);
938 dbus_pending_call_unref(call);
941 static void unregister_device(gpointer data)
943 struct connman_device *device = data;
947 remove_device_networks(device);
949 connman_device_unregister(device);
950 connman_device_unref(device);
953 static void remove_network(gpointer data)
955 struct connman_network *network = data;
956 struct connman_device *device;
958 DBG("network %p", network);
960 device = connman_network_get_device(network);
962 connman_device_remove_network(device, network);
964 connman_network_unref(network);
967 static void remove_pending_networks(gpointer data)
971 g_slist_free_full(list, g_free);
974 static void bluetooth_connect(DBusConnection *conn, void *user_data)
976 DBusMessage *message;
977 DBusPendingCall *call;
979 DBG("connection %p", conn);
981 bluetooth_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
982 g_free, unregister_device);
984 bluetooth_networks = g_hash_table_new_full(g_str_hash, g_str_equal,
985 g_free, remove_network);
987 pending_networks = g_hash_table_new_full(g_str_hash, g_str_equal,
988 g_free, remove_pending_networks);
990 message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
991 BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS);
995 dbus_message_set_auto_start(message, FALSE);
997 if (!dbus_connection_send_with_reply(conn, message, &call, TIMEOUT)) {
998 connman_error("Failed to get Bluetooth adapters");
1003 connman_error("D-Bus connection not available");
1007 dbus_pending_call_set_notify(call, list_adapters_reply, NULL, NULL);
1010 dbus_message_unref(message);
1013 static void bluetooth_disconnect(DBusConnection *conn, void *user_data)
1015 DBG("connection %p", conn);
1017 if (!bluetooth_devices)
1020 g_hash_table_destroy(bluetooth_networks);
1021 bluetooth_networks = NULL;
1022 g_hash_table_destroy(bluetooth_devices);
1023 bluetooth_devices = NULL;
1024 g_hash_table_destroy(pending_networks);
1025 pending_networks = NULL;
1028 static int bluetooth_probe(struct connman_device *device)
1030 GHashTableIter iter;
1031 gpointer key, value;
1033 DBG("device %p", device);
1035 if (!bluetooth_devices)
1038 g_hash_table_iter_init(&iter, bluetooth_devices);
1040 while (g_hash_table_iter_next(&iter, &key, &value)) {
1041 struct connman_device *device_pan = value;
1043 if (device == device_pan)
1050 static void bluetooth_remove(struct connman_device *device)
1052 DBG("device %p", device);
1055 static void powered_reply(DBusPendingCall *call, void *user_data)
1062 reply = dbus_pending_call_steal_reply(call);
1064 dbus_error_init(&error);
1066 if (dbus_set_error_from_message(&error, reply)) {
1067 connman_error("%s", error.message);
1068 dbus_error_free(&error);
1069 dbus_message_unref(reply);
1070 dbus_pending_call_unref(call);
1074 dbus_message_unref(reply);
1075 dbus_pending_call_unref(call);
1077 add_adapter(connection, user_data);
1080 static int change_powered(DBusConnection *conn, const char *path,
1081 dbus_bool_t powered)
1083 DBusMessage *message;
1084 DBusMessageIter iter;
1085 DBusPendingCall *call;
1092 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
1093 BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY);
1097 dbus_message_set_auto_start(message, FALSE);
1099 dbus_message_iter_init_append(message, &iter);
1100 connman_dbus_property_append_basic(&iter, "Powered",
1101 DBUS_TYPE_BOOLEAN, &powered);
1103 if (!dbus_connection_send_with_reply(conn, message, &call, TIMEOUT)) {
1104 connman_error("Failed to change Powered property");
1105 dbus_message_unref(message);
1110 connman_error("D-Bus connection not available");
1111 dbus_message_unref(message);
1115 dbus_pending_call_set_notify(call, powered_reply,
1116 g_strdup(path), g_free);
1118 dbus_message_unref(message);
1120 return -EINPROGRESS;
1123 static int bluetooth_enable(struct connman_device *device)
1125 const char *path = connman_device_get_string(device, "Path");
1127 DBG("device %p", device);
1129 return change_powered(connection, path, TRUE);
1132 static int bluetooth_disable(struct connman_device *device)
1134 const char *path = connman_device_get_string(device, "Path");
1136 DBG("device %p", device);
1138 return change_powered(connection, path, FALSE);
1141 static struct connman_device_driver bluetooth_driver = {
1142 .name = "bluetooth_legacy",
1143 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
1144 .probe = bluetooth_probe,
1145 .remove = bluetooth_remove,
1146 .enable = bluetooth_enable,
1147 .disable = bluetooth_disable,
1150 static int tech_probe(struct connman_technology *technology)
1155 static void tech_remove(struct connman_technology *technology)
1159 static void server_register_reply(DBusPendingCall *call, void *user_data)
1161 struct connman_technology *technology = user_data;
1167 reply = dbus_pending_call_steal_reply(call);
1169 dbus_error_init(&error);
1171 if (dbus_set_error_from_message(&error, reply)) {
1172 connman_error("%s", error.message);
1173 dbus_error_free(&error);
1174 dbus_message_unref(reply);
1175 dbus_pending_call_unref(call);
1179 dbus_message_unref(reply);
1180 dbus_pending_call_unref(call);
1182 connman_technology_tethering_notify(technology, true);
1185 static void server_unregister_reply(DBusPendingCall *call, void *user_data)
1187 struct connman_technology *technology = user_data;
1193 reply = dbus_pending_call_steal_reply(call);
1195 dbus_error_init(&error);
1197 if (dbus_set_error_from_message(&error, reply)) {
1198 connman_error("%s", error.message);
1199 dbus_error_free(&error);
1200 dbus_message_unref(reply);
1201 dbus_pending_call_unref(call);
1205 dbus_message_unref(reply);
1206 dbus_pending_call_unref(call);
1208 connman_technology_tethering_notify(technology, false);
1212 static void server_register(const char *path, const char *uuid,
1213 struct connman_technology *technology,
1214 const char *bridge, bool enabled)
1216 DBusMessage *message;
1217 DBusPendingCall *call;
1220 DBG("path %s enabled %d", path, enabled);
1222 command = enabled ? REGISTER : UNREGISTER;
1224 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
1225 BLUEZ_NETWORK_SERVER, command);
1229 dbus_message_set_auto_start(message, FALSE);
1231 dbus_message_append_args(message, DBUS_TYPE_STRING, &uuid,
1235 dbus_message_append_args(message, DBUS_TYPE_STRING, &bridge,
1238 if (!dbus_connection_send_with_reply(connection, message,
1240 connman_error("Failed to enable PAN server");
1241 dbus_message_unref(message);
1246 connman_error("D-Bus connection not available");
1247 dbus_message_unref(message);
1252 dbus_pending_call_set_notify(call, server_register_reply,
1255 dbus_pending_call_set_notify(call, server_unregister_reply,
1258 dbus_message_unref(message);
1261 struct tethering_info {
1262 struct connman_technology *technology;
1266 static void enable_nap(gpointer key, gpointer value, gpointer user_data)
1268 struct tethering_info *info = user_data;
1269 struct connman_device *device = value;
1274 path = connman_device_get_string(device, "Path");
1276 server_register(path, "nap", info->technology, info->bridge, true);
1279 static void disable_nap(gpointer key, gpointer value, gpointer user_data)
1281 struct tethering_info *info = user_data;
1282 struct connman_device *device = value;
1287 path = connman_device_get_string(device, "Path");
1289 server_register(path, "nap", info->technology, info->bridge, false);
1292 static int tech_set_tethering(struct connman_technology *technology,
1293 const char *identifier, const char *passphrase,
1294 const char *bridge, bool enabled, bool hidden)
1296 struct tethering_info info = {
1297 .technology = technology,
1301 DBG("bridge %s", bridge);
1303 if (!bluetooth_devices)
1307 g_hash_table_foreach(bluetooth_devices, enable_nap, &info);
1309 g_hash_table_foreach(bluetooth_devices, disable_nap, &info);
1314 static struct connman_technology_driver tech_driver = {
1315 .name = "bluetooth_legacy",
1316 .type = CONNMAN_SERVICE_TYPE_BLUETOOTH,
1318 .probe = tech_probe,
1319 .remove = tech_remove,
1320 .set_tethering = tech_set_tethering,
1324 static guint added_watch;
1325 static guint removed_watch;
1326 static guint adapter_watch;
1327 static guint device_watch;
1328 static guint device_removed_watch;
1329 static guint network_watch;
1330 static guint peerconnected_watch;
1331 static guint peerdisconnected_watch;
1333 static int bluetooth_init(void)
1337 connection = connman_dbus_get_connection();
1341 watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
1342 bluetooth_connect, bluetooth_disconnect, NULL, NULL);
1344 added_watch = g_dbus_add_signal_watch(connection, BLUEZ_SERVICE, NULL,
1345 BLUEZ_MANAGER_INTERFACE,
1346 ADAPTER_ADDED, adapter_added,
1349 removed_watch = g_dbus_add_signal_watch(connection, BLUEZ_SERVICE, NULL,
1350 BLUEZ_MANAGER_INTERFACE,
1351 ADAPTER_REMOVED, adapter_removed,
1354 adapter_watch = g_dbus_add_signal_watch(connection, BLUEZ_SERVICE,
1355 NULL, BLUEZ_ADAPTER_INTERFACE,
1356 PROPERTY_CHANGED, adapter_changed,
1359 device_removed_watch = g_dbus_add_signal_watch(connection,
1360 BLUEZ_SERVICE, NULL,
1361 BLUEZ_ADAPTER_INTERFACE,
1362 DEVICE_REMOVED, device_removed,
1365 device_watch = g_dbus_add_signal_watch(connection, BLUEZ_SERVICE, NULL,
1366 BLUEZ_DEVICE_INTERFACE,
1367 PROPERTY_CHANGED, device_changed,
1370 network_watch = g_dbus_add_signal_watch(connection, BLUEZ_SERVICE,
1371 NULL, BLUEZ_NETWORK_INTERFACE,
1372 PROPERTY_CHANGED, network_changed,
1375 peerconnected_watch = g_dbus_add_signal_watch(connection,
1377 NULL, BLUEZ_NETWORK_SERVER,
1378 PEER_CONNECTED, peer_connected,
1381 peerdisconnected_watch = g_dbus_add_signal_watch(connection,
1383 NULL, BLUEZ_NETWORK_SERVER,
1388 if (watch == 0 || added_watch == 0 || removed_watch == 0
1389 || adapter_watch == 0 || network_watch == 0 || device_watch == 0
1390 || peerconnected_watch == 0 || peerdisconnected_watch == 0
1391 || device_removed_watch == 0) {
1396 err = connman_network_driver_register(&pan_driver);
1400 err = connman_device_driver_register(&bluetooth_driver);
1402 connman_network_driver_unregister(&pan_driver);
1406 err = connman_technology_driver_register(&tech_driver);
1408 connman_device_driver_unregister(&bluetooth_driver);
1409 connman_network_driver_unregister(&pan_driver);
1416 g_dbus_remove_watch(connection, watch);
1417 g_dbus_remove_watch(connection, added_watch);
1418 g_dbus_remove_watch(connection, removed_watch);
1419 g_dbus_remove_watch(connection, adapter_watch);
1420 g_dbus_remove_watch(connection, device_removed_watch);
1421 g_dbus_remove_watch(connection, device_watch);
1422 g_dbus_remove_watch(connection, network_watch);
1423 g_dbus_remove_watch(connection, peerconnected_watch);
1424 g_dbus_remove_watch(connection, peerdisconnected_watch);
1426 dbus_connection_unref(connection);
1431 static void bluetooth_exit(void)
1433 g_dbus_remove_watch(connection, watch);
1434 g_dbus_remove_watch(connection, added_watch);
1435 g_dbus_remove_watch(connection, removed_watch);
1436 g_dbus_remove_watch(connection, adapter_watch);
1437 g_dbus_remove_watch(connection, device_removed_watch);
1438 g_dbus_remove_watch(connection, device_watch);
1439 g_dbus_remove_watch(connection, network_watch);
1440 g_dbus_remove_watch(connection, peerconnected_watch);
1441 g_dbus_remove_watch(connection, peerdisconnected_watch);
1444 * We unset the disabling of the Bluetooth device when shutting down
1445 * so that non-PAN BT connections are not affected.
1447 bluetooth_driver.disable = NULL;
1449 bluetooth_disconnect(connection, NULL);
1451 connman_technology_driver_unregister(&tech_driver);
1453 connman_device_driver_unregister(&bluetooth_driver);
1454 connman_network_driver_unregister(&pan_driver);
1456 dbus_connection_unref(connection);
1459 CONNMAN_PLUGIN_DEFINE(bluetooth_legacy, "Bluetooth technology plugin (legacy)",
1460 VERSION, CONNMAN_PLUGIN_PRIORITY_LOW,
1461 bluetooth_init, bluetooth_exit)