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.
13 #include "wps/wps_i.h"
15 #define FLAG_MESSAGE_BEGIN (1 << 7)
16 #define FLAG_MESSAGE_END (1 << 6)
17 #define FLAG_CHUNK (1 << 5)
18 #define FLAG_SHORT_RECORD (1 << 4)
19 #define FLAG_ID_LENGTH_PRESENT (1 << 3)
20 #define FLAG_TNF_RFC2046 (0x02)
32 static char wifi_handover_type[] = "application/vnd.wfa.wsc";
34 static int ndef_parse_record(u8 *data, u32 size, struct ndef_record *record)
40 record->type_length = *pos++;
41 if (data[0] & FLAG_SHORT_RECORD) {
44 record->payload_length = *pos++;
48 record->payload_length = ntohl(*(u32 *)pos);
52 if (data[0] & FLAG_ID_LENGTH_PRESENT) {
53 if ((int) size < pos - data + 1)
55 record->id_length = *pos++;
57 record->id_length = 0;
59 record->type = record->type_length == 0 ? NULL : pos;
60 pos += record->type_length;
62 record->id = record->id_length == 0 ? NULL : pos;
63 pos += record->id_length;
65 record->payload = record->payload_length == 0 ? NULL : pos;
66 pos += record->payload_length;
68 record->total_length = pos - data;
69 if (record->total_length > size)
75 static struct wpabuf * ndef_parse_records(struct wpabuf *buf,
76 int (*filter)(struct ndef_record *))
78 struct ndef_record record;
79 int len = wpabuf_len(buf);
80 u8 *data = wpabuf_mhead(buf);
83 if (ndef_parse_record(data, len, &record) < 0) {
84 wpa_printf(MSG_ERROR, "NDEF : Failed to parse");
87 if (filter == NULL || filter(&record))
88 return wpabuf_alloc_copy(record.payload,
89 record.payload_length);
90 data += record.total_length;
91 len -= record.total_length;
93 wpa_printf(MSG_ERROR, "NDEF : Record not found");
98 static struct wpabuf * ndef_build_record(u8 flags, void *type,
99 u8 type_length, void *id,
100 u8 id_length, void *payload,
103 struct wpabuf *record;
108 short_record = payload_length < 256 ? 1 : 0;
110 total_len = 2; /* flag + type length */
112 total_len += short_record ? sizeof(u8) : sizeof(u32);
115 total_len += type_length + id_length + payload_length;
116 record = wpabuf_alloc(total_len);
117 if (record == NULL) {
118 wpa_printf(MSG_ERROR, "NDEF : Failed to allocate "
125 local_flag |= FLAG_ID_LENGTH_PRESENT;
127 local_flag |= FLAG_SHORT_RECORD;
128 wpabuf_put_u8(record, local_flag);
130 wpabuf_put_u8(record, type_length);
133 wpabuf_put_u8(record, payload_length);
135 wpabuf_put_be32(record, payload_length);
138 wpabuf_put_u8(record, id_length);
139 wpabuf_put_data(record, type, type_length);
140 wpabuf_put_data(record, id, id_length);
141 wpabuf_put_data(record, payload, payload_length);
146 static int wifi_filter(struct ndef_record *record)
148 if (record->type_length != os_strlen(wifi_handover_type))
150 if (os_memcmp(record->type, wifi_handover_type,
151 os_strlen(wifi_handover_type)) != 0)
157 struct wpabuf * ndef_parse_wifi(struct wpabuf *buf)
159 return ndef_parse_records(buf, wifi_filter);
163 struct wpabuf * ndef_build_wifi(struct wpabuf *buf)
165 return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
166 FLAG_TNF_RFC2046, wifi_handover_type,
167 os_strlen(wifi_handover_type), NULL, 0,
168 wpabuf_mhead(buf), wpabuf_len(buf));