upload tizen1.0 source
[profile/ivi/obexd.git] / plugins / mas.c
index 8e470c1..9466039 100644 (file)
@@ -28,9 +28,6 @@
 #include <errno.h>
 #include <glib.h>
 #include <fcntl.h>
-#ifdef TIZEN_PATCH
-#include <string.h>
-#endif
 #include <inttypes.h>
 
 #include "obexd.h"
 #include "manager.h"
 
 #include "messages.h"
+#ifdef TIZEN_PATCH
+#include "map_ap.h"
+#include "bmessage-parser.h"
+#endif
 
 /* Channel number according to bluez doc/assigned-numbers.txt */
 #define MAS_CHANNEL    16
 #define ML_BODY_BEGIN "<MAP-msg-listing version=\"1.0\">"
 #define ML_BODY_END "</MAP-msg-listing>"
 
-#ifdef TIZEN_PATCH
-enum mas_apparam_tag {
-       MAXLISTCOUNT_T = 0x01,
-       STARTOFFSET_T = 0x02,
-       FILTERMESSAGETYPE_T = 0x03,
-       FILTERPERIODBEGIN_T = 0x04,
-       FILTERPERIODEND_T = 0x05,
-       FILTERREADSTATUS_T = 0x06,
-       FILTERRECIPIENT_T = 0x07,
-       FILTERORIGINATOR_T = 0x08,
-       FILTERPRIORITY_T = 0x09,
-       ATTACHMENT_T = 0x0A,
-       TRANSPARENT_T = 0x0B,
-       RETRY_T = 0x0C,
-       NEWMESSAGE_T = 0x0D,
-       NOTIFICATIONSTATUS_T = 0x0E,
-       MASINSTANCEID_T = 0x0F,
-       PARAMETERMASK_T = 0x10,
-       FOLDERLISTINGSIZE_T = 0x11,
-       MESSAGESLISTINGSIZE_T = 0x12,
-       SUBJECTLENGTH_T = 0x13,
-       CHARSET_T = 0x14,
-       FRACTIONREQUEST_T = 0x15,
-       FRACTIONDELIVER_T = 0x16,
-       STATUSINDICATOR_T = 0x17,
-       STATUSVALUE_T = 0x18,
-       MSETIME_T = 0x19,
-       INVALID_T = 0x99,
-};
-
-#define MAXLISTCOUNT_L 2
-#define STARTOFFSET_L 2
-#define FILTERMESSAGETYPE_L 1
-#define FILTERREADSTATUS_L 1
-#define FILTERPRIORITY_L 1
-#define ATTACHMENT_L 1
-#define TRANSPARENT_L 1
-#define RETRY_L 1
-#define NEWMESSAGE_L 1
-#define NOTIFICATIONSTATUS_L 1
-#define MASINSTANCEID_L 1
-#define PARAMETERMASK_L 4
-#define FOLDERLISTINGSIZE_L 2
-#define MESSAGESLISTINGSIZE_L 2
-#define SUBJECTLENGTH_L 1
-#define CHARSET_L 1
-#define FRACTIONREQUEST_L 1
-#define FRACTIONDELIVER_L 1
-#define STATUSINDICATOR_L 1
-#define STATUSVALUE_L 1
-
-#define MAS_MAXLISTCOUNT 1024
-#endif
-
 struct mas_session {
        struct mas_request *request;
        void *backend_data;
@@ -166,8 +113,7 @@ struct mas_session {
        gboolean nth_call;
        GString *buffer;
 #ifdef TIZEN_PATCH
-       struct apparam_field *apparams;
-       GByteArray *outparams;
+       map_ap_t *ap;
 #endif
 };
 
@@ -176,212 +122,11 @@ static const uint8_t MAS_TARGET[TARGET_SIZE] = {
                        0xb0, 0xde, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66  };
 
 #ifdef TIZEN_PATCH
-struct aparam_header {
-       uint8_t tag;
-       uint8_t len;
-       uint8_t val[0];
-} __attribute__ ((packed));
-
-static void apparam_free(struct apparam_field *apparams);
-
-static struct apparam_field *msg_parse_apparam(const uint8_t *buffer,
-                                                       uint32_t hlen)
+static void bmessage_parser(struct mas_session *mas,
+                          struct bmessage_property *bmessage)
 {
-       struct apparam_field *param = NULL;
-       struct aparam_header *hdr = NULL;
-       uint32_t len = 0;
-       uint16_t val16;
-       uint32_t val32;
-
-       param = g_new0(struct apparam_field, 1);
-       param->filter = g_new0(struct messages_filter, 1);
-       param->maxlistcount = MAS_MAXLISTCOUNT;
-
-       while (len < hlen) {
-               hdr = (void *)(buffer + len);
-
-               switch (hdr->tag) {
-               case MAXLISTCOUNT_T:
-                       if (hdr->len != MAXLISTCOUNT_L)
-                               goto failed;
-
-                       memcpy(&val16, hdr->val, sizeof(val16));
-                       param->maxlistcount = GUINT16_FROM_BE(val16);
-                       break;
-
-               case STARTOFFSET_T:
-                       if (hdr->len != STARTOFFSET_L)
-                               goto failed;
-
-                       memcpy(&val16, hdr->val, sizeof(val16));
-                       param->offset = GUINT16_FROM_BE(val16);
-                       break;
-
-               case SUBJECTLENGTH_T:
-                       if (hdr->len != SUBJECTLENGTH_L)
-                               goto failed;
-
-                       param->subject_length = hdr->val[0];
-                       break;
-
-               case PARAMETERMASK_T:
-                       if (hdr->len != PARAMETERMASK_L)
-                               goto failed;
-
-                       memcpy(&val32, hdr->val, sizeof(val32));
-                       param->filter->parameter_mask = GUINT32_FROM_BE(val32);
-                       break;
-
-               case FILTERMESSAGETYPE_T:
-                       if (hdr->len != FILTERMESSAGETYPE_L)
-                               goto failed;
-
-                       param->filter->type = hdr->val[0];
-                       break;
-
-               case FILTERPERIODBEGIN_T:
-                       param->filter->period_begin = g_malloc0(hdr->len + 1);
-                       memcpy(param->filter->period_begin, hdr->val, hdr->len);
-                       break;
-
-               case FILTERPERIODEND_T:
-                       param->filter->period_end = g_malloc0(hdr->len + 1);
-                       memcpy(param->filter->period_end, hdr->val, hdr->len);
-                       break;
-
-               case FILTERREADSTATUS_T:
-                       if (hdr->len != FILTERREADSTATUS_L)
-                               goto failed;
-
-                       param->filter->read_status = hdr->val[0];
-                       break;
-
-               case FILTERRECIPIENT_T:
-                       param->filter->recipient = g_malloc0(hdr->len + 1);
-                       memcpy(param->filter->recipient, hdr->val, hdr->len);
-                       break;
-
-               case FILTERORIGINATOR_T:
-                       param->filter->originator = g_malloc0(hdr->len + 1);
-                       memcpy(param->filter->originator, hdr->val, hdr->len);
-                       break;
-
-               case FILTERPRIORITY_T:
-                       if (hdr->len != FILTERPRIORITY_L)
-                               goto failed;
-
-                       param->filter->priority = hdr->val[0];
-                       break;
-
-               case ATTACHMENT_T:
-                       if (hdr->len != ATTACHMENT_L)
-                               goto failed;
-
-                       param->attachment = hdr->val[0];
-                       break;
-
-               case CHARSET_T:
-                       if (hdr->len != CHARSET_L)
-                               goto failed;
-
-                       param->charset = hdr->val[0];
-                       break;
-
-               case STATUSINDICATOR_T:
-                       if (hdr->len != STATUSINDICATOR_L)
-                               goto failed;
-
-                       param->status_indicator = hdr->val[0];
-                       break;
-
-               case STATUSVALUE_T:
-                       if (hdr->len != STATUSVALUE_L)
-                               goto failed;
-
-                       param->status_value = hdr->val[0];
-                       break;
-
-               case FRACTIONREQUEST_T:
-                       if (hdr->len != FRACTIONREQUEST_L)
-                               goto failed;
-
-                       param->fraction_request = hdr->val[0];
-                       break;
-
-               case TRANSPARENT_T:
-                       if (hdr->len != TRANSPARENT_L)
-                               goto failed;
-
-                       param->transparent = hdr->val[0];
-                       break;
-
-               case RETRY_T:
-                       if (hdr->len != RETRY_L)
-                               goto failed;
-
-                       param->retry = hdr->val[0];
-                       break;
-
-               case NOTIFICATIONSTATUS_T:
-                       if (hdr->len != NOTIFICATIONSTATUS_L)
-                               goto failed;
-
-                       param->notification_status = hdr->val[0];
-                       break;
-
-               case MASINSTANCEID_T:
-                       if (hdr->len != MASINSTANCEID_L)
-                               goto failed;
-
-                       param->mas_instance = hdr->val[0];
-                       break;
-
-               default:
-                       goto failed;
-               }
-
-               len += hdr->len + sizeof(struct aparam_header);
-       }
-
-       return param;
-
-failed:
-       apparam_free(param);
-
-       return NULL;
-}
-
-static void apparam_free(struct apparam_field *apparams)
-{
-       if (apparams->filter) {
-               g_free(apparams->filter->period_begin);
-               g_free(apparams->filter->period_end);
-               g_free(apparams->filter->recipient);
-               g_free(apparams->filter->originator);
-               g_free(apparams->filter);
-       }
-       g_free(apparams->msetime);
-       g_free(apparams);
-}
-
-static GByteArray *append_apparam_header(GByteArray *buf, uint8_t tag,
-                                                       const void *val)
-{
-       uint8_t outparam[sizeof(struct aparam_header) + FOLDERLISTINGSIZE_L];
-       struct aparam_header *hdr = (struct aparam_header *) outparam;
-
-       switch (tag) {
-       case FOLDERLISTINGSIZE_T:
-               hdr->tag = FOLDERLISTINGSIZE_T;
-               hdr->len = FOLDERLISTINGSIZE_L;
-               memcpy(hdr->val, val, FOLDERLISTINGSIZE_L);
-
-               return g_byte_array_append(buf, outparam,
-                       sizeof(struct aparam_header) + FOLDERLISTINGSIZE_L);
-
-       default:
-               return buf;
-       }
+       /* STRING PARSER : Under implementation */
+       DBG("%s", mas->buffer->str);
 }
 #endif
 
@@ -392,10 +137,8 @@ static void reset_request(struct mas_session *mas)
                mas->buffer = NULL;
        }
 #ifdef TIZEN_PATCH
-       if (mas->apparams) {
-               apparam_free(mas->apparams);
-               mas->apparams = NULL;
-       }
+       map_ap_free(mas->ap);
+       mas->ap = NULL;
 #endif
        mas->nth_call = FALSE;
        mas->finished = FALSE;
@@ -448,7 +191,6 @@ static int mas_get(struct obex_session *os, void *user_data)
        const char *name = obex_get_name(os);
        int ret;
 #ifdef TIZEN_PATCH
-       struct apparam_field *apparams = NULL;
        const uint8_t *buffer = NULL;
        ssize_t rsize = 0;
 #endif
@@ -460,13 +202,6 @@ static int mas_get(struct obex_session *os, void *user_data)
                return -EBADR;
 
 #ifdef TIZEN_PATCH
-       if (mas->apparams) {
-               apparam_free(mas->apparams);
-               mas->apparams = NULL;
-       }
-
-       DBG("GET: name %s type %s mas %p", name, type, mas);
-
        rsize = obex_get_apparam(os, &buffer);
 
        if (rsize < 0) {
@@ -475,14 +210,11 @@ static int mas_get(struct obex_session *os, void *user_data)
                        goto failed;
                }
        } else {
-               apparams = msg_parse_apparam(buffer, rsize);
-
-               if (apparams == NULL) {
+               mas->ap = map_ap_decode(buffer, rsize);
+               if (mas->ap == NULL) {
                        ret = -EBADR;
                        goto failed;
                }
-
-               mas->apparams = apparams;
        }
 #endif
        ret = obex_get_stream_start(os, name);
@@ -504,7 +236,6 @@ static int mas_put(struct obex_session *os, void *user_data)
        const char *name = obex_get_name(os);
        int ret;
 #ifdef TIZEN_PATCH
-       struct apparam_field *apparams = NULL;
        const uint8_t *buffer = NULL;
        ssize_t rsize = 0;
 #endif
@@ -515,13 +246,6 @@ static int mas_put(struct obex_session *os, void *user_data)
                return -EBADR;
 
 #ifdef TIZEN_PATCH
-       if (mas->apparams) {
-               apparam_free(mas->apparams);
-               mas->apparams = NULL;
-       }
-
-       DBG("PUT: name %s type %s mas %p", name, type, mas);
-
        rsize = obex_get_apparam(os, &buffer);
        if (rsize < 0) {
                if (g_ascii_strcasecmp(type, "x-bt/messageStatus") == 0 ||
@@ -530,14 +254,11 @@ static int mas_put(struct obex_session *os, void *user_data)
                        goto failed;
                }
        } else {
-               apparams = msg_parse_apparam(buffer, rsize);
-
-               if (apparams == NULL) {
+               mas->ap = map_ap_decode(buffer, rsize);
+               if (mas->ap == NULL) {
                        ret = -EBADR;
                        goto failed;
                }
-
-               mas->apparams = apparams;
        }
 #endif
        ret = obex_put_stream_start(os, name);
@@ -757,8 +478,8 @@ static void update_inbox_cb(void *session, int err, void *user_data)
        else
                obex_object_set_io_flags(mas, G_IO_OUT, 0);
 }
-#ifdef TIZEN_PATCH
 
+#ifdef TIZEN_PATCH
 static void message_status_cb(void *session, int err, void *user_data)
 {
        struct mas_session *mas = user_data;
@@ -778,9 +499,8 @@ static void folder_size_result_cb(void *session, int err, uint16_t size,
 {
        struct mas_session *mas = user_data;
 
-       mas->outparams = g_byte_array_new();
-       mas->outparams = append_apparam_header(mas->outparams,
-                                       FOLDERLISTINGSIZE_T, &size);
+       map_ap_set_u16(mas->ap, MAP_AP_FOLDERLISTINGSIZE,
+                                       GUINT16_FROM_BE(size));
 
        if (err < 0)
                obex_object_set_io_flags(mas, G_IO_ERR, err);
@@ -818,6 +538,8 @@ static void *folder_listing_open(const char *name, int oflag, mode_t mode,
        struct mas_session *mas = driver_data;
 #ifdef TIZEN_PATCH
        messages_folder_listing_cb cb;
+       uint16_t maxlistcount = 1024;
+       uint16_t offset = 0;
 #endif
 
        if (oflag != O_RDONLY) {
@@ -828,15 +550,20 @@ static void *folder_listing_open(const char *name, int oflag, mode_t mode,
        DBG("name = %s", name);
 
 #ifdef TIZEN_PATCH
-       if (mas->apparams->maxlistcount == 0)
+       if (mas->ap != NULL) {
+               map_ap_get_u16(mas->ap, MAP_AP_MAXLISTCOUNT, &maxlistcount);
+               map_ap_get_u16(mas->ap, MAP_AP_STARTOFFSET, &offset);
+       }
+
+       DBG("Maxlistcount = %d \n offset = %d\n", maxlistcount, offset);
+
+       if (maxlistcount == 0)
                cb = folder_size_result_cb;
        else
                cb = get_folder_listing_cb;
 
        *err = messages_get_folder_listing(mas->backend_data, name,
-                                       mas->apparams->maxlistcount,
-                                       mas->apparams->offset,
-                                       cb, mas);
+                                               maxlistcount, offset, cb, mas);
 #else
        /* 1024 is the default when there was no MaxListCount sent */
        *err = messages_get_folder_listing(mas->backend_data, name, 1024, 0,
@@ -876,6 +603,28 @@ static void *msg_listing_open(const char *name, int oflag, mode_t mode,
                return mas;
 }
 
+#ifdef TIZEN_PATCH
+static void message_get(struct mas_session *mas, const char *name, int *err)
+{
+       DBG("");
+
+       /* TODO: check for Mandatory application parameter
+       * Attachment and Charset and optional parameter fraction request */
+       *err = messages_get_message(mas->backend_data, name, 0,
+                       get_message_cb, mas);
+       mas->buffer = g_string_new("");
+}
+
+static void message_put(struct mas_session *mas, const char *name, int *err)
+{
+       DBG("");
+
+       /* TODO: check for Mandatory application parameter
+       * Charset and optional parameter transparent and retry */
+       mas->buffer = g_string_new("");
+}
+#endif
+
 static void *message_open(const char *name, int oflag, mode_t mode,
                                void *driver_data, size_t *size, int *err)
 {
@@ -883,6 +632,12 @@ static void *message_open(const char *name, int oflag, mode_t mode,
 
        DBG("");
 
+#ifdef TIZEN_PATCH
+       if (oflag == O_RDONLY)
+               message_get(mas, name, err);
+       else
+               message_put(mas, name, err);
+#else
        if (oflag != O_RDONLY) {
                DBG("Message pushing unsupported");
                *err = -EINVAL;
@@ -894,7 +649,7 @@ static void *message_open(const char *name, int oflag, mode_t mode,
                        get_message_cb, mas);
 
        mas->buffer = g_string_new("");
-
+#endif
        if (*err < 0)
                return NULL;
        else
@@ -920,6 +675,7 @@ static void *message_update_open(const char *name, int oflag, mode_t mode,
        else
                return mas;
 }
+
 #ifdef TIZEN_PATCH
 static void *message_status_open(const char *name, int oflag, mode_t mode,
                                        void *driver_data, size_t *size,
@@ -935,11 +691,8 @@ static void *message_status_open(const char *name, int oflag, mode_t mode,
                return NULL;
         }
 
-       if (mas->apparams == NULL)
-               return NULL;
-
-       indicator = mas->apparams->status_indicator;
-       value = mas->apparams->status_value;
+       map_ap_get_u8(mas->ap, MAP_AP_STATUSINDICATOR, &indicator);
+       map_ap_get_u8(mas->ap, MAP_AP_STATUSVALUE, &value);
 
        DBG("Indicator = %d \n value = %d\n", indicator, value);
 
@@ -951,6 +704,36 @@ static void *message_status_open(const char *name, int oflag, mode_t mode,
        else
                return mas;
 }
+
+static ssize_t message_write(void *object, const void *buf, size_t count)
+{
+       struct mas_session *mas = object;
+       GString *string;
+
+       DBG("");
+
+       string = g_string_append_len(mas->buffer, buf, count);
+
+       return string->len;
+}
+
+static int message_close(void *obj)
+{
+       struct mas_session *mas = obj;
+       struct bmessage_property bmessage = { 0, };
+
+       DBG("");
+
+       /* Bmessage body parsing */
+       bmessage_parser(mas, &bmessage);
+
+       if (!mas->finished)
+               messages_abort(mas->backend_data);
+
+       reset_request(mas);
+
+       return 0;
+}
 #endif
 
 static void *any_open(const char *name, int oflag, mode_t mode,
@@ -1029,9 +812,17 @@ static struct obex_mime_type_driver mime_message = {
        .target_size = TARGET_SIZE,
        .mimetype = "x-bt/message",
        .open = message_open,
+#ifdef TIZEN_PATCH
+       .close = message_close,
+#else
        .close = any_close,
+#endif
        .read = any_read,
+#ifdef TIZEN_PATCH
+       .write = message_write,
+#else
        .write = any_write,
+#endif
 };
 
 static struct obex_mime_type_driver mime_folder_listing = {
@@ -1082,11 +873,7 @@ static struct obex_mime_type_driver mime_message_update = {
        .target = MAS_TARGET,
        .target_size = TARGET_SIZE,
        .mimetype = "x-bt/MAP-messageUpdate",
-#ifdef TIZEN_PATCH
        .open = message_update_open,
-#else
-       .open = any_open,
-#endif
        .close = any_close,
        .read = any_read,
        .write = any_write,