/* * Author: Jon Trulson * Copyright (c) 2015 Intel Corporation. * * This code is heavily based on the Adafruit-PN532 library at * https://github.com/adafruit/Adafruit-PN532, which is licensed under * the BSD license. See upm/src/pn532/license.txt * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include "pn532.h" using namespace upm; using namespace std; #define PN532_PACKBUFFSIZ 64 static uint8_t pn532_packetbuffer[PN532_PACKBUFFSIZ]; static uint8_t pn532ack[] = {0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00}; static uint32_t pn532_firmwarerev = 0x00320106; PN532::PN532(int irq, int reset, int bus, uint8_t address): m_gpioIRQ(irq), m_gpioReset(reset), m_i2c(bus) { m_addr = address; m_uidLen = 0; m_inListedTag = 0; m_SAK = 0; m_ATQA = 0; m_isrInstalled = false; m_irqRcvd = false; memset(m_uid, 0, 7); memset(m_key, 0, 6); // turn off debugging by default pn532Debug(false); mifareDebug(false); mraa::Result rv; if ( (rv = m_i2c.address(m_addr)) != mraa::SUCCESS) { throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.address() failed"); return; } m_gpioIRQ.dir(mraa::DIR_IN); m_gpioReset.dir(mraa::DIR_OUT); } PN532::~PN532() { if (m_isrInstalled) m_gpioIRQ.isrExit(); } bool PN532::init() { m_gpioReset.write(1); m_gpioReset.write(0); usleep(400000); // install an interrupt handler m_gpioIRQ.isr(mraa::EDGE_FALLING, dataReadyISR, this); m_isrInstalled = true; m_gpioReset.write(1); return true; } /**************************************************************************/ /*! @brief Prints a hexadecimal value in plain characters @param data Pointer to the byte data @param numBytes Data length in bytes */ /**************************************************************************/ static void PrintHex(const uint8_t * data, const uint32_t numBytes) { uint32_t szPos; for (szPos=0; szPos < numBytes; szPos++) { fprintf(stderr, "0x%02x ", data[szPos] & 0xff); } fprintf(stderr, "\n"); } /**************************************************************************/ /*! @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 */ /**************************************************************************/ static void PrintHexChar(const uint8_t * data, const uint32_t numBytes) { uint32_t szPos; for (szPos=0; szPos < numBytes; szPos++) { fprintf(stderr, "0x%02x ", data[szPos] & 0xff); } fprintf(stderr, " "); for (szPos=0; szPos < numBytes; szPos++) { if (data[szPos] <= 0x1F) fprintf(stderr, "."); else fprintf(stderr, "%c ", (char)data[szPos]); } fprintf(stderr, "\n"); } /**************************************************************************/ /*! @brief Checks the firmware version of the PN5xx chip @returns The chip's firmware version and ID */ /**************************************************************************/ uint32_t PN532::getFirmwareVersion() { uint32_t response = 0; pn532_packetbuffer[0] = CMD_GETFIRMWAREVERSION; if (! sendCommandCheckAck(pn532_packetbuffer, 1)) return 0; // read data packet readData(pn532_packetbuffer, 12); int offset = 7; // Skip the ready byte when using I2C response <<= 8; response |= pn532_packetbuffer[offset++]; response <<= 8; response |= pn532_packetbuffer[offset++]; response <<= 8; response |= pn532_packetbuffer[offset++]; if (response != pn532_firmwarerev) fprintf(stderr, "Warning: firmware revision 0x%08x does not match expected rev 0x%08x\n", response, pn532_firmwarerev); 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 PN532::sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout) { uint16_t timer = 0; // clear any outstanding irq's isReady(); // write the command writeCommand(cmd, cmdlen); // Wait for chip to say its ready! if (!waitForReady(timeout)) { cerr << __FUNCTION__ << ": Not ready, timeout" << endl; return false; } if (m_pn532Debug) cerr << __FUNCTION__ << ": IRQ received" << endl; // read acknowledgement if (!readAck()) { if (m_pn532Debug) cerr << __FUNCTION__ << ": No ACK frame received!" << endl; return false; } return true; // ack'd command } /**************************************************************************/ /*! @brief Configures the SAM (Secure Access Module) */ /**************************************************************************/ bool PN532::SAMConfig(void) { pn532_packetbuffer[0] = CMD_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 = 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 PN532::setPassiveActivationRetries(uint8_t maxRetries) { pn532_packetbuffer[0] = CMD_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; if (m_mifareDebug) cerr << __FUNCTION__ << ": Setting MxRtyPassiveActivation to " << (int)maxRetries << endl; if (! sendCommandCheckAck(pn532_packetbuffer, 5)) return false; // no ACK return true; } /***** 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 PN532::readPassiveTargetID(BAUD_T cardbaudrate, uint8_t * uid, uint8_t * uidLength, uint16_t timeout) { pn532_packetbuffer[0] = CMD_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)) { if (m_pn532Debug) cerr << __FUNCTION__ << ": No card(s) read" << endl; return false; // no cards read } // wait for a card to enter the field (only possible with I2C) if (m_pn532Debug) cerr << __FUNCTION__ << ": Waiting for IRQ (indicates card presence)" << endl; if (!waitForReady(timeout)) { if (m_pn532Debug) cerr << __FUNCTION__ << ": IRQ Timeout" << endl; return false; } // 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 */ // SENS_RES SEL_RES Manufacturer/Card Type NFCID Len // -------- ------- ----------------------- --------- // 00 04 08 NXP Mifare Classic 1K 4 bytes // 00 02 18 NXP Mifare Classic 4K 4 bytes if (m_mifareDebug) cerr << __FUNCTION__ << ": Found " << (int)pn532_packetbuffer[7] << " tags" << endl; // only one card can be handled currently if (pn532_packetbuffer[7] != 1) return false; uint16_t sens_res = pn532_packetbuffer[9]; sens_res <<= 8; sens_res |= pn532_packetbuffer[10]; // store these for later retrieval, they can be used to more accurately // ID the type of card. m_ATQA = sens_res; m_SAK = pn532_packetbuffer[11]; // SEL_RES if (m_mifareDebug) { fprintf(stderr, "ATQA: 0x%04x\n", m_ATQA); fprintf(stderr, "SAK: 0x%02x\n", m_SAK); } /* Card appears to be Mifare Classic */ // JET: How so? *uidLength = pn532_packetbuffer[12]; if (m_mifareDebug) fprintf(stderr, "UID: "); for (uint8_t i=0; i < pn532_packetbuffer[12]; i++) { uid[i] = pn532_packetbuffer[13+i]; if (m_mifareDebug) fprintf(stderr, "0x%02x ", uid[i]); } if (m_mifareDebug) fprintf(stderr, "\n"); return true; } /**************************************************************************/ /*! @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 PN532::inDataExchange(uint8_t * send, uint8_t sendLength, uint8_t * response, uint8_t * responseLength) { if (sendLength > PN532_PACKBUFFSIZ-2) { if (m_pn532Debug) cerr << __FUNCTION__ << ": APDU length too long for packet buffer" << endl; return false; } uint8_t i; pn532_packetbuffer[0] = CMD_INDATAEXCHANGE; // 0x40 pn532_packetbuffer[1] = m_inListedTag; for (i=0; i *responseLength) { length = *responseLength; // silent truncation... } for (i=0; i 15)) return false; // Make sure the URI payload is between 1 and 38 chars if ((len < 1) || (len > 38)) return false; // 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, static_cast(len+5), 0xD1, 0x01, static_cast(len+1), 0x55, static_cast(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-23] = 0xFE; } // Now write all three blocks back to the card if (!(mifareclassic_WriteDataBlock (sectorNumber*4, sectorbuffer1))) return false; if (!(mifareclassic_WriteDataBlock ((sectorNumber*4)+1, sectorbuffer2))) return false; if (!(mifareclassic_WriteDataBlock ((sectorNumber*4)+2, sectorbuffer3))) return false; if (!(mifareclassic_WriteDataBlock ((sectorNumber*4)+3, sectorbuffer4))) return false; // Seems that everything was OK (?!) return true; } /***** NTAG2xx/ultralight Functions ******/ // Ultralight tags are limited to 64 pages max, with ntag2XX tags can // have up to 231 pages. /* MIFARE ULTRALIGHT DESCRIPTION ============================= Taken from: https://www.kismetwireless.net/code-old/svn/hardware/kisbee-02/firmware/drivers/rf/pn532/helpers/pn532_mifare_ultralight.c MIFARE Ultralight cards typically contain 512 bits (64 bytes) of memory, including 4 bytes (32-bits) of OTP (One Time Programmable) memory where the individual bits can be written but not erased. MF0ICU1 Mifare Ultralight Functional Specification: http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf Mifare Ultralight cards have a 7-byte UID EEPROM MEMORY ============= Mifare Ultralight cards have 512 bits (64 bytes) of EEPROM memory, including 4 byte (32 bits) of OTP memory. Unlike Mifare Classic cards, there is no authentication on a per block level, although the blocks can be set to "read-only" mode using Lock Bytes (described below). EEPROM memory is organised into 16 pages of four bytes eachs, in the following order Page Description ---- ------------ 0 Serial Number (4 bytes) 1 Serial Number (4 bytes) 2 Byte 0: Serial Number Byte 1: Internal Memory Byte 2..3: lock bytes 3 One-time programmable memory (4 bytes) 4..15 User memory (4 bytes) Lock Bytes (Page 2) ------------------- Bytes 2 and 3 of page 2 are referred to as "Lock Bytes". Each page from 0x03 and higher can individually locked by setting the corresponding locking bit to "1" to prevent further write access, effectively making the memory read only. For information on the lock byte mechanism, refer to section 8.5.2 of the datasheet (referenced above). OTP Bytes (Page 3) ------------------ Page 3 is the OTP memory, and by default all bits on this page are set to 0. These bits can be bitwise modified using the Mifare WRITE command, and individual bits can be set to 1, but can not be changed back to 0. Data Pages (Pages 4..15) ------------------------ Pages 4 to 15 are can be freely read from and written to, provided there is no conflict with the Lock Bytes described above. After production, the bytes have the following default values: Page Byte Values ---- ---------------------- 0 1 2 3 4 0xFF 0xFF 0xFF 0xFF 5..15 0x00 0x00 0x00 0x00 ACCESSING DATA BLOCKS ===================== Before you can access the cards, you must following two steps: 1.) 'Connect' to a Mifare Ultralight card and retrieve the 7 byte UID of the card. 2.) Memory can be read and written directly once a passive mode connection has been made. No authentication is required for Mifare Ultralight cards. */ /**************************************************************************/ /*! 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) */ /**************************************************************************/ bool 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) { cerr << __FUNCTION__ << ": Page value out of range" << endl; return false; } if (m_mifareDebug) fprintf(stderr, "Reading page %d\n", page); /* Prepare the command */ pn532_packetbuffer[0] = CMD_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)) { if (m_mifareDebug) cerr << __FUNCTION__ << ": Failed to receive ACK for write command" << endl; return false; } /* Read the response packet */ readData(pn532_packetbuffer, 26); if (m_mifareDebug) { fprintf(stderr, "Received: \n"); PrintHexChar(pn532_packetbuffer, 26); } /* 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 { if (m_mifareDebug) { fprintf(stderr, "Unexpected response reading block: \n"); PrintHexChar(pn532_packetbuffer, 26); } return false; } /* Display data for debug if requested */ if (m_mifareDebug) { fprintf(stderr, "Page %d:\n", page); PrintHexChar(buffer, 4); } // Return OK signal return true; } /**************************************************************************/ /*! 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 */ /**************************************************************************/ bool 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)) { cerr << __FUNCTION__ << ": Page value out of range" << endl; return false; } if (m_mifareDebug) fprintf(stderr, "Trying to write 4 byte page %d\n", page); /* Prepare the first command */ pn532_packetbuffer[0] = CMD_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)) { if (m_mifareDebug) cerr << __FUNCTION__ << ": Failed to receive ACK for write command" << endl; // Return Failed Signal return false; } usleep(10000); /* Read the response packet */ readData(pn532_packetbuffer, 26); // Return OK Signal return true; } /**************************************************************************/ /*! 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 */ /**************************************************************************/ bool PN532::ntag2xx_WriteNDEFURI (NDEF_URI_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 false; // 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) */ static_cast(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 */ static_cast(len+1), /* Payload len */ 0x55, /* Record Type Indicator (0x55 or 'U' = URI Record) */ static_cast(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 false; memcpy (pageBuffer, pageHeader+4, 4); if (!(ntag2xx_WritePage (5, pageBuffer))) return false; memcpy (pageBuffer, pageHeader+8, 4); if (!(ntag2xx_WritePage (6, pageBuffer))) return false; // 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 false; // DONE! return true; } else if (len == 4) { memcpy(pageBuffer, urlcopy, len); if (!(ntag2xx_WritePage (currentPage, pageBuffer))) return false; memset(pageBuffer, 0, 4); pageBuffer[0] = 0xFE; // NDEF record footer currentPage++; if (!(ntag2xx_WritePage (currentPage, pageBuffer))) return false; // DONE! return true; } else { // More than one page of data left memcpy(pageBuffer, urlcopy, 4); if (!(ntag2xx_WritePage (currentPage, pageBuffer))) return false; currentPage++; urlcopy+=4; len-=4; } } // Seems that everything was OK (?!) return true; } /**************************************************************************/ /*! @brief Tries to read/verify the ACK packet */ /**************************************************************************/ bool PN532::readAck() { uint8_t ackbuff[6]; readData(ackbuff, 6); return (0 == memcmp((char *)ackbuff, (char *)pn532ack, 6)); } /**************************************************************************/ /*! @brief Return true if the PN532 is ready with a response. */ /**************************************************************************/ bool PN532::isReady() { // ALWAYS clear the m_irqRcvd flag if set. if (m_irqRcvd) { m_irqRcvd = false; return true; } return false; } /**************************************************************************/ /*! @brief Waits until the PN532 is ready. @param timeout Timeout before giving up */ /**************************************************************************/ bool PN532::waitForReady(uint16_t timeout) { uint16_t timer = 0; while(!isReady()) { if (timeout != 0) { timer += 10; if (timer > timeout) { return false; } } usleep(10000); } 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 PN532::readData(uint8_t* buff, uint8_t n) { uint8_t buf[n + 2]; int rv; memset(buf, 0, n+2); usleep(2000); if (m_i2c.address(m_addr) != mraa::SUCCESS) { throw std::runtime_error(std::string(__FUNCTION__) + ": mraa_i2c_address() failed"); return; } rv = m_i2c.read(buf, n + 2); if (m_pn532Debug) { cerr << __FUNCTION__ << ": read returned " << rv << "bytes" << endl; fprintf(stderr, "(raw) buf (%d) = ", rv); PrintHex(buf, rv); fprintf(stderr, "\n"); } for (int i=0; im_irqRcvd = true; } PN532::TAG_TYPE_T PN532::tagType() { // This is really just a guess, ATQA and SAK could in theory be used // to refine the detection. if (m_uidLen == 4) return TAG_TYPE_MIFARE_CLASSIC; else if (m_uidLen == 7) return TAG_TYPE_NFC2; else return TAG_TYPE_UNKNOWN; }