Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Couple of small updates to rfid.esp and add more comments #621

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 77 additions & 14 deletions src/rfid.esp
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,16 @@ void pn532Read()
}
}


/*
* Try first to read from RDM6300 hardware. If that received
* nothing, check the other configured reader.
*/
void genericRead()
{
/*
* Test RDM6300 125khz reader
*/
while (Serial.available() > 0)
{
RFIDr.rfidSerial(Serial.read());
Expand All @@ -181,46 +189,74 @@ void genericRead()
#endif
}

if (config.readertype == READER_MFRC522_RDM6300 && uid.length() == 0)
{
mfrc522Read();
}
/*
* If nothing read from the RDM6300, check the other hardware
*/
if (uid.length() == 0) {
if (config.readertype == READER_MFRC522_RDM6300)
{
mfrc522Read();
}

else if (config.readertype == READER_WIEGAND_RDM6300 && uid.length() == 0)
{
wiegandRead();
}
else if (config.readertype == READER_WIEGAND_RDM6300)
{
wiegandRead();
}

else if (config.readertype == READER_PN532_RDM6300 && uid.length() == 0)
{
pn532Read();
else if (config.readertype == READER_PN532_RDM6300)
{
pn532Read();
}
}
}


/*
* Main function to read RFID cards. This function will call the
* correct reader function depending on the configured hardware,
* or otherwise call genericRead to read both RDM6300 and another
* configured reader.
*/
void rfidRead()
{
/*
* Do not try and read if we are already processing a card
*/
if (rfidState == cardSwiped)
{
return;
}

/*
* Call the appropriate function based on the configured
* hardware
*/
if (config.readertype == READER_MFRC522)
{
mfrc522Read();
}

else if (config.readertype == READER_WIEGAND)
{
wiegandRead();
}

else if (config.readertype == READER_PN532)
{
pn532Read();
}

else if (config.readertype > READER_PN532)
{
// This is a combination of RDM6300 and one of the above
genericRead();
}
}


/*
* Try and read a PIN code from Wiegand hardware
*/
void pinCodeRead()
{
if (config.readertype != READER_WIEGAND ||
Expand Down Expand Up @@ -288,6 +324,11 @@ int weekdayFromMonday(int weekdayFromSunday) {
return ( weekdayFromSunday + 5 ) % 7;
}


/*
* If we have successfully read an RFID card, check if access
* should be granted
*/
void rfidProcess()
{
if (rfidState == waitingRfid ||
Expand All @@ -296,12 +337,14 @@ void rfidProcess()
return;
}

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

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

/*
* Read the user's settings
*/
size_t size = f.size();
std::unique_ptr<char[]> buf(new char[size]);
f.readBytes(buf.get(), size);
f.close();
DynamicJsonDocument json(512);
auto error = deserializeJson(json, buf.get(), size);

/*
* Corrupt user data file, so return invalid user
*/
if (error)
{
processingState = notValid;
Expand All @@ -348,24 +397,32 @@ void rfidProcess()
return;
}

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

#ifdef DEBUG
Serial.println(" = known PICC");
Serial.print("[ INFO ] User Name: ");
Serial.print("[ INFO ] User Name: '");
if (username == "undefined")
Serial.print(uid);
else
Serial.print(username);
Serial.print("'");
#endif

if (accountType == ACCESS_GRANTED)
{
/*
* Normal user - relay but no admin access
*/
unsigned long validSinceL = json["validsince"];
unsigned long validUntilL = json["validuntil"];
unsigned long nowL = epoch;
int hourTz = timeinfo.tm_hour;

if (validUntilL < nowL || validSinceL > nowL)
{
processingState = expired;
Expand All @@ -378,9 +435,15 @@ void rfidProcess()
}
} else if (accountType == ACCESS_ADMIN)
{
/*
* Admin user - enable relay (with no time limits) and wifi
*/
doEnableWifi = true;
processingState = validAdmin;
} else {
/*
* User exists but does not have access
*/
processingState = notValid;
}

Expand Down