NFC: NDEF message and record parser

When using an NFC polling device, you can use the NDEF message parser module to interpret the NDEF messages the are read from the NFC tag. See the documentation for the NFC: Type 2 Tag parser for more information on how to read the content of the tag.

This library can also be used together with the NFC tag 4 type emulation library in writable mode to parse the written NDEF Message.

The library provides parsers for NDEF messages and records. The message parser module provides functionality for parsing NDEF messages and printing out their content. It then uses the NDEF record parser module for parsing NDEF records to record descriptions.

When parsing a message, you must provide memory for the message descriptor instance which holds pointers to the record descriptors. The record descriptors, in turn, point to the locations of the record fields which are located in the NFC data that was parsed. These fields are not copied.

The following code example shows how to parse an NDEF message, after you have used the NFC: Type 2 Tag parser to read the NFC data:

int  err;
u8_t desc_buf[NFC_NDEF_PARSER_REQIRED_MEMO_SIZE_CALC(MAX_NDEF_RECORDS)];
size_t desc_buf_len = sizeof(desc_buf);

err = nfc_ndef_msg_parse(desc_buf,
                         &desc_buf_len,
                         ndef_msg_buff,
                         &nfc_data_len);

if (err) {
     printk("Error during parsing an NDEF message, err: %d.\n", err);
}

nfc_ndef_msg_printout((struct nfc_ndef_msg_desc *) desc_buf);

The NFC: Tag Reader sample shows how to use the library in an application.

API documentation

NFC NDEF message parser API

group nfc_ndef_msg_parser

Parser for NFC NDEF messages and records.

Defines

NFC_NDEF_MSG_PARSER_DELTA

Amount of memory that is required per record in addition to the memory allocated for the message descriptor.

NFC_NDEF_PARSER_REQIRED_MEMO_SIZE_CALC(max_count_of_records)

Calculate the memory size required for holding the description of a message that consists of a certain number of NDEF records.

Parameters
  • max_count_of_records: Maximum number of records to hold.

Functions

int nfc_ndef_msg_parse(const u8_t *result_buf, u32_t *result_buf_len, const u8_t *raw_data, u32_t *raw_data_len)

Parse NFC NDEF messages.

This function parses NDEF messages using NDEF binary record descriptors.

Parameters
  • result_buf: Pointer to the buffer that will be used to hold the NDEF message descriptor. After parsing is completed successfully, the first address in the buffer is filled by the NDEF message descriptor (nfc_ndef_msg_desc), which provides a full description of the parsed NDEF message.
  • result_buf_len: As input: size of the buffer specified by result_buf. As output: size of the reserved (used) part of the buffer specified by result_buf.
  • raw_data: Pointer to the data to be parsed.
  • raw_data_len: As input: size of the NFC data in the nfc_data buffer. As output: size of the parsed message.
Return Value
  • 0: If the operation was successful. Otherwise, a (negative) error code is returned.

void nfc_ndef_msg_printout(const struct nfc_ndef_msg_desc *msg_desc)

Print the parsed contents of an NDEF message.

Parameters
  • msg_desc: Pointer to the descriptor of the message that should be printed.

struct nfc_ndef_msg_parser_msg_1
#include <msg_parser.h>

Memory allocated for a one-record message.

struct nfc_ndef_msg_parser_msg_2
#include <msg_parser.h>

Memory allocated for a two-record message.

NFC NDEF record parser API

group nfc_ndef_record_parser

Parser for NFC NDEF records.

Functions

int nfc_ndef_record_parse(struct nfc_ndef_bin_payload_desc *bin_pay_desc, struct nfc_ndef_record_desc *rec_desc, enum nfc_ndef_record_location *record_location, const u8_t *nfc_data, u32_t *nfc_data_len)

Parse NDEF records.

This parsing implementation uses the binary payload descriptor (nfc_ndef_bin_payload_desc) to describe the payload for the record.

Parameters
  • bin_pay_desc: Pointer to the binary payload descriptor that will be filled and referenced by the record descriptor.
  • rec_desc: Pointer to the record descriptor that will be filled with parsed data.
  • record_location: Pointer to the record location.
  • nfc_data: Pointer to the raw data to be parsed.
  • nfc_data_len: As input: size of the NFC data in the nfc_data buffer. As output: size of the parsed record.
Return Value
  • 0: If the operation was successful. Otherwise, a (negative) error code is returned.

void nfc_ndef_record_printout(u32_t num, const struct nfc_ndef_record_desc *rec_desc)

Print the parsed contents of the NDEF record.

Parameters
  • num: Sequence number of the record within the NDEF message.
  • rec_desc: Pointer to the descriptor of the record that should be printed.