X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=plugins%2Fbluetooth.c;h=9e1aaa2c26a97c4be6e31f9afe38e701611349a3;hb=091f5fa9229897a52424cd442b325f97d361dbc3;hp=ef852dd4fd4720837adc4c790b4246dff1293215;hpb=b7d93813b4f141ff26356f305f2c0881f7d20cf0;p=framework%2Fconnectivity%2Fconnman.git diff --git a/plugins/bluetooth.c b/plugins/bluetooth.c index ef852dd..9e1aaa2 100644 --- a/plugins/bluetooth.c +++ b/plugins/bluetooth.c @@ -2,7 +2,7 @@ * * Connection Manager * - * Copyright (C) 2007-2010 Intel Corporation. All rights reserved. + * Copyright (C) 2007-2012 Intel Corporation. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as @@ -33,6 +33,7 @@ #define CONNMAN_API_SUBJECT_TO_CHANGE #include +#include #include #include #include @@ -43,10 +44,12 @@ #define BLUEZ_ADAPTER_INTERFACE BLUEZ_SERVICE ".Adapter" #define BLUEZ_DEVICE_INTERFACE BLUEZ_SERVICE ".Device" #define BLUEZ_NETWORK_INTERFACE BLUEZ_SERVICE ".Network" +#define BLUEZ_NETWORK_SERVER BLUEZ_SERVICE ".NetworkServer" #define LIST_ADAPTERS "ListAdapters" #define ADAPTER_ADDED "AdapterAdded" #define ADAPTER_REMOVED "AdapterRemoved" +#define DEVICE_REMOVED "DeviceRemoved" #define PROPERTY_CHANGED "PropertyChanged" #define GET_PROPERTIES "GetProperties" @@ -55,6 +58,9 @@ #define CONNECT "Connect" #define DISCONNECT "Disconnect" +#define REGISTER "Register" +#define UNREGISTER "Unregister" + #define UUID_NAP "00001116-0000-1000-8000-00805f9b34fb" #define TIMEOUT 5000 @@ -62,6 +68,7 @@ static DBusConnection *connection; static GHashTable *bluetooth_devices = NULL; +static GHashTable *bluetooth_networks = NULL; static int pan_probe(struct connman_network *network) { @@ -89,6 +96,13 @@ static void connect_reply(DBusPendingCall *call, void *user_data) dbus_error_init(&error); + if (dbus_set_error_from_message(&error, reply) == TRUE) { + connman_error("%s", error.message); + dbus_error_free(&error); + + goto err; + } + if (dbus_message_get_args(reply, &error, DBUS_TYPE_STRING, &interface, DBUS_TYPE_INVALID) == FALSE) { @@ -97,11 +111,11 @@ static void connect_reply(DBusPendingCall *call, void *user_data) dbus_error_free(&error); } else connman_error("Wrong arguments for connect"); - goto done; + goto err; } if (interface == NULL) - goto done; + goto err; DBG("interface %s", interface); @@ -109,11 +123,17 @@ static void connect_reply(DBusPendingCall *call, void *user_data) connman_network_set_index(network, index); - connman_network_set_method(network, CONNMAN_IPCONFIG_METHOD_DHCP); - connman_network_set_connected(network, TRUE); -done: + dbus_message_unref(reply); + + dbus_pending_call_unref(call); + + return; +err: + + connman_network_set_connected(network, FALSE); + dbus_message_unref(reply); dbus_pending_call_unref(call); @@ -131,9 +151,6 @@ static int pan_connect(struct connman_network *network) if (path == NULL) return -EINVAL; - if (connman_network_get_index(network) >= 0) - return -EISCONN; - message = dbus_message_new_method_call(BLUEZ_SERVICE, path, BLUEZ_NETWORK_INTERFACE, CONNECT); if (message == NULL) @@ -176,6 +193,12 @@ static void disconnect_reply(DBusPendingCall *call, void *user_data) dbus_error_init(&error); + if (dbus_set_error_from_message(&error, reply) == TRUE) { + connman_error("%s", error.message); + dbus_error_free(&error); + goto done; + } + if (dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) == FALSE) { if (dbus_error_is_set(&error) == TRUE) { connman_error("%s", error.message); @@ -186,12 +209,13 @@ static void disconnect_reply(DBusPendingCall *call, void *user_data) } connman_network_set_connected(network, FALSE); - connman_network_set_index(network, -1); done: dbus_message_unref(reply); dbus_pending_call_unref(call); + + connman_network_unref(network); } static int pan_disconnect(struct connman_network *network) @@ -205,9 +229,6 @@ static int pan_disconnect(struct connman_network *network) if (path == NULL) return -EINVAL; - if (connman_network_get_index(network) < 0) - return -ENOTCONN; - message = dbus_message_new_method_call(BLUEZ_SERVICE, path, BLUEZ_NETWORK_INTERFACE, DISCONNECT); if (message == NULL) @@ -230,6 +251,10 @@ static int pan_disconnect(struct connman_network *network) return -EINVAL; } + connman_network_ref(network); + + connman_network_set_associating(network, FALSE); + dbus_pending_call_set_notify(call, disconnect_reply, network, NULL); dbus_message_unref(message); @@ -246,101 +271,43 @@ static struct connman_network_driver pan_driver = { .disconnect = pan_disconnect, }; -static int bluetooth_probe(struct connman_device *device) -{ - DBG("device %p", device); - - return 0; -} - -static void bluetooth_remove(struct connman_device *device) -{ - DBG("device %p", device); -} - -static void powered_reply(DBusPendingCall *call, void *user_data) +static gboolean network_changed(DBusConnection *connection, + DBusMessage *message, void *user_data) { - DBusMessage *reply; - - DBG(""); - - reply = dbus_pending_call_steal_reply(call); - - dbus_message_unref(reply); + const char *path = dbus_message_get_path(message); + struct connman_network *network; + DBusMessageIter iter, value; + const char *key; - dbus_pending_call_unref(call); -} + DBG("path %s", path); -static int change_powered(DBusConnection *connection, const char *path, - dbus_bool_t powered) -{ - DBusMessage *message; - DBusMessageIter iter; - DBusPendingCall *call; + network = g_hash_table_lookup(bluetooth_networks, path); + if (network == NULL) + return TRUE; - DBG(""); + if (dbus_message_iter_init(message, &iter) == FALSE) + return TRUE; - if (path == NULL) - return -EINVAL; + dbus_message_iter_get_basic(&iter, &key); - message = dbus_message_new_method_call(BLUEZ_SERVICE, path, - BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY); - if (message == NULL) - return -ENOMEM; + dbus_message_iter_next(&iter); + dbus_message_iter_recurse(&iter, &value); - dbus_message_set_auto_start(message, FALSE); + if (g_str_equal(key, "Connected") == TRUE) { + dbus_bool_t connected; - dbus_message_iter_init_append(message, &iter); - connman_dbus_property_append_basic(&iter, "Powered", - DBUS_TYPE_BOOLEAN, &powered); + dbus_message_iter_get_basic(&value, &connected); - if (dbus_connection_send_with_reply(connection, message, - &call, TIMEOUT) == FALSE) { - connman_error("Failed to change Powered property"); - dbus_message_unref(message); - return -EINVAL; - } + if (connected == TRUE) + return TRUE; - if (call == NULL) { - connman_error("D-Bus connection not available"); - dbus_message_unref(message); - return -EINVAL; + connman_network_set_associating(network, FALSE); + connman_network_set_connected(network, FALSE); } - dbus_pending_call_set_notify(call, powered_reply, NULL, NULL); - - dbus_message_unref(message); - - return -EINPROGRESS; -} - -static int bluetooth_enable(struct connman_device *device) -{ - const char *path = connman_device_get_string(device, "Path"); - - DBG("device %p", device); - - return change_powered(connection, path, TRUE); -} - -static int bluetooth_disable(struct connman_device *device) -{ - const char *path = connman_device_get_string(device, "Path"); - - DBG("device %p", device); - - return change_powered(connection, path, FALSE); + return TRUE; } -static struct connman_device_driver bluetooth_driver = { - .name = "bluetooth", - .type = CONNMAN_DEVICE_TYPE_BLUETOOTH, - .probe = bluetooth_probe, - .remove = bluetooth_remove, - .enable = bluetooth_enable, - .disable = bluetooth_disable, -}; - static void extract_properties(DBusMessage *reply, const char **parent, const char **address, const char **name, @@ -473,14 +440,14 @@ static void network_properties_reply(DBusPendingCall *call, void *user_data) connman_network_set_string(network, "Path", path); - connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP); - connman_network_set_name(network, name); connman_device_add_network(device, network); connman_network_set_group(network, ident); + g_hash_table_replace(bluetooth_networks, g_strdup(path), network); + done: dbus_message_unref(reply); @@ -579,6 +546,89 @@ static gboolean adapter_changed(DBusConnection *connection, return TRUE; } +static gboolean device_removed(DBusConnection *connection, + DBusMessage *message, void *user_data) +{ + const char *network_path; + struct connman_network *network; + struct connman_device *device; + DBusMessageIter iter; + + DBG(""); + + if (dbus_message_iter_init(message, &iter) == FALSE) + return TRUE; + + dbus_message_iter_get_basic(&iter, &network_path); + + network = g_hash_table_lookup(bluetooth_networks, network_path); + if (network == NULL) + return TRUE; + + device = connman_network_get_device(network); + if (device == NULL) + return TRUE; + + g_hash_table_remove(bluetooth_networks, network_path); + + return TRUE; +} + +static gboolean device_changed(DBusConnection *connection, + DBusMessage *message, void *user_data) +{ + const char *path = dbus_message_get_path(message); + DBusMessageIter iter, value; + const char *key; + + DBG("path %s", path); + + if (dbus_message_iter_init(message, &iter) == FALSE) + return TRUE; + + dbus_message_iter_get_basic(&iter, &key); + + dbus_message_iter_next(&iter); + dbus_message_iter_recurse(&iter, &value); + + DBG("key %s", key); + + if (g_str_equal(key, "UUIDs") == TRUE) + add_network(NULL, path); + + return TRUE; +} + +static void remove_device_networks(struct connman_device *device) +{ + GHashTableIter iter; + gpointer key, value; + GSList *key_list = NULL; + GSList *list; + + if (bluetooth_networks == NULL) + return; + + g_hash_table_iter_init(&iter, bluetooth_networks); + + while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) { + struct connman_network *network = value; + + if (connman_network_get_device(network) != device) + continue; + + key_list = g_slist_append(key_list, key); + } + + for (list = key_list; list != NULL; list = list->next) { + const char *network_path = list->data; + + g_hash_table_remove(bluetooth_networks, network_path); + } + + g_slist_free(key_list); +} + static void adapter_properties_reply(DBusPendingCall *call, void *user_data) { char *path = user_data; @@ -603,6 +653,9 @@ static void adapter_properties_reply(DBusPendingCall *call, void *user_data) if (address == NULL) goto done; + if (g_strcmp0(address, "00:00:00:00:00:00") == 0) + goto done; + device = g_hash_table_lookup(bluetooth_devices, path); if (device != NULL) goto update; @@ -623,8 +676,6 @@ static void adapter_properties_reply(DBusPendingCall *call, void *user_data) connman_device_set_ident(device, ident); - connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE); - connman_device_set_string(device, "Path", path); if (connman_device_register(device) < 0) { @@ -644,6 +695,8 @@ update: if (powered == TRUE) check_networks(device, &networks); + else + remove_device_networks(device); done: dbus_message_unref(reply); @@ -725,6 +778,12 @@ static void list_adapters_reply(DBusPendingCall *call, void *user_data) dbus_error_init(&error); + if (dbus_set_error_from_message(&error, reply) == TRUE) { + connman_error("%s", error.message); + dbus_error_free(&error); + goto done; + } + if (dbus_message_get_args(reply, &error, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &adapters, &num_adapters, @@ -754,10 +813,26 @@ static void unregister_device(gpointer data) DBG(""); + remove_device_networks(device); + connman_device_unregister(device); connman_device_unref(device); } +static void remove_network(gpointer data) +{ + struct connman_network *network = data; + struct connman_device *device; + + DBG("network %p", network); + + device = connman_network_get_device(network); + if (device != NULL) + connman_device_remove_network(device, network); + + connman_network_unref(network); +} + static void bluetooth_connect(DBusConnection *connection, void *user_data) { DBusMessage *message; @@ -768,6 +843,9 @@ static void bluetooth_connect(DBusConnection *connection, void *user_data) bluetooth_devices = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, unregister_device); + bluetooth_networks = g_hash_table_new_full(g_str_hash, g_str_equal, + g_free, remove_network); + message = dbus_message_new_method_call(BLUEZ_SERVICE, "/", BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS); if (message == NULL) @@ -799,14 +877,311 @@ static void bluetooth_disconnect(DBusConnection *connection, void *user_data) if (bluetooth_devices == NULL) return; + g_hash_table_destroy(bluetooth_networks); + bluetooth_networks = NULL; g_hash_table_destroy(bluetooth_devices); bluetooth_devices = NULL; } +static int bluetooth_probe(struct connman_device *device) +{ + GHashTableIter iter; + gpointer key, value; + + DBG("device %p", device); + + if (bluetooth_devices == NULL) + return -ENOTSUP; + + g_hash_table_iter_init(&iter, bluetooth_devices); + + while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) { + struct connman_device *device_pan = value; + + if (device == device_pan) + return 0; + } + + return -ENOTSUP; +} + +static void bluetooth_remove(struct connman_device *device) +{ + DBG("device %p", device); +} + +static void powered_reply(DBusPendingCall *call, void *user_data) +{ + DBusError error; + DBusMessage *reply; + + DBG(""); + + reply = dbus_pending_call_steal_reply(call); + + dbus_error_init(&error); + + if (dbus_set_error_from_message(&error, reply) == TRUE) { + connman_error("%s", error.message); + dbus_error_free(&error); + dbus_message_unref(reply); + dbus_pending_call_unref(call); + return; + } + + dbus_message_unref(reply); + dbus_pending_call_unref(call); + + add_adapter(connection, user_data); +} + +static int change_powered(DBusConnection *connection, const char *path, + dbus_bool_t powered) +{ + DBusMessage *message; + DBusMessageIter iter; + DBusPendingCall *call; + + DBG(""); + + if (path == NULL) + return -EINVAL; + + message = dbus_message_new_method_call(BLUEZ_SERVICE, path, + BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY); + if (message == NULL) + return -ENOMEM; + + dbus_message_set_auto_start(message, FALSE); + + dbus_message_iter_init_append(message, &iter); + connman_dbus_property_append_basic(&iter, "Powered", + DBUS_TYPE_BOOLEAN, &powered); + + if (dbus_connection_send_with_reply(connection, message, + &call, TIMEOUT) == FALSE) { + connman_error("Failed to change Powered property"); + dbus_message_unref(message); + return -EINVAL; + } + + if (call == NULL) { + connman_error("D-Bus connection not available"); + dbus_message_unref(message); + return -EINVAL; + } + + dbus_pending_call_set_notify(call, powered_reply, + g_strdup(path), g_free); + + dbus_message_unref(message); + + return -EINPROGRESS; +} + +static int bluetooth_enable(struct connman_device *device) +{ + const char *path = connman_device_get_string(device, "Path"); + + DBG("device %p", device); + + return change_powered(connection, path, TRUE); +} + +static int bluetooth_disable(struct connman_device *device) +{ + const char *path = connman_device_get_string(device, "Path"); + + DBG("device %p", device); + + return change_powered(connection, path, FALSE); +} + +static struct connman_device_driver bluetooth_driver = { + .name = "bluetooth", + .type = CONNMAN_DEVICE_TYPE_BLUETOOTH, + .probe = bluetooth_probe, + .remove = bluetooth_remove, + .enable = bluetooth_enable, + .disable = bluetooth_disable, +}; + +static int tech_probe(struct connman_technology *technology) +{ + return 0; +} + +static void tech_remove(struct connman_technology *technology) +{ +} + +static void server_register_reply(DBusPendingCall *call, void *user_data) +{ + struct connman_technology *technology = user_data; + DBusError error; + DBusMessage *reply; + + DBG(""); + + reply = dbus_pending_call_steal_reply(call); + + dbus_error_init(&error); + + if (dbus_set_error_from_message(&error, reply) == TRUE) { + connman_error("%s", error.message); + dbus_error_free(&error); + dbus_message_unref(reply); + dbus_pending_call_unref(call); + return; + } + + dbus_message_unref(reply); + dbus_pending_call_unref(call); + + connman_technology_tethering_notify(technology, TRUE); +} + +static void server_unregister_reply(DBusPendingCall *call, void *user_data) +{ + struct connman_technology *technology = user_data; + DBusError error; + DBusMessage *reply; + + DBG(""); + + reply = dbus_pending_call_steal_reply(call); + + dbus_error_init(&error); + + if (dbus_set_error_from_message(&error, reply) == TRUE) { + connman_error("%s", error.message); + dbus_error_free(&error); + dbus_message_unref(reply); + dbus_pending_call_unref(call); + return; + } + + dbus_message_unref(reply); + dbus_pending_call_unref(call); + + connman_technology_tethering_notify(technology, FALSE); +} + + +static void server_register(const char *path, const char *uuid, + struct connman_technology *technology, + const char *bridge, connman_bool_t enabled) +{ + DBusMessage *message; + DBusPendingCall *call; + char *command; + + DBG("path %s enabled %d", path, enabled); + + command = enabled ? REGISTER : UNREGISTER; + + message = dbus_message_new_method_call(BLUEZ_SERVICE, path, + BLUEZ_NETWORK_SERVER, command); + if (message == NULL) + return; + + dbus_message_set_auto_start(message, FALSE); + + dbus_message_append_args(message, DBUS_TYPE_STRING, &uuid, + DBUS_TYPE_INVALID); + + if (enabled == TRUE) + dbus_message_append_args(message, DBUS_TYPE_STRING, &bridge, + DBUS_TYPE_INVALID); + + if (dbus_connection_send_with_reply(connection, message, + &call, TIMEOUT) == FALSE) { + connman_error("Failed to enable PAN server"); + dbus_message_unref(message); + return; + } + + if (call == NULL) { + connman_error("D-Bus connection not available"); + dbus_message_unref(message); + return; + } + + if (enabled == TRUE) + dbus_pending_call_set_notify(call, server_register_reply, + technology, NULL); + else + dbus_pending_call_set_notify(call, server_unregister_reply, + technology, NULL); + + dbus_message_unref(message); +} + +struct tethering_info { + struct connman_technology *technology; + const char *bridge; +}; + +static void enable_nap(gpointer key, gpointer value, gpointer user_data) +{ + struct tethering_info *info = user_data; + struct connman_device *device = value; + const char *path; + + DBG(""); + + path = connman_device_get_string(device, "Path"); + + server_register(path, "nap", info->technology, info->bridge, TRUE); +} + +static void disable_nap(gpointer key, gpointer value, gpointer user_data) +{ + struct tethering_info *info = user_data; + struct connman_device *device = value; + const char *path; + + DBG(""); + + path = connman_device_get_string(device, "Path"); + + server_register(path, "nap", info->technology, info->bridge, FALSE); +} + +static int tech_set_tethering(struct connman_technology *technology, + const char *identifier, const char *passphrase, + const char *bridge, connman_bool_t enabled) +{ + struct tethering_info info = { + .technology = technology, + .bridge = bridge, + }; + + DBG("bridge %s", bridge); + + if (enabled) + g_hash_table_foreach(bluetooth_devices, enable_nap, &info); + else + g_hash_table_foreach(bluetooth_devices, disable_nap, &info); + + return 0; +} + +static struct connman_technology_driver tech_driver = { + .name = "bluetooth", + .type = CONNMAN_SERVICE_TYPE_BLUETOOTH, + .probe = tech_probe, + .remove = tech_remove, + .set_tethering = tech_set_tethering, +}; + static guint watch; static guint added_watch; static guint removed_watch; static guint adapter_watch; +static guint device_watch; +static guint device_removed_watch; +static guint network_watch; static int bluetooth_init(void) { @@ -830,12 +1205,29 @@ static int bluetooth_init(void) NULL, NULL); adapter_watch = g_dbus_add_signal_watch(connection, NULL, NULL, - BLUEZ_MANAGER_INTERFACE, + BLUEZ_ADAPTER_INTERFACE, PROPERTY_CHANGED, adapter_changed, NULL, NULL); + device_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL, + BLUEZ_ADAPTER_INTERFACE, + DEVICE_REMOVED, device_removed, + NULL, NULL); + + device_watch = g_dbus_add_signal_watch(connection, NULL, NULL, + BLUEZ_DEVICE_INTERFACE, + PROPERTY_CHANGED, device_changed, + NULL, NULL); + + network_watch = g_dbus_add_signal_watch(connection, NULL, NULL, + BLUEZ_NETWORK_INTERFACE, + PROPERTY_CHANGED, network_changed, + NULL, NULL); + if (watch == 0 || added_watch == 0 || removed_watch == 0 - || adapter_watch == 0) { + || adapter_watch == 0 || network_watch == 0 + || device_watch == 0 + || device_removed_watch == 0) { err = -EIO; goto remove; } @@ -850,6 +1242,13 @@ static int bluetooth_init(void) goto remove; } + err = connman_technology_driver_register(&tech_driver); + if (err < 0) { + connman_device_driver_unregister(&bluetooth_driver); + connman_network_driver_unregister(&pan_driver); + goto remove; + } + return 0; remove: @@ -857,6 +1256,9 @@ remove: g_dbus_remove_watch(connection, added_watch); g_dbus_remove_watch(connection, removed_watch); g_dbus_remove_watch(connection, adapter_watch); + g_dbus_remove_watch(connection, device_removed_watch); + g_dbus_remove_watch(connection, device_watch); + g_dbus_remove_watch(connection, network_watch); dbus_connection_unref(connection); @@ -869,9 +1271,14 @@ static void bluetooth_exit(void) g_dbus_remove_watch(connection, added_watch); g_dbus_remove_watch(connection, removed_watch); g_dbus_remove_watch(connection, adapter_watch); + g_dbus_remove_watch(connection, device_removed_watch); + g_dbus_remove_watch(connection, device_watch); + g_dbus_remove_watch(connection, network_watch); bluetooth_disconnect(connection, NULL); + connman_technology_driver_unregister(&tech_driver); + connman_device_driver_unregister(&bluetooth_driver); connman_network_driver_unregister(&pan_driver);