Skip to content

Commit 56c0fb9

Browse files
authored
Merge pull request #625 from mcnewton/uid-v1-compat
Ensure users are compatible between v1 and v2
2 parents 9e970bf + e9ffc6e commit 56c0fb9

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/rfid.esp

+36-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ String currentInput = "";
44
String pinCode = "";
55
String type = "";
66
String uid = "";
7+
String v1uid = "";
78
String username = "";
89
bool wiegandAvailable = false;
910

@@ -110,21 +111,29 @@ void mfrc522Read()
110111
}
111112
mfrc522.PICC_HaltA();
112113
cooldown = millis() + COOLDOWN_MILIS;
113-
#ifdef DEBUG
114-
Serial.print(F("[ INFO ] PICC's UID: "));
115-
#endif
114+
115+
/*
116+
* Convert RC522 UID into string
117+
* esp-rfid v1 had a bug where the UID string may miss some '0's. To
118+
* maintain compatibility, calculate incorrect UID here as well for
119+
* later checking in case old users exist in the config.
120+
*/
116121
for (byte i = 0; i < mfrc522.uid.size; i++)
117-
{
118-
uid+=(String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""));
119-
uid+=(String(mfrc522.uid.uidByte[i], HEX));
120-
}
122+
{
123+
uid+=(String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""));
124+
uid+=(String(mfrc522.uid.uidByte[i], HEX));
125+
v1uid+=(String(mfrc522.uid.uidByte[i], HEX));
126+
}
121127
rfidState = cardSwiped;
122128

123129
#ifdef DEBUG
130+
Serial.print(F("[ INFO ] PICC's UID: "));
124131
Serial.print(uid);
125132
#endif
133+
126134
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
127135
type = mfrc522.PICC_GetTypeName(piccType);
136+
128137
#ifdef DEBUG
129138
Serial.print(" " + type);
130139
#endif
@@ -288,10 +297,27 @@ void rfidProcess()
288297
}
289298

290299
File f = SPIFFS.open("/P/" + uid, "r");
300+
301+
/*
302+
* 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.
305+
*/
291306
if (!f)
292307
{
293-
processingState = unknown;
294-
return;
308+
/* Test to see if there was a uid in v1 format */
309+
f = SPIFFS.open("/P/" + v1uid, "r");
310+
if (!f)
311+
{
312+
processingState = unknown;
313+
return;
314+
}
315+
uid = v1uid;
316+
#ifdef DEBUG
317+
Serial.print(" (found uid in v1 format: ");
318+
Serial.print(v1uid);
319+
Serial.print(")");
320+
#endif
295321
}
296322

297323
size_t size = f.size();
@@ -494,6 +520,7 @@ void cleanRfidLoop()
494520
currentInput = "";
495521
type = "";
496522
uid = "";
523+
v1uid = "";
497524
rfidState = waitingRfid;
498525
processingState = waitingProcessing;
499526
ledWaitingOff();

0 commit comments

Comments
 (0)