adafruit / Adafruit-PN532

Arduino library for SPI and I2C access to the PN532 RFID/Near Field Communication chip
Other
421 stars 265 forks source link

ntag2xx_WriteNDEFURI sets up incorrect Lock Control TLV #87

Open vmsh0 opened 3 years ago

vmsh0 commented 3 years ago

Hello,

The Adafruit_PN532::ntag2xx_WriteNDEFURI method writes an NDEF URI in a NTAG2xx tag. To do so, it completely reformats the user data memory area. This is how that's achieved:

  // 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 */
      [ ... etc ... ]
  };

While reformatting the memory area, the method writes a Lock Control TLV.

There are two issues with this: 1) The TLV, as hardcoded in the method, seems (i.e. as per back-of-the-envelope computation) to be completely wrong for all NTAG2xx platforms, and as it is hardcoded, it can ever be correct correct for at most a specific model of the platform series 2) In contrast to what is suggested in the comment, the TVL block does not always have to be present; the NFC Forum standard for T2T platforms states (RQ_T2T_MEM_021): "The Lock Control TLV can be present inside the Type 2 Tag Platform." And in fact, in the case of NTAG2xx platforms, it is completely useless, as the user data area is contiguous and limited by the Capability Container.

Since it is both wrong and useless, I suggest that it should not be written. If you agree, I would be happy to submit a pull request.