-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfid.ino
254 lines (208 loc) · 6.31 KB
/
rfid.ino
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
#include <arduino.h>
#include <Regexp.h>
#include <SPI.h>
#include "card.h"
#include "mfoc.h"
#define CMDLEN 50
void setup()
{
Serial.begin(57600);
SPI.begin();
pinMode(chipSelectPin,OUTPUT); // Set digital pin 10 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(chipSelectPin, LOW); // Activate the RFID reader
pinMode(NRSTPD,OUTPUT); // Set digital pin 5 , Not Reset and Power-down
//MFRC522_Init();
}
void loop()
{
MatchState ms;
char cmd[CMDLEN] = {0};
uint index = 0;
if (ReadCMD(cmd))
{
ms.Target(cmd);
char result = ms.Match ("(%a+)(.*)", index);
if (result == REGEXP_MATCHED)
{
char buf[200];
//Serial.println(ms.level);
//for (int i = 0; i < ms.level; ++i)
//{
// Serial.println(ms.GetCapture(buf, i));
//}
char *p = ms.GetCapture(buf, 0);
//Serial.println(p);
Serial.print(">>>");
Serial.print(cmd);
Serial.println("");
if (strcmp(p, "test") == 0)
{
uchar str[MAX_LEN];
DebugOn();
MFRC522_Init();
readdata();
readdata();
readdata();
readdata();
readdata();
//uchar buf[] = {0x26};
/*
uchar buf[] = {0x04, 0x00};
Write_MFRC522(BitFramingReg, 0x07);
test(buf, 2, str);
/*
uchar buf1[] = {0x93, 0x20};
Write_MFRC522(BitFramingReg, 0x00);
test(buf1, 2, str);
*/
//MFRC522_Halt();
}
else if (strcmp(p, "help") == 0 || strcmp(cmd, "?") == 0)
{
usage();
}
else if (strcmp(p, "show") == 0)
{
//Serial.println(ms.GetCapture(buf, 1));
if (strcmp(ms.GetCapture(buf, 1), " tag") == 0)
showTag();
}
else if (strcmp(p, "debug") == 0)
{
if (strcmp(ms.GetCapture(buf, 1), " off") == 0)
DebugOff();
else
DebugOn();
}
else if (strcmp(p, "auth") == 0)
{
result = ms.Match("auth (%d+) 0x(.+) ([abAB])", index);
if (result == REGEXP_MATCHED)
{
uchar type;
char *ptype = ms.GetCapture(buf, 2);
if (strcmp(ptype, "A") == 0 || strcmp(ptype, "a") == 0)
{
type = PICC_AUTHENT1A;
}
else
{
type = PICC_AUTHENT1B;
}
uint block = atoi(ms.GetCapture(buf, 0));
uchar key[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
char * pkey = ms.GetCapture(buf, 1);
char kk[12] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};
for (int i = 0; i < 12; ++i)
{
if (pkey[i] == NULL)
{
break;
}
kk[i] = pkey[i];
}
for (int i = 0; i < 6; ++i)
{
char p[2] = {kk[i * 2], kk[i * 2 + 1]};
key[i] = strtoul(p, NULL, 16);
//Serial.println(s.substring(i * 2, i * 2 + 2));
//Serial.print(key[i], HEX);
//Serial.print(" ");
}
//Serial.println("KEY");
if (checkKey(block, key, type))
Serial.println("Successed!");
else
Serial.println("Failed!");
}
else
{
Serial.println("auth <block addr> <key: 0xffffffffffffff> <type: A/B>: verifiy key.");
}
//checkKey(uchar *uid, uint block, uchar *key, uchar type);
}
else if (strcmp(cmd, "check default key") == 0)
{
checkDefaultKey();
}
else
{
Serial.println("Unkown command!");
}
Serial.print("\n");
}
}
}
//char *command(char* This)
bool ReadCMD(char * cmd)
{
int index = 0;
bool b = false;
if (Serial.available())
{
b = true;
/*
for (int i = 0; i < CMDLEN; ++i)
{
cmd[i] = '\0';
}
*/
}
while(b && index < CMDLEN)
{
cmd[index] = Serial.read();
index ++;
for (int i = 0; i < 20; ++i)
{
if (Serial.available())
{
break;
}
delay(1);
if (i >= 19)
{
b = false;
break;
}
}
}
if (index > 0)
{
cmd[index] = '\0';
return true;
}
return false;
}
void showTag()
{
uchar status;
uchar str[MAX_LEN];
MFRC522_Init();
status = MFRC522_Request(PICC_REQIDL, str);
if (status != MI_OK)
{
Serial.println("Show Tag Informations Failed!");
return;
}
// Show card type
ShowCardType(str);
//Prevent conflict, return the 4 bytes Serial number of the card
status = MFRC522_Anticoll(str);
// str[0..3]: serial number of the card
// str[4]: XOR checksum of the SN.
if (status == MI_OK)
{
ShowCardID(str);
}
MFRC522_Halt();
}
void usage()
{
Serial.println("NFC Tools (version 1.0): by R5");
Serial.println("help: help");
Serial.println("show tag: show the tag informations.");
Serial.println("check default key: Check password by default.");
Serial.println("check password by default: Check password by default.");
Serial.println("auth <block addr> <key: 0xffffffffffffff> <type: A/B>: verifiy key.");
Serial.println("read <block addr> <keyA> [keyB]");
}