5 * Copyright (C) 2013 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
29 #define CONNMAN_API_SUBJECT_TO_CHANGE
30 #include <connman/plugin.h>
31 #include <connman/dbus.h>
32 #include <connman/technology.h>
33 #include <connman/device.h>
34 #include <connman/inet.h>
37 #define BLUEZ_SERVICE "org.bluez"
38 #define BLUEZ_PATH "/org/bluez"
39 #define BLUETOOTH_PAN_PANU "00001115-0000-1000-8000-00805f9b34fb"
40 #define BLUETOOTH_PAN_NAP "00001116-0000-1000-8000-00805f9b34fb"
41 #define BLUETOOTH_PAN_GN "00001117-0000-1000-8000-00805f9b34fb"
43 #define BLUETOOTH_ADDR_LEN 6
45 static DBusConnection *connection;
46 static GDBusClient *client;
47 static GHashTable *devices;
48 static GHashTable *networks;
49 static bool bluetooth_tethering;
51 struct bluetooth_pan {
52 struct connman_network *network;
53 GDBusProxy *btdevice_proxy;
54 GDBusProxy *btnetwork_proxy;
58 static void address2ident(const char *address, char *ident)
62 for (i = 0; i < BLUETOOTH_ADDR_LEN; i++) {
63 ident[i * 2] = address[i * 3];
64 ident[i * 2 + 1] = address[i * 3 + 1];
66 ident[BLUETOOTH_ADDR_LEN * 2] = '\0';
69 static const char *proxy_get_string(GDBusProxy *proxy, const char *property)
74 if (!g_dbus_proxy_get_property(proxy, property, &iter))
76 dbus_message_iter_get_basic(&iter, &str);
80 static bool proxy_get_bool(GDBusProxy *proxy, const char *property)
85 if (!g_dbus_proxy_get_property(proxy, property, &iter))
87 dbus_message_iter_get_basic(&iter, &value);
91 static const char *proxy_get_role(GDBusProxy *proxy)
93 DBusMessageIter iter, value;
94 const char *pref = NULL;
99 if (!g_dbus_proxy_get_property(proxy, "UUIDs", &iter))
102 dbus_message_iter_recurse(&iter, &value);
103 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
106 dbus_message_iter_get_basic(&value, &uuid);
108 * If a device offers more than one role, we prefer NAP,
109 * then GN, then PANU.
111 if (!strcmp(uuid, BLUETOOTH_PAN_NAP))
113 if (!strcmp(uuid, BLUETOOTH_PAN_GN))
115 if (!strcmp(uuid, BLUETOOTH_PAN_PANU) && !pref)
118 dbus_message_iter_next(&value);
124 static int bluetooth_pan_probe(struct connman_network *network)
129 DBG("network %p", network);
131 g_hash_table_iter_init(&iter, networks);
133 while (g_hash_table_iter_next(&iter, &key, &value)) {
134 struct bluetooth_pan *pan = value;
136 if (network == pan->network)
143 static void pan_remove_nap(struct bluetooth_pan *pan)
145 struct connman_device *device;
146 struct connman_network *network = pan->network;
148 DBG("network %p pan %p", pan->network, pan);
154 connman_network_set_data(network, NULL);
156 device = connman_network_get_device(network);
158 connman_device_remove_network(device, network);
160 connman_network_unref(network);
163 static void bluetooth_pan_remove(struct connman_network *network)
165 struct bluetooth_pan *pan = connman_network_get_data(network);
167 DBG("network %p pan %p", network, pan);
169 connman_network_set_data(network, NULL);
175 static bool pan_connect(struct bluetooth_pan *pan,
181 if (!proxy_get_bool(pan->btnetwork_proxy, "Connected"))
183 iface = proxy_get_string(pan->btnetwork_proxy, "Interface");
189 index = connman_inet_ifindex(iface);
191 DBG("network %p invalid index %d", pan->network, index);
195 #if defined TIZEN_EXT
198 connman_network_set_index(pan->network, index);
199 connman_network_set_connected(pan->network, true);
200 #if defined TIZEN_EXT
207 static void pan_connect_cb(DBusMessage *message, void *user_data)
209 const char *path = user_data;
210 const char *iface = NULL;
211 struct bluetooth_pan *pan;
212 DBusMessageIter iter;
214 pan = g_hash_table_lookup(networks, path);
215 if (!pan || !pan->network) {
216 DBG("network already removed");
220 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
221 const char *dbus_error = dbus_message_get_error_name(message);
223 DBG("network %p %s", pan->network, dbus_error);
225 if (strcmp(dbus_error,
226 "org.bluez.Error.AlreadyConnected") != 0) {
227 connman_network_set_associating(pan->network, false);
228 connman_network_set_error(pan->network,
229 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
233 if (dbus_message_iter_init(message, &iter) &&
234 dbus_message_iter_get_arg_type(&iter) ==
236 dbus_message_iter_get_basic(&iter, &iface);
239 DBG("network %p interface %s", pan->network, iface);
241 pan_connect(pan, iface);
244 static void pan_connect_append(DBusMessageIter *iter,
247 const char *path = user_data;
248 struct bluetooth_pan *pan;
250 pan = g_hash_table_lookup(networks, path);
251 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &pan->pan_role);
254 static int bluetooth_pan_connect(struct connman_network *network)
256 struct bluetooth_pan *pan = connman_network_get_data(network);
259 DBG("network %p", network);
264 path = g_dbus_proxy_get_path(pan->btnetwork_proxy);
266 if (!g_dbus_proxy_method_call(pan->btnetwork_proxy, "Connect",
267 pan_connect_append, pan_connect_cb,
268 g_strdup(path), g_free))
271 #if defined TIZEN_EXT
274 connman_network_set_associating(pan->network, true);
279 static void pan_disconnect_cb(DBusMessage *message, void *user_data)
281 const char *path = user_data;
282 struct bluetooth_pan *pan;
284 pan = g_hash_table_lookup(networks, path);
285 if (!pan || !pan->network) {
286 DBG("network already removed");
290 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
291 const char *dbus_error = dbus_message_get_error_name(message);
293 DBG("network %p %s", pan->network, dbus_error);
296 DBG("network %p", pan->network);
298 #if defined TIZEN_EXT
301 connman_network_set_connected(pan->network, false);
304 static int bluetooth_pan_disconnect(struct connman_network *network)
306 struct bluetooth_pan *pan = connman_network_get_data(network);
309 DBG("network %p", network);
314 #if defined TIZEN_EXT
315 if (connman_network_get_associating(network) == TRUE)
316 connman_network_clear_associating(network);
319 path = g_dbus_proxy_get_path(pan->btnetwork_proxy);
321 if (!g_dbus_proxy_method_call(pan->btnetwork_proxy, "Disconnect",
322 NULL, pan_disconnect_cb, g_strdup(path), g_free))
328 static void btnetwork_property_change(GDBusProxy *proxy, const char *name,
329 DBusMessageIter *iter, void *user_data)
331 struct bluetooth_pan *pan;
332 dbus_bool_t connected;
333 bool proxy_connected, network_connected;
335 if (strcmp(name, "Connected") != 0)
338 pan = g_hash_table_lookup(networks, g_dbus_proxy_get_path(proxy));
339 if (!pan || !pan->network)
342 dbus_message_iter_get_basic(iter, &connected);
343 proxy_connected = connected;
345 network_connected = connman_network_get_connected(pan->network);
347 DBG("network %p network connected %d proxy connected %d",
348 pan->network, network_connected, proxy_connected);
350 if (network_connected != proxy_connected)
351 connman_network_set_connected(pan->network, proxy_connected);
354 static void pan_create_nap(struct bluetooth_pan *pan)
356 struct connman_device *device;
360 role = proxy_get_role(pan->btdevice_proxy);
366 adapter = proxy_get_string(pan->btdevice_proxy, "Adapter");
371 device = g_hash_table_lookup(devices, adapter);
373 if (!device || !connman_device_get_powered(device))
378 char ident[BLUETOOTH_ADDR_LEN * 2 + 1];
379 const char *name, *path;
381 address = proxy_get_string(pan->btdevice_proxy, "Address");
383 connman_warn("Bluetooth device address missing");
387 address2ident(address, ident);
389 pan->network = connman_network_create(ident,
390 CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
392 name = proxy_get_string(pan->btdevice_proxy, "Alias");
393 path = g_dbus_proxy_get_path(pan->btnetwork_proxy);
395 DBG("network %p %s %s", pan->network, path, name);
398 connman_warn("Bluetooth network %s creation failed",
403 connman_network_set_data(pan->network, pan);
404 connman_network_set_name(pan->network, name);
405 connman_network_set_group(pan->network, ident);
408 pan->pan_role = role;
409 connman_device_add_network(device, pan->network);
411 if (pan_connect(pan, NULL))
412 DBG("network %p already connected", pan->network);
415 static void btdevice_property_change(GDBusProxy *proxy, const char *name,
416 DBusMessageIter *iter, void *user_data)
418 struct bluetooth_pan *pan;
419 const char *old_role = NULL;
420 const char *new_role;
422 if (strcmp(name, "UUIDs"))
425 pan = g_hash_table_lookup(networks, g_dbus_proxy_get_path(proxy));
430 connman_network_get_device(pan->network))
431 old_role = pan->pan_role;
432 new_role = proxy_get_role(pan->btdevice_proxy);
434 DBG("network %p network role %s proxy role %s", pan->network, old_role,
437 if (old_role && new_role && !strcmp(old_role, new_role))
443 static void pan_free(gpointer data)
445 struct bluetooth_pan *pan = data;
447 if (pan->btnetwork_proxy) {
448 g_dbus_proxy_unref(pan->btnetwork_proxy);
449 pan->btnetwork_proxy = NULL;
452 if (pan->btdevice_proxy) {
453 g_dbus_proxy_unref(pan->btdevice_proxy);
454 pan->btdevice_proxy = NULL;
462 static void pan_create(GDBusProxy *network_proxy)
464 const char *path = g_dbus_proxy_get_path(network_proxy);
465 struct bluetooth_pan *pan;
467 pan = g_try_new0(struct bluetooth_pan, 1);
470 connman_error("Out of memory creating PAN NAP");
474 g_hash_table_replace(networks, g_strdup(path), pan);
476 pan->btnetwork_proxy = g_dbus_proxy_ref(network_proxy);
477 pan->btdevice_proxy = g_dbus_proxy_new(client, path,
478 "org.bluez.Device1");
480 if (!pan->btdevice_proxy) {
481 connman_error("Cannot create BT PAN watcher %s", path);
482 g_hash_table_remove(networks, path);
486 g_dbus_proxy_set_property_watch(pan->btnetwork_proxy,
487 btnetwork_property_change, NULL);
489 g_dbus_proxy_set_property_watch(pan->btdevice_proxy,
490 btdevice_property_change, NULL);
492 DBG("pan %p %s role %s", pan, path, proxy_get_role(pan->btdevice_proxy));
497 static struct connman_network_driver network_driver = {
499 .type = CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN,
500 .probe = bluetooth_pan_probe,
501 .remove = bluetooth_pan_remove,
502 .connect = bluetooth_pan_connect,
503 .disconnect = bluetooth_pan_disconnect,
506 static void enable_device(struct connman_device *device, const char *path)
511 DBG("device %p %s", device, path);
512 connman_device_set_powered(device, true);
514 g_hash_table_iter_init(&iter, networks);
515 while (g_hash_table_iter_next(&iter, &key, &value)) {
516 struct bluetooth_pan *pan = value;
518 if (g_strcmp0(proxy_get_string(pan->btdevice_proxy, "Adapter"),
521 DBG("enable network %p", pan->network);
527 static void device_enable_cb(const DBusError *error, void *user_data)
529 char *path = user_data;
530 struct connman_device *device;
532 device = g_hash_table_lookup(devices, path);
534 DBG("device already removed");
538 if (dbus_error_is_set(error)) {
539 connman_warn("Bluetooth device %s not enabled %s",
540 path, error->message);
544 #if !defined TIZEN_EXT
545 enable_device(device, path);
551 static int bluetooth_device_enable(struct connman_device *device)
553 GDBusProxy *proxy = connman_device_get_data(device);
554 dbus_bool_t device_powered = TRUE;
560 path = g_dbus_proxy_get_path(proxy);
562 if (proxy_get_bool(proxy, "Powered")) {
563 DBG("already enabled %p %s", device, path);
567 DBG("device %p %s", device, path);
569 g_dbus_proxy_set_property_basic(proxy, "Powered",
570 DBUS_TYPE_BOOLEAN, &device_powered,
571 device_enable_cb, g_strdup(path), NULL);
576 static void disable_device(struct connman_device *device, const char *path)
581 DBG("device %p %s", device, path);
582 connman_device_set_powered(device, false);
584 g_hash_table_iter_init(&iter, networks);
585 while (g_hash_table_iter_next(&iter, &key, &value)) {
586 struct bluetooth_pan *pan = value;
588 if (pan->network && connman_network_get_device(pan->network)
590 DBG("disable network %p", pan->network);
591 connman_device_remove_network(device, pan->network);
596 static void device_disable_cb(const DBusError *error, void *user_data)
598 char *path = user_data;
599 struct connman_device *device;
601 device = g_hash_table_lookup(devices, path);
603 DBG("device already removed");
607 if (dbus_error_is_set(error)) {
608 connman_warn("Bluetooth device %s not disabled: %s",
609 path, error->message);
613 #if !defined TIZEN_EXT
614 disable_device(device, path);
621 static int bluetooth_device_disable(struct connman_device *device)
623 GDBusProxy *proxy = connman_device_get_data(device);
624 dbus_bool_t device_powered = FALSE;
630 path = g_dbus_proxy_get_path(proxy);
632 if (!proxy_get_bool(proxy, "Powered")) {
633 DBG("already disabled %p %s", device, path);
637 DBG("device %p %s", device, path);
639 g_dbus_proxy_set_property_basic(proxy, "Powered",
640 DBUS_TYPE_BOOLEAN, &device_powered,
641 device_disable_cb, g_strdup(path), NULL);
646 static void adapter_property_change(GDBusProxy *proxy, const char *name,
647 DBusMessageIter *iter, void *user_data)
649 struct connman_device *device;
651 bool adapter_powered, device_powered;
653 if (strcmp(name, "Powered") != 0)
656 path = g_dbus_proxy_get_path(proxy);
657 device = g_hash_table_lookup(devices, path);
659 adapter_powered = proxy_get_bool(proxy, "Powered");
660 device_powered = connman_device_get_powered(device);
662 DBG("device %p %s device powered %d adapter powered %d", device, path,
663 device_powered, adapter_powered);
665 if (device_powered != adapter_powered) {
667 enable_device(device, path);
669 disable_device(device, path);
673 static void device_free(gpointer data)
675 struct connman_device *device = data;
676 GDBusProxy *proxy = connman_device_get_data(device);
678 connman_device_set_data(device, NULL);
680 g_dbus_proxy_unref(proxy);
682 connman_device_unregister(device);
683 connman_device_unref(device);
686 struct tethering_info {
687 struct connman_technology *technology;
692 static void tethering_free(void *user_data)
694 struct tethering_info *tethering = user_data;
696 g_free(tethering->bridge);
700 static void tethering_create_cb(DBusMessage *message, void *user_data)
702 struct tethering_info *tethering = user_data;
704 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
705 const char *dbus_error = dbus_message_get_error_name(message);
707 DBG("%s tethering failed: %s",
708 tethering->enable ? "enable" : "disable",
713 DBG("bridge %s %s", tethering->bridge, tethering->enable ?
714 "enabled": "disabled");
716 if (tethering->technology)
717 connman_technology_tethering_notify(tethering->technology,
721 static void tethering_append(DBusMessageIter *iter, void *user_data)
723 struct tethering_info *tethering = user_data;
724 const char *nap = "nap";
726 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &nap);
727 if (tethering->enable)
728 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
732 static bool tethering_create(const char *path,
733 struct connman_technology *technology, const char *bridge,
736 struct tethering_info *tethering = g_new0(struct tethering_info, 1);
741 DBG("path %s bridge %s", path, bridge);
748 proxy = g_dbus_proxy_new(client, path, "org.bluez.NetworkServer1");
754 tethering->technology = technology;
755 tethering->bridge = g_strdup(bridge);
756 tethering->enable = enabled;
758 if (tethering->enable)
761 method = "Unregister";
763 result = g_dbus_proxy_method_call(proxy, method, tethering_append,
764 tethering_create_cb, tethering, tethering_free);
766 g_dbus_proxy_unref(proxy);
771 static void device_create(GDBusProxy *proxy)
773 struct connman_device *device = NULL;
774 const char *path = g_dbus_proxy_get_path(proxy);
776 char ident[BLUETOOTH_ADDR_LEN * 2 + 1];
779 address = proxy_get_string(proxy, "Address");
783 address2ident(address, ident);
785 device = connman_device_create("bluetooth",
786 CONNMAN_DEVICE_TYPE_BLUETOOTH);
790 connman_device_set_data(device, g_dbus_proxy_ref(proxy));
791 connman_device_set_ident(device, ident);
793 g_hash_table_replace(devices, g_strdup(path), device);
795 DBG("device %p %s device powered %d adapter powered %d", device,
796 path, connman_device_get_powered(device),
797 proxy_get_bool(proxy, "Powered"));
799 if (connman_device_register(device) < 0) {
800 g_hash_table_remove(devices, device);
804 g_dbus_proxy_set_property_watch(proxy, adapter_property_change, NULL);
806 powered = proxy_get_bool(proxy, "Powered");
807 connman_device_set_powered(device, powered);
809 if (proxy_get_role(proxy) && !bluetooth_tethering)
810 tethering_create(path, NULL, NULL, false);
813 static void object_added(GDBusProxy *proxy, void *user_data)
815 const char *interface;
817 interface = g_dbus_proxy_get_interface(proxy);
819 connman_warn("Interface or proxy missing when adding "
824 if (strcmp(interface, "org.bluez.Adapter1") == 0) {
825 DBG("%s %s", interface, g_dbus_proxy_get_path(proxy));
826 device_create(proxy);
830 if (strcmp(interface, "org.bluez.Network1") == 0) {
831 DBG("%s %s", interface, g_dbus_proxy_get_path(proxy));
837 static void object_removed(GDBusProxy *proxy, void *user_data)
839 const char *interface, *path;
841 interface = g_dbus_proxy_get_interface(proxy);
843 connman_warn("Interface or proxy missing when removing "
848 if (strcmp(interface, "org.bluez.Adapter1") == 0) {
849 path = g_dbus_proxy_get_path(proxy);
850 DBG("%s %s", interface, path);
852 g_hash_table_remove(devices, path);
855 if (strcmp(interface, "org.bluez.Network1") == 0) {
856 path = g_dbus_proxy_get_path(proxy);
857 DBG("%s %s", interface, path);
859 g_hash_table_remove(networks, path);
864 static int bluetooth_device_probe(struct connman_device *device)
869 g_hash_table_iter_init(&iter, devices);
871 while (g_hash_table_iter_next(&iter, &key, &value)) {
872 struct connman_device *known = value;
881 static void bluetooth_device_remove(struct connman_device *device)
886 static struct connman_device_driver device_driver = {
888 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
889 .probe = bluetooth_device_probe,
890 .remove = bluetooth_device_remove,
891 .enable = bluetooth_device_enable,
892 .disable = bluetooth_device_disable,
895 static int bluetooth_tech_probe(struct connman_technology *technology)
900 static void bluetooth_tech_remove(struct connman_technology *technology)
905 static int bluetooth_tech_set_tethering(struct connman_technology *technology,
906 const char *identifier, const char *passphrase,
907 const char *bridge, bool enabled)
909 GHashTableIter hash_iter;
913 bluetooth_tethering = enabled;
915 g_hash_table_iter_init(&hash_iter, devices);
917 while (g_hash_table_iter_next(&hash_iter, &key, &value)) {
918 const char *path = key;
919 struct connman_device *device = value;
921 DBG("device %p", device);
923 if (tethering_create(path, technology, bridge, enabled)
928 DBG("%s %d device(s)", enabled ? "enabled" : "disabled", i);
936 static struct connman_technology_driver tech_driver = {
938 .type = CONNMAN_SERVICE_TYPE_BLUETOOTH,
939 .probe = bluetooth_tech_probe,
940 .remove = bluetooth_tech_remove,
941 .set_tethering = bluetooth_tech_set_tethering,
944 static int bluetooth_init(void)
946 connection = connman_dbus_get_connection();
950 if (connman_technology_driver_register(&tech_driver) < 0) {
951 connman_warn("Failed to initialize technology for Bluez 5");
955 devices = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
958 if (connman_device_driver_register(&device_driver) < 0) {
959 connman_warn("Failed to initialize device driver for "
961 connman_technology_driver_unregister(&tech_driver);
965 if (connman_network_driver_register(&network_driver) < 0) {
966 connman_technology_driver_unregister(&tech_driver);
967 connman_device_driver_unregister(&device_driver);
971 networks = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
974 client = g_dbus_client_new(connection, BLUEZ_SERVICE, BLUEZ_PATH);
976 connman_warn("Failed to initialize D-Bus client for "
981 g_dbus_client_set_proxy_handlers(client, object_added, object_removed,
988 g_hash_table_destroy(networks);
991 g_hash_table_destroy(devices);
994 g_dbus_client_unref(client);
997 dbus_connection_unref(connection);
1002 static void bluetooth_exit(void)
1005 * We unset the disabling of the Bluetooth device when shutting down
1006 * so that non-PAN BT connections are not affected.
1008 device_driver.disable = NULL;
1010 g_dbus_client_unref(client);
1012 connman_network_driver_unregister(&network_driver);
1013 g_hash_table_destroy(networks);
1015 connman_device_driver_unregister(&device_driver);
1016 g_hash_table_destroy(devices);
1018 connman_technology_driver_unregister(&tech_driver);
1019 dbus_connection_unref(connection);
1022 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
1023 CONNMAN_PLUGIN_PRIORITY_DEFAULT, bluetooth_init, bluetooth_exit)