Diff between 3f2536cb1da0c96a3f270e616f53130898bd3ead and 6abdccbf94dd16c3c9d4619fd969826a02564105

Changed Files

File Additions Deletions Status
gobex/gobex-transfer.c +57 -2 modified
gobex/gobex-transfer.h +4 -0 modified
unit/test-gobex-transfer.c +145 -31 modified

Full Patch

diff --git a/gobex/gobex-transfer.c b/gobex/gobex-transfer.c
index 5c560d0..e41c8e9 100644
--- a/gobex/gobex-transfer.c
+++ b/gobex/gobex-transfer.c
@@ -136,13 +136,33 @@ static void transfer_response(GObex *obex, GError *err, GObexPacket *rsp,
 		return;
 	}
 
+	if (transfer->opcode == G_OBEX_OP_GET) {
+		GObexHeader *body;
+		body = g_obex_packet_get_header(rsp, G_OBEX_HDR_ID_BODY);
+		if (body == NULL)
+			body = g_obex_packet_get_header(rsp,
+						G_OBEX_HDR_ID_BODY_END);
+		if (body != NULL) {
+			const guint8 *buf;
+			gsize len;
+
+			g_obex_header_get_bytes(body, &buf, &len);
+
+			if (len > 0)
+				transfer->data_consumer(buf, len,
+							transfer->user_data);
+		}
+	}
+
 	if (rspcode == G_OBEX_RSP_SUCCESS) {
 		transfer_complete(transfer, NULL);
 		return;
 	}
 
-	req = g_obex_packet_new(G_OBEX_OP_PUT, TRUE, NULL);
-	g_obex_packet_add_body(req, put_get_data, transfer);
+	req = g_obex_packet_new(transfer->opcode, TRUE, NULL);
+
+	if (transfer->opcode == G_OBEX_OP_PUT)
+		g_obex_packet_add_body(req, put_get_data, transfer);
 
 	transfer->req_id = g_obex_send_req(obex, req, -1, transfer_response,
 							transfer, &err);
@@ -280,3 +300,38 @@ guint g_obex_put_rsp(GObex *obex, GObexPacket *req,
 
 	return transfer->id;
 }
+
+guint g_obex_get_req(GObex *obex, const char *type, const char *name,
+			GObexDataConsumer data_func, GObexFunc complete_func,
+			gpointer user_data, GError **err)
+{
+	struct transfer *transfer;
+	GObexPacket *req;
+	GObexHeader *hdr;
+
+	transfer = transfer_new(obex, G_OBEX_OP_GET, complete_func, user_data);
+	transfer->data_consumer = data_func;
+
+	req = g_obex_packet_new(G_OBEX_OP_GET, TRUE, NULL);
+
+	if (type) {
+		hdr = g_obex_header_new_bytes(G_OBEX_HDR_ID_TYPE,
+					(char *) type, strlen(type) + 1,
+					G_OBEX_DATA_COPY);
+		g_obex_packet_add_header(req, hdr);
+	}
+
+	if (name) {
+		hdr = g_obex_header_new_unicode(G_OBEX_HDR_ID_NAME, name);
+		g_obex_packet_add_header(req, hdr);
+	}
+
+	transfer->req_id = g_obex_send_req(obex, req, -1, transfer_response,
+								transfer, err);
+	if (transfer->req_id == 0) {
+		transfer_free(transfer);
+		return 0;
+	}
+
+	return transfer->id;
+}
diff --git a/gobex/gobex-transfer.h b/gobex/gobex-transfer.h
index fdc2878..5a9510e 100644
--- a/gobex/gobex-transfer.h
+++ b/gobex/gobex-transfer.h
@@ -31,6 +31,10 @@ guint g_obex_put_req(GObex *obex, const char *type, const char *name,
 			GObexDataProducer data_func, GObexFunc complete_func,
 			gpointer user_data, GError **err);
 
+guint g_obex_get_req(GObex *obex, const char *type, const char *name,
+			GObexDataConsumer data_func, GObexFunc complete_func,
+			gpointer user_data, GError **err);
+
 guint g_obex_put_rsp(GObex *obex, GObexPacket *req,
 			GObexDataConsumer data_func, GObexFunc complete_func,
 			gpointer user_data, GError **err);
diff --git a/unit/test-gobex-transfer.c b/unit/test-gobex-transfer.c
index b2f6643..a94a62d 100644
--- a/unit/test-gobex-transfer.c
+++ b/unit/test-gobex-transfer.c
@@ -36,7 +36,7 @@
 
 static GMainLoop *mainloop = NULL;
 
-static guint8 pkt_put_first[] = { G_OBEX_OP_PUT | FINAL_BIT, 0x00, 0x30,
+static guint8 put_req_first[] = { G_OBEX_OP_PUT | FINAL_BIT, 0x00, 0x30,
 	G_OBEX_HDR_ID_TYPE, 0x00, 0x0b,
 	'f', 'o', 'o', '/', 'b', 'a', 'r', '\0',
 	G_OBEX_HDR_ID_NAME, 0x00, 0x15,
@@ -44,13 +44,28 @@ static guint8 pkt_put_first[] = { G_OBEX_OP_PUT | FINAL_BIT, 0x00, 0x30,
 	G_OBEX_HDR_ID_BODY, 0x00, 0x0d,
 	0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
-static guint8 pkt_put_last[] = { G_OBEX_OP_PUT | FINAL_BIT, 0x00, 0x06,
+static guint8 put_req_last[] = { G_OBEX_OP_PUT | FINAL_BIT, 0x00, 0x06,
 					G_OBEX_HDR_ID_BODY_END, 0x00, 0x03 };
 
-static guint8 pkt_put_rsp[] = { G_OBEX_RSP_CONTINUE | FINAL_BIT, 0x00, 0x03 };
-static guint8 pkt_put_rsp_last[] = { G_OBEX_RSP_SUCCESS | FINAL_BIT, 0x00, 0x03 };
+static guint8 put_rsp_first[] = { G_OBEX_RSP_CONTINUE | FINAL_BIT,
+								0x00, 0x03 };
+static guint8 put_rsp_last[] = { G_OBEX_RSP_SUCCESS | FINAL_BIT, 0x00, 0x03 };
 
-static guint8 put_test_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+static guint8 get_req_first[] = { G_OBEX_OP_GET | FINAL_BIT, 0x00, 0x23,
+	G_OBEX_HDR_ID_TYPE, 0x00, 0x0b,
+	'f', 'o', 'o', '/', 'b', 'a', 'r', '\0',
+	G_OBEX_HDR_ID_NAME, 0x00, 0x15,
+	0, 'f', 0, 'i', 0, 'l', 0, 'e', 0, '.', 0, 't', 0, 'x', 0, 't', 0, 0 };
+
+static guint8 get_req_last[] = { G_OBEX_OP_GET | FINAL_BIT, 0x00, 0x03, };
+
+static guint8 get_rsp_first[] = { G_OBEX_RSP_CONTINUE | FINAL_BIT, 0x00, 0x10,
+					G_OBEX_HDR_ID_BODY, 0x00, 0x0d,
+					0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+static guint8 get_rsp_last[] = { G_OBEX_RSP_SUCCESS | FINAL_BIT, 0x00, 0x06,
+					G_OBEX_HDR_ID_BODY_END, 0x00, 0x03 };
+
+static guint8 body_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
 static gboolean test_timeout(gpointer user_data)
 {
@@ -82,15 +97,15 @@ static gboolean put_srv(GIOChannel *io, GIOCondition cond, gpointer user_data)
 	d->count++;
 
 	if (d->count > 1) {
-		send_buf = (const char *) pkt_put_rsp_last;
-		send_buf_len = sizeof(pkt_put_rsp_last);
-		expect = (const char *) pkt_put_last;
-		expect_len = sizeof(pkt_put_last);
+		send_buf = (const char *) put_rsp_last;
+		send_buf_len = sizeof(put_rsp_last);
+		expect = (const char *) put_req_last;
+		expect_len = sizeof(put_req_last);
 	} else {
-		send_buf = (const char *) pkt_put_rsp;
-		send_buf_len = sizeof(pkt_put_rsp);
-		expect = (const char *) pkt_put_first;
-		expect_len = sizeof(pkt_put_first);
+		send_buf = (const char *) put_rsp_first;
+		send_buf_len = sizeof(put_rsp_first);
+		expect = (const char *) put_req_first;
+		expect_len = sizeof(put_req_first);
 	}
 
 	status = g_io_channel_read_chars(io, buf, sizeof(buf), &rbytes, NULL);
@@ -133,7 +148,7 @@ failed:
 	return FALSE;
 }
 
-static void put_complete(GObex *obex, GError *err, gpointer user_data)
+static void transfer_complete(GObex *obex, GError *err, gpointer user_data)
 {
 	struct test_data *d = user_data;
 
@@ -150,16 +165,16 @@ static gssize put_provide_data(void *buf, gsize len, gpointer user_data)
 	if (d->count > 0)
 		return 0;
 
-	if (len < sizeof(put_test_data)) {
+	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(mainloop);
 		return -1;
 	}
 
-	memcpy(buf, put_test_data, sizeof(put_test_data));
+	memcpy(buf, body_data, sizeof(body_data));
 
-	return sizeof(put_test_data);
+	return sizeof(body_data);
 }
 
 static void test_put_req(void)
@@ -180,7 +195,7 @@ static void test_put_req(void)
 	timer_id = g_timeout_add_seconds(1, test_timeout, &d);
 
 	g_obex_put_req(obex, "foo/bar", "file.txt", put_provide_data,
-						put_complete, &d, &d.err);
+						transfer_complete, &d, &d.err);
 	g_assert_no_error(d.err);
 
 	g_main_loop_run(mainloop);
@@ -196,16 +211,16 @@ static void test_put_req(void)
 	g_assert_no_error(d.err);
 }
 
-static gboolean put_rcv_data(const void *buf, gsize len, gpointer user_data)
+static gboolean rcv_data(const void *buf, gsize len, gpointer user_data)
 {
 	struct test_data *d = user_data;
 
-	if (len != sizeof(put_test_data))
+	if (len != sizeof(body_data))
 		d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
 					"Unexpected byte count %zu", len);
 
-	if (memcmp(buf, put_test_data, sizeof(put_test_data)) != 0) {
-		dump_bufs(put_test_data, sizeof(put_test_data), buf, len);
+	if (memcmp(buf, body_data, sizeof(body_data)) != 0) {
+		dump_bufs(body_data, sizeof(body_data), buf, len);
 		d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
 					"Unexpected byte count %zu", len);
 	}
@@ -226,7 +241,8 @@ static void handle_put(GObex *obex, GObexPacket *req, gpointer user_data)
 		return;
 	}
 
-	id = g_obex_put_rsp(obex, req, put_rcv_data, put_complete, d, &d->err);
+	id = g_obex_put_rsp(obex, req, rcv_data, transfer_complete, d,
+								&d->err);
 	if (id == 0)
 		g_main_loop_quit(mainloop);
 }
@@ -242,15 +258,15 @@ static gboolean put_cli(GIOChannel *io, GIOCondition cond, gpointer user_data)
 	d->count++;
 
 	if (d->count > 1) {
-		expect = (const char *) pkt_put_rsp_last;
-		expect_len = sizeof(pkt_put_rsp_last);
+		expect = (const char *) put_rsp_last;
+		expect_len = sizeof(put_rsp_last);
 		send_buf = NULL;
 		send_buf_len = 0;
 	} else {
-		expect = (const char *) pkt_put_rsp;
-		expect_len = sizeof(pkt_put_rsp);
-		send_buf = (const char *) pkt_put_last;
-		send_buf_len = sizeof(pkt_put_last);
+		expect = (const char *) put_rsp_first;
+		expect_len = sizeof(put_rsp_first);
+		send_buf = (const char *) put_req_last;
+		send_buf_len = sizeof(put_req_last);
 	}
 
 	status = g_io_channel_read_chars(io, buf, sizeof(buf), &rbytes, NULL);
@@ -315,8 +331,104 @@ static void test_put_rsp(void)
 
 	g_obex_add_request_function(obex, G_OBEX_OP_PUT, handle_put, &d);
 
-	g_io_channel_write_chars(io, (char *) pkt_put_first,
-					sizeof(pkt_put_first), NULL, &d.err);
+	g_io_channel_write_chars(io, (char *) put_req_first,
+					sizeof(put_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);
+}
+
+static gboolean get_srv(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) {
+		send_buf = (const char *) get_rsp_last;
+		send_buf_len = sizeof(get_rsp_last);
+		expect = (const char *) get_req_last;
+		expect_len = sizeof(get_req_last);
+	} else {
+		send_buf = (const char *) get_rsp_first;
+		send_buf_len = sizeof(get_rsp_first);
+		expect = (const char *) get_req_first;
+		expect_len = sizeof(get_req_first);
+	}
+
+	status = g_io_channel_read_chars(io, buf, sizeof(buf), &rbytes, NULL);
+	if (status != G_IO_STATUS_NORMAL) {
+		g_print("put_srv 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("put_srv 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("put_srv 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;
+	}
+
+	g_io_channel_write_chars(io, send_buf, send_buf_len, &bytes_written,
+									NULL);
+	if (bytes_written != send_buf_len) {
+		g_print("put_srv 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 test_get_req(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_srv, &d);
+
+	mainloop = g_main_loop_new(NULL, FALSE);
+
+	timer_id = g_timeout_add_seconds(1, test_timeout, &d);
+
+	g_obex_get_req(obex, "foo/bar", "file.txt", rcv_data,
+						transfer_complete, &d, &d.err);
 	g_assert_no_error(d.err);
 
 	g_main_loop_run(mainloop);
@@ -339,6 +451,8 @@ int main(int argc, char *argv[])
 	g_test_add_func("/gobex/test_put_req", test_put_req);
 	g_test_add_func("/gobex/test_put_rsp", test_put_rsp);
 
+	g_test_add_func("/gobex/test_get_req", test_get_req);
+
 	g_test_run();
 
 	return 0;