-
Notifications
You must be signed in to change notification settings - Fork 0
/
NfcAuthenticator.cpp
54 lines (41 loc) · 1.14 KB
/
NfcAuthenticator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#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;
logger.debug("NFC tag detected, NUID: %02hhX %02hhX %02hhX %02hhX", nuid[0], nuid[1], nuid[2], nuid[3]);
releaseCard();
return MifareClassicKey(rfid.uid.uidByte);
}
bool NfcAuthenticator::initializeCard()
{
rfid.PCD_Init();
if (!rfid.PICC_IsNewCardPresent())
return false;
if (!rfid.PICC_ReadCardSerial())
return false;
auto piccType = rfid.PICC_GetType(rfid.uid.sak);
if (piccType == MFRC522::PICC_TYPE_NOT_COMPLETE)
{
logger.warning("Incomplete UID detected in SAK procedure");
return false;
}
logger.debug("Tag type: %d", static_cast<int>(piccType));
return true;
}
void NfcAuthenticator::releaseCard()
{
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}