nfc_t2t_lib.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**
  2. * Copyright (c) 2015 - 2018, 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_T2T_LIB_H__
  41. #define NFC_T2T_LIB_H__
  42. /** @file
  43. *
  44. * @addtogroup nfc_api
  45. *
  46. * @defgroup nfc_t2t NFC Type 2 Tag
  47. * @ingroup nfc_api
  48. * @brief Implementation of NFC Type 2 Tag.
  49. *
  50. * @defgroup nfc_t2t_lib NFC tag 2 type emulation library
  51. * @{
  52. * @ingroup nfc_t2t
  53. * @brief The T2T emulation library interface.
  54. */
  55. #include <string.h>
  56. #include <sdk_errors.h>
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. #define NFC_T2T_SIZEOF_INTERNAL_BYTES 10 ///< T2T internal byte size.
  61. #define NFC_T2T_MAX_PAYLOAD_SIZE 988 ///< Maximum NDEF message size.
  62. #define NFC_T2T_MAX_PAYLOAD_SIZE_RAW 1008 ///< No NDEF-TLV and no implicit lock bytes at the end.
  63. /** @brief Events passed to the callback function. */
  64. typedef enum
  65. {
  66. NFC_T2T_EVENT_NONE,
  67. ///< Not used.
  68. NFC_T2T_EVENT_FIELD_ON,
  69. ///< NFC tag has detected external NFC field and was selected by an NFC polling device.
  70. NFC_T2T_EVENT_FIELD_OFF,
  71. ///< External NFC field has been removed.
  72. NFC_T2T_EVENT_DATA_READ,
  73. ///< NFC polling device has read all tag data.
  74. /**<
  75. * Repeated reading in the same session i.e. before @ref NFC_T2T_EVENT_FIELD_OFF event,
  76. * will not trigger another @ref NFC_T2T_EVENT_DATA_READ event.
  77. */
  78. NFC_T2T_EVENT_STOPPED
  79. ///< Reference to the application NFC callback has been released using @ref nfc_t2t_done.
  80. } nfc_t2t_event_t;
  81. typedef enum
  82. {
  83. NFC_T2T_PARAM_TESTING, ///< Used for unit tests.
  84. NFC_T2T_PARAM_NFCID1, /**< NFCID1 value, data can be 4, 7, or 10 bytes long (single, double, or triple size).
  85. To use default NFCID1 of specific length pass one byte containing requested length.
  86. Default 7-byte NFCID1 will be used if this parameter was not set. This parameter can be
  87. set before nfc_t2t_setup() to set initial NFCID1 and it can be changed later. */
  88. } nfc_t2t_param_id_t;
  89. /** @brief Callback to pass events from NFC T2T Library to application.
  90. *
  91. * @param[in] p_context Application context for callback execution.
  92. * @param[in] event The event that occurred.
  93. * @param[in] p_data Data to send to the application (event specific).
  94. * @param[in] data_length Length of the data.
  95. */
  96. typedef void (*nfc_t2t_callback_t)(void * p_context,
  97. nfc_t2t_event_t event,
  98. const uint8_t * p_data,
  99. size_t data_length);
  100. /** @brief Function for registering the application callback for event signaling.
  101. *
  102. * The callback will be called by NFC T2T Library to notify the application of relevant
  103. * events. It will be called from the HAL_NFC callback context.
  104. *
  105. * @param[in] callback Function pointer to the callback.
  106. * @param[in] p_context Pointer to a memory area used by the callback for execution (optional).
  107. *
  108. * @retval NRF_SUCCESS If the application callback was registered successfully. If one
  109. * of the arguments was invalid, an error code is returned.
  110. */
  111. ret_code_t nfc_t2t_setup(nfc_t2t_callback_t callback, void * p_context);
  112. /** @brief Function for setting an NFC parameter.
  113. *
  114. * @note Not implemented. For future use.
  115. *
  116. * This function allows to set any parameter defined as available by HAL_NFC.
  117. *
  118. * @param[in] id ID of the parameter to set.
  119. * @param[in] p_data Pointer to a buffer containing the data to set.
  120. * @param[in] data_length Size of the buffer containing the data to set.
  121. *
  122. * @retval NRF_SUCCESS If the parameter was set successfully. If one of the arguments
  123. * was invalid (for example, a wrong data length), an error code
  124. * is returned.
  125. */
  126. ret_code_t nfc_t2t_parameter_set(nfc_t2t_param_id_t id, void * p_data, size_t data_length);
  127. /** @brief Function for querying an NFC parameter value.
  128. *
  129. * @note Not implemented. For future use.
  130. *
  131. * The queried value will be placed into the passed data buffer. If the buffer
  132. * is too small, p_max_data_length will contain the required buffer size. If the
  133. * buffer is big enough, p_max_data_length will contain the actual size of the
  134. * data.
  135. *
  136. * @param[in] id ID of the parameter to query.
  137. * @param[in] p_data Pointer to a buffer receiving the queried data.
  138. * @param[in, out] p_max_data_length Size of the buffer, receives actual size of queried data.
  139. *
  140. * @retval NRF_SUCCESS If the parameter was received successfully. If one of the arguments
  141. * was invalid (for example, the buffer was too small), an error code
  142. * is returned.
  143. */
  144. ret_code_t nfc_t2t_parameter_get(nfc_t2t_param_id_t id, void * p_data, size_t * p_max_data_length);
  145. /** @brief Function for registering the payload to send on reception of a READ request.
  146. *
  147. * The payload is considered to only contain the NDEF message to deliver to a
  148. * reader. The required NDEF TLV will be created implicitly by NFC T2T Library.
  149. *
  150. * The pointer to the payload must stay valid for the duration of the library
  151. * execution, or until it is explicitly released.
  152. *
  153. * If the pointer is not NULL, but the length is zero, the paypload is
  154. * considered to be an empty NDEF message.
  155. *
  156. * If a new payload is registered, the previously registered one is considered
  157. * released.
  158. *
  159. * Passing a NULL pointer releases the current payload without registering a
  160. * new one.
  161. *
  162. * If an invalid size is given (too big), the function returns with an error
  163. * and the currently registered payload is left unchanged.
  164. *
  165. * @note Provided pointer must point to RAM region.
  166. *
  167. * @param[in] p_payload Pointer to the memory area in RAM containing the payload to send.
  168. * @param[in] payload_length Size of the payload in bytes.
  169. *
  170. * @retval NRF_SUCCESS If the operation was successful. If one
  171. * of the arguments was invalid, an error code is returned.
  172. */
  173. ret_code_t nfc_t2t_payload_set(const uint8_t * p_payload, size_t payload_length);
  174. /** @brief Function for registering the raw payload to send on reception of a READ request.
  175. *
  176. * The payload will be delivered directly as-is to the reader, without
  177. * implicitly adding an NDEF TLV container. This can be used if the
  178. * application wants to define the TLVs itself, for example, to provide a different
  179. * memory layout.
  180. *
  181. * The pointer to the payload must stay valid for the duration of the library
  182. * execution, or until it is explicitly released.
  183. *
  184. * If a new payload is registered, the previously registered one is considered
  185. * released.
  186. *
  187. * Passing a NULL pointer releases the current payload, without registering a
  188. * new one.
  189. *
  190. * If an invalid size is given (too big), the function returns with an error
  191. * and the currently registered payload is left unchanged.
  192. *
  193. * @note Provided pointer must points to RAM region.
  194. *
  195. * @param[in] p_payload Pointer to the memory area in RAM containing the payload to send.
  196. * @param[in] payload_length Size of the payload in bytes.
  197. *
  198. * @retval NRF_SUCCESS If the operation was successful. If one
  199. * of the arguments was invalid, an error code is returned.
  200. */
  201. ret_code_t nfc_t2t_payload_raw_set(const uint8_t * p_payload, size_t payload_length);
  202. /** @brief Function for registering the sequence of internal bytes.
  203. *
  204. * This refers to the first 10 bytes of the tag memory. The library will set
  205. * a sensible default for these bytes. The application can use this function
  206. * to override the default.
  207. *
  208. * Passing a NULL pointer reverts back to the default sequence.
  209. * The data will be copied by NFC T2T Library, so the memory does not have to remain valid
  210. * after the function returns.
  211. *
  212. * @note When modifying the internal bytes, remember that they must be consistent
  213. * with the NFC hardware register settings (see @ref nfc_t2t_format_internal).
  214. *
  215. * @param[in] p_data Pointer to the memory area containing the data.
  216. * @param[in] data_length Size of the data in bytes.
  217. *
  218. * @retval NRF_SUCCESS If the operation was successful. If the data was not NULL and the
  219. * data length was not 10, an error code is returned.
  220. */
  221. ret_code_t nfc_t2t_internal_set(const uint8_t * p_data, size_t data_length);
  222. /** @brief Function for activating the NFC frontend.
  223. *
  224. * You must call this function so that events are posted to the application
  225. * callback.
  226. *
  227. * @retval NRF_SUCCESS If the NFC frontend was activated successfully. If the lower layer
  228. * could not be started, an error code is returned.
  229. */
  230. ret_code_t nfc_t2t_emulation_start(void);
  231. /** @brief Function for deactivating the NFC frontend.
  232. *
  233. * After calling this function, no more events will be posted to the
  234. * application callback.
  235. *
  236. * @retval NRF_SUCCESS If the NFC frontend was deactivated successfully. If the lower layer
  237. * could not be stopped, an error code is returned.
  238. */
  239. ret_code_t nfc_t2t_emulation_stop(void);
  240. /** @brief Function for releasing the reference to the application callback.
  241. *
  242. * After calling this function, the passed callback pointer is no longer
  243. * considered valid.
  244. *
  245. * @retval NRF_SUCCESS This function always succeeds.
  246. */
  247. ret_code_t nfc_t2t_done(void);
  248. #ifdef __cplusplus
  249. }
  250. #endif
  251. /** @} */
  252. #endif // NFC_T2T_LIB_H__