5 * Copyright (C) 2007-2009 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;
66 static int pan_probe(struct connman_network *network)
68 DBG("network %p", network);
73 static void pan_remove(struct connman_network *network)
75 DBG("network %p", network);
78 static void connect_reply(DBusPendingCall *call, void *user_data)
80 struct connman_network *network = user_data;
83 const char *interface = NULL;
86 DBG("network %p", network);
88 reply = dbus_pending_call_steal_reply(call);
92 dbus_error_init(&error);
94 if (dbus_message_get_args(reply, &error,
95 DBUS_TYPE_STRING, &interface,
96 DBUS_TYPE_INVALID) == FALSE) {
97 if (dbus_error_is_set(&error) == TRUE) {
98 connman_error("%s", error.message);
99 dbus_error_free(&error);
101 connman_error("Wrong arguments for connect");
105 if (interface == NULL)
108 DBG("interface %s", interface);
110 index = connman_inet_ifindex(interface);
112 connman_network_set_index(network, index);
114 connman_network_set_method(network, CONNMAN_IPCONFIG_METHOD_DHCP);
116 connman_network_set_connected(network, TRUE);
119 dbus_message_unref(reply);
122 static int pan_connect(struct connman_network *network)
124 const char *path = connman_network_get_string(network, "Path");
125 const char *uuid = "nap";
126 DBusMessage *message;
127 DBusPendingCall *call;
129 DBG("network %p", network);
134 if (connman_network_get_index(network) >= 0)
137 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
138 BLUEZ_NETWORK_INTERFACE, CONNECT);
142 dbus_message_set_auto_start(message, FALSE);
144 dbus_message_append_args(message, DBUS_TYPE_STRING, &uuid,
147 if (dbus_connection_send_with_reply(connection, message,
148 &call, TIMEOUT * 10) == FALSE) {
149 connman_error("Failed to connect service");
150 dbus_message_unref(message);
155 connman_error("D-Bus connection not available");
156 dbus_message_unref(message);
160 dbus_pending_call_set_notify(call, connect_reply, network, NULL);
162 dbus_message_unref(message);
167 static void disconnect_reply(DBusPendingCall *call, void *user_data)
169 struct connman_network *network = user_data;
173 DBG("network %p", network);
175 reply = dbus_pending_call_steal_reply(call);
179 dbus_error_init(&error);
181 if (dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) == FALSE) {
182 if (dbus_error_is_set(&error) == TRUE) {
183 connman_error("%s", error.message);
184 dbus_error_free(&error);
186 connman_error("Wrong arguments for disconnect");
190 connman_network_set_connected(network, FALSE);
191 connman_network_set_index(network, -1);
194 dbus_message_unref(reply);
197 static int pan_disconnect(struct connman_network *network)
199 const char *path = connman_network_get_string(network, "Path");
200 DBusMessage *message;
201 DBusPendingCall *call;
203 DBG("network %p", network);
208 if (connman_network_get_index(network) < 0)
211 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
212 BLUEZ_NETWORK_INTERFACE, DISCONNECT);
216 dbus_message_set_auto_start(message, FALSE);
218 dbus_message_append_args(message, DBUS_TYPE_INVALID);
220 if (dbus_connection_send_with_reply(connection, message,
221 &call, TIMEOUT) == FALSE) {
222 connman_error("Failed to disconnect service");
223 dbus_message_unref(message);
228 connman_error("D-Bus connection not available");
229 dbus_message_unref(message);
233 dbus_pending_call_set_notify(call, disconnect_reply, network, NULL);
235 dbus_message_unref(message);
240 static struct connman_network_driver pan_driver = {
241 .name = "bluetooth-pan",
242 .type = CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN,
244 .remove = pan_remove,
245 .connect = pan_connect,
246 .disconnect = pan_disconnect,
249 static int bluetooth_probe(struct connman_device *device)
251 DBG("device %p", device);
256 static void bluetooth_remove(struct connman_device *device)
258 DBG("device %p", device);
261 static void powered_reply(DBusPendingCall *call, void *user_data)
267 reply = dbus_pending_call_steal_reply(call);
269 dbus_message_unref(reply);
272 static int change_powered(DBusConnection *connection, const char *path,
275 DBusMessage *message;
276 DBusMessageIter iter;
277 DBusPendingCall *call;
284 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
285 BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY);
289 dbus_message_set_auto_start(message, FALSE);
291 dbus_message_iter_init_append(message, &iter);
292 connman_dbus_property_append_basic(&iter, "Powered",
293 DBUS_TYPE_BOOLEAN, &powered);
295 if (dbus_connection_send_with_reply(connection, message,
296 &call, TIMEOUT) == FALSE) {
297 connman_error("Failed to change Powered property");
298 dbus_message_unref(message);
303 connman_error("D-Bus connection not available");
304 dbus_message_unref(message);
308 dbus_pending_call_set_notify(call, powered_reply, NULL, NULL);
310 dbus_message_unref(message);
315 static int bluetooth_enable(struct connman_device *device)
317 const char *path = connman_device_get_string(device, "Path");
319 DBG("device %p", device);
321 return change_powered(connection, path, TRUE);
324 static int bluetooth_disable(struct connman_device *device)
326 const char *path = connman_device_get_string(device, "Path");
328 DBG("device %p", device);
330 return change_powered(connection, path, FALSE);
333 static struct connman_device_driver bluetooth_driver = {
335 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
336 .probe = bluetooth_probe,
337 .remove = bluetooth_remove,
338 .enable = bluetooth_enable,
339 .disable = bluetooth_disable,
342 static void extract_properties(DBusMessage *reply, const char **parent,
343 const char **address,
346 dbus_bool_t *powered,
347 dbus_bool_t *scanning,
348 DBusMessageIter *uuids,
349 DBusMessageIter *networks)
351 DBusMessageIter array, dict;
353 if (dbus_message_iter_init(reply, &array) == FALSE)
356 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
359 dbus_message_iter_recurse(&array, &dict);
361 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
362 DBusMessageIter entry, value;
365 dbus_message_iter_recurse(&dict, &entry);
366 dbus_message_iter_get_basic(&entry, &key);
368 dbus_message_iter_next(&entry);
369 dbus_message_iter_recurse(&entry, &value);
371 if (g_str_equal(key, "Adapter") == TRUE) {
373 dbus_message_iter_get_basic(&value, parent);
374 } else if (g_str_equal(key, "Address") == TRUE) {
376 dbus_message_iter_get_basic(&value, address);
377 } else if (g_str_equal(key, "Name") == TRUE) {
379 dbus_message_iter_get_basic(&value, name);
380 } else if (g_str_equal(key, "Alias") == TRUE) {
382 dbus_message_iter_get_basic(&value, alias);
383 } else if (g_str_equal(key, "Powered") == TRUE) {
385 dbus_message_iter_get_basic(&value, powered);
386 } else if (g_str_equal(key, "Discovering") == TRUE) {
387 if (scanning != NULL)
388 dbus_message_iter_get_basic(&value, scanning);
389 } else if (g_str_equal(key, "Devices") == TRUE) {
390 if (networks != NULL)
391 memcpy(networks, &value, sizeof(value));
392 } else if (g_str_equal(key, "UUIDs") == TRUE) {
394 memcpy(uuids, &value, sizeof(value));
397 dbus_message_iter_next(&dict);
401 static dbus_bool_t has_pan(DBusMessageIter *array)
403 DBusMessageIter value;
405 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
408 dbus_message_iter_recurse(array, &value);
410 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
413 dbus_message_iter_get_basic(&value, &uuid);
415 if (g_strcmp0(uuid, UUID_NAP) == 0)
418 dbus_message_iter_next(&value);
424 static void network_properties_reply(DBusPendingCall *call, void *user_data)
426 char *path = user_data;
427 struct connman_device *device;
428 struct connman_network *network;
430 DBusMessageIter uuids;
431 const char *parent = NULL, *address = NULL, *name = NULL;
432 struct ether_addr addr;
435 reply = dbus_pending_call_steal_reply(call);
439 extract_properties(reply, &parent, &address, NULL, &name,
440 NULL, NULL, &uuids, NULL);
445 device = g_hash_table_lookup(bluetooth_devices, parent);
452 ether_aton_r(address, &addr);
454 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
455 addr.ether_addr_octet[0],
456 addr.ether_addr_octet[1],
457 addr.ether_addr_octet[2],
458 addr.ether_addr_octet[3],
459 addr.ether_addr_octet[4],
460 addr.ether_addr_octet[5]);
462 if (has_pan(&uuids) == FALSE)
465 network = connman_device_get_network(device, ident);
469 network = connman_network_create(ident,
470 CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
474 connman_network_set_string(network, "Path", path);
476 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
478 connman_network_set_name(network, name);
480 connman_device_add_network(device, network);
482 connman_network_set_group(network, ident);
485 dbus_message_unref(reply);
488 static void add_network(struct connman_device *device, const char *path)
490 DBusMessage *message;
491 DBusPendingCall *call;
493 DBG("path %s", path);
495 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
496 BLUEZ_DEVICE_INTERFACE, GET_PROPERTIES);
500 dbus_message_set_auto_start(message, FALSE);
502 if (dbus_connection_send_with_reply(connection, message,
503 &call, TIMEOUT) == FALSE) {
504 connman_error("Failed to get network properties for %s", path);
509 connman_error("D-Bus connection not available");
513 dbus_pending_call_set_notify(call, network_properties_reply,
514 g_strdup(path), g_free);
517 dbus_message_unref(message);
520 static void check_networks(struct connman_device *device,
521 DBusMessageIter *array)
523 DBusMessageIter value;
525 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
528 dbus_message_iter_recurse(array, &value);
530 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
533 dbus_message_iter_get_basic(&value, &path);
535 add_network(device, path);
537 dbus_message_iter_next(&value);
541 static gboolean adapter_changed(DBusConnection *connection,
542 DBusMessage *message, void *user_data)
544 const char *path = dbus_message_get_path(message);
545 struct connman_device *device;
546 DBusMessageIter iter, value;
549 DBG("path %s", path);
551 device = g_hash_table_lookup(bluetooth_devices, path);
555 if (dbus_message_iter_init(message, &iter) == FALSE)
558 dbus_message_iter_get_basic(&iter, &key);
560 dbus_message_iter_next(&iter);
561 dbus_message_iter_recurse(&iter, &value);
563 if (g_str_equal(key, "Powered") == TRUE) {
566 dbus_message_iter_get_basic(&value, &val);
567 connman_device_set_powered(device, val);
568 } else if (g_str_equal(key, "Discovering") == TRUE) {
571 dbus_message_iter_get_basic(&value, &val);
572 connman_device_set_scanning(device, val);
573 } else if (g_str_equal(key, "Devices") == TRUE) {
574 check_networks(device, &value);
580 static void adapter_properties_reply(DBusPendingCall *call, void *user_data)
582 char *path = user_data;
583 struct connman_device *device;
585 DBusMessageIter networks;
586 const char *address = NULL, *name = NULL;
587 dbus_bool_t powered = FALSE, scanning = FALSE;
588 struct ether_addr addr;
591 DBG("path %s", path);
593 reply = dbus_pending_call_steal_reply(call);
600 extract_properties(reply, NULL, &address, &name, NULL,
601 &powered, &scanning, NULL, &networks);
606 device = g_hash_table_lookup(bluetooth_devices, path);
610 ether_aton_r(address, &addr);
612 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
613 addr.ether_addr_octet[0],
614 addr.ether_addr_octet[1],
615 addr.ether_addr_octet[2],
616 addr.ether_addr_octet[3],
617 addr.ether_addr_octet[4],
618 addr.ether_addr_octet[5]);
620 device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_BLUETOOTH);
624 connman_device_set_ident(device, ident);
626 connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE);
628 if (connman_device_register(device) < 0) {
629 connman_device_unref(device);
633 g_hash_table_insert(bluetooth_devices, g_strdup(path), device);
636 connman_device_set_string(device, "Address", address);
637 connman_device_set_string(device, "Name", name);
638 connman_device_set_string(device, "Path", path);
640 connman_device_set_powered(device, powered);
641 connman_device_set_scanning(device, scanning);
644 check_networks(device, &networks);
647 dbus_message_unref(reply);
650 static void add_adapter(DBusConnection *connection, const char *path)
652 DBusMessage *message;
653 DBusPendingCall *call;
655 DBG("path %s", path);
657 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
658 BLUEZ_ADAPTER_INTERFACE, GET_PROPERTIES);
662 dbus_message_set_auto_start(message, FALSE);
664 if (dbus_connection_send_with_reply(connection, message,
665 &call, TIMEOUT) == FALSE) {
666 connman_error("Failed to get adapter properties for %s", path);
671 connman_error("D-Bus connection not available");
675 dbus_pending_call_set_notify(call, adapter_properties_reply,
676 g_strdup(path), g_free);
679 dbus_message_unref(message);
682 static gboolean adapter_added(DBusConnection *connection, DBusMessage *message,
687 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
689 add_adapter(connection, path);
693 static void remove_adapter(DBusConnection *connection, const char *path)
695 DBG("path %s", path);
697 g_hash_table_remove(bluetooth_devices, path);
700 static gboolean adapter_removed(DBusConnection *connection, DBusMessage *message,
705 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
707 remove_adapter(connection, path);
711 static void list_adapters_reply(DBusPendingCall *call, void *user_data)
720 reply = dbus_pending_call_steal_reply(call);
724 dbus_error_init(&error);
726 if (dbus_message_get_args(reply, &error,
727 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
728 &adapters, &num_adapters,
729 DBUS_TYPE_INVALID) == FALSE) {
730 if (dbus_error_is_set(&error) == TRUE) {
731 connman_error("%s", error.message);
732 dbus_error_free(&error);
734 connman_error("Wrong arguments for adapter list");
738 for (i = 0; i < num_adapters; i++)
739 add_adapter(connection, adapters[i]);
741 g_strfreev(adapters);
744 dbus_message_unref(reply);
747 static void unregister_device(gpointer data)
749 struct connman_device *device = data;
753 connman_device_unregister(device);
754 connman_device_unref(device);
757 static void bluetooth_connect(DBusConnection *connection, void *user_data)
759 DBusMessage *message;
760 DBusPendingCall *call;
762 DBG("connection %p", connection);
764 bluetooth_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
765 g_free, unregister_device);
767 message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
768 BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS);
772 dbus_message_set_auto_start(message, FALSE);
774 if (dbus_connection_send_with_reply(connection, message,
775 &call, TIMEOUT) == FALSE) {
776 connman_error("Failed to get Bluetooth adapters");
781 connman_error("D-Bus connection not available");
785 dbus_pending_call_set_notify(call, list_adapters_reply, NULL, NULL);
788 dbus_message_unref(message);
791 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
793 DBG("connection %p", connection);
795 if (bluetooth_devices == NULL)
798 g_hash_table_destroy(bluetooth_devices);
799 bluetooth_devices = NULL;
803 static guint added_watch;
804 static guint removed_watch;
805 static guint adapter_watch;
807 static int bluetooth_init(void)
811 connection = connman_dbus_get_connection();
812 if (connection == NULL)
815 watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
816 bluetooth_connect, bluetooth_disconnect, NULL, NULL);
818 added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
819 BLUEZ_MANAGER_INTERFACE,
820 ADAPTER_ADDED, adapter_added,
823 removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
824 BLUEZ_MANAGER_INTERFACE,
825 ADAPTER_REMOVED, adapter_removed,
828 adapter_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
829 BLUEZ_MANAGER_INTERFACE,
830 PROPERTY_CHANGED, adapter_changed,
833 if (watch == 0 || added_watch == 0 || removed_watch == 0
834 || adapter_watch == 0) {
839 err = connman_network_driver_register(&pan_driver);
843 err = connman_device_driver_register(&bluetooth_driver);
845 connman_network_driver_unregister(&pan_driver);
852 g_dbus_remove_watch(connection, watch);
853 g_dbus_remove_watch(connection, added_watch);
854 g_dbus_remove_watch(connection, removed_watch);
855 g_dbus_remove_watch(connection, adapter_watch);
857 dbus_connection_unref(connection);
862 static void bluetooth_exit(void)
864 g_dbus_remove_watch(connection, watch);
865 g_dbus_remove_watch(connection, added_watch);
866 g_dbus_remove_watch(connection, removed_watch);
867 g_dbus_remove_watch(connection, adapter_watch);
869 bluetooth_disconnect(connection, NULL);
871 connman_device_driver_unregister(&bluetooth_driver);
872 connman_network_driver_unregister(&pan_driver);
874 dbus_connection_unref(connection);
877 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
878 CONNMAN_PLUGIN_PRIORITY_DEFAULT, bluetooth_init, bluetooth_exit)