nfc_t2t_lib.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /** @file
  2. *
  3. * @addtogroup nfc_api
  4. *
  5. * @defgroup nfc_library NFC Type 2 Tag
  6. * @ingroup nfc_api
  7. * @brief @tagAPI52 Implementation of NFC Type 2 Tag.
  8. *
  9. * @defgroup nfc_lib NFC Type 2 Tag library
  10. * @{
  11. * @ingroup nfc_library
  12. * @brief @tagAPI52 Type 2 Tag library.
  13. */
  14. #ifndef NFC_LIB_H
  15. #define NFC_LIB_H
  16. #include <string.h>
  17. #define NFC_SIZEOF_INTERNAL_BYTES 10
  18. #define NFC_MAX_PAYLOAD_SIZE 988
  19. #define NFC_MAX_PAYLOAD_SIZE_RAW 1008 /* No NDEF-TLV and no implicit lock bytes at the end */
  20. /** @brief Return values generated by NFC_LIB functions. */
  21. typedef enum
  22. {
  23. NFC_RETVAL_OK,
  24. NFC_RETVAL_ERROR,
  25. NFC_RETVAL_INVALID_STATE,
  26. NFC_RETVAL_INVALID_SIZE,
  27. NFC_RETVAL_INVALID_ARGUMENT
  28. } NfcRetval;
  29. /** @brief Events passed to the callback function. */
  30. typedef enum
  31. {
  32. NFC_EVENT_NONE,
  33. NFC_EVENT_FIELD_ON,
  34. NFC_EVENT_FIELD_OFF,
  35. NFC_EVENT_DATA_READ,
  36. NFC_EVENT_STOPPED
  37. } NfcEvent;
  38. typedef enum
  39. {
  40. NFC_PARAM_TESTING /* Used for unit tests */
  41. } NfcParamId;
  42. /** \brief Callback to pass events from NFCLib to application.
  43. *
  44. * \param context Application context for callback execution.
  45. * \param event The event that occurred.
  46. * \param data Data to send to the application (event specific).
  47. * \param dataLength Length of the data.
  48. */
  49. typedef void (*NfcCallbackFunction) (void *context, NfcEvent event, const char *data, size_t dataLength);
  50. /** \brief Function for registering the application callback for event signaling.
  51. *
  52. * The callback will be called by NFCLib to notify the application of relevant
  53. * events. It will be called from the HAL_NFC callback context.
  54. *
  55. * \param callback Function pointer to the callback.
  56. * \param cbContext Pointer to a memory area used by the callback for execution (optional).
  57. *
  58. * \retval OK If the application callback was registered successfully. If one
  59. * of the arguments was invalid, an error code is returned.
  60. */
  61. NfcRetval nfcSetup (NfcCallbackFunction callback, void *cbContext);
  62. /** \brief Function for setting an NFC parameter.
  63. *
  64. * \note Not implemented. For future use.
  65. *
  66. * This function allows to set any parameter defined as available by HAL_NFC.
  67. *
  68. * \param id ID of the parameter to set.
  69. * \param data Pointer to a buffer containing the data to set.
  70. * \param dataLength Size of the buffer containing the data to set.
  71. *
  72. * \retval OK If the parameter was set successfully. If one of the arguments
  73. * was invalid (for example, a wrong data length), an error code
  74. * is returned.
  75. */
  76. NfcRetval nfcSetParameter (NfcParamId id, void *data, size_t dataLength);
  77. /** \brief Function for querying an NFC parameter value.
  78. *
  79. * \note Not implemented. For future use.
  80. *
  81. * The queried value will be placed into the passed data buffer. If the buffer
  82. * is too small, maxDataLength will contain the required buffer size. If the
  83. * buffer is big enough, maxDataLength will contain the actual size of the
  84. * data.
  85. *
  86. * \param id ID of the parameter to query.
  87. * \param data Pointer to a buffer receiving the queried data.
  88. * \param maxDataLength Size of the buffer, receives actual size of queried data.
  89. *
  90. * \retval OK If the parameter was received successfully. If one of the arguments
  91. * was invalid (for example, the buffer was too small), an error code
  92. * is returned.
  93. */
  94. NfcRetval nfcGetParameter (NfcParamId id, void *data, size_t *maxDataLength);
  95. /** \brief Function for registering the payload to send on reception of a READ request.
  96. *
  97. * The payload is considered to only contain the NDEF message to deliver to a
  98. * reader. The required NDEF TLV will be created implicitly by NFCLib.
  99. *
  100. * The pointer to the payload must stay valid for the duration of the library
  101. * execution, or until it is explicitly released.
  102. *
  103. * If the pointer is not NULL, but the length is zero, the paypload is
  104. * considered to be an empty NDEF message.
  105. *
  106. * If a new payload is registered, the previously registered one is considered
  107. * released.
  108. *
  109. * Passing a NULL pointer releases the current payload without registering a
  110. * new one.
  111. *
  112. * If an invalid size is given (too big), the function returns with an error
  113. * and the currently registered payload is left unchanged.
  114. *
  115. * \param payload Pointer to the memory area containing the payload to send.
  116. * \param payloadLength Size of the payload in bytes.
  117. *
  118. * \retval OK If the operation was successful. If one
  119. * of the arguments was invalid, an error code is returned.
  120. */
  121. NfcRetval nfcSetPayload (const char *payload, size_t payloadLength);
  122. /** \brief Function for registering the raw payload to send on reception of a READ request.
  123. *
  124. * The payload will be delivered directly as-is to the reader, without
  125. * implicitly adding an NDEF TLV container. This can be used if the
  126. * application wants to define the TLVs itself, for example, to provide a different
  127. * memory layout.
  128. *
  129. * The pointer to the payload must stay valid for the duration of the library
  130. * execution, or until it is explicitly released.
  131. *
  132. * If a new payload is registered, the previously registered one is considered
  133. * released.
  134. *
  135. * Passing a NULL pointer releases the current payload, without registering a
  136. * new one.
  137. *
  138. * If an invalid size is given (too big), the function returns with an error
  139. * and the currently registered payload is left unchanged.
  140. *
  141. * \param payload Pointer to the memory area containing the payload to send.
  142. * \param payloadLength Size of the payload in bytes.
  143. *
  144. * \retval OK If the operation was successful. If one
  145. * of the arguments was invalid, an error code is returned.
  146. */
  147. NfcRetval nfcSetPayloadRaw (const char *payload, size_t payloadLength);
  148. /** \brief Function for registering the sequence of internal bytes.
  149. *
  150. * This refers to the first 10 bytes of the tag memory. The library will set
  151. * a sensible default for these bytes. The application can use this function
  152. * to override the default.
  153. *
  154. * Passing a NULL pointer reverts back to the default sequence.
  155. * The data will be copied by NFCLib, so the memory does not have to remain valid
  156. * after the function returns.
  157. *
  158. * \note When modifying the internal bytes, remember that they must be consistent
  159. * with the NFC hardware register settings (see @ref nfc_t2t_format_internal).
  160. *
  161. * \param data Pointer to the memory area containing the data.
  162. * \param dataLength Size of the data in bytes.
  163. *
  164. * \retval OK If the operation was successful. If the data was not NULL and the
  165. * data length was not 10, an error code is returned.
  166. */
  167. NfcRetval nfcSetInternal (const char *data, size_t dataLength);
  168. /** \brief Function for activating the NFC frontend.
  169. *
  170. * You must call this function so that events are posted to the application
  171. * callback.
  172. *
  173. * \retval OK If the NFC frontend was activated successfully. If the lower layer
  174. * could not be started, an error code is returned.
  175. */
  176. NfcRetval nfcStartEmulation (void);
  177. /** \brief Function for deactivating the NFC frontend.
  178. *
  179. * After calling this function, no more events will be posted to the
  180. * application callback.
  181. *
  182. * \retval OK If the NFC frontend was deactivated successfully. If the lower layer
  183. * could not be stopped, an error code is returned.
  184. */
  185. NfcRetval nfcStopEmulation (void);
  186. /** \brief Function for releasing the reference to the application callback.
  187. *
  188. * After calling this function, the passed callback pointer is no longer
  189. * considered valid.
  190. *
  191. * \retval OK This function always succeeds.
  192. */
  193. NfcRetval nfcDone (void);
  194. #endif /* NFC_LIB_H */
  195. /** @} */