-
Notifications
You must be signed in to change notification settings - Fork 416
/
rfid.esp
252 lines (248 loc) · 6.72 KB
/
rfid.esp
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
void ICACHE_FLASH_ATTR rfidloop() {
String uid = "";
String type = "";
#ifndef OFFICIALBOARD
if (readerType == 0) {
if (!mfrc522.PICC_IsNewCardPresent()) {
delay(50);
return;
}
if (!mfrc522.PICC_ReadCardSerial()) {
delay(50);
return;
}
mfrc522.PICC_HaltA();
cooldown = millis() + 2000;
#ifdef DEBUG
Serial.print(F("[ INFO ] PICC's UID: "));
#endif
for (int i = 0; i < mfrc522.uid.size; ++i) {
uid += String(mfrc522.uid.uidByte[i], HEX);
}
#ifdef DEBUG
Serial.print(uid);
#endif
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
type = mfrc522.PICC_GetTypeName(piccType);
#ifdef DEBUG
Serial.print(" " + type);
#endif
} else if (readerType == 1) {
if (wg.available()) {
#ifdef DEBUG
Serial.print(F("[ INFO ] PICC's UID: "));
Serial.println(wg.getCode());
#endif
uid = String(wg.getCode(), DEC);
type = String(wg.getWiegandType(), DEC);
cooldown = millis() + 2000;
} else {
return;
}
} else if (readerType == 2) {
bool found = false;
byte pnuid[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
eCardType e_CardType;
byte u8_UidLength = 0x00; // UID = 4 or 7 bytes
found = pn532.ReadPassiveTargetID(pnuid, &u8_UidLength, &e_CardType);
if (found && u8_UidLength >= 4) {
#ifdef DEBUG
Serial.print(F("[ INFO ] PICC's UID: "));
#endif
for (uint8_t i = 0; i < u8_UidLength; i++) {
uid += String(pnuid[i], HEX);
}
#ifdef DEBUG
Serial.print(uid);
#endif
cooldown = millis() + 2000;
} else {
delay(50);
return;
}
} else {
delay(50);
return;
}
#endif
#ifdef OFFICIALBOARD
if (wg.available()) {
#ifdef DEBUG
Serial.print(F("[ INFO ] PICC's UID: "));
Serial.print(wg.getCode());
#endif
uid = String(wg.getCode(), DEC);
type = String(wg.getWiegandType(), HEX);
cooldown = millis() + 2000;
} else {
return;
}
#endif
int AccType = 0;
String filename = "/P/";
filename += uid;
File f = SPIFFS.open(filename, "r");
if (f) {
size_t size = f.size();
std::unique_ptr<char[]> buf(new char[size]);
f.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.parseObject(buf.get());
if (json.success()) {
String username = json["user"];
AccType = json["acctype"];
#ifdef DEBUG
Serial.println(" = known PICC");
Serial.print("[ INFO ] User Name: ");
if (username == "undefined")
Serial.print(uid);
else
Serial.print(username);
#endif
if (AccType == 1) {
activateRelay = true;
ws.textAll("{\"command\":\"giveAccess\"}");
#ifdef DEBUG
Serial.println(" have access");
#endif
if (mqttenabled == 1) {
mqtt_publish_access(now(), "true", "Always", username, uid);
}
} else if (AccType == 99) {
doEnableWifi = true;
activateRelay = true;
ws.textAll("{\"command\":\"giveAccess\"}");
#ifdef DEBUG
Serial.println(" have admin access, enable wifi");
#endif
if (mqttenabled == 1) {
mqtt_publish_access(now(), "true", "Admin", username, uid);
}
} else {
#ifdef DEBUG
Serial.println(" does not have access");
#endif
if (mqttenabled == 1) {
mqtt_publish_access(now(), "true", "Disabled", username, uid);
}
}
writeLatest(uid, username, AccType);
DynamicJsonBuffer jsonBuffer2;
JsonObject &root = jsonBuffer2.createObject();
root["command"] = "piccscan";
root["uid"] = uid;
root["type"] = type;
root["known"] = 1;
root["acctype"] = AccType;
root["user"] = username;
size_t len = root.measureLength();
AsyncWebSocketMessageBuffer *buffer = ws.makeBuffer(len);
if (buffer) {
root.printTo((char *)buffer->get(), len + 1);
ws.textAll(buffer);
}
} else {
#ifdef DEBUG
Serial.println("");
Serial.println(F("[ WARN ] Failed to parse User Data"));
#endif
}
f.close();
} else {
String data = String(uid);
data += " " + String(type);
writeEvent("WARN", "rfid", "Unknown rfid tag is scanned", data);
writeLatest(uid, "Unknown", 98);
#ifdef DEBUG
Serial.println(" = unknown PICC");
#endif
DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["command"] = "piccscan";
root["uid"] = uid;
root["type"] = type;
root["known"] = 0;
size_t len = root.measureLength();
AsyncWebSocketMessageBuffer *buffer = ws.makeBuffer(len);
if (buffer) {
root.printTo((char *)buffer->get(), len + 1);
ws.textAll(buffer);
}
if (mqttenabled == 1) {
mqtt_publish_access(now(), "false", "Denied", "Unknown", uid);
}
}
}
#ifndef OFFICIALBOARD
#ifdef DEBUG
void ICACHE_FLASH_ATTR ShowMFRC522ReaderDetails() {
// Get the MFRC522 software version
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print(F("[ INFO ] MFRC522 Version: 0x"));
Serial.print(v, HEX);
if (v == 0x91)
Serial.print(F(" = v1.0"));
else if (v == 0x92)
Serial.print(F(" = v2.0"));
else if (v == 0x88)
Serial.print(F(" = clone"));
else
Serial.print(F(" (unknown)"));
Serial.println("");
// When 0x00 or 0xFF is returned, communication probably failed
if ((v == 0x00) || (v == 0xFF)) {
Serial.println(F("[ WARN ] Communication failure, check if MFRC522 properly connected"));
}
}
#endif
#endif
void ICACHE_FLASH_ATTR setupWiegandReader(int d0, int d1) {
wg.begin(d0, d1);
}
#ifndef OFFICIALBOARD
void ICACHE_FLASH_ATTR setupMFRC522Reader(int rfidss, int rfidgain) {
SPI.begin(); // MFRC522 Hardware uses SPI protocol
mfrc522.PCD_Init(rfidss, UINT8_MAX); // Initialize MFRC522 Hardware
// Set RFID Hardware Antenna Gain
// This may not work with some boards
mfrc522.PCD_SetAntennaGain(rfidgain);
#ifdef DEBUG
Serial.printf("[ INFO ] RFID SS_PIN: %u and Gain Factor: %u", rfidss, rfidgain);
Serial.println("");
#endif
#ifdef DEBUG
ShowMFRC522ReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
#endif
}
#endif
#ifndef OFFICIALBOARD
void ICACHE_FLASH_ATTR setupPN532Reader(int rfidss) {
// init controller
pn532.InitSoftwareSPI(14, 12, 13, rfidss, 0);
do { // pseudo loop (just used for aborting with break;)
// Reset the PN532
pn532.begin(); // delay > 400 ms
byte IC, VersionHi, VersionLo, Flags;
if (!pn532.GetFirmwareVersion(&IC, &VersionHi, &VersionLo, &Flags))
break;
#ifdef DEBUG
char Buf[80];
sprintf(Buf, "Chip: PN5%02X, Firmware version: %d.%d\r\n", IC, VersionHi, VersionLo);
Utils::Print(Buf);
sprintf(Buf, "Supports ISO 14443A:%s, ISO 14443B:%s, ISO 18092:%s\r\n", (Flags & 1) ? "Yes" : "No",
(Flags & 2) ? "Yes" : "No",
(Flags & 4) ? "Yes" : "No");
Utils::Print(Buf);
#endif
// Set the max number of retry attempts to read from a card.
// This prevents us from waiting forever for a card, which is the default behaviour of the PN532.
if (!pn532.SetPassiveActivationRetries()) {
break;
}
// configure the PN532 to read RFID tags
if (!pn532.SamConfig()) {
break;
}
} while (false);
}
#endif