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);
564 static int bluetooth_device_enable(struct connman_device *device)
566 GDBusProxy *proxy = connman_device_get_data(device);
567 dbus_bool_t device_powered = TRUE;
573 path = g_dbus_proxy_get_path(proxy);
575 if (proxy_get_bool(proxy, "Powered")) {
576 DBG("already enabled %p %s", device, path);
580 DBG("device %p %s", device, path);
582 g_dbus_proxy_set_property_basic(proxy, "Powered",
583 DBUS_TYPE_BOOLEAN, &device_powered,
584 device_enable_cb, g_strdup(path), NULL);
589 static void disable_device(struct connman_device *device, const char *path)
594 DBG("device %p %s", device, path);
595 connman_device_set_powered(device, false);
597 g_hash_table_iter_init(&iter, networks);
598 while (g_hash_table_iter_next(&iter, &key, &value)) {
599 struct bluetooth_pan *pan = value;
601 if (pan->network && connman_network_get_device(pan->network)
603 DBG("disable network %p", pan->network);
604 connman_device_remove_network(device, pan->network);
609 static void device_disable_cb(const DBusError *error, void *user_data)
611 char *path = user_data;
612 struct connman_device *device;
614 device = g_hash_table_lookup(devices, path);
616 DBG("device already removed");
620 if (dbus_error_is_set(error)) {
621 connman_warn("Bluetooth device %s not disabled: %s",
622 path, error->message);
626 #if !defined TIZEN_EXT
627 disable_device(device, path);
634 static int bluetooth_device_disable(struct connman_device *device)
636 GDBusProxy *proxy = connman_device_get_data(device);
637 dbus_bool_t device_powered = FALSE;
643 path = g_dbus_proxy_get_path(proxy);
645 if (!proxy_get_bool(proxy, "Powered")) {
646 DBG("already disabled %p %s", device, path);
650 DBG("device %p %s", device, path);
652 g_dbus_proxy_set_property_basic(proxy, "Powered",
653 DBUS_TYPE_BOOLEAN, &device_powered,
654 device_disable_cb, g_strdup(path), NULL);
659 static void adapter_property_change(GDBusProxy *proxy, const char *name,
660 DBusMessageIter *iter, void *user_data)
662 struct connman_device *device;
664 bool adapter_powered, device_powered;
666 if (strcmp(name, "Powered") != 0)
669 path = g_dbus_proxy_get_path(proxy);
670 device = g_hash_table_lookup(devices, path);
672 adapter_powered = proxy_get_bool(proxy, "Powered");
673 device_powered = connman_device_get_powered(device);
675 DBG("device %p %s device powered %d adapter powered %d", device, path,
676 device_powered, adapter_powered);
678 if (device_powered != adapter_powered) {
680 enable_device(device, path);
682 disable_device(device, path);
686 static void device_free(gpointer data)
688 struct connman_device *device = data;
689 GDBusProxy *proxy = connman_device_get_data(device);
691 connman_device_set_data(device, NULL);
693 g_dbus_proxy_unref(proxy);
695 connman_device_unregister(device);
696 connman_device_unref(device);
699 struct tethering_info {
700 struct connman_technology *technology;
705 static void tethering_free(void *user_data)
707 struct tethering_info *tethering = user_data;
709 g_free(tethering->bridge);
713 static void tethering_create_cb(DBusMessage *message, void *user_data)
715 struct tethering_info *tethering = user_data;
717 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
718 const char *dbus_error = dbus_message_get_error_name(message);
720 DBG("%s tethering failed: %s",
721 tethering->enable ? "enable" : "disable",
726 DBG("bridge %s %s", tethering->bridge, tethering->enable ?
727 "enabled": "disabled");
729 if (tethering->technology)
730 connman_technology_tethering_notify(tethering->technology,
734 static void tethering_append(DBusMessageIter *iter, void *user_data)
736 struct tethering_info *tethering = user_data;
737 const char *nap = "nap";
739 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &nap);
740 if (tethering->enable)
741 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
745 static bool tethering_create(const char *path,
746 struct connman_technology *technology, const char *bridge,
749 struct tethering_info *tethering = g_new0(struct tethering_info, 1);
754 DBG("path %s bridge %s", path, bridge);
761 proxy = g_dbus_proxy_new(client, path, "org.bluez.NetworkServer1");
767 tethering->technology = technology;
768 tethering->bridge = g_strdup(bridge);
769 tethering->enable = enabled;
771 if (tethering->enable)
774 method = "Unregister";
776 result = g_dbus_proxy_method_call(proxy, method, tethering_append,
777 tethering_create_cb, tethering, tethering_free);
779 g_dbus_proxy_unref(proxy);
784 static void device_create(GDBusProxy *proxy)
786 struct connman_device *device = NULL;
787 const char *path = g_dbus_proxy_get_path(proxy);
789 char ident[BLUETOOTH_ADDR_LEN * 2 + 1];
792 address = proxy_get_string(proxy, "Address");
796 address2ident(address, ident);
798 device = connman_device_create("bluetooth",
799 CONNMAN_DEVICE_TYPE_BLUETOOTH);
803 connman_device_set_data(device, g_dbus_proxy_ref(proxy));
804 connman_device_set_ident(device, ident);
806 g_hash_table_replace(devices, g_strdup(path), device);
808 DBG("device %p %s device powered %d adapter powered %d", device,
809 path, connman_device_get_powered(device),
810 proxy_get_bool(proxy, "Powered"));
812 if (connman_device_register(device) < 0) {
813 g_hash_table_remove(devices, device);
817 g_dbus_proxy_set_property_watch(proxy, adapter_property_change, NULL);
819 powered = proxy_get_bool(proxy, "Powered");
820 connman_device_set_powered(device, powered);
822 if (proxy_get_role(proxy) && !bluetooth_tethering)
823 tethering_create(path, NULL, NULL, false);
826 static void object_added(GDBusProxy *proxy, void *user_data)
828 const char *interface;
830 interface = g_dbus_proxy_get_interface(proxy);
832 connman_warn("Interface or proxy missing when adding "
837 if (strcmp(interface, "org.bluez.Adapter1") == 0) {
838 DBG("%s %s", interface, g_dbus_proxy_get_path(proxy));
839 device_create(proxy);
843 if (strcmp(interface, "org.bluez.Network1") == 0) {
844 DBG("%s %s", interface, g_dbus_proxy_get_path(proxy));
850 static void object_removed(GDBusProxy *proxy, void *user_data)
852 const char *interface, *path;
854 interface = g_dbus_proxy_get_interface(proxy);
856 connman_warn("Interface or proxy missing when removing "
861 if (strcmp(interface, "org.bluez.Adapter1") == 0) {
862 path = g_dbus_proxy_get_path(proxy);
863 DBG("%s %s", interface, path);
865 g_hash_table_remove(devices, path);
868 if (strcmp(interface, "org.bluez.Network1") == 0) {
869 path = g_dbus_proxy_get_path(proxy);
870 DBG("%s %s", interface, path);
872 g_hash_table_remove(networks, path);
877 static int bluetooth_device_probe(struct connman_device *device)
882 g_hash_table_iter_init(&iter, devices);
884 while (g_hash_table_iter_next(&iter, &key, &value)) {
885 struct connman_device *known = value;
894 static void bluetooth_device_remove(struct connman_device *device)
899 static struct connman_device_driver device_driver = {
901 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
902 .probe = bluetooth_device_probe,
903 .remove = bluetooth_device_remove,
904 .enable = bluetooth_device_enable,
905 .disable = bluetooth_device_disable,
908 static int bluetooth_tech_probe(struct connman_technology *technology)
913 static void bluetooth_tech_remove(struct connman_technology *technology)
918 static int bluetooth_tech_set_tethering(struct connman_technology *technology,
919 const char *identifier, const char *passphrase,
920 const char *bridge, bool enabled)
922 GHashTableIter hash_iter;
926 bluetooth_tethering = enabled;
928 g_hash_table_iter_init(&hash_iter, devices);
930 while (g_hash_table_iter_next(&hash_iter, &key, &value)) {
931 const char *path = key;
932 struct connman_device *device = value;
934 DBG("device %p", device);
936 if (tethering_create(path, technology, bridge, enabled)
941 DBG("%s %d device(s)", enabled ? "enabled" : "disabled", i);
949 static struct connman_technology_driver tech_driver = {
951 .type = CONNMAN_SERVICE_TYPE_BLUETOOTH,
952 .probe = bluetooth_tech_probe,
953 .remove = bluetooth_tech_remove,
954 .set_tethering = bluetooth_tech_set_tethering,
957 static int bluetooth_init(void)
959 connection = connman_dbus_get_connection();
963 if (connman_technology_driver_register(&tech_driver) < 0) {
964 connman_warn("Failed to initialize technology for Bluez 5");
968 devices = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
971 if (connman_device_driver_register(&device_driver) < 0) {
972 connman_warn("Failed to initialize device driver for "
974 connman_technology_driver_unregister(&tech_driver);
978 if (connman_network_driver_register(&network_driver) < 0) {
979 connman_technology_driver_unregister(&tech_driver);
980 connman_device_driver_unregister(&device_driver);
984 networks = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
987 client = g_dbus_client_new(connection, BLUEZ_SERVICE, BLUEZ_PATH);
989 connman_warn("Failed to initialize D-Bus client for "
994 g_dbus_client_set_proxy_handlers(client, object_added, object_removed,
1001 g_hash_table_destroy(networks);
1004 g_hash_table_destroy(devices);
1007 g_dbus_client_unref(client);
1010 dbus_connection_unref(connection);
1015 static void bluetooth_exit(void)
1018 * We unset the disabling of the Bluetooth device when shutting down
1019 * so that non-PAN BT connections are not affected.
1021 device_driver.disable = NULL;
1023 g_dbus_client_unref(client);
1025 connman_network_driver_unregister(&network_driver);
1026 g_hash_table_destroy(networks);
1028 connman_device_driver_unregister(&device_driver);
1029 g_hash_table_destroy(devices);
1031 connman_technology_driver_unregister(&tech_driver);
1032 dbus_connection_unref(connection);
1035 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
1036 CONNMAN_PLUGIN_PRIORITY_DEFAULT, bluetooth_init, bluetooth_exit)