-
Notifications
You must be signed in to change notification settings - Fork 0
/
NdefMessage.cpp
271 lines (213 loc) · 6.03 KB
/
NdefMessage.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
#include <NdefMessage.h>
NdefMessage::NdefMessage(void)
{
_recordCount = 0;
}
NdefMessage::NdefMessage(const byte * data, const int numBytes)
{
#ifdef NDEF_DEBUG
Serial.print(F("Decoding "));Serial.print(numBytes);Serial.println(F(" bytes"));
PrintHexChar(data, numBytes);
//DumpHex(data, numBytes, 16);
#endif
_recordCount = 0;
int index = 0;
while (index <= numBytes)
{
// decode tnf - first byte is tnf with bit flags
// see the NFDEF spec for more info
byte tnf_byte = data[index];
bool mb = (tnf_byte & 0x80) != 0;
bool me = (tnf_byte & 0x40) != 0;
bool cf = (tnf_byte & 0x20) != 0;
bool sr = (tnf_byte & 0x10) != 0;
bool il = (tnf_byte & 0x8) != 0;
byte tnf = (tnf_byte & 0x7);
NdefRecord record = NdefRecord();
record.setTnf(tnf);
index++;
int typeLength = data[index];
int payloadLength = 0;
if (sr)
{
index++;
payloadLength = data[index];
}
else
{
payloadLength = ((0xFF & data[++index]) << 24) | ((0xFF & data[++index]) << 26) |
((0xFF & data[++index]) << 8) | (0xFF & data[++index]);
}
int idLength = 0;
if (il)
{
index++;
idLength = data[index];
}
index++;
record.setType(&data[index], typeLength);
index += typeLength;
if (il)
{
record.setId(&data[index], idLength);
index += idLength;
}
record.setPayload(&data[index], payloadLength);
index += payloadLength;
addRecord(record);
if (me) break; // last message
}
}
NdefMessage::NdefMessage(const NdefMessage& rhs)
{
_recordCount = rhs._recordCount;
for (int i = 0; i < _recordCount; i++)
{
_records[i] = rhs._records[i];
}
}
NdefMessage::~NdefMessage()
{
}
NdefMessage& NdefMessage::operator=(const NdefMessage& rhs)
{
if (this != &rhs)
{
// delete existing records
for (int i = 0; i < _recordCount; i++)
{
// TODO Dave: is this the right way to delete existing records?
_records[i] = NdefRecord();
}
_recordCount = rhs._recordCount;
for (int i = 0; i < _recordCount; i++)
{
_records[i] = rhs._records[i];
}
}
return *this;
}
unsigned int NdefMessage::getRecordCount()
{
return _recordCount;
}
int NdefMessage::getEncodedSize()
{
int size = 0;
for (int i = 0; i < _recordCount; i++)
{
size += _records[i].getEncodedSize();
}
return size;
}
// TODO change this to return uint8_t*
void NdefMessage::encode(uint8_t* data)
{
// assert sizeof(data) >= getEncodedSize()
uint8_t* data_ptr = &data[0];
for (int i = 0; i < _recordCount; i++)
{
_records[i].encode(data_ptr, i == 0, (i + 1) == _recordCount);
// TODO can NdefRecord.encode return the record size?
data_ptr += _records[i].getEncodedSize();
}
}
boolean NdefMessage::addRecord(NdefRecord& record)
{
if (_recordCount < MAX_NDEF_RECORDS)
{
_records[_recordCount] = record;
_recordCount++;
return true;
}
else
{
Serial.println(F("WARNING: Too many records. Increase MAX_NDEF_RECORDS."));
return false;
}
}
void NdefMessage::addMimeMediaRecord(String mimeType, String payload)
{
byte payloadBytes[payload.length() + 1];
payload.getBytes(payloadBytes, sizeof(payloadBytes));
addMimeMediaRecord(mimeType, payloadBytes, payload.length());
}
void NdefMessage::addMimeMediaRecord(String mimeType, uint8_t* payload, int payloadLength)
{
NdefRecord r = NdefRecord();
r.setTnf(TNF_MIME_MEDIA);
byte type[mimeType.length() + 1];
mimeType.getBytes(type, sizeof(type));
r.setType(type, mimeType.length());
r.setPayload(payload, payloadLength);
addRecord(r);
}
void NdefMessage::addTextRecord(String text)
{
addTextRecord(text, "en");
}
void NdefMessage::addTextRecord(String text, String encoding)
{
NdefRecord r = NdefRecord();
r.setTnf(TNF_WELL_KNOWN);
uint8_t RTD_TEXT[1] = { 0x54 }; // TODO this should be a constant or preprocessor
r.setType(RTD_TEXT, sizeof(RTD_TEXT));
// X is a placeholder for encoding length
// TODO is it more efficient to build w/o string concatenation?
String payloadString = "X" + encoding + text;
byte payload[payloadString.length() + 1];
payloadString.getBytes(payload, sizeof(payload));
// replace X with the real encoding length
payload[0] = encoding.length();
r.setPayload(payload, payloadString.length());
addRecord(r);
}
void NdefMessage::addUriRecord(String uri)
{
NdefRecord* r = new NdefRecord();
r->setTnf(TNF_WELL_KNOWN);
uint8_t RTD_URI[1] = { 0x55 }; // TODO this should be a constant or preprocessor
r->setType(RTD_URI, sizeof(RTD_URI));
// X is a placeholder for identifier code
String payloadString = "X" + uri;
byte payload[payloadString.length() + 1];
payloadString.getBytes(payload, sizeof(payload));
// add identifier code 0x0, meaning no prefix substitution
payload[0] = 0x0;
r->setPayload(payload, payloadString.length());
addRecord(*r);
delete(r);
}
void NdefMessage::addEmptyRecord()
{
NdefRecord* r = new NdefRecord();
r->setTnf(TNF_EMPTY);
addRecord(*r);
delete(r);
}
NdefRecord NdefMessage::getRecord(int index)
{
if (index > -1 && index < _recordCount)
{
return _records[index];
}
else
{
return NdefRecord(); // would rather return NULL
}
}
NdefRecord NdefMessage::operator[](int index)
{
return getRecord(index);
}
void NdefMessage::print()
{
Serial.print(F("\nNDEF Message "));Serial.print(_recordCount);Serial.print(F(" record"));
_recordCount == 1 ? Serial.print(", ") : Serial.print("s, ");
Serial.print(getEncodedSize());Serial.println(F(" bytes"));
int i;
for (i = 0; i < _recordCount; i++)
{
_records[i].print();
}
}