From a47064623286c0327eedd9f6cbb1e14e50de8032 Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Mon, 4 Jul 2011 22:13:28 +0300 Subject: [PATCH] gobex: Add convenience response sending function --- gobex/gobex-packet.c | 5 +++-- gobex/gobex-packet.h | 2 +- gobex/gobex.c | 42 +++++++++++++++++++++++++++++++--------- gobex/gobex.h | 2 ++ unit/test-gobex-packet.c | 4 ++-- unit/test-gobex.c | 10 +++++----- 6 files changed, 46 insertions(+), 19 deletions(-) diff --git a/gobex/gobex-packet.c b/gobex/gobex-packet.c index 89f14e4e3..e55b20569 100644 --- a/gobex/gobex-packet.c +++ b/gobex/gobex-packet.c @@ -116,7 +116,7 @@ gboolean g_obex_packet_set_data(GObexPacket *pkt, const void *data, gsize len, return TRUE; } -GObexPacket *g_obex_packet_new(guint8 opcode, gboolean final) +GObexPacket *g_obex_packet_new(guint8 opcode, gboolean final, GSList *headers) { GObexPacket *pkt; @@ -124,6 +124,7 @@ GObexPacket *g_obex_packet_new(guint8 opcode, gboolean final) pkt->opcode = opcode; pkt->final = final; + pkt->headers = headers; pkt->data_policy = G_OBEX_DATA_COPY; @@ -214,7 +215,7 @@ GObexPacket *g_obex_packet_decode(const void *data, gsize len, final = (opcode & FINAL_BIT) ? TRUE : FALSE; opcode &= ~FINAL_BIT; - pkt = g_obex_packet_new(opcode, final); + pkt = g_obex_packet_new(opcode, final, NULL); if (header_offset == 0) goto headers; diff --git a/gobex/gobex-packet.h b/gobex/gobex-packet.h index 299e7f029..16dcf0fab 100644 --- a/gobex/gobex-packet.h +++ b/gobex/gobex-packet.h @@ -86,7 +86,7 @@ gboolean g_obex_packet_add_header(GObexPacket *pkt, GObexHeader *header); gboolean g_obex_packet_set_data(GObexPacket *pkt, const void *data, gsize len, GObexDataPolicy data_policy); const void *g_obex_packet_get_data(GObexPacket *pkt, gsize *len); -GObexPacket *g_obex_packet_new(guint8 opcode, gboolean final); +GObexPacket *g_obex_packet_new(guint8 opcode, gboolean final, GSList *headers); void g_obex_packet_free(GObexPacket *pkt); GObexPacket *g_obex_packet_decode(const void *data, gsize len, diff --git a/gobex/gobex.c b/gobex/gobex.c index 2e9d3bb20..5357e0262 100644 --- a/gobex/gobex.c +++ b/gobex/gobex.c @@ -347,7 +347,7 @@ static gboolean pending_req_abort(GObex *obex, GError **err) obex->pending_req->cancelled = TRUE; - pkt = g_obex_packet_new(G_OBEX_OP_ABORT, TRUE); + pkt = g_obex_packet_new(G_OBEX_OP_ABORT, TRUE, NULL); return g_obex_send(obex, pkt, err); } @@ -421,6 +421,19 @@ void g_obex_set_disconnect_function(GObex *obex, GObexDisconnectFunc func, obex->disconn_func_data = user_data; } +static void init_connect_data(GObex *obex, struct connect_data *data) +{ + guint16 u16; + + memset(data, 0, sizeof(*data)); + + data->version = 0x10; + data->flags = 0; + + u16 = g_htons(obex->rx_mtu); + memcpy(&data->mtu, &u16, sizeof(u16)); +} + static void parse_connect_data(GObex *obex, GObexPacket *pkt) { const struct connect_data *data; @@ -743,22 +756,33 @@ void g_obex_unref(GObex *obex) /* Higher level functions */ +gboolean g_obex_response(GObex *obex, GObexPacket *req, guint8 rspcode, + GSList *headers, GError **err) +{ + GObexPacket *rsp; + + rsp = g_obex_packet_new(rspcode, TRUE, headers); + + if (g_obex_packet_get_operation(req, NULL) == G_OBEX_OP_CONNECT) { + struct connect_data data; + init_connect_data(obex, &data); + g_obex_packet_set_data(rsp, &data, sizeof(data), + G_OBEX_DATA_COPY); + } + + return g_obex_send(obex, rsp, err); +} + guint g_obex_connect(GObex *obex, void *target, gsize target_len, GObexResponseFunc func, gpointer user_data, GError **err) { GObexPacket *req; struct connect_data data; - guint16 u16; - req = g_obex_packet_new(G_OBEX_OP_CONNECT, TRUE); - - memset(&data, 0, sizeof(data)); - data.version = 0x10; - data.flags = 0; - u16 = g_htons(obex->rx_mtu); - memcpy(&data.mtu, &u16, sizeof(u16)); + req = g_obex_packet_new(G_OBEX_OP_CONNECT, TRUE, NULL); + init_connect_data(obex, &data); g_obex_packet_set_data(req, &data, sizeof(data), G_OBEX_DATA_COPY); if (target != NULL) { diff --git a/gobex/gobex.h b/gobex/gobex.h index ce307c041..aecfffa71 100644 --- a/gobex/gobex.h +++ b/gobex/gobex.h @@ -64,5 +64,7 @@ void g_obex_unref(GObex *obex); guint g_obex_connect(GObex *obex, void *target, gsize target_len, GObexResponseFunc func, gpointer user_data, GError **err); +gboolean g_obex_response(GObex *obex, GObexPacket *req, guint8 rspcode, + GSList *headers, GError **err); #endif /* __GOBEX_H */ diff --git a/unit/test-gobex-packet.c b/unit/test-gobex-packet.c index 464382a2c..501d924f6 100644 --- a/unit/test-gobex-packet.c +++ b/unit/test-gobex-packet.c @@ -43,7 +43,7 @@ static void test_pkt(void) { GObexPacket *pkt; - pkt = g_obex_packet_new(G_OBEX_OP_PUT, TRUE); + pkt = g_obex_packet_new(G_OBEX_OP_PUT, TRUE, NULL); g_assert(pkt != NULL); @@ -161,7 +161,7 @@ static void test_encode_on_demand(void) uint8_t buf[255]; gssize len; - pkt = g_obex_packet_new(G_OBEX_OP_PUT, FALSE); + pkt = g_obex_packet_new(G_OBEX_OP_PUT, FALSE, NULL); hdr = g_obex_header_new_on_demand(G_OBEX_HDR_ID_BODY, get_body_data, NULL); diff --git a/unit/test-gobex.c b/unit/test-gobex.c index 84006c94c..5d65dde58 100644 --- a/unit/test-gobex.c +++ b/unit/test-gobex.c @@ -282,7 +282,7 @@ static void send_connect(GObexResponseFunc rsp_func, GIOFunc send_rsp_func, GObexPacket *req; guint8 connect_data[] = { 0x10, 0x00, 0x10, 0x00 }; - req = g_obex_packet_new(G_OBEX_OP_CONNECT, TRUE); + req = g_obex_packet_new(G_OBEX_OP_CONNECT, TRUE, NULL); g_assert(req != NULL); g_obex_packet_set_data(req, connect_data, sizeof(connect_data), @@ -356,7 +356,7 @@ static void test_cancel_req_immediate(void) r.err = NULL; - req = g_obex_packet_new(G_OBEX_OP_PUT, TRUE); + req = g_obex_packet_new(G_OBEX_OP_PUT, TRUE, NULL); r.id = g_obex_send_req(r.obex, req, -1, req_done, &r, &r.err); g_assert_no_error(r.err); g_assert(r.id != 0); @@ -437,7 +437,7 @@ static void test_cancel_req_delay(int transport_type) r.err = NULL; - req = g_obex_packet_new(G_OBEX_OP_PUT, TRUE); + req = g_obex_packet_new(G_OBEX_OP_PUT, TRUE, NULL); r.id = g_obex_send_req(r.obex, req, -1, req_done, &r, &r.err); g_assert_no_error(r.err); g_assert(r.id != 0); @@ -532,7 +532,7 @@ static void test_send_connect(int transport_type) r.buf = pkt_connect_req; r.len = sizeof(pkt_connect_req); - req = g_obex_packet_new(G_OBEX_OP_CONNECT, TRUE); + req = g_obex_packet_new(G_OBEX_OP_CONNECT, TRUE, NULL); g_assert(req != NULL); g_obex_packet_set_data(req, connect_data, sizeof(connect_data), @@ -596,7 +596,7 @@ static void test_send_on_demand(int transport_type) r.buf = pkt_put_body; r.len = sizeof(pkt_put_body); - req = g_obex_packet_new(G_OBEX_OP_PUT, FALSE); + req = g_obex_packet_new(G_OBEX_OP_PUT, FALSE, NULL); hdr = g_obex_header_new_on_demand(G_OBEX_HDR_ID_BODY, get_body_data, NULL); -- 2.47.3