rfal_isoDep.c 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801
  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: NFCC firmware
  23. * LANGUAGE: ISO C99
  24. */
  25. /*! \file rfal_isoDep.c
  26. *
  27. * \author Gustavo Patricio
  28. *
  29. * \brief Implementation of ISO-DEP protocol
  30. *
  31. * This implementation was based on the following specs:
  32. * - ISO/IEC 14443-4 2nd Edition 2008-07-15
  33. * - NFC Forum Digital Protocol 1.1 2014-01-14
  34. *
  35. */
  36. /*
  37. ******************************************************************************
  38. * INCLUDES
  39. ******************************************************************************
  40. */
  41. #include "rfal_isoDep.h"
  42. #include "rfal_rf.h"
  43. #include "utils.h"
  44. /*
  45. ******************************************************************************
  46. * ENABLE SWITCH
  47. ******************************************************************************
  48. */
  49. #if RFAL_FEATURE_ISO_DEP
  50. #if ( !RFAL_FEATURE_ISO_DEP_POLL && !RFAL_FEATURE_ISO_DEP_LISTEN )
  51. #error " RFAL: Invalid ISO-DEP Configuration. Please select at least one mode: Poller and/or Listener. "
  52. #endif
  53. /* Check for valid I-Block length [RFAL_ISODEP_FSX_16 ; RFAL_ISODEP_FSX_4096]*/
  54. #if( (RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN > 4096 ) || (RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN < 16) )
  55. #error " RFAL: Invalid ISO-DEP IBlock Max length. Please change RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN. "
  56. #endif
  57. /* Check for valid APDU length. */
  58. #if( (RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN < RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN) )
  59. #error " RFAL: Invalid ISO-DEP APDU Max length. Please change RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN. "
  60. #endif
  61. /*
  62. ******************************************************************************
  63. * DEFINES
  64. ******************************************************************************
  65. */
  66. #define ISODEP_CRC_LEN RFAL_CRC_LEN /*!< ISO1443 CRC Length */
  67. #define ISODEP_PCB_POS (0U) /*!< PCB position on message header*/
  68. #define ISODEP_SWTX_INF_POS (1U) /*!< INF position in a S-WTX */
  69. #define ISODEP_DID_POS (1U) /*!< DID position on message header*/
  70. #define ISODEP_SWTX_PARAM_LEN (1U) /*!< SWTX parameter length */
  71. #define ISODEP_DSL_MAX_LEN ( RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN ) /*!< Deselect Req/Res length */
  72. #define ISODEP_PCB_xBLOCK_MASK (0xC0U) /*!< Bit mask for Block type */
  73. #define ISODEP_PCB_IBLOCK (0x00U) /*!< Bit mask indicating a I-Block */
  74. #define ISODEP_PCB_RBLOCK (0x80U) /*!< Bit mask indicating a R-Block */
  75. #define ISODEP_PCB_SBLOCK (0xC0U) /*!< Bit mask indicating a S-Block */
  76. #define ISODEP_PCB_INVALID (0x40U) /*!< Bit mask of an Invalid PCB */
  77. #define ISODEP_HDR_MAX_LEN (RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN + RFAL_ISODEP_NAD_LEN) /*!< Max header length (PCB + DID + NAD) */
  78. #define ISODEP_PCB_IB_VALID_MASK (ISODEP_PCB_B6_BIT | ISODEP_PCB_B2_BIT) /*!< Bit mask for the MUST bits on I-Block */
  79. #define ISODEP_PCB_IB_VALID_VAL (ISODEP_PCB_B2_BIT) /*!< Value for the MUST bits on I-Block */
  80. #define ISODEP_PCB_RB_VALID_MASK (ISODEP_PCB_B6_BIT | ISODEP_PCB_B3_BIT | ISODEP_PCB_B2_BIT) /*!< Bit mask for the MUST bits on R-Block */
  81. #define ISODEP_PCB_RB_VALID_VAL (ISODEP_PCB_B6_BIT | ISODEP_PCB_B2_BIT) /*!< Value for the MUST bits on R-Block */
  82. #define ISODEP_PCB_SB_VALID_MASK (ISODEP_PCB_B3_BIT | ISODEP_PCB_B2_BIT | ISODEP_PCB_B1_BIT) /*!< Bit mask for the MUST bits on I-Block */
  83. #define ISODEP_PCB_SB_VALID_VAL (ISODEP_PCB_B2_BIT) /*!< Value for the MUST bits on I-Block */
  84. #define ISODEP_PCB_B1_BIT (0x01U) /*!< Bit mask for the RFU S Blocks */
  85. #define ISODEP_PCB_B2_BIT (0x02U) /*!< Bit mask for the RFU bit2 in I,S,R Blocks */
  86. #define ISODEP_PCB_B3_BIT (0x04U) /*!< Bit mask for the RFU bit3 in R Blocks */
  87. #define ISODEP_PCB_B6_BIT (0x20U) /*!< Bit mask for the RFU bit2 in R Blocks */
  88. #define ISODEP_PCB_CHAINING_BIT (0x10U) /*!< Bit mask for the chaining bit of an ISO DEP I-Block in PCB. */
  89. #define ISODEP_PCB_DID_BIT (0x08U) /*!< Bit mask for the DID presence bit of an ISO DEP I,S,R Blocks PCB. */
  90. #define ISODEP_PCB_NAD_BIT (0x04U) /*!< Bit mask for the NAD presence bit of an ISO DEP I,S,R Blocks in PCB */
  91. #define ISODEP_PCB_BN_MASK (0x01U) /*!< Bit mask for the block number of an ISO DEP I,R Block in PCB */
  92. #define ISODEP_SWTX_PL_MASK (0xC0U) /*!< Bit mask for the Power Level bits of the inf byte of an WTX request or response */
  93. #define ISODEP_SWTX_WTXM_MASK (0x3FU) /*!< Bit mask for the WTXM bits of the inf byte of an WTX request or response */
  94. #define ISODEP_RBLOCK_INF_LEN (0U) /*!< INF length of R-Block Digital 1.1 15.1.3 */
  95. #define ISODEP_SDSL_INF_LEN (0U) /*!< INF length of S(DSL) Digital 1.1 15.1.3 */
  96. #define ISODEP_SWTX_INF_LEN (1U) /*!< INF length of S(WTX) Digital 1.1 15.2.2 */
  97. #define ISODEP_WTXM_MIN (1U) /*!< Minimum allowed value for the WTXM, Digital 1.0 13.2.2 */
  98. #define ISODEP_WTXM_MAX (59U) /*!< Maximum allowed value for the WTXM, Digital 1.0 13.2.2 */
  99. #define ISODEP_PCB_Sxx_MASK (0x30U) /*!< Bit mask for the S-Block type */
  100. #define ISODEP_PCB_DESELECT (0x00U) /*!< Bit mask for S-Block indicating Deselect */
  101. #define ISODEP_PCB_WTX (0x30U) /*!< Bit mask for S-Block indicating Waiting Time eXtension */
  102. #define ISODEP_PCB_Rx_MASK (0x10U) /*!< Bit mask for the R-Block type */
  103. #define ISODEP_PCB_ACK (0x00U) /*!< Bit mask for R-Block indicating ACK */
  104. #define ISODEP_PCB_NAK (0x10U) /*!< Bit mask for R-Block indicating NAK */
  105. /*! Maximum length of control message (no INF) */
  106. #define ISODEP_CONTROLMSG_BUF_LEN (RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN + RFAL_ISODEP_NAD_LEN + ISODEP_SWTX_PARAM_LEN)
  107. #define ISODEP_FWT_DEACTIVATION (71680U) /*!< FWT used for DESELECT Digital 2.2 B10 ISO1444-4 7.2 & 8.1 */
  108. #define ISODEP_MAX_RERUNS (0x0FFFFFFFU)/*!< Maximum rerun retrys for a blocking protocol run*/
  109. #define ISODEP_PCBSBLOCK ( 0x00U | ISODEP_PCB_SBLOCK | ISODEP_PCB_B2_BIT ) /*!< PCB Value of a S-Block */
  110. #define ISODEP_PCB_SDSL ( ISODEP_PCBSBLOCK | ISODEP_PCB_DESELECT ) /*!< PCB Value of a S-Block with DESELECT */
  111. #define ISODEP_PCB_SWTX ( ISODEP_PCBSBLOCK | ISODEP_PCB_WTX ) /*!< PCB Value of a S-Block with WTX */
  112. #define ISODEP_PCB_SPARAMETERS ( ISODEP_PCB_SBLOCK | ISODEP_PCB_WTX ) /*!< PCB Value of a S-Block with PARAMETERS */
  113. #define ISODEP_FWI_LIS_MAX_NFC 8U /*!< FWT Listener Max FWIT4ATmax FWIBmax Digital 1.1 A6 & A3 */
  114. #define ISODEP_FWI_LIS_MAX_EMVCO 7U /*!< FWT Listener Max FWIMAX EMVCo 2.6 A.5 */
  115. #define ISODEP_FWI_LIS_MAX (uint8_t)((gIsoDep.compMode == RFAL_COMPLIANCE_MODE_EMV) ? ISODEP_FWI_LIS_MAX_EMVCO : ISODEP_FWI_LIS_MAX_NFC) /*!< FWI Listener Max as NFC / EMVCo */
  116. #define ISODEP_FWT_LIS_MAX rfalIsoDepFWI2FWT(ISODEP_FWI_LIS_MAX) /*!< FWT Listener Max */
  117. #define ISODEP_FWI_MIN_10 (1U) /*!< Minimum value for FWI Digital 1.0 11.6.2.17 */
  118. #define ISODEP_FWI_MIN_11 (0U) /*!< Default value for FWI Digital 1.1 13.6.2 */
  119. #define ISODEP_FWI_MAX (14U) /*!< Maximum value for FWI Digital 1.0 11.6.2.17 */
  120. #define ISODEP_SFGI_MIN (0U) /*!< Default value for FWI Digital 1.1 13.6.2.22 */
  121. #define ISODEP_SFGI_MAX (14U) /*!< Maximum value for FWI Digital 1.1 13.6.2.22 */
  122. #define RFAL_ISODEP_SPARAM_TVL_HDR_LEN (2U) /*!< S(PARAMETERS) TVL header length: Tag + Len */
  123. #define RFAL_ISODEP_SPARAM_HDR_LEN (RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_SPARAM_TVL_HDR_LEN) /*!< S(PARAMETERS) header length: PCB + Tag + Len */
  124. /**********************************************************************************************************************/
  125. /**********************************************************************************************************************/
  126. #define RFAL_ISODEP_NO_PARAM (0U) /*!< No parameter flag for isoDepHandleControlMsg() */
  127. #define RFAL_ISODEP_CMD_RATS (0xE0U) /*!< RATS command Digital 1.1 13.6.1 */
  128. #define RFAL_ISODEP_ATS_MIN_LEN (1U) /*!< Minimum ATS length Digital 1.1 13.6.2 */
  129. #define RFAL_ISODEP_ATS_HDR_LEN (5U) /*!< ATS headerlength Digital 1.1 13.6.2 */
  130. #define RFAL_ISODEP_ATS_MAX_LEN (RFAL_ISODEP_ATS_HDR_LEN + RFAL_ISODEP_ATS_HB_MAX_LEN) /*!< Maximum ATS length Digital 1.1 13.6.2 */
  131. #define RFAL_ISODEP_ATS_T0_FSCI_MASK (0x0FU) /*!< ATS T0's FSCI mask Digital 1.1 13.6.2 */
  132. #define RFAL_ISODEP_ATS_TB_FWI_SHIFT (4U) /*!< ATS TB's FWI shift Digital 1.1 13.6.2 */
  133. #define RFAL_ISODEP_ATS_FWI_MASK (0x0FU) /*!< ATS TB's FWI shift Digital 1.1 13.6.2 */
  134. #define RFAL_ISODEP_ATS_TL_POS (0x00U) /*!< ATS TL's position Digital 1.1 13.6.2 */
  135. #define RFAL_ISODEP_PPS_SB (0xD0U) /*!< PPS REQ PPSS's SB value (no CID) ISO14443-4 5.3 */
  136. #define RFAL_ISODEP_PPS_MASK (0xF0U) /*!< PPS REQ PPSS's SB mask ISO14443-4 5.3 */
  137. #define RFAL_ISODEP_PPS_SB_DID_MASK (0x0FU) /*!< PPS REQ PPSS's DID|CID mask ISO14443-4 5.3 */
  138. #define RFAL_ISODEP_PPS_PPS0_PPS1_PRESENT (0x11U) /*!< PPS REQ PPS0 indicating that PPS1 is present */
  139. #define RFAL_ISODEP_PPS_PPS1 (0x00U) /*!< PPS REQ PPS1 fixed value ISO14443-4 5.3 */
  140. #define RFAL_ISODEP_PPS_PPS1_DSI_SHIFT (2U) /*!< PPS REQ PPS1 fixed value ISO14443-4 5.3 */
  141. #define RFAL_ISODEP_PPS_PPS1_DXI_MASK (0x0FU) /*!< PPS REQ PPS1 fixed value ISO14443-4 5.3 */
  142. #define RFAL_ISODEP_PPS_RES_LEN (1U) /*!< PPS Response length ISO14443-4 5.4 */
  143. #define RFAL_ISODEP_PPS_STARTBYTE_POS (0U) /*!< PPS REQ PPSS's byte position ISO14443-4 5.4 */
  144. #define RFAL_ISODEP_PPS_PPS0_POS (1U) /*!< PPS REQ PPS0's byte position ISO14443-4 5.4 */
  145. #define RFAL_ISODEP_PPS_PPS1_POS (2U) /*!< PPS REQ PPS1's byte position ISO14443-4 5.4 */
  146. #define RFAL_ISODEP_PPS0_VALID_MASK (0xEFU) /*!< PPS REQ PPS0 valid coding mask ISO14443-4 5.4 */
  147. #define RFAL_ISODEP_CMD_ATTRIB (0x1DU) /*!< ATTRIB command Digital 1.1 14.6.1 */
  148. #define RFAL_ISODEP_ATTRIB_PARAM2_DSI_SHIFT (6U) /*!< ATTRIB PARAM2 DSI shift Digital 1.1 14.6.1 */
  149. #define RFAL_ISODEP_ATTRIB_PARAM2_DRI_SHIFT (4U) /*!< ATTRIB PARAM2 DRI shift Digital 1.1 14.6.1 */
  150. #define RFAL_ISODEP_ATTRIB_PARAM2_DXI_MASK (0xF0U) /*!< ATTRIB PARAM2 DxI mask Digital 1.1 14.6.1 */
  151. #define RFAL_ISODEP_ATTRIB_PARAM2_FSDI_MASK (0x0FU) /*!< ATTRIB PARAM2 FSDI mask Digital 1.1 14.6.1 */
  152. #define RFAL_ISODEP_ATTRIB_PARAM4_DID_MASK (0x0FU) /*!< ATTRIB PARAM4 DID mask Digital 1.1 14.6.1 */
  153. #define RFAL_ISODEP_ATTRIB_HDR_LEN (9U) /*!< ATTRIB REQ header length Digital 1.1 14.6.1 */
  154. #define RFAL_ISODEP_ATTRIB_RES_HDR_LEN (1U) /*!< ATTRIB RES header length Digital 1.1 14.6.2 */
  155. #define RFAL_ISODEP_ATTRIB_RES_MBLIDID_POS (0U) /*!< ATTRIB RES MBLI|DID position Digital 1.1 14.6.2 */
  156. #define RFAL_ISODEP_ATTRIB_RES_DID_MASK (0x0FU) /*!< ATTRIB RES DID mask Digital 1.1 14.6.2 */
  157. #define RFAL_ISODEP_ATTRIB_RES_MBLI_MASK (0x0FU) /*!< ATTRIB RES MBLI mask Digital 1.1 14.6.2 */
  158. #define RFAL_ISODEP_ATTRIB_RES_MBLI_SHIFT (4U) /*!< ATTRIB RES MBLI shift Digital 1.1 14.6.2 */
  159. #define RFAL_ISODEP_DID_MASK (0x0FU) /*!< ISODEP's DID mask */
  160. #define RFAL_ISODEP_DID_00 (0U) /*!< ISODEP's DID value 0 */
  161. #define RFAL_ISODEP_FSDI_MAX_NFC (8U) /*!< Max FSDI value Digital 2.0 14.6.1.9 & B7 & B8 */
  162. #define RFAL_ISODEP_FSDI_MAX_NFC_21 (0x0CU) /*!< Max FSDI value Digital 2.1 14.6.1.9 & Table 72 */
  163. #define RFAL_ISODEP_FSDI_MAX_EMV (0x0CU) /*!< Max FSDI value EMVCo 3.0 5.7.2.5 */
  164. #define RFAL_ISODEP_RATS_PARAM_FSDI_MASK (0xF0U) /*!< Mask bits for FSDI in RATS */
  165. #define RFAL_ISODEP_RATS_PARAM_FSDI_SHIFT (4U) /*!< Shift for FSDI in RATS */
  166. #define RFAL_ISODEP_RATS_PARAM_DID_MASK (0x0FU) /*!< Mask bits for DID in RATS */
  167. #define RFAL_ISODEP_ATS_TL_OFFSET (0x00U) /*!< Offset of TL on ATS */
  168. #define RFAL_ISODEP_ATS_TA_OFFSET (0x02U) /*!< Offset of TA if it is present on ATS */
  169. #define RFAL_ISODEP_ATS_TB_OFFSET (0x03U) /*!< Offset of TB if both TA and TB is present on ATS */
  170. #define RFAL_ISODEP_ATS_TC_OFFSET (0x04U) /*!< Offset of TC if both TA,TB & TC are present on ATS */
  171. #define RFAL_ISODEP_ATS_HIST_OFFSET (0x05U) /*!< Offset of Historical Bytes if TA, TB & TC are present on ATS */
  172. #define RFAL_ISODEP_ATS_TC_ADV_FEAT (0x10U) /*!< Bit mask indicating support for Advanced protocol features: DID & NAD */
  173. #define RFAL_ISODEP_ATS_TC_DID (0x02U) /*!< Bit mask indicating support for DID */
  174. #define RFAL_ISODEP_ATS_TC_NAD (0x01U) /*!< Bit mask indicating support for NAD */
  175. #define RFAL_ISODEP_PPS0_PPS1_PRESENT (0x11U) /*!< PPS0 byte indicating that PPS1 is present */
  176. #define RFAL_ISODEP_PPS0_PPS1_NOT_PRESENT (0x01U) /*!< PPS0 byte indicating that PPS1 is NOT present */
  177. #define RFAL_ISODEP_PPS1_DRI_MASK (0x03U) /*!< PPS1 byte DRI mask bits */
  178. #define RFAL_ISODEP_PPS1_DSI_MASK (0x0CU) /*!< PPS1 byte DSI mask bits */
  179. #define RFAL_ISODEP_PPS1_DSI_SHIFT (2U) /*!< PPS1 byte DSI shift */
  180. #define RFAL_ISODEP_PPS1_DxI_MASK (0x03U) /*!< PPS1 byte DSI/DRS mask bits */
  181. /*! Delta Time for polling during Activation (ATS) : 20ms Digital 1.0 11.7.1.1 & A.7 */
  182. #define RFAL_ISODEP_T4T_DTIME_POLL_10 rfalConvMsTo1fc(20)
  183. /*! Delta Time for polling during Activation (ATS) : 16.4ms Digital 1.1 13.8.1.1 & A.6
  184. * Use 16 ms as testcase T4AT_BI_10_03 sends a frame exactly at the border */
  185. #define RFAL_ISODEP_T4T_DTIME_POLL_11 216960U
  186. /*! Activation frame waiting time FWT(act) = 71680/fc (~5286us) Digital 1.1 13.8.1.1 & A.6 */
  187. #define RFAL_ISODEP_T4T_FWT_ACTIVATION (71680U + RFAL_ISODEP_T4T_DTIME_POLL_11)
  188. /*! Delta frame waiting time = 16/fc Digital 1.0 11.7.1.3 & A.7*/
  189. #define RFAL_ISODEP_DFWT_10 16U
  190. /*! Delta frame waiting time = 16/fc Digital 2.0 14.8.1.3 & B.7*/
  191. #define RFAL_ISODEP_DFWT_20 49152U
  192. /*
  193. ******************************************************************************
  194. * MACROS
  195. ******************************************************************************
  196. */
  197. #define isoDep_PCBisIBlock( pcb ) ( ((pcb) & (ISODEP_PCB_xBLOCK_MASK | ISODEP_PCB_IB_VALID_MASK)) == (ISODEP_PCB_IBLOCK | ISODEP_PCB_IB_VALID_VAL)) /*!< Checks if pcb is a I-Block */
  198. #define isoDep_PCBisRBlock( pcb ) ( ((pcb) & (ISODEP_PCB_xBLOCK_MASK | ISODEP_PCB_RB_VALID_MASK)) == (ISODEP_PCB_RBLOCK | ISODEP_PCB_RB_VALID_VAL)) /*!< Checks if pcb is a R-Block */
  199. #define isoDep_PCBisSBlock( pcb ) ( ((pcb) & (ISODEP_PCB_xBLOCK_MASK | ISODEP_PCB_SB_VALID_MASK)) == (ISODEP_PCB_SBLOCK | ISODEP_PCB_SB_VALID_VAL)) /*!< Checks if pcb is a S-Block */
  200. #define isoDep_PCBisChaining( pcb ) ( ((pcb) & ISODEP_PCB_CHAINING_BIT) == ISODEP_PCB_CHAINING_BIT) /*!< Checks if pcb is indicating chaining */
  201. #define isoDep_PCBisDeselect( pcb ) ( ((pcb) & ISODEP_PCB_Sxx_MASK) == ISODEP_PCB_DESELECT) /*!< Checks if pcb is indicating DESELECT */
  202. #define isoDep_PCBisWTX( pcb ) ( ((pcb) & ISODEP_PCB_Sxx_MASK) == ISODEP_PCB_WTX) /*!< Checks if pcb is indicating WTX */
  203. #define isoDep_PCBisACK( pcb ) ( ((pcb) & ISODEP_PCB_Rx_MASK) == ISODEP_PCB_ACK) /*!< Checks if pcb is indicating ACK */
  204. #define isoDep_PCBisNAK( pcb ) ( ((pcb) & ISODEP_PCB_Rx_MASK) == ISODEP_PCB_NAK) /*!< Checks if pcb is indicating ACK */
  205. #define isoDep_PCBhasDID( pcb ) ( ((pcb) & ISODEP_PCB_DID_BIT) == ISODEP_PCB_DID_BIT) /*!< Checks if pcb is indicating DID */
  206. #define isoDep_PCBhasNAD( pcb ) ( ((pcb) & ISODEP_PCB_NAD_BIT) == ISODEP_PCB_NAD_BIT) /*!< Checks if pcb is indicating NAD */
  207. #define isoDep_PCBisIChaining( pcb ) ( isoDep_PCBisIBlock(pcb) && isoDep_PCBisChaining(pcb) ) /*!< Checks if pcb is I-Block indicating chaining*/
  208. #define isoDep_PCBisSDeselect( pcb ) ( isoDep_PCBisSBlock(pcb) && isoDep_PCBisDeselect(pcb) ) /*!< Checks if pcb is S-Block indicating DESELECT*/
  209. #define isoDep_PCBisSWTX( pcb ) ( isoDep_PCBisSBlock(pcb) && isoDep_PCBisWTX(pcb) ) /*!< Checks if pcb is S-Block indicating WTX */
  210. #define isoDep_PCBisRACK( pcb ) ( isoDep_PCBisRBlock(pcb) && isoDep_PCBisACK(pcb) ) /*!< Checks if pcb is R-Block indicating ACK */
  211. #define isoDep_PCBisRNAK( pcb ) ( isoDep_PCBisRBlock(pcb) && isoDep_PCBisNAK(pcb) ) /*!< Checks if pcb is R-Block indicating NAK */
  212. #define isoDep_PCBIBlock( bn ) ( (uint8_t)( 0x00U | ISODEP_PCB_IBLOCK | ISODEP_PCB_B2_BIT | ((bn) & ISODEP_PCB_BN_MASK) )) /*!< Returns an I-Block with the given block number (bn) */
  213. #define isoDep_PCBIBlockChaining( bn ) ( (uint8_t)(isoDep_PCBIBlock(bn) | ISODEP_PCB_CHAINING_BIT)) /*!< Returns an I-Block with the given block number (bn) indicating chaining */
  214. #define isoDep_PCBRBlock( bn ) ( (uint8_t)( 0x00U | ISODEP_PCB_RBLOCK | ISODEP_PCB_B6_BIT | ISODEP_PCB_B2_BIT | ((bn) & ISODEP_PCB_BN_MASK) ) ) /*!< Returns an R-Block with the given block number (bn) */
  215. #define isoDep_PCBRACK( bn ) ( (uint8_t)( isoDep_PCBRBlock( bn ) | ISODEP_PCB_ACK ) ) /*!< Returns an R-Block with the given block number (bn) indicating ACK */
  216. #define isoDep_PCBRNAK( bn ) ( (uint8_t)( isoDep_PCBRBlock( bn ) | ISODEP_PCB_NAK ) ) /*!< Returns an R-Block with the given block number (bn) indicating NAK */
  217. #define isoDep_GetBN( pcb ) ( (uint8_t) ((pcb) & ISODEP_PCB_BN_MASK ) ) /*!< Returns the block number (bn) from the given pcb */
  218. #define isoDep_GetWTXM( inf ) ( (uint8_t) ((inf) & ISODEP_SWTX_WTXM_MASK) ) /*!< Returns the WTX value from the given inf byte */
  219. #define isoDep_isWTXMValid( wtxm ) (((wtxm) >= ISODEP_WTXM_MIN) && ((wtxm) <= ISODEP_WTXM_MAX)) /*!< Checks if the given wtxm is valid */
  220. #define isoDep_WTXMListenerMax( fwt ) ( MIN( (uint8_t)(ISODEP_FWT_LIS_MAX / (fwt)), ISODEP_WTXM_MAX) ) /*!< Calculates the Max WTXM value for the given fwt as a Listener */
  221. #define isoDepCalcdSGFT( s ) (384U * ((uint32_t)1U << (s))) /*!< Calculates the dSFGT with given SFGI Digital 1.1 13.8.2.1 & A.6*/
  222. #define isoDepCalcSGFT( s ) (4096U * ((uint32_t)1U << (s))) /*!< Calculates the SFGT with given SFGI Digital 1.1 13.8.2 */
  223. #define isoDep_PCBNextBN( bn ) (((uint8_t)(bn)^0x01U) & ISODEP_PCB_BN_MASK) /*!< Returns the value of the next block number based on bn */
  224. #define isoDep_PCBPrevBN( bn ) isoDep_PCBNextBN(bn) /*!< Returns the value of the previous block number based on bn */
  225. #define isoDep_ToggleBN( bn ) ((bn) = (((bn)^0x01U) & ISODEP_PCB_BN_MASK) ) /*!< Toggles the block number value of the given bn */
  226. #define isoDep_WTXAdjust( v ) ((v) - ((v)>>3)) /*!< Adjust WTX timer value to a percentage of the total, current 88% */
  227. /*! ISO 14443-4 7.5.6.2 & Digital 1.1 - 15.2.6.2 The CE SHALL NOT attempt error recovery and remains in Rx mode upon Transmission or a Protocol Error */
  228. #define isoDepReEnableRx( rxB, rxBL, rxL ) rfalTransceiveBlockingTx( NULL, 0, rxB, rxBL, rxL, RFAL_TXRX_FLAGS_DEFAULT, RFAL_FWT_NONE )
  229. /*! Macro used for the blocking methods */
  230. #define rfalIsoDepRunBlocking( e, fn ) do{ (e)=(fn); rfalWorker(); }while( (e) == ERR_BUSY )
  231. #define isoDepTimerStart( timer, time_ms ) do{ platformTimerDestroy( timer ); (timer) = platformTimerCreate((uint16_t)(time_ms));} while (0) /*!< Configures and starts the WTX timer */
  232. #define isoDepTimerisExpired( timer ) platformTimerIsExpired( timer ) /*!< Checks WTX timer has expired */
  233. #define isoDepTimerDestroy( timer ) platformTimerDestroy( timer ) /*!< Destroys WTX timer */
  234. /*
  235. ******************************************************************************
  236. * LOCAL DATA TYPES
  237. ******************************************************************************
  238. */
  239. /*! Internal structure to be used in handling of S(PARAMETRS) only */
  240. typedef struct
  241. {
  242. uint8_t pcb; /*!< PCB byte */
  243. rfalIsoDepSParameter sParam; /*!< S(PARAMETERS) */
  244. } rfalIsoDepControlMsgSParam;
  245. /*! Enumeration of the possible control message types */
  246. typedef enum
  247. {
  248. ISODEP_R_ACK, /*!< R-ACK Acknowledge */
  249. ISODEP_R_NAK, /*!< R-NACK Negative acknowledge */
  250. ISODEP_S_WTX, /*!< S-WTX Waiting Time Extension */
  251. ISODEP_S_DSL /*!< S-DSL Deselect */
  252. } rfalIsoDepControlMsg;
  253. /*! Enumeration of the IsoDep roles */
  254. typedef enum
  255. {
  256. ISODEP_ROLE_PCD, /*!< Perform as Reader/PCD */
  257. ISODEP_ROLE_PICC /*!< Perform as Card/PICC */
  258. } rfalIsoDepRole;
  259. /*! ISO-DEP layer states */
  260. typedef enum
  261. {
  262. ISODEP_ST_IDLE, /*!< Idle State */
  263. ISODEP_ST_PCD_TX, /*!< PCD Transmission State */
  264. ISODEP_ST_PCD_RX, /*!< PCD Reception State */
  265. ISODEP_ST_PCD_WAIT_DSL, /*!< PCD Wait for DSL response */
  266. ISODEP_ST_PICC_ACT_ATS, /*!< PICC has replied to RATS (ATS) */
  267. ISODEP_ST_PICC_ACT_ATTRIB, /*!< PICC has replied to ATTRIB */
  268. ISODEP_ST_PICC_RX, /*!< PICC Reception State */
  269. ISODEP_ST_PICC_SWTX, /*!< PICC Waiting Time eXtension */
  270. ISODEP_ST_PICC_SDSL, /*!< PICC S(DSL) response ongoing */
  271. ISODEP_ST_PICC_TX, /*!< PICC Transmission State */
  272. ISODEP_ST_PCD_ACT_RATS, /*!< PCD activation (RATS) */
  273. ISODEP_ST_PCD_ACT_PPS, /*!< PCD activation (PPS) */
  274. } rfalIsoDepState;
  275. /*! Holds all ISO-DEP data(counters, buffers, ID, timeouts, frame size) */
  276. typedef struct{
  277. rfalIsoDepState state; /*!< ISO-DEP module state */
  278. rfalIsoDepRole role; /*!< Current ISO-DEP role */
  279. uint8_t blockNumber; /*!< Current block number */
  280. uint8_t did; /*!< Current DID */
  281. uint8_t nad; /*!< Current DID */
  282. uint8_t cntIRetrys; /*!< I-Block retry counter */
  283. uint8_t cntRRetrys; /*!< R-Block retry counter */
  284. uint8_t cntSDslRetrys; /*!< S(DESELECT) retry counter */
  285. uint8_t cntSWtxRetrys; /*!< Overall S(WTX) retry counter */
  286. uint8_t cntSWtxNack; /*!< R(NACK) answered with S(WTX) counter */
  287. uint32_t fwt; /*!< Current FWT (Frame Waiting Time) */
  288. uint32_t dFwt; /*!< Current delta FWT */
  289. uint16_t fsx; /*!< Current FSx FSC or FSD (max Frame size) */
  290. bool isTxChaining; /*!< Flag for chaining on Tx */
  291. bool isRxChaining; /*!< Flag for chaining on Rx */
  292. uint8_t* txBuf; /*!< Tx buffer pointer */
  293. uint8_t* rxBuf; /*!< Rx buffer pointer */
  294. uint16_t txBufLen; /*!< Tx buffer length */
  295. uint16_t rxBufLen; /*!< Rx buffer length */
  296. uint8_t txBufInfPos; /*!< Start of payload in txBuf */
  297. uint8_t rxBufInfPos; /*!< Start of payload in rxBuf */
  298. uint16_t ourFsx; /*!< Our current FSx FSC or FSD (Frame size) */
  299. uint8_t lastPCB; /*!< Last PCB sent */
  300. uint8_t lastWTXM; /*!< Last WTXM sent */
  301. uint8_t atsTA; /*!< TA on ATS */
  302. uint8_t hdrLen; /*!< Current ISO-DEP length */
  303. rfalBitRate txBR; /*!< Current Tx Bit Rate */
  304. rfalBitRate rxBR; /*!< Current Rx Bit Rate */
  305. uint16_t *rxLen; /*!< Output parameter ptr to Rx length */
  306. bool *rxChaining; /*!< Output parameter ptr to Rx chaining flag */
  307. uint32_t WTXTimer; /*!< Timer used for WTX */
  308. bool lastDID00; /*!< Last PCD block had DID flag (for DID = 0) */
  309. bool isTxPending; /*!< Flag pending Block while waiting WTX Ack */
  310. bool isWait4WTX; /*!< Flag for waiting WTX Ack */
  311. uint8_t maxRetriesI; /*!< Number of retries for a I-Block */
  312. uint8_t maxRetriesR; /*!< Number of retries for a R-Block */
  313. uint8_t maxRetriesSDSL;/*!< Number of retries for S(DESELECT) errors */
  314. uint8_t maxRetriesSWTX;/*!< Number of retries for S(WTX) errors */
  315. uint8_t maxRetriesSnWTX;/*!< Number of retries S(WTX) replied w NACK */
  316. uint8_t maxRetriesRATS;/*!< Number of retries for RATS */
  317. rfalComplianceMode compMode; /*!< Compliance mode */
  318. uint8_t ctrlBuf[ISODEP_CONTROLMSG_BUF_LEN]; /*!< Control msg buf */
  319. uint16_t ctrlRxLen; /*!< Control msg rcvd len */
  320. union { /* PRQA S 0750 # MISRA 19.2 - Members of the union will not be used concurrently, only one frame at a time */
  321. #if RFAL_FEATURE_NFCA
  322. rfalIsoDepRats ratsReq;
  323. rfalIsoDepPpsReq ppsReq;
  324. #endif /* RFAL_FEATURE_NFCA */
  325. #if RFAL_FEATURE_NFCB
  326. rfalIsoDepAttribCmd attribReq;
  327. #endif /* RFAL_FEATURE_NFCB */
  328. }actv; /*!< Activation buffer */
  329. uint8_t* rxLen8; /*!< Receive length (8-bit) */
  330. rfalIsoDepDevice* actvDev; /*!< Activation Device Info */
  331. rfalIsoDepListenActvParam actvParam; /*!< Listen Activation context */
  332. rfalIsoDepApduTxRxParam APDUParam; /*!< APDU TxRx params */
  333. uint16_t APDUTxPos; /*!< APDU Tx position */
  334. uint16_t APDURxPos; /*!< APDU Rx position */
  335. bool isAPDURxChaining; /*!< APDU Transceive chaining flag */
  336. }rfalIsoDep;
  337. /*
  338. ******************************************************************************
  339. * LOCAL VARIABLES
  340. ******************************************************************************
  341. */
  342. static rfalIsoDep gIsoDep; /*!< ISO-DEP Module instance */
  343. /*
  344. ******************************************************************************
  345. * LOCAL FUNCTION PROTOTYPES
  346. ******************************************************************************
  347. */
  348. static void isoDepClearCounters( void );
  349. static ReturnCode isoDepTx( uint8_t pcb, const uint8_t* txBuf, uint8_t *infBuf, uint16_t infLen, uint32_t fwt );
  350. static ReturnCode isoDepHandleControlMsg( rfalIsoDepControlMsg controlMsg, uint8_t param );
  351. static void rfalIsoDepApdu2IBLockParam( rfalIsoDepApduTxRxParam apduParam, rfalIsoDepTxRxParam *iBlockParam, uint16_t txPos, uint16_t rxPos );
  352. #if RFAL_FEATURE_ISO_DEP_POLL
  353. static ReturnCode isoDepDataExchangePCD( uint16_t *outActRxLen, bool *outIsChaining );
  354. static void rfalIsoDepCalcBitRate(rfalBitRate maxAllowedBR, uint8_t piccBRCapability, rfalBitRate *dsi, rfalBitRate *dri);
  355. static uint32_t rfalIsoDepSFGI2SFGT( uint8_t sfgi );
  356. #if RFAL_FEATURE_NFCA
  357. static ReturnCode rfalIsoDepStartRATS( rfalIsoDepFSxI FSDI, uint8_t DID, rfalIsoDepAts *ats, uint8_t *atsLen );
  358. static ReturnCode rfalIsoDepGetRATSStatus( void );
  359. static ReturnCode rfalIsoDepStartPPS( uint8_t DID, rfalBitRate DSI, rfalBitRate DRI, rfalIsoDepPpsRes *ppsRes );
  360. static ReturnCode rfalIsoDepGetPPSSTatus( void );
  361. #endif /* RFAL_FEATURE_NFCA */
  362. #if RFAL_FEATURE_NFCB
  363. static ReturnCode rfalIsoDepStartATTRIB( const uint8_t* nfcid0, uint8_t PARAM1, rfalBitRate DSI, rfalBitRate DRI, rfalIsoDepFSxI FSDI, uint8_t PARAM3, uint8_t DID, const uint8_t* HLInfo, uint8_t HLInfoLen, uint32_t fwt, rfalIsoDepAttribRes *attribRes, uint8_t *attribResLen );
  364. static ReturnCode rfalIsoDepGetATTRIBStatus( void );
  365. #endif /* RFAL_FEATURE_NFCB */
  366. #endif /* RFAL_FEATURE_ISO_DEP_POLL */
  367. #if RFAL_FEATURE_ISO_DEP_LISTEN
  368. static ReturnCode isoDepDataExchangePICC( void );
  369. static ReturnCode isoDepReSendControlMsg( void );
  370. #endif
  371. /*
  372. ******************************************************************************
  373. * LOCAL FUNCTIONS
  374. ******************************************************************************
  375. */
  376. /*******************************************************************************/
  377. static void isoDepClearCounters( void )
  378. {
  379. gIsoDep.cntIRetrys = 0;
  380. gIsoDep.cntRRetrys = 0;
  381. gIsoDep.cntSDslRetrys = 0;
  382. gIsoDep.cntSWtxRetrys = 0;
  383. gIsoDep.cntSWtxNack = 0;
  384. }
  385. /*******************************************************************************/
  386. static ReturnCode isoDepTx( uint8_t pcb, const uint8_t* txBuf, uint8_t *infBuf, uint16_t infLen, uint32_t fwt )
  387. {
  388. uint8_t *txBlock;
  389. uint16_t txBufLen;
  390. uint8_t computedPcb;
  391. rfalTransceiveContext ctx;
  392. txBlock = infBuf; /* Point to beginning of the INF, and go backwards */
  393. gIsoDep.lastPCB = pcb; /* Store the last PCB sent */
  394. if ( infLen > 0U )
  395. {
  396. if ( ((uint32_t)infBuf - (uint32_t)txBuf) < gIsoDep.hdrLen ) /* Check that we can fit the header in the given space */
  397. {
  398. return ERR_NOMEM;
  399. }
  400. }
  401. /*******************************************************************************/
  402. /* Compute optional PCB bits */
  403. computedPcb = pcb;
  404. if ((gIsoDep.did != RFAL_ISODEP_NO_DID) || ((gIsoDep.did == RFAL_ISODEP_DID_00) && gIsoDep.lastDID00) ) { computedPcb |= ISODEP_PCB_DID_BIT; }
  405. if (gIsoDep.nad != RFAL_ISODEP_NO_NAD) { computedPcb |= ISODEP_PCB_NAD_BIT; }
  406. if ((gIsoDep.isTxChaining) && (isoDep_PCBisIBlock(computedPcb)) ) { computedPcb |= ISODEP_PCB_CHAINING_BIT; }
  407. /*******************************************************************************/
  408. /* Compute Payload on the given txBuf, start by the PCB | DID | NAD | before INF */
  409. if (gIsoDep.nad != RFAL_ISODEP_NO_NAD)
  410. {
  411. *(--txBlock) = gIsoDep.nad; /* NAD is optional */
  412. }
  413. if ( (gIsoDep.did != RFAL_ISODEP_NO_DID) || ((gIsoDep.did == RFAL_ISODEP_DID_00) && gIsoDep.lastDID00) )
  414. {
  415. *(--txBlock) = gIsoDep.did; /* DID is optional */
  416. }
  417. *(--txBlock) = computedPcb; /* PCB always present */
  418. txBufLen = (infLen + (uint16_t)((uint32_t)infBuf - (uint32_t)txBlock)); /* Calculate overall buffer size */
  419. if ( txBufLen > (gIsoDep.fsx - ISODEP_CRC_LEN) ) /* Check if msg length violates the maximum frame size FSC */
  420. {
  421. return ERR_NOTSUPP;
  422. }
  423. rfalCreateByteFlagsTxRxContext( ctx, txBlock, txBufLen, gIsoDep.rxBuf, gIsoDep.rxBufLen, gIsoDep.rxLen, RFAL_TXRX_FLAGS_DEFAULT, ((gIsoDep.role == ISODEP_ROLE_PICC) ? RFAL_FWT_NONE : fwt ) );
  424. return rfalStartTransceive( &ctx );
  425. }
  426. /*******************************************************************************/
  427. static ReturnCode isoDepHandleControlMsg( rfalIsoDepControlMsg controlMsg, uint8_t param )
  428. {
  429. uint8_t pcb;
  430. uint8_t infLen;
  431. uint32_t fwtTemp;
  432. infLen = 0;
  433. fwtTemp = (gIsoDep.fwt + gIsoDep.dFwt);
  434. ST_MEMSET( gIsoDep.ctrlBuf, 0x00, ISODEP_CONTROLMSG_BUF_LEN );
  435. switch( controlMsg )
  436. {
  437. /*******************************************************************************/
  438. case ISODEP_R_ACK:
  439. if( gIsoDep.cntRRetrys++ > gIsoDep.maxRetriesR )
  440. {
  441. return ERR_TIMEOUT; /* NFC Forum mandates timeout or transmission error depending on previous errors */
  442. }
  443. pcb = isoDep_PCBRACK( gIsoDep.blockNumber );
  444. break;
  445. /*******************************************************************************/
  446. case ISODEP_R_NAK:
  447. if( ( gIsoDep.cntRRetrys++ > gIsoDep.maxRetriesR ) || /* Max R Block retries reached */
  448. ( gIsoDep.cntSWtxNack >= gIsoDep.maxRetriesSnWTX ) ) /* Max number PICC is allowed to respond with S(WTX) to R(NAK) */
  449. {
  450. return ERR_TIMEOUT;
  451. }
  452. pcb = isoDep_PCBRNAK( gIsoDep.blockNumber );
  453. break;
  454. /*******************************************************************************/
  455. case ISODEP_S_WTX:
  456. if( (gIsoDep.cntSWtxRetrys++ > gIsoDep.maxRetriesSWTX) && (gIsoDep.maxRetriesSWTX != RFAL_ISODEP_MAX_WTX_RETRYS_ULTD) )
  457. {
  458. return ERR_PROTO;
  459. }
  460. /* Check if WTXM is valid */
  461. if( ! isoDep_isWTXMValid(param) )
  462. {
  463. return ERR_PROTO;
  464. }
  465. if( gIsoDep.role == ISODEP_ROLE_PCD )
  466. {
  467. /* Calculate temp Wait Time eXtension */
  468. fwtTemp = (gIsoDep.fwt * param);
  469. fwtTemp = MIN( RFAL_ISODEP_MAX_FWT, fwtTemp );
  470. fwtTemp += gIsoDep.dFwt;
  471. }
  472. pcb = ISODEP_PCB_SWTX;
  473. gIsoDep.ctrlBuf[ RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN + infLen++] = param;
  474. break;
  475. /*******************************************************************************/
  476. case ISODEP_S_DSL:
  477. if( gIsoDep.cntSDslRetrys++ > gIsoDep.maxRetriesSDSL )
  478. {
  479. return ERR_TIMEOUT; /* NFC Forum mandates timeout or transmission error depending on previous errors */
  480. }
  481. if( gIsoDep.role == ISODEP_ROLE_PCD )
  482. {
  483. /* Digital 1.0 - 13.2.7.3 Poller must wait fwtDEACTIVATION */
  484. fwtTemp = ISODEP_FWT_DEACTIVATION;
  485. gIsoDep.state = ISODEP_ST_PCD_WAIT_DSL;
  486. }
  487. pcb = ISODEP_PCB_SDSL;
  488. break;
  489. /*******************************************************************************/
  490. default:
  491. return ERR_INTERNAL;
  492. }
  493. return isoDepTx( pcb, gIsoDep.ctrlBuf, &gIsoDep.ctrlBuf[RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN], infLen, fwtTemp );
  494. }
  495. #if RFAL_FEATURE_ISO_DEP_LISTEN
  496. /*******************************************************************************/
  497. static ReturnCode isoDepReSendControlMsg( void )
  498. {
  499. if( isoDep_PCBisRACK( gIsoDep.lastPCB ) )
  500. {
  501. return isoDepHandleControlMsg( ISODEP_R_ACK, RFAL_ISODEP_NO_PARAM );
  502. }
  503. if( isoDep_PCBisRNAK( gIsoDep.lastPCB ) )
  504. {
  505. return isoDepHandleControlMsg( ISODEP_R_NAK, RFAL_ISODEP_NO_PARAM );
  506. }
  507. if( isoDep_PCBisSDeselect( gIsoDep.lastPCB ) )
  508. {
  509. return isoDepHandleControlMsg( ISODEP_S_DSL, RFAL_ISODEP_NO_PARAM );
  510. }
  511. if( isoDep_PCBisSWTX( gIsoDep.lastPCB ) )
  512. {
  513. return isoDepHandleControlMsg( ISODEP_S_WTX, gIsoDep.lastWTXM );
  514. }
  515. return ERR_WRONG_STATE;
  516. }
  517. #endif /* RFAL_FEATURE_ISO_DEP_LISTEN */
  518. /*
  519. ******************************************************************************
  520. * GLOBAL FUNCTIONS
  521. ******************************************************************************
  522. */
  523. /*******************************************************************************/
  524. void rfalIsoDepInitialize( void )
  525. {
  526. gIsoDep.state = ISODEP_ST_IDLE;
  527. gIsoDep.role = ISODEP_ROLE_PCD;
  528. gIsoDep.did = RFAL_ISODEP_NO_DID;
  529. gIsoDep.nad = RFAL_ISODEP_NO_NAD;
  530. gIsoDep.blockNumber = 0;
  531. gIsoDep.isTxChaining = false;
  532. gIsoDep.isRxChaining = false;
  533. gIsoDep.lastDID00 = false;
  534. gIsoDep.lastPCB = ISODEP_PCB_INVALID;
  535. gIsoDep.fsx = (uint16_t)RFAL_ISODEP_FSX_16;
  536. gIsoDep.ourFsx = (uint16_t)RFAL_ISODEP_FSX_16;
  537. gIsoDep.hdrLen = RFAL_ISODEP_PCB_LEN;
  538. gIsoDep.rxLen = NULL;
  539. gIsoDep.rxBuf = NULL;
  540. gIsoDep.rxBufInfPos = 0U;
  541. gIsoDep.txBufInfPos = 0U;
  542. gIsoDep.isTxPending = false;
  543. gIsoDep.isWait4WTX = false;
  544. gIsoDep.compMode = RFAL_COMPLIANCE_MODE_NFC;
  545. gIsoDep.maxRetriesR = RFAL_ISODEP_MAX_R_RETRYS;
  546. gIsoDep.maxRetriesI = RFAL_ISODEP_MAX_I_RETRYS;
  547. gIsoDep.maxRetriesSDSL = RFAL_ISODEP_MAX_DSL_RETRYS;
  548. gIsoDep.maxRetriesSWTX = RFAL_ISODEP_MAX_WTX_RETRYS;
  549. gIsoDep.maxRetriesSnWTX = RFAL_ISODEP_MAX_WTX_NACK_RETRYS;
  550. gIsoDep.maxRetriesRATS = RFAL_ISODEP_RATS_RETRIES;
  551. gIsoDep.APDURxPos = 0;
  552. gIsoDep.APDUTxPos = 0;
  553. gIsoDep.APDUParam.rxLen = NULL;
  554. gIsoDep.APDUParam.rxBuf = NULL;
  555. gIsoDep.APDUParam.txBuf = NULL;
  556. isoDepClearCounters();
  557. /* Destroy any ongoing WTX timer */
  558. isoDepTimerDestroy( gIsoDep.WTXTimer );
  559. gIsoDep.WTXTimer = 0U;
  560. }
  561. /*******************************************************************************/
  562. void rfalIsoDepInitializeWithParams( rfalComplianceMode compMode,
  563. uint8_t maxRetriesR,
  564. uint8_t maxRetriesSnWTX,
  565. uint8_t maxRetriesSWTX,
  566. uint8_t maxRetriesSDSL,
  567. uint8_t maxRetriesI,
  568. uint8_t maxRetriesRATS )
  569. {
  570. rfalIsoDepInitialize();
  571. gIsoDep.compMode = compMode;
  572. gIsoDep.maxRetriesR = maxRetriesR;
  573. gIsoDep.maxRetriesSnWTX = maxRetriesSnWTX;
  574. gIsoDep.maxRetriesSWTX = maxRetriesSWTX;
  575. gIsoDep.maxRetriesSDSL = maxRetriesSDSL;
  576. gIsoDep.maxRetriesI = maxRetriesI;
  577. gIsoDep.maxRetriesRATS = maxRetriesRATS;
  578. }
  579. #if RFAL_FEATURE_ISO_DEP_POLL
  580. /*******************************************************************************/
  581. static ReturnCode isoDepDataExchangePCD( uint16_t *outActRxLen, bool *outIsChaining )
  582. {
  583. ReturnCode ret;
  584. uint8_t rxPCB;
  585. /* Check out parameters */
  586. if( (outActRxLen == NULL) || (outIsChaining == NULL) )
  587. {
  588. return ERR_PARAM;
  589. }
  590. *outIsChaining = false;
  591. /* Calculate header required and check if the buffers InfPositions are suitable */
  592. gIsoDep.hdrLen = RFAL_ISODEP_PCB_LEN;
  593. if (gIsoDep.did != RFAL_ISODEP_NO_DID) { gIsoDep.hdrLen += RFAL_ISODEP_DID_LEN; }
  594. if (gIsoDep.nad != RFAL_ISODEP_NO_NAD) { gIsoDep.hdrLen += RFAL_ISODEP_NAD_LEN; }
  595. /* Check if there is enough space before the infPos to append ISO-DEP headers on rx and tx */
  596. if( (gIsoDep.rxBufInfPos < gIsoDep.hdrLen) || (gIsoDep.txBufInfPos < gIsoDep.hdrLen) )
  597. {
  598. return ERR_PARAM;
  599. }
  600. /*******************************************************************************/
  601. switch( gIsoDep.state )
  602. {
  603. /*******************************************************************************/
  604. case ISODEP_ST_IDLE:
  605. return ERR_NONE;
  606. /*******************************************************************************/
  607. case ISODEP_ST_PCD_TX:
  608. ret = isoDepTx( isoDep_PCBIBlock( gIsoDep.blockNumber ), gIsoDep.txBuf, &gIsoDep.txBuf[gIsoDep.txBufInfPos], gIsoDep.txBufLen, (gIsoDep.fwt + gIsoDep.dFwt) );
  609. switch( ret )
  610. {
  611. case ERR_NONE:
  612. gIsoDep.state = ISODEP_ST_PCD_RX;
  613. break;
  614. default:
  615. return ret;
  616. }
  617. /* fall through */
  618. /*******************************************************************************/
  619. case ISODEP_ST_PCD_WAIT_DSL: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  620. case ISODEP_ST_PCD_RX:
  621. ret = rfalGetTransceiveStatus();
  622. switch( ret )
  623. {
  624. /* Data rcvd with error or timeout -> Send R-NAK */
  625. case ERR_TIMEOUT:
  626. case ERR_CRC:
  627. case ERR_PAR:
  628. case ERR_FRAMING: /* added to handle test cases scenario TC_POL_NFCB_T4AT_BI_82_x_y & TC_POL_NFCB_T4BT_BI_82_x_y */
  629. case ERR_INCOMPLETE_BYTE: /* added to handle test cases scenario TC_POL_NFCB_T4AT_BI_82_x_y & TC_POL_NFCB_T4BT_BI_82_x_y */
  630. if( gIsoDep.isRxChaining )
  631. { /* Rule 5 - In PICC chaining when a invalid/timeout occurs -> R-ACK */
  632. EXIT_ON_ERR( ret, isoDepHandleControlMsg( ISODEP_R_ACK, RFAL_ISODEP_NO_PARAM ) );
  633. }
  634. else if( gIsoDep.state == ISODEP_ST_PCD_WAIT_DSL )
  635. { /* Rule 8 - If s-Deselect response fails MAY retransmit */
  636. EXIT_ON_ERR( ret, isoDepHandleControlMsg( ISODEP_S_DSL, RFAL_ISODEP_NO_PARAM ) );
  637. }
  638. else
  639. { /* Rule 4 - When a invalid block or timeout occurs -> R-NACK */
  640. EXIT_ON_ERR( ret, isoDepHandleControlMsg( ISODEP_R_NAK, RFAL_ISODEP_NO_PARAM ) );
  641. }
  642. return ERR_BUSY;
  643. case ERR_NONE:
  644. break;
  645. case ERR_BUSY:
  646. return ERR_BUSY; /* Debug purposes */
  647. default:
  648. return ret;
  649. }
  650. /*******************************************************************************/
  651. /* No error, process incoming msg */
  652. /*******************************************************************************/
  653. (*outActRxLen) = rfalConvBitsToBytes( *outActRxLen );
  654. /* Check rcvd msg length, cannot be less then the expected header */
  655. if( ((*outActRxLen) < gIsoDep.hdrLen) || ((*outActRxLen) >= gIsoDep.ourFsx) )
  656. {
  657. return ERR_PROTO;
  658. }
  659. /* Grab rcvd PCB */
  660. rxPCB = gIsoDep.rxBuf[ ISODEP_PCB_POS ];
  661. /* EMVCo doesn't allow usage of for CID or NAD EMVCo 2.6 TAble 10.2 */
  662. if( (gIsoDep.compMode == RFAL_COMPLIANCE_MODE_EMV) && ( isoDep_PCBhasDID(rxPCB) || isoDep_PCBhasNAD(rxPCB)) )
  663. {
  664. return ERR_PROTO;
  665. }
  666. /* If we are expecting DID, check if PCB signals its presence and if device ID match*/
  667. if( (gIsoDep.did != RFAL_ISODEP_NO_DID) && ( !isoDep_PCBhasDID(rxPCB) || (gIsoDep.did != gIsoDep.rxBuf[ ISODEP_DID_POS ])) )
  668. {
  669. return ERR_PROTO;
  670. }
  671. /*******************************************************************************/
  672. /* Process S-Block */
  673. /*******************************************************************************/
  674. if( isoDep_PCBisSBlock(rxPCB) )
  675. {
  676. /* Check if is a Wait Time eXtension */
  677. if( isoDep_PCBisSWTX(rxPCB) )
  678. {
  679. /* Check if PICC has requested S(WTX) as response to R(NAK) EMVCo 3.0 10.3.5.5 / Digital 2.0 16.2.6.5 */
  680. if( isoDep_PCBisRNAK( gIsoDep.lastPCB ) )
  681. {
  682. gIsoDep.cntSWtxNack++; /* Count S(WTX) upon R(NAK) */
  683. gIsoDep.cntRRetrys = 0; /* Reset R-Block counter has PICC has responded */
  684. }
  685. else
  686. {
  687. gIsoDep.cntSWtxNack = 0; /* Reset R(NACK)->S(WTX) counter */
  688. }
  689. /* Rule 3 - respond to S-block: get 1st INF byte S(STW): Power + WTXM */
  690. EXIT_ON_ERR( ret, isoDepHandleControlMsg( ISODEP_S_WTX, isoDep_GetWTXM(gIsoDep.rxBuf[gIsoDep.hdrLen]) ) );
  691. return ERR_BUSY;
  692. }
  693. /* Check if is a deselect response */
  694. if( isoDep_PCBisSDeselect(rxPCB) )
  695. {
  696. if( gIsoDep.state == ISODEP_ST_PCD_WAIT_DSL )
  697. {
  698. rfalIsoDepInitialize(); /* Session finished reInit vars */
  699. return ERR_NONE;
  700. }
  701. /* Deselect response not expected */
  702. /* fall through to PROTO error */
  703. }
  704. /* Unexpected S-Block */
  705. return ERR_PROTO;
  706. }
  707. /*******************************************************************************/
  708. /* Process R-Block */
  709. /*******************************************************************************/
  710. else if( isoDep_PCBisRBlock(rxPCB) )
  711. {
  712. if( isoDep_PCBisRACK(rxPCB) ) /* Check if is a R-ACK */
  713. {
  714. if( isoDep_GetBN(rxPCB) == gIsoDep.blockNumber ) /* Expected block number */
  715. {
  716. /* Rule B - ACK with expected bn -> Increment block number */
  717. gIsoDep.blockNumber = isoDep_PCBNextBN( gIsoDep.blockNumber );
  718. /* R-ACK only allowed when PCD chaining */
  719. if( !gIsoDep.isTxChaining )
  720. {
  721. return ERR_PROTO;
  722. }
  723. /* Rule 7 - Chaining transaction done, continue chaining */
  724. isoDepClearCounters();
  725. return ERR_NONE; /* This block has been transmitted */
  726. }
  727. else
  728. {
  729. /* Rule 6 - R-ACK with wrong block number retransmit */
  730. /* Digital 2.0 16.2.5.4 - Retransmit maximum two times */
  731. /* EMVCo 3.0 10.3.4.3 - PCD may re-transmit the last I-Block or report error */
  732. if( gIsoDep.cntIRetrys++ < gIsoDep.maxRetriesI )
  733. {
  734. gIsoDep.cntRRetrys = 0; /* Clear R counter only */
  735. gIsoDep.state = ISODEP_ST_PCD_TX;
  736. return ERR_BUSY;
  737. }
  738. return ERR_TIMEOUT; /* NFC Forum mandates timeout or transmission error depending on previous errors */
  739. }
  740. }
  741. else /* Unexcpected R-Block */
  742. {
  743. return ERR_PROTO;
  744. }
  745. }
  746. /*******************************************************************************/
  747. /* Process I-Block */
  748. /*******************************************************************************/
  749. else if( isoDep_PCBisIBlock(rxPCB) )
  750. {
  751. /*******************************************************************************/
  752. /* is PICC performing chaining */
  753. if( isoDep_PCBisChaining(rxPCB) )
  754. {
  755. gIsoDep.isRxChaining = true;
  756. *outIsChaining = true;
  757. if( isoDep_GetBN(rxPCB) == gIsoDep.blockNumber )
  758. {
  759. /* Rule B - ACK with correct block number -> Increase Block number */
  760. isoDep_ToggleBN( gIsoDep.blockNumber );
  761. isoDepClearCounters(); /* Clear counters in case R counter is already at max */
  762. /* Rule 2 - Send ACK */
  763. EXIT_ON_ERR( ret, isoDepHandleControlMsg( ISODEP_R_ACK, RFAL_ISODEP_NO_PARAM ) );
  764. /* Received I-Block with chaining, send current data to DH */
  765. /* remove ISO DEP header, check is necessary to move the INF data on the buffer */
  766. *outActRxLen -= gIsoDep.hdrLen;
  767. if( (gIsoDep.hdrLen != gIsoDep.rxBufInfPos) && (*outActRxLen > 0U) )
  768. {
  769. ST_MEMMOVE( &gIsoDep.rxBuf[gIsoDep.rxBufInfPos], &gIsoDep.rxBuf[gIsoDep.hdrLen], *outActRxLen );
  770. }
  771. isoDepClearCounters();
  772. return ERR_AGAIN; /* Send Again signalling to run again, but some chaining data has arrived */
  773. }
  774. else
  775. {
  776. /* Rule 5 - PICC chaining invalid I-Block -> R-ACK */
  777. EXIT_ON_ERR( ret, isoDepHandleControlMsg( ISODEP_R_ACK, RFAL_ISODEP_NO_PARAM ) );
  778. }
  779. return ERR_BUSY;
  780. }
  781. gIsoDep.isRxChaining = false; /* clear PICC chaining flag */
  782. if( isoDep_GetBN(rxPCB) == gIsoDep.blockNumber )
  783. {
  784. /* Rule B - I-Block with correct block number -> Increase Block number */
  785. isoDep_ToggleBN( gIsoDep.blockNumber );
  786. /* I-Block transaction done successfully */
  787. /* remove ISO DEP header, check is necessary to move the INF data on the buffer */
  788. *outActRxLen -= gIsoDep.hdrLen;
  789. if( (gIsoDep.hdrLen != gIsoDep.rxBufInfPos) && (*outActRxLen > 0U) )
  790. {
  791. ST_MEMMOVE( &gIsoDep.rxBuf[gIsoDep.rxBufInfPos], &gIsoDep.rxBuf[gIsoDep.hdrLen], *outActRxLen );
  792. }
  793. gIsoDep.state = ISODEP_ST_IDLE;
  794. isoDepClearCounters();
  795. return ERR_NONE;
  796. }
  797. else
  798. {
  799. if( (gIsoDep.compMode != RFAL_COMPLIANCE_MODE_ISO) )
  800. {
  801. /* Invalid Block (not chaining) -> Raise error Digital 1.1 15.2.6.4 EMVCo 2.6 10.3.5.4 */
  802. return ERR_PROTO;
  803. }
  804. /* Rule 4 - Invalid Block -> R-NAK */
  805. EXIT_ON_ERR( ret, isoDepHandleControlMsg( ISODEP_R_NAK, RFAL_ISODEP_NO_PARAM ) );
  806. return ERR_BUSY;
  807. }
  808. }
  809. else /* not S/R/I - Block */
  810. {
  811. return ERR_PROTO;
  812. }
  813. /* fall through */
  814. /*******************************************************************************/
  815. default: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  816. /* MISRA 16.4: no empty default (comment will suffice) */
  817. break;
  818. }
  819. return ERR_INTERNAL;
  820. }
  821. /*******************************************************************************/
  822. ReturnCode rfalIsoDepDeselect( void )
  823. {
  824. ReturnCode ret;
  825. uint32_t cntRerun;
  826. bool dummyB;
  827. /*******************************************************************************/
  828. /* Using local static vars and static config to cope with a Deselect after *
  829. * RATS\ATTRIB without any I-Block exchanged */
  830. gIsoDep.rxLen = &gIsoDep.ctrlRxLen;
  831. gIsoDep.rxBuf = gIsoDep.ctrlBuf;
  832. gIsoDep.rxBufLen = ISODEP_CONTROLMSG_BUF_LEN - (RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN);
  833. gIsoDep.rxBufInfPos = (RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN);
  834. gIsoDep.txBufInfPos = (RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN);
  835. /*******************************************************************************/
  836. /* The Deselect process is being done blocking, Digital 1.0 - 13.2.7.1 MUST wait response and retry*/
  837. /* Set the maximum reruns while we will wait for a response */
  838. cntRerun = ISODEP_MAX_RERUNS;
  839. /* Send DSL request and run protocol until get a response, error or "timeout" */
  840. EXIT_ON_ERR( ret, isoDepHandleControlMsg( ISODEP_S_DSL, RFAL_ISODEP_NO_PARAM ));
  841. do{
  842. ret = isoDepDataExchangePCD( gIsoDep.rxLen, &dummyB );
  843. rfalWorker();
  844. }
  845. while( ((cntRerun--) != 0U) && (ret == ERR_BUSY) );
  846. rfalIsoDepInitialize();
  847. return ((cntRerun == 0U) ? ERR_TIMEOUT : ret);
  848. }
  849. #endif /* RFAL_FEATURE_ISO_DEP_POLL */
  850. /*******************************************************************************/
  851. uint32_t rfalIsoDepFWI2FWT( uint8_t fwi )
  852. {
  853. uint32_t result;
  854. uint8_t tmpFWI;
  855. tmpFWI = fwi;
  856. /* RFU values -> take the default value
  857. * Digital 1.0 11.6.2.17 FWI[1,14]
  858. * Digital 1.1 7.6.2.22 FWI[0,14]
  859. * EMVCo 2.6 Table A.5 FWI[0,14] */
  860. if( tmpFWI > ISODEP_FWI_MAX )
  861. {
  862. tmpFWI = RFAL_ISODEP_FWI_DEFAULT;
  863. }
  864. /* FWT = (256 x 16/fC) x 2^FWI => 2^(FWI+12) Digital 1.1 13.8.1 & 7.9.1 */
  865. result = ((uint32_t)1U << (tmpFWI + 12U));
  866. result = MIN( RFAL_ISODEP_MAX_FWT, result); /* Maximum Frame Waiting Time must be fulfilled */
  867. return result;
  868. }
  869. /*******************************************************************************/
  870. uint16_t rfalIsoDepFSxI2FSx( uint8_t FSxI )
  871. {
  872. uint16_t fsx;
  873. uint8_t fsi;
  874. /* Enforce maximum FSxI/FSx allowed - NFC Forum and EMVCo differ */
  875. fsi = (( gIsoDep.compMode == RFAL_COMPLIANCE_MODE_EMV ) ? MIN( FSxI, RFAL_ISODEP_FSDI_MAX_EMV ) : MIN( FSxI, RFAL_ISODEP_FSDI_MAX_NFC ));
  876. switch( fsi )
  877. {
  878. case (uint8_t)RFAL_ISODEP_FSXI_16: fsx = (uint16_t)RFAL_ISODEP_FSX_16; break;
  879. case (uint8_t)RFAL_ISODEP_FSXI_24: fsx = (uint16_t)RFAL_ISODEP_FSX_24; break;
  880. case (uint8_t)RFAL_ISODEP_FSXI_32: fsx = (uint16_t)RFAL_ISODEP_FSX_32; break;
  881. case (uint8_t)RFAL_ISODEP_FSXI_40: fsx = (uint16_t)RFAL_ISODEP_FSX_40; break;
  882. case (uint8_t)RFAL_ISODEP_FSXI_48: fsx = (uint16_t)RFAL_ISODEP_FSX_48; break;
  883. case (uint8_t)RFAL_ISODEP_FSXI_64: fsx = (uint16_t)RFAL_ISODEP_FSX_64; break;
  884. case (uint8_t)RFAL_ISODEP_FSXI_96: fsx = (uint16_t)RFAL_ISODEP_FSX_96; break;
  885. case (uint8_t)RFAL_ISODEP_FSXI_128: fsx = (uint16_t)RFAL_ISODEP_FSX_128; break;
  886. case (uint8_t)RFAL_ISODEP_FSXI_256: fsx = (uint16_t)RFAL_ISODEP_FSX_256; break;
  887. case (uint8_t)RFAL_ISODEP_FSXI_512: fsx = (uint16_t)RFAL_ISODEP_FSX_512; break;
  888. case (uint8_t)RFAL_ISODEP_FSXI_1024: fsx = (uint16_t)RFAL_ISODEP_FSX_1024; break;
  889. case (uint8_t)RFAL_ISODEP_FSXI_2048: fsx = (uint16_t)RFAL_ISODEP_FSX_2048; break;
  890. case (uint8_t)RFAL_ISODEP_FSXI_4096: fsx = (uint16_t)RFAL_ISODEP_FSX_4096; break;
  891. default: fsx = (uint16_t)RFAL_ISODEP_FSX_256; break;
  892. }
  893. return fsx;
  894. }
  895. #if RFAL_FEATURE_ISO_DEP_LISTEN
  896. /*******************************************************************************/
  897. bool rfalIsoDepIsRats( const uint8_t *buf, uint8_t bufLen )
  898. {
  899. if(buf != NULL)
  900. {
  901. if ((RFAL_ISODEP_CMD_RATS == (uint8_t)*buf) && (sizeof(rfalIsoDepRats) == bufLen))
  902. {
  903. return true;
  904. }
  905. }
  906. return false;
  907. }
  908. /*******************************************************************************/
  909. bool rfalIsoDepIsAttrib( const uint8_t *buf, uint8_t bufLen )
  910. {
  911. if(buf != NULL)
  912. {
  913. if ( (RFAL_ISODEP_CMD_ATTRIB == (uint8_t)*buf) &&
  914. (RFAL_ISODEP_ATTRIB_REQ_MIN_LEN <= bufLen) &&
  915. ((RFAL_ISODEP_ATTRIB_REQ_MIN_LEN + RFAL_ISODEP_ATTRIB_HLINFO_LEN) >= bufLen) )
  916. {
  917. return true;
  918. }
  919. }
  920. return false;
  921. }
  922. /*******************************************************************************/
  923. ReturnCode rfalIsoDepListenStartActivation( rfalIsoDepAtsParam *atsParam, const rfalIsoDepAttribResParam *attribResParam, const uint8_t *buf, uint16_t bufLen, rfalIsoDepListenActvParam actParam)
  924. {
  925. uint8_t *txBuf;
  926. uint8_t bufIt;
  927. const uint8_t *buffer = buf;
  928. /*******************************************************************************/
  929. bufIt = 0;
  930. txBuf = (uint8_t*)actParam.rxBuf; /* Use the rxBuf as TxBuf as well, the struct enforces a size enough MAX( NFCA_ATS_MAX_LEN, NFCB_ATTRIB_RES_MAX_LEN ) */
  931. gIsoDep.txBR = RFAL_BR_106;
  932. gIsoDep.rxBR = RFAL_BR_106;
  933. /* Check for a valid buffer pointer */
  934. if( buffer == NULL )
  935. {
  936. return ERR_PARAM;
  937. }
  938. /*******************************************************************************/
  939. if( *buffer == RFAL_ISODEP_CMD_RATS )
  940. {
  941. /* Check ATS parameters */
  942. if( atsParam == NULL )
  943. {
  944. return ERR_PARAM;
  945. }
  946. /* If requested copy RATS to device info */
  947. if( actParam.isoDepDev != NULL )
  948. {
  949. ST_MEMCPY( (uint8_t*)&actParam.isoDepDev->activation.A.Poller.RATS, buffer, sizeof(rfalIsoDepRats) ); /* Copy RATS' CMD + PARAM */
  950. }
  951. /*******************************************************************************/
  952. /* Process RATS */
  953. buffer++;
  954. gIsoDep.fsx = rfalIsoDepFSxI2FSx( (((*buffer) & RFAL_ISODEP_RATS_PARAM_FSDI_MASK) >> RFAL_ISODEP_RATS_PARAM_FSDI_SHIFT) );
  955. gIsoDep.did = (*buffer & RFAL_ISODEP_DID_MASK);
  956. /*******************************************************************************/
  957. /* Digital 1.1 13.6.1.8 - DID as to between 0 and 14 */
  958. if( gIsoDep.did > RFAL_ISODEP_DID_MAX )
  959. {
  960. return ERR_PROTO;
  961. }
  962. /* Check if we are configured to support DID */
  963. if( (gIsoDep.did != RFAL_ISODEP_DID_00) && (!atsParam->didSupport) )
  964. {
  965. return ERR_NOTSUPP;
  966. }
  967. /*******************************************************************************/
  968. /* Check RFAL supported bit rates */
  969. if( (!(RFAL_SUPPORT_BR_CE_A_212) && (((atsParam->ta & RFAL_ISODEP_ATS_TA_DPL_212) != 0U) || ((atsParam->ta & RFAL_ISODEP_ATS_TA_DLP_212) != 0U))) ||
  970. (!(RFAL_SUPPORT_BR_CE_A_424) && (((atsParam->ta & RFAL_ISODEP_ATS_TA_DPL_424) != 0U) || ((atsParam->ta & RFAL_ISODEP_ATS_TA_DLP_424) != 0U))) ||
  971. (!(RFAL_SUPPORT_BR_CE_A_848) && (((atsParam->ta & RFAL_ISODEP_ATS_TA_DPL_848) != 0U) || ((atsParam->ta & RFAL_ISODEP_ATS_TA_DLP_848) != 0U))) )
  972. {
  973. return ERR_NOTSUPP;
  974. }
  975. /* Enforce proper FWI configuration */
  976. if( atsParam->fwi > ISODEP_FWI_LIS_MAX)
  977. {
  978. atsParam->fwi = ISODEP_FWI_LIS_MAX;
  979. }
  980. gIsoDep.atsTA = atsParam->ta;
  981. gIsoDep.fwt = rfalIsoDepFWI2FWT(atsParam->fwi);
  982. gIsoDep.ourFsx = rfalIsoDepFSxI2FSx(atsParam->fsci);
  983. /* Ensure proper/maximum Historical Bytes length */
  984. atsParam->hbLen = MIN( RFAL_ISODEP_ATS_HB_MAX_LEN, atsParam->hbLen );
  985. /*******************************************************************************/
  986. /* Compute ATS */
  987. txBuf[ bufIt++ ] = (RFAL_ISODEP_ATS_HIST_OFFSET + atsParam->hbLen); /* TL */
  988. txBuf[ bufIt++ ] = ( (RFAL_ISODEP_ATS_T0_TA_PRESENCE_MASK | RFAL_ISODEP_ATS_T0_TB_PRESENCE_MASK |
  989. RFAL_ISODEP_ATS_T0_TC_PRESENCE_MASK)| atsParam->fsci ); /* T0 */
  990. txBuf[ bufIt++ ] = atsParam->ta; /* TA */
  991. txBuf[ bufIt++ ] = ( (atsParam->fwi << RFAL_ISODEP_RATS_PARAM_FSDI_SHIFT) |
  992. (atsParam->sfgi & RFAL_ISODEP_RATS_PARAM_FSDI_MASK) ); /* TB */
  993. txBuf[ bufIt++ ] = (uint8_t)((atsParam->didSupport) ? RFAL_ISODEP_ATS_TC_DID : 0U); /* TC */
  994. if( atsParam->hbLen > 0U ) /* MISRA 21.18 */
  995. {
  996. ST_MEMCPY( &txBuf[bufIt], atsParam->hb, atsParam->hbLen ); /* T1-Tk */
  997. bufIt += atsParam->hbLen;
  998. }
  999. gIsoDep.state = ISODEP_ST_PICC_ACT_ATS;
  1000. }
  1001. /*******************************************************************************/
  1002. else if( *buffer == RFAL_ISODEP_CMD_ATTRIB )
  1003. {
  1004. /* Check ATTRIB parameters */
  1005. if( attribResParam == NULL )
  1006. {
  1007. return ERR_PARAM;
  1008. }
  1009. /* REMARK: ATTRIB handling */
  1010. NO_WARNING(attribResParam);
  1011. NO_WARNING(bufLen);
  1012. return ERR_NOT_IMPLEMENTED;
  1013. }
  1014. else
  1015. {
  1016. return ERR_PARAM;
  1017. }
  1018. gIsoDep.actvParam = actParam;
  1019. /*******************************************************************************/
  1020. /* If requested copy to ISO-DEP device info */
  1021. if( actParam.isoDepDev != NULL )
  1022. {
  1023. actParam.isoDepDev->info.DID = gIsoDep.did;
  1024. actParam.isoDepDev->info.FSx = gIsoDep.fsx;
  1025. actParam.isoDepDev->info.FWT = gIsoDep.fwt;
  1026. actParam.isoDepDev->info.dFWT = 0;
  1027. actParam.isoDepDev->info.DSI = gIsoDep.txBR;
  1028. actParam.isoDepDev->info.DRI = gIsoDep.rxBR;
  1029. }
  1030. return rfalTransceiveBlockingTx( txBuf, bufIt, (uint8_t*)actParam.rxBuf, sizeof( rfalIsoDepBufFormat ), actParam.rxLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_FWT_NONE );
  1031. }
  1032. /*******************************************************************************/
  1033. ReturnCode rfalIsoDepListenGetActivationStatus( void )
  1034. {
  1035. ReturnCode err;
  1036. uint8_t* txBuf;
  1037. uint8_t bufIt;
  1038. rfalBitRate dsi;
  1039. rfalBitRate dri;
  1040. /* Check if Activation is running */
  1041. if( gIsoDep.state < ISODEP_ST_PICC_ACT_ATS )
  1042. {
  1043. return ERR_WRONG_STATE;
  1044. }
  1045. /* Check if Activation has finished already */
  1046. if( gIsoDep.state >= ISODEP_ST_PICC_RX )
  1047. {
  1048. return ERR_NONE;
  1049. }
  1050. /*******************************************************************************/
  1051. /* Check for incoming msg */
  1052. err = rfalGetTransceiveStatus();
  1053. switch( err )
  1054. {
  1055. /*******************************************************************************/
  1056. case ERR_NONE:
  1057. break;
  1058. /*******************************************************************************/
  1059. case ERR_LINK_LOSS:
  1060. case ERR_BUSY:
  1061. return err;
  1062. /*******************************************************************************/
  1063. case ERR_CRC:
  1064. case ERR_PAR:
  1065. case ERR_FRAMING:
  1066. /* ISO14443 4 5.6.2.2 2 If ATS has been replied upon a invalid block, PICC disables the PPS responses */
  1067. if( gIsoDep.state == ISODEP_ST_PICC_ACT_ATS )
  1068. {
  1069. gIsoDep.state = ISODEP_ST_PICC_RX;
  1070. break;
  1071. }
  1072. /* fall through */
  1073. /*******************************************************************************/
  1074. default: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  1075. /* ReEnable the receiver and wait for another frame */
  1076. isoDepReEnableRx( (uint8_t*)gIsoDep.actvParam.rxBuf, sizeof( rfalIsoDepBufFormat ), gIsoDep.actvParam.rxLen );
  1077. return ERR_BUSY;
  1078. }
  1079. txBuf = (uint8_t*)gIsoDep.actvParam.rxBuf; /* Use the rxBuf as TxBuf as well, the struct enforces a size enough MAX(NFCA_PPS_RES_LEN, ISODEP_DSL_MAX_LEN) */
  1080. dri = RFAL_BR_KEEP; /* The RFAL_BR_KEEP is used to check if PPS with BR change was requested */
  1081. dsi = RFAL_BR_KEEP; /* MISRA 9.1 */
  1082. bufIt = 0;
  1083. /*******************************************************************************/
  1084. gIsoDep.role = ISODEP_ROLE_PICC;
  1085. /*******************************************************************************/
  1086. if( gIsoDep.state == ISODEP_ST_PICC_ACT_ATS )
  1087. {
  1088. /* Check for a PPS ISO 14443-4 5.3 */
  1089. if( ( ((uint8_t*)gIsoDep.actvParam.rxBuf)[RFAL_ISODEP_PPS_STARTBYTE_POS] & RFAL_ISODEP_PPS_MASK) == RFAL_ISODEP_PPS_SB )
  1090. {
  1091. /* ISO 14443-4 5.3.1 Check if the we are the addressed DID/CID */
  1092. /* ISO 14443-4 5.3.2 Check for a valid PPS0 */
  1093. if( (( ((uint8_t*)gIsoDep.actvParam.rxBuf)[RFAL_ISODEP_PPS_STARTBYTE_POS] & RFAL_ISODEP_DID_MASK) != gIsoDep.did) ||
  1094. (( ((uint8_t*)gIsoDep.actvParam.rxBuf)[RFAL_ISODEP_PPS_PPS0_POS] & RFAL_ISODEP_PPS0_VALID_MASK) != RFAL_ISODEP_PPS0_PPS1_NOT_PRESENT) )
  1095. {
  1096. /* Invalid DID on PPS request or Invalid PPS0, reEnable the receiver and wait another frame */
  1097. isoDepReEnableRx( (uint8_t*)gIsoDep.actvParam.rxBuf, sizeof( rfalIsoDepBufFormat ), gIsoDep.actvParam.rxLen );
  1098. return ERR_BUSY;
  1099. }
  1100. /*******************************************************************************/
  1101. /* Check PPS1 presence */
  1102. if( ((uint8_t*)gIsoDep.actvParam.rxBuf)[RFAL_ISODEP_PPS_PPS0_POS] == RFAL_ISODEP_PPS0_PPS1_PRESENT )
  1103. {
  1104. uint8_t newdri = ((uint8_t*)gIsoDep.actvParam.rxBuf)[RFAL_ISODEP_PPS_PPS1_POS] & RFAL_ISODEP_PPS1_DxI_MASK; /* MISRA 10.8 */
  1105. uint8_t newdsi = (((uint8_t*)gIsoDep.actvParam.rxBuf)[RFAL_ISODEP_PPS_PPS1_POS] >> RFAL_ISODEP_PPS1_DSI_SHIFT) & RFAL_ISODEP_PPS1_DxI_MASK; /* MISRA 10.8 */
  1106. /* PRQA S 4342 2 # MISRA 10.5 - Layout of enum rfalBitRate and above masks guarantee no invalid enum values to be created */
  1107. dri = (rfalBitRate) (newdri);
  1108. dsi = (rfalBitRate) (newdsi);
  1109. if( (!(RFAL_SUPPORT_BR_CE_A_106) && (( dsi == RFAL_BR_106 ) || ( dri == RFAL_BR_106 ))) ||
  1110. (!(RFAL_SUPPORT_BR_CE_A_212) && (( dsi == RFAL_BR_212 ) || ( dri == RFAL_BR_212 ))) ||
  1111. (!(RFAL_SUPPORT_BR_CE_A_424) && (( dsi == RFAL_BR_424 ) || ( dri == RFAL_BR_424 ))) ||
  1112. (!(RFAL_SUPPORT_BR_CE_A_848) && (( dsi == RFAL_BR_848 ) || ( dri == RFAL_BR_848 ))) )
  1113. {
  1114. return ERR_PROTO;
  1115. }
  1116. }
  1117. /*******************************************************************************/
  1118. /* Compute and send PPS RES / Ack */
  1119. txBuf[ bufIt++ ] = ((uint8_t*)gIsoDep.actvParam.rxBuf)[RFAL_ISODEP_PPS_STARTBYTE_POS];
  1120. rfalTransceiveBlockingTx( txBuf, bufIt, (uint8_t*)gIsoDep.actvParam.rxBuf, sizeof( rfalIsoDepBufFormat ), gIsoDep.actvParam.rxLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_FWT_NONE );
  1121. /*******************************************************************************/
  1122. /* Exchange the bit rates if requested */
  1123. if( dri != RFAL_BR_KEEP )
  1124. {
  1125. rfalSetBitRate( dsi, dri ); /* PRQA S 2880 # MISRA 2.1 - Unreachable code due to configuration option being set/unset above (RFAL_SUPPORT_BR_CE_A_xxx) */
  1126. gIsoDep.txBR = dsi; /* DSI codes the divisor from PICC to PCD */
  1127. gIsoDep.rxBR = dri; /* DRI codes the divisor from PCD to PICC */
  1128. if(gIsoDep.actvParam.isoDepDev != NULL)
  1129. {
  1130. gIsoDep.actvParam.isoDepDev->info.DSI = dsi;
  1131. gIsoDep.actvParam.isoDepDev->info.DRI = dri;
  1132. }
  1133. }
  1134. }
  1135. /* Check for a S-Deselect is done on Data Exchange Activity */
  1136. }
  1137. /*******************************************************************************/
  1138. gIsoDep.hdrLen = RFAL_ISODEP_PCB_LEN;
  1139. gIsoDep.hdrLen += RFAL_ISODEP_DID_LEN; /* Always assume DID to be aligned with Digital 1.1 15.1.2 and ISO14443 4 5.6.3 #454 */
  1140. gIsoDep.hdrLen += (uint8_t)((gIsoDep.nad != RFAL_ISODEP_NO_NAD) ? RFAL_ISODEP_NAD_LEN : 0U);
  1141. /*******************************************************************************/
  1142. /* Rule C - The PICC block number shall be initialized to 1 at activation */
  1143. gIsoDep.blockNumber = 1;
  1144. /* Activation done, keep the rcvd data in, reMap the activation buffer to the global to be retrieved by the DEP method */
  1145. gIsoDep.rxBuf = (uint8_t*)gIsoDep.actvParam.rxBuf;
  1146. gIsoDep.rxBufLen = sizeof( rfalIsoDepBufFormat );
  1147. gIsoDep.rxBufInfPos = (uint8_t)((uint32_t)gIsoDep.actvParam.rxBuf->inf - (uint32_t)gIsoDep.actvParam.rxBuf->prologue);
  1148. gIsoDep.rxLen = gIsoDep.actvParam.rxLen;
  1149. gIsoDep.rxChaining = gIsoDep.actvParam.isRxChaining;
  1150. gIsoDep.state = ISODEP_ST_PICC_RX;
  1151. return ERR_NONE;
  1152. }
  1153. #endif /* RFAL_FEATURE_ISO_DEP_LISTEN */
  1154. /*******************************************************************************/
  1155. uint16_t rfalIsoDepGetMaxInfLen( void )
  1156. {
  1157. /* Check whether all parameters are valid, otherwise return minimum default value */
  1158. if( (gIsoDep.fsx < (uint16_t)RFAL_ISODEP_FSX_16) || (gIsoDep.fsx > (uint16_t)RFAL_ISODEP_FSX_4096) || (gIsoDep.hdrLen > ISODEP_HDR_MAX_LEN) )
  1159. {
  1160. uint16_t isodepFsx16 = (uint16_t)RFAL_ISODEP_FSX_16; /* MISRA 10.1 */
  1161. return (isodepFsx16 - RFAL_ISODEP_PCB_LEN - ISODEP_CRC_LEN);
  1162. }
  1163. return (gIsoDep.fsx - gIsoDep.hdrLen - ISODEP_CRC_LEN);
  1164. }
  1165. /*******************************************************************************/
  1166. ReturnCode rfalIsoDepStartTransceive( rfalIsoDepTxRxParam param )
  1167. {
  1168. gIsoDep.txBuf = param.txBuf->prologue;
  1169. gIsoDep.txBufInfPos = (uint8_t)((uint32_t)param.txBuf->inf - (uint32_t)param.txBuf->prologue);
  1170. gIsoDep.txBufLen = param.txBufLen;
  1171. gIsoDep.isTxChaining = param.isTxChaining;
  1172. gIsoDep.rxBuf = param.rxBuf->prologue;
  1173. gIsoDep.rxBufInfPos = (uint8_t)((uint32_t)param.rxBuf->inf - (uint32_t)param.rxBuf->prologue);
  1174. gIsoDep.rxBufLen = sizeof(rfalIsoDepBufFormat);
  1175. gIsoDep.rxLen = param.rxLen;
  1176. gIsoDep.rxChaining = param.isRxChaining;
  1177. gIsoDep.fwt = param.FWT;
  1178. gIsoDep.dFwt = param.dFWT;
  1179. gIsoDep.fsx = param.FSx;
  1180. gIsoDep.did = param.DID;
  1181. /* Only change the FSx from activation if no to Keep */
  1182. gIsoDep.ourFsx = (( param.ourFSx != RFAL_ISODEP_FSX_KEEP ) ? param.ourFSx : gIsoDep.ourFsx);
  1183. /* Clear inner control params for next dataExchange */
  1184. gIsoDep.isRxChaining = false;
  1185. isoDepClearCounters();
  1186. if(gIsoDep.role == ISODEP_ROLE_PICC)
  1187. {
  1188. if(gIsoDep.txBufLen > 0U)
  1189. {
  1190. /* Ensure that an RTOX Ack is not being expected at moment */
  1191. if( !gIsoDep.isWait4WTX )
  1192. {
  1193. gIsoDep.state = ISODEP_ST_PICC_TX;
  1194. return ERR_NONE;
  1195. }
  1196. else
  1197. {
  1198. /* If RTOX Ack is expected, signal a pending Tx to be transmitted right after */
  1199. gIsoDep.isTxPending = true;
  1200. }
  1201. }
  1202. /* Digital 1.1 15.2.5.1 The first block SHALL be sent by the Reader/Writer */
  1203. gIsoDep.state = ISODEP_ST_PICC_RX;
  1204. return ERR_NONE;
  1205. }
  1206. gIsoDep.state = ISODEP_ST_PCD_TX;
  1207. return ERR_NONE;
  1208. }
  1209. /*******************************************************************************/
  1210. ReturnCode rfalIsoDepGetTransceiveStatus( void )
  1211. {
  1212. if( gIsoDep.role == ISODEP_ROLE_PICC)
  1213. {
  1214. #if RFAL_FEATURE_ISO_DEP_LISTEN
  1215. return isoDepDataExchangePICC();
  1216. #else
  1217. return ERR_NOTSUPP;
  1218. #endif /* RFAL_FEATURE_ISO_DEP_LISTEN */
  1219. }
  1220. else
  1221. {
  1222. #if RFAL_FEATURE_ISO_DEP_POLL
  1223. return isoDepDataExchangePCD( gIsoDep.rxLen, gIsoDep.rxChaining );
  1224. #else
  1225. return ERR_NOTSUPP;
  1226. #endif /* RFAL_FEATURE_ISO_DEP_POLL */
  1227. }
  1228. }
  1229. #if RFAL_FEATURE_ISO_DEP_LISTEN
  1230. /*******************************************************************************/
  1231. static ReturnCode isoDepDataExchangePICC( void )
  1232. {
  1233. uint8_t rxPCB;
  1234. ReturnCode ret;
  1235. switch( gIsoDep.state )
  1236. {
  1237. /*******************************************************************************/
  1238. case ISODEP_ST_IDLE:
  1239. return ERR_NONE;
  1240. /*******************************************************************************/
  1241. case ISODEP_ST_PICC_TX:
  1242. ret = isoDepTx( isoDep_PCBIBlock( gIsoDep.blockNumber ), gIsoDep.txBuf, &gIsoDep.txBuf[gIsoDep.txBufInfPos], gIsoDep.txBufLen, RFAL_FWT_NONE );
  1243. /* Clear pending Tx flag */
  1244. gIsoDep.isTxPending = false;
  1245. switch( ret )
  1246. {
  1247. case ERR_NONE:
  1248. gIsoDep.state = ISODEP_ST_PICC_RX;
  1249. return ERR_BUSY;
  1250. default:
  1251. /* MISRA 16.4: no empty default statement (a comment being enough) */
  1252. break;
  1253. }
  1254. return ret;
  1255. /*******************************************************************************/
  1256. case ISODEP_ST_PICC_RX:
  1257. ret = rfalGetTransceiveStatus();
  1258. switch( ret )
  1259. {
  1260. /*******************************************************************************/
  1261. /* Data rcvd with error or timeout -> mute */
  1262. case ERR_TIMEOUT:
  1263. case ERR_CRC:
  1264. case ERR_PAR:
  1265. case ERR_FRAMING:
  1266. /* Digital 1.1 - 15.2.6.2 The CE SHALL NOT attempt error recovery and remains in Rx mode upon Transmission or a Protocol Error */
  1267. isoDepReEnableRx( (uint8_t*)gIsoDep.rxBuf, sizeof( rfalIsoDepBufFormat ), gIsoDep.rxLen );
  1268. return ERR_BUSY;
  1269. /*******************************************************************************/
  1270. case ERR_LINK_LOSS:
  1271. return ret; /* Debug purposes */
  1272. case ERR_BUSY:
  1273. return ret; /* Debug purposes */
  1274. /*******************************************************************************/
  1275. case ERR_NONE:
  1276. *gIsoDep.rxLen = rfalConvBitsToBytes( *gIsoDep.rxLen );
  1277. break;
  1278. /*******************************************************************************/
  1279. default:
  1280. return ret;
  1281. }
  1282. break;
  1283. /*******************************************************************************/
  1284. case ISODEP_ST_PICC_SWTX:
  1285. if( !isoDepTimerisExpired( gIsoDep.WTXTimer ) ) /* Do nothing until WTX timer has expired */
  1286. {
  1287. return ERR_BUSY;
  1288. }
  1289. /* Set waiting for WTX Ack Flag */
  1290. gIsoDep.isWait4WTX = true;
  1291. /* Digital 1.1 15.2.2.9 - Calculate the WTXM such that FWTtemp <= FWTmax */
  1292. gIsoDep.lastWTXM = (uint8_t)isoDep_WTXMListenerMax( gIsoDep.fwt );
  1293. EXIT_ON_ERR( ret, isoDepHandleControlMsg( ISODEP_S_WTX, gIsoDep.lastWTXM ) );
  1294. gIsoDep.state = ISODEP_ST_PICC_RX; /* Go back to Rx to process WTX ack */
  1295. return ERR_BUSY;
  1296. /*******************************************************************************/
  1297. case ISODEP_ST_PICC_SDSL:
  1298. if( rfalIsTransceiveInRx() ) /* Wait until DSL response has been sent */
  1299. {
  1300. rfalIsoDepInitialize(); /* Session finished reInit vars */
  1301. return ERR_SLEEP_REQ; /* Notify Deselect request */
  1302. }
  1303. return ERR_BUSY;
  1304. /*******************************************************************************/
  1305. default:
  1306. return ERR_INTERNAL;
  1307. }
  1308. /* ISO 14443-4 7.5.6.2 CE SHALL NOT attempt error recovery -> clear counters */
  1309. isoDepClearCounters();
  1310. /*******************************************************************************/
  1311. /* No error, process incoming msg */
  1312. /*******************************************************************************/
  1313. /* Grab rcvd PCB */
  1314. rxPCB = gIsoDep.rxBuf[ ISODEP_PCB_POS ];
  1315. /*******************************************************************************/
  1316. /* When DID=0 PCD may or may not use DID, therefore check whether current PCD request
  1317. * has DID present to be reflected on max INF length #454 */
  1318. /* ReCalculate Header Length */
  1319. gIsoDep.hdrLen = RFAL_ISODEP_PCB_LEN;
  1320. gIsoDep.hdrLen += (uint8_t)( (isoDep_PCBhasDID(rxPCB)) ? RFAL_ISODEP_DID_LEN : 0U );
  1321. gIsoDep.hdrLen += (uint8_t)( (isoDep_PCBhasNAD(rxPCB)) ? RFAL_ISODEP_NAD_LEN : 0U );
  1322. /* Store whether last PCD block had DID. for PICC special handling of DID = 0 */
  1323. if( gIsoDep.did == RFAL_ISODEP_DID_00 )
  1324. {
  1325. gIsoDep.lastDID00 = ( (isoDep_PCBhasDID(rxPCB)) ? true : false );
  1326. }
  1327. /*******************************************************************************/
  1328. /* Check rcvd msg length, cannot be less then the expected header OR *
  1329. * if the rcvd msg exceeds our announced frame size (FSD) */
  1330. if( ((*gIsoDep.rxLen) < gIsoDep.hdrLen) || ((*gIsoDep.rxLen) > (gIsoDep.ourFsx - ISODEP_CRC_LEN)) )
  1331. {
  1332. isoDepReEnableRx( (uint8_t*)gIsoDep.actvParam.rxBuf, sizeof( rfalIsoDepBufFormat ), gIsoDep.actvParam.rxLen );
  1333. return ERR_BUSY; /* ERR_PROTO Ignore this protocol request */
  1334. }
  1335. /* If we are expecting DID, check if PCB signals its presence and if device ID match OR
  1336. * If our DID=0 and DID is sent but with an incorrect value */
  1337. if( ((gIsoDep.did != RFAL_ISODEP_DID_00) && ( !isoDep_PCBhasDID(rxPCB) || (gIsoDep.did != gIsoDep.rxBuf[ ISODEP_DID_POS ]))) ||
  1338. ((gIsoDep.did == RFAL_ISODEP_DID_00) && isoDep_PCBhasDID(rxPCB) && (RFAL_ISODEP_DID_00 != gIsoDep.rxBuf[ ISODEP_DID_POS ]) ) )
  1339. {
  1340. isoDepReEnableRx( (uint8_t*)gIsoDep.actvParam.rxBuf, sizeof( rfalIsoDepBufFormat ), gIsoDep.actvParam.rxLen );
  1341. return ERR_BUSY; /* Ignore a wrong DID request */
  1342. }
  1343. /* If we aren't expecting NAD and it's received */
  1344. if( (gIsoDep.nad == RFAL_ISODEP_NO_NAD) && isoDep_PCBhasNAD(rxPCB) )
  1345. {
  1346. isoDepReEnableRx( (uint8_t*)gIsoDep.actvParam.rxBuf, sizeof( rfalIsoDepBufFormat ), gIsoDep.actvParam.rxLen );
  1347. return ERR_BUSY; /* Ignore a unexpected NAD request */
  1348. }
  1349. /*******************************************************************************/
  1350. /* Process S-Block */
  1351. /*******************************************************************************/
  1352. if( isoDep_PCBisSBlock(rxPCB) )
  1353. {
  1354. /* Check if is a Wait Time eXtension */
  1355. if( isoDep_PCBisSWTX(rxPCB) )
  1356. {
  1357. /* Check if we're expecting a S-WTX */
  1358. if( isoDep_PCBisWTX( gIsoDep.lastPCB ) )
  1359. {
  1360. /* Digital 1.1 15.2.2.11 S(WTX) Ack with different WTXM -> Protocol Error *
  1361. * Power level indication also should be set to 0 */
  1362. if( ( gIsoDep.rxBuf[ gIsoDep.hdrLen ] == gIsoDep.lastWTXM) && ((*gIsoDep.rxLen - gIsoDep.hdrLen) == ISODEP_SWTX_INF_LEN) )
  1363. {
  1364. /* Clear waiting for RTOX Ack Flag */
  1365. gIsoDep.isWait4WTX = false;
  1366. /* Check if a Tx is already pending */
  1367. if( gIsoDep.isTxPending )
  1368. {
  1369. /* Has a pending Tx, go immediately to TX */
  1370. gIsoDep.state = ISODEP_ST_PICC_TX;
  1371. return ERR_BUSY;
  1372. }
  1373. /* Set WTX timer */
  1374. isoDepTimerStart( gIsoDep.WTXTimer, isoDep_WTXAdjust( (gIsoDep.lastWTXM * rfalConv1fcToMs( gIsoDep.fwt )) ) );
  1375. gIsoDep.state = ISODEP_ST_PICC_SWTX;
  1376. return ERR_BUSY;
  1377. }
  1378. }
  1379. /* Unexpected/Incorrect S-WTX, fall into reRenable */
  1380. }
  1381. /* Check if is a Deselect request */
  1382. if( isoDep_PCBisSDeselect(rxPCB) && ((*gIsoDep.rxLen - gIsoDep.hdrLen) == ISODEP_SDSL_INF_LEN) )
  1383. {
  1384. EXIT_ON_ERR( ret, isoDepHandleControlMsg( ISODEP_S_DSL, RFAL_ISODEP_NO_PARAM ) );
  1385. /* S-DSL transmission ongoing, wait until complete */
  1386. gIsoDep.state = ISODEP_ST_PICC_SDSL;
  1387. return ERR_BUSY;
  1388. }
  1389. /* Unexpected S-Block, fall into reRenable */
  1390. }
  1391. /*******************************************************************************/
  1392. /* Process R-Block */
  1393. /*******************************************************************************/
  1394. else if( isoDep_PCBisRBlock(rxPCB) && ((*gIsoDep.rxLen - gIsoDep.hdrLen) == ISODEP_RBLOCK_INF_LEN))
  1395. {
  1396. if( isoDep_PCBisRACK(rxPCB) ) /* Check if is a R-ACK */
  1397. {
  1398. if( isoDep_GetBN(rxPCB) == gIsoDep.blockNumber ) /* Check block number */
  1399. {
  1400. /* Rule 11 - R(ACK) with current bn -> re-transmit */
  1401. if( !isoDep_PCBisIBlock(gIsoDep.lastPCB) )
  1402. {
  1403. isoDepReSendControlMsg();
  1404. }
  1405. else
  1406. {
  1407. gIsoDep.state = ISODEP_ST_PICC_TX;
  1408. }
  1409. return ERR_BUSY;
  1410. }
  1411. else
  1412. {
  1413. if( !gIsoDep.isTxChaining )
  1414. {
  1415. /* Rule 13 violation R(ACK) without performing chaining */
  1416. isoDepReEnableRx( (uint8_t*)gIsoDep.rxBuf, sizeof( rfalIsoDepBufFormat ), gIsoDep.rxLen );
  1417. return ERR_BUSY;
  1418. }
  1419. /* Rule E - R(ACK) with not current bn -> toogle bn */
  1420. isoDep_ToggleBN( gIsoDep.blockNumber );
  1421. /* This block has been transmitted and acknowledged, perform WTX until next data is provided */
  1422. /* Rule 9 - PICC is allowed to send an S(WTX) instead of an I-block or an R(ACK) */
  1423. isoDepTimerStart( gIsoDep.WTXTimer, isoDep_WTXAdjust( rfalConv1fcToMs( gIsoDep.fwt )) );
  1424. gIsoDep.state = ISODEP_ST_PICC_SWTX;
  1425. /* Rule 13 - R(ACK) with not current bn -> continue chaining */
  1426. return ERR_NONE; /* This block has been transmitted */
  1427. }
  1428. }
  1429. else if( isoDep_PCBisRNAK(rxPCB) ) /* Check if is a R-NACK */
  1430. {
  1431. if( isoDep_GetBN(rxPCB) == gIsoDep.blockNumber ) /* Check block number */
  1432. {
  1433. /* Rule 11 - R(NAK) with current bn -> re-transmit last x-Block */
  1434. if( !isoDep_PCBisIBlock(gIsoDep.lastPCB) )
  1435. {
  1436. isoDepReSendControlMsg();
  1437. }
  1438. else
  1439. {
  1440. gIsoDep.state = ISODEP_ST_PICC_TX;
  1441. }
  1442. return ERR_BUSY;
  1443. }
  1444. else
  1445. {
  1446. /* Rule 12 - R(NAK) with not current bn -> R(ACK) */
  1447. EXIT_ON_ERR( ret, isoDepHandleControlMsg( ISODEP_R_ACK, RFAL_ISODEP_NO_PARAM ) );
  1448. return ERR_BUSY;
  1449. }
  1450. }
  1451. else
  1452. {
  1453. /* MISRA 15.7 - Empty else */
  1454. }
  1455. /* Unexpected R-Block, fall into reRenable */
  1456. }
  1457. /*******************************************************************************/
  1458. /* Process I-Block */
  1459. /*******************************************************************************/
  1460. else if( isoDep_PCBisIBlock(rxPCB) )
  1461. {
  1462. /* Rule D - When an I-block is received, the PICC shall toggle its block number before sending a block */
  1463. isoDep_ToggleBN( gIsoDep.blockNumber );
  1464. /*******************************************************************************/
  1465. /* Check if the block number is the one expected */
  1466. /* Check if PCD sent an I-Block instead ACK/NACK when we are chaining */
  1467. if( (isoDep_GetBN(rxPCB) != gIsoDep.blockNumber) || (gIsoDep.isTxChaining) )
  1468. {
  1469. /* Remain in the same Block Number */
  1470. isoDep_ToggleBN( gIsoDep.blockNumber );
  1471. /* ISO 14443-4 7.5.6.2 & Digital 1.1 - 15.2.6.2 The CE SHALL NOT attempt error recovery and remains in Rx mode upon Transmission or a Protocol Error */
  1472. isoDepReEnableRx( (uint8_t*)gIsoDep.rxBuf, sizeof( rfalIsoDepBufFormat ), gIsoDep.rxLen );
  1473. return ERR_BUSY;
  1474. }
  1475. /*******************************************************************************/
  1476. /* is PCD performing chaining ? */
  1477. if( isoDep_PCBisChaining(rxPCB) )
  1478. {
  1479. gIsoDep.isRxChaining = true;
  1480. *gIsoDep.rxChaining = true; /* Output Parameter*/
  1481. EXIT_ON_ERR( ret, isoDepHandleControlMsg( ISODEP_R_ACK, RFAL_ISODEP_NO_PARAM ) );
  1482. /* Received I-Block with chaining, send current data to DH */
  1483. /* remove ISO DEP header, check is necessary to move the INF data on the buffer */
  1484. *gIsoDep.rxLen -= gIsoDep.hdrLen;
  1485. if( (gIsoDep.hdrLen != gIsoDep.rxBufInfPos) && (*gIsoDep.rxLen > 0U) )
  1486. {
  1487. ST_MEMMOVE( &gIsoDep.rxBuf[gIsoDep.rxBufInfPos], &gIsoDep.rxBuf[gIsoDep.hdrLen], *gIsoDep.rxLen );
  1488. }
  1489. return ERR_AGAIN; /* Send Again signalling to run again, but some chaining data has arrived*/
  1490. }
  1491. /*******************************************************************************/
  1492. /* PCD is not performing chaining */
  1493. gIsoDep.isRxChaining = false; /* clear PCD chaining flag */
  1494. *gIsoDep.rxChaining = false; /* Output Parameter */
  1495. /* remove ISO DEP header, check is necessary to move the INF data on the buffer */
  1496. *gIsoDep.rxLen -= gIsoDep.hdrLen;
  1497. if( (gIsoDep.hdrLen != gIsoDep.rxBufInfPos) && (*gIsoDep.rxLen > 0U) )
  1498. {
  1499. ST_MEMMOVE( &gIsoDep.rxBuf[gIsoDep.rxBufInfPos], &gIsoDep.rxBuf[gIsoDep.hdrLen], *gIsoDep.rxLen );
  1500. }
  1501. /*******************************************************************************/
  1502. /* Reception done, send data back and start WTX timer */
  1503. isoDepTimerStart( gIsoDep.WTXTimer, isoDep_WTXAdjust( rfalConv1fcToMs( gIsoDep.fwt )) );
  1504. gIsoDep.state = ISODEP_ST_PICC_SWTX;
  1505. return ERR_NONE;
  1506. }
  1507. else
  1508. {
  1509. /* MISRA 15.7 - Empty else */
  1510. }
  1511. /* Unexpected/Unknown Block */
  1512. /* ISO 14443-4 7.5.6.2 & Digital 1.1 - 15.2.6.2 The CE SHALL NOT attempt error recovery and remains in Rx mode upon Transmission or a Protocol Error */
  1513. isoDepReEnableRx( (uint8_t*)gIsoDep.rxBuf, sizeof( rfalIsoDepBufFormat ), gIsoDep.rxLen );
  1514. return ERR_BUSY;
  1515. }
  1516. #endif /* RFAL_FEATURE_ISO_DEP_LISTEN */
  1517. #if RFAL_FEATURE_ISO_DEP_POLL
  1518. #if RFAL_FEATURE_NFCA
  1519. /*******************************************************************************/
  1520. static ReturnCode rfalIsoDepStartRATS( rfalIsoDepFSxI FSDI, uint8_t DID, rfalIsoDepAts *ats, uint8_t *atsLen )
  1521. {
  1522. rfalTransceiveContext ctx;
  1523. if( ats == NULL)
  1524. {
  1525. return ERR_PARAM;
  1526. }
  1527. gIsoDep.rxBuf = (uint8_t*) ats;
  1528. gIsoDep.rxLen8 = atsLen;
  1529. gIsoDep.did = DID;
  1530. /*******************************************************************************/
  1531. /* Compose RATS */
  1532. gIsoDep.actv.ratsReq.CMD = RFAL_ISODEP_CMD_RATS;
  1533. gIsoDep.actv.ratsReq.PARAM = (((uint8_t)FSDI << RFAL_ISODEP_RATS_PARAM_FSDI_SHIFT) & RFAL_ISODEP_RATS_PARAM_FSDI_MASK) | (DID & RFAL_ISODEP_RATS_PARAM_DID_MASK);
  1534. rfalCreateByteFlagsTxRxContext( ctx, (uint8_t*)&gIsoDep.actv.ratsReq, sizeof(rfalIsoDepRats), (uint8_t*)ats, sizeof(rfalIsoDepAts), &gIsoDep.rxBufLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_ISODEP_T4T_FWT_ACTIVATION );
  1535. return rfalStartTransceive( &ctx );
  1536. }
  1537. /*******************************************************************************/
  1538. static ReturnCode rfalIsoDepGetRATSStatus( void )
  1539. {
  1540. ReturnCode ret;
  1541. ret = rfalGetTransceiveStatus();
  1542. if( ret == ERR_NONE )
  1543. {
  1544. gIsoDep.rxBufLen = rfalConvBitsToBytes(gIsoDep.rxBufLen);
  1545. /* Check for valid ATS length Digital 1.1 13.6.2.1 & 13.6.2.3 */
  1546. if( (gIsoDep.rxBufLen < RFAL_ISODEP_ATS_MIN_LEN) || (gIsoDep.rxBufLen > RFAL_ISODEP_ATS_MAX_LEN) || ( gIsoDep.rxBuf[RFAL_ISODEP_ATS_TL_POS] != gIsoDep.rxBufLen) )
  1547. {
  1548. return ERR_PROTO;
  1549. }
  1550. /* Assign our FSx, in case the a Deselect is send without Transceive */
  1551. gIsoDep.ourFsx = rfalIsoDepFSxI2FSx( (uint8_t) (gIsoDep.actv.ratsReq.PARAM >> RFAL_ISODEP_RATS_PARAM_FSDI_SHIFT) );
  1552. /* Check and assign if ATS length was requested (length also available on TL) */
  1553. if( gIsoDep.rxLen8 != NULL )
  1554. {
  1555. *gIsoDep.rxLen8 = (uint8_t)gIsoDep.rxBufLen;
  1556. }
  1557. }
  1558. return ret;
  1559. }
  1560. /*******************************************************************************/
  1561. ReturnCode rfalIsoDepRATS( rfalIsoDepFSxI FSDI, uint8_t DID, rfalIsoDepAts *ats , uint8_t *atsLen)
  1562. {
  1563. ReturnCode ret;
  1564. EXIT_ON_ERR( ret, rfalIsoDepStartRATS(FSDI, DID, ats, atsLen) );
  1565. rfalIsoDepRunBlocking( ret, rfalIsoDepGetRATSStatus() );
  1566. return ret;
  1567. }
  1568. /*******************************************************************************/
  1569. static ReturnCode rfalIsoDepStartPPS( uint8_t DID, rfalBitRate DSI, rfalBitRate DRI, rfalIsoDepPpsRes *ppsRes )
  1570. {
  1571. rfalTransceiveContext ctx;
  1572. if( (ppsRes == NULL) || (DSI > RFAL_BR_848) || (DRI > RFAL_BR_848) || (DID > RFAL_ISODEP_DID_MAX) )
  1573. {
  1574. return ERR_PARAM;
  1575. }
  1576. gIsoDep.rxBuf = (uint8_t*) ppsRes;
  1577. /*******************************************************************************/
  1578. /* Compose PPS Request */
  1579. gIsoDep.actv.ppsReq.PPSS = (RFAL_ISODEP_PPS_SB | (DID & RFAL_ISODEP_PPS_SB_DID_MASK));
  1580. gIsoDep.actv.ppsReq.PPS0 = RFAL_ISODEP_PPS_PPS0_PPS1_PRESENT;
  1581. gIsoDep.actv.ppsReq.PPS1 = (RFAL_ISODEP_PPS_PPS1 | ((((uint8_t)DSI<<RFAL_ISODEP_PPS_PPS1_DSI_SHIFT) | (uint8_t)DRI) & RFAL_ISODEP_PPS_PPS1_DXI_MASK));
  1582. rfalCreateByteFlagsTxRxContext( ctx, (uint8_t*)&gIsoDep.actv.ppsReq, sizeof(rfalIsoDepPpsReq), (uint8_t*)ppsRes, sizeof(rfalIsoDepPpsRes), &gIsoDep.rxBufLen, RFAL_TXRX_FLAGS_DEFAULT, RFAL_ISODEP_T4T_FWT_ACTIVATION );
  1583. return rfalStartTransceive( &ctx );
  1584. }
  1585. /*******************************************************************************/
  1586. static ReturnCode rfalIsoDepGetPPSSTatus( void )
  1587. {
  1588. ReturnCode ret;
  1589. ret = rfalGetTransceiveStatus();
  1590. if( ret == ERR_NONE )
  1591. {
  1592. gIsoDep.rxBufLen = rfalConvBitsToBytes(gIsoDep.rxBufLen);
  1593. /* Check for valid PPS Response */
  1594. if( (gIsoDep.rxBufLen != RFAL_ISODEP_PPS_RES_LEN) || (*gIsoDep.rxBuf != gIsoDep.actv.ppsReq.PPSS) )
  1595. {
  1596. return ERR_PROTO;
  1597. }
  1598. }
  1599. return ret;
  1600. }
  1601. /*******************************************************************************/
  1602. ReturnCode rfalIsoDepPPS( uint8_t DID, rfalBitRate DSI, rfalBitRate DRI, rfalIsoDepPpsRes *ppsRes )
  1603. {
  1604. ReturnCode ret;
  1605. EXIT_ON_ERR( ret, rfalIsoDepStartPPS(DID, DSI, DRI, ppsRes) );
  1606. rfalIsoDepRunBlocking( ret, rfalIsoDepGetPPSSTatus() );
  1607. return ret;
  1608. }
  1609. #endif /* RFAL_FEATURE_NFCA */
  1610. #if RFAL_FEATURE_NFCB
  1611. static ReturnCode rfalIsoDepStartATTRIB( const uint8_t* nfcid0, uint8_t PARAM1, rfalBitRate DSI, rfalBitRate DRI, rfalIsoDepFSxI FSDI, uint8_t PARAM3, uint8_t DID, const uint8_t* HLInfo, uint8_t HLInfoLen, uint32_t fwt, rfalIsoDepAttribRes *attribRes, uint8_t *attribResLen )
  1612. {
  1613. rfalTransceiveContext ctx;
  1614. if( (attribRes == NULL) || (attribResLen == NULL) || (DSI > RFAL_BR_848) || (DRI > RFAL_BR_848) || (DID > RFAL_ISODEP_DID_MAX) )
  1615. {
  1616. return ERR_NONE;
  1617. }
  1618. gIsoDep.rxBuf = (uint8_t*) attribRes;
  1619. gIsoDep.rxLen8 = attribResLen;
  1620. gIsoDep.did = DID;
  1621. /*******************************************************************************/
  1622. /* Compose ATTRIB command */
  1623. gIsoDep.actv.attribReq.cmd = RFAL_ISODEP_CMD_ATTRIB;
  1624. gIsoDep.actv.attribReq.Param.PARAM1 = PARAM1;
  1625. gIsoDep.actv.attribReq.Param.PARAM2 = ( ((((uint8_t)DSI<<RFAL_ISODEP_ATTRIB_PARAM2_DSI_SHIFT) | ((uint8_t)DRI<<RFAL_ISODEP_ATTRIB_PARAM2_DRI_SHIFT)) & RFAL_ISODEP_ATTRIB_PARAM2_DXI_MASK) | ((uint8_t)FSDI & RFAL_ISODEP_ATTRIB_PARAM2_FSDI_MASK) );
  1626. gIsoDep.actv.attribReq.Param.PARAM3 = PARAM3;
  1627. gIsoDep.actv.attribReq.Param.PARAM4 = (DID & RFAL_ISODEP_ATTRIB_PARAM4_DID_MASK);
  1628. ST_MEMCPY(gIsoDep.actv.attribReq.nfcid0, nfcid0, RFAL_NFCB_NFCID0_LEN);
  1629. /* Append the Higher layer Info if provided */
  1630. if( (HLInfo != NULL) && (HLInfoLen > 0U) )
  1631. {
  1632. ST_MEMCPY(gIsoDep.actv.attribReq.HLInfo, HLInfo, MIN(HLInfoLen, RFAL_ISODEP_ATTRIB_HLINFO_LEN) );
  1633. }
  1634. rfalCreateByteFlagsTxRxContext( ctx, (uint8_t*)&gIsoDep.actv.attribReq, (uint16_t)(RFAL_ISODEP_ATTRIB_HDR_LEN + MIN((uint16_t)HLInfoLen, RFAL_ISODEP_ATTRIB_HLINFO_LEN)), (uint8_t*)gIsoDep.rxBuf, sizeof(rfalIsoDepAttribRes), &gIsoDep.rxBufLen, RFAL_TXRX_FLAGS_DEFAULT, fwt );
  1635. return rfalStartTransceive( &ctx );
  1636. }
  1637. /*******************************************************************************/
  1638. static ReturnCode rfalIsoDepGetATTRIBStatus( void )
  1639. {
  1640. ReturnCode ret;
  1641. ret = rfalGetTransceiveStatus();
  1642. if( ret == ERR_NONE )
  1643. {
  1644. gIsoDep.rxBufLen = rfalConvBitsToBytes(gIsoDep.rxBufLen);
  1645. /* Check a for valid ATTRIB Response Digital 1.1 15.6.2.1 */
  1646. if( (gIsoDep.rxBufLen < RFAL_ISODEP_ATTRIB_RES_HDR_LEN) || ((gIsoDep.rxBuf[RFAL_ISODEP_ATTRIB_RES_MBLIDID_POS] & RFAL_ISODEP_ATTRIB_RES_DID_MASK) != gIsoDep.did) )
  1647. {
  1648. return ERR_PROTO;
  1649. }
  1650. if( gIsoDep.rxLen8 != NULL )
  1651. {
  1652. *gIsoDep.rxLen8 = (uint8_t)gIsoDep.rxBufLen;
  1653. }
  1654. gIsoDep.ourFsx = rfalIsoDepFSxI2FSx( (uint8_t)(gIsoDep.actv.attribReq.Param.PARAM2 & RFAL_ISODEP_ATTRIB_PARAM2_FSDI_MASK) );
  1655. }
  1656. return ret;
  1657. }
  1658. /*******************************************************************************/
  1659. ReturnCode rfalIsoDepATTRIB( const uint8_t* nfcid0, uint8_t PARAM1, rfalBitRate DSI, rfalBitRate DRI, rfalIsoDepFSxI FSDI, uint8_t PARAM3, uint8_t DID, const uint8_t* HLInfo, uint8_t HLInfoLen, uint32_t fwt, rfalIsoDepAttribRes *attribRes, uint8_t *attribResLen )
  1660. {
  1661. ReturnCode ret;
  1662. EXIT_ON_ERR( ret, rfalIsoDepStartATTRIB( nfcid0, PARAM1, DSI, DRI, FSDI, PARAM3, DID, HLInfo, HLInfoLen, fwt, attribRes, attribResLen ) );
  1663. rfalIsoDepRunBlocking( ret, rfalIsoDepGetATTRIBStatus() );
  1664. return ret;
  1665. }
  1666. #endif /* RFAL_FEATURE_NFCB */
  1667. #if RFAL_FEATURE_NFCA
  1668. /*******************************************************************************/
  1669. ReturnCode rfalIsoDepPollAHandleActivation( rfalIsoDepFSxI FSDI, uint8_t DID, rfalBitRate maxBR, rfalIsoDepDevice *isoDepDev )
  1670. {
  1671. ReturnCode ret;
  1672. EXIT_ON_ERR( ret, rfalIsoDepPollAStartActivation( FSDI, DID, maxBR, isoDepDev ) );
  1673. rfalIsoDepRunBlocking( ret, rfalIsoDepPollAGetActivationStatus() );
  1674. return ret;
  1675. }
  1676. /*******************************************************************************/
  1677. ReturnCode rfalIsoDepPollAStartActivation( rfalIsoDepFSxI FSDI, uint8_t DID, rfalBitRate maxBR, rfalIsoDepDevice *isoDepDev )
  1678. {
  1679. ReturnCode ret;
  1680. if( isoDepDev == NULL )
  1681. {
  1682. return ERR_PARAM;
  1683. }
  1684. /* Enable EMD handling according Digital 1.1 4.1.1.1 ; EMVCo 2.6 4.9.2 */
  1685. rfalSetErrorHandling( RFAL_ERRORHANDLING_EMVCO );
  1686. /* Start RATS Transceive */
  1687. EXIT_ON_ERR( ret, rfalIsoDepStartRATS( FSDI, DID, &isoDepDev->activation.A.Listener.ATS, &isoDepDev->activation.A.Listener.ATSLen ) );
  1688. isoDepDev->info.DSI = maxBR;
  1689. gIsoDep.actvDev = isoDepDev;
  1690. gIsoDep.cntRRetrys = gIsoDep.maxRetriesRATS;
  1691. gIsoDep.state = ISODEP_ST_PCD_ACT_RATS;
  1692. return ret;
  1693. }
  1694. /*******************************************************************************/
  1695. ReturnCode rfalIsoDepPollAGetActivationStatus( void )
  1696. {
  1697. ReturnCode ret;
  1698. uint8_t msgIt;
  1699. rfalBitRate maxBR;
  1700. switch( gIsoDep.state )
  1701. {
  1702. /*******************************************************************************/
  1703. case ISODEP_ST_PCD_ACT_RATS:
  1704. ret = rfalIsoDepGetRATSStatus();
  1705. if( ret != ERR_BUSY )
  1706. {
  1707. if( ret != ERR_NONE )
  1708. {
  1709. /* EMVCo 2.6 9.6.1.1 & 9.6.1.2 If a timeout error is detected retransmit, on transmission error abort */
  1710. if( (gIsoDep.compMode == RFAL_COMPLIANCE_MODE_EMV) && (ret != ERR_TIMEOUT) )
  1711. {
  1712. break;
  1713. }
  1714. if( gIsoDep.cntRRetrys != 0U )
  1715. {
  1716. /* Ensure FDT before retransmission (reuse RFAL GT timer) */
  1717. rfalSetGT( rfalGetFDTPoll() );
  1718. rfalFieldOnAndStartGT();
  1719. /* Send RATS retransmission */ /* PRQA S 4342 1 # MISRA 10.5 - Layout of enum rfalIsoDepFSxI is guaranteed whithin 4bit range */
  1720. EXIT_ON_ERR( ret, rfalIsoDepStartRATS( (rfalIsoDepFSxI)(uint8_t)(gIsoDep.actv.ratsReq.PARAM >> RFAL_ISODEP_RATS_PARAM_FSDI_SHIFT),
  1721. gIsoDep.did,
  1722. &gIsoDep.actvDev->activation.A.Listener.ATS,
  1723. &gIsoDep.actvDev->activation.A.Listener.ATSLen ) );
  1724. gIsoDep.cntRRetrys--;
  1725. ret = ERR_BUSY;
  1726. }
  1727. /* Switch between NFC Forum and ISO14443-4 behaviour #595
  1728. * ISO14443-4 5.6.1 If RATS fails, a Deactivation sequence should be performed as defined on clause 8
  1729. * Activity 1.1 9.6 Device Deactivation Activity is to be only performed when there's an active device */
  1730. else if( gIsoDep.compMode == RFAL_COMPLIANCE_MODE_ISO )
  1731. {
  1732. rfalIsoDepDeselect();
  1733. }
  1734. else
  1735. {
  1736. /* MISRA 15.7 - Empty else */
  1737. }
  1738. }
  1739. else /* ATS received */
  1740. {
  1741. maxBR = gIsoDep.actvDev->info.DSI; /* Retrieve requested max bitrate */
  1742. /*******************************************************************************/
  1743. /* Process ATS Response */
  1744. gIsoDep.actvDev->info.FWI = RFAL_ISODEP_FWI_DEFAULT; /* Default value EMVCo 2.6 5.7.2.6 */
  1745. gIsoDep.actvDev->info.SFGI = 0U;
  1746. gIsoDep.actvDev->info.MBL = 0U;
  1747. gIsoDep.actvDev->info.DSI = RFAL_BR_106;
  1748. gIsoDep.actvDev->info.DRI = RFAL_BR_106;
  1749. gIsoDep.actvDev->info.FSxI = (uint8_t)RFAL_ISODEP_FSXI_32; /* FSC default value is 32 bytes ISO14443-A 5.2.3 */
  1750. /*******************************************************************************/
  1751. /* Check for ATS optional fields */
  1752. if( gIsoDep.actvDev->activation.A.Listener.ATS.TL > RFAL_ISODEP_ATS_MIN_LEN )
  1753. {
  1754. msgIt = RFAL_ISODEP_ATS_MIN_LEN;
  1755. /* Format byte T0 is optional, if present assign FSDI */
  1756. gIsoDep.actvDev->info.FSxI = (gIsoDep.actvDev->activation.A.Listener.ATS.T0 & RFAL_ISODEP_ATS_T0_FSCI_MASK);
  1757. /* T0 has already been processed, always the same position */
  1758. msgIt++;
  1759. /* Check if TA is present */
  1760. if( (gIsoDep.actvDev->activation.A.Listener.ATS.T0 & RFAL_ISODEP_ATS_T0_TA_PRESENCE_MASK) != 0U )
  1761. {
  1762. rfalIsoDepCalcBitRate( maxBR, ((uint8_t*)&gIsoDep.actvDev->activation.A.Listener.ATS)[msgIt++], &gIsoDep.actvDev->info.DSI, &gIsoDep.actvDev->info.DRI );
  1763. }
  1764. /* Check if TB is present */
  1765. if( (gIsoDep.actvDev->activation.A.Listener.ATS.T0 & RFAL_ISODEP_ATS_T0_TB_PRESENCE_MASK) != 0U )
  1766. {
  1767. gIsoDep.actvDev->info.SFGI = ((uint8_t*)&gIsoDep.actvDev->activation.A.Listener.ATS)[msgIt++];
  1768. gIsoDep.actvDev->info.FWI = (uint8_t)((gIsoDep.actvDev->info.SFGI >> RFAL_ISODEP_ATS_TB_FWI_SHIFT) & RFAL_ISODEP_ATS_FWI_MASK);
  1769. gIsoDep.actvDev->info.SFGI &= RFAL_ISODEP_ATS_TB_SFGI_MASK;
  1770. }
  1771. /* Check if TC is present */
  1772. if( (gIsoDep.actvDev->activation.A.Listener.ATS.T0 & RFAL_ISODEP_ATS_T0_TC_PRESENCE_MASK) != 0U )
  1773. {
  1774. /* Check for Protocol features support */
  1775. /* Advanced protocol features defined on Digital 1.0 Table 69, removed after */
  1776. gIsoDep.actvDev->info.supAdFt = (((((uint8_t*)&gIsoDep.actvDev->activation.A.Listener.ATS)[msgIt] & RFAL_ISODEP_ATS_TC_ADV_FEAT) != 0U) ? true : false);
  1777. gIsoDep.actvDev->info.supDID = (((((uint8_t*)&gIsoDep.actvDev->activation.A.Listener.ATS)[msgIt] & RFAL_ISODEP_ATS_TC_DID) != 0U) ? true : false);
  1778. gIsoDep.actvDev->info.supNAD = (((((uint8_t*)&gIsoDep.actvDev->activation.A.Listener.ATS)[msgIt++] & RFAL_ISODEP_ATS_TC_NAD) != 0U) ? true : false);
  1779. }
  1780. }
  1781. gIsoDep.actvDev->info.FSx = rfalIsoDepFSxI2FSx(gIsoDep.actvDev->info.FSxI);
  1782. gIsoDep.fsx = gIsoDep.actvDev->info.FSx;
  1783. gIsoDep.actvDev->info.SFGT = rfalIsoDepSFGI2SFGT( (uint8_t)gIsoDep.actvDev->info.SFGI );
  1784. /* Ensure SFGT before following frame (reuse RFAL GT timer) */
  1785. rfalSetGT( rfalConvMsTo1fc(gIsoDep.actvDev->info.SFGT) );
  1786. rfalFieldOnAndStartGT();
  1787. gIsoDep.actvDev->info.FWT = rfalIsoDepFWI2FWT( gIsoDep.actvDev->info.FWI );
  1788. gIsoDep.actvDev->info.dFWT = RFAL_ISODEP_DFWT_20;
  1789. gIsoDep.actvDev->info.DID = ( (gIsoDep.actvDev->info.supDID) ? gIsoDep.did : RFAL_ISODEP_NO_DID);
  1790. gIsoDep.actvDev->info.NAD = RFAL_ISODEP_NO_NAD;
  1791. /*******************************************************************************/
  1792. /* If higher bit rates are supported by both devices, send PPS */
  1793. if( (gIsoDep.actvDev->info.DSI != RFAL_BR_106) || (gIsoDep.actvDev->info.DRI != RFAL_BR_106) )
  1794. {
  1795. /* Send PPS */ /* PRQA S 0310 1 # MISRA 11.3 - Intentional safe cast to avoiding buffer duplication */
  1796. EXIT_ON_ERR( ret, rfalIsoDepStartPPS( gIsoDep.actvDev->info.DID, gIsoDep.actvDev->info.DSI, gIsoDep.actvDev->info.DRI, (rfalIsoDepPpsRes*)&gIsoDep.ctrlBuf ));
  1797. gIsoDep.state = ISODEP_ST_PCD_ACT_PPS;
  1798. return ERR_BUSY;
  1799. }
  1800. return ERR_NONE;
  1801. }
  1802. }
  1803. break;
  1804. /*******************************************************************************/
  1805. case ISODEP_ST_PCD_ACT_PPS:
  1806. ret = rfalIsoDepGetPPSSTatus();
  1807. if( ret != ERR_BUSY )
  1808. {
  1809. /* Check whether PPS has been acknowledge */
  1810. if( ret == ERR_NONE )
  1811. {
  1812. /* DSI code the divisor from PICC to PCD */
  1813. /* DRI code the divisor from PCD to PICC */
  1814. rfalSetBitRate( gIsoDep.actvDev->info.DRI, gIsoDep.actvDev->info.DSI );
  1815. }
  1816. else
  1817. {
  1818. /* If PPS has faled keep activation bit rate */
  1819. gIsoDep.actvDev->info.DSI = RFAL_BR_106;
  1820. gIsoDep.actvDev->info.DRI = RFAL_BR_106;
  1821. }
  1822. }
  1823. break;
  1824. /*******************************************************************************/
  1825. default:
  1826. ret = ERR_WRONG_STATE;
  1827. break;
  1828. }
  1829. return ret;
  1830. }
  1831. #endif /* RFAL_FEATURE_NFCA */
  1832. #if RFAL_FEATURE_NFCB
  1833. /*******************************************************************************/
  1834. ReturnCode rfalIsoDepPollBHandleActivation( rfalIsoDepFSxI FSDI, uint8_t DID, rfalBitRate maxBR, uint8_t PARAM1, const rfalNfcbListenDevice *nfcbDev, const uint8_t* HLInfo, uint8_t HLInfoLen, rfalIsoDepDevice *isoDepDev )
  1835. {
  1836. ReturnCode ret;
  1837. EXIT_ON_ERR( ret, rfalIsoDepPollBStartActivation( FSDI, DID, maxBR, PARAM1, nfcbDev, HLInfo, HLInfoLen, isoDepDev ) );
  1838. rfalIsoDepRunBlocking( ret, rfalIsoDepPollBGetActivationStatus() );
  1839. return ret;
  1840. }
  1841. /*******************************************************************************/
  1842. ReturnCode rfalIsoDepPollBStartActivation( rfalIsoDepFSxI FSDI, uint8_t DID, rfalBitRate maxBR, uint8_t PARAM1, const rfalNfcbListenDevice *nfcbDev, const uint8_t* HLInfo, uint8_t HLInfoLen, rfalIsoDepDevice *isoDepDev )
  1843. {
  1844. ReturnCode ret;
  1845. /***************************************************************************/
  1846. /* Initialize ISO-DEP Device with info from SENSB_RES */
  1847. isoDepDev->info.FWI = ((nfcbDev->sensbRes.protInfo.FwiAdcFo >> RFAL_NFCB_SENSB_RES_FWI_SHIFT) & RFAL_NFCB_SENSB_RES_FWI_MASK);
  1848. isoDepDev->info.FWT = rfalIsoDepFWI2FWT( isoDepDev->info.FWI );
  1849. isoDepDev->info.dFWT = RFAL_NFCB_DFWT;
  1850. isoDepDev->info.SFGI = (((uint32_t)nfcbDev->sensbRes.protInfo.SFGI >> RFAL_NFCB_SENSB_RES_SFGI_SHIFT) & RFAL_NFCB_SENSB_RES_SFGI_MASK);
  1851. isoDepDev->info.SFGT = rfalIsoDepSFGI2SFGT( (uint8_t)isoDepDev->info.SFGI );
  1852. isoDepDev->info.FSxI = ((nfcbDev->sensbRes.protInfo.FsciProType >> RFAL_NFCB_SENSB_RES_FSCI_SHIFT) & RFAL_NFCB_SENSB_RES_FSCI_MASK);
  1853. isoDepDev->info.FSx = rfalIsoDepFSxI2FSx(isoDepDev->info.FSxI);
  1854. isoDepDev->info.DID = DID;
  1855. isoDepDev->info.supDID = ((( nfcbDev->sensbRes.protInfo.FwiAdcFo & RFAL_NFCB_SENSB_RES_FO_DID_MASK ) != 0U) ? true : false);
  1856. isoDepDev->info.supNAD = ((( nfcbDev->sensbRes.protInfo.FwiAdcFo & RFAL_NFCB_SENSB_RES_FO_NAD_MASK ) != 0U) ? true : false);
  1857. /* Check if DID requested is supported by PICC */
  1858. if( (DID != RFAL_ISODEP_NO_DID) && (!isoDepDev->info.supDID) )
  1859. {
  1860. return ERR_PARAM;
  1861. }
  1862. /* Enable EMD handling according Digital 2.1 4.1.1.1 ; EMVCo 3.0 4.9.2 */
  1863. rfalSetErrorHandling( RFAL_ERRORHANDLING_EMVCO );
  1864. /***************************************************************************/
  1865. /* Set FDT Poll to be used on upcoming communications */
  1866. if( gIsoDep.compMode == RFAL_COMPLIANCE_MODE_EMV )
  1867. {
  1868. /* Disregard Minimum TR2 returned by PICC, always use FDTb MIN EMVCo 3.0 6.3.2.10 */
  1869. rfalSetFDTPoll( RFAL_FDT_POLL_NFCB_POLLER );
  1870. }
  1871. else
  1872. {
  1873. /* Apply minimum TR2 from SENSB_RES Digital 2.1 7.6.2.23 */
  1874. rfalSetFDTPoll( rfalNfcbTR2ToFDT(((nfcbDev->sensbRes.protInfo.FsciProType >>RFAL_NFCB_SENSB_RES_PROTO_TR2_SHIFT) & RFAL_NFCB_SENSB_RES_PROTO_TR2_MASK)) );
  1875. }
  1876. /* Calculate max Bit Rate */
  1877. rfalIsoDepCalcBitRate( maxBR, nfcbDev->sensbRes.protInfo.BRC, &isoDepDev->info.DSI, &isoDepDev->info.DRI );
  1878. /***************************************************************************/
  1879. /* Send ATTRIB Command */
  1880. EXIT_ON_ERR( ret, rfalIsoDepStartATTRIB( (const uint8_t*)&nfcbDev->sensbRes.nfcid0,
  1881. (((nfcbDev->sensbRes.protInfo.FwiAdcFo & RFAL_NFCB_SENSB_RES_ADC_ADV_FEATURE_MASK) != 0U) ? PARAM1 : RFAL_ISODEP_ATTRIB_REQ_PARAM1_DEFAULT),
  1882. isoDepDev->info.DSI,
  1883. isoDepDev->info.DRI,
  1884. FSDI,
  1885. (gIsoDep.compMode == RFAL_COMPLIANCE_MODE_EMV) ? RFAL_NFCB_SENSB_RES_PROTO_ISO_MASK : (nfcbDev->sensbRes.protInfo.FsciProType & ( (RFAL_NFCB_SENSB_RES_PROTO_TR2_MASK<<RFAL_NFCB_SENSB_RES_PROTO_TR2_SHIFT) | RFAL_NFCB_SENSB_RES_PROTO_ISO_MASK)), /* EMVCo 2.6 6.4.1.9 */
  1886. DID,
  1887. HLInfo,
  1888. HLInfoLen,
  1889. (isoDepDev->info.FWT + isoDepDev->info.dFWT),
  1890. &isoDepDev->activation.B.Listener.ATTRIB_RES,
  1891. &isoDepDev->activation.B.Listener.ATTRIB_RESLen
  1892. ) );
  1893. gIsoDep.actvDev = isoDepDev;
  1894. return ret;
  1895. }
  1896. /*******************************************************************************/
  1897. ReturnCode rfalIsoDepPollBGetActivationStatus( void )
  1898. {
  1899. ReturnCode ret;
  1900. uint8_t mbli;
  1901. /***************************************************************************/
  1902. /* Process ATTRIB Response */
  1903. ret = rfalIsoDepGetATTRIBStatus();
  1904. if( ret != ERR_BUSY)
  1905. {
  1906. if( ret == ERR_NONE )
  1907. {
  1908. /* Digital 1.1 14.6.2.3 - Check if received DID match */
  1909. if( (gIsoDep.actvDev->activation.B.Listener.ATTRIB_RES.mbliDid & RFAL_ISODEP_ATTRIB_RES_DID_MASK) != gIsoDep.did )
  1910. {
  1911. return ERR_PROTO;
  1912. }
  1913. /* Retrieve MBLI and calculate new FDS/MBL (Maximum Buffer Length) */
  1914. mbli = ((gIsoDep.actvDev->activation.B.Listener.ATTRIB_RES.mbliDid >> RFAL_ISODEP_ATTRIB_RES_MBLI_SHIFT) & RFAL_ISODEP_ATTRIB_RES_MBLI_MASK);
  1915. if( mbli > 0U)
  1916. {
  1917. /* Digital 1.1 14.6.2 Calculate Maximum Buffer Length MBL = FSC x 2^(MBLI-1) */
  1918. gIsoDep.actvDev->info.MBL = (gIsoDep.actvDev->info.FSx * ((uint32_t)1U<<(mbli-1U)));
  1919. }
  1920. /* DSI code the divisor from PICC to PCD */
  1921. /* DRI code the divisor from PCD to PICC */
  1922. rfalSetBitRate( gIsoDep.actvDev->info.DRI, gIsoDep.actvDev->info.DSI );
  1923. /* REMARK: SoF EoF TR0 and TR1 are not passed on to RF layer */
  1924. /* Start the SFGT timer (reuse RFAL GT timer) */
  1925. rfalSetGT( rfalConvMsTo1fc(gIsoDep.actvDev->info.SFGT) );
  1926. rfalFieldOnAndStartGT();
  1927. }
  1928. else
  1929. {
  1930. gIsoDep.actvDev->info.DSI = RFAL_BR_106;
  1931. gIsoDep.actvDev->info.DRI = RFAL_BR_106;
  1932. }
  1933. /*******************************************************************************/
  1934. /* Store already FS info, rfalIsoDepGetMaxInfLen() may be called before setting TxRx params */
  1935. gIsoDep.fsx = gIsoDep.actvDev->info.FSx;
  1936. }
  1937. return ret;
  1938. }
  1939. #endif /* RFAL_FEATURE_NFCB */
  1940. /*******************************************************************************/
  1941. ReturnCode rfalIsoDepPollHandleSParameters( rfalIsoDepDevice *isoDepDev, rfalBitRate maxTxBR, rfalBitRate maxRxBR )
  1942. {
  1943. uint8_t it;
  1944. uint8_t supPCD2PICC;
  1945. uint8_t supPICC2PCD;
  1946. uint8_t currenttxBR;
  1947. uint8_t currentrxBR;
  1948. rfalBitRate txBR;
  1949. rfalBitRate rxBR;
  1950. uint16_t rcvLen;
  1951. ReturnCode ret;
  1952. rfalIsoDepControlMsgSParam sParam;
  1953. if( (isoDepDev == NULL) || (maxTxBR > RFAL_BR_13560) || (maxRxBR > RFAL_BR_13560) )
  1954. {
  1955. return ERR_PARAM;
  1956. }
  1957. it = 0;
  1958. supPICC2PCD = 0x00;
  1959. supPCD2PICC = 0x00;
  1960. txBR = RFAL_BR_106;
  1961. rxBR = RFAL_BR_106;
  1962. sParam.pcb = ISODEP_PCB_SPARAMETERS;
  1963. /*******************************************************************************/
  1964. /* Send S(PARAMETERS) - Block Info */
  1965. sParam.sParam.tag = RFAL_ISODEP_SPARAM_TAG_BLOCKINFO;
  1966. sParam.sParam.value[it++] = RFAL_ISODEP_SPARAM_TAG_BRREQ;
  1967. sParam.sParam.value[it++] = RFAL_ISODEP_SPARAM_TAG_BRREQ_LEN;
  1968. sParam.sParam.length = it;
  1969. /* Send S(PARAMETERS). Use a fixed FWI of 4 ISO14443-4 2016 7.2 */
  1970. EXIT_ON_ERR( ret, rfalTransceiveBlockingTxRx( (uint8_t*)&sParam, (RFAL_ISODEP_SPARAM_HDR_LEN + (uint16_t)it), (uint8_t*)&sParam, sizeof(rfalIsoDepControlMsgSParam), &rcvLen, RFAL_TXRX_FLAGS_DEFAULT, ISODEP_FWT_DEACTIVATION ));
  1971. it = 0;
  1972. /*******************************************************************************/
  1973. /* Check S(PARAMETERS) response */
  1974. if( (sParam.pcb != ISODEP_PCB_SPARAMETERS) || (sParam.sParam.tag != RFAL_ISODEP_SPARAM_TAG_BLOCKINFO) ||
  1975. (sParam.sParam.value[it] != RFAL_ISODEP_SPARAM_TAG_BRIND) || (rcvLen < RFAL_ISODEP_SPARAM_HDR_LEN) ||
  1976. (rcvLen != ((uint16_t)sParam.sParam.length + RFAL_ISODEP_SPARAM_HDR_LEN)) )
  1977. {
  1978. return ERR_PROTO;
  1979. }
  1980. /* Retrieve PICC's bit rate PICC capabilities */
  1981. for( it=0; it<(rcvLen-(uint16_t)RFAL_ISODEP_SPARAM_TAG_LEN); it++ )
  1982. {
  1983. if( (sParam.sParam.value[it] == RFAL_ISODEP_SPARAM_TAG_SUP_PCD2PICC) && (sParam.sParam.value[it+(uint16_t)RFAL_ISODEP_SPARAM_TAG_LEN] == RFAL_ISODEP_SPARAM_TAG_PCD2PICC_LEN) )
  1984. {
  1985. supPCD2PICC = sParam.sParam.value[it + RFAL_ISODEP_SPARAM_TAG_PCD2PICC_LEN];
  1986. }
  1987. if( (sParam.sParam.value[it] == RFAL_ISODEP_SPARAM_TAG_SUP_PICC2PCD) && (sParam.sParam.value[it+(uint16_t)RFAL_ISODEP_SPARAM_TAG_LEN] == RFAL_ISODEP_SPARAM_TAG_PICC2PCD_LEN) )
  1988. {
  1989. supPICC2PCD = sParam.sParam.value[it + RFAL_ISODEP_SPARAM_TAG_PICC2PCD_LEN];
  1990. }
  1991. }
  1992. /*******************************************************************************/
  1993. /* Check if requested bit rates are supported by PICC */
  1994. if( (supPICC2PCD == 0x00U) || (supPCD2PICC == 0x00U) )
  1995. {
  1996. return ERR_PROTO;
  1997. }
  1998. for( it=0; it<=(uint8_t)maxTxBR; it++ )
  1999. {
  2000. if( (supPCD2PICC & (0x01U << it)) != 0U )
  2001. {
  2002. txBR = (rfalBitRate)it; /* PRQA S 4342 # MISRA 10.5 - Layout of enum rfalBitRate and above clamping of maxTxBR guarantee no invalid enum values to be created */
  2003. }
  2004. }
  2005. for( it=0; it<=(uint8_t)maxRxBR; it++ )
  2006. {
  2007. if( (supPICC2PCD & (0x01U << it)) != 0U )
  2008. {
  2009. rxBR = (rfalBitRate)it; /* PRQA S 4342 # MISRA 10.5 - Layout of enum rfalBitRate and above clamping of maxTxBR guarantee no invalid enum values to be created */
  2010. }
  2011. }
  2012. it = 0;
  2013. currenttxBR = (uint8_t)txBR;
  2014. currentrxBR = (uint8_t)rxBR;
  2015. /*******************************************************************************/
  2016. /* Send S(PARAMETERS) - Bit rates Activation */
  2017. sParam.sParam.tag = RFAL_ISODEP_SPARAM_TAG_BLOCKINFO;
  2018. sParam.sParam.value[it++] = RFAL_ISODEP_SPARAM_TAG_BRACT;
  2019. sParam.sParam.value[it++] = ( RFAL_ISODEP_SPARAM_TVL_HDR_LEN + RFAL_ISODEP_SPARAM_TAG_PCD2PICC_LEN + RFAL_ISODEP_SPARAM_TVL_HDR_LEN + RFAL_ISODEP_SPARAM_TAG_PICC2PCD_LEN);
  2020. sParam.sParam.value[it++] = RFAL_ISODEP_SPARAM_TAG_SEL_PCD2PICC;
  2021. sParam.sParam.value[it++] = RFAL_ISODEP_SPARAM_TAG_PCD2PICC_LEN;
  2022. sParam.sParam.value[it++] = ((uint8_t)0x01U << currenttxBR);
  2023. sParam.sParam.value[it++] = 0x00U;
  2024. sParam.sParam.value[it++] = RFAL_ISODEP_SPARAM_TAG_SEL_PICC2PCD;
  2025. sParam.sParam.value[it++] = RFAL_ISODEP_SPARAM_TAG_PICC2PCD_LEN;
  2026. sParam.sParam.value[it++] = ((uint8_t)0x01U << currentrxBR);
  2027. sParam.sParam.value[it++] = 0x00U;
  2028. sParam.sParam.length = it;
  2029. EXIT_ON_ERR( ret, rfalTransceiveBlockingTxRx( (uint8_t*)&sParam, (RFAL_ISODEP_SPARAM_HDR_LEN + (uint16_t)it), (uint8_t*)&sParam, sizeof(rfalIsoDepControlMsgSParam), &rcvLen, RFAL_TXRX_FLAGS_DEFAULT, (isoDepDev->info.FWT + isoDepDev->info.dFWT) ));
  2030. it = 0;
  2031. /*******************************************************************************/
  2032. /* Check S(PARAMETERS) Acknowledge */
  2033. if( (sParam.pcb != ISODEP_PCB_SPARAMETERS) || (sParam.sParam.tag != RFAL_ISODEP_SPARAM_TAG_BLOCKINFO) ||
  2034. (sParam.sParam.value[it] != RFAL_ISODEP_SPARAM_TAG_BRACK) || (rcvLen < RFAL_ISODEP_SPARAM_HDR_LEN) )
  2035. {
  2036. return ERR_PROTO;
  2037. }
  2038. EXIT_ON_ERR( ret, rfalSetBitRate( txBR, rxBR ) );
  2039. isoDepDev->info.DRI = txBR;
  2040. isoDepDev->info.DSI = rxBR;
  2041. return ERR_NONE;
  2042. }
  2043. /*******************************************************************************/
  2044. static void rfalIsoDepCalcBitRate( rfalBitRate maxAllowedBR, uint8_t piccBRCapability, rfalBitRate *dsi, rfalBitRate *dri )
  2045. {
  2046. uint8_t driMask;
  2047. uint8_t dsiMask;
  2048. int8_t i;
  2049. bool bitrateFound;
  2050. rfalBitRate curMaxBR;
  2051. curMaxBR = maxAllowedBR;
  2052. do
  2053. {
  2054. bitrateFound = true;
  2055. (*dsi) = RFAL_BR_106;
  2056. (*dri) = RFAL_BR_106;
  2057. /* Digital 1.0 5.6.2.5 & 11.6.2.14: A received RFU value of b4 = 1b MUST be interpreted as if b7 to b1 ? 0000000b (only 106 kbits/s in both direction) */
  2058. if( ((RFAL_ISODEP_BITRATE_RFU_MASK & piccBRCapability) != 0U) || (curMaxBR > RFAL_BR_848) || (curMaxBR == RFAL_BR_KEEP) )
  2059. {
  2060. return;
  2061. }
  2062. /***************************************************************************/
  2063. /* Determine Listen->Poll bit rate */
  2064. dsiMask = (piccBRCapability & RFAL_ISODEP_BSI_MASK);
  2065. for( i = 2; i >= 0; i-- ) // Check supported bit rate from the highest
  2066. {
  2067. if (((dsiMask & (0x10U << (uint8_t)i)) != 0U) && (((uint8_t)i+1U) <= (uint8_t)curMaxBR))
  2068. {
  2069. uint8_t newdsi = ((uint8_t) i) + 1U;
  2070. (*dsi) = (rfalBitRate)newdsi; /* PRQA S 4342 # MISRA 10.5 - Layout of enum rfalBitRate and range of loop variable guarantee no invalid enum values to be created */
  2071. break;
  2072. }
  2073. }
  2074. /***************************************************************************/
  2075. /* Determine Poll->Listen bit rate */
  2076. driMask = (piccBRCapability & RFAL_ISODEP_BRI_MASK);
  2077. for( i = 2; i >= 0; i-- ) /* Check supported bit rate from the highest */
  2078. {
  2079. if (((driMask & (0x01U << (uint8_t)i)) != 0U) && (((uint8_t)i+1U) <= (uint8_t)curMaxBR))
  2080. {
  2081. uint8_t newdri = ((uint8_t) i) + 1U;
  2082. (*dri) = (rfalBitRate)newdri; /* PRQA S 4342 # MISRA 10.5 - Layout of enum rfalBitRate and range of loop variable guarantee no invalid enum values to be created */
  2083. break;
  2084. }
  2085. }
  2086. /***************************************************************************/
  2087. /* Check if different bit rate is supported */
  2088. /* Digital 1.0 Table 67: if b8=1b, then only the same bit rate divisor for both directions is supported */
  2089. if( (piccBRCapability & RFAL_ISODEP_SAME_BITRATE_MASK) != 0U )
  2090. {
  2091. (*dsi) = MIN((*dsi), (*dri));
  2092. (*dri) = (*dsi);
  2093. /* Check that the baudrate is supported */
  2094. if( (RFAL_BR_106 != (*dsi)) && ( !(((dsiMask & (0x10U << ((uint8_t)(*dsi) - 1U))) != 0U) && ((driMask & (0x01U << ((uint8_t)(*dri) - 1U))) != 0U)) ) )
  2095. {
  2096. bitrateFound = false;
  2097. curMaxBR = (*dsi); /* set allowed bitrate to be lowest and determine bit rate again */
  2098. }
  2099. }
  2100. } while (!(bitrateFound));
  2101. }
  2102. /*******************************************************************************/
  2103. static uint32_t rfalIsoDepSFGI2SFGT( uint8_t sfgi )
  2104. {
  2105. uint32_t sfgt;
  2106. uint8_t tmpSFGI;
  2107. tmpSFGI = sfgi;
  2108. if (tmpSFGI > ISODEP_SFGI_MAX)
  2109. {
  2110. tmpSFGI = ISODEP_SFGI_MIN;
  2111. }
  2112. if (tmpSFGI != ISODEP_SFGI_MIN)
  2113. {
  2114. /* If sfgi != 0 wait SFGT + dSFGT Digital 1.1 13.8.2.1 */
  2115. sfgt = isoDepCalcSGFT(sfgi) + isoDepCalcdSGFT(sfgi);
  2116. }
  2117. /* Otherwise use FDTPoll min Digital 1.1 13.8.2.3*/
  2118. else
  2119. {
  2120. sfgt = RFAL_FDT_POLL_NFCA_POLLER;
  2121. }
  2122. /* Convert carrier cycles to milli seconds */
  2123. return (rfalConv1fcToMs(sfgt) + 1U);
  2124. }
  2125. #endif /* RFAL_FEATURE_ISO_DEP_POLL */
  2126. /*******************************************************************************/
  2127. static void rfalIsoDepApdu2IBLockParam( rfalIsoDepApduTxRxParam apduParam, rfalIsoDepTxRxParam *iBlockParam, uint16_t txPos, uint16_t rxPos )
  2128. {
  2129. NO_WARNING(rxPos); /* Keep this param for future use */
  2130. iBlockParam->DID = apduParam.DID;
  2131. iBlockParam->FSx = apduParam.FSx;
  2132. iBlockParam->ourFSx = apduParam.ourFSx;
  2133. iBlockParam->FWT = apduParam.FWT;
  2134. iBlockParam->dFWT = apduParam.dFWT;
  2135. if( (apduParam.txBufLen - txPos) > rfalIsoDepGetMaxInfLen() )
  2136. {
  2137. iBlockParam->isTxChaining = true;
  2138. iBlockParam->txBufLen = rfalIsoDepGetMaxInfLen();
  2139. }
  2140. else
  2141. {
  2142. iBlockParam->isTxChaining = false;
  2143. iBlockParam->txBufLen = (apduParam.txBufLen - txPos);
  2144. }
  2145. /* TxBuf is moved to the beginning for every I-Block */
  2146. iBlockParam->txBuf = (rfalIsoDepBufFormat*)apduParam.txBuf; /* PRQA S 0310 # MISRA 11.3 - Intentional safe cast to avoiding large buffer duplication */
  2147. iBlockParam->rxBuf = apduParam.tmpBuf; /* Simply using the apdu buffer is not possible because of current ACK handling */
  2148. iBlockParam->isRxChaining = &gIsoDep.isAPDURxChaining;
  2149. iBlockParam->rxLen = apduParam.rxLen;
  2150. }
  2151. /*******************************************************************************/
  2152. ReturnCode rfalIsoDepStartApduTransceive( rfalIsoDepApduTxRxParam param )
  2153. {
  2154. rfalIsoDepTxRxParam txRxParam;
  2155. /* Initialize and store APDU context */
  2156. gIsoDep.APDUParam = param;
  2157. gIsoDep.APDUTxPos = 0;
  2158. gIsoDep.APDURxPos = 0;
  2159. /* Assign current FSx to calculate INF length (only change the FSx from activation if no to Keep) */
  2160. gIsoDep.ourFsx = (( param.ourFSx != RFAL_ISODEP_FSX_KEEP ) ? param.ourFSx : gIsoDep.ourFsx);
  2161. gIsoDep.fsx = param.FSx;
  2162. /* Convert APDU TxRxParams to I-Block TxRxParams */
  2163. rfalIsoDepApdu2IBLockParam( gIsoDep.APDUParam, &txRxParam, gIsoDep.APDUTxPos, gIsoDep.APDURxPos );
  2164. return rfalIsoDepStartTransceive( txRxParam );
  2165. }
  2166. /*******************************************************************************/
  2167. ReturnCode rfalIsoDepGetApduTransceiveStatus( void )
  2168. {
  2169. ReturnCode ret;
  2170. rfalIsoDepTxRxParam txRxParam;
  2171. ret = rfalIsoDepGetTransceiveStatus();
  2172. switch( ret )
  2173. {
  2174. /*******************************************************************************/
  2175. case ERR_NONE:
  2176. /* Check if we are still doing chaining on Tx */
  2177. if( gIsoDep.isTxChaining )
  2178. {
  2179. /* Add already Tx bytes */
  2180. gIsoDep.APDUTxPos += gIsoDep.txBufLen;
  2181. /* Convert APDU TxRxParams to I-Block TxRxParams */
  2182. rfalIsoDepApdu2IBLockParam( gIsoDep.APDUParam, &txRxParam, gIsoDep.APDUTxPos, gIsoDep.APDURxPos );
  2183. if( txRxParam.txBufLen > 0U ) /* MISRA 21.18 */
  2184. {
  2185. /* Move next I-Block to beginning of APDU Tx buffer */
  2186. ST_MEMCPY( gIsoDep.APDUParam.txBuf->apdu, &gIsoDep.APDUParam.txBuf->apdu[gIsoDep.APDUTxPos], txRxParam.txBufLen );
  2187. }
  2188. EXIT_ON_ERR( ret, rfalIsoDepStartTransceive( txRxParam ) );
  2189. return ERR_BUSY;
  2190. }
  2191. /* APDU TxRx is done */
  2192. /* fall through */
  2193. /*******************************************************************************/
  2194. case ERR_AGAIN: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  2195. /* Check if no APDU transceive has been started before (data from rfalIsoDepListenStartActivation) */
  2196. if( gIsoDep.APDUParam.rxLen == NULL )
  2197. {
  2198. if( ret == ERR_AGAIN )
  2199. {
  2200. /* In Listen mode first chained packet cannot be retrieved via APDU interface */
  2201. return ERR_NOTSUPP;
  2202. }
  2203. /* TxRx is complete and full data is already available */
  2204. return ERR_NONE;
  2205. }
  2206. if( *gIsoDep.APDUParam.rxLen > 0U ) /* MISRA 21.18 */
  2207. {
  2208. /* Ensure that data in tmpBuf still fits into APDU buffer */
  2209. if( (gIsoDep.APDURxPos + (*gIsoDep.APDUParam.rxLen)) > (uint16_t)RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN )
  2210. {
  2211. return ERR_NOMEM;
  2212. }
  2213. /* Copy chained packet from tmp buffer to APDU buffer */
  2214. ST_MEMCPY( &gIsoDep.APDUParam.rxBuf->apdu[gIsoDep.APDURxPos], gIsoDep.APDUParam.tmpBuf->inf, *gIsoDep.APDUParam.rxLen );
  2215. gIsoDep.APDURxPos += *gIsoDep.APDUParam.rxLen;
  2216. }
  2217. /* Update output param rxLen */
  2218. *gIsoDep.APDUParam.rxLen = gIsoDep.APDURxPos;
  2219. /* Wait for following I-Block or APDU TxRx has finished */
  2220. return ((ret == ERR_AGAIN) ? ERR_BUSY : ERR_NONE);
  2221. /*******************************************************************************/
  2222. default:
  2223. /* MISRA 16.4: no empty default statement (a comment being enough) */
  2224. break;
  2225. }
  2226. return ret;
  2227. }
  2228. #endif /* RFAL_FEATURE_ISO_DEP */