NFC ndef records doesn't work with t4t

I am trying to make an ndef record using t4t, but it fails.

For example I'm trying to convert the example 'record_text' to work with t4t setup, and it always fail.

Is it even possible for t4t to work with ndef records ?.

My code : main.c

/*
 * Copyright (c) 2021 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

#include <zephyr/kernel.h>
#include <zephyr/sys/reboot.h>

#include <nfc_t2t_lib.h>
#include <nfc/ndef/msg.h>
#include <nfc_t4t_lib.h>
#include <nfc/ndef/text_rec.h>
#include <nfc/ndef/launchapp_rec.h>
// #include <nfc/ndef/launchapp_msg.h>
#include <nfc/t4t/ndef_file.h>
#include <dk_buttons_and_leds.h>

#define NDEF_MSG_BUF_SIZE	256
#define NFC_FIELD_LED		DK_LED1

#define MAX_REC_COUNT		3

/** .. include_startingpoint_pkg_def_launchapp_rst */
/* Package: no.nordicsemi.android.nrftoolbox */
/* Text message in English with its language code. */
static const uint8_t en_payload[] = {
	'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'
};
static const uint8_t en_code[] = {'e', 'n'};

/* Text message in Norwegian with its language code. */
static const uint8_t no_payload[] = {
	'H', 'a', 'l', 'l', 'o', ' ', 'V', 'e', 'r', 'd', 'e', 'n', '!'
};
static const uint8_t no_code[] = {'N', 'O'};

/* Text message in Polish with its language code. */
static const uint8_t pl_payload[] = {
	'W', 'i', 't', 'a', 'j', ' ', 0xc5, 0x9a, 'w', 'i', 'e', 'c', 'i',
	'e', '!'
};
static const uint8_t pl_code[] = {'P', 'L'};

static const uint8_t android_pkg_name[] = {
	'n', 'o', '.', 'n', 'o', 'r', 'd', 'i', 'c', 's', 'e', 'm', 'i', '.', 'a', 'n', 'd', 'r',
	'o', 'i', 'd', '.', 'n', 'r', 'f', 't', 'o', 'o', 'l', 'b', 'o', 'x' };

/* Buffer used to hold an NFC NDEF message. */
static uint8_t ndef_msg_buf[NDEF_MSG_BUF_SIZE];

void print_d(uint8_t* buf, uint8_t size){
	int i = 0;
	for(i = 0; i < size; i++){
		printf("%x", buf[i]);
	}
	printf("\n");
	for(i = 0; i < size; i++){
		printf("%c", buf[i]);
	}
	printf("\n");
}


static void nfc_callback(void *context,
			 nfc_t4t_event_t event,
			 const uint8_t *data,
			 size_t data_length,
			 uint32_t flags)
{
	ARG_UNUSED(context);
	ARG_UNUSED(data);
	ARG_UNUSED(data_length);

	switch (event) {
	case NFC_T4T_EVENT_FIELD_ON:
		dk_set_led_on(NFC_FIELD_LED);
		break;
	case NFC_T4T_EVENT_FIELD_OFF:
		dk_set_led_off(NFC_FIELD_LED);
		break;
	default:
		break;
	}
}

size_t len = sizeof(ndef_msg_buf);


static int welcome_msg_encode(uint8_t *buffer, uint32_t *len)
{
	int err;

	/* Create NFC NDEF text record description in English */
	NFC_NDEF_TEXT_RECORD_DESC_DEF(nfc_en_text_rec,
				      UTF_8,
				      en_code,
				      sizeof(en_code),
				      en_payload,
				      sizeof(en_payload));

	/* Create NFC NDEF text record description in Norwegian */
	NFC_NDEF_TEXT_RECORD_DESC_DEF(nfc_no_text_rec,
				      UTF_8,
				      no_code,
				      sizeof(no_code),
				      no_payload,
				      sizeof(no_payload));

	/* Create NFC NDEF text record description in Polish */
	NFC_NDEF_TEXT_RECORD_DESC_DEF(nfc_pl_text_rec,
				      UTF_8,
				      pl_code,
				      sizeof(pl_code),
				      pl_payload,
				      sizeof(pl_payload));
	NFC_NDEF_MSG_DEF(nfc_text_msg, MAX_REC_COUNT);

	/* Add text records to NDEF text message */
	err = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(nfc_text_msg),
				   &NFC_NDEF_TEXT_RECORD_DESC(nfc_en_text_rec));
	if (err < 0) {
		printk("Cannot add first record!\n");
		return err;
	}
	err = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(nfc_text_msg),
				   &NFC_NDEF_TEXT_RECORD_DESC(nfc_no_text_rec));
	if (err < 0) {
		printk("Cannot add second record!\n");
		return err;
	}
	err = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(nfc_text_msg),
				   &NFC_NDEF_TEXT_RECORD_DESC(nfc_pl_text_rec));
	if (err < 0) {
		printk("Cannot add third record!\n");
		return err;
	}

	err = nfc_ndef_msg_encode(&NFC_NDEF_MSG(nfc_text_msg),
				      buffer,
				      len);
	if (err < 0) {
		printk("Cannot encode message!\n");
	}

	return err;
}


static int android_msg(uint8_t* buffer, uint8_t* len){
	int err;
	NFC_NDEF_MSG_DEF(my_msg, 5);

	NFC_NDEF_ANDROID_LAUNCHAPP_RECORD_DESC_DEF(first_rec, android_pkg_name, sizeof(android_pkg_name));

	if (err < 0) {
		printk("Cannot add first record!\n");
		return err;
	}
	err = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(my_msg),
				   &NFC_NDEF_ANDROID_LAUNCHAPP_RECORD_DESC(first_rec));
	if (err < 0) {
		printk("Cannot add second record!\n");
		return err;
	}

	err = nfc_ndef_msg_encode(&NFC_NDEF_MSG(my_msg), buffer, len);
	if (err) {
		printk("Cannot set payload!\n");
	}

	return 0;
}


int main(void)
{
	int err;

	printk("Starting NFC Launch app example\n");


	err = nfc_t4t_setup(nfc_callback, NULL);
	if (err) {
		printk("Cannot setup NFC T4T library!\n");
		goto fail;
	}

	if (err) {
		printk("Cannot encode message!\n");
		goto fail;
	}
	/* Set created message as the NFC payload */


	welcome_msg_encode(ndef_msg_buf, &len);

	err = nfc_t4t_ndef_rwpayload_set(ndef_msg_buf, len);
	print_d(ndef_msg_buf, len);
	if (err) {
		printk("Cannot set payload!\n");
		goto fail;
	}

	/* Start sensing NFC field */
	err = nfc_t4t_emulation_start();
	if (err) {
		printk("Cannot start emulation!\n");
		goto fail;
	}

	printk("NFC configuration done\n");
	return 0;

fail:
#if CONFIG_REBOOT
	sys_reboot(SYS_REBOOT_COLD);
#endif /* CONFIG_REBOOT */

	return err;
}

prj.conf : 

#
# Copyright (c) 2021 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
CONFIG_NCS_SAMPLES_DEFAULTS=y

CONFIG_REBOOT=y
CONFIG_DK_LIBRARY=y

CONFIG_NFC_T4T_NRFXLIB=y

CONFIG_NFC_NDEF=y
CONFIG_NFC_NDEF_MSG=y
CONFIG_NFC_NDEF_RECORD=y
CONFIG_NFC_NDEF_URI_REC=y
CONFIG_NFC_NDEF_LAUNCHAPP_REC=y
CONFIG_NFC_NDEF_LAUNCHAPP_MSG=y
CONFIG_NFC_NDEF_TEXT_RECORD=y

Parents
  • Hi,

    Could you specify how it fails? Does it build successfully?

    regards

    Jared 

  • I am sorry for the late answer.

    Yes, it build successfully.

    But there is something wrong in the message I guess, I don't know what is it.

    The message is "811000f542656e48656c6c6f20576f726c642111000105424e4f48616c6c6f2056657264656e2141100012542504c576974616a20c59a77696563696521
    �TenHello World!TNOHallo Verden!ATPLWitaj Świecie!"

    When I scan it with nRF NFC Toolbox it shows 'scanning failed'

  • Hi,

    Looks like the log messages are getting scrambled.

    Are you using UART as backend for the logs, have you double checked that you are using correct baudrate? 

    Are you using the devkit?

    regards

    Jared 

  • Yes, I'm using nrf52840DK

    I've also tried another sample (record_launch_app) to make it to work with t4t

    /*
     * Copyright (c) 2021 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
     */
    
    #include <zephyr/kernel.h>
    #include <zephyr/sys/reboot.h>
    
    // #include <nfc_t2t_lib.h>
    #include <nfc/ndef/launchapp_msg.h>
    #include <nfc_t4t_lib.h>
    
    #include <nfc/t4t/ndef_file.h>
    
    #include <dk_buttons_and_leds.h>
    
    #define NDEF_MSG_BUF_SIZE	256
    #define NFC_FIELD_LED		DK_LED1
    
    #define NFC_WRITE_LED		DK_LED2
    #define NFC_READ_LED		DK_LED4
    
    
    /** .. include_startingpoint_pkg_def_launchapp_rst */
    /* Package: no.nordicsemi.android.nrftoolbox */
    static const uint8_t android_pkg_name[] = {
    	'n', 'o', '.', 'n', 'o', 'r', 'd', 'i', 'c', 's', 'e', 'm', 'i', '.', 'a', 'n', 'd', 'r',
    	'o', 'i', 'd', '.', 'n', 'r', 'f', 't', 'o', 'o', 'l', 'b', 'o', 'x' };
    
    /* URI nrf-toolbox://main/ */
    static const uint8_t universal_link[] = {
    	'n', 'r', 'f', '-', 't', 'o', 'o', 'l', 'b', 'o', 'x', ':', '/', '/', 'm', 'a', 'i', 'n',
    	'/'};
    /** .. include_endpoint_pkg_def_launchapp_rst */
    
    /* Buffer used to hold an NFC NDEF message. */
    static uint8_t ndef_msg_buf[NDEF_MSG_BUF_SIZE];
    
    
    // static void nfc_callback(void *context,
    // 			 nfc_t2t_event_t event,
    // 			 const uint8_t *data,
    // 			 size_t data_length)
    // {
    // 	ARG_UNUSED(context);
    // 	ARG_UNUSED(data);
    // 	ARG_UNUSED(data_length);
    
    // 	switch (event) {
    // 	case NFC_T2T_EVENT_FIELD_ON:
    // 		dk_set_led_on(NFC_FIELD_LED);
    // 		break;
    // 	case NFC_T2T_EVENT_FIELD_OFF:
    // 		dk_set_led_off(NFC_FIELD_LED);
    // 		break;
    // 	default:
    // 		break;
    // 	}
    // }
    
    static void nfc_callback(void *context,
    			 nfc_t4t_event_t event,
    			 const uint8_t *data,
    			 size_t data_length,
    			 uint32_t flags)
    {
    	ARG_UNUSED(context);
    	ARG_UNUSED(data);
    	ARG_UNUSED(flags);
    
    	switch (event) {
    	case NFC_T4T_EVENT_FIELD_ON:
    		dk_set_led_on(NFC_FIELD_LED);
    		break;
    
    	case NFC_T4T_EVENT_FIELD_OFF:
    		dk_set_leds(DK_NO_LEDS_MSK);
    		break;
    
    	case NFC_T4T_EVENT_NDEF_READ:
    		dk_set_led_on(NFC_READ_LED);
    		break;
    
    	case NFC_T4T_EVENT_NDEF_UPDATED:
    		if (data_length > 0) {
    			dk_set_led_on(NFC_WRITE_LED);
    			printk("data is written \r\n");
    		}
    		break;
    
    	default:
    		break;
    	}
    }
    
    
    int main(void)
    {
    	int err;
    	size_t len = sizeof(ndef_msg_buf);
    
    	printk("Starting NFC Launch app example\n");
    
    	/* Configure LED-pins as outputs */
    	err = dk_leds_init();
    	if (err) {
    		printk("Cannot init LEDs!\n");
    		goto fail;
    	}
    
    	/* Set up NFC */
    	err = nfc_t4t_setup(nfc_callback, NULL);
    	if (err) {
    		printk("Cannot setup NFC T2T library!\n");
    		goto fail;
    	}
    
    	/* Encode launch app data  */
    	err = nfc_launchapp_msg_encode(android_pkg_name,
    				       sizeof(android_pkg_name),
    				       universal_link,
    				       sizeof(universal_link),
    				       ndef_msg_buf,
    				       &len);
    	if (err) {
    		printk("Cannot encode message!\n");
    		goto fail;
    	}
    
    	/* Set created message as the NFC payload */
    	err = nfc_t4t_ndef_rwpayload_set(ndef_msg_buf, len);
    	if (err) {
    		printk("Cannot set payload!\n");
    		goto fail;
    	}
    
    	/* Start sensing NFC field */
    	err = nfc_t4t_emulation_start();
    	if (err) {
    		printk("Cannot start emulation!\n");
    		goto fail;
    	}
    
    	printk("NFC configuration done\n");
    	return 0;
    
    fail:
    #if CONFIG_REBOOT
    	sys_reboot(SYS_REBOOT_COLD);
    #endif /* CONFIG_REBOOT */
    
    	return err;
    }
    

    But it shows a message on the mobile ''Communication with the NFC tag was interrupted. scan result may be incompelete or inaccurate. Please hold the device closer to the tag"

    And it work well with the original record_launch_app version

Reply
  • Yes, I'm using nrf52840DK

    I've also tried another sample (record_launch_app) to make it to work with t4t

    /*
     * Copyright (c) 2021 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
     */
    
    #include <zephyr/kernel.h>
    #include <zephyr/sys/reboot.h>
    
    // #include <nfc_t2t_lib.h>
    #include <nfc/ndef/launchapp_msg.h>
    #include <nfc_t4t_lib.h>
    
    #include <nfc/t4t/ndef_file.h>
    
    #include <dk_buttons_and_leds.h>
    
    #define NDEF_MSG_BUF_SIZE	256
    #define NFC_FIELD_LED		DK_LED1
    
    #define NFC_WRITE_LED		DK_LED2
    #define NFC_READ_LED		DK_LED4
    
    
    /** .. include_startingpoint_pkg_def_launchapp_rst */
    /* Package: no.nordicsemi.android.nrftoolbox */
    static const uint8_t android_pkg_name[] = {
    	'n', 'o', '.', 'n', 'o', 'r', 'd', 'i', 'c', 's', 'e', 'm', 'i', '.', 'a', 'n', 'd', 'r',
    	'o', 'i', 'd', '.', 'n', 'r', 'f', 't', 'o', 'o', 'l', 'b', 'o', 'x' };
    
    /* URI nrf-toolbox://main/ */
    static const uint8_t universal_link[] = {
    	'n', 'r', 'f', '-', 't', 'o', 'o', 'l', 'b', 'o', 'x', ':', '/', '/', 'm', 'a', 'i', 'n',
    	'/'};
    /** .. include_endpoint_pkg_def_launchapp_rst */
    
    /* Buffer used to hold an NFC NDEF message. */
    static uint8_t ndef_msg_buf[NDEF_MSG_BUF_SIZE];
    
    
    // static void nfc_callback(void *context,
    // 			 nfc_t2t_event_t event,
    // 			 const uint8_t *data,
    // 			 size_t data_length)
    // {
    // 	ARG_UNUSED(context);
    // 	ARG_UNUSED(data);
    // 	ARG_UNUSED(data_length);
    
    // 	switch (event) {
    // 	case NFC_T2T_EVENT_FIELD_ON:
    // 		dk_set_led_on(NFC_FIELD_LED);
    // 		break;
    // 	case NFC_T2T_EVENT_FIELD_OFF:
    // 		dk_set_led_off(NFC_FIELD_LED);
    // 		break;
    // 	default:
    // 		break;
    // 	}
    // }
    
    static void nfc_callback(void *context,
    			 nfc_t4t_event_t event,
    			 const uint8_t *data,
    			 size_t data_length,
    			 uint32_t flags)
    {
    	ARG_UNUSED(context);
    	ARG_UNUSED(data);
    	ARG_UNUSED(flags);
    
    	switch (event) {
    	case NFC_T4T_EVENT_FIELD_ON:
    		dk_set_led_on(NFC_FIELD_LED);
    		break;
    
    	case NFC_T4T_EVENT_FIELD_OFF:
    		dk_set_leds(DK_NO_LEDS_MSK);
    		break;
    
    	case NFC_T4T_EVENT_NDEF_READ:
    		dk_set_led_on(NFC_READ_LED);
    		break;
    
    	case NFC_T4T_EVENT_NDEF_UPDATED:
    		if (data_length > 0) {
    			dk_set_led_on(NFC_WRITE_LED);
    			printk("data is written \r\n");
    		}
    		break;
    
    	default:
    		break;
    	}
    }
    
    
    int main(void)
    {
    	int err;
    	size_t len = sizeof(ndef_msg_buf);
    
    	printk("Starting NFC Launch app example\n");
    
    	/* Configure LED-pins as outputs */
    	err = dk_leds_init();
    	if (err) {
    		printk("Cannot init LEDs!\n");
    		goto fail;
    	}
    
    	/* Set up NFC */
    	err = nfc_t4t_setup(nfc_callback, NULL);
    	if (err) {
    		printk("Cannot setup NFC T2T library!\n");
    		goto fail;
    	}
    
    	/* Encode launch app data  */
    	err = nfc_launchapp_msg_encode(android_pkg_name,
    				       sizeof(android_pkg_name),
    				       universal_link,
    				       sizeof(universal_link),
    				       ndef_msg_buf,
    				       &len);
    	if (err) {
    		printk("Cannot encode message!\n");
    		goto fail;
    	}
    
    	/* Set created message as the NFC payload */
    	err = nfc_t4t_ndef_rwpayload_set(ndef_msg_buf, len);
    	if (err) {
    		printk("Cannot set payload!\n");
    		goto fail;
    	}
    
    	/* Start sensing NFC field */
    	err = nfc_t4t_emulation_start();
    	if (err) {
    		printk("Cannot start emulation!\n");
    		goto fail;
    	}
    
    	printk("NFC configuration done\n");
    	return 0;
    
    fail:
    #if CONFIG_REBOOT
    	sys_reboot(SYS_REBOOT_COLD);
    #endif /* CONFIG_REBOOT */
    
    	return err;
    }
    

    But it shows a message on the mobile ''Communication with the NFC tag was interrupted. scan result may be incompelete or inaccurate. Please hold the device closer to the tag"

    And it work well with the original record_launch_app version

Children
  • Hi, 

    Can you build the project in debug mode, enable logging and see what the log output says when you get that message on the phone?

    regards

    Jared 

  • I updated the code to be

     

    static void nfc_callback(void *context,
    			 nfc_t4t_event_t event,
    			 const uint8_t *data,
    			 size_t data_length,
    			 uint32_t flags)
    {
    	ARG_UNUSED(context);
    	ARG_UNUSED(data);
    	ARG_UNUSED(flags);
    
    	switch (event) {
    	case NFC_T4T_EVENT_FIELD_ON:
    		dk_set_led_on(NFC_FIELD_LED);
    		printf("data is read %s \r\n", data);
    		printf("data is read %d \r\n", data_length);
    		break;
    
    	case NFC_T4T_EVENT_FIELD_OFF:
    		dk_set_leds(DK_NO_LEDS_MSK);
    		printf("data is read %s \r\n", data);
    		printf("data is read %d \r\n", data_length);
    		break;
    
    	case NFC_T4T_EVENT_NDEF_READ:
    		dk_set_led_on(NFC_READ_LED);
    		printf("data is read %s \r\n", data);
    		printf("data is read %d \r\n", data_length);
    		break;
    
    	case NFC_T4T_EVENT_NDEF_UPDATED:
    		if (data_length > 0) {
    			dk_set_led_on(NFC_WRITE_LED);
    			printf("NFC_T4T_EVENT_NDEF_UPDATED is written %s\r\n", data);
    			printf("NFC_T4T_EVENT_NDEF_UPDATED is written %d\r\n", data_length);
    		}
    		break;
    	case NFC_T4T_EVENT_DATA_TRANSMITTED:
    		printf("NFC_T4T_EVENT_DATA_TRANSMITTED is read %s\r\n", data);
    		printf("NFC_T4T_EVENT_DATA_TRANSMITTED is read %d\r\n", data_length);
    		break;
    
    	default:
    		break;
    	}
    }

    And the logs I got only happens in two cases. :

    case NFC_T4T_EVENT_FIELD_OFF:

    case NFC_T4T_EVENT_FIELD_ON:

    And it only prints 

    data is read (null) 
    data is read 0 
    data is read (null) 
    data is read 0 
    data is read (null) 
    data is read 0 
    data is read (null) 
    data is read 0 

    It doesn't reach any other case.

  • Hi,

    Looking at the writeable sample, I see that the code enters this line during a NFC_T4T_EVENT_NDEF_READ event:

    Can you build your code in debug mode, and set a breakpoint there and see if the code enters this part in the nrfx_nrfct.c driver when you expose the tag to a field?

    regards

    Jared

Related