Skip to content

Commit

Permalink
Add method to retrive ATQA and SAK
Browse files Browse the repository at this point in the history
  • Loading branch information
andreock committed Dec 27, 2024
1 parent e1a8daf commit de8c452
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
48 changes: 43 additions & 5 deletions Adafruit_PN532.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,41 @@ bool Adafruit_PN532::setPassiveActivationRetries(uint8_t maxRetries) {

/***** ISO14443A Commands ******/

/**************************************************************************/
/*!
@brief Waits for an ISO14443A target to enter the field and reads
its ID.
@param cardbaudrate Baud rate of the card
@param uid Pointer to the array that will be populated
with the card's UID (up to 7 bytes)
@param uidLength Pointer to the variable that will hold the
length of the card's UID.
@param atqa Pointer to the variable that will hold the
ATQA of the card
@param sak Pointer to the variable that will hold the
SAK of the card
@param timeout Timeout in milliseconds.
@return 1 if everything executed properly, 0 for an error
*/
/**************************************************************************/
bool Adafruit_PN532::readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid,
uint8_t *uidLength, uint16_t *atqa, uint8_t *sak, uint16_t timeout) {
pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
pn532_packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later)
pn532_packetbuffer[2] = cardbaudrate;

if (!sendCommandCheckAck(pn532_packetbuffer, 3, timeout)) {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("No card(s) read"));
#endif
return 0x0; // no cards read
}

return readDetectedPassiveTargetID(uid, uidLength, atqa, sak);
}

/**************************************************************************/
/*!
@brief Waits for an ISO14443A target to enter the field and reads
Expand Down Expand Up @@ -566,8 +601,10 @@ bool Adafruit_PN532::readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid,
#endif
return 0x0; // no cards read
}
uint16_t atqa;
uint8_t sak;

return readDetectedPassiveTargetID(uid, uidLength);
return readDetectedPassiveTargetID(uid, uidLength, &atqa, &sak);
}

/**************************************************************************/
Expand Down Expand Up @@ -599,7 +636,7 @@ bool Adafruit_PN532::startPassiveTargetIDDetection(uint8_t cardbaudrate) {
*/
/**************************************************************************/
bool Adafruit_PN532::readDetectedPassiveTargetID(uint8_t *uid,
uint8_t *uidLength) {
uint8_t *uidLength, uint16_t *atqa, uint8_t *sak) {
// read data packet
readdata(pn532_packetbuffer, 20);
// check some basic stuff
Expand All @@ -624,9 +661,10 @@ bool Adafruit_PN532::readDetectedPassiveTargetID(uint8_t *uid,
if (pn532_packetbuffer[7] != 1)
return 0;

uint16_t sens_res = pn532_packetbuffer[9];
sens_res <<= 8;
sens_res |= pn532_packetbuffer[10];
*atqa = pn532_packetbuffer[9];
*atqa <<= 8;
*atqa |= pn532_packetbuffer[10];
*sak = pn532_packetbuffer[11];
#ifdef MIFAREDEBUG
PN532DEBUGPRINT.print(F("ATQA: 0x"));
PN532DEBUGPRINT.println(sens_res, HEX);
Expand Down
5 changes: 3 additions & 2 deletions Adafruit_PN532.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,11 @@ class Adafruit_PN532 {

// ISO14443A functions
bool readPassiveTargetID(
uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength,
uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, uint16_t *atqa, uint8_t *sak,
uint16_t timeout = 0); // timeout 0 means no timeout - will block forever.
bool readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, uint16_t timeout = 0);
bool startPassiveTargetIDDetection(uint8_t cardbaudrate);
bool readDetectedPassiveTargetID(uint8_t *uid, uint8_t *uidLength);
bool readDetectedPassiveTargetID(uint8_t *uid, uint8_t *uidLength, uint16_t *atqa, uint8_t *sak);
bool inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response,
uint8_t *responseLength);
bool inListPassiveTarget();
Expand Down

0 comments on commit de8c452

Please sign in to comment.