10 #if RUUVI_NRF5_SDK15_NFC_ENABLED
19 #include "nfc_ndef_msg.h"
20 #include "nfc_uri_rec.h"
21 #include "nfc_text_rec.h"
22 #include "nfc_launchapp_rec.h"
23 #include "nfc_ndef_msg_parser.h"
25 #define RUUVI_NRF5_SDK15_COMM_NFC_MAX_RECORDS 4
26 #define BIN_PAY_DESC_HEADER_LEN 3U
30 volatile uint8_t connected;
32 volatile uint8_t rx_updated;
33 volatile uint8_t tx_updated;
34 volatile uint8_t configurable;
35 uint8_t nfc_ndef_msg[ (RUUVI_NRF5_SDK15_COMM_NFC_MAX_RECORDS + 1)
37 volatile size_t nfc_ndef_msg_len;
38 uint8_t desc_buf[NFC_NDEF_PARSER_REQIRED_MEMO_SIZE_CALC (
39 RUUVI_NRF5_SDK15_COMM_NFC_MAX_RECORDS)];
42 } nrf5_sdk15_nfc_state;
47 static void nfc_callback (
void * context,
58 if (NULL != * (nrf5_sdk15_nfc_state.on_nfc_evt)
59 &&
false == nrf5_sdk15_nfc_state.connected)
64 nrf5_sdk15_nfc_state.connected =
true;
68 if (NULL != * (nrf5_sdk15_nfc_state.on_nfc_evt) &&
true == nrf5_sdk15_nfc_state.connected)
74 nrf5_sdk15_nfc_state.connected =
false;
78 if (NULL != * (nrf5_sdk15_nfc_state.on_nfc_evt)) { (* (nrf5_sdk15_nfc_state.on_nfc_evt)) (
RI_COMM_SENT, NULL, 0); }
87 nrf5_sdk15_nfc_state.nfc_ndef_msg_len = dataLength;
88 nrf5_sdk15_nfc_state.rx_updated =
true;
91 if (!nrf5_sdk15_nfc_state.configurable) { nrf5_sdk15_nfc_state.tx_updated =
true;}
94 if (NULL != * (nrf5_sdk15_nfc_state.on_nfc_evt)) { (* (nrf5_sdk15_nfc_state.on_nfc_evt)) (
RI_COMM_RECEIVED, nrf5_sdk15_nfc_state.nfc_ndef_msg, dataLength); }
104 NFC_NDEF_MSG_DEF (nfc_ndef_msg,
105 RUUVI_NRF5_SDK15_COMM_NFC_MAX_RECORDS);
109 static size_t nfc_fw_length = 0;
111 static size_t nfc_addr_length = 0;
113 static size_t nfc_id_length = 0;
115 static size_t nfc_tx_length = 0;
124 ret_code_t err_code = NRF_SUCCESS;
128 if (NRF_ERROR_INVALID_STATE == err_code)
130 err_code = NRF_SUCCESS;
133 memset (nrf5_sdk15_nfc_state.nfc_ndef_msg, 0, sizeof (nrf5_sdk15_nfc_state.nfc_ndef_msg));
136 sizeof (nrf5_sdk15_nfc_state.nfc_ndef_msg));
143 nrf5_sdk15_nfc_state.on_nfc_evt = & (channel->
on_evt);
146 nrf5_sdk15_nfc_state.initialized =
true;
147 nrf5_sdk15_nfc_state.msg_index = 0;
155 memset (&nrf5_sdk15_nfc_state, 0,
sizeof (nrf5_sdk15_nfc_state));
168 if (! (nrf5_sdk15_nfc_state.tx_updated)) {
return RD_SUCCESS; }
171 ret_code_t err_code = NRF_SUCCESS;
173 NFC_NDEF_TEXT_RECORD_DESC_DEF (nfc_fw_rec,
180 NFC_NDEF_TEXT_RECORD_DESC_DEF (nfc_addr_rec,
187 NFC_NDEF_TEXT_RECORD_DESC_DEF (nfc_id_rec,
194 NFC_NDEF_TEXT_RECORD_DESC_DEF (nfc_bin_rec, \
196 data_code,
sizeof (data_code), \
200 nfc_ndef_msg_clear (&NFC_NDEF_MSG (nfc_ndef_msg));
205 err_code |= nfc_ndef_msg_record_add (&NFC_NDEF_MSG (nfc_ndef_msg),
206 &NFC_NDEF_TEXT_RECORD_DESC (nfc_id_rec));
211 err_code |= nfc_ndef_msg_record_add (&NFC_NDEF_MSG (nfc_ndef_msg),
212 &NFC_NDEF_TEXT_RECORD_DESC (nfc_addr_rec));
217 err_code |= nfc_ndef_msg_record_add (&NFC_NDEF_MSG (nfc_ndef_msg),
218 &NFC_NDEF_TEXT_RECORD_DESC (nfc_fw_rec));
223 err_code |= nfc_ndef_msg_record_add (&NFC_NDEF_MSG (nfc_ndef_msg),
224 &NFC_NDEF_TEXT_RECORD_DESC (nfc_bin_rec));
229 uint32_t msg_len =
sizeof (nrf5_sdk15_nfc_state.nfc_ndef_msg);
230 err_code |= nfc_ndef_msg_encode (&NFC_NDEF_MSG (nfc_ndef_msg),
231 nrf5_sdk15_nfc_state.nfc_ndef_msg,
234 sizeof (nrf5_sdk15_nfc_state.nfc_ndef_msg));
237 nrf5_sdk15_nfc_state.tx_updated =
false;
248 memcpy (nfc_tx_buf, message->
data, nfc_tx_length);
249 nrf5_sdk15_nfc_state.tx_updated =
true;
259 nfc_fw_length = length;
260 memcpy (nfc_fw_buf, version, nfc_fw_length);
261 nrf5_sdk15_nfc_state.tx_updated =
true;
271 nfc_addr_length = length;
272 memcpy (nfc_addr_buf, address, nfc_addr_length);
273 nrf5_sdk15_nfc_state.tx_updated =
true;
278 const uint8_t length)
284 nfc_id_length = length;
285 memcpy (nfc_id_buf,
id, nfc_id_length);
286 nrf5_sdk15_nfc_state.tx_updated =
true;
315 if (0 == nrf5_sdk15_nfc_state.msg_index)
317 uint32_t desc_buf_len =
sizeof (nrf5_sdk15_nfc_state.desc_buf);
318 uint32_t data_lenu32 =
sizeof (
319 nrf5_sdk15_nfc_state.nfc_ndef_msg);
320 err_code = ndef_msg_parser (nrf5_sdk15_nfc_state.desc_buf,
322 nrf5_sdk15_nfc_state.nfc_ndef_msg + 2,
329 if (nrf5_sdk15_nfc_state.msg_index < ( (nfc_ndef_msg_desc_t *)
330 nrf5_sdk15_nfc_state.desc_buf)->record_count)
333 nfc_ndef_record_desc_t *
const p_rec_desc = ( (nfc_ndef_msg_desc_t *)
334 nrf5_sdk15_nfc_state.desc_buf)->pp_record[nrf5_sdk15_nfc_state.msg_index];
335 nfc_ndef_bin_payload_desc_t * p_bin_pay_desc = p_rec_desc->p_payload_descriptor;
337 size_t payload_len = (p_bin_pay_desc->payload_length - BIN_PAY_DESC_HEADER_LEN);
345 memcpy (msg->
data, ( (uint8_t *) p_bin_pay_desc->p_payload + BIN_PAY_DESC_HEADER_LEN),
350 nrf5_sdk15_nfc_state.msg_index++;
357 || nrf5_sdk15_nfc_state.msg_index == ( (nfc_ndef_msg_desc_t *)
358 nrf5_sdk15_nfc_state.desc_buf)->record_count)
360 nrf5_sdk15_nfc_state.msg_index = 0;
361 nrf5_sdk15_nfc_state.rx_updated =
false;
364 if (nrf5_sdk15_nfc_state.configurable)
#define RD_ERROR_NULL
Null Pointer.
uint32_t rd_status_t
bitfield for representing errors
#define RD_ERROR_INVALID_LENGTH
Invalid Length.
#define RD_STATUS_MORE_AVAILABLE
Driver has more data queued.
rd_status_t ruuvi_nrf5_sdk15_to_ruuvi_error(const ret_code_t error)
convert nrf5 sdk15 error code into Ruuvi error code.
#define RD_SUCCESS
Internal Error.
#define RD_ERROR_DATA_SIZE
Invalid Data size.
#define RD_ERROR_NOT_FOUND
Not found.
#define RD_ERROR_INVALID_STATE
Invalid state, operation disallowed in this state.
ret_code_t nfc_t4t_done(void)
Release reference to application callback.
ret_code_t nfc_t4t_ndef_rwpayload_set(uint8_t *p_emulation_buffer, size_t buffer_length)
Set emulation buffer and content for a NDEF Tag emulation that is Read/Writable.
ret_code_t nfc_t4t_setup(nfc_t4t_callback_t callback, void *p_context)
Register the application callback for event signaling.
ret_code_t nfc_t4t_emulation_start(void)
Activate the NFC frontend.
ret_code_t nfc_t4t_emulation_stop(void)
Deactivate the NFC frontend.
@ NFC_T4T_EVENT_NDEF_UPDATED
External Reader has written to length information of NDEF-Data from Emulation.
@ NFC_T4T_EVENT_FIELD_OFF
External Reader polling ended.
@ NFC_T4T_EVENT_FIELD_ON
External Reader polling detected.
@ NFC_T4T_EVENT_NDEF_READ
External Reader has read static NDEF-Data from Emulation.
Header to enable and disable module compilation.
Ruuvi error codes and error check function.
rd_status_t(* ri_comm_evt_handler_fp_t)(const ri_comm_evt_t evt, void *p_data, size_t data_len)
Application event handler for communication events.
#define RI_COMM_DIS_STRLEN
Maximum length for device information strings.
@ RI_COMM_CONNECTED
Connection established, OK to send, may receive data.
@ RI_COMM_RECEIVED
New data received, available to read with read function.
@ RI_COMM_SENT
One queued message was sent with all repetitions.
@ RI_COMM_DISCONNECTED
Connection lost, cannot send, may not receive data.
rd_status_t ri_nfc_address_set(const uint8_t *const address, const uint8_t length)
#define RI_NFC_DATA_FIELD_CODE
#define RI_NFC_ID_FIELD_CODE
#define RI_NFC_ADDR_FIELD_CODE
rd_status_t ri_nfc_init(ri_comm_channel_t *const channel)
rd_status_t ri_nfc_data_set(void)
rd_status_t ri_nfc_send(ri_comm_message_t *messge)
rd_status_t ri_nfc_fw_version_set(const uint8_t *const version, const uint8_t length)
rd_status_t ri_nfc_receive(ri_comm_message_t *messge)
rd_status_t ri_nfc_uninit(ri_comm_channel_t *const channel)
#define RI_NFC_SW_FIELD_CODE
rd_status_t ri_nfc_id_set(const uint8_t *const id, const uint8_t length)
control API for communication via outside world
ri_comm_evt_handler_fp_t on_evt
Callback to application-level event handler, must be set in application.
ri_comm_xfer_fp_t send
Asynchronous send function.
ri_comm_init_fp_t init
Initialize and populate channel api control.
ri_comm_init_fp_t uninit
Uninitialize and depopulate channel api control.
ri_comm_xfer_fp_t read
Asynchronous read function.
Application message structure used for communication.
uint8_t data[RI_COMM_MESSAGE_MAX_LENGTH]
Data payload.
uint8_t data_length
Length of data.