forked from don/NDEF
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNfcAdapter.cpp
250 lines (225 loc) · 5.87 KB
/
NfcAdapter.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
#include <NfcAdapter.h>
NfcAdapter::NfcAdapter(PN532Interface &interface)
{
shield = new PN532(interface);
}
NfcAdapter::~NfcAdapter(void)
{
delete shield;
}
void NfcAdapter::begin(boolean verbose)
{
shield->begin();
uint32_t versiondata = shield->getFirmwareVersion();
if (! versiondata)
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Didn't find PN53x board"));
#endif
while (1); // halt
}
if (verbose)
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Found chip PN5")); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print(F("Firmware ver. ")); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
#endif
}
// configure board to read RFID tags
shield->SAMConfig();
}
boolean NfcAdapter::tagPresent(unsigned long timeout)
{
uint8_t success;
uidLength = 0;
if (timeout == 0)
{
// default value in PNE532lib is 1000
timeout = 1000;
}
// aligot why do I need inlisted @true ? FIXME
success = shield->readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, (uint8_t*)&uidLength, timeout, true, &ATQA, &SAK);
return success;
}
boolean NfcAdapter::erase()
{
NdefMessage message = NdefMessage();
message.addEmptyRecord();
return write(message);
}
boolean NfcAdapter::format()
{
boolean success;
#ifdef NDEF_SUPPORT_MIFARE_CLASSIC
if (uidLength == 4)
{
MifareClassic mifareClassic = MifareClassic(*shield);
success = mifareClassic.formatNDEF(uid, uidLength);
}
else
#endif
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Unsupported Tag."));
#endif
success = false;
}
return success;
}
boolean NfcAdapter::clean()
{
uint8_t type = guessTagType();
#ifdef NDEF_SUPPORT_MIFARE_CLASSIC
if (type == TAG_TYPE_MIFARE_CLASSIC)
{
#ifdef NDEF_DEBUG
Serial.println(F("Cleaning Mifare Classic"));
#endif
MifareClassic mifareClassic = MifareClassic(*shield);
return mifareClassic.formatMifare(uid, uidLength);
}
else
#endif
if (type == TAG_TYPE_4)
{
#ifdef NDEF_DEBUG
Serial.println(F("Cleaning Mifare Plus"));
#endif
MifarePlus mifarePlus = MifarePlus(*shield);
return mifarePlus.clean();
}
else
if (type == TAG_TYPE_2)
{
#ifdef NDEF_DEBUG
Serial.println(F("Cleaning Mifare Ultralight"));
#endif
MifareUltralight ultralight = MifareUltralight(*shield);
return ultralight.clean();
}
else
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("No driver for card type "));Serial.println(type);
#endif
return false;
}
}
NfcTag NfcAdapter::read()
{
uint8_t type = guessTagType();
#ifdef NDEF_SUPPORT_MIFARE_CLASSIC
if (type == TAG_TYPE_MIFARE_CLASSIC)
{
#ifdef NDEF_DEBUG
Serial.println(F("Reading Mifare Classic"));
#endif
MifareClassic mifareClassic = MifareClassic(*shield);
return mifareClassic.read(uid, uidLength);
}
else
#endif
if (type == TAG_TYPE_2)
{
#ifdef NDEF_DEBUG
Serial.println(F("Reading Mifare Ultralight"));
#endif
MifareUltralight ultralight = MifareUltralight(*shield);
return ultralight.read(uid, uidLength);
}
else
if (type == TAG_TYPE_4)
{
#ifdef NDEF_DEBUG
Serial.println(F("Reading Mifare Plus"));
#endif
MifarePlus mifplus = MifarePlus(*shield);
return mifplus.read(uid, uidLength);
}
else if (type == TAG_TYPE_UNKNOWN)
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Can not determine tag type"));
#endif
return NfcTag(uid, uidLength);
}
else
{
// Serial.print(F("No driver for card type "));Serial.println(type);
// TODO should set type here
return NfcTag(uid, uidLength);
}
}
boolean NfcAdapter::write(NdefMessage& ndefMessage)
{
boolean success;
uint8_t type = guessTagType();
#ifdef NDEF_SUPPORT_MIFARE_CLASSIC
if (type == TAG_TYPE_MIFARE_CLASSIC)
{
#ifdef NDEF_DEBUG
Serial.println(F("Writing Mifare Classic"));
#endif
MifareClassic mifareClassic = MifareClassic(*shield);
success = mifareClassic.write(ndefMessage, uid, uidLength);
}
else
#endif
if (type == TAG_TYPE_2)
{
#ifdef NDEF_DEBUG
Serial.println(F("Writing Mifare Ultralight"));
#endif
MifareUltralight mifareUltralight = MifareUltralight(*shield);
success = mifareUltralight.write(ndefMessage, uid, uidLength);
}
else
if (type == TAG_TYPE_4)
{
#ifdef NDEF_DEBUG
Serial.println(F("Writing Mifare Plus"));
#endif
MifarePlus mifarePlus = MifarePlus(*shield);
success = mifarePlus.write(ndefMessage, uid, uidLength);
}
else if (type == TAG_TYPE_UNKNOWN)
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Can not determine tag type"));
#endif
success = false;
}
else
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("No driver for card type "));Serial.println(type);
#endif
success = false;
}
return success;
}
// TODO this should return a Driver MifareClassic, MifareUltralight, Type 4, Unknown
// Guess Tag Type by looking at the ATQA and SAK values
// Need to follow spec for Card Identification. Maybe AN1303, AN1305 and ???
unsigned int NfcAdapter::guessTagType()
{
// 4 byte id - Mifare Classic
// - ATQA 0x4 && SAK 0x8
// 7 byte id
// - ATQA 0x44 && SAK 0x8 - Mifare Classic
// - ATQA 0x44 && SAK 0x0 - Mifare Ultralight NFC Forum Type 2
// - ATQA 0x344 && SAK 0x20 - NFC Forum Type 4
if (ATQA == 0x44 && SAK == 0x20)
{
return TAG_TYPE_4;
}
else if (uidLength == 4)
{
return TAG_TYPE_MIFARE_CLASSIC;
}
else
{
return TAG_TYPE_2;
}
}