forked from ATrappmann/PN5180-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NDEFReader.cpp
235 lines (184 loc) · 5 KB
/
NDEFReader.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
#include <Arduino.h>
#include "NDEFReader.h"
#include "PN5180ISO15693.h"
ISO15693ErrorCode NDEFReader::readNextByte(uint8_t& data) {
// read metadata
ISO15693ErrorCode rc;
if (_blockSize <= 0) {
rc = _nfc->getSystemInfo(_uid, &_blockSize, &_blockCount);
if (ISO15693_EC_OK != rc) {
return rc;
}
}
// read next block?
if (_blockDataPos >= _blockSize) {
_blockIndex++;
if (_blockIndex >= _blockCount) {
// done with final block
return EC_NO_BLOCK_LEFT;
}
rc = _nfc->readSingleBlock(_uid, _blockIndex, _blockData, _blockSize);
if (ISO15693_EC_OK != rc) {
return rc;
}
_blockDataPos = 0;
}
// return next byte
data = _blockData[_blockDataPos];
_blockDataPos++;
return ISO15693_EC_OK;
}
ISO15693ErrorCode NDEFReader::readMessageAsText(String& text) {
// read first byte
byte firstByte;
ISO15693ErrorCode rc;
rc = readNextByte(firstByte);
if (ISO15693_EC_OK != rc) {
return rc;
}
// check if TLV container
// (this check is sufficient as 0xe1 is an invalid NDEF header)
if (firstByte == 0xe1) {
uint8_t version;
rc = readNextByte(version);
if (ISO15693_EC_OK != rc) {
return rc;
}
if ((version & 0xf0) != 0x40) {
return EC_TLV_FORMAT_ERROR;
}
// skip page count
// skip access level
skipNextByte(2);
uint8_t contentType;
rc = readNextByte(contentType);
if (ISO15693_EC_OK != rc) {
return rc;
}
if (contentType != 0x03 /* NDEF */) {
// not a NDEF message
return EC_NO_BLOCK_LEFT;
}
// skip content length
// (will be taken from NDEF not the TLV)
skipNextByte();
// read first byte of content/NDEF message
rc = readNextByte(firstByte);
if (ISO15693_EC_OK != rc) {
return rc;
}
}
// decode tnf - first byte is tnf with bit flags
//bool mb = (tnf_byte & 0x80) == 0x80;
//bool me = (firstByte & 0x40) == 0x40;
//bool cf = (tnf_byte & 0x20) == 0x20;
bool sr = (firstByte & 0x10) == 0x10;
bool il = (firstByte & 0x08) == 0x08;
uint8_t tnf = (firstByte & 0x07);
// currently, only well known
// types are supported
if (tnf != 0x01) {
return EC_NDEF_FORMAT_ERROR;
}
// get type length
uint8_t typeLength;
rc = readNextByte(typeLength);
if (ISO15693_EC_OK != rc) {
return rc;
}
// get payload length
uint32_t payloadLength;
if (sr) {
uint8_t pl1;
rc = readNextByte(pl1);
if (ISO15693_EC_OK != rc) {
return rc;
}
payloadLength = pl1;
} else {
uint8_t pl1, pl2, pl3, pl4;
rc = readNextByte(pl1);
if (ISO15693_EC_OK != rc) {
return rc;
}
rc = readNextByte(pl2);
if (ISO15693_EC_OK != rc) {
return rc;
}
rc = readNextByte(pl3);
if (ISO15693_EC_OK != rc) {
return rc;
}
rc = readNextByte(pl4);
if (ISO15693_EC_OK != rc) {
return rc;
}
payloadLength =
(static_cast<uint32_t>(pl1) << 24)
| (static_cast<uint32_t>(pl2) << 16)
| (static_cast<uint32_t>(pl3) << 8)
| static_cast<uint32_t>(pl4);
}
// skip the id length
uint8_t idLength = 0;
if (il) {
rc = readNextByte(idLength);
if (ISO15693_EC_OK != rc) {
return rc;
}
}
// read type information
// currently, only text is supported
if (typeLength != 1) {
return EC_NDEF_FORMAT_ERROR;
}
uint8_t type;
rc = readNextByte(type);
if (ISO15693_EC_OK != rc) {
return rc;
}
if (type != 'T') {
return EC_NDEF_FORMAT_ERROR;
}
// skip the id
skipNextByte(idLength);
// skip text language code
uint8_t langCodeLength;
rc = readNextByte(langCodeLength);
if (ISO15693_EC_OK != rc) {
return rc;
}
skipNextByte(langCodeLength);
payloadLength--;
payloadLength -= langCodeLength;
// read payload
uint8_t data[payloadLength + 1];
for (uint32_t i = 0; i < payloadLength; i++) {
rc = readNextByte(data[i]);
if (ISO15693_EC_OK != rc) {
return rc;
}
}
// done
data[payloadLength] = 0;
text = std::move(String((const char*)data));
return ISO15693_EC_OK;
}
ISO15693ErrorCode NDEFReader::skipNextByte(uint8_t count) {
ISO15693ErrorCode rc;
for (uint8_t i = 0; i < count; i++) {
uint8_t t;
rc = readNextByte(t);
if (ISO15693_EC_OK != rc) {
return rc;
}
}
return ISO15693_EC_OK;
}
NDEFReader::NDEFReader(PN5180ISO15693& nfc, uint8_t *uid) {
assert(uid != nullptr);
_nfc = &nfc;
_uid = uid;
}
NDEFReader::~NDEFReader() {
}