Re: [PATCH -v3 1/7] bluetooth: Add bluetooth server support

2011-02-08 Thread Denis Kenzior
Hi Gustavo,

On 02/08/2011 04:00 PM, Gustavo F. Padovan wrote:
> From: Frédéric Danis 
> 
> ---
>  Makefile.am |1 +
>  plugins/bluetooth.c |  247 
> ++-
>  plugins/bluetooth.h |9 ++
>  3 files changed, 254 insertions(+), 3 deletions(-)
> 

Patch has been applied, thanks.

Regards,
-Denis
___
ofono mailing list
ofono@ofono.org
http://lists.ofono.org/listinfo/ofono


[PATCH -v3 1/7] bluetooth: Add bluetooth server support

2011-02-08 Thread Gustavo F. Padovan
From: Frédéric Danis 

---
 Makefile.am |1 +
 plugins/bluetooth.c |  247 ++-
 plugins/bluetooth.h |9 ++
 3 files changed, 254 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 13f3b3b..d17e056 100644
--- a/plugins/bluetooth.c
+++ b/plugins/bluetooth.c
@@ -35,12 +35,30 @@
 
 #include 
 
+#include 
 #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)
@@ -417,6 +435,166 @@ static void get_adapter_properties(const char *path, 
const char *handle,
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 new_connection(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->so