rfal_nfca.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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_nfca.h
  27. *
  28. * \author Gustavo Patricio
  29. *
  30. * \brief Provides several NFC-A convenience methods and definitions
  31. *
  32. * It provides a Poller (ISO14443A PCD) interface and as well as
  33. * some NFC-A Listener (ISO14443A PICC) helpers.
  34. *
  35. * The definitions and helpers methods provided by this module are only
  36. * up to ISO14443-3 layer
  37. *
  38. *
  39. * An usage example is provided here: \ref exampleRfalNfca.c
  40. * \example exampleRfalNfca.c
  41. *
  42. *
  43. * \addtogroup RFAL
  44. * @{
  45. *
  46. * \addtogroup RFAL-AL
  47. * \brief RFAL Abstraction Layer
  48. * @{
  49. *
  50. * \addtogroup NFC-A
  51. * \brief RFAL NFC-A Module
  52. * @{
  53. *
  54. */
  55. #ifndef RFAL_NFCA_H
  56. #define RFAL_NFCA_H
  57. /*
  58. ******************************************************************************
  59. * INCLUDES
  60. ******************************************************************************
  61. */
  62. #include "../platform.h"
  63. #include "../st_errno.h"
  64. #include "rfal_rf.h"
  65. #include "rfal_t1t.h"
  66. /*
  67. ******************************************************************************
  68. * GLOBAL DEFINES
  69. ******************************************************************************
  70. */
  71. #define RFAL_NFCA_CASCADE_1_UID_LEN \
  72. 4U /*!< UID length of cascade level 1 only tag */
  73. #define RFAL_NFCA_CASCADE_2_UID_LEN \
  74. 7U /*!< UID length of cascade level 2 only tag */
  75. #define RFAL_NFCA_CASCADE_3_UID_LEN \
  76. 10U /*!< UID length of cascade level 3 only tag */
  77. #define RFAL_NFCA_SENS_RES_PLATFORM_MASK \
  78. 0x0FU /*!< SENS_RES (ATQA) platform configuration mask Digital 1.1 Table 10 */
  79. #define RFAL_NFCA_SENS_RES_PLATFORM_T1T \
  80. 0x0CU /*!< SENS_RES (ATQA) T1T platform configuration Digital 1.1 Table 10 */
  81. #define RFAL_NFCA_SEL_RES_CONF_MASK \
  82. 0x60U /*!< SEL_RES (SAK) platform configuration mask Digital 1.1 Table 19 */
  83. #define RFAL_NFCA_SEL_RES_CONF_T2T \
  84. 0x00U /*!< SEL_RES (SAK) T2T configuration Digital 1.1 Table 19 */
  85. #define RFAL_NFCA_SEL_RES_CONF_T4T \
  86. 0x20U /*!< SEL_RES (SAK) T4T configuration Digital 1.1 Table 19 */
  87. #define RFAL_NFCA_SEL_RES_CONF_NFCDEP \
  88. 0x40U /*!< SEL_RES (SAK) NFC-DEP configuration Digital 1.1 Table 19 */
  89. #define RFAL_NFCA_SEL_RES_CONF_T4T_NFCDEP \
  90. 0x60U /*!< SEL_RES (SAK) T4T and NFC-DEP configuration Digital 1.1 Table 19 */
  91. /*! NFC-A minimum FDT(listen) = ((n * 128 + (84)) / fc) with n_min = 9 Digital 1.1 6.10.1
  92. * = (1236)/fc
  93. * Relax with 3etu: (3*128)/fc as with multiple NFC-A cards, response may take longer (JCOP cards)
  94. * = (1236 + 384)/fc = 1620 / fc */
  95. #define RFAL_NFCA_FDTMIN 1620U
  96. /*
  97. ******************************************************************************
  98. * GLOBAL MACROS
  99. ******************************************************************************
  100. */
  101. /*! Checks if device is a T1T given its SENS_RES */
  102. #define rfalNfcaIsSensResT1T(sensRes) \
  103. ((((rfalNfcaSensRes*)(sensRes))->platformInfo & RFAL_NFCA_SENS_RES_PLATFORM_MASK) == \
  104. RFAL_NFCA_SENS_RES_PLATFORM_T1T)
  105. /*! Checks if device is a T2T given its SENS_RES */
  106. #define rfalNfcaIsSelResT2T(selRes) \
  107. ((((rfalNfcaSelRes*)(selRes))->sak & RFAL_NFCA_SEL_RES_CONF_MASK) == \
  108. RFAL_NFCA_SEL_RES_CONF_T2T)
  109. /*! Checks if device is a T4T given its SENS_RES */
  110. #define rfalNfcaIsSelResT4T(selRes) \
  111. ((((rfalNfcaSelRes*)(selRes))->sak & RFAL_NFCA_SEL_RES_CONF_MASK) == \
  112. RFAL_NFCA_SEL_RES_CONF_T4T)
  113. /*! Checks if device supports NFC-DEP protocol given its SENS_RES */
  114. #define rfalNfcaIsSelResNFCDEP(selRes) \
  115. ((((rfalNfcaSelRes*)(selRes))->sak & RFAL_NFCA_SEL_RES_CONF_MASK) == \
  116. RFAL_NFCA_SEL_RES_CONF_NFCDEP)
  117. /*! Checks if device supports ISO-DEP and NFC-DEP protocol given its SENS_RES */
  118. #define rfalNfcaIsSelResT4TNFCDEP(selRes) \
  119. ((((rfalNfcaSelRes*)(selRes))->sak & RFAL_NFCA_SEL_RES_CONF_MASK) == \
  120. RFAL_NFCA_SEL_RES_CONF_T4T_NFCDEP)
  121. /*! Checks if a NFC-A listener device supports multiple protocols (ISO-DEP and NFC-DEP) */
  122. #define rfalNfcaLisDevIsMultiProto(lisDev) \
  123. (((rfalNfcaListenDevice*)(lisDev))->type == RFAL_NFCA_T4T_NFCDEP)
  124. /*
  125. ******************************************************************************
  126. * GLOBAL TYPES
  127. ******************************************************************************
  128. */
  129. /*! NFC-A Listen device types */
  130. typedef enum {
  131. RFAL_NFCA_T1T =
  132. 0x01, /* Device configured for T1T Digital 1.1 Table 9 */
  133. RFAL_NFCA_T2T =
  134. 0x00, /* Device configured for T2T Digital 1.1 Table 19 */
  135. RFAL_NFCA_T4T =
  136. 0x20, /* Device configured for T4T Digital 1.1 Table 19 */
  137. RFAL_NFCA_NFCDEP =
  138. 0x40, /* Device configured for NFC-DEP Digital 1.1 Table 19 */
  139. RFAL_NFCA_T4T_NFCDEP =
  140. 0x60 /* Device configured for NFC-DEP and T4T Digital 1.1 Table 19 */
  141. } rfalNfcaListenDeviceType;
  142. /*! SENS_RES (ATQA) format Digital 1.1 6.6.3 & Table 7 */
  143. typedef struct {
  144. uint8_t
  145. anticollisionInfo; /*!< SENS_RES Anticollision Information */
  146. uint8_t
  147. platformInfo; /*!< SENS_RES Platform Information */
  148. } rfalNfcaSensRes;
  149. /*! SDD_REQ (Anticollision) format Digital 1.1 6.7.1 & Table 11 */
  150. typedef struct {
  151. uint8_t
  152. selCmd; /*!< SDD_REQ SEL_CMD: cascade Level */
  153. uint8_t
  154. selPar; /*!< SDD_REQ SEL_PAR: Byte Count[4b] | Bit Count[4b] (NVB: Number of Valid Bits)*/
  155. } rfalNfcaSddReq;
  156. /*! SDD_RES (UID CLn) format Digital 1.1 6.7.2 & Table 15 */
  157. typedef struct {
  158. uint8_t nfcid1
  159. [RFAL_NFCA_CASCADE_1_UID_LEN]; /*!< NFCID1 cascade level NFCID */
  160. uint8_t bcc; /*!< BCC Exclusive-OR over first 4 bytes of SDD_RES */
  161. } rfalNfcaSddRes;
  162. /*! SEL_REQ (Select) format Digital 1.1 6.8.1 & Table 17 */
  163. typedef struct {
  164. uint8_t
  165. selCmd; /*!< SDD_REQ SEL_CMD: cascade Level */
  166. uint8_t
  167. selPar; /*!< SDD_REQ SEL_PAR: Byte Count[4b] | Bit Count[4b] (NVB: Number of Valid Bits)*/
  168. uint8_t nfcid1
  169. [RFAL_NFCA_CASCADE_1_UID_LEN]; /*!< NFCID1 data */
  170. uint8_t bcc; /*!< Checksum calculated as exclusive-OR over the 4 bytes of NFCID1 CLn */
  171. } rfalNfcaSelReq;
  172. /*! SEL_RES (SAK) format Digital 1.1 6.8.2 & Table 19 */
  173. typedef struct {
  174. uint8_t sak; /*!< Select Acknowledge */
  175. } rfalNfcaSelRes;
  176. /*! NFC-A listener device (PICC) struct */
  177. typedef struct {
  178. rfalNfcaListenDeviceType
  179. type; /*!< NFC-A Listen device type */
  180. rfalNfcaSensRes
  181. sensRes; /*!< SENS_RES (ATQA) */
  182. rfalNfcaSelRes
  183. selRes; /*!< SEL_RES (SAK) */
  184. uint8_t
  185. nfcId1Len; /*!< NFCID1 Length */
  186. uint8_t nfcId1
  187. [RFAL_NFCA_CASCADE_3_UID_LEN]; /*!< NFCID1 (UID) */
  188. #ifdef RFAL_FEATURE_T1T
  189. rfalT1TRidRes
  190. ridRes; /*!< RID_RES */
  191. #endif /* RFAL_FEATURE_T1T */
  192. bool isSleep; /*!< Device sleeping flag */
  193. } rfalNfcaListenDevice;
  194. /*
  195. ******************************************************************************
  196. * GLOBAL FUNCTION PROTOTYPES
  197. ******************************************************************************
  198. */
  199. /*!
  200. *****************************************************************************
  201. * \brief Initialize NFC-A Poller mode
  202. *
  203. * This methods configures RFAL RF layer to perform as a
  204. * NFC-A Poller/RW (ISO14443A PCD) including all default timings and bit rate
  205. * to 106 kbps
  206. *
  207. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  208. * \return ERR_NONE : No error
  209. *****************************************************************************
  210. */
  211. ReturnCode rfalNfcaPollerInitialize(void);
  212. /*!
  213. *****************************************************************************
  214. * \brief NFC-A Poller Check Presence
  215. *
  216. * This method checks if a NFC-A Listen device (PICC) is present on the field
  217. * by sending an ALL_REQ (WUPA) or SENS_REQ (REQA)
  218. *
  219. * \param[in] cmd : Indicate if to send an ALL_REQ or a SENS_REQ
  220. * \param[out] sensRes : If received, the SENS_RES
  221. *
  222. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  223. * \return ERR_PARAM : Invalid parameters
  224. * \return ERR_IO : Generic internal error
  225. * \return ERR_RF_COLLISION : Collision detected one or more device in the field
  226. * \return ERR_PAR : Parity error detected, one or more device in the field
  227. * \return ERR_CRC : CRC error detected, one or more device in the field
  228. * \return ERR_FRAMING : Framing error detected, one or more device in the field
  229. * \return ERR_PROTO : Protocol error detected, one or more device in the field
  230. * \return ERR_TIMEOUT : Timeout error, no listener device detected
  231. * \return ERR_NONE : No error, one or more device in the field
  232. *****************************************************************************
  233. */
  234. ReturnCode rfalNfcaPollerCheckPresence(rfal14443AShortFrameCmd cmd, rfalNfcaSensRes* sensRes);
  235. /*!
  236. *****************************************************************************
  237. * \brief NFC-A Poller Select
  238. *
  239. * This method selects a NFC-A Listener device (PICC)
  240. *
  241. * \param[in] nfcid1 : Listener device NFCID1 to be selected
  242. * \param[in] nfcidLen : Length of the NFCID1 to be selected
  243. * \param[out] selRes : pointer to place the SEL_RES
  244. *
  245. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  246. * \return ERR_PARAM : Invalid parameters
  247. * \return ERR_IO : Generic internal error
  248. * \return ERR_TIMEOUT : Timeout error
  249. * \return ERR_PAR : Parity error detected
  250. * \return ERR_CRC : CRC error detected
  251. * \return ERR_FRAMING : Framing error detected
  252. * \return ERR_PROTO : Protocol error detected
  253. * \return ERR_NONE : No error, SEL_RES received
  254. *****************************************************************************
  255. */
  256. ReturnCode rfalNfcaPollerSelect(const uint8_t* nfcid1, uint8_t nfcidLen, rfalNfcaSelRes* selRes);
  257. /*!
  258. *****************************************************************************
  259. * \brief NFC-A Poller Sleep
  260. *
  261. * This method sends a SLP_REQ (HLTA)
  262. * No response is expected afterwards Digital 1.1 6.9.2.1
  263. *
  264. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  265. * \return ERR_PARAM : Invalid parameters
  266. * \return ERR_IO : Generic internal error
  267. * \return ERR_NONE : No error
  268. *****************************************************************************
  269. */
  270. ReturnCode rfalNfcaPollerSleep(void);
  271. /*!
  272. *****************************************************************************
  273. * \brief NFC-A Technology Detection
  274. *
  275. * This method performs NFC-A Technology Detection as defined in the spec
  276. * given in the compliance mode
  277. *
  278. * \param[in] compMode : compliance mode to be performed
  279. * \param[out] sensRes : location to store the SENS_RES, if received
  280. *
  281. * When compMode is set to ISO compliance a SLP_REQ (HLTA) is not sent
  282. * after detection. When set to EMV a ALL_REQ (WUPA) is sent instead of
  283. * a SENS_REQ (REQA)
  284. *
  285. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  286. * \return ERR_PARAM : Invalid parameters
  287. * \return ERR_IO : Generic internal error
  288. * \return ERR_NONE : No error, one or more device in the field
  289. *****************************************************************************
  290. */
  291. ReturnCode
  292. rfalNfcaPollerTechnologyDetection(rfalComplianceMode compMode, rfalNfcaSensRes* sensRes);
  293. /*!
  294. *****************************************************************************
  295. * \brief NFC-A Poller Collision Resolution
  296. *
  297. * Collision resolution for one NFC-A Listener device/card (PICC) as
  298. * defined in Activity 2.1 9.3.4
  299. *
  300. * This method executes anti collision loop and select the device with higher NFCID1
  301. *
  302. * When devLimit = 0 it is configured to perform collision detection only. Once a collision
  303. * is detected the collision resolution is aborted immediately. If only one device is found
  304. * with no collisions, it will properly resolved.
  305. *
  306. * \param[in] devLimit : device limit value (CON_DEVICES_LIMIT)
  307. * \param[out] collPending : pointer to collision pending flag (INT_COLL_PEND)
  308. * \param[out] selRes : location to store the last Select Response from listener device (PICC)
  309. * \param[out] nfcId1 : location to store the NFCID1 (UID), ensure RFAL_NFCA_CASCADE_3_UID_LEN
  310. * \param[out] nfcId1Len : pointer to length of NFCID1 (UID)
  311. *
  312. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  313. * \return ERR_PARAM : Invalid parameters
  314. * \return ERR_IO : Generic internal error
  315. * \return ERR_PROTO : Card length invalid
  316. * \return ERR_IGNORE : conDevLimit is 0 and there is a collision
  317. * \return ERR_NONE : No error
  318. *****************************************************************************
  319. */
  320. ReturnCode rfalNfcaPollerSingleCollisionResolution(
  321. uint8_t devLimit,
  322. bool* collPending,
  323. rfalNfcaSelRes* selRes,
  324. uint8_t* nfcId1,
  325. uint8_t* nfcId1Len);
  326. /*!
  327. *****************************************************************************
  328. * \brief NFC-A Poller Full Collision Resolution
  329. *
  330. * Performs a full Collision resolution as defined in Activity 2.1 9.3.4
  331. *
  332. * \param[in] compMode : compliance mode to be performed
  333. * \param[in] devLimit : device limit value, and size nfcaDevList
  334. * \param[out] nfcaDevList : NFC-A listener device info
  335. * \param[out] devCnt : Devices found counter
  336. *
  337. * When compMode is set to ISO compliance it assumes that the device is
  338. * not sleeping and therefore no ALL_REQ (WUPA) is sent at the beginning.
  339. * When compMode is set to NFC compliance an additional ALL_REQ (WUPA) is sent
  340. * at the beginning.
  341. *
  342. *
  343. * When devLimit = 0 it is configured to perform collision detection only. Once a collision
  344. * is detected the collision resolution is aborted immediately. If only one device is found
  345. * with no collisions, it will properly resolved.
  346. *
  347. *
  348. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  349. * \return ERR_PARAM : Invalid parameters
  350. * \return ERR_IO : Generic internal error
  351. * \return ERR_NONE : No error
  352. *****************************************************************************
  353. */
  354. ReturnCode rfalNfcaPollerFullCollisionResolution(
  355. rfalComplianceMode compMode,
  356. uint8_t devLimit,
  357. rfalNfcaListenDevice* nfcaDevList,
  358. uint8_t* devCnt);
  359. /*!
  360. *****************************************************************************
  361. * \brief NFC-A Poller Full Collision Resolution with Sleep
  362. *
  363. * Performs a full Collision resolution similar to rfalNfcaPollerFullCollisionResolution
  364. * but an additional SLP_REQ (HLTA) -> SENS_RES (REQA) is sent regardless if there
  365. * was a collision.
  366. * This proprietary behaviour ensures proper activation of certain devices that suffer
  367. * from influence of Type B commands as foreseen in ISO14443-3 5.2.3 or were somehow
  368. * not detected by the first round of collision resolution
  369. *
  370. * \param[in] devLimit : device limit value, and size nfcaDevList
  371. * \param[out] nfcaDevList : NFC-A listener device info
  372. * \param[out] devCnt : Devices found counter
  373. *
  374. *
  375. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  376. * \return ERR_PARAM : Invalid parameters
  377. * \return ERR_IO : Generic internal error
  378. * \return ERR_NONE : No error
  379. *****************************************************************************
  380. */
  381. ReturnCode rfalNfcaPollerSleepFullCollisionResolution(
  382. uint8_t devLimit,
  383. rfalNfcaListenDevice* nfcaDevList,
  384. uint8_t* devCnt);
  385. /*!
  386. *****************************************************************************
  387. * \brief NFC-A Poller Start Full Collision Resolution
  388. *
  389. * This method starts the full Collision resolution as defined
  390. * in Activity 1.0 or 1.1 9.3.4
  391. *
  392. * \param[in] compMode : compliance mode to be performed
  393. * \param[in] devLimit : device limit value, and size nfcaDevList
  394. * \param[out] nfcaDevList : NFC-A listener device info
  395. * \param[out] devCnt : Devices found counter
  396. *
  397. * When compMode is set to ISO compliance it assumes that the device is
  398. * not sleeping and therefore no ALL_REQ (WUPA) is sent at the beginning.
  399. * When compMode is set to NFC compliance an additional ALL_REQ (WUPA) is sent at
  400. * the beginning.
  401. *
  402. *
  403. * When devLimit = 0 it is configured to perform collision detection only. Once a collision
  404. * is detected the collision resolution is aborted immediately. If only one device is found
  405. * with no collisions, it will properly resolved.
  406. *
  407. *
  408. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  409. * \return ERR_PARAM : Invalid parameters
  410. * \return ERR_IO : Generic internal error
  411. * \return ERR_NONE : No error
  412. *****************************************************************************
  413. */
  414. ReturnCode rfalNfcaPollerStartFullCollisionResolution(
  415. rfalComplianceMode compMode,
  416. uint8_t devLimit,
  417. rfalNfcaListenDevice* nfcaDevList,
  418. uint8_t* devCnt);
  419. /*!
  420. *****************************************************************************
  421. * \brief NFC-A Get Full Collision Resolution Status
  422. *
  423. * Returns the Collision Resolution status
  424. *
  425. * \return ERR_BUSY : Operation is ongoing
  426. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  427. * \return ERR_PARAM : Invalid parameters
  428. * \return ERR_IO : Generic internal error
  429. * \return ERR_TIMEOUT : Timeout error
  430. * \return ERR_PAR : Parity error detected
  431. * \return ERR_CRC : CRC error detected
  432. * \return ERR_FRAMING : Framing error detected
  433. * \return ERR_PROTO : Protocol error detected
  434. * \return ERR_NONE : No error, activation successful
  435. *****************************************************************************
  436. */
  437. ReturnCode rfalNfcaPollerGetFullCollisionResolutionStatus(void);
  438. /*!
  439. *****************************************************************************
  440. * \brief NFC-A Listener is SLP_REQ
  441. *
  442. * Checks if the given buffer contains valid NFC-A SLP_REQ (HALT)
  443. *
  444. * \param[in] buf: buffer containing data
  445. * \param[in] bufLen: length of the data in buffer to be checked
  446. *
  447. * \return true if data in buf contains a SLP_REQ ; false otherwise
  448. *****************************************************************************
  449. */
  450. bool rfalNfcaListenerIsSleepReq(const uint8_t* buf, uint16_t bufLen);
  451. #endif /* RFAL_NFCA_H */
  452. /**
  453. * @}
  454. *
  455. * @}
  456. *
  457. * @}
  458. */