Skip to contentSkip to conversation

osso-gwobex patches for review

PATCH In review Bastien Nocera 2 participants 3 messages Feb 21, 2010 – Mar 5, 2010 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 osso-gwobex patches for review

Heya,

The first patch fixes a few typos.

The second one requires the openobex patches I posted to the list, but should fail gracefully if the support does not exist.

The dependency on openobex itself should probably be upped as well.

Cheers

Quoted 1 line

From f15d528af880b639567e8c2924be50cc6a6d0cd3 Mon Sep 17 00:00:00 2001

From: Bastien Nocera <[email protected]>
Date: Fri, 19 Feb 2010 15:49:27 +0000
Subject: [PATCH 1/2] Fix a few typos

---
 src/obex-priv.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
src/obex-priv.c +2 -2
index f99740a..a43c56a 100644
@@ -221,7 +221,7 @@ static void obex_connect_done(GwObex *ctx, obex_object_t *object, int obex_rsp)
 #endif
             case OBEX_HDR_CONNECTION:
                 ctx->conid = hv.bq4;
-                debug("got Conection ID: %#x\n", hv.bq4);
+                debug("got Connection ID: %#x\n", hv.bq4);
                 break;
             default:
                 debug("Skipped header %02x\n", hi);
@@ -372,7 +372,7 @@ static void obex_readstream(GwObex *ctx, obex_object_t *object) {
 
     if (!xfer) {
         debug("Incoming data even though no xfer active!\n");
-        /* Flush incomming stream */
+        /* Flush incoming stream */
         actual = OBEX_ObjectReadStream(ctx->handle, object, &buf);
         if (actual > 0)
             debug("Ignored %d bytes\n", actual);
Signature · 1 line
1.6.6.1
Quoted 1 line

From 841d913a1b31ada32cfeb7a2f36fd0ccc820c088 Mon Sep 17 00:00:00 2001

From: Bastien Nocera <[email protected]>
Date: Sun, 21 Feb 2010 14:18:37 +0000
Subject: [PATCH 2/2] Add USB support to osso-gwobex

Pass a "usb:busid,deviceid,interface" string to
gw_obex_setup_dev() to setup a USB Obex device.

Then you can access the FTP service as normal.

---
 src/gw-obex.c   |  117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/obex-priv.c |   20 +++++++++
 src/obex-priv.h |    4 ++
 src/obex-xfer.c |    2 +-
 4 files changed, 142 insertions(+), 1 deletions(-)
src/gw-obex.c +117 -0

128 lines not on this page · raw message

src/obex-priv.c +20 -0
index a43c56a..278a0a7 100644
@@ -515,6 +515,7 @@ static void obex_writestream(GwObex *ctx, obex_object_t *object) {
 static void obex_event_handler(obex_t *handle, obex_object_t *object, int mode,
                                int event, int obex_cmd, int obex_rsp) {
     GwObex *ctx = OBEX_GetCustomData(handle);
+
     switch (event) {
         case OBEX_EV_ABORT:
             debug("OBEX_EV_ABORT\n");
@@ -630,6 +631,16 @@ gboolean gw_obex_transport_setup(int fd, obex_t **handle) {
     return TRUE;
 }
 
+gboolean gw_obex_transport_setup_usb(obex_t **handle) {
+    *handle = OBEX_Init(OBEX_TRANS_USB, obex_event_handler, 0);
+    if (*handle == NULL) {
+        debug("OBEX_Init() failed\n");
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
 void gw_obex_get_error(GwObex *ctx, gint *error) {
     if (error)
         *error = ctx->error;
@@ -728,6 +739,15 @@ GwObex *make_context(obex_t *handle) {
     return context;
 }
 
+void update_context(GwObex *ctx)
+{
+    int fd;
+
+    fd = OBEX_GetFD(ctx->handle);
+    if (fd > 0)
+        ctx->conn_fd = fd;
+}
+
 gboolean gw_obex_action_op(GwObex *ctx, const gchar *src, const gchar *dst,
                            uint8_t action) {
     gboolean ret = FALSE;
src/obex-priv.h +4 -0
index ce7748c..4991d49 100644
@@ -162,6 +162,8 @@ struct gw_obex {
 
 GwObex *make_context(obex_t *handle);
 
+void update_context(GwObex *ctx);
+
 gboolean gw_obex_set_error(GwObex *ctx);
 
 void gw_obex_get_error(GwObex *ctx, gint *error);
@@ -176,6 +178,8 @@ gboolean gw_obex_disconnect(GwObex *ctx);
 
 gboolean gw_obex_transport_setup(int fd, obex_t **handle);
 
+gboolean gw_obex_transport_setup_usb(obex_t **handle);
+
 gboolean gw_obex_action_op(GwObex *ctx, const gchar *src, const gchar *dst,
                            uint8_t action);
 
src/obex-xfer.c +1 -1
index 81ac5dc..8c7b645 100644
@@ -48,7 +48,7 @@ static gboolean handle_input(GwObex *ctx, gint *err) {
     r = OBEX_HandleInput(ctx->handle, 22);
 
     if (r < 0) {
-        debug("OBEX_HandleInput() failed\n");
+        debug("OBEX_HandleInput() failed (%d)\n", r);
         obex_link_error(ctx);
         if (err)
             *err = GW_OBEX_ERROR_INTERNAL;
Signature · 1 line
1.6.6.1
#1 lore raw

Hi Bastien,

Quoted 1 line · Bastien Nocera

The first patch fixes a few typos.

Yep, looks good and I've pushed it upstream.

Quoted 2 lines

The second one requires the openobex patches I posted to the list, but should fail gracefully if the support does not exist.

This patch has some issues:

src/gw-obex.c GwObex *gw_obex_setup_fd(int fd, const gchar *uuid, gint uuid_len, · line 384

Quoted 5 lines
src/gw-obex.c +5 -0
+    /* Setup low-level USB */
+    if (!gw_obex_transport_setup_usb(&handle)) {
+        debug("gw_obex_transport_setup_usb() failed, no USB support?\n");
+        return NULL;
+    }

You forget to set *error before returning NULL here.

src/gw-obex.c GwObex *gw_obex_setup_fd(int fd, const gchar *uuid, gint uuid_len, · line 436

Quoted 7 lines
src/gw-obex.c +7 -0
+    /* Do the transport connection */
+    if (OBEX_InterfaceConnect(handle, iface) < 0) {
+        debug("Could not connect to USB device '%s'\n", dev);
+        if (error)
+            *error = GW_OBEX_ERROR_NO_SERVICE;
+        return NULL;
+    }

This looks like a memory leak since you don't do the same cleanup of ctx->mutex and ctx as you do in the other failure cases. Maybe the function could use a "fail" label where you jump to for cleanup tasks?

Besides those issues the patch seems ok'ish.

Johan

#2 lore raw
Quoted 11 lines · Johan Hedberg

Hi Bastien,

Quoted 1 line · Bastien Nocera

The first patch fixes a few typos.

Yep, looks good and I've pushed it upstream.

Quoted 2 lines

The second one requires the openobex patches I posted to the list, but should fail gracefully if the support does not exist.

This patch has some issues:

<snip>

Quoted 1 line

Besides those issues the patch seems ok'ish.

Updated patch below for the USB support. Not tested, as I'm having some problems with recent changes in OpenObex (as I mailed recently).

Cheers

Quoted 1 line

From addf804a4865729b9185727d1f38b02ad1329cd2 Mon Sep 17 00:00:00 2001

From: Bastien Nocera <[email protected]>
Date: Sun, 21 Feb 2010 14:18:37 +0000
Subject: [PATCH] Add USB support to osso-gwobex

Pass a "usb:busid,deviceid,interface" string to
gw_obex_setup_dev() to setup a USB Obex device.

Then you can access the FTP service as normal.

---
 src/gw-obex.c   |  120 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/obex-priv.c |   20 +++++++++
 src/obex-priv.h |    4 ++
 src/obex-xfer.c |    2 +-
 4 files changed, 145 insertions(+), 1 deletions(-)
src/gw-obex.c +120 -0
index 8352782..fea270d 100644
@@ -370,11 +370,131 @@ GwObex *gw_obex_setup_fd(int fd, const gchar *uuid, gint uuid_len,
     return ctx;
 }
 
+static GwObex *gw_obex_setup_usb(const char *dev, const gchar *uuid, gint uuid_len,
+                                 GMainContext *context, gint *error) {
+    GwObex *ctx;
+    obex_t *handle;
+    obex_interface_t *iface;
+    int i, num;
+
+    /* Setup low-level USB */
+    if (!gw_obex_transport_setup_usb(&handle)) {
+        debug("gw_obex_transport_setup_usb() failed, no USB support?\n");
+        if (error)
+            *error = GW_OBEX_ERROR_NO_SERVICE;
+        return NULL;
+    }
+
+    /* Look for the requested interface */
+    num = OBEX_EnumerateInterfaces(handle);
+    if (num == 0) {
+        debug("usbobex_find_interfaces() found no USB devices\n");
+        if (error)
+            *error = GW_OBEX_ERROR_NO_SERVICE;
+        return NULL;
+    }
+
+    iface = NULL;
+    for (i = 0; i < num; i++) {
+        gchar *path;
+        obex_interface_t *tmp;
+        tmp = OBEX_GetInterfaceByIndex(handle, i);
+
+        path = g_strdup_printf("usb:%d,%d,%d", tmp->usb.bus_number,
+                               tmp->usb.device_address,
+                               tmp->usb.interface_number);
+        debug("Checking requested '%s' against found '%s'\n", dev, path);
+        /* Is that the interface we're looking for? */
+        if (g_str_equal(path, dev)) {
+            iface = tmp;
+            g_free(path);
+            break;
+        }
+        g_free(path);
+    }
+
+    if (iface == NULL) {
+        debug("Could not find matching USB interface for %s\n", dev);
+        OBEX_Cleanup(handle);
+        if (error)
+            *error = GW_OBEX_ERROR_NO_SERVICE;
+        return NULL;
+    }
+
+    /* Prepare the context */
+    ctx = make_context(handle);
+
+    if (!g_thread_supported() && !g_thread_get_initialized())
+        g_thread_init(NULL);
+    ctx->mutex = g_mutex_new();
+
+    OBEX_SetCustomData(handle, ctx);
+
+    /* Do the transport connection */
+    if (OBEX_InterfaceConnect(handle, iface) < 0) {
+        debug("Could not connect to USB device '%s'\n", dev);
+        if (error)
+            *error = GW_OBEX_ERROR_NO_SERVICE;
+        goto fail;
+    }
+
+    debug("Transport connection opened.\n");
+
+    /* Set the file descriptor for listening in */
+    update_context(ctx);
+    if (ctx->conn_fd < 0) {
+        debug("Unable to get a file descriptor for the USB connection\n");
+        if (error)
+            *error = GW_OBEX_ERROR_NO_SERVICE;
+        goto fail;
+    }
+
+    debug("New FD is %d\n", ctx->conn_fd);
+
+    /* Do the service connection */
+    debug("Connecting to OBEX service\n");
+    if (!gw_obex_connect(ctx, uuid, uuid_len)) {
+        debug("Unable to connect to OBEX service\n");
+        if (error)
+            *error = GW_OBEX_ERROR_NO_SERVICE;
+        goto fail;
+    }
+
+    debug("Connected (Connection ID: %#x)\n", ctx->conid);
+
+    /* Setup the file descriptor to get events from */
+    ctx->gio = g_io_channel_unix_new(ctx->conn_fd);
+    ctx->gio_source = g_io_create_watch (ctx->gio,
+                                         G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL);
+    g_source_set_callback(ctx->gio_source, (GSourceFunc)gw_obex_cb, ctx, NULL);
+    (void) g_source_attach(ctx->gio_source, context);
+    g_source_unref(ctx->gio_source);
+
+    ctx->main_ctx = context;
+
+    return ctx;
+
+fail:
+    if (ctx != NULL) {
+        if (ctx->mutex != NULL)
+            g_mutex_free(ctx->mutex);
+        g_free(ctx);
+    }
+    OBEX_Cleanup(handle);
+
+    return NULL;
+}
+
 GwObex *gw_obex_setup_dev(const char *dev, const gchar *uuid, gint uuid_len,
                           GMainContext *context, gint *error) {
     GwObex *ctx;
     int fd;
 
+    if (strncmp(dev, "usb:", 4) == 0) {
+        debug("Trying to set up USB device '%s'\n", dev);
+        return gw_obex_setup_usb(dev, uuid, uuid_len, context, error);
+    }
+
     fd = open(dev, O_RDWR | O_NOCTTY | O_SYNC);
     if (fd < 0) {
         debug("open(\"%s\"): %s\n", dev, strerror(errno));
src/obex-priv.c +20 -0
index a43c56a..278a0a7 100644
@@ -515,6 +515,7 @@ static void obex_writestream(GwObex *ctx, obex_object_t *object) {
 static void obex_event_handler(obex_t *handle, obex_object_t *object, int mode,
                                int event, int obex_cmd, int obex_rsp) {
     GwObex *ctx = OBEX_GetCustomData(handle);
+
     switch (event) {
         case OBEX_EV_ABORT:
             debug("OBEX_EV_ABORT\n");
@@ -630,6 +631,16 @@ gboolean gw_obex_transport_setup(int fd, obex_t **handle) {
     return TRUE;
 }
 
+gboolean gw_obex_transport_setup_usb(obex_t **handle) {
+    *handle = OBEX_Init(OBEX_TRANS_USB, obex_event_handler, 0);
+    if (*handle == NULL) {
+        debug("OBEX_Init() failed\n");
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
 void gw_obex_get_error(GwObex *ctx, gint *error) {
     if (error)
         *error = ctx->error;
@@ -728,6 +739,15 @@ GwObex *make_context(obex_t *handle) {
     return context;
 }
 
+void update_context(GwObex *ctx)
+{
+    int fd;
+
+    fd = OBEX_GetFD(ctx->handle);
+    if (fd > 0)
+        ctx->conn_fd = fd;
+}
+
 gboolean gw_obex_action_op(GwObex *ctx, const gchar *src, const gchar *dst,
                            uint8_t action) {
     gboolean ret = FALSE;
src/obex-priv.h +4 -0
index 37b20de..f8d98f7 100644
@@ -164,6 +164,8 @@ struct gw_obex {
 
 GwObex *make_context(obex_t *handle);
 
+void update_context(GwObex *ctx);
+
 gboolean gw_obex_set_error(GwObex *ctx);
 
 void gw_obex_get_error(GwObex *ctx, gint *error);
@@ -178,6 +180,8 @@ gboolean gw_obex_disconnect(GwObex *ctx);
 
 gboolean gw_obex_transport_setup(int fd, obex_t **handle);
 
+gboolean gw_obex_transport_setup_usb(obex_t **handle);
+
 gboolean gw_obex_action_op(GwObex *ctx, const gchar *src, const gchar *dst,
                            uint8_t action);
 
src/obex-xfer.c +1 -1
index 81ac5dc..8c7b645 100644
@@ -48,7 +48,7 @@ static gboolean handle_input(GwObex *ctx, gint *err) {
     r = OBEX_HandleInput(ctx->handle, 22);
 
     if (r < 0) {
-        debug("OBEX_HandleInput() failed\n");
+        debug("OBEX_HandleInput() failed (%d)\n", r);
         obex_link_error(ctx);
         if (err)
             *err = GW_OBEX_ERROR_INTERNAL;
Signature · 1 line
1.6.6.1