All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 2/2] bluetooth: add Bluetooth service authorization support
  2011-01-27 20:20 [PATCH v3 0/2] bluetooth: Add bluetooth server support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
@ 2011-01-27 20:20 ` =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
  0 siblings, 0 replies; 7+ messages in thread
From: =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis @ 2011-01-27 20:20 UTC (permalink / raw)
  To: ofono

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

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

diff --git a/plugins/bluetooth.c b/plugins/bluetooth.c
index c766205..1a17441 100644
--- a/plugins/bluetooth.c
+++ b/plugins/bluetooth.c
@@ -44,6 +44,8 @@ static GHashTable *adapter_address_hash = NULL;
 static gint bluetooth_refcount;
 static GSList *server_list = NULL;
 
+#define TIMEOUT (60*1000) /* Timeout for user response (milliseconds) */
+
 struct server {
 	guint8 channel;
 	char *sdp_record;
@@ -58,6 +60,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,
@@ -489,13 +492,103 @@ 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;
+	GHashTableIter iter;
+	gpointer key, value;
 
 	bt_io_get(io, BT_IO_RFCOMM, &err, BT_IO_OPT_SOURCE, laddress,
 					BT_IO_OPT_DEST, raddress,
@@ -510,14 +603,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");
@@ -525,9 +610,39 @@ static void confirm_event(GIOChannel *io, gpointer user_data)
 	}
 
 	client_data->server = server;
+
+	g_hash_table_iter_init(&iter, adapter_address_hash);
+
+	while (g_hash_table_iter_next(&iter, &key, &value)) {
+		if (g_strcmp0(laddress, value) == 0) {
+			client_data->path = g_strdup(key);
+			DBG("adapter path : %s", client_data->path);
+			break;
+		}
+	}
+
+	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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/2] bluetooth: Add Bluetooth service authorization support
  2011-01-28  9:45 [PATCH v4 0/2] bluetooth: Add bluetooth server support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
@ 2011-01-28  9:45 ` =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
  0 siblings, 0 replies; 7+ messages in thread
From: =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis @ 2011-01-28  9:45 UTC (permalink / raw)
  To: ofono

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

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

diff --git a/plugins/bluetooth.c b/plugins/bluetooth.c
index 0aaa406..a9e49a6 100644
--- a/plugins/bluetooth.c
+++ b/plugins/bluetooth.c
@@ -44,6 +44,8 @@ static GHashTable *adapter_address_hash = NULL;
 static gint bluetooth_refcount;
 static GSList *server_list = NULL;
 
+#define TIMEOUT (60*1000) /* Timeout for user response (milliseconds) */
+
 struct server {
 	guint8 channel;
 	char *sdp_record;
@@ -58,6 +60,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,
@@ -495,13 +498,103 @@ 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;
+	GHashTableIter iter;
+	gpointer key, value;
 
 	bt_io_get(io, BT_IO_RFCOMM, &err, BT_IO_OPT_SOURCE, laddress,
 					BT_IO_OPT_DEST, raddress,
@@ -516,14 +609,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");
@@ -531,9 +616,39 @@ static void confirm_event(GIOChannel *io, gpointer user_data)
 	}
 
 	client_data->server = server;
+
+	g_hash_table_iter_init(&iter, adapter_address_hash);
+
+	while (g_hash_table_iter_next(&iter, &key, &value)) {
+		if (g_strcmp0(laddress, value) == 0) {
+			client_data->path = g_strdup(key);
+			DBG("adapter path : %s", client_data->path);
+			break;
+		}
+	}
+
+	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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v5 0/2] bluetooth: Add bluetooth server support
@ 2011-02-04 14:44 =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
  2011-02-04 14:44 ` [PATCH 1/2] " =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
  2011-02-04 14:44 ` [PATCH 2/2] bluetooth: Add Bluetooth service authorization support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
  0 siblings, 2 replies; 7+ messages in thread
From: =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis @ 2011-02-04 14:44 UTC (permalink / raw)
  To: ofono

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

It watches Bluetooth adapter property changes and adds SDP record to
listen client connection request.
It supports multiple adapters, servers and client connections.

Frédéric Danis (2):
  bluetooth: Add bluetooth server support
  bluetooth: Add Bluetooth service authorization support

 Makefile.am         |    1 +
 plugins/bluetooth.c |  386 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 plugins/bluetooth.h |    9 ++
 3 files changed, 393 insertions(+), 3 deletions(-)


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/2] bluetooth: Add bluetooth server support
  2011-02-04 14:44 [PATCH v5 0/2] bluetooth: Add bluetooth server support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
@ 2011-02-04 14:44 ` =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
  2011-02-04 14:44 ` [PATCH 2/2] bluetooth: Add Bluetooth service authorization support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
  1 sibling, 0 replies; 7+ messages in thread
From: =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis @ 2011-02-04 14:44 UTC (permalink / raw)
  To: ofono

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

---
 Makefile.am         |    1 +
 plugins/bluetooth.c |  281 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 plugins/bluetooth.h |    9 ++
 3 files changed, 288 insertions(+), 3 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 758fb10..e402de4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -321,6 +321,7 @@ builtin_sources += plugins/bluetooth.c plugins/bluetooth.h
 builtin_modules += hfp
 builtin_sources += plugins/hfp.c plugins/bluetooth.h
 
+builtin_sources += $(btio_sources)
 builtin_cflags += @BLUEZ_CFLAGS@
 builtin_libadd += @BLUEZ_LIBS@
 endif
diff --git a/plugins/bluetooth.c b/plugins/bluetooth.c
index 93dd7a1..b489dad 100644
--- a/plugins/bluetooth.c
+++ b/plugins/bluetooth.c
@@ -35,12 +35,30 @@
 
 #include <ofono/dbus.h>
 
+#include <btio.h>
 #include "bluetooth.h"
 
 static DBusConnection *connection;
 static GHashTable *uuid_hash = NULL;
 static GHashTable *adapter_address_hash = NULL;
 static gint bluetooth_refcount;
+static GSList *server_list = NULL;
+
+struct server {
+	guint8 channel;
+	char *sdp_record;
+	GIOChannel *io;
+	GHashTable *adapter_hash;
+	ConnectFunc connect_cb;
+	gpointer user_data;
+	GSList *client_list;
+};
+
+struct cb_data {
+	struct server *server;
+	char *path;
+	guint source;
+};
 
 void bluetooth_create_path(const char *dev_addr, const char *adapter_addr,
 				char *buf, int size)
@@ -409,6 +427,202 @@ done:
 	dbus_message_unref(reply);
 }
 
+static void get_adapter_properties(const char *path, const char *handle,
+						gpointer user_data)
+{
+	bluetooth_send_with_reply(path, BLUEZ_ADAPTER_INTERFACE,
+			"GetProperties", adapter_properties_cb,
+			g_strdup(path), g_free, -1, DBUS_TYPE_INVALID);
+}
+
+static void remove_record(char *path, guint handle, struct server *server)
+{
+	DBusMessage *msg;
+
+	msg = dbus_message_new_method_call(BLUEZ_SERVICE, path,
+					BLUEZ_SERVICE_INTERFACE,
+					"RemoveRecord");
+	if (msg == NULL) {
+		ofono_error("Unable to allocate D-Bus RemoveRecord message");
+		return;
+	}
+
+	dbus_message_append_args(msg, DBUS_TYPE_UINT32, &handle,
+					DBUS_TYPE_INVALID);
+	g_dbus_send_message(connection, msg);
+
+	ofono_info("Unregistered handle for %s, channel %d: 0x%x", path,
+			server->channel, handle);
+}
+
+static void server_stop(struct server *server)
+{
+	/* Remove all client sources related to server */
+	while (server->client_list) {
+		g_source_remove(GPOINTER_TO_UINT(server->client_list->data));
+		server->client_list = g_slist_remove(server->client_list,
+						server->client_list->data);
+	}
+
+	g_hash_table_foreach_remove(server->adapter_hash,
+					(GHRFunc) remove_record, server);
+
+	if (server->io != NULL) {
+		g_io_channel_shutdown(server->io, TRUE, NULL);
+		g_io_channel_unref(server->io);
+		server->io = NULL;
+	}
+}
+
+static void cb_data_destroy(gpointer data)
+{
+	struct cb_data *cb_data = data;
+
+	if (cb_data->path != NULL)
+		g_free(cb_data->path);
+	g_free(cb_data);
+}
+
+static gboolean client_event(GIOChannel *chan, GIOCondition cond, gpointer data)
+{
+	struct cb_data *cb_data = data;
+	struct server *server = cb_data->server;
+
+	server->client_list = g_slist_remove(server->client_list,
+					GUINT_TO_POINTER(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;
+	GError *err = NULL;
+	char laddress[18], raddress[18];
+	guint8 channel;
+
+	bt_io_get(io, BT_IO_RFCOMM, &err, BT_IO_OPT_SOURCE, laddress,
+					BT_IO_OPT_DEST, raddress,
+					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 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");
+		return;
+	}
+
+	client_data->server = server;
+	client_data->source = g_io_add_watch(io,
+					G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+					client_event, client_data);
+	server->client_list = g_slist_prepend(server->client_list,
+					GUINT_TO_POINTER(client_data->source));
+}
+
+static void add_record_cb(DBusPendingCall *call, gpointer user_data)
+{
+	struct cb_data *cb_data = user_data;
+	DBusMessage *reply = dbus_pending_call_steal_reply(call);
+	DBusError derr;
+	guint32 handle;
+
+	dbus_error_init(&derr);
+
+	if (dbus_set_error_from_message(&derr, reply)) {
+		ofono_error("Replied with an error: %s, %s",
+					derr.name, derr.message);
+		dbus_error_free(&derr);
+		g_free(cb_data->path);
+		goto done;
+	}
+
+	dbus_message_get_args(reply, NULL, DBUS_TYPE_UINT32, &handle,
+					DBUS_TYPE_INVALID);
+
+	g_hash_table_insert(cb_data->server->adapter_hash, cb_data->path,
+				GUINT_TO_POINTER(handle));
+
+	ofono_info("Registered handle for %s, channel %d: 0x%x", cb_data->path,
+			cb_data->server->channel, handle);
+
+done:
+	/* Do not free cb_data->path, it is used in adapter_hash */
+	g_free(cb_data);
+	dbus_message_unref(reply);
+}
+
+static void server_add_record(const char *path, const char *handle,
+				struct server *server)
+{
+	struct cb_data *cb_data;
+
+	cb_data = g_try_new0(struct cb_data, 1);
+	if (cb_data == NULL) {
+		ofono_error("Unable to allocate cb_data structure");
+		return;
+	}
+
+	cb_data->server = server;
+	cb_data->path = g_strdup(path);
+
+	bluetooth_send_with_reply(path, BLUEZ_SERVICE_INTERFACE, "AddRecord",
+				add_record_cb, cb_data, NULL, -1,
+				DBUS_TYPE_STRING, &server->sdp_record,
+				DBUS_TYPE_INVALID);
+}
+
+static void server_start(struct server *server, char *path)
+{
+	GError *err = NULL;
+
+	if (server->io != NULL)
+		goto out;
+
+	server->io = bt_io_listen(BT_IO_RFCOMM, NULL, confirm_event,
+					server, NULL, &err,
+					BT_IO_OPT_CHANNEL, server->channel,
+					BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
+					BT_IO_OPT_INVALID);
+	if (server->io == NULL) {
+		ofono_error("Bluetooth channel %d register failed: %s",
+					server->channel, err->message);
+		g_error_free(err);
+		server_stop(server);
+		return;
+	}
+
+out:
+	if (server->sdp_record == NULL)
+		return;
+
+	if (path != NULL)
+		server_add_record(path, NULL, server);
+	else
+		g_hash_table_foreach(adapter_address_hash,
+					(GHFunc) server_add_record, server);
+
+}
+
 static gboolean adapter_added(DBusConnection *connection, DBusMessage *message,
 				void *user_data)
 {
@@ -422,6 +636,10 @@ static gboolean adapter_added(DBusConnection *connection, DBusMessage *message,
 			"GetProperties", adapter_properties_cb, g_strdup(path),
 			g_free, -1, DBUS_TYPE_INVALID);
 
+	if (server_list)
+		g_slist_foreach(server_list, (GFunc) server_start,
+				(gpointer) path);
+
 	return TRUE;
 }
 
@@ -429,11 +647,19 @@ static gboolean adapter_removed(DBusConnection *connection,
 				DBusMessage *message, void *user_data)
 {
 	const char *path;
+	GSList *l;
 
 	if (dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
 				DBUS_TYPE_INVALID) == TRUE)
 		g_hash_table_remove(adapter_address_hash, path);
 
+	for (l = server_list; l; l = l->next) {
+		struct server *server = l->data;
+
+		/* Handle have already been removed, so removing related path */
+		g_hash_table_remove(server->adapter_hash, path);
+	}
+
 	return TRUE;
 }
 
@@ -460,6 +686,10 @@ static void parse_adapters(DBusMessageIter *array, gpointer user_data)
 				"GetProperties", adapter_properties_cb,
 				g_strdup(path), g_free, -1, DBUS_TYPE_INVALID);
 
+		if (server_list)
+			g_slist_foreach(server_list, (GFunc) server_start,
+					(gpointer) path);
+
 		dbus_message_iter_next(&value);
 	}
 }
@@ -541,6 +771,10 @@ static void bluetooth_ref(void)
 	adapter_address_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
 						g_free, g_free);
 
+	bluetooth_send_with_reply("/", BLUEZ_MANAGER_INTERFACE, "GetProperties",
+				manager_properties_cb, NULL, NULL, -1,
+				DBUS_TYPE_INVALID);
+
 increment:
 	g_atomic_int_inc(&bluetooth_refcount);
 
@@ -576,9 +810,8 @@ int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
 
 	g_hash_table_insert(uuid_hash, g_strdup(uuid), profile);
 
-	bluetooth_send_with_reply("/", BLUEZ_MANAGER_INTERFACE, "GetProperties",
-				manager_properties_cb, NULL, NULL, -1,
-				DBUS_TYPE_INVALID);
+	g_hash_table_foreach(adapter_address_hash,
+				(GHFunc) get_adapter_properties, NULL);
 
 	return 0;
 }
@@ -590,5 +823,47 @@ void bluetooth_unregister_uuid(const char *uuid)
 	bluetooth_unref();
 }
 
+struct server *bluetooth_register_server(guint8 channel, const char *sdp_record,
+					ConnectFunc cb, gpointer user_data)
+{
+	struct server *server;
+
+	server = g_try_new0(struct server, 1);
+	if (!server)
+		return NULL;
+
+	bluetooth_ref();
+
+	if (bluetooth_refcount == 0) {
+		g_free(server);
+		return NULL;
+	}
+
+	server->channel = channel;
+	if (sdp_record != NULL)
+		server->sdp_record = g_strdup(sdp_record);
+	server->connect_cb = cb;
+	server->user_data = user_data;
+	server->adapter_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
+						g_free, NULL);
+
+	server_start(server, NULL);
+
+	server_list = g_slist_prepend(server_list, server);
+
+	return server;
+}
+
+void bluetooth_unregister_server(struct server *server)
+{
+	server_list = g_slist_remove(server_list, server);
+	server_stop(server);
+	g_hash_table_destroy(server->adapter_hash);
+	g_free(server->sdp_record);
+	g_free(server);
+
+	bluetooth_unref();
+}
+
 OFONO_PLUGIN_DEFINE(bluetooth, "Bluetooth Utils Plugins", VERSION,
 			OFONO_PLUGIN_PRIORITY_DEFAULT, NULL, NULL)
diff --git a/plugins/bluetooth.h b/plugins/bluetooth.h
index 42b0d13..505d908 100644
--- a/plugins/bluetooth.h
+++ b/plugins/bluetooth.h
@@ -23,6 +23,7 @@
 #define	BLUEZ_MANAGER_INTERFACE		BLUEZ_SERVICE ".Manager"
 #define	BLUEZ_ADAPTER_INTERFACE		BLUEZ_SERVICE ".Adapter"
 #define	BLUEZ_DEVICE_INTERFACE		BLUEZ_SERVICE ".Device"
+#define	BLUEZ_SERVICE_INTERFACE		BLUEZ_SERVICE ".Service"
 
 #define DBUS_TIMEOUT 15
 
@@ -39,10 +40,18 @@ struct bluetooth_profile {
 	void (*set_alias)(const char *device, const char *);
 };
 
+struct server;
+
+typedef void (*ConnectFunc)(GIOChannel *io, GError *err, gpointer user_data);
+
 int bluetooth_register_uuid(const char *uuid,
 				struct bluetooth_profile *profile);
 void bluetooth_unregister_uuid(const char *uuid);
 
+struct server *bluetooth_register_server(guint8 channel, const char *sdp_record,
+					ConnectFunc cb, gpointer user_data);
+void bluetooth_unregister_server(struct server *server);
+
 void bluetooth_create_path(const char *dev_addr, const char *adapter_addr,
 							char *buf, int size);
 
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/2] bluetooth: Add Bluetooth service authorization support
  2011-02-04 14:44 [PATCH v5 0/2] bluetooth: Add bluetooth server support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
  2011-02-04 14:44 ` [PATCH 1/2] " =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
@ 2011-02-04 14:44 ` =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
  2011-02-04 18:08   ` Gustavo F. Padovan
  1 sibling, 1 reply; 7+ messages in thread
From: =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis @ 2011-02-04 14:44 UTC (permalink / raw)
  To: ofono

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

---
 plugins/bluetooth.c |  125 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 115 insertions(+), 10 deletions(-)

diff --git a/plugins/bluetooth.c b/plugins/bluetooth.c
index b489dad..8d28b07 100644
--- a/plugins/bluetooth.c
+++ b/plugins/bluetooth.c
@@ -44,6 +44,8 @@ static GHashTable *adapter_address_hash = NULL;
 static gint bluetooth_refcount;
 static GSList *server_list = NULL;
 
+#define TIMEOUT (60) /* Timeout for user response (seconds) */
+
 struct server {
 	guint8 channel;
 	char *sdp_record;
@@ -58,6 +60,8 @@ struct cb_data {
 	struct server *server;
 	char *path;
 	guint source;
+	GIOChannel *io;
+	gboolean pending_auth;
 };
 
 void bluetooth_create_path(const char *dev_addr, const char *adapter_addr,
@@ -483,26 +487,104 @@ static void cb_data_destroy(gpointer data)
 	g_free(cb_data);
 }
 
+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");
+
+	if (msg == NULL) {
+		ofono_error("Unable to allocate D-Bus CancelAuthorization"
+				" message");
+		return;
+	}
+
+	g_dbus_send_message(connection, msg);
+}
+
 static gboolean client_event(GIOChannel *chan, GIOCondition cond, gpointer data)
 {
 	struct cb_data *cb_data = data;
 	struct server *server = cb_data->server;
 
-	server->client_list = g_slist_remove(server->client_list,
+	if (cb_data->pending_auth == TRUE) {
+		cancel_authorization(cb_data);
+
+		cb_data->pending_auth = FALSE;
+	} else {
+		server->client_list = g_slist_remove(server->client_list,
 					GUINT_TO_POINTER(cb_data->source));
 
-	cb_data_destroy(cb_data);
+		cb_data_destroy(cb_data);
+	}
 
 	return FALSE;
 }
 
+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);
+
+	cb_data->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(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;
+	}
+
+	return;
+
+failed:
+	g_source_remove(cb_data->source);
+	server->client_list = g_slist_remove(server->client_list,
+					GUINT_TO_POINTER(cb_data->source));
+
+	cb_data_destroy(cb_data);
+}
+
 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;
+	GHashTableIter iter;
+	gpointer key, value;
 
 	bt_io_get(io, BT_IO_RFCOMM, &err, BT_IO_OPT_SOURCE, laddress,
 					BT_IO_OPT_DEST, raddress,
@@ -517,14 +599,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");
@@ -532,11 +606,42 @@ static void confirm_event(GIOChannel *io, gpointer user_data)
 	}
 
 	client_data->server = server;
+
+	g_hash_table_iter_init(&iter, adapter_address_hash);
+
+	while (g_hash_table_iter_next(&iter, &key, &value)) {
+		if (g_strcmp0(laddress, value) == 0) {
+			client_data->path = g_strdup(key);
+			DBG("adapter path : %s", client_data->path);
+			break;
+		}
+	}
+
+	client_data->io = io;
+
+	handle = GPOINTER_TO_UINT(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);
 	server->client_list = g_slist_prepend(server->client_list,
 					GUINT_TO_POINTER(client_data->source));
+	client_data->pending_auth = TRUE;
 }
 
 static void add_record_cb(DBusPendingCall *call, gpointer user_data)
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] bluetooth: Add Bluetooth service authorization support
  2011-02-04 14:44 ` [PATCH 2/2] bluetooth: Add Bluetooth service authorization support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
@ 2011-02-04 18:08   ` Gustavo F. Padovan
  2011-02-07 16:06     ` Frederic Danis
  0 siblings, 1 reply; 7+ messages in thread
From: Gustavo F. Padovan @ 2011-02-04 18:08 UTC (permalink / raw)
  To: ofono

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

Hi Frédéric,

* Frédéric Danis <frederic.danis@linux.intel.com> [2011-02-04 15:44:04 +0100]:

> ---
>  plugins/bluetooth.c |  125 ++++++++++++++++++++++++++++++++++++++++++++++----
>  1 files changed, 115 insertions(+), 10 deletions(-)
> 
> diff --git a/plugins/bluetooth.c b/plugins/bluetooth.c
> index b489dad..8d28b07 100644
> --- a/plugins/bluetooth.c
> +++ b/plugins/bluetooth.c
> @@ -44,6 +44,8 @@ static GHashTable *adapter_address_hash = NULL;
>  static gint bluetooth_refcount;
>  static GSList *server_list = NULL;
>  
> +#define TIMEOUT (60) /* Timeout for user response (seconds) */
> +
>  struct server {
>  	guint8 channel;
>  	char *sdp_record;
> @@ -58,6 +60,8 @@ struct cb_data {
>  	struct server *server;
>  	char *path;
>  	guint source;
> +	GIOChannel *io;
> +	gboolean pending_auth;
>  };
>  
>  void bluetooth_create_path(const char *dev_addr, const char *adapter_addr,
> @@ -483,26 +487,104 @@ static void cb_data_destroy(gpointer data)
>  	g_free(cb_data);
>  }
>  
> +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");
> +
> +	if (msg == NULL) {
> +		ofono_error("Unable to allocate D-Bus CancelAuthorization"
> +				" message");
> +		return;
> +	}
> +
> +	g_dbus_send_message(connection, msg);
> +}
> +
>  static gboolean client_event(GIOChannel *chan, GIOCondition cond, gpointer data)
>  {
>  	struct cb_data *cb_data = data;
>  	struct server *server = cb_data->server;
>  
> -	server->client_list = g_slist_remove(server->client_list,
> +	if (cb_data->pending_auth == TRUE) {
> +		cancel_authorization(cb_data);
> +
> +		cb_data->pending_auth = FALSE;
> +	} else {
> +		server->client_list = g_slist_remove(server->client_list,
>  					GUINT_TO_POINTER(cb_data->source));
>  
> -	cb_data_destroy(cb_data);
> +		cb_data_destroy(cb_data);
> +	}

Don't you have to call g_slist_remove and cb_data_destroy for both cases? when
pending_auth is TRUE or FALSE.

-- 
Gustavo F. Padovan
http://profusion.mobi

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] bluetooth: Add Bluetooth service authorization support
  2011-02-04 18:08   ` Gustavo F. Padovan
@ 2011-02-07 16:06     ` Frederic Danis
  0 siblings, 0 replies; 7+ messages in thread
From: Frederic Danis @ 2011-02-07 16:06 UTC (permalink / raw)
  To: ofono

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

Hello Gustavo,

Le 04/02/2011 19:08, Gustavo F. Padovan a écrit :
> Hi Frédéric,
>
> * Frédéric Danis<frederic.danis@linux.intel.com>  [2011-02-04 15:44:04 +0100]:
>
>> ---
>>   plugins/bluetooth.c |  125 ++++++++++++++++++++++++++++++++++++++++++++++----
>>   1 files changed, 115 insertions(+), 10 deletions(-)
>>
>> diff --git a/plugins/bluetooth.c b/plugins/bluetooth.c
>> index b489dad..8d28b07 100644
>> --- a/plugins/bluetooth.c
>> +++ b/plugins/bluetooth.c
>> @@ -44,6 +44,8 @@ static GHashTable *adapter_address_hash = NULL;
>>   static gint bluetooth_refcount;
>>   static GSList *server_list = NULL;
>>
>> +#define TIMEOUT (60) /* Timeout for user response (seconds) */
>> +
>>   struct server {
>>   	guint8 channel;
>>   	char *sdp_record;
>> @@ -58,6 +60,8 @@ struct cb_data {
>>   	struct server *server;
>>   	char *path;
>>   	guint source;
>> +	GIOChannel *io;
>> +	gboolean pending_auth;
>>   };
>>
>>   void bluetooth_create_path(const char *dev_addr, const char *adapter_addr,
>> @@ -483,26 +487,104 @@ static void cb_data_destroy(gpointer data)
>>   	g_free(cb_data);
>>   }
>>
>> +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");
>> +
>> +	if (msg == NULL) {
>> +		ofono_error("Unable to allocate D-Bus CancelAuthorization"
>> +				" message");
>> +		return;
>> +	}
>> +
>> +	g_dbus_send_message(connection, msg);
>> +}
>> +
>>   static gboolean client_event(GIOChannel *chan, GIOCondition cond, gpointer data)
>>   {
>>   	struct cb_data *cb_data = data;
>>   	struct server *server = cb_data->server;
>>
>> -	server->client_list = g_slist_remove(server->client_list,
>> +	if (cb_data->pending_auth == TRUE) {
>> +		cancel_authorization(cb_data);
>> +
>> +		cb_data->pending_auth = FALSE;
>> +	} else {
>> +		server->client_list = g_slist_remove(server->client_list,
>>   					GUINT_TO_POINTER(cb_data->source));
>>
>> -	cb_data_destroy(cb_data);
>> +		cb_data_destroy(cb_data);
>> +	}
>
> Don't you have to call g_slist_remove and cb_data_destroy for both cases? when
> pending_auth is TRUE or FALSE.

In case of socket disconnection during authorization phase (if user do 
not perform any action in authorization dialog), if g_slist_remove and 
cb_data_destroy are called here we will get a crash when auth_cb will be 
called with an error (as we will try to call both functions on already 
freed memory).
This is why I let auth_cb in charge of removing source from the list and 
freeing memory during authorization phase.

Regards

Fred

-- 
Frederic Danis                            Open Source Technology Centre
frederic.danis(a)intel.com                              Intel Corporation


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-02-07 16:06 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-02-04 14:44 [PATCH v5 0/2] bluetooth: Add bluetooth server support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
2011-02-04 14:44 ` [PATCH 1/2] " =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
2011-02-04 14:44 ` [PATCH 2/2] bluetooth: Add Bluetooth service authorization support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
2011-02-04 18:08   ` Gustavo F. Padovan
2011-02-07 16:06     ` Frederic Danis
  -- strict thread matches above, loose matches on Subject: below --
2011-01-28  9:45 [PATCH v4 0/2] bluetooth: Add bluetooth server support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
2011-01-28  9:45 ` [PATCH 2/2] bluetooth: Add Bluetooth service authorization support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
2011-01-27 20:20 [PATCH v3 0/2] bluetooth: Add bluetooth server support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis
2011-01-27 20:20 ` [PATCH 2/2] bluetooth: add Bluetooth service authorization support =?unknown-8bit?q?Fr=C3=A9d=C3=A9ric?= Danis

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.