upgrade obexd to 0.47
[profile/ivi/obexd.git] / gobex / gobex.c
index 4afc21b..b20542d 100644 (file)
@@ -28,7 +28,6 @@
 #include <errno.h>
 
 #include "gobex.h"
-#include "glib-helper.h"
 #include "gobex-debug.h"
 
 #define G_OBEX_DEFAULT_MTU     4096
@@ -247,10 +246,6 @@ static gboolean req_timeout(gpointer user_data)
 
        g_obex_debug(G_OBEX_DEBUG_ERROR, "%s", err->message);
 
-#ifndef TIZEN_PATCH
-       obex->pending_req = NULL;
-#endif
-
        if (p->rsp_func)
                p->rsp_func(obex, err, NULL, p->rsp_data);
 
@@ -303,6 +298,110 @@ static gboolean write_packet(GObex *obex, GError **err)
        return TRUE;
 }
 
+static void set_srmp(GObex *obex, guint8 srmp, gboolean outgoing)
+{
+       struct srm_config *config = obex->srm;
+
+       if (config == NULL)
+               return;
+
+       /* Dont't reset if direction doesn't match */
+       if (srmp > G_OBEX_SRMP_NEXT_WAIT && config->outgoing != outgoing)
+               return;
+
+       config->srmp = srmp;
+       config->outgoing = outgoing;
+}
+
+static void set_srm(GObex *obex, guint8 op, guint8 srm)
+{
+       struct srm_config *config = obex->srm;
+       gboolean enable;
+
+       if (config == NULL) {
+               if (srm == G_OBEX_SRM_DISABLE)
+                       return;
+
+               config = g_new0(struct srm_config, 1);
+               config->op = op;
+               config->srm = srm;
+               obex->srm = config;
+               return;
+       }
+
+       /* Indicate response, treat it as request */
+       if (config->srm == G_OBEX_SRM_INDICATE) {
+               if (srm != G_OBEX_SRM_ENABLE)
+                       goto done;
+               config->srm = srm;
+               return;
+       }
+
+       enable = (srm == G_OBEX_SRM_ENABLE);
+       if (config->enabled == enable)
+               goto done;
+
+       config->enabled = enable;
+
+       g_obex_debug(G_OBEX_DEBUG_COMMAND, "SRM %s", config->enabled ?
+                                               "Enabled" : "Disabled");
+
+done:
+       if (config->enabled)
+               return;
+
+       g_free(obex->srm);
+       obex->srm = NULL;
+}
+
+static void check_srm_final(GObex *obex, guint8 op)
+{
+       if (obex->srm == NULL || !obex->srm->enabled)
+               return;
+
+       switch (obex->srm->op) {
+       case G_OBEX_OP_CONNECT:
+               return;
+       default:
+               if (op <= G_OBEX_RSP_CONTINUE)
+                       return;
+       }
+
+       set_srm(obex, op, G_OBEX_SRM_DISABLE);
+}
+
+static void setup_srm(GObex *obex, GObexPacket *pkt, gboolean outgoing)
+{
+       GObexHeader *hdr;
+       guint8 op;
+       gboolean final;
+
+       if (!obex->use_srm)
+               return;
+
+       op = g_obex_packet_get_operation(pkt, &final);
+
+       hdr = g_obex_packet_get_header(pkt, G_OBEX_HDR_SRM);
+       if (hdr != NULL) {
+               guint8 srm;
+               g_obex_header_get_uint8(hdr, &srm);
+               g_obex_debug(G_OBEX_DEBUG_COMMAND, "srm 0x%02x", srm);
+               set_srm(obex, op, srm);
+       }
+
+       hdr = g_obex_packet_get_header(pkt, G_OBEX_HDR_SRMP);
+       if (hdr != NULL) {
+               guint8 srmp;
+               g_obex_header_get_uint8(hdr, &srmp);
+               g_obex_debug(G_OBEX_DEBUG_COMMAND, "srmp 0x%02x", srmp);
+               set_srmp(obex, srmp, outgoing);
+       } else
+               set_srmp(obex, -1, outgoing);
+
+       if (final)
+               check_srm_final(obex, op);
+}
+
 static gboolean write_data(GIOChannel *io, GIOCondition cond,
                                                        gpointer user_data)
 {
@@ -321,6 +420,8 @@ static gboolean write_data(GIOChannel *io, GIOCondition cond,
                if (p == NULL)
                        goto stop_tx;
 
+               setup_srm(obex, p->pkt, TRUE);
+
                if (g_obex_srm_active(obex))
                        goto encode;
 
@@ -349,8 +450,13 @@ encode:
                        obex->pending_req = p;
                        p->timeout_id = g_timeout_add_seconds(p->timeout,
                                                        req_timeout, obex);
-               } else
+               } else {
+                       /* During packet encode final bit can be set */
+                       if (obex->tx_buf[0] & FINAL_BIT)
+                               check_srm_final(obex,
+                                               obex->tx_buf[0] & ~FINAL_BIT);
                        pending_pkt_free(p);
+               }
 
                obex->tx_data = len;
                obex->tx_sent = 0;
@@ -446,104 +552,6 @@ static void prepare_connect_rsp(GObex *obex, GObexPacket *rsp)
        g_obex_packet_prepend_header(rsp, connid);
 }
 
-static void set_srmp(GObex *obex, guint8 srmp, gboolean outgoing)
-{
-       struct srm_config *config = obex->srm;
-
-       if (config == NULL)
-               return;
-
-       /* Dont't reset if direction doesn't match */
-       if (srmp > G_OBEX_SRMP_NEXT_WAIT && config->outgoing != outgoing)
-               return;
-
-       config->srmp = srmp;
-       config->outgoing = outgoing;
-}
-
-static void set_srm(GObex *obex, guint8 op, guint8 srm)
-{
-       struct srm_config *config = obex->srm;
-       gboolean enable;
-
-       if (config == NULL) {
-               if (srm == G_OBEX_SRM_DISABLE)
-                       return;
-
-               config = g_new0(struct srm_config, 1);
-               config->op = op;
-               config->srm = srm;
-               obex->srm = config;
-               return;
-       }
-
-       /* Indicate response, treat it as request */
-       if (config->srm == G_OBEX_SRM_INDICATE) {
-               if (srm != G_OBEX_SRM_ENABLE)
-                       goto done;
-               config->srm = srm;
-               return;
-       }
-
-       enable = (srm == G_OBEX_SRM_ENABLE);
-       if (config->enabled == enable)
-               goto done;
-
-       config->enabled = enable;
-
-       g_obex_debug(G_OBEX_DEBUG_COMMAND, "SRM %s", config->enabled ?
-                                               "Enabled" : "Disabled");
-
-done:
-       if (config->enabled)
-               return;
-
-       g_free(obex->srm);
-       obex->srm = NULL;
-}
-
-static void setup_srm(GObex *obex, GObexPacket *pkt, gboolean outgoing)
-{
-       GObexHeader *hdr;
-       guint8 op;
-       gboolean final;
-
-       if (!obex->use_srm)
-               return;
-
-       op = g_obex_packet_get_operation(pkt, &final);
-
-       hdr = g_obex_packet_get_header(pkt, G_OBEX_HDR_SRM);
-       if (hdr != NULL) {
-               guint8 srm;
-               g_obex_header_get_uint8(hdr, &srm);
-               g_obex_debug(G_OBEX_DEBUG_COMMAND, "srm 0x%02x", srm);
-               set_srm(obex, op, srm);
-       }
-
-       hdr = g_obex_packet_get_header(pkt, G_OBEX_HDR_SRMP);
-       if (hdr != NULL) {
-               guint8 srmp;
-               g_obex_header_get_uint8(hdr, &srmp);
-               g_obex_debug(G_OBEX_DEBUG_COMMAND, "srmp 0x%02x", srmp);
-               set_srmp(obex, srmp, outgoing);
-       } else
-               set_srmp(obex, -1, outgoing);
-
-       if (obex->srm == NULL || !obex->srm->enabled || !final)
-               return;
-
-       switch (obex->srm->op) {
-       case G_OBEX_OP_CONNECT:
-               return;
-       default:
-               if (op <= G_OBEX_RSP_CONTINUE)
-                       return;
-       }
-
-       set_srm(obex, op, G_OBEX_SRM_DISABLE);
-}
-
 static void prepare_srm_rsp(GObex *obex, GObexPacket *pkt)
 {
        GObexHeader *hdr;
@@ -579,14 +587,13 @@ gboolean g_obex_send(GObex *obex, GObexPacket *pkt, GError **err)
        switch (obex->rx_last_op) {
        case G_OBEX_OP_CONNECT:
                prepare_connect_rsp(obex, pkt);
+               break;
        case G_OBEX_OP_GET:
        case G_OBEX_OP_PUT:
                prepare_srm_rsp(obex, pkt);
                break;
        }
 
-       setup_srm(obex, pkt, TRUE);
-
        p = g_new0(struct pending_pkt, 1);
        p->pkt = pkt;
 
@@ -632,8 +639,6 @@ guint g_obex_send_req(GObex *obex, GObexPacket *req, gint timeout,
                prepare_srm_req(obex, req);
        }
 
-       setup_srm(obex, req, TRUE);
-
        if (obex->conn_id == CONNID_INVALID)
                goto create_pending;
 
@@ -679,7 +684,7 @@ static gint pending_pkt_cmp(gconstpointer a, gconstpointer b)
        return (p->id - id);
 }
 
-static gboolean pending_req_abort(GObex *obex, GError **err)
+gboolean g_obex_pending_req_abort(GObex *obex, GError **err)
 {
        struct pending_pkt *p = obex->pending_req;
        GObexPacket *req;
@@ -723,12 +728,15 @@ gboolean g_obex_cancel_req(GObex *obex, guint req_id, gboolean remove_callback)
        struct pending_pkt *p;
 
        if (obex->pending_req && obex->pending_req->id == req_id) {
-               if (!pending_req_abort(obex, NULL)) {
+               if (!g_obex_pending_req_abort(obex, NULL)) {
                        p = obex->pending_req;
                        obex->pending_req = NULL;
                        goto immediate_completion;
                }
 
+               if (remove_callback)
+                       obex->pending_req->rsp_func = NULL;
+
                return TRUE;
        }
 
@@ -851,6 +859,9 @@ gboolean g_obex_srm_active(GObex *obex)
 {
        gboolean ret = FALSE;
 
+       if (!obex->use_srm)
+               return FALSE;
+
        if (obex->srm == NULL || !obex->srm->enabled)
                goto done;
 
@@ -1154,11 +1165,7 @@ static gboolean incoming_data(GIOChannel *io, GIOCondition cond,
        } else {
                opcode = obex->rx_last_op;
                /* Unexpected response -- fail silently */
-#ifdef TIZEN_PATCH
                if (opcode > 0x1f && opcode != G_OBEX_OP_ABORT) {
-#else
-               if (opcode > 0x1f && opcode < 0xff) {
-#endif
                        obex->rx_data = 0;
                        return TRUE;
                }
@@ -1316,11 +1323,7 @@ GObex *g_obex_ref(GObex *obex)
 
 void g_obex_unref(GObex *obex)
 {
-#ifdef TIZEN_PATCH
-       gboolean last_ref, ret;
-#else
        gboolean last_ref;
-#endif
 
        last_ref = g_atomic_int_dec_and_test(&obex->ref_count);
 
@@ -1331,14 +1334,6 @@ void g_obex_unref(GObex *obex)
 
        g_slist_free_full(obex->req_handlers, g_free);
 
-#ifdef TIZEN_PATCH
-       do {
-               ret = write_data(obex->io, G_IO_OUT, obex);
-               if (obex->pending_req && obex->pending_req->cancelled)
-                       break;
-       } while(ret);
-#endif
-
        g_queue_foreach(obex->tx_queue, (GFunc) pending_pkt_free, NULL);
        g_queue_free(obex->tx_queue);
 
@@ -1478,3 +1473,28 @@ guint g_obex_move(GObex *obex, const char *name, const char *dest,
 
        return g_obex_send_req(obex, req, -1, func, user_data, err);
 }
+
+guint8 g_obex_errno_to_rsp(int err)
+{
+       switch (err) {
+       case 0:
+               return G_OBEX_RSP_SUCCESS;
+       case -EPERM:
+       case -EACCES:
+               return G_OBEX_RSP_FORBIDDEN;
+       case -ENOENT:
+               return G_OBEX_RSP_NOT_FOUND;
+       case -EINVAL:
+       case -EBADR:
+               return G_OBEX_RSP_BAD_REQUEST;
+       case -EFAULT:
+               return G_OBEX_RSP_SERVICE_UNAVAILABLE;
+       case -ENOSYS:
+               return G_OBEX_RSP_NOT_IMPLEMENTED;
+       case -ENOTEMPTY:
+       case -EEXIST:
+               return G_OBEX_RSP_PRECONDITION_FAILED;
+       default:
+               return G_OBEX_RSP_INTERNAL_SERVER_ERROR;
+       }
+}