Modifying the text_record NFC example

Hi,

I'm trying to modify the text_record example for NFC data transfer to transmit a numeric payload (working on an nrf52832). The original example transmits "Hello World!" with its language code, and I got that flashed just fine. I'm modifying the code to transmit an ID (uint8), a timestamp (YYYYMMDD) and a value (uint8).

The main problem is adding an extra two variables to the structs. I've made project-local copies of the header files "msg.h" and "record.h", and my own header file which is based on the header "text_rec.h". I've attached all my code to this ticket.

The error I'm currently getting is 

c:\nordic\attempt1\src\attempt1.h:59:7: error: expected identifier before numeric constant
59 | 0, \

And I'm not sure how to fix this, since the 0's in this structure were in the original code, but I did add two more for the new variables...

All the attached code is in the same directory, with one dependency on a header I haven't edited. 

Hope this all makes sense! Thank you for your help.

#ifndef NFC_NDEF_ATTEMPT1_H_
#define NFC_NDEF_ATTEMPT1_H_

#include <zephyr/types.h>
#include "record.h"

#ifdef __cplusplus
extern "C" {
#endif

struct nfc_ndef_text_rec_payload {
	/** Pointer to the ID. */
	uint8_t const *id;
	/** Length of the ID. */
	uint8_t id_length;
	/** Pointer to the timestamp. */
	uint8_t const *time;
	/** Length of the timestamp. */
	uint32_t time_length;
	/** Pointer to the data. */
	uint8_t const *data;
	/** Length of the data. */
	uint32_t data_length;
};

int nfc_ndef_text_rec_payload_encode(
		struct nfc_ndef_text_rec_payload *nfc_rec_text_payload_desc,
		uint8_t *buff,
		uint32_t *len);

extern const uint8_t nfc_ndef_text_rec_type_field[];

#define NFC_NDEF_TEXT_REC_TYPE_LENGTH 1

#define NFC_NDEF_TEXT_RECORD_DESC_DEF(name,				      \
				      id_arg,			      \
				      id_len_arg,			      \
				      time_arg,				      \
				      time_len_arg,				      \
				      data_arg,				      \
				      data_len_arg)			      \
	struct nfc_ndef_text_rec_payload name##_nfc_ndef_text_rec_payload =   \
	{								      \
		.id_length = id_len_arg,					    \
		.id = id_arg,						    \
		.time_length = time_len_arg,					    \
		.time = time_arg,						    \
		.data_length = data_len_arg,				    \
		.data = data_arg,				      \
	};								      \
	NFC_NDEF_GENERIC_RECORD_DESC_DEF(name,				      \
					 0,				      \
					 0,				      \
					 0,				      \
					 0,				      \
					 nfc_ndef_text_rec_type_field,	      \
					 NFC_NDEF_TEXT_REC_TYPE_LENGTH,	      \
					 nfc_ndef_text_rec_payload_encode,    \
					 &(name##_nfc_ndef_text_rec_payload))

#define NFC_NDEF_TEXT_RECORD_DESC(name) NFC_NDEF_GENERIC_RECORD_DESC(name)

#ifdef __cplusplus
}
#endif

#endif /* NFC_NDEF_ATTEMPT1_H_ */
// main.c

#include <zephyr.h>
// #include <sys/reboot.h'>
#include <dk_buttons_and_leds.h>
#include <nfc_t2t_lib.h>

#include "msg.h"
#include "attempt1.h"

#define NFC_FIELD_LED		DK_LED1
#define NDEF_BUF_SIZE 128
#define MAX_REC_COUNT 1

//strain reading
static const uint32_t strainreading = 289319;
static const uint8_t timestamp[] = {'2', '0', '2', '2', '0', '4', '2', '7'};
static const uint32_t identifier = 1;

//buffer to hold NFC NDEF
static uint8_t ndef_buf[NDEF_BUF_SIZE];

//callback function
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;
	}
}

//encoding the NDEF message
static int encode(uint8_t *buffer, uint32_t *len)
{
    int err;

    NFC_NDEF_TEXT_RECORD_DESC_DEF(nfc_payload_1,
                    identifier,
                    sizeof(identifier),
                    timestamp,
                    sizeof(timestamp),
                    strainreading,
                    sizeof(strainreading));
    
    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_payload_1));
	if (err < 0) {
		printk("Cannot add first record!\n");
		return err;
	}

    return err;
}

int main(void)
{
    uint32_t len = sizeof(ndef_buf);

	printk("Attempt 1\n");

	/* Configure LED-pins as outputs */
	if (dk_leds_init() < 0) {
		printk("Cannot init LEDs!\n");
	}

	/* Set up NFC */
	if (nfc_t2t_setup(nfc_callback, NULL) < 0) {
		printk("Cannot setup NFC T2T library!\n");
	}

    //encode
    /* Encode welcome message */
	if (encode(ndef_buf, &len) < 0) {
		printk("Cannot encode message!\n");
	}

    	/* Set created message as the NFC payload */
	if (nfc_t2t_payload_set(ndef_buf, len) < 0) {
		printk("Cannot set payload!\n");
	}

	/* Start sensing NFC field */
	if (nfc_t2t_emulation_start() < 0) {
		printk("Cannot start emulation!\n");
	}

	printk("NFC configuration done\n");

	return 0;

}
#ifndef NFC_NDEF_RECORD_H_
#define NFC_NDEF_RECORD_H_

#include <zephyr/types.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef int (*payload_constructor_t)(void *payload_descriptor,
				     uint8_t *buffer,
				     uint32_t *len);

struct nfc_ndef_record_desc {
	uint8_t id_length;
	uint8_t const *id;
	uint8_t time_length;
	uint8_t const *time;
	uint8_t type_length;
	uint8_t const *type;
	payload_constructor_t payload_constructor;
	void *payload_descriptor;
};                     

enum nfc_ndef_record_location {
	NDEF_FIRST_RECORD  = 0x80, /**< First record. */
	NDEF_MIDDLE_RECORD = 0x00, /**< Middle record. */
	NDEF_LAST_RECORD   = 0x100, /**< Last record. */
	NDEF_LONE_RECORD   = 0x180  /**< Only one record in the message. */
};

#define NDEF_RECORD_LOCATION_MASK (NDEF_LONE_RECORD)

struct nfc_ndef_bin_payload_desc {
	uint8_t const *payload;  /**< Pointer to the buffer with the data. */
	uint32_t payload_length; /**< Length of data in bytes. */
};

#define NFC_NDEF_GENERIC_RECORD_DESC_DEF(name, \
                id_arg,\
                id_length,\
                time_arg,\
                time_length,\
                type_arg,\
                type_length, \
                payload_constructor_arg,\
                payload_descriptor_arg) \
    struct nfc_ndef_record_desc name##_ndef_generic_record_desc =	    \
	{								    \
		.id_length = id_length,					    \
		.id = id_arg,						    \
		.time_length = time_length,					    \
		.time = time_arg,						    \
		.type_length = type_length,				    \
		.type = type_arg,					    \
		.payload_constructor  =					    \
			(payload_constructor_t)payload_constructor_arg,	    \
		.payload_descriptor = (void *) payload_descriptor_arg	    \
	}

#define NFC_NDEF_GENERIC_RECORD_DESC(name) (name##_ndef_generic_record_desc)

#define NFC_NDEF_RECORD_BIN_DATA_DEF(name,				    \
				     id_arg,				    \
				     id_len,				    \
                     time_arg,\
                     time_len,\
				     type_arg,				    \
				     type_len,				    \
				     payload_arg,			    \
				     payload_len)			    \
	struct nfc_ndef_bin_payload_desc name##_nfc_ndef_bin_payload_desc = \
	{								    \
		.payload  = payload_arg,				    \
		.payload_length = payload_len				    \
	};								    \
									    \
	struct nfc_ndef_record_desc name##_nfc_ndef_bin_record_desc =	    \
	{								    			\
		.id_length = id_len,					    \
		.id = id_arg,								\
		.time_length = time_len,				 	\
		.time = time_arg,							\		    
		.type_length = type_len,				    \
		.type = type_arg,					    	\
		.payload_constructor  =					    \
		    (payload_constructor_t) nfc_ndef_bin_payload_memcopy,   \
		.payload_descriptor =					    \
		    (void *) &name##_nfc_ndef_bin_payload_desc		    \
	}

#define NFC_NDEF_RECORD_BIN_DATA(name) (name##_nfc_ndef_bin_record_desc)

#define NFC_NDEF_BIN_PAYLOAD_DESC(name) (name##_nfc_ndef_bin_payload_desc)

int nfc_ndef_record_encode(struct nfc_ndef_record_desc const *ndef_record_desc,
			   enum nfc_ndef_record_location const record_location,
			   uint8_t *record_buffer,
			   uint32_t *record_len);

int nfc_ndef_bin_payload_memcopy(
			struct nfc_ndef_bin_payload_desc *payload_descriptor,
			uint8_t *buffer,
			uint32_t *len);

#ifdef __cplusplus
}
#endif

#endif /* NFC_NDEF_RECORD_H_ */
#ifndef NFC_NDEF_MSG_H_
#define NFC_NDEF_MSG_H_

#include <zephyr/types.h>
#include "record.h"

#ifdef __cplusplus
extern "C" {
#endif

struct nfc_ndef_msg_desc {
	/** Pointer to an array of pointers to NDEF record descriptors. */
	struct nfc_ndef_record_desc const **record;
	/** Number of elements in the allocated record array, which defines
	 *  the maximum number of records within the NDEF message.
	 */
	uint32_t max_record_count;
	/** Number of records in the NDEF message. */
	uint32_t record_count;
};

int nfc_ndef_msg_encode(struct nfc_ndef_msg_desc const *ndef_msg_desc,
			uint8_t *msg_buffer,
			uint32_t *msg_len);

void nfc_ndef_msg_clear(struct nfc_ndef_msg_desc *msg);

int nfc_ndef_msg_record_add(struct nfc_ndef_msg_desc *msg,
			    struct nfc_ndef_record_desc const *record);


#define NFC_NDEF_MSG_DEF(name, max_record_cnt)				    \
	struct nfc_ndef_record_desc const				    \
		*name##_nfc_ndef_record_desc_array[max_record_cnt];	    \
	struct nfc_ndef_msg_desc name##_nfc_ndef_msg_desc =		    \
	{								    \
		.record = name##_nfc_ndef_record_desc_array,		    \
		.max_record_count = max_record_cnt,			    \
		.record_count = 0					    \
	}


#define NFC_NDEF_MSG(name) (name##_nfc_ndef_msg_desc)


#define NFC_NDEF_NESTED_NDEF_MSG_RECORD_DEF(name,			\
					    id_arg,			\
					    id_len,			\
					    time_arg,			\
					    time_len,			\
					    type_arg,			\
					    type_len,			\
					    nested_message)		\
	struct nfc_ndef_record_desc name##_ndef_record_nested_desc =	\
	{								\
		.id_length = id_len,					\
		.id =  id_arg,						\
		.time_length = time_len,					\
		.time =  time_arg,						\
		.type_length = type_len,				\
		.type = type_arg,					\
		.payload_constructor =					\
			(payload_constructor_t)(nfc_ndef_msg_encode),	\
		.payload_descriptor = (void *) (nested_message)		\
	}


#define NFC_NDEF_NESTED_NDEF_MSG_RECORD(name) (name##_ndef_record_nested_desc)


#ifdef __cplusplus
}
#endif

#endif /* NFC_NDEF_MSG_H_*/

Related