Parser for messages and records
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 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 API documentation 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 Type 2 Tag parser to read the NFC data:
int err;
uint8_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
NDEF message parser API
include/nfc/ndef/msg_parser.h
subsys/nfc/ndef/msg_parser.c
- 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 – [in] Maximum number of records to hold.
Functions
-
int nfc_ndef_msg_parse(const uint8_t *result_buf, uint32_t *result_buf_len, const uint8_t *raw_data, uint32_t *raw_data_len)
Parse NFC NDEF messages.
This function parses NDEF messages using NDEF binary record descriptors.
- Parameters
result_buf – [out] 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 – [inout] As input: size of the buffer specified by
result_buf
. As output: size of the reserved (used) part of the buffer specified byresult_buf
.raw_data – [in] Pointer to the data to be parsed.
raw_data_len – [inout] As input: size of the NFC data in the
nfc_data
buffer. As output: size of the parsed message.
- Return values
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 – [in] 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_MSG_PARSER_DELTA
NDEF record parser API
include/nfc/ndef/record_parser.h
subsys/nfc/ndef/record_parser.c
- 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 uint8_t *nfc_data, uint32_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 – [out] Pointer to the binary payload descriptor that will be filled and referenced by the record descriptor.
rec_desc – [out] Pointer to the record descriptor that will be filled with parsed data.
record_location – [out] Pointer to the record location.
nfc_data – [in] Pointer to the raw data to be parsed.
nfc_data_len – [inout] As input: size of the NFC data in the
nfc_data
buffer. As output: size of the parsed record.
- Return values
0 – If the operation was successful. Otherwise, a (negative) error code is returned.
-
void nfc_ndef_record_printout(uint32_t num, const struct nfc_ndef_record_desc *rec_desc)
Print the parsed contents of the NDEF record.
- Parameters
num – [in] Sequence number of the record within the NDEF message.
rec_desc – [in] Pointer to the descriptor of the record that should be printed.
-
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 uint8_t *nfc_data, uint32_t *nfc_data_len)