NFC: Type 2 Tag parser

In addition to programming the NFC tag that is part of an nRF52 Series IC, you can also use other nRF Connect SDK functions to read the content of another NFC tag. Note that an nRF52 Series IC does not provide the hardware that is required for a polling device. If you want to create a polling device, you can use an ST25R3911B NFC shield together with an nRF5 Development Kit.

Using the NFC Reader ST25R3911B, you can read raw data from a Type 2 Tag, such as:

  • Number of TLV structures in the tag

  • Type 2 Tag internal bytes

  • Type 2 Tag lock bytes

  • Type 2 Tag capability container

  • Array of pointer to the TLV structures of the tag

See the NFC: Tag Reader sample for more information on how to do this.

The following code example shows how to parse the raw data from a Type 2 Tag:

/* Buffer with raw data from a Type 2 Tag. */
u8_t raw_tag_data[] = ..;

/* Declaration of Type 2 Tag structure which
 * can contain a maximum of 10 TLV blocks.
 */
NFC_T2T_DESC_DEF(tag_data, 10);
struct nfc_t2t *t2t = &NFC_T2T_DESC(tag_data);

err = nfc_t2t_parse(t2t, raw_tag_data);
if (err) {
        printk("Not enough memory to read the whole tag. Printing what has been read.\n");
}

/* Print the tag data. */
nfc_t2t_printout(t2t);

Note that the data can be further parsed by the NFC: NDEF message and record parser, given that the type of the analyzed TLV block is NDEF TLV.

API documentation

NFC Type 2 Tag parser

group nfc_t2t_parser

NFC Type 2 Tag parser.

Defines

NFC_T2T_DESC_DEF(_name, _max_blocks)

Create and initialize a Type 2 Tag descriptor.

This macro creates and initializes a static instance of an nfc_t2t structure and an array of Type 2 Tag TLV blocks descriptors.

Use the macro NFC_T2T_DESC to access the Type 2 Tag descriptor instance.

Parameters
  • [in] _name: Name of the created descriptor instance.

  • [in] _max_blocks: Maximum number of Type 2 Tag TLV blocks descriptors that can be stored in the array.

NFC_T2T_DESC(_name)

Access the nfc_t2t instance that was created with NFC_T2T_DESC_DEF.

NFC_T2T_NFC_FORUM_DEFINED_DATA

Value indicating that the Type 2 Tag contains NFC Forum defined data.

NFC_T2T_UID_BCC_CASCADE_BYTE

Value used for calculating the first BCC byte of a Type 2 Tag serial number.

NFC_T2T_SUPPORTED_MAJOR_VERSION

Supported major version of the Type 2 Tag specification.

NFC_T2T_SUPPORTED_MINOR_VERSION

Supported minor version of the Type 2 Tag specification.

NFC_T2T_BLOCK_SIZE

Type 2 Tag block size in bytes.

NFC_T2T_CC_BLOCK_OFFSET

Offset of the Capability Container area in the Type 2 Tag.

NFC_T2T_FIRST_DATA_BLOCK_OFFSET

Offset of the data area in the Type 2 Tag.

Functions

void nfc_t2t_clear(struct nfc_t2t *t2t)

Clear the nfc_t2t structure.

Parameters
  • [inout] t2t: Pointer to the structure that should be cleared.

int nfc_t2t_parse(struct nfc_t2t *t2t, const u8_t *raw_data)

Parse raw data that was read from a Type 2 Tag.

This function parses the header and the following TLV blocks of a Type 2 Tag. The data is read from a buffer and stored in a nfc_t2t structure.

Parameters
  • [out] t2t: Pointer to the structure that will be filled with parsed data.

  • [in] raw_data: Pointer to the buffer with raw data from the tag (should point at the first byte of the first block of the tag).

Return Value
  • 0: If the operation was successful. Otherwise, a (negative) error code is returned.

void nfc_t2t_printout(const struct nfc_t2t *t2t)

Print parsed contents of the Type 2 Tag.

Parameters
  • [in] t2t: Pointer to the structure that should be printed.

struct nfc_t2t_sn
#include <parser.h>

Descriptor for the internal bytes of a Type 2 Tag.

Public Members

u8_t manufacturer_id

Manufacturer ID (the most significant byte of the UID/serial number).

u16_t serial_number_part_1

Bytes 5-4 of the tag UID.

u32_t serial_number_part_2

Bytes 3-0 of the tag UID.

u8_t check_byte_0

First block check character byte (XOR of the cascade tag byte, manufacturer ID byte, and the serial_number_part_1 bytes).

u8_t check_byte_1

Second block check character byte (XOR of the serial_number_part_2 bytes).

u8_t internal

Tag internal bytes.

struct nfc_t2t_cc
#include <parser.h>

Descriptor for the Capability Container (CC) bytes of a Type 2 Tag.

Public Members

u8_t major_version

Major version of the supported Type 2 Tag specification.

u8_t minor_version

Minor version of the supported Type 2 Tag specification.

u16_t data_area_size

Size of the data area in bytes.

u8_t read_access

Read access for the data area.

u8_t write_access

Write access for the data area.

struct nfc_t2t
#include <parser.h>

Type 2 Tag descriptor.

Public Members

struct nfc_t2t_sn sn

Values within the serial number area of the tag.

u16_t lock_bytes

Value of the lock bytes.

struct nfc_t2t_cc cc

Values within the Capability Container area of the tag.

const u16_t max_tlv_blocks

Maximum number of TLV blocks that can be stored.

struct nfc_t2t_tlv_block *tlv_block_array

Pointer to the array for TLV blocks.

u16_t tlv_count

Number of TLV blocks stored in the Type 2 Tag.

Type 2 Tag TLV blocks

group nfc_t2t_tlv_block

Descriptor for a Type 2 Tag TLV block.

Defines

NFC_T2T_TLV_T_LENGTH

Length of a tag field.

NFC_T2T_TLV_L_SHORT_LENGTH

Length of a short length field.

NFC_T2T_TLV_L_LONG_LENGTH

Length of an extended length field.

NFC_T2T_TLV_L_FORMAT_FLAG

Value indicating the use of an extended length field.

NFC_T2T_TLV_NULL_TERMINATOR_LEN

Predefined length of the NULL and TERMINATOR TLV blocks.

NFC_T2T_TLV_LOCK_MEMORY_CTRL_LEN

Predefined length of the LOCK CONTROL and MEMORY CONTROL blocks.

Enums

enum nfc_t2t_tlv_block_types

Tag field values.

Possible values for the tag field in a TLV block.

Values:

NFC_T2T_TLV_NULL = 0x00

Can be used for padding of memory areas.

NFC_T2T_TLV_LOCK_CONTROL = 0x01

Defines details of the lock bits.

NFC_T2T_TLV_MEMORY_CONTROL = 0x02

Identifies reserved memory areas.

NFC_T2T_TLV_NDEF_MESSAGE = 0x03

Contains an NDEF message.

NFC_T2T_TLV_PROPRIETARY = 0xFD

Tag proprietary information.

NFC_T2T_TLV_TERMINATOR = 0xFE

Last TLV block in the data area.

struct nfc_t2t_tlv_block
#include <tlv_block.h>

TLV block descriptor.

Public Members

u8_t tag

Type of the TLV block.

u16_t length

Length of the value field.

const u8_t *value

Pointer to the value field (NULL if no value field is present in the block).