forked from DoESLiverpool/simple_sl030_rfid_reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_first_ndef_text_record.c
300 lines (278 loc) · 8.21 KB
/
get_first_ndef_text_record.c
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
/* Find the first text record from an NDEF-formatted Ultralight/NTAG2xx tag
and write the text to stdout
*/
#include <bcm2835.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
/* Set this to 1 in order to get additional output of what's
going on. Set to 0 to disable spurious output */
#define DEBUG 0
#define SL030_ID 0x50
#define SL030_CMD_SELECT 0x01
#define SL030_READ_PAGE 0x10
#define MAX_TAG_ID_LENGTH 7
#define CMD_BUF_LEN 11
int cmdBufferLen = CMD_BUF_LEN;
unsigned char cmdBuffer[CMD_BUF_LEN];
/* Buffer to hold the contents of the RFID tag */
#define kContentsBufferLen 2048
uint8_t gContentsBuffer[kContentsBufferLen];
struct s_cmdResponse
{
unsigned char iLength;
unsigned char iCommand;
unsigned char iStatus;
unsigned char iTag[MAX_TAG_ID_LENGTH];
unsigned char iTagType;
};
// Constants to make parsing NDEF records easier
#define NDEF_RECORD_MESSAGE_BEGIN (1<<7)
#define NDEF_RECORD_MESSAGE_END (1<<6)
#define NDEF_RECORD_CHUNK_FLAG (1<<5)
#define NDEF_RECORD_SHORT_RECORD (1<<4)
#define NDEF_RECORD_ID_LENGTH_PRESENT (1<<3)
#define NDEF_RECORD_TNF_MASK 0x07
#define NDEF_TNF_EMPTY 0x00
#define NDEF_TNF_NFC_WELL_KNOWN 0x01
#define NDEF_TNF_MEDIA_TYPE_RFC2046 0x02
#define NDEF_TNF_URI_RFC3986 0x03
#define NDEF_TNF_NFC_EXTERNAL 0x04
#define NDEF_TNF_NFC_EXTERNAL 0x04
// NDEF well-known record types
#define NDEF_RTD_TEXT 'T'
#define NDEF_RTD_URI 'U'
#define MIN(a, b) ((a) < (b) ? (a) : (b))
int readPage(int aI2CDevice, char aPage, unsigned char* aTagBuffer, int* aTagBufferLen)
{
int ret = BCM2835_I2C_REASON_ERROR_NACK; // Just needs to be not BCM2835_I2C_REASON_OK
int i;
struct s_cmdResponse* resp;
/* Set up command to select a tag */
cmdBuffer[0] = 2;
cmdBuffer[1] = SL030_READ_PAGE;
cmdBuffer[2] = aPage;
int tries = 0;
while ((tries++ < 12) && (ret != BCM2835_I2C_REASON_OK))
{
bcm2835_i2c_begin();
bcm2835_i2c_setSlaveAddress(SL030_ID);
ret = bcm2835_i2c_write(cmdBuffer, 3);
#if DEBUG
printf("write returned %d\n", ret);
#endif
usleep(12000);
memset(cmdBuffer, 0, CMD_BUF_LEN);
ret = bcm2835_i2c_read(cmdBuffer, cmdBufferLen);
bcm2835_i2c_end();
}
#if DEBUG
printf("read returned %d\n", ret);
for (i = 0; i < cmdBufferLen; i++)
{
printf("%02x ", cmdBuffer[i]);
}
printf("\n");
#endif
resp = (struct s_cmdResponse*)cmdBuffer;
#if DEBUG
printf("Length: %d\n", resp->iLength);
printf("Status: %d\n", resp->iStatus);
printf("Status: %u\n", (char)resp->iStatus);
#endif
if ((resp->iStatus == 0) && (resp->iLength > 2))
{
/* We found a tag! */
/* Copy the ID across */
/* drop 2 bytes from iLength: one each for command and status */
*aTagBufferLen = (resp->iLength - 2 > MAX_TAG_ID_LENGTH ? MAX_TAG_ID_LENGTH : resp->iLength-2);
memcpy(aTagBuffer, resp->iTag, *aTagBufferLen);
return 1;
}
else
{
return 0;
}
}
int checkForTag(int aI2CDevice, unsigned char* aTagBuffer, int* aTagBufferLen)
{
int ret = 0;
int i;
struct s_cmdResponse* resp;
/* Set up command to select a tag */
cmdBuffer[0] = 1;
cmdBuffer[1] = SL030_CMD_SELECT;
bcm2835_i2c_begin();
bcm2835_i2c_setSlaveAddress(SL030_ID);
ret = bcm2835_i2c_write(cmdBuffer, 2);
#if DEBUG
printf("write returned %d\n", ret);
#endif
usleep(20000);
memset(cmdBuffer, 0, CMD_BUF_LEN);
ret = bcm2835_i2c_read(cmdBuffer, cmdBufferLen);
bcm2835_i2c_end();
#if DEBUG
printf("read returned %d\n", ret);
for (i = 0; i < cmdBufferLen; i++)
{
printf("%02x ", cmdBuffer[i]);
}
printf("\n");
#endif
resp = (struct s_cmdResponse*)cmdBuffer;
#if DEBUG
printf("Length: %d\n", resp->iLength);
printf("Status: %d\n", resp->iStatus);
printf("Status: %u\n", (char)resp->iStatus);
#endif
if ((resp->iStatus == 0) && (resp->iLength > 2))
{
/* We found a tag! */
/* Copy the ID across */
/* drop 3 bytes from iLength: one each for command, status and tag type */
*aTagBufferLen = (resp->iLength - 3 > MAX_TAG_ID_LENGTH ? MAX_TAG_ID_LENGTH : resp->iLength-3);
memcpy(aTagBuffer, resp->iTag, *aTagBufferLen);
return 1;
}
else
{
return 0;
}
}
int main(void)
{
int ret;
int fd;
int tries = 0;
int tagIdLen = MAX_TAG_ID_LENGTH;
unsigned char tagId[MAX_TAG_ID_LENGTH];
bcm2835_init();
// We're assuming the caller has made sure there's a tag present
int page = 4; // skip the first four pages as they hold general info on the tag
int pageLen = MAX_TAG_ID_LENGTH;
int contentIdx = 0; // where in the content buffer we're up to
while ((contentIdx < kContentsBufferLen) && readPage(fd, page, &gContentsBuffer[contentIdx], &pageLen))
{
contentIdx += pageLen;
page++; // move onto the next page
}
#if DEBUG
printf("Read in %d bytes\n", contentIdx);
int i;
for (i = 0; i < contentIdx; i++)
{
printf("%02x ", gContentsBuffer[i]);
if (i % 16 == 15)
{
printf("\n");
}
}
printf("\n");
#endif
// Parse the content to look for NDEF records
int idx = 0;
while (idx < contentIdx)
{
// Look for the initial TLV structure
uint8_t t = gContentsBuffer[idx++];
if (t != 0)
{
// It's not a NULL TLV
#if DEBUG
printf("t: %02x\n", t);
#endif
int l = gContentsBuffer[idx++];
idx = MIN(idx, contentIdx);
if (l == 0xff)
{
// 3-byte length format, so the next two bytes are the actual length
l = gContentsBuffer[idx++] << 8 | gContentsBuffer[idx++];
idx = MIN(idx, contentIdx);
}
uint8_t tnf_byte;
uint8_t typeLength =0;
uint32_t payloadLength =0;
uint8_t idLength =0;
int messageEnd = idx + l;
switch (t)
{
case 0x03:
// We've found an NDEF message
// Parse out each record in it
#if DEBUG
printf("NDEF message found\n", t);
#endif
while ((idx < messageEnd) && (idx < contentIdx))
{
tnf_byte = gContentsBuffer[idx++];
idx = MIN(idx, contentIdx);
typeLength = gContentsBuffer[idx++];
idx = MIN(idx, contentIdx);
if (tnf_byte & NDEF_RECORD_SHORT_RECORD)
{
payloadLength = gContentsBuffer[idx++];
}
else
{
payloadLength = (gContentsBuffer[idx++] << 24) |
(gContentsBuffer[idx++] << 16) |
(gContentsBuffer[idx++] << 8) |
(gContentsBuffer[idx++]);
}
idx = MIN(idx, contentIdx);
if (tnf_byte & NDEF_RECORD_ID_LENGTH_PRESENT) {
idLength = gContentsBuffer[idx++];
idx = MIN(idx, contentIdx);
}
#if DEBUG
printf("NDEF record: tnf_byte: 0x%02x, typeLength: %d, idLength: %d, payloadLength: %d, next byte: 0x%02x\n", tnf_byte, typeLength, idLength, payloadLength, gContentsBuffer[idx]);
#endif
// Let's see if it's a record we're interested in...
if ( ((tnf_byte & NDEF_RECORD_TNF_MASK) == NDEF_TNF_NFC_WELL_KNOWN) &&
(typeLength == 1) && (gContentsBuffer[idx] == NDEF_RTD_TEXT) )
{
// It's text! Let's output it...
idx += typeLength; // skip over the type
// skip the language
int langLength = gContentsBuffer[idx++];
payloadLength--; // for the langLength byte
payloadLength -= langLength;
idx += langLength;
while (payloadLength-- > 0)
{
idx = MIN(idx, contentIdx);
printf("%c", gContentsBuffer[idx++]);
}
// ...and quit
exit(0);
}
else
{
// Skip this message
#if DEBUG
printf("Skipping (tnf_byte: %02x)\n", tnf_byte);
printf("idx: %d messageEnd: %d typeLength: %d idLength: %d payloadLength: %d contentIdx: %d\n", idx, messageEnd, typeLength, idLength, payloadLength, contentIdx);
#endif
idx += typeLength+idLength+payloadLength;
idx = MIN(idx, contentIdx);
}
if (tnf_byte & NDEF_RECORD_MESSAGE_END)
{
break;
}
}
break;
case 0xfe:
// Terminator TLV block, give up now
exit(1);
break;
default:
// Skip to the next TLV
idx += l;
break;
};
}
}
}