5 * Copyright (C) 2007-2010 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/device.h>
37 #include <connman/inet.h>
38 #include <connman/dbus.h>
39 #include <connman/log.h>
41 #define BLUEZ_SERVICE "org.bluez"
42 #define BLUEZ_MANAGER_INTERFACE BLUEZ_SERVICE ".Manager"
43 #define BLUEZ_ADAPTER_INTERFACE BLUEZ_SERVICE ".Adapter"
44 #define BLUEZ_DEVICE_INTERFACE BLUEZ_SERVICE ".Device"
45 #define BLUEZ_NETWORK_INTERFACE BLUEZ_SERVICE ".Network"
47 #define LIST_ADAPTERS "ListAdapters"
48 #define ADAPTER_ADDED "AdapterAdded"
49 #define ADAPTER_REMOVED "AdapterRemoved"
51 #define PROPERTY_CHANGED "PropertyChanged"
52 #define GET_PROPERTIES "GetProperties"
53 #define SET_PROPERTY "SetProperty"
55 #define CONNECT "Connect"
56 #define DISCONNECT "Disconnect"
58 #define UUID_NAP "00001116-0000-1000-8000-00805f9b34fb"
62 static DBusConnection *connection;
64 static GHashTable *bluetooth_devices = NULL;
65 static GHashTable *bluetooth_networks = NULL;
67 static int pan_probe(struct connman_network *network)
69 DBG("network %p", network);
74 static void pan_remove(struct connman_network *network)
76 DBG("network %p", network);
79 static void connect_reply(DBusPendingCall *call, void *user_data)
81 struct connman_network *network = user_data;
84 const char *interface = NULL;
87 DBG("network %p", network);
89 reply = dbus_pending_call_steal_reply(call);
91 dbus_error_init(&error);
93 if (dbus_set_error_from_message(&error, reply) == TRUE) {
94 connman_error("%s", error.message);
95 dbus_error_free(&error);
99 if (dbus_message_get_args(reply, &error,
100 DBUS_TYPE_STRING, &interface,
101 DBUS_TYPE_INVALID) == FALSE) {
102 if (dbus_error_is_set(&error) == TRUE) {
103 connman_error("%s", error.message);
104 dbus_error_free(&error);
106 connman_error("Wrong arguments for connect");
110 if (interface == NULL)
113 DBG("interface %s", interface);
115 index = connman_inet_ifindex(interface);
117 connman_network_set_index(network, index);
119 connman_network_set_connected(network, TRUE);
122 dbus_message_unref(reply);
124 dbus_pending_call_unref(call);
127 static int pan_connect(struct connman_network *network)
129 const char *path = connman_network_get_string(network, "Path");
130 const char *uuid = "nap";
131 DBusMessage *message;
132 DBusPendingCall *call;
134 DBG("network %p", network);
139 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
140 BLUEZ_NETWORK_INTERFACE, CONNECT);
144 dbus_message_set_auto_start(message, FALSE);
146 dbus_message_append_args(message, DBUS_TYPE_STRING, &uuid,
149 if (dbus_connection_send_with_reply(connection, message,
150 &call, TIMEOUT * 10) == FALSE) {
151 connman_error("Failed to connect service");
152 dbus_message_unref(message);
157 connman_error("D-Bus connection not available");
158 dbus_message_unref(message);
162 dbus_pending_call_set_notify(call, connect_reply, network, NULL);
164 dbus_message_unref(message);
169 static void disconnect_reply(DBusPendingCall *call, void *user_data)
171 struct connman_network *network = user_data;
175 DBG("network %p", network);
177 reply = dbus_pending_call_steal_reply(call);
179 dbus_error_init(&error);
181 if (dbus_set_error_from_message(&error, reply) == TRUE) {
182 connman_error("%s", error.message);
183 dbus_error_free(&error);
187 if (dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) == FALSE) {
188 if (dbus_error_is_set(&error) == TRUE) {
189 connman_error("%s", error.message);
190 dbus_error_free(&error);
192 connman_error("Wrong arguments for disconnect");
196 connman_network_set_connected(network, FALSE);
199 dbus_message_unref(reply);
201 dbus_pending_call_unref(call);
203 connman_network_unref(network);
206 static int pan_disconnect(struct connman_network *network)
208 const char *path = connman_network_get_string(network, "Path");
209 DBusMessage *message;
210 DBusPendingCall *call;
212 DBG("network %p", network);
217 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
218 BLUEZ_NETWORK_INTERFACE, DISCONNECT);
222 dbus_message_set_auto_start(message, FALSE);
224 dbus_message_append_args(message, DBUS_TYPE_INVALID);
226 if (dbus_connection_send_with_reply(connection, message,
227 &call, TIMEOUT) == FALSE) {
228 connman_error("Failed to disconnect service");
229 dbus_message_unref(message);
234 connman_error("D-Bus connection not available");
235 dbus_message_unref(message);
239 connman_network_ref(network);
241 connman_network_set_associating(network, FALSE);
243 dbus_pending_call_set_notify(call, disconnect_reply, network, NULL);
245 dbus_message_unref(message);
250 static struct connman_network_driver pan_driver = {
251 .name = "bluetooth-pan",
252 .type = CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN,
254 .remove = pan_remove,
255 .connect = pan_connect,
256 .disconnect = pan_disconnect,
259 static gboolean network_changed(DBusConnection *connection,
260 DBusMessage *message, void *user_data)
262 const char *path = dbus_message_get_path(message);
263 struct connman_network *network;
264 DBusMessageIter iter, value;
267 DBG("path %s", path);
269 network = g_hash_table_lookup(bluetooth_networks, path);
273 if (dbus_message_iter_init(message, &iter) == FALSE)
276 dbus_message_iter_get_basic(&iter, &key);
278 dbus_message_iter_next(&iter);
279 dbus_message_iter_recurse(&iter, &value);
281 if (g_str_equal(key, "Connected") == TRUE) {
282 dbus_bool_t connected;
284 dbus_message_iter_get_basic(&value, &connected);
286 if (connected == TRUE)
289 connman_network_set_associating(network, FALSE);
290 connman_network_set_connected(network, FALSE);
296 static void extract_properties(DBusMessage *reply, const char **parent,
297 const char **address,
300 dbus_bool_t *powered,
301 dbus_bool_t *scanning,
302 DBusMessageIter *uuids,
303 DBusMessageIter *networks)
305 DBusMessageIter array, dict;
307 if (dbus_message_iter_init(reply, &array) == FALSE)
310 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
313 dbus_message_iter_recurse(&array, &dict);
315 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
316 DBusMessageIter entry, value;
319 dbus_message_iter_recurse(&dict, &entry);
320 dbus_message_iter_get_basic(&entry, &key);
322 dbus_message_iter_next(&entry);
323 dbus_message_iter_recurse(&entry, &value);
325 if (g_str_equal(key, "Adapter") == TRUE) {
327 dbus_message_iter_get_basic(&value, parent);
328 } else if (g_str_equal(key, "Address") == TRUE) {
330 dbus_message_iter_get_basic(&value, address);
331 } else if (g_str_equal(key, "Name") == TRUE) {
333 dbus_message_iter_get_basic(&value, name);
334 } else if (g_str_equal(key, "Alias") == TRUE) {
336 dbus_message_iter_get_basic(&value, alias);
337 } else if (g_str_equal(key, "Powered") == TRUE) {
339 dbus_message_iter_get_basic(&value, powered);
340 } else if (g_str_equal(key, "Discovering") == TRUE) {
341 if (scanning != NULL)
342 dbus_message_iter_get_basic(&value, scanning);
343 } else if (g_str_equal(key, "Devices") == TRUE) {
344 if (networks != NULL)
345 memcpy(networks, &value, sizeof(value));
346 } else if (g_str_equal(key, "UUIDs") == TRUE) {
348 memcpy(uuids, &value, sizeof(value));
351 dbus_message_iter_next(&dict);
355 static dbus_bool_t has_pan(DBusMessageIter *array)
357 DBusMessageIter value;
359 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
362 dbus_message_iter_recurse(array, &value);
364 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
367 dbus_message_iter_get_basic(&value, &uuid);
369 if (g_strcmp0(uuid, UUID_NAP) == 0)
372 dbus_message_iter_next(&value);
378 static void network_properties_reply(DBusPendingCall *call, void *user_data)
380 char *path = user_data;
381 struct connman_device *device;
382 struct connman_network *network;
384 DBusMessageIter uuids;
385 const char *parent = NULL, *address = NULL, *name = NULL;
386 struct ether_addr addr;
389 reply = dbus_pending_call_steal_reply(call);
391 extract_properties(reply, &parent, &address, NULL, &name,
392 NULL, NULL, &uuids, NULL);
397 device = g_hash_table_lookup(bluetooth_devices, parent);
404 ether_aton_r(address, &addr);
406 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
407 addr.ether_addr_octet[0],
408 addr.ether_addr_octet[1],
409 addr.ether_addr_octet[2],
410 addr.ether_addr_octet[3],
411 addr.ether_addr_octet[4],
412 addr.ether_addr_octet[5]);
414 if (has_pan(&uuids) == FALSE)
417 network = connman_device_get_network(device, ident);
421 network = connman_network_create(ident,
422 CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
426 connman_network_set_string(network, "Path", path);
428 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
430 connman_network_set_name(network, name);
432 connman_device_add_network(device, network);
434 connman_network_set_group(network, ident);
436 g_hash_table_insert(bluetooth_networks, g_strdup(path), network);
439 dbus_message_unref(reply);
441 dbus_pending_call_unref(call);
444 static void add_network(struct connman_device *device, const char *path)
446 DBusMessage *message;
447 DBusPendingCall *call;
449 DBG("path %s", path);
451 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
452 BLUEZ_DEVICE_INTERFACE, GET_PROPERTIES);
456 dbus_message_set_auto_start(message, FALSE);
458 if (dbus_connection_send_with_reply(connection, message,
459 &call, TIMEOUT) == FALSE) {
460 connman_error("Failed to get network properties for %s", path);
465 connman_error("D-Bus connection not available");
469 dbus_pending_call_set_notify(call, network_properties_reply,
470 g_strdup(path), g_free);
473 dbus_message_unref(message);
476 static void check_networks(struct connman_device *device,
477 DBusMessageIter *array)
479 DBusMessageIter value;
481 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
484 dbus_message_iter_recurse(array, &value);
486 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
489 dbus_message_iter_get_basic(&value, &path);
491 add_network(device, path);
493 dbus_message_iter_next(&value);
497 static gboolean adapter_changed(DBusConnection *connection,
498 DBusMessage *message, void *user_data)
500 const char *path = dbus_message_get_path(message);
501 struct connman_device *device;
502 DBusMessageIter iter, value;
505 DBG("path %s", path);
507 device = g_hash_table_lookup(bluetooth_devices, path);
511 if (dbus_message_iter_init(message, &iter) == FALSE)
514 dbus_message_iter_get_basic(&iter, &key);
516 dbus_message_iter_next(&iter);
517 dbus_message_iter_recurse(&iter, &value);
519 if (g_str_equal(key, "Powered") == TRUE) {
522 dbus_message_iter_get_basic(&value, &val);
523 connman_device_set_powered(device, val);
524 } else if (g_str_equal(key, "Discovering") == TRUE) {
527 dbus_message_iter_get_basic(&value, &val);
528 connman_device_set_scanning(device, val);
529 } else if (g_str_equal(key, "Devices") == TRUE) {
530 check_networks(device, &value);
536 static void adapter_properties_reply(DBusPendingCall *call, void *user_data)
538 char *path = user_data;
539 struct connman_device *device;
541 DBusMessageIter networks;
542 const char *address = NULL, *name = NULL;
543 dbus_bool_t powered = FALSE, scanning = FALSE;
544 struct ether_addr addr;
547 DBG("path %s", path);
549 reply = dbus_pending_call_steal_reply(call);
554 extract_properties(reply, NULL, &address, &name, NULL,
555 &powered, &scanning, NULL, &networks);
560 device = g_hash_table_lookup(bluetooth_devices, path);
564 ether_aton_r(address, &addr);
566 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
567 addr.ether_addr_octet[0],
568 addr.ether_addr_octet[1],
569 addr.ether_addr_octet[2],
570 addr.ether_addr_octet[3],
571 addr.ether_addr_octet[4],
572 addr.ether_addr_octet[5]);
574 device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_BLUETOOTH);
578 connman_device_set_ident(device, ident);
580 connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE);
582 connman_device_set_string(device, "Path", path);
584 if (connman_device_register(device) < 0) {
585 connman_device_unref(device);
589 g_hash_table_insert(bluetooth_devices, g_strdup(path), device);
592 connman_device_set_string(device, "Address", address);
593 connman_device_set_string(device, "Name", name);
594 connman_device_set_string(device, "Path", path);
596 connman_device_set_powered(device, powered);
597 connman_device_set_scanning(device, scanning);
600 check_networks(device, &networks);
603 dbus_message_unref(reply);
605 dbus_pending_call_unref(call);
608 static void add_adapter(DBusConnection *connection, const char *path)
610 DBusMessage *message;
611 DBusPendingCall *call;
613 DBG("path %s", path);
615 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
616 BLUEZ_ADAPTER_INTERFACE, GET_PROPERTIES);
620 dbus_message_set_auto_start(message, FALSE);
622 if (dbus_connection_send_with_reply(connection, message,
623 &call, TIMEOUT) == FALSE) {
624 connman_error("Failed to get adapter properties for %s", path);
629 connman_error("D-Bus connection not available");
633 dbus_pending_call_set_notify(call, adapter_properties_reply,
634 g_strdup(path), g_free);
637 dbus_message_unref(message);
640 static gboolean adapter_added(DBusConnection *connection, DBusMessage *message,
645 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
647 add_adapter(connection, path);
651 static void remove_adapter(DBusConnection *connection, const char *path)
653 DBG("path %s", path);
655 g_hash_table_remove(bluetooth_devices, path);
658 static gboolean adapter_removed(DBusConnection *connection, DBusMessage *message,
663 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
665 remove_adapter(connection, path);
669 static void list_adapters_reply(DBusPendingCall *call, void *user_data)
678 reply = dbus_pending_call_steal_reply(call);
680 dbus_error_init(&error);
682 if (dbus_set_error_from_message(&error, reply) == TRUE) {
683 connman_error("%s", error.message);
684 dbus_error_free(&error);
688 if (dbus_message_get_args(reply, &error,
689 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
690 &adapters, &num_adapters,
691 DBUS_TYPE_INVALID) == FALSE) {
692 if (dbus_error_is_set(&error) == TRUE) {
693 connman_error("%s", error.message);
694 dbus_error_free(&error);
696 connman_error("Wrong arguments for adapter list");
700 for (i = 0; i < num_adapters; i++)
701 add_adapter(connection, adapters[i]);
703 g_strfreev(adapters);
706 dbus_message_unref(reply);
708 dbus_pending_call_unref(call);
711 static void unregister_device(gpointer data)
713 struct connman_device *device = data;
717 connman_device_unregister(device);
718 connman_device_unref(device);
721 static void bluetooth_connect(DBusConnection *connection, void *user_data)
723 DBusMessage *message;
724 DBusPendingCall *call;
726 DBG("connection %p", connection);
728 bluetooth_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
729 g_free, unregister_device);
731 bluetooth_networks = g_hash_table_new_full(g_str_hash, g_str_equal,
734 message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
735 BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS);
739 dbus_message_set_auto_start(message, FALSE);
741 if (dbus_connection_send_with_reply(connection, message,
742 &call, TIMEOUT) == FALSE) {
743 connman_error("Failed to get Bluetooth adapters");
748 connman_error("D-Bus connection not available");
752 dbus_pending_call_set_notify(call, list_adapters_reply, NULL, NULL);
755 dbus_message_unref(message);
758 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
760 DBG("connection %p", connection);
762 if (bluetooth_devices == NULL)
765 g_hash_table_destroy(bluetooth_networks);
766 g_hash_table_destroy(bluetooth_devices);
767 bluetooth_devices = NULL;
770 static int bluetooth_probe(struct connman_device *device)
772 DBG("device %p", device);
777 static void bluetooth_remove(struct connman_device *device)
779 DBG("device %p", device);
782 static void powered_reply(DBusPendingCall *call, void *user_data)
789 reply = dbus_pending_call_steal_reply(call);
791 dbus_error_init(&error);
793 if (dbus_set_error_from_message(&error, reply) == TRUE) {
794 connman_error("%s", error.message);
795 dbus_error_free(&error);
796 dbus_message_unref(reply);
797 dbus_pending_call_unref(call);
801 dbus_message_unref(reply);
802 dbus_pending_call_unref(call);
804 add_adapter(connection, user_data);
807 static int change_powered(DBusConnection *connection, const char *path,
810 DBusMessage *message;
811 DBusMessageIter iter;
812 DBusPendingCall *call;
819 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
820 BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY);
824 dbus_message_set_auto_start(message, FALSE);
826 dbus_message_iter_init_append(message, &iter);
827 connman_dbus_property_append_basic(&iter, "Powered",
828 DBUS_TYPE_BOOLEAN, &powered);
830 if (dbus_connection_send_with_reply(connection, message,
831 &call, TIMEOUT) == FALSE) {
832 connman_error("Failed to change Powered property");
833 dbus_message_unref(message);
838 connman_error("D-Bus connection not available");
839 dbus_message_unref(message);
843 dbus_pending_call_set_notify(call, powered_reply,
844 g_strdup(path), g_free);
846 dbus_message_unref(message);
851 static int bluetooth_enable(struct connman_device *device)
853 const char *path = connman_device_get_string(device, "Path");
855 DBG("device %p", device);
857 return change_powered(connection, path, TRUE);
860 static int bluetooth_disable(struct connman_device *device)
862 const char *path = connman_device_get_string(device, "Path");
864 DBG("device %p", device);
866 return change_powered(connection, path, FALSE);
869 static struct connman_device_driver bluetooth_driver = {
871 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
872 .probe = bluetooth_probe,
873 .remove = bluetooth_remove,
874 .enable = bluetooth_enable,
875 .disable = bluetooth_disable,
879 static guint added_watch;
880 static guint removed_watch;
881 static guint adapter_watch;
882 static guint network_watch;
884 static int bluetooth_init(void)
888 connection = connman_dbus_get_connection();
889 if (connection == NULL)
892 watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
893 bluetooth_connect, bluetooth_disconnect, NULL, NULL);
895 added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
896 BLUEZ_MANAGER_INTERFACE,
897 ADAPTER_ADDED, adapter_added,
900 removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
901 BLUEZ_MANAGER_INTERFACE,
902 ADAPTER_REMOVED, adapter_removed,
905 adapter_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
906 BLUEZ_MANAGER_INTERFACE,
907 PROPERTY_CHANGED, adapter_changed,
910 network_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
911 BLUEZ_NETWORK_INTERFACE,
912 PROPERTY_CHANGED, network_changed,
915 if (watch == 0 || added_watch == 0 || removed_watch == 0
916 || adapter_watch == 0 || network_watch == 0) {
921 err = connman_network_driver_register(&pan_driver);
925 err = connman_device_driver_register(&bluetooth_driver);
927 connman_network_driver_unregister(&pan_driver);
934 g_dbus_remove_watch(connection, watch);
935 g_dbus_remove_watch(connection, added_watch);
936 g_dbus_remove_watch(connection, removed_watch);
937 g_dbus_remove_watch(connection, adapter_watch);
938 g_dbus_remove_watch(connection, network_watch);
940 dbus_connection_unref(connection);
945 static void bluetooth_exit(void)
947 g_dbus_remove_watch(connection, watch);
948 g_dbus_remove_watch(connection, added_watch);
949 g_dbus_remove_watch(connection, removed_watch);
950 g_dbus_remove_watch(connection, adapter_watch);
951 g_dbus_remove_watch(connection, network_watch);
953 bluetooth_disconnect(connection, NULL);
955 connman_device_driver_unregister(&bluetooth_driver);
956 connman_network_driver_unregister(&pan_driver);
958 dbus_connection_unref(connection);
961 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
962 CONNMAN_PLUGIN_PRIORITY_DEFAULT, bluetooth_init, bluetooth_exit)