rfal_nfcf.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /******************************************************************************
  2. * \attention
  3. *
  4. * <h2><center>&copy; COPYRIGHT 2020 STMicroelectronics</center></h2>
  5. *
  6. * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
  7. * You may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at:
  9. *
  10. * www.st.com/myliberty
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
  15. * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. ******************************************************************************/
  21. /*
  22. * PROJECT: ST25R391x firmware
  23. * Revision:
  24. * LANGUAGE: ISO C99
  25. */
  26. /*! \file rfal_nfcf.h
  27. *
  28. * \author Gustavo Patricio
  29. *
  30. * \brief Implementation of NFC-F Poller (FeliCa PCD) device
  31. *
  32. * The definitions and helpers methods provided by this module are
  33. * aligned with NFC-F (FeliCa - JIS X6319-4)
  34. *
  35. *
  36. * \addtogroup RFAL
  37. * @{
  38. *
  39. * \addtogroup RFAL-AL
  40. * \brief RFAL Abstraction Layer
  41. * @{
  42. *
  43. * \addtogroup NFC-F
  44. * \brief RFAL NFC-F Module
  45. * @{
  46. *
  47. */
  48. #ifndef RFAL_NFCF_H
  49. #define RFAL_NFCF_H
  50. /*
  51. ******************************************************************************
  52. * INCLUDES
  53. ******************************************************************************
  54. */
  55. #include "platform.h"
  56. #include "st_errno.h"
  57. #include "rfal_rf.h"
  58. /*
  59. ******************************************************************************
  60. * GLOBAL DEFINES
  61. ******************************************************************************
  62. */
  63. #define RFAL_NFCF_NFCID2_LEN 8U /*!< NFCID2 (FeliCa IDm) length */
  64. #define RFAL_NFCF_SENSF_RES_LEN_MIN 16U /*!< SENSF_RES minimum length */
  65. #define RFAL_NFCF_SENSF_RES_LEN_MAX 18U /*!< SENSF_RES maximum length */
  66. #define RFAL_NFCF_SENSF_RES_PAD0_LEN 2U /*!< SENSF_RES PAD0 length */
  67. #define RFAL_NFCF_SENSF_RES_PAD1_LEN 2U /*!< SENSF_RES PAD1 length */
  68. #define RFAL_NFCF_SENSF_RES_RD_LEN 2U /*!< SENSF_RES Request Data length */
  69. #define RFAL_NFCF_SENSF_RES_BYTE1 1U /*!< SENSF_RES first byte value */
  70. #define RFAL_NFCF_SENSF_SC_LEN 2U /*!< Felica SENSF_REQ System Code length */
  71. #define RFAL_NFCF_SENSF_PARAMS_SC1_POS 0U /*!< System Code byte1 position in the SENSF_REQ */
  72. #define RFAL_NFCF_SENSF_PARAMS_SC2_POS 1U /*!< System Code byte2 position in the SENSF_REQ */
  73. #define RFAL_NFCF_SENSF_PARAMS_RC_POS 2U /*!< Request Code position in the SENSF_REQ */
  74. #define RFAL_NFCF_SENSF_PARAMS_TSN_POS 3U /*!< Time Slot Number position in the SENSF_REQ */
  75. #define RFAL_NFCF_POLL_MAXCARDS 16U /*!< Max number slots/cards 16 */
  76. #define RFAL_NFCF_CMD_POS 0U /*!< Command/Responce code length */
  77. #define RFAL_NFCF_CMD_LEN 1U /*!< Command/Responce code length */
  78. #define RFAL_NFCF_LENGTH_LEN 1U /*!< LEN field length */
  79. #define RFAL_NFCF_HEADER_LEN (RFAL_NFCF_LENGTH_LEN + RFAL_NFCF_CMD_LEN) /*!< Header length*/
  80. #define RFAL_NFCF_SENSF_NFCID2_BYTE1_POS 0U /*!< NFCID2 byte1 position */
  81. #define RFAL_NFCF_SENSF_NFCID2_BYTE2_POS 1U /*!< NFCID2 byte2 position */
  82. #define RFAL_NFCF_SENSF_NFCID2_PROT_TYPE_LEN 2U /*!< NFCID2 length for byte 1 and byte 2 indicating NFC-DEP or T3T support */
  83. #define RFAL_NFCF_SENSF_NFCID2_BYTE1_NFCDEP 0x01U /*!< NFCID2 byte1 NFC-DEP support Digital 1.0 Table 44 */
  84. #define RFAL_NFCF_SENSF_NFCID2_BYTE2_NFCDEP 0xFEU /*!< NFCID2 byte2 NFC-DEP support Digital 1.0 Table 44 */
  85. #define RFAL_NFCF_SYSTEMCODE 0xFFFFU /*!< SENSF_RES Default System Code Digital 1.0 6.6.1.1 */
  86. #define RFAL_NFCF_BLOCK_LEN 16U /*!< NFCF T3T Block size T3T 1.0 4.1 */
  87. #define RFAL_NFCF_CHECKUPDATE_RES_ST1_POS 9U /*!< Check|Update Res Status Flag 1 position T3T 1.0 Table 8 */
  88. #define RFAL_NFCF_CHECKUPDATE_RES_ST2_POS 10U /*!< Check|Update Res Status Flag 2 position T3T 1.0 Table 8 */
  89. #define RFAL_NFCF_CHECKUPDATE_RES_NOB_POS 11U /*!< Check|Update Res Number of Blocks position T3T 1.0 Table 8 */
  90. #define RFAL_NFCF_STATUS_FLAG_SUCCESS 0x00U /*!< Check response Number of Blocks position T3T 1.0 Table 11 */
  91. #define RFAL_NFCF_STATUS_FLAG_ERROR 0xFFU /*!< Check response Number of Blocks position T3T 1.0 Table 11 */
  92. #define RFAL_NFCF_BLOCKLISTELEM_LEN 0x80U /*!< Block List Element Length bit (2|3 bytes) T3T 1.0 5.6.1 */
  93. #define RFAL_NFCF_SERVICECODE_RDONLY 0x000BU /*!< NDEF Service Code as Read-Only T3T 1.0 7.2.1 */
  94. #define RFAL_NFCF_SERVICECODE_RDWR 0x0009U /*!< NDEF Service Code as Read and Write T3T 1.0 7.2.1 */
  95. /*! NFC-F Felica command set JIS X6319-4 9.1 */
  96. enum
  97. {
  98. RFAL_NFCF_CMD_POLLING = 0x00, /*!< SENSF_REQ (Felica Poll/REQC command to identify a card ) */
  99. RFAL_NFCF_CMD_POLLING_RES = 0x01, /*!< SENSF_RES (Felica Poll/REQC command response ) */
  100. RFAL_NFCF_CMD_REQUEST_SERVICE = 0x02, /*!< verify the existence of Area and Service */
  101. RFAL_NFCF_CMD_REQUEST_RESPONSE = 0x04, /*!< verify the existence of a card */
  102. RFAL_NFCF_CMD_READ_WITHOUT_ENCRYPTION = 0x06, /*!< read Block Data from a Service that requires no authentication */
  103. RFAL_NFCF_CMD_READ_WITHOUT_ENCRYPTION_RES = 0x07, /*!< read Block Data response from a Service with no authentication */
  104. RFAL_NFCF_CMD_WRITE_WITHOUT_ENCRYPTION = 0x08, /*!< write Block Data to a Service that requires no authentication */
  105. RFAL_NFCF_CMD_WRITE_WITHOUT_ENCRYPTION_RES = 0x09, /*!< write Block Data response to a Service with no authentication */
  106. RFAL_NFCF_CMD_REQUEST_SYSTEM_CODE = 0x0c, /*!< acquire the System Code registered to a card */
  107. RFAL_NFCF_CMD_AUTHENTICATION1 = 0x10, /*!< authenticate a card */
  108. RFAL_NFCF_CMD_AUTHENTICATION2 = 0x12, /*!< allow a card to authenticate a Reader/Writer */
  109. RFAL_NFCF_CMD_READ = 0x14, /*!< read Block Data from a Service that requires authentication */
  110. RFAL_NFCF_CMD_WRITE = 0x16, /*!< write Block Data to a Service that requires authentication */
  111. };
  112. /*
  113. ******************************************************************************
  114. * GLOBAL MACROS
  115. ******************************************************************************
  116. */
  117. /*! Checks if the given NFC-F device indicates NFC-DEP support */
  118. #define rfalNfcfIsNfcDepSupported( dev ) ( (((rfalNfcfListenDevice*)(dev))->sensfRes.NFCID2[RFAL_NFCF_SENSF_NFCID2_BYTE1_POS] == RFAL_NFCF_SENSF_NFCID2_BYTE1_NFCDEP) && \
  119. (((rfalNfcfListenDevice*)(dev))->sensfRes.NFCID2[RFAL_NFCF_SENSF_NFCID2_BYTE2_POS] == RFAL_NFCF_SENSF_NFCID2_BYTE2_NFCDEP) )
  120. /*
  121. ******************************************************************************
  122. * GLOBAL TYPES
  123. ******************************************************************************
  124. */
  125. /*! NFC-F SENSF_RES format Digital 1.1 8.6.2 */
  126. typedef struct
  127. {
  128. uint8_t CMD; /*!< Command Code: 01h */
  129. uint8_t NFCID2[RFAL_NFCF_NFCID2_LEN]; /*!< NFCID2 */
  130. uint8_t PAD0[RFAL_NFCF_SENSF_RES_PAD0_LEN]; /*!< PAD0 */
  131. uint8_t PAD1[RFAL_NFCF_SENSF_RES_PAD1_LEN]; /*!< PAD1 */
  132. uint8_t MRTIcheck; /*!< MRTIcheck */
  133. uint8_t MRTIupdate; /*!< MRTIupdate */
  134. uint8_t PAD2; /*!< PAD2 */
  135. uint8_t RD[RFAL_NFCF_SENSF_RES_RD_LEN]; /*!< Request Data */
  136. } rfalNfcfSensfRes;
  137. /*! NFC-F poller device (PCD) struct */
  138. typedef struct
  139. {
  140. uint8_t NFCID2[RFAL_NFCF_NFCID2_LEN]; /*!< NFCID2 */
  141. } rfalNfcfPollDevice;
  142. /*! NFC-F listener device (PICC) struct */
  143. typedef struct
  144. {
  145. uint8_t sensfResLen; /*!< SENF_RES length */
  146. rfalNfcfSensfRes sensfRes; /*!< SENF_RES */
  147. } rfalNfcfListenDevice;
  148. typedef uint16_t rfalNfcfServ; /*!< NFC-F Service Code */
  149. /*! NFC-F Block List Element (2 or 3 bytes element) T3T 1.0 5.6.1 */
  150. typedef struct
  151. {
  152. uint8_t conf; /*!< Access Mode | Serv Code List Order */
  153. uint16_t blockNum; /*!< Block Number */
  154. }rfalNfcfBlockListElem;
  155. /*! Check Update Service list and Block list parameter */
  156. typedef struct
  157. {
  158. uint8_t numServ; /*!< Number of Services */
  159. rfalNfcfServ *servList; /*!< Service Code List */
  160. uint8_t numBlock; /*!< Number of Blocks */
  161. rfalNfcfBlockListElem *blockList; /*!< Block Number List */
  162. }rfalNfcfServBlockListParam;
  163. /*
  164. ******************************************************************************
  165. * GLOBAL FUNCTION PROTOTYPES
  166. ******************************************************************************
  167. */
  168. /*!
  169. *****************************************************************************
  170. * \brief Initialize NFC-F Poller mode
  171. *
  172. * This methods configures RFAL RF layer to perform as a
  173. * NFC-F Poller/RW (FeliCa PCD) including all default timings
  174. *
  175. * \param[in] bitRate : NFC-F bitrate to be initialize (212 or 424)
  176. *
  177. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  178. * \return ERR_PARAM : Incorrect bitrate
  179. * \return ERR_NONE : No error
  180. *****************************************************************************
  181. */
  182. ReturnCode rfalNfcfPollerInitialize( rfalBitRate bitRate );
  183. /*!
  184. *****************************************************************************
  185. * \brief NFC-F Poller Check Presence
  186. *
  187. * This function sends a Poll/SENSF command according to NFC Activity spec
  188. * It detects if a NCF-F device is within range
  189. *
  190. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  191. * \return ERR_PARAM : Invalid parameters
  192. * \return ERR_IO : Generic internal error
  193. * \return ERR_CRC : CRC error detected
  194. * \return ERR_FRAMING : Framing error detected
  195. * \return ERR_PROTO : Protocol error detected
  196. * \return ERR_TIMEOUT : Timeout error, no listener device detected
  197. * \return ERR_NONE : No error and some NFC-F device was detected
  198. *
  199. *****************************************************************************
  200. */
  201. ReturnCode rfalNfcfPollerCheckPresence( void );
  202. /*!
  203. *****************************************************************************
  204. * \brief NFC-F Poller Poll
  205. *
  206. * This function sends to all PICCs in field the POLL command with the given
  207. * number of slots.
  208. *
  209. * \param[in] slots : the number of slots to be performed
  210. * \param[in] sysCode : as given in FeliCa poll command
  211. * \param[in] reqCode : FeliCa communication parameters
  212. * \param[out] cardList : Parameter of type rfalFeliCaPollRes which will hold the cards found
  213. * \param[out] devCnt : actual number of cards found
  214. * \param[out] collisions : number of collisions encountered
  215. *
  216. * \warning the list cardList has to be as big as the number of slots for the Poll
  217. *
  218. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  219. * \return ERR_PARAM : Invalid parameters
  220. * \return ERR_IO : Generic internal error
  221. * \return ERR_CRC : CRC error detected
  222. * \return ERR_FRAMING : Framing error detected
  223. * \return ERR_PROTO : Protocol error detected
  224. * \return ERR_TIMEOUT : Timeout error, no listener device detected
  225. * \return ERR_NONE : No error and some NFC-F device was detected
  226. *
  227. *****************************************************************************
  228. */
  229. ReturnCode rfalNfcfPollerPoll( rfalFeliCaPollSlots slots, uint16_t sysCode, uint8_t reqCode, rfalFeliCaPollRes *cardList, uint8_t *devCnt, uint8_t *collisions );
  230. /*!
  231. *****************************************************************************
  232. * \brief NFC-F Poller Full Collision Resolution
  233. *
  234. * Performs a full Collision resolution as defined in Activity 1.1 9.3.4
  235. *
  236. * \param[in] compMode : compliance mode to be performed
  237. * \param[in] devLimit : device limit value, and size nfcaDevList
  238. * \param[out] nfcfDevList : NFC-F listener devices list
  239. * \param[out] devCnt : Devices found counter
  240. *
  241. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  242. * \return ERR_PARAM : Invalid parameters
  243. * \return ERR_IO : Generic internal error
  244. * \return ERR_NONE : No error
  245. *****************************************************************************
  246. */
  247. ReturnCode rfalNfcfPollerCollisionResolution( rfalComplianceMode compMode, uint8_t devLimit, rfalNfcfListenDevice *nfcfDevList, uint8_t *devCnt );
  248. /*!
  249. *****************************************************************************
  250. * \brief NFC-F Poller Check/Read
  251. *
  252. * It computes a Check / Read command accoring to T3T 1.0 and JIS X6319-4 and
  253. * sends it to PICC. If sucessfully, the rxBuf will contain the the number of
  254. * blocks in the first byte followed by the blocks data.
  255. *
  256. * \param[in] nfcid2 : nfcid2 of the device
  257. * \param[in] servBlock : parameter containing the list of Services and
  258. * Blocks to be addressed by this command
  259. * \param[out] rxBuf : buffer to place check/read data
  260. * \param[in] rxBufLen : size of the rxBuf
  261. * \param[out] rcvdLen : length of data placed in rxBuf
  262. *
  263. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  264. * \return ERR_PARAM : Invalid parameters
  265. * \return ERR_IO : Generic internal error
  266. * \return ERR_REQUEST : The request was executed with error
  267. * \return ERR_NONE : No error
  268. *****************************************************************************
  269. */
  270. ReturnCode rfalNfcfPollerCheck( const uint8_t* nfcid2, const rfalNfcfServBlockListParam *servBlock, uint8_t *rxBuf, uint16_t rxBufLen, uint16_t *rcvdLen );
  271. /*!
  272. *****************************************************************************
  273. * \brief NFC-F Poller Update/Write
  274. *
  275. * It computes a Update / Write command accoring to T3T 1.0 and JIS X6319-4 and
  276. * sends it to PICC.
  277. *
  278. * \param[in] nfcid2 : nfcid2 of the device
  279. * \param[in] servBlock : parameter containing the list of Services and
  280. * Blocks to be addressed by this command
  281. * \param[in] txBuf : buffer where the request will be composed
  282. * \param[in] txBufLen : size of txBuf
  283. * \param[in] blockData : data to written on the given block(s)
  284. * \param[out] rxBuf : buffer to place check/read data
  285. * \param[in] rxBufLen : size of the rxBuf
  286. *
  287. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  288. * \return ERR_PARAM : Invalid parameters
  289. * \return ERR_IO : Generic internal error
  290. * \return ERR_REQUEST : The request was executed with error
  291. * \return ERR_NONE : No error
  292. *****************************************************************************
  293. */
  294. ReturnCode rfalNfcfPollerUpdate( const uint8_t* nfcid2, const rfalNfcfServBlockListParam *servBlock, uint8_t *txBuf, uint16_t txBufLen, const uint8_t *blockData, uint8_t *rxBuf, uint16_t rxBufLen);
  295. /*!
  296. *****************************************************************************
  297. * \brief NFC-F Listener is T3T Request
  298. *
  299. * This method checks if the given data is a valid T3T command (Read or Write)
  300. * and in case a valid request has been received it may output the request's NFCID2
  301. *
  302. * \param[in] buf : buffer holding Initiator's received command
  303. * \param[in] bufLen : length of received command in bytes
  304. * \param[out] nfcid2 : pointer to where the NFCID2 may be outputed,
  305. * nfcid2 has NFCF_SENSF_NFCID2_LEN as length
  306. * Pass NULL if output parameter not desired
  307. *
  308. * \return true : Valid T3T command (Read or Write) received
  309. * \return false : Invalid protocol request
  310. *
  311. *****************************************************************************
  312. */
  313. bool rfalNfcfListenerIsT3TReq( const uint8_t* buf, uint16_t bufLen, uint8_t* nfcid2 );
  314. #endif /* RFAL_NFCF_H */
  315. /**
  316. * @}
  317. *
  318. * @}
  319. *
  320. * @}
  321. */