3 * OBEX library with GLib integration
5 * Copyright (C) 2012 Intel Corporation.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include "gobex-apparam.h"
32 #include "gobex-debug.h"
34 struct _GObexApparam {
42 /* Data is stored in network order */
50 } __attribute__ ((packed));
52 static struct apparam_tag *tag_new(guint8 id, guint8 len, const void *data)
54 struct apparam_tag *tag;
56 tag = g_malloc0(2 + len);
59 memcpy(tag->value.data, data, len);
64 static GObexApparam *g_obex_apparam_new(void)
66 GObexApparam *apparam;
68 apparam = g_new0(GObexApparam, 1);
69 apparam->tags = g_hash_table_new_full(g_direct_hash, g_direct_equal,
75 static struct apparam_tag *apparam_tag_decode(const void *data, gsize size,
78 struct apparam_tag *tag;
79 const guint8 *ptr = data;
92 tag = tag_new(id, len, ptr + 2);
96 *parsed = 2 + tag->len;
101 GObexApparam *g_obex_apparam_decode(const void *data, gsize size)
103 GObexApparam *apparam;
107 #ifndef __TIZEN_PATCH__
110 #endif /* __TIZEN_PATCH__ */
112 apparam = g_obex_apparam_new();
114 tags = apparam->tags;
115 while (count < size) {
116 struct apparam_tag *tag;
120 tag = apparam_tag_decode(data + count, size - count, &parsed);
125 g_hash_table_insert(tags, GUINT_TO_POINTER(id), tag);
131 g_obex_apparam_free(apparam);
138 static gssize tag_encode(struct apparam_tag *tag, void *buf, gsize len)
140 gsize count = 2 + tag->len;
145 memcpy(buf, tag, count);
150 gssize g_obex_apparam_encode(GObexApparam *apparam, void *buf, gsize len)
157 g_hash_table_iter_init(&iter, apparam->tags);
158 while (g_hash_table_iter_next(&iter, &key, &value)) {
159 struct apparam_tag *tag = value;
161 ret = tag_encode(tag, buf + count, len - count);
171 #ifdef __TIZEN_PATCH__
172 void g_obex_apparam_remove_all(GObexApparam *apparam)
174 g_hash_table_remove_all(apparam->tags);
178 GObexApparam *g_obex_apparam_set_bytes(GObexApparam *apparam, guint8 id,
179 const void *value, gsize len)
181 struct apparam_tag *tag;
185 apparam = g_obex_apparam_new();
187 tag = tag_new(id, len, value);
188 g_hash_table_replace(apparam->tags, GUINT_TO_POINTER(uid), tag);
193 GObexApparam *g_obex_apparam_set_uint8(GObexApparam *apparam, guint8 id,
196 g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x value %u", id, value);
198 return g_obex_apparam_set_bytes(apparam, id, &value, 1);
201 GObexApparam *g_obex_apparam_set_uint16(GObexApparam *apparam, guint8 id,
204 guint16 num = g_htons(value);
206 g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x value %u", id, value);
208 return g_obex_apparam_set_bytes(apparam, id, &num, 2);
211 GObexApparam *g_obex_apparam_set_uint32(GObexApparam *apparam, guint8 id,
214 guint32 num = g_htonl(value);
216 g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x value %u", id, value);
218 return g_obex_apparam_set_bytes(apparam, id, &num, 4);
221 GObexApparam *g_obex_apparam_set_uint64(GObexApparam *apparam, guint8 id,
224 guint64 num = GUINT64_TO_BE(value);
226 g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x value %"
227 G_GUINT64_FORMAT, id, value);
229 return g_obex_apparam_set_bytes(apparam, id, &num, 8);
232 GObexApparam *g_obex_apparam_set_string(GObexApparam *apparam, guint8 id,
237 g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x value %s", id, value);
239 len = strlen(value) + 1;
240 if (len > G_MAXUINT8) {
241 ((char *) value)[G_MAXUINT8 - 1] = '\0';
245 return g_obex_apparam_set_bytes(apparam, id, value, len);
248 static struct apparam_tag *g_obex_apparam_find_tag(GObexApparam *apparam,
251 return g_hash_table_lookup(apparam->tags, GUINT_TO_POINTER(id));
254 gboolean g_obex_apparam_get_uint8(GObexApparam *apparam, guint8 id,
257 struct apparam_tag *tag;
259 g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
261 tag = g_obex_apparam_find_tag(apparam, id);
265 *dest = tag->value.u8;
267 g_obex_debug(G_OBEX_DEBUG_APPARAM, "%u", *dest);
272 gboolean g_obex_apparam_get_uint16(GObexApparam *apparam, guint8 id,
275 struct apparam_tag *tag;
277 g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
279 tag = g_obex_apparam_find_tag(apparam, id);
283 if (tag->len < sizeof(*dest))
286 *dest = g_ntohs(tag->value.u16);
288 g_obex_debug(G_OBEX_DEBUG_APPARAM, "%u", *dest);
293 gboolean g_obex_apparam_get_uint32(GObexApparam *apparam, guint8 id,
296 struct apparam_tag *tag;
298 g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
300 tag = g_obex_apparam_find_tag(apparam, id);
304 if (tag->len < sizeof(*dest))
307 *dest = g_ntohl(tag->value.u32);
309 g_obex_debug(G_OBEX_DEBUG_APPARAM, "%u", *dest);
314 gboolean g_obex_apparam_get_uint64(GObexApparam *apparam, guint8 id,
317 struct apparam_tag *tag;
319 g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
321 tag = g_obex_apparam_find_tag(apparam, id);
325 if (tag->len < sizeof(*dest))
328 *dest = GUINT64_FROM_BE(tag->value.u64);
330 g_obex_debug(G_OBEX_DEBUG_APPARAM, "%" G_GUINT64_FORMAT, *dest);
335 char *g_obex_apparam_get_string(GObexApparam *apparam, guint8 id)
337 struct apparam_tag *tag;
340 g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
342 tag = g_obex_apparam_find_tag(apparam, id);
346 string = g_strndup(tag->value.string, tag->len);
348 g_obex_debug(G_OBEX_DEBUG_APPARAM, "%s", string);
353 gboolean g_obex_apparam_get_bytes(GObexApparam *apparam, guint8 id,
354 const guint8 **val, gsize *len)
356 struct apparam_tag *tag;
358 g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
360 tag = g_obex_apparam_find_tag(apparam, id);
365 *val = tag->value.data;
370 void g_obex_apparam_free(GObexApparam *apparam)
372 g_hash_table_unref(apparam->tags);