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_basic(&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);
632 connman_device_set_string(device, "Path", path);
634 connman_device_set_powered(device, powered);
635 connman_device_set_scanning(device, scanning);
638 check_networks(device, &networks);
641 dbus_message_unref(reply);
644 static void add_adapter(DBusConnection *connection, const char *path)
646 DBusMessage *message;
647 DBusPendingCall *call;
649 DBG("path %s", path);
651 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
652 BLUEZ_ADAPTER_INTERFACE, GET_PROPERTIES);
656 dbus_message_set_auto_start(message, FALSE);
658 if (dbus_connection_send_with_reply(connection, message,
659 &call, TIMEOUT) == FALSE) {
660 connman_error("Failed to get adapter properties for %s", path);
665 connman_error("D-Bus connection not available");
669 dbus_pending_call_set_notify(call, adapter_properties_reply,
670 g_strdup(path), g_free);
673 dbus_message_unref(message);
676 static void remove_adapter(DBusConnection *connection, const char *path)
678 DBG("path %s", path);
680 g_hash_table_remove(bluetooth_devices, path);
683 static void list_adapters_reply(DBusPendingCall *call, void *user_data)
692 reply = dbus_pending_call_steal_reply(call);
696 dbus_error_init(&error);
698 if (dbus_message_get_args(reply, &error,
699 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
700 &adapters, &num_adapters,
701 DBUS_TYPE_INVALID) == FALSE) {
702 if (dbus_error_is_set(&error) == TRUE) {
703 connman_error("%s", error.message);
704 dbus_error_free(&error);
706 connman_error("Wrong arguments for adapter list");
710 for (i = 0; i < num_adapters; i++)
711 add_adapter(connection, adapters[i]);
713 g_strfreev(adapters);
716 dbus_message_unref(reply);
719 static void unregister_device(gpointer data)
721 struct connman_device *device = data;
725 connman_device_unregister(device);
726 connman_device_unref(device);
729 static void bluetooth_connect(DBusConnection *connection, void *user_data)
731 DBusMessage *message;
732 DBusPendingCall *call;
734 DBG("connection %p", connection);
736 bluetooth_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
737 g_free, unregister_device);
739 message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
740 BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS);
744 dbus_message_set_auto_start(message, FALSE);
746 if (dbus_connection_send_with_reply(connection, message,
747 &call, TIMEOUT) == FALSE) {
748 connman_error("Failed to get Bluetooth adapters");
753 connman_error("D-Bus connection not available");
757 dbus_pending_call_set_notify(call, list_adapters_reply, NULL, NULL);
760 dbus_message_unref(message);
763 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
765 DBG("connection %p", connection);
767 if (bluetooth_devices == NULL)
770 g_hash_table_destroy(bluetooth_devices);
771 bluetooth_devices = NULL;
774 static DBusHandlerResult bluetooth_signal(DBusConnection *connection,
775 DBusMessage *message, void *user_data)
777 if (dbus_message_is_signal(message, BLUEZ_ADAPTER_INTERFACE,
778 PROPERTY_CHANGED) == TRUE) {
779 adapter_changed(connection, message);
780 } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
781 ADAPTER_ADDED) == TRUE) {
783 dbus_message_get_args(message, NULL,
784 DBUS_TYPE_OBJECT_PATH, &path,
786 add_adapter(connection, path);
787 } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
788 ADAPTER_REMOVED) == TRUE) {
790 dbus_message_get_args(message, NULL,
791 DBUS_TYPE_OBJECT_PATH, &path,
793 remove_adapter(connection, path);
796 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
801 static const char *added_rule = "type=signal,member=" ADAPTER_ADDED
802 ",interface=" BLUEZ_MANAGER_INTERFACE;
803 static const char *removed_rule = "type=signal,member=" ADAPTER_REMOVED
804 ",interface=" BLUEZ_MANAGER_INTERFACE;
806 static const char *adapter_rule = "type=signal,member=" PROPERTY_CHANGED
807 ",interface=" BLUEZ_ADAPTER_INTERFACE;
809 static int bluetooth_init(void)
813 connection = connman_dbus_get_connection();
814 if (connection == NULL)
817 if (dbus_connection_add_filter(connection, bluetooth_signal,
818 NULL, NULL) == FALSE) {
823 err = connman_network_driver_register(&pan_driver);
827 err = connman_device_driver_register(&bluetooth_driver);
829 connman_network_driver_unregister(&pan_driver);
833 watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
834 bluetooth_connect, bluetooth_disconnect, NULL, NULL);
836 connman_device_driver_unregister(&bluetooth_driver);
837 connman_network_driver_unregister(&pan_driver);
842 dbus_bus_add_match(connection, added_rule, NULL);
843 dbus_bus_add_match(connection, removed_rule, NULL);
844 dbus_bus_add_match(connection, adapter_rule, NULL);
845 dbus_connection_flush(connection);
850 dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
853 dbus_connection_unref(connection);
858 static void bluetooth_exit(void)
860 dbus_bus_remove_match(connection, adapter_rule, NULL);
861 dbus_bus_remove_match(connection, removed_rule, NULL);
862 dbus_bus_remove_match(connection, added_rule, NULL);
863 dbus_connection_flush(connection);
865 g_dbus_remove_watch(connection, watch);
867 bluetooth_disconnect(connection, NULL);
869 connman_device_driver_unregister(&bluetooth_driver);
870 connman_network_driver_unregister(&pan_driver);
872 dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
874 dbus_connection_unref(connection);
877 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
878 CONNMAN_PLUGIN_PRIORITY_DEFAULT, bluetooth_init, bluetooth_exit)