2017-12-17 11:31:13 +00:00
|
|
|
#include "NfcAuthenticator.h"
|
|
|
|
|
|
|
|
#include "ILogger.h"
|
|
|
|
#include <SPI.h>
|
|
|
|
|
|
|
|
NfcAuthenticator::NfcAuthenticator(int nfcSlaveSelectPin, int nfcResetPin, const ILogger &logger):
|
|
|
|
rfid(nfcSlaveSelectPin, nfcResetPin),
|
|
|
|
logger(logger)
|
|
|
|
{
|
|
|
|
SPI.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
NfcAuthenticator::~NfcAuthenticator()
|
|
|
|
{}
|
|
|
|
|
|
|
|
Key NfcAuthenticator::getKey()
|
|
|
|
{
|
|
|
|
if(!initializeCard())
|
|
|
|
return Key();
|
|
|
|
|
|
|
|
uint8_t *nuid = rfid.uid.uidByte;
|
2018-03-14 22:21:13 +00:00
|
|
|
logger.debug("NFC tag detected, NUID: %02hhX %02hhX %02hhX %02hhX", nuid[0], nuid[1], nuid[2], nuid[3]);
|
2017-12-17 11:31:13 +00:00
|
|
|
|
|
|
|
releaseCard();
|
|
|
|
|
|
|
|
return MifareClassicKey(rfid.uid.uidByte);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NfcAuthenticator::initializeCard()
|
|
|
|
{
|
2018-03-28 20:38:45 +00:00
|
|
|
rfid.PCD_Init();
|
|
|
|
|
2017-12-17 11:31:13 +00:00
|
|
|
if (!rfid.PICC_IsNewCardPresent())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!rfid.PICC_ReadCardSerial())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto piccType = rfid.PICC_GetType(rfid.uid.sak);
|
2018-03-14 22:21:13 +00:00
|
|
|
if (piccType == MFRC522::PICC_TYPE_NOT_COMPLETE)
|
2017-12-17 11:31:13 +00:00
|
|
|
{
|
2018-03-14 22:21:13 +00:00
|
|
|
logger.warning("Incomplete UID detected in SAK procedure");
|
2017-12-17 11:31:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-14 22:21:13 +00:00
|
|
|
logger.debug("Tag type: %d", static_cast<int>(piccType));
|
2017-12-17 11:31:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NfcAuthenticator::releaseCard()
|
|
|
|
{
|
|
|
|
rfid.PICC_HaltA();
|
|
|
|
rfid.PCD_StopCrypto1();
|
|
|
|
}
|