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
31 #define CONNMAN_API_SUBJECT_TO_CHANGE
32 #include <connman/plugin.h>
33 #include <connman/device.h>
34 #include <connman/inet.h>
35 #include <connman/dbus.h>
36 #include <connman/log.h>
38 #define BLUEZ_SERVICE "org.bluez"
39 #define BLUEZ_MANAGER_INTERFACE BLUEZ_SERVICE ".Manager"
40 #define BLUEZ_ADAPTER_INTERFACE BLUEZ_SERVICE ".Adapter"
41 #define BLUEZ_DEVICE_INTERFACE BLUEZ_SERVICE ".Device"
42 #define BLUEZ_NETWORK_INTERFACE BLUEZ_SERVICE ".Network"
44 #define LIST_ADAPTERS "ListAdapters"
45 #define ADAPTER_ADDED "AdapterAdded"
46 #define ADAPTER_REMOVED "AdapterRemoved"
48 #define PROPERTY_CHANGED "PropertyChanged"
49 #define GET_PROPERTIES "GetProperties"
50 #define SET_PROPERTY "SetProperty"
52 #define CONNECT "Connect"
53 #define DISCONNECT "Disconnect"
57 typedef void (* properties_callback_t) (DBusConnection *connection,
62 struct properties_data {
63 DBusConnection *connection;
65 properties_callback_t callback;
69 static void get_properties_reply(DBusPendingCall *call, void *user_data)
71 struct properties_data *data = user_data;
75 reply = dbus_pending_call_steal_reply(call);
79 path = dbus_message_get_path(data->message);
81 data->callback(data->connection, path, reply, data->user_data);
83 dbus_message_unref(reply);
86 dbus_message_unref(data->message);
90 static void get_properties(DBusConnection *connection,
91 const char *path, const char *interface,
92 properties_callback_t callback, void *user_data)
94 struct properties_data *data;
96 DBusPendingCall *call;
98 DBG("path %s interface %s", path, interface);
100 data = g_try_new0(struct properties_data, 1);
104 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
105 interface, GET_PROPERTIES);
106 if (message == NULL) {
111 dbus_message_set_auto_start(message, FALSE);
113 if (dbus_connection_send_with_reply(connection, message,
114 &call, TIMEOUT) == FALSE) {
115 connman_error("Failed to get properties for %s", interface);
116 dbus_message_unref(message);
122 connman_error("D-Bus connection not available");
123 dbus_message_unref(message);
128 data->connection = connection;
129 data->message = message;
130 data->callback = callback;
131 data->user_data = user_data;
133 dbus_pending_call_set_notify(call, get_properties_reply, data, NULL);
136 struct adapter_data {
137 DBusConnection *connection;
140 struct network_data {
141 DBusConnection *connection;
145 static int pan_probe(struct connman_network *network)
147 struct connman_device *device = connman_network_get_device(network);
148 struct adapter_data *adapter;
149 struct network_data *data;
151 DBG("network %p", network);
156 adapter = connman_device_get_data(device);
160 data = g_try_new0(struct network_data, 1);
164 data->connection = adapter->connection;
166 connman_network_set_data(network, data);
171 static void pan_remove(struct connman_network *network)
173 struct network_data *data = connman_network_get_data(network);
175 DBG("network %p", network);
177 connman_network_set_data(network, NULL);
182 static void connect_reply(DBusPendingCall *call, void *user_data)
184 struct connman_network *network = user_data;
185 struct network_data *data = connman_network_get_data(network);
188 const char *interface = NULL;
191 DBG("network %p", network);
193 reply = dbus_pending_call_steal_reply(call);
197 dbus_error_init(&error);
199 if (dbus_message_get_args(reply, &error,
200 DBUS_TYPE_STRING, &interface,
201 DBUS_TYPE_INVALID) == FALSE) {
202 if (dbus_error_is_set(&error) == TRUE) {
203 connman_error("%s", error.message);
204 dbus_error_free(&error);
206 connman_error("Wrong arguments for connect");
210 if (interface == NULL)
213 DBG("interface %s", interface);
215 data->interface = g_strdup(interface);
217 index = connman_inet_ifindex(interface);
219 connman_network_set_index(network, index);
220 connman_network_set_connected(network, TRUE);
223 dbus_message_unref(reply);
226 static int pan_connect(struct connman_network *network)
228 struct network_data *data = connman_network_get_data(network);
229 const char *path = connman_network_get_string(network, "Node");
230 const char *uuid = "nap";
231 DBusMessage *message;
232 DBusPendingCall *call;
234 DBG("network %p", network);
236 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
237 BLUEZ_NETWORK_INTERFACE, CONNECT);
241 dbus_message_set_auto_start(message, FALSE);
243 dbus_message_append_args(message, DBUS_TYPE_STRING, &uuid,
246 if (dbus_connection_send_with_reply(data->connection, message,
247 &call, TIMEOUT * 10) == FALSE) {
248 connman_error("Failed to connect service");
249 dbus_message_unref(message);
254 connman_error("D-Bus connection not available");
255 dbus_message_unref(message);
259 dbus_pending_call_set_notify(call, connect_reply, network, NULL);
261 dbus_message_unref(message);
266 static void disconnect_reply(DBusPendingCall *call, void *user_data)
268 struct connman_network *network = user_data;
269 struct network_data *data = connman_network_get_data(network);
273 DBG("network %p", network);
275 reply = dbus_pending_call_steal_reply(call);
279 dbus_error_init(&error);
281 if (dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) == FALSE) {
282 if (dbus_error_is_set(&error) == TRUE) {
283 connman_error("%s", error.message);
284 dbus_error_free(&error);
286 connman_error("Wrong arguments for disconnect");
290 g_free(data->interface);
291 data->interface = NULL;
293 connman_network_set_connected(network, FALSE);
294 connman_network_set_index(network, -1);
297 dbus_message_unref(reply);
300 static int pan_disconnect(struct connman_network *network)
302 struct network_data *data = connman_network_get_data(network);
303 const char *path = connman_network_get_string(network, "Node");
304 DBusMessage *message;
305 DBusPendingCall *call;
307 DBG("network %p", network);
309 if (data->interface == NULL)
312 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
313 BLUEZ_NETWORK_INTERFACE, DISCONNECT);
317 dbus_message_set_auto_start(message, FALSE);
319 dbus_message_append_args(message, DBUS_TYPE_INVALID);
321 if (dbus_connection_send_with_reply(data->connection, message,
322 &call, TIMEOUT) == FALSE) {
323 connman_error("Failed to disconnect service");
324 dbus_message_unref(message);
329 connman_error("D-Bus connection not available");
330 dbus_message_unref(message);
334 dbus_pending_call_set_notify(call, disconnect_reply, network, NULL);
336 dbus_message_unref(message);
341 static struct connman_network_driver pan_driver = {
342 .name = "bluetooth-pan",
343 .type = CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN,
345 .remove = pan_remove,
346 .connect = pan_connect,
347 .disconnect = pan_disconnect,
350 static int bluetooth_probe(struct connman_device *adapter)
352 struct adapter_data *data;
354 DBG("adapter %p", adapter);
356 data = g_try_new0(struct adapter_data, 1);
360 data->connection = connman_dbus_get_connection();
361 if (data->connection == NULL) {
366 connman_device_set_data(adapter, data);
371 static void bluetooth_remove(struct connman_device *adapter)
373 struct adapter_data *data = connman_device_get_data(adapter);
375 DBG("adapter %p", adapter);
377 connman_device_set_data(adapter, NULL);
379 dbus_connection_unref(data->connection);
384 static void powered_reply(DBusPendingCall *call, void *user_data)
390 reply = dbus_pending_call_steal_reply(call);
392 dbus_message_unref(reply);
395 static int change_powered(DBusConnection *connection, const char *path,
398 DBusMessage *message;
399 DBusMessageIter iter;
400 DBusPendingCall *call;
404 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
405 BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY);
409 dbus_message_set_auto_start(message, FALSE);
411 dbus_message_iter_init_append(message, &iter);
412 connman_dbus_property_append_variant(&iter, "Powered",
413 DBUS_TYPE_BOOLEAN, &powered);
415 if (dbus_connection_send_with_reply(connection, message,
416 &call, TIMEOUT) == FALSE) {
417 connman_error("Failed to change Powered property");
418 dbus_message_unref(message);
423 connman_error("D-Bus connection not available");
424 dbus_message_unref(message);
428 dbus_pending_call_set_notify(call, powered_reply, NULL, NULL);
430 dbus_message_unref(message);
435 static int bluetooth_enable(struct connman_device *adapter)
437 struct adapter_data *data = connman_device_get_data(adapter);
438 const char *path = connman_device_get_string(adapter, "Node");
440 DBG("adapter %p", adapter);
442 return change_powered(data->connection, path, TRUE);
445 static int bluetooth_disable(struct connman_device *adapter)
447 struct adapter_data *data = connman_device_get_data(adapter);
448 const char *path = connman_device_get_string(adapter, "Node");
450 DBG("adapter %p", adapter);
452 return change_powered(data->connection, path, FALSE);
455 static int bluetooth_scan(struct connman_device *adapter)
457 DBG("adapter %p", adapter);
462 static struct connman_device_driver bluetooth_driver = {
464 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
465 .probe = bluetooth_probe,
466 .remove = bluetooth_remove,
467 .enable = bluetooth_enable,
468 .disable = bluetooth_disable,
469 .scan = bluetooth_scan,
472 static GSList *adapter_list = NULL;
474 static void free_adapters(void)
480 for (list = adapter_list; list; list = list->next) {
481 struct connman_device *adapter = list->data;
483 connman_device_unregister(adapter);
484 connman_device_unref(adapter);
487 g_slist_free(adapter_list);
491 static struct connman_device *find_adapter(const char *path)
495 DBG("path %s", path);
497 for (list = adapter_list; list; list = list->next) {
498 struct connman_device *adapter = list->data;
499 const char *adapter_path = connman_device_get_string(adapter,
502 if (adapter_path == NULL)
505 if (g_str_equal(adapter_path, path) == TRUE)
512 static void device_properties(DBusConnection *connection, const char *path,
513 DBusMessage *message, void *user_data)
515 struct connman_device *device = user_data;
516 const char *node = g_basename(path);
517 struct connman_network *network;
519 DBG("path %s", path);
521 network = connman_device_get_network(device, node);
525 network = connman_network_create(node,
526 CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
530 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
532 connman_network_set_string(network, "Node", path);
534 connman_network_set_name(network, node);
536 connman_device_add_network(device, network);
538 connman_network_set_group(network, node);
541 static void check_devices(struct connman_device *adapter,
542 DBusConnection *connection, DBusMessageIter *array)
544 DBusMessageIter value;
546 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
549 dbus_message_iter_recurse(array, &value);
551 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
554 dbus_message_iter_get_basic(&value, &path);
556 get_properties(connection, path, BLUEZ_DEVICE_INTERFACE,
557 device_properties, adapter);
559 dbus_message_iter_next(&value);
563 static void property_changed(DBusConnection *connection, DBusMessage *message)
565 const char *path = dbus_message_get_path(message);
566 struct connman_device *adapter;
567 DBusMessageIter iter, value;
570 DBG("path %s", path);
572 adapter = find_adapter(path);
576 if (dbus_message_iter_init(message, &iter) == FALSE)
579 dbus_message_iter_get_basic(&iter, &key);
581 dbus_message_iter_next(&iter);
582 dbus_message_iter_recurse(&iter, &value);
584 if (g_str_equal(key, "Powered") == TRUE) {
587 dbus_message_iter_get_basic(&value, &val);
588 connman_device_set_powered(adapter, val);
589 } else if (g_str_equal(key, "Discovering") == TRUE) {
592 dbus_message_iter_get_basic(&value, &val);
593 connman_device_set_scanning(adapter, val);
597 static void parse_adapter_properties(struct connman_device *adapter,
598 DBusConnection *connection,
601 DBusMessageIter array, dict;
603 if (dbus_message_iter_init(reply, &array) == FALSE)
606 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
609 dbus_message_iter_recurse(&array, &dict);
611 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
612 DBusMessageIter entry, value;
615 dbus_message_iter_recurse(&dict, &entry);
616 dbus_message_iter_get_basic(&entry, &key);
618 dbus_message_iter_next(&entry);
619 dbus_message_iter_recurse(&entry, &value);
621 if (g_str_equal(key, "Powered") == TRUE) {
624 dbus_message_iter_get_basic(&value, &val);
625 connman_device_set_powered(adapter, val);
626 } else if (g_str_equal(key, "Discovering") == TRUE) {
629 dbus_message_iter_get_basic(&value, &val);
630 connman_device_set_scanning(adapter, val);
631 } else if (g_str_equal(key, "Devices") == TRUE) {
632 check_devices(adapter, connection, &value);
635 dbus_message_iter_next(&dict);
639 static void adapter_properties(DBusConnection *connection, const char *path,
640 DBusMessage *message, void *user_data)
642 const char *node = g_basename(path);
643 struct connman_device *adapter;
645 DBG("path %s", path);
647 adapter = find_adapter(path);
651 adapter = connman_device_create(node, CONNMAN_DEVICE_TYPE_BLUETOOTH);
655 connman_device_set_string(adapter, "Node", path);
657 if (node != NULL && g_str_has_prefix(node, "hci") == TRUE) {
660 index = atoi(node + 3);
662 connman_device_set_index(adapter, index);
665 connman_device_set_interface(adapter, node, NULL);
667 connman_device_set_ident(adapter, node);
669 connman_device_set_mode(adapter, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE);
671 if (connman_device_register(adapter) < 0) {
672 connman_device_unref(adapter);
676 adapter_list = g_slist_append(adapter_list, adapter);
679 parse_adapter_properties(adapter, connection, message);
682 static void add_adapter(DBusConnection *connection, const char *path)
684 DBG("path %s", path);
686 get_properties(connection, path, BLUEZ_ADAPTER_INTERFACE,
687 adapter_properties, NULL);
690 static void remove_adapter(DBusConnection *connection, const char *path)
692 struct connman_device *adapter;
694 DBG("path %s", path);
696 adapter = find_adapter(path);
700 adapter_list = g_slist_remove(adapter_list, adapter);
702 connman_device_unregister(adapter);
703 connman_device_unref(adapter);
706 static void list_adapters_reply(DBusPendingCall *call, void *user_data)
708 DBusConnection *connection = user_data;
716 reply = dbus_pending_call_steal_reply(call);
718 dbus_error_init(&error);
720 if (dbus_message_get_args(reply, &error,
721 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
722 &adapters, &num_adapters,
723 DBUS_TYPE_INVALID) == FALSE) {
724 if (dbus_error_is_set(&error) == TRUE) {
725 connman_error("%s", error.message);
726 dbus_error_free(&error);
728 connman_error("Wrong arguments for adapter list");
732 for (i = 0; i < num_adapters; i++)
733 get_properties(connection, adapters[i],
734 BLUEZ_ADAPTER_INTERFACE,
735 adapter_properties, NULL);
737 g_strfreev(adapters);
740 dbus_message_unref(reply);
743 static void bluetooth_connect(DBusConnection *connection, void *user_data)
745 DBusMessage *message;
746 DBusPendingCall *call;
748 DBG("connection %p", connection);
750 message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
751 BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS);
755 dbus_message_set_auto_start(message, FALSE);
757 if (dbus_connection_send_with_reply(connection, message,
758 &call, TIMEOUT) == FALSE) {
759 connman_error("Failed to get Bluetooth adapters");
764 connman_error("D-Bus connection not available");
768 dbus_pending_call_set_notify(call, list_adapters_reply,
772 dbus_message_unref(message);
775 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
777 DBG("connection %p", connection);
782 static DBusHandlerResult bluetooth_signal(DBusConnection *connection,
783 DBusMessage *message, void *user_data)
785 if (dbus_message_has_interface(message,
786 BLUEZ_MANAGER_INTERFACE) == FALSE &&
787 dbus_message_has_interface(message,
788 BLUEZ_ADAPTER_INTERFACE) == FALSE)
789 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
791 DBG("connection %p", connection);
793 if (dbus_message_is_signal(message, BLUEZ_ADAPTER_INTERFACE,
794 PROPERTY_CHANGED) == TRUE) {
795 property_changed(connection, message);
796 } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
797 ADAPTER_ADDED) == TRUE) {
799 dbus_message_get_args(message, NULL,
800 DBUS_TYPE_OBJECT_PATH, &path,
802 add_adapter(connection, path);
803 } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
804 ADAPTER_REMOVED) == TRUE) {
806 dbus_message_get_args(message, NULL,
807 DBUS_TYPE_OBJECT_PATH, &path,
809 remove_adapter(connection, path);
812 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
815 static DBusConnection *connection;
818 static const char *added_rule = "type=signal,member=" ADAPTER_ADDED
819 ",interface=" BLUEZ_MANAGER_INTERFACE;
820 static const char *removed_rule = "type=signal,member=" ADAPTER_REMOVED
821 ",interface=" BLUEZ_MANAGER_INTERFACE;
823 static const char *adapter_rule = "type=signal,member=" PROPERTY_CHANGED
824 ",interface=" BLUEZ_ADAPTER_INTERFACE;
826 static int bluetooth_init(void)
830 connection = connman_dbus_get_connection();
831 if (connection == NULL)
834 if (dbus_connection_add_filter(connection, bluetooth_signal,
835 NULL, NULL) == FALSE)
838 err = connman_network_driver_register(&pan_driver);
842 err = connman_device_driver_register(&bluetooth_driver);
844 connman_network_driver_unregister(&pan_driver);
848 watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
849 bluetooth_connect, bluetooth_disconnect, NULL, NULL);
851 connman_device_driver_unregister(&bluetooth_driver);
852 connman_network_driver_unregister(&pan_driver);
857 if (g_dbus_check_service(connection, BLUEZ_SERVICE) == TRUE)
858 bluetooth_connect(connection, NULL);
860 dbus_bus_add_match(connection, added_rule, NULL);
861 dbus_bus_add_match(connection, removed_rule, NULL);
862 dbus_bus_add_match(connection, adapter_rule, NULL);
863 dbus_connection_flush(connection);
868 dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
871 dbus_connection_unref(connection);
876 static void bluetooth_exit(void)
878 dbus_bus_remove_match(connection, adapter_rule, NULL);
879 dbus_bus_remove_match(connection, removed_rule, NULL);
880 dbus_bus_remove_match(connection, added_rule, NULL);
881 dbus_connection_flush(connection);
883 g_dbus_remove_watch(connection, watch);
887 connman_device_driver_unregister(&bluetooth_driver);
888 connman_network_driver_unregister(&pan_driver);
890 dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
892 dbus_connection_unref(connection);
895 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
896 CONNMAN_PLUGIN_PRIORITY_DEFAULT, bluetooth_init, bluetooth_exit)