forked from don/NDEF
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNdefMessage.cpp
More file actions
298 lines (237 loc) · 6.68 KB
/
NdefMessage.cpp
File metadata and controls
298 lines (237 loc) · 6.68 KB
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
#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;
bool me = tnf_byte & 0x40;
// bool cf = tnf_byte & 0x20;
bool sr = tnf_byte & 0x10;
bool il = tnf_byte & 0x8;
byte tnf = (tnf_byte & 0x7);
NdefRecord record = NdefRecord();
record.setTnf(tnf);
index++;
int typeLength = data[index];
uint32_t payloadLength = 0;
if (sr)
{
index++;
payloadLength = data[index];
}
else
{
payloadLength =
(static_cast<uint32_t>(data[index]) << 24)
| (static_cast<uint32_t>(data[index+1]) << 16)
| (static_cast<uint32_t>(data[index+2]) << 8)
| static_cast<uint32_t>(data[index+3]);
index += 4;
}
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 (unsigned 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 (unsigned int i = 0; i < _recordCount; i++)
{
// TODO Dave: is this the right way to delete existing records?
_records[i] = NdefRecord();
}
_recordCount = rhs._recordCount;
for (unsigned 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 (unsigned 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 (unsigned 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
{
#ifdef NDEF_USE_SERIAL
Serial.println(F("WARNING: Too many records. Increase MAX_NDEF_RECORDS."));
#endif
return false;
}
}
void NdefMessage::addMimeMediaRecord(const char *mimeType, const char* payload)
{
addMimeMediaRecord(mimeType, reinterpret_cast<const byte*>(payload), strlen(payload));
}
void NdefMessage::addMimeMediaRecord(const char *mimeType, const byte* payload, int payloadLength)
{
NdefRecord r;
r.setTnf(TNF_MIME_MEDIA);
r.setType(reinterpret_cast<const byte*>(mimeType), strlen(mimeType));
r.setPayload(payload, payloadLength);
addRecord(r);
}
void NdefMessage::addUnknownRecord(const byte *payload, int payloadLength)
{
NdefRecord r;
r.setTnf(TNF_UNKNOWN);
r.setType(payload, 0);
r.setPayload(payload, payloadLength);
addRecord(r);
}
void NdefMessage::addTextRecord(const char *text)
{
addTextRecord(text, "");
}
void NdefMessage::addTextRecord(const char *text, const char *encoding)
{
NdefRecord r;
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));
// encoding length
const uint8_t prefixSize = 5;
byte prefix[prefixSize];
byte encodingSize = strlen(encoding);
prefix[0] = encodingSize;
for (uint8_t i=0; encoding[i] && (i+1) < prefixSize; ++i) // limit encoding to max 4 bytes
prefix[i+1] = encoding[i];
// set payload
r.setPayload(prefix, prefixSize, reinterpret_cast<const byte*>(text), strlen(text));
addRecord(r);
}
void NdefMessage::addUriRecord(const char *uri)
{
NdefRecord r;
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));
// encoding prefix
const uint8_t prefixSize = 1;
byte prefix[prefixSize] = {0};
// set payload
r.setPayload(prefix, prefixSize, reinterpret_cast<const byte*>(uri), strlen(uri));
addRecord(r);
}
void NdefMessage::addExternalRecord(const char *type,const char* payload)
{
addExternalRecord(type, reinterpret_cast<const byte*>(payload), strlen(payload));
}
void NdefMessage::addExternalRecord(const char *type, const byte *payload, int payloadLength)
{
NdefRecord r;
r.setTnf(TNF_EXTERNAL_TYPE);
r.setType(reinterpret_cast<const byte*>(type), strlen(type));
r.setPayload(payload, payloadLength);
addRecord(r);
}
void NdefMessage::addAndroidApplicationRecord(const char *packageName)
{
addExternalRecord("android.com:pkg", packageName);
}
void NdefMessage::addEmptyRecord()
{
NdefRecord r;
r.setTnf(TNF_EMPTY);
addRecord(r);
}
NdefRecord NdefMessage::getRecord(int index)
{
if (index > -1 && index < static_cast<int>(_recordCount))
{
return _records[index];
}
else
{
return NdefRecord(); // would rather return NULL
}
}
NdefRecord NdefMessage::operator[](int index)
{
return getRecord(index);
}
#ifdef NDEF_USE_SERIAL
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"));
for (unsigned int i = 0; i < _recordCount; i++)
{
_records[i].print();
}
}
#endif