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;
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);
90 dbus_error_init(&error);
92 if (dbus_message_get_args(reply, &error,
93 DBUS_TYPE_STRING, &interface,
94 DBUS_TYPE_INVALID) == FALSE) {
95 if (dbus_error_is_set(&error) == TRUE) {
96 connman_error("%s", error.message);
97 dbus_error_free(&error);
99 connman_error("Wrong arguments for connect");
103 if (interface == NULL)
106 DBG("interface %s", interface);
108 index = connman_inet_ifindex(interface);
110 connman_network_set_index(network, index);
112 connman_network_set_method(network, CONNMAN_IPCONFIG_METHOD_DHCP);
114 connman_network_set_connected(network, TRUE);
117 dbus_message_unref(reply);
119 dbus_pending_call_unref(call);
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 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);
174 dbus_error_init(&error);
176 if (dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) == FALSE) {
177 if (dbus_error_is_set(&error) == TRUE) {
178 connman_error("%s", error.message);
179 dbus_error_free(&error);
181 connman_error("Wrong arguments for disconnect");
185 connman_network_set_connected(network, FALSE);
188 dbus_message_unref(reply);
190 dbus_pending_call_unref(call);
192 connman_network_unref(network);
195 static int pan_disconnect(struct connman_network *network)
197 const char *path = connman_network_get_string(network, "Path");
198 DBusMessage *message;
199 DBusPendingCall *call;
201 DBG("network %p", network);
206 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
207 BLUEZ_NETWORK_INTERFACE, DISCONNECT);
211 dbus_message_set_auto_start(message, FALSE);
213 dbus_message_append_args(message, DBUS_TYPE_INVALID);
215 if (dbus_connection_send_with_reply(connection, message,
216 &call, TIMEOUT) == FALSE) {
217 connman_error("Failed to disconnect service");
218 dbus_message_unref(message);
223 connman_error("D-Bus connection not available");
224 dbus_message_unref(message);
228 connman_network_ref(network);
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 void extract_properties(DBusMessage *reply, const char **parent,
247 const char **address,
250 dbus_bool_t *powered,
251 dbus_bool_t *scanning,
252 DBusMessageIter *uuids,
253 DBusMessageIter *networks)
255 DBusMessageIter array, dict;
257 if (dbus_message_iter_init(reply, &array) == FALSE)
260 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
263 dbus_message_iter_recurse(&array, &dict);
265 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
266 DBusMessageIter entry, value;
269 dbus_message_iter_recurse(&dict, &entry);
270 dbus_message_iter_get_basic(&entry, &key);
272 dbus_message_iter_next(&entry);
273 dbus_message_iter_recurse(&entry, &value);
275 if (g_str_equal(key, "Adapter") == TRUE) {
277 dbus_message_iter_get_basic(&value, parent);
278 } else if (g_str_equal(key, "Address") == TRUE) {
280 dbus_message_iter_get_basic(&value, address);
281 } else if (g_str_equal(key, "Name") == TRUE) {
283 dbus_message_iter_get_basic(&value, name);
284 } else if (g_str_equal(key, "Alias") == TRUE) {
286 dbus_message_iter_get_basic(&value, alias);
287 } else if (g_str_equal(key, "Powered") == TRUE) {
289 dbus_message_iter_get_basic(&value, powered);
290 } else if (g_str_equal(key, "Discovering") == TRUE) {
291 if (scanning != NULL)
292 dbus_message_iter_get_basic(&value, scanning);
293 } else if (g_str_equal(key, "Devices") == TRUE) {
294 if (networks != NULL)
295 memcpy(networks, &value, sizeof(value));
296 } else if (g_str_equal(key, "UUIDs") == TRUE) {
298 memcpy(uuids, &value, sizeof(value));
301 dbus_message_iter_next(&dict);
305 static dbus_bool_t has_pan(DBusMessageIter *array)
307 DBusMessageIter value;
309 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
312 dbus_message_iter_recurse(array, &value);
314 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
317 dbus_message_iter_get_basic(&value, &uuid);
319 if (g_strcmp0(uuid, UUID_NAP) == 0)
322 dbus_message_iter_next(&value);
328 static void network_properties_reply(DBusPendingCall *call, void *user_data)
330 char *path = user_data;
331 struct connman_device *device;
332 struct connman_network *network;
334 DBusMessageIter uuids;
335 const char *parent = NULL, *address = NULL, *name = NULL;
336 struct ether_addr addr;
339 reply = dbus_pending_call_steal_reply(call);
341 extract_properties(reply, &parent, &address, NULL, &name,
342 NULL, NULL, &uuids, NULL);
347 device = g_hash_table_lookup(bluetooth_devices, parent);
354 ether_aton_r(address, &addr);
356 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
357 addr.ether_addr_octet[0],
358 addr.ether_addr_octet[1],
359 addr.ether_addr_octet[2],
360 addr.ether_addr_octet[3],
361 addr.ether_addr_octet[4],
362 addr.ether_addr_octet[5]);
364 if (has_pan(&uuids) == FALSE)
367 network = connman_device_get_network(device, ident);
371 network = connman_network_create(ident,
372 CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
376 connman_network_set_string(network, "Path", path);
378 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
380 connman_network_set_name(network, name);
382 connman_device_add_network(device, network);
384 connman_network_set_group(network, ident);
387 dbus_message_unref(reply);
389 dbus_pending_call_unref(call);
392 static void add_network(struct connman_device *device, const char *path)
394 DBusMessage *message;
395 DBusPendingCall *call;
397 DBG("path %s", path);
399 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
400 BLUEZ_DEVICE_INTERFACE, GET_PROPERTIES);
404 dbus_message_set_auto_start(message, FALSE);
406 if (dbus_connection_send_with_reply(connection, message,
407 &call, TIMEOUT) == FALSE) {
408 connman_error("Failed to get network properties for %s", path);
413 connman_error("D-Bus connection not available");
417 dbus_pending_call_set_notify(call, network_properties_reply,
418 g_strdup(path), g_free);
421 dbus_message_unref(message);
424 static void check_networks(struct connman_device *device,
425 DBusMessageIter *array)
427 DBusMessageIter value;
429 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
432 dbus_message_iter_recurse(array, &value);
434 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) {
437 dbus_message_iter_get_basic(&value, &path);
439 add_network(device, path);
441 dbus_message_iter_next(&value);
445 static gboolean adapter_changed(DBusConnection *connection,
446 DBusMessage *message, void *user_data)
448 const char *path = dbus_message_get_path(message);
449 struct connman_device *device;
450 DBusMessageIter iter, value;
453 DBG("path %s", path);
455 device = g_hash_table_lookup(bluetooth_devices, path);
459 if (dbus_message_iter_init(message, &iter) == FALSE)
462 dbus_message_iter_get_basic(&iter, &key);
464 dbus_message_iter_next(&iter);
465 dbus_message_iter_recurse(&iter, &value);
467 if (g_str_equal(key, "Powered") == TRUE) {
470 dbus_message_iter_get_basic(&value, &val);
471 connman_device_set_powered(device, val);
472 } else if (g_str_equal(key, "Discovering") == TRUE) {
475 dbus_message_iter_get_basic(&value, &val);
476 connman_device_set_scanning(device, val);
477 } else if (g_str_equal(key, "Devices") == TRUE) {
478 check_networks(device, &value);
484 static void adapter_properties_reply(DBusPendingCall *call, void *user_data)
486 char *path = user_data;
487 struct connman_device *device;
489 DBusMessageIter networks;
490 const char *address = NULL, *name = NULL;
491 dbus_bool_t powered = FALSE, scanning = FALSE;
492 struct ether_addr addr;
495 DBG("path %s", path);
497 reply = dbus_pending_call_steal_reply(call);
502 extract_properties(reply, NULL, &address, &name, NULL,
503 &powered, &scanning, NULL, &networks);
508 device = g_hash_table_lookup(bluetooth_devices, path);
512 ether_aton_r(address, &addr);
514 snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
515 addr.ether_addr_octet[0],
516 addr.ether_addr_octet[1],
517 addr.ether_addr_octet[2],
518 addr.ether_addr_octet[3],
519 addr.ether_addr_octet[4],
520 addr.ether_addr_octet[5]);
522 device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_BLUETOOTH);
526 connman_device_set_ident(device, ident);
528 connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE);
530 connman_device_set_string(device, "Path", path);
532 if (connman_device_register(device) < 0) {
533 connman_device_unref(device);
537 g_hash_table_insert(bluetooth_devices, g_strdup(path), device);
540 connman_device_set_string(device, "Address", address);
541 connman_device_set_string(device, "Name", name);
542 connman_device_set_string(device, "Path", path);
544 connman_device_set_powered(device, powered);
545 connman_device_set_scanning(device, scanning);
548 check_networks(device, &networks);
551 dbus_message_unref(reply);
553 dbus_pending_call_unref(call);
556 static void add_adapter(DBusConnection *connection, const char *path)
558 DBusMessage *message;
559 DBusPendingCall *call;
561 DBG("path %s", path);
563 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
564 BLUEZ_ADAPTER_INTERFACE, GET_PROPERTIES);
568 dbus_message_set_auto_start(message, FALSE);
570 if (dbus_connection_send_with_reply(connection, message,
571 &call, TIMEOUT) == FALSE) {
572 connman_error("Failed to get adapter properties for %s", path);
577 connman_error("D-Bus connection not available");
581 dbus_pending_call_set_notify(call, adapter_properties_reply,
582 g_strdup(path), g_free);
585 dbus_message_unref(message);
588 static gboolean adapter_added(DBusConnection *connection, DBusMessage *message,
593 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
595 add_adapter(connection, path);
599 static void remove_adapter(DBusConnection *connection, const char *path)
601 DBG("path %s", path);
603 g_hash_table_remove(bluetooth_devices, path);
606 static gboolean adapter_removed(DBusConnection *connection, DBusMessage *message,
611 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
613 remove_adapter(connection, path);
617 static void list_adapters_reply(DBusPendingCall *call, void *user_data)
626 reply = dbus_pending_call_steal_reply(call);
628 dbus_error_init(&error);
630 if (dbus_message_get_args(reply, &error,
631 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
632 &adapters, &num_adapters,
633 DBUS_TYPE_INVALID) == FALSE) {
634 if (dbus_error_is_set(&error) == TRUE) {
635 connman_error("%s", error.message);
636 dbus_error_free(&error);
638 connman_error("Wrong arguments for adapter list");
642 for (i = 0; i < num_adapters; i++)
643 add_adapter(connection, adapters[i]);
645 g_strfreev(adapters);
648 dbus_message_unref(reply);
650 dbus_pending_call_unref(call);
653 static void unregister_device(gpointer data)
655 struct connman_device *device = data;
659 connman_device_unregister(device);
660 connman_device_unref(device);
663 static void bluetooth_connect(DBusConnection *connection, void *user_data)
665 DBusMessage *message;
666 DBusPendingCall *call;
668 DBG("connection %p", connection);
670 bluetooth_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
671 g_free, unregister_device);
673 message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
674 BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS);
678 dbus_message_set_auto_start(message, FALSE);
680 if (dbus_connection_send_with_reply(connection, message,
681 &call, TIMEOUT) == FALSE) {
682 connman_error("Failed to get Bluetooth adapters");
687 connman_error("D-Bus connection not available");
691 dbus_pending_call_set_notify(call, list_adapters_reply, NULL, NULL);
694 dbus_message_unref(message);
697 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
699 DBG("connection %p", connection);
701 if (bluetooth_devices == NULL)
704 g_hash_table_destroy(bluetooth_devices);
705 bluetooth_devices = NULL;
708 static int bluetooth_probe(struct connman_device *device)
710 DBG("device %p", device);
715 static void bluetooth_remove(struct connman_device *device)
717 DBG("device %p", device);
720 static void powered_reply(DBusPendingCall *call, void *user_data)
726 reply = dbus_pending_call_steal_reply(call);
728 dbus_message_unref(reply);
730 dbus_pending_call_unref(call);
732 add_adapter(connection, user_data);
735 static int change_powered(DBusConnection *connection, const char *path,
738 DBusMessage *message;
739 DBusMessageIter iter;
740 DBusPendingCall *call;
747 message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
748 BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY);
752 dbus_message_set_auto_start(message, FALSE);
754 dbus_message_iter_init_append(message, &iter);
755 connman_dbus_property_append_basic(&iter, "Powered",
756 DBUS_TYPE_BOOLEAN, &powered);
758 if (dbus_connection_send_with_reply(connection, message,
759 &call, TIMEOUT) == FALSE) {
760 connman_error("Failed to change Powered property");
761 dbus_message_unref(message);
766 connman_error("D-Bus connection not available");
767 dbus_message_unref(message);
771 dbus_pending_call_set_notify(call, powered_reply,
772 g_strdup(path), g_free);
774 dbus_message_unref(message);
779 static int bluetooth_enable(struct connman_device *device)
781 const char *path = connman_device_get_string(device, "Path");
783 DBG("device %p", device);
785 return change_powered(connection, path, TRUE);
788 static int bluetooth_disable(struct connman_device *device)
790 const char *path = connman_device_get_string(device, "Path");
792 DBG("device %p", device);
794 return change_powered(connection, path, FALSE);
797 static struct connman_device_driver bluetooth_driver = {
799 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
800 .probe = bluetooth_probe,
801 .remove = bluetooth_remove,
802 .enable = bluetooth_enable,
803 .disable = bluetooth_disable,
807 static guint added_watch;
808 static guint removed_watch;
809 static guint adapter_watch;
811 static int bluetooth_init(void)
815 connection = connman_dbus_get_connection();
816 if (connection == NULL)
819 watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
820 bluetooth_connect, bluetooth_disconnect, NULL, NULL);
822 added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
823 BLUEZ_MANAGER_INTERFACE,
824 ADAPTER_ADDED, adapter_added,
827 removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
828 BLUEZ_MANAGER_INTERFACE,
829 ADAPTER_REMOVED, adapter_removed,
832 adapter_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
833 BLUEZ_MANAGER_INTERFACE,
834 PROPERTY_CHANGED, adapter_changed,
837 if (watch == 0 || added_watch == 0 || removed_watch == 0
838 || adapter_watch == 0) {
843 err = connman_network_driver_register(&pan_driver);
847 err = connman_device_driver_register(&bluetooth_driver);
849 connman_network_driver_unregister(&pan_driver);
856 g_dbus_remove_watch(connection, watch);
857 g_dbus_remove_watch(connection, added_watch);
858 g_dbus_remove_watch(connection, removed_watch);
859 g_dbus_remove_watch(connection, adapter_watch);
861 dbus_connection_unref(connection);
866 static void bluetooth_exit(void)
868 g_dbus_remove_watch(connection, watch);
869 g_dbus_remove_watch(connection, added_watch);
870 g_dbus_remove_watch(connection, removed_watch);
871 g_dbus_remove_watch(connection, adapter_watch);
873 bluetooth_disconnect(connection, NULL);
875 connman_device_driver_unregister(&bluetooth_driver);
876 connman_network_driver_unregister(&pan_driver);
878 dbus_connection_unref(connection);
881 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
882 CONNMAN_PLUGIN_PRIORITY_DEFAULT, bluetooth_init, bluetooth_exit)