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);
113 connman_network_set_connected(network, TRUE);
116 dbus_message_unref(reply);
119 static int pan_connect(struct connman_network *network)
121 const char *path = connman_network_get_string(network, "Path");
122 const char *uuid = "nap";
123 DBusMessage *message;
124 DBusPendingCall *call;
126 DBG("network %p", network);
131 if (connman_network_get_index(network) >= 0)
134 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
135 BLUEZ_NETWORK_INTERFACE, CONNECT);
139 dbus_message_set_auto_start(message, FALSE);
141 dbus_message_append_args(message, DBUS_TYPE_STRING, &uuid,
144 if (dbus_connection_send_with_reply(connection, message,
145 &call, TIMEOUT * 10) == FALSE) {
146 connman_error("Failed to connect service");
147 dbus_message_unref(message);
152 connman_error("D-Bus connection not available");
153 dbus_message_unref(message);
157 dbus_pending_call_set_notify(call, connect_reply, network, NULL);
159 dbus_message_unref(message);
164 static void disconnect_reply(DBusPendingCall *call, void *user_data)
166 struct connman_network *network = user_data;
170 DBG("network %p", network);
172 reply = dbus_pending_call_steal_reply(call);
176 dbus_error_init(&error);
178 if (dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) == FALSE) {
179 if (dbus_error_is_set(&error) == TRUE) {
180 connman_error("%s", error.message);
181 dbus_error_free(&error);
183 connman_error("Wrong arguments for disconnect");
187 connman_network_set_connected(network, FALSE);
188 connman_network_set_index(network, -1);
191 dbus_message_unref(reply);
194 static int pan_disconnect(struct connman_network *network)
196 const char *path = connman_network_get_string(network, "Path");
197 DBusMessage *message;
198 DBusPendingCall *call;
200 DBG("network %p", network);
205 if (connman_network_get_index(network) < 0)
208 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
209 BLUEZ_NETWORK_INTERFACE, DISCONNECT);
213 dbus_message_set_auto_start(message, FALSE);
215 dbus_message_append_args(message, DBUS_TYPE_INVALID);
217 if (dbus_connection_send_with_reply(connection, message,
218 &call, TIMEOUT) == FALSE) {
219 connman_error("Failed to disconnect service");
220 dbus_message_unref(message);
225 connman_error("D-Bus connection not available");
226 dbus_message_unref(message);
230 dbus_pending_call_set_notify(call, disconnect_reply, network, NULL);
232 dbus_message_unref(message);
237 static struct connman_network_driver pan_driver = {
238 .name = "bluetooth-pan",
239 .type = CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN,
241 .remove = pan_remove,
242 .connect = pan_connect,
243 .disconnect = pan_disconnect,
246 static int bluetooth_probe(struct connman_device *device)
248 DBG("device %p", device);
253 static void bluetooth_remove(struct connman_device *device)
255 DBG("device %p", device);
258 static void powered_reply(DBusPendingCall *call, void *user_data)
264 reply = dbus_pending_call_steal_reply(call);
266 dbus_message_unref(reply);
269 static int change_powered(DBusConnection *connection, const char *path,
272 DBusMessage *message;
273 DBusMessageIter iter;
274 DBusPendingCall *call;
281 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
282 BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY);
286 dbus_message_set_auto_start(message, FALSE);
288 dbus_message_iter_init_append(message, &iter);
289 connman_dbus_property_append_variant(&iter, "Powered",
290 DBUS_TYPE_BOOLEAN, &powered);
292 if (dbus_connection_send_with_reply(connection, message,
293 &call, TIMEOUT) == FALSE) {
294 connman_error("Failed to change Powered property");
295 dbus_message_unref(message);
300 connman_error("D-Bus connection not available");
301 dbus_message_unref(message);
305 dbus_pending_call_set_notify(call, powered_reply, NULL, NULL);
307 dbus_message_unref(message);
312 static int bluetooth_enable(struct connman_device *device)
314 const char *path = connman_device_get_string(device, "Path");
316 DBG("device %p", device);
318 return change_powered(connection, path, TRUE);
321 static int bluetooth_disable(struct connman_device *device)
323 const char *path = connman_device_get_string(device, "Path");
325 DBG("device %p", device);
327 return change_powered(connection, path, FALSE);
330 static struct connman_device_driver bluetooth_driver = {
332 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
333 .probe = bluetooth_probe,
334 .remove = bluetooth_remove,
335 .enable = bluetooth_enable,
336 .disable = bluetooth_disable,
339 static void extract_properties(DBusMessage *reply, const char **parent,
340 const char **address,
343 dbus_bool_t *powered,
344 dbus_bool_t *scanning,
345 DBusMessageIter *uuids,
346 DBusMessageIter *networks)
348 DBusMessageIter array, dict;
350 if (dbus_message_iter_init(reply, &array) == FALSE)
353 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
356 dbus_message_iter_recurse(&array, &dict);
358 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
359 DBusMessageIter entry, value;
362 dbus_message_iter_recurse(&dict, &entry);
363 dbus_message_iter_get_basic(&entry, &key);
365 dbus_message_iter_next(&entry);
366 dbus_message_iter_recurse(&entry, &value);
368 if (g_str_equal(key, "Adapter") == TRUE) {
370 dbus_message_iter_get_basic(&value, parent);
371 } else if (g_str_equal(key, "Address") == TRUE) {
373 dbus_message_iter_get_basic(&value, address);
374 } else if (g_str_equal(key, "Name") == TRUE) {
376 dbus_message_iter_get_basic(&value, name);
377 } else if (g_str_equal(key, "Alias") == TRUE) {
379 dbus_message_iter_get_basic(&value, alias);
380 } else if (g_str_equal(key, "Powered") == TRUE) {
382 dbus_message_iter_get_basic(&value, powered);
383 } else if (g_str_equal(key, "Discovering") == TRUE) {
384 if (scanning != NULL)
385 dbus_message_iter_get_basic(&value, scanning);
386 } else if (g_str_equal(key, "Devices") == TRUE) {
387 if (networks != NULL)
388 memcpy(networks, &value, sizeof(value));
389 } else if (g_str_equal(key, "UUIDs") == TRUE) {
391 memcpy(uuids, &value, sizeof(value));
394 dbus_message_iter_next(&dict);
398 static dbus_bool_t has_pan(DBusMessageIter *array)
400 DBusMessageIter value;
402 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
405 dbus_message_iter_recurse(array, &value);
407 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
410 dbus_message_iter_get_basic(&value, &uuid);
412 if (g_strcmp0(uuid, UUID_NAP) == 0)
415 dbus_message_iter_next(&value);
421 static void network_properties_reply(DBusPendingCall *call, void *user_data)
423 char *path = user_data;
424 struct connman_device *device;
425 struct connman_network *network;
427 DBusMessageIter uuids;
428 const char *parent = NULL, *address = NULL, *name = NULL;
429 struct ether_addr addr;
432 reply = dbus_pending_call_steal_reply(call);
436 extract_properties(reply, &parent, &address, NULL, &name,
437 NULL, NULL, &uuids, NULL);
442 device = g_hash_table_lookup(bluetooth_devices, parent);
449 ether_aton_r(address, &addr);
451 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
452 addr.ether_addr_octet[0],
453 addr.ether_addr_octet[1],
454 addr.ether_addr_octet[2],
455 addr.ether_addr_octet[3],
456 addr.ether_addr_octet[4],
457 addr.ether_addr_octet[5]);
459 if (has_pan(&uuids) == FALSE)
462 network = connman_device_get_network(device, ident);
466 network = connman_network_create(ident,
467 CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
471 connman_network_set_string(network, "Path", path);
473 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
475 connman_network_set_name(network, name);
477 connman_device_add_network(device, network);
479 connman_network_set_group(network, ident);
482 dbus_message_unref(reply);
485 static void add_network(struct connman_device *device, const char *path)
487 DBusMessage *message;
488 DBusPendingCall *call;
490 DBG("path %s", path);
492 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
493 BLUEZ_DEVICE_INTERFACE, GET_PROPERTIES);
497 dbus_message_set_auto_start(message, FALSE);
499 if (dbus_connection_send_with_reply(connection, message,
500 &call, TIMEOUT) == FALSE) {
501 connman_error("Failed to get network properties for %s", path);
506 connman_error("D-Bus connection not available");
510 dbus_pending_call_set_notify(call, network_properties_reply,
511 g_strdup(path), g_free);
514 dbus_message_unref(message);
517 static void check_networks(struct connman_device *device,
518 DBusMessageIter *array)
520 DBusMessageIter value;
522 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
525 dbus_message_iter_recurse(array, &value);
527 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
530 dbus_message_iter_get_basic(&value, &path);
532 add_network(device, path);
534 dbus_message_iter_next(&value);
538 static void adapter_changed(DBusConnection *connection, DBusMessage *message)
540 const char *path = dbus_message_get_path(message);
541 struct connman_device *device;
542 DBusMessageIter iter, value;
545 DBG("path %s", path);
547 device = g_hash_table_lookup(bluetooth_devices, path);
551 if (dbus_message_iter_init(message, &iter) == FALSE)
554 dbus_message_iter_get_basic(&iter, &key);
556 dbus_message_iter_next(&iter);
557 dbus_message_iter_recurse(&iter, &value);
559 if (g_str_equal(key, "Powered") == TRUE) {
562 dbus_message_iter_get_basic(&value, &val);
563 connman_device_set_powered(device, val);
564 } else if (g_str_equal(key, "Discovering") == TRUE) {
567 dbus_message_iter_get_basic(&value, &val);
568 connman_device_set_scanning(device, val);
569 } else if (g_str_equal(key, "Devices") == TRUE) {
570 check_networks(device, &value);
574 static void adapter_properties_reply(DBusPendingCall *call, void *user_data)
576 char *path = user_data;
577 struct connman_device *device;
579 DBusMessageIter networks;
580 const char *address = NULL, *name = NULL;
581 dbus_bool_t powered = FALSE, scanning = FALSE;
582 struct ether_addr addr;
585 DBG("path %s", path);
587 reply = dbus_pending_call_steal_reply(call);
594 extract_properties(reply, NULL, &address, &name, NULL,
595 &powered, &scanning, NULL, &networks);
600 device = g_hash_table_lookup(bluetooth_devices, path);
604 ether_aton_r(address, &addr);
606 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
607 addr.ether_addr_octet[0],
608 addr.ether_addr_octet[1],
609 addr.ether_addr_octet[2],
610 addr.ether_addr_octet[3],
611 addr.ether_addr_octet[4],
612 addr.ether_addr_octet[5]);
614 device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_BLUETOOTH);
618 connman_device_set_ident(device, ident);
620 connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE);
622 if (connman_device_register(device) < 0) {
623 connman_device_unref(device);
627 g_hash_table_insert(bluetooth_devices, g_strdup(path), device);
630 connman_device_set_string(device, "Address", address);
631 connman_device_set_string(device, "Name", name);
633 connman_device_set_powered(device, powered);
634 connman_device_set_scanning(device, scanning);
636 check_networks(device, &networks);
639 dbus_message_unref(reply);
642 static void add_adapter(DBusConnection *connection, const char *path)
644 DBusMessage *message;
645 DBusPendingCall *call;
647 DBG("path %s", path);
649 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
650 BLUEZ_ADAPTER_INTERFACE, GET_PROPERTIES);
654 dbus_message_set_auto_start(message, FALSE);
656 if (dbus_connection_send_with_reply(connection, message,
657 &call, TIMEOUT) == FALSE) {
658 connman_error("Failed to get adapter properties for %s", path);
663 connman_error("D-Bus connection not available");
667 dbus_pending_call_set_notify(call, adapter_properties_reply,
668 g_strdup(path), g_free);
671 dbus_message_unref(message);
674 static void remove_adapter(DBusConnection *connection, const char *path)
676 DBG("path %s", path);
678 g_hash_table_remove(bluetooth_devices, path);
681 static void list_adapters_reply(DBusPendingCall *call, void *user_data)
690 reply = dbus_pending_call_steal_reply(call);
694 dbus_error_init(&error);
696 if (dbus_message_get_args(reply, &error,
697 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
698 &adapters, &num_adapters,
699 DBUS_TYPE_INVALID) == FALSE) {
700 if (dbus_error_is_set(&error) == TRUE) {
701 connman_error("%s", error.message);
702 dbus_error_free(&error);
704 connman_error("Wrong arguments for adapter list");
708 for (i = 0; i < num_adapters; i++)
709 add_adapter(connection, adapters[i]);
711 g_strfreev(adapters);
714 dbus_message_unref(reply);
717 static void unregister_device(gpointer data)
719 struct connman_device *device = data;
723 connman_device_unregister(device);
724 connman_device_unref(device);
727 static void bluetooth_connect(DBusConnection *connection, void *user_data)
729 DBusMessage *message;
730 DBusPendingCall *call;
732 DBG("connection %p", connection);
734 bluetooth_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
735 g_free, unregister_device);
737 message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
738 BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS);
742 dbus_message_set_auto_start(message, FALSE);
744 if (dbus_connection_send_with_reply(connection, message,
745 &call, TIMEOUT) == FALSE) {
746 connman_error("Failed to get Bluetooth adapters");
751 connman_error("D-Bus connection not available");
755 dbus_pending_call_set_notify(call, list_adapters_reply, NULL, NULL);
758 dbus_message_unref(message);
761 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
763 DBG("connection %p", connection);
765 g_hash_table_destroy(bluetooth_devices);
766 bluetooth_devices = NULL;
769 static DBusHandlerResult bluetooth_signal(DBusConnection *connection,
770 DBusMessage *message, void *user_data)
772 if (dbus_message_is_signal(message, BLUEZ_ADAPTER_INTERFACE,
773 PROPERTY_CHANGED) == TRUE) {
774 adapter_changed(connection, message);
775 } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
776 ADAPTER_ADDED) == TRUE) {
778 dbus_message_get_args(message, NULL,
779 DBUS_TYPE_OBJECT_PATH, &path,
781 add_adapter(connection, path);
782 } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
783 ADAPTER_REMOVED) == TRUE) {
785 dbus_message_get_args(message, NULL,
786 DBUS_TYPE_OBJECT_PATH, &path,
788 remove_adapter(connection, path);
791 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
796 static const char *added_rule = "type=signal,member=" ADAPTER_ADDED
797 ",interface=" BLUEZ_MANAGER_INTERFACE;
798 static const char *removed_rule = "type=signal,member=" ADAPTER_REMOVED
799 ",interface=" BLUEZ_MANAGER_INTERFACE;
801 static const char *adapter_rule = "type=signal,member=" PROPERTY_CHANGED
802 ",interface=" BLUEZ_ADAPTER_INTERFACE;
804 static int bluetooth_init(void)
808 connection = connman_dbus_get_connection();
809 if (connection == NULL)
812 if (dbus_connection_add_filter(connection, bluetooth_signal,
813 NULL, NULL) == FALSE)
816 err = connman_network_driver_register(&pan_driver);
820 err = connman_device_driver_register(&bluetooth_driver);
822 connman_network_driver_unregister(&pan_driver);
826 watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
827 bluetooth_connect, bluetooth_disconnect, NULL, NULL);
829 connman_device_driver_unregister(&bluetooth_driver);
830 connman_network_driver_unregister(&pan_driver);
835 dbus_bus_add_match(connection, added_rule, NULL);
836 dbus_bus_add_match(connection, removed_rule, NULL);
837 dbus_bus_add_match(connection, adapter_rule, NULL);
838 dbus_connection_flush(connection);
843 dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
846 dbus_connection_unref(connection);
851 static void bluetooth_exit(void)
853 dbus_bus_remove_match(connection, adapter_rule, NULL);
854 dbus_bus_remove_match(connection, removed_rule, NULL);
855 dbus_bus_remove_match(connection, added_rule, NULL);
856 dbus_connection_flush(connection);
858 g_dbus_remove_watch(connection, watch);
860 bluetooth_disconnect(connection, NULL);
862 connman_device_driver_unregister(&bluetooth_driver);
863 connman_network_driver_unregister(&pan_driver);
865 dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
867 dbus_connection_unref(connection);
870 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
871 CONNMAN_PLUGIN_PRIORITY_DEFAULT, bluetooth_init, bluetooth_exit)