From 680e9abc4b78c755f8c0347f9ff26921c1c42da1 Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Fri, 1 Jul 2011 11:25:20 +0300 Subject: [PATCH] gobex: Split ev_func into req_func and disconn_func --- gobex/gobex.c | 46 ++++++++++++++++++++++++++++------------------ gobex/gobex.h | 8 ++++++-- unit/test-gobex.c | 32 ++++++++++++++++++++------------ 3 files changed, 54 insertions(+), 32 deletions(-) diff --git a/gobex/gobex.c b/gobex/gobex.c index 6ee5e09d7..f5e817954 100644 --- a/gobex/gobex.c +++ b/gobex/gobex.c @@ -55,8 +55,11 @@ struct _GObex { GQueue *tx_queue; - GObexEventFunc ev_func; - gpointer ev_func_data; + GObexRequestFunc req_func; + gpointer req_func_data; + + GObexDisconnectFunc disconn_func; + gpointer disconn_func_data; struct pending_pkt *pending_req; }; @@ -398,11 +401,18 @@ immediate_completion: return TRUE; } -void g_obex_set_event_function(GObex *obex, GObexEventFunc func, +void g_obex_set_request_function(GObex *obex, GObexRequestFunc func, + gpointer user_data) +{ + obex->req_func = func; + obex->req_func_data = user_data; +} + +void g_obex_set_disconnect_function(GObex *obex, GObexDisconnectFunc func, gpointer user_data) { - obex->ev_func = func; - obex->ev_func_data = user_data; + obex->disconn_func = func; + obex->disconn_func_data = user_data; } static void parse_connect_data(GObex *obex, GObexPacket *pkt) @@ -424,6 +434,7 @@ static void parse_connect_data(GObex *obex, GObexPacket *pkt) static void handle_response(GObex *obex, GError *err, GObexPacket *rsp) { struct pending_pkt *p = obex->pending_req; + gboolean disconn = err ? TRUE : FALSE; if (rsp != NULL) { guint8 op = g_obex_packet_get_operation(p->pkt, NULL); @@ -444,20 +455,17 @@ static void handle_response(GObex *obex, GError *err, GObexPacket *rsp) pending_pkt_free(p); obex->pending_req = NULL; - if (g_queue_get_length(obex->tx_queue) > 0) + if (!disconn && g_queue_get_length(obex->tx_queue) > 0) enable_tx(obex); } -static void handle_request(GObex *obex, GError *err, GObexPacket *req) +static void handle_request(GObex *obex, GObexPacket *req) { - if (req != NULL) { - guint8 op = g_obex_packet_get_operation(req, NULL); - if (op == G_OBEX_OP_CONNECT) - parse_connect_data(obex, req); - } + if (g_obex_packet_get_operation(req, NULL) == G_OBEX_OP_CONNECT) + parse_connect_data(obex, req); - if (obex->ev_func) - obex->ev_func(obex, err, req, obex->ev_func_data); + if (obex->req_func) + obex->req_func(obex, req, obex->req_func_data); } static gboolean read_stream(GObex *obex, GError **err) @@ -590,11 +598,13 @@ static gboolean incoming_data(GIOChannel *io, GIOCondition cond, pkt = g_obex_packet_decode(obex->rx_buf, obex->rx_data, header_offset, G_OBEX_DATA_REF, &err); + if (pkt == NULL) + goto failed; if (obex->pending_req) - handle_response(obex, err, pkt); + handle_response(obex, NULL, pkt); else - handle_request(obex, err, pkt); + handle_request(obex, pkt); if (err != NULL) g_error_free(err); @@ -614,8 +624,8 @@ failed: if (obex->pending_req) handle_response(obex, err, NULL); - if (obex->ev_func) - obex->ev_func(obex, err, NULL, obex->ev_func_data); + if (obex->disconn_func) + obex->disconn_func(obex, err, obex->disconn_func_data); g_error_free(err); diff --git a/gobex/gobex.h b/gobex/gobex.h index 4bf7b1df5..19a0fb75e 100644 --- a/gobex/gobex.h +++ b/gobex/gobex.h @@ -33,7 +33,9 @@ typedef enum { typedef struct _GObex GObex; -typedef void (*GObexEventFunc) (GObex *obex, GError *err, GObexPacket *req, +typedef void (*GObexRequestFunc) (GObex *obex, GObexPacket *req, + gpointer user_data); +typedef void (*GObexDisconnectFunc) (GObex *obex, GError *err, gpointer user_data); typedef void (*GObexResponseFunc) (GObex *obex, GError *err, GObexPacket *rsp, gpointer user_data); @@ -46,7 +48,9 @@ guint g_obex_send_req(GObex *obex, GObexPacket *req, gint timeout, gboolean g_obex_cancel_req(GObex *obex, guint req_id, gboolean remove_callback); -void g_obex_set_event_function(GObex *obex, GObexEventFunc func, +void g_obex_set_request_function(GObex *obex, GObexRequestFunc func, + gpointer user_data); +void g_obex_set_disconnect_function(GObex *obex, GObexDisconnectFunc func, gpointer user_data); GObex *g_obex_new(GIOChannel *io, GObexTransportType transport_type); diff --git a/unit/test-gobex.c b/unit/test-gobex.c index 2d8630ece..1492ab9cd 100644 --- a/unit/test-gobex.c +++ b/unit/test-gobex.c @@ -631,21 +631,29 @@ static void test_send_on_demand_pkt(void) test_send_on_demand(SOCK_SEQPACKET); } -static void handle_connect_event(GObex *obex, GError *err, GObexPacket *pkt, +static void handle_connect_req(GObex *obex, GObexPacket *req, gpointer user_data) { GError **test_err = user_data; + if (g_obex_packet_get_operation(req, NULL) != G_OBEX_OP_CONNECT) + g_set_error(test_err, TEST_ERROR, TEST_ERROR_UNEXPECTED, + "Unexpected operation"); g_main_loop_quit(mainloop); - if (err != NULL) { - *test_err = g_error_copy(err); - return; - } +} - if (g_obex_packet_get_operation(pkt, NULL) != G_OBEX_OP_CONNECT) - g_set_error(test_err, TEST_ERROR, TEST_ERROR_UNEXPECTED, - "Unexpected operation"); +static void handle_connect_err(GObex *obex, GError *err, gpointer user_data) +{ + GError **test_err = user_data; + + g_main_loop_quit(mainloop); + + if (err != NULL) + *test_err = g_error_copy(err); + else + *test_err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED, + "Disconnected"); } static void recv_connect(int transport_type) @@ -659,7 +667,8 @@ static void recv_connect(int transport_type) create_endpoints(&obex, &io, transport_type); - g_obex_set_event_function(obex, handle_connect_event, &gerr); + g_obex_set_request_function(obex, handle_connect_req, &gerr); + g_obex_set_disconnect_function(obex, handle_connect_err, &gerr); status = g_io_channel_write_chars(io, (gchar *) pkt_connect_req, sizeof(pkt_connect_req), @@ -693,8 +702,7 @@ static void test_recv_connect_pkt(void) recv_connect(SOCK_SEQPACKET); } -static void disconn_ev(GObex *obex, GError *err, GObexPacket *req, - gpointer user_data) +static void disconn_ev(GObex *obex, GError *err, gpointer user_data) { GError **test_err = user_data; @@ -714,7 +722,7 @@ static void test_disconnect(void) create_endpoints(&obex, &io, SOCK_STREAM); - g_obex_set_event_function(obex, disconn_ev, &gerr); + g_obex_set_disconnect_function(obex, disconn_ev, &gerr); timer_id = g_timeout_add_seconds(1, test_timeout, &gerr); -- 2.47.3