@@ -163,8 +163,16 @@ void pn532Read()
163
163
}
164
164
}
165
165
166
+
167
+ /*
168
+ * Try first to read from RDM6300 hardware. If that received
169
+ * nothing, check the other configured reader.
170
+ */
166
171
void genericRead()
167
172
{
173
+ /*
174
+ * Test RDM6300 125khz reader
175
+ */
168
176
while (Serial.available() > 0)
169
177
{
170
178
RFIDr.rfidSerial(Serial.read());
@@ -181,46 +189,74 @@ void genericRead()
181
189
#endif
182
190
}
183
191
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
+ }
188
200
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
+ }
193
205
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
+ }
197
210
}
198
211
}
199
212
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
+ */
200
220
void rfidRead()
201
221
{
222
+ /*
223
+ * Do not try and read if we are already processing a card
224
+ */
202
225
if (rfidState == cardSwiped)
203
226
{
204
227
return;
205
228
}
229
+
230
+ /*
231
+ * Call the appropriate function based on the configured
232
+ * hardware
233
+ */
206
234
if (config.readertype == READER_MFRC522)
207
235
{
208
236
mfrc522Read();
209
237
}
238
+
210
239
else if (config.readertype == READER_WIEGAND)
211
240
{
212
241
wiegandRead();
213
242
}
243
+
214
244
else if (config.readertype == READER_PN532)
215
245
{
216
246
pn532Read();
217
247
}
248
+
218
249
else if (config.readertype > READER_PN532)
219
250
{
251
+ // This is a combination of RDM6300 and one of the above
220
252
genericRead();
221
253
}
222
254
}
223
255
256
+
257
+ /*
258
+ * Try and read a PIN code from Wiegand hardware
259
+ */
224
260
void pinCodeRead()
225
261
{
226
262
if (config.readertype != READER_WIEGAND ||
@@ -288,6 +324,11 @@ int weekdayFromMonday(int weekdayFromSunday) {
288
324
return ( weekdayFromSunday + 5 ) % 7;
289
325
}
290
326
327
+
328
+ /*
329
+ * If we have successfully read an RFID card, check if access
330
+ * should be granted
331
+ */
291
332
void rfidProcess()
292
333
{
293
334
if (rfidState == waitingRfid ||
@@ -296,12 +337,14 @@ void rfidProcess()
296
337
return;
297
338
}
298
339
340
+ /* Each user has a file named after the RFID UID */
299
341
File f = SPIFFS.open("/P/" + uid, "r");
300
342
301
343
/*
302
344
* 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.
305
348
*/
306
349
if (!f)
307
350
{
@@ -320,13 +363,19 @@ void rfidProcess()
320
363
#endif
321
364
}
322
365
366
+ /*
367
+ * Read the user's settings
368
+ */
323
369
size_t size = f.size();
324
370
std::unique_ptr<char[]> buf(new char[size]);
325
371
f.readBytes(buf.get(), size);
326
372
f.close();
327
373
DynamicJsonDocument json(512);
328
374
auto error = deserializeJson(json, buf.get(), size);
329
375
376
+ /*
377
+ * Corrupt user data file, so return invalid user
378
+ */
330
379
if (error)
331
380
{
332
381
processingState = notValid;
@@ -348,24 +397,32 @@ void rfidProcess()
348
397
return;
349
398
}
350
399
400
+ /*
401
+ * Get account type (for FIRST relay only) and username from user's data
402
+ */
351
403
accountType = json["acctype"];
352
404
username = json["user"].as<String>();
353
405
354
406
#ifdef DEBUG
355
407
Serial.println(" = known PICC");
356
- Serial.print("[ INFO ] User Name: ");
408
+ Serial.print("[ INFO ] User Name: ' ");
357
409
if (username == "undefined")
358
410
Serial.print(uid);
359
411
else
360
412
Serial.print(username);
413
+ Serial.print("'");
361
414
#endif
362
415
363
416
if (accountType == ACCESS_GRANTED)
364
417
{
418
+ /*
419
+ * Normal user - relay but no admin access
420
+ */
365
421
unsigned long validSinceL = json["validsince"];
366
422
unsigned long validUntilL = json["validuntil"];
367
423
unsigned long nowL = epoch;
368
424
int hourTz = timeinfo.tm_hour;
425
+
369
426
if (validUntilL < nowL || validSinceL > nowL)
370
427
{
371
428
processingState = expired;
@@ -378,9 +435,15 @@ void rfidProcess()
378
435
}
379
436
} else if (accountType == ACCESS_ADMIN)
380
437
{
438
+ /*
439
+ * Admin user - enable relay (with no time limits) and wifi
440
+ */
381
441
doEnableWifi = true;
382
442
processingState = validAdmin;
383
443
} else {
444
+ /*
445
+ * User exists but does not have access
446
+ */
384
447
processingState = notValid;
385
448
}
386
449
0 commit comments