-
Notifications
You must be signed in to change notification settings - Fork 8
/
NdefRecord.cpp
434 lines (393 loc) · 12.9 KB
/
NdefRecord.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* Copyright 2014 Ten Wong, wangtengoo7@gmail.com
* https://github.com/awong1900/RF430CL330H_Shield
*/
#include "NdefRecord.h"
/* Uncomment these lines to enable debug output */
//define NDEFRECORD_DEBUG
#define STORED_IN_RAM FLASE
static const byte FLAG_MB = (byte) 0x80;
static const byte FLAG_ME = (byte) 0x40;
static const byte FLAG_CF = (byte) 0x20;
static const byte FLAG_SR = (byte) 0x10;
static const byte FLAG_IL = (byte) 0x08;
static const int MAX_PAYLOAD_SIZE = 10 * (1 << 20); // 10 MB payload limit
/**
* NFC Forum "URI Record Type Definition"<p>
* This is a mapping of "URI Identifier Codes" to URI string prefixes,
* per section 3.2.2 of the NFC Forum URI Record Type Definition document.
*/
#if STORED_IN_RAM //stored in ram, need 387 bytes, not recommend
static const String URI_PREFIX_MAP[] =
{
"", // 0x00
"http://www.", // 0x01
"https://www.", // 0x02
"http://", // 0x03
"https://", // 0x04
"tel:", // 0x05
"mailto:", // 0x06
"ftp://anonymous:anonymous@", // 0x07
"ftp://ftp.", // 0x08
"ftps://", // 0x09
"sftp://", // 0x0A
"smb://", // 0x0B
"nfs://", // 0x0C
"ftp://", // 0x0D
"dav://", // 0x0E
"news:", // 0x0F
"telnet://", // 0x10
"imap:", // 0x11
"rtsp://", // 0x12
"urn:", // 0x13
"pop:", // 0x14
"sip:", // 0x15
"sips:", // 0x16
"tftp:", // 0x17
"btspp://", // 0x18
"btl2cap://", // 0x19
"btgoep://", // 0x1A
"tcpobex://", // 0x1B
"irdaobex://", // 0x1C
"file://", // 0x1D
"urn:epc:id:", // 0x1E
"urn:epc:tag:", // 0x1F
"urn:epc:pat:", // 0x20
"urn:epc:raw:", // 0x21
"urn:epc:", // 0x22
};
#else // stored in PROGram MEMory, the flash ROM memory that your code lives in
#include <avr/pgmspace.h>
const char string_0[] PROGMEM = ""; // 0x00
const char string_1[] PROGMEM = "http://www."; // 0x01
const char string_2[] PROGMEM = "https://www."; // 0x02
const char string_3[] PROGMEM = "http://"; // 0x03
const char string_4[] PROGMEM = "https://"; // 0x04
const char string_5[] PROGMEM = "tel:"; // 0x05
const char string_6[] PROGMEM = "mailto:"; // 0x06
const char string_7[] PROGMEM = "ftp://anonymous:anonymous@"; // 0x07
const char string_8[] PROGMEM = "ftp://ftp."; // 0x08
const char string_9[] PROGMEM = "ftps://"; // 0x09
const char string_10[] PROGMEM = "sftp://"; // 0x0A
const char string_11[] PROGMEM = "smb://"; // 0x0B
const char string_12[] PROGMEM = "nfs://"; // 0x0C
const char string_13[] PROGMEM = "ftp://"; // 0x0D
const char string_14[] PROGMEM = "dav://"; // 0x0E
const char string_15[] PROGMEM = "news:"; // 0x0F
const char string_16[] PROGMEM = "telnet://"; // 0x10
const char string_17[] PROGMEM = "imap:"; // 0x11
const char string_18[] PROGMEM = "rtsp://"; // 0x12
const char string_19[] PROGMEM = "urn:"; // 0x13
const char string_20[] PROGMEM = "pop:"; // 0x14
const char string_21[] PROGMEM = "sip:"; // 0x15
const char string_22[] PROGMEM = "sips:"; // 0x16
const char string_23[] PROGMEM = "tftp:"; // 0x17
const char string_24[] PROGMEM = "btspp://"; // 0x18
const char string_25[] PROGMEM = "btl2cap://"; // 0x19
const char string_26[] PROGMEM = "btgoep://"; // 0x1A
const char string_27[] PROGMEM = "tcpobex://"; // 0x1B
const char string_28[] PROGMEM = "irdaobex://"; // 0x1C
const char string_29[] PROGMEM = "file://"; // 0x1D
const char string_30[] PROGMEM = "urn:epc:id:"; // 0x1E
const char string_31[] PROGMEM = "urn:epc:tag:"; // 0x1F
const char string_32[] PROGMEM = "urn:epc:pat:"; // 0x20
const char string_33[] PROGMEM = "urn:epc:raw:"; // 0x21
const char string_34[] PROGMEM = "urn:epc:"; // 0x22
const char * const URI_PREFIX_MAP[] PROGMEM = {
string_0 ,
string_1 ,
string_2 ,
string_3 ,
string_4 ,
string_5 ,
string_6 ,
string_7 ,
string_8 ,
string_9 ,
string_10,
string_11,
string_12,
string_13,
string_14,
string_15,
string_16,
string_17,
string_18,
string_19,
string_20,
string_21,
string_22,
string_23,
string_24,
string_25,
string_26,
string_27,
string_28,
string_29,
string_30,
string_31,
string_32,
string_33,
string_34,
};
#endif
/**
* Indicates the record is empty.
* Type, id and payload fields are empty in a TNF_EMPTY} record.
*/
NdefRecord::NdefRecord() {
mTnf = TNF_EMPTY;
mType = EMPTY_BYTE_ARRAY;
mType_length = 0;
mId = EMPTY_BYTE_ARRAY;
mId_length = 0;
mPayload = EMPTY_BYTE_ARRAY;
mPayload_length = 0;
}
/**
* @brief Construct an NDEF Record from its component fields
* @param tnf a 3-bit TNF constant
* @param type byte array, containing zero to 255 bytes
* @param type_length length of type
* @param id byte array, containing zero to 255 bytes
* @param id_length length of id
* @param payload byte array, containing zero to (2 ** 32 - 1) bytes
* @param payload_length length of payload
* @return
*/
void NdefRecord::createNdefRecord(short tnf, byte type[], uint16_t type_length, byte id[],
uint16_t id_length, byte payload[], uint16_t payload_length)
{
mTnf = tnf;
mType = (byte*)malloc(type_length); //import: alloc memory size, free mem on Class NdefMessage
memcpy(mType, type, type_length); //cope memory
mType_length = type_length;
mId = (byte*)malloc(id_length);
memcpy(mId, payload, id_length);
mId_length = id_length;
mPayload = (byte*)malloc(payload_length);
memcpy(mPayload, payload, payload_length);
mPayload_length = payload_length;
#ifdef NDEFRECORD_DEBUG
Serial.println("--------Record info--------");
Serial.print("mTnf = ");Serial.println(mTnf, HEX);
Serial.print("mId = ");
for (int i=0; i < mId_length; i++){Serial.print(mId[i], HEX);Serial.print(" ");}
Serial.println();
Serial.print("mType = ");
for (int i=0; i < mType_length; i++){Serial.print(mType[i], HEX);Serial.print(" ");}
Serial.println();
Serial.print("mPayload = ");
for (int i=0; i < mPayload_length; i++){Serial.print(mPayload[i], HEX);Serial.print(" ");}
Serial.println();
Serial.println("-------------End-----------");
#endif
}
/**
* Reference specification: NFCForum-TS-RTD_URI_1.0
* @brief an NDEF Record containing the URI
* @param uriString string URI to encode.
* @return
*/
void NdefRecord::createUri(String uriString) {
if (uriString.length() == 0) {
Serial.println("[ERROR]uri is empty");
return;
}
byte prefix = 0;
#if STORED_IN_RAM
for (int i = 1; i < sizeof(URI_PREFIX_MAP)/sizeof(URI_PREFIX_MAP[0]); i++) {
if (uriString.startsWith(URI_PREFIX_MAP[i])) {
prefix = (byte) i;
uriString = uriString.substring(URI_PREFIX_MAP[i].length());
break;
}
}
#else
uint8_t len = 0x23;
String URI_PREFIX;
char buffer[15]; // make sure this is large enough for the largest string it must hold
for (int i = 1; i < len; i++) {
strcpy_P(buffer, (char*)pgm_read_word(&(URI_PREFIX_MAP[i]))); // Necessary casts and dereferencing, just copy.
URI_PREFIX = buffer;
if (uriString.startsWith(URI_PREFIX)) {
prefix = (byte) i;
uriString = uriString.substring(URI_PREFIX.length());
break;
}
}
#endif
byte uriBytes[uriString.length() + 1];
uriString.getBytes(uriBytes, uriString.length() + 1);
byte recordBytes[uriString.length() + 1];
recordBytes[0] = prefix;
arraycopy(uriBytes, 0, recordBytes, 1, uriString.length());
createNdefRecord(TNF_WELL_KNOWN, RTD_URI, 1, EMPTY_BYTE_ARRAY,
0, recordBytes, sizeof(recordBytes));
}
/**
* Reference specification: NFCForum-TS-RTD_1.0
* @brief a new NDEF Record containing external (application-specific) data
* @param domain domain-name of issuing organization
* @param type domain-specific type of data
* @param data payload as bytes
* @param data_length length of payload
* @return
*/
void NdefRecord::createExternal(String domain, String type, byte data[], uint16_t data_length) {
if (domain.length() == 0) {
Serial.println("[ERROR]domain is empty");
return;
}
if (type.length() == 0) {
Serial.println("[ERROR]type is empty");
return;
}
domain.toLowerCase();
type.toLowerCase();
byte byteDomain[domain.length()+1];
domain.getBytes(byteDomain, domain.length()+1);
byte byteType[type.length()+1];
type.getBytes(byteType, type.length()+1);
byte b[domain.length() + 1 + type.length()];
arraycopy(byteDomain, 0, b, 0, domain.length());
b[domain.length()] = ':';
arraycopy(byteType, 0, b, domain.length() + 1, type.length());
createNdefRecord(TNF_EXTERNAL_TYPE, b, sizeof(b), EMPTY_BYTE_ARRAY, 0,
data, data_length);
}
/**
* @brief an NDEF Record containing the MIME-typed data
* @param mimeType a valid MIME type
* @param mimeData MIME data as bytes
* @param mimeData_length length of MIME data
* @return
*
*/
void NdefRecord::createMime(String mimeType, byte mimeData[], uint16_t mimeData_length) {
if (mimeType.length() == 0) {
Serial.println("[ERROR]mimeType is empty");
return;
}
// We only do basic MIME type validation: trying to follow the
// RFCs strictly only ends in tears, since there are lots of MIME
// types in common use that are not strictly valid as per RFC rules
mimeType.toLowerCase();
int semicolonIndex = mimeType.indexOf(';');
if (semicolonIndex != -1) {
mimeType = mimeType.substring(0, semicolonIndex);
}
if (mimeType.length() == 0) {
Serial.println("[ERROR]mimeType is empty");
return;
}
int slashIndex = mimeType.indexOf('/');
if (slashIndex == 0) {
Serial.println("[ERROR]mimeType must have major type");
return;
}
if (slashIndex == mimeType.length()) {
Serial.println("[ERROR]mimeType must have minor type");
return;
}
// missing '/' is allowed
// MIME RFCs suggest ASCII encoding for content-type
byte typeBytes[mimeType.length()+1];
mimeType.getBytes(typeBytes, mimeType.length()+1);
createNdefRecord(TNF_MIME_MEDIA, typeBytes, mimeType.length(), EMPTY_BYTE_ARRAY, 0,
mimeData, mimeData_length);
}
/**
* @breif a new Android Application Record (AAR)
* @param packageName Android package name
* @return
*/
void NdefRecord::createApplicationRecord(String packageName) {
if (packageName.length() == 0) {
Serial.println("[ERROR]packageName is empty");
return;
}
byte typeBytes[packageName.length()+1];
packageName.getBytes(typeBytes, packageName.length()+1);
createNdefRecord(TNF_EXTERNAL_TYPE, RTD_ANDROID_APP, 15, EMPTY_BYTE_ARRAY, 0,
typeBytes, packageName.length());
}
/**
* @breif an NDEF Record containing the Text-typed data
* @param text_encode text encoded data as bytes
* @param language see NdefRecord.h "unicode locale"
* @param payload_length length of payload_encode
* @param encodeInUtf8 ture:utf-8 encode, false:utf-16 encode
* @return
*/
void NdefRecord::createText(byte text_encode[],uint16_t payload_length,
byte* language, boolean encodeInUtf8)
{
int utfBit = encodeInUtf8 ? 0 : (1 << 7);
char status = (char) (utfBit + 2);
byte data[1 + 2 + payload_length];
data[0] = (byte) status;
arraycopy(language, 0, data, 1, 2);
arraycopy(text_encode, 0, data, (1+2), payload_length);
createNdefRecord(TNF_WELL_KNOWN, RTD_TEXT, 1, EMPTY_BYTE_ARRAY, 0,
data, 1 + 2 + payload_length);
}
/**
* Serialize record for network transmission.<p>
* Uses specified MB and ME flags.<p>
* Does not chunk records.
*/
void NdefRecord::writeToByteBuffer(byte buffer[], uint16_t index, boolean mb, boolean me) {
boolean sr = mPayload_length < 256;
boolean il = mId_length > 0;
byte flags = (byte)((mb ? FLAG_MB : 0) | (me ? FLAG_ME : 0) |
(sr ? FLAG_SR : 0) | (il ? FLAG_IL : 0) | mTnf);
buffer[index++] = flags;
buffer[index++] = (byte)mType_length;
if (sr) {
buffer[index++] = (byte)mPayload_length;
} else {
buffer[index++] = (byte)mPayload_length;
}
if (il) {
buffer[index++] = (byte)mId_length;
}
arraycopy(mType, 0, buffer, index, mType_length);
index += mType_length;
arraycopy(mId, 0, buffer, index, mId_length);
index += mId_length;
arraycopy(mPayload, 0, buffer, index, mPayload_length);
index += mPayload_length;
}
/**
* Get byte length of serialized record.
*/
uint16_t NdefRecord::getByteLength() {
int length = 3 + mType_length + mId_length + mPayload_length;
boolean sr = mPayload_length < 256;
boolean il = mId_length > 0;
if (!sr) length += 3;
if (il) length += 1;
return length;
}
/**
* free memory of class NdefRecord
*/
void NdefRecord::freeRecord() {
free(mType);
free(mId);
free(mPayload);
}
/**
* @breif Copies elements from the array src, starting at offset srcPos,
* into the array dst, starting at offset dstPos
* @param src the source array to copy the content.
* @param srcPos the starting index of the content in src
* @param dst the destination array to copy the data into
* @param dstPos the starting index for the copied content in dst
* @param length the number of elements to be copied
* @return
*/
void arraycopy(byte src[], int srcPos, byte dst[], int dstPos, int length) {
for (int i=0; i<length; i++)
dst[dstPos+i] = src[srcPos+i];
}