OSDN Git Service

gobex: Add support for returning -EAGAIN from producer callback
authorJohan Hedberg <johan.hedberg@intel.com>
Tue, 12 Jul 2011 08:52:05 +0000 (11:52 +0300)
committerMarcel Holtmann <marcel@holtmann.org>
Tue, 4 Dec 2012 21:22:02 +0000 (22:22 +0100)
gobex/gobex-transfer.c
gobex/gobex.c
unit/test-gobex-packet.c
unit/test-gobex-transfer.c

index a23442e..38a0b86 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 #include <string.h>
+#include <errno.h>
 
 #include "gobex.h"
 
@@ -99,6 +100,9 @@ static gssize put_get_data(void *buf, gsize len, gpointer user_data)
        if (ret >= 0)
                return ret;
 
+       if (ret == -EAGAIN)
+               return ret;
+
        req = g_obex_packet_new(G_OBEX_OP_ABORT, TRUE, G_OBEX_HDR_INVALID);
        transfer->req_id = g_obex_send_req(transfer->obex, req, -1,
                                                transfer_abort_response,
@@ -376,6 +380,9 @@ static gssize get_get_data(void *buf, gsize len, gpointer user_data)
        if (ret > 0)
                return ret;
 
+       if (ret == -EAGAIN)
+               return ret;
+
        if (ret == 0) {
                transfer_complete(transfer, NULL);
                return ret;
index a4dc69b..93be21f 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <unistd.h>
 #include <string.h>
+#include <errno.h>
 
 #include "gobex.h"
 
@@ -237,6 +238,12 @@ static gboolean write_data(GIOChannel *io, GIOCondition cond,
                }
 
                len = g_obex_packet_encode(p->pkt, obex->tx_buf, obex->tx_mtu);
+               if (len == -EAGAIN) {
+                       g_queue_push_head(obex->tx_queue, p);
+                       g_obex_suspend(obex);
+                       goto stop_tx;
+               }
+
                if (len < 0) {
                        pending_pkt_free(p);
                        goto done;
index 20d4804..6da974a 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <stdint.h>
 #include <string.h>
+#include <errno.h>
 
 #include <gobex/gobex-packet.h>
 
@@ -185,7 +186,7 @@ static void test_encode_on_demand(void)
 
 static gssize get_body_data_fail(void *buf, gsize len, gpointer user_data)
 {
-       return -1;
+       return -EIO;
 }
 
 static void test_encode_on_demand_fail(void)
@@ -199,7 +200,7 @@ static void test_encode_on_demand_fail(void)
 
        len = g_obex_packet_encode(pkt, buf, sizeof(buf));
 
-       g_assert_cmpint(len, ==, -1);
+       g_assert_cmpint(len, ==, -EIO);
 
        g_obex_packet_free(pkt);
 }
index 914a185..aeea846 100644 (file)
@@ -81,6 +81,31 @@ static gboolean resume_obex(gpointer user_data)
        return FALSE;
 }
 
+static gssize provide_eagain(void *buf, gsize len, gpointer user_data)
+{
+       struct test_data *d = user_data;
+
+       if (d->count > 0)
+               return 0;
+
+       if (len < sizeof(body_data)) {
+               g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
+                               "Got data request for only %zu bytes", len);
+               g_main_loop_quit(d->mainloop);
+               return -1;
+       }
+
+       if (d->provide_delay > 0) {
+               g_timeout_add(d->provide_delay, resume_obex, d->obex);
+               d->provide_delay = 0;
+               return -EAGAIN;
+       }
+
+       memcpy(buf, body_data, sizeof(body_data));
+
+       return sizeof(body_data);
+}
+
 static gssize provide_data(void *buf, gsize len, gpointer user_data)
 {
        struct test_data *d = user_data;
@@ -264,6 +289,26 @@ static void test_get_req(void)
        g_assert_no_error(d.err);
 }
 
+static void handle_get_eagain(GObex *obex, GObexPacket *req,
+                                               gpointer user_data)
+{
+       struct test_data *d = user_data;
+       guint8 op = g_obex_packet_get_operation(req, NULL);
+       guint id;
+
+       if (op != G_OBEX_OP_GET) {
+               d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
+                                       "Unexpected opcode 0x%02x", op);
+               g_main_loop_quit(d->mainloop);
+               return;
+       }
+
+       id = g_obex_get_rsp(obex, provide_eagain, transfer_complete, d,
+                                               &d->err, G_OBEX_HDR_INVALID);
+       if (id == 0)
+               g_main_loop_quit(d->mainloop);
+}
+
 static void handle_get(GObex *obex, GObexPacket *req, gpointer user_data)
 {
        struct test_data *d = user_data;
@@ -367,6 +412,49 @@ static void test_put_req_delay(void)
        g_assert_no_error(d.err);
 }
 
+static void test_put_req_eagain(void)
+{
+       GIOChannel *io;
+       GIOCondition cond;
+       guint io_id, timer_id;
+       GObex *obex;
+       struct test_data d = { 0, NULL, {
+                               { put_req_first, sizeof(put_req_first) },
+                               { put_req_last, sizeof(put_req_last) } }, {
+                               { put_rsp_first, sizeof(put_rsp_first) },
+                               { put_rsp_last, sizeof(put_rsp_last) } } };
+
+       create_endpoints(&obex, &io, SOCK_STREAM);
+       d.obex = obex;
+       d.provide_delay = 200;
+
+       cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
+       io_id = g_io_add_watch(io, cond, test_io_cb, &d);
+
+       d.mainloop = g_main_loop_new(NULL, FALSE);
+
+       timer_id = g_timeout_add_seconds(1, test_timeout, &d);
+
+       g_obex_put_req(obex, provide_eagain, transfer_complete, &d, &d.err,
+                                       G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
+                                       G_OBEX_HDR_NAME, "file.txt",
+                                       G_OBEX_HDR_INVALID);
+       g_assert_no_error(d.err);
+
+       g_main_loop_run(d.mainloop);
+
+       g_assert_cmpuint(d.count, ==, 2);
+
+       g_main_loop_unref(d.mainloop);
+
+       g_source_remove(timer_id);
+       g_io_channel_unref(io);
+       g_source_remove(io_id);
+       g_obex_unref(obex);
+
+       g_assert_no_error(d.err);
+}
+
 static void test_get_rsp_delay(void)
 {
        GIOChannel *io;
@@ -410,6 +498,50 @@ static void test_get_rsp_delay(void)
        g_assert_no_error(d.err);
 }
 
+static void test_get_rsp_eagain(void)
+{
+       GIOChannel *io;
+       GIOCondition cond;
+       guint io_id, timer_id;
+       GObex *obex;
+       struct test_data d = { 0, NULL, {
+                               { get_rsp_first, sizeof(get_rsp_first) },
+                               { get_rsp_last, sizeof(get_rsp_last) } }, {
+                               { get_req_last, sizeof(get_req_last) },
+                               { NULL, 0 } } };
+
+       create_endpoints(&obex, &io, SOCK_STREAM);
+       d.obex = obex;
+       d.provide_delay = 200;
+
+       cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
+       io_id = g_io_add_watch(io, cond, test_io_cb, &d);
+
+       d.mainloop = g_main_loop_new(NULL, FALSE);
+
+       timer_id = g_timeout_add_seconds(1, test_timeout, &d);
+
+       g_obex_add_request_function(obex, G_OBEX_OP_GET, handle_get_eagain,
+                                                                       &d);
+
+       g_io_channel_write_chars(io, (char *) get_req_first,
+                                       sizeof(get_req_first), NULL, &d.err);
+       g_assert_no_error(d.err);
+
+       g_main_loop_run(d.mainloop);
+
+       g_assert_cmpuint(d.count, ==, 1);
+
+       g_main_loop_unref(d.mainloop);
+
+       g_source_remove(timer_id);
+       g_io_channel_unref(io);
+       g_source_remove(io_id);
+       g_obex_unref(obex);
+
+       g_assert_no_error(d.err);
+}
+
 int main(int argc, char *argv[])
 {
        g_test_init(&argc, &argv, NULL);
@@ -423,6 +555,9 @@ int main(int argc, char *argv[])
        g_test_add_func("/gobex/test_put_req_delay", test_put_req_delay);
        g_test_add_func("/gobex/test_get_rsp_delay", test_get_rsp_delay);
 
+       g_test_add_func("/gobex/test_put_req_eagain", test_put_req_eagain);
+       g_test_add_func("/gobex/test_put_req_eagain", test_get_rsp_eagain);
+
        g_test_run();
 
        return 0;