All of lore.kernel.org
 help / color / mirror / Atom feed
From: =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis <frederic.danis@linux.intel.com>
To: ofono@ofono.org
Subject: [PATCH 4/4] bluetooth: add Bluetooth service authorization support
Date: Thu, 27 Jan 2011 16:05:40 +0100	[thread overview]
Message-ID: <1296140740-11486-5-git-send-email-frederic.danis@linux.intel.com> (raw)
In-Reply-To: <1296140740-11486-1-git-send-email-frederic.danis@linux.intel.com>

[-- Attachment #1: Type: text/plain, Size: 5496 bytes --]

---
 plugins/bluetooth.c |  136 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 127 insertions(+), 9 deletions(-)

diff --git a/plugins/bluetooth.c b/plugins/bluetooth.c
index 86d4c25..7e4dbc1 100644
--- a/plugins/bluetooth.c
+++ b/plugins/bluetooth.c
@@ -44,6 +44,8 @@ static GSList *adapter_list = NULL;
 static gint bluetooth_refcount;
 static GSList *server_list = NULL;
 
+#define TIMEOUT (60*1000) /* Timeout for user response (milliseconds) */
+
 struct adapter_address {
 	char *adapter;
 	char *address;
@@ -63,6 +65,7 @@ struct cb_data {
 	struct server *server;
 	char *path;
 	guint source;
+	GIOChannel *io;
 };
 
 void bluetooth_create_path(const char *dev_addr, const char *adapter_addr,
@@ -266,6 +269,14 @@ static gint adapter_compare(gconstpointer a, gconstpointer b)
 	return g_strcmp0(entry->adapter, key);
 }
 
+static gint address_compare(gconstpointer a, gconstpointer b)
+{
+	const struct adapter_address *entry = a;
+	const char *key = b;
+
+	return g_strcmp0(entry->address, key);
+}
+
 static GSList* adapter_address_add(GSList *list, const char *path,
 					const char *address)
 {
@@ -507,13 +518,102 @@ static gboolean client_event(GIOChannel *chan, GIOCondition cond, gpointer data)
 	return FALSE;
 }
 
+static void cancel_authorization(struct cb_data *user_data)
+{
+	DBusMessage *msg;
+
+	if (user_data->path == NULL)
+		return;
+
+	msg = dbus_message_new_method_call(BLUEZ_SERVICE, user_data->path,
+						BLUEZ_SERVICE_INTERFACE,
+						"CancelAuthorization");
+
+	g_dbus_send_message(connection, msg);
+}
+
+static void auth_cb(DBusPendingCall *call, gpointer user_data)
+{
+	struct cb_data *cb_data = user_data;
+	struct server *server = cb_data->server;
+
+	DBusMessage *reply = dbus_pending_call_steal_reply(call);
+	DBusError derr;
+	GError *err = NULL;
+
+	dbus_error_init(&derr);
+
+	if (dbus_set_error_from_message(&derr, reply)) {
+		ofono_error("RequestAuthorization error: %s, %s",
+				derr.name, derr.message);
+
+		if (dbus_error_has_name(&derr, DBUS_ERROR_NO_REPLY))
+			cancel_authorization(cb_data);
+
+		dbus_error_free(&derr);
+
+		dbus_message_unref(reply);
+
+		goto failed;
+	}
+
+	dbus_message_unref(reply);
+
+	ofono_info("RequestAuthorization succeeded");
+
+	if (!bt_io_accept(cb_data->io, server->connect_cb, server->user_data,
+						NULL, &err)) {
+		ofono_error("%s", err->message);
+		g_error_free(err);
+		goto failed;
+	}
+
+	g_source_remove(cb_data->source);
+	server->client_list = g_slist_remove(server->client_list,
+						(void *) cb_data->source);
+
+	cb_data->source = g_io_add_watch(cb_data->io,
+					G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+					client_event, cb_data);
+	server->client_list = g_slist_prepend(server->client_list,
+						(void *)cb_data->source);
+
+	return;
+
+failed:
+	g_source_remove(cb_data->source);
+	server->client_list = g_slist_remove(server->client_list,
+						(void *) cb_data->source);
+
+	cb_data_destroy(cb_data);
+}
+
+static gboolean auth_watch(GIOChannel *io, GIOCondition cond,
+						gpointer user_data)
+{
+	struct cb_data *cb_data = user_data;
+	struct server *server = cb_data->server;
+
+	cancel_authorization(cb_data);
+	server->client_list = g_slist_remove(server->client_list,
+						(void *) cb_data->source);
+
+	cb_data_destroy(cb_data);
+
+	return FALSE;
+}
+
 static void confirm_event(GIOChannel *io, gpointer user_data)
 {
 	struct server *server = user_data;
 	struct cb_data *client_data;
+	guint handle;
+	const char *addr;
+	int ret;
 	GError *err = NULL;
 	char laddress[18], raddress[18];
 	guint8 channel;
+	GSList *entry;
 
 	bt_io_get(io, BT_IO_RFCOMM, &err, BT_IO_OPT_SOURCE, laddress,
 					BT_IO_OPT_DEST, raddress,
@@ -528,14 +628,6 @@ static void confirm_event(GIOChannel *io, gpointer user_data)
 	ofono_info("New connection for %s on  channel %u from: %s,", laddress,
 							channel, raddress);
 
-	if (!bt_io_accept(io, server->connect_cb, server->user_data,
-						NULL, &err)) {
-		ofono_error("%s", err->message);
-		g_error_free(err);
-		g_io_channel_unref(io);
-		return;
-	}
-
 	client_data = g_try_new0(struct cb_data, 1);
 	if (client_data == NULL) {
 		ofono_error("Unable to allocate client cb_data structure");
@@ -543,9 +635,35 @@ static void confirm_event(GIOChannel *io, gpointer user_data)
 	}
 
 	client_data->server = server;
+	entry = g_slist_find_custom(adapter_list, laddress, address_compare);
+	if (entry != NULL) {
+		struct adapter_address *a = entry->data;
+
+		client_data->path = g_strdup(a->adapter);
+	}
+
+	client_data->io = io;
+
+	handle = (guint) g_hash_table_lookup(server->adapter_hash,
+						client_data->path);
+	addr = raddress;
+	ret = bluetooth_send_with_reply(client_data->path,
+					BLUEZ_SERVICE_INTERFACE,
+					"RequestAuthorization",
+					auth_cb, client_data, NULL, TIMEOUT,
+					DBUS_TYPE_STRING, &addr,
+					DBUS_TYPE_UINT32, &handle,
+					DBUS_TYPE_INVALID);
+	if (ret < 0) {
+		ofono_error("Request Bluetooth authorization failed");
+		return;
+	}
+
+	ofono_info("RequestAuthorization(%s, 0x%x)", raddress, handle);
+
 	client_data->source = g_io_add_watch(io,
 					G_IO_HUP | G_IO_ERR | G_IO_NVAL,
-					client_event, client_data);
+					auth_watch, client_data);
 	server->client_list = g_slist_prepend(server->client_list,
 						(void *)client_data->source);
 }
-- 
1.7.1


      parent reply	other threads:[~2011-01-27 15:05 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-27 15:05 [PATCH v2 0/4] bluetooth: Add bluetooth server support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
2011-01-27 15:05 ` [PATCH 1/4] bluetooth: only use bluetooth_refcount in bluetooth_ref/bluetooth_unref =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
2011-01-27 15:12   ` Marcel Holtmann
2011-01-27 15:25     ` Frederic Danis
2011-01-27 16:17       ` Gustavo F. Padovan
2011-01-27 16:18         ` Marcel Holtmann
2011-01-27 15:05 ` [PATCH 2/4] bluetooth: use GSList instead of GHashTable to store adapter path/address =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
2011-01-27 15:13   ` Marcel Holtmann
2011-01-27 15:22     ` Frederic Danis
2011-01-27 15:24       ` Marcel Holtmann
2011-01-27 15:35         ` Frederic Danis
2011-01-27 16:19           ` Gustavo F. Padovan
2011-01-27 15:05 ` [PATCH 3/4] bluetooth: Add bluetooth server support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
2011-01-27 16:26   ` Gustavo F. Padovan
2011-01-27 16:32     ` Frederic Danis
2011-01-27 15:05 ` =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1296140740-11486-5-git-send-email-frederic.danis@linux.intel.com \
    --to=frederic.danis@linux.intel.com \
    --cc=ofono@ofono.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.