A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol
C arduinobluetoothcan-busdebuggingeepromesp32flipperzerogpiohardware-hackingi2cjtagopenocdprotocolspwmradiorfidspisubghzuartwifi
add pn532 lib
Geoe7e31db164c2
diff --git a/lib/PN532/Adafruit_I2CDevice.cpp b/lib/PN532/Adafruit_I2CDevice.cppnew file mode 100644index 00000000..f98ec5b7--- /dev/null+++ b/lib/PN532/Adafruit_I2CDevice.cpp@@ -0,0 +1,341 @@+#include "Adafruit_I2CDevice.h"++//#define DEBUG_SERIAL Serial++/*!+ * @brief Create an I2C device at a given address+ * @param addr The 7-bit I2C address for the device+ * @param theWire The I2C bus to use, defaults to &Wire+ */+Adafruit_I2CDevice::Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire) {+ _addr = addr;+ _wire = theWire;+ _begun = false;+ _sdaPin = -1;+ _sclPin = -1;+#ifdef ARDUINO_ARCH_SAMD+ _maxBufferSize = 250; // as defined in Wire.h's RingBuffer+#elif defined(ESP32)+ _maxBufferSize = I2C_BUFFER_LENGTH;+#else+ _maxBufferSize = 32;+#endif+}++/*!+ * @brief Create an I2C device at a given address+ * @param addr The 7-bit I2C address for the device+ * @param sdaPin The I2C SDA pin to use+ * @param sclPin The I2C SCL pin to use+ * @param theWire The I2C bus to use, defaults to &Wire+ */+Adafruit_I2CDevice::Adafruit_I2CDevice(uint8_t addr, int sdaPin, int sclPin, TwoWire *theWire) {+ _addr = addr;+ _wire = theWire;+ _begun = false;+ _sdaPin = sdaPin;+ _sclPin = sclPin;+#ifdef ARDUINO_ARCH_SAMD+ _maxBufferSize = 250; // as defined in Wire.h's RingBuffer+#elif defined(ESP32)+ _maxBufferSize = I2C_BUFFER_LENGTH;+#else+ _maxBufferSize = 32;+#endif+}++/*!+ * @brief Initializes and does basic address detection+ * @param addr_detect Whether we should attempt to detect the I2C address+ * with a scan. 99% of sensors/devices don't mind, but once in a while they+ * don't respond well to a scan!+ * @return True if I2C initialized and a device with the addr found+ */+bool Adafruit_I2CDevice::begin(bool addr_detect) {+ _wire->begin(_sdaPin, _sclPin);+ _begun = true;++ if (addr_detect) {+ return detected();+ }+ return true;+}++/*!+ * @brief De-initialize device, turn off the Wire interface+ */+void Adafruit_I2CDevice::end(void) {+ // Not all port implement Wire::end(), such as+ // - ESP8266+ // - AVR core without WIRE_HAS_END+ // - ESP32: end() is implemented since 2.0.1 which is latest at the moment.+ // Temporarily disable for now to give time for user to update.+#if !(defined(ESP8266) || \+ (defined(ARDUINO_ARCH_AVR) && !defined(WIRE_HAS_END)) || \+ defined(ARDUINO_ARCH_ESP32))+ _wire->end();+ _begun = false;+#endif+}++/*!+ * @brief Scans I2C for the address - note will give a false-positive+ * if there's no pullups on I2C+ * @return True if I2C initialized and a device with the addr found+ */+bool Adafruit_I2CDevice::detected(void) {+ // Init I2C if not done yet+ if (!_begun && !begin()) {+ return false;+ }++ // A basic scanner, see if it ACK's+ _wire->beginTransmission(_addr);+#ifdef DEBUG_SERIAL+ DEBUG_SERIAL.print(F("Address 0x"));+ DEBUG_SERIAL.print(_addr);+#endif+ if (_wire->endTransmission() == 0) {+#ifdef DEBUG_SERIAL+ DEBUG_SERIAL.println(F(" Detected"));+#endif+ return true;+ }+#ifdef DEBUG_SERIAL+ DEBUG_SERIAL.println(F(" Not detected"));+#endif+ return false;+}++/*!+ * @brief Write a buffer or two to the I2C device. Cannot be more than+ * maxBufferSize() bytes.+ * @param buffer Pointer to buffer of data to write. This is const to+ * ensure the content of this buffer doesn't change.+ * @param len Number of bytes from buffer to write+ * @param prefix_buffer Pointer to optional array of data to write before+ * buffer. Cannot be more than maxBufferSize() bytes. This is const to+ * ensure the content of this buffer doesn't change.+ * @param prefix_len Number of bytes from prefix buffer to write+ * @param stop Whether to send an I2C STOP signal on write+ * @return True if write was successful, otherwise false.+ */+bool Adafruit_I2CDevice::write(const uint8_t *buffer, size_t len, bool stop,+ const uint8_t *prefix_buffer,+ size_t prefix_len) {+ if ((len + prefix_len) > maxBufferSize()) {+ // currently not guaranteed to work if more than 32 bytes!+ // we will need to find out if some platforms have larger+ // I2C buffer sizes :/+#ifdef DEBUG_SERIAL+ DEBUG_SERIAL.println(F("\tI2CDevice could not write such a large buffer"));+#endif+ return false;+ }++ _wire->beginTransmission(_addr);++ // Write the prefix data (usually an address)+ if ((prefix_len != 0) && (prefix_buffer != nullptr)) {+ if (_wire->write(prefix_buffer, prefix_len) != prefix_len) {+#ifdef DEBUG_SERIAL+ DEBUG_SERIAL.println(F("\tI2CDevice failed to write"));+#endif+ return false;+ }+ }++ // Write the data itself+ if (_wire->write(buffer, len) != len) {+#ifdef DEBUG_SERIAL+ DEBUG_SERIAL.println(F("\tI2CDevice failed to write"));+#endif+ return false;+ }++#ifdef DEBUG_SERIAL++ DEBUG_SERIAL.print(F("\tI2CWRITE @ 0x"));+ DEBUG_SERIAL.print(_addr, HEX);+ DEBUG_SERIAL.print(F(" :: "));+ if ((prefix_len != 0) && (prefix_buffer != nullptr)) {+ for (uint16_t i = 0; i < prefix_len; i++) {+ DEBUG_SERIAL.print(F("0x"));+ DEBUG_SERIAL.print(prefix_buffer[i], HEX);+ DEBUG_SERIAL.print(F(", "));+ }+ }+ for (uint16_t i = 0; i < len; i++) {+ DEBUG_SERIAL.print(F("0x"));+ DEBUG_SERIAL.print(buffer[i], HEX);+ DEBUG_SERIAL.print(F(", "));+ if (i % 32 == 31) {+ DEBUG_SERIAL.println();+ }+ }++ if (stop) {+ DEBUG_SERIAL.print("\tSTOP");+ }+#endif++ if (_wire->endTransmission(stop) == 0) {+#ifdef DEBUG_SERIAL+ DEBUG_SERIAL.println();+ // DEBUG_SERIAL.println("Sent!");+#endif+ return true;+ } else {+#ifdef DEBUG_SERIAL+ DEBUG_SERIAL.println("\tFailed to send!");+#endif+ return false;+ }+}++/*!+ * @brief Read from I2C into a buffer from the I2C device.+ * Cannot be more than maxBufferSize() bytes.+ * @param buffer Pointer to buffer of data to read into+ * @param len Number of bytes from buffer to read.+ * @param stop Whether to send an I2C STOP signal on read+ * @return True if read was successful, otherwise false.+ */+bool Adafruit_I2CDevice::read(uint8_t *buffer, size_t len, bool stop) {+ size_t pos = 0;+ while (pos < len) {+ size_t read_len =+ ((len - pos) > maxBufferSize()) ? maxBufferSize() : (len - pos);+ bool read_stop = (pos < (len - read_len)) ? false : stop;+ if (!_read(buffer + pos, read_len, read_stop))+ return false;+ pos += read_len;+ }+ return true;+}++bool Adafruit_I2CDevice::_read(uint8_t *buffer, size_t len, bool stop) {+#if defined(TinyWireM_h)+ size_t recv = _wire->requestFrom((uint8_t)_addr, (uint8_t)len);+#elif defined(ARDUINO_ARCH_MEGAAVR)+ size_t recv = _wire->requestFrom(_addr, len, stop);+#else+ size_t recv = _wire->requestFrom((uint8_t)_addr, (uint8_t)len, (uint8_t)stop);+#endif++ if (recv != len) {+ // Not enough data available to fulfill our obligation!+#ifdef DEBUG_SERIAL+ DEBUG_SERIAL.print(F("\tI2CDevice did not receive enough data: "));+ DEBUG_SERIAL.println(recv);+#endif+ return false;+ }++ for (uint16_t i = 0; i < len; i++) {+ buffer[i] = _wire->read();+ }++#ifdef DEBUG_SERIAL+ DEBUG_SERIAL.print(F("\tI2CREAD @ 0x"));+ DEBUG_SERIAL.print(_addr, HEX);+ DEBUG_SERIAL.print(F(" :: "));+ for (uint16_t i = 0; i < len; i++) {+ DEBUG_SERIAL.print(F("0x"));+ DEBUG_SERIAL.print(buffer[i], HEX);+ DEBUG_SERIAL.print(F(", "));+ if (len % 32 == 31) {+ DEBUG_SERIAL.println();+ }+ }+ DEBUG_SERIAL.println();+#endif++ return true;+}++/*!+ * @brief Write some data, then read some data from I2C into another buffer.+ * Cannot be more than maxBufferSize() bytes. The buffers can point to+ * same/overlapping locations.+ * @param write_buffer Pointer to buffer of data to write from+ * @param write_len Number of bytes from buffer to write.+ * @param read_buffer Pointer to buffer of data to read into.+ * @param read_len Number of bytes from buffer to read.+ * @param stop Whether to send an I2C STOP signal between the write and read+ * @return True if write & read was successful, otherwise false.+ */+bool Adafruit_I2CDevice::write_then_read(const uint8_t *write_buffer,+ size_t write_len, uint8_t *read_buffer,+ size_t read_len, bool stop) {+ if (!write(write_buffer, write_len, stop)) {+ return false;+ }++ return read(read_buffer, read_len);+}++/*!+ * @brief Returns the 7-bit address of this device+ * @return The 7-bit address of this device+ */+uint8_t Adafruit_I2CDevice::address(void) { return _addr; }++/*!+ * @brief Change the I2C clock speed to desired (relies on+ * underlying Wire support!+ * @param desiredclk The desired I2C SCL frequency+ * @return True if this platform supports changing I2C speed.+ * Not necessarily that the speed was achieved!+ */+bool Adafruit_I2CDevice::setSpeed(uint32_t desiredclk) {+#if defined(__AVR_ATmega328__) || \+ defined(__AVR_ATmega328P__) // fix arduino core set clock+ // calculate TWBR correctly++ if ((F_CPU / 18) < desiredclk) {+#ifdef DEBUG_SERIAL+ Serial.println(F("I2C.setSpeed too high."));+#endif+ return false;+ }+ uint32_t atwbr = ((F_CPU / desiredclk) - 16) / 2;+ if (atwbr > 16320) {+#ifdef DEBUG_SERIAL+ Serial.println(F("I2C.setSpeed too low."));+#endif+ return false;+ }++ if (atwbr <= 255) {+ atwbr /= 1;+ TWSR = 0x0;+ } else if (atwbr <= 1020) {+ atwbr /= 4;+ TWSR = 0x1;+ } else if (atwbr <= 4080) {+ atwbr /= 16;+ TWSR = 0x2;+ } else { // if (atwbr <= 16320)+ atwbr /= 64;+ TWSR = 0x3;+ }+ TWBR = atwbr;++#ifdef DEBUG_SERIAL+ Serial.print(F("TWSR prescaler = "));+ Serial.println(pow(4, TWSR));+ Serial.print(F("TWBR = "));+ Serial.println(atwbr);+#endif+ return true;+#elif (ARDUINO >= 157) && !defined(ARDUINO_STM32_FEATHER) && \+ !defined(TinyWireM_h)+ _wire->setClock(desiredclk);+ return true;++#else+ (void)desiredclk;+ return false;+#endif+}diff --git a/lib/PN532/Adafruit_I2CDevice.h b/lib/PN532/Adafruit_I2CDevice.hnew file mode 100644index 00000000..c6eb8e2d--- /dev/null+++ b/lib/PN532/Adafruit_I2CDevice.h@@ -0,0 +1,39 @@+#ifndef Adafruit_I2CDevice_h+#define Adafruit_I2CDevice_h++#include <Arduino.h>+#include <Wire.h>++///< The class which defines how we will talk to this device over I2C+class Adafruit_I2CDevice {+public:+ Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire = &Wire);+ Adafruit_I2CDevice(uint8_t addr, int sdaPin = -1, int sclPin = -1, TwoWire *theWire = &Wire);+ uint8_t address(void);+ bool begin(bool addr_detect = true);+ void end(void);+ bool detected(void);++ bool read(uint8_t *buffer, size_t len, bool stop = true);+ bool write(const uint8_t *buffer, size_t len, bool stop = true,+ const uint8_t *prefix_buffer = nullptr, size_t prefix_len = 0);+ bool write_then_read(const uint8_t *write_buffer, size_t write_len,+ uint8_t *read_buffer, size_t read_len,+ bool stop = false);+ bool setSpeed(uint32_t desiredclk);++ /*! @brief How many bytes we can read in a transaction+ * @return The size of the Wire receive/transmit buffer */+ size_t maxBufferSize() { return _maxBufferSize; }++private:+ int _sdaPin;+ int _sclPin;+ uint8_t _addr;+ TwoWire *_wire;+ bool _begun;+ size_t _maxBufferSize;+ bool _read(uint8_t *buffer, size_t len, bool stop);+};++#endif // Adafruit_I2CDevice_hdiff --git a/lib/PN532/Adafruit_PN532.cpp b/lib/PN532/Adafruit_PN532.cppnew file mode 100644index 00000000..18123e05--- /dev/null+++ b/lib/PN532/Adafruit_PN532.cpp@@ -0,0 +1,2485 @@+/**************************************************************************/+/*!+ @file Adafruit_PN532.cpp++ @section intro_sec Introduction++ Driver for NXP's PN532 NFC/13.56MHz RFID Transceiver++ This is a library for the Adafruit PN532 NFC/RFID breakout boards+ This library works with the Adafruit NFC breakout+ ----> https://www.adafruit.com/products/364++ Check out the links above for our tutorials and wiring diagrams+ These chips use SPI or I2C to communicate.++ Adafruit invests time and resources providing this open source code,+ please support Adafruit and open-source hardware by purchasing+ products from Adafruit!++ @section author Author++ Adafruit Industries++ @section license License++ BSD (see license.txt)++ @section HISTORY++ v2.2 - Added startPassiveTargetIDDetection() to start card detection and+ readDetectedPassiveTargetID() to read it, useful when using the+ IRQ pin.++ v2.1 - Added NTAG2xx helper functions++ v2.0 - Refactored to add I2C support from Adafruit_NFCShield_I2C library.++ v1.4 - Added setPassiveActivationRetries()++ v1.2 - Added writeGPIO()+ - Added readGPIO()++ v1.1 - Changed readPassiveTargetID() to handle multiple UID sizes+ - Added the following helper functions for text display+ static void PrintHex(const byte * data, const uint32_t numBytes)+ static void PrintHexChar(const byte * pbtData, const uint32_t+ numBytes)+ - Added the following Mifare Classic functions:+ bool mifareclassic_IsFirstBlock (uint32_t uiBlock)+ bool mifareclassic_IsTrailerBlock (uint32_t uiBlock)+ uint8_t mifareclassic_AuthenticateBlock (uint8_t * uid, uint8_t+ uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t * keyData) uint8_t+ mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t * data) uint8_t+ mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t * data)+ - Added the following Mifare Ultalight functions:+ uint8_t mifareultralight_ReadPage (uint8_t page, uint8_t * buffer)+*/+/**************************************************************************/++#include "Adafruit_PN532.h"++byte pn532ack[] = {0x00, 0x00, 0xFF,+ 0x00, 0xFF, 0x00}; ///< ACK message from PN532+byte pn532response_firmwarevers[] = {+ 0x00, 0x00, 0xFF,+ 0x06, 0xFA, 0xD5}; ///< Expected firmware version message from PN532++// Uncomment these lines to enable debug output for PN532(SPI) and/or MIFARE+// related code++// #define PN532DEBUG+// #define MIFAREDEBUG++// If using Native Port on Arduino Zero or Due define as SerialUSB+#define PN532DEBUGPRINT Serial ///< Fixed name for debug Serial instance+//#define PN532DEBUGPRINT SerialUSB ///< Fixed name for debug Serial instance++#define PN532_PACKBUFFSIZ 240 ///< Packet buffer size in bytes+byte pn532_packetbuffer[PN532_PACKBUFFSIZ]; ///< Packet buffer used in various+ ///< transactions++Adafruit_PN532::Adafruit_PN532() {}++/**************************************************************************/+/*!+ @brief Instantiates a new PN532 class using software SPI.++ @param clk SPI clock pin (SCK)+ @param miso SPI MISO pin+ @param mosi SPI MOSI pin+ @param ss SPI chip select pin (CS/SSEL)+*/+/**************************************************************************/+Adafruit_PN532::Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi,+ uint8_t ss) {+ _cs = ss;+ spi_dev = new Adafruit_SPIDevice(ss, clk, miso, mosi, 1000000,+ SPI_BITORDER_LSBFIRST, SPI_MODE0);+}++/**************************************************************************/+/*!+ @brief Instantiates a new PN532 class using I2C.++ @param irq Location of the IRQ pin+ @param reset Location of the RSTPD_N pin+ @param theWire pointer to I2C bus to use+*/+/**************************************************************************/+Adafruit_PN532::Adafruit_PN532(uint8_t irq, uint8_t reset, TwoWire *theWire)+ : _irq(irq), _reset(reset) {+ pinMode(_irq, INPUT);+ pinMode(_reset, OUTPUT);+ i2c_dev = new Adafruit_I2CDevice(PN532_I2C_ADDRESS, theWire);+}++/**************************************************************************/+/*!+ @brief Instantiates a new PN532 class using hardware SPI.++ @param ss SPI chip select pin (CS/SSEL)+ @param theSPI pointer to the SPI bus to use+*/+/**************************************************************************/+Adafruit_PN532::Adafruit_PN532(uint8_t ss, SPIClass *theSPI) {+ _cs = ss;+ spi_dev = new Adafruit_SPIDevice(ss, 1000000, SPI_BITORDER_LSBFIRST,+ SPI_MODE0, theSPI);+}++/**************************************************************************/+/*!+ @brief Instantiates a new PN532 class using hardware UART (HSU).++ @param reset Location of the RSTPD_N pin+ @param theSer pointer to HardWare Serial bus to use+*/+/**************************************************************************/+Adafruit_PN532::Adafruit_PN532(uint8_t reset, HardwareSerial *theSer)+ : _reset(reset) {+ pinMode(_reset, OUTPUT);+ ser_dev = theSer;+}++/**************************************************************************/+/*!+ @brief Set PN532 class to use software SPI.++ @param clk SPI clock pin (SCK)+ @param miso SPI MISO pin+ @param mosi SPI MOSI pin+ @param ss SPI chip select pin (CS/SSEL)+*/+/**************************************************************************/+void Adafruit_PN532::setInterface(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t ss) {+ _cs = ss;+ spi_dev = new Adafruit_SPIDevice(ss, clk, miso, mosi, 1000000, SPI_BITORDER_LSBFIRST, SPI_MODE0);+}++/**************************************************************************/+/*!+ @brief Set PN532 class to use I2C.++ @param theWire pointer to I2C bus to use+*/+/**************************************************************************/+void Adafruit_PN532::setInterface(uint8_t sdaPin, uint8_t sclPin) {+ i2c_dev = new Adafruit_I2CDevice(PN532_I2C_ADDRESS, sdaPin, sclPin);+}++/**************************************************************************/+/*!+ @brief Setups the HW++ @returns true if successful, otherwise false+*/+/**************************************************************************/+bool Adafruit_PN532::begin() {+ if (spi_dev) {+ // SPI initialization+ if (!spi_dev->begin()) {+ return false;+ }+ } else if (i2c_dev) {+ // I2C initialization+ // PN532 will fail address check since its asleep, so suppress+ if (!i2c_dev->begin(false)) {+ return false;+ }+ } else if (ser_dev) {+ ser_dev->begin(115200);+ // clear out anything in read buffer+ while (ser_dev->available())+ ser_dev->read();+ } else {+ // no interface specified+ return false;+ }+ reset(); // HW reset - put in known state+ delay(10);+ wakeup(); // hey! wakeup!+ return true;+}++/**************************************************************************/+/*!+ @brief Perform a hardware reset. Requires reset pin to have been provided.+*/+/**************************************************************************/+void Adafruit_PN532::reset(void) {+ // see Datasheet p.209, Fig.48 for timings+ if (_reset != -1) {+ digitalWrite(_reset, LOW);+ delay(1); // min 20ns+ digitalWrite(_reset, HIGH);+ delay(2); // max 2ms+ }+}++/**************************************************************************/+/*!+ @brief Wakeup from LowVbat mode into Normal Mode.+*/+/**************************************************************************/+void Adafruit_PN532::wakeup(void) {+ // interface specific wakeups - each one is unique!+ if (spi_dev) {+ // hold CS low for 2ms+ digitalWrite(_cs, LOW);+ delay(2);+ } else if (ser_dev) {+ uint8_t w[3] = {0x55, 0x00, 0x00};+ ser_dev->write(w, 3);+ delay(2);+ }++ // PN532 will clock stretch I2C during SAMConfig as a "wakeup"++ // need to config SAM to stay in Normal Mode+ SAMConfig();+}++/**************************************************************************/+/*!+ @brief Prints a hexadecimal value in plain characters++ @param data Pointer to the byte data+ @param numBytes Data length in bytes+*/+/**************************************************************************/+void Adafruit_PN532::PrintHex(const byte *data, const uint32_t numBytes) {+ uint32_t szPos;+ for (szPos = 0; szPos < numBytes; szPos++) {+ PN532DEBUGPRINT.print(F("0x"));+ // Append leading 0 for small values+ if (data[szPos] <= 0xF)+ PN532DEBUGPRINT.print(F("0"));+ PN532DEBUGPRINT.print(data[szPos] & 0xff, HEX);+ if ((numBytes > 1) && (szPos != numBytes - 1)) {+ PN532DEBUGPRINT.print(F(" "));+ }+ }+ PN532DEBUGPRINT.println();+}++/**************************************************************************/+/*!+ @brief Prints a hexadecimal value in plain characters, along with+ the char equivalents in the following format++ 00 00 00 00 00 00 ......++ @param data Pointer to the byte data+ @param numBytes Data length in bytes+*/+/**************************************************************************/+void Adafruit_PN532::PrintHexChar(const byte *data, const uint32_t numBytes) {+ uint32_t szPos;+ for (szPos = 0; szPos < numBytes; szPos++) {+ // Append leading 0 for small values+ if (data[szPos] <= 0xF)+ PN532DEBUGPRINT.print(F("0"));+ PN532DEBUGPRINT.print(data[szPos], HEX);+ if ((numBytes > 1) && (szPos != numBytes - 1)) {+ PN532DEBUGPRINT.print(F(" "));+ }+ }+ PN532DEBUGPRINT.print(F(" "));+ for (szPos = 0; szPos < numBytes; szPos++) {+ if (data[szPos] <= 0x1F)+ PN532DEBUGPRINT.print(F("."));+ else+ PN532DEBUGPRINT.print((char)data[szPos]);+ }+ PN532DEBUGPRINT.println();+}++/**************************************************************************/+/*!+ @brief Checks the firmware version of the PN5xx chip++ @returns The chip's firmware version and ID+*/+/**************************************************************************/+uint32_t Adafruit_PN532::getFirmwareVersion(void) {+ uint32_t response;++ pn532_packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION;++ if (!sendCommandCheckAck(pn532_packetbuffer, 1)) {+ return 0;+ }++ // read data packet+ readdata(pn532_packetbuffer, 13);++ // check some basic stuff+ if (0 != memcmp((char *)pn532_packetbuffer,+ (char *)pn532response_firmwarevers, 6)) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println(F("Firmware doesn't match!"));+#endif+ return 0;+ }++ int offset = 7;+ response = pn532_packetbuffer[offset++];+ response <<= 8;+ response |= pn532_packetbuffer[offset++];+ response <<= 8;+ response |= pn532_packetbuffer[offset++];+ response <<= 8;+ response |= pn532_packetbuffer[offset++];++ return response;+}++/**************************************************************************/+/*!+ @brief Sends a command and waits a specified period for the ACK++ @param cmd Pointer to the command buffer+ @param cmdlen The size of the command in bytes+ @param timeout timeout before giving up++ @returns 1 if everything is OK, 0 if timeout occured before an+ ACK was recieved+*/+/**************************************************************************/+// default timeout of one second+bool Adafruit_PN532::sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen,+ uint16_t timeout) {++ // I2C works without using IRQ pin by polling for RDY byte+ // seems to work best with some delays between transactions+ uint8_t SLOWDOWN = 1;++ // write the command+ writecommand(cmd, cmdlen);++ // I2C TUNING+ delay(SLOWDOWN);++ // Wait for chip to say its ready!+ if (!waitready(timeout)) {+ return false;+ }++#ifdef PN532DEBUG+ if (spi_dev == NULL) {+ PN532DEBUGPRINT.println(F("IRQ received"));+ }+#endif++ // read acknowledgement+ if (!readack()) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println(F("No ACK frame received!"));+#endif+ return false;+ }++ // I2C TUNING+ delay(SLOWDOWN);++ // Wait for chip to say its ready!+ if (!waitready(timeout)) {+ return false;+ }++ return true; // ack'd command+}++/**************************************************************************/+/*!+ @brief Writes an 8-bit value that sets the state of the PN532's GPIO+ pins.+ @param pinstate P3 pins state.++ @warning This function is provided exclusively for board testing and+ is dangerous since it will throw an error if any pin other+ than the ones marked "Can be used as GPIO" are modified! All+ pins that can not be used as GPIO should ALWAYS be left high+ (value = 1) or the system will become unstable and a HW reset+ will be required to recover the PN532.++ pinState[0] = P30 Can be used as GPIO+ pinState[1] = P31 Can be used as GPIO+ pinState[2] = P32 *** RESERVED (Must be 1!) ***+ pinState[3] = P33 Can be used as GPIO+ pinState[4] = P34 *** RESERVED (Must be 1!) ***+ pinState[5] = P35 Can be used as GPIO++ @return 1 if everything executed properly, 0 for an error+*/+/**************************************************************************/+bool Adafruit_PN532::writeGPIO(uint8_t pinstate) {+ // uint8_t errorbit;++ // Make sure pinstate does not try to toggle P32 or P34+ pinstate |= (1 << PN532_GPIO_P32) | (1 << PN532_GPIO_P34);++ // Fill command buffer+ pn532_packetbuffer[0] = PN532_COMMAND_WRITEGPIO;+ pn532_packetbuffer[1] = PN532_GPIO_VALIDATIONBIT | pinstate; // P3 Pins+ pn532_packetbuffer[2] = 0x00; // P7 GPIO Pins (not used ... taken by SPI)++#ifdef PN532DEBUG+ PN532DEBUGPRINT.print(F("Writing P3 GPIO: "));+ PN532DEBUGPRINT.println(pn532_packetbuffer[1], HEX);+#endif++ // Send the WRITEGPIO command (0x0E)+ if (!sendCommandCheckAck(pn532_packetbuffer, 3))+ return 0x0;++ // Read response packet (00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0F) DATACHECKSUM+ // 00)+ readdata(pn532_packetbuffer, 8);++#ifdef PN532DEBUG+ PN532DEBUGPRINT.print(F("Received: "));+ PrintHex(pn532_packetbuffer, 8);+ PN532DEBUGPRINT.println();+#endif++ int offset = 6;+ return (pn532_packetbuffer[offset] == 0x0F);+}++/**************************************************************************/+/*!+ Reads the state of the PN532's GPIO pins++ @returns An 8-bit value containing the pin state where:++ pinState[0] = P30+ pinState[1] = P31+ pinState[2] = P32+ pinState[3] = P33+ pinState[4] = P34+ pinState[5] = P35+*/+/**************************************************************************/+uint8_t Adafruit_PN532::readGPIO(void) {+ pn532_packetbuffer[0] = PN532_COMMAND_READGPIO;++ // Send the READGPIO command (0x0C)+ if (!sendCommandCheckAck(pn532_packetbuffer, 1))+ return 0x0;++ // Read response packet (00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0D) P3 P7 IO1+ // DATACHECKSUM 00)+ readdata(pn532_packetbuffer, 11);++ /* READGPIO response should be in the following format:++ byte Description+ ------------- ------------------------------------------+ b0..5 Frame header and preamble (with I2C there is an extra 0x00)+ b6 P3 GPIO Pins+ b7 P7 GPIO Pins (not used ... taken by SPI)+ b8 Interface Mode Pins (not used ... bus select pins)+ b9..10 checksum */++ int p3offset = 7;++#ifdef PN532DEBUG+ PN532DEBUGPRINT.print(F("Received: "));+ PrintHex(pn532_packetbuffer, 11);+ PN532DEBUGPRINT.println();+ PN532DEBUGPRINT.print(F("P3 GPIO: 0x"));+ PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset], HEX);+ PN532DEBUGPRINT.print(F("P7 GPIO: 0x"));+ PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset + 1], HEX);+ PN532DEBUGPRINT.print(F("IO GPIO: 0x"));+ PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset + 2], HEX);+ // Note: You can use the IO GPIO value to detect the serial bus being used+ switch (pn532_packetbuffer[p3offset + 2]) {+ case 0x00: // Using UART+ PN532DEBUGPRINT.println(F("Using UART (IO = 0x00)"));+ break;+ case 0x01: // Using I2C+ PN532DEBUGPRINT.println(F("Using I2C (IO = 0x01)"));+ break;+ case 0x02: // Using SPI+ PN532DEBUGPRINT.println(F("Using SPI (IO = 0x02)"));+ break;+ }+#endif++ return pn532_packetbuffer[p3offset];+}++/**************************************************************************/+/*!+ @brief Configures the SAM (Secure Access Module)+ @return true on success, false otherwise.+*/+/**************************************************************************/+bool Adafruit_PN532::SAMConfig(void) {+ pn532_packetbuffer[0] = PN532_COMMAND_SAMCONFIGURATION;+ pn532_packetbuffer[1] = 0x01; // normal mode;+ pn532_packetbuffer[2] = 0x14; // timeout 50ms * 20 = 1 second+ pn532_packetbuffer[3] = 0x01; // use IRQ pin!++ if (!sendCommandCheckAck(pn532_packetbuffer, 4))+ return false;++ // read data packet+ readdata(pn532_packetbuffer, 9);++ int offset = 6;+ return (pn532_packetbuffer[offset] == 0x15);+}++bool Adafruit_PN532::powerDown() {+ pn532_packetbuffer[0] = PN532_COMMAND_POWERDOWN;+ pn532_packetbuffer[1] = 0xC0; // I2C or SPI Wakeup+ pn532_packetbuffer[2] = 0x00; // no IRQ++ return sendCommandCheckAck(pn532_packetbuffer, 3);+}+++/**************************************************************************/+/*!+ Sets the MxRtyPassiveActivation byte of the RFConfiguration register++ @param maxRetries 0xFF to wait forever, 0x00..0xFE to timeout+ after mxRetries++ @returns 1 if everything executed properly, 0 for an error+*/+/**************************************************************************/+bool Adafruit_PN532::setPassiveActivationRetries(uint8_t maxRetries) {+ pn532_packetbuffer[0] = PN532_COMMAND_RFCONFIGURATION;+ pn532_packetbuffer[1] = 5; // Config item 5 (MaxRetries)+ pn532_packetbuffer[2] = 0xFF; // MxRtyATR (default = 0xFF)+ pn532_packetbuffer[3] = 0x01; // MxRtyPSL (default = 0x01)+ pn532_packetbuffer[4] = maxRetries;++#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F("Setting MxRtyPassiveActivation to "));+ PN532DEBUGPRINT.print(maxRetries, DEC);+ PN532DEBUGPRINT.println(F(" "));+#endif++ if (!sendCommandCheckAck(pn532_packetbuffer, 5))+ return 0x0; // no ACK++ return 1;+}++/***** 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 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 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);+}++/**************************************************************************/+/*!+ @brief Put the reader in detection mode, non blocking so interrupts+ must be enabled.+ @param cardbaudrate Baud rate of the card+ @return 1 if everything executed properly, 0 for an error+*/+/**************************************************************************/+bool Adafruit_PN532::startPassiveTargetIDDetection(uint8_t cardbaudrate) {+ 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;++ return sendCommandCheckAck(pn532_packetbuffer, 3);+}++/**************************************************************************/+/*!+ Reads the ID of the passive target the reader has deteceted.++ @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.++ @returns 1 if everything executed properly, 0 for an error+*/+/**************************************************************************/+bool Adafruit_PN532::readDetectedPassiveTargetID(uint8_t *uid,+ uint8_t *uidLength) {+ // read data packet+ readdata(pn532_packetbuffer, 20);+ // check some basic stuff++ /* ISO14443A card response should be in the following format:++ byte Description+ ------------- ------------------------------------------+ b0..6 Frame header and preamble+ b7 Tags Found+ b8 Tag Number (only one used in this example)+ b9..10 SENS_RES+ b11 SEL_RES+ b12 NFCID Length+ b13..NFCIDLen NFCID */++#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F("Found "));+ PN532DEBUGPRINT.print(pn532_packetbuffer[7], DEC);+ PN532DEBUGPRINT.println(F(" tags"));+#endif+ if (pn532_packetbuffer[7] != 1)+ return 0;++ uint16_t sens_res = pn532_packetbuffer[9];+ sens_res <<= 8;+ sens_res |= pn532_packetbuffer[10];+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F("ATQA: 0x"));+ PN532DEBUGPRINT.println(sens_res, HEX);+ PN532DEBUGPRINT.print(F("SAK: 0x"));+ PN532DEBUGPRINT.println(pn532_packetbuffer[11], HEX);+#endif++ /* Card appears to be Mifare Classic */+ *uidLength = pn532_packetbuffer[12];+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F("UID:"));+#endif+ for (uint8_t i = 0; i < pn532_packetbuffer[12]; i++) {+ uid[i] = pn532_packetbuffer[13 + i];+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F(" 0x"));+ PN532DEBUGPRINT.print(uid[i], HEX);+#endif+ }+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println();+#endif++ return 1;+}++/**************************************************************************/+/*!+ Reads the ID of the passive target the reader has deteceted.++ @returns 1 if everything executed properly, 0 for an error+*/+/**************************************************************************/+bool Adafruit_PN532::readDetectedPassiveTargetID() {+ // read data packet+ readdata(pn532_packetbuffer, 20);+ // check some basic stuff++ /* ISO14443A card response should be in the following format:++ byte Description+ ------------- ------------------------------------------+ b0..6 Frame header and preamble+ b7 Tags Found+ b8 Tag Number (only one used in this example)+ b9..10 SENS_RES+ b11 SEL_RES+ b12 NFCID Length+ b13..NFCIDLen NFCID */++#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F("Found "));+ PN532DEBUGPRINT.print(pn532_packetbuffer[7], DEC);+ PN532DEBUGPRINT.println(F(" tags"));+#endif+ if (pn532_packetbuffer[7] != 1)+ return 0;++ for (int i = 0; i < 2; i++) targetUid.atqaByte[i] = pn532_packetbuffer[9 + i];+ targetUid.sak = pn532_packetbuffer[11];+ targetUid.size = pn532_packetbuffer[12];+ for (uint8_t i = 0; i < pn532_packetbuffer[12]; i++) {+ targetUid.uidByte[i] = pn532_packetbuffer[13 + i];+ }++#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F("ATQA:"));+ for (uint8_t i = 0; i < 2; i++) {+ PN532DEBUGPRINT.print(F(" 0x"));+ PN532DEBUGPRINT.print(targetUid.atqaByte[i], HEX);+ }+ PN532DEBUGPRINT.print(F("SAK: 0x"));+ PN532DEBUGPRINT.println(targetUid.sak, HEX);+ PN532DEBUGPRINT.print(F("UID:"));+ for (uint8_t i = 0; i < targetUid.size; i++) {+ PN532DEBUGPRINT.print(F(" 0x"));+ PN532DEBUGPRINT.print(targetUid.uidByte[i], HEX);+ }+ PN532DEBUGPRINT.println();+#endif++ return 1;+}++String Adafruit_PN532::PICC_GetTypeName(byte sak) {+ if (sak & 0x04) { // UID not complete+ return "SAK indicates UID is not complete.";+ }++ switch (sak) {+ case 0x09: return "MIFARE Mini, 320 bytes"; break;+ case 0x08: return "MIFARE 1KB"; break;+ case 0x18: return "MIFARE 4KB"; break;+ case 0x00: return "MIFARE Ultralight or Ultralight C"; break;+ case 0x10:+ case 0x11: return "MIFARE Plus"; break;+ case 0x01: return "MIFARE TNP3XXX"; break;+ default: break;+ }++ if (sak & 0x20) {+ return "PICC compliant with ISO/IEC 14443-4";+ }++ if (sak & 0x40) {+ return "PICC compliant with ISO/IEC 18092 (NFC)";+ }++ return "Unknown type";+}++/**************************************************************************/+/*!+ @brief Exchanges an APDU with the currently inlisted peer++ @param send Pointer to data to send+ @param sendLength Length of the data to send+ @param response Pointer to response data+ @param responseLength Pointer to the response data length+ @return true on success, false otherwise.+*/+/**************************************************************************/+bool Adafruit_PN532::inDataExchange(uint8_t *send, uint8_t sendLength,+ uint8_t *response,+ uint8_t *responseLength) {+ if (sendLength > PN532_PACKBUFFSIZ - 2) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println(F("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+ PN532DEBUGPRINT.println(F("Could not send APDU"));+#endif+ return false;+ }++ if (!waitready(1000)) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println(F("Response never received for APDU..."));+#endif+ return false;+ }++ readdata(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+ 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) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println(F("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 {+ PN532DEBUGPRINT.print(F("Don't know how to handle this command: "));+ PN532DEBUGPRINT.println(pn532_packetbuffer[6], HEX);+ return false;+ }+ } else {+ PN532DEBUGPRINT.println(F("Preamble missing"));+ return false;+ }+}++/**************************************************************************/+/*!+ @brief 'InLists' a passive target. PN532 acting as reader/initiator,+ peer acting as card/responder.+ @return true on success, false otherwise.+*/+/**************************************************************************/+bool Adafruit_PN532::inListPassiveTarget() {+ pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;+ pn532_packetbuffer[1] = 1;+ pn532_packetbuffer[2] = 0;++#ifdef PN532DEBUG+ PN532DEBUGPRINT.print(F("About to inList passive target"));+#endif++ if (!sendCommandCheckAck(pn532_packetbuffer, 3, 1000)) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println(F("Could not send inlist message"));+#endif+ return false;+ }++ if (!waitready(30000)) {+ return false;+ }++ readdata(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+ 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_INLISTPASSIVETARGET) {+ if (pn532_packetbuffer[7] != 1) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println(F("Unhandled number of targets inlisted"));+#endif+ PN532DEBUGPRINT.println(F("Number of tags inlisted:"));+ PN532DEBUGPRINT.println(pn532_packetbuffer[7]);+ return false;+ }++ _inListedTag = pn532_packetbuffer[8];+ PN532DEBUGPRINT.print(F("Tag number: "));+ PN532DEBUGPRINT.println(_inListedTag);++ return true;+ } else {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.print(F("Unexpected response to inlist passive host"));+#endif+ return false;+ }+ } else {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println(F("Preamble missing"));+#endif+ return false;+ }++ return true;+}++/********************************************************************/+/*!+* @brief 'TgInitAsTarget' is used to configure a PN532 as a tag by the host.+*+*/+/********************************************************************/+bool Adafruit_PN532::TgInitAsTarget() {++ uint8_t cmd[] = {+ PN532_COMMAND_TGINITASTARGET,+ 0x05, // MODE: PICC only, Passive only++ 0x04, 0x00, // SENS_RES+ 0x12, 0x34, 0x56, // NFCID1+ 0x20, // SEL_RES++ 0, 0, 0, 0, 0, 0, 0, 0,+ 0, 0, 0, 0, 0, 0, 0, 0, // FeliCaParams+ 0, 0,++ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // NFCID3t++ 0, // length of general bytes+ 0 // length of historical bytes+ };++ if (!sendCommandCheckAck(cmd, sizeof(cmd), 1000)) {+ #ifdef PN532DEBUG+ PN532DEBUGPRINT.print(F("Failed at sendCommandCheckAck command"));+ #endif+ return false;+ }+ if (!waitready(1000)) {+ #ifdef PN532DEBUG+ PN532DEBUGPRINT.println(F("Response never received for ADPU..."));+ #endif+ return false;+ }+ readdata(pn532_packetbuffer, sizeof(pn532_packetbuffer));+ return pn532_packetbuffer[7] == 0x08;+}++/**************************************************************************/+/*!+ @brief Gets an APDU from the currently inlisted peer+ @param response Pointer to response data+ @param responseLength Pointer to the response data length+*/+/**************************************************************************/++bool Adafruit_PN532::TgGetData(uint8_t * response, uint8_t * responseLength) {++ pn532_packetbuffer[0] = PN532_COMMAND_TGGETDATA; // 0x86+ if (!sendCommandCheckAck(pn532_packetbuffer, 1, 1000)) {+ #ifdef PN532DEBUG+ PN532DEBUGPRINT.println(F("Failed at sendCommandCheckAck"));+ #endif+ return false;+ }+ if (!waitready(1000)) {+ #ifdef PN532DEBUG+ PN532DEBUGPRINT.println(F("Response never received for ADPU..."));+ #endif+ return false;+ }+ readdata(pn532_packetbuffer, sizeof(pn532_packetbuffer));+ #ifdef PN532DEBUG+ PN532DEBUGPRINT.print(F("Received Packetbuffer: "));+ PrintHex(pn532_packetbuffer, sizeof(pn532_packetbuffer));+ #endif+ *responseLength = pn532_packetbuffer[3]-3;+ for(uint8_t i=0; i<*responseLength; i++) {+ response[i] = pn532_packetbuffer[i+8];+ }+ #ifdef PN532DEBUG+ PN532DEBUGPRINT.print(F("Received GetData Packet: "));+ PrintHex(response, *responseLength);+ #endif+ return pn532_packetbuffer[7] == 0x00;+}++/**************************************************************************/+/*!+ @brief Sends an APDU to the currently inlisted peer+ @param send Pointer to data to send+ @param sendLength Length of the data to send+*/+/**************************************************************************/++bool Adafruit_PN532::TgSetData(uint8_t * send, uint8_t sendLength) {++ pn532_packetbuffer[0] = PN532_COMMAND_TGSETDATA; //0x8E+ for (uint8_t i=0; i<sendLength; i++) {+ pn532_packetbuffer[i+1] = send[i];+ }+ #ifdef PN532DEBUG+ PN532DEBUGPRINT.print(F("Sending SetData Packet: "));+ PrintHex(send, sendLength);+ #endif++ if (!sendCommandCheckAck(pn532_packetbuffer, sendLength+1, 1000))+ return false;+ if (!waitready(1000)) {+ #ifdef PN532DEBUG+ PN532DEBUGPRINT.println(F("Response never received for ADPU..."));+ #endif+ return false;+ }+ readdata(pn532_packetbuffer, sizeof(pn532_packetbuffer));+ return pn532_packetbuffer[8] == 0x00;+}++int16_t Adafruit_PN532::inRelease(const uint8_t relevantTarget) {++ pn532_packetbuffer[0] = PN532_COMMAND_INRELEASE;+ pn532_packetbuffer[1] = relevantTarget;++ if (!sendCommandCheckAck(pn532_packetbuffer, 2, 1000)) {+ #ifdef PN532DEBUG+ PN532DEBUGPRINT.print(F("Failed at sendCommandCheckAck command"));+ #endif+ return false;+ }+ if (!waitready(1000)) {+ #ifdef PN532DEBUG+ PN532DEBUGPRINT.println(F("Response never received for ADPU..."));+ #endif+ return false;+ }+ readdata(pn532_packetbuffer, sizeof(pn532_packetbuffer));+ return true;+}++/***** Mifare Classic Functions ******/++/**************************************************************************/+/*!+ @brief Indicates whether the specified block number is the first block+ in the sector (block 0 relative to the current sector)+ @param uiBlock Block number to test.+ @return true if first block, false otherwise.+*/+/**************************************************************************/+bool Adafruit_PN532::mifareclassic_IsFirstBlock(uint32_t uiBlock) {+ // Test if we are in the small or big sectors+ if (uiBlock < 128)+ return ((uiBlock) % 4 == 0);+ else+ return ((uiBlock) % 16 == 0);+}++/**************************************************************************/+/*!+ @brief Indicates whether the specified block number is the sector+ trailer.+ @param uiBlock Block number to test.+ @return true if sector trailer, false otherwise.+*/+/**************************************************************************/+bool Adafruit_PN532::mifareclassic_IsTrailerBlock(uint32_t uiBlock) {+ // Test if we are in the small or big sectors+ if (uiBlock < 128)+ return ((uiBlock + 1) % 4 == 0);+ else+ return ((uiBlock + 1) % 16 == 0);+}++/**************************************************************************/+/*!+ Tries to authenticate a block of memory on a MIFARE card using the+ INDATAEXCHANGE command. See section 7.3.8 of the PN532 User Manual+ for more information on sending MIFARE and other commands.++ @param uid Pointer to a byte array containing the card UID+ @param uidLen The length (in bytes) of the card's UID (Should+ be 4 for MIFARE Classic)+ @param blockNumber The block number to authenticate. (0..63 for+ 1KB cards, and 0..255 for 4KB cards).+ @param keyNumber Which key type to use during authentication+ (0 = MIFARE_CMD_AUTH_A, 1 = MIFARE_CMD_AUTH_B)+ @param keyData Pointer to a byte array containing the 6 byte+ key value++ @returns 1 if everything executed properly, 0 for an error+*/+/**************************************************************************/+uint8_t Adafruit_PN532::mifareclassic_AuthenticateBlock(uint8_t *uid,+ uint8_t uidLen,+ uint32_t blockNumber,+ uint8_t keyNumber,+ uint8_t *keyData) {+ // uint8_t len;+ uint8_t i;++ // Hang on to the key and uid data+ memcpy(_key, keyData, 6);+ memcpy(_uid, uid, uidLen);+ _uidLen = uidLen;++#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F("Trying to authenticate card "));+ Adafruit_PN532::PrintHex(_uid, _uidLen);+ PN532DEBUGPRINT.print(F("Using authentication KEY "));+ PN532DEBUGPRINT.print(keyNumber ? 'B' : 'A');+ PN532DEBUGPRINT.print(F(": "));+ Adafruit_PN532::PrintHex(_key, 6);+#endif++ // Prepare the authentication command //+ pn532_packetbuffer[0] =+ PN532_COMMAND_INDATAEXCHANGE; /* Data Exchange Header */+ pn532_packetbuffer[1] = 1; /* Max card numbers */+ pn532_packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_B : MIFARE_CMD_AUTH_A;+ pn532_packetbuffer[3] =+ blockNumber; /* Block Number (1K = 0..63, 4K = 0..255 */+ memcpy(pn532_packetbuffer + 4, _key, 6);+ for (i = 0; i < _uidLen; i++) {+ pn532_packetbuffer[10 + i] = _uid[i]; /* 4 byte card ID */+ }++ if (!sendCommandCheckAck(pn532_packetbuffer, 10 + _uidLen))+ return 0;++ // Read the response packet+ readdata(pn532_packetbuffer, 12);++ // check if the response is valid and we are authenticated???+ // for an auth success it should be bytes 5-7: 0xD5 0x41 0x00+ // Mifare auth error is technically byte 7: 0x14 but anything other and 0x00+ // is not good+ if (pn532_packetbuffer[7] != 0x00) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.print(F("Authentification failed: "));+ Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 12);+#endif+ return 0;+ }++ return 1;+}++/**************************************************************************/+/*!+ Tries to read an entire 16-byte data block at the specified block+ address.++ @param blockNumber The block number to authenticate. (0..63 for+ 1KB cards, and 0..255 for 4KB cards).+ @param data Pointer to the byte array that will hold the+ retrieved data (if any)++ @returns 1 if everything executed properly, 0 for an error+*/+/**************************************************************************/+uint8_t Adafruit_PN532::mifareclassic_ReadDataBlock(uint8_t blockNumber,+ uint8_t *data) {+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F("Trying to read 16 bytes from block "));+ PN532DEBUGPRINT.println(blockNumber);+#endif++ /* Prepare the command */+ pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;+ pn532_packetbuffer[1] = 1; /* Card number */+ pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */+ pn532_packetbuffer[3] =+ blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */++ /* Send the command */+ if (!sendCommandCheckAck(pn532_packetbuffer, 4)) {+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println(F("Failed to receive ACK for read command"));+#endif+ return 0;+ }++ /* Read the response packet */+ readdata(pn532_packetbuffer, 26);++ /* If byte 8 isn't 0x00 we probably have an error */+ if (pn532_packetbuffer[7] != 0x00) {+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println(F("Unexpected response"));+ Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26);+#endif+ return 0;+ }++ /* Copy the 16 data bytes to the output buffer */+ /* Block content starts at byte 9 of a valid response */+ memcpy(data, pn532_packetbuffer + 8, 16);++/* Display data for debug if requested */+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F("Block "));+ PN532DEBUGPRINT.println(blockNumber);+ Adafruit_PN532::PrintHexChar(data, 16);+#endif++ return 1;+}++/**************************************************************************/+/*!+ Tries to write an entire 16-byte data block at the specified block+ address.++ @param blockNumber The block number to authenticate. (0..63 for+ 1KB cards, and 0..255 for 4KB cards).+ @param data The byte array that contains the data to write.++ @returns 1 if everything executed properly, 0 for an error+*/+/**************************************************************************/+uint8_t Adafruit_PN532::mifareclassic_WriteDataBlock(uint8_t blockNumber,+ uint8_t *data) {+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F("Trying to write 16 bytes to block "));+ PN532DEBUGPRINT.println(blockNumber);+#endif++ /* Prepare the first command */+ pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;+ pn532_packetbuffer[1] = 1; /* Card number */+ pn532_packetbuffer[2] = MIFARE_CMD_WRITE; /* Mifare Write command = 0xA0 */+ pn532_packetbuffer[3] =+ blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */+ memcpy(pn532_packetbuffer + 4, data, 16); /* Data Payload */++ /* Send the command */+ if (!sendCommandCheckAck(pn532_packetbuffer, 20)) {+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println(F("Failed to receive ACK for write command"));+#endif+ return 0;+ }+ delay(10);++ /* Read the response packet */+ readdata(pn532_packetbuffer, 26);++ return 1;+}++/**************************************************************************/+/*!+ Formats a Mifare Classic card to store NDEF Records++ @returns 1 if everything executed properly, 0 for an error+*/+/**************************************************************************/+uint8_t Adafruit_PN532::mifareclassic_FormatNDEF(void) {+ uint8_t sectorbuffer1[16] = {0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1,+ 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};+ uint8_t sectorbuffer2[16] = {0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1,+ 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};+ uint8_t sectorbuffer3[16] = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77,+ 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};++ // Note 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 must be used for key A+ // for the MAD sector in NDEF records (sector 0)++ // Write block 1 and 2 to the card+ if (!(mifareclassic_WriteDataBlock(1, sectorbuffer1)))+ return 0;+ if (!(mifareclassic_WriteDataBlock(2, sectorbuffer2)))+ return 0;+ // Write key A and access rights card+ if (!(mifareclassic_WriteDataBlock(3, sectorbuffer3)))+ return 0;++ // Seems that everything was OK (?!)+ return 1;+}++/**************************************************************************/+/*!+ Writes an NDEF URI Record to the specified sector (1..15)++ Note that this function assumes that the Mifare Classic card is+ already formatted to work as an "NFC Forum Tag" and uses a MAD1+ file system. You can use the NXP TagWriter app on Android to+ properly format cards for this.++ @param sectorNumber The sector that the URI record should be written+ to (can be 1..15 for a 1K card)+ @param uriIdentifier The uri identifier code (0 = none, 0x01 =+ "http://www.", etc.)+ @param url The uri text to write (max 38 characters).++ @returns 1 if everything executed properly, 0 for an error+*/+/**************************************************************************/+uint8_t Adafruit_PN532::mifareclassic_WriteNDEFURI(uint8_t sectorNumber,+ uint8_t uriIdentifier,+ const char *url) {+ // Figure out how long the string is+ uint8_t len = strlen(url);++ // Make sure we're within a 1K limit for the sector number+ if ((sectorNumber < 1) || (sectorNumber > 15))+ return 0;++ // Make sure the URI payload is between 1 and 38 chars+ if ((len < 1) || (len > 38))+ return 0;++ // Note 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 must be used for key A+ // in NDEF records++ // Setup the sector buffer (w/pre-formatted TLV wrapper and NDEF message)+ uint8_t sectorbuffer1[16] = {0x00,+ 0x00,+ 0x03,+ (uint8_t)(len + 5),+ 0xD1,+ 0x01,+ (uint8_t)(len + 1),+ 0x55,+ uriIdentifier,+ 0x00,+ 0x00,+ 0x00,+ 0x00,+ 0x00,+ 0x00,+ 0x00};+ uint8_t sectorbuffer2[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};+ uint8_t sectorbuffer3[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};+ uint8_t sectorbuffer4[16] = {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07,+ 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};+ if (len <= 6) {+ // Unlikely we'll get a url this short, but why not ...+ memcpy(sectorbuffer1 + 9, url, len);+ sectorbuffer1[len + 9] = 0xFE;+ } else if (len == 7) {+ // 0xFE needs to be wrapped around to next block+ memcpy(sectorbuffer1 + 9, url, len);+ sectorbuffer2[0] = 0xFE;+ } else if ((len > 7) && (len <= 22)) {+ // Url fits in two blocks+ memcpy(sectorbuffer1 + 9, url, 7);+ memcpy(sectorbuffer2, url + 7, len - 7);+ sectorbuffer2[len - 7] = 0xFE;+ } else if (len == 23) {+ // 0xFE needs to be wrapped around to final block+ memcpy(sectorbuffer1 + 9, url, 7);+ memcpy(sectorbuffer2, url + 7, len - 7);+ sectorbuffer3[0] = 0xFE;+ } else {+ // Url fits in three blocks+ memcpy(sectorbuffer1 + 9, url, 7);+ memcpy(sectorbuffer2, url + 7, 16);+ memcpy(sectorbuffer3, url + 23, len - 24);+ sectorbuffer3[len - 22] = 0xFE;+ }++ // Now write all three blocks back to the card+ if (!(mifareclassic_WriteDataBlock(sectorNumber * 4, sectorbuffer1)))+ return 0;+ if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 1, sectorbuffer2)))+ return 0;+ if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 2, sectorbuffer3)))+ return 0;+ if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 3, sectorbuffer4)))+ return 0;++ // Seems that everything was OK (?!)+ return 1;+}++/***** Mifare Ultralight Functions ******/++/**************************************************************************/+/*!+ @brief Tries to read an entire 4-byte page at the specified address.++ @param page The page number (0..63 in most cases)+ @param buffer Pointer to the byte array that will hold the+ retrieved data (if any)+ @return 1 on success, 0 on error.+*/+/**************************************************************************/+uint8_t Adafruit_PN532::mifareultralight_ReadPage(uint8_t page,+ uint8_t *buffer) {+ if (page >= 64) {+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println(F("Page value out of range"));+#endif+ return 0;+ }++#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F("Reading page "));+ PN532DEBUGPRINT.println(page);+#endif++ /* Prepare the command */+ pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;+ pn532_packetbuffer[1] = 1; /* Card number */+ pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */+ pn532_packetbuffer[3] = page; /* Page Number (0..63 in most cases) */++ /* Send the command */+ if (!sendCommandCheckAck(pn532_packetbuffer, 4)) {+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println(F("Failed to receive ACK for write command"));+#endif+ return 0;+ }++ /* Read the response packet */+ readdata(pn532_packetbuffer, 26);+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println(F("Received: "));+ Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26);+#endif++ /* If byte 8 isn't 0x00 we probably have an error */+ if (pn532_packetbuffer[7] == 0x00) {+ /* Copy the 4 data bytes to the output buffer */+ /* Block content starts at byte 9 of a valid response */+ /* Note that the command actually reads 16 byte or 4 */+ /* pages at a time ... we simply discard the last 12 */+ /* bytes */+ memcpy(buffer, pn532_packetbuffer + 8, 4);+ } else {+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println(F("Unexpected response reading block: "));+ Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26);+#endif+ return 0;+ }++/* Display data for debug if requested */+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F("Page "));+ PN532DEBUGPRINT.print(page);+ PN532DEBUGPRINT.println(F(":"));+ Adafruit_PN532::PrintHexChar(buffer, 4);+#endif++ // Return OK signal+ return 1;+}++/**************************************************************************/+/*!+ Tries to write an entire 4-byte page at the specified block+ address.++ @param page The page number to write. (0..63 for most cases)+ @param data The byte array that contains the data to write.+ Should be exactly 4 bytes long.++ @returns 1 if everything executed properly, 0 for an error+*/+/**************************************************************************/+uint8_t Adafruit_PN532::mifareultralight_WritePage(uint8_t page,+ uint8_t *data) {++ if (page >= 64) {+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println(F("Page value out of range"));+#endif+ // Return Failed Signal+ return 0;+ }++#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F("Trying to write 4 byte page"));+ PN532DEBUGPRINT.println(page);+#endif++ /* Prepare the first command */+ pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;+ pn532_packetbuffer[1] = 1; /* Card number */+ pn532_packetbuffer[2] =+ MIFARE_ULTRALIGHT_CMD_WRITE; /* Mifare Ultralight Write command = 0xA2 */+ pn532_packetbuffer[3] = page; /* Page Number (0..63 for most cases) */+ memcpy(pn532_packetbuffer + 4, data, 4); /* Data Payload */++ /* Send the command */+ if (!sendCommandCheckAck(pn532_packetbuffer, 8)) {+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println(F("Failed to receive ACK for write command"));+#endif++ // Return Failed Signal+ return 0;+ }+ delay(10);++ /* Read the response packet */+ readdata(pn532_packetbuffer, 26);++ // Return OK Signal+ return 1;+}++/***** NTAG2xx Functions ******/++/**************************************************************************/+/*!+ @brief Tries to read an entire 4-byte page at the specified address.++ @param page The page number (0..63 in most cases)+ @param buffer Pointer to the byte array that will hold the+ retrieved data (if any)+ @return 1 on success, 0 on error.+*/+/**************************************************************************/+uint8_t Adafruit_PN532::ntag2xx_ReadPage(uint8_t page, uint8_t *buffer) {+ // TAG Type PAGES USER START USER STOP+ // -------- ----- ---------- ---------+ // NTAG 203 42 4 39+ // NTAG 213 45 4 39+ // NTAG 215 135 4 129+ // NTAG 216 231 4 225++ if (page >= 231) {+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println(F("Page value out of range"));+#endif+ return 0;+ }++#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F("Reading page "));+ PN532DEBUGPRINT.println(page);+#endif++ /* Prepare the command */+ pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;+ pn532_packetbuffer[1] = 1; /* Card number */+ pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */+ pn532_packetbuffer[3] = page; /* Page Number (0..63 in most cases) */++ /* Send the command */+ if (!sendCommandCheckAck(pn532_packetbuffer, 4)) {+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println(F("Failed to receive ACK for write command"));+#endif+ return 0;+ }++ /* Read the response packet */+ readdata(pn532_packetbuffer, 26);+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println(F("Received: "));+ Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26);+#endif++ /* If byte 8 isn't 0x00 we probably have an error */+ if (pn532_packetbuffer[7] == 0x00) {+ /* Copy the 4 data bytes to the output buffer */+ /* Block content starts at byte 9 of a valid response */+ /* Note that the command actually reads 16 byte or 4 */+ /* pages at a time ... we simply discard the last 12 */+ /* bytes */+ memcpy(buffer, pn532_packetbuffer + 8, 16);+ } else {+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println(F("Unexpected response reading block: "));+ Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26);+#endif+ return 0;+ }++/* Display data for debug if requested */+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F("Page "));+ PN532DEBUGPRINT.print(page);+ PN532DEBUGPRINT.println(F(":"));+ Adafruit_PN532::PrintHexChar(buffer, 4);+#endif++ // Return OK signal+ return 1;+}++/**************************************************************************/+/*!+ Tries to write an entire 4-byte page at the specified block+ address.++ @param page The page number to write. (0..63 for most cases)+ @param data The byte array that contains the data to write.+ Should be exactly 4 bytes long.++ @returns 1 if everything executed properly, 0 for an error+*/+/**************************************************************************/+uint8_t Adafruit_PN532::ntag2xx_WritePage(uint8_t page, uint8_t *data) {+ // TAG Type PAGES USER START USER STOP+ // -------- ----- ---------- ---------+ // NTAG 203 42 4 39+ // NTAG 213 45 4 39+ // NTAG 215 135 4 129+ // NTAG 216 231 4 225++ if ((page < 4) || (page > 225)) {+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println(F("Page value out of range"));+#endif+ // Return Failed Signal+ return 0;+ }++#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.print(F("Trying to write 4 byte page"));+ PN532DEBUGPRINT.println(page);+#endif++ /* Prepare the first command */+ pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;+ pn532_packetbuffer[1] = 1; /* Card number */+ pn532_packetbuffer[2] =+ MIFARE_ULTRALIGHT_CMD_WRITE; /* Mifare Ultralight Write command = 0xA2 */+ pn532_packetbuffer[3] = page; /* Page Number (0..63 for most cases) */+ memcpy(pn532_packetbuffer + 4, data, 4); /* Data Payload */++ /* Send the command */+ if (!sendCommandCheckAck(pn532_packetbuffer, 8)) {+#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println(F("Failed to receive ACK for write command"));+#endif++ // Return Failed Signal+ return 0;+ }+ delay(10);++ /* Read the response packet */+ readdata(pn532_packetbuffer, 26);++ // Return OK Signal+ return 1;+}++/**************************************************************************/+/*!+ Writes an NDEF URI Record starting at the specified page (4..nn)++ Note that this function assumes that the NTAG2xx card is+ already formatted to work as an "NFC Forum Tag".++ @param uriIdentifier The uri identifier code (0 = none, 0x01 =+ "http://www.", etc.)+ @param url The uri text to write (null-terminated string).+ @param dataLen The size of the data area for overflow checks.++ @returns 1 if everything executed properly, 0 for an error+*/+/**************************************************************************/+uint8_t Adafruit_PN532::ntag2xx_WriteNDEFURI(uint8_t uriIdentifier, char *url,+ uint8_t dataLen) {+ uint8_t pageBuffer[4] = {0, 0, 0, 0};++ // Remove NDEF record overhead from the URI data (pageHeader below)+ uint8_t wrapperSize = 12;++ // Figure out how long the string is+ uint8_t len = strlen(url);++ // Make sure the URI payload will fit in dataLen (include 0xFE trailer)+ if ((len < 1) || (len + 1 > (dataLen - wrapperSize)))+ return 0;++ // Setup the record header+ // See NFCForum-TS-Type-2-Tag_1.1.pdf for details+ uint8_t pageHeader[12] = {+ /* NDEF Lock Control TLV (must be first and always present) */+ 0x01, /* Tag Field (0x01 = Lock Control TLV) */+ 0x03, /* Payload Length (always 3) */+ 0xA0, /* The position inside the tag of the lock bytes (upper 4 = page+ address, lower 4 = byte offset) */+ 0x10, /* Size in bits of the lock area */+ 0x44, /* Size in bytes of a page and the number of bytes each lock bit can+ lock (4 bit + 4 bits) */+ /* NDEF Message TLV - URI Record */+ 0x03, /* Tag Field (0x03 = NDEF Message) */+ (uint8_t)(len + 5), /* Payload Length (not including 0xFE trailer) */+ 0xD1, /* NDEF Record Header (TNF=0x1:Well known record + SR + ME + MB) */+ 0x01, /* Type Length for the record type indicator */+ (uint8_t)(len + 1), /* Payload len */+ 0x55, /* Record Type Indicator (0x55 or 'U' = URI Record) */+ uriIdentifier /* URI Prefix (ex. 0x01 = "http://www.") */+ };++ // Write 12 byte header (three pages of data starting at page 4)+ memcpy(pageBuffer, pageHeader, 4);+ if (!(ntag2xx_WritePage(4, pageBuffer)))+ return 0;+ memcpy(pageBuffer, pageHeader + 4, 4);+ if (!(ntag2xx_WritePage(5, pageBuffer)))+ return 0;+ memcpy(pageBuffer, pageHeader + 8, 4);+ if (!(ntag2xx_WritePage(6, pageBuffer)))+ return 0;++ // Write URI (starting at page 7)+ uint8_t currentPage = 7;+ char *urlcopy = url;+ while (len) {+ if (len < 4) {+ memset(pageBuffer, 0, 4);+ memcpy(pageBuffer, urlcopy, len);+ pageBuffer[len] = 0xFE; // NDEF record footer+ if (!(ntag2xx_WritePage(currentPage, pageBuffer)))+ return 0;+ // DONE!+ return 1;+ } else if (len == 4) {+ memcpy(pageBuffer, urlcopy, len);+ if (!(ntag2xx_WritePage(currentPage, pageBuffer)))+ return 0;+ memset(pageBuffer, 0, 4);+ pageBuffer[0] = 0xFE; // NDEF record footer+ currentPage++;+ if (!(ntag2xx_WritePage(currentPage, pageBuffer)))+ return 0;+ // DONE!+ return 1;+ } else {+ // More than one page of data left+ memcpy(pageBuffer, urlcopy, 4);+ if (!(ntag2xx_WritePage(currentPage, pageBuffer)))+ return 0;+ currentPage++;+ urlcopy += 4;+ len -= 4;+ }+ }++ // Seems that everything was OK (?!)+ return 1;+}++/***** Felica Functions ******/++/**************************************************************************/+/*!+ @brief Poll FeliCa card. PN532 acting as reader/initiator,+ peer acting as card/responder.+ @param[in] systemCode Designation of System Code. When sending+ FFFFh as System Code, all FeliCa cards can return response.+ @param[in] requestCode Designation of Request Data as follows:+ 00h: No Request+ 01h: System Code request (to acquire+ System Code of the card) 02h: Communication perfomance request+ @param[out] idm IDm of the card (8 bytes)+ @param[out] pmm PMm of the card (8 bytes)+ @param[out] systemCodeResponse System Code of the card (Optional,+ 2bytes)+ @param[in] timeout Timeout for every command+ @return = 1: A FeliCa card has detected+ = 0: No card has detected+*/+/**************************************************************************/+uint8_t Adafruit_PN532::felica_Polling(uint16_t systemCode, uint8_t requestCode,+ uint8_t *idm, uint8_t *pmm,+ uint16_t *systemCodeResponse,+ uint16_t timeout) {+ pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;+ pn532_packetbuffer[1] = 1;+ pn532_packetbuffer[2] = 1;+ pn532_packetbuffer[3] = FELICA_CMD_POLLING;+ pn532_packetbuffer[4] = (systemCode >> 8) & 0xFF;+ pn532_packetbuffer[5] = systemCode & 0xFF;+ pn532_packetbuffer[6] = requestCode;+ pn532_packetbuffer[7] = 0;++ if (!sendCommandCheckAck(pn532_packetbuffer, 8, timeout)) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println("Error sending command");+#endif+ return 0;+ }+ /* Response structure:+ 0-9 not used+ 10 response code(should be 01)+ 11-19 IDm+ 19-27 PMm+ 27-29 Request data(only when request code is not 0x00 and supported by+ card)+ */+ readdata(pn532_packetbuffer, requestCode == 0x01 ? 29 : 27);+#ifdef PN532DEBUG+ PrintHex(pn532_packetbuffer, requestCode == 0x01 ? 29 : 27);+#endif+ if ((sizeof(pn532_packetbuffer) / sizeof(byte)) == 0) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println("Could not receive response");+#endif+ return 0;+ }++ // Check for Response code == 0x01+ if (pn532_packetbuffer[10] != 0x01) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println("No card had detected");+#endif+ return 0;+ }++ _inListedTag = pn532_packetbuffer[8];++ // Fill data+ memcpy(idm, &pn532_packetbuffer[11], 8);+ memcpy(_felicaIDm, &pn532_packetbuffer[11], 8);+ memcpy(pmm, &pn532_packetbuffer[19], 8);+ memcpy(_felicaPMm, &pn532_packetbuffer[19], 8);+ if (requestCode == 0x01)+ *systemCodeResponse =+ (pn532_packetbuffer[27] << 8) + pn532_packetbuffer[28];++ return 1;+}++/**************************************************************************/+/*!+ @brief Sends FeliCa command to the currently inlisted peer and get response+ in buffer++ @param[in] command FeliCa command packet. (e.g. 00 FF FF 00 00 for+ Polling command)+ @param[in] commandlength Length of the FeliCa command packet. (e.g. 0x05+ for above Polling command )+ @param[out] responseLength Length of the FeliCa response packet. (e.g. 0x11+ for above Polling command )+ @return = 1: Success+ = 0: error+*/+/**************************************************************************/+uint8_t Adafruit_PN532::felica_SendCommand(const uint8_t *command,+ uint8_t commandlength,+ uint8_t responseLength) {+ if (commandlength > 0xFE) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println("Command length too long");+#endif+ return -1;+ }++ pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;+ pn532_packetbuffer[1] = _inListedTag;+ pn532_packetbuffer[2] = commandlength + 1;+ memcpy(&pn532_packetbuffer[3], command, commandlength);++ if (!sendCommandCheckAck(pn532_packetbuffer, 3 + commandlength)) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println("Could not send FeliCa command");+#endif+ return 0;+ }++ /* Read data in packet buffer */+ readdata(pn532_packetbuffer, responseLength);++ return 1;+}++/**************************************************************************/+/*!+ @brief Sends FeliCa Read Without Encryption command++ @param[in] numService Length of the serviceCodeList+ @param[in] serviceCodeList Service Code List (Big Endian)+ @param[in] numBlock Length of the blockList+ @param[in] blockList Block List (Big Endian, This API only accepts+ 2-byte block list element)+ @param[out] blockData Block Data+ @return = 1: Success+ = 0: error+*/+/**************************************************************************/+uint8_t Adafruit_PN532::felica_ReadWithoutEncryption(+ uint8_t numService, const uint16_t *serviceCodeList, uint8_t numBlock,+ const uint16_t *blockList, uint8_t blockData[][16]) {+ if (numService > FELICA_READ_MAX_SERVICE_NUM) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println("numService is too large");+#endif+ return 0;+ }+ if (numBlock > FELICA_READ_MAX_BLOCK_NUM) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println("numBlock is too large");+#endif+ return 0;+ }++ uint8_t i;+ uint8_t cmdLen = 1 + 8 + 1 + 2 * numService + 1 + 2 * numBlock;+ uint8_t cmd[cmdLen];+ cmd[0] = FELICA_CMD_READ_WITHOUT_ENCRYPTION;+ memcpy(&cmd[1], _felicaIDm, 8); // Fill card IDm++ /* Fill number of service and service code list */+ cmd[9] = numService;+ int j = 10;+ for (i = 0; i < numService; ++i) {+ cmd[j++] = serviceCodeList[i] & 0xFF;+ cmd[j++] = (serviceCodeList[i] >> 8) & 0xff;+ }++ /* Fill number of block and blocklist */+ cmd[j++] = numBlock;+ for (i = 0; i < numBlock; ++i) {+ cmd[j++] = (blockList[i] >> 8) & 0xFF;+ cmd[j++] = blockList[i] & 0xff;+ }++ const uint8_t responseLength = 20 + 16 * numBlock;+ if (felica_SendCommand(cmd, cmdLen, responseLength) != 1) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println("Read Without Encryption command failed");+#endif+ return 0;+ }++ if (pn532_packetbuffer[9] != 0x07) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println("Invalid response");+#endif+ return 0;+ }++ if (pn532_packetbuffer[18] != 0x00) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println("Invalid status flag");+#endif+ return 0;+ }++ /* Copy block data from buffer to blockData parameter */+ for (size_t i = 0; i < numBlock; i++) {+ memcpy(blockData[i], &pn532_packetbuffer[(21 * i) + 21], 16);+ }++ return 1;+}++/**************************************************************************/+/*!+ @brief Sends FeliCa Write Without Encryption command++ @param[in] numService Length of the serviceCodeList+ @param[in] serviceCodeList Service Code List (Big Endian)+ @param[in] numBlock Length of the blockList+ @param[in] blockList Block List (Big Endian, This API only accepts+ 2-byte block list element)+ @param[in] blockData Block Data (each Block has 16 bytes)+ @return = 1: Success+ = 0: error+*/+/**************************************************************************/+uint8_t Adafruit_PN532::felica_WriteWithoutEncryption(+ uint8_t numService, const uint16_t *serviceCodeList, uint8_t numBlock,+ const uint16_t *blockList, uint8_t blockData[][16]) {+ if (numService > FELICA_WRITE_MAX_SERVICE_NUM) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println("numService is too large");+#endif+ return 0;+ }+ if (numBlock > FELICA_WRITE_MAX_BLOCK_NUM) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println("numBlock is too large");+#endif+ return 0;+ }++ uint8_t i, k;+ uint8_t cmdLen =+ 1 + 8 + 1 + 2 * numService + 1 + 2 * numBlock + 16 * numBlock;+ uint8_t cmd[cmdLen];+ cmd[0] = FELICA_CMD_WRITE_WITHOUT_ENCRYPTION;+ memcpy(&cmd[1], _felicaIDm, 8); // Fill card IDm+ int j = 10;+ cmd[j++] = numService;+ for (i = 0; i < numService; ++i) {+ cmd[j++] = serviceCodeList[i] & 0xFF;+ cmd[j++] = (serviceCodeList[i] >> 8) & 0xff;+ }+ cmd[j++] = numBlock;+ for (i = 0; i < numBlock; ++i) {+ cmd[j++] = (blockList[i] >> 8) & 0xFF;+ cmd[j++] = blockList[i] & 0xff;+ }+ for (i = 0; i < numBlock; ++i) {+ for (k = 0; k < 16; k++) {+ cmd[j++] = blockData[i][k];+ }+ }++ if (felica_SendCommand(cmd, cmdLen, 11) != 1) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println("Write Without Encryption command failed");+#endif+ return 0;+ }++ if (pn532_packetbuffer[9] != 0x09) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.printf("Response code doesn't match. Status: %i\n",+ pn532_packetbuffer[9]);+#endif+ return 0;+ }+ return 1;+}++/**************************************************************************/+/*!+ @brief Release FeliCa card+ @return = 1: Success+ = 0: error+*/+/**************************************************************************/+uint8_t Adafruit_PN532::felica_Release() {+ // InRelease+ pn532_packetbuffer[0] = PN532_COMMAND_INRELEASE;+ pn532_packetbuffer[1] = 0x00; // All target+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println("Release all FeliCa target");+#endif++ if (sendCommandCheckAck(pn532_packetbuffer, 2)) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println("No ACK");+#endif+ return 0; // no ACK+ }++ return 1;+}++/************** high level communication functions (handles both I2C and SPI) */++/**************************************************************************/+/*!+ @brief Tries to read the SPI or I2C ACK signal+*/+/**************************************************************************/+bool Adafruit_PN532::readack() {+ uint8_t ackbuff[6];++ if (spi_dev) {+ uint8_t cmd = PN532_SPI_DATAREAD;+ spi_dev->write_then_read(&cmd, 1, ackbuff, 6);+ } else if (i2c_dev || ser_dev) {+ readdata(ackbuff, 6);+ }++ return (0 == memcmp((char *)ackbuff, (char *)pn532ack, 6));+}++/**************************************************************************/+/*!+ @brief Return true if the PN532 is ready with a response.+*/+/**************************************************************************/+bool Adafruit_PN532::isready() {+ if (spi_dev) {+ // SPI ready check via Status Request+ uint8_t cmd = PN532_SPI_STATREAD;+ uint8_t reply;+ spi_dev->write_then_read(&cmd, 1, &reply, 1);+ return reply == PN532_SPI_READY;+ } else if (i2c_dev) {+ // I2C ready check via reading RDY byte+ uint8_t rdy[1];+ i2c_dev->read(rdy, 1);+ return rdy[0] == PN532_I2C_READY;+ } else if (ser_dev) {+ // Serial ready check based on non-zero read buffer+ return (ser_dev->available() != 0);+ } else if (_irq != -1) {+ uint8_t x = digitalRead(_irq);+ return x == 0;+ }+ return false;+}++/**************************************************************************/+/*!+ @brief Waits until the PN532 is ready.++ @param timeout Timeout before giving up+*/+/**************************************************************************/+bool Adafruit_PN532::waitready(uint16_t timeout) {+ uint16_t timer = 0;+ while (!isready()) {+ if (timeout != 0) {+ timer += 10;+ if (timer > timeout) {+#ifdef PN532DEBUG+ PN532DEBUGPRINT.println("TIMEOUT!");+#endif+ return false;+ }+ }+ delay(10);+ }+ return true;+}++/**************************************************************************/+/*!+ @brief Reads n bytes of data from the PN532 via SPI or I2C.++ @param buff Pointer to the buffer where data will be written+ @param n Number of bytes to be read+*/+/**************************************************************************/+void Adafruit_PN532::readdata(uint8_t *buff, uint8_t n) {+ if (spi_dev) {+ // SPI read+ uint8_t cmd = PN532_SPI_DATAREAD;+ spi_dev->write_then_read(&cmd, 1, buff, n);+ } else if (i2c_dev) {+ // I2C read+ uint8_t rbuff[n + 1]; // +1 for leading RDY byte+ i2c_dev->read(rbuff, n + 1);+ for (uint8_t i = 0; i < n; i++) {+ buff[i] = rbuff[i + 1];+ }+ } else if (ser_dev) {+ // Serial read+ ser_dev->readBytes(buff, n);+ }+#ifdef PN532DEBUG+ PN532DEBUGPRINT.print(F("Reading: "));+ for (uint8_t i = 0; i < n; i++) {+ PN532DEBUGPRINT.print(F(" 0x"));+ PN532DEBUGPRINT.print(buff[i], HEX);+ }+ PN532DEBUGPRINT.println();+#endif+}++/**************************************************************************/+/*!+ @brief set the PN532 as iso14443a Target behaving as a SmartCard+ @return true on success, false otherwise.+ @note Author: Salvador Mendoza (salmg.net) new functions:+ -AsTarget+ -getDataTarget+ -setDataTarget+*/+/**************************************************************************/+uint8_t Adafruit_PN532::AsTarget() {+ pn532_packetbuffer[0] = 0x8C;+ uint8_t target[] = {+ 0x8C, // INIT AS TARGET+ 0x00, // MODE -> BITFIELD+ 0x08, 0x00, // SENS_RES - MIFARE PARAMS+ 0xdc, 0x44, 0x20, // NFCID1T+ 0x60, // SEL_RES+ 0x01, 0xfe, // NFCID2T MUST START WITH 01fe - FELICA PARAMS - POL_RES+ 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xc0,+ 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, // PAD+ 0xff, 0xff, // SYSTEM CODE+ 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44,+ 0x33, 0x22, 0x11, 0x01, 0x00, // NFCID3t MAX 47 BYTES ATR_RES+ 0x0d, 0x52, 0x46, 0x49, 0x44, 0x49, 0x4f,+ 0x74, 0x20, 0x50, 0x4e, 0x35, 0x33, 0x32 // HISTORICAL BYTES+ };+ if (!sendCommandCheckAck(target, sizeof(target)))+ return false;++ // read data packet+ readdata(pn532_packetbuffer, 8);++ int offset = 6;+ return (pn532_packetbuffer[offset] == 0x15);+}+/**************************************************************************/+/*!+ @brief Retrieve response from the emulation mode++ @param cmd = data+ @param cmdlen = data length+ @return true on success, false otherwise.+*/+/**************************************************************************/+uint8_t Adafruit_PN532::getDataTarget(uint8_t *cmd, uint8_t *cmdlen) {+ uint8_t length;+ pn532_packetbuffer[0] = 0x86;+ if (!sendCommandCheckAck(pn532_packetbuffer, 1, 1000)) {+ PN532DEBUGPRINT.println(F("Error en ack"));+ return false;+ }++ // read data packet+ readdata(pn532_packetbuffer, 64);+ length = pn532_packetbuffer[3] - 3;++ // if (length > *responseLength) {// Bug, should avoid it in the reading+ // target data+ // length = *responseLength; // silent truncation...+ //}++ for (int i = 0; i < length; ++i) {+ cmd[i] = pn532_packetbuffer[8 + i];+ }+ *cmdlen = length;+ return true;+}++/**************************************************************************/+/*!+ @brief Set data in PN532 in the emulation mode++ @param cmd = data+ @param cmdlen = data length+ @return true on success, false otherwise.+*/+/**************************************************************************/+uint8_t Adafruit_PN532::setDataTarget(uint8_t *cmd, uint8_t cmdlen) {+ uint8_t length;+ // cmd1[0] = 0x8E; Must!++ if (!sendCommandCheckAck(cmd, cmdlen))+ return false;++ // read data packet+ readdata(pn532_packetbuffer, 8);+ length = pn532_packetbuffer[3] - 3;+ for (int i = 0; i < length; ++i) {+ cmd[i] = pn532_packetbuffer[8 + i];+ }+ // cmdl = 0+ cmdlen = length;++ int offset = 6;+ return (pn532_packetbuffer[offset] == 0x15);+}++/**************************************************************************/+/*!+ @brief Writes a command to the PN532, automatically inserting the+ preamble and required frame details (checksum, len, etc.)++ @param cmd Pointer to the command buffer+ @param cmdlen Command length in bytes+*/+/**************************************************************************/+void Adafruit_PN532::writecommand(uint8_t *cmd, uint8_t cmdlen) {+ if (spi_dev) {+ // SPI command write.+ uint8_t checksum;+ uint8_t packet[9 + cmdlen];+ uint8_t *p = packet;+ cmdlen++;++ p[0] = PN532_SPI_DATAWRITE;+ p++;++ p[0] = PN532_PREAMBLE;+ p++;+ p[0] = PN532_STARTCODE1;+ p++;+ p[0] = PN532_STARTCODE2;+ p++;+ checksum = PN532_PREAMBLE + PN532_STARTCODE1 + PN532_STARTCODE2;++ p[0] = cmdlen;+ p++;+ p[0] = ~cmdlen + 1;+ p++;++ p[0] = PN532_HOSTTOPN532;+ p++;+ checksum += PN532_HOSTTOPN532;++ for (uint8_t i = 0; i < cmdlen - 1; i++) {+ p[0] = cmd[i];+ p++;+ checksum += cmd[i];+ }++ p[0] = ~checksum;+ p++;+ p[0] = PN532_POSTAMBLE;+ p++;++#ifdef PN532DEBUG+ Serial.print("Sending : ");+ for (int i = 1; i < 8 + cmdlen; i++) {+ Serial.print("0x");+ Serial.print(packet[i], HEX);+ Serial.print(", ");+ }+ Serial.println();+#endif++ spi_dev->write(packet, 8 + cmdlen);+ } else if (i2c_dev || ser_dev) {+ // I2C or Serial command write.+ uint8_t packet[8 + cmdlen];+ uint8_t LEN = cmdlen + 1;++ packet[0] = PN532_PREAMBLE;+ packet[1] = PN532_STARTCODE1;+ packet[2] = PN532_STARTCODE2;+ packet[3] = LEN;+ packet[4] = ~LEN + 1;+ packet[5] = PN532_HOSTTOPN532;+ uint8_t sum = 0;+ for (uint8_t i = 0; i < cmdlen; i++) {+ packet[6 + i] = cmd[i];+ sum += cmd[i];+ }+ packet[6 + cmdlen] = ~(PN532_HOSTTOPN532 + sum) + 1;+ packet[7 + cmdlen] = PN532_POSTAMBLE;++#ifdef PN532DEBUG+ Serial.print("Sending : ");+ for (int i = 1; i < 8 + cmdlen; i++) {+ Serial.print("0x");+ Serial.print(packet[i], HEX);+ Serial.print(", ");+ }+ Serial.println();+#endif++ if (i2c_dev) {+ i2c_dev->write(packet, 8 + cmdlen);+ } else {+ ser_dev->write(packet, 8 + cmdlen);+ }+ }+}+++/**************************************************************************/+/*!+ @brief Builds the command to send to write to the PN532 registers+ @param reg Pointer to the command buffer, goes in the form of+ ADDR1H ADDR1L VAL1...ADDRnH ADDRnL VALn+ @param len Command length in bytes+*/+/**************************************************************************/++bool Adafruit_PN532::WriteRegister(uint8_t *reg, uint8_t len) {+ uint8_t cmd[len + 1];+ uint8_t result[8];+ cmd[0] = PN532_COMMAND_WRITEREGISTER;+ for (uint8_t i = 0; i < len; i++) {+ cmd[i + 1] = reg[i];+ }++ if (!sendCommandCheckAck(cmd, len + 1)) return false;++ readdata(result, 8);+ return true;+}++/**************************************************************************/+/*!+ @brief Builds the command to send commands directly to the PICC+ Works like inDataExchange but doesn't handles all the+ protocol features+ @param data Pointer to the command buffer+ @param len Command length in bytes+*/+/**************************************************************************/+bool Adafruit_PN532::InCommunicateThru(uint8_t *data, uint8_t len) {+ uint8_t cmd[len + 1];+ uint8_t result[8];+ cmd[0] = PN532_COMMAND_INCOMMUNICATETHRU;+ for (uint8_t i = 0; i < len; i++) {+ cmd[i + 1] = data[i];+ }+ if (!sendCommandCheckAck(cmd, len + 1)) return false;++ readdata(result, 8);+ // If byte 8 isn't 0x00 we probably have an error,+ if (result[7] != 0x00) return false;+ return true;+}++/**************************************************************************/+/*!+ @brief Performs the command sequence in some chinese MiFare Classic tags+ that unlocks a special backdoor to perform write/read+ commands without authentication+ >HALT + CRC (50 00 57 CD)+ >UNLOCK1 (40) 7 bits+ <ACK (A) 4 bits+ >UNLOCK2 (43)+ <ACK (A) 4 bits+*/+/**************************************************************************/+bool Adafruit_PN532::UnlockBackdoor() {+ // Disable automatic CRC performed by the PN532+ uint8_t regState1[6] = {0x63, 0x02, 0x00, 0x63, 0x03, 0x00};+ if (!WriteRegister(regState1, 6)) {+ return false;+ }+ // HALT+ uint8_t halt[4] = {0x50, 0x00, 0x57, 0xcd};+ InCommunicateThru(halt, 4);++ // Set BitFraming to only send 7 bits+ uint8_t reg1[3] = {0x63, 0x3d, 0x07};+ if (!WriteRegister(reg1, 3)) {+ return false;+ }+ // UNLOCK1+ bool unlockSuccess;+ uint8_t unlock1[1] = {0x40};+ unlockSuccess = InCommunicateThru(unlock1, 1);++ // Set BitFraming back to normal+ uint8_t reg2[3] = {0x63, 0x3d, 0x00};+ if (!WriteRegister(reg2, 3)) {+ return false;+ }+ // UNLOCK2+ if (unlockSuccess == true) {+ uint8_t unlock2[1] = {0x43};+ if (!InCommunicateThru(unlock2, 1)) {+ return false;+ }+ }+ // Enable automatic CRC performed by the PN532+ uint8_t regState2[6] = {0x63, 0x02, 0x80, 0x63, 0x03, 0x80};+ if (!WriteRegister(regState2, 6)) {+ return false;+ }+ if (unlockSuccess == false) {+ return false;+ }+ return true;+}++bool Adafruit_PN532::mifareclassic_WriteBlock0(uint8_t *data) {+ if (!data || !UnlockBackdoor()) return false;++#ifdef MIFAREDEBUG+ PN532DEBUGPRINT.println(F("Unlock success. WriteDataBlock 0"));+#endif+ return mifareclassic_WriteDataBlock(0, data);+}diff --git a/lib/PN532/Adafruit_PN532.h b/lib/PN532/Adafruit_PN532.hnew file mode 100644index 00000000..988d0ffb--- /dev/null+++ b/lib/PN532/Adafruit_PN532.h@@ -0,0 +1,280 @@+/**************************************************************************/+/*!+ @file Adafruit_PN532.h++ v2.0 - Refactored to add I2C support from Adafruit_NFCShield_I2C library.++ v1.1 - Added full command list+ - Added 'verbose' mode flag to constructor to toggle debug output+ - Changed readPassiveTargetID() to return variable length values+*/+/**************************************************************************/++#ifndef ADAFRUIT_PN532_H+#define ADAFRUIT_PN532_H++#include "Arduino.h"++#include "Adafruit_I2CDevice.h"+#include "Adafruit_SPIDevice.h"++#define PN532_PREAMBLE (0x00) ///< Command sequence start, byte 1/3+#define PN532_STARTCODE1 (0x00) ///< Command sequence start, byte 2/3+#define PN532_STARTCODE2 (0xFF) ///< Command sequence start, byte 3/3+#define PN532_POSTAMBLE (0x00) ///< EOD++#define PN532_HOSTTOPN532 (0xD4) ///< Host-to-PN532+#define PN532_PN532TOHOST (0xD5) ///< PN532-to-host++// PN532 Commands+#define PN532_COMMAND_DIAGNOSE (0x00) ///< Diagnose+#define PN532_COMMAND_GETFIRMWAREVERSION (0x02) ///< Get firmware version+#define PN532_COMMAND_GETGENERALSTATUS (0x04) ///< Get general status+#define PN532_COMMAND_READREGISTER (0x06) ///< Read register+#define PN532_COMMAND_WRITEREGISTER (0x08) ///< Write register+#define PN532_COMMAND_READGPIO (0x0C) ///< Read GPIO+#define PN532_COMMAND_WRITEGPIO (0x0E) ///< Write GPIO+#define PN532_COMMAND_SETSERIALBAUDRATE (0x10) ///< Set serial baud rate+#define PN532_COMMAND_SETPARAMETERS (0x12) ///< Set parameters+#define PN532_COMMAND_SAMCONFIGURATION (0x14) ///< SAM configuration+#define PN532_COMMAND_POWERDOWN (0x16) ///< Power down+#define PN532_COMMAND_RFCONFIGURATION (0x32) ///< RF config+#define PN532_COMMAND_RFREGULATIONTEST (0x58) ///< RF regulation test+#define PN532_COMMAND_INJUMPFORDEP (0x56) ///< Jump for DEP+#define PN532_COMMAND_INJUMPFORPSL (0x46) ///< Jump for PSL+#define PN532_COMMAND_INLISTPASSIVETARGET (0x4A) ///< List passive target+#define PN532_COMMAND_INATR (0x50) ///< ATR+#define PN532_COMMAND_INPSL (0x4E) ///< PSL+#define PN532_COMMAND_INDATAEXCHANGE (0x40) ///< Data exchange+#define PN532_COMMAND_INCOMMUNICATETHRU (0x42) ///< Communicate through+#define PN532_COMMAND_INDESELECT (0x44) ///< Deselect+#define PN532_COMMAND_INRELEASE (0x52) ///< Release+#define PN532_COMMAND_INSELECT (0x54) ///< Select+#define PN532_COMMAND_INAUTOPOLL (0x60) ///< Auto poll+#define PN532_COMMAND_TGINITASTARGET (0x8C) ///< Init as target+#define PN532_COMMAND_TGSETGENERALBYTES (0x92) ///< Set general bytes+#define PN532_COMMAND_TGGETDATA (0x86) ///< Get data+#define PN532_COMMAND_TGSETDATA (0x8E) ///< Set data+#define PN532_COMMAND_TGSETMETADATA (0x94) ///< Set metadata+#define PN532_COMMAND_TGGETINITIATORCOMMAND (0x88) ///< Get initiator command+#define PN532_COMMAND_TGRESPONSETOINITIATOR (0x90) ///< Response to initiator+#define PN532_COMMAND_TGGETTARGETSTATUS (0x8A) ///< Get target status++#define PN532_RESPONSE_INDATAEXCHANGE (0x41) ///< Data exchange+#define PN532_RESPONSE_INLISTPASSIVETARGET (0x4B) ///< List passive target++#define PN532_WAKEUP (0x55) ///< Wake++#define PN532_SPI_STATREAD (0x02) ///< Stat read+#define PN532_SPI_DATAWRITE (0x01) ///< Data write+#define PN532_SPI_DATAREAD (0x03) ///< Data read+#define PN532_SPI_READY (0x01) ///< Ready++#define PN532_I2C_ADDRESS (0x48 >> 1) ///< Default I2C address+#define PN532_I2C_READBIT (0x01) ///< Read bit+#define PN532_I2C_BUSY (0x00) ///< Busy+#define PN532_I2C_READY (0x01) ///< Ready+#define PN532_I2C_READYTIMEOUT (20) ///< Ready timeout++#define PN532_MIFARE_ISO14443A (0x00) ///< MiFare++// Mifare Commands+#define MIFARE_CMD_AUTH_A (0x60) ///< Auth A+#define MIFARE_CMD_AUTH_B (0x61) ///< Auth B+#define MIFARE_CMD_READ (0x30) ///< Read+#define MIFARE_CMD_WRITE (0xA0) ///< Write+#define MIFARE_CMD_TRANSFER (0xB0) ///< Transfer+#define MIFARE_CMD_DECREMENT (0xC0) ///< Decrement+#define MIFARE_CMD_INCREMENT (0xC1) ///< Increment+#define MIFARE_CMD_STORE (0xC2) ///< Store+#define MIFARE_ULTRALIGHT_CMD_WRITE (0xA2) ///< Write (MiFare Ultralight)++// FeliCa Commands+#define FELICA_CMD_POLLING (0x00) ///< Polling cmd+#define FELICA_CMD_READ_WITHOUT_ENCRYPTION (0x06) ///< Read without encryption+#define FELICA_CMD_WRITE_WITHOUT_ENCRYPTION (0x08) ///< Write without encryption++// Prefixes for NDEF Records (to identify record type)+#define NDEF_URIPREFIX_NONE (0x00) ///< No prefix+#define NDEF_URIPREFIX_HTTP_WWWDOT (0x01) ///< HTTP www. prefix+#define NDEF_URIPREFIX_HTTPS_WWWDOT (0x02) ///< HTTPS www. prefix+#define NDEF_URIPREFIX_HTTP (0x03) ///< HTTP prefix+#define NDEF_URIPREFIX_HTTPS (0x04) ///< HTTPS prefix+#define NDEF_URIPREFIX_TEL (0x05) ///< Tel prefix+#define NDEF_URIPREFIX_MAILTO (0x06) ///< Mailto prefix+#define NDEF_URIPREFIX_FTP_ANONAT (0x07) ///< FTP+#define NDEF_URIPREFIX_FTP_FTPDOT (0x08) ///< FTP dot+#define NDEF_URIPREFIX_FTPS (0x09) ///< FTPS+#define NDEF_URIPREFIX_SFTP (0x0A) ///< SFTP+#define NDEF_URIPREFIX_SMB (0x0B) ///< SMB+#define NDEF_URIPREFIX_NFS (0x0C) ///< NFS+#define NDEF_URIPREFIX_FTP (0x0D) ///< FTP+#define NDEF_URIPREFIX_DAV (0x0E) ///< DAV+#define NDEF_URIPREFIX_NEWS (0x0F) ///< NEWS+#define NDEF_URIPREFIX_TELNET (0x10) ///< Telnet prefix+#define NDEF_URIPREFIX_IMAP (0x11) ///< IMAP prefix+#define NDEF_URIPREFIX_RTSP (0x12) ///< RTSP+#define NDEF_URIPREFIX_URN (0x13) ///< URN+#define NDEF_URIPREFIX_POP (0x14) ///< POP+#define NDEF_URIPREFIX_SIP (0x15) ///< SIP+#define NDEF_URIPREFIX_SIPS (0x16) ///< SIPS+#define NDEF_URIPREFIX_TFTP (0x17) ///< TFPT+#define NDEF_URIPREFIX_BTSPP (0x18) ///< BTSPP+#define NDEF_URIPREFIX_BTL2CAP (0x19) ///< BTL2CAP+#define NDEF_URIPREFIX_BTGOEP (0x1A) ///< BTGOEP+#define NDEF_URIPREFIX_TCPOBEX (0x1B) ///< TCPOBEX+#define NDEF_URIPREFIX_IRDAOBEX (0x1C) ///< IRDAOBEX+#define NDEF_URIPREFIX_FILE (0x1D) ///< File+#define NDEF_URIPREFIX_URN_EPC_ID (0x1E) ///< URN EPC ID+#define NDEF_URIPREFIX_URN_EPC_TAG (0x1F) ///< URN EPC tag+#define NDEF_URIPREFIX_URN_EPC_PAT (0x20) ///< URN EPC pat+#define NDEF_URIPREFIX_URN_EPC_RAW (0x21) ///< URN EPC raw+#define NDEF_URIPREFIX_URN_EPC (0x22) ///< URN EPC+#define NDEF_URIPREFIX_URN_NFC (0x23) ///< URN NFC++#define PN532_GPIO_VALIDATIONBIT (0x80) ///< GPIO validation bit+#define PN532_GPIO_P30 (0) ///< GPIO 30+#define PN532_GPIO_P31 (1) ///< GPIO 31+#define PN532_GPIO_P32 (2) ///< GPIO 32+#define PN532_GPIO_P33 (3) ///< GPIO 33+#define PN532_GPIO_P34 (4) ///< GPIO 34+#define PN532_GPIO_P35 (5) ///< GPIO 35++// FeliCa consts+#define FELICA_READ_MAX_SERVICE_NUM \+ 16 ///< Maximum number of services code during reading+#define FELICA_READ_MAX_BLOCK_NUM 12 ///< for typical FeliCa card+#define FELICA_WRITE_MAX_SERVICE_NUM \+ 16 ///< Maximum number of services code during writing+#define FELICA_WRITE_MAX_BLOCK_NUM 10 ///< for typical FeliCa card+#define FELICA_REQ_SERVICE_MAX_NODE_NUM \+ 32 ///< Max node for a request service call++/**+ * @brief Class for working with Adafruit PN532 NFC/RFID breakout boards.+ */+class Adafruit_PN532 {+public:+ typedef struct {+ byte size;+ byte uidByte[10];+ byte sak;+ byte atqaByte[2];+ } Uid;++ Uid targetUid;++ Adafruit_PN532();+ Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi,+ uint8_t ss); // Software SPI+ Adafruit_PN532(uint8_t ss, SPIClass *theSPI = &SPI); // Hardware SPI+ Adafruit_PN532(uint8_t irq, uint8_t reset,+ TwoWire *theWire = &Wire); // Hardware I2C+ Adafruit_PN532(uint8_t reset, HardwareSerial *theSer); // Hardware UART+ bool begin(void);++ void reset(void);+ void wakeup(void);+ void setInterface(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t ss);+ void setInterface(uint8_t sdaPin, uint8_t sclPin);++ String PICC_GetTypeName(byte sak);++ // Generic PN532 functions+ bool SAMConfig(void);+ uint32_t getFirmwareVersion(void);+ bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen,+ uint16_t timeout = 100);+ bool writeGPIO(uint8_t pinstate);+ uint8_t readGPIO(void);+ bool setPassiveActivationRetries(uint8_t maxRetries);+ bool powerDown();++ // ISO14443A functions+ bool readPassiveTargetID(+ uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength,+ uint16_t timeout = 0); // timeout 0 means no timeout - will block forever.+ bool startPassiveTargetIDDetection(uint8_t cardbaudrate = PN532_MIFARE_ISO14443A);+ bool readDetectedPassiveTargetID(uint8_t *uid, uint8_t *uidLength);+ bool readDetectedPassiveTargetID();+ bool inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response,+ uint8_t *responseLength);+ bool inListPassiveTarget();+ uint8_t AsTarget();+ uint8_t getDataTarget(uint8_t *cmd, uint8_t *cmdlen);+ uint8_t setDataTarget(uint8_t *cmd, uint8_t cmdlen);+ bool TgInitAsTarget();+ bool TgGetData(uint8_t * response, uint8_t * responseLength);+ bool TgSetData(uint8_t * send, uint8_t sendLength);+ int16_t inRelease(const uint8_t relevantTarget = 0);++ // Mifare Classic functions+ bool UnlockBackdoor();+ bool mifareclassic_WriteBlock0(uint8_t *data);+ bool mifareclassic_IsFirstBlock(uint32_t uiBlock);+ bool mifareclassic_IsTrailerBlock(uint32_t uiBlock);+ uint8_t mifareclassic_AuthenticateBlock(uint8_t *uid, uint8_t uidLen,+ uint32_t blockNumber,+ uint8_t keyNumber, uint8_t *keyData);+ uint8_t mifareclassic_ReadDataBlock(uint8_t blockNumber, uint8_t *data);+ uint8_t mifareclassic_WriteDataBlock(uint8_t blockNumber, uint8_t *data);+ uint8_t mifareclassic_FormatNDEF(void);+ uint8_t mifareclassic_WriteNDEFURI(uint8_t sectorNumber,+ uint8_t uriIdentifier, const char *url);++ // Mifare Ultralight functions+ uint8_t mifareultralight_ReadPage(uint8_t page, uint8_t *buffer);+ uint8_t mifareultralight_WritePage(uint8_t page, uint8_t *data);++ // NTAG2xx functions+ uint8_t ntag2xx_ReadPage(uint8_t page, uint8_t *buffer);+ uint8_t ntag2xx_WritePage(uint8_t page, uint8_t *data);+ uint8_t ntag2xx_WriteNDEFURI(uint8_t uriIdentifier, char *url,+ uint8_t dataLen);++ // Felica functions+ uint8_t felica_Polling(uint16_t systemCode, uint8_t requestCode, uint8_t *idm,+ uint8_t *pmm, uint16_t *systemCodeResponse,+ uint16_t timeout = 100);+ uint8_t felica_SendCommand(const uint8_t *command, uint8_t commandlength,+ uint8_t responseLength);+ uint8_t felica_ReadWithoutEncryption(uint8_t numService,+ const uint16_t *serviceCodeList,+ uint8_t numBlock,+ const uint16_t *blockList,+ uint8_t blockData[][16]);+ uint8_t felica_WriteWithoutEncryption(uint8_t numService,+ const uint16_t *serviceCodeList,+ uint8_t numBlock,+ const uint16_t *blockList,+ uint8_t blockData[][16]);+ uint8_t felica_Release();++ // Help functions to display formatted text+ static void PrintHex(const byte *data, const uint32_t numBytes);+ static void PrintHexChar(const byte *pbtData, const uint32_t numBytes);++private:+ int8_t _irq = -1, _reset = -1, _cs = -1;+ int8_t _uid[7]; // ISO14443A uid+ int8_t _uidLen; // uid len+ int8_t _key[6]; // Mifare Classic key+ int8_t _inListedTag; // Tg number of inlisted tag.+ uint8_t _felicaIDm[8]; // FeliCa IDm (NFCID2)+ uint8_t _felicaPMm[8]; // FeliCa PMm (PAD)++ // Low level communication functions that handle both SPI and I2C.+ void readdata(uint8_t *buff, uint8_t n);+ void writecommand(uint8_t *cmd, uint8_t cmdlen);+ bool isready();+ bool waitready(uint16_t timeout);+ bool readack();+ bool WriteRegister(uint8_t *reg, uint8_t len);+ bool InCommunicateThru(uint8_t *data, uint8_t len);++ Adafruit_SPIDevice *spi_dev = NULL;+ Adafruit_I2CDevice *i2c_dev = NULL;+ HardwareSerial *ser_dev = NULL;+};++#endifdiff --git a/lib/PN532/Adafruit_SPIDevice.cpp b/lib/PN532/Adafruit_SPIDevice.cppnew file mode 100644index 00000000..034dc085--- /dev/null+++ b/lib/PN532/Adafruit_SPIDevice.cpp@@ -0,0 +1,508 @@+#include "Adafruit_SPIDevice.h"++//#define DEBUG_SERIAL Serial++/*!+ * @brief Create an SPI device with the given CS pin and settings+ * @param cspin The arduino pin number to use for chip select+ * @param freq The SPI clock frequency to use, defaults to 1MHz+ * @param dataOrder The SPI data order to use for bits within each byte,+ * defaults to SPI_BITORDER_MSBFIRST+ * @param dataMode The SPI mode to use, defaults to SPI_MODE0+ * @param theSPI The SPI bus to use, defaults to &theSPI+ */+Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t cspin, uint32_t freq,+ BusIOBitOrder dataOrder,+ uint8_t dataMode, SPIClass *theSPI) {+#ifdef BUSIO_HAS_HW_SPI+ _cs = cspin;+ _sck = _mosi = _miso = -1;+ _spi = theSPI;+ _begun = false;+ _spiSetting = new SPISettings(freq, dataOrder, dataMode);+ _freq = freq;+ _dataOrder = dataOrder;+ _dataMode = dataMode;+#else+ // unused, but needed to suppress compiler warns+ (void)cspin;+ (void)freq;+ (void)dataOrder;+ (void)dataMode;+ (void)theSPI;+#endif+}++/*!+ * @brief Create an SPI device with the given CS pin and settings+ * @param cspin The arduino pin number to use for chip select+ * @param sckpin The arduino pin number to use for SCK+ * @param misopin The arduino pin number to use for MISO, set to -1 if not+ * used+ * @param mosipin The arduino pin number to use for MOSI, set to -1 if not+ * used+ * @param freq The SPI clock frequency to use, defaults to 1MHz+ * @param dataOrder The SPI data order to use for bits within each byte,+ * defaults to SPI_BITORDER_MSBFIRST+ * @param dataMode The SPI mode to use, defaults to SPI_MODE0+ */+Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t cspin, int8_t sckpin,+ int8_t misopin, int8_t mosipin,+ uint32_t freq, BusIOBitOrder dataOrder,+ uint8_t dataMode) {+ _cs = cspin;+ _sck = sckpin;+ _miso = misopin;+ _mosi = mosipin;++#ifdef BUSIO_USE_FAST_PINIO+ csPort = (BusIO_PortReg *)portOutputRegister(digitalPinToPort(cspin));+ csPinMask = digitalPinToBitMask(cspin);+ if (mosipin != -1) {+ mosiPort = (BusIO_PortReg *)portOutputRegister(digitalPinToPort(mosipin));+ mosiPinMask = digitalPinToBitMask(mosipin);+ }+ if (misopin != -1) {+ misoPort = (BusIO_PortReg *)portInputRegister(digitalPinToPort(misopin));+ misoPinMask = digitalPinToBitMask(misopin);+ }+ clkPort = (BusIO_PortReg *)portOutputRegister(digitalPinToPort(sckpin));+ clkPinMask = digitalPinToBitMask(sckpin);+#endif++ _freq = freq;+ _dataOrder = dataOrder;+ _dataMode = dataMode;+ _begun = false;+}++/*!+ * @brief Release memory allocated in constructors+ */+Adafruit_SPIDevice::~Adafruit_SPIDevice() {+ if (_spiSetting)+ delete _spiSetting;+}++/*!+ * @brief Initializes SPI bus and sets CS pin high+ * @return Always returns true because there's no way to test success of SPI+ * init+ */+bool Adafruit_SPIDevice::begin(void) {+ if (_cs != -1) {+ pinMode(_cs, OUTPUT);+ digitalWrite(_cs, HIGH);+ }++ if (_spi) { // hardware SPI+#ifdef BUSIO_HAS_HW_SPI+ _spi->begin();+#endif+ } else {+ pinMode(_sck, OUTPUT);++ if ((_dataMode == SPI_MODE0) || (_dataMode == SPI_MODE1)) {+ // idle low on mode 0 and 1+ digitalWrite(_sck, LOW);+ } else {+ // idle high on mode 2 or 3+ digitalWrite(_sck, HIGH);+ }+ if (_mosi != -1) {+ pinMode(_mosi, OUTPUT);+ digitalWrite(_mosi, HIGH);+ }+ if (_miso != -1) {+ pinMode(_miso, INPUT);+ }+ }++ _begun = true;+ return true;+}++/*!+ * @brief Transfer (send/receive) a buffer over hard/soft SPI, without+ * transaction management+ * @param buffer The buffer to send and receive at the same time+ * @param len The number of bytes to transfer+ */+void Adafruit_SPIDevice::transfer(uint8_t *buffer, size_t len) {+ //+ // HARDWARE SPI+ //+ if (_spi) {+#ifdef BUSIO_HAS_HW_SPI+#if defined(SPARK)+ _spi->transfer(buffer, buffer, len, nullptr);+#elif defined(STM32)+ for (size_t i = 0; i < len; i++) {+ _spi->transfer(buffer[i]);+ }+#else+ _spi->transfer(buffer, len);+#endif+ return;+#endif+ }++ //+ // SOFTWARE SPI+ //+ uint8_t startbit;+ if (_dataOrder == SPI_BITORDER_LSBFIRST) {+ startbit = 0x1;+ } else {+ startbit = 0x80;+ }++ bool towrite, lastmosi = !(buffer[0] & startbit);+ uint8_t bitdelay_us = (1000000 / _freq) / 2;++ for (size_t i = 0; i < len; i++) {+ uint8_t reply = 0;+ uint8_t send = buffer[i];++ /*+ Serial.print("\tSending software SPI byte 0x");+ Serial.print(send, HEX);+ Serial.print(" -> 0x");+ */++ // Serial.print(send, HEX);+ for (uint8_t b = startbit; b != 0;+ b = (_dataOrder == SPI_BITORDER_LSBFIRST) ? b << 1 : b >> 1) {++ if (bitdelay_us) {+ delayMicroseconds(bitdelay_us);+ }++ if (_dataMode == SPI_MODE0 || _dataMode == SPI_MODE2) {+ towrite = send & b;+ if ((_mosi != -1) && (lastmosi != towrite)) {+#ifdef BUSIO_USE_FAST_PINIO+ if (towrite)+ *mosiPort = *mosiPort | mosiPinMask;+ else+ *mosiPort = *mosiPort & ~mosiPinMask;+#else+ digitalWrite(_mosi, towrite);+#endif+ lastmosi = towrite;+ }++#ifdef BUSIO_USE_FAST_PINIO+ *clkPort = *clkPort | clkPinMask; // Clock high+#else+ digitalWrite(_sck, HIGH);+#endif++ if (bitdelay_us) {+ delayMicroseconds(bitdelay_us);+ }++ if (_miso != -1) {+#ifdef BUSIO_USE_FAST_PINIO+ if (*misoPort & misoPinMask) {+#else+ if (digitalRead(_miso)) {+#endif+ reply |= b;+ }+ }++#ifdef BUSIO_USE_FAST_PINIO+ *clkPort = *clkPort & ~clkPinMask; // Clock low+#else+ digitalWrite(_sck, LOW);+#endif+ } else { // if (_dataMode == SPI_MODE1 || _dataMode == SPI_MODE3)++#ifdef BUSIO_USE_FAST_PINIO+ *clkPort = *clkPort | clkPinMask; // Clock high+#else+ digitalWrite(_sck, HIGH);+#endif++ if (bitdelay_us) {+ delayMicroseconds(bitdelay_us);+ }++ if (_mosi != -1) {+#ifdef BUSIO_USE_FAST_PINIO+ if (send & b)+ *mosiPort = *mosiPort | mosiPinMask;+ else+ *mosiPort = *mosiPort & ~mosiPinMask;+#else+ digitalWrite(_mosi, send & b);+#endif+ }++#ifdef BUSIO_USE_FAST_PINIO+ *clkPort = *clkPort & ~clkPinMask; // Clock low+#else+ digitalWrite(_sck, LOW);+#endif++ if (_miso != -1) {+#ifdef BUSIO_USE_FAST_PINIO+ if (*misoPort & misoPinMask) {+#else+ if (digitalRead(_miso)) {+#endif+ reply |= b;+ }+ }+ }+ if (_miso != -1) {+ buffer[i] = reply;+ }+ }+ }+ return;+}++/*!+ * @brief Transfer (send/receive) one byte over hard/soft SPI, without+ * transaction management+ * @param send The byte to send+ * @return The byte received while transmitting+ */+uint8_t Adafruit_SPIDevice::transfer(uint8_t send) {+ uint8_t data = send;+ transfer(&data, 1);+ return data;+}++/*!+ * @brief Manually begin a transaction (calls beginTransaction if hardware+ * SPI)+ */+void Adafruit_SPIDevice::beginTransaction(void) {+ if (_spi) {+#ifdef BUSIO_HAS_HW_SPI+ _spi->beginTransaction(*_spiSetting);+#endif+ }+}++/*!+ * @brief Manually end a transaction (calls endTransaction if hardware SPI)+ */+void Adafruit_SPIDevice::endTransaction(void) {+ if (_spi) {+#ifdef BUSIO_HAS_HW_SPI+ _spi->endTransaction();+#endif+ }+}++/*!+ * @brief Assert/Deassert the CS pin if it is defined+ * @param value The state the CS is set to+ */+void Adafruit_SPIDevice::setChipSelect(int value) {+ if (_cs != -1) {+ digitalWrite(_cs, value);+ }+}++/*!+ * @brief Write a buffer or two to the SPI device, with transaction+ * management.+ * @brief Manually begin a transaction (calls beginTransaction if hardware+ * SPI) with asserting the CS pin+ */+void Adafruit_SPIDevice::beginTransactionWithAssertingCS() {+ beginTransaction();+ setChipSelect(LOW);+}++/*!+ * @brief Manually end a transaction (calls endTransaction if hardware SPI)+ * with deasserting the CS pin+ */+void Adafruit_SPIDevice::endTransactionWithDeassertingCS() {+ setChipSelect(HIGH);+ endTransaction();+}++/*!+ * @brief Write a buffer or two to the SPI device, with transaction+ * management.+ * @param buffer Pointer to buffer of data to write+ * @param len Number of bytes from buffer to write+ * @param prefix_buffer Pointer to optional array of data to write before+ * buffer.+ * @param prefix_len Number of bytes from prefix buffer to write+ * @return Always returns true because there's no way to test success of SPI+ * writes+ */+bool Adafruit_SPIDevice::write(const uint8_t *buffer, size_t len,+ const uint8_t *prefix_buffer,+ size_t prefix_len) {+ beginTransactionWithAssertingCS();++ // do the writing+#if defined(ARDUINO_ARCH_ESP32)+ if (_spi) {+ if (prefix_len > 0) {+ _spi->transferBytes((uint8_t *)prefix_buffer, nullptr, prefix_len);+ }+ if (len > 0) {+ _spi->transferBytes((uint8_t *)buffer, nullptr, len);+ }+ } else+#endif+ {+ for (size_t i = 0; i < prefix_len; i++) {+ transfer(prefix_buffer[i]);+ }+ for (size_t i = 0; i < len; i++) {+ transfer(buffer[i]);+ }+ }+ endTransactionWithDeassertingCS();++#ifdef DEBUG_SERIAL+ DEBUG_SERIAL.print(F("\tSPIDevice Wrote: "));+ if ((prefix_len != 0) && (prefix_buffer != nullptr)) {+ for (uint16_t i = 0; i < prefix_len; i++) {+ DEBUG_SERIAL.print(F("0x"));+ DEBUG_SERIAL.print(prefix_buffer[i], HEX);+ DEBUG_SERIAL.print(F(", "));+ }+ }+ for (uint16_t i = 0; i < len; i++) {+ DEBUG_SERIAL.print(F("0x"));+ DEBUG_SERIAL.print(buffer[i], HEX);+ DEBUG_SERIAL.print(F(", "));+ if (i % 32 == 31) {+ DEBUG_SERIAL.println();+ }+ }+ DEBUG_SERIAL.println();+#endif++ return true;+}++/*!+ * @brief Read from SPI into a buffer from the SPI device, with transaction+ * management.+ * @param buffer Pointer to buffer of data to read into+ * @param len Number of bytes from buffer to read.+ * @param sendvalue The 8-bits of data to write when doing the data read,+ * defaults to 0xFF+ * @return Always returns true because there's no way to test success of SPI+ * writes+ */+bool Adafruit_SPIDevice::read(uint8_t *buffer, size_t len, uint8_t sendvalue) {+ memset(buffer, sendvalue, len); // clear out existing buffer++ beginTransactionWithAssertingCS();+ transfer(buffer, len);+ endTransactionWithDeassertingCS();++#ifdef DEBUG_SERIAL+ DEBUG_SERIAL.print(F("\tSPIDevice Read: "));+ for (uint16_t i = 0; i < len; i++) {+ DEBUG_SERIAL.print(F("0x"));+ DEBUG_SERIAL.print(buffer[i], HEX);+ DEBUG_SERIAL.print(F(", "));+ if (len % 32 == 31) {+ DEBUG_SERIAL.println();+ }+ }+ DEBUG_SERIAL.println();+#endif++ return true;+}++/*!+ * @brief Write some data, then read some data from SPI into another buffer,+ * with transaction management. The buffers can point to same/overlapping+ * locations. This does not transmit-receive at the same time!+ * @param write_buffer Pointer to buffer of data to write from+ * @param write_len Number of bytes from buffer to write.+ * @param read_buffer Pointer to buffer of data to read into.+ * @param read_len Number of bytes from buffer to read.+ * @param sendvalue The 8-bits of data to write when doing the data read,+ * defaults to 0xFF+ * @return Always returns true because there's no way to test success of SPI+ * writes+ */+bool Adafruit_SPIDevice::write_then_read(const uint8_t *write_buffer,+ size_t write_len, uint8_t *read_buffer,+ size_t read_len, uint8_t sendvalue) {+ beginTransactionWithAssertingCS();+ // do the writing+#if defined(ARDUINO_ARCH_ESP32)+ if (_spi) {+ if (write_len > 0) {+ _spi->transferBytes((uint8_t *)write_buffer, nullptr, write_len);+ }+ } else+#endif+ {+ for (size_t i = 0; i < write_len; i++) {+ transfer(write_buffer[i]);+ }+ }++#ifdef DEBUG_SERIAL+ DEBUG_SERIAL.print(F("\tSPIDevice Wrote: "));+ for (uint16_t i = 0; i < write_len; i++) {+ DEBUG_SERIAL.print(F("0x"));+ DEBUG_SERIAL.print(write_buffer[i], HEX);+ DEBUG_SERIAL.print(F(", "));+ if (write_len % 32 == 31) {+ DEBUG_SERIAL.println();+ }+ }+ DEBUG_SERIAL.println();+#endif++ // do the reading+ for (size_t i = 0; i < read_len; i++) {+ read_buffer[i] = transfer(sendvalue);+ }++#ifdef DEBUG_SERIAL+ DEBUG_SERIAL.print(F("\tSPIDevice Read: "));+ for (uint16_t i = 0; i < read_len; i++) {+ DEBUG_SERIAL.print(F("0x"));+ DEBUG_SERIAL.print(read_buffer[i], HEX);+ DEBUG_SERIAL.print(F(", "));+ if (read_len % 32 == 31) {+ DEBUG_SERIAL.println();+ }+ }+ DEBUG_SERIAL.println();+#endif++ endTransactionWithDeassertingCS();++ return true;+}++/*!+ * @brief Write some data and read some data at the same time from SPI+ * into the same buffer, with transaction management. This is basicaly a wrapper+ * for transfer() with CS-pin and transaction management. This /does/+ * transmit-receive at the same time!+ * @param buffer Pointer to buffer of data to write/read to/from+ * @param len Number of bytes from buffer to write/read.+ * @return Always returns true because there's no way to test success of SPI+ * writes+ */+bool Adafruit_SPIDevice::write_and_read(uint8_t *buffer, size_t len) {+ beginTransactionWithAssertingCS();+ transfer(buffer, len);+ endTransactionWithDeassertingCS();++ return true;+}diff --git a/lib/PN532/Adafruit_SPIDevice.h b/lib/PN532/Adafruit_SPIDevice.hnew file mode 100644index 00000000..1d1f953f--- /dev/null+++ b/lib/PN532/Adafruit_SPIDevice.h@@ -0,0 +1,144 @@+#ifndef Adafruit_SPIDevice_h+#define Adafruit_SPIDevice_h++#include <Arduino.h>++#if !defined(SPI_INTERFACES_COUNT) || \+ (defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0))+// HW SPI available+#include <SPI.h>+#define BUSIO_HAS_HW_SPI+#else+// SW SPI ONLY+enum { SPI_MODE0, SPI_MODE1, SPI_MODE2, _SPI_MODE4 };+typedef uint8_t SPIClass;+#endif++// some modern SPI definitions don't have BitOrder enum+#if (defined(__AVR__) && !defined(ARDUINO_ARCH_MEGAAVR)) || \+ defined(ESP8266) || defined(TEENSYDUINO) || defined(SPARK) || \+ defined(ARDUINO_ARCH_SPRESENSE) || defined(MEGATINYCORE) || \+ defined(DXCORE) || defined(ARDUINO_AVR_ATmega4809) || \+ defined(ARDUINO_AVR_ATmega4808) || defined(ARDUINO_AVR_ATmega3209) || \+ defined(ARDUINO_AVR_ATmega3208) || defined(ARDUINO_AVR_ATmega1609) || \+ defined(ARDUINO_AVR_ATmega1608) || defined(ARDUINO_AVR_ATmega809) || \+ defined(ARDUINO_AVR_ATmega808) || defined(ARDUINO_ARCH_ARC32) || \+ defined(ARDUINO_ARCH_XMC)++typedef enum _BitOrder {+ SPI_BITORDER_MSBFIRST = MSBFIRST,+ SPI_BITORDER_LSBFIRST = LSBFIRST,+} BusIOBitOrder;++#elif defined(ESP32) || defined(__ASR6501__) || defined(__ASR6502__)++// some modern SPI definitions don't have BitOrder enum and have different SPI+// mode defines+typedef enum _BitOrder {+ SPI_BITORDER_MSBFIRST = SPI_MSBFIRST,+ SPI_BITORDER_LSBFIRST = SPI_LSBFIRST,+} BusIOBitOrder;++#else+// Some platforms have a BitOrder enum but its named MSBFIRST/LSBFIRST+#define SPI_BITORDER_MSBFIRST MSBFIRST+#define SPI_BITORDER_LSBFIRST LSBFIRST+typedef BitOrder BusIOBitOrder;+#endif++#if defined(__IMXRT1062__) // Teensy 4.x+// *Warning* I disabled the usage of FAST_PINIO as the set/clear operations+// used in the cpp file are not atomic and can effect multiple IO pins+// and if an interrupt happens in between the time the code reads the register+// and writes out the updated value, that changes one or more other IO pins+// on that same IO port, those change will be clobbered when the updated+// values are written back. A fast version can be implemented that uses the+// ports set and clear registers which are atomic.+// typedef volatile uint32_t BusIO_PortReg;+// typedef uint32_t BusIO_PortMask;+//#define BUSIO_USE_FAST_PINIO++#elif defined(ARDUINO_ARCH_XMC)+#undef BUSIO_USE_FAST_PINIO++#elif defined(__AVR__) || defined(TEENSYDUINO)+typedef volatile uint8_t BusIO_PortReg;+typedef uint8_t BusIO_PortMask;+#define BUSIO_USE_FAST_PINIO++#elif defined(ESP8266) || defined(ESP32) || defined(__SAM3X8E__) || \+ defined(ARDUINO_ARCH_SAMD)+typedef volatile uint32_t BusIO_PortReg;+typedef uint32_t BusIO_PortMask;+#define BUSIO_USE_FAST_PINIO++#elif (defined(__arm__) || defined(ARDUINO_FEATHER52)) && \+ !defined(ARDUINO_ARCH_MBED) && !defined(ARDUINO_ARCH_RP2040) && \+ !defined(ARDUINO_SILABS) && !defined(ARDUINO_UNOR4_MINIMA) && \+ !defined(ARDUINO_UNOR4_WIFI)+typedef volatile uint32_t BusIO_PortReg;+typedef uint32_t BusIO_PortMask;+#if !defined(__ASR6501__) && !defined(__ASR6502__)+#define BUSIO_USE_FAST_PINIO+#endif++#else+#undef BUSIO_USE_FAST_PINIO+#endif++/**! The class which defines how we will talk to this device over SPI **/+class Adafruit_SPIDevice {+public:+#ifdef BUSIO_HAS_HW_SPI+ Adafruit_SPIDevice(int8_t cspin, uint32_t freq = 1000000,+ BusIOBitOrder dataOrder = SPI_BITORDER_MSBFIRST,+ uint8_t dataMode = SPI_MODE0, SPIClass *theSPI = &SPI);+#else+ Adafruit_SPIDevice(int8_t cspin, uint32_t freq = 1000000,+ BusIOBitOrder dataOrder = SPI_BITORDER_MSBFIRST,+ uint8_t dataMode = SPI_MODE0, SPIClass *theSPI = nullptr);+#endif+ Adafruit_SPIDevice(int8_t cspin, int8_t sck, int8_t miso, int8_t mosi,+ uint32_t freq = 1000000,+ BusIOBitOrder dataOrder = SPI_BITORDER_MSBFIRST,+ uint8_t dataMode = SPI_MODE0);+ ~Adafruit_SPIDevice();++ bool begin(void);+ bool read(uint8_t *buffer, size_t len, uint8_t sendvalue = 0xFF);+ bool write(const uint8_t *buffer, size_t len,+ const uint8_t *prefix_buffer = nullptr, size_t prefix_len = 0);+ bool write_then_read(const uint8_t *write_buffer, size_t write_len,+ uint8_t *read_buffer, size_t read_len,+ uint8_t sendvalue = 0xFF);+ bool write_and_read(uint8_t *buffer, size_t len);++ uint8_t transfer(uint8_t send);+ void transfer(uint8_t *buffer, size_t len);+ void beginTransaction(void);+ void endTransaction(void);+ void beginTransactionWithAssertingCS();+ void endTransactionWithDeassertingCS();++private:+#ifdef BUSIO_HAS_HW_SPI+ SPIClass *_spi = nullptr;+ SPISettings *_spiSetting = nullptr;+#else+ uint8_t *_spi = nullptr;+ uint8_t *_spiSetting = nullptr;+#endif+ uint32_t _freq;+ BusIOBitOrder _dataOrder;+ uint8_t _dataMode;+ void setChipSelect(int value);++ int8_t _cs, _sck, _mosi, _miso;+#ifdef BUSIO_USE_FAST_PINIO+ BusIO_PortReg *mosiPort, *clkPort, *misoPort, *csPort;+ BusIO_PortMask mosiPinMask, misoPinMask, clkPinMask, csPinMask;+#endif+ bool _begun;+};++#endif // Adafruit_SPIDevice_hdiff --git a/lib/PN532/README.md b/lib/PN532/README.mdnew file mode 100644index 00000000..012cd93a--- /dev/null+++ b/lib/PN532/README.md@@ -0,0 +1,53 @@+# Adafruit-PN532 [](https://github.com/adafruit/Adafruit-PN532/actions)+++This is a library for the Adafruit PN532 NFC/RFID breakout boards+This library works with the Adafruit NFC breakout++ * https://www.adafruit.com/products/364++Check out the links above for our tutorials and wiring diagrams+These chips use I2C or SPI to communicate.++# Dependencies+* [Adafruit_BusIO](https://github.com/adafruit/Adafruit_BusIO)+++# Contributing++Contributions are welcome! Please read our [Code of Conduct](https://github.com/adafruit/Adafruit-PN532/blob/master/CODE_OF_CONDUCT.md>)+before contributing to help this project stay welcoming.++## Documentation and doxygen+Documentation is produced by doxygen. Contributions should include documentation for any new code added.++Some examples of how to use doxygen can be found in these guide pages:++https://learn.adafruit.com/the-well-automated-arduino-library/doxygen++https://learn.adafruit.com/the-well-automated-arduino-library/doxygen-tips++## Formatting and clang-format+This library uses [`clang-format`](https://releases.llvm.org/download.html) to standardize the formatting of `.cpp` and `.h` files.+Contributions should be formatted using `clang-format`:++The `-i` flag will make the changes to the file.+```bash+clang-format -i *.cpp *.h+```+If you prefer to make the changes yourself, running `clang-format` without the `-i` flag will print out a formatted version of the file. You can save this to a file and diff it against the original to see the changes.++Note that the formatting output by `clang-format` is what the automated formatting checker will expect. Any diffs from this formatting will result in a failed build until they are addressed. Using the `-i` flag is highly recommended.++### clang-format resources+ * [Binary builds and source available on the LLVM downloads page](https://releases.llvm.org/download.html)+ * [Documentation and IDE integration](https://clang.llvm.org/docs/ClangFormat.html)+Adafruit invests time and resources providing this open source code,+please support Adafruit and open-source hardware by purchasing+products from Adafruit!++Written by Limor Fried/Ladyada & Kevin Townsend for Adafruit Industries.+BSD license, check license.txt for more information+All text above must be included in any redistribution++To install, use the Arduino Library Manager and search for "Adafruit PN532" and install the library. Adafruit BusIO library is also requireddiff --git a/lib/PN532/examples/apduToBlackBerry/.esp32.test.skip b/lib/PN532/examples/apduToBlackBerry/.esp32.test.skipnew file mode 100644index 00000000..e69de29bdiff --git a/lib/PN532/examples/apduToBlackBerry/BlackBerry7/.classpath b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/.classpathnew file mode 100755index 00000000..4426948c--- /dev/null+++ b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/.classpath@@ -0,0 +1,7 @@+<?xml version="1.0" encoding="UTF-8"?>+<classpath>+ <classpathentry kind="src" path="src"/>+ <classpathentry kind="src" path="res"/>+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/net.rim.ejde.BlackBerryVMInstallType/BlackBerry JRE 7.0.0"/>+ <classpathentry kind="output" path="bin"/>+</classpath>diff --git a/lib/PN532/examples/apduToBlackBerry/BlackBerry7/.project b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/.projectnew file mode 100755index 00000000..1a8239a2--- /dev/null+++ b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/.project@@ -0,0 +1,29 @@+<?xml version="1.0" encoding="UTF-8"?>+<projectDescription>+ <name>TargetEmulation</name>+ <comment></comment>+ <projects>+ </projects>+ <buildSpec>+ <buildCommand>+ <name>net.rim.ejde.internal.builder.BlackBerryPreprocessBuilder</name>+ <arguments>+ </arguments>+ </buildCommand>+ <buildCommand>+ <name>net.rim.ejde.internal.builder.BlackBerryResourcesBuilder</name>+ <arguments>+ </arguments>+ </buildCommand>+ <buildCommand>+ <name>org.eclipse.jdt.core.javabuilder</name>+ <arguments>+ </arguments>+ </buildCommand>+ </buildSpec>+ <natures>+ <nature>net.rim.ejde.BlackBerryPreProcessNature</nature>+ <nature>net.rim.ejde.BlackBerryProjectCoreNature</nature>+ <nature>org.eclipse.jdt.core.javanature</nature>+ </natures>+</projectDescription>diff --git a/lib/PN532/examples/apduToBlackBerry/BlackBerry7/.settings/org.eclipse.jdt.core.prefs b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/.settings/org.eclipse.jdt.core.prefsnew file mode 100755index 00000000..3f59a3e0--- /dev/null+++ b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/.settings/org.eclipse.jdt.core.prefs@@ -0,0 +1,12 @@+#Fri Jun 22 00:02:13 EDT 2012+eclipse.preferences.version=1+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=disabled+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.2+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve+org.eclipse.jdt.core.compiler.compliance=1.4+org.eclipse.jdt.core.compiler.debug.lineNumber=generate+org.eclipse.jdt.core.compiler.debug.localVariable=generate+org.eclipse.jdt.core.compiler.debug.sourceFile=generate+org.eclipse.jdt.core.compiler.problem.assertIdentifier=ignore+org.eclipse.jdt.core.compiler.problem.enumIdentifier=ignore+org.eclipse.jdt.core.compiler.source=1.3diff --git a/lib/PN532/examples/apduToBlackBerry/BlackBerry7/BlackBerry_App_Descriptor.xml b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/BlackBerry_App_Descriptor.xmlnew file mode 100755index 00000000..aa610d2a--- /dev/null+++ b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/BlackBerry_App_Descriptor.xml@@ -0,0 +1,40 @@+<!--++ Copyright (c) 2013 Research In Motion Limited.++ Licensed under the Apache License, Version 2.0 (the "License");+ you may not use this file except in compliance with the License.+ You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.++-->+<!-- This file has been generated by the BlackBerry Plugin for Eclipse v3.6.0. -->++<Properties ModelVersion="1.1.2">+ <General Title="" Version="1.0.0" Vendor="BlackBerry Developer" Description=""/>+ <Application Type="BlackBerry Application" MainMIDletName="" MainArgs="" HomeScreenPosition="0" StartupTier="7" IsSystemModule="false" IsAutostartup="false"/>+ <Resources hasTitleResource="false" TitleResourceBundleName="" TitleResourceBundleRelativePath="" TitleResourceBundleClassName="" TitleResourceBundleKey="" DescriptionId="">+ <Icons>+ <Icon CanonicalFileName="res\img\icon.png" IsFocus="false"/>+ </Icons>+ </Resources>+ <KeywordResources KeywordResourceBundleName="" KeywordResourceBundleRelativePath="" KeywordResourceBundleClassName="" KeywordResourceBundleKey=""/>+ <Compile OutputCompilerMessages="false" ConvertImages="true" CreateWarningForNoExportedRoutine="true" CompressResources="false" AliasList="">+ <PreprocessorDefines/>+ </Compile>+ <Packaging PreBuildStep="" PostBuildStep="" CleanStep="" OutputFileName="TargetEmulation" OutputFolder="deliverables" GenerateALXFile="true">+ <AlxFiles/>+ </Packaging>+ <HiddenProperties>+ <ClassProtection/>+ <PackageProtection/>+ </HiddenProperties>+ <AlternateEntryPoints/>+</Properties>diff --git a/lib/PN532/examples/apduToBlackBerry/BlackBerry7/build.xml b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/build.xmlnew file mode 100755index 00000000..72bf0c3e--- /dev/null+++ b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/build.xml@@ -0,0 +1,35 @@+<!--++ Copyright (c) 2013 Research In Motion Limited.++ Licensed under the Apache License, Version 2.0 (the "License");+ you may not use this file except in compliance with the License.+ You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.++-->+<project default="all">+ <property name="jde.path" value="c:/Program Files/Research In Motion/BlackBerry JDE 7.1.0" />+ <target name="all" depends="sign,deploy">+ </target>++ <target name="sign">+ <java failonerror="true" fork="true"+ jar="c:/Program Files/Research In Motion/BlackBerry JDE 7.1.0/bin/SignatureTool.jar" args="-p s0ftc0ck -a -c deliverables/Standard/7.0.0/TargetEmulation.cod">+ </java>+ </target>+ <target name="deploy">+ <exec executable="${jde.path}/bin/JavaLoader.exe">+ <arg value="-wpasscc" />+ <arg value="load" />+ <arg value="deliverables/Standard/7.0.0/TargetEmulation.cod" />+ </exec>+ </target>+</project>diff --git a/lib/PN532/examples/apduToBlackBerry/BlackBerry7/res/img/icon.png b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/res/img/icon.pngnew file mode 100755index 00000000..193950ceBinary files /dev/null and b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/res/img/icon.png differdiff --git a/lib/PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/ColorMixer.java b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/ColorMixer.javanew file mode 100755index 00000000..d81f802b--- /dev/null+++ b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/ColorMixer.java@@ -0,0 +1,152 @@+/**+ Copyright (c) 2011, 2012, 2013 Research In Motion Limited.++ Licensed under the Apache License, Version 2.0 (the "License");+ you may not use this file except in compliance with the License.+ You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.+**/+package mypackage;++import net.rim.device.api.ui.Field;+import net.rim.device.api.ui.Graphics;+import net.rim.device.api.ui.TouchEvent;++public class ColorMixer extends Field {+ private static final int[] colors = new int[] { 0xff0000, 0xff00, 0xff };+ private int barGap = 5;++ private int barHeight = 64;+ private int color;+ private MyScreen owner;++ private double r, g, b;++ public ColorMixer(MyScreen owner) {+ this.owner = owner;+ }++ protected void layout(int width, int height) {+ setExtent(640, barHeight * 3 + barGap * 2);+ }++ protected void paint(Graphics graphics) {+ int barWidth = getWidth() - barHeight * 3 - barGap * 3;+ double[] values = new double[] { r, g, b };++ // Draw color sliders+ for (int i = 0; i < 3; ++i) {+ int top = i * (barHeight + barGap);+ int toDraw = (int) (values[i] * barWidth + 0.5);+ graphics.setColor(0);+ graphics.fillRect(toDraw, top, barWidth - toDraw, barHeight);++ graphics.setColor(colors[i]);+ graphics.fillRect(0, top, toDraw, barHeight);++ graphics.setColor(0x7f7f7f);+ graphics.drawRect(0, top, barWidth, barHeight);+ }++ // Draw mixed down color+ int boxWidth = barHeight * 3 + barGap * 2;+ graphics.setColor(color);+ graphics.fillRect(getWidth() - 1 - boxWidth, 0, boxWidth, boxWidth);++ graphics.setColor(0x7f7f7f);+ graphics.drawRect(getWidth() - 1 - boxWidth, 0, boxWidth, boxWidth);+ }++ public void setColor(int color) {+ if (color != this.color) {+ this.color = color;+ r = ((color >> 16) & 0xff) / 255.0;+ g = ((color >> 8) & 0xff) / 255.0;+ b = (color & 0xff) / 255.0;+ invalidate();+ }+ }++ private void setPart(int which, double value) {+ double source = which == 0 ? r : which == 1 ? g : b;+ if (value == source) {+ return;+ }+ int bv = (int) (value * 255 + 0.5);+ bv &= 0xff;+ if (which == 0) {+ r = value;+ color &= 0xffff;+ color |= bv << 16;+ } else if (which == 1) {+ g = value;+ color &= 0xff00ff;+ color |= bv << 8;+ } else {+ b = value;+ color &= 0xffff00;+ color |= bv;+ }+ owner.setColor(color);+ invalidate();+ }++ private int touchTarget = -1;++ protected boolean touchEvent(TouchEvent message) {+ int event = message.getEvent();++ int x = message.getX(1);+ int y = message.getY(1);++ int barWidth = getWidth() - barHeight * 3 - barGap * 3;++ if (event == TouchEvent.UP) {+ touchTarget = -1;+ return false;+ } else if (event == TouchEvent.DOWN) {+ if (y < -5 || y > getWidth() + 5) {+ return false;+ }++ if (x <= barWidth) {+ for (int i = 0; i < 3; ++i) {+ if (y < 0) {+ break;+ }+ if (y < barHeight) {+ touchTarget = i;+ break;+ }+ y -= barHeight;+ y -= barGap;+ }+ } else {+ return false;+ }+ }++ if (touchTarget == -1) {+ return false;+ }++ double newValue = 1.0 * x / barWidth;+ if (newValue < 0) {+ newValue = 0;+ }+ if (newValue > 1) {+ newValue = 1;+ }++ setPart(touchTarget, newValue);++ return true;+ }+}diff --git a/lib/PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/MyApp.java b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/MyApp.javanew file mode 100755index 00000000..4da1da5a--- /dev/null+++ b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/MyApp.java@@ -0,0 +1,47 @@+/**+ Copyright (c) 2011, 2012, 2013 Research In Motion Limited.++ Licensed under the Apache License, Version 2.0 (the "License");+ you may not use this file except in compliance with the License.+ You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.+**/+package mypackage;++import net.rim.device.api.ui.UiApplication;++/**+ * This class extends the UiApplication class, providing a+ * graphical user interface.+ */+public class MyApp extends UiApplication+{+ /**+ * Entry point for application+ * @param args Command line arguments (not used)+ */+ public static void main(String[] args)+ {+ // Create a new instance of the application and make the currently+ // running thread the application's event dispatch thread.+ MyApp theApp = new MyApp();+ theApp.enterEventDispatcher();+ }+++ /**+ * Creates a new MyApp object+ */+ public MyApp()+ {+ // Push a screen onto the UI stack for rendering.+ pushScreen(new MyScreen());+ }+}diff --git a/lib/PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/MyScreen.java b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/MyScreen.javanew file mode 100755index 00000000..0f80667b--- /dev/null+++ b/lib/PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/MyScreen.java@@ -0,0 +1,163 @@+/**+ Copyright (c) 2011, 2012, 2013 Research In Motion Limited.++ Licensed under the Apache License, Version 2.0 (the "License");+ you may not use this file except in compliance with the License.+ You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.+**/+package mypackage;++import net.rim.device.api.io.nfc.NFCException;+import net.rim.device.api.io.nfc.emulation.VirtualISO14443Part4TargetCallback;+import net.rim.device.api.io.nfc.emulation.VirtualISO14443Part4TypeATarget;+import net.rim.device.api.ui.Field;+import net.rim.device.api.ui.FieldChangeListener;+import net.rim.device.api.ui.component.CheckboxField;+import net.rim.device.api.ui.component.LabelField;+import net.rim.device.api.ui.container.MainScreen;+import net.rim.device.api.ui.decor.BackgroundFactory;++/**+ * A class extending the MainScreen class, which provides default standard+ * behavior for BlackBerry GUI applications.+ */+public final class MyScreen extends MainScreen implements+ VirtualISO14443Part4TargetCallback, FieldChangeListener {++ private int color = 0x7f7f7f;++ private boolean colorQueued = false;+ private CheckboxField colorQueueLocked;+ private VirtualISO14443Part4TypeATarget emulated;+ private ColorMixer mixer;++ private LabelField status;++ /**+ * Creates a new MyScreen object+ */+ public MyScreen() {+ // Set the displayed title of the screen+ setTitle("Colour Control Setter");+ setBackground(BackgroundFactory.createSolidBackground(0x3f3f3f));++ add(new LabelField(""));+ add(colorQueueLocked = new CheckboxField("Lock Color", false));+ colorQueueLocked.setChangeListener(this);++ add(mixer = new ColorMixer(this));+ mixer.setColor(0x7f7f7f);++ add(status = new LabelField(""));++ try {+ emulated = new VirtualISO14443Part4TypeATarget(this, "10451045",+ null);+ log("created");+ emulated.startEmulation();+ log("emulating");+ } catch (NFCException e) {+ log("Failed to listen");+ status.setText(e.getClass().getName() + " " + e.getMessage());+ e.printStackTrace();+ }+ }++ // private TextField textField;++ public void fieldChanged(Field field, int context) {+ if (colorQueueLocked.getChecked()) {+ setColor(color);+ } else {+ colorQueued = false;+ status.setText("");+ }+ }++ private boolean isColorQueueLocked() {+ return colorQueueLocked.getChecked();+ }++ protected void log(String message) {+ System.out.println("SUPERDUPER: " + message);+ // textField.setText(textField.getText() + "\n" + message);+ }++ public void onVirtualTargetEvent(int targetEvent) {+ log("Target event: " + targetEvent);+ }++ public byte[] processCommand(byte[] command) {+ byte[] toReturn = processCommandInternal(command);+ String message = "[" + toString(command) + "," + toString(toReturn)+ + "]";+ // status.setText(message);+ // log(message);+ return toReturn;+ }++ private byte[] processCommandInternal(byte[] command) {+ if (command == null) {+ return new byte[0];+ }+ int messageType = command[0] & 0xff;+ switch (messageType) {+ case 1:+ if (command.length == 4) {+ int colorIn = (command[1] & 0xff) << 16;+ colorIn |= (command[2] & 0xff) << 8;+ colorIn |= (command[3] & 0xff);++ if (colorIn != color) {+ if (colorQueued) {++ byte[] toReturn = new byte[4];+ toReturn[0] = 1;+ toReturn[1] = (byte) (color >> 16);+ toReturn[2] = (byte) (color >> 8);+ toReturn[3] = (byte) color;++ if (!isColorQueueLocked()) {+ colorQueued = false;+ status.setText("");+ }++ return toReturn;+ } else {+ this.color = colorIn;+ mixer.setColor(colorIn);+ }+ }+ }+ break;+ }+ return new byte[] { 0 };+ }++ public void setColor(int color) {+ colorQueued = true;+ this.color = color;+ status.setText("#" + toHex(color));+ }++ private String toHex(int i) {+ String toReturn = "000000" + Integer.toString(i, 16);+ return toReturn.substring(toReturn.length() - 6);+ }++ private String toString(byte[] command) {+ String toReturn = "";+ for (int i = 0; i < command.length; ++i) {+ toReturn += Integer.toString(command[i] & 0xff, 16) + " ";+ }+ return toReturn;+ }+}diff --git a/lib/PN532/examples/apduToBlackBerry/apduToBlackBerry.ino b/lib/PN532/examples/apduToBlackBerry/apduToBlackBerry.inonew file mode 100755index 00000000..0b5269a9--- /dev/null+++ b/lib/PN532/examples/apduToBlackBerry/apduToBlackBerry.ino@@ -0,0 +1,148 @@+/**+ Copyright (c) 2011, 2012, 2013 Research In Motion Limited.++ Licensed under the Apache License, Version 2.0 (the "License");+ you may not use this file except in compliance with the License.+ You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.+**/+#include <Wire.h>+#include <SPI.h>+#include <Adafruit_PN532.h>++// If using the breakout with SPI, define the pins for SPI communication.+#define PN532_SCK (2)+#define PN532_MOSI (3)+#define PN532_SS (4)+#define PN532_MISO (5)++// If using the breakout or shield with I2C, define just the pins connected+// to the IRQ and reset lines. Use the values below (2, 3) for the shield!+#define PN532_IRQ (2)+#define PN532_RESET (3) // Not connected by default on the NFC Shield++// Uncomment just _one_ line below depending on how your breakout or shield+// is connected to the Arduino:++// Use this line for a breakout with a SPI connection:+Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);++// Use this line for a breakout with a hardware SPI connection. Note that+// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's+// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are+// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.+//Adafruit_PN532 nfc(PN532_SS);++// Or use this line for a breakout or shield with an I2C connection:+//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);++// PWM LED will be on the following PINs.+#define R_PIN (9)+#define G_PIN (10)+#define B_PIN (11)++// Initial values of RGB.+uint8_t r = 0x00;+uint8_t g = 0x00;+uint8_t b = 0x7f;++/**+ * Write the current color to the output pins.+ */+void showColor() {+ analogWrite(R_PIN,r);+ analogWrite(G_PIN,g);+ analogWrite(B_PIN,b);+}+++void setup() {+ Serial.begin(115200);+ while (!Serial) delay(10); // for Leonardo/Micro/Zero++ pinMode(R_PIN,OUTPUT);+ pinMode(G_PIN,OUTPUT);+ pinMode(B_PIN,OUTPUT);+ showColor();++ nfc.begin();++ uint32_t versiondata = nfc.getFirmwareVersion();+ if (! versiondata) {+ Serial.println("Did not find the shield - locking up");+ while (true) {+ }+ }++ Serial.print("Found chip PN5");+ Serial.println((versiondata>>24) & 0xFF, HEX);+ Serial.print("Firmware ver. ");+ Serial.print((versiondata>>16) & 0xFF, DEC);+ Serial.print('.');+ Serial.println((versiondata>>8) & 0xFF, DEC);++ nfc.begin();+}++uint8_t message[4];++void loop(void) {+ uint8_t i;++ // Serial.println("Listening...");+ if (nfc.inListPassiveTarget()) {+ // Serial.println("Something's there...");+ while(true) {+ message[0] = 1;+ message[1] = r;+ message[2] = g;+ message[3] = b;+ uint8_t responseLength = sizeof(message);+ if (nfc.inDataExchange(message,sizeof(message),message,&responseLength)) {+ uint8_t command = message[0];+ switch(command) {+ case 0:+ // NOP+ break;+ case 1:+ if (responseLength==4) {+ r = message[1];+ g = message[2];+ b = message[3];+ showColor();+ //Serial.print("Read a color: ");+ //for (i=0; i<3; ++i) {+ // Serial.print(message[i+1],HEX);+ // Serial.print(' ');+ //}+ //Serial.println();+ }+ else {+ //Serial.println("Doesn't seem to be a color...");+ }+ break;+ default:+ //Serial.print("Unknown command ");+ //Serial.println(message[0]);+ ;+ }+ delay(10);+ }+ else {+ //Serial.println("It's gone...");+ break;+ }+ }+ }+ else {+ //Serial.print("Trying again...");+ }+}+diff --git a/lib/PN532/examples/felica_polling/felica_polling.ino b/lib/PN532/examples/felica_polling/felica_polling.inonew file mode 100644index 00000000..61acedf7--- /dev/null+++ b/lib/PN532/examples/felica_polling/felica_polling.ino@@ -0,0 +1,94 @@+/**************************************************************************/+/*!+ @file iso14443a_uid.pde+ @author Andrea Canale(andreock)+ @license BSD (see license.txt)++ This example will attempt to connect to an ISO14443A+ card or tag and retrieve some basic information about it+ that can be used to determine what type of card it is.++ Note that you need the baud rate to be 115200 because we need to print+ out the data and read from the card at the same time!++This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards+This library works with the Adafruit NFC breakout+ ----> https://www.adafruit.com/products/364++Check out the links above for our tutorials and wiring diagrams+These chips use SPI or I2C to communicate.++Adafruit invests time and resources providing this open source code,+please support Adafruit and open-source hardware by purchasing+products from Adafruit!++*/+/**************************************************************************/+#include <Wire.h>+#include <SPI.h>+#include <Adafruit_PN532.h>++// If using the breakout with SPI, define the pins for SPI communication.+#define PN532_SCK (2)+#define PN532_MOSI (3)+#define PN532_SS (4)+#define PN532_MISO (5)++// If using the breakout or shield with I2C, define just the pins connected+// to the IRQ and reset lines. Use the values below (2, 3) for the shield!+#define PN532_IRQ (2)+#define PN532_RESET (3) // Not connected by default on the NFC Shield++// Uncomment just _one_ line below depending on how your breakout or shield+// is connected to the Arduino:++// Use this line for a breakout with a SPI connection:+Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);++// Use this line for a breakout with a hardware SPI connection. Note that+// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's+// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are+// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.+//Adafruit_PN532 nfc(PN532_SS);++// Or use this line for a breakout or shield with an I2C connection:+//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);++#define DEFAULT_SYSTEM_CODE 0xFFFF+#define DEFAULT_REQUEST_CODE 0x01++void setup() {+ Serial.begin(115200);+ while (!Serial) delay(10); // for Leonardo/Micro/Zero++ Serial.println("Looking for PN532...");++ nfc.begin();++ uint32_t versiondata = nfc.getFirmwareVersion();+ if (! versiondata) {+ Serial.print("Didn't find PN53x board");+ while (1); // halt+ }++ // Got ok data, print it out!+ Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);+ Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);+ Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);+}+++void loop() {+ uint8_t idm[8] = { 0 };+ uint8_t pmm[8] = { 0 };+ uint16_t sys_code[4] = { 0 };+ int polling_result = nfc.felica_Polling(DEFAULT_SYSTEM_CODE, DEFAULT_REQUEST_CODE, idm, pmm, sys_code);+ if (polling_result < 0)+ {+ Serial.print("Failed to poll with result: ");+ Serial.println(polling_result);+ }else+ Serial.println("Found FeliCa card!");++ delay(2000); // Wait 2000ms for the next polling+}diff --git a/lib/PN532/examples/iso14443a_uid/iso14443a_uid.ino b/lib/PN532/examples/iso14443a_uid/iso14443a_uid.inonew file mode 100644index 00000000..069e73e9--- /dev/null+++ b/lib/PN532/examples/iso14443a_uid/iso14443a_uid.ino@@ -0,0 +1,110 @@+/**************************************************************************/+/*!+ @file iso14443a_uid.pde+ @author Adafruit Industries+ @license BSD (see license.txt)++ This example will attempt to connect to an ISO14443A+ card or tag and retrieve some basic information about it+ that can be used to determine what type of card it is.++ Note that you need the baud rate to be 115200 because we need to print+ out the data and read from the card at the same time!++This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards+This library works with the Adafruit NFC breakout+ ----> https://www.adafruit.com/products/364++Check out the links above for our tutorials and wiring diagrams+These chips use SPI or I2C to communicate.++Adafruit invests time and resources providing this open source code,+please support Adafruit and open-source hardware by purchasing+products from Adafruit!++*/+/**************************************************************************/+#include <Wire.h>+#include <SPI.h>+#include <Adafruit_PN532.h>++// If using the breakout with SPI, define the pins for SPI communication.+#define PN532_SCK (2)+#define PN532_MOSI (3)+#define PN532_SS (4)+#define PN532_MISO (5)++// If using the breakout or shield with I2C, define just the pins connected+// to the IRQ and reset lines. Use the values below (2, 3) for the shield!+#define PN532_IRQ (2)+#define PN532_RESET (3) // Not connected by default on the NFC Shield++// Uncomment just _one_ line below depending on how your breakout or shield+// is connected to the Arduino:++// Use this line for a breakout with a SPI connection:+Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);++// Use this line for a breakout with a hardware SPI connection. Note that+// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's+// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are+// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.+//Adafruit_PN532 nfc(PN532_SS);++// Or use this line for a breakout or shield with an I2C connection:+//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);++void setup(void) {+ Serial.begin(115200);+ while (!Serial) delay(10); // for Leonardo/Micro/Zero+ Serial.println("Hello!");++ nfc.begin();++ uint32_t versiondata = nfc.getFirmwareVersion();+ if (! versiondata) {+ Serial.print("Didn't find PN53x board");+ while (1); // halt+ }++ // Got ok data, print it out!+ Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);+ Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);+ Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);++ // Set the max number of retry attempts to read from a card+ // This prevents us from waiting forever for a card, which is+ // the default behaviour of the PN532.+ nfc.setPassiveActivationRetries(0xFF);++ Serial.println("Waiting for an ISO14443A card");+}++void loop(void) {+ boolean success;+ uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID+ uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)++ // Wait for an ISO14443A type cards (Mifare, etc.). When one is found+ // 'uid' will be populated with the UID, and uidLength will indicate+ // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)+ success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);++ if (success) {+ Serial.println("Found a card!");+ Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");+ Serial.print("UID Value: ");+ for (uint8_t i=0; i < uidLength; i++)+ {+ Serial.print(" 0x");Serial.print(uid[i], HEX);+ }+ Serial.println("");+ // Wait 1 second before continuing+ delay(1000);+ }+ else+ {+ // PN532 probably timed out waiting for a card+ Serial.println("Timed out waiting for a card");+ }+}diff --git a/lib/PN532/examples/iso14443as_target/iso14443as_target.ino b/lib/PN532/examples/iso14443as_target/iso14443as_target.inonew file mode 100644index 00000000..a3a7a2a2--- /dev/null+++ b/lib/PN532/examples/iso14443as_target/iso14443as_target.ino@@ -0,0 +1,106 @@+/**************************************************************************/+/*!+ @file iso14443as_target.pde+ @original Adafruit Industries+ @modified Salvador Mendoza(@Netxing)+ @license BSD (see license.txt)++ This example will attempt to mimic an ISO14443A smart card+ and retrieve some basic information from a PoS or terminal,+ this can be used to establish a communication process.++ Note that you need the baud rate to be 115200 because we need to print+ out the data and read from the card at the same time!++This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards+This library works with the Adafruit NFC breakout+ ----> https://www.adafruit.com/products/364++Check out the links above for our tutorials and wiring diagrams+These chips use SPI or I2C to communicate.++Adafruit invests time and resources providing this open source code,+please support Adafruit and open-source hardware by purchasing+products from Adafruit!++*/+/**************************************************************************/+#include <Wire.h>+#include <SPI.h>+#include <Adafruit_PN532.h>++// If using the breakout with SPI, define the pins for SPI communication.+#define PN532_SCK (2)+#define PN532_MOSI (3)+#define PN532_SS (4)+#define PN532_MISO (5)++// If using the breakout or shield with I2C, define just the pins connected+// to the IRQ and reset lines. Use the values below (2, 3) for the shield!+#define PN532_IRQ (2)+#define PN532_RESET (3) // Not connected by default on the NFC Shield++// Uncomment just _one_ line below depending on how your breakout or shield+// is connected to the Arduino:++// Use this line for a breakout with a SPI connection:+Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);++// Use this line for a breakout with a hardware SPI connection. Note that+// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's+// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are+// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.+//Adafruit_PN532 nfc(PN532_SS);++// Or use this line for a breakout or shield with an I2C connection:+//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);++void setup(void) {+ Serial.begin(115200);+ while (!Serial) delay(10); // for Leonardo/Micro/Zero++ Serial.println("Hello!");++ nfc.begin();++ uint32_t versiondata = nfc.getFirmwareVersion();+ if (! versiondata) {+ Serial.print("Didn't find PN53x board");+ while (1); // halt+ }++ // Got ok data, print it out!+ Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);+ Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);+ Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);++ // Set the max number of retry attempts to read from a card+ // This prevents us from waiting forever for a card, which is+ // the default behaviour of the PN532.+ nfc.setPassiveActivationRetries(0xFF);++ Serial.println("As Target... Approach the NFC PN532 Board to a PoS or terminal!");+ delay(200);+}++void loop(void) {+ uint8_t apdubuffer[255] = {}, apdulen = 0;+ uint8_t ppse[] = {0x8E, 0x6F, 0x23, 0x84, 0x0E, 0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31, 0xA5, 0x11, 0xBF, 0x0C, 0x0E, 0x61, 0x0C, 0x4F, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0x87, 0x01, 0x01, 0x90, 0x00};+ nfc.AsTarget();+ (void)nfc.getDataTarget(apdubuffer, &apdulen); //Read initial APDU+ if (apdulen>0){+ for (uint8_t i = 0; i < apdulen; i++){+ Serial.print(" 0x"); Serial.print(apdubuffer[i], HEX);+ }+ Serial.println("");+ }+ nfc.setDataTarget(ppse, sizeof(ppse)); //Mimic a smart card response with a PPSE APDU+ nfc.getDataTarget(apdubuffer, &apdulen); //Read respond from the PoS or Terminal+ if (apdulen>0){+ for (uint8_t i = 0; i < apdulen; i++){+ Serial.print(" 0x"); Serial.print(apdubuffer[i], HEX);+ }+ Serial.println("");+ }+ delay(1000);+}diff --git a/lib/PN532/examples/mifareclassic_formatndef/mifareclassic_formatndef.ino b/lib/PN532/examples/mifareclassic_formatndef/mifareclassic_formatndef.inonew file mode 100644index 00000000..feeb083b--- /dev/null+++ b/lib/PN532/examples/mifareclassic_formatndef/mifareclassic_formatndef.ino@@ -0,0 +1,201 @@+/**************************************************************************/+/*!+ @file mifareclassic_formatndef.pde+ @author Adafruit Industries+ @license BSD (see license.txt)++ This example attempts to format a clean Mifare Classic 1K card as+ an NFC Forum tag (to store NDEF messages that can be read by any+ NFC enabled Android phone, etc.)++ Note that you need the baud rate to be 115200 because we need to print+ out the data and read from the card at the same time!++ This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards+ This library works with the Adafruit NFC Shield+ ----> https://www.adafruit.com/products/789++ Check out the links above for our tutorials and wiring diagrams+ These chips use SPI or I2C to communicate++ Adafruit invests time and resources providing this open source code,+ please support Adafruit and open-source hardware by purchasing+ products from Adafruit!++*/+/**************************************************************************/++#include <Wire.h>+#include <SPI.h>+#include <Adafruit_PN532.h>++// If using the breakout with SPI, define the pins for SPI communication.+#define PN532_SCK (2)+#define PN532_MOSI (3)+#define PN532_SS (4)+#define PN532_MISO (5)++// If using the breakout or shield with I2C, define just the pins connected+// to the IRQ and reset lines. Use the values below (2, 3) for the shield!+#define PN532_IRQ (2)+#define PN532_RESET (3) // Not connected by default on the NFC Shield++// Uncomment just _one_ line below depending on how your breakout or shield+// is connected to the Arduino:++// Use this line for a breakout with a SPI connection:+Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);++// Use this line for a breakout with a hardware SPI connection. Note that+// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's+// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are+// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.+//Adafruit_PN532 nfc(PN532_SS);++// Or use this line for a breakout or shield with an I2C connection:+//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);++/*+ We can encode many different kinds of pointers to the card,+ from a URL, to an Email address, to a phone number, and many more+ check the library header .h file to see the large # of supported+ prefixes!+*/+// For a http://www. url:+const char * url = "adafruit.com";+uint8_t ndefprefix = NDEF_URIPREFIX_HTTP_WWWDOT;++// for an email address+//const char * url = "[email protected]";+//uint8_t ndefprefix = NDEF_URIPREFIX_MAILTO;++// for a phone number+//const char * url = "+1 212 555 1212";+//uint8_t ndefprefix = NDEF_URIPREFIX_TEL;+++void setup(void) {+ Serial.begin(115200);+ while (!Serial) delay(10); // for Leonardo/Micro/Zero++ Serial.println("Looking for PN532...");++ nfc.begin();++ uint32_t versiondata = nfc.getFirmwareVersion();+ if (! versiondata) {+ Serial.print("Didn't find PN53x board");+ while (1); // halt+ }++ // Got ok data, print it out!+ Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);+ Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);+ Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);+}++void loop(void) {+ uint8_t success; // Flag to check if there was an error with the PN532+ uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID+ uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)++ // Use the default key+ uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };++ Serial.println("");+ Serial.println("PLEASE NOTE: Formatting your card for NDEF records will change the");+ Serial.println("authentication keys. To reformat your NDEF tag as a clean Mifare");+ Serial.println("Classic tag, use the mifareclassic_ndeftoclassic example!");+ Serial.println("");+ Serial.println("Place your Mifare Classic card on the reader to format with NDEF");+ Serial.println("and press any key to continue ...");+ // Wait for user input before proceeding+ while (!Serial.available());+ // a key was pressed1+ while (Serial.available()) Serial.read();++ // Wait for an ISO14443A type card (Mifare, etc.). When one is found+ // 'uid' will be populated with the UID, and uidLength will indicate+ // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)+ success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);++ if (success)+ {+ // Display some basic information about the card+ Serial.println("Found an ISO14443A card");+ Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");+ Serial.print(" UID Value: ");+ nfc.PrintHex(uid, uidLength);+ Serial.println("");++ // Make sure this is a Mifare Classic card+ if (uidLength != 4)+ {+ Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!");+ return;+ }++ // We probably have a Mifare Classic card ...+ Serial.println("Seems to be a Mifare Classic card (4 byte UID)");++ // Try to format the card for NDEF data+ success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 0, 0, keya);+ if (!success)+ {+ Serial.println("Unable to authenticate block 0 to enable card formatting!");+ return;+ }+ success = nfc.mifareclassic_FormatNDEF();+ if (!success)+ {+ Serial.println("Unable to format the card for NDEF");+ return;+ }++ Serial.println("Card has been formatted for NDEF data using MAD1");++ // Try to authenticate block 4 (first block of sector 1) using our key+ success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 4, 0, keya);++ // Make sure the authentification process didn't fail+ if (!success)+ {+ Serial.println("Authentication failed.");+ return;+ }++ // Try to write a URL+ Serial.println("Writing URI to sector 1 as an NDEF Message");++ // Authenticated seems to have worked+ // Try to write an NDEF record to sector 1+ // Use 0x01 for the URI Identifier Code to prepend "http://www."+ // to the url (and save some space). For information on URI ID Codes+ // see http://www.ladyada.net/wiki/private/articlestaging/nfc/ndef+ if (strlen(url) > 38)+ {+ // The length is also checked in the WriteNDEFURI function, but lets+ // warn users here just in case they change the value and it's bigger+ // than it should be+ Serial.println("URI is too long ... must be less than 38 characters long");+ return;+ }++ // URI is within size limits ... write it to the card and report success/failure+ success = nfc.mifareclassic_WriteNDEFURI(1, ndefprefix, url);+ if (success)+ {+ Serial.println("NDEF URI Record written to sector 1");+ }+ else+ {+ Serial.println("NDEF Record creation failed! :(");+ }+ }++ // Wait a bit before trying again+ Serial.println("\n\nDone!");+ delay(1000);+ Serial.flush();+ while(Serial.available()) Serial.read();+}diff --git a/lib/PN532/examples/mifareclassic_memdump/mifareclassic_memdump.ino b/lib/PN532/examples/mifareclassic_memdump/mifareclassic_memdump.inonew file mode 100644index 00000000..5a502227--- /dev/null+++ b/lib/PN532/examples/mifareclassic_memdump/mifareclassic_memdump.ino@@ -0,0 +1,190 @@+/**************************************************************************/+/*!+ @file mifareclassic_memdump.pde+ @author Adafruit Industries+ @license BSD (see license.txt)++ This example attempts to dump the contents of a Mifare Classic 1K card++ Note that you need the baud rate to be 115200 because we need to print+ out the data and read from the card at the same time!++ This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards+ This library works with the Adafruit NFC breakout+ ----> https://www.adafruit.com/products/364++ Check out the links above for our tutorials and wiring diagrams+ These chips use SPI or I2C to communicate++ Adafruit invests time and resources providing this open source code,+ please support Adafruit and open-source hardware by purchasing+ products from Adafruit!++*/+/**************************************************************************/++#include <Wire.h>+#include <SPI.h>+#include <Adafruit_PN532.h>++// If using the breakout with SPI, define the pins for SPI communication.+#define PN532_SCK (2)+#define PN532_MOSI (3)+#define PN532_SS (4)+#define PN532_MISO (5)++// If using the breakout or shield with I2C, define just the pins connected+// to the IRQ and reset lines. Use the values below (2, 3) for the shield!+#define PN532_IRQ (2)+#define PN532_RESET (3) // Not connected by default on the NFC Shield++// Uncomment just _one_ line below depending on how your breakout or shield+// is connected to the Arduino:++// Use this line for a breakout with a SPI connection:+Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);++// Use this line for a breakout with a hardware SPI connection. Note that+// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's+// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are+// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.+//Adafruit_PN532 nfc(PN532_SS);++// Or use this line for a breakout or shield with an I2C connection:+//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);++void setup(void) {+ // has to be fast to dump the entire memory contents!+ Serial.begin(115200);+ while (!Serial) delay(10); // for Leonardo/Micro/Zero++ Serial.println("Looking for PN532...");++ nfc.begin();++ uint32_t versiondata = nfc.getFirmwareVersion();+ if (! versiondata) {+ Serial.print("Didn't find PN53x board");+ while (1); // halt+ }+ // Got ok data, print it out!+ Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);+ Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);+ Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);++ Serial.println("Waiting for an ISO14443A Card ...");+}+++void loop(void) {+ uint8_t success; // Flag to check if there was an error with the PN532+ uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID+ uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)+ uint8_t currentblock; // Counter to keep track of which block we're on+ bool authenticated = false; // Flag to indicate if the sector is authenticated+ uint8_t data[16]; // Array to store block data during reads++ // Keyb on NDEF and Mifare Classic should be the same+ uint8_t keyuniversal[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };++ // Wait for an ISO14443A type cards (Mifare, etc.). When one is found+ // 'uid' will be populated with the UID, and uidLength will indicate+ // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)+ success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);++ if (success) {+ // Display some basic information about the card+ Serial.println("Found an ISO14443A card");+ Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");+ Serial.print(" UID Value: ");+ nfc.PrintHex(uid, uidLength);+ Serial.println("");++ if (uidLength == 4)+ {+ // We probably have a Mifare Classic card ...+ Serial.println("Seems to be a Mifare Classic card (4 byte UID)");++ // Now we try to go through all 16 sectors (each having 4 blocks)+ // authenticating each sector, and then dumping the blocks+ for (currentblock = 0; currentblock < 64; currentblock++)+ {+ // Check if this is a new block so that we can reauthenticate+ if (nfc.mifareclassic_IsFirstBlock(currentblock)) authenticated = false;++ // If the sector hasn't been authenticated, do so first+ if (!authenticated)+ {+ // Starting of a new sector ... try to to authenticate+ Serial.print("------------------------Sector ");Serial.print(currentblock/4, DEC);Serial.println("-------------------------");+ if (currentblock == 0)+ {+ // This will be 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for Mifare Classic (non-NDEF!)+ // or 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 for NDEF formatted cards using key a,+ // but keyb should be the same for both (0xFF 0xFF 0xFF 0xFF 0xFF 0xFF)+ success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, currentblock, 1, keyuniversal);+ }+ else+ {+ // This will be 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for Mifare Classic (non-NDEF!)+ // or 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 for NDEF formatted cards using key a,+ // but keyb should be the same for both (0xFF 0xFF 0xFF 0xFF 0xFF 0xFF)+ success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, currentblock, 1, keyuniversal);+ }+ if (success)+ {+ authenticated = true;+ }+ else+ {+ Serial.println("Authentication error");+ }+ }+ // If we're still not authenticated just skip the block+ if (!authenticated)+ {+ Serial.print("Block ");Serial.print(currentblock, DEC);Serial.println(" unable to authenticate");+ }+ else+ {+ // Authenticated ... we should be able to read the block now+ // Dump the data into the 'data' array+ success = nfc.mifareclassic_ReadDataBlock(currentblock, data);+ if (success)+ {+ // Read successful+ Serial.print("Block ");Serial.print(currentblock, DEC);+ if (currentblock < 10)+ {+ Serial.print(" ");+ }+ else+ {+ Serial.print(" ");+ }+ // Dump the raw data+ nfc.PrintHexChar(data, 16);+ }+ else+ {+ // Oops ... something happened+ Serial.print("Block ");Serial.print(currentblock, DEC);+ Serial.println(" unable to read this block");+ }+ }+ }+ }+ else+ {+ Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!");+ }+ }+ // Wait a bit before trying again+ Serial.println("\n\nSend a character to run the mem dumper again!");+ Serial.flush();+ while (!Serial.available());+ while (Serial.available()) {+ Serial.read();+ }+ Serial.flush();+}diff --git a/lib/PN532/examples/mifareclassic_ndeftoclassic/mifareclassic_ndeftoclassic.ino b/lib/PN532/examples/mifareclassic_ndeftoclassic/mifareclassic_ndeftoclassic.inonew file mode 100644index 00000000..1c9a06af--- /dev/null+++ b/lib/PN532/examples/mifareclassic_ndeftoclassic/mifareclassic_ndeftoclassic.ino@@ -0,0 +1,207 @@+/**************************************************************************/+/*!+ @file mifareclassic_ndeftoclassic.pde+ @author KTOWN (Adafruit Industries)+ @license BSD (see license.txt)++ This examples attempts to take a Mifare Classic 1K card that has been+ formatted for NDEF messages using mifareclassic_formatndef, and resets+ the authentication keys back to the Mifare Classic defaults++ This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards+ This library works with the Adafruit NFC Shield+ ----> https://www.adafruit.com/products/789++ Check out the links above for our tutorials and wiring diagrams+ These chips use SPI or I2C to communicate++ Adafruit invests time and resources providing this open source code,+ please support Adafruit and open-source hardware by purchasing+ products from Adafruit!++*/+/**************************************************************************/++#include <Wire.h>+#include <SPI.h>+#include <Adafruit_PN532.h>++// If using the breakout with SPI, define the pins for SPI communication.+#define PN532_SCK (2)+#define PN532_MOSI (3)+#define PN532_SS (4)+#define PN532_MISO (5)++// If using the breakout or shield with I2C, define just the pins connected+// to the IRQ and reset lines. Use the values below (2, 3) for the shield!+#define PN532_IRQ (2)+#define PN532_RESET (3) // Not connected by default on the NFC Shield++// Uncomment just _one_ line below depending on how your breakout or shield+// is connected to the Arduino:++// Use this line for a breakout with a SPI connection:+Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);++// Use this line for a breakout with a hardware SPI connection. Note that+// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's+// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are+// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.+//Adafruit_PN532 nfc(PN532_SS);++// Or use this line for a breakout or shield with an I2C connection:+//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);++#define NR_SHORTSECTOR (32) // Number of short sectors on Mifare 1K/4K+#define NR_LONGSECTOR (8) // Number of long sectors on Mifare 4K+#define NR_BLOCK_OF_SHORTSECTOR (4) // Number of blocks in a short sector+#define NR_BLOCK_OF_LONGSECTOR (16) // Number of blocks in a long sector++// Determine the sector trailer block based on sector number+#define BLOCK_NUMBER_OF_SECTOR_TRAILER(sector) (((sector)<NR_SHORTSECTOR)? \+ ((sector)*NR_BLOCK_OF_SHORTSECTOR + NR_BLOCK_OF_SHORTSECTOR-1):\+ (NR_SHORTSECTOR*NR_BLOCK_OF_SHORTSECTOR + (sector-NR_SHORTSECTOR)*NR_BLOCK_OF_LONGSECTOR + NR_BLOCK_OF_LONGSECTOR-1))++// Determine the sector's first block based on the sector number+#define BLOCK_NUMBER_OF_SECTOR_1ST_BLOCK(sector) (((sector)<NR_SHORTSECTOR)? \+ ((sector)*NR_BLOCK_OF_SHORTSECTOR):\+ (NR_SHORTSECTOR*NR_BLOCK_OF_SHORTSECTOR + (sector-NR_SHORTSECTOR)*NR_BLOCK_OF_LONGSECTOR))++// The default Mifare Classic key+static const uint8_t KEY_DEFAULT_KEYAB[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};+++void setup(void) {+ Serial.begin(115200);+ while (!Serial) delay(10); // for Leonardo/Micro/Zero++ Serial.println("Looking for PN532...");++ nfc.begin();++ uint32_t versiondata = nfc.getFirmwareVersion();+ if (! versiondata) {+ Serial.print("Didn't find PN53x board");+ while (1); // halt+ }++ // Got ok data, print it out!+ Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);+ Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);+ Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);+}++void loop(void) {+ uint8_t success; // Flag to check if there was an error with the PN532+ uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID+ uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)+ uint8_t blockBuffer[16]; // Buffer to store block contents+ uint8_t blankAccessBits[3] = { 0xff, 0x07, 0x80 };+ uint8_t idx = 0;+ uint8_t numOfSector = 16; // Assume Mifare Classic 1K for now (16 4-block sectors)++ Serial.println("Place your NDEF formatted Mifare Classic 1K card on the reader");+ Serial.println("and press any key to continue ...");++ // Wait for user input before proceeding+ while (!Serial.available());+ while (Serial.available()) Serial.read();++ // Wait for an ISO14443A type card (Mifare, etc.). When one is found+ // 'uid' will be populated with the UID, and uidLength will indicate+ // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)+ success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);++ if (success)+ {+ // We seem to have a tag ...+ // Display some basic information about it+ Serial.println("Found an ISO14443A card/tag");+ Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");+ Serial.print(" UID Value: ");+ nfc.PrintHex(uid, uidLength);+ Serial.println("");++ // Make sure this is a Mifare Classic card+ if (uidLength != 4)+ {+ Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!");+ return;+ }++ Serial.println("Seems to be a Mifare Classic card (4 byte UID)");+ Serial.println("");+ Serial.println("Reformatting card for Mifare Classic (please don't touch it!) ... ");++ // Now run through the card sector by sector+ for (idx = 0; idx < numOfSector; idx++)+ {+ // Step 1: Authenticate the current sector using key B 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF+ success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, BLOCK_NUMBER_OF_SECTOR_TRAILER(idx), 1, (uint8_t *)KEY_DEFAULT_KEYAB);+ if (!success)+ {+ Serial.print("Authentication failed for sector "); Serial.println(numOfSector);+ return;+ }++ // Step 2: Write to the other blocks+ if (idx == 16)+ {+ memset(blockBuffer, 0, sizeof(blockBuffer));+ if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 3, blockBuffer)))+ {+ Serial.print("Unable to write to sector "); Serial.println(numOfSector);+ return;+ }+ }+ if ((idx == 0) || (idx == 16))+ {+ memset(blockBuffer, 0, sizeof(blockBuffer));+ if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 2, blockBuffer)))+ {+ Serial.print("Unable to write to sector "); Serial.println(numOfSector);+ return;+ }+ }+ else+ {+ memset(blockBuffer, 0, sizeof(blockBuffer));+ if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 3, blockBuffer)))+ {+ Serial.print("Unable to write to sector "); Serial.println(numOfSector);+ return;+ }+ if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 2, blockBuffer)))+ {+ Serial.print("Unable to write to sector "); Serial.println(numOfSector);+ return;+ }+ }+ memset(blockBuffer, 0, sizeof(blockBuffer));+ if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 1, blockBuffer)))+ {+ Serial.print("Unable to write to sector "); Serial.println(numOfSector);+ return;+ }++ // Step 3: Reset both keys to 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF+ memcpy(blockBuffer, KEY_DEFAULT_KEYAB, sizeof(KEY_DEFAULT_KEYAB));+ memcpy(blockBuffer + 6, blankAccessBits, sizeof(blankAccessBits));+ blockBuffer[9] = 0x69;+ memcpy(blockBuffer + 10, KEY_DEFAULT_KEYAB, sizeof(KEY_DEFAULT_KEYAB));++ // Step 4: Write the trailer block+ if (!(nfc.mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)), blockBuffer)))+ {+ Serial.print("Unable to write trailer block of sector "); Serial.println(numOfSector);+ return;+ }+ }+ }++ // Wait a bit before trying again+ Serial.println("\n\nDone!");+ delay(1000);+ Serial.flush();+ while(Serial.available()) Serial.read();+}diff --git a/lib/PN532/examples/mifareclassic_updatendef/mifareclassic_updatendef.ino b/lib/PN532/examples/mifareclassic_updatendef/mifareclassic_updatendef.inonew file mode 100644index 00000000..0bdf1263--- /dev/null+++ b/lib/PN532/examples/mifareclassic_updatendef/mifareclassic_updatendef.ino@@ -0,0 +1,177 @@+/**************************************************************************/+/*!+ @file mifareclassic_updatendef.pde+ @author Adafruit Industries+ @license BSD (see license.txt)++ Updates a sector that is already formatted for NDEF (using+ mifareclassic_formatndef.pde for example), inserting a new url++ This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards+ This library works with the Adafruit NFC Shield+ ----> https://www.adafruit.com/products/789++ Check out the links above for our tutorials and wiring diagrams+ These chips use SPI or I2C to communicate++ Adafruit invests time and resources providing this open source code,+ please support Adafruit and open-source hardware by purchasing+ products from Adafruit!++*/+/**************************************************************************/++#include <Wire.h>+#include <SPI.h>+#include <Adafruit_PN532.h>++// If using the breakout with SPI, define the pins for SPI communication.+#define PN532_SCK (2)+#define PN532_MOSI (3)+#define PN532_SS (4)+#define PN532_MISO (5)++// If using the breakout or shield with I2C, define just the pins connected+// to the IRQ and reset lines. Use the values below (2, 3) for the shield!+#define PN532_IRQ (2)+#define PN532_RESET (3) // Not connected by default on the NFC Shield++// Uncomment just _one_ line below depending on how your breakout or shield+// is connected to the Arduino:++// Use this line for a breakout with a SPI connection:+Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);++// Use this line for a breakout with a hardware SPI connection. Note that+// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's+// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are+// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.+//Adafruit_PN532 nfc(PN532_SS);++// Or use this line for a breakout or shield with an I2C connection:+//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);++/*+ We can encode many different kinds of pointers to the card,+ from a URL, to an Email address, to a phone number, and many more+ check the library header .h file to see the large # of supported+ prefixes!+*/+// For a http://www. url:+const char * url = "adafruit.com/blog/";+uint8_t ndefprefix = NDEF_URIPREFIX_HTTP_WWWDOT;++// for an email address+//const char * url = "[email protected]";+//uint8_t ndefprefix = NDEF_URIPREFIX_MAILTO;++// for a phone number+//const char * url = "+1 212 555 1212";+//uint8_t ndefprefix = NDEF_URIPREFIX_TEL;+++void setup(void) {+ Serial.begin(115200);+ while (!Serial) delay(10); // for Leonardo/Micro/Zero++ Serial.println("Looking for PN532...");++ nfc.begin();++ uint32_t versiondata = nfc.getFirmwareVersion();+ if (! versiondata) {+ Serial.print("Didn't find PN53x board");+ while (1); // halt+ }++ // Got ok data, print it out!+ Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);+ Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);+ Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);+}++void loop(void) {+ uint8_t success; // Flag to check if there was an error with the PN532+ uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID+ uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)++ // Use the default NDEF keys (these would have have set by mifareclassic_formatndef.pde!)+ uint8_t keyb[6] = { 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 };++ Serial.println("Place your NDEF formatted Mifare Classic card on the reader to update the");+ Serial.println("NDEF record and press any key to continue ...");+ // Wait for user input before proceeding+ while (!Serial.available());+ // a key was pressed1+ while (Serial.available()) Serial.read();++ // Wait for an ISO14443A type card (Mifare, etc.). When one is found+ // 'uid' will be populated with the UID, and uidLength will indicate+ // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)+ success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);++ if (success)+ {+ // Display some basic information about the card+ Serial.println("Found an ISO14443A card");+ Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");+ Serial.print(" UID Value: ");+ nfc.PrintHex(uid, uidLength);+ Serial.println("");++ // Make sure this is a Mifare Classic card+ if (uidLength != 4)+ {+ Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!");+ return;+ }++ // We probably have a Mifare Classic card ...+ Serial.println("Seems to be a Mifare Classic card (4 byte UID)");++ // Check if this is an NDEF card (using first block of sector 1 from mifareclassic_formatndef.pde)+ // Must authenticate on the first key using 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7+ success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 4, 0, keyb);+ if (!success)+ {+ Serial.println("Unable to authenticate block 4 ... is this card NDEF formatted?");+ return;+ }++ Serial.println("Authentication succeeded (seems to be an NDEF/NFC Forum tag) ...");++ // Authenticated seems to have worked+ // Try to write an NDEF record to sector 1+ // Use 0x01 for the URI Identifier Code to prepend "http://www."+ // to the url (and save some space). For information on URI ID Codes+ // see http://www.ladyada.net/wiki/private/articlestaging/nfc/ndef+ if (strlen(url) > 38)+ {+ // The length is also checked in the WriteNDEFURI function, but lets+ // warn users here just in case they change the value and it's bigger+ // than it should be+ Serial.println("URI is too long ... must be less than 38 characters!");+ return;+ }++ Serial.println("Updating sector 1 with URI as NDEF Message");++ // URI is within size limits ... write it to the card and report success/failure+ success = nfc.mifareclassic_WriteNDEFURI(1, ndefprefix, url);+ if (success)+ {+ Serial.println("NDEF URI Record written to sector 1");+ Serial.println("");+ }+ else+ {+ Serial.println("NDEF Record creation failed! :(");+ }+ }++ // Wait a bit before trying again+ Serial.println("\n\nDone!");+ delay(1000);+ Serial.flush();+ while(Serial.available()) Serial.read();+}diff --git a/lib/PN532/examples/ntag2xx_erase/ntag2xx_erase.ino b/lib/PN532/examples/ntag2xx_erase/ntag2xx_erase.inonew file mode 100644index 00000000..4bc5d429--- /dev/null+++ b/lib/PN532/examples/ntag2xx_erase/ntag2xx_erase.ino@@ -0,0 +1,158 @@+/**************************************************************************/+/*!+ @file ntag2xx_erase.pde+ @author KTOWN (Adafruit Industries)+ @license BSD (see license.txt)++ This example will wait for any NTAG203 or NTAG213 card or tag,+ and will attempt to erase the user data section of the card (setting+ all user bytes to 0x00)++ This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards+ This library works with the Adafruit NFC breakout+ ----> https://www.adafruit.com/products/364++ Check out the links above for our tutorials and wiring diagrams+ These chips use SPI or I2C to communicate.++ Adafruit invests time and resources providing this open source code,+ please support Adafruit and open-source hardware by purchasing+ products from Adafruit!+*/+/**************************************************************************/+#include <Wire.h>+#include <SPI.h>+#include <Adafruit_PN532.h>++// If using the breakout with SPI, define the pins for SPI communication.+#define PN532_SCK (2)+#define PN532_MOSI (3)+#define PN532_SS (4)+#define PN532_MISO (5)++// If using the breakout or shield with I2C, define just the pins connected+// to the IRQ and reset lines. Use the values below (2, 3) for the shield!+#define PN532_IRQ (2)+#define PN532_RESET (3) // Not connected by default on the NFC Shield++// Uncomment just _one_ line below depending on how your breakout or shield+// is connected to the Arduino:++// Use this line for a breakout with a software SPI connection (recommended):+Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);++// Use this line for a breakout with a hardware SPI connection. Note that+// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's+// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are+// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.+//Adafruit_PN532 nfc(PN532_SS);++// Or use this line for a breakout or shield with an I2C connection:+//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);++void setup(void) {+ Serial.begin(115200);+ while (!Serial) delay(10); // for Leonardo/Micro/Zero++ Serial.println("Hello!");++ nfc.begin();++ uint32_t versiondata = nfc.getFirmwareVersion();+ if (! versiondata) {+ Serial.print("Didn't find PN53x board");+ while (1); // halt+ }+ // Got ok data, print it out!+ Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);+ Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);+ Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);++ Serial.println("Waiting for an ISO14443A Card ...");+}++void loop(void) {+ uint8_t success;+ uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID+ uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)++ // Wait for an NTAG203 card. When one is found 'uid' will be populated with+ // the UID, and uidLength will indicate the size of the UUID (normally 7)+ success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);++ if (success) {+ // Display some basic information about the card+ Serial.println("Found an ISO14443A card");+ Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");+ Serial.print(" UID Value: ");+ nfc.PrintHex(uid, uidLength);+ Serial.println("");++ if (uidLength == 7)+ {+ uint8_t data[32];++ // We probably have an NTAG2xx card (though it could be Ultralight as well)+ Serial.println("Seems to be an NTAG2xx tag (7 byte UID)");++ // NTAG2x3 cards have 39*4 bytes of user pages (156 user bytes),+ // starting at page 4 ... larger cards just add pages to the end of+ // this range:++ // See: http://www.nxp.com/documents/short_data_sheet/NTAG203_SDS.pdf++ // TAG Type PAGES USER START USER STOP+ // -------- ----- ---------- ---------+ // NTAG 203 42 4 39+ // NTAG 213 45 4 39+ // NTAG 215 135 4 129+ // NTAG 216 231 4 225++ Serial.println("");+ Serial.println("Writing 0x00 0x00 0x00 0x00 to pages 4..39");+ Serial.println("");+ for (uint8_t i = 4; i < 39; i++)+ {+ memset(data, 0, 4);+ success = nfc.ntag2xx_WritePage(i, data);++ // Display the current page number+ Serial.print("Page ");+ if (i < 10)+ {+ Serial.print("0");+ Serial.print(i);+ }+ else+ {+ Serial.print(i);+ }+ Serial.print(": ");++ // Display the results, depending on 'success'+ if (success)+ {+ Serial.println("Erased");+ }+ else+ {+ Serial.println("Unable to write to the requested page!");+ }+ }+ }+ else+ {+ Serial.println("This doesn't seem to be an NTAG203 tag (UUID length != 7 bytes)!");+ }++ // Wait a bit before trying again+ Serial.println("\n\nSend a character to scan another tag!");+ Serial.flush();+ while (!Serial.available());+ while (Serial.available()) {+ Serial.read();+ }+ Serial.flush();+ }+}+diff --git a/lib/PN532/examples/ntag2xx_read/ntag2xx_read.ino b/lib/PN532/examples/ntag2xx_read/ntag2xx_read.inonew file mode 100644index 00000000..b0bd3650--- /dev/null+++ b/lib/PN532/examples/ntag2xx_read/ntag2xx_read.ino@@ -0,0 +1,155 @@+/**************************************************************************/+/*!+ @file readntag203.pde+ @author KTOWN (Adafruit Industries)+ @license BSD (see license.txt)++ This example will wait for any NTAG203 or NTAG213 card or tag,+ and will attempt to read from it.++ This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards+ This library works with the Adafruit NFC breakout+ ----> https://www.adafruit.com/products/364++ Check out the links above for our tutorials and wiring diagrams+ These chips use SPI or I2C to communicate.++ Adafruit invests time and resources providing this open source code,+ please support Adafruit and open-source hardware by purchasing+ products from Adafruit!+*/+/**************************************************************************/+#include <Wire.h>+#include <SPI.h>+#include <Adafruit_PN532.h>++// If using the breakout with SPI, define the pins for SPI communication.+#define PN532_SCK (2)+#define PN532_MOSI (3)+#define PN532_SS (4)+#define PN532_MISO (5)++// If using the breakout or shield with I2C, define just the pins connected+// to the IRQ and reset lines. Use the values below (2, 3) for the shield!+#define PN532_IRQ (2)+#define PN532_RESET (3) // Not connected by default on the NFC Shield++// Uncomment just _one_ line below depending on how your breakout or shield+// is connected to the Arduino:++// Use this line for a breakout with a software SPI connection (recommended):+Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);++// Use this line for a breakout with a hardware SPI connection. Note that+// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's+// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are+// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.+//Adafruit_PN532 nfc(PN532_SS);++// Or use this line for a breakout or shield with an I2C connection:+//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);+++void setup(void) {+ Serial.begin(115200);+ while (!Serial) delay(10); // for Leonardo/Micro/Zero++ Serial.println("Hello!");++ nfc.begin();++ uint32_t versiondata = nfc.getFirmwareVersion();+ if (! versiondata) {+ Serial.print("Didn't find PN53x board");+ while (1); // halt+ }+ // Got ok data, print it out!+ Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);+ Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);+ Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);++ Serial.println("Waiting for an ISO14443A Card ...");+}++void loop(void) {+ uint8_t success;+ uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID+ uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)++ // Wait for an NTAG203 card. When one is found 'uid' will be populated with+ // the UID, and uidLength will indicate the size of the UUID (normally 7)+ success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);++ if (success) {+ // Display some basic information about the card+ Serial.println("Found an ISO14443A card");+ Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");+ Serial.print(" UID Value: ");+ nfc.PrintHex(uid, uidLength);+ Serial.println("");++ if (uidLength == 7)+ {+ uint8_t data[32];++ // We probably have an NTAG2xx card (though it could be Ultralight as well)+ Serial.println("Seems to be an NTAG2xx tag (7 byte UID)");++ // NTAG2x3 cards have 39*4 bytes of user pages (156 user bytes),+ // starting at page 4 ... larger cards just add pages to the end of+ // this range:++ // See: http://www.nxp.com/documents/short_data_sheet/NTAG203_SDS.pdf++ // TAG Type PAGES USER START USER STOP+ // -------- ----- ---------- ---------+ // NTAG 203 42 4 39+ // NTAG 213 45 4 39+ // NTAG 215 135 4 129+ // NTAG 216 231 4 225++ for (uint8_t i = 0; i < 42; i++)+ {+ success = nfc.ntag2xx_ReadPage(i, data);++ // Display the current page number+ Serial.print("PAGE ");+ if (i < 10)+ {+ Serial.print("0");+ Serial.print(i);+ }+ else+ {+ Serial.print(i);+ }+ Serial.print(": ");++ // Display the results, depending on 'success'+ if (success)+ {+ // Dump the page data+ nfc.PrintHexChar(data, 4);+ }+ else+ {+ Serial.println("Unable to read the requested page!");+ }+ }+ }+ else+ {+ Serial.println("This doesn't seem to be an NTAG203 tag (UUID length != 7 bytes)!");+ }++ // Wait a bit before trying again+ Serial.println("\n\nSend a character to scan another tag!");+ Serial.flush();+ while (!Serial.available());+ while (Serial.available()) {+ Serial.read();+ }+ Serial.flush();+ }+}+diff --git a/lib/PN532/examples/ntag2xx_updatendef/ntag2xx_updatendef.ino b/lib/PN532/examples/ntag2xx_updatendef/ntag2xx_updatendef.inonew file mode 100644index 00000000..36166860--- /dev/null+++ b/lib/PN532/examples/ntag2xx_updatendef/ntag2xx_updatendef.ino@@ -0,0 +1,212 @@+/**************************************************************************/+/*!+ @file ntag2xx_updatendef.pde+ @author KTOWN (Adafruit Industries)+ @license BSD (see license.txt)++ This example will wait for any NTAG203 or NTAG213 card or tag,+ and will attempt to add or update an NDEF URI at the start of the+ tag's memory.++ This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards+ This library works with the Adafruit NFC breakout+ ----> https://www.adafruit.com/products/364++ Check out the links above for our tutorials and wiring diagrams+ These chips use SPI or I2C to communicate.++ Adafruit invests time and resources providing this open source code,+ please support Adafruit and open-source hardware by purchasing+ products from Adafruit!+*/+/**************************************************************************/+#include <Wire.h>+#include <SPI.h>+#include <Adafruit_PN532.h>++// If using the breakout with SPI, define the pins for SPI communication.+#define PN532_SCK (2)+#define PN532_MOSI (3)+#define PN532_SS (4)+#define PN532_MISO (5)++// If using the breakout or shield with I2C, define just the pins connected+// to the IRQ and reset lines. Use the values below (2, 3) for the shield!+#define PN532_IRQ (2)+#define PN532_RESET (3) // Not connected by default on the NFC Shield++// Uncomment just _one_ line below depending on how your breakout or shield+// is connected to the Arduino:++// Use this line for a breakout with a software SPI connection (recommended):+Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);++// Use this line for a breakout with a hardware SPI connection. Note that+// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's+// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are+// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.+//Adafruit_PN532 nfc(PN532_SS);++// Or use this line for a breakout or shield with an I2C connection:+//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);++/*+ We can encode many different kinds of pointers to the card,+ from a URL, to an Email address, to a phone number, and many more+ check the library header .h file to see the large # of supported+ prefixes!+*/+// For a http://www. url:+char * url = "adafruit.com/blog/";+uint8_t ndefprefix = NDEF_URIPREFIX_HTTP_WWWDOT;++// for an email address+//char * url = "[email protected]";+//uint8_t ndefprefix = NDEF_URIPREFIX_MAILTO;++// for a phone number+//char * url = "+1 212 555 1212";+//uint8_t ndefprefix = NDEF_URIPREFIX_TEL;++void setup(void) {+ Serial.begin(115200);+ while (!Serial) delay(10); // for Leonardo/Micro/Zero++ Serial.println("Hello!");++ nfc.begin();++ uint32_t versiondata = nfc.getFirmwareVersion();+ if (! versiondata) {+ Serial.print("Didn't find PN53x board");+ while (1); // halt+ }+ // Got ok data, print it out!+ Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);+ Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);+ Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);+}++void loop(void)+{+ uint8_t success;+ uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID+ uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)+ uint8_t dataLength;++ // Require some user feedback before running this example!+ Serial.println("\r\nPlace your NDEF formatted NTAG2xx tag on the reader to update the");+ Serial.println("NDEF record and press any key to continue ...\r\n");+ // Wait for user input before proceeding+ while (!Serial.available());+ // a key was pressed1+ while (Serial.available()) Serial.read();++ // 1.) Wait for an NTAG203 card. When one is found 'uid' will be populated with+ // the UID, and uidLength will indicate the size of the UID (normally 7)+ success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);++ // It seems we found a valid ISO14443A Tag!+ if (success)+ {+ // 2.) Display some basic information about the card+ Serial.println("Found an ISO14443A card");+ Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");+ Serial.print(" UID Value: ");+ nfc.PrintHex(uid, uidLength);+ Serial.println("");++ if (uidLength != 7)+ {+ Serial.println("This doesn't seem to be an NTAG203 tag (UUID length != 7 bytes)!");+ }+ else+ {+ uint8_t data[32];++ // We probably have an NTAG2xx card (though it could be Ultralight as well)+ Serial.println("Seems to be an NTAG2xx tag (7 byte UID)");++ // NTAG2x3 cards have 39*4 bytes of user pages (156 user bytes),+ // starting at page 4 ... larger cards just add pages to the end of+ // this range:++ // See: http://www.nxp.com/documents/short_data_sheet/NTAG203_SDS.pdf++ // TAG Type PAGES USER START USER STOP+ // -------- ----- ---------- ---------+ // NTAG 203 42 4 39+ // NTAG 213 45 4 39+ // NTAG 215 135 4 129+ // NTAG 216 231 4 225+++ // 3.) Check if the NDEF Capability Container (CC) bits are already set+ // in OTP memory (page 3)+ memset(data, 0, 4);+ success = nfc.ntag2xx_ReadPage(3, data);+ if (!success)+ {+ Serial.println("Unable to read the Capability Container (page 3)");+ return;+ }+ else+ {+ // If the tag has already been formatted as NDEF, byte 0 should be:+ // Byte 0 = Magic Number (0xE1)+ // Byte 1 = NDEF Version (Should be 0x10)+ // Byte 2 = Data Area Size (value * 8 bytes)+ // Byte 3 = Read/Write Access (0x00 for full read and write)+ if (!((data[0] == 0xE1) && (data[1] == 0x10)))+ {+ Serial.println("This doesn't seem to be an NDEF formatted tag.");+ Serial.println("Page 3 should start with 0xE1 0x10.");+ }+ else+ {+ // 4.) Determine and display the data area size+ dataLength = data[2]*8;+ Serial.print("Tag is NDEF formatted. Data area size = ");+ Serial.print(dataLength);+ Serial.println(" bytes");++ // 5.) Erase the old data area+ Serial.print("Erasing previous data area ");+ for (uint8_t i = 4; i < (dataLength/4)+4; i++)+ {+ memset(data, 0, 4);+ success = nfc.ntag2xx_WritePage(i, data);+ Serial.print(".");+ if (!success)+ {+ Serial.println(" ERROR!");+ return;+ }+ }+ Serial.println(" DONE!");++ // 6.) Try to add a new NDEF URI record+ Serial.print("Writing URI as NDEF Record ... ");+ success = nfc.ntag2xx_WriteNDEFURI(ndefprefix, url, dataLength);+ if (success)+ {+ Serial.println("DONE!");+ }+ else+ {+ Serial.println("ERROR! (URI length?)");+ }++ } // CC contents NDEF record check+ } // CC page read check+ } // UUID length check++ // Wait a bit before trying again+ Serial.flush();+ while (!Serial.available());+ while (Serial.available()) {+ Serial.read();+ }+ Serial.flush();+ } // Start waiting for a new ISO14443A tag+}\ No newline at end of filediff --git a/lib/PN532/examples/readMifare/readMifare.ino b/lib/PN532/examples/readMifare/readMifare.inonew file mode 100644index 00000000..ecd4d28c--- /dev/null+++ b/lib/PN532/examples/readMifare/readMifare.ino@@ -0,0 +1,184 @@+/**************************************************************************/+/*!+ @file readMifare.pde+ @author Adafruit Industries+ @license BSD (see license.txt)++ This example will wait for any ISO14443A card or tag, and+ depending on the size of the UID will attempt to read from it.++ If the card has a 4-byte UID it is probably a Mifare+ Classic card, and the following steps are taken:++ - Authenticate block 4 (the first block of Sector 1) using+ the default KEYA of 0XFF 0XFF 0XFF 0XFF 0XFF 0XFF+ - If authentication succeeds, we can then read any of the+ 4 blocks in that sector (though only block 4 is read here)++ If the card has a 7-byte UID it is probably a Mifare+ Ultralight card, and the 4 byte pages can be read directly.+ Page 4 is read by default since this is the first 'general-+ purpose' page on the tags.+++This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards+This library works with the Adafruit NFC breakout+ ----> https://www.adafruit.com/products/364++Check out the links above for our tutorials and wiring diagrams+These chips use SPI or I2C to communicate.++Adafruit invests time and resources providing this open source code,+please support Adafruit and open-source hardware by purchasing+products from Adafruit!++*/+/**************************************************************************/+#include <Wire.h>+#include <SPI.h>+#include <Adafruit_PN532.h>++// If using the breakout with SPI, define the pins for SPI communication.+#define PN532_SCK (2)+#define PN532_MOSI (3)+#define PN532_SS (4)+#define PN532_MISO (5)++// If using the breakout or shield with I2C, define just the pins connected+// to the IRQ and reset lines. Use the values below (2, 3) for the shield!+#define PN532_IRQ (2)+#define PN532_RESET (3) // Not connected by default on the NFC Shield++// Uncomment just _one_ line below depending on how your breakout or shield+// is connected to the Arduino:++// Use this line for a breakout with a software SPI connection (recommended):+Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);++// Use this line for a breakout with a hardware SPI connection. Note that+// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's+// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are+// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.+//Adafruit_PN532 nfc(PN532_SS);++// Or use this line for a breakout or shield with an I2C connection:+//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);++// Or use hardware Serial:+//Adafruit_PN532 nfc(PN532_RESET, &Serial1);++void setup(void) {+ Serial.begin(115200);+ while (!Serial) delay(10); // for Leonardo/Micro/Zero++ Serial.println("Hello!");++ nfc.begin();++ uint32_t versiondata = nfc.getFirmwareVersion();+ if (! versiondata) {+ Serial.print("Didn't find PN53x board");+ while (1); // halt+ }+ // Got ok data, print it out!+ Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);+ Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);+ Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);++ Serial.println("Waiting for an ISO14443A Card ...");+}+++void loop(void) {+ uint8_t success;+ uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID+ uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)++ // Wait for an ISO14443A type cards (Mifare, etc.). When one is found+ // 'uid' will be populated with the UID, and uidLength will indicate+ // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)+ success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);++ if (success) {+ // Display some basic information about the card+ Serial.println("Found an ISO14443A card");+ Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");+ Serial.print(" UID Value: ");+ nfc.PrintHex(uid, uidLength);+ Serial.println("");++ if (uidLength == 4)+ {+ // We probably have a Mifare Classic card ...+ Serial.println("Seems to be a Mifare Classic card (4 byte UID)");++ // Now we need to try to authenticate it for read/write access+ // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF+ Serial.println("Trying to authenticate block 4 with default KEYA value");+ uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };++ // Start with block 4 (the first block of sector 1) since sector 0+ // contains the manufacturer data and it's probably better just+ // to leave it alone unless you know what you're doing+ success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);++ if (success)+ {+ Serial.println("Sector 1 (Blocks 4..7) has been authenticated");+ uint8_t data[16];++ // If you want to write something to block 4 to test with, uncomment+ // the following line and this text should be read back in a minute+ //memcpy(data, (const uint8_t[]){ 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data);+ // success = nfc.mifareclassic_WriteDataBlock (4, data);++ // Try to read the contents of block 4+ success = nfc.mifareclassic_ReadDataBlock(4, data);++ if (success)+ {+ // Data seems to have been read ... spit it out+ Serial.println("Reading Block 4:");+ nfc.PrintHexChar(data, 16);+ Serial.println("");++ // Wait a bit before reading the card again+ delay(1000);+ }+ else+ {+ Serial.println("Ooops ... unable to read the requested block. Try another key?");+ }+ }+ else+ {+ Serial.println("Ooops ... authentication failed: Try another key?");+ }+ }++ if (uidLength == 7)+ {+ // We probably have a Mifare Ultralight card ...+ Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");++ // Try to read the first general-purpose user page (#4)+ Serial.println("Reading page 4");+ uint8_t data[32];+ success = nfc.mifareultralight_ReadPage (4, data);+ if (success)+ {+ // Data seems to have been read ... spit it out+ nfc.PrintHexChar(data, 4);+ Serial.println("");++ // Wait a bit before reading the card again+ delay(1000);+ }+ else+ {+ Serial.println("Ooops ... unable to read the requested page!?");+ }+ }+ }+}+diff --git a/lib/PN532/examples/readMifareClassic/readMifareClassic.ino b/lib/PN532/examples/readMifareClassic/readMifareClassic.inonew file mode 100644index 00000000..a2edaf8b--- /dev/null+++ b/lib/PN532/examples/readMifareClassic/readMifareClassic.ino@@ -0,0 +1,119 @@+/**************************************************************************/+/*!+ @file readMifareClassic.pde+ @author Adafruit Industries+ @license BSD (see license.txt)++ This example will wait for any ISO14443A card or tag, and+ depending on the size of the UID will attempt to read from it.++ If the card has a 4-byte UID it is probably a Mifare+ Classic card, and the following steps are taken:++ Reads the 4 byte (32 bit) ID of a MiFare Classic card.+ Since the classic cards have only 32 bit identifiers you can stick+ them in a single variable and use that to compare card ID's as a+ number. This doesn't work for ultralight cards that have longer 7+ byte IDs!++ Note that you need the baud rate to be 115200 because we need to+ print out the data and read from the card at the same time!++This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards+This library works with the Adafruit NFC breakout+ ----> https://www.adafruit.com/products/364++Check out the links above for our tutorials and wiring diagrams+These chips use SPI to communicate, 4 required to interface++Adafruit invests time and resources providing this open source code,+please support Adafruit and open-source hardware by purchasing+products from Adafruit!+*/+/**************************************************************************/+#include <Wire.h>+#include <SPI.h>+#include <Adafruit_PN532.h>++// If using the breakout with SPI, define the pins for SPI communication.+#define PN532_SCK (2)+#define PN532_MOSI (3)+#define PN532_SS (4)+#define PN532_MISO (5)++// If using the breakout or shield with I2C, define just the pins connected+// to the IRQ and reset lines. Use the values below (2, 3) for the shield!+#define PN532_IRQ (2)+#define PN532_RESET (3) // Not connected by default on the NFC Shield++// Uncomment just _one_ line below depending on how your breakout or shield+// is connected to the Arduino:++// Use this line for a breakout with a SPI connection:+Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);++// Use this line for a breakout with a hardware SPI connection. Note that+// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's+// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are+// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.+//Adafruit_PN532 nfc(PN532_SS);++// Or use this line for a breakout or shield with an I2C connection:+//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);++void setup(void) {+ Serial.begin(115200);+ while (!Serial) delay(10); // for Leonardo/Micro/Zero++ Serial.println("Hello!");++ nfc.begin();++ uint32_t versiondata = nfc.getFirmwareVersion();+ if (! versiondata) {+ Serial.print("Didn't find PN53x board");+ while (1); // halt+ }+ // Got ok data, print it out!+ Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);+ Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);+ Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);++ Serial.println("Waiting for an ISO14443A Card ...");+}+++void loop(void) {+ uint8_t success;+ uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID+ uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)++ // Wait for an ISO14443A type cards (Mifare, etc.). When one is found+ // 'uid' will be populated with the UID, and uidLength will indicate+ // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)+ success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);++ if (success) {+ // Display some basic information about the card+ Serial.println("Found an ISO14443A card");+ Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");+ Serial.print(" UID Value: ");+ nfc.PrintHex(uid, uidLength);++ if (uidLength == 4)+ {+ // We probably have a Mifare Classic card ...+ uint32_t cardid = uid[0];+ cardid <<= 8;+ cardid |= uid[1];+ cardid <<= 8;+ cardid |= uid[2];+ cardid <<= 8;+ cardid |= uid[3];+ Serial.print("Seems to be a Mifare Classic card #");+ Serial.println(cardid);+ }+ Serial.println("");+ }+}+diff --git a/lib/PN532/examples/readMifareClassicIrq/readMifareClassicIrq.ino b/lib/PN532/examples/readMifareClassicIrq/readMifareClassicIrq.inonew file mode 100644index 00000000..345e317e--- /dev/null+++ b/lib/PN532/examples/readMifareClassicIrq/readMifareClassicIrq.ino@@ -0,0 +1,156 @@+/**************************************************************************/+/*!+ @file readMifareClassicIrq.pde+ @author Adafruit Industries+ @license BSD (see license.txt)++ This example will wait for any ISO14443A card or tag, and+ depending on the size of the UID will attempt to read from it.++ If the card has a 4-byte UID it is probably a Mifare+ Classic card, and the following steps are taken:++ Reads the 4 byte (32 bit) ID of a MiFare Classic card.+ Since the classic cards have only 32 bit identifiers you can stick+ them in a single variable and use that to compare card ID's as a+ number. This doesn't work for ultralight cards that have longer 7+ byte IDs!++ Note that you need the baud rate to be 115200 because we need to+ print out the data and read from the card at the same time!++This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards+This library works with the Adafruit NFC breakout+ ----> https://www.adafruit.com/products/364++Check out the links above for our tutorials and wiring diagrams++This example is for communicating with the PN532 chip using I2C. Wiring+should be as follows:+ PN532 SDA -> SDA pin+ PN532 SCL -> SCL pin+ PN532 IRQ -> D2+ PN532 SDA -> 3.3v (with 2k resistor)+ PN532 SCL -> 3.3v (with 2k resistor)+ PN532 3.3v -> 3.3v+ PN532 GND -> GND++Adafruit invests time and resources providing this open source code,+please support Adafruit and open-source hardware by purchasing+products from Adafruit!+*/+/**************************************************************************/+#include <Wire.h>+#include <SPI.h>+#include <Adafruit_PN532.h>++// If using the breakout with SPI, define the pins for SPI communication.+#define PN532_SCK (2)+#define PN532_MOSI (3)+#define PN532_SS (4)+#define PN532_MISO (5)++// If using the breakout or shield with I2C, define just the pins connected+// to the IRQ and reset lines. Use the values below (2, 3) for the shield!+#define PN532_IRQ (2)+#define PN532_RESET (3) // Not connected by default on the NFC Shield++const int DELAY_BETWEEN_CARDS = 500;+long timeLastCardRead = 0;+boolean readerDisabled = false;+int irqCurr;+int irqPrev;++// This example uses the IRQ line, which is available when in I2C mode.+Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);++void setup(void) {+ Serial.begin(115200);+ while (!Serial) delay(10); // for Leonardo/Micro/Zero++ Serial.println("Hello!");++ nfc.begin();++ uint32_t versiondata = nfc.getFirmwareVersion();+ if (! versiondata) {+ Serial.print("Didn't find PN53x board");+ while (1); // halt+ }+ // Got ok data, print it out!+ Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);+ Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);+ Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);++ startListeningToNFC();+}++void loop(void) {+ if (readerDisabled) {+ if (millis() - timeLastCardRead > DELAY_BETWEEN_CARDS) {+ readerDisabled = false;+ startListeningToNFC();+ }+ } else {+ irqCurr = digitalRead(PN532_IRQ);++ // When the IRQ is pulled low - the reader has got something for us.+ if (irqCurr == LOW && irqPrev == HIGH) {+ Serial.println("Got NFC IRQ");+ handleCardDetected();+ }++ irqPrev = irqCurr;+ }+}++void startListeningToNFC() {+ // Reset our IRQ indicators+ irqPrev = irqCurr = HIGH;++ Serial.println("Starting passive read for an ISO14443A Card ...");+ if (!nfc.startPassiveTargetIDDetection(PN532_MIFARE_ISO14443A)) {+ Serial.println("No card found. Waiting...");+ } else {+ Serial.println("Card already present.");+ handleCardDetected();+ }+}++void handleCardDetected() {+ uint8_t success = false;+ uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID+ uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)++ // read the NFC tag's info+ success = nfc.readDetectedPassiveTargetID(uid, &uidLength);+ Serial.println(success ? "Read successful" : "Read failed (not a card?)");++ if (success) {+ // Display some basic information about the card+ Serial.println("Found an ISO14443A card");+ Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");+ Serial.print(" UID Value: ");+ nfc.PrintHex(uid, uidLength);++ if (uidLength == 4)+ {+ // We probably have a Mifare Classic card ...+ uint32_t cardid = uid[0];+ cardid <<= 8;+ cardid |= uid[1];+ cardid <<= 8;+ cardid |= uid[2];+ cardid <<= 8;+ cardid |= uid[3];+ Serial.print("Seems to be a Mifare Classic card #");+ Serial.println(cardid);+ }+ Serial.println("");++ timeLastCardRead = millis();+ }++ // The reader will be enabled again after DELAY_BETWEEN_CARDS ms will pass.+ readerDisabled = true;+}diff --git a/lib/PN532/library.properties b/lib/PN532/library.propertiesnew file mode 100644index 00000000..36b954cc--- /dev/null+++ b/lib/PN532/library.properties@@ -0,0 +1,10 @@+name=Adafruit PN532+version=1.3.3+author=Adafruit+maintainer=Adafruit <[email protected]>+sentence=Arduino library for SPI and I2C access to the PN532 RFID/Near Field Communication chip+paragraph=Arduino library for SPI and I2C access to the PN532 RFID/Near Field Communication chip+category=Communication+url=https://github.com/adafruit/Adafruit-PN532+architectures=*+depends=Adafruit BusIOdiff --git a/lib/PN532/license.txt b/lib/PN532/license.txtnew file mode 100644index 00000000..f6a0f22b--- /dev/null+++ b/lib/PN532/license.txt@@ -0,0 +1,26 @@+Software License Agreement (BSD License)++Copyright (c) 2012, Adafruit Industries+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:+1. Redistributions of source code must retain the above copyright+notice, this list of conditions and the following disclaimer.+2. Redistributions in binary form must reproduce the above copyright+notice, this list of conditions and the following disclaimer in the+documentation and/or other materials provided with the distribution.+3. Neither the name of the copyright holders nor the+names of its contributors may be used to endorse or promote products+derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.