5 * Copyright (C) 2010-2011 Nokia Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #ifdef OPENSOURCE_PATCH
34 #ifdef OPENSOURCE_PATCH
42 /* NOTE: ap_defs array has to be kept in sync with map_ap_tag. */
43 static const struct ap_def {
47 { "MAXLISTCOUNT", APT_UINT16 },
48 { "STARTOFFSET", APT_UINT16 },
49 { "FILTERMESSAGETYPE", APT_UINT8 },
50 { "FILTERPERIODBEGIN", APT_STR },
51 { "FILTERPERIODEND", APT_STR },
52 { "FILTERREADSTATUS", APT_UINT8 },
53 { "FILTERRECIPIENT", APT_STR },
54 { "FILTERORIGINATOR", APT_STR },
55 { "FILTERPRIORITY", APT_UINT8 },
56 { "ATTACHMENT", APT_UINT8 },
57 { "TRANSPARENT", APT_UINT8 },
58 { "RETRY", APT_UINT8 },
59 { "NEWMESSAGE", APT_UINT8 },
60 { "NOTIFICATIONSTATUS", APT_UINT8 },
61 { "MASINSTANCEID", APT_UINT8 },
62 { "PARAMETERMASK", APT_UINT32 },
63 { "FOLDERLISTINGSIZE", APT_UINT16 },
64 { "MESSAGESLISTINGSIZE", APT_UINT16 },
65 { "SUBJECTLENGTH", APT_UINT8 },
66 { "CHARSET", APT_UINT8 },
67 { "FRACTIONREQUEST", APT_UINT8 },
68 { "FRACTIONDELIVER", APT_UINT8 },
69 { "STATUSINDICATOR", APT_UINT8 },
70 { "STATUSVALUE", APT_UINT8 },
71 { "MSETIME", APT_STR },
84 /* This comes from OBEX specs */
85 struct obex_ap_header {
89 } __attribute__ ((packed));
91 static int find_ap_def_offset(uint8_t tag)
93 if (tag == 0 || tag > G_N_ELEMENTS(ap_defs))
99 static void ap_entry_dump(gpointer tag, gpointer val, gpointer user_data)
101 struct ap_entry *entry = val;
104 offset = find_ap_def_offset(GPOINTER_TO_INT(tag));
106 switch (ap_defs[offset].type) {
108 DBG("%-30s %08x", ap_defs[offset].name, entry->val.u8);
111 DBG("%-30s %08x", ap_defs[offset].name, entry->val.u16);
114 DBG("%-30s %08x", ap_defs[offset].name, entry->val.u32);
117 DBG("%-30s %s", ap_defs[offset].name, entry->val.str);
122 static void ap_entry_free(gpointer val)
124 struct ap_entry *entry = val;
127 offset = find_ap_def_offset(entry->tag);
129 if (offset >= 0 && ap_defs[offset].type == APT_STR)
130 g_free(entry->val.str);
136 map_ap_t *map_ap_new(void)
138 #ifdef OPENSOURCE_PATCH
139 return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
146 void map_ap_free(map_ap_t *ap)
148 #ifdef OPENSOURCE_PATCH
152 g_hash_table_destroy(ap);
156 #ifdef OPENSOURCE_PATCH
157 static void ap_decode_u8(map_ap_t *ap, const struct obex_ap_header *hdr)
160 DBG("Value of tag %u is %u byte(s) long instead of expected "
161 "1 byte - skipped!", hdr->tag, hdr->len);
165 map_ap_set_u8(ap, hdr->tag, hdr->val[0]);
168 static void ap_decode_u16(map_ap_t *ap, const struct obex_ap_header *hdr)
173 DBG("Value of tag %u is %u byte(s) long instead of expected "
174 "2 bytes - skipped!", hdr->tag, hdr->len);
178 memcpy(&val, hdr->val, sizeof(val));
179 map_ap_set_u16(ap, hdr->tag, GUINT16_FROM_BE(val));
182 static void ap_decode_u32(map_ap_t *ap, const struct obex_ap_header *hdr)
187 DBG("Value of tag %u is %u byte(s) long instead of expected "
188 "4 bytes - skipped!", hdr->tag, hdr->len);
192 memcpy(&val, hdr->val, sizeof(val));
193 map_ap_set_u32(ap, hdr->tag, GUINT32_FROM_BE(val));
196 static void ap_decode_str(map_ap_t *ap, const struct obex_ap_header *hdr)
198 char *val = g_malloc0(hdr->len + 1);
200 memcpy(val, hdr->val, hdr->len);
201 map_ap_set_string(ap, hdr->tag, val);
207 map_ap_t *map_ap_decode(const uint8_t *buffer, size_t length)
209 #ifdef OPENSOURCE_PATCH
211 struct obex_ap_header *hdr;
219 for (done = 0; done < length; done += hdr->len + sizeof(*hdr)) {
220 hdr = (struct obex_ap_header *)(buffer + done);
222 offset = find_ap_def_offset(hdr->tag);
225 DBG("Unknown tag %u (length %u) - skipped.",
230 switch (ap_defs[offset].type) {
232 ap_decode_u8(ap, hdr);
235 ap_decode_u16(ap, hdr);
238 ap_decode_u32(ap, hdr);
241 ap_decode_str(ap, hdr);
246 g_hash_table_foreach(ap, ap_entry_dump, NULL);
254 uint8_t *map_ap_encode(map_ap_t *ap, size_t *length)
261 gboolean map_ap_get_u8(map_ap_t *ap, enum map_ap_tag tag, uint8_t *val)
264 struct ap_entry *entry;
265 int offset = find_ap_def_offset(tag);
267 if (offset < 0 || ap_defs[offset].type != APT_UINT8)
270 entry = g_hash_table_lookup(ap, GINT_TO_POINTER(tag));
275 *val = entry->val.u8;
283 gboolean map_ap_get_u16(map_ap_t *ap, enum map_ap_tag tag, uint16_t *val)
286 struct ap_entry *entry;
287 int offset = find_ap_def_offset(tag);
289 if (offset < 0 || ap_defs[offset].type != APT_UINT16)
292 entry = g_hash_table_lookup(ap, GINT_TO_POINTER(tag));
297 *val = entry->val.u16;
305 gboolean map_ap_get_u32(map_ap_t *ap, enum map_ap_tag tag, uint32_t *val)
308 struct ap_entry *entry;
309 int offset = find_ap_def_offset(tag);
311 if (offset < 0 || ap_defs[offset].type != APT_UINT32)
314 entry = g_hash_table_lookup(ap, GINT_TO_POINTER(tag));
319 *val = entry->val.u32;
327 const char *map_ap_get_string(map_ap_t *ap, enum map_ap_tag tag)
330 struct ap_entry *entry;
331 int offset = find_ap_def_offset(tag);
333 if (offset < 0 || ap_defs[offset].type != APT_STR)
336 entry = g_hash_table_lookup(ap, GINT_TO_POINTER(tag));
341 return g_strdup(entry->val.str);
347 gboolean map_ap_set_u8(map_ap_t *ap, enum map_ap_tag tag, uint8_t val)
349 #ifdef OPENSOURCE_PATCH
350 struct ap_entry *entry;
351 int offset = find_ap_def_offset(tag);
353 if (offset < 0 || ap_defs[offset].type != APT_UINT8)
356 entry = g_new0(struct ap_entry, 1);
360 g_hash_table_insert(ap, GINT_TO_POINTER(tag), entry);
368 gboolean map_ap_set_u16(map_ap_t *ap, enum map_ap_tag tag, uint16_t val)
370 #ifdef OPENSOURCE_PATCH
371 struct ap_entry *entry;
372 int offset = find_ap_def_offset(tag);
374 if (offset < 0 || ap_defs[offset].type != APT_UINT16)
377 entry = g_new0(struct ap_entry, 1);
379 entry->val.u16 = val;
381 g_hash_table_insert(ap, GINT_TO_POINTER(tag), entry);
389 gboolean map_ap_set_u32(map_ap_t *ap, enum map_ap_tag tag, uint32_t val)
391 #ifdef OPENSOURCE_PATCH
392 struct ap_entry *entry;
393 int offset = find_ap_def_offset(tag);
395 if (offset < 0 || ap_defs[offset].type != APT_UINT32)
398 entry = g_new0(struct ap_entry, 1);
400 entry->val.u32 = val;
402 g_hash_table_insert(ap, GINT_TO_POINTER(tag), entry);
410 gboolean map_ap_set_string(map_ap_t *ap, enum map_ap_tag tag, const char *val)
412 #ifdef OPENSOURCE_PATCH
413 struct ap_entry *entry;
414 int offset = find_ap_def_offset(tag);
416 if (offset < 0 || ap_defs[offset].type != APT_STR)
419 entry = g_new0(struct ap_entry, 1);
421 entry->val.str = g_strdup(val);
423 g_hash_table_insert(ap, GINT_TO_POINTER(tag), entry);