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");
222 * Network could be removed because of BT adapter power off
223 * This is to handle the scenario where network is removed
224 * before the connect_cb is called
227 DBG("network already removed");
232 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
233 const char *dbus_error = dbus_message_get_error_name(message);
235 DBG("network %p %s", pan->network, dbus_error);
237 if (strcmp(dbus_error,
238 "org.bluez.Error.AlreadyConnected") != 0) {
239 connman_network_set_associating(pan->network, false);
240 connman_network_set_error(pan->network,
241 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
245 if (dbus_message_iter_init(message, &iter) &&
246 dbus_message_iter_get_arg_type(&iter) ==
248 dbus_message_iter_get_basic(&iter, &iface);
251 DBG("network %p interface %s", pan->network, iface);
253 pan_connect(pan, iface);
256 static void pan_connect_append(DBusMessageIter *iter,
259 const char *path = user_data;
260 struct bluetooth_pan *pan;
262 pan = g_hash_table_lookup(networks, path);
263 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &pan->pan_role);
266 static int bluetooth_pan_connect(struct connman_network *network)
268 struct bluetooth_pan *pan = connman_network_get_data(network);
271 DBG("network %p", network);
276 path = g_dbus_proxy_get_path(pan->btnetwork_proxy);
278 if (!g_dbus_proxy_method_call(pan->btnetwork_proxy, "Connect",
279 pan_connect_append, pan_connect_cb,
280 g_strdup(path), g_free))
283 #if defined TIZEN_EXT
286 connman_network_set_associating(pan->network, true);
291 static void pan_disconnect_cb(DBusMessage *message, void *user_data)
293 const char *path = user_data;
294 struct bluetooth_pan *pan;
296 pan = g_hash_table_lookup(networks, path);
297 if (!pan || !pan->network) {
298 DBG("network already removed");
302 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
303 const char *dbus_error = dbus_message_get_error_name(message);
305 DBG("network %p %s", pan->network, dbus_error);
308 DBG("network %p", pan->network);
310 #if defined TIZEN_EXT
313 connman_network_set_connected(pan->network, false);
316 static int bluetooth_pan_disconnect(struct connman_network *network)
318 struct bluetooth_pan *pan = connman_network_get_data(network);
321 DBG("network %p", network);
326 #if defined TIZEN_EXT
327 if (connman_network_get_associating(network) == TRUE)
328 connman_network_clear_associating(network);
331 path = g_dbus_proxy_get_path(pan->btnetwork_proxy);
333 if (!g_dbus_proxy_method_call(pan->btnetwork_proxy, "Disconnect",
334 NULL, pan_disconnect_cb, g_strdup(path), g_free))
340 static void btnetwork_property_change(GDBusProxy *proxy, const char *name,
341 DBusMessageIter *iter, void *user_data)
343 struct bluetooth_pan *pan;
344 dbus_bool_t connected;
345 bool proxy_connected, network_connected;
347 if (strcmp(name, "Connected") != 0)
350 pan = g_hash_table_lookup(networks, g_dbus_proxy_get_path(proxy));
351 if (!pan || !pan->network)
354 dbus_message_iter_get_basic(iter, &connected);
355 proxy_connected = connected;
357 network_connected = connman_network_get_connected(pan->network);
359 DBG("network %p network connected %d proxy connected %d",
360 pan->network, network_connected, proxy_connected);
362 if (network_connected != proxy_connected)
363 connman_network_set_connected(pan->network, proxy_connected);
366 static void pan_create_nap(struct bluetooth_pan *pan)
368 struct connman_device *device;
372 role = proxy_get_role(pan->btdevice_proxy);
378 adapter = proxy_get_string(pan->btdevice_proxy, "Adapter");
383 device = g_hash_table_lookup(devices, adapter);
385 if (!device || !connman_device_get_powered(device))
390 char ident[BLUETOOTH_ADDR_LEN * 2 + 1];
391 const char *name, *path;
393 address = proxy_get_string(pan->btdevice_proxy, "Address");
395 connman_warn("Bluetooth device address missing");
399 address2ident(address, ident);
401 pan->network = connman_network_create(ident,
402 CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
404 name = proxy_get_string(pan->btdevice_proxy, "Alias");
405 path = g_dbus_proxy_get_path(pan->btnetwork_proxy);
407 DBG("network %p %s %s", pan->network, path, name);
410 connman_warn("Bluetooth network %s creation failed",
415 connman_network_set_data(pan->network, pan);
416 connman_network_set_name(pan->network, name);
417 connman_network_set_group(pan->network, ident);
420 pan->pan_role = role;
421 connman_device_add_network(device, pan->network);
423 if (pan_connect(pan, NULL))
424 DBG("network %p already connected", pan->network);
427 static void btdevice_property_change(GDBusProxy *proxy, const char *name,
428 DBusMessageIter *iter, void *user_data)
430 struct bluetooth_pan *pan;
431 const char *old_role = NULL;
432 const char *new_role;
434 if (strcmp(name, "UUIDs"))
437 pan = g_hash_table_lookup(networks, g_dbus_proxy_get_path(proxy));
442 connman_network_get_device(pan->network))
443 old_role = pan->pan_role;
444 new_role = proxy_get_role(pan->btdevice_proxy);
446 DBG("network %p network role %s proxy role %s", pan->network, old_role,
449 if (old_role && new_role && !strcmp(old_role, new_role))
455 static void pan_free(gpointer data)
457 struct bluetooth_pan *pan = data;
459 if (pan->btnetwork_proxy) {
460 g_dbus_proxy_unref(pan->btnetwork_proxy);
461 pan->btnetwork_proxy = NULL;
464 if (pan->btdevice_proxy) {
465 g_dbus_proxy_unref(pan->btdevice_proxy);
466 pan->btdevice_proxy = NULL;
474 static void pan_create(GDBusProxy *network_proxy)
476 const char *path = g_dbus_proxy_get_path(network_proxy);
477 struct bluetooth_pan *pan;
479 pan = g_try_new0(struct bluetooth_pan, 1);
482 connman_error("Out of memory creating PAN NAP");
486 g_hash_table_replace(networks, g_strdup(path), pan);
488 pan->btnetwork_proxy = g_dbus_proxy_ref(network_proxy);
489 pan->btdevice_proxy = g_dbus_proxy_new(client, path,
490 "org.bluez.Device1");
492 if (!pan->btdevice_proxy) {
493 connman_error("Cannot create BT PAN watcher %s", path);
494 g_hash_table_remove(networks, path);
498 g_dbus_proxy_set_property_watch(pan->btnetwork_proxy,
499 btnetwork_property_change, NULL);
501 g_dbus_proxy_set_property_watch(pan->btdevice_proxy,
502 btdevice_property_change, NULL);
504 DBG("pan %p %s role %s", pan, path, proxy_get_role(pan->btdevice_proxy));
509 static struct connman_network_driver network_driver = {
511 .type = CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN,
512 .probe = bluetooth_pan_probe,
513 .remove = bluetooth_pan_remove,
514 .connect = bluetooth_pan_connect,
515 .disconnect = bluetooth_pan_disconnect,
518 static void enable_device(struct connman_device *device, const char *path)
523 DBG("device %p %s", device, path);
524 connman_device_set_powered(device, true);
526 g_hash_table_iter_init(&iter, networks);
527 while (g_hash_table_iter_next(&iter, &key, &value)) {
528 struct bluetooth_pan *pan = value;
530 if (g_strcmp0(proxy_get_string(pan->btdevice_proxy, "Adapter"),
533 DBG("enable network %p", pan->network);
539 static void device_enable_cb(const DBusError *error, void *user_data)
541 char *path = user_data;
542 struct connman_device *device;
544 device = g_hash_table_lookup(devices, path);
546 DBG("device already removed");
550 if (dbus_error_is_set(error)) {
551 connman_warn("Bluetooth device %s not enabled %s",
552 path, error->message);
556 #if !defined TIZEN_EXT
557 enable_device(device, path);
563 static int bluetooth_device_enable(struct connman_device *device)
565 GDBusProxy *proxy = connman_device_get_data(device);
566 dbus_bool_t device_powered = TRUE;
572 path = g_dbus_proxy_get_path(proxy);
574 if (proxy_get_bool(proxy, "Powered")) {
575 DBG("already enabled %p %s", device, path);
579 DBG("device %p %s", device, path);
581 g_dbus_proxy_set_property_basic(proxy, "Powered",
582 DBUS_TYPE_BOOLEAN, &device_powered,
583 device_enable_cb, g_strdup(path), NULL);
588 static void disable_device(struct connman_device *device, const char *path)
593 DBG("device %p %s", device, path);
594 connman_device_set_powered(device, false);
596 g_hash_table_iter_init(&iter, networks);
597 while (g_hash_table_iter_next(&iter, &key, &value)) {
598 struct bluetooth_pan *pan = value;
600 if (pan->network && connman_network_get_device(pan->network)
602 DBG("disable network %p", pan->network);
603 connman_device_remove_network(device, pan->network);
608 static void device_disable_cb(const DBusError *error, void *user_data)
610 char *path = user_data;
611 struct connman_device *device;
613 device = g_hash_table_lookup(devices, path);
615 DBG("device already removed");
619 if (dbus_error_is_set(error)) {
620 connman_warn("Bluetooth device %s not disabled: %s",
621 path, error->message);
625 #if !defined TIZEN_EXT
626 disable_device(device, path);
633 static int bluetooth_device_disable(struct connman_device *device)
635 GDBusProxy *proxy = connman_device_get_data(device);
636 dbus_bool_t device_powered = FALSE;
642 path = g_dbus_proxy_get_path(proxy);
644 if (!proxy_get_bool(proxy, "Powered")) {
645 DBG("already disabled %p %s", device, path);
649 DBG("device %p %s", device, path);
651 g_dbus_proxy_set_property_basic(proxy, "Powered",
652 DBUS_TYPE_BOOLEAN, &device_powered,
653 device_disable_cb, g_strdup(path), NULL);
658 static void adapter_property_change(GDBusProxy *proxy, const char *name,
659 DBusMessageIter *iter, void *user_data)
661 struct connman_device *device;
663 bool adapter_powered, device_powered;
665 if (strcmp(name, "Powered") != 0)
668 path = g_dbus_proxy_get_path(proxy);
669 device = g_hash_table_lookup(devices, path);
671 adapter_powered = proxy_get_bool(proxy, "Powered");
672 device_powered = connman_device_get_powered(device);
674 DBG("device %p %s device powered %d adapter powered %d", device, path,
675 device_powered, adapter_powered);
677 if (device_powered != adapter_powered) {
679 enable_device(device, path);
681 disable_device(device, path);
685 static void device_free(gpointer data)
687 struct connman_device *device = data;
688 GDBusProxy *proxy = connman_device_get_data(device);
690 connman_device_set_data(device, NULL);
692 g_dbus_proxy_unref(proxy);
694 connman_device_unregister(device);
695 connman_device_unref(device);
698 struct tethering_info {
699 struct connman_technology *technology;
704 static void tethering_free(void *user_data)
706 struct tethering_info *tethering = user_data;
708 g_free(tethering->bridge);
712 static void tethering_create_cb(DBusMessage *message, void *user_data)
714 struct tethering_info *tethering = user_data;
716 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
717 const char *dbus_error = dbus_message_get_error_name(message);
719 DBG("%s tethering failed: %s",
720 tethering->enable ? "enable" : "disable",
725 DBG("bridge %s %s", tethering->bridge, tethering->enable ?
726 "enabled": "disabled");
728 if (tethering->technology)
729 connman_technology_tethering_notify(tethering->technology,
733 static void tethering_append(DBusMessageIter *iter, void *user_data)
735 struct tethering_info *tethering = user_data;
736 const char *nap = "nap";
738 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &nap);
739 if (tethering->enable)
740 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
744 static bool tethering_create(const char *path,
745 struct connman_technology *technology, const char *bridge,
748 struct tethering_info *tethering = g_new0(struct tethering_info, 1);
753 DBG("path %s bridge %s", path, bridge);
760 proxy = g_dbus_proxy_new(client, path, "org.bluez.NetworkServer1");
766 tethering->technology = technology;
767 tethering->bridge = g_strdup(bridge);
768 tethering->enable = enabled;
770 if (tethering->enable)
773 method = "Unregister";
775 result = g_dbus_proxy_method_call(proxy, method, tethering_append,
776 tethering_create_cb, tethering, tethering_free);
778 g_dbus_proxy_unref(proxy);
783 static void device_create(GDBusProxy *proxy)
785 struct connman_device *device = NULL;
786 const char *path = g_dbus_proxy_get_path(proxy);
788 char ident[BLUETOOTH_ADDR_LEN * 2 + 1];
791 address = proxy_get_string(proxy, "Address");
795 address2ident(address, ident);
797 device = connman_device_create("bluetooth",
798 CONNMAN_DEVICE_TYPE_BLUETOOTH);
802 connman_device_set_data(device, g_dbus_proxy_ref(proxy));
803 connman_device_set_ident(device, ident);
805 g_hash_table_replace(devices, g_strdup(path), device);
807 DBG("device %p %s device powered %d adapter powered %d", device,
808 path, connman_device_get_powered(device),
809 proxy_get_bool(proxy, "Powered"));
811 if (connman_device_register(device) < 0) {
812 g_hash_table_remove(devices, device);
816 g_dbus_proxy_set_property_watch(proxy, adapter_property_change, NULL);
818 powered = proxy_get_bool(proxy, "Powered");
819 connman_device_set_powered(device, powered);
821 if (proxy_get_role(proxy) && !bluetooth_tethering)
822 tethering_create(path, NULL, NULL, false);
825 static void object_added(GDBusProxy *proxy, void *user_data)
827 const char *interface;
829 interface = g_dbus_proxy_get_interface(proxy);
831 connman_warn("Interface or proxy missing when adding "
836 if (strcmp(interface, "org.bluez.Adapter1") == 0) {
837 DBG("%s %s", interface, g_dbus_proxy_get_path(proxy));
838 device_create(proxy);
842 if (strcmp(interface, "org.bluez.Network1") == 0) {
843 DBG("%s %s", interface, g_dbus_proxy_get_path(proxy));
849 static void object_removed(GDBusProxy *proxy, void *user_data)
851 const char *interface, *path;
853 interface = g_dbus_proxy_get_interface(proxy);
855 connman_warn("Interface or proxy missing when removing "
860 if (strcmp(interface, "org.bluez.Adapter1") == 0) {
861 path = g_dbus_proxy_get_path(proxy);
862 DBG("%s %s", interface, path);
864 g_hash_table_remove(devices, path);
867 if (strcmp(interface, "org.bluez.Network1") == 0) {
868 path = g_dbus_proxy_get_path(proxy);
869 DBG("%s %s", interface, path);
871 g_hash_table_remove(networks, path);
876 static int bluetooth_device_probe(struct connman_device *device)
881 g_hash_table_iter_init(&iter, devices);
883 while (g_hash_table_iter_next(&iter, &key, &value)) {
884 struct connman_device *known = value;
893 static void bluetooth_device_remove(struct connman_device *device)
898 static struct connman_device_driver device_driver = {
900 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
901 .probe = bluetooth_device_probe,
902 .remove = bluetooth_device_remove,
903 .enable = bluetooth_device_enable,
904 .disable = bluetooth_device_disable,
907 static int bluetooth_tech_probe(struct connman_technology *technology)
912 static void bluetooth_tech_remove(struct connman_technology *technology)
917 static int bluetooth_tech_set_tethering(struct connman_technology *technology,
918 const char *identifier, const char *passphrase,
919 const char *bridge, bool enabled)
921 GHashTableIter hash_iter;
925 bluetooth_tethering = enabled;
927 g_hash_table_iter_init(&hash_iter, devices);
929 while (g_hash_table_iter_next(&hash_iter, &key, &value)) {
930 const char *path = key;
931 struct connman_device *device = value;
933 DBG("device %p", device);
935 if (tethering_create(path, technology, bridge, enabled)
940 DBG("%s %d device(s)", enabled ? "enabled" : "disabled", i);
948 static struct connman_technology_driver tech_driver = {
950 .type = CONNMAN_SERVICE_TYPE_BLUETOOTH,
951 .probe = bluetooth_tech_probe,
952 .remove = bluetooth_tech_remove,
953 .set_tethering = bluetooth_tech_set_tethering,
956 static int bluetooth_init(void)
958 connection = connman_dbus_get_connection();
962 if (connman_technology_driver_register(&tech_driver) < 0) {
963 connman_warn("Failed to initialize technology for Bluez 5");
967 devices = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
970 if (connman_device_driver_register(&device_driver) < 0) {
971 connman_warn("Failed to initialize device driver for "
973 connman_technology_driver_unregister(&tech_driver);
977 if (connman_network_driver_register(&network_driver) < 0) {
978 connman_technology_driver_unregister(&tech_driver);
979 connman_device_driver_unregister(&device_driver);
983 networks = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
986 client = g_dbus_client_new(connection, BLUEZ_SERVICE, BLUEZ_PATH);
988 connman_warn("Failed to initialize D-Bus client for "
993 g_dbus_client_set_proxy_handlers(client, object_added, object_removed,
1000 g_hash_table_destroy(networks);
1003 g_hash_table_destroy(devices);
1006 g_dbus_client_unref(client);
1009 dbus_connection_unref(connection);
1014 static void bluetooth_exit(void)
1017 * We unset the disabling of the Bluetooth device when shutting down
1018 * so that non-PAN BT connections are not affected.
1020 device_driver.disable = NULL;
1022 g_dbus_client_unref(client);
1024 connman_network_driver_unregister(&network_driver);
1025 g_hash_table_destroy(networks);
1027 connman_device_driver_unregister(&device_driver);
1028 g_hash_table_destroy(devices);
1030 connman_technology_driver_unregister(&tech_driver);
1031 dbus_connection_unref(connection);
1034 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
1035 CONNMAN_PLUGIN_PRIORITY_DEFAULT, bluetooth_init, bluetooth_exit)