linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH obexd 0/9] GObexApparam API
@ 2012-08-09 10:32 Luiz Augusto von Dentz
  2012-08-09 10:32 ` [PATCH obexd 1/9] gobex: Introduce " Luiz Augusto von Dentz
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: Luiz Augusto von Dentz @ 2012-08-09 10:32 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

GObexApparam can encode/decode OBEX application parameter in a generic
way, only the tags are expecific to each profile.

Some remarks:

  - Tag data is stored in network order/big endian because its type is
    not known.
  - Internally it uses a GHashTable to store the values which doesn't
    guarantee the order of the tag will be exact the same as inserted.

Luiz Augusto von Dentz (9):
  gobex: Introduce GObexApparam API
  gobex: Add unit tests for GObexApparam API
  gobex: Integrate GObexApparam with GObexHeader
  gobex: Add unit test for encoding/decoding apparam headers
  gobex: Add debug option to apparam
  client: Port PBAP module to use GObexApparam
  PBAP: Make use of GObexApparam API
  MAP: Make use of GObexApparam API
  core: Remove map_ap.c

 Makefile.am               |  18 +-
 client/pbap.c             | 181 +++++-------------
 gobex/gobex-apparam.c     | 355 +++++++++++++++++++++++++++++++++++
 gobex/gobex-apparam.h     |  59 ++++++
 gobex/gobex-debug.h       |   7 +-
 gobex/gobex-header.c      |  25 +++
 gobex/gobex-header.h      |   3 +
 gobex/gobex.c             |  11 +-
 plugins/mas.c             |  81 ++++----
 plugins/pbap.c            | 180 ++++--------------
 plugins/phonebook.h       |   2 +-
 src/map_ap.c              | 466 ----------------------------------------------
 src/map_ap.h              |  63 -------
 unit/test-gobex-apparam.c | 422 +++++++++++++++++++++++++++++++++++++++++
 unit/test-gobex-header.c  |  46 +++++
 15 files changed, 1058 insertions(+), 861 deletions(-)
 create mode 100644 gobex/gobex-apparam.c
 create mode 100644 gobex/gobex-apparam.h
 delete mode 100644 src/map_ap.c
 create mode 100644 unit/test-gobex-apparam.c

-- 
1.7.11.2


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH obexd 1/9] gobex: Introduce GObexApparam API
  2012-08-09 10:32 [PATCH obexd 0/9] GObexApparam API Luiz Augusto von Dentz
@ 2012-08-09 10:32 ` Luiz Augusto von Dentz
  2012-08-09 10:32 ` [PATCH obexd 2/9] gobex: Add unit tests for " Luiz Augusto von Dentz
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Luiz Augusto von Dentz @ 2012-08-09 10:32 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

GObexApparam abstract the handling of application parameter tags, it
can be used to read/parse application parameter header data using
g_obex_apparam_get_* functions or to generate the data using
g_obex_apparam_set_*.
---
 Makefile.am           |   3 +-
 gobex/gobex-apparam.c | 314 ++++++++++++++++++++++++++++++++++++++++++++++++++
 gobex/gobex-apparam.h |  59 ++++++++++
 3 files changed, 375 insertions(+), 1 deletion(-)
 create mode 100644 gobex/gobex-apparam.c
 create mode 100644 gobex/gobex-apparam.h

diff --git a/Makefile.am b/Makefile.am
index 97a1553..a1b4da0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -20,7 +20,8 @@ gobex_sources = gobex/gobex.h gobex/gobex.c \
 			gobex/gobex-defs.h gobex/gobex-defs.c \
 			gobex/gobex-packet.c gobex/gobex-packet.h \
 			gobex/gobex-header.c gobex/gobex-header.h \
-			gobex/gobex-transfer.c gobex/gobex-debug.h
+			gobex/gobex-transfer.c gobex/gobex-debug.h \
+			gobex/gobex-apparam.h gobex/gobex-apparam.c
 
 noinst_PROGRAMS =
 libexec_PROGRAMS =
diff --git a/gobex/gobex-apparam.c b/gobex/gobex-apparam.c
new file mode 100644
index 0000000..09bf034
--- /dev/null
+++ b/gobex/gobex-apparam.c
@@ -0,0 +1,314 @@
+/*
+ *
+ *  OBEX library with GLib integration
+ *
+ *  Copyright (C) 2012  Intel Corporation.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "gobex-apparam.h"
+
+struct _GObexApparam {
+	GHashTable *tags;
+};
+
+struct apparam_tag {
+	guint8 id;
+	guint8 len;
+	union {
+		/* Data is stored in network order */
+		char string[0];
+		guint8 data[0];
+		guint8 u8;
+		guint16 u16;
+		guint32 u32;
+		guint64 u64;
+	} value;
+} __attribute__ ((packed));
+
+static struct apparam_tag *tag_new(guint8 id, guint8 len, const void *data)
+{
+	struct apparam_tag *tag;
+
+	tag = g_malloc0(2 + len);
+	tag->id = id;
+	tag->len = len;
+	memcpy(tag->value.data, data, len);
+
+	return tag;
+}
+
+static GObexApparam *g_obex_apparam_new(void)
+{
+	GObexApparam *apparam;
+
+	apparam = g_new0(GObexApparam, 1);
+	apparam->tags = g_hash_table_new_full(g_direct_hash, g_direct_equal,
+							NULL, g_free);
+
+	return apparam;
+}
+
+static struct apparam_tag *apparam_tag_decode(const void *data, gsize size,
+							gsize *parsed)
+{
+	struct apparam_tag *tag;
+	const guint8 *ptr = data;
+	guint8 id;
+	guint8 len;
+
+	if (size < 2)
+		return NULL;
+
+	id = ptr[0];
+	len = ptr[1];
+
+	if (len > size - 2)
+		return NULL;
+
+	tag = tag_new(id, len, ptr + 2);
+	if (tag == NULL)
+		return NULL;
+
+	*parsed = 2 + tag->len;
+
+	return tag;
+}
+
+GObexApparam *g_obex_apparam_decode(const void *data, gsize size)
+{
+	GObexApparam *apparam;
+	GHashTable *tags;
+	gsize count = 0;
+
+	if (size < 2)
+		return NULL;
+
+	apparam = g_obex_apparam_new();
+
+	tags = apparam->tags;
+	while (count < size) {
+		struct apparam_tag *tag;
+		gsize parsed;
+
+		tag = apparam_tag_decode(data + count, size - count, &parsed);
+		if (tag == NULL)
+			break;
+
+		g_hash_table_insert(tags, GUINT_TO_POINTER(tag->id), tag);
+
+		count += parsed;
+	}
+
+	if (count != size) {
+		g_obex_apparam_free(apparam);
+		return NULL;
+	}
+
+	return apparam;
+}
+
+static gssize tag_encode(struct apparam_tag *tag, void *buf, gsize len)
+{
+	gsize count = 2 + tag->len;
+
+	if (len < count)
+		return -ENOBUFS;
+
+	memcpy(buf, tag, count);
+
+	return count;
+}
+
+gssize g_obex_apparam_encode(GObexApparam *apparam, void *buf, gsize len)
+{
+	gsize count = 0;
+	gssize ret;
+	GHashTableIter iter;
+	gpointer key, value;
+
+	g_hash_table_iter_init(&iter, apparam->tags);
+	while (g_hash_table_iter_next(&iter, &key, &value)) {
+		struct apparam_tag *tag = value;
+
+		ret = tag_encode(tag, buf + count, len - count);
+		if (ret < 0)
+			return ret;
+
+		count += ret;
+	}
+
+	return count;
+}
+
+GObexApparam *g_obex_apparam_set_bytes(GObexApparam *apparam, guint8 id,
+						const void *value, gsize len)
+{
+	struct apparam_tag *tag;
+
+	if (apparam == NULL)
+		apparam = g_obex_apparam_new();
+
+	tag = tag_new(id, len, value);
+	g_hash_table_replace(apparam->tags, GUINT_TO_POINTER(id), tag);
+
+	return apparam;
+}
+
+GObexApparam *g_obex_apparam_set_uint8(GObexApparam *apparam, guint8 id,
+							guint8 value)
+{
+	return g_obex_apparam_set_bytes(apparam, id, &value, 1);
+}
+
+GObexApparam *g_obex_apparam_set_uint16(GObexApparam *apparam, guint8 id,
+							guint16 value)
+{
+	guint16 num = g_htons(value);
+
+	return g_obex_apparam_set_bytes(apparam, id, &num, 2);
+}
+
+GObexApparam *g_obex_apparam_set_uint32(GObexApparam *apparam, guint8 id,
+							guint32 value)
+{
+	guint32 num = g_htonl(value);
+
+	return g_obex_apparam_set_bytes(apparam, id, &num, 4);
+}
+
+GObexApparam *g_obex_apparam_set_uint64(GObexApparam *apparam, guint8 id,
+							guint64 value)
+{
+	guint64 num = GUINT64_TO_BE(value);
+
+	return g_obex_apparam_set_bytes(apparam, id, &num, 8);
+}
+
+GObexApparam *g_obex_apparam_set_string(GObexApparam *apparam, guint8 id,
+							const char *value)
+{
+	gsize len;
+
+	len = strlen(value) + 1;
+	if (len > G_MAXUINT8) {
+		((char *) value)[G_MAXUINT8 - 1] = '\0';
+		len = G_MAXUINT8;
+	}
+
+	return g_obex_apparam_set_bytes(apparam, id, value, len);
+}
+
+gboolean g_obex_apparam_get_uint8(GObexApparam *apparam, guint8 id,
+							guint8 *dest)
+{
+	struct apparam_tag *tag;
+
+	tag = g_hash_table_lookup(apparam->tags, GUINT_TO_POINTER(id));
+	if (tag == NULL)
+		return FALSE;
+
+	*dest = tag->value.u8;
+	return TRUE;
+}
+
+gboolean g_obex_apparam_get_uint16(GObexApparam *apparam, guint8 id,
+							guint16 *dest)
+{
+	struct apparam_tag *tag;
+
+	tag = g_hash_table_lookup(apparam->tags, GUINT_TO_POINTER(id));
+	if (tag == NULL)
+		return FALSE;
+
+	if (tag->len < sizeof(*dest))
+		return FALSE;
+
+	*dest = g_ntohs(tag->value.u16);
+	return TRUE;
+}
+
+gboolean g_obex_apparam_get_uint32(GObexApparam *apparam, guint8 id,
+							guint32 *dest)
+{
+	struct apparam_tag *tag;
+
+	tag = g_hash_table_lookup(apparam->tags, GUINT_TO_POINTER(id));
+	if (tag == NULL)
+		return FALSE;
+
+	if (tag->len < sizeof(*dest))
+		return FALSE;
+
+	*dest = g_ntohl(tag->value.u32);
+	return TRUE;
+}
+
+gboolean g_obex_apparam_get_uint64(GObexApparam *apparam, guint8 id,
+							guint64 *dest)
+{
+	struct apparam_tag *tag;
+
+	tag = g_hash_table_lookup(apparam->tags, GUINT_TO_POINTER(id));
+	if (tag == NULL)
+		return FALSE;
+
+	if (tag->len < sizeof(*dest))
+		return FALSE;
+
+	*dest = GUINT64_FROM_BE(tag->value.u64);
+	return TRUE;
+}
+
+char *g_obex_apparam_get_string(GObexApparam *apparam, guint8 id)
+{
+	struct apparam_tag *tag;
+
+	tag = g_hash_table_lookup(apparam->tags, GUINT_TO_POINTER(id));
+	if (tag == NULL)
+		return NULL;
+
+	return g_strndup(tag->value.string, tag->len);
+}
+
+gboolean g_obex_apparam_get_bytes(GObexApparam *apparam, guint8 id,
+					const guint8 **val, gsize *len)
+{
+	struct apparam_tag *tag;
+
+	tag = g_hash_table_lookup(apparam->tags, GUINT_TO_POINTER(id));
+	if (tag == NULL)
+		return FALSE;
+
+	*len = tag->len;
+	*val = tag->value.data;
+
+	return TRUE;
+}
+
+void g_obex_apparam_free(GObexApparam *apparam)
+{
+	g_hash_table_unref(apparam->tags);
+	g_free(apparam);
+}
diff --git a/gobex/gobex-apparam.h b/gobex/gobex-apparam.h
new file mode 100644
index 0000000..46e20d2
--- /dev/null
+++ b/gobex/gobex-apparam.h
@@ -0,0 +1,59 @@
+/*
+ *
+ *  OBEX library with GLib integration
+ *
+ *  Copyright (C) 2012  Intel Corporation.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __GOBEX_APPARAM_H
+#define __GOBEX_APPARAM_H
+
+#include <glib.h>
+
+typedef struct _GObexApparam GObexApparam;
+
+GObexApparam *g_obex_apparam_decode(const void *data, gsize size);
+gssize g_obex_apparam_encode(GObexApparam *apparam, void *buf, gsize size);
+
+GObexApparam *g_obex_apparam_set_bytes(GObexApparam *apparam, guint8 id,
+						const void *value, gsize size);
+GObexApparam *g_obex_apparam_set_uint8(GObexApparam *apparam, guint8 id,
+							guint8 value);
+GObexApparam *g_obex_apparam_set_uint16(GObexApparam *apparam, guint8 id,
+							guint16 value);
+GObexApparam *g_obex_apparam_set_uint32(GObexApparam *apparam, guint8 id,
+							guint32 value);
+GObexApparam *g_obex_apparam_set_uint64(GObexApparam *apparam, guint8 id,
+							guint64 value);
+GObexApparam *g_obex_apparam_set_string(GObexApparam *apparam, guint8 id,
+							const char *value);
+
+gboolean g_obex_apparam_get_bytes(GObexApparam *apparam, guint8 id,
+					const guint8 **val, gsize *len);
+gboolean g_obex_apparam_get_uint8(GObexApparam *apparam, guint8 id,
+							guint8 *value);
+gboolean g_obex_apparam_get_uint16(GObexApparam *apparam, guint8 id,
+							guint16 *value);
+gboolean g_obex_apparam_get_uint32(GObexApparam *apparam, guint8 id,
+							guint32 *value);
+gboolean g_obex_apparam_get_uint64(GObexApparam *apparam, guint8 id,
+							guint64 *value);
+char *g_obex_apparam_get_string(GObexApparam *apparam, guint8 id);
+
+void g_obex_apparam_free(GObexApparam *apparam);
+
+#endif /* __GOBEX_APPARAM_H */
-- 
1.7.11.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH obexd 2/9] gobex: Add unit tests for GObexApparam API
  2012-08-09 10:32 [PATCH obexd 0/9] GObexApparam API Luiz Augusto von Dentz
  2012-08-09 10:32 ` [PATCH obexd 1/9] gobex: Introduce " Luiz Augusto von Dentz
@ 2012-08-09 10:32 ` Luiz Augusto von Dentz
  2012-08-09 10:32 ` [PATCH obexd 3/9] gobex: Integrate GObexApparam with GObexHeader Luiz Augusto von Dentz
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Luiz Augusto von Dentz @ 2012-08-09 10:32 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

---
 Makefile.am               |  11 +-
 unit/test-gobex-apparam.c | 422 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 430 insertions(+), 3 deletions(-)
 create mode 100644 unit/test-gobex-apparam.c

diff --git a/Makefile.am b/Makefile.am
index a1b4da0..4757f60 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -166,11 +166,12 @@ plugins/phonebook.c: plugins/@PHONEBOOK_DRIVER@
 plugins/messages.c: plugins/@MESSAGES_DRIVER@
 	$(AM_V_GEN)$(LN_S) @abs_top_srcdir@/$< $@
 
-TESTS = unit/test-gobex-header unit/test-gobex-packet unit/test-gobex \
-						unit/test-gobex-transfer
+TESTS = unit/test-gobex-apparam unit/test-gobex-header unit/test-gobex-packet \
+				unit/test-gobex unit/test-gobex-transfer
 
 noinst_PROGRAMS += unit/test-gobex-header unit/test-gobex-packet \
-				unit/test-gobex unit/test-gobex-transfer
+			unit/test-gobex unit/test-gobex-transfer \
+			unit/test-gobex-apparam
 
 unit_test_gobex_SOURCES = $(gobex_sources) unit/test-gobex.c \
 							unit/util.c unit/util.h
@@ -188,6 +189,10 @@ unit_test_gobex_transfer_SOURCES = $(gobex_sources) unit/util.c unit/util.h \
 						unit/test-gobex-transfer.c
 unit_test_gobex_transfer_LDADD = @GLIB_LIBS@
 
+unit_test_gobex_apparam_SOURCES = $(gobex_sources) unit/util.c unit/util.h \
+						unit/test-gobex-apparam.c
+unit_test_gobex_apparam_LDADD = @GLIB_LIBS@
+
 if READLINE
 noinst_PROGRAMS += tools/test-client
 tools_test_client_SOURCES = $(gobex_sources) $(btio_sources) \
diff --git a/unit/test-gobex-apparam.c b/unit/test-gobex-apparam.c
new file mode 100644
index 0000000..573de99
--- /dev/null
+++ b/unit/test-gobex-apparam.c
@@ -0,0 +1,422 @@
+/*
+ *
+ *  OBEX library with GLib integration
+ *
+ *  Copyright (C) 2012  Intel Corporation.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include <stdint.h>
+#include <string.h>
+
+#include <gobex/gobex-apparam.h>
+
+#include "util.h"
+
+#define TAG_U8 0x00
+#define TAG_U16 0x01
+#define TAG_U32 0x02
+#define TAG_U64 0x03
+#define TAG_STRING 0x04
+#define TAG_BYTES 0x05
+
+static uint8_t tag_nval_short[] = { TAG_U8 };
+static uint8_t tag_nval_data[] = { TAG_U8, 0x01 };
+static uint8_t tag_nval2_short[] = { TAG_U8, 0x01, 0x1, TAG_U16 };
+static uint8_t tag_nval2_data[] = { TAG_U8, 0x01, 0x1, TAG_U16, 0x02 };
+static uint8_t tag_uint8[] = { TAG_U8, 0x01, 0x01 };
+static uint8_t tag_uint16[] = { TAG_U16, 0x02, 0x01, 0x02 };
+static uint8_t tag_uint32[] = { TAG_U32, 0x04, 0x01, 0x02, 0x03, 0x04 };
+static uint8_t tag_uint64[] = { TAG_U64, 0x08, 0x01, 0x02, 0x03, 0x04,
+						0x05, 0x06, 0x07, 0x08 };
+static uint8_t tag_string[] = { TAG_STRING, 0x04, 'A', 'B', 'C', '\0' };
+static uint8_t tag_bytes[257] = { TAG_BYTES, 0xFF };
+static uint8_t tag_multi[] = { TAG_U8, 0x01, 0x01,
+				TAG_U16, 0x02, 0x01, 0x02,
+				TAG_U32, 0x04, 0x01, 0x02, 0x03, 0x04,
+				TAG_U64, 0x08, 0x01, 0x02, 0x03, 0x04,
+						0x05, 0x06, 0x07, 0x08,
+				TAG_STRING, 0x04, 'A', 'B', 'C', '\0' };
+
+
+static GObexApparam *parse_and_decode(const void *data, gsize size)
+{
+	GObexApparam *apparam;
+	guint8 encoded[1024];
+	gsize len;
+
+	apparam = g_obex_apparam_decode(data, size);
+
+	g_assert(apparam != NULL);
+
+	len = g_obex_apparam_encode(apparam, encoded, sizeof(encoded));
+
+	assert_memequal(data, size, encoded, len);
+
+	return apparam;
+}
+
+static void test_apparam_nval_short(void)
+{
+	GObexApparam *apparam;
+
+	apparam = g_obex_apparam_decode(tag_nval_short,
+						sizeof(tag_nval_short));
+
+	g_assert(apparam == NULL);
+}
+
+static void test_apparam_nval_data(void)
+{
+	GObexApparam *apparam;
+
+	apparam = g_obex_apparam_decode(tag_nval_data,
+						sizeof(tag_nval_data));
+
+	g_assert(apparam == NULL);
+}
+
+static void test_apparam_nval2_short(void)
+{
+	GObexApparam *apparam;
+
+	apparam = g_obex_apparam_decode(tag_nval2_short,
+						sizeof(tag_nval2_short));
+
+	g_assert(apparam == NULL);
+}
+
+static void test_apparam_nval2_data(void)
+{
+	GObexApparam *apparam;
+
+	apparam = g_obex_apparam_decode(tag_nval2_data,
+						sizeof(tag_nval2_data));
+
+	g_assert(apparam == NULL);
+}
+
+static void test_apparam_get_uint8(void)
+{
+	GObexApparam *apparam;
+	guint8 data;
+	gboolean ret;
+
+	apparam = parse_and_decode(tag_uint8, sizeof(tag_uint8));
+
+	ret = g_obex_apparam_get_uint8(apparam, TAG_U8, &data);
+
+	g_assert(ret == TRUE);
+	g_assert(data == 0x01);
+
+	g_obex_apparam_free(apparam);
+}
+
+static void test_apparam_get_uint16(void)
+{
+	GObexApparam *apparam;
+	uint16_t data;
+	gboolean ret;
+
+	apparam = parse_and_decode(tag_uint16, sizeof(tag_uint16));
+
+	ret = g_obex_apparam_get_uint16(apparam, TAG_U16, &data);
+
+	g_assert(ret == TRUE);
+	g_assert(data == 0x0102);
+
+	g_obex_apparam_free(apparam);
+}
+
+static void test_apparam_get_uint32(void)
+{
+	GObexApparam *apparam;
+	uint32_t data;
+	gboolean ret;
+
+	apparam = parse_and_decode(tag_uint32, sizeof(tag_uint32));
+
+	ret = g_obex_apparam_get_uint32(apparam, TAG_U32, &data);
+
+	g_assert(ret == TRUE);
+	g_assert(data == 0x01020304);
+
+	g_obex_apparam_free(apparam);
+}
+
+static void test_apparam_get_uint64(void)
+{
+	GObexApparam *apparam;
+	uint64_t data;
+	gboolean ret;
+
+	apparam = parse_and_decode(tag_uint64, sizeof(tag_uint64));
+
+	ret = g_obex_apparam_get_uint64(apparam, TAG_U64, &data);
+
+	g_assert(ret == TRUE);
+	g_assert(data == 0x0102030405060708);
+
+	g_obex_apparam_free(apparam);
+}
+
+static void test_apparam_get_string(void)
+{
+	GObexApparam *apparam;
+	char *string;
+
+	apparam = parse_and_decode(tag_string, sizeof(tag_string));
+
+	string = g_obex_apparam_get_string(apparam, TAG_STRING);
+
+	g_assert(string != NULL);
+	g_assert_cmpstr(string, ==, "ABC");
+
+	g_free(string);
+	g_obex_apparam_free(apparam);
+}
+
+static void test_apparam_get_bytes(void)
+{
+	GObexApparam *apparam;
+	const uint8_t *data;
+	gsize len;
+	gboolean ret;
+
+	apparam = parse_and_decode(tag_bytes, sizeof(tag_bytes));
+
+	ret = g_obex_apparam_get_bytes(apparam, TAG_BYTES, &data, &len);
+
+	g_assert(ret == TRUE);
+	assert_memequal(tag_bytes + 2, sizeof(tag_bytes) - 2, data, len);
+
+	g_obex_apparam_free(apparam);
+}
+
+static void test_apparam_get_multi(void)
+{
+	GObexApparam *apparam;
+	char *string;
+	uint8_t data8;
+	uint16_t data16;
+	uint32_t data32;
+	uint64_t data64;
+	gboolean ret;
+
+	apparam = g_obex_apparam_decode(tag_multi, sizeof(tag_multi));
+
+	g_assert(apparam != NULL);
+
+	ret = g_obex_apparam_get_uint8(apparam, TAG_U8, &data8);
+
+	g_assert(ret == TRUE);
+	g_assert(data8 == 0x01);
+
+	ret = g_obex_apparam_get_uint16(apparam, TAG_U16, &data16);
+
+	g_assert(ret == TRUE);
+	g_assert(data16 == 0x0102);
+
+	ret = g_obex_apparam_get_uint32(apparam, TAG_U32, &data32);
+
+	g_assert(ret == TRUE);
+	g_assert(data32 == 0x01020304);
+
+	ret = g_obex_apparam_get_uint64(apparam, TAG_U64, &data64);
+
+	g_assert(ret == TRUE);
+	g_assert(data64 == 0x0102030405060708);
+
+	string = g_obex_apparam_get_string(apparam, TAG_STRING);
+
+	g_assert(string != NULL);
+	g_assert_cmpstr(string, ==, "ABC");
+
+	g_obex_apparam_free(apparam);
+}
+
+static void test_apparam_set_uint8(void)
+{
+	GObexApparam *apparam;
+	guint8 buf[1024];
+	gsize len;
+
+	apparam = g_obex_apparam_set_uint8(NULL, TAG_U8, 0x01);
+	g_assert(apparam != NULL);
+
+	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
+	assert_memequal(tag_uint8, sizeof(tag_uint8), buf, len);
+
+	g_obex_apparam_free(apparam);
+}
+
+static void test_apparam_set_uint16(void)
+{
+	GObexApparam *apparam;
+	guint8 buf[1024];
+	gsize len;
+
+	apparam = g_obex_apparam_set_uint16(NULL, TAG_U16, 0x0102);
+	g_assert(apparam != NULL);
+
+	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
+	assert_memequal(tag_uint16, sizeof(tag_uint16), buf, len);
+
+	g_obex_apparam_free(apparam);
+}
+
+static void test_apparam_set_uint32(void)
+{
+	GObexApparam *apparam;
+	guint8 buf[1024];
+	gsize len;
+
+	apparam = g_obex_apparam_set_uint32(NULL, TAG_U32, 0x01020304);
+	g_assert(apparam != NULL);
+
+	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
+	assert_memequal(tag_uint32, sizeof(tag_uint32), buf, len);
+
+	g_obex_apparam_free(apparam);
+}
+
+static void test_apparam_set_uint64(void)
+{
+	GObexApparam *apparam;
+	guint8 buf[1024];
+	gsize len;
+
+	apparam = g_obex_apparam_set_uint64(NULL, TAG_U64, 0x0102030405060708);
+	g_assert(apparam != NULL);
+
+	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
+	assert_memequal(tag_uint64, sizeof(tag_uint64), buf, len);
+
+	g_obex_apparam_free(apparam);
+}
+
+static void test_apparam_set_string(void)
+{
+	GObexApparam *apparam;
+	guint8 buf[1024];
+	gsize len;
+
+	apparam = g_obex_apparam_set_string(NULL, TAG_STRING, "ABC");
+	g_assert(apparam != NULL);
+
+	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
+	assert_memequal(tag_string, sizeof(tag_string), buf, len);
+
+	g_obex_apparam_free(apparam);
+}
+
+static void test_apparam_set_bytes(void)
+{
+	GObexApparam *apparam;
+	guint8 buf[1024];
+	gsize len;
+
+	apparam = g_obex_apparam_set_bytes(NULL, TAG_BYTES, tag_bytes + 2, 255);
+	g_assert(apparam != NULL);
+
+	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
+	assert_memequal(tag_bytes, sizeof(tag_bytes), buf, len);
+
+	g_obex_apparam_free(apparam);
+}
+
+static void test_apparam_set_multi(void)
+{
+	GObexApparam *apparam;
+	guint8 buf[1024];
+	gsize len;
+
+	apparam = g_obex_apparam_set_uint8(NULL, TAG_U8, 0x01);
+
+	g_assert(apparam != NULL);
+
+	apparam = g_obex_apparam_set_uint16(apparam, TAG_U16, 0x0102);
+
+	g_assert(apparam != NULL);
+
+	apparam = g_obex_apparam_set_uint32(apparam, TAG_U32, 0x01020304);
+
+	g_assert(apparam != NULL);
+
+	apparam = g_obex_apparam_set_uint64(apparam, TAG_U64,
+							0x0102030405060708);
+
+	g_assert(apparam != NULL);
+
+	apparam = g_obex_apparam_set_string(apparam, TAG_STRING, "ABC");
+
+	g_assert(apparam != NULL);
+
+	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
+
+	g_assert_cmpuint(len, ==, sizeof(tag_multi));
+
+	g_obex_apparam_free(apparam);
+}
+
+int main(int argc, char *argv[])
+{
+	g_test_init(&argc, &argv, NULL);
+
+	g_test_add_func("/gobex/test_apparam_nval_short",
+						test_apparam_nval_short);
+	g_test_add_func("/gobex/test_apparam_nval_data",
+						test_apparam_nval_data);
+
+	g_test_add_func("/gobex/test_apparam_nval2_short",
+						test_apparam_nval2_short);
+	g_test_add_func("/gobex/test_apparam_nval2_data",
+						test_apparam_nval2_data);
+
+	g_test_add_func("/gobex/test_apparam_get_uint8",
+						test_apparam_get_uint8);
+	g_test_add_func("/gobex/test_apparam_get_uint16",
+						test_apparam_get_uint16);
+	g_test_add_func("/gobex/test_apparam_get_uint32",
+						test_apparam_get_uint32);
+	g_test_add_func("/gobex/test_apparam_get_uint64",
+						test_apparam_get_uint64);
+	g_test_add_func("/gobex/test_apparam_get_string",
+						test_apparam_get_string);
+	g_test_add_func("/gobex/test_apparam_get_bytes",
+						test_apparam_get_bytes);
+	g_test_add_func("/gobex/test_apparam_get_multi",
+						test_apparam_get_multi);
+
+	g_test_add_func("/gobex/test_apparam_set_uint8",
+						test_apparam_set_uint8);
+	g_test_add_func("/gobex/test_apparam_set_uint16",
+						test_apparam_set_uint16);
+	g_test_add_func("/gobex/test_apparam_set_uint32",
+						test_apparam_set_uint32);
+	g_test_add_func("/gobex/test_apparam_set_uint64",
+						test_apparam_set_uint64);
+	g_test_add_func("/gobex/test_apparam_set_string",
+						test_apparam_set_string);
+	g_test_add_func("/gobex/test_apparam_set_bytes",
+						test_apparam_set_bytes);
+	g_test_add_func("/gobex/test_apparam_set_multi",
+						test_apparam_set_multi);
+
+	g_test_run();
+
+	return 0;
+}
-- 
1.7.11.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH obexd 3/9] gobex: Integrate GObexApparam with GObexHeader
  2012-08-09 10:32 [PATCH obexd 0/9] GObexApparam API Luiz Augusto von Dentz
  2012-08-09 10:32 ` [PATCH obexd 1/9] gobex: Introduce " Luiz Augusto von Dentz
  2012-08-09 10:32 ` [PATCH obexd 2/9] gobex: Add unit tests for " Luiz Augusto von Dentz
@ 2012-08-09 10:32 ` Luiz Augusto von Dentz
  2012-08-09 10:32 ` [PATCH obexd 4/9] gobex: Add unit test for encoding/decoding apparam headers Luiz Augusto von Dentz
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Luiz Augusto von Dentz @ 2012-08-09 10:32 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This introduce 2 new convenient functions: g_obex_header_get_apparam
which parses and decode an header into GObexApparam and
gobex_header_new_apparam that encode GObexApparam into GObexHeader.
---
 gobex/gobex-header.c | 25 +++++++++++++++++++++++++
 gobex/gobex-header.h |  3 +++
 2 files changed, 28 insertions(+)

diff --git a/gobex/gobex-header.c b/gobex/gobex-header.c
index 56dd9b2..80e8e4e 100644
--- a/gobex/gobex-header.c
+++ b/gobex/gobex-header.c
@@ -337,6 +337,19 @@ gboolean g_obex_header_get_bytes(GObexHeader *header, const guint8 **val,
 	return TRUE;
 }
 
+GObexApparam *g_obex_header_get_apparam(GObexHeader *header)
+{
+	gboolean ret;
+	const guint8 *val;
+	gsize len;
+
+	ret = g_obex_header_get_bytes(header, &val, &len);
+	if (!ret)
+		return NULL;
+
+	return g_obex_apparam_decode(val, len);
+}
+
 gboolean g_obex_header_get_uint8(GObexHeader *header, guint8 *val)
 {
 	g_obex_debug(G_OBEX_DEBUG_HEADER, "header 0x%02x",
@@ -411,6 +424,18 @@ GObexHeader *g_obex_header_new_bytes(guint8 id, const void *data, gsize len)
 	return header;
 }
 
+GObexHeader *g_obex_header_new_apparam(GObexApparam *apparam)
+{
+	guint8 buf[1024];
+	gssize len;
+
+	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
+	if (len < 0)
+		return NULL;
+
+	return g_obex_header_new_bytes(G_OBEX_HDR_APPARAM, buf, len);
+}
+
 GObexHeader *g_obex_header_new_uint8(guint8 id, guint8 val)
 {
 	GObexHeader *header;
diff --git a/gobex/gobex-header.h b/gobex/gobex-header.h
index 2ee8364..196cb20 100644
--- a/gobex/gobex-header.h
+++ b/gobex/gobex-header.h
@@ -25,6 +25,7 @@
 #include <glib.h>
 
 #include <gobex/gobex-defs.h>
+#include <gobex/gobex-apparam.h>
 
 /* Header ID's */
 #define G_OBEX_HDR_INVALID	0x00
@@ -77,11 +78,13 @@ gboolean g_obex_header_get_bytes(GObexHeader *header, const guint8 **val,
 								gsize *len);
 gboolean g_obex_header_get_uint8(GObexHeader *header, guint8 *val);
 gboolean g_obex_header_get_uint32(GObexHeader *header, guint32 *val);
+GObexApparam *g_obex_header_get_apparam(GObexHeader *header);
 
 GObexHeader *g_obex_header_new_unicode(guint8 id, const char *str);
 GObexHeader *g_obex_header_new_bytes(guint8 id, const void *data, gsize len);
 GObexHeader *g_obex_header_new_uint8(guint8 id, guint8 val);
 GObexHeader *g_obex_header_new_uint32(guint8 id, guint32 val);
+GObexHeader *g_obex_header_new_apparam(GObexApparam *apparam);
 
 GSList *g_obex_header_create_list(guint8 first_hdr_id, va_list args,
 							gsize *total_len);
-- 
1.7.11.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH obexd 4/9] gobex: Add unit test for encoding/decoding apparam headers
  2012-08-09 10:32 [PATCH obexd 0/9] GObexApparam API Luiz Augusto von Dentz
                   ` (2 preceding siblings ...)
  2012-08-09 10:32 ` [PATCH obexd 3/9] gobex: Integrate GObexApparam with GObexHeader Luiz Augusto von Dentz
@ 2012-08-09 10:32 ` Luiz Augusto von Dentz
  2012-08-09 10:32 ` [PATCH obexd 5/9] gobex: Add debug option to apparam Luiz Augusto von Dentz
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Luiz Augusto von Dentz @ 2012-08-09 10:32 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

---
 unit/test-gobex-header.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/unit/test-gobex-header.c b/unit/test-gobex-header.c
index 86e69f3..feb7d17 100644
--- a/unit/test-gobex-header.c
+++ b/unit/test-gobex-header.c
@@ -47,6 +47,8 @@ static uint8_t hdr_bytes_nval_short[] = { G_OBEX_HDR_BODY, 0xab, 0xcd,
 						0x01, 0x02, 0x03 };
 static uint8_t hdr_bytes_nval_data[] = { G_OBEX_HDR_BODY, 0xab };
 static uint8_t hdr_bytes_nval_len[] = { G_OBEX_HDR_BODY, 0x00, 0x00 };
+static uint8_t hdr_apparam[] = { G_OBEX_HDR_APPARAM, 0x00, 0x09, 0x00, 0x04,
+						0x01, 0x02, 0x03, 0x04 };
 
 static void test_header_name_empty(void)
 {
@@ -115,6 +117,27 @@ static void test_header_bytes(void)
 	g_obex_header_free(header);
 }
 
+static void test_header_apparam(void)
+{
+	GObexHeader *header;
+	GObexApparam *apparam;
+	uint8_t buf[1024];
+	size_t len;
+
+	apparam = g_obex_apparam_set_uint32(NULL, 0, 0x01020304);
+	g_assert(apparam != NULL);
+
+	header = g_obex_header_new_apparam(apparam);
+	g_assert(header != NULL);
+
+	len = g_obex_header_encode(header, buf, sizeof(buf));
+
+	assert_memequal(hdr_apparam, sizeof(hdr_apparam), buf, len);
+
+	g_obex_apparam_free(apparam);
+	g_obex_header_free(header);
+}
+
 static void test_header_uint8(void)
 {
 	GObexHeader *header;
@@ -247,6 +270,26 @@ static void test_header_encode_body(void)
 	g_obex_header_free(header);
 }
 
+static void test_header_encode_apparam(void)
+{
+	GObexHeader *header;
+	GObexApparam *apparam;
+	gboolean ret;
+	guint32 data;
+
+	header = parse_and_encode(hdr_apparam, sizeof(hdr_apparam));
+
+	apparam = g_obex_header_get_apparam(header);
+	g_assert(apparam != NULL);
+
+	ret = g_obex_apparam_get_uint32(apparam, 0x00, &data);
+	g_assert(ret == TRUE);
+	g_assert(data == 0x01020304);
+
+	g_obex_apparam_free(apparam);
+	g_obex_header_free(header);
+}
+
 static void test_header_encode_actionid(void)
 {
 	GObexHeader *header;
@@ -507,6 +550,8 @@ int main(int argc, char *argv[])
 						test_header_encode_body);
 	g_test_add_func("/gobex/test_header_encode_connid",
 						test_header_encode_actionid);
+	g_test_add_func("/gobex/test_header_encode_apparam",
+						test_header_encode_apparam);
 
 	g_test_add_func("/gobex/test_header_name_empty",
 						test_header_name_empty);
@@ -517,6 +562,7 @@ int main(int argc, char *argv[])
 	g_test_add_func("/gobex/test_header_bytes", test_header_bytes);
 	g_test_add_func("/gobex/test_header_uint8", test_header_uint8);
 	g_test_add_func("/gobex/test_header_uint32", test_header_uint32);
+	g_test_add_func("/gobex/test_header_apparam", test_header_apparam);
 
 	g_test_run();
 
-- 
1.7.11.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH obexd 5/9] gobex: Add debug option to apparam
  2012-08-09 10:32 [PATCH obexd 0/9] GObexApparam API Luiz Augusto von Dentz
                   ` (3 preceding siblings ...)
  2012-08-09 10:32 ` [PATCH obexd 4/9] gobex: Add unit test for encoding/decoding apparam headers Luiz Augusto von Dentz
@ 2012-08-09 10:32 ` Luiz Augusto von Dentz
  2012-08-09 10:32 ` [PATCH obexd 6/9] client: Port PBAP module to use GObexApparam Luiz Augusto von Dentz
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Luiz Augusto von Dentz @ 2012-08-09 10:32 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This adds "apparam" to the debug options of GOBEX_DEBUG
---
 gobex/gobex-apparam.c | 43 ++++++++++++++++++++++++++++++++++++++++++-
 gobex/gobex-debug.h   |  7 ++++---
 gobex/gobex.c         | 11 ++++++-----
 3 files changed, 52 insertions(+), 9 deletions(-)

diff --git a/gobex/gobex-apparam.c b/gobex/gobex-apparam.c
index 09bf034..8f72aa7 100644
--- a/gobex/gobex-apparam.c
+++ b/gobex/gobex-apparam.c
@@ -28,6 +28,7 @@
 #include <errno.h>
 
 #include "gobex-apparam.h"
+#include "gobex-debug.h"
 
 struct _GObexApparam {
 	GHashTable *tags;
@@ -179,6 +180,8 @@ GObexApparam *g_obex_apparam_set_bytes(GObexApparam *apparam, guint8 id,
 GObexApparam *g_obex_apparam_set_uint8(GObexApparam *apparam, guint8 id,
 							guint8 value)
 {
+	g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x value %u", id, value);
+
 	return g_obex_apparam_set_bytes(apparam, id, &value, 1);
 }
 
@@ -187,6 +190,8 @@ GObexApparam *g_obex_apparam_set_uint16(GObexApparam *apparam, guint8 id,
 {
 	guint16 num = g_htons(value);
 
+	g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x value %u", id, value);
+
 	return g_obex_apparam_set_bytes(apparam, id, &num, 2);
 }
 
@@ -195,6 +200,8 @@ GObexApparam *g_obex_apparam_set_uint32(GObexApparam *apparam, guint8 id,
 {
 	guint32 num = g_htonl(value);
 
+	g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x value %u", id, value);
+
 	return g_obex_apparam_set_bytes(apparam, id, &num, 4);
 }
 
@@ -203,6 +210,9 @@ GObexApparam *g_obex_apparam_set_uint64(GObexApparam *apparam, guint8 id,
 {
 	guint64 num = GUINT64_TO_BE(value);
 
+	g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x value %"
+						G_GUINT64_FORMAT, id, value);
+
 	return g_obex_apparam_set_bytes(apparam, id, &num, 8);
 }
 
@@ -211,6 +221,8 @@ GObexApparam *g_obex_apparam_set_string(GObexApparam *apparam, guint8 id,
 {
 	gsize len;
 
+	g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x value %s", id, value);
+
 	len = strlen(value) + 1;
 	if (len > G_MAXUINT8) {
 		((char *) value)[G_MAXUINT8 - 1] = '\0';
@@ -225,11 +237,16 @@ gboolean g_obex_apparam_get_uint8(GObexApparam *apparam, guint8 id,
 {
 	struct apparam_tag *tag;
 
+	g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
+
 	tag = g_hash_table_lookup(apparam->tags, GUINT_TO_POINTER(id));
 	if (tag == NULL)
 		return FALSE;
 
 	*dest = tag->value.u8;
+
+	g_obex_debug(G_OBEX_DEBUG_APPARAM, "%u", *dest);
+
 	return TRUE;
 }
 
@@ -238,6 +255,8 @@ gboolean g_obex_apparam_get_uint16(GObexApparam *apparam, guint8 id,
 {
 	struct apparam_tag *tag;
 
+	g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
+
 	tag = g_hash_table_lookup(apparam->tags, GUINT_TO_POINTER(id));
 	if (tag == NULL)
 		return FALSE;
@@ -246,6 +265,9 @@ gboolean g_obex_apparam_get_uint16(GObexApparam *apparam, guint8 id,
 		return FALSE;
 
 	*dest = g_ntohs(tag->value.u16);
+
+	g_obex_debug(G_OBEX_DEBUG_APPARAM, "%u", *dest);
+
 	return TRUE;
 }
 
@@ -254,6 +276,8 @@ gboolean g_obex_apparam_get_uint32(GObexApparam *apparam, guint8 id,
 {
 	struct apparam_tag *tag;
 
+	g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
+
 	tag = g_hash_table_lookup(apparam->tags, GUINT_TO_POINTER(id));
 	if (tag == NULL)
 		return FALSE;
@@ -262,6 +286,9 @@ gboolean g_obex_apparam_get_uint32(GObexApparam *apparam, guint8 id,
 		return FALSE;
 
 	*dest = g_ntohl(tag->value.u32);
+
+	g_obex_debug(G_OBEX_DEBUG_APPARAM, "%u", *dest);
+
 	return TRUE;
 }
 
@@ -270,6 +297,8 @@ gboolean g_obex_apparam_get_uint64(GObexApparam *apparam, guint8 id,
 {
 	struct apparam_tag *tag;
 
+	g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
+
 	tag = g_hash_table_lookup(apparam->tags, GUINT_TO_POINTER(id));
 	if (tag == NULL)
 		return FALSE;
@@ -278,18 +307,28 @@ gboolean g_obex_apparam_get_uint64(GObexApparam *apparam, guint8 id,
 		return FALSE;
 
 	*dest = GUINT64_FROM_BE(tag->value.u64);
+
+	g_obex_debug(G_OBEX_DEBUG_APPARAM, "%" G_GUINT64_FORMAT, *dest);
+
 	return TRUE;
 }
 
 char *g_obex_apparam_get_string(GObexApparam *apparam, guint8 id)
 {
 	struct apparam_tag *tag;
+	char *string;
+
+	g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
 
 	tag = g_hash_table_lookup(apparam->tags, GUINT_TO_POINTER(id));
 	if (tag == NULL)
 		return NULL;
 
-	return g_strndup(tag->value.string, tag->len);
+	string = g_strndup(tag->value.string, tag->len);
+
+	g_obex_debug(G_OBEX_DEBUG_APPARAM, "%s", string);
+
+	return string;
 }
 
 gboolean g_obex_apparam_get_bytes(GObexApparam *apparam, guint8 id,
@@ -297,6 +336,8 @@ gboolean g_obex_apparam_get_bytes(GObexApparam *apparam, guint8 id,
 {
 	struct apparam_tag *tag;
 
+	g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
+
 	tag = g_hash_table_lookup(apparam->tags, GUINT_TO_POINTER(id));
 	if (tag == NULL)
 		return FALSE;
diff --git a/gobex/gobex-debug.h b/gobex/gobex-debug.h
index 14faa10..14e2bcd 100644
--- a/gobex/gobex-debug.h
+++ b/gobex/gobex-debug.h
@@ -32,6 +32,7 @@
 #define G_OBEX_DEBUG_HEADER	(1 << 4)
 #define G_OBEX_DEBUG_PACKET	(1 << 5)
 #define G_OBEX_DEBUG_DATA	(1 << 6)
+#define G_OBEX_DEBUG_APPARAM	(1 << 7)
 
 extern guint gobex_debug;
 
@@ -40,13 +41,13 @@ extern guint gobex_debug;
 		g_log("gobex", G_LOG_LEVEL_DEBUG, "%s:%s() " format, __FILE__, \
 						__FUNCTION__, ## __VA_ARGS__)
 
-static inline void g_obex_dump(const char *prefix, const void *buf,
-								gsize len)
+static inline void g_obex_dump(guint level, const char *prefix,
+					const void *buf, gsize len)
 {
 	const guint8 *data = buf;
 	int n = 0;
 
-	if (!(gobex_debug & G_OBEX_DEBUG_DATA))
+	if (!(gobex_debug & level))
 		return;
 
 	while (len > 0) {
diff --git a/gobex/gobex.c b/gobex/gobex.c
index b6126b8..7c136af 100644
--- a/gobex/gobex.c
+++ b/gobex/gobex.c
@@ -267,7 +267,7 @@ static gboolean write_stream(GObex *obex, GError **err)
 	if (status != G_IO_STATUS_NORMAL)
 		return FALSE;
 
-	g_obex_dump("<", buf, bytes_written);
+	g_obex_dump(G_OBEX_DEBUG_DATA, "<", buf, bytes_written);
 
 	obex->tx_sent += bytes_written;
 	obex->tx_data -= bytes_written;
@@ -290,7 +290,7 @@ static gboolean write_packet(GObex *obex, GError **err)
 	if (bytes_written != obex->tx_data)
 		return FALSE;
 
-	g_obex_dump("<", buf, bytes_written);
+	g_obex_dump(G_OBEX_DEBUG_DATA, "<", buf, bytes_written);
 
 	obex->tx_sent += bytes_written;
 	obex->tx_data -= bytes_written;
@@ -1078,7 +1078,7 @@ read_body:
 	} while (rbytes > 0 && obex->rx_data < obex->rx_pkt_len);
 
 done:
-	g_obex_dump(">", obex->rx_buf, obex->rx_data);
+	g_obex_dump(G_OBEX_DEBUG_DATA, ">", obex->rx_buf, obex->rx_data);
 
 	return TRUE;
 }
@@ -1124,7 +1124,7 @@ static gboolean read_packet(GObex *obex, GError **err)
 		return FALSE;
 	}
 
-	g_obex_dump(">", obex->rx_buf, obex->rx_data);
+	g_obex_dump(G_OBEX_DEBUG_DATA, ">", obex->rx_buf, obex->rx_data);
 
 	return TRUE;
 fail:
@@ -1236,6 +1236,7 @@ static GDebugKey keys[] = {
 	{ "header",	G_OBEX_DEBUG_HEADER },
 	{ "packet",	G_OBEX_DEBUG_PACKET },
 	{ "data",	G_OBEX_DEBUG_DATA },
+	{ "apparam",	G_OBEX_DEBUG_APPARAM },
 };
 
 GObex *g_obex_new(GIOChannel *io, GObexTransportType transport_type,
@@ -1248,7 +1249,7 @@ GObex *g_obex_new(GIOChannel *io, GObexTransportType transport_type,
 		const char *env = g_getenv("GOBEX_DEBUG");
 
 		if (env) {
-			gobex_debug = g_parse_debug_string(env, keys, 6);
+			gobex_debug = g_parse_debug_string(env, keys, 7);
 			g_setenv("G_MESSAGES_DEBUG", "gobex", FALSE);
 		} else
 			gobex_debug = G_OBEX_DEBUG_NONE;
-- 
1.7.11.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH obexd 6/9] client: Port PBAP module to use GObexApparam
  2012-08-09 10:32 [PATCH obexd 0/9] GObexApparam API Luiz Augusto von Dentz
                   ` (4 preceding siblings ...)
  2012-08-09 10:32 ` [PATCH obexd 5/9] gobex: Add debug option to apparam Luiz Augusto von Dentz
@ 2012-08-09 10:32 ` Luiz Augusto von Dentz
  2012-08-09 10:32 ` [PATCH obexd 7/9] PBAP: Make use of GObexApparam API Luiz Augusto von Dentz
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Luiz Augusto von Dentz @ 2012-08-09 10:32 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

---
 client/pbap.c | 181 ++++++++++++++++------------------------------------------
 1 file changed, 49 insertions(+), 132 deletions(-)

diff --git a/client/pbap.c b/client/pbap.c
index 48dbac1..9a1da8d 100644
--- a/client/pbap.c
+++ b/client/pbap.c
@@ -33,6 +33,7 @@
 #include <gdbus.h>
 
 #include <bluetooth/bluetooth.h>
+#include <gobex-apparam.h>
 
 #include "log.h"
 
@@ -72,18 +73,6 @@
 #define PHONEBOOKSIZE_TAG	0X08
 #define NEWMISSEDCALLS_TAG	0X09
 
-/* The following length is in the unit of byte */
-#define ORDER_LEN		1
-#define SEARCHATTRIB_LEN	1
-#define MAXLISTCOUNT_LEN	2
-#define LISTSTARTOFFSET_LEN	2
-#define FILTER_LEN		8
-#define FORMAT_LEN		1
-#define PHONEBOOKSIZE_LEN	2
-#define NEWMISSEDCALLS_LEN	1
-
-#define get_be16(val)	GUINT16_FROM_BE(bt_get_unaligned((guint16 *) val))
-
 static const char *filter_list[] = {
 	"VERSION",
 	"FN",
@@ -137,38 +126,6 @@ struct pending_request {
 	DBusMessage *msg;
 };
 
-struct pullphonebook_apparam {
-	uint8_t     filter_tag;
-	uint8_t     filter_len;
-	uint64_t    filter;
-	uint8_t     format_tag;
-	uint8_t     format_len;
-	uint8_t     format;
-	uint8_t     maxlistcount_tag;
-	uint8_t     maxlistcount_len;
-	uint16_t    maxlistcount;
-	uint8_t     liststartoffset_tag;
-	uint8_t     liststartoffset_len;
-	uint16_t    liststartoffset;
-} __attribute__ ((packed));
-
-struct pullvcardentry_apparam {
-        uint8_t     filter_tag;
-        uint8_t     filter_len;
-        uint64_t    filter;
-        uint8_t     format_tag;
-        uint8_t     format_len;
-        uint8_t     format;
-} __attribute__ ((packed));
-
-struct apparam_hdr {
-	uint8_t		tag;
-	uint8_t		len;
-	uint8_t		val[0];
-} __attribute__ ((packed));
-
-#define APPARAM_HDR_SIZE 2
-
 static DBusConnection *conn = NULL;
 
 static struct pending_request *pending_request_new(struct pbap_data *pbap,
@@ -295,48 +252,28 @@ static void pbap_setpath_cb(struct obc_session *session,
 static void read_return_apparam(struct obc_transfer *transfer,
 				guint16 *phone_book_size, guint8 *new_missed_calls)
 {
-	const struct apparam_hdr *hdr;
+	GObexApparam *apparam;
+	const guint8 *data;
 	size_t size;
 
 	*phone_book_size = 0;
 	*new_missed_calls = 0;
 
-	hdr = obc_transfer_get_params(transfer, &size);
-	if (hdr == NULL)
+	data = obc_transfer_get_params(transfer, &size);
+	if (data == NULL)
 		return;
 
-	if (size < APPARAM_HDR_SIZE)
+	apparam = g_obex_apparam_decode(data, size);
+	if (apparam == NULL)
 		return;
 
-	while (size > APPARAM_HDR_SIZE) {
-		if (hdr->len > size - APPARAM_HDR_SIZE) {
-			error("Unexpected PBAP pullphonebook app"
-					" length, tag %d, len %d",
-					hdr->tag, hdr->len);
-			return;
-		}
+	g_obex_apparam_get_uint16(apparam, PHONEBOOKSIZE_TAG,
+							phone_book_size);
+	g_obex_apparam_get_uint8(apparam, NEWMISSEDCALLS_TAG,
+							new_missed_calls);
 
-		switch (hdr->tag) {
-		case PHONEBOOKSIZE_TAG:
-			if (hdr->len == PHONEBOOKSIZE_LEN) {
-				guint16 val;
-				memcpy(&val, hdr->val, sizeof(val));
-				*phone_book_size = get_be16(&val);
-			}
-			break;
-		case NEWMISSEDCALLS_TAG:
-			if (hdr->len == NEWMISSEDCALLS_LEN)
-				*new_missed_calls = hdr->val[0];
-			break;
-		default:
-			error("Unexpected PBAP pullphonebook app"
-					" parameter, tag %d, len %d",
-					hdr->tag, hdr->len);
-		}
 
-		size -= APPARAM_HDR_SIZE + hdr->len;
-		hdr += APPARAM_HDR_SIZE + hdr->len;
-	}
+	g_obex_apparam_free(apparam);
 }
 
 static void phonebook_size_callback(struct obc_session *session,
@@ -425,25 +362,21 @@ static struct obc_transfer *pull_phonebook(struct pbap_data *pbap,
 {
 	struct pending_request *request;
 	struct obc_transfer *transfer;
-	struct pullphonebook_apparam apparam;
+	GObexApparam *apparam;
+	guint8 buf[32];
+	gsize len;
 	session_callback_t func;
 
 	transfer = obc_transfer_get("x-bt/phonebook", name, targetfile, err);
 	if (transfer == NULL)
 		return NULL;
 
-	apparam.filter_tag = FILTER_TAG;
-	apparam.filter_len = FILTER_LEN;
-	apparam.filter = GUINT64_TO_BE(filter);
-	apparam.format_tag = FORMAT_TAG;
-	apparam.format_len = FORMAT_LEN;
-	apparam.format = format;
-	apparam.maxlistcount_tag = MAXLISTCOUNT_TAG;
-	apparam.maxlistcount_len = MAXLISTCOUNT_LEN;
-	apparam.maxlistcount = GUINT16_TO_BE(maxlistcount);
-	apparam.liststartoffset_tag = LISTSTARTOFFSET_TAG;
-	apparam.liststartoffset_len = LISTSTARTOFFSET_LEN;
-	apparam.liststartoffset = GUINT16_TO_BE(liststartoffset);
+	apparam = g_obex_apparam_set_uint64(NULL, FILTER_TAG, filter);
+	apparam = g_obex_apparam_set_uint8(apparam, FORMAT_TAG, format);
+	apparam = g_obex_apparam_set_uint16(apparam, MAXLISTCOUNT_TAG,
+							maxlistcount);
+	apparam = g_obex_apparam_set_uint16(apparam, LISTSTARTOFFSET_TAG,
+							liststartoffset);
 
 	switch (type) {
 	case PULLPHONEBOOK:
@@ -459,7 +392,11 @@ static struct obc_transfer *pull_phonebook(struct pbap_data *pbap,
 		return NULL;
 	}
 
-	obc_transfer_set_params(transfer, &apparam, sizeof(apparam));
+	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
+
+	obc_transfer_set_params(transfer, buf, len);
+
+	g_obex_apparam_free(apparam);
 
 	if (!obc_session_queue(pbap->session, transfer, func, request, err)) {
 		if (request != NULL)
@@ -472,18 +409,6 @@ static struct obc_transfer *pull_phonebook(struct pbap_data *pbap,
 	return transfer;
 }
 
-static guint8 *fill_apparam(guint8 *dest, void *buf, guint8 tag, guint8 len)
-{
-	if (dest && buf) {
-		*dest++ = tag;
-		*dest++ = len;
-		memcpy(dest, buf, len);
-		dest += len;
-	}
-
-	return dest;
-}
-
 static DBusMessage *pull_vcard_listing(struct pbap_data *pbap,
 					DBusMessage *message, const char *name,
 					guint8 order, char *searchval, guint8 attrib,
@@ -491,41 +416,31 @@ static DBusMessage *pull_vcard_listing(struct pbap_data *pbap,
 {
 	struct pending_request *request;
 	struct obc_transfer *transfer;
-	guint8 *p, apparam[272];
-	gint apparam_size;
+	guint8 buf[272];
+	gsize len;
 	GError *err = NULL;
+	GObexApparam *apparam;
 	DBusMessage *reply;
 
 	transfer = obc_transfer_get("x-bt/vcard-listing", name, NULL, &err);
 	if (transfer == NULL)
 		goto fail;
 
-	/* trunc the searchval string if it's length exceed the max value of guint8 */
-	if (strlen(searchval) > 254)
-		searchval[255] = '\0';
-
-	apparam_size = APPARAM_HDR_SIZE + ORDER_LEN +
-			(APPARAM_HDR_SIZE + strlen(searchval) + 1) +
-			(APPARAM_HDR_SIZE + SEARCHATTRIB_LEN) +
-			(APPARAM_HDR_SIZE + MAXLISTCOUNT_LEN) +
-			(APPARAM_HDR_SIZE + LISTSTARTOFFSET_LEN);
-
-	p = apparam;
+	apparam = g_obex_apparam_set_uint8(NULL, ORDER_TAG, order);
+	apparam = g_obex_apparam_set_uint8(apparam, SEARCHATTRIB_TAG, attrib);
+	apparam = g_obex_apparam_set_string(apparam, SEARCHVALUE_TAG,
+								searchval);
+	apparam = g_obex_apparam_set_uint16(apparam, MAXLISTCOUNT_TAG, count);
+	apparam = g_obex_apparam_set_uint16(apparam, LISTSTARTOFFSET_TAG,
+								offset);
 
-	p = fill_apparam(p, &order, ORDER_TAG, ORDER_LEN);
-	p = fill_apparam(p, searchval, SEARCHVALUE_TAG, strlen(searchval) + 1);
-	p = fill_apparam(p, &attrib, SEARCHATTRIB_TAG, SEARCHATTRIB_LEN);
+	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
 
-	count = GUINT16_TO_BE(count);
-	p = fill_apparam(p, &count, MAXLISTCOUNT_TAG, MAXLISTCOUNT_LEN);
+	obc_transfer_set_params(transfer, buf, len);
 
-	offset = GUINT16_TO_BE(offset);
-	fill_apparam(p, &offset, LISTSTARTOFFSET_TAG, LISTSTARTOFFSET_LEN);
+	g_obex_apparam_free(apparam);
 
 	request = pending_request_new(pbap, message);
-
-	obc_transfer_set_params(transfer, apparam, apparam_size);
-
 	if (obc_session_queue(pbap->session, transfer,
 				pull_vcard_listing_callback, request, &err))
 		return NULL;
@@ -739,7 +654,9 @@ static DBusMessage *pbap_pull_vcard(DBusConnection *connection,
 {
 	struct pbap_data *pbap = user_data;
 	struct obc_transfer *transfer;
-	struct pullvcardentry_apparam apparam;
+	GObexApparam *apparam;
+	guint8 buf[32];
+	gsize len;
 	const char *name, *targetfile;
 	DBusMessage *reply;
 	GError *err = NULL;
@@ -760,14 +677,14 @@ static DBusMessage *pbap_pull_vcard(DBusConnection *connection,
 	if (transfer == NULL)
 		goto fail;
 
-	apparam.filter_tag = FILTER_TAG;
-	apparam.filter_len = FILTER_LEN;
-	apparam.filter = GUINT64_TO_BE(pbap->filter);
-	apparam.format_tag = FORMAT_TAG;
-	apparam.format_len = FORMAT_LEN;
-	apparam.format = pbap->format;
+	apparam = g_obex_apparam_set_uint64(NULL, FILTER_TAG, pbap->filter);
+	apparam = g_obex_apparam_set_uint8(apparam, FORMAT_TAG, pbap->format);
+
+	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
+
+	obc_transfer_set_params(transfer, buf, len);
 
-	obc_transfer_set_params(transfer, &apparam, sizeof(apparam));
+	g_obex_apparam_free(apparam);
 
 	if (!obc_session_queue(pbap->session, transfer, NULL, NULL, &err))
 		goto fail;
-- 
1.7.11.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH obexd 7/9] PBAP: Make use of GObexApparam API
  2012-08-09 10:32 [PATCH obexd 0/9] GObexApparam API Luiz Augusto von Dentz
                   ` (5 preceding siblings ...)
  2012-08-09 10:32 ` [PATCH obexd 6/9] client: Port PBAP module to use GObexApparam Luiz Augusto von Dentz
@ 2012-08-09 10:32 ` Luiz Augusto von Dentz
  2012-08-09 10:32 ` [PATCH obexd 8/9] MAP: " Luiz Augusto von Dentz
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Luiz Augusto von Dentz @ 2012-08-09 10:32 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

---
 plugins/pbap.c      | 180 +++++++++++-----------------------------------------
 plugins/phonebook.h |   2 +-
 2 files changed, 39 insertions(+), 143 deletions(-)

diff --git a/plugins/pbap.c b/plugins/pbap.c
index c7e792a..c36fab2 100644
--- a/plugins/pbap.c
+++ b/plugins/pbap.c
@@ -39,6 +39,7 @@
 #include <inttypes.h>
 
 #include <gobex.h>
+#include <gobex-apparam.h>
 
 #include "obexd.h"
 #include "plugin.h"
@@ -64,16 +65,6 @@
 #define PHONEBOOKSIZE_TAG	0X08
 #define NEWMISSEDCALLS_TAG	0X09
 
-/* The following length is in the unit of byte */
-#define ORDER_LEN		1
-#define SEARCHATTRIB_LEN	1
-#define MAXLISTCOUNT_LEN	2
-#define LISTSTARTOFFSET_LEN	2
-#define FILTER_LEN		8
-#define FORMAT_LEN		1
-#define PHONEBOOKSIZE_LEN	2
-#define NEWMISSEDCALLS_LEN	1
-
 #define PBAP_CHANNEL	15
 
 #define PBAP_RECORD "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>	\
@@ -117,12 +108,6 @@
   </attribute>								\
 </record>"
 
-struct aparam_header {
-	uint8_t tag;
-	uint8_t len;
-	uint8_t val[0];
-} __attribute__ ((packed));
-
 struct cache {
 	gboolean valid;
 	uint32_t index;
@@ -147,7 +132,7 @@ struct pbap_session {
 
 struct pbap_object {
 	GString *buffer;
-	GByteArray *aparams;
+	GObexApparam *apparam;
 	gboolean firstpacket;
 	gboolean lastpart;
 	struct pbap_session *session;
@@ -229,33 +214,6 @@ static void cache_clear(struct cache *cache)
 	cache->entries = NULL;
 }
 
-static GByteArray *append_aparam_header(GByteArray *buf, uint8_t tag,
-							const void *val)
-{
-	/* largest aparam is for phonebooksize (4 bytes) */
-	uint8_t aparam[sizeof(struct aparam_header) + PHONEBOOKSIZE_LEN];
-	struct aparam_header *hdr = (struct aparam_header *) aparam;
-
-	switch (tag) {
-	case PHONEBOOKSIZE_TAG:
-		hdr->tag = PHONEBOOKSIZE_TAG;
-		hdr->len = PHONEBOOKSIZE_LEN;
-		memcpy(hdr->val, val, PHONEBOOKSIZE_LEN);
-
-		return g_byte_array_append(buf,	aparam,
-			sizeof(struct aparam_header) + PHONEBOOKSIZE_LEN);
-	case NEWMISSEDCALLS_TAG:
-		hdr->tag = NEWMISSEDCALLS_TAG;
-		hdr->len = NEWMISSEDCALLS_LEN;
-		memcpy(hdr->val, val, NEWMISSEDCALLS_LEN);
-
-		return g_byte_array_append(buf,	aparam,
-			sizeof(struct aparam_header) + NEWMISSEDCALLS_LEN);
-	default:
-		return buf;
-	}
-}
-
 static void phonebook_size_result(const char *buffer, size_t bufsize,
 					int vcards, int missed,
 					gboolean lastpart, void *user_data)
@@ -275,15 +233,16 @@ static void phonebook_size_result(const char *buffer, size_t bufsize,
 
 	phonebooksize = htons(vcards);
 
-	pbap->obj->aparams = g_byte_array_new();
-	pbap->obj->aparams = append_aparam_header(pbap->obj->aparams,
-					PHONEBOOKSIZE_TAG, &phonebooksize);
+	pbap->obj->apparam = g_obex_apparam_set_uint16(NULL, PHONEBOOKSIZE_TAG,
+								phonebooksize);
 
 	if (missed > 0)	{
 		DBG("missed %d", missed);
 
-		pbap->obj->aparams = append_aparam_header(pbap->obj->aparams,
-						NEWMISSEDCALLS_TAG, &missed);
+		pbap->obj->apparam = g_obex_apparam_set_uint16(
+							pbap->obj->apparam,
+							NEWMISSEDCALLS_TAG,
+							missed);
 	}
 
 	obex_object_set_io_flags(pbap->obj, G_IO_IN, 0);
@@ -319,9 +278,10 @@ static void query_result(const char *buffer, size_t bufsize, int vcards,
 
 		pbap->obj->firstpacket = TRUE;
 
-		pbap->obj->aparams = g_byte_array_new();
-		pbap->obj->aparams = append_aparam_header(pbap->obj->aparams,
-						NEWMISSEDCALLS_TAG, &missed);
+		pbap->obj->apparam = g_obex_apparam_set_uint16(
+							pbap->obj->apparam,
+							NEWMISSEDCALLS_TAG,
+							missed);
 	}
 
 	obex_object_set_io_flags(pbap->obj, G_IO_IN, 0);
@@ -450,9 +410,10 @@ static int generate_response(void *user_data)
 		/* Ignore all other parameter and return PhoneBookSize */
 		uint16_t size = htons(g_slist_length(pbap->cache.entries));
 
-		pbap->obj->aparams = g_byte_array_new();
-		pbap->obj->aparams = append_aparam_header(pbap->obj->aparams,
-						PHONEBOOKSIZE_TAG, &size);
+		pbap->obj->apparam = g_obex_apparam_set_uint16(
+							pbap->obj->apparam,
+							PHONEBOOKSIZE_TAG,
+							size);
 
 		return 0;
 	}
@@ -527,85 +488,34 @@ static void cache_entry_done(void *user_data)
 
 static struct apparam_field *parse_aparam(const uint8_t *buffer, uint32_t hlen)
 {
+	GObexApparam *apparam;
 	struct apparam_field *param;
-	struct aparam_header *hdr;
-	uint64_t val64;
-	uint32_t len = 0;
-	uint16_t val16;
-
-	param = g_new0(struct apparam_field, 1);
-
-	while (len < hlen) {
-		hdr = (void *) buffer + len;
-
-		switch (hdr->tag) {
-		case ORDER_TAG:
-			if (hdr->len != ORDER_LEN)
-				goto failed;
-
-			param->order = hdr->val[0];
-			break;
-
-		case SEARCHATTRIB_TAG:
-			if (hdr->len != SEARCHATTRIB_LEN)
-				goto failed;
-
-			param->searchattrib = hdr->val[0];
-			break;
-		case SEARCHVALUE_TAG:
-			if (hdr->len == 0)
-				goto failed;
-
-			param->searchval = g_try_malloc0(hdr->len + 1);
-			if (param->searchval)
-				memcpy(param->searchval, hdr->val, hdr->len);
-			break;
-		case FILTER_TAG:
-			if (hdr->len != FILTER_LEN)
-				goto failed;
-
-			memcpy(&val64, hdr->val, sizeof(val64));
-			param->filter = GUINT64_FROM_BE(val64);
-
-			break;
-		case FORMAT_TAG:
-			if (hdr->len != FORMAT_LEN)
-				goto failed;
 
-			param->format = hdr->val[0];
-			break;
-		case MAXLISTCOUNT_TAG:
-			if (hdr->len != MAXLISTCOUNT_LEN)
-				goto failed;
+	apparam = g_obex_apparam_decode(buffer, hlen);
+	if (apparam == NULL)
+		return NULL;
 
-			memcpy(&val16, hdr->val, sizeof(val16));
-			param->maxlistcount = GUINT16_FROM_BE(val16);
-			break;
-		case LISTSTARTOFFSET_TAG:
-			if (hdr->len != LISTSTARTOFFSET_LEN)
-				goto failed;
-
-			memcpy(&val16, hdr->val, sizeof(val16));
-			param->liststartoffset = GUINT16_FROM_BE(val16);
-			break;
-		default:
-			goto failed;
-		}
+	param = g_new0(struct apparam_field, 1);
 
-		len += hdr->len + sizeof(struct aparam_header);
-	}
+	g_obex_apparam_get_uint8(apparam, ORDER_TAG, &param->order);
+	g_obex_apparam_get_uint8(apparam, SEARCHATTRIB_TAG,
+						&param->searchattrib);
+	g_obex_apparam_get_uint8(apparam, FORMAT_TAG, &param->format);
+	g_obex_apparam_get_uint16(apparam, MAXLISTCOUNT_TAG,
+						&param->maxlistcount);
+	g_obex_apparam_get_uint16(apparam, LISTSTARTOFFSET_TAG,
+						&param->liststartoffset);
+	g_obex_apparam_get_uint64(apparam, FILTER_TAG, &param->filter);
+	param->searchval = g_obex_apparam_get_string(apparam, SEARCHVALUE_TAG);
 
 	DBG("o %x sa %x sv %s fil %" G_GINT64_MODIFIER "x for %x max %x off %x",
 			param->order, param->searchattrib, param->searchval,
 			param->filter, param->format, param->maxlistcount,
 			param->liststartoffset);
 
-	return param;
-
-failed:
-	g_free(param);
+	g_obex_apparam_free(apparam);
 
-	return NULL;
+	return param;
 }
 
 static void *pbap_connect(struct obex_session *os, int *err)
@@ -839,8 +749,8 @@ static int vobject_close(void *object)
 	if (obj->buffer)
 		g_string_free(obj->buffer, TRUE);
 
-	if (obj->aparams)
-		g_byte_array_free(obj->aparams, TRUE);
+	if (obj->apparam)
+		g_obex_apparam_free(obj->apparam);
 
 	if (obj->request)
 		phonebook_req_finalize(obj->request);
@@ -952,27 +862,13 @@ fail:
 	return NULL;
 }
 
-static ssize_t array_read(GByteArray *array, void *buf, size_t count)
-{
-	ssize_t len;
-
-	if (array->len == 0)
-		return 0;
-
-	len = MIN(array->len, count);
-	memcpy(buf, array->data, len);
-	g_byte_array_remove_range(array, 0, len);
-
-	return len;
-}
-
 static ssize_t vobject_pull_get_next_header(void *object, void *buf, size_t mtu,
 								uint8_t *hi)
 {
 	struct pbap_object *obj = object;
 	struct pbap_session *pbap = obj->session;
 
-	if (!obj->buffer && !obj->aparams)
+	if (!obj->buffer && !obj->apparam)
 		return -EAGAIN;
 
 	*hi = G_OBEX_HDR_APPARAM;
@@ -980,7 +876,7 @@ static ssize_t vobject_pull_get_next_header(void *object, void *buf, size_t mtu,
 	if (pbap->params->maxlistcount == 0 || obj->firstpacket) {
 		obj->firstpacket = FALSE;
 
-		return array_read(obj->aparams, buf, mtu);
+		return g_obex_apparam_encode(obj->apparam, buf, mtu);
 	}
 
 	return 0;
@@ -1031,7 +927,7 @@ static ssize_t vobject_list_get_next_header(void *object, void *buf, size_t mtu,
 	*hi = G_OBEX_HDR_APPARAM;
 
 	if (pbap->params->maxlistcount == 0)
-		return array_read(obj->aparams, buf, mtu);
+		return g_obex_apparam_encode(obj->apparam, buf, mtu);
 
 	return 0;
 }
diff --git a/plugins/phonebook.h b/plugins/phonebook.h
index 6e51c73..441cff2 100644
--- a/plugins/phonebook.h
+++ b/plugins/phonebook.h
@@ -61,7 +61,7 @@ struct apparam_field {
 	/* list attributes only */
 	uint8_t order;
 	uint8_t searchattrib;
-	uint8_t *searchval;
+	char *searchval;
 };
 
 /*
-- 
1.7.11.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH obexd 8/9] MAP: Make use of GObexApparam API
  2012-08-09 10:32 [PATCH obexd 0/9] GObexApparam API Luiz Augusto von Dentz
                   ` (6 preceding siblings ...)
  2012-08-09 10:32 ` [PATCH obexd 7/9] PBAP: Make use of GObexApparam API Luiz Augusto von Dentz
@ 2012-08-09 10:32 ` Luiz Augusto von Dentz
  2012-08-09 10:32 ` [PATCH obexd 9/9] core: Remove map_ap.c Luiz Augusto von Dentz
  2012-08-16  9:43 ` [PATCH obexd 0/9] GObexApparam API Johan Hedberg
  9 siblings, 0 replies; 11+ messages in thread
From: Luiz Augusto von Dentz @ 2012-08-09 10:32 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

---
 plugins/mas.c | 81 ++++++++++++++++++++++++++++-------------------------------
 1 file changed, 38 insertions(+), 43 deletions(-)

diff --git a/plugins/mas.c b/plugins/mas.c
index 576c206..30e529f 100644
--- a/plugins/mas.c
+++ b/plugins/mas.c
@@ -32,6 +32,7 @@
 #include <inttypes.h>
 
 #include <gobex/gobex.h>
+#include <gobex/gobex-apparam.h>
 
 #include "obexd.h"
 #include "plugin.h"
@@ -112,8 +113,8 @@ struct mas_session {
 	gboolean finished;
 	gboolean nth_call;
 	GString *buffer;
-	map_ap_t *inparams;
-	map_ap_t *outparams;
+	GObexApparam *inparams;
+	GObexApparam *outparams;
 	gboolean ap_sent;
 };
 
@@ -130,14 +131,12 @@ static int get_params(struct obex_session *os, struct mas_session *mas)
 	if (size < 0)
 		size = 0;
 
-	mas->inparams = map_ap_decode(buffer, size);
+	mas->inparams = g_obex_apparam_decode(buffer, size);
 	if (mas->inparams == NULL) {
 		DBG("Error when parsing parameters!");
 		return -EBADR;
 	}
 
-	mas->outparams = map_ap_new();
-
 	return 0;
 }
 
@@ -148,10 +147,15 @@ static void reset_request(struct mas_session *mas)
 		mas->buffer = NULL;
 	}
 
-	map_ap_free(mas->inparams);
-	mas->inparams = NULL;
-	map_ap_free(mas->outparams);
-	mas->outparams = NULL;
+	if (mas->inparams) {
+		g_obex_apparam_free(mas->inparams);
+		mas->inparams = NULL;
+	}
+
+	if (mas->outparams) {
+		g_obex_apparam_free(mas->outparams);
+		mas->outparams = NULL;
+	}
 
 	mas->nth_call = FALSE;
 	mas->finished = FALSE;
@@ -289,7 +293,7 @@ static void get_messages_listing_cb(void *session, int err, uint16_t size,
 		return;
 	}
 
-	map_ap_get_u16(mas->inparams, MAP_AP_MAXLISTCOUNT, &max);
+	g_obex_apparam_get_uint16(mas->inparams, MAP_AP_MAXLISTCOUNT, &max);
 
 	if (max == 0) {
 		if (!entry)
@@ -390,10 +394,12 @@ static void get_messages_listing_cb(void *session, int err, uint16_t size,
 
 proceed:
 	if (!entry) {
-		map_ap_set_u16(mas->outparams, MAP_AP_MESSAGESLISTINGSIZE,
-							size);
-		map_ap_set_u8(mas->outparams, MAP_AP_NEWMESSAGE,
-							newmsg ? 1 : 0);
+		mas->outparams = g_obex_apparam_set_uint16(mas->outparams,
+						MAP_AP_MESSAGESLISTINGSIZE,
+						size);
+		mas->outparams = g_obex_apparam_set_uint8(mas->outparams,
+						MAP_AP_NEWMESSAGE,
+						newmsg ? 1 : 0);
 	}
 
 	if (err != -EAGAIN)
@@ -435,12 +441,14 @@ static void get_folder_listing_cb(void *session, int err, uint16_t size,
 		return;
 	}
 
-	map_ap_get_u16(mas->inparams, MAP_AP_MAXLISTCOUNT, &max);
+	g_obex_apparam_get_uint16(mas->inparams, MAP_AP_MAXLISTCOUNT, &max);
 
 	if (max == 0) {
 		if (err != -EAGAIN)
-			map_ap_set_u16(mas->outparams,
-					MAP_AP_FOLDERLISTINGSIZE, size);
+			mas->outparams = g_obex_apparam_set_uint16(
+						mas->outparams,
+						MAP_AP_FOLDERLISTINGSIZE,
+						size);
 
 		if (!name)
 			mas->finished = TRUE;
@@ -529,8 +537,8 @@ static void *folder_listing_open(const char *name, int oflag, mode_t mode,
 
 	DBG("name = %s", name);
 
-	map_ap_get_u16(mas->inparams, MAP_AP_MAXLISTCOUNT, &max);
-	map_ap_get_u16(mas->inparams, MAP_AP_STARTOFFSET, &offset);
+	g_obex_apparam_get_uint16(mas->inparams, MAP_AP_MAXLISTCOUNT, &max);
+	g_obex_apparam_get_uint16(mas->inparams, MAP_AP_STARTOFFSET, &offset);
 
 	*err = messages_get_folder_listing(mas->backend_data, name, max,
 					offset, get_folder_listing_cb, mas);
@@ -559,24 +567,24 @@ static void *msg_listing_open(const char *name, int oflag, mode_t mode,
 		return NULL;
 	}
 
-	map_ap_get_u16(mas->inparams, MAP_AP_MAXLISTCOUNT, &max);
-	map_ap_get_u16(mas->inparams, MAP_AP_STARTOFFSET, &offset);
+	g_obex_apparam_get_uint16(mas->inparams, MAP_AP_MAXLISTCOUNT, &max);
+	g_obex_apparam_get_uint16(mas->inparams, MAP_AP_STARTOFFSET, &offset);
 
-	map_ap_get_u32(mas->inparams, MAP_AP_PARAMETERMASK,
+	g_obex_apparam_get_uint32(mas->inparams, MAP_AP_PARAMETERMASK,
 						&filter.parameter_mask);
-	map_ap_get_u8(mas->inparams, MAP_AP_FILTERMESSAGETYPE,
+	g_obex_apparam_get_uint8(mas->inparams, MAP_AP_FILTERMESSAGETYPE,
 						&filter.type);
-	filter.period_begin = map_ap_get_string(mas->inparams,
+	filter.period_begin = g_obex_apparam_get_string(mas->inparams,
 						MAP_AP_FILTERPERIODBEGIN);
-	filter.period_end = map_ap_get_string(mas->inparams,
+	filter.period_end = g_obex_apparam_get_string(mas->inparams,
 						MAP_AP_FILTERPERIODEND);
-	map_ap_get_u8(mas->inparams, MAP_AP_FILTERREADSTATUS,
+	g_obex_apparam_get_uint8(mas->inparams, MAP_AP_FILTERREADSTATUS,
 						&filter.read_status);
-	filter.recipient = map_ap_get_string(mas->inparams,
+	filter.recipient = g_obex_apparam_get_string(mas->inparams,
 						MAP_AP_FILTERRECIPIENT);
-	filter.originator = map_ap_get_string(mas->inparams,
+	filter.originator = g_obex_apparam_get_string(mas->inparams,
 						MAP_AP_FILTERORIGINATOR);
-	map_ap_get_u8(mas->inparams, MAP_AP_FILTERPRIORITY,
+	g_obex_apparam_get_uint8(mas->inparams, MAP_AP_FILTERPRIORITY,
 						&filter.priority);
 
 	*err = messages_get_messages_listing(mas->backend_data, name, max,
@@ -640,8 +648,6 @@ static ssize_t any_get_next_header(void *object, void *buf, size_t mtu,
 								uint8_t *hi)
 {
 	struct mas_session *mas = object;
-	size_t len;
-	uint8_t *apbuf;
 
 	DBG("");
 
@@ -654,18 +660,7 @@ static ssize_t any_get_next_header(void *object, void *buf, size_t mtu,
 		return 0;
 
 	mas->ap_sent = TRUE;
-	apbuf = map_ap_encode(mas->outparams, &len);
-
-	if (len > mtu) {
-		DBG("MTU is to small to fit application parameters header!");
-		g_free(apbuf);
-
-		return -EIO;
-	}
-
-	memcpy(buf, apbuf, len);
-
-	return len;
+	return g_obex_apparam_encode(mas->outparams, buf, mtu);
 }
 
 static void *any_open(const char *name, int oflag, mode_t mode,
-- 
1.7.11.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH obexd 9/9] core: Remove map_ap.c
  2012-08-09 10:32 [PATCH obexd 0/9] GObexApparam API Luiz Augusto von Dentz
                   ` (7 preceding siblings ...)
  2012-08-09 10:32 ` [PATCH obexd 8/9] MAP: " Luiz Augusto von Dentz
@ 2012-08-09 10:32 ` Luiz Augusto von Dentz
  2012-08-16  9:43 ` [PATCH obexd 0/9] GObexApparam API Johan Hedberg
  9 siblings, 0 replies; 11+ messages in thread
From: Luiz Augusto von Dentz @ 2012-08-09 10:32 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

It is no longer needed as MAP plugin is now using GObexApparam API
---
 Makefile.am  |   4 +-
 src/map_ap.c | 466 -----------------------------------------------------------
 src/map_ap.h |  63 --------
 3 files changed, 2 insertions(+), 531 deletions(-)
 delete mode 100644 src/map_ap.c

diff --git a/Makefile.am b/Makefile.am
index 4757f60..724dd5d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -63,7 +63,7 @@ builtin_sources += plugins/pbap.c plugins/phonebook.h \
 
 builtin_modules += mas
 builtin_sources += plugins/mas.c plugins/messages.h \
-			src/map_ap.c src/map_ap.h
+			src/map_ap.h
 
 builtin_modules += irmc
 builtin_sources += plugins/irmc.c
@@ -127,7 +127,7 @@ client_obex_client_SOURCES = $(gdbus_sources) $(gobex_sources) \
 				client/transport.h client/transport.c \
 				client/dbus.h client/dbus.c \
 				client/driver.h client/driver.c \
-				src/map_ap.h src/map_ap.c
+				src/map_ap.h
 
 client_obex_client_LDADD = @GLIB_LIBS@ @DBUS_LIBS@ @BLUEZ_LIBS@
 endif
diff --git a/src/map_ap.c b/src/map_ap.c
deleted file mode 100644
index 6efc484..0000000
--- a/src/map_ap.c
+++ /dev/null
@@ -1,466 +0,0 @@
-/*
- *
- *  OBEX Server
- *
- *  Copyright (C) 2010-2011  Nokia Corporation
- *
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- *
- */
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <string.h>
-
-#include "log.h"
-
-#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;
-};
-
-/* 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))
-		return -1;
-
-	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;
-	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 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);
-}
-
-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)
-{
-	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;
-		}
-	}
-
-	g_hash_table_foreach(ap, ap_entry_dump, NULL);
-
-	return ap;
-}
-
-static void ap_encode_u8(GByteArray *buf, struct ap_entry *entry)
-{
-	struct obex_ap_header *hdr;
-
-	hdr = (struct obex_ap_header *) buf->data + buf->len;
-	g_byte_array_set_size(buf, buf->len + sizeof(*hdr) + 1);
-
-	hdr->tag = entry->tag;
-	hdr->len = 1;
-	hdr->val[0] = entry->val.u8;
-}
-
-static void ap_encode_u16(GByteArray *buf, struct ap_entry *entry)
-{
-	struct obex_ap_header *hdr;
-	uint16_t val;
-
-	hdr = (struct obex_ap_header *) buf->data + buf->len;
-
-	g_byte_array_set_size(buf, buf->len + sizeof(*hdr) + 2);
-
-	hdr->tag = entry->tag;
-	hdr->len = 2;
-
-	val = GUINT16_TO_BE(entry->val.u16);
-	memcpy(hdr->val, &val, sizeof(val));
-}
-
-static void ap_encode_u32(GByteArray *buf, struct ap_entry *entry)
-{
-	uint32_t val;
-	struct obex_ap_header *hdr;
-
-	hdr = (struct obex_ap_header *) buf->data + buf->len;
-	g_byte_array_set_size(buf, buf->len + sizeof(*hdr) + 4);
-
-	hdr->tag = entry->tag;
-	hdr->len = 4;
-
-	val = GUINT32_TO_BE(entry->val.u16);
-	memcpy(hdr->val, &val, sizeof(val));
-}
-
-static void ap_encode_str(GByteArray *buf, struct ap_entry *entry)
-{
-	size_t len;
-	struct obex_ap_header *hdr;
-
-	hdr = (struct obex_ap_header *) buf->data + buf->len;
-	len = strlen(entry->val.str);
-	g_byte_array_set_size(buf, buf->len + sizeof(*hdr) + len);
-
-	hdr->tag = entry->tag;
-	hdr->len = len;
-
-	memcpy(hdr->val, entry->val.str, len);
-}
-
-uint8_t *map_ap_encode(map_ap_t *ap, size_t *length)
-{
-	GByteArray *buf;
-	GHashTableIter iter;
-	gpointer key, value;
-	struct ap_entry *entry;
-	int offset;
-
-	buf = g_byte_array_new();
-	g_hash_table_iter_init(&iter, ap);
-
-	while (g_hash_table_iter_next(&iter, &key, &value)) {
-		entry = (struct ap_entry *) value;
-		offset = find_ap_def_offset(entry->tag);
-
-		switch (ap_defs[offset].type) {
-		case APT_UINT8:
-			ap_encode_u8(buf, entry);
-			break;
-		case APT_UINT16:
-			ap_encode_u16(buf, entry);
-			break;
-		case APT_UINT32:
-			ap_encode_u32(buf, entry);
-			break;
-		case APT_STR:
-			ap_encode_str(buf, entry);
-			break;
-		}
-	}
-
-	*length = buf->len;
-
-	return g_byte_array_free(buf, FALSE);
-}
-
-gboolean map_ap_get_u8(map_ap_t *ap, enum map_ap_tag tag, uint8_t *val)
-{
-	struct ap_entry *entry;
-	int offset = find_ap_def_offset(tag);
-
-	if (offset < 0 || ap_defs[offset].type != APT_UINT8)
-		return FALSE;
-
-	entry = g_hash_table_lookup(ap, GINT_TO_POINTER(tag));
-	if (entry == NULL)
-		return FALSE;
-
-	*val = entry->val.u8;
-
-	return TRUE;
-}
-
-gboolean map_ap_get_u16(map_ap_t *ap, enum map_ap_tag tag, uint16_t *val)
-{
-	struct ap_entry *entry;
-	int offset = find_ap_def_offset(tag);
-
-	if (offset < 0 || ap_defs[offset].type != APT_UINT16)
-		return FALSE;
-
-	entry = g_hash_table_lookup(ap, GINT_TO_POINTER(tag));
-	if (entry == NULL)
-		return FALSE;
-
-	*val = entry->val.u16;
-
-	return TRUE;
-}
-
-gboolean map_ap_get_u32(map_ap_t *ap, enum map_ap_tag tag, uint32_t *val)
-{
-	struct ap_entry *entry;
-	int offset = find_ap_def_offset(tag);
-
-	if (offset < 0 || ap_defs[offset].type != APT_UINT32)
-		return FALSE;
-
-	entry = g_hash_table_lookup(ap, GINT_TO_POINTER(tag));
-	if (entry == NULL)
-		return FALSE;
-
-	*val = entry->val.u32;
-
-	return TRUE;
-}
-
-const char *map_ap_get_string(map_ap_t *ap, enum map_ap_tag tag)
-{
-	struct ap_entry *entry;
-	int offset = find_ap_def_offset(tag);
-
-	if (offset < 0 || ap_defs[offset].type != APT_STR)
-		return NULL;
-
-	entry = g_hash_table_lookup(ap, GINT_TO_POINTER(tag));
-	if (entry == NULL)
-		return NULL;
-
-	return entry->val.str;
-}
-
-gboolean map_ap_set_u8(map_ap_t *ap, enum map_ap_tag tag, uint8_t val)
-{
-	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)
-{
-	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)
-{
-	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)
-{
-	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;
-}
diff --git a/src/map_ap.h b/src/map_ap.h
index 24254af..da108fe 100644
--- a/src/map_ap.h
+++ b/src/map_ap.h
@@ -21,9 +21,6 @@
  *
  */
 
-#include <glib.h>
-#include <inttypes.h>
-
 /* List of OBEX application parameters tags as per MAP specification. */
 enum map_ap_tag {
 	MAP_AP_MAXLISTCOUNT		= 0x01,		/* uint16_t	*/
@@ -52,63 +49,3 @@ enum map_ap_tag {
 	MAP_AP_STATUSVALUE		= 0x18,		/* uint8_t	*/
 	MAP_AP_MSETIME			= 0x19,		/* char *	*/
 };
-
-/* Data type representing MAP application parameters. Consider opaque. */
-typedef GHashTable map_ap_t;
-
-/* Creates a new empty MAP application parameters object. */
-map_ap_t *map_ap_new(void);
-
-/* Frees all the memory used by MAP application parameters object. */
-void map_ap_free(map_ap_t *ap);
-
-/* Parses given buffer that is a payload of OBEX application parameter header
- * with a given length. Returned value can be used in calls to map_ap_get_*()
- * and map_ap_set_*(). It has to be freed using map_ap_free(). It also takes
- * care of converting all the data to host byte order, so this is the byte
- * order used in map_ap_get_*()/map_ap_set_*().
- *
- * Returns NULL in case of failure.
- */
-map_ap_t *map_ap_decode(const uint8_t *buffer, size_t length);
-
-/* Takes all parameters currently set and packs them into a buffer with OBEX
- * application parameters header payload format.
- *
- * Returns newly allocated buffer of size 'length'. Free with g_free().
- */
-uint8_t *map_ap_encode(map_ap_t *ap, size_t *length);
-
-/* Following family of functions reads value of MAP parameter with given tag.
- * Use the one with appropriate type for a given tag, as noted above in
- * map_ap_tag declaration comments.
- *
- * Returns TRUE when value is present. FALSE if it is not or the function is
- * used get a parameter of a different type. When FALSE is returned, variable
- * pointed by 'val' is left intact.
- */
-gboolean map_ap_get_u8(map_ap_t *ap, enum map_ap_tag tag, uint8_t *val);
-gboolean map_ap_get_u16(map_ap_t *ap, enum map_ap_tag tag, uint16_t *val);
-gboolean map_ap_get_u32(map_ap_t *ap, enum map_ap_tag tag, uint32_t *val);
-
-/* Reads value of MAP parameter with given tag that is of a string type.
- *
- * Returns NULL if parameter is not present in ap or given tag is not of a
- * string type.
- */
-const char *map_ap_get_string(map_ap_t *ap, enum map_ap_tag tag);
-
-/* Following family of functions sets the value of MAP parameter with given
- * tag. Use the one with appropriate type for a given tag, as noted above in
- * map_ap_tag declaration comments.
- *
- * If there is already a parameter with given tag present, it will be
- * replaced. map_ap_set_string() makes its own copy of given string.
- *
- * Returns TRUE on success (the tag is known and the function chosen matches
- * the type of tag).
- */
-gboolean map_ap_set_u8(map_ap_t *ap, enum map_ap_tag tag, uint8_t val);
-gboolean map_ap_set_u16(map_ap_t *ap, enum map_ap_tag tag, uint16_t val);
-gboolean map_ap_set_u32(map_ap_t *ap, enum map_ap_tag tag, uint32_t val);
-gboolean map_ap_set_string(map_ap_t *ap, enum map_ap_tag tag, const char *val);
-- 
1.7.11.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH obexd 0/9] GObexApparam API
  2012-08-09 10:32 [PATCH obexd 0/9] GObexApparam API Luiz Augusto von Dentz
                   ` (8 preceding siblings ...)
  2012-08-09 10:32 ` [PATCH obexd 9/9] core: Remove map_ap.c Luiz Augusto von Dentz
@ 2012-08-16  9:43 ` Johan Hedberg
  9 siblings, 0 replies; 11+ messages in thread
From: Johan Hedberg @ 2012-08-16  9:43 UTC (permalink / raw)
  To: Luiz Augusto von Dentz; +Cc: linux-bluetooth

Hi Luiz,

On Thu, Aug 09, 2012, Luiz Augusto von Dentz wrote:
> GObexApparam can encode/decode OBEX application parameter in a generic
> way, only the tags are expecific to each profile.
> 
> Some remarks:
> 
>   - Tag data is stored in network order/big endian because its type is
>     not known.
>   - Internally it uses a GHashTable to store the values which doesn't
>     guarantee the order of the tag will be exact the same as inserted.
> 
> Luiz Augusto von Dentz (9):
>   gobex: Introduce GObexApparam API
>   gobex: Add unit tests for GObexApparam API
>   gobex: Integrate GObexApparam with GObexHeader
>   gobex: Add unit test for encoding/decoding apparam headers
>   gobex: Add debug option to apparam
>   client: Port PBAP module to use GObexApparam
>   PBAP: Make use of GObexApparam API
>   MAP: Make use of GObexApparam API
>   core: Remove map_ap.c
> 
>  Makefile.am               |  18 +-
>  client/pbap.c             | 181 +++++-------------
>  gobex/gobex-apparam.c     | 355 +++++++++++++++++++++++++++++++++++
>  gobex/gobex-apparam.h     |  59 ++++++
>  gobex/gobex-debug.h       |   7 +-
>  gobex/gobex-header.c      |  25 +++
>  gobex/gobex-header.h      |   3 +
>  gobex/gobex.c             |  11 +-
>  plugins/mas.c             |  81 ++++----
>  plugins/pbap.c            | 180 ++++--------------
>  plugins/phonebook.h       |   2 +-
>  src/map_ap.c              | 466 ----------------------------------------------
>  src/map_ap.h              |  63 -------
>  unit/test-gobex-apparam.c | 422 +++++++++++++++++++++++++++++++++++++++++
>  unit/test-gobex-header.c  |  46 +++++
>  15 files changed, 1058 insertions(+), 861 deletions(-)
>  create mode 100644 gobex/gobex-apparam.c
>  create mode 100644 gobex/gobex-apparam.h
>  delete mode 100644 src/map_ap.c
>  create mode 100644 unit/test-gobex-apparam.c

All patches in this set have been applied. Thanks.

Johan

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2012-08-16  9:43 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-08-09 10:32 [PATCH obexd 0/9] GObexApparam API Luiz Augusto von Dentz
2012-08-09 10:32 ` [PATCH obexd 1/9] gobex: Introduce " Luiz Augusto von Dentz
2012-08-09 10:32 ` [PATCH obexd 2/9] gobex: Add unit tests for " Luiz Augusto von Dentz
2012-08-09 10:32 ` [PATCH obexd 3/9] gobex: Integrate GObexApparam with GObexHeader Luiz Augusto von Dentz
2012-08-09 10:32 ` [PATCH obexd 4/9] gobex: Add unit test for encoding/decoding apparam headers Luiz Augusto von Dentz
2012-08-09 10:32 ` [PATCH obexd 5/9] gobex: Add debug option to apparam Luiz Augusto von Dentz
2012-08-09 10:32 ` [PATCH obexd 6/9] client: Port PBAP module to use GObexApparam Luiz Augusto von Dentz
2012-08-09 10:32 ` [PATCH obexd 7/9] PBAP: Make use of GObexApparam API Luiz Augusto von Dentz
2012-08-09 10:32 ` [PATCH obexd 8/9] MAP: " Luiz Augusto von Dentz
2012-08-09 10:32 ` [PATCH obexd 9/9] core: Remove map_ap.c Luiz Augusto von Dentz
2012-08-16  9:43 ` [PATCH obexd 0/9] GObexApparam API Johan Hedberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).