Skip to content

Commit d7be1a4

Browse files
authored
Merge pull request #621 from mcnewton/rfidesptidy
Couple of small updates to rfid.esp and add more comments
2 parents 56c0fb9 + 195d29d commit d7be1a4

File tree

1 file changed

+77
-14
lines changed

1 file changed

+77
-14
lines changed

src/rfid.esp

+77-14
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,16 @@ void pn532Read()
163163
}
164164
}
165165

166+
167+
/*
168+
* Try first to read from RDM6300 hardware. If that received
169+
* nothing, check the other configured reader.
170+
*/
166171
void genericRead()
167172
{
173+
/*
174+
* Test RDM6300 125khz reader
175+
*/
168176
while (Serial.available() > 0)
169177
{
170178
RFIDr.rfidSerial(Serial.read());
@@ -181,46 +189,74 @@ void genericRead()
181189
#endif
182190
}
183191

184-
if (config.readertype == READER_MFRC522_RDM6300 && uid.length() == 0)
185-
{
186-
mfrc522Read();
187-
}
192+
/*
193+
* If nothing read from the RDM6300, check the other hardware
194+
*/
195+
if (uid.length() == 0) {
196+
if (config.readertype == READER_MFRC522_RDM6300)
197+
{
198+
mfrc522Read();
199+
}
188200

189-
else if (config.readertype == READER_WIEGAND_RDM6300 && uid.length() == 0)
190-
{
191-
wiegandRead();
192-
}
201+
else if (config.readertype == READER_WIEGAND_RDM6300)
202+
{
203+
wiegandRead();
204+
}
193205

194-
else if (config.readertype == READER_PN532_RDM6300 && uid.length() == 0)
195-
{
196-
pn532Read();
206+
else if (config.readertype == READER_PN532_RDM6300)
207+
{
208+
pn532Read();
209+
}
197210
}
198211
}
199212

213+
214+
/*
215+
* Main function to read RFID cards. This function will call the
216+
* correct reader function depending on the configured hardware,
217+
* or otherwise call genericRead to read both RDM6300 and another
218+
* configured reader.
219+
*/
200220
void rfidRead()
201221
{
222+
/*
223+
* Do not try and read if we are already processing a card
224+
*/
202225
if (rfidState == cardSwiped)
203226
{
204227
return;
205228
}
229+
230+
/*
231+
* Call the appropriate function based on the configured
232+
* hardware
233+
*/
206234
if (config.readertype == READER_MFRC522)
207235
{
208236
mfrc522Read();
209237
}
238+
210239
else if (config.readertype == READER_WIEGAND)
211240
{
212241
wiegandRead();
213242
}
243+
214244
else if (config.readertype == READER_PN532)
215245
{
216246
pn532Read();
217247
}
248+
218249
else if (config.readertype > READER_PN532)
219250
{
251+
// This is a combination of RDM6300 and one of the above
220252
genericRead();
221253
}
222254
}
223255

256+
257+
/*
258+
* Try and read a PIN code from Wiegand hardware
259+
*/
224260
void pinCodeRead()
225261
{
226262
if (config.readertype != READER_WIEGAND ||
@@ -288,6 +324,11 @@ int weekdayFromMonday(int weekdayFromSunday) {
288324
return ( weekdayFromSunday + 5 ) % 7;
289325
}
290326

327+
328+
/*
329+
* If we have successfully read an RFID card, check if access
330+
* should be granted
331+
*/
291332
void rfidProcess()
292333
{
293334
if (rfidState == waitingRfid ||
@@ -296,12 +337,14 @@ void rfidProcess()
296337
return;
297338
}
298339

340+
/* Each user has a file named after the RFID UID */
299341
File f = SPIFFS.open("/P/" + uid, "r");
300342

301343
/*
302344
* If the file was not found then this is an unknown user, so no more
303-
* processing to be done. However, we do a secondary check here to see
304-
* if an old esp-rfid v1 uid exists and if so use that.
345+
* processing to be done. However, for backwards compatibility we do a
346+
* secondary check here to see if an old esp-rfid v1 uid exists and if
347+
* so use that.
305348
*/
306349
if (!f)
307350
{
@@ -320,13 +363,19 @@ void rfidProcess()
320363
#endif
321364
}
322365

366+
/*
367+
* Read the user's settings
368+
*/
323369
size_t size = f.size();
324370
std::unique_ptr<char[]> buf(new char[size]);
325371
f.readBytes(buf.get(), size);
326372
f.close();
327373
DynamicJsonDocument json(512);
328374
auto error = deserializeJson(json, buf.get(), size);
329375

376+
/*
377+
* Corrupt user data file, so return invalid user
378+
*/
330379
if (error)
331380
{
332381
processingState = notValid;
@@ -348,24 +397,32 @@ void rfidProcess()
348397
return;
349398
}
350399

400+
/*
401+
* Get account type (for FIRST relay only) and username from user's data
402+
*/
351403
accountType = json["acctype"];
352404
username = json["user"].as<String>();
353405

354406
#ifdef DEBUG
355407
Serial.println(" = known PICC");
356-
Serial.print("[ INFO ] User Name: ");
408+
Serial.print("[ INFO ] User Name: '");
357409
if (username == "undefined")
358410
Serial.print(uid);
359411
else
360412
Serial.print(username);
413+
Serial.print("'");
361414
#endif
362415

363416
if (accountType == ACCESS_GRANTED)
364417
{
418+
/*
419+
* Normal user - relay but no admin access
420+
*/
365421
unsigned long validSinceL = json["validsince"];
366422
unsigned long validUntilL = json["validuntil"];
367423
unsigned long nowL = epoch;
368424
int hourTz = timeinfo.tm_hour;
425+
369426
if (validUntilL < nowL || validSinceL > nowL)
370427
{
371428
processingState = expired;
@@ -378,9 +435,15 @@ void rfidProcess()
378435
}
379436
} else if (accountType == ACCESS_ADMIN)
380437
{
438+
/*
439+
* Admin user - enable relay (with no time limits) and wifi
440+
*/
381441
doEnableWifi = true;
382442
processingState = validAdmin;
383443
} else {
444+
/*
445+
* User exists but does not have access
446+
*/
384447
processingState = notValid;
385448
}
386449

0 commit comments

Comments
 (0)