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

  • 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