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-2012, Masashi Honma <masashi.honma@gmail.com>
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
14 #define FLAG_MESSAGE_BEGIN (1 << 7)
15 #define FLAG_MESSAGE_END (1 << 6)
16 #define FLAG_CHUNK (1 << 5)
17 #define FLAG_SHORT_RECORD (1 << 4)
18 #define FLAG_ID_LENGTH_PRESENT (1 << 3)
19 #define FLAG_TNF_NFC_FORUM (0x01)
20 #define FLAG_TNF_RFC2046 (0x02)
32 static const char wifi_handover_type[] = "application/vnd.wfa.wsc";
33 static const char p2p_handover_type[] = "application/vnd.wfa.p2p";
35 static int ndef_parse_record(const u8 *data, u32 size,
36 struct ndef_record *record)
38 const u8 *pos = data + 1;
42 record->type_length = *pos++;
43 if (data[0] & FLAG_SHORT_RECORD) {
46 record->payload_length = *pos++;
52 len = WPA_GET_BE32(pos);
53 if (len > size - 6 || len > 20000)
55 record->payload_length = len;
59 if (data[0] & FLAG_ID_LENGTH_PRESENT) {
60 if ((int) size < pos - data + 1)
62 record->id_length = *pos++;
64 record->id_length = 0;
66 record->type = record->type_length == 0 ? NULL : pos;
67 pos += record->type_length;
69 record->id = record->id_length == 0 ? NULL : pos;
70 pos += record->id_length;
72 record->payload = record->payload_length == 0 ? NULL : pos;
73 pos += record->payload_length;
75 record->total_length = pos - data;
76 if (record->total_length > size ||
77 record->total_length < record->payload_length)
83 static struct wpabuf * ndef_parse_records(const struct wpabuf *buf,
84 int (*filter)(struct ndef_record *))
86 struct ndef_record record;
87 int len = wpabuf_len(buf);
88 const u8 *data = wpabuf_head(buf);
91 if (ndef_parse_record(data, len, &record) < 0) {
92 wpa_printf(MSG_ERROR, "NDEF : Failed to parse");
95 if (filter == NULL || filter(&record))
96 return wpabuf_alloc_copy(record.payload,
97 record.payload_length);
98 data += record.total_length;
99 len -= record.total_length;
101 wpa_printf(MSG_ERROR, "NDEF : Record not found");
106 static struct wpabuf * ndef_build_record(u8 flags, const void *type,
107 u8 type_length, void *id,
109 const struct wpabuf *payload)
111 struct wpabuf *record;
115 size_t payload_length = wpabuf_len(payload);
117 short_record = payload_length < 256 ? 1 : 0;
119 total_len = 2; /* flag + type length */
121 total_len += short_record ? sizeof(u8) : sizeof(u32);
124 total_len += type_length + id_length + payload_length;
125 record = wpabuf_alloc(total_len);
126 if (record == NULL) {
127 wpa_printf(MSG_ERROR, "NDEF : Failed to allocate "
134 local_flag |= FLAG_ID_LENGTH_PRESENT;
136 local_flag |= FLAG_SHORT_RECORD;
137 wpabuf_put_u8(record, local_flag);
139 wpabuf_put_u8(record, type_length);
142 wpabuf_put_u8(record, payload_length);
144 wpabuf_put_be32(record, payload_length);
147 wpabuf_put_u8(record, id_length);
148 wpabuf_put_data(record, type, type_length);
149 wpabuf_put_data(record, id, id_length);
150 wpabuf_put_buf(record, payload);
155 static int wifi_filter(struct ndef_record *record)
157 if (record->type == NULL ||
158 record->type_length != os_strlen(wifi_handover_type))
160 if (os_memcmp(record->type, wifi_handover_type,
161 os_strlen(wifi_handover_type)) != 0)
167 struct wpabuf * ndef_parse_wifi(const struct wpabuf *buf)
169 return ndef_parse_records(buf, wifi_filter);
173 struct wpabuf * ndef_build_wifi(const struct wpabuf *buf)
175 return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
176 FLAG_TNF_RFC2046, wifi_handover_type,
177 os_strlen(wifi_handover_type), NULL, 0, buf);
181 static int p2p_filter(struct ndef_record *record)
183 if (record->type == NULL ||
184 record->type_length != os_strlen(p2p_handover_type))
186 if (os_memcmp(record->type, p2p_handover_type,
187 os_strlen(p2p_handover_type)) != 0)
193 struct wpabuf * ndef_parse_p2p(const struct wpabuf *buf)
195 return ndef_parse_records(buf, p2p_filter);
199 struct wpabuf * ndef_build_p2p(const struct wpabuf *buf)
201 return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
202 FLAG_TNF_RFC2046, p2p_handover_type,
203 os_strlen(p2p_handover_type), NULL, 0, buf);