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 char wifi_handover_type[] = "application/vnd.wfa.wsc";
34 static int ndef_parse_record(const u8 *data, u32 size,
35 struct ndef_record *record)
37 const u8 *pos = data + 1;
41 record->type_length = *pos++;
42 if (data[0] & FLAG_SHORT_RECORD) {
45 record->payload_length = *pos++;
49 record->payload_length = ntohl(*(u32 *)pos);
53 if (data[0] & FLAG_ID_LENGTH_PRESENT) {
54 if ((int) size < pos - data + 1)
56 record->id_length = *pos++;
58 record->id_length = 0;
60 record->type = record->type_length == 0 ? NULL : pos;
61 pos += record->type_length;
63 record->id = record->id_length == 0 ? NULL : pos;
64 pos += record->id_length;
66 record->payload = record->payload_length == 0 ? NULL : pos;
67 pos += record->payload_length;
69 record->total_length = pos - data;
70 if (record->total_length > size)
76 static struct wpabuf * ndef_parse_records(const struct wpabuf *buf,
77 int (*filter)(struct ndef_record *))
79 struct ndef_record record;
80 int len = wpabuf_len(buf);
81 const u8 *data = wpabuf_head(buf);
84 if (ndef_parse_record(data, len, &record) < 0) {
85 wpa_printf(MSG_ERROR, "NDEF : Failed to parse");
88 if (filter == NULL || filter(&record))
89 return wpabuf_alloc_copy(record.payload,
90 record.payload_length);
91 data += record.total_length;
92 len -= record.total_length;
94 wpa_printf(MSG_ERROR, "NDEF : Record not found");
99 static struct wpabuf * ndef_build_record(u8 flags, void *type,
100 u8 type_length, void *id,
102 const struct wpabuf *payload)
104 struct wpabuf *record;
108 size_t payload_length = wpabuf_len(payload);
110 short_record = payload_length < 256 ? 1 : 0;
112 total_len = 2; /* flag + type length */
114 total_len += short_record ? sizeof(u8) : sizeof(u32);
117 total_len += type_length + id_length + payload_length;
118 record = wpabuf_alloc(total_len);
119 if (record == NULL) {
120 wpa_printf(MSG_ERROR, "NDEF : Failed to allocate "
127 local_flag |= FLAG_ID_LENGTH_PRESENT;
129 local_flag |= FLAG_SHORT_RECORD;
130 wpabuf_put_u8(record, local_flag);
132 wpabuf_put_u8(record, type_length);
135 wpabuf_put_u8(record, payload_length);
137 wpabuf_put_be32(record, payload_length);
140 wpabuf_put_u8(record, id_length);
141 wpabuf_put_data(record, type, type_length);
142 wpabuf_put_data(record, id, id_length);
143 wpabuf_put_buf(record, payload);
148 static int wifi_filter(struct ndef_record *record)
150 if (record->type_length != os_strlen(wifi_handover_type))
152 if (os_memcmp(record->type, wifi_handover_type,
153 os_strlen(wifi_handover_type)) != 0)
159 struct wpabuf * ndef_parse_wifi(const struct wpabuf *buf)
161 return ndef_parse_records(buf, wifi_filter);
165 struct wpabuf * ndef_build_wifi(const struct wpabuf *buf)
167 return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
168 FLAG_TNF_RFC2046, wifi_handover_type,
169 os_strlen(wifi_handover_type), NULL, 0, buf);
173 struct wpabuf * ndef_build_wifi_hr(void)
175 struct wpabuf *rn, *cr, *ac_payload, *ac, *hr_payload, *hr;
176 struct wpabuf *carrier, *hc;
178 rn = wpabuf_alloc(2);
181 wpabuf_put_be16(rn, os_random() & 0xffff);
183 cr = ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_TNF_NFC_FORUM, "cr", 2,
190 ac_payload = wpabuf_alloc(4);
191 if (ac_payload == NULL) {
195 wpabuf_put_u8(ac_payload, 0x01); /* Carrier Flags: CRS=1 "active" */
196 wpabuf_put_u8(ac_payload, 0x01); /* Carrier Data Reference Length */
197 wpabuf_put_u8(ac_payload, '0'); /* Carrier Data Reference: "0" */
198 wpabuf_put_u8(ac_payload, 0); /* Aux Data Reference Count */
200 ac = ndef_build_record(FLAG_MESSAGE_END | FLAG_TNF_NFC_FORUM, "ac", 2,
201 NULL, 0, ac_payload);
202 wpabuf_free(ac_payload);
208 hr_payload = wpabuf_alloc(1 + wpabuf_len(cr) + wpabuf_len(ac));
209 if (hr_payload == NULL) {
215 wpabuf_put_u8(hr_payload, 0x12); /* Connection Handover Version 1.2 */
216 wpabuf_put_buf(hr_payload, cr);
217 wpabuf_put_buf(hr_payload, ac);
221 hr = ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_TNF_NFC_FORUM, "Hr", 2,
222 NULL, 0, hr_payload);
223 wpabuf_free(hr_payload);
227 carrier = wpabuf_alloc(2 + os_strlen(wifi_handover_type));
228 if (carrier == NULL) {
232 wpabuf_put_u8(carrier, 0x02); /* Carrier Type Format */
233 wpabuf_put_u8(carrier, os_strlen(wifi_handover_type));
234 wpabuf_put_str(carrier, wifi_handover_type);
236 hc = ndef_build_record(FLAG_MESSAGE_END | FLAG_TNF_NFC_FORUM, "Hc", 2,
238 wpabuf_free(carrier);
244 return wpabuf_concat(hr, hc);