rfal_nfca.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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.c
  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. /*
  40. ******************************************************************************
  41. * INCLUDES
  42. ******************************************************************************
  43. */
  44. #include "rfal_nfca.h"
  45. #include "utils.h"
  46. /*
  47. ******************************************************************************
  48. * ENABLE SWITCH
  49. ******************************************************************************
  50. */
  51. #ifndef RFAL_FEATURE_NFCA
  52. #define RFAL_FEATURE_NFCA false /* NFC-A module configuration missing. Disabled by default */
  53. #endif
  54. #if RFAL_FEATURE_NFCA
  55. /*
  56. ******************************************************************************
  57. * GLOBAL DEFINES
  58. ******************************************************************************
  59. */
  60. #define RFAL_NFCA_SLP_FWT \
  61. rfalConvMsTo1fc(1) /*!< Check 1ms for any modulation ISO14443-3 6.4.3 */
  62. #define RFAL_NFCA_SLP_CMD 0x50U /*!< SLP cmd (byte1) Digital 1.1 6.9.1 & Table 20 */
  63. #define RFAL_NFCA_SLP_BYTE2 0x00U /*!< SLP byte2 Digital 1.1 6.9.1 & Table 20 */
  64. #define RFAL_NFCA_SLP_CMD_POS 0U /*!< SLP cmd position Digital 1.1 6.9.1 & Table 20 */
  65. #define RFAL_NFCA_SLP_BYTE2_POS 1U /*!< SLP byte2 position Digital 1.1 6.9.1 & Table 20 */
  66. #define RFAL_NFCA_SDD_CT 0x88U /*!< Cascade Tag value Digital 1.1 6.7.2 */
  67. #define RFAL_NFCA_SDD_CT_LEN 1U /*!< Cascade Tag length */
  68. #define RFAL_NFCA_SLP_REQ_LEN 2U /*!< SLP_REQ length */
  69. #define RFAL_NFCA_SEL_CMD_LEN 1U /*!< SEL_CMD length */
  70. #define RFAL_NFCA_SEL_PAR_LEN 1U /*!< SEL_PAR length */
  71. #define RFAL_NFCA_SEL_SELPAR \
  72. rfalNfcaSelPar(7U, 0U) /*!< SEL_PAR on Select is always with 4 data/nfcid */
  73. #define RFAL_NFCA_BCC_LEN 1U /*!< BCC length */
  74. #define RFAL_NFCA_SDD_REQ_LEN \
  75. (RFAL_NFCA_SEL_CMD_LEN + RFAL_NFCA_SEL_PAR_LEN) /*!< SDD_REQ length */
  76. #define RFAL_NFCA_SDD_RES_LEN \
  77. (RFAL_NFCA_CASCADE_1_UID_LEN + RFAL_NFCA_BCC_LEN) /*!< SDD_RES length */
  78. #define RFAL_NFCA_T_RETRANS 5U /*!< t RETRANSMISSION [3, 33]ms EMVCo 2.6 A.5 */
  79. #define RFAL_NFCA_N_RETRANS 2U /*!< Number of retries EMVCo 2.6 9.6.1.3 */
  80. /*! SDD_REQ (Select) Cascade Levels */
  81. enum {
  82. RFAL_NFCA_SEL_CASCADE_L1 = 0, /*!< SDD_REQ Cascade Level 1 */
  83. RFAL_NFCA_SEL_CASCADE_L2 = 1, /*!< SDD_REQ Cascade Level 2 */
  84. RFAL_NFCA_SEL_CASCADE_L3 = 2 /*!< SDD_REQ Cascade Level 3 */
  85. };
  86. /*! SDD_REQ (Select) request Cascade Level command Digital 1.1 Table 15 */
  87. enum {
  88. RFAL_NFCA_CMD_SEL_CL1 = 0x93, /*!< SDD_REQ command Cascade Level 1 */
  89. RFAL_NFCA_CMD_SEL_CL2 = 0x95, /*!< SDD_REQ command Cascade Level 2 */
  90. RFAL_NFCA_CMD_SEL_CL3 = 0x97, /*!< SDD_REQ command Cascade Level 3 */
  91. };
  92. /*
  93. ******************************************************************************
  94. * GLOBAL MACROS
  95. ******************************************************************************
  96. */
  97. #define rfalNfcaSelPar(nBy, nbi) \
  98. (uint8_t)( \
  99. (((nBy) << 4U) & 0xF0U) | \
  100. ((nbi)&0x0FU)) /*!< Calculates SEL_PAR with the bytes/bits to be sent */
  101. #define rfalNfcaCLn2SELCMD(cl) \
  102. (uint8_t)( \
  103. (uint8_t)(RFAL_NFCA_CMD_SEL_CL1) + \
  104. (2U * (cl))) /*!< Calculates SEL_CMD with the given cascade level */
  105. #define rfalNfcaNfcidLen2CL(len) \
  106. ((len) / 5U) /*!< Calculates cascade level by the NFCID length */
  107. #define rfalNfcaRunBlocking(e, fn) \
  108. do { \
  109. (e) = (fn); \
  110. rfalWorker(); \
  111. } while((e) == ERR_BUSY) /*!< Macro used for the blocking methods */
  112. /*
  113. ******************************************************************************
  114. * GLOBAL TYPES
  115. ******************************************************************************
  116. */
  117. /*! Colission Resolution states */
  118. typedef enum {
  119. RFAL_NFCA_CR_IDLE, /*!< IDLE state */
  120. RFAL_NFCA_CR_CL, /*!< New Cascading Level state */
  121. RFAL_NFCA_CR_SDD, /*!< Perform anticollsion state */
  122. RFAL_NFCA_CR_SEL, /*!< Perform CL Selection state */
  123. RFAL_NFCA_CR_DONE /*!< Collision Resolution done state */
  124. } colResState;
  125. /*! Colission Resolution context */
  126. typedef struct {
  127. uint8_t devLimit; /*!< Device limit to be used */
  128. rfalComplianceMode compMode; /*!< Compliancy mode to be used */
  129. rfalNfcaListenDevice*
  130. nfcaDevList; /*!< Location of the device list */
  131. uint8_t* devCnt; /*!< Location of the device counter */
  132. bool collPending; /*!< Collision pending flag */
  133. bool* collPend; /*!< Location of collision pending flag (Single CR) */
  134. rfalNfcaSelReq selReq; /*!< SelReqused during anticollision (Single CR) */
  135. rfalNfcaSelRes* selRes; /*!< Location to place of the SEL_RES(SAK) (Single CR) */
  136. uint8_t* nfcId1; /*!< Location to place the NFCID1 (Single CR) */
  137. uint8_t* nfcId1Len; /*!< Location to place the NFCID1 length (Single CR) */
  138. uint8_t cascadeLv; /*!< Current Cascading Level (Single CR) */
  139. colResState state; /*!< Single Collision Resolution state (Single CR) */
  140. uint8_t bytesTxRx; /*!< TxRx bytes used during anticollision loop (Single CR) */
  141. uint8_t bitsTxRx; /*!< TxRx bits used during anticollision loop (Single CR) */
  142. uint16_t rxLen;
  143. uint32_t tmrFDT; /*!< FDT timer used between SED_REQs (Single CR) */
  144. uint8_t retries; /*!< Retries to be performed upon a timeout error (Single CR)*/
  145. uint8_t backtrackCnt; /*!< Backtrack retries (Single CR) */
  146. bool doBacktrack; /*!< Backtrack flag (Single CR) */
  147. } colResParams;
  148. /*! RFAL NFC-A instance */
  149. typedef struct {
  150. colResParams CR; /*!< Collision Resolution context */
  151. } rfalNfca;
  152. /*! SLP_REQ (HLTA) format Digital 1.1 6.9.1 & Table 20 */
  153. typedef struct {
  154. uint8_t frame[RFAL_NFCA_SLP_REQ_LEN]; /*!< SLP: 0x50 0x00 */
  155. } rfalNfcaSlpReq;
  156. /*
  157. ******************************************************************************
  158. * LOCAL VARIABLES
  159. ******************************************************************************
  160. */
  161. static rfalNfca gNfca; /*!< RFAL NFC-A instance */
  162. /*
  163. ******************************************************************************
  164. * LOCAL FUNCTION PROTOTYPES
  165. ******************************************************************************
  166. */
  167. static uint8_t rfalNfcaCalculateBcc(const uint8_t* buf, uint8_t bufLen);
  168. static ReturnCode rfalNfcaPollerStartSingleCollisionResolution(
  169. uint8_t devLimit,
  170. bool* collPending,
  171. rfalNfcaSelRes* selRes,
  172. uint8_t* nfcId1,
  173. uint8_t* nfcId1Len);
  174. static ReturnCode rfalNfcaPollerGetSingleCollisionResolutionStatus(void);
  175. /*
  176. ******************************************************************************
  177. * LOCAL FUNCTIONS
  178. ******************************************************************************
  179. */
  180. static uint8_t rfalNfcaCalculateBcc(const uint8_t* buf, uint8_t bufLen) {
  181. uint8_t i;
  182. uint8_t BCC;
  183. BCC = 0;
  184. /* BCC is XOR over first 4 bytes of the SDD_RES Digital 1.1 6.7.2 */
  185. for(i = 0; i < bufLen; i++) {
  186. BCC ^= buf[i];
  187. }
  188. return BCC;
  189. }
  190. /*******************************************************************************/
  191. static ReturnCode rfalNfcaPollerStartSingleCollisionResolution(
  192. uint8_t devLimit,
  193. bool* collPending,
  194. rfalNfcaSelRes* selRes,
  195. uint8_t* nfcId1,
  196. uint8_t* nfcId1Len) {
  197. /* Check parameters */
  198. if((collPending == NULL) || (selRes == NULL) || (nfcId1 == NULL) || (nfcId1Len == NULL)) {
  199. return ERR_PARAM;
  200. }
  201. /* Initialize output parameters */
  202. *collPending = false; /* Activity 1.1 9.3.4.6 */
  203. *nfcId1Len = 0;
  204. ST_MEMSET(nfcId1, 0x00, RFAL_NFCA_CASCADE_3_UID_LEN);
  205. /* Save parameters */
  206. gNfca.CR.devLimit = devLimit;
  207. gNfca.CR.collPend = collPending;
  208. gNfca.CR.selRes = selRes;
  209. gNfca.CR.nfcId1 = nfcId1;
  210. gNfca.CR.nfcId1Len = nfcId1Len;
  211. platformTimerDestroy(gNfca.CR.tmrFDT);
  212. gNfca.CR.tmrFDT = 0U;
  213. gNfca.CR.retries = RFAL_NFCA_N_RETRANS;
  214. gNfca.CR.cascadeLv = (uint8_t)RFAL_NFCA_SEL_CASCADE_L1;
  215. gNfca.CR.state = RFAL_NFCA_CR_CL;
  216. gNfca.CR.doBacktrack = false;
  217. gNfca.CR.backtrackCnt = 3U;
  218. return ERR_NONE;
  219. }
  220. /*******************************************************************************/
  221. static ReturnCode rfalNfcaPollerGetSingleCollisionResolutionStatus(void) {
  222. ReturnCode ret;
  223. uint8_t collBit = 1U; /* standards mandate or recommend collision bit to be set to One. */
  224. /* Check if FDT timer is still running */
  225. if(!platformTimerIsExpired(gNfca.CR.tmrFDT) && (gNfca.CR.tmrFDT != 0U)) {
  226. return ERR_BUSY;
  227. }
  228. /*******************************************************************************/
  229. /* Go through all Cascade Levels Activity 1.1 9.3.4 */
  230. if(gNfca.CR.cascadeLv > (uint8_t)RFAL_NFCA_SEL_CASCADE_L3) {
  231. return ERR_INTERNAL;
  232. }
  233. switch(gNfca.CR.state) {
  234. /*******************************************************************************/
  235. case RFAL_NFCA_CR_CL:
  236. /* Initialize the SDD_REQ to send for the new cascade level */
  237. ST_MEMSET((uint8_t*)&gNfca.CR.selReq, 0x00, sizeof(rfalNfcaSelReq));
  238. gNfca.CR.bytesTxRx = RFAL_NFCA_SDD_REQ_LEN;
  239. gNfca.CR.bitsTxRx = 0U;
  240. gNfca.CR.state = RFAL_NFCA_CR_SDD;
  241. /* fall through */
  242. /*******************************************************************************/
  243. case RFAL_NFCA_CR_SDD: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  244. /* Calculate SEL_CMD and SEL_PAR with the bytes/bits to be sent */
  245. gNfca.CR.selReq.selCmd = rfalNfcaCLn2SELCMD(gNfca.CR.cascadeLv);
  246. gNfca.CR.selReq.selPar = rfalNfcaSelPar(gNfca.CR.bytesTxRx, gNfca.CR.bitsTxRx);
  247. /* Send SDD_REQ (Anticollision frame) */
  248. ret = rfalISO14443ATransceiveAnticollisionFrame(
  249. (uint8_t*)&gNfca.CR.selReq,
  250. &gNfca.CR.bytesTxRx,
  251. &gNfca.CR.bitsTxRx,
  252. &gNfca.CR.rxLen,
  253. RFAL_NFCA_FDTMIN);
  254. /* Retry upon timeout EMVCo 2.6 9.6.1.3 */
  255. if((ret == ERR_TIMEOUT) && (gNfca.CR.devLimit == 0U) && (gNfca.CR.retries != 0U)) {
  256. gNfca.CR.retries--;
  257. platformTimerDestroy(gNfca.CR.tmrFDT);
  258. gNfca.CR.tmrFDT = platformTimerCreate(RFAL_NFCA_T_RETRANS);
  259. break;
  260. }
  261. /* Covert rxLen into bytes */
  262. gNfca.CR.rxLen = rfalConvBitsToBytes(gNfca.CR.rxLen);
  263. if((ret == ERR_TIMEOUT) && (gNfca.CR.backtrackCnt != 0U) && (!gNfca.CR.doBacktrack) &&
  264. !((RFAL_NFCA_SDD_REQ_LEN == gNfca.CR.bytesTxRx) && (0U == gNfca.CR.bitsTxRx))) {
  265. /* In multiple card scenarios it may always happen that some
  266. * collisions of a weaker tag go unnoticed. If then a later
  267. * collision is recognized and the strong tag has a 0 at the
  268. * collision position then no tag will respond. Catch this
  269. * corner case and then try with the bit being sent as zero. */
  270. rfalNfcaSensRes sensRes;
  271. ret = ERR_RF_COLLISION;
  272. rfalNfcaPollerCheckPresence(RFAL_14443A_SHORTFRAME_CMD_REQA, &sensRes);
  273. /* Algorithm below does a post-increment, decrement to go back to current position */
  274. if(0U == gNfca.CR.bitsTxRx) {
  275. gNfca.CR.bitsTxRx = 7;
  276. gNfca.CR.bytesTxRx--;
  277. } else {
  278. gNfca.CR.bitsTxRx--;
  279. }
  280. collBit =
  281. (uint8_t)(((uint8_t*)&gNfca.CR.selReq)[gNfca.CR.bytesTxRx] & (1U << gNfca.CR.bitsTxRx));
  282. collBit = (uint8_t)((0U == collBit) ? 1U : 0U); // invert the collision bit
  283. gNfca.CR.doBacktrack = true;
  284. gNfca.CR.backtrackCnt--;
  285. } else {
  286. gNfca.CR.doBacktrack = false;
  287. }
  288. if(ret == ERR_RF_COLLISION) {
  289. /* Check received length */
  290. if((gNfca.CR.bytesTxRx + ((gNfca.CR.bitsTxRx != 0U) ? 1U : 0U)) >
  291. (RFAL_NFCA_SDD_RES_LEN + RFAL_NFCA_SDD_REQ_LEN)) {
  292. return ERR_PROTO;
  293. }
  294. if(((gNfca.CR.bytesTxRx + ((gNfca.CR.bitsTxRx != 0U) ? 1U : 0U)) >
  295. (RFAL_NFCA_CASCADE_1_UID_LEN + RFAL_NFCA_SDD_REQ_LEN)) &&
  296. (gNfca.CR.backtrackCnt != 0U)) { /* Collision in BCC: Anticollide only UID part */
  297. gNfca.CR.backtrackCnt--;
  298. gNfca.CR.bytesTxRx = RFAL_NFCA_CASCADE_1_UID_LEN + RFAL_NFCA_SDD_REQ_LEN - 1U;
  299. gNfca.CR.bitsTxRx = 7;
  300. collBit =
  301. (uint8_t)(((uint8_t*)&gNfca.CR.selReq)[gNfca.CR.bytesTxRx] & (1U << gNfca.CR.bitsTxRx)); /* Not a real collision, extract the actual bit for the subsequent code */
  302. }
  303. if((gNfca.CR.devLimit == 0U) && !(*gNfca.CR.collPend)) {
  304. /* Activity 1.0 & 1.1 9.3.4.12: If CON_DEVICES_LIMIT has a value of 0, then
  305. * NFC Forum Device is configured to perform collision detection only */
  306. *gNfca.CR.collPend = true;
  307. return ERR_IGNORE;
  308. }
  309. *gNfca.CR.collPend = true;
  310. /* Set and select the collision bit, with the number of bytes/bits successfully TxRx */
  311. if(collBit != 0U) {
  312. ((uint8_t*)&gNfca.CR.selReq)[gNfca.CR.bytesTxRx] =
  313. (uint8_t)(((uint8_t*)&gNfca.CR.selReq)[gNfca.CR.bytesTxRx] | (1U << gNfca.CR.bitsTxRx)); /* MISRA 10.3 */
  314. } else {
  315. ((uint8_t*)&gNfca.CR.selReq)[gNfca.CR.bytesTxRx] =
  316. (uint8_t)(((uint8_t*)&gNfca.CR.selReq)[gNfca.CR.bytesTxRx] & ~(1U << gNfca.CR.bitsTxRx)); /* MISRA 10.3 */
  317. }
  318. gNfca.CR.bitsTxRx++;
  319. /* Check if number of bits form a byte */
  320. if(gNfca.CR.bitsTxRx == RFAL_BITS_IN_BYTE) {
  321. gNfca.CR.bitsTxRx = 0;
  322. gNfca.CR.bytesTxRx++;
  323. }
  324. break;
  325. }
  326. /*******************************************************************************/
  327. /* Check if Collision loop has failed */
  328. if(ret != ERR_NONE) {
  329. return ret;
  330. }
  331. /* If collisions are to be reported check whether the response is complete */
  332. if((gNfca.CR.devLimit == 0U) && (gNfca.CR.rxLen != sizeof(rfalNfcaSddRes))) {
  333. return ERR_PROTO;
  334. }
  335. /* Check if the received BCC match */
  336. if(gNfca.CR.selReq.bcc !=
  337. rfalNfcaCalculateBcc(gNfca.CR.selReq.nfcid1, RFAL_NFCA_CASCADE_1_UID_LEN)) {
  338. return ERR_PROTO;
  339. }
  340. /*******************************************************************************/
  341. /* Anticollision OK, Select this Cascade Level */
  342. gNfca.CR.selReq.selPar = RFAL_NFCA_SEL_SELPAR;
  343. gNfca.CR.retries = RFAL_NFCA_N_RETRANS;
  344. gNfca.CR.state = RFAL_NFCA_CR_SEL;
  345. break;
  346. /*******************************************************************************/
  347. case RFAL_NFCA_CR_SEL:
  348. /* Send SEL_REQ (Select command) - Retry upon timeout EMVCo 2.6 9.6.1.3 */
  349. ret = rfalTransceiveBlockingTxRx(
  350. (uint8_t*)&gNfca.CR.selReq,
  351. sizeof(rfalNfcaSelReq),
  352. (uint8_t*)gNfca.CR.selRes,
  353. sizeof(rfalNfcaSelRes),
  354. &gNfca.CR.rxLen,
  355. RFAL_TXRX_FLAGS_DEFAULT,
  356. RFAL_NFCA_FDTMIN);
  357. /* Retry upon timeout EMVCo 2.6 9.6.1.3 */
  358. if((ret == ERR_TIMEOUT) && (gNfca.CR.devLimit == 0U) && (gNfca.CR.retries != 0U)) {
  359. gNfca.CR.retries--;
  360. platformTimerDestroy(gNfca.CR.tmrFDT);
  361. gNfca.CR.tmrFDT = platformTimerCreate(RFAL_NFCA_T_RETRANS);
  362. break;
  363. }
  364. if(ret != ERR_NONE) {
  365. return ret;
  366. }
  367. /* Ensure proper response length */
  368. if(gNfca.CR.rxLen != sizeof(rfalNfcaSelRes)) {
  369. return ERR_PROTO;
  370. }
  371. /*******************************************************************************/
  372. /* Check cascade byte, if cascade tag then go next cascade level */
  373. if(*gNfca.CR.selReq.nfcid1 == RFAL_NFCA_SDD_CT) {
  374. /* Cascade Tag present, store nfcid1 bytes (excluding cascade tag) and continue for next CL */
  375. ST_MEMCPY(
  376. &gNfca.CR.nfcId1[*gNfca.CR.nfcId1Len],
  377. &((uint8_t*)&gNfca.CR.selReq.nfcid1)[RFAL_NFCA_SDD_CT_LEN],
  378. (RFAL_NFCA_CASCADE_1_UID_LEN - RFAL_NFCA_SDD_CT_LEN));
  379. *gNfca.CR.nfcId1Len += (RFAL_NFCA_CASCADE_1_UID_LEN - RFAL_NFCA_SDD_CT_LEN);
  380. /* Go to next cascade level */
  381. gNfca.CR.state = RFAL_NFCA_CR_CL;
  382. gNfca.CR.cascadeLv++;
  383. } else {
  384. /* UID Selection complete, Stop Cascade Level loop */
  385. ST_MEMCPY(
  386. &gNfca.CR.nfcId1[*gNfca.CR.nfcId1Len],
  387. (uint8_t*)&gNfca.CR.selReq.nfcid1,
  388. RFAL_NFCA_CASCADE_1_UID_LEN);
  389. *gNfca.CR.nfcId1Len += RFAL_NFCA_CASCADE_1_UID_LEN;
  390. gNfca.CR.state = RFAL_NFCA_CR_DONE;
  391. break; /* Only flag operation complete on the next execution */
  392. }
  393. break;
  394. /*******************************************************************************/
  395. case RFAL_NFCA_CR_DONE:
  396. return ERR_NONE;
  397. /*******************************************************************************/
  398. default:
  399. return ERR_WRONG_STATE;
  400. }
  401. return ERR_BUSY;
  402. }
  403. /*
  404. ******************************************************************************
  405. * GLOBAL FUNCTIONS
  406. ******************************************************************************
  407. */
  408. /*******************************************************************************/
  409. ReturnCode rfalNfcaPollerInitialize(void) {
  410. ReturnCode ret;
  411. EXIT_ON_ERR(ret, rfalSetMode(RFAL_MODE_POLL_NFCA, RFAL_BR_106, RFAL_BR_106));
  412. rfalSetErrorHandling(RFAL_ERRORHANDLING_NFC);
  413. rfalSetGT(RFAL_GT_NFCA);
  414. rfalSetFDTListen(RFAL_FDT_LISTEN_NFCA_POLLER);
  415. rfalSetFDTPoll(RFAL_FDT_POLL_NFCA_POLLER);
  416. return ERR_NONE;
  417. }
  418. /*******************************************************************************/
  419. ReturnCode rfalNfcaPollerCheckPresence(rfal14443AShortFrameCmd cmd, rfalNfcaSensRes* sensRes) {
  420. ReturnCode ret;
  421. uint16_t rcvLen;
  422. /* Digital 1.1 6.10.1.3 For Commands ALL_REQ, SENS_REQ, SDD_REQ, and SEL_REQ, the NFC Forum Device *
  423. * MUST treat receipt of a Listen Frame at a time after FDT(Listen, min) as a Timeour Error */
  424. ret = rfalISO14443ATransceiveShortFrame(
  425. cmd,
  426. (uint8_t*)sensRes,
  427. (uint8_t)rfalConvBytesToBits(sizeof(rfalNfcaSensRes)),
  428. &rcvLen,
  429. RFAL_NFCA_FDTMIN);
  430. if((ret == ERR_RF_COLLISION) || (ret == ERR_CRC) || (ret == ERR_NOMEM) ||
  431. (ret == ERR_FRAMING) || (ret == ERR_PAR)) {
  432. ret = ERR_NONE;
  433. }
  434. return ret;
  435. }
  436. /*******************************************************************************/
  437. ReturnCode
  438. rfalNfcaPollerTechnologyDetection(rfalComplianceMode compMode, rfalNfcaSensRes* sensRes) {
  439. ReturnCode ret;
  440. EXIT_ON_ERR(
  441. ret,
  442. rfalNfcaPollerCheckPresence(
  443. ((compMode == RFAL_COMPLIANCE_MODE_EMV) ? RFAL_14443A_SHORTFRAME_CMD_WUPA :
  444. RFAL_14443A_SHORTFRAME_CMD_REQA),
  445. sensRes));
  446. /* Send SLP_REQ as Activity 1.1 9.2.3.6 and EMVCo 2.6 9.2.1.3 */
  447. if(compMode != RFAL_COMPLIANCE_MODE_ISO) {
  448. rfalNfcaPollerSleep();
  449. }
  450. return ERR_NONE;
  451. }
  452. /*******************************************************************************/
  453. ReturnCode rfalNfcaPollerSingleCollisionResolution(
  454. uint8_t devLimit,
  455. bool* collPending,
  456. rfalNfcaSelRes* selRes,
  457. uint8_t* nfcId1,
  458. uint8_t* nfcId1Len) {
  459. ReturnCode ret;
  460. EXIT_ON_ERR(
  461. ret,
  462. rfalNfcaPollerStartSingleCollisionResolution(
  463. devLimit, collPending, selRes, nfcId1, nfcId1Len));
  464. rfalNfcaRunBlocking(ret, rfalNfcaPollerGetSingleCollisionResolutionStatus());
  465. return ret;
  466. }
  467. /*******************************************************************************/
  468. ReturnCode rfalNfcaPollerStartFullCollisionResolution(
  469. rfalComplianceMode compMode,
  470. uint8_t devLimit,
  471. rfalNfcaListenDevice* nfcaDevList,
  472. uint8_t* devCnt) {
  473. ReturnCode ret;
  474. rfalNfcaSensRes sensRes;
  475. uint16_t rcvLen;
  476. if((nfcaDevList == NULL) || (devCnt == NULL)) {
  477. return ERR_PARAM;
  478. }
  479. *devCnt = 0;
  480. ret = ERR_NONE;
  481. /*******************************************************************************/
  482. /* Send ALL_REQ before Anticollision if a Sleep was sent before Activity 1.1 9.3.4.1 and EMVco 2.6 9.3.2.1 */
  483. if(compMode != RFAL_COMPLIANCE_MODE_ISO) {
  484. ret = rfalISO14443ATransceiveShortFrame(
  485. RFAL_14443A_SHORTFRAME_CMD_WUPA,
  486. (uint8_t*)&nfcaDevList->sensRes,
  487. (uint8_t)rfalConvBytesToBits(sizeof(rfalNfcaSensRes)),
  488. &rcvLen,
  489. RFAL_NFCA_FDTMIN);
  490. if(ret != ERR_NONE) {
  491. if((compMode == RFAL_COMPLIANCE_MODE_EMV) ||
  492. ((ret != ERR_RF_COLLISION) && (ret != ERR_CRC) && (ret != ERR_FRAMING) &&
  493. (ret != ERR_PAR))) {
  494. return ret;
  495. }
  496. }
  497. /* Check proper SENS_RES/ATQA size */
  498. if((ret == ERR_NONE) && (rfalConvBytesToBits(sizeof(rfalNfcaSensRes)) != rcvLen)) {
  499. return ERR_PROTO;
  500. }
  501. }
  502. /*******************************************************************************/
  503. /* Store the SENS_RES from Technology Detection or from WUPA */
  504. sensRes = nfcaDevList->sensRes;
  505. if(devLimit > 0U) /* MISRA 21.18 */
  506. {
  507. ST_MEMSET(nfcaDevList, 0x00, (sizeof(rfalNfcaListenDevice) * devLimit));
  508. }
  509. /* Restore the prev SENS_RES, assuming that the SENS_RES received is from first device
  510. * When only one device is detected it's not woken up then we'll have no SENS_RES (ATQA) */
  511. nfcaDevList->sensRes = sensRes;
  512. /* Save parameters */
  513. gNfca.CR.devCnt = devCnt;
  514. gNfca.CR.devLimit = devLimit;
  515. gNfca.CR.nfcaDevList = nfcaDevList;
  516. gNfca.CR.compMode = compMode;
  517. #if RFAL_FEATURE_T1T
  518. /*******************************************************************************/
  519. /* Only check for T1T if previous SENS_RES was received without a transmission *
  520. * error. When collisions occur bits in the SENS_RES may look like a T1T */
  521. /* If T1T Anticollision is not supported Activity 1.1 9.3.4.3 */
  522. if(rfalNfcaIsSensResT1T(&nfcaDevList->sensRes) && (devLimit != 0U) && (ret == ERR_NONE) &&
  523. (compMode != RFAL_COMPLIANCE_MODE_EMV)) {
  524. /* RID_REQ shall be performed Activity 1.1 9.3.4.24 */
  525. rfalT1TPollerInitialize();
  526. EXIT_ON_ERR(ret, rfalT1TPollerRid(&nfcaDevList->ridRes));
  527. *devCnt = 1U;
  528. nfcaDevList->isSleep = false;
  529. nfcaDevList->type = RFAL_NFCA_T1T;
  530. nfcaDevList->nfcId1Len = RFAL_NFCA_CASCADE_1_UID_LEN;
  531. ST_MEMCPY(&nfcaDevList->nfcId1, &nfcaDevList->ridRes.uid, RFAL_NFCA_CASCADE_1_UID_LEN);
  532. return ERR_NONE;
  533. }
  534. #endif /* RFAL_FEATURE_T1T */
  535. return rfalNfcaPollerStartSingleCollisionResolution(
  536. devLimit,
  537. &gNfca.CR.collPending,
  538. &nfcaDevList->selRes,
  539. (uint8_t*)&nfcaDevList->nfcId1,
  540. &nfcaDevList->nfcId1Len);
  541. }
  542. /*******************************************************************************/
  543. ReturnCode rfalNfcaPollerGetFullCollisionResolutionStatus(void) {
  544. ReturnCode ret;
  545. uint8_t newDevType;
  546. if((gNfca.CR.nfcaDevList == NULL) || (gNfca.CR.devCnt == NULL)) {
  547. return ERR_WRONG_STATE;
  548. }
  549. /*******************************************************************************/
  550. /* Check whether a T1T has already been detected */
  551. if(rfalNfcaIsSensResT1T(&gNfca.CR.nfcaDevList->sensRes) &&
  552. (gNfca.CR.nfcaDevList->type == RFAL_NFCA_T1T)) {
  553. /* T1T doesn't support Anticollision */
  554. return ERR_NONE;
  555. }
  556. /*******************************************************************************/
  557. EXIT_ON_ERR(ret, rfalNfcaPollerGetSingleCollisionResolutionStatus());
  558. /* Assign Listen Device */
  559. newDevType = ((uint8_t)gNfca.CR.nfcaDevList[*gNfca.CR.devCnt].selRes.sak) &
  560. RFAL_NFCA_SEL_RES_CONF_MASK; /* MISRA 10.8 */
  561. /* PRQA S 4342 1 # MISRA 10.5 - Guaranteed that no invalid enum values are created: see guard_eq_RFAL_NFCA_T2T, .... */
  562. gNfca.CR.nfcaDevList[*gNfca.CR.devCnt].type = (rfalNfcaListenDeviceType)newDevType;
  563. gNfca.CR.nfcaDevList[*gNfca.CR.devCnt].isSleep = false;
  564. (*gNfca.CR.devCnt)++;
  565. /* If a collision was detected and device counter is lower than limit Activity 1.1 9.3.4.21 */
  566. if((*gNfca.CR.devCnt < gNfca.CR.devLimit) && (gNfca.CR.collPending)) {
  567. /* Put this device to Sleep Activity 1.1 9.3.4.22 */
  568. rfalNfcaPollerSleep();
  569. gNfca.CR.nfcaDevList[(*gNfca.CR.devCnt - 1U)].isSleep = true;
  570. /* Send a new SENS_REQ to check for other cards Activity 1.1 9.3.4.23 */
  571. ret = rfalNfcaPollerCheckPresence(
  572. RFAL_14443A_SHORTFRAME_CMD_REQA, &gNfca.CR.nfcaDevList[*gNfca.CR.devCnt].sensRes);
  573. if(ret == ERR_TIMEOUT) {
  574. /* No more devices found, exit */
  575. gNfca.CR.collPending = false;
  576. } else {
  577. /* Another device found, continue loop */
  578. gNfca.CR.collPending = true;
  579. }
  580. } else {
  581. /* Exit loop */
  582. gNfca.CR.collPending = false;
  583. }
  584. /*******************************************************************************/
  585. /* Check if collision resolution shall continue */
  586. if((*gNfca.CR.devCnt < gNfca.CR.devLimit) && (gNfca.CR.collPending)) {
  587. EXIT_ON_ERR(
  588. ret,
  589. rfalNfcaPollerStartSingleCollisionResolution(
  590. gNfca.CR.devLimit,
  591. &gNfca.CR.collPending,
  592. &gNfca.CR.nfcaDevList[*gNfca.CR.devCnt].selRes,
  593. (uint8_t*)&gNfca.CR.nfcaDevList[*gNfca.CR.devCnt].nfcId1,
  594. &gNfca.CR.nfcaDevList[*gNfca.CR.devCnt].nfcId1Len));
  595. return ERR_BUSY;
  596. }
  597. return ERR_NONE;
  598. }
  599. /*******************************************************************************/
  600. ReturnCode rfalNfcaPollerFullCollisionResolution(
  601. rfalComplianceMode compMode,
  602. uint8_t devLimit,
  603. rfalNfcaListenDevice* nfcaDevList,
  604. uint8_t* devCnt) {
  605. ReturnCode ret;
  606. EXIT_ON_ERR(
  607. ret, rfalNfcaPollerStartFullCollisionResolution(compMode, devLimit, nfcaDevList, devCnt));
  608. rfalNfcaRunBlocking(ret, rfalNfcaPollerGetFullCollisionResolutionStatus());
  609. return ret;
  610. }
  611. ReturnCode rfalNfcaPollerSleepFullCollisionResolution(
  612. uint8_t devLimit,
  613. rfalNfcaListenDevice* nfcaDevList,
  614. uint8_t* devCnt) {
  615. bool firstRound;
  616. uint8_t tmpDevCnt;
  617. ReturnCode ret;
  618. if((nfcaDevList == NULL) || (devCnt == NULL)) {
  619. return ERR_PARAM;
  620. }
  621. /* Only use ALL_REQ (WUPA) on the first round */
  622. firstRound = true;
  623. *devCnt = 0;
  624. /* Perform collision resolution until no new device is found */
  625. do {
  626. tmpDevCnt = 0;
  627. ret = rfalNfcaPollerFullCollisionResolution(
  628. (firstRound ? RFAL_COMPLIANCE_MODE_NFC : RFAL_COMPLIANCE_MODE_ISO),
  629. (devLimit - *devCnt),
  630. &nfcaDevList[*devCnt],
  631. &tmpDevCnt);
  632. if((ret == ERR_NONE) && (tmpDevCnt > 0U)) {
  633. *devCnt += tmpDevCnt;
  634. /* Check whether to seacrh for more devices */
  635. if(*devCnt < devLimit) {
  636. /* Set last found device to sleep (all others are slept already) */
  637. rfalNfcaPollerSleep();
  638. nfcaDevList[((*devCnt) - 1U)].isSleep = true;
  639. /* Check if any other device is present */
  640. ret = rfalNfcaPollerCheckPresence(
  641. RFAL_14443A_SHORTFRAME_CMD_REQA, &nfcaDevList[*devCnt].sensRes);
  642. if(ret == ERR_NONE) {
  643. firstRound = false;
  644. continue;
  645. }
  646. }
  647. }
  648. break;
  649. } while(true);
  650. return ((*devCnt > 0U) ? ERR_NONE : ret);
  651. }
  652. /*******************************************************************************/
  653. ReturnCode rfalNfcaPollerSelect(const uint8_t* nfcid1, uint8_t nfcidLen, rfalNfcaSelRes* selRes) {
  654. uint8_t i;
  655. uint8_t cl;
  656. uint8_t nfcidOffset;
  657. uint16_t rxLen;
  658. ReturnCode ret;
  659. rfalNfcaSelReq selReq;
  660. if((nfcid1 == NULL) || (nfcidLen > RFAL_NFCA_CASCADE_3_UID_LEN) || (selRes == NULL)) {
  661. return ERR_PARAM;
  662. }
  663. /* Calculate Cascate Level */
  664. cl = rfalNfcaNfcidLen2CL(nfcidLen);
  665. nfcidOffset = 0;
  666. /*******************************************************************************/
  667. /* Go through all Cascade Levels Activity 1.1 9.4.4 */
  668. for(i = RFAL_NFCA_SEL_CASCADE_L1; i <= cl; i++) {
  669. /* Assign SEL_CMD according to the CLn and SEL_PAR*/
  670. selReq.selCmd = rfalNfcaCLn2SELCMD(i);
  671. selReq.selPar = RFAL_NFCA_SEL_SELPAR;
  672. /* Compute NFCID/Data on the SEL_REQ command Digital 1.1 Table 18 */
  673. if(cl != i) {
  674. *selReq.nfcid1 = RFAL_NFCA_SDD_CT;
  675. ST_MEMCPY(
  676. &selReq.nfcid1[RFAL_NFCA_SDD_CT_LEN],
  677. &nfcid1[nfcidOffset],
  678. (RFAL_NFCA_CASCADE_1_UID_LEN - RFAL_NFCA_SDD_CT_LEN));
  679. nfcidOffset += (RFAL_NFCA_CASCADE_1_UID_LEN - RFAL_NFCA_SDD_CT_LEN);
  680. } else {
  681. ST_MEMCPY(selReq.nfcid1, &nfcid1[nfcidOffset], RFAL_NFCA_CASCADE_1_UID_LEN);
  682. }
  683. /* Calculate nfcid's BCC */
  684. selReq.bcc = rfalNfcaCalculateBcc((uint8_t*)&selReq.nfcid1, sizeof(selReq.nfcid1));
  685. /*******************************************************************************/
  686. /* Send SEL_REQ */
  687. EXIT_ON_ERR(
  688. ret,
  689. rfalTransceiveBlockingTxRx(
  690. (uint8_t*)&selReq,
  691. sizeof(rfalNfcaSelReq),
  692. (uint8_t*)selRes,
  693. sizeof(rfalNfcaSelRes),
  694. &rxLen,
  695. RFAL_TXRX_FLAGS_DEFAULT,
  696. RFAL_NFCA_FDTMIN));
  697. /* Ensure proper response length */
  698. if(rxLen != sizeof(rfalNfcaSelRes)) {
  699. return ERR_PROTO;
  700. }
  701. }
  702. /* REMARK: Could check if NFCID1 is complete */
  703. return ERR_NONE;
  704. }
  705. /*******************************************************************************/
  706. ReturnCode rfalNfcaPollerSleep(void) {
  707. rfalNfcaSlpReq slpReq;
  708. uint8_t rxBuf; /* dummy buffer, just to perform Rx */
  709. slpReq.frame[RFAL_NFCA_SLP_CMD_POS] = RFAL_NFCA_SLP_CMD;
  710. slpReq.frame[RFAL_NFCA_SLP_BYTE2_POS] = RFAL_NFCA_SLP_BYTE2;
  711. rfalTransceiveBlockingTxRx(
  712. (uint8_t*)&slpReq,
  713. sizeof(rfalNfcaSlpReq),
  714. &rxBuf,
  715. sizeof(rxBuf),
  716. NULL,
  717. RFAL_TXRX_FLAGS_DEFAULT,
  718. RFAL_NFCA_SLP_FWT);
  719. /* ISO14443-3 6.4.3 HLTA - If PICC responds with any modulation during 1 ms this response shall be interpreted as not acknowledge
  720. Digital 2.0 6.9.2.1 & EMVCo 3.0 5.6.2.1 - consider the HLTA command always acknowledged
  721. No check to be compliant with NFC and EMVCo, and to improve interoprability (Kovio RFID Tag)
  722. */
  723. return ERR_NONE;
  724. }
  725. /*******************************************************************************/
  726. bool rfalNfcaListenerIsSleepReq(const uint8_t* buf, uint16_t bufLen) {
  727. /* Check if length and payload match */
  728. if((bufLen != sizeof(rfalNfcaSlpReq)) || (buf[RFAL_NFCA_SLP_CMD_POS] != RFAL_NFCA_SLP_CMD) ||
  729. (buf[RFAL_NFCA_SLP_BYTE2_POS] != RFAL_NFCA_SLP_BYTE2)) {
  730. return false;
  731. }
  732. return true;
  733. }
  734. /* If the guards here don't compile then the code above cannot work anymore. */
  735. extern uint8_t guard_eq_RFAL_NFCA_T2T
  736. [((RFAL_NFCA_SEL_RES_CONF_MASK & (uint8_t)RFAL_NFCA_T2T) == (uint8_t)RFAL_NFCA_T2T) ? 1 : (-1)];
  737. extern uint8_t guard_eq_RFAL_NFCA_T4T
  738. [((RFAL_NFCA_SEL_RES_CONF_MASK & (uint8_t)RFAL_NFCA_T4T) == (uint8_t)RFAL_NFCA_T4T) ? 1 : (-1)];
  739. extern uint8_t guard_eq_RFAL_NFCA_NFCDEP
  740. [((RFAL_NFCA_SEL_RES_CONF_MASK & (uint8_t)RFAL_NFCA_NFCDEP) == (uint8_t)RFAL_NFCA_NFCDEP) ?
  741. 1 :
  742. (-1)];
  743. extern uint8_t guard_eq_RFAL_NFCA_T4T_NFCDEP
  744. [((RFAL_NFCA_SEL_RES_CONF_MASK & (uint8_t)RFAL_NFCA_T4T_NFCDEP) ==
  745. (uint8_t)RFAL_NFCA_T4T_NFCDEP) ?
  746. 1 :
  747. (-1)];
  748. #endif /* RFAL_FEATURE_NFCA */