2
2
* NDEF(NFC Data Exchange Format) routines for Wi-Fi Protected Setup
3
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.
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.
16
10
#include "includes.h"
17
11
#include "common.h"
18
12
#include "wps/wps.h"
19
#include "wps/wps_i.h"
21
14
#define FLAG_MESSAGE_BEGIN (1 << 7)
22
15
#define FLAG_MESSAGE_END (1 << 6)
23
16
#define FLAG_CHUNK (1 << 5)
24
17
#define FLAG_SHORT_RECORD (1 << 4)
25
18
#define FLAG_ID_LENGTH_PRESENT (1 << 3)
19
#define FLAG_TNF_NFC_FORUM (0x01)
26
20
#define FLAG_TNF_RFC2046 (0x02)
28
22
struct ndef_record {
34
28
u32 payload_length;
38
32
static char wifi_handover_type[] = "application/vnd.wfa.wsc";
33
static char p2p_handover_type[] = "application/vnd.wfa.p2p";
40
static int ndef_parse_record(u8 *data, u32 size, struct ndef_record *record)
35
static int ndef_parse_record(const u8 *data, u32 size,
36
struct ndef_record *record)
38
const u8 *pos = data + 1;
81
static struct wpabuf * ndef_parse_records(struct wpabuf *buf,
77
static struct wpabuf * ndef_parse_records(const struct wpabuf *buf,
82
78
int (*filter)(struct ndef_record *))
84
80
struct ndef_record record;
85
81
int len = wpabuf_len(buf);
86
u8 *data = wpabuf_mhead(buf);
82
const u8 *data = wpabuf_head(buf);
89
85
if (ndef_parse_record(data, len, &record) < 0) {
104
100
static struct wpabuf * ndef_build_record(u8 flags, void *type,
105
101
u8 type_length, void *id,
106
u8 id_length, void *payload,
103
const struct wpabuf *payload)
109
105
struct wpabuf *record;
110
106
size_t total_len;
111
107
int short_record;
109
size_t payload_length = wpabuf_len(payload);
114
111
short_record = payload_length < 256 ? 1 : 0;
144
141
wpabuf_put_u8(record, id_length);
145
142
wpabuf_put_data(record, type, type_length);
146
143
wpabuf_put_data(record, id, id_length);
147
wpabuf_put_data(record, payload, payload_length);
144
wpabuf_put_buf(record, payload);
163
struct wpabuf * ndef_parse_wifi(struct wpabuf *buf)
160
struct wpabuf * ndef_parse_wifi(const struct wpabuf *buf)
165
162
return ndef_parse_records(buf, wifi_filter);
169
struct wpabuf * ndef_build_wifi(struct wpabuf *buf)
166
struct wpabuf * ndef_build_wifi(const struct wpabuf *buf)
171
168
return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
172
169
FLAG_TNF_RFC2046, wifi_handover_type,
173
os_strlen(wifi_handover_type), NULL, 0,
174
wpabuf_mhead(buf), wpabuf_len(buf));
170
os_strlen(wifi_handover_type), NULL, 0, buf);
174
static int p2p_filter(struct ndef_record *record)
176
if (record->type_length != os_strlen(p2p_handover_type))
178
if (os_memcmp(record->type, p2p_handover_type,
179
os_strlen(p2p_handover_type)) != 0)
185
struct wpabuf * ndef_parse_p2p(const struct wpabuf *buf)
187
return ndef_parse_records(buf, p2p_filter);
191
struct wpabuf * ndef_build_p2p(const struct wpabuf *buf)
193
return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
194
FLAG_TNF_RFC2046, p2p_handover_type,
195
os_strlen(p2p_handover_type), NULL, 0, buf);