Skip to contentSkip to conversation

bluetooth: Add bluetooth server support

PATCH Superseded Frédéric Danis 2 participants 2 messages Jan 21, 2011 Kbuild

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bluetooth: Add bluetooth server support

Based on patches from: Zhenhua Zhang <[email protected]>

It watches Bluetooth adapter property changes and adds SDP record to listen client connection request.
It supports multiple servers and multiple client connections for each server.
---
Authorization code will be added in next patch

 Makefile.am         |    1 +
 plugins/bluetooth.c |  247 +++++++++++++++++++++++++++++++++++++++++++++++++++
 plugins/bluetooth.h |    9 ++
 3 files changed, 257 insertions(+), 0 deletions(-)
Makefile.am +1 -0
index 9933e32..abd5d11 100644
@@ -317,6 +317,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
plugins/bluetooth.c +247 -0

299 lines not on this page · raw message

plugins/bluetooth.h +9 -0
index 42b0d13..505d908 100644
@@ -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);
 
Signature · 1 line
1.7.1
#1 lore raw

Hi Frédéric,

Quoted 8 lines · Frédéric Danis

Based on patches from: Zhenhua Zhang <[email protected]>

It watches Bluetooth adapter property changes and adds SDP record to listen client connection request.
It supports multiple servers and multiple client connections for each server.
---
Authorization code will be added in next patch

You can send all patches together, review goes faster this way. ;)

Quoted 94 lines
 Makefile.am         |    1 +
 plugins/bluetooth.c |  247 +++++++++++++++++++++++++++++++++++++++++++++++++++
 plugins/bluetooth.h |    9 ++
 3 files changed, 257 insertions(+), 0 deletions(-)
Makefile.am +1 -0
index 9933e32..abd5d11 100644
@@ -317,6 +317,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
plugins/bluetooth.c +55 -0
index 93dd7a1..56e22c6 100644
@@ -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;
+	char		*adapter;
+	guint		handle;
+	ConnectFunc	connect_cb;
+	gpointer	user_data;
+	GSList		*client_list;
+};
+
+struct client_data {
+	struct server *server;
+	guint source;
+};
 
 void bluetooth_create_path(const char *dev_addr, const char *adapter_addr,
 				char *buf, int size)
@@ -371,6 +389,161 @@ static gboolean property_changed(DBusConnection *connection, DBusMessage *msg,
 	return TRUE;
 }
 
+static void server_stop(gpointer data, gpointer user_data)
+{
+	struct server *server = data;
+	DBusMessage *msg;
+
+	/* calling g_source_remove will also remove it from client list */
+	while (server->client_list)
+		g_source_remove((guint) server->client_list->data);
+
+	if (server->handle == 0)
+		goto out;
+
+	msg = dbus_message_new_method_call(BLUEZ_SERVICE, server->adapter,
+					BLUEZ_SERVICE_INTERFACE,
+					"RemoveRecord");
+	if (msg == NULL) {
+		ofono_error("Unable to allocate D-Bus RemoveRecord message");
+		goto out;
+	}
+
+	dbus_message_append_args(msg, DBUS_TYPE_UINT32, &server->handle,
+					DBUS_TYPE_INVALID);
+	g_dbus_send_message(connection, msg);
+
+	server->handle = 0;
+
+out:
+	if (server->io != NULL) {
+		g_io_channel_shutdown(server->io, TRUE, NULL);
+		g_io_channel_unref(server->io);
+		server->io = NULL;
+	}
+
+	g_free(server->adapter);
+	server->adapter = NULL;
+}
+

plugins/bluetooth.c static gboolean property_changed(DBusConnection *connection, DBusMessage *msg, · line 438

Quoted 10 lines
plugins/bluetooth.c +10 -0
+static void client_remove(struct client_data *cl)
+{
+	cl->server->client_list = g_slist_remove(cl->server->client_list,
+						(void *) cl->source);
+	g_free(cl);
+}
+
+static gboolean client_event(GIOChannel *chan, GIOCondition cond, gpointer data)
+{
+	return FALSE;

What is that function for?

Quoted 57 lines
plugins/bluetooth.c +57 -0
+}
+
+static void confirm_event(GIOChannel *io, gpointer user_data)
+{
+	struct server *server = user_data;
+	GError *err = NULL;
+	char address[18];
+	guint8 channel;
+	struct client_data *cl;
+
+	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);
+
+	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;
+	}
+
+	cl = g_try_new0(struct client_data, 1);
+	if (cl == NULL) {
+		ofono_error("Unable to allocate new client event structure");
+		return;
+	}
+
+	cl->server = server;
+	cl->source = g_io_add_watch_full(io, G_PRIORITY_DEFAULT,
+					G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+					client_event, cl,
+					(GDestroyNotify) client_remove);
+	server->client_list = g_slist_prepend(server->client_list,
+						(void *)cl->source);
+}
+
+static void add_record_cb(DBusPendingCall *call, gpointer user_data)
+{
+	struct server *server = 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);

plugins/bluetooth.c static gboolean property_changed(DBusConnection *connection, DBusMessage *msg, · line 505

Quoted 10 lines
plugins/bluetooth.c +10 -0
+		server_stop(server, NULL);
+		goto done;
+	}
+
+	dbus_message_get_args(reply, NULL, DBUS_TYPE_UINT32, &handle,
+					DBUS_TYPE_INVALID);
+	server->handle = handle;
+
+	ofono_info("Registered handle: 0x%x for channel %d", handle,
+			server->channel);

Can we also have the adapter path or address in the ofono_info?

Quoted 45 lines
plugins/bluetooth.c +41 -0
+
+done:
+	dbus_message_unref(reply);
+}
+
+static void server_start(gpointer data, gpointer user_data)
+{
+	struct server *server = data;
+	char *path = user_data;
+	char *adapter_addr;
+	GError *err = NULL;
+
+	if (server->io != NULL)
+		return;
+
+	adapter_addr = g_hash_table_lookup(adapter_address_hash, path);
+
+	server->io = bt_io_listen(BT_IO_RFCOMM, NULL, confirm_event,
+					server, NULL, &err,
+					BT_IO_OPT_SOURCE, adapter_addr,
+					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, NULL);
+		return;
+	}
+
+	server->adapter = g_strdup(path);
+
+	if (server->sdp_record != NULL)
+		bluetooth_send_with_reply(path, BLUEZ_SERVICE_INTERFACE,
+					"AddRecord", add_record_cb,
+					server, NULL, -1,
+					DBUS_TYPE_STRING, &server->sdp_record,
+					DBUS_TYPE_INVALID);
+}
+
 static void adapter_properties_cb(DBusPendingCall *call, gpointer user_data)
 {
 	const char *path = user_data;

plugins/bluetooth.c static void adapter_properties_cb(DBusPendingCall *call, gpointer user_data) · line 573

Quoted 6 lines
plugins/bluetooth.c +3 -0
@@ -395,6 +568,9 @@ static void adapter_properties_cb(DBusPendingCall *call, gpointer user_data)
 	g_hash_table_insert(adapter_address_hash,
 				g_strdup(path), g_strdup(addr));
 
+	if (server_list)
+		g_slist_foreach(server_list, server_start, (gpointer)path);
+

I don't if you fixed that, but in the original Zhenhua patches have the server started when I plug a new dongle with oFono running is not working for me.

Quoted 8 lines
plugins/bluetooth.c +1 -0
 	for (l = device_list; l; l = l->next) {
 		const char *device = l->data;
 
@@ -429,11 +605,26 @@ static gboolean adapter_removed(DBusConnection *connection,
 				DBusMessage *message, void *user_data)
 {
 	const char *path;
+	GSList *l;

plugins/bluetooth.c static gboolean adapter_removed(DBusConnection *connection, · line 618

Quoted 10 lines
plugins/bluetooth.c +5 -0
 
 	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;
+
+		if (server->adapter == NULL)
+			continue;

I really don't know why the adapter could be NULL here.

plugins/bluetooth.c static gboolean adapter_removed(DBusConnection *connection, · line 623

Quoted 5 lines
plugins/bluetooth.c +5 -0
+
+		if (g_str_equal(path, server->adapter) == FALSE)
+			continue;
+
+		/* Don't remove handle if the adapter has been removed */

/* Handle have already been removed, so setting to 0 */ is better.

Quoted 17 lines
plugins/bluetooth.c +6 -0
+		server->handle = 0;
+		server_stop(server, NULL);
+	}
+
 	return TRUE;
 }
 
@@ -555,6 +746,8 @@ remove:
 
 static void bluetooth_unref(void)
 {
+	GSList *l;
+
 	if (g_atomic_int_dec_and_test(&bluetooth_refcount) == FALSE)
 		return;
 

plugins/bluetooth.c static void bluetooth_unref(void) · line 763

Quoted 6 lines
plugins/bluetooth.c +3 -0
@@ -565,6 +758,19 @@ static void bluetooth_unref(void)
 
 	g_hash_table_destroy(uuid_hash);
 	g_hash_table_destroy(adapter_address_hash);
+
+	if (server_list == NULL)
+		return;

Do we really need this check?

plugins/bluetooth.c static void bluetooth_unref(void) · line 769

Quoted 6 lines
plugins/bluetooth.c +6 -0
+
+	for (l = server_list; l; l = l->next) {
+		struct server *server = l->data;
+
+		server_stop(server, NULL);
+		g_free(server);

This server_stop and g_free alreadand server_list will be NULL already.server() is called, so why do you need to run this 'for' here. You can trust that the server is already freed and server_list will be NULL already.

You were leaking sdp_record here, btw.

Quoted 16 lines
plugins/bluetooth.c +9 -0
+	}
+
+	g_slist_free(server_list);
+	server_list = NULL;
 }
 
 int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
@@ -590,5 +796,46 @@ 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;
+

plugins/bluetooth.c void bluetooth_unregister_uuid(const char *uuid) · line 813

Quoted 10 lines
plugins/bluetooth.c +10 -0
+	server = g_try_new0(struct server, 1);
+	if (!server)
+		return NULL;
+
+	bluetooth_ref();
+
+	if (bluetooth_refcount == 0) {
+		g_free(server);
+		return NULL;
+	}

You can't have this check here. bluetooth_refcount can only be read/write by bluetooth_ref/unref.

Signature · 2 lines
Gustavo F. Padovan
http://profusion.mobi