summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergiusz 'q3k' Bazański <q3k@q3k.org>2015-04-02 20:49:42 +0200
committerSergiusz 'q3k' Bazański <q3k@q3k.org>2015-04-02 20:49:42 +0200
commit93f976357de46711217c1bfeca92063b82aca9f4 (patch)
tree0fa57d1066dbac7e41c5c8c8d89366035c0029d4
parente9094c716590e26e7fb09de8929c98c27bcc2f69 (diff)
downloaddoorman-93f976357de46711217c1bfeca92063b82aca9f4.tar.gz
doorman-93f976357de46711217c1bfeca92063b82aca9f4.tar.bz2
doorman-93f976357de46711217c1bfeca92063b82aca9f4.zip
Add Adafruit PN532 as just a bunch of files
-rw-r--r--arduino/lib/Adafruit-PN532/Adafruit_PN532.cpp1801
-rw-r--r--arduino/lib/Adafruit-PN532/Adafruit_PN532.h222
-rw-r--r--arduino/lib/Adafruit-PN532/README.txt18
-rwxr-xr-xarduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/.classpath7
-rwxr-xr-xarduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/.project29
-rwxr-xr-xarduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/.settings/org.eclipse.jdt.core.prefs12
-rwxr-xr-xarduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/BlackBerry_App_Descriptor.xml40
-rwxr-xr-xarduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/build.xml35
-rwxr-xr-xarduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/res/img/icon.pngbin0 -> 771 bytes
-rwxr-xr-xarduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/ColorMixer.java152
-rwxr-xr-xarduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/MyApp.java47
-rwxr-xr-xarduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/MyScreen.java163
-rwxr-xr-xarduino/lib/Adafruit-PN532/examples/apduToBlackBerry/apduToBlackBerry.pde149
-rw-r--r--arduino/lib/Adafruit-PN532/examples/iso14443a_uid/iso14443a_uid.pde112
-rw-r--r--arduino/lib/Adafruit-PN532/examples/mifareclassic_formatndef/mifareclassic_formatndef.pde203
-rw-r--r--arduino/lib/Adafruit-PN532/examples/mifareclassic_memdump/mifareclassic_memdump.pde191
-rw-r--r--arduino/lib/Adafruit-PN532/examples/mifareclassic_ndeftoclassic/mifareclassic_ndeftoclassic.pde208
-rw-r--r--arduino/lib/Adafruit-PN532/examples/mifareclassic_updatendef/mifareclassic_updatendef.pde180
-rw-r--r--arduino/lib/Adafruit-PN532/examples/ntag2xx_erase/ntag2xx_erase.pde159
-rw-r--r--arduino/lib/Adafruit-PN532/examples/ntag2xx_read/ntag2xx_read.pde155
-rw-r--r--arduino/lib/Adafruit-PN532/examples/ntag2xx_updatendef/ntag2xx_updatendef.pde213
-rw-r--r--arduino/lib/Adafruit-PN532/examples/readMifare/readMifare.pde182
-rw-r--r--arduino/lib/Adafruit-PN532/examples/readMifareClassic/readMifareClassic.pde120
-rw-r--r--arduino/lib/Adafruit-PN532/license.txt26
24 files changed, 4424 insertions, 0 deletions
diff --git a/arduino/lib/Adafruit-PN532/Adafruit_PN532.cpp b/arduino/lib/Adafruit-PN532/Adafruit_PN532.cpp
new file mode 100644
index 0000000..3b3cd4c
--- /dev/null
+++ b/arduino/lib/Adafruit-PN532/Adafruit_PN532.cpp
@@ -0,0 +1,1801 @@
+/**************************************************************************/
+/*!
+ @file Adafruit_PN532.cpp
+ @author Adafruit Industries
+ @license BSD (see license.txt)
+
+ 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 HISTORY
+
+ 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)
+*/
+/**************************************************************************/
+#if ARDUINO >= 100
+ #include "Arduino.h"
+#else
+ #include "WProgram.h"
+#endif
+
+#include <Wire.h>
+#if defined(__AVR__) || defined(__i386__) //compatibility with Intel Galileo
+ #define WIRE Wire
+#else // Arduino Due
+ #define WIRE Wire1
+#endif
+
+#include <SPI.h>
+
+#include "Adafruit_PN532.h"
+
+byte pn532ack[] = {0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00};
+byte pn532response_firmwarevers[] = {0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03};
+
+// Uncomment these lines to enable debug output for PN532(SPI) and/or MIFARE related code
+//#define PN532DEBUG
+//#define MIFAREDEBUG
+
+// Hardware SPI-specific configuration:
+#define PN532_SPI_SETTING SPISettings(1000000, LSBFIRST, SPI_MODE0)
+#define PN532_SPI_CLOCKDIV SPI_CLOCK_DIV16
+
+#define PN532_PACKBUFFSIZ 64
+byte pn532_packetbuffer[PN532_PACKBUFFSIZ];
+
+#ifndef _BV
+ #define _BV(bit) (1<<(bit))
+#endif
+
+/**************************************************************************/
+/*!
+ @brief Sends a single byte via I2C
+
+ @param x The byte to send
+*/
+/**************************************************************************/
+static inline void i2c_send(uint8_t x)
+{
+ #if ARDUINO >= 100
+ WIRE.write((uint8_t)x);
+ #else
+ WIRE.send(x);
+ #endif
+}
+
+/**************************************************************************/
+/*!
+ @brief Reads a single byte via I2C
+*/
+/**************************************************************************/
+static inline uint8_t i2c_recv(void)
+{
+ #if ARDUINO >= 100
+ return WIRE.read();
+ #else
+ return WIRE.receive();
+ #endif
+}
+
+/**************************************************************************/
+/*!
+ @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):
+ _clk(clk),
+ _miso(miso),
+ _mosi(mosi),
+ _ss(ss),
+ _irq(0),
+ _reset(0),
+ _usingSPI(true),
+ _hardwareSPI(false)
+{
+ pinMode(_ss, OUTPUT);
+ pinMode(_clk, OUTPUT);
+ pinMode(_mosi, OUTPUT);
+ pinMode(_miso, INPUT);
+}
+
+/**************************************************************************/
+/*!
+ @brief Instantiates a new PN532 class using I2C.
+
+ @param irq Location of the IRQ pin
+ @param reset Location of the RSTPD_N pin
+*/
+/**************************************************************************/
+Adafruit_PN532::Adafruit_PN532(uint8_t irq, uint8_t reset):
+ _clk(0),
+ _miso(0),
+ _mosi(0),
+ _ss(0),
+ _irq(irq),
+ _reset(reset),
+ _usingSPI(false),
+ _hardwareSPI(false)
+{
+ pinMode(_irq, INPUT);
+ //digitalWrite(_irq, 1);
+ pinMode(_reset, OUTPUT);
+}
+
+/**************************************************************************/
+/*!
+ @brief Instantiates a new PN532 class using hardware SPI.
+
+ @param ss SPI chip select pin (CS/SSEL)
+*/
+/**************************************************************************/
+Adafruit_PN532::Adafruit_PN532(uint8_t ss):
+ _clk(0),
+ _miso(0),
+ _mosi(0),
+ _ss(ss),
+ _irq(0),
+ _reset(0),
+ _usingSPI(true),
+ _hardwareSPI(true)
+{
+ pinMode(_ss, OUTPUT);
+}
+
+/**************************************************************************/
+/*!
+ @brief Setups the HW
+*/
+/**************************************************************************/
+void Adafruit_PN532::begin() {
+ if (_usingSPI) {
+ // SPI initialization
+ if (_hardwareSPI) {
+ SPI.begin();
+ SPI.setDataMode(SPI_MODE0);
+ SPI.setBitOrder(LSBFIRST);
+ SPI.setClockDivider(PN532_SPI_CLOCKDIV);
+
+ #ifdef SPI_HAS_TRANSACTION
+ SPI.beginTransaction(PN532_SPI_SETTING);
+ #endif
+ }
+ digitalWrite(_ss, LOW);
+
+ delay(1000);
+
+ // not exactly sure why but we have to send a dummy command to get synced up
+ pn532_packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION;
+ sendCommandCheckAck(pn532_packetbuffer, 1);
+
+ // ignore response!
+
+ digitalWrite(_ss, HIGH);
+ #ifdef SPI_HAS_TRANSACTION
+ if (_hardwareSPI) SPI.endTransaction();
+ #endif
+ }
+ else {
+ // I2C initialization.
+ WIRE.begin();
+
+ // Reset the PN532
+ digitalWrite(_reset, HIGH);
+ digitalWrite(_reset, LOW);
+ delay(400);
+ digitalWrite(_reset, HIGH);
+ }
+}
+
+/**************************************************************************/
+/*!
+ @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++)
+ {
+ Serial.print(F("0x"));
+ // Append leading 0 for small values
+ if (data[szPos] <= 0xF)
+ Serial.print(F("0"));
+ Serial.print(data[szPos]&0xff, HEX);
+ if ((numBytes > 1) && (szPos != numBytes - 1))
+ {
+ Serial.print(F(" "));
+ }
+ }
+ Serial.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)
+ Serial.print(F("0"));
+ Serial.print(data[szPos], HEX);
+ if ((numBytes > 1) && (szPos != numBytes - 1))
+ {
+ Serial.print(F(" "));
+ }
+ }
+ Serial.print(F(" "));
+ for (szPos=0; szPos < numBytes; szPos++)
+ {
+ if (data[szPos] <= 0x1F)
+ Serial.print(F("."));
+ else
+ Serial.print((char)data[szPos]);
+ }
+ Serial.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, 12);
+
+ // check some basic stuff
+ if (0 != strncmp((char *)pn532_packetbuffer, (char *)pn532response_firmwarevers, 6)) {
+ #ifdef PN532DEBUG
+ Serial.println(F("Firmware doesn't match!"));
+ #endif
+ return 0;
+ }
+
+ int offset = _usingSPI ? 6 : 7; // Skip a response byte when using I2C to ignore extra data.
+ 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) {
+ uint16_t timer = 0;
+
+ // write the command
+ writecommand(cmd, cmdlen);
+
+ // Wait for chip to say its ready!
+ if (!waitready(timeout)) {
+ return false;
+ }
+
+ #ifdef PN532DEBUG
+ if (!_usingSPI) {
+ Serial.println(F("IRQ received"));
+ }
+ #endif
+
+ // read acknowledgement
+ if (!readack()) {
+ #ifdef PN532DEBUG
+ Serial.println(F("No ACK frame received!"));
+ #endif
+ return false;
+ }
+
+ // For SPI only wait for the chip to be ready again.
+ // This is unnecessary with I2C.
+ if (_usingSPI) {
+ if (!waitready(timeout)) {
+ return false;
+ }
+ }
+
+ return true; // ack'd command
+}
+
+/**************************************************************************/
+/*!
+ Writes an 8-bit value that sets the state of the PN532's GPIO pins
+
+ @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
+
+ @returns 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
+ Serial.print(F("Writing P3 GPIO: ")); Serial.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
+ Serial.print(F("Received: "));
+ PrintHex(pn532_packetbuffer, 8);
+ Serial.println();
+ #endif
+
+ int offset = _usingSPI ? 5 : 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 = _usingSPI ? 6 : 7;
+
+ #ifdef PN532DEBUG
+ Serial.print(F("Received: "));
+ PrintHex(pn532_packetbuffer, 11);
+ Serial.println();
+ Serial.print(F("P3 GPIO: 0x")); Serial.println(pn532_packetbuffer[p3offset], HEX);
+ Serial.print(F("P7 GPIO: 0x")); Serial.println(pn532_packetbuffer[p3offset+1], HEX);
+ Serial.print(F("IO GPIO: 0x")); Serial.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
+ Serial.println(F("Using UART (IO = 0x00)"));
+ break;
+ case 0x01: // Using I2C
+ Serial.println(F("Using I2C (IO = 0x01)"));
+ break;
+ case 0x02: // Using SPI
+ Serial.println(F("Using SPI (IO = 0x02)"));
+ break;
+ }
+ #endif
+
+ return pn532_packetbuffer[p3offset];
+}
+
+/**************************************************************************/
+/*!
+ @brief Configures the SAM (Secure Access Module)
+*/
+/**************************************************************************/
+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, 8);
+
+ int offset = _usingSPI ? 5 : 6;
+ return (pn532_packetbuffer[offset] == 0x15);
+}
+
+/**************************************************************************/
+/*!
+ 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
+ Serial.print(F("Setting MxRtyPassiveActivation to ")); Serial.print(maxRetries, DEC); Serial.println(F(" "));
+ #endif
+
+ if (! sendCommandCheckAck(pn532_packetbuffer, 5))
+ return 0x0; // no ACK
+
+ return 1;
+}
+
+/***** ISO14443A Commands ******/
+
+/**************************************************************************/
+/*!
+ Waits for an ISO14443A target to enter the field
+
+ @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.
+
+ @returns 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
+ Serial.println(F("No card(s) read"));
+ #endif
+ return 0x0; // no cards read
+ }
+
+ // wait for a card to enter the field (only possible with I2C)
+ if (!_usingSPI) {
+ #ifdef PN532DEBUG
+ Serial.println(F("Waiting for IRQ (indicates card presence)"));
+ #endif
+ if (!waitready(timeout)) {
+ #ifdef PN532DEBUG
+ Serial.println(F("IRQ Timeout"));
+ #endif
+ return 0x0;
+ }
+ }
+
+ // 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
+ Serial.print(F("Found ")); Serial.print(pn532_packetbuffer[7], DEC); Serial.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
+ Serial.print(F("ATQA: 0x")); Serial.println(sens_res, HEX);
+ Serial.print(F("SAK: 0x")); Serial.println(pn532_packetbuffer[11], HEX);
+ #endif
+
+ /* Card appears to be Mifare Classic */
+ *uidLength = pn532_packetbuffer[12];
+ #ifdef MIFAREDEBUG
+ Serial.print(F("UID:"));
+ #endif
+ for (uint8_t i=0; i < pn532_packetbuffer[12]; i++)
+ {
+ uid[i] = pn532_packetbuffer[13+i];
+ #ifdef MIFAREDEBUG
+ Serial.print(F(" 0x"));Serial.print(uid[i], HEX);
+ #endif
+ }
+ #ifdef MIFAREDEBUG
+ Serial.println();
+ #endif
+
+ return 1;
+}
+
+/**************************************************************************/
+/*!
+ @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
+*/
+/**************************************************************************/
+bool Adafruit_PN532::inDataExchange(uint8_t * send, uint8_t sendLength, uint8_t * response, uint8_t * responseLength) {
+ if (sendLength > PN532_PACKBUFFSIZ-2) {
+ #ifdef PN532DEBUG
+ Serial.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
+ Serial.println(F("Could not send ADPU"));
+ #endif
+ return false;
+ }
+
+ if (!waitready(1000)) {
+ #ifdef PN532DEBUG
+ Serial.println(F("Response never received for ADPU..."));
+ #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
+ Serial.println(F("Length check invalid"));
+ Serial.println(length,HEX);
+ Serial.println((~length)+1,HEX);
+ #endif
+ return false;
+ }
+ if (pn532_packetbuffer[5]==PN532_PN532TOHOST && pn532_packetbuffer[6]==PN532_RESPONSE_INDATAEXCHANGE) {
+ if ((pn532_packetbuffer[7] & 0x3f)!=0) {
+ #ifdef PN532DEBUG
+ Serial.println(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 {
+ Serial.print(F("Don't know how to handle this command: "));
+ Serial.println(pn532_packetbuffer[6],HEX);
+ return false;
+ }
+ }
+ else {
+ Serial.println(F("Preamble missing"));
+ return false;
+ }
+}
+
+/**************************************************************************/
+/*!
+ @brief 'InLists' a passive target. PN532 acting as reader/initiator,
+ peer acting as card/responder.
+*/
+/**************************************************************************/
+bool Adafruit_PN532::inListPassiveTarget() {
+ pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
+ pn532_packetbuffer[1] = 1;
+ pn532_packetbuffer[2] = 0;
+
+ #ifdef PN532DEBUG
+ Serial.print(F("About to inList passive target"));
+ #endif
+
+ if (!sendCommandCheckAck(pn532_packetbuffer,3,1000)) {
+ #ifdef PN532DEBUG
+ Serial.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
+ Serial.println(F("Length check invalid"));
+ Serial.println(length,HEX);
+ Serial.println((~length)+1,HEX);
+ #endif
+ return false;
+ }
+ if (pn532_packetbuffer[5]==PN532_PN532TOHOST && pn532_packetbuffer[6]==PN532_RESPONSE_INLISTPASSIVETARGET) {
+ if (pn532_packetbuffer[7] != 1) {
+ #ifdef PN532DEBUG
+ Serial.println(F("Unhandled number of targets inlisted"));
+ #endif
+ Serial.println(F("Number of tags inlisted:"));
+ Serial.println(pn532_packetbuffer[7]);
+ return false;
+ }
+
+ _inListedTag = pn532_packetbuffer[8];
+ Serial.print(F("Tag number: "));
+ Serial.println(_inListedTag);
+
+ return true;
+ } else {
+ #ifdef PN532DEBUG
+ Serial.print(F("Unexpected response to inlist passive host"));
+ #endif
+ return false;
+ }
+ }
+ else {
+ #ifdef PN532DEBUG
+ Serial.println(F("Preamble missing"));
+ #endif
+ return false;
+ }
+
+ return true;
+}
+
+
+/***** Mifare Classic Functions ******/
+
+/**************************************************************************/
+/*!
+ Indicates whether the specified block number is the first block
+ in the sector (block 0 relative to the current sector)
+*/
+/**************************************************************************/
+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);
+}
+
+/**************************************************************************/
+/*!
+ Indicates whether the specified block number is the sector trailer
+*/
+/**************************************************************************/
+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
+ Serial.print(F("Trying to authenticate card "));
+ Adafruit_PN532::PrintHex(_uid, _uidLen);
+ Serial.print(F("Using authentication KEY "));Serial.print(keyNumber ? 'B' : 'A');Serial.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
+ Serial.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
+ Serial.print(F("Trying to read 16 bytes from block "));Serial.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
+ Serial.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
+ Serial.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
+ Serial.print(F("Block "));
+ Serial.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
+ Serial.print(F("Trying to write 16 bytes to block "));Serial.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
+ Serial.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, len+5, 0xD1, 0x01, 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 ******/
+
+/**************************************************************************/
+/*!
+ 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)
+*/
+/**************************************************************************/
+uint8_t Adafruit_PN532::mifareultralight_ReadPage (uint8_t page, uint8_t * buffer)
+{
+ if (page >= 64)
+ {
+ #ifdef MIFAREDEBUG
+ Serial.println(F("Page value out of range"));
+ #endif
+ return 0;
+ }
+
+ #ifdef MIFAREDEBUG
+ Serial.print(F("Reading page "));Serial.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
+ Serial.println(F("Failed to receive ACK for write command"));
+ #endif
+ return 0;
+ }
+
+ /* Read the response packet */
+ readdata(pn532_packetbuffer, 26);
+ #ifdef MIFAREDEBUG
+ Serial.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
+ Serial.println(F("Unexpected response reading block: "));
+ Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26);
+ #endif
+ return 0;
+ }
+
+ /* Display data for debug if requested */
+ #ifdef MIFAREDEBUG
+ Serial.print(F("Page "));Serial.print(page);Serial.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
+ Serial.println(F("Page value out of range"));
+ #endif
+ // Return Failed Signal
+ return 0;
+ }
+
+ #ifdef MIFAREDEBUG
+ Serial.print(F("Trying to write 4 byte page"));Serial.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
+ Serial.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 ******/
+
+/**************************************************************************/
+/*!
+ 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)
+*/
+/**************************************************************************/
+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
+ Serial.println(F("Page value out of range"));
+ #endif
+ return 0;
+ }
+
+ #ifdef MIFAREDEBUG
+ Serial.print(F("Reading page "));Serial.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
+ Serial.println(F("Failed to receive ACK for write command"));
+ #endif
+ return 0;
+ }
+
+ /* Read the response packet */
+ readdata(pn532_packetbuffer, 26);
+ #ifdef MIFAREDEBUG
+ Serial.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
+ Serial.println(F("Unexpected response reading block: "));
+ Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26);
+ #endif
+ return 0;
+ }
+
+ /* Display data for debug if requested */
+ #ifdef MIFAREDEBUG
+ Serial.print(F("Page "));Serial.print(page);Serial.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
+ Serial.println(F("Page value out of range"));
+ #endif
+ // Return Failed Signal
+ return 0;
+ }
+
+ #ifdef MIFAREDEBUG
+ Serial.print(F("Trying to write 4 byte page"));Serial.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
+ Serial.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) */
+ 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 */
+ 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;
+}
+
+
+/************** 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];
+
+ readdata(ackbuff, 6);
+
+ return (0 == strncmp((char *)ackbuff, (char *)pn532ack, 6));
+}
+
+
+/**************************************************************************/
+/*!
+ @brief Return true if the PN532 is ready with a response.
+*/
+/**************************************************************************/
+bool Adafruit_PN532::isready() {
+ if (_usingSPI) {
+ // SPI read status and check if ready.
+ #ifdef SPI_HAS_TRANSACTION
+ if (_hardwareSPI) SPI.beginTransaction(PN532_SPI_SETTING);
+ #endif
+ digitalWrite(_ss, LOW);
+ delay(2);
+ spi_write(PN532_SPI_STATREAD);
+ // read byte
+ uint8_t x = spi_read();
+
+ digitalWrite(_ss, HIGH);
+ #ifdef SPI_HAS_TRANSACTION
+ if (_hardwareSPI) SPI.endTransaction();
+ #endif
+
+ // Check if status is ready.
+ return x == PN532_SPI_READY;
+ }
+ else {
+ // I2C check if status is ready by IRQ line being pulled low.
+ uint8_t x = digitalRead(_irq);
+ return x == 0;
+ }
+}
+
+/**************************************************************************/
+/*!
+ @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) {
+ 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 (_usingSPI) {
+ // SPI write.
+ #ifdef SPI_HAS_TRANSACTION
+ if (_hardwareSPI) SPI.beginTransaction(PN532_SPI_SETTING);
+ #endif
+ digitalWrite(_ss, LOW);
+ delay(2);
+ spi_write(PN532_SPI_DATAREAD);
+
+ #ifdef PN532DEBUG
+ Serial.print(F("Reading: "));
+ #endif
+ for (uint8_t i=0; i<n; i++) {
+ delay(1);
+ buff[i] = spi_read();
+ #ifdef PN532DEBUG
+ Serial.print(F(" 0x"));
+ Serial.print(buff[i], HEX);
+ #endif
+ }
+
+ #ifdef PN532DEBUG
+ Serial.println();
+ #endif
+
+ digitalWrite(_ss, HIGH);
+ #ifdef SPI_HAS_TRANSACTION
+ if (_hardwareSPI) SPI.endTransaction();
+ #endif
+ }
+ else {
+ // I2C write.
+ uint16_t timer = 0;
+
+ delay(2);
+
+ #ifdef PN532DEBUG
+ Serial.print(F("Reading: "));
+ #endif
+ // Start read (n+1 to take into account leading 0x01 with I2C)
+ WIRE.requestFrom((uint8_t)PN532_I2C_ADDRESS, (uint8_t)(n+2));
+ // Discard the leading 0x01
+ i2c_recv();
+ for (uint8_t i=0; i<n; i++) {
+ delay(1);
+ buff[i] = i2c_recv();
+ #ifdef PN532DEBUG
+ Serial.print(F(" 0x"));
+ Serial.print(buff[i], HEX);
+ #endif
+ }
+ // Discard trailing 0x00 0x00
+ // i2c_recv();
+
+ #ifdef PN532DEBUG
+ Serial.println();
+ #endif
+ }
+}
+
+/**************************************************************************/
+/*!
+ @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 (_usingSPI) {
+ // SPI command write.
+ uint8_t checksum;
+
+ cmdlen++;
+
+ #ifdef PN532DEBUG
+ Serial.print(F("\nSending: "));
+ #endif
+
+ #ifdef SPI_HAS_TRANSACTION
+ if (_hardwareSPI) SPI.beginTransaction(PN532_SPI_SETTING);
+ #endif
+ digitalWrite(_ss, LOW);
+ delay(2); // or whatever the delay is for waking up the board
+ spi_write(PN532_SPI_DATAWRITE);
+
+ checksum = PN532_PREAMBLE + PN532_PREAMBLE + PN532_STARTCODE2;
+ spi_write(PN532_PREAMBLE);
+ spi_write(PN532_PREAMBLE);
+ spi_write(PN532_STARTCODE2);
+
+ spi_write(cmdlen);
+ spi_write(~cmdlen + 1);
+
+ spi_write(PN532_HOSTTOPN532);
+ checksum += PN532_HOSTTOPN532;
+
+ #ifdef PN532DEBUG
+ Serial.print(F(" 0x")); Serial.print(PN532_PREAMBLE, HEX);
+ Serial.print(F(" 0x")); Serial.print(PN532_PREAMBLE, HEX);
+ Serial.print(F(" 0x")); Serial.print(PN532_STARTCODE2, HEX);
+ Serial.print(F(" 0x")); Serial.print(cmdlen, HEX);
+ Serial.print(F(" 0x")); Serial.print(~cmdlen + 1, HEX);
+ Serial.print(F(" 0x")); Serial.print(PN532_HOSTTOPN532, HEX);
+ #endif
+
+ for (uint8_t i=0; i<cmdlen-1; i++) {
+ spi_write(cmd[i]);
+ checksum += cmd[i];
+ #ifdef PN532DEBUG
+ Serial.print(F(" 0x")); Serial.print(cmd[i], HEX);
+ #endif
+ }
+
+ spi_write(~checksum);
+ spi_write(PN532_POSTAMBLE);
+ digitalWrite(_ss, HIGH);
+ #ifdef SPI_HAS_TRANSACTION
+ if (_hardwareSPI) SPI.endTransaction();
+ #endif
+
+ #ifdef PN532DEBUG
+ Serial.print(F(" 0x")); Serial.print(~checksum, HEX);
+ Serial.print(F(" 0x")); Serial.print(PN532_POSTAMBLE, HEX);
+ Serial.println();
+ #endif
+ }
+ else {
+ // I2C command write.
+ uint8_t checksum;
+
+ cmdlen++;
+
+ #ifdef PN532DEBUG
+ Serial.print(F("\nSending: "));
+ #endif
+
+ delay(2); // or whatever the delay is for waking up the board
+
+ // I2C START
+ WIRE.beginTransmission(PN532_I2C_ADDRESS);
+ checksum = PN532_PREAMBLE + PN532_PREAMBLE + PN532_STARTCODE2;
+ i2c_send(PN532_PREAMBLE);
+ i2c_send(PN532_PREAMBLE);
+ i2c_send(PN532_STARTCODE2);
+
+ i2c_send(cmdlen);
+ i2c_send(~cmdlen + 1);
+
+ i2c_send(PN532_HOSTTOPN532);
+ checksum += PN532_HOSTTOPN532;
+
+ #ifdef PN532DEBUG
+ Serial.print(F(" 0x")); Serial.print(PN532_PREAMBLE, HEX);
+ Serial.print(F(" 0x")); Serial.print(PN532_PREAMBLE, HEX);
+ Serial.print(F(" 0x")); Serial.print(PN532_STARTCODE2, HEX);
+ Serial.print(F(" 0x")); Serial.print(cmdlen, HEX);
+ Serial.print(F(" 0x")); Serial.print(~cmdlen + 1, HEX);
+ Serial.print(F(" 0x")); Serial.print(PN532_HOSTTOPN532, HEX);
+ #endif
+
+ for (uint8_t i=0; i<cmdlen-1; i++) {
+ i2c_send(cmd[i]);
+ checksum += cmd[i];
+ #ifdef PN532DEBUG
+ Serial.print(F(" 0x")); Serial.print(cmd[i], HEX);
+ #endif
+ }
+
+ i2c_send(~checksum);
+ i2c_send(PN532_POSTAMBLE);
+
+ // I2C STOP
+ WIRE.endTransmission();
+
+ #ifdef PN532DEBUG
+ Serial.print(F(" 0x")); Serial.print(~checksum, HEX);
+ Serial.print(F(" 0x")); Serial.print(PN532_POSTAMBLE, HEX);
+ Serial.println();
+ #endif
+
+ }
+}
+/************** low level SPI */
+
+/**************************************************************************/
+/*!
+ @brief Low-level SPI write wrapper
+
+ @param c 8-bit command to write to the SPI bus
+*/
+/**************************************************************************/
+void Adafruit_PN532::spi_write(uint8_t c) {
+ if (_hardwareSPI) {
+ // Hardware SPI write.
+ SPI.transfer(c);
+ }
+ else {
+ // Software SPI write.
+ int8_t i;
+ digitalWrite(_clk, HIGH);
+
+ for (i=0; i<8; i++) {
+ digitalWrite(_clk, LOW);
+ if (c & _BV(i)) {
+ digitalWrite(_mosi, HIGH);
+ } else {
+ digitalWrite(_mosi, LOW);
+ }
+ digitalWrite(_clk, HIGH);
+ }
+ }
+}
+
+/**************************************************************************/
+/*!
+ @brief Low-level SPI read wrapper
+
+ @returns The 8-bit value that was read from the SPI bus
+*/
+/**************************************************************************/
+uint8_t Adafruit_PN532::spi_read(void) {
+ int8_t i, x;
+ x = 0;
+
+ if (_hardwareSPI) {
+ // Hardware SPI read.
+ x = SPI.transfer(0x00);
+ }
+ else {
+ // Software SPI read.
+ digitalWrite(_clk, HIGH);
+
+ for (i=0; i<8; i++) {
+ if (digitalRead(_miso)) {
+ x |= _BV(i);
+ }
+ digitalWrite(_clk, LOW);
+ digitalWrite(_clk, HIGH);
+ }
+ }
+
+ return x;
+}
diff --git a/arduino/lib/Adafruit-PN532/Adafruit_PN532.h b/arduino/lib/Adafruit-PN532/Adafruit_PN532.h
new file mode 100644
index 0000000..e5888bf
--- /dev/null
+++ b/arduino/lib/Adafruit-PN532/Adafruit_PN532.h
@@ -0,0 +1,222 @@
+/**************************************************************************/
+/*!
+ @file Adafruit_PN532.h
+ @author Adafruit Industries
+ @license BSD (see license.txt)
+
+
+ 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 HISTORY
+
+ 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
+
+#if ARDUINO >= 100
+ #include "Arduino.h"
+#else
+ #include "WProgram.h"
+#endif
+
+#define PN532_PREAMBLE (0x00)
+#define PN532_STARTCODE1 (0x00)
+#define PN532_STARTCODE2 (0xFF)
+#define PN532_POSTAMBLE (0x00)
+
+#define PN532_HOSTTOPN532 (0xD4)
+#define PN532_PN532TOHOST (0xD5)
+
+// PN532 Commands
+#define PN532_COMMAND_DIAGNOSE (0x00)
+#define PN532_COMMAND_GETFIRMWAREVERSION (0x02)
+#define PN532_COMMAND_GETGENERALSTATUS (0x04)
+#define PN532_COMMAND_READREGISTER (0x06)
+#define PN532_COMMAND_WRITEREGISTER (0x08)
+#define PN532_COMMAND_READGPIO (0x0C)
+#define PN532_COMMAND_WRITEGPIO (0x0E)
+#define PN532_COMMAND_SETSERIALBAUDRATE (0x10)
+#define PN532_COMMAND_SETPARAMETERS (0x12)
+#define PN532_COMMAND_SAMCONFIGURATION (0x14)
+#define PN532_COMMAND_POWERDOWN (0x16)
+#define PN532_COMMAND_RFCONFIGURATION (0x32)
+#define PN532_COMMAND_RFREGULATIONTEST (0x58)
+#define PN532_COMMAND_INJUMPFORDEP (0x56)
+#define PN532_COMMAND_INJUMPFORPSL (0x46)
+#define PN532_COMMAND_INLISTPASSIVETARGET (0x4A)
+#define PN532_COMMAND_INATR (0x50)
+#define PN532_COMMAND_INPSL (0x4E)
+#define PN532_COMMAND_INDATAEXCHANGE (0x40)
+#define PN532_COMMAND_INCOMMUNICATETHRU (0x42)
+#define PN532_COMMAND_INDESELECT (0x44)
+#define PN532_COMMAND_INRELEASE (0x52)
+#define PN532_COMMAND_INSELECT (0x54)
+#define PN532_COMMAND_INAUTOPOLL (0x60)
+#define PN532_COMMAND_TGINITASTARGET (0x8C)
+#define PN532_COMMAND_TGSETGENERALBYTES (0x92)
+#define PN532_COMMAND_TGGETDATA (0x86)
+#define PN532_COMMAND_TGSETDATA (0x8E)
+#define PN532_COMMAND_TGSETMETADATA (0x94)
+#define PN532_COMMAND_TGGETINITIATORCOMMAND (0x88)
+#define PN532_COMMAND_TGRESPONSETOINITIATOR (0x90)
+#define PN532_COMMAND_TGGETTARGETSTATUS (0x8A)
+
+#define PN532_RESPONSE_INDATAEXCHANGE (0x41)
+#define PN532_RESPONSE_INLISTPASSIVETARGET (0x4B)
+
+#define PN532_WAKEUP (0x55)
+
+#define PN532_SPI_STATREAD (0x02)
+#define PN532_SPI_DATAWRITE (0x01)
+#define PN532_SPI_DATAREAD (0x03)
+#define PN532_SPI_READY (0x01)
+
+#define PN532_I2C_ADDRESS (0x48 >> 1)
+#define PN532_I2C_READBIT (0x01)
+#define PN532_I2C_BUSY (0x00)
+#define PN532_I2C_READY (0x01)
+#define PN532_I2C_READYTIMEOUT (20)
+
+#define PN532_MIFARE_ISO14443A (0x00)
+
+// Mifare Commands
+#define MIFARE_CMD_AUTH_A (0x60)
+#define MIFARE_CMD_AUTH_B (0x61)
+#define MIFARE_CMD_READ (0x30)
+#define MIFARE_CMD_WRITE (0xA0)
+#define MIFARE_CMD_TRANSFER (0xB0)
+#define MIFARE_CMD_DECREMENT (0xC0)
+#define MIFARE_CMD_INCREMENT (0xC1)
+#define MIFARE_CMD_STORE (0xC2)
+#define MIFARE_ULTRALIGHT_CMD_WRITE (0xA2)
+
+// Prefixes for NDEF Records (to identify record type)
+#define NDEF_URIPREFIX_NONE (0x00)
+#define NDEF_URIPREFIX_HTTP_WWWDOT (0x01)
+#define NDEF_URIPREFIX_HTTPS_WWWDOT (0x02)
+#define NDEF_URIPREFIX_HTTP (0x03)
+#define NDEF_URIPREFIX_HTTPS (0x04)
+#define NDEF_URIPREFIX_TEL (0x05)
+#define NDEF_URIPREFIX_MAILTO (0x06)
+#define NDEF_URIPREFIX_FTP_ANONAT (0x07)
+#define NDEF_URIPREFIX_FTP_FTPDOT (0x08)
+#define NDEF_URIPREFIX_FTPS (0x09)
+#define NDEF_URIPREFIX_SFTP (0x0A)
+#define NDEF_URIPREFIX_SMB (0x0B)
+#define NDEF_URIPREFIX_NFS (0x0C)
+#define NDEF_URIPREFIX_FTP (0x0D)
+#define NDEF_URIPREFIX_DAV (0x0E)
+#define NDEF_URIPREFIX_NEWS (0x0F)
+#define NDEF_URIPREFIX_TELNET (0x10)
+#define NDEF_URIPREFIX_IMAP (0x11)
+#define NDEF_URIPREFIX_RTSP (0x12)
+#define NDEF_URIPREFIX_URN (0x13)
+#define NDEF_URIPREFIX_POP (0x14)
+#define NDEF_URIPREFIX_SIP (0x15)
+#define NDEF_URIPREFIX_SIPS (0x16)
+#define NDEF_URIPREFIX_TFTP (0x17)
+#define NDEF_URIPREFIX_BTSPP (0x18)
+#define NDEF_URIPREFIX_BTL2CAP (0x19)
+#define NDEF_URIPREFIX_BTGOEP (0x1A)
+#define NDEF_URIPREFIX_TCPOBEX (0x1B)
+#define NDEF_URIPREFIX_IRDAOBEX (0x1C)
+#define NDEF_URIPREFIX_FILE (0x1D)
+#define NDEF_URIPREFIX_URN_EPC_ID (0x1E)
+#define NDEF_URIPREFIX_URN_EPC_TAG (0x1F)
+#define NDEF_URIPREFIX_URN_EPC_PAT (0x20)
+#define NDEF_URIPREFIX_URN_EPC_RAW (0x21)
+#define NDEF_URIPREFIX_URN_EPC (0x22)
+#define NDEF_URIPREFIX_URN_NFC (0x23)
+
+#define PN532_GPIO_VALIDATIONBIT (0x80)
+#define PN532_GPIO_P30 (0)
+#define PN532_GPIO_P31 (1)
+#define PN532_GPIO_P32 (2)
+#define PN532_GPIO_P33 (3)
+#define PN532_GPIO_P34 (4)
+#define PN532_GPIO_P35 (5)
+
+class Adafruit_PN532{
+ public:
+ Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t ss); // Software SPI
+ Adafruit_PN532(uint8_t irq, uint8_t reset); // Hardware I2C
+ Adafruit_PN532(uint8_t ss); // Hardware SPI
+ void begin(void);
+
+ // Generic PN532 functions
+ bool SAMConfig(void);
+ uint32_t getFirmwareVersion(void);
+ bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout = 1000);
+ bool writeGPIO(uint8_t pinstate);
+ uint8_t readGPIO(void);
+ bool setPassiveActivationRetries(uint8_t maxRetries);
+
+ // 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 inDataExchange(uint8_t * send, uint8_t sendLength, uint8_t * response, uint8_t * responseLength);
+ bool inListPassiveTarget();
+
+ // 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);
+ 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);
+
+ // 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:
+ uint8_t _ss, _clk, _mosi, _miso;
+ uint8_t _irq, _reset;
+ uint8_t _uid[7]; // ISO14443A uid
+ uint8_t _uidLen; // uid len
+ uint8_t _key[6]; // Mifare Classic key
+ uint8_t _inListedTag; // Tg number of inlisted tag.
+ bool _usingSPI; // True if using SPI, false if using I2C.
+ bool _hardwareSPI; // True is using hardware SPI, false if using software SPI.
+
+ // 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();
+
+ // SPI-specific functions.
+ void spi_write(uint8_t c);
+ uint8_t spi_read(void);
+
+ // Note there are i2c_read and i2c_write inline functions defined in the .cpp file.
+};
+
+#endif
diff --git a/arduino/lib/Adafruit-PN532/README.txt b/arduino/lib/Adafruit-PN532/README.txt
new file mode 100644
index 0000000..37276b1
--- /dev/null
+++ b/arduino/lib/Adafruit-PN532/README.txt
@@ -0,0 +1,18 @@
+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.
+
+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 download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder Adafruit_PN532. Check that the Adafruit_PN532 folder contains Adafruit_PN532.cpp and Adafruit_PN532.h
+
+Place the Adafruit_PN532 library folder your <arduinosketchfolder>/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.
diff --git a/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/.classpath b/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/.classpath
new file mode 100755
index 0000000..4426948
--- /dev/null
+++ b/arduino/lib/Adafruit-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/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/.project b/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/.project
new file mode 100755
index 0000000..1a8239a
--- /dev/null
+++ b/arduino/lib/Adafruit-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/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/.settings/org.eclipse.jdt.core.prefs b/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/.settings/org.eclipse.jdt.core.prefs
new file mode 100755
index 0000000..3f59a3e
--- /dev/null
+++ b/arduino/lib/Adafruit-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.3
diff --git a/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/BlackBerry_App_Descriptor.xml b/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/BlackBerry_App_Descriptor.xml
new file mode 100755
index 0000000..aa610d2
--- /dev/null
+++ b/arduino/lib/Adafruit-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/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/build.xml b/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/build.xml
new file mode 100755
index 0000000..72bf0c3
--- /dev/null
+++ b/arduino/lib/Adafruit-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/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/res/img/icon.png b/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/res/img/icon.png
new file mode 100755
index 0000000..193950c
--- /dev/null
+++ b/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/res/img/icon.png
Binary files differ
diff --git a/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/ColorMixer.java b/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/ColorMixer.java
new file mode 100755
index 0000000..d81f802
--- /dev/null
+++ b/arduino/lib/Adafruit-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/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/MyApp.java b/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/MyApp.java
new file mode 100755
index 0000000..4da1da5
--- /dev/null
+++ b/arduino/lib/Adafruit-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/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/MyScreen.java b/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/BlackBerry7/src/mypackage/MyScreen.java
new file mode 100755
index 0000000..0f80667
--- /dev/null
+++ b/arduino/lib/Adafruit-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/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/apduToBlackBerry.pde b/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/apduToBlackBerry.pde
new file mode 100755
index 0000000..df8fc25
--- /dev/null
+++ b/arduino/lib/Adafruit-PN532/examples/apduToBlackBerry/apduToBlackBerry.pde
@@ -0,0 +1,149 @@
+/**
+ 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);
+
+ 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);
+
+ // configure board to read RFID tags
+ nfc.SAMConfig();
+
+ 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/arduino/lib/Adafruit-PN532/examples/iso14443a_uid/iso14443a_uid.pde b/arduino/lib/Adafruit-PN532/examples/iso14443a_uid/iso14443a_uid.pde
new file mode 100644
index 0000000..e347635
--- /dev/null
+++ b/arduino/lib/Adafruit-PN532/examples/iso14443a_uid/iso14443a_uid.pde
@@ -0,0 +1,112 @@
+/**************************************************************************/
+/*!
+ @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);
+ 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);
+
+ // configure board to read RFID tags
+ nfc.SAMConfig();
+
+ 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/arduino/lib/Adafruit-PN532/examples/mifareclassic_formatndef/mifareclassic_formatndef.pde b/arduino/lib/Adafruit-PN532/examples/mifareclassic_formatndef/mifareclassic_formatndef.pde
new file mode 100644
index 0000000..e522c02
--- /dev/null
+++ b/arduino/lib/Adafruit-PN532/examples/mifareclassic_formatndef/mifareclassic_formatndef.pde
@@ -0,0 +1,203 @@
+/**************************************************************************/
+/*!
+ @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 = "mail@example.com";
+//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);
+ 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);
+
+ // configure board to read RFID tags
+ nfc.SAMConfig();
+}
+
+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)
+ bool authenticated = false; // Flag to indicate if the sector is authenticated
+
+ // 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/arduino/lib/Adafruit-PN532/examples/mifareclassic_memdump/mifareclassic_memdump.pde b/arduino/lib/Adafruit-PN532/examples/mifareclassic_memdump/mifareclassic_memdump.pde
new file mode 100644
index 0000000..4a88058
--- /dev/null
+++ b/arduino/lib/Adafruit-PN532/examples/mifareclassic_memdump/mifareclassic_memdump.pde
@@ -0,0 +1,191 @@
+/**************************************************************************/
+/*!
+ @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);
+ 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);
+
+ // configure board to read RFID tags
+ nfc.SAMConfig();
+
+ 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/arduino/lib/Adafruit-PN532/examples/mifareclassic_ndeftoclassic/mifareclassic_ndeftoclassic.pde b/arduino/lib/Adafruit-PN532/examples/mifareclassic_ndeftoclassic/mifareclassic_ndeftoclassic.pde
new file mode 100644
index 0000000..ca4acb8
--- /dev/null
+++ b/arduino/lib/Adafruit-PN532/examples/mifareclassic_ndeftoclassic/mifareclassic_ndeftoclassic.pde
@@ -0,0 +1,208 @@
+/**************************************************************************/
+/*!
+ @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);
+ 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);
+
+ // configure board to read RFID tags
+ nfc.SAMConfig();
+}
+
+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)
+ bool authenticated = false; // Flag to indicate if the sector is authenticated
+ 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();
+} \ No newline at end of file
diff --git a/arduino/lib/Adafruit-PN532/examples/mifareclassic_updatendef/mifareclassic_updatendef.pde b/arduino/lib/Adafruit-PN532/examples/mifareclassic_updatendef/mifareclassic_updatendef.pde
new file mode 100644
index 0000000..1f041be
--- /dev/null
+++ b/arduino/lib/Adafruit-PN532/examples/mifareclassic_updatendef/mifareclassic_updatendef.pde
@@ -0,0 +1,180 @@
+/**************************************************************************/
+/*!
+ @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 = "mail@example.com";
+//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);
+ 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);
+
+ // configure board to read RFID tags
+ nfc.SAMConfig();
+}
+
+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)
+ bool authenticated = false; // Flag to indicate if the sector is authenticated
+
+ // Use the default NDEF keys (these would have have set by mifareclassic_formatndef.pde!)
+ uint8_t keya[6] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 };
+ 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/arduino/lib/Adafruit-PN532/examples/ntag2xx_erase/ntag2xx_erase.pde b/arduino/lib/Adafruit-PN532/examples/ntag2xx_erase/ntag2xx_erase.pde
new file mode 100644
index 0000000..a14a62a
--- /dev/null
+++ b/arduino/lib/Adafruit-PN532/examples/ntag2xx_erase/ntag2xx_erase.pde
@@ -0,0 +1,159 @@
+/**************************************************************************/
+/*!
+ @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);
+ 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);
+
+ // configure board to read RFID tags
+ nfc.SAMConfig();
+
+ 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..29");
+ 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/arduino/lib/Adafruit-PN532/examples/ntag2xx_read/ntag2xx_read.pde b/arduino/lib/Adafruit-PN532/examples/ntag2xx_read/ntag2xx_read.pde
new file mode 100644
index 0000000..ed09cb9
--- /dev/null
+++ b/arduino/lib/Adafruit-PN532/examples/ntag2xx_read/ntag2xx_read.pde
@@ -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);
+ 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);
+
+ // configure board to read RFID tags
+ nfc.SAMConfig();
+
+ 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/arduino/lib/Adafruit-PN532/examples/ntag2xx_updatendef/ntag2xx_updatendef.pde b/arduino/lib/Adafruit-PN532/examples/ntag2xx_updatendef/ntag2xx_updatendef.pde
new file mode 100644
index 0000000..fc8f27a
--- /dev/null
+++ b/arduino/lib/Adafruit-PN532/examples/ntag2xx_updatendef/ntag2xx_updatendef.pde
@@ -0,0 +1,213 @@
+/**************************************************************************/
+/*!
+ @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 = "mail@example.com";
+//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);
+ 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);
+
+ // configure board to read RFID tags
+ nfc.SAMConfig();
+}
+
+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 file
diff --git a/arduino/lib/Adafruit-PN532/examples/readMifare/readMifare.pde b/arduino/lib/Adafruit-PN532/examples/readMifare/readMifare.pde
new file mode 100644
index 0000000..b30ca67
--- /dev/null
+++ b/arduino/lib/Adafruit-PN532/examples/readMifare/readMifare.pde
@@ -0,0 +1,182 @@
+/**************************************************************************/
+/*!
+ @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);
+
+void setup(void) {
+ Serial.begin(115200);
+ 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);
+
+ // configure board to read RFID tags
+ nfc.SAMConfig();
+
+ 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/arduino/lib/Adafruit-PN532/examples/readMifareClassic/readMifareClassic.pde b/arduino/lib/Adafruit-PN532/examples/readMifareClassic/readMifareClassic.pde
new file mode 100644
index 0000000..c890be4
--- /dev/null
+++ b/arduino/lib/Adafruit-PN532/examples/readMifareClassic/readMifareClassic.pde
@@ -0,0 +1,120 @@
+/**************************************************************************/
+/*!
+ @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);
+ 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);
+
+ // configure board to read RFID tags
+ nfc.SAMConfig();
+
+ 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/arduino/lib/Adafruit-PN532/license.txt b/arduino/lib/Adafruit-PN532/license.txt
new file mode 100644
index 0000000..f6a0f22
--- /dev/null
+++ b/arduino/lib/Adafruit-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.