Skip to content

Commit

Permalink
Added support for extended frames, fixes adafruit#38 adafruit#39 #5
Browse files Browse the repository at this point in the history
Added support for extended frames, fixes adafruit#38 adafruit#39 #5
  • Loading branch information
solhuebner authored Aug 21, 2020
1 parent 12d6cf7 commit 8ddae6f
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions Adafruit_PN532.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
@section HISTORY
v2.2 - Added support for extended frames
v2.1 - Added NTAG2xx helper functions
v2.0 - Refactored to add I2C support from Adafruit_NFCShield_I2C library.
Expand Down Expand Up @@ -598,7 +600,7 @@ bool Adafruit_PN532::readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid,
/**************************************************************************/
bool Adafruit_PN532::inDataExchange(uint8_t *send, uint8_t sendLength,
uint8_t *response,
uint8_t *responseLength) {
uint16_t *responseLength) {
if (sendLength > PN532_PACKBUFFSIZ - 2) {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("APDU length too long for packet buffer"));
Expand All @@ -611,7 +613,7 @@ bool Adafruit_PN532::inDataExchange(uint8_t *send, uint8_t sendLength,
#endif
return false;
}
uint8_t i;
uint16_t i;

pn532_packetbuffer[0] = 0x40; // PN532_COMMAND_INDATAEXCHANGE;
pn532_packetbuffer[1] = _inListedTag;
Expand All @@ -637,18 +639,33 @@ bool Adafruit_PN532::inDataExchange(uint8_t *send, uint8_t sendLength,

if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 &&
pn532_packetbuffer[2] == 0xff) {
uint8_t length = pn532_packetbuffer[3];
if (pn532_packetbuffer[4] != (uint8_t)(~length + 1)) {

uint16_t length;
uint8_t checksumOffset;
uint8_t lengthChecksum;
if (pn532_packetbuffer[3] == PN532_EXTENDED_FRAME_FIXED_VALUE &&
pn532_packetbuffer[4] == PN532_EXTENDED_FRAME_FIXED_VALUE) {
//It's an extended frame
length = (((uint16_t) pn532_packetbuffer[5]) << 8) | pn532_packetbuffer[6];
lengthChecksum = (uint8_t)(~(pn532_packetbuffer[5]+pn532_packetbuffer[6])+1);
checksumOffset = 3; //length is encoded in three more bytes (two fixed values 0xFF and an additional octet for the length itself)
} else {
length = pn532_packetbuffer[3];
lengthChecksum = (uint8_t)(~length+1);
checksumOffset = 0;
}

if (pn532_packetbuffer[checksumOffset+4]!=lengthChecksum) {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("Length check invalid"));
PN532DEBUGPRINT.println(length, HEX);
PN532DEBUGPRINT.println((~length) + 1, HEX);
#endif
return false;
}
if (pn532_packetbuffer[5] == PN532_PN532TOHOST &&
pn532_packetbuffer[6] == PN532_RESPONSE_INDATAEXCHANGE) {
if ((pn532_packetbuffer[7] & 0x3f) != 0) {
if (pn532_packetbuffer[checksumOffset+5]==PN532_PN532TOHOST &&
pn532_packetbuffer[checksumOffset+6]==PN532_RESPONSE_INDATAEXCHANGE) {
if ((pn532_packetbuffer[checksumOffset+7] & 0x3f)!=0) {
#ifdef PN532DEBUG
PN532DEBUGPRINT.println(F("Status code indicates an error"));
#endif
Expand All @@ -662,7 +679,7 @@ bool Adafruit_PN532::inDataExchange(uint8_t *send, uint8_t sendLength,
}

for (i = 0; i < length; ++i) {
response[i] = pn532_packetbuffer[8 + i];
response[i] = pn532_packetbuffer[checksumOffset+8+i];
}
*responseLength = length;

Expand Down Expand Up @@ -1580,7 +1597,7 @@ bool Adafruit_PN532::waitready(uint16_t timeout) {
@param n Number of bytes to be read
*/
/**************************************************************************/
void Adafruit_PN532::readdata(uint8_t *buff, uint8_t n) {
void Adafruit_PN532::readdata(uint8_t *buff, uint16_t n) {
if (spi_dev) {
uint8_t cmd = PN532_SPI_DATAREAD;

Expand All @@ -1604,10 +1621,10 @@ void Adafruit_PN532::readdata(uint8_t *buff, uint8_t n) {
PN532DEBUGPRINT.print(F("Reading: "));
#endif
// Start read (n+1 to take into account leading 0x01 with I2C)
WIRE.requestFrom((uint8_t)PN532_I2C_ADDRESS, (uint8_t)(n + 2));
WIRE.requestFrom((uint8_t)PN532_I2C_ADDRESS, (uint16_t)(n + 2));
// Discard the leading 0x01
i2c_recv();
for (uint8_t i = 0; i < n; i++) {
for (uint16_t i = 0; i < n; i++) {
delay(1);
buff[i] = i2c_recv();
#ifdef PN532DEBUG
Expand Down

0 comments on commit 8ddae6f

Please sign in to comment.