Skip to content

Commit 600af4b

Browse files
committed
nfc: separate TNEP signalling functionality for NFC tag
The NFC TNEP signalling functionality in the nRF Connect SDK is based on Zephyr kernel primitives. In the Bare Metal the Zephyr primitives are not available. The Bare Metal will need a separate implementation based on Bare Metal primitives. To allow that the TNEP signalling is extracted from the subsys/nfc/tnep/tag.c into separate files. The include/nfc/tnep/tag_signalling.h is the header that defines the signalling API that is used by subsys/nfc/tnep/tag.c implementation. The subsys/nfc/tnep/tag.c is now signalling-agnostic and uses the signalling API defined in include/nfc/tnep/tag_signalling.h. The subsys/nfc/tnep/tag_signalling_zephyr.c contains the Zephyr-based implementation of the signalling API and the Zephyr-specific initialization function. For other non Zephyr-based platforms separate signalling implementation files may be created. Because the way of handling signalling is platform-specific, the `nfc_tnep_tag_init()` API is changed to not require `struct k_poll_event *events, uint8_t event_cnt` parameters. Instead, the platform-specific signalling initialization must be called before calling `nfc_tnep_tag_init()`. The Zephyr-based signalling initialization function is named `nfc_tnep_tag_signalling_init()`, its prototype is declared in include/nfc/tnep/tag_signalling_zephyr.h, and it is implemented in subsys/nfc/tnep/tag_signalling_zephyr.c. To allow selection of applicable signalling implementation the Kconfig choice `NFC_TNEP_TAG_SIGNALLING` is added. In the nRF Connect SDK it has only one choice: `NFC_TNEP_TAG_SIGNALLING_ZEPHYR`. The Bare Metal will extend the choice and provide separate signalling implementation. The applications based on nRF Connect SDK using the NFC TNEP tag functionality must be updated in the following way: - include the signalling header `#include <nfc/tnep/tag_signalling_zephyr.h>` - call the signalling initialization function `nfc_tnep_tag_signalling_init()` before calling `nfc_tnep_tag_init()` The `events` and `event_cnt` parameters that were previously passed to `nfc_tnep_tag_init()` now are to be passed to `nfc_tnep_tag_signalling_init` function. Signed-off-by: Andrzej Kuros <[email protected]>
1 parent d6c88a7 commit 600af4b

File tree

7 files changed

+187
-62
lines changed

7 files changed

+187
-62
lines changed

include/nfc/tnep/tag.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
#include <nfc/tnep/base.h>
1818
#include <zephyr/kernel.h>
1919

20-
/** NFC TNEP library event count. */
21-
#define NFC_TNEP_EVENTS_NUMBER 2
22-
2320
/** Maximum Service Waiting Time. */
2421
#define NFC_TNEP_TAG_MAX_WAIT_TIME 63
2522

@@ -188,8 +185,8 @@ int nfc_tnep_tag_tx_msg_buffer_register(uint8_t *tx_buff,
188185
/**
189186
* @brief Start communication using TNEP.
190187
*
191-
* @param[out] events TNEP Tag Events.
192-
* @param[in] event_cnt Event count. This library needs 2 events.
188+
* @note This function must be called after @ref nfc_tnep_tag_signalling_init.
189+
*
193190
* @param[in] payload_set Function for setting NDEF data for NFC TNEP
194191
* Tag Device. This library use it internally
195192
* to set raw NDEF message to the Tag NDEF file.
@@ -199,8 +196,7 @@ int nfc_tnep_tag_tx_msg_buffer_register(uint8_t *tx_buff,
199196
* @retval 0 If the operation was successful.
200197
* Otherwise, a (negative) error code is returned.
201198
*/
202-
int nfc_tnep_tag_init(struct k_poll_event *events, uint8_t event_cnt,
203-
nfc_payload_set_t payload_set);
199+
int nfc_tnep_tag_init(nfc_payload_set_t payload_set);
204200

205201
/**
206202
* @brief Create the Initial TNEP NDEF message.

include/nfc/tnep/tag_signalling.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2026 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#ifndef NFC_TNEP_TAG_SIGNALLING_H_
8+
#define NFC_TNEP_TAG_SIGNALLING_H_
9+
10+
#include <stdbool.h>
11+
12+
enum tnep_event {
13+
TNEP_EVENT_DUMMY,
14+
TNEP_EVENT_MSG_RX_NEW,
15+
TNEP_EVENT_TAG_SELECTED,
16+
};
17+
18+
/** @brief Raises a receive event.
19+
*
20+
* @param event Event to be raised.
21+
*/
22+
void nfc_tnep_tag_signalling_rx_event_raise(enum tnep_event event);
23+
24+
/** @brief Raises a transmit event.
25+
*
26+
* @param event Event to be raised.
27+
*/
28+
void nfc_tnep_tag_signalling_tx_event_raise(enum tnep_event event);
29+
30+
/** @brief Checks and clears receive event.
31+
*
32+
* @param event Pointer to store the raised event.
33+
* The value is valid only if the function returns @c true.
34+
*
35+
* @retval true if a receive event was raised.
36+
* @retval false if no receive event was raised.
37+
*/
38+
bool nfc_tnep_tag_signalling_rx_event_check_and_clear(enum tnep_event *event);
39+
40+
/** @brief Checks and clears transmit event.
41+
*
42+
* @param event Pointer to store the raised event.
43+
* The value is valid only if the function returns @c true.
44+
*
45+
* @retval true if a transmit event was raised.
46+
* @retval false if no transmit event was raised.
47+
*/
48+
bool nfc_tnep_tag_signalling_tx_event_check_and_clear(enum tnep_event *event);
49+
50+
#endif /* NFC_TNEP_TAG_SIGNALLING_H_ */
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2026 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#ifndef NFC_TNEP_TAG_SIGNALLING_ZEPHYR_H_
8+
#define NFC_TNEP_TAG_SIGNALLING_ZEPHYR_H_
9+
10+
#include <stdint.h>
11+
#include <zephyr/kernel.h>
12+
13+
/** NFC TNEP library event count. */
14+
#define NFC_TNEP_EVENTS_NUMBER 2
15+
16+
/** @brief Initializes TNEP signalling using Zephyr primitives.
17+
*
18+
* @param[out] events Pointer to array of k_poll_event structures.
19+
* @param[in] event_cnt Number of events in the array.
20+
* Must be @ref NFC_TNEP_EVENTS_NUMBER.
21+
*
22+
* @retval 0 If the operation was successful.
23+
* Otherwise, an (negative) error code is returned.
24+
*/
25+
int nfc_tnep_tag_signalling_init(struct k_poll_event *events, uint8_t event_cnt);
26+
27+
#endif /* NFC_TNEP_TAG_SIGNALLING_ZEPHYR_H_ */

subsys/nfc/tnep/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
zephyr_library()
88
zephyr_library_sources_ifdef(CONFIG_NFC_TNEP_TAG tag.c)
9+
zephyr_library_sources_ifdef(CONFIG_NFC_TNEP_TAG_SIGNALLING_ZEPHYR tag_signalling_zephyr.c)
910
zephyr_library_sources_ifdef(CONFIG_NFC_TNEP_POLLER poller.c)
1011

1112
add_subdirectory_ifdef(CONFIG_NFC_TNEP_CH ch)

subsys/nfc/tnep/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ config NFC_TNEP_RX_MAX_RECORD_SIZE
2828
help
2929
Set the maximum size of received NDEF Record
3030

31+
choice NFC_TNEP_TAG_SIGNALLING
32+
prompt "NFC TNEP Tag signalling method"
33+
34+
config NFC_TNEP_TAG_SIGNALLING_ZEPHYR
35+
bool "NFC TNEP Tag signalling using Zephyr primitives"
36+
select POLL
37+
38+
endchoice
39+
3140
module = NFC_TNEP_TAG
3241
module-str = TNEP_TAG
3342
source "$(ZEPHYR_BASE)/subsys/logging/Kconfig.template.log_config"

subsys/nfc/tnep/tag.c

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,14 @@
77
#include <stdbool.h>
88
#include <zephyr/kernel.h>
99
#include <nfc/tnep/tag.h>
10+
#include <nfc/tnep/tag_signalling.h>
1011
#include <nfc/ndef/msg.h>
1112
#include <nfc/t4t/ndef_file.h>
1213
#include <nfc/ndef/msg_parser.h>
1314
#include <zephyr/logging/log.h>
1415

1516
LOG_MODULE_REGISTER(nfc_tnep_tag, CONFIG_NFC_TNEP_TAG_LOG_LEVEL);
1617

17-
#define TNEP_EVENT_RX_IDX 0
18-
#define TNEP_EVENT_TX_IDX 1
19-
20-
enum tnep_event {
21-
TNEP_EVENT_DUMMY,
22-
TNEP_EVENT_MSG_RX_NEW,
23-
TNEP_EVENT_TAG_SELECTED,
24-
};
25-
2618
enum tnep_state_name {
2719
TNEP_STATE_DISABLED,
2820
TNEP_STATE_SERVICE_READY,
@@ -53,12 +45,6 @@ struct tnep_rx_buffer {
5345
size_t len;
5446
};
5547

56-
struct tnep_control {
57-
struct k_poll_event *events;
58-
struct k_poll_signal msg_rx;
59-
struct k_poll_signal msg_tx;
60-
};
61-
6248
struct tnep_tag {
6349
struct nfc_ndef_msg_desc *msg;
6450
struct tnep_buffer tx;
@@ -72,7 +58,6 @@ struct tnep_tag {
7258
uint8_t *current_buff;
7359
};
7460

75-
static struct tnep_control tnep_ctrl;
7661
static struct tnep_tag tnep;
7762

7863
struct tnep_state {
@@ -501,11 +486,10 @@ void nfc_tnep_tag_rx_msg_indicate(const uint8_t *rx_buffer, size_t len)
501486
tnep.rx.len = len;
502487
tnep.rx.data = rx_buffer;
503488

504-
k_poll_signal_raise(&tnep_ctrl.msg_rx, TNEP_EVENT_MSG_RX_NEW);
489+
nfc_tnep_tag_signalling_rx_event_raise(TNEP_EVENT_MSG_RX_NEW);
505490
}
506491

507-
int nfc_tnep_tag_init(struct k_poll_event *events, uint8_t event_cnt,
508-
nfc_payload_set_t payload_set)
492+
int nfc_tnep_tag_init(nfc_payload_set_t payload_set)
509493
{
510494
extern struct nfc_tnep_tag_service
511495
_nfc_tnep_tag_service_list_start[];
@@ -514,21 +498,11 @@ int nfc_tnep_tag_init(struct k_poll_event *events, uint8_t event_cnt,
514498

515499
LOG_DBG("TNEP initialization");
516500

517-
if (!events) {
518-
return -EINVAL;
519-
}
520-
521501
if (!payload_set) {
522502
LOG_ERR("No function for NFC payload set provided");
523503
return -EINVAL;
524504
}
525505

526-
if (event_cnt != NFC_TNEP_EVENTS_NUMBER) {
527-
LOG_ERR("Invalid k_pool events count. Got %d events, required %d",
528-
event_cnt, NFC_TNEP_EVENTS_NUMBER);
529-
return -EINVAL;
530-
}
531-
532506
if (!atomic_cas(&current_state, TNEP_STATE_DISABLED,
533507
TNEP_STATE_DISABLED)) {
534508
LOG_ERR("TNEP already running");
@@ -541,25 +515,11 @@ int nfc_tnep_tag_init(struct k_poll_event *events, uint8_t event_cnt,
541515
return -EIO;
542516
}
543517

544-
tnep_ctrl.events = events;
545518
tnep.current_buff = tnep.tx.data;
546519
tnep.data_set = payload_set;
547520
tnep.svc_cnt = _nfc_tnep_tag_service_list_end -
548521
_nfc_tnep_tag_service_list_start;
549522

550-
LOG_DBG("k_pool signals initialization");
551-
552-
k_poll_signal_init(&tnep_ctrl.msg_rx);
553-
k_poll_signal_init(&tnep_ctrl.msg_tx);
554-
555-
k_poll_event_init(&tnep_ctrl.events[TNEP_EVENT_RX_IDX],
556-
K_POLL_TYPE_SIGNAL, K_POLL_MODE_NOTIFY_ONLY,
557-
&tnep_ctrl.msg_rx);
558-
559-
k_poll_event_init(&tnep_ctrl.events[TNEP_EVENT_TX_IDX],
560-
K_POLL_TYPE_SIGNAL, K_POLL_MODE_NOTIFY_ONLY,
561-
&tnep_ctrl.msg_tx);
562-
563523
return 0;
564524
}
565525

@@ -635,19 +595,16 @@ int nfc_tnep_initial_msg_encode(struct nfc_ndef_msg_desc *msg,
635595

636596
void nfc_tnep_tag_process(void)
637597
{
638-
/* Check for signals */
639-
for (size_t i = 0; i < NFC_TNEP_EVENTS_NUMBER; i++) {
640-
if (tnep_ctrl.events[i].state == K_POLL_STATE_SIGNALED) {
641-
enum tnep_event event;
642-
643-
event = tnep_ctrl.events[i].signal->result;
598+
enum tnep_event event = TNEP_EVENT_DUMMY;
644599

645-
k_poll_signal_reset(tnep_ctrl.events[i].signal);
646-
tnep_ctrl.events[i].state = K_POLL_STATE_NOT_READY;
600+
if (nfc_tnep_tag_signalling_rx_event_check_and_clear(&event)) {
601+
/* Run TNEP State Machine - prepare response */
602+
tnep_state_machine[atomic_get(&current_state)].process(event);
603+
}
647604

648-
/* Run TNEP State Machine - prepare response */
649-
tnep_state_machine[atomic_get(&current_state)].process(event);
650-
}
605+
if (nfc_tnep_tag_signalling_tx_event_check_and_clear(&event)) {
606+
/* Run TNEP State Machine - prepare response */
607+
tnep_state_machine[atomic_get(&current_state)].process(event);
651608
}
652609
}
653610

@@ -707,7 +664,7 @@ int nfc_tnep_tag_tx_msg_no_app_data(void)
707664

708665
void nfc_tnep_tag_on_selected(void)
709666
{
710-
k_poll_signal_raise(&tnep_ctrl.msg_tx, TNEP_EVENT_TAG_SELECTED);
667+
nfc_tnep_tag_signalling_tx_event_raise(TNEP_EVENT_TAG_SELECTED);
711668
}
712669

713670
size_t nfc_tnep_tag_svc_count_get(void)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2026 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/logging/log.h>
9+
#include <nfc/tnep/tag_signalling.h>
10+
#include <nfc/tnep/tag_signalling_zephyr.h>
11+
12+
LOG_MODULE_DECLARE(nfc_tnep_tag, CONFIG_NFC_TNEP_TAG_LOG_LEVEL);
13+
14+
#define TNEP_EVENT_RX_IDX 0
15+
#define TNEP_EVENT_TX_IDX 1
16+
17+
struct tnep_control {
18+
struct k_poll_event *events;
19+
struct k_poll_signal msg_rx;
20+
struct k_poll_signal msg_tx;
21+
};
22+
23+
static struct tnep_control tnep_ctrl;
24+
25+
int nfc_tnep_tag_signalling_init(struct k_poll_event *events, uint8_t event_cnt)
26+
{
27+
if (!events) {
28+
return -EINVAL;
29+
}
30+
31+
if (event_cnt != NFC_TNEP_EVENTS_NUMBER) {
32+
LOG_ERR("Invalid k_pool events count. Got %d events, required %d",
33+
event_cnt, NFC_TNEP_EVENTS_NUMBER);
34+
return -EINVAL;
35+
}
36+
37+
tnep_ctrl.events = events;
38+
39+
k_poll_signal_init(&tnep_ctrl.msg_rx);
40+
k_poll_signal_init(&tnep_ctrl.msg_tx);
41+
42+
k_poll_event_init(&tnep_ctrl.events[TNEP_EVENT_RX_IDX],
43+
K_POLL_TYPE_SIGNAL, K_POLL_MODE_NOTIFY_ONLY,
44+
&tnep_ctrl.msg_rx);
45+
46+
k_poll_event_init(&tnep_ctrl.events[TNEP_EVENT_TX_IDX],
47+
K_POLL_TYPE_SIGNAL, K_POLL_MODE_NOTIFY_ONLY,
48+
&tnep_ctrl.msg_tx);
49+
50+
return 0;
51+
}
52+
53+
void nfc_tnep_tag_signalling_rx_event_raise(enum tnep_event event)
54+
{
55+
k_poll_signal_raise(&tnep_ctrl.msg_rx, event);
56+
}
57+
58+
void nfc_tnep_tag_signalling_tx_event_raise(enum tnep_event event)
59+
{
60+
k_poll_signal_raise(&tnep_ctrl.msg_tx, event);
61+
}
62+
63+
static bool event_check_and_clear(int event_idx, enum tnep_event *event)
64+
{
65+
if (tnep_ctrl.events[event_idx].state == K_POLL_STATE_SIGNALED) {
66+
*event = tnep_ctrl.events[event_idx].signal->result;
67+
68+
k_poll_signal_reset(tnep_ctrl.events[event_idx].signal);
69+
tnep_ctrl.events[event_idx].state = K_POLL_STATE_NOT_READY;
70+
71+
return true;
72+
}
73+
74+
return false;
75+
}
76+
77+
bool nfc_tnep_tag_signalling_rx_event_check_and_clear(enum tnep_event *event)
78+
{
79+
return event_check_and_clear(TNEP_EVENT_RX_IDX, event);
80+
}
81+
82+
bool nfc_tnep_tag_signalling_tx_event_check_and_clear(enum tnep_event *event)
83+
{
84+
return event_check_and_clear(TNEP_EVENT_TX_IDX, event);
85+
}

0 commit comments

Comments
 (0)