uint8_t 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; }
uint8_t Adafruit_NFCShield_I2C::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}; // 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; }
uint8_t 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; }