rfal_nfcf.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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/Response code length */
  77. #define RFAL_NFCF_CMD_LEN 1U /*!< Command/Response 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 \
  81. 0U /*!< NFCID2 byte1 position */
  82. #define RFAL_NFCF_SENSF_NFCID2_BYTE2_POS \
  83. 1U /*!< NFCID2 byte2 position */
  84. #define RFAL_NFCF_SENSF_NFCID2_PROT_TYPE_LEN \
  85. 2U /*!< NFCID2 length for byte 1 and byte 2 indicating NFC-DEP or T3T support */
  86. #define RFAL_NFCF_SENSF_NFCID2_BYTE1_NFCDEP \
  87. 0x01U /*!< NFCID2 byte1 NFC-DEP support Digital 1.0 Table 44 */
  88. #define RFAL_NFCF_SENSF_NFCID2_BYTE2_NFCDEP \
  89. 0xFEU /*!< NFCID2 byte2 NFC-DEP support Digital 1.0 Table 44 */
  90. #define RFAL_NFCF_SYSTEMCODE \
  91. 0xFFFFU /*!< SENSF_RES Default System Code Digital 1.0 6.6.1.1 */
  92. #define RFAL_NFCF_BLOCK_LEN \
  93. 16U /*!< NFCF T3T Block size T3T 1.0 4.1 */
  94. #define RFAL_NFCF_CHECKUPDATE_RES_ST1_POS \
  95. 9U /*!< Check|Update Res Status Flag 1 position T3T 1.0 Table 8 */
  96. #define RFAL_NFCF_CHECKUPDATE_RES_ST2_POS \
  97. 10U /*!< Check|Update Res Status Flag 2 position T3T 1.0 Table 8 */
  98. #define RFAL_NFCF_CHECKUPDATE_RES_NOB_POS \
  99. 11U /*!< Check|Update Res Number of Blocks position T3T 1.0 Table 8 */
  100. #define RFAL_NFCF_STATUS_FLAG_SUCCESS \
  101. 0x00U /*!< Check response Number of Blocks position T3T 1.0 Table 11 */
  102. #define RFAL_NFCF_STATUS_FLAG_ERROR \
  103. 0xFFU /*!< Check response Number of Blocks position T3T 1.0 Table 11 */
  104. #define RFAL_NFCF_BLOCKLISTELEM_LEN \
  105. 0x80U /*!< Block List Element Length bit (2|3 bytes) T3T 1.0 5.6.1 */
  106. #define RFAL_NFCF_SERVICECODE_RDONLY \
  107. 0x000BU /*!< NDEF Service Code as Read-Only T3T 1.0 7.2.1 */
  108. #define RFAL_NFCF_SERVICECODE_RDWR \
  109. 0x0009U /*!< NDEF Service Code as Read and Write T3T 1.0 7.2.1 */
  110. /*! NFC-F Felica command set JIS X6319-4 9.1 */
  111. enum {
  112. RFAL_NFCF_CMD_POLLING =
  113. 0x00, /*!< SENSF_REQ (Felica Poll/REQC command to identify a card ) */
  114. RFAL_NFCF_CMD_POLLING_RES =
  115. 0x01, /*!< SENSF_RES (Felica Poll/REQC command response ) */
  116. RFAL_NFCF_CMD_REQUEST_SERVICE =
  117. 0x02, /*!< verify the existence of Area and Service */
  118. RFAL_NFCF_CMD_REQUEST_RESPONSE =
  119. 0x04, /*!< verify the existence of a card */
  120. RFAL_NFCF_CMD_READ_WITHOUT_ENCRYPTION =
  121. 0x06, /*!< read Block Data from a Service that requires no authentication */
  122. RFAL_NFCF_CMD_READ_WITHOUT_ENCRYPTION_RES =
  123. 0x07, /*!< read Block Data response from a Service with no authentication */
  124. RFAL_NFCF_CMD_WRITE_WITHOUT_ENCRYPTION =
  125. 0x08, /*!< write Block Data to a Service that requires no authentication */
  126. RFAL_NFCF_CMD_WRITE_WITHOUT_ENCRYPTION_RES =
  127. 0x09, /*!< write Block Data response to a Service with no authentication */
  128. RFAL_NFCF_CMD_REQUEST_SYSTEM_CODE =
  129. 0x0c, /*!< acquire the System Code registered to a card */
  130. RFAL_NFCF_CMD_AUTHENTICATION1 =
  131. 0x10, /*!< authenticate a card */
  132. RFAL_NFCF_CMD_AUTHENTICATION2 =
  133. 0x12, /*!< allow a card to authenticate a Reader/Writer */
  134. RFAL_NFCF_CMD_READ =
  135. 0x14, /*!< read Block Data from a Service that requires authentication */
  136. RFAL_NFCF_CMD_WRITE =
  137. 0x16, /*!< write Block Data to a Service that requires authentication */
  138. };
  139. /*
  140. ******************************************************************************
  141. * GLOBAL MACROS
  142. ******************************************************************************
  143. */
  144. /*! Checks if the given NFC-F device indicates NFC-DEP support */
  145. #define rfalNfcfIsNfcDepSupported(dev) \
  146. ((((rfalNfcfListenDevice*)(dev))->sensfRes.NFCID2[RFAL_NFCF_SENSF_NFCID2_BYTE1_POS] == \
  147. RFAL_NFCF_SENSF_NFCID2_BYTE1_NFCDEP) && \
  148. (((rfalNfcfListenDevice*)(dev))->sensfRes.NFCID2[RFAL_NFCF_SENSF_NFCID2_BYTE2_POS] == \
  149. RFAL_NFCF_SENSF_NFCID2_BYTE2_NFCDEP))
  150. /*
  151. ******************************************************************************
  152. * GLOBAL TYPES
  153. ******************************************************************************
  154. */
  155. /*! NFC-F SENSF_RES format Digital 1.1 8.6.2 */
  156. typedef struct {
  157. uint8_t CMD; /*!< Command Code: 01h */
  158. uint8_t NFCID2[RFAL_NFCF_NFCID2_LEN]; /*!< NFCID2 */
  159. uint8_t PAD0[RFAL_NFCF_SENSF_RES_PAD0_LEN]; /*!< PAD0 */
  160. uint8_t PAD1[RFAL_NFCF_SENSF_RES_PAD1_LEN]; /*!< PAD1 */
  161. uint8_t MRTIcheck; /*!< MRTIcheck */
  162. uint8_t MRTIupdate; /*!< MRTIupdate */
  163. uint8_t PAD2; /*!< PAD2 */
  164. uint8_t RD[RFAL_NFCF_SENSF_RES_RD_LEN]; /*!< Request Data */
  165. } rfalNfcfSensfRes;
  166. /*! NFC-F poller device (PCD) struct */
  167. typedef struct {
  168. uint8_t NFCID2[RFAL_NFCF_NFCID2_LEN]; /*!< NFCID2 */
  169. } rfalNfcfPollDevice;
  170. /*! NFC-F listener device (PICC) struct */
  171. typedef struct {
  172. uint8_t sensfResLen; /*!< SENF_RES length */
  173. rfalNfcfSensfRes sensfRes; /*!< SENF_RES */
  174. } rfalNfcfListenDevice;
  175. typedef uint16_t rfalNfcfServ; /*!< NFC-F Service Code */
  176. /*! NFC-F Block List Element (2 or 3 bytes element) T3T 1.0 5.6.1 */
  177. typedef struct {
  178. uint8_t conf; /*!< Access Mode | Serv Code List Order */
  179. uint16_t blockNum; /*!< Block Number */
  180. } rfalNfcfBlockListElem;
  181. /*! Check Update Service list and Block list parameter */
  182. typedef struct {
  183. uint8_t numServ; /*!< Number of Services */
  184. rfalNfcfServ* servList; /*!< Service Code List */
  185. uint8_t numBlock; /*!< Number of Blocks */
  186. rfalNfcfBlockListElem* blockList; /*!< Block Number List */
  187. } rfalNfcfServBlockListParam;
  188. /*
  189. ******************************************************************************
  190. * GLOBAL FUNCTION PROTOTYPES
  191. ******************************************************************************
  192. */
  193. /*!
  194. *****************************************************************************
  195. * \brief Initialize NFC-F Poller mode
  196. *
  197. * This methods configures RFAL RF layer to perform as a
  198. * NFC-F Poller/RW (FeliCa PCD) including all default timings
  199. *
  200. * \param[in] bitRate : NFC-F bitrate to be initialize (212 or 424)
  201. *
  202. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  203. * \return ERR_PARAM : Incorrect bitrate
  204. * \return ERR_NONE : No error
  205. *****************************************************************************
  206. */
  207. ReturnCode rfalNfcfPollerInitialize(rfalBitRate bitRate);
  208. /*!
  209. *****************************************************************************
  210. * \brief NFC-F Poller Check Presence
  211. *
  212. * This function sends a Poll/SENSF command according to NFC Activity spec
  213. * It detects if a NCF-F device is within range
  214. *
  215. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  216. * \return ERR_PARAM : Invalid parameters
  217. * \return ERR_IO : Generic internal error
  218. * \return ERR_CRC : CRC error detected
  219. * \return ERR_FRAMING : Framing error detected
  220. * \return ERR_PROTO : Protocol error detected
  221. * \return ERR_TIMEOUT : Timeout error, no listener device detected
  222. * \return ERR_NONE : No error and some NFC-F device was detected
  223. *
  224. *****************************************************************************
  225. */
  226. ReturnCode rfalNfcfPollerCheckPresence(void);
  227. /*!
  228. *****************************************************************************
  229. * \brief NFC-F Poller Poll
  230. *
  231. * This function sends to all PICCs in field the POLL command with the given
  232. * number of slots.
  233. *
  234. * \param[in] slots : the number of slots to be performed
  235. * \param[in] sysCode : as given in FeliCa poll command
  236. * \param[in] reqCode : FeliCa communication parameters
  237. * \param[out] cardList : Parameter of type rfalFeliCaPollRes which will hold the cards found
  238. * \param[out] devCnt : actual number of cards found
  239. * \param[out] collisions : number of collisions encountered
  240. *
  241. * \warning the list cardList has to be as big as the number of slots for the Poll
  242. *
  243. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  244. * \return ERR_PARAM : Invalid parameters
  245. * \return ERR_IO : Generic internal error
  246. * \return ERR_CRC : CRC error detected
  247. * \return ERR_FRAMING : Framing error detected
  248. * \return ERR_PROTO : Protocol error detected
  249. * \return ERR_TIMEOUT : Timeout error, no listener device detected
  250. * \return ERR_NONE : No error and some NFC-F device was detected
  251. *
  252. *****************************************************************************
  253. */
  254. ReturnCode rfalNfcfPollerPoll(
  255. rfalFeliCaPollSlots slots,
  256. uint16_t sysCode,
  257. uint8_t reqCode,
  258. rfalFeliCaPollRes* cardList,
  259. uint8_t* devCnt,
  260. uint8_t* collisions);
  261. /*!
  262. *****************************************************************************
  263. * \brief NFC-F Poller Full Collision Resolution
  264. *
  265. * Performs a full Collision resolution as defined in Activity 1.1 9.3.4
  266. *
  267. * \param[in] compMode : compliance mode to be performed
  268. * \param[in] devLimit : device limit value, and size nfcaDevList
  269. * \param[out] nfcfDevList : NFC-F listener devices list
  270. * \param[out] devCnt : Devices found counter
  271. *
  272. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  273. * \return ERR_PARAM : Invalid parameters
  274. * \return ERR_IO : Generic internal error
  275. * \return ERR_NONE : No error
  276. *****************************************************************************
  277. */
  278. ReturnCode rfalNfcfPollerCollisionResolution(
  279. rfalComplianceMode compMode,
  280. uint8_t devLimit,
  281. rfalNfcfListenDevice* nfcfDevList,
  282. uint8_t* devCnt);
  283. /*!
  284. *****************************************************************************
  285. * \brief NFC-F Poller Check/Read
  286. *
  287. * It computes a Check / Read command according to T3T 1.0 and JIS X6319-4 and
  288. * sends it to PICC. If successfully, the rxBuf will contain the the number of
  289. * blocks in the first byte followed by the blocks data.
  290. *
  291. * \param[in] nfcid2 : nfcid2 of the device
  292. * \param[in] servBlock : parameter containing the list of Services and
  293. * Blocks to be addressed by this command
  294. * \param[out] rxBuf : buffer to place check/read data
  295. * \param[in] rxBufLen : size of the rxBuf
  296. * \param[out] rcvdLen : length of data placed in rxBuf
  297. *
  298. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  299. * \return ERR_PARAM : Invalid parameters
  300. * \return ERR_IO : Generic internal error
  301. * \return ERR_REQUEST : The request was executed with error
  302. * \return ERR_NONE : No error
  303. *****************************************************************************
  304. */
  305. ReturnCode rfalNfcfPollerCheck(
  306. const uint8_t* nfcid2,
  307. const rfalNfcfServBlockListParam* servBlock,
  308. uint8_t* rxBuf,
  309. uint16_t rxBufLen,
  310. uint16_t* rcvdLen);
  311. /*!
  312. *****************************************************************************
  313. * \brief NFC-F Poller Update/Write
  314. *
  315. * It computes a Update / Write command according to T3T 1.0 and JIS X6319-4 and
  316. * sends it to PICC.
  317. *
  318. * \param[in] nfcid2 : nfcid2 of the device
  319. * \param[in] servBlock : parameter containing the list of Services and
  320. * Blocks to be addressed by this command
  321. * \param[in] txBuf : buffer where the request will be composed
  322. * \param[in] txBufLen : size of txBuf
  323. * \param[in] blockData : data to written on the given block(s)
  324. * \param[out] rxBuf : buffer to place check/read data
  325. * \param[in] rxBufLen : size of the rxBuf
  326. *
  327. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  328. * \return ERR_PARAM : Invalid parameters
  329. * \return ERR_IO : Generic internal error
  330. * \return ERR_REQUEST : The request was executed with error
  331. * \return ERR_NONE : No error
  332. *****************************************************************************
  333. */
  334. ReturnCode rfalNfcfPollerUpdate(
  335. const uint8_t* nfcid2,
  336. const rfalNfcfServBlockListParam* servBlock,
  337. uint8_t* txBuf,
  338. uint16_t txBufLen,
  339. const uint8_t* blockData,
  340. uint8_t* rxBuf,
  341. uint16_t rxBufLen);
  342. /*!
  343. *****************************************************************************
  344. * \brief NFC-F Listener is T3T Request
  345. *
  346. * This method checks if the given data is a valid T3T command (Read or Write)
  347. * and in case a valid request has been received it may output the request's NFCID2
  348. *
  349. * \param[in] buf : buffer holding Initiator's received command
  350. * \param[in] bufLen : length of received command in bytes
  351. * \param[out] nfcid2 : pointer to where the NFCID2 may be outputted,
  352. * nfcid2 has NFCF_SENSF_NFCID2_LEN as length
  353. * Pass NULL if output parameter not desired
  354. *
  355. * \return true : Valid T3T command (Read or Write) received
  356. * \return false : Invalid protocol request
  357. *
  358. *****************************************************************************
  359. */
  360. bool rfalNfcfListenerIsT3TReq(const uint8_t* buf, uint16_t bufLen, uint8_t* nfcid2);
  361. #endif /* RFAL_NFCF_H */
  362. /**
  363. * @}
  364. *
  365. * @}
  366. *
  367. * @}
  368. */