err = g_error_new(G_OBEX_ERROR, G_OBEX_ERROR_CANCELLED,
"Request was aborted");
- transfer_complete(transfer, err);
- g_error_free(err);
-
rsp = g_obex_packet_new(G_OBEX_RSP_SUCCESS, TRUE, NULL);
g_obex_send(obex, rsp, NULL);
+
+ transfer_complete(transfer, err);
+ g_error_free(err);
}
guint g_obex_put_rsp(GObex *obex, GObexPacket *req,
return transfer->id;
}
+
+static gssize get_get_data(void *buf, gsize len, gpointer user_data)
+{
+ struct transfer *transfer = user_data;
+ GObexPacket *req;
+ GError *err = NULL;
+ gssize ret;
+
+ ret = transfer->data_producer(buf, len, transfer->user_data);
+ if (ret >= 0)
+ return ret;
+
+ req = g_obex_packet_new(G_OBEX_RSP_INTERNAL_SERVER_ERROR, TRUE, NULL);
+ g_obex_send(transfer->obex, req, NULL);
+
+ err = g_error_new(G_OBEX_ERROR, G_OBEX_ERROR_CANCELLED,
+ "Data producer function failed");
+ transfer_complete(transfer, err);
+ g_error_free(err);
+
+ return ret;
+}
+
+static void transfer_get_req(GObex *obex, GObexPacket *req, gpointer user_data)
+{
+ struct transfer *transfer = user_data;
+ GError *err = NULL;
+ GObexPacket *rsp;
+
+ rsp = g_obex_packet_new(G_OBEX_RSP_CONTINUE, TRUE, NULL);
+ g_obex_packet_add_body(req, get_get_data, transfer);
+
+ if (!g_obex_send(obex, rsp, &err)) {
+ transfer_complete(transfer, err);
+ g_error_free(err);
+ }
+}
+
+guint g_obex_get_rsp(GObex *obex, GObexPacket *req,
+ GObexDataProducer data_func, GObexFunc complete_func,
+ gpointer user_data, GError **err)
+{
+ struct transfer *transfer;
+ gint id;
+
+ transfer = transfer_new(obex, G_OBEX_OP_GET, complete_func, user_data);
+ transfer->data_producer = data_func;
+
+ transfer_put_req(obex, req, transfer);
+ if (!g_slist_find(transfers, transfer))
+ return 0;
+
+ id = g_obex_add_request_function(obex, G_OBEX_OP_GET, transfer_get_req,
+ transfer);
+ transfer->get_id = id;
+
+ id = g_obex_add_request_function(obex, G_OBEX_OP_ABORT,
+ transfer_abort_req, transfer);
+ transfer->abort_id = id;
+
+ return transfer->id;
+}
g_main_loop_quit(mainloop);
}
-static gssize put_provide_data(void *buf, gsize len, gpointer user_data)
+static gssize provide_data(void *buf, gsize len, gpointer user_data)
{
struct test_data *d = user_data;
timer_id = g_timeout_add_seconds(1, test_timeout, &d);
- g_obex_put_req(obex, "foo/bar", "file.txt", put_provide_data,
+ g_obex_put_req(obex, "foo/bar", "file.txt", provide_data,
transfer_complete, &d, &d.err);
g_assert_no_error(d.err);
g_assert_no_error(d.err);
}
+static gboolean get_cli(GIOChannel *io, GIOCondition cond, gpointer user_data)
+{
+ struct test_data *d = user_data;
+ GIOStatus status;
+ gsize bytes_written, rbytes, send_buf_len, expect_len;
+ char buf[255];
+ const char *send_buf, *expect;
+
+ d->count++;
+
+ if (d->count > 1) {
+ expect = (const char *) get_rsp_last;
+ expect_len = sizeof(get_rsp_last);
+ send_buf = NULL;
+ send_buf_len = 0;
+ } else {
+ expect = (const char *) get_rsp_first;
+ expect_len = sizeof(get_rsp_first);
+ send_buf = (const char *) get_req_last;
+ send_buf_len = sizeof(get_req_last);
+ }
+
+ status = g_io_channel_read_chars(io, buf, sizeof(buf), &rbytes, NULL);
+ if (status != G_IO_STATUS_NORMAL) {
+ g_print("get_cli count %u\n", d->count);
+ g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
+ "Reading data failed with status %d", status);
+ goto failed;
+ }
+
+ if (rbytes < expect_len) {
+ g_print("get_cli count %u\n", d->count);
+ dump_bufs(expect, expect_len, buf, rbytes);
+ g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
+ "Not enough data from socket");
+ goto failed;
+ }
+
+ if (memcmp(buf, expect, expect_len) != 0) {
+ g_print("get_cli count %u\n", d->count);
+ dump_bufs(expect, expect_len, buf, rbytes);
+ g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
+ "Received data is not correct");
+ goto failed;
+ }
+
+ if (send_buf == NULL)
+ return TRUE;
+
+ g_io_channel_write_chars(io, send_buf, send_buf_len, &bytes_written,
+ NULL);
+ if (bytes_written != send_buf_len) {
+ g_print("get_cli count %u\n", d->count);
+ g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
+ "Unable to write to socket");
+ goto failed;
+ }
+
+ return TRUE;
+
+failed:
+ g_main_loop_quit(mainloop);
+ return FALSE;
+}
+
+static void handle_get(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(mainloop);
+ return;
+ }
+
+ id = g_obex_get_rsp(obex, req, provide_data, transfer_complete,
+ d, &d->err);
+ if (id == 0)
+ g_main_loop_quit(mainloop);
+}
+
+static void test_get_rsp(void)
+{
+ GIOChannel *io;
+ GIOCondition cond;
+ guint io_id, timer_id;
+ GObex *obex;
+ struct test_data d = { 0, NULL };
+
+ create_endpoints(&obex, &io, SOCK_STREAM);
+
+ cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
+ io_id = g_io_add_watch(io, cond, get_cli, &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, &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(mainloop);
+
+ g_main_loop_unref(mainloop);
+ mainloop = NULL;
+
+ 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);
g_test_add_func("/gobex/test_put_rsp", test_put_rsp);
g_test_add_func("/gobex/test_get_req", test_get_req);
+ g_test_add_func("/gobex/test_get_rsp", test_get_rsp);
g_test_run();