Skip to content

Commit

Permalink
nfc: TNEP encode NDEF message directly in app data function
Browse files Browse the repository at this point in the history
This commit provides encoding the NDEF messages directly in
the app_data function. This allows to encode locally definied
NDEF rocords.

NCSDK-4934

Signed-off-by: Kamil Gawor <[email protected]>
  • Loading branch information
KAGA164 authored and anangl committed Aug 25, 2020
1 parent fde77b9 commit 7d4d4a2
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 114 deletions.
29 changes: 22 additions & 7 deletions include/nfc/tnep/tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include <nfc/tnep/base.h>
#include <kernel.h>

/** NFC TNEP library event count. */
#define NFC_TNEP_EVENTS_NUMBER 2
Expand All @@ -25,6 +26,19 @@
/** Maximum Waiting Time extension. */
#define NFC_TNEP_TAG_MAX_N_WAIT_TIME 15

/**
* @brief Macro for creating and initializing an NFC NDEF TNEP
* Application message descriptor.
*
* Use the macro @ref NFC_NDEF_MSG to access the NDEF message descriptor
* instance. This macro reserves place for the TNEP Status Record.
*
* @param _name Name of the related instance.
* @param _max_record_cnt Maximal count of records in the message.
*/
#define NFC_TNEP_TAG_APP_MSG_DEF(_name, _max_record_cnt) \
NFC_NDEF_MSG_DEF(_name, (_max_record_cnt + 1))

struct nfc_tnep_tag_service_cb {
/** @brief Function called when service was selected. */
void (*selected)(void);
Expand Down Expand Up @@ -160,8 +174,6 @@ int nfc_tnep_tag_tx_msg_buffer_register(uint8_t *tx_buff,
*
* @param[out] events TNEP Tag Events.
* @param[in] event_cnt Event count. This library needs 2 events.
* @param[in,out] msg Pointer to NDEF message structure. It is used to
* create TNEP NDEF message.
* @param[in] payload_set Function for setting NDEF data for NFC TNEP
* Tag Device. This library use it internally
* to set raw NDEF message to the Tag NDEF file.
Expand All @@ -172,7 +184,6 @@ int nfc_tnep_tag_tx_msg_buffer_register(uint8_t *tx_buff,
* Otherwise, a (negative) error code is returned.
*/
int nfc_tnep_tag_init(struct k_poll_event *events, uint8_t event_cnt,
struct nfc_ndef_msg_desc *msg,
nfc_payload_set_t payload_set);

/**
Expand Down Expand Up @@ -230,15 +241,19 @@ void nfc_tnep_tag_rx_msg_indicate(const uint8_t *rx_buffer, size_t len);
* To indicate that application has no more data use
* nfc_tnep_tag_tx_msg_no_app_data().
*
* @param[in] record Pointer to application data records.
* @param[in] cnt Records count.
* @param[in] msg Pointer to NDEF Message with application data. The Message
* must have at least one free slot for the TNEP Status Record
* which is added automatically by this function. So if you
* want to encode for example 2 NDEF Records the NDEF Message
* minimal Record count is 3. Use @ref NFC_TNEP_TAG_APP_MSG_DEF
* which reserves slot for the TNEP Status Record.
* @param[in] status TNEP App data message status.
*
* @retval 0 If the operation was successful.
* Otherwise, a (negative) error code is returned.
*/
int nfc_tnep_tag_tx_msg_app_data(const struct nfc_ndef_record_desc *record,
size_t cnt, enum nfc_tnep_status_value status);
int nfc_tnep_tag_tx_msg_app_data(struct nfc_ndef_msg_desc *msg,
enum nfc_tnep_status_value status);

/**
* @brief Respond with no more application data.
Expand Down
38 changes: 22 additions & 16 deletions samples/nfc/tnep_tag/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,34 @@
static const uint8_t msg[] = "Hello World";
static const uint8_t en_code[] = {'e', 'n'};

static const char svc_one_msg[] = "Service pi = 3.14159265358979323846";
static const char svc_two_msg[] = "Service e = 2.71828182845904523536";

static const uint8_t svc_one_uri[] = "svc:pi";
static const uint8_t svc_two_uri[] = "svc:e";

static uint8_t tag_buffer[NDEF_TNEP_MSG_SIZE];
static uint8_t tag_buffer2[NDEF_TNEP_MSG_SIZE];

NFC_NDEF_MSG_DEF(ndef_msg, 16);

NFC_NDEF_TEXT_RECORD_DESC_DEF(nfc_text, UTF_8, en_code, sizeof(en_code),
msg, sizeof(msg));
NFC_NDEF_TEXT_RECORD_DESC_DEF(svc_one_rec, UTF_8, en_code, sizeof(en_code),
svc_one_msg, sizeof(svc_one_msg));
NFC_NDEF_TEXT_RECORD_DESC_DEF(svc_two_rec, UTF_8, en_code, sizeof(en_code),
svc_two_msg, sizeof(svc_two_msg));

msg, strlen(msg));

static struct k_poll_event events[NFC_TNEP_EVENTS_NUMBER];

static void tnep_svc_one_selected(void)
{
int err;
const char svc_one_msg[] = "Service pi = 3.14159265358979323846";

printk("Service one selected\n");

err = nfc_tnep_tag_tx_msg_app_data(&NFC_NDEF_TEXT_RECORD_DESC(svc_one_rec),
1, NFC_TNEP_STATUS_SUCCESS);
NFC_TNEP_TAG_APP_MSG_DEF(app_msg, 1);
NFC_NDEF_TEXT_RECORD_DESC_DEF(svc_one_rec, UTF_8, en_code,
sizeof(en_code), svc_one_msg,
strlen(svc_one_msg));

err = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(app_msg),
&NFC_NDEF_TEXT_RECORD_DESC(svc_one_rec));

err = nfc_tnep_tag_tx_msg_app_data(&NFC_NDEF_MSG(app_msg),
NFC_TNEP_STATUS_SUCCESS);
if (err) {
printk("Service app data set err: %d\n", err);
}
Expand Down Expand Up @@ -152,11 +151,19 @@ static void button_pressed(uint32_t button_state, uint32_t has_changed)
{
int err;
uint32_t button = button_state & has_changed;
const char svc_two_msg[] = "Service e = 2.71828182845904523536";

if (button & DK_BTN1_MSK) {
err = nfc_tnep_tag_tx_msg_app_data(&NFC_NDEF_TEXT_RECORD_DESC(svc_two_rec),
1, NFC_TNEP_STATUS_SUCCESS);
NFC_TNEP_TAG_APP_MSG_DEF(app_msg, 1);
NFC_NDEF_TEXT_RECORD_DESC_DEF(svc_two_rec, UTF_8, en_code,
sizeof(en_code), svc_two_msg,
strlen(svc_two_msg));

err = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(app_msg),
&NFC_NDEF_TEXT_RECORD_DESC(svc_two_rec));

err = nfc_tnep_tag_tx_msg_app_data(&NFC_NDEF_MSG(app_msg),
NFC_TNEP_STATUS_SUCCESS);
if (err == -EACCES) {
printk("Service is not in selected state. App data cannot be set\n");
} else {
Expand Down Expand Up @@ -192,7 +199,6 @@ void main(void)
}

err = nfc_tnep_tag_init(events, ARRAY_SIZE(events),
&NFC_NDEF_MSG(ndef_msg),
nfc_t4t_ndef_rwpayload_set);
if (err) {
printk("Cannot initialize TNEP protocol, err: %d\n", err);
Expand Down
Loading

0 comments on commit 7d4d4a2

Please sign in to comment.