All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gustavo F. Padovan <padovan@profusion.mobi>
To: ofono@ofono.org
Subject: [PATCH 3/8] bluetooth: add Request auth code for new connections
Date: Mon, 31 Jan 2011 18:51:57 -0200	[thread overview]
Message-ID: <1296507122-10936-3-git-send-email-padovan@profusion.mobi> (raw)
In-Reply-To: <1296507122-10936-2-git-send-email-padovan@profusion.mobi>

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

Now a RFCOMM incoming connection can be made, all the pieces are here.
---
 plugins/bluetooth.c |  123 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 122 insertions(+), 1 deletions(-)

diff --git a/plugins/bluetooth.c b/plugins/bluetooth.c
index 0b5a021..f3b9140 100644
--- a/plugins/bluetooth.c
+++ b/plugins/bluetooth.c
@@ -90,6 +90,9 @@ struct server {
 	guint		handle;
 	ConnectFunc	connect_cb;
 	gpointer	user_data;
+	gboolean	pending_auth;
+	GIOChannel	*client_io;
+	guint		client_watch;
 };
 
 typedef struct {
@@ -453,10 +456,25 @@ static gboolean property_changed(DBusConnection *connection, DBusMessage *msg,
 	return TRUE;
 }
 
+static void disconnect(struct server *server)
+{
+	if (server->client_io == NULL)
+		return;
+
+	server->client_io = NULL;
+
+	if (server->client_watch > 0) {
+		g_source_remove(server->client_watch);
+		server->client_watch = 0;
+	}
+}
+
 static void server_stop(gpointer data)
 {
 	struct server *server = data;
 
+	disconnect(server);
+
 	if (server->handle > 0) {
 		DBusMessage *msg;
 
@@ -481,11 +499,114 @@ static void server_stop(gpointer data)
 	server->adapter = NULL;
 }
 
+static void cancel_authorization(struct server *server)
+{
+	DBusMessage *msg;
+
+	msg = dbus_message_new_method_call(BLUEZ_SERVICE, server->adapter,
+			BLUEZ_SERVICE_INTERFACE, "CancelAuthorization");
+	if (!msg)
+		return;
+
+	g_dbus_send_message(connection, msg);
+}
+
+static void auth_cb(DBusPendingCall *call, gpointer user_data)
+{
+	struct server *server = user_data;
+	DBusMessage *reply = dbus_pending_call_steal_reply(call);
+	DBusError derr;
+	GError *err = NULL;
+
+	dbus_error_init(&derr);
+
+	server->pending_auth = FALSE;
+
+	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(server);
+
+		dbus_error_free(&derr);
+		goto failed;
+	}
+
+	ofono_info("RequestAuthorization() succeeded");
+
+	if (!bt_io_accept(server->client_io, server->connect_cb,
+					server->user_data, NULL, &err)) {
+		ofono_error("%s", err->message);
+		g_error_free(err);
+		goto failed;
+	}
+	return;
+
+failed:
+	dbus_message_unref(reply);
+	disconnect(server);
+}
+
+static gboolean client_event(GIOChannel *io, GIOCondition cond,
+						gpointer user_data)
+{
+	struct server *server = user_data;
+
+	if (server->pending_auth == TRUE)
+		cancel_authorization(server);
+
+	server->pending_auth = FALSE;
+
+	disconnect(server);
+
+	return FALSE;
+}
+
 static void new_connection(GIOChannel *io, gpointer user_data)
 {
 	struct server *server = user_data;
+	GError *err = NULL;
+	char address[18];
+	const char *addr;
+	guint8 channel;
+	int ret;
+
+	if (server->client_watch != 0) {
+		ofono_info("Client already connected");
+		return;
+	}
+
+	bt_io_get(io, BT_IO_RFCOMM, &err, BT_IO_OPT_DEST, address,
+					BT_IO_OPT_CHANNEL, &channel,
+					BT_IO_OPT_INVALID);
+	if (err) {
+		ofono_error("%s", err->message);
+		g_error_free(err);
+		return;
+	}
+
+	ofono_info("New connection from: %s, channel %u", address, channel);
+
+	addr = address;
+	ret = bluetooth_send_with_reply(server->adapter,
+					BLUEZ_SERVICE_INTERFACE,
+					"RequestAuthorization",
+					auth_cb, server, NULL, DBUS_TIMEOUT,
+					DBUS_TYPE_STRING, &addr,
+					DBUS_TYPE_UINT32, &server->handle,
+					DBUS_TYPE_INVALID);
+	if (ret < 0) {
+		ofono_error("RequestAuthorization() failed");
+		return;
+	}
+
+	server->client_io = io;
+	server->client_watch = g_io_add_watch(server->client_io,
+					G_IO_NVAL | G_IO_HUP | G_IO_ERR,
+					client_event, server);
 
-	DBG("%p", server);
+	server->pending_auth = TRUE;
 }
 
 static void add_record_cb(DBusPendingCall *call, gpointer user_data)
-- 
1.7.4.rc3


  reply	other threads:[~2011-01-31 20:51 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-31 20:51 [PATCH 1/8] bluetooth: add server support Gustavo F. Padovan
2011-01-31 20:51 ` [PATCH 2/8] bluetooth: add support to register Bluetooth Service Gustavo F. Padovan
2011-01-31 20:51   ` Gustavo F. Padovan [this message]
2011-01-31 20:51     ` [PATCH 4/8] include: add public headed to emulator atom Gustavo F. Padovan
2011-01-31 20:51       ` [PATCH 5/8] emulator: Add emulator atom in oFono Gustavo F. Padovan
2011-01-31 20:52         ` [PATCH 6/8] dun_gw: Add DUN server plugin for oFono Gustavo F. Padovan
2011-01-31 20:52           ` [PATCH 7/8] emulator: Implement dialing up for DUN Gustavo F. Padovan
2011-01-31 20:52             ` [PATCH 8/8] gsmdial: add option for Bluetooth DUN dialing Gustavo F. Padovan
2011-02-01 14:25   ` [PATCH 2/8] bluetooth: add support to register Bluetooth Service Frederic Danis
2011-02-01 14:17 ` [PATCH 1/8] bluetooth: add server support Frederic Danis
2011-02-02 18:21   ` Gustavo F. Padovan
2011-02-03 15:56     ` Frederic Danis
2011-02-03 16:30       ` Gustavo F. Padovan

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=1296507122-10936-3-git-send-email-padovan@profusion.mobi \
    --to=padovan@profusion.mobi \
    --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.