Skip to content
This repository has been archived by the owner on Jan 24, 2019. It is now read-only.

Commit

Permalink
Changes to inlist targets and exchange data.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Williams Jnr committed Oct 17, 2012
1 parent ab541df commit c4b9624
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 2 deletions.
155 changes: 153 additions & 2 deletions Adafruit_NFCShield_I2C.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void Adafruit_NFCShield_I2C::PrintHex(const byte * data, const uint32_t numBytes
// Append leading 0 for small values
if (data[szPos] <= 0xF)
Serial.print("0");
Serial.print(data[szPos], HEX);
Serial.print(data[szPos]&0xff, HEX);
if ((numBytes > 1) && (szPos != numBytes - 1))
{
Serial.print(" ");
Expand Down Expand Up @@ -178,7 +178,7 @@ void Adafruit_NFCShield_I2C::PrintHexChar(const byte * data, const uint32_t numB
if (data[szPos] <= 0x1F)
Serial.print(".");
else
Serial.print(data[szPos]);
Serial.print((char)data[szPos]);
}
Serial.println("");
}
Expand Down Expand Up @@ -1032,3 +1032,154 @@ void Adafruit_NFCShield_I2C::wiresendcommand(uint8_t* cmd, uint8_t cmdlen) {
#endif
}

boolean Adafruit_NFCShield_I2C::waitUntilReady(uint16_t timeout) {
uint16_t timer = 0;
while(wirereadstatus() != PN532_I2C_READY) {
if (timeout != 0) {
timer += 10;
if (timer > timeout) {
return false;
}
}
delay(10);
}
return true;
}

boolean Adafruit_NFCShield_I2C::inDataExchange(uint8_t * send, uint8_t sendLength, uint8_t * response, uint8_t * responseLength) {
if (sendLength > PN532_PACKBUFFSIZ -2) {
#ifdef PN532DEBUG
Serial.println("APDU length too long for packet buffer");
#endif
return false;
}
uint8_t i;

pn532_packetbuffer[0] = 0x40; // PN532_COMMAND_INDATAEXCHANGE;
pn532_packetbuffer[1] = inListedTag;
for (i=0; i<sendLength; ++i) {
pn532_packetbuffer[i+2] = send[i];
}

if (!sendCommandCheckAck(pn532_packetbuffer,sendLength+2,1000)) {
#ifdef PN532DEBUG
Serial.println("Could not send ADPU");
#endif
return false;
}

if (!waitUntilReady(1000)) {
#ifdef PN532DEBUG
Serial.println("Response never received for ADPU...");
#endif
return false;
}

wirereaddata(pn532_packetbuffer,sizeof(pn532_packetbuffer));

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)) {
#ifdef PN532DEBUG
Serial.println("Length check invalid");
Serial.println(length,HEX);
Serial.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) {
#ifdef PN532DEBUG
Serial.println("Status code indicates an error");
#endif
return false;
}

length -= 3;

if (length > *responseLength) {
length = *responseLength; // silent truncation...
}

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

return true;
}
else {
Serial.print("Don't know how to handle this command: ");
Serial.println(pn532_packetbuffer[6],HEX);
return false;
}
}
else {
Serial.println("Preamble missing");
return false;
}
}

boolean Adafruit_NFCShield_I2C::inListPassiveTarget() {
pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
pn532_packetbuffer[1] = 1;
pn532_packetbuffer[2] = 0;

#ifdef PN532DEBUG
Serial.print("About to inList passive target");
#endif

if (!sendCommandCheckAck(pn532_packetbuffer,3,1000)) {
#ifdef PN532DEBUG
Serial.println("Could not send inlist message");
#endif
return false;
}

if (!waitUntilReady(30000)) {
return false;
}

wirereaddata(pn532_packetbuffer,sizeof(pn532_packetbuffer));

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)) {
#ifdef PN532DEBUG
Serial.println("Length check invalid");
Serial.println(length,HEX);
Serial.println((~length)+1,HEX);
#endif
return false;
}
if (pn532_packetbuffer[5]==PN532_PN532TOHOST && pn532_packetbuffer[6]==PN532_RESPONSE_INLISTPASSIVETARGET) {
if (pn532_packetbuffer[7] != 1) {
#ifdef PN532DEBUG
Serial.println("Unhandled number of targets inlisted");
#endif
Serial.println("Number of tags inlisted:");
Serial.println(pn532_packetbuffer[7]);
return false;
}

inListedTag = pn532_packetbuffer[8];
Serial.print("Tag number: ");
Serial.println(inListedTag);

return true;
} else {
#ifdef PN532DEBUG
Serial.print("Unexpected response to inlist passive host");
#endif
return false;
}
}
else {
#ifdef PN532DEBUG
Serial.println("Preamble missing");
#endif
return false;
}

return true;
}
9 changes: 9 additions & 0 deletions Adafruit_NFCShield_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#define PN532_POSTAMBLE (0x00)

#define PN532_HOSTTOPN532 (0xD4)
#define PN532_PN532TOHOST (0xD5)

// PN532 Commands
#define PN532_COMMAND_DIAGNOSE (0x00)
Expand Down Expand Up @@ -75,6 +76,10 @@
#define PN532_COMMAND_TGRESPONSETOINITIATOR (0x90)
#define PN532_COMMAND_TGGETTARGETSTATUS (0x8A)

#define PN532_RESPONSE_INDATAEXCHANGE (0x41)
#define PN532_RESPONSE_INLISTPASSIVETARGET (0x4B)


#define PN532_WAKEUP (0x55)

#define PN532_SPI_STATREAD (0x02)
Expand Down Expand Up @@ -160,7 +165,9 @@ class Adafruit_NFCShield_I2C{
boolean setPassiveActivationRetries(uint8_t maxRetries);

// ISO14443A functions
boolean inListPassiveTarget();
boolean readPassiveTargetID(uint8_t cardbaudrate, uint8_t * uid, uint8_t * uidLength);
boolean inDataExchange(uint8_t * send, uint8_t sendLength, uint8_t * response, uint8_t * responseLength);

// Mifare Classic functions
bool mifareclassic_IsFirstBlock (uint32_t uiBlock);
Expand All @@ -183,9 +190,11 @@ class Adafruit_NFCShield_I2C{
uint8_t _uid[7]; // ISO14443A uid
uint8_t _uidLen; // uid len
uint8_t _key[6]; // Mifare Classic key
uint8_t inListedTag; // Tg number of inlisted tag.

boolean readackframe(void);
uint8_t wirereadstatus(void);
void wirereaddata(uint8_t* buff, uint8_t n);
void wiresendcommand(uint8_t* cmd, uint8_t cmdlen);
boolean waitUntilReady(uint16_t timeout);
};

0 comments on commit c4b9624

Please sign in to comment.