forked from nurun/arduino_NFC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NDEF.cpp
372 lines (320 loc) · 10.8 KB
/
NDEF.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**************************************************************************/
/*!
@file PN532_I2C.h
@author Odopod, a Nurun Company
@license BSD
*/
/**************************************************************************/
#include "NDEF.h"
NDEF::NDEF(){
}
/**
* Parse the actual NDEF message and call specific handlers for dealing with
* a particular type of NDEF message.
*
* @param msg The NDEF message
* @return struct FOUND_MESSAGE which contains type, format, and the actual payload
*/
FOUND_MESSAGE NDEF::decode_message(uint8_t * msg) {
int offset = 2;
FOUND_MESSAGE m;
bool mb = (*(msg + offset) & 0x80) == 0x80; /* Message Begin */
bool me = (*(msg + offset) & 0x40) == 0x40; /* Message End */
bool cf = (*(msg + offset) & 0x20) == 0x20; /* Chunk Flag */
bool sr = (*(msg + offset) & 0x10) == 0x10; /* Short Record */
bool il = (*(msg + offset) & 0x08) == 0x08; /* ID Length Present */
uint8_t tnf = (*(msg + offset) & 0x07);
offset++;
#ifdef NDEFDEBUG
Serial.print("MB:"); Serial.println(mb, DEC);
Serial.print("ME:"); Serial.println(me, DEC);
Serial.print("CF:"); Serial.println(cf, DEC);
Serial.print("SR:"); Serial.println(sr, DEC);
Serial.print("IL:"); Serial.println(il, DEC);
Serial.print("TNF:"); Serial.println(tnf, HEX);
#endif
if (cf) {
Serial.println("chunk flag not supported yet");
m.type = 0;
return m;
}
int typeLength = *(msg + offset);
offset++;
int payloadLength;
if (sr) {
payloadLength = *(msg + offset);
payloadLength = (payloadLength < 0) ? payloadLength + 256 : payloadLength;
offset++;
} else {
offset += 4;
}
int idLength = 0;
if (il) {
idLength = *(msg + offset);
offset++;
}
switch ((int)tnf) {
case 1:
//well known record type
m.type = *(msg + offset);
offset += typeLength;
if (il) {
offset += idLength;
}
memcpy(msg, msg + offset, payloadLength);
offset += payloadLength;
char lang [3];
char text [NDEF_BUFFER_SIZE];
char uri [NDEF_BUFFER_SIZE];
switch (m.type) {
case NDEF_TYPE_URI:
if(parse_uri(msg, payloadLength, uri)){
// Serial.print("uri: "); Serial.println(uri);
m.format = (char *)(uint8_t)msg[0];
m.payload = (uint8_t*)uri;
}
break;
case NDEF_TYPE_TEXT:
if(parse_text(msg, payloadLength, lang, text)) {
// Serial.print("lang: "); Serial.println(lang);
// Serial.print("text: "); Serial.println(text);
m.format = (char *)(uint8_t*)lang;
m.payload = (uint8_t*)text;
}
break;
default:
Serial.println("err, NDEF type: "); Serial.println((char)m.type);
break;
}
break;
case 2:
//mime type record
m.type = NDEF_TYPE_MIME;
char mimetype [typeLength-2];
uint8_t payload [NDEF_BUFFER_SIZE];
memcpy(mimetype, msg+offset, typeLength);
memcpy(payload, msg +typeLength +offset, payloadLength - typeLength);
// Serial.print("mimetype: "); Serial.println(mimetype);
// Serial.print("data: "); Serial.println((char*)payload);
m.format = mimetype;
m.payload = payload;
break;
default:
Serial.println("err");
break;
}
return m;
}
/**
* encodes the URI message attaches the proper formatted header and terminating character
*
* @param uriPrefix URI prefix char
* @param msg the payload
* @return length of the encoded message
*/
uint8_t NDEF::encode_URI(uint8_t uriPrefix, uint8_t * msg){
uint8_t len = strlen((char *)msg);
uint8_t record_header = encode_record_header(1, 1, 0, 1, 0, NDEF_WELL_KNOWN_RECORD);
uint8_t payload_head[7] = {0x03, len+5, record_header, 0x01, len+1, 0x55, uriPrefix};
const uint8_t term[1] ={0xFE};
memmove(msg+7, msg, len);
memcpy(msg+0, payload_head, 7);
memcpy(msg + len + 7, term , 1);
#ifdef DEBUG
for (uint8_t i = 0 ; i < len + 8; i++) {
Serial.print(msg[i], HEX);Serial.print(" ");
}
Serial.println("");
#endif
return len + 8;
}
/**
* encodes the TEXT message attaches the proper formatted header and terminating character
*
* @param lang 2 letter language code ie 'en, de, es'
* @param msg the payload
* @return length of the encoded message
*/
uint8_t NDEF::encode_TEXT(uint8_t * lang, uint8_t * msg){
uint8_t len = strlen((char *)msg);
uint8_t record_header = encode_record_header(1, 1, 0, 1, 0, NDEF_WELL_KNOWN_RECORD);
uint8_t payload_head[9] = {0x03, len+7, record_header, 0x01, len+3, 0x54, 0x02, lang[0], lang[1]};
const uint8_t term[1] ={0xFE};
memmove(msg + 9, msg, len);
memcpy(msg, payload_head, 9);
memcpy(msg + 9 + len, term , 1);
#ifdef DEBUG
for (uint8_t i = 0 ; i < len + 10; i++) {
Serial.print(msg[i], HEX);Serial.print(" ");
}
Serial.println("");
#endif
return len + 10;
}
/**
* encodes the MIME message attaches the proper formatted header and terminating character
*
* @param mimetype char array of the mimetype ie "image/gif"
* @param data the payload
* @param length length of the payload
* @return length of the encoded message
*/
uint8_t NDEF::encode_MIME(uint8_t * mimetype, uint8_t * data, uint8_t len){
uint8_t typeLen = strlen((char *) mimetype);
uint8_t record_header = encode_record_header(1, 1, 0, 1, 0, NDEF_MIME_TYPE_RECORD);
uint8_t payload_head[5] = {0x03, len + typeLen + 3, record_header, typeLen, len};
const uint8_t term[1] ={0xFE};
memmove(data + 5 + typeLen, data, len);
memcpy(data, payload_head, 5);
memcpy(data + 5, mimetype, typeLen);
memcpy(data + 5 + typeLen + len, term , 1);
return typeLen + len + 6;
}
/**
* helper function to create the binary encoded header type byte for the ndef header
*
* @param mb Message Begin
* @param me Message End
* @param cf Chunk Flag
* @param sr Short Record
* @param il ID Length Present
* @param tnf Type Name Field
* @return binary encoded header type byte (ie D1, D2, etc)
*/
uint8_t NDEF::encode_record_header(bool mb, bool me, bool cf, bool sr, bool il, uint8_t tnf){
uint8_t record_header = 0;
if(mb)
record_header += 0x80;
if(me)
record_header += 0x40;
if(cf)
record_header += 0x20;
if(sr)
record_header += 0x10;
if(il)
record_header += 0x08;
record_header += tnf;
return record_header;
}
/**
* Convert type of URI to the actual URI prefix to be used in conjunction
* with the URI stored in the NDEF record itself. This is specified in section
* 3.2.2 "URI Identifier Code" of "URI Record Type Definition Technical
* Specification".
*
* !!! comment out ones you don't want to support to save memory size, they add up to alot!
*
* @param b the code of the URI to convert to the actual prefix
* @return the URI prefix
*/
char * NDEF::get_uri_prefix(uint8_t b)
{
switch (b) {
case 0x00:
return "";
case 0x01:
return "http://www.";
case 0x02:
return "https://www.";
case 0x03:
return "http://";
case 0x04:
return "https://";
case 0x05:
return "tel:";
case 0x06:
return "mailto:";
// case 0x07:
// return "ftp://anonymous:anonymous@";
// case 0x08:
// return "ftp://ftp.";
// case 0x09:
// return "ftps://";
// case 0x0A:
// return "sftp://";
case 0x0B:
return "smb://";
// case 0x0C:
// return "nfs://";
// case 0x0D:
// return "ftp://";
// case 0x0E:
// return "dav://";
// case 0x0F:
// return "news:";
// case 0x10:
// return "telnet://";
// case 0x11:
// return "imap:";
// case 0x12:
// return "rtsp://";
// case 0x13:
// return "urn:";
// case 0x14:
// return "pop:";
// case 0x15:
// return "sip:";
// case 0x16:
// return "sips:";
// case 0x17:
// return "tftp:";
// case 0x18:
// return "btspp://";
// case 0x19:
// return "btl2cap://";
// case 0x1A:
// return "btgoep://";
// case 0x1B:
// return "tcpobex://";
// case 0x1C:
// return "irdaobex://";
case 0x1D:
return "file://";
// case 0x1E:
// return "urn:epc:id:";
// case 0x1F:
// return "urn:epc:tag:";
// case 0x20:
// return "urn:epc:pat:";
// case 0x21:
// return "urn:epc:raw:";
// case 0x22:
// return "urn:epc:";
// case 0x23:
// return "urn:nfc:";
default:
return "unknown";
}
}
/**
* Concatenates the prefix with the contents of the NDEF URI record.
*
* @param payload The NDEF URI payload
* @param payload_len The length of the NDEF URI payload
* @return The full reconstructed URI
*/
bool NDEF::parse_uri(uint8_t * payload, int payload_len, char * uri ){
char * prefix = get_uri_prefix(payload[0]);
int prefix_len = strlen(prefix);
memcpy(uri, prefix, prefix_len);
memcpy(uri + prefix_len, payload + 1, payload_len - 1);
*(uri + prefix_len + payload_len - 1) = 0x00;
return true;
}
/**
* Concatenates the lang prefix with the contents of the NDEF TEXT record.
*
* @param payload The NDEF Text Record payload
* @param payload_len The length of the NDEF Text Record payload
* @param lang The IANA language code lenght
* @param text The text contained in NDEF Text Record
* @return Success or not.
*/
bool NDEF::parse_text(uint8_t * payload, int payload_len, char * lang, char * text){
memcpy(lang, payload + 1, 2);
*(lang + 2) = 0x00;
const int text_len = payload_len - 3;
memcpy(text, payload + 3, text_len);
*(text + text_len) = 0x00;
return true;
}