2 * NDEF(NFC Data Exchange Format) routines for Wi-Fi Protected Setup
3 * Reference is "NFCForum-TS-NDEF_1.0 2006-07-24".
4 * Copyright (c) 2009, Masashi Honma <honma@ictec.co.jp>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
20 #define FLAG_MESSAGE_BEGIN (1 << 7)
21 #define FLAG_MESSAGE_END (1 << 6)
22 #define FLAG_CHUNK (1 << 5)
23 #define FLAG_SHORT_RECORD (1 << 4)
24 #define FLAG_ID_LENGTH_PRESENT (1 << 3)
25 #define FLAG_TNF_RFC2046 (0x02)
37 static char wifi_handover_type[] = "application/vnd.wfa.wsc";
39 static int ndef_parse_record(u8 *data, u32 size, struct ndef_record *record)
45 record->type_length = *pos++;
46 if (data[0] & FLAG_SHORT_RECORD) {
49 record->payload_length = *pos++;
53 record->payload_length = ntohl(*(u32 *)pos);
57 if (data[0] & FLAG_ID_LENGTH_PRESENT) {
58 if ((int) size < pos - data + 1)
60 record->id_length = *pos++;
62 record->id_length = 0;
64 record->type = record->type_length == 0 ? NULL : pos;
65 pos += record->type_length;
67 record->id = record->id_length == 0 ? NULL : pos;
68 pos += record->id_length;
70 record->payload = record->payload_length == 0 ? NULL : pos;
71 pos += record->payload_length;
73 record->total_length = pos - data;
74 if (record->total_length > size)
80 static struct wpabuf * ndef_parse_records(struct wpabuf *buf,
81 int (*filter)(struct ndef_record *))
83 struct ndef_record record;
84 int len = wpabuf_len(buf);
85 u8 *data = wpabuf_mhead(buf);
88 if (ndef_parse_record(data, len, &record) < 0) {
89 wpa_printf(MSG_ERROR, "NDEF : Failed to parse");
92 if (filter == NULL || filter(&record))
93 return wpabuf_alloc_copy(record.payload,
94 record.payload_length);
95 data += record.total_length;
96 len -= record.total_length;
98 wpa_printf(MSG_ERROR, "NDEF : Record not found");
103 static struct wpabuf * ndef_build_record(u8 flags, void *type,
104 u8 type_length, void *id,
105 u8 id_length, void *payload,
108 struct wpabuf *record;
113 short_record = payload_length < 256 ? 1 : 0;
115 total_len = 2; /* flag + type length */
117 total_len += short_record ? sizeof(u8) : sizeof(u32);
120 total_len += type_length + id_length + payload_length;
121 record = wpabuf_alloc(total_len);
122 if (record == NULL) {
123 wpa_printf(MSG_ERROR, "NDEF : Failed to allocate "
130 local_flag |= FLAG_ID_LENGTH_PRESENT;
132 local_flag |= FLAG_SHORT_RECORD;
133 wpabuf_put_u8(record, local_flag);
135 wpabuf_put_u8(record, type_length);
138 wpabuf_put_u8(record, payload_length);
140 wpabuf_put_be32(record, payload_length);
143 wpabuf_put_u8(record, id_length);
144 wpabuf_put_data(record, type, type_length);
145 wpabuf_put_data(record, id, id_length);
146 wpabuf_put_data(record, payload, payload_length);
151 static int wifi_filter(struct ndef_record *record)
153 if (record->type_length != os_strlen(wifi_handover_type))
155 if (os_memcmp(record->type, wifi_handover_type,
156 os_strlen(wifi_handover_type)) != 0)
162 struct wpabuf * ndef_parse_wifi(struct wpabuf *buf)
164 return ndef_parse_records(buf, wifi_filter);
168 struct wpabuf * ndef_build_wifi(struct wpabuf *buf)
170 return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
171 FLAG_TNF_RFC2046, wifi_handover_type,
172 os_strlen(wifi_handover_type), NULL, 0,
173 wpabuf_mhead(buf), wpabuf_len(buf));