nfc_t4t_lib.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /**
  2. * Copyright (c) 2016 - 2019, Telit Communications Cyprus Ltd
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NFC_T4T_LIB_H__
  41. #define NFC_T4T_LIB_H__
  42. /** @file
  43. *
  44. * @addtogroup nfc_api
  45. *
  46. * @defgroup nfc_t4t NFC Type 4 Tag
  47. * @ingroup nfc_api
  48. * @brief Implementation of NFC Type 4 Tag.
  49. *
  50. * @defgroup nfc_t4t_lib NFC tag 4 type emulation library
  51. * @{
  52. * @ingroup nfc_t4t
  53. * @brief The T4T emulation library interface
  54. *
  55. * This is the NFC Forum NDEF tag 4 type emulation library. It implements the ISO14443-4A protocol
  56. * (ISO-DEP) and additionally can emulate a read-writable NDEF content. If the emulation of the NDEF
  57. * content is not needed, the library works in a raw mode where all APDUs are delivered to the user,
  58. * who is then responsible to generate a timely RPDU as a response.
  59. *
  60. * The sequence of initializing functions determines whether the NDEF emulation will run or whether
  61. * the raw mode is used.
  62. *
  63. * - E.g. NDEF emulation
  64. * * @ref nfc_t4t_setup
  65. * * @ref nfc_t4t_ndef_rwpayload_set or @ref nfc_t4t_ndef_staticpayload_set
  66. * * @ref nfc_t4t_emulation_start
  67. * * ... running in NDEF emulation mode ...
  68. * - E.g. RAW mode
  69. * * @ref nfc_t4t_setup
  70. * * @ref nfc_t4t_emulation_start
  71. * * ... running in RAW emulation mode ...
  72. */
  73. #include <stdint.h>
  74. #include <stdbool.h>
  75. #include <string.h>
  76. #include "sdk_errors.h"
  77. #ifdef __cplusplus
  78. extern "C" {
  79. #endif
  80. #define NFC_T4T_MAX_PAYLOAD_SIZE 0xFFF0U
  81. /**< @brief Emulation mode. */
  82. typedef enum
  83. {
  84. NFC_T4T_EMUMODE_NDEF, ///< Emulated NDEF AID and EF-Files.
  85. NFC_T4T_EMUMODE_PICC ///< Run just ISO-DEP, deliver I-Frames up.
  86. } nfc_t4t_emu_mode_t;
  87. /**< @brief Event identifiers used by the @ref nfc_t4t_callback_t */
  88. typedef enum
  89. {
  90. NFC_T4T_EVENT_NONE,
  91. ///< This ID is never used. Dummy value for completeness.
  92. NFC_T4T_EVENT_FIELD_ON,
  93. ///< External Reader polling detected.
  94. NFC_T4T_EVENT_FIELD_OFF,
  95. ///< External Reader polling ended.
  96. NFC_T4T_EVENT_NDEF_READ,
  97. ///< External Reader has read static NDEF-Data from Emulation.
  98. /**<
  99. * A Read operation happened on last byte of NDEF-Data.
  100. */
  101. NFC_T4T_EVENT_NDEF_UPDATED,
  102. ///< External Reader has written to length information of NDEF-Data from Emulation.
  103. /**<
  104. * The usual behavior of a Reader-Writer that accesses NDEF information for update is to set
  105. * the length to zero at the beginning of the update process. It then writes the content
  106. * of NDEF-Data. When all content is written it will update the length information inside
  107. * the NDEF file. This event will be generated every time an update to the length is happening.
  108. * This length information is residing in the first 2 bytes of the NDEF-Content container and is called 'NLEN'.
  109. * Since this callback is triggered on any access to these bytes the returned data_length
  110. * information might not be consistent (e.g. in case of only a single byte write to the length).
  111. *
  112. * @param[out] data_length Current value of NDEF content length
  113. * information i.e. 'NLEN' field.
  114. */
  115. NFC_T4T_EVENT_DATA_TRANSMITTED,
  116. ///< In Raw mode it signals that the data from @ref nfc_t4t_response_pdu_send have been sent out.
  117. NFC_T4T_EVENT_DATA_IND,
  118. ///< In Raw mode delivers the APDU fragments
  119. /**<
  120. * All @ref NFC_T4T_EVENT_DATA_IND events that have the @ref NFC_T4T_DI_FLAG_MORE flag set
  121. * belong to the same APDU. The first @ref NFC_T4T_EVENT_DATA_IND without @ref NFC_T4T_DI_FLAG_MORE
  122. * flag signals the completeness of the APDU. The Application then has to reply with a call
  123. * to @ref nfc_t4t_response_pdu_send. The library will handle internally the fragmentation of
  124. * the response towards the Reader-Writer. The data of the response PDU must be kept
  125. * valid until the next callback from the library happens (e.g. next @ref NFC_T4T_EVENT_DATA_IND
  126. * or @ref NFC_T4T_EVENT_FIELD_OFF).
  127. *
  128. * @param[out] p_data Pointer to the fragment of APDU.
  129. * @param[out] data_length Length of data.
  130. * @param[out] flags @ref nfc_t4t_data_ind_flags_t.
  131. */
  132. } nfc_t4t_event_t;
  133. /**< @brief Flags coming with nfc_t4t_callback_t at @ref NFC_T4T_EVENT_DATA_IND event.*/
  134. typedef enum
  135. {
  136. NFC_T4T_DI_FLAG_NONE = 0x00, ///< Dummy value.
  137. NFC_T4T_DI_FLAG_MORE = 0x01 ///< This signals that more data is expected to be received.
  138. } nfc_t4t_data_ind_flags_t;
  139. /**< @brief Parameter IDs that can be set/get with @ref nfc_t4t_parameter_set or
  140. * @ref nfc_t4t_parameter_get.
  141. */
  142. typedef enum
  143. {
  144. NFC_T4T_PARAM_TESTING, ///< Internal usage only for Unit-Testing.
  145. NFC_T4T_PARAM_FWI, ///< Frame Wait Time parameter
  146. NFC_T4T_PARAM_SELRES, ///< Parameter for setting 'Protocol' bits for SEL_RES packet
  147. NFC_T4T_PARAM_NFCID1, /**< NFCID1 value, data can be 4, 7, or 10 bytes long (single, double, or triple size).
  148. To use default NFCID1 of specific length pass one byte containing requested length.
  149. Default 7-byte NFCID1 will be used if this parameter was not set. This parameter can be
  150. set before nfc_t2t_setup() to set initial NFCID1 and it can be changed later. */
  151. } nfc_t4t_param_id_t;
  152. /** @brief Callback to pass events from NFCLib to application.
  153. *
  154. * @param[out] p_context Application context for callback execution.
  155. * @param[out] event The event that occurred. see @ref nfc_t4t_event_t.
  156. * @param[out] p_data Data to send to the application (event specific).
  157. * @param[out] data_length Length of the data. In case of @ref NFC_T4T_EVENT_NDEF_UPDATED, this parameter
  158. * contains the value of the 'NLEN' field of the NDEF File;
  159. * if the value is non-zero, it corresponds to the new size of the NDEF Message in the updated NDEF File.
  160. * @param[out] flags Some events deliver flags. see @ref nfc_t4t_event_t for details.
  161. */
  162. typedef void (*nfc_t4t_callback_t)(void * p_context,
  163. nfc_t4t_event_t event,
  164. const uint8_t * p_data,
  165. size_t data_length,
  166. uint32_t flags);
  167. /** @brief Register the application callback for event signaling.
  168. *
  169. * The callback will be called by NFCLib to notify the application of relevant events. It will be
  170. * called from the HAL_NFC callback context. The library support 3 different Modes of Emulation:
  171. * - Raw ISO-Dep exchanges. All PDUs are signaled through the callback.
  172. * - Read-Only T4T NDEF-Tag. A static buffer is served. Only Field-Status callbacks.
  173. * - Read-Write T4T NDEF-Tag. A mutable buffer is used. Only Field-Status callbacks.
  174. *
  175. * The default mode is Raw ISO-Dep mode. The two other NDEF T4T modes are activated through
  176. * the corresponding @ref nfc_t4t_ndef_rwpayload_set/ @ref nfc_t4t_ndef_staticpayload_set functions.
  177. * The mode is locked in with a call to @ref nfc_t4t_emulation_start.
  178. *
  179. * @param[in] callback Function pointer to the callback.
  180. * @param[in] p_context Pointer to a memory area used by the callback for execution (optional).
  181. *
  182. * @retval NRF_SUCCESS Success.
  183. * @retval NRF_ERROR_INVALID_STATE If emulation is in running state.
  184. */
  185. ret_code_t nfc_t4t_setup(nfc_t4t_callback_t callback, void * p_context);
  186. /** @brief Set emulation buffer and content for a NDEF Tag emulation that is Read/Writable.
  187. *
  188. * The buffer needs to be kept accessible for the lifetime of the emulation.
  189. * If an external Reader-Writer changes the NDEF content it is signaled through the app-callback.
  190. *
  191. * @param[in] p_emulation_buffer Buffer pointer
  192. * @param[in] buffer_length Length of buffer (maximum writable NDEF size)
  193. *
  194. * @retval NRF_SUCCESS Success.
  195. * @retval NRF_ERROR_DATA_SIZE Invalid argument (e.g. wrong data length).
  196. * @retval NRF_ERROR_INVALID_PARAM Invalid argument (e.g. NULL pointer).
  197. * @retval NRF_ERROR_INVALID_STATE If emulation is in running state.
  198. */
  199. ret_code_t nfc_t4t_ndef_rwpayload_set(uint8_t * p_emulation_buffer, size_t buffer_length);
  200. /** @brief Set emulationBuffer and Content for a NDEF Tag emulation that is Read-Only.
  201. *
  202. * The buffer needs to be kept accessible for the lifetime of the emulation.
  203. * Since no write access is done to the buffer, the content could reside in flash memory.
  204. *
  205. * @param[in] p_emulation_buffer Const buffer pointer
  206. * @param[in] buffer_length Length of contained NDEF payload message
  207. *
  208. * @retval NRF_SUCCESS Success.
  209. * @retval NRF_ERROR_DATA_SIZE Invalid argument (e.g. wrong data length).
  210. * @retval NRF_ERROR_INVALID_PARAM Invalid argument (e.g. NULL pointer).
  211. * @retval NRF_ERROR_INVALID_STATE Emulation is in running stated.
  212. */
  213. ret_code_t nfc_t4t_ndef_staticpayload_set(const uint8_t * p_emulation_buffer, size_t buffer_length);
  214. /** @brief Send a raw response PDU after getting a Request PDU callback.
  215. *
  216. * When the library works in raw ISO-DEP mode it will signal request PDUs through the callback.
  217. * The application then has to answer with a response PDU. It will use this function to send back
  218. * the response PDU. This function can not be used in T4T NDEF (RW / STATIC) emulation modes.
  219. *
  220. * The lower ISODEP layer will handle the defragmentation of a long response PDU into smaller
  221. * pieces that the PCD can understand.
  222. *
  223. * @param[in] p_pdu Const PDU pointer.
  224. * @param[in] pdu_length Length of PDU.
  225. *
  226. * @retval NRF_SUCCESS Success.
  227. * @retval NRF_ERROR_DATA_SIZE Invalid argument (e.g. wrong data length).
  228. * @retval NRF_ERROR_INVALID_PARAM Invalid argument (e.g. NULL pointer).
  229. * @retval NRF_ERROR_INVALID_STATE Emulation is in running state.
  230. */
  231. ret_code_t nfc_t4t_response_pdu_send(const uint8_t * p_pdu, size_t pdu_length);
  232. /** @brief Set an NFC parameter.
  233. *
  234. * Allows to set any parameter defined as available by HAL_NFC.
  235. *
  236. * @param[in] id ID of the parameter to set.
  237. * @param[in] p_data Pointer to a buffer containing the data to set.
  238. * @param[in] data_length Size of the buffer containing the data to set.
  239. *
  240. * @retval NRF_SUCCESS Success.
  241. * @retval NRF_ERROR_DATA_SIZE Invalid argument (e.g. wrong data length).
  242. * @retval NRF_ERROR_INVALID_PARAM Invalid argument (e.g. NULL pointer).
  243. */
  244. ret_code_t nfc_t4t_parameter_set(nfc_t4t_param_id_t id, void * p_data, size_t data_length);
  245. /** @brief Query an NFC parameter value.
  246. *
  247. * The queried value will be placed into the passed data buffer.
  248. * If the buffer is too small, p_max_data_length will contain the required buffer size.
  249. * If the buffer is big enough, p_max_data_length will contain the actual size of the data.
  250. *
  251. * @param[in] id ID of the parameter to query.
  252. * @param[out] p_data Pointer to a buffer receiving the queried data.
  253. * @param[out] p_max_data_length Size of the buffer, receives actual size of queried data.
  254. *
  255. * @retval NRF_SUCCESS Success.
  256. * @retval NRF_ERROR_DATA_SIZE Invalid argument (e.g. wrong data length).
  257. * @retval NRF_ERROR_INVALID_PARAM Invalid argument (e.g. NULL pointer).
  258. */
  259. ret_code_t nfc_t4t_parameter_get(nfc_t4t_param_id_t id, void * p_data, size_t * p_max_data_length);
  260. /** @brief Activate the NFC frontend.
  261. *
  262. * Only after calling this function, events will be posted to the application callback.
  263. *
  264. * @retval NRF_SUCCESS Success.
  265. * @retval NRF_ERROR_INVALID_STATE Already started.
  266. */
  267. ret_code_t nfc_t4t_emulation_start(void);
  268. /**
  269. * @brief Deactivate the NFC frontend.
  270. *
  271. * After calling this function, no more events will be posted to the application callback.
  272. *
  273. * @retval NRF_SUCCESS Success.
  274. * @retval NRF_ERROR_INVALID_STATE Emulation was already stopped
  275. */
  276. ret_code_t nfc_t4t_emulation_stop(void);
  277. /**
  278. * @brief Release reference to application callback.
  279. *
  280. * After calling this function, the passed callback pointer is no longer considered valid.
  281. * After calling this function, the passed p_ndef pointer is no longer considered valid.
  282. *
  283. * You need to restart with @ref nfc_t4t_setup to run a new Emulation.
  284. *
  285. * @retval NRF_SUCCESS Always succeeds.
  286. */
  287. ret_code_t nfc_t4t_done(void);
  288. #ifdef __cplusplus
  289. }
  290. #endif
  291. /**
  292. @}
  293. */
  294. #endif // NFC_T4T_LIB_H__