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_NAP "00001116-0000-1000-8000-00805f9b34fb"
41 #define BLUETOOTH_ADDR_LEN 6
43 static DBusConnection *connection;
44 static GDBusClient *client;
45 static GHashTable *devices;
46 static GHashTable *networks;
47 static bool bluetooth_tethering;
49 struct bluetooth_pan {
50 struct connman_network *network;
51 GDBusProxy *btdevice_proxy;
52 GDBusProxy *btnetwork_proxy;
55 static void address2ident(const char *address, char *ident)
59 for (i = 0; i < BLUETOOTH_ADDR_LEN; i++) {
60 ident[i * 2] = address[i * 3];
61 ident[i * 2 + 1] = address[i * 3 + 1];
63 ident[BLUETOOTH_ADDR_LEN * 2] = '\0';
66 static const char *proxy_get_string(GDBusProxy *proxy, const char *property)
71 if (!g_dbus_proxy_get_property(proxy, property, &iter))
73 dbus_message_iter_get_basic(&iter, &str);
77 static bool proxy_get_bool(GDBusProxy *proxy, const char *property)
82 if (!g_dbus_proxy_get_property(proxy, property, &iter))
84 dbus_message_iter_get_basic(&iter, &value);
88 static bool proxy_get_nap(GDBusProxy *proxy)
90 DBusMessageIter iter, value;
95 if (!g_dbus_proxy_get_property(proxy, "UUIDs", &iter))
98 dbus_message_iter_recurse(&iter, &value);
99 while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
102 dbus_message_iter_get_basic(&value, &uuid);
103 if (strcmp(uuid, BLUETOOTH_PAN_NAP) == 0)
106 dbus_message_iter_next(&value);
112 static int bluetooth_pan_probe(struct connman_network *network)
117 DBG("network %p", network);
119 g_hash_table_iter_init(&iter, networks);
121 while (g_hash_table_iter_next(&iter, &key, &value)) {
122 struct bluetooth_pan *pan = value;
124 if (network == pan->network)
131 static void pan_remove_nap(struct bluetooth_pan *pan)
133 struct connman_device *device;
134 struct connman_network *network = pan->network;
136 DBG("network %p pan %p", pan->network, pan);
142 connman_network_set_data(network, NULL);
144 device = connman_network_get_device(network);
146 connman_device_remove_network(device, network);
148 connman_network_unref(network);
151 static void bluetooth_pan_remove(struct connman_network *network)
153 struct bluetooth_pan *pan = connman_network_get_data(network);
155 DBG("network %p pan %p", network, pan);
157 connman_network_set_data(network, NULL);
163 static bool pan_connect(struct bluetooth_pan *pan,
169 if (!proxy_get_bool(pan->btnetwork_proxy, "Connected"))
171 iface = proxy_get_string(pan->btnetwork_proxy, "Interface");
177 index = connman_inet_ifindex(iface);
179 DBG("network %p invalid index %d", pan->network, index);
183 connman_network_set_index(pan->network, index);
184 connman_network_set_connected(pan->network, true);
189 static void pan_connect_cb(DBusMessage *message, void *user_data)
191 const char *path = user_data;
192 const char *iface = NULL;
193 struct bluetooth_pan *pan;
194 DBusMessageIter iter;
196 pan = g_hash_table_lookup(networks, path);
198 DBG("network already removed");
202 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
203 const char *dbus_error = dbus_message_get_error_name(message);
205 DBG("network %p %s", pan->network, dbus_error);
207 if (strcmp(dbus_error,
208 "org.bluez.Error.AlreadyConnected") != 0) {
209 connman_network_set_error(pan->network,
210 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
214 if (dbus_message_iter_init(message, &iter) &&
215 dbus_message_iter_get_arg_type(&iter) ==
217 dbus_message_iter_get_basic(&iter, &iface);
220 DBG("network %p interface %s", pan->network, iface);
222 pan_connect(pan, iface);
225 static void pan_connect_append(DBusMessageIter *iter,
228 const char *role = BLUETOOTH_PAN_NAP;
230 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &role);
233 static int bluetooth_pan_connect(struct connman_network *network)
235 struct bluetooth_pan *pan = connman_network_get_data(network);
238 DBG("network %p", network);
243 path = g_dbus_proxy_get_path(pan->btnetwork_proxy);
245 if (!g_dbus_proxy_method_call(pan->btnetwork_proxy, "Connect",
246 pan_connect_append, pan_connect_cb,
247 g_strdup(path), g_free))
250 connman_network_set_associating(pan->network, true);
255 static void pan_disconnect_cb(DBusMessage *message, void *user_data)
257 const char *path = user_data;
258 struct bluetooth_pan *pan;
260 pan = g_hash_table_lookup(networks, path);
262 DBG("network already removed");
266 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
267 const char *dbus_error = dbus_message_get_error_name(message);
269 DBG("network %p %s", pan->network, dbus_error);
272 DBG("network %p", pan->network);
274 connman_network_set_connected(pan->network, false);
277 static int bluetooth_pan_disconnect(struct connman_network *network)
279 struct bluetooth_pan *pan = connman_network_get_data(network);
282 DBG("network %p", network);
287 path = g_dbus_proxy_get_path(pan->btnetwork_proxy);
289 if (!g_dbus_proxy_method_call(pan->btnetwork_proxy, "Disconnect",
290 NULL, pan_disconnect_cb, g_strdup(path), g_free))
296 static void btnetwork_property_change(GDBusProxy *proxy, const char *name,
297 DBusMessageIter *iter, void *user_data)
299 struct bluetooth_pan *pan;
300 dbus_bool_t connected;
301 bool proxy_connected, network_connected;
303 if (strcmp(name, "Connected") != 0)
306 pan = g_hash_table_lookup(networks, g_dbus_proxy_get_path(proxy));
307 if (!pan || !pan->network)
310 dbus_message_iter_get_basic(iter, &connected);
311 proxy_connected = connected;
313 network_connected = connman_network_get_connected(pan->network);
315 DBG("network %p network connected %d proxy connected %d",
316 pan->network, network_connected, proxy_connected);
318 if (network_connected != proxy_connected)
319 connman_network_set_connected(pan->network, proxy_connected);
322 static void pan_create_nap(struct bluetooth_pan *pan)
324 struct connman_device *device;
326 if (!proxy_get_nap(pan->btdevice_proxy)) {
331 device = g_hash_table_lookup(devices,
332 proxy_get_string(pan->btdevice_proxy, "Adapter"));
334 if (!device || !connman_device_get_powered(device))
339 char ident[BLUETOOTH_ADDR_LEN * 2 + 1];
340 const char *name, *path;
342 address = proxy_get_string(pan->btdevice_proxy, "Address");
344 connman_warn("Bluetooth device address missing");
348 address2ident(address, ident);
350 pan->network = connman_network_create(ident,
351 CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
353 name = proxy_get_string(pan->btdevice_proxy, "Alias");
354 path = g_dbus_proxy_get_path(pan->btnetwork_proxy);
356 DBG("network %p %s %s", pan->network, path, name);
359 connman_warn("Bluetooth network %s creation failed",
364 connman_network_set_data(pan->network, pan);
365 connman_network_set_name(pan->network, name);
366 connman_network_set_group(pan->network, ident);
369 connman_device_add_network(device, pan->network);
371 if (pan_connect(pan, NULL))
372 DBG("network %p already connected", pan->network);
375 static void btdevice_property_change(GDBusProxy *proxy, const char *name,
376 DBusMessageIter *iter, void *user_data)
378 struct bluetooth_pan *pan;
379 bool pan_nap = false;
381 if (strcmp(name, "UUIDs") != 0)
384 pan = g_hash_table_lookup(networks, g_dbus_proxy_get_path(proxy));
389 connman_network_get_device(pan->network))
392 DBG("network %p network nap %d proxy nap %d", pan->network, pan_nap,
393 proxy_get_nap(pan->btdevice_proxy));
395 if (proxy_get_nap(pan->btdevice_proxy) == pan_nap)
401 static void pan_free(gpointer data)
403 struct bluetooth_pan *pan = data;
405 if (pan->btnetwork_proxy) {
406 g_dbus_proxy_unref(pan->btnetwork_proxy);
407 pan->btnetwork_proxy = NULL;
410 if (pan->btdevice_proxy) {
411 g_dbus_proxy_unref(pan->btdevice_proxy);
412 pan->btdevice_proxy = NULL;
420 static void pan_create(GDBusProxy *network_proxy)
422 const char *path = g_dbus_proxy_get_path(network_proxy);
423 struct bluetooth_pan *pan;
425 pan = g_try_new0(struct bluetooth_pan, 1);
428 connman_error("Out of memory creating PAN NAP");
432 g_hash_table_replace(networks, g_strdup(path), pan);
434 pan->btnetwork_proxy = g_dbus_proxy_ref(network_proxy);
435 pan->btdevice_proxy = g_dbus_proxy_new(client, path,
436 "org.bluez.Device1");
438 if (!pan->btdevice_proxy) {
439 connman_error("Cannot create BT PAN watcher %s", path);
440 g_hash_table_remove(networks, path);
444 g_dbus_proxy_set_property_watch(pan->btnetwork_proxy,
445 btnetwork_property_change, NULL);
447 g_dbus_proxy_set_property_watch(pan->btdevice_proxy,
448 btdevice_property_change, NULL);
450 DBG("pan %p %s nap %d", pan, path, proxy_get_nap(pan->btdevice_proxy));
455 static struct connman_network_driver network_driver = {
457 .type = CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN,
458 .probe = bluetooth_pan_probe,
459 .remove = bluetooth_pan_remove,
460 .connect = bluetooth_pan_connect,
461 .disconnect = bluetooth_pan_disconnect,
464 static void enable_device(struct connman_device *device, const char *path)
469 DBG("device %p %s", device, path);
470 connman_device_set_powered(device, true);
472 g_hash_table_iter_init(&iter, networks);
473 while (g_hash_table_iter_next(&iter, &key, &value)) {
474 struct bluetooth_pan *pan = value;
476 if (g_strcmp0(proxy_get_string(pan->btdevice_proxy, "Adapter"),
479 DBG("enable network %p", pan->network);
485 static void device_enable_cb(const DBusError *error, void *user_data)
487 char *path = user_data;
488 struct connman_device *device;
490 device = g_hash_table_lookup(devices, path);
492 DBG("device already removed");
496 if (dbus_error_is_set(error)) {
497 connman_warn("Bluetooth device %s not enabled %s",
498 path, error->message);
502 enable_device(device, path);
507 static int bluetooth_device_enable(struct connman_device *device)
509 GDBusProxy *proxy = connman_device_get_data(device);
510 dbus_bool_t device_powered = TRUE;
516 path = g_dbus_proxy_get_path(proxy);
518 if (proxy_get_bool(proxy, "Powered")) {
519 DBG("already enabled %p %s", device, path);
523 DBG("device %p %s", device, path);
525 g_dbus_proxy_set_property_basic(proxy, "Powered",
526 DBUS_TYPE_BOOLEAN, &device_powered,
527 device_enable_cb, g_strdup(path), NULL);
532 static void disable_device(struct connman_device *device, const char *path)
537 DBG("device %p %s", device, path);
538 connman_device_set_powered(device, false);
540 g_hash_table_iter_init(&iter, networks);
541 while (g_hash_table_iter_next(&iter, &key, &value)) {
542 struct bluetooth_pan *pan = value;
544 if (pan->network && connman_network_get_device(pan->network)
546 DBG("disable network %p", pan->network);
547 connman_device_remove_network(device, pan->network);
552 static void device_disable_cb(const DBusError *error, void *user_data)
554 char *path = user_data;
555 struct connman_device *device;
557 device = g_hash_table_lookup(devices, path);
559 DBG("device already removed");
563 if (dbus_error_is_set(error)) {
564 connman_warn("Bluetooth device %s not disabled: %s",
565 path, error->message);
569 disable_device(device, path);
575 static int bluetooth_device_disable(struct connman_device *device)
577 GDBusProxy *proxy = connman_device_get_data(device);
578 dbus_bool_t device_powered = FALSE;
584 path = g_dbus_proxy_get_path(proxy);
586 if (!proxy_get_bool(proxy, "Powered")) {
587 DBG("already disabled %p %s", device, path);
591 DBG("device %p %s", device, path);
593 g_dbus_proxy_set_property_basic(proxy, "Powered",
594 DBUS_TYPE_BOOLEAN, &device_powered,
595 device_disable_cb, g_strdup(path), NULL);
600 static void adapter_property_change(GDBusProxy *proxy, const char *name,
601 DBusMessageIter *iter, void *user_data)
603 struct connman_device *device;
605 bool adapter_powered, device_powered;
607 if (strcmp(name, "Powered") != 0)
610 path = g_dbus_proxy_get_path(proxy);
611 device = g_hash_table_lookup(devices, path);
613 adapter_powered = proxy_get_bool(proxy, "Powered");
614 device_powered = connman_device_get_powered(device);
616 DBG("device %p %s device powered %d adapter powered %d", device, path,
617 device_powered, adapter_powered);
619 if (device_powered != adapter_powered) {
621 enable_device(device, path);
623 disable_device(device, path);
627 static void device_free(gpointer data)
629 struct connman_device *device = data;
630 GDBusProxy *proxy = connman_device_get_data(device);
632 connman_device_set_data(device, NULL);
634 g_dbus_proxy_unref(proxy);
636 connman_device_unregister(device);
637 connman_device_unref(device);
640 struct tethering_info {
641 struct connman_technology *technology;
646 static void tethering_free(void *user_data)
648 struct tethering_info *tethering = user_data;
650 g_free(tethering->bridge);
654 static void tethering_create_cb(DBusMessage *message, void *user_data)
656 struct tethering_info *tethering = user_data;
658 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
659 const char *dbus_error = dbus_message_get_error_name(message);
661 DBG("%s tethering failed: %s",
662 tethering->enable ? "enable" : "disable",
667 DBG("bridge %s %s", tethering->bridge, tethering->enable ?
668 "enabled": "disabled");
670 if (tethering->technology)
671 connman_technology_tethering_notify(tethering->technology,
675 static void tethering_append(DBusMessageIter *iter, void *user_data)
677 struct tethering_info *tethering = user_data;
678 const char *nap = "nap";
680 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &nap);
681 if (tethering->enable)
682 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
686 static bool tethering_create(const char *path,
687 struct connman_technology *technology, const char *bridge,
690 struct tethering_info *tethering = g_new0(struct tethering_info, 1);
695 DBG("path %s bridge %s", path, bridge);
700 proxy = g_dbus_proxy_new(client, path, "org.bluez.NetworkServer1");
704 tethering->technology = technology;
705 tethering->bridge = g_strdup(bridge);
706 tethering->enable = enabled;
708 if (tethering->enable)
711 method = "Unregister";
713 result = g_dbus_proxy_method_call(proxy, method, tethering_append,
714 tethering_create_cb, tethering, tethering_free);
716 g_dbus_proxy_unref(proxy);
721 static void device_create(GDBusProxy *proxy)
723 struct connman_device *device = NULL;
724 const char *path = g_dbus_proxy_get_path(proxy);
726 char ident[BLUETOOTH_ADDR_LEN * 2 + 1];
729 address = proxy_get_string(proxy, "Address");
733 address2ident(address, ident);
735 device = connman_device_create("bluetooth",
736 CONNMAN_DEVICE_TYPE_BLUETOOTH);
740 connman_device_set_data(device, g_dbus_proxy_ref(proxy));
741 connman_device_set_ident(device, ident);
743 g_hash_table_replace(devices, g_strdup(path), device);
745 DBG("device %p %s device powered %d adapter powered %d", device,
746 path, connman_device_get_powered(device),
747 proxy_get_bool(proxy, "Powered"));
749 if (connman_device_register(device) < 0) {
750 g_hash_table_remove(devices, device);
754 g_dbus_proxy_set_property_watch(proxy, adapter_property_change, NULL);
756 powered = proxy_get_bool(proxy, "Powered");
757 connman_device_set_powered(device, powered);
759 if (proxy_get_nap(proxy) && !bluetooth_tethering)
760 tethering_create(path, NULL, NULL, false);
763 static void object_added(GDBusProxy *proxy, void *user_data)
765 const char *interface;
767 interface = g_dbus_proxy_get_interface(proxy);
769 connman_warn("Interface or proxy missing when adding "
774 if (strcmp(interface, "org.bluez.Adapter1") == 0) {
775 DBG("%s %s", interface, g_dbus_proxy_get_path(proxy));
776 device_create(proxy);
780 if (strcmp(interface, "org.bluez.Network1") == 0) {
781 DBG("%s %s", interface, g_dbus_proxy_get_path(proxy));
787 static void object_removed(GDBusProxy *proxy, void *user_data)
789 const char *interface, *path;
791 interface = g_dbus_proxy_get_interface(proxy);
793 connman_warn("Interface or proxy missing when removing "
798 if (strcmp(interface, "org.bluez.Adapter1") == 0) {
799 path = g_dbus_proxy_get_path(proxy);
800 DBG("%s %s", interface, path);
802 g_hash_table_remove(devices, path);
805 if (strcmp(interface, "org.bluez.Network1") == 0) {
806 path = g_dbus_proxy_get_path(proxy);
807 DBG("%s %s", interface, path);
809 g_hash_table_remove(networks, path);
814 static int bluetooth_device_probe(struct connman_device *device)
819 g_hash_table_iter_init(&iter, devices);
821 while (g_hash_table_iter_next(&iter, &key, &value)) {
822 struct connman_device *known = value;
831 static void bluetooth_device_remove(struct connman_device *device)
836 static struct connman_device_driver device_driver = {
838 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
839 .probe = bluetooth_device_probe,
840 .remove = bluetooth_device_remove,
841 .enable = bluetooth_device_enable,
842 .disable = bluetooth_device_disable,
845 static int bluetooth_tech_probe(struct connman_technology *technology)
850 static void bluetooth_tech_remove(struct connman_technology *technology)
855 static int bluetooth_tech_set_tethering(struct connman_technology *technology,
856 const char *identifier, const char *passphrase,
857 const char *bridge, bool enabled)
859 GHashTableIter hash_iter;
863 bluetooth_tethering = enabled;
865 g_hash_table_iter_init(&hash_iter, devices);
867 while (g_hash_table_iter_next(&hash_iter, &key, &value)) {
868 const char *path = key;
869 struct connman_device *device = value;
871 DBG("device %p", device);
873 if (tethering_create(path, technology, bridge, enabled)
878 DBG("%s %d device(s)", enabled ? "enabled" : "disabled", i);
886 static struct connman_technology_driver tech_driver = {
888 .type = CONNMAN_SERVICE_TYPE_BLUETOOTH,
889 .probe = bluetooth_tech_probe,
890 .remove = bluetooth_tech_remove,
891 .set_tethering = bluetooth_tech_set_tethering,
894 static int bluetooth_init(void)
896 connection = connman_dbus_get_connection();
900 if (connman_technology_driver_register(&tech_driver) < 0) {
901 connman_warn("Failed to initialize technology for Bluez 5");
905 devices = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
908 if (connman_device_driver_register(&device_driver) < 0) {
909 connman_warn("Failed to initialize device driver for "
911 connman_technology_driver_unregister(&tech_driver);
915 if (connman_network_driver_register(&network_driver) < 0) {
916 connman_technology_driver_unregister(&tech_driver);
917 connman_device_driver_unregister(&device_driver);
921 networks = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
924 client = g_dbus_client_new(connection, BLUEZ_SERVICE, BLUEZ_PATH);
926 connman_warn("Failed to initialize D-Bus client for "
931 g_dbus_client_set_proxy_handlers(client, object_added, object_removed,
938 g_hash_table_destroy(networks);
941 g_hash_table_destroy(devices);
944 g_dbus_client_unref(client);
947 dbus_connection_unref(connection);
952 static void bluetooth_exit(void)
955 * We unset the disabling of the Bluetooth device when shutting down
956 * so that non-PAN BT connections are not affected.
958 device_driver.disable = NULL;
960 connman_network_driver_unregister(&network_driver);
961 g_hash_table_destroy(networks);
963 connman_device_driver_unregister(&device_driver);
964 g_hash_table_destroy(devices);
966 connman_technology_driver_unregister(&tech_driver);
967 dbus_connection_unref(connection);
970 CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
971 CONNMAN_PLUGIN_PRIORITY_DEFAULT, bluetooth_init, bluetooth_exit)