2012-01-19 17:01:51

by Slawomir Bochenski

[permalink] [raw]
Subject: Decoding of MAP application parameters.

v5: Change resolving of AP definitions as per discussion on IRC.


2012-01-20 14:54:06

by Johan Hedberg

[permalink] [raw]
Subject: Re: [PATCH obexd v5 1/5] MAP: Implementation of MAP AP core functions

Hi Slawek,

On Thu, Jan 19, 2012, Slawomir Bochenski wrote:
> This adds implementation for creation and destruction of map_ap_t and
> the definitions of MAP supported application parameters.
> ---
> src/map_ap.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
> 1 files changed, 76 insertions(+), 1 deletions(-)

All five patches (v5.1 of 4/5) have been pushed upstream. Thanks.

Johan

2012-01-20 12:25:23

by Slawomir Bochenski

[permalink] [raw]
Subject: [PATCH obexd v5.1 4/5] map_ap.c: Add implementation for map_ap_decode()

---
v5.1:
Fixed pointer arithmetic
Fixed format specifier for unsigned integers

src/map_ap.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 97 insertions(+), 1 deletions(-)

diff --git a/src/map_ap.c b/src/map_ap.c
index b71cd35..1c6185e 100644
--- a/src/map_ap.c
+++ b/src/map_ap.c
@@ -25,6 +25,10 @@
#include <config.h>
#endif

+#include <string.h>
+
+#include "log.h"
+
#include "map_ap.h"

enum ap_type {
@@ -76,6 +80,13 @@ struct ap_entry {
} val;
};

+/* This comes from OBEX specs */
+struct obex_ap_header {
+ uint8_t tag;
+ uint8_t len;
+ uint8_t val[0];
+} __attribute__ ((packed));
+
static int find_ap_def_offset(uint8_t tag)
{
if (tag == 0 || tag > G_N_ELEMENTS(ap_defs))
@@ -111,9 +122,94 @@ void map_ap_free(map_ap_t *ap)
g_hash_table_destroy(ap);
}

+static void ap_decode_u8(map_ap_t *ap, const struct obex_ap_header *hdr)
+{
+ if (hdr->len != 1) {
+ DBG("Value of tag %u is %u byte(s) long instead of expected "
+ "1 byte - skipped!", hdr->tag, hdr->len);
+ return;
+ }
+
+ map_ap_set_u8(ap, hdr->tag, hdr->val[0]);
+}
+
+static void ap_decode_u16(map_ap_t *ap, const struct obex_ap_header *hdr)
+{
+ uint16_t val;
+
+ if (hdr->len != 2) {
+ DBG("Value of tag %u is %u byte(s) long instead of expected "
+ "2 bytes - skipped!", hdr->tag, hdr->len);
+ return;
+ }
+
+ memcpy(&val, hdr->val, sizeof(val));
+ map_ap_set_u16(ap, hdr->tag, GUINT16_FROM_BE(val));
+}
+
+static void ap_decode_u32(map_ap_t *ap, const struct obex_ap_header *hdr)
+{
+ uint32_t val;
+
+ if (hdr->len != 4) {
+ DBG("Value of tag %u is %u byte(s) long instead of expected "
+ "4 bytes - skipped!", hdr->tag, hdr->len);
+ return;
+ }
+
+ memcpy(&val, hdr->val, sizeof(val));
+ map_ap_set_u32(ap, hdr->tag, GUINT32_FROM_BE(val));
+}
+
+static void ap_decode_str(map_ap_t *ap, const struct obex_ap_header *hdr)
+{
+ char *val = g_malloc0(hdr->len + 1);
+
+ memcpy(val, hdr->val, hdr->len);
+ map_ap_set_string(ap, hdr->tag, val);
+
+ g_free(val);
+}
+
map_ap_t *map_ap_decode(const uint8_t *buffer, size_t length)
{
- return NULL;
+ map_ap_t *ap;
+ struct obex_ap_header *hdr;
+ uint32_t done;
+ int offset;
+
+ ap = map_ap_new();
+ if (!ap)
+ return NULL;
+
+ for (done = 0; done < length; done += hdr->len + sizeof(*hdr)) {
+ hdr = (struct obex_ap_header *)(buffer + done);
+
+ offset = find_ap_def_offset(hdr->tag);
+
+ if (offset < 0) {
+ DBG("Unknown tag %u (length %u) - skipped.",
+ hdr->tag, hdr->len);
+ continue;
+ }
+
+ switch (ap_defs[offset].type) {
+ case APT_UINT8:
+ ap_decode_u8(ap, hdr);
+ break;
+ case APT_UINT16:
+ ap_decode_u16(ap, hdr);
+ break;
+ case APT_UINT32:
+ ap_decode_u32(ap, hdr);
+ break;
+ case APT_STR:
+ ap_decode_str(ap, hdr);
+ break;
+ }
+ }
+
+ return ap;
}

uint8_t *map_ap_encode(map_ap_t *ap, size_t *length)
--
1.7.4.1


2012-01-19 17:01:56

by Slawomir Bochenski

[permalink] [raw]
Subject: [PATCH obexd v5 5/5] map_ap.c: Add dumping of map_ap_t after decoding

---
src/map_ap.c | 25 +++++++++++++++++++++++++
1 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/src/map_ap.c b/src/map_ap.c
index 6c9c623..cbf433f 100644
--- a/src/map_ap.c
+++ b/src/map_ap.c
@@ -95,6 +95,29 @@ static int find_ap_def_offset(uint8_t tag)
return tag - 1;
}

+static void ap_entry_dump(gpointer tag, gpointer val, gpointer user_data)
+{
+ struct ap_entry *entry = val;
+ int offset;
+
+ offset = find_ap_def_offset(GPOINTER_TO_INT(tag));
+
+ switch (ap_defs[offset].type) {
+ case APT_UINT8:
+ DBG("%-30s %08x", ap_defs[offset].name, entry->val.u8);
+ break;
+ case APT_UINT16:
+ DBG("%-30s %08x", ap_defs[offset].name, entry->val.u16);
+ break;
+ case APT_UINT32:
+ DBG("%-30s %08x", ap_defs[offset].name, entry->val.u32);
+ break;
+ case APT_STR:
+ DBG("%-30s %s", ap_defs[offset].name, entry->val.str);
+ break;
+ }
+}
+
static void ap_entry_free(gpointer val)
{
struct ap_entry *entry = val;
@@ -209,6 +232,8 @@ map_ap_t *map_ap_decode(const uint8_t *buffer, size_t length)
}
}

+ g_hash_table_foreach(ap, ap_entry_dump, NULL);
+
return ap;
}

--
1.7.5.1


2012-01-19 17:01:55

by Slawomir Bochenski

[permalink] [raw]
Subject: [PATCH obexd v5 4/5] map_ap.c: Add implementation for map_ap_decode()

---
src/map_ap.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 97 insertions(+), 1 deletions(-)

diff --git a/src/map_ap.c b/src/map_ap.c
index b71cd35..6c9c623 100644
--- a/src/map_ap.c
+++ b/src/map_ap.c
@@ -25,6 +25,10 @@
#include <config.h>
#endif

+#include <string.h>
+
+#include "log.h"
+
#include "map_ap.h"

enum ap_type {
@@ -76,6 +80,13 @@ struct ap_entry {
} val;
};

+/* This comes from OBEX specs */
+struct obex_ap_header {
+ uint8_t tag;
+ uint8_t len;
+ uint8_t val[0];
+} __attribute__ ((packed));
+
static int find_ap_def_offset(uint8_t tag)
{
if (tag == 0 || tag > G_N_ELEMENTS(ap_defs))
@@ -111,9 +122,94 @@ void map_ap_free(map_ap_t *ap)
g_hash_table_destroy(ap);
}

+static void ap_decode_u8(map_ap_t *ap, const struct obex_ap_header *hdr)
+{
+ if (hdr->len != 1) {
+ DBG("Value of tag %d is %d bytes long instead of expected "
+ "1 byte - skipped!", hdr->tag, hdr->len);
+ return;
+ }
+
+ map_ap_set_u8(ap, hdr->tag, hdr->val[0]);
+}
+
+static void ap_decode_u16(map_ap_t *ap, const struct obex_ap_header *hdr)
+{
+ uint16_t val;
+
+ if (hdr->len != 2) {
+ DBG("Value of tag %d is %d bytes long instead of expected "
+ "2 bytes - skipped!", hdr->tag, hdr->len);
+ return;
+ }
+
+ memcpy(&val, hdr->val, sizeof(val));
+ map_ap_set_u16(ap, hdr->tag, GUINT16_FROM_BE(val));
+}
+
+static void ap_decode_u32(map_ap_t *ap, const struct obex_ap_header *hdr)
+{
+ uint32_t val;
+
+ if (hdr->len != 4) {
+ DBG("Value of tag %d is %d bytes long instead of expected "
+ "4 bytes - skipped!", hdr->tag, hdr->len);
+ return;
+ }
+
+ memcpy(&val, hdr->val, sizeof(val));
+ map_ap_set_u32(ap, hdr->tag, GUINT32_FROM_BE(val));
+}
+
+static void ap_decode_str(map_ap_t *ap, const struct obex_ap_header *hdr)
+{
+ char *val = g_malloc0(hdr->len + 1);
+
+ memcpy(val, hdr->val, hdr->len);
+ map_ap_set_string(ap, hdr->tag, val);
+
+ g_free(val);
+}
+
map_ap_t *map_ap_decode(const uint8_t *buffer, size_t length)
{
- return NULL;
+ map_ap_t *ap;
+ struct obex_ap_header *hdr;
+ uint32_t done;
+ int offset;
+
+ ap = map_ap_new();
+ if (!ap)
+ return NULL;
+
+ for (done = 0; done < length; done += hdr->len + sizeof(*hdr)) {
+ hdr = (void *) buffer + done;
+
+ offset = find_ap_def_offset(hdr->tag);
+
+ if (offset < 0) {
+ DBG("Unknown tag %d (length %d) - skipped.",
+ hdr->tag, hdr->len);
+ continue;
+ }
+
+ switch (ap_defs[offset].type) {
+ case APT_UINT8:
+ ap_decode_u8(ap, hdr);
+ break;
+ case APT_UINT16:
+ ap_decode_u16(ap, hdr);
+ break;
+ case APT_UINT32:
+ ap_decode_u32(ap, hdr);
+ break;
+ case APT_STR:
+ ap_decode_str(ap, hdr);
+ break;
+ }
+ }
+
+ return ap;
}

uint8_t *map_ap_encode(map_ap_t *ap, size_t *length)
--
1.7.5.1


2012-01-19 17:01:54

by Slawomir Bochenski

[permalink] [raw]
Subject: [PATCH obexd v5 3/5] map_ap.c: Add implementation for map_ap_set_*

---
src/map_ap.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 52 insertions(+), 4 deletions(-)

diff --git a/src/map_ap.c b/src/map_ap.c
index b3b4a3f..b71cd35 100644
--- a/src/map_ap.c
+++ b/src/map_ap.c
@@ -145,20 +145,68 @@ const char *map_ap_get_string(map_ap_t *ap, enum map_ap_tag tag)

gboolean map_ap_set_u8(map_ap_t *ap, enum map_ap_tag tag, uint8_t val)
{
- return FALSE;
+ struct ap_entry *entry;
+ int offset = find_ap_def_offset(tag);
+
+ if (offset < 0 || ap_defs[offset].type != APT_UINT8)
+ return FALSE;
+
+ entry = g_new0(struct ap_entry, 1);
+ entry->tag = tag;
+ entry->val.u8 = val;
+
+ g_hash_table_insert(ap, GINT_TO_POINTER(tag), entry);
+
+ return TRUE;
}

gboolean map_ap_set_u16(map_ap_t *ap, enum map_ap_tag tag, uint16_t val)
{
- return FALSE;
+ struct ap_entry *entry;
+ int offset = find_ap_def_offset(tag);
+
+ if (offset < 0 || ap_defs[offset].type != APT_UINT16)
+ return FALSE;
+
+ entry = g_new0(struct ap_entry, 1);
+ entry->tag = tag;
+ entry->val.u16 = val;
+
+ g_hash_table_insert(ap, GINT_TO_POINTER(tag), entry);
+
+ return TRUE;
}

gboolean map_ap_set_u32(map_ap_t *ap, enum map_ap_tag tag, uint32_t val)
{
- return FALSE;
+ struct ap_entry *entry;
+ int offset = find_ap_def_offset(tag);
+
+ if (offset < 0 || ap_defs[offset].type != APT_UINT32)
+ return FALSE;
+
+ entry = g_new0(struct ap_entry, 1);
+ entry->tag = tag;
+ entry->val.u32 = val;
+
+ g_hash_table_insert(ap, GINT_TO_POINTER(tag), entry);
+
+ return TRUE;
}

gboolean map_ap_set_string(map_ap_t *ap, enum map_ap_tag tag, const char *val)
{
- return FALSE;
+ struct ap_entry *entry;
+ int offset = find_ap_def_offset(tag);
+
+ if (offset < 0 || ap_defs[offset].type != APT_STR)
+ return FALSE;
+
+ entry = g_new0(struct ap_entry, 1);
+ entry->tag = tag;
+ entry->val.str = g_strdup(val);
+
+ g_hash_table_insert(ap, GINT_TO_POINTER(tag), entry);
+
+ return TRUE;
}
--
1.7.5.1


2012-01-19 17:01:53

by Slawomir Bochenski

[permalink] [raw]
Subject: [PATCH obexd v5 2/5] map_ap.h: Remove MAP_AP_INVALID

Due to the change in MAP AP definitions resolving, there is no need for
guard value.
---
src/map_ap.h | 1 -
1 files changed, 0 insertions(+), 1 deletions(-)

diff --git a/src/map_ap.h b/src/map_ap.h
index c0fb79a..24254af 100644
--- a/src/map_ap.h
+++ b/src/map_ap.h
@@ -51,7 +51,6 @@ enum map_ap_tag {
MAP_AP_STATUSINDICATOR = 0x17, /* uint8_t */
MAP_AP_STATUSVALUE = 0x18, /* uint8_t */
MAP_AP_MSETIME = 0x19, /* char * */
- MAP_AP_INVALID = 0x100,
};

/* Data type representing MAP application parameters. Consider opaque. */
--
1.7.5.1


2012-01-19 17:01:52

by Slawomir Bochenski

[permalink] [raw]
Subject: [PATCH obexd v5 1/5] MAP: Implementation of MAP AP core functions

This adds implementation for creation and destruction of map_ap_t and
the definitions of MAP supported application parameters.
---
src/map_ap.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 76 insertions(+), 1 deletions(-)

diff --git a/src/map_ap.c b/src/map_ap.c
index 9d13adf..b3b4a3f 100644
--- a/src/map_ap.c
+++ b/src/map_ap.c
@@ -27,13 +27,88 @@

#include "map_ap.h"

+enum ap_type {
+ APT_UINT8,
+ APT_UINT16,
+ APT_UINT32,
+ APT_STR
+};
+
+/* NOTE: ap_defs array has to be kept in sync with map_ap_tag. */
+static const struct ap_def {
+ const char *name;
+ enum ap_type type;
+} ap_defs[] = {
+ { "MAXLISTCOUNT", APT_UINT16 },
+ { "STARTOFFSET", APT_UINT16 },
+ { "FILTERMESSAGETYPE", APT_UINT8 },
+ { "FILTERPERIODBEGIN", APT_STR },
+ { "FILTERPERIODEND", APT_STR },
+ { "FILTERREADSTATUS", APT_UINT8 },
+ { "FILTERRECIPIENT", APT_STR },
+ { "FILTERORIGINATOR", APT_STR },
+ { "FILTERPRIORITY", APT_UINT8 },
+ { "ATTACHMENT", APT_UINT8 },
+ { "TRANSPARENT", APT_UINT8 },
+ { "RETRY", APT_UINT8 },
+ { "NEWMESSAGE", APT_UINT8 },
+ { "NOTIFICATIONSTATUS", APT_UINT8 },
+ { "MASINSTANCEID", APT_UINT8 },
+ { "PARAMETERMASK", APT_UINT32 },
+ { "FOLDERLISTINGSIZE", APT_UINT16 },
+ { "MESSAGESLISTINGSIZE", APT_UINT16 },
+ { "SUBJECTLENGTH", APT_UINT8 },
+ { "CHARSET", APT_UINT8 },
+ { "FRACTIONREQUEST", APT_UINT8 },
+ { "FRACTIONDELIVER", APT_UINT8 },
+ { "STATUSINDICATOR", APT_UINT8 },
+ { "STATUSVALUE", APT_UINT8 },
+ { "MSETIME", APT_STR },
+};
+
+struct ap_entry {
+ enum map_ap_tag tag;
+ union {
+ uint32_t u32;
+ uint16_t u16;
+ uint8_t u8;
+ char *str;
+ } val;
+};
+
+static int find_ap_def_offset(uint8_t tag)
+{
+ if (tag == 0 || tag > G_N_ELEMENTS(ap_defs))
+ return -1;
+
+ return tag - 1;
+}
+
+static void ap_entry_free(gpointer val)
+{
+ struct ap_entry *entry = val;
+ int offset;
+
+ offset = find_ap_def_offset(entry->tag);
+
+ if (offset >= 0 && ap_defs[offset].type == APT_STR)
+ g_free(entry->val.str);
+
+ g_free(entry);
+}
+
map_ap_t *map_ap_new(void)
{
- return NULL;
+ return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
+ ap_entry_free);
}

void map_ap_free(map_ap_t *ap)
{
+ if (!ap)
+ return;
+
+ g_hash_table_destroy(ap);
}

map_ap_t *map_ap_decode(const uint8_t *buffer, size_t length)
--
1.7.5.1