OSDN Git Service

gobex: Split ev_func into req_func and disconn_func
authorJohan Hedberg <johan.hedberg@intel.com>
Fri, 1 Jul 2011 08:25:20 +0000 (11:25 +0300)
committerMarcel Holtmann <marcel@holtmann.org>
Tue, 4 Dec 2012 21:21:58 +0000 (22:21 +0100)
gobex/gobex.c
gobex/gobex.h
unit/test-gobex.c

index 6ee5e09..f5e8179 100644 (file)
@@ -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);
 
index 4bf7b1d..19a0fb7 100644 (file)
@@ -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);
index 2d8630e..1492ab9 100644 (file)
@@ -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);