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
[in] max_count_of_records
: 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
[out] 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.[inout] result_buf_len
: As input: size of the buffer specified byresult_buf
. As output: size of the reserved (used) part of the buffer specified byresult_buf
.[in] raw_data
: Pointer to the data to be parsed.[inout] raw_data_len
: As input: size of the NFC data in thenfc_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
[in] 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.
-
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
[out] bin_pay_desc
: Pointer to the binary payload descriptor that will be filled and referenced by the record descriptor.[out] rec_desc
: Pointer to the record descriptor that will be filled with parsed data.[out] record_location
: Pointer to the record location.[in] nfc_data
: Pointer to the raw data to be parsed.[inout] nfc_data_len
: As input: size of the NFC data in thenfc_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
(uint32_t num, const struct nfc_ndef_record_desc *rec_desc)¶ Print the parsed contents of the NDEF record.
- Parameters
[in] num
: Sequence number of the record within the NDEF message.[in] rec_desc
: Pointer to the descriptor of the record that should be printed.
-
int