rfal_nfcDep.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714
  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_nfcDep.c
  26. *
  27. * \author Gustavo Patricio
  28. *
  29. * \brief Implementation of NFC-DEP protocol
  30. *
  31. * NFC-DEP is also known as NFCIP - Near Field Communication
  32. * Interface and Protocol
  33. *
  34. * This implementation was based on the following specs:
  35. * - NFC Forum Digital 1.1
  36. * - ECMA 340 3rd Edition 2013
  37. *
  38. */
  39. /*
  40. ******************************************************************************
  41. * INCLUDES
  42. ******************************************************************************
  43. */
  44. #include "rfal_nfcDep.h"
  45. #include "rfal_nfcf.h"
  46. #include "utils.h"
  47. /*
  48. ******************************************************************************
  49. * ENABLE SWITCH
  50. ******************************************************************************
  51. */
  52. #if RFAL_FEATURE_NFC_DEP
  53. /* Check for valid Block/Payload length Digital 2.0 Table 90*/
  54. #if( (RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN != 64) && (RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN != 128) && (RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN != 192) && (RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN != 254) )
  55. #error " RFAL: Invalid NFC-DEP Block Max length. Please change RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN. "
  56. #endif
  57. /* Check for valid PDU length */
  58. #if( (RFAL_FEATURE_NFC_DEP_PDU_MAX_LEN < RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN) )
  59. #error " RFAL: Invalid NFC-DEP PDU Max length. Please change RFAL_FEATURE_NFC_DEP_PDU_MAX_LEN. "
  60. #endif
  61. /*
  62. ******************************************************************************
  63. * DEFINES
  64. ******************************************************************************
  65. */
  66. #define NFCIP_ATR_RETRY_MAX 2U /*!< Max consecutive retrys of an ATR REQ with transm error*/
  67. #define NFCIP_PSLPAY_LEN (2U) /*!< PSL Payload length (BRS + FSL) */
  68. #define NFCIP_PSLREQ_LEN (3U + RFAL_NFCDEP_LEN_LEN) /*!< PSL REQ length (incl LEN) */
  69. #define NFCIP_PSLRES_LEN (3U + RFAL_NFCDEP_LEN_LEN) /*!< PSL RES length (incl LEN) */
  70. #define NFCIP_ATRREQ_BUF_LEN (RFAL_NFCDEP_ATRREQ_MAX_LEN + RFAL_NFCDEP_LEN_LEN) /*!< ATR REQ max length (incl LEN) */
  71. #define NFCIP_ATRRES_BUF_LEN (RFAL_NFCDEP_ATRRES_MAX_LEN + RFAL_NFCDEP_LEN_LEN) /*!< ATR RES max length (incl LEN) */
  72. #define NFCIP_RLSREQ_LEN (3U + RFAL_NFCDEP_LEN_LEN) /*!< RLS REQ length (incl LEN) */
  73. #define NFCIP_RLSRES_LEN (3U + RFAL_NFCDEP_LEN_LEN) /*!< RSL RES length (incl LEN) */
  74. #define NFCIP_RLSRES_MIN (2U + RFAL_NFCDEP_LEN_LEN) /*!< Minimum length for a RLS RES (incl LEN) */
  75. #define NFCIP_DSLREQ_LEN (3U + RFAL_NFCDEP_LEN_LEN) /*!< DSL REQ length (incl LEN) */
  76. #define NFCIP_DSLRES_LEN (3U + RFAL_NFCDEP_LEN_LEN) /*!< DSL RES length (incl LEN) */
  77. #define NFCIP_DSLRES_MIN (2U + RFAL_NFCDEP_LEN_LEN) /*!< Minimum length for a DSL RES (incl LEN) */
  78. #define NFCIP_DSLRES_MAX_LEN (3U + RFAL_NFCDEP_LEN_LEN) /*!< Maximum length for a DSL RES (incl LEN) */
  79. #define NFCIP_RLSRES_MAX_LEN (3U + RFAL_NFCDEP_LEN_LEN) /*!< Minimum length for a RLS RES (incl LEN) */
  80. #define NFCIP_TARGET_RES_MAX ( MAX( NFCIP_RLSRES_MAX_LEN, NFCIP_DSLRES_MAX_LEN) ) /*!< Max target control res length */
  81. #define NFCIP_NO_FWT RFAL_FWT_NONE /*!< No FWT value - Target Mode */
  82. #define NFCIP_INIT_MIN_RTOX 1U /*!< Minimum RTOX value Digital 1.0 14.8.4.1 */
  83. #define NFCIP_INIT_MAX_RTOX 59U /*!< Maximum RTOX value Digital 1.0 14.8.4.1 */
  84. #define NFCIP_TARG_MIN_RTOX 1U /*!< Minimum target RTOX value Digital 1.0 14.8.4.1 */
  85. #define NFCIP_TARG_MAX_RTOX 59U /*!< Maximum target RTOX value Digital 1.0 14.8.4.1 */
  86. #define NFCIP_TRECOV 1280U /*!< Digital 1.0 A.10 Trecov */
  87. #define NFCIP_TIMEOUT_ADJUSTMENT 3072U /*!< Timeout Adjustment to compensate timing from end of Tx to end of frame */
  88. #define NFCIP_RWT_ACTIVATION (0x1000001U + NFCIP_TIMEOUT_ADJUSTMENT) /*!< Digital 2.2 B.11 RWT ACTIVATION 2^24 + RWT Delta + Adjustment*/
  89. #define NFCIP_RWT_ACM_ACTIVATION (0x200001U + NFCIP_TIMEOUT_ADJUSTMENT) /*!< Digital 2.2 B.11 RWT ACTIVATION 2^21 + RWT Delta + Adjustment*/
  90. #define RFAL_NFCDEP_HEADER_PAD (RFAL_NFCDEP_DEPREQ_HEADER_LEN - RFAL_NFCDEP_LEN_MIN) /*!< Difference between expected rcvd header len and max foreseen */
  91. #ifndef RFAL_NFCDEP_MAX_TX_RETRYS
  92. #define RFAL_NFCDEP_MAX_TX_RETRYS (uint8_t)3U /*!< Number of retransmit retyrs */
  93. #endif /* RFAL_NFCDEP_MAX_TX_RETRYS */
  94. #ifndef RFAL_NFCDEP_TO_RETRYS
  95. #define RFAL_NFCDEP_TO_RETRYS (uint8_t)3U /*!< Number of retrys for Timeout */
  96. #endif /* RFAL_NFCDEP_TO_RETRYS */
  97. #ifndef RFAL_NFCDEP_MAX_RTOX_RETRYS
  98. #define RFAL_NFCDEP_MAX_RTOX_RETRYS (uint8_t)10U /*!< Number of retrys for RTOX Digital 2.0 17.12.4.3 */
  99. #endif /* RFAL_NFCDEP_MAX_RTOX_RETRYS */
  100. #ifndef RFAL_NFCDEP_MAX_NACK_RETRYS
  101. #define RFAL_NFCDEP_MAX_NACK_RETRYS (uint8_t)3U /*!< Number of retrys for NACK */
  102. #endif /* RFAL_NFCDEP_MAX_NACK_RETRYS */
  103. #ifndef RFAL_NFCDEP_MAX_ATN_RETRYS
  104. #define RFAL_NFCDEP_MAX_ATN_RETRYS (uint8_t)3U /*!< Number of retrys for ATN */
  105. #endif /* RFAL_NFCDEP_MAX_ATN_RETRYS */
  106. #define NFCIP_MIN_TXERROR_LEN 4U /*!< Minimum frame length with error to be ignored Digital 1.0 14.12.5.4 */
  107. #define NFCIP_REQ (uint8_t)0xD4U /*!<NFCIP REQuest code */
  108. #define NFCIP_RES (uint8_t)0xD5U /*!<NFCIP RESponce code */
  109. #define NFCIP_BS_MASK 0x0FU /*!< Bit mask for BS value on a ATR REQ/RES */
  110. #define NFCIP_BR_MASK NFCIP_BS_MASK /*!< Bit mask for BR value on a ATR REQ/RES */
  111. #define NFCIP_PP_GB_MASK 0x02U /*!< Bit mask for GB value in PP byte on a ATR REQ/RES */
  112. #define NFCIP_PP_NAD_MASK 0x01U /*!< Bit mask for NAD value in PP byte on a ATR REQ/RES */
  113. #define NFCIP_PFB_xPDU_MASK 0xE0U /*!< Bit mask for PDU type */
  114. #define NFCIP_PFB_IPDU 0x00U /*!< Bit mask indicating a Information PDU */
  115. #define NFCIP_PFB_RPDU 0x40U /*!< Bit mask indicating a Response PDU */
  116. #define NFCIP_PFB_SPDU 0x80U /*!< Bit mask indicating a Supervisory PDU */
  117. #define NFCIP_PFB_MI_BIT 0x10U /*!< Bit mask for the chaining bit (MI) of PFB */
  118. #define NFCIP_PFB_DID_BIT 0x04U /*!< Bit mask for the DID presence bit of PFB */
  119. #define NFCIP_PFB_NAD_BIT 0x08U /*!< Bit mask for the NAD presence bit of PFB */
  120. #define NFCIP_PFB_PNI_MASK 0x03U /*!< Bit mask for the Packet Number Information */
  121. #define NFCIP_PFB_Rx_MASK 0x10U /*!< Bit mask for the R-PDU type */
  122. #define NFCIP_PFB_ACK 0x00U /*!< Bit mask for R-PDU indicating ACK */
  123. #define NFCIP_PFB_NACK 0x10U /*!< Bit mask for R-PDU indicating NAK */
  124. #define NFCIP_PFB_Sx_MASK 0x10U /*!< Bit mask for the R-PDU type */
  125. #define NFCIP_PFB_ATN 0x00U /*!< Bit mask for R-PDU indicating ACK */
  126. #define NFCIP_PFB_TO 0x10U /*!< Bit mask for R-PDU indicating NAK */
  127. #define NFCIP_PFB_INVALID 0xFFU /*!< Invalid PFB value */
  128. /*
  129. ******************************************************************************
  130. * MACROS
  131. ******************************************************************************
  132. */
  133. #define nfcipIsTransmissionError(e) ( ((e) == ERR_CRC) || ((e) == ERR_FRAMING) || ((e) == ERR_PAR) ) /*!< Checks if is a Trasmission error */
  134. #define nfcipConv1FcToMs( v ) (rfalConv1fcToMs((v)) + 1U) /*!< Converts value v 1fc into milliseconds (fc=13.56) */
  135. #define nfcipCmdIsReq( cmd ) (((uint8_t)(cmd) % 2U) == 0U) /*!< Checks if the nfcip cmd is a REQ */
  136. #define nfcip_PFBhasDID( pfb ) ( ((pfb) & NFCIP_PFB_DID_BIT) == NFCIP_PFB_DID_BIT) /*!< Checks if pfb is signalling DID */
  137. #define nfcip_PFBhasNAD( pfb ) ( ((pfb) & NFCIP_PFB_NAD_BIT) == NFCIP_PFB_NAD_BIT) /*!< Checks if pfb is signalling NAD */
  138. #define nfcip_PFBisIPDU( pfb ) ( ((pfb) & NFCIP_PFB_xPDU_MASK) == NFCIP_PFB_IPDU) /*!< Checks if pfb is a Information PDU */
  139. #define nfcip_PFBisRPDU( pfb ) ( ((pfb) & NFCIP_PFB_xPDU_MASK) == NFCIP_PFB_RPDU) /*!< Checks if pfb is Response PDU */
  140. #define nfcip_PFBisSPDU( pfb ) ( ((pfb) & NFCIP_PFB_xPDU_MASK) == NFCIP_PFB_SPDU) /*!< Checks if pfb is a Supervisory PDU */
  141. #define nfcip_PFBisIMI( pfb ) ( nfcip_PFBisIPDU( pfb ) && (((pfb) & NFCIP_PFB_MI_BIT) == NFCIP_PFB_MI_BIT)) /*!< Checks if pfb is a Information PDU indicating MI chaining */
  142. #define nfcip_PFBisRNACK( pfb ) ( nfcip_PFBisRPDU( pfb ) && (((pfb) & NFCIP_PFB_Rx_MASK) == NFCIP_PFB_NACK)) /*!< Checks if pfb is a R-PDU indicating NACK */
  143. #define nfcip_PFBisRACK( pfb ) ( nfcip_PFBisRPDU( pfb ) && (((pfb) & NFCIP_PFB_Rx_MASK) == NFCIP_PFB_ACK )) /*!< Checks if pfb is a R-PDU indicating ACK */
  144. #define nfcip_PFBisSATN( pfb ) ( nfcip_PFBisSPDU( pfb ) && (((pfb) & NFCIP_PFB_Sx_MASK) == NFCIP_PFB_ATN)) /*!< Checks if pfb is a R-PDU indicating ATN */
  145. #define nfcip_PFBisSTO( pfb ) ( nfcip_PFBisSPDU( pfb ) && (((pfb) & NFCIP_PFB_Sx_MASK) == NFCIP_PFB_TO) ) /*!< Checks if pfb is a R-PDU indicating TO */
  146. #define nfcip_PFBIPDU( pni ) ( (uint8_t)( 0x00U | NFCIP_PFB_IPDU | ((pni) & NFCIP_PFB_PNI_MASK) ))/*!< Returns a PFB I-PDU with the given packet number (pni) */
  147. #define nfcip_PFBIPDU_MI( pni ) ( (uint8_t)(isoDep_PCBIBlock(pni) | NFCIP_PFB_MI_BIT)) /*!< Returns a PFB I-PDU with the given packet number (pni) indicating chaing */
  148. #define nfcip_PFBRPDU( pni ) ( (uint8_t)( 0x00U | NFCIP_PFB_RPDU | ((pni) & NFCIP_PFB_PNI_MASK) ))/*!< Returns a PFB R-PDU with the given packet number (pni) */
  149. #define nfcip_PFBRPDU_NACK( pni ) ( (uint8_t)(nfcip_PFBRPDU(pni) | NFCIP_PFB_NACK)) /*!< Returns a PFB R-PDU with the given packet number (pni) indicating NACK */
  150. #define nfcip_PFBRPDU_ACK( pni ) ( (uint8_t)(nfcip_PFBRPDU(pni) | NFCIP_PFB_ACK)) /*!< Returns a PFB R-PDU with the given packet number (pni) indicating ACK */
  151. #define nfcip_PFBSPDU() ( (uint8_t)( 0x00U | NFCIP_PFB_SPDU )) /*!< Returns a PFB S-PDU */
  152. #define nfcip_PFBSPDU_ATN() ( (uint8_t)(nfcip_PFBSPDU() | NFCIP_PFB_ATN)) /*!< Returns a PFB S-PDU indicating ATN */
  153. #define nfcip_PFBSPDU_TO() ( (uint8_t)(nfcip_PFBSPDU() | NFCIP_PFB_TO)) /*!< Returns a PFB S-PDU indicating TO */
  154. #define nfcip_PNIInc( pni ) ( (uint8_t) (((pni)+1U) & NFCIP_PFB_PNI_MASK) ) /*!< Returns a incremented PNI from the given (pni) */
  155. #define nfcip_PNIDec( pni ) ( (uint8_t) (((pni)-1U) & NFCIP_PFB_PNI_MASK) ) /*!< Returns a decremented PNI from the given (pni) */
  156. #define nfcip_PBF_PNI( pfb ) ( (uint8_t) ((pfb) & NFCIP_PFB_PNI_MASK )) /*!< Returns the Packet Number Information (pni) */
  157. #define nfcip_PPwGB( lr ) ( rfalNfcDepLR2PP( lr ) | NFCIP_PP_GB_MASK) /*!< Returns a PP byte containing the given PP value indicating GB */
  158. #define nfcip_DIDMax( did ) ( MIN( (did), RFAL_NFCDEP_DID_MAX) ) /*!< Ensures that the given did has proper value Digital 14.6.2.3 DID [0 14] */
  159. #define nfcip_RTOXTargMax( wt ) (uint8_t)( MIN( (RFAL_NFCDEP_RWT_TRG_MAX / rfalNfcDepWT2RWT(wt)), NFCIP_TARG_MAX_RTOX) )/*!< Calculates the Maximum RTOX value for the given wt as a Target */
  160. #define nfcipIsInitiator( st ) ( ((st) >= NFCIP_ST_INIT_IDLE) && ((st) <= NFCIP_ST_INIT_RLS) ) /*!< Checks if module is set as Initiator */
  161. #define nfcipIsTarget( st ) (!nfcipIsInitiator(st)) /*!< Checks if module is set as Target */
  162. #define nfcipIsBRAllowed( br, mBR ) (((1U<<(br)) & (mBR)) != 0U) /*!< Checks bit rate is allowed by given mask */
  163. #define nfcipIsEmptyDEPEnabled( op ) (!nfcipIsEmptyDEPDisabled(op)) /*!< Checks if empty payload is allowed by operation config NCI 1.0 Table 81 */
  164. #define nfcipIsEmptyDEPDisabled( op ) (((op) & RFAL_NFCDEP_OPER_EMPTY_DEP_DIS) != 0U) /*!< Checks if empty payload is not allowed by operation config NCI 1.0 Table 81 */
  165. #define nfcipIsRTOXReqEnabled( op ) (!nfcipIsRTOXReqDisabled(op)) /*!< Checks if send a RTOX_REQ is allowed by operation config NCI 1.0 Table 81 */
  166. #define nfcipIsRTOXReqDisabled( op ) (((op) & RFAL_NFCDEP_OPER_RTOX_REQ_DIS) != 0U) /*!< Checks if send a RTOX_REQ is not allowed by operation config NCI 1.0 Table 81 */
  167. /*! Checks if isDeactivating callback is set and calls it, otherwise returns false */
  168. #define nfcipIsDeactivationPending() ( (gNfcip.isDeactivating == NULL) ? false : gNfcip.isDeactivating() )
  169. /*! Returns the RWT Activation according to the current communication mode */
  170. #define nfcipRWTActivation() ((gNfcip.cfg.commMode == RFAL_NFCDEP_COMM_ACTIVE) ? NFCIP_RWT_ACM_ACTIVATION : NFCIP_RWT_ACTIVATION)
  171. #define nfcipRTOXAdjust( v ) ((v) - ((v)>>3)) /*!< Adjust RTOX timer value to a percentage of the total, current 88% */
  172. /*******************************************************************************/
  173. // timerPollTimeoutValue is necessary after timerCalculateTimeout so that system will wake up upon timer timeout.
  174. #define nfcipTimerStart( timer, time_ms ) do{platformTimerDestroy( timer); (timer) = platformTimerCreate((uint16_t)(time_ms));} while (0) /*!< Configures and starts the RTOX timer */
  175. #define nfcipTimerisExpired( timer ) platformTimerIsExpired( timer ) /*!< Checks RTOX timer has expired */
  176. #define nfcipTimerDestroy( timer ) platformTimerDestroy( timer ) /*!< Destroys RTOX timer */
  177. #define nfcipLogE(...) /*!< Macro for the error log method */
  178. #define nfcipLogW(...) /*!< Macro for the warning log method */
  179. #define nfcipLogI(...) /*!< Macro for the info log method */
  180. #define nfcipLogD(...) /*!< Macro for the debug log method */
  181. /*! Digital 1.1 - 16.12.5.2 The Target SHALL NOT attempt any error recovery and remains in Rx mode upon Transmission or a Protocol Error */
  182. #define nfcDepReEnableRx( rxB, rxBL, rxL ) rfalTransceiveBlockingTx( NULL, 0, (rxB), (rxBL), (rxL), ( RFAL_TXRX_FLAGS_DEFAULT | (uint32_t)RFAL_TXRX_FLAGS_NFCIP1_ON ), RFAL_FWT_NONE )
  183. /*
  184. ******************************************************************************
  185. * LOCAL DATA TYPES
  186. ******************************************************************************
  187. */
  188. /*! Struct that holds all DEP parameters/configs for the following communications */
  189. typedef struct{
  190. uint8_t did; /*!< Device ID (DID) to be used */
  191. uint8_t* txBuf; /*!< Pointer to the Tx buffer to be sent */
  192. uint16_t txBufLen; /*!< Length of the data in the txBuf */
  193. uint8_t txBufPaylPos; /*!< Position inside txBuf where data starts */
  194. bool txChaining; /*!< Flag indicating chaining on transmission */
  195. uint8_t* rxBuf; /*!< Pointer to the Rx buffer for incoming data */
  196. uint16_t rxBufLen; /*!< Length of the data in the rxBuf */
  197. uint8_t rxBufPaylPos; /*!< Position inside rxBuf where data is to be placed*/
  198. uint32_t fwt; /*!< Frame Waiting Time (FWT) to be used */
  199. uint32_t dFwt; /*!< Delta Frame Waiting Time (dFWT) to be used */
  200. uint16_t fsc; /*!< Frame Size (FSC) to be used */
  201. } rfalNfcDepDEPParams;
  202. /*! NFCIP module states */
  203. typedef enum
  204. {
  205. NFCIP_ST_IDLE,
  206. NFCIP_ST_INIT_IDLE,
  207. NFCIP_ST_INIT_ATR,
  208. NFCIP_ST_INIT_PSL,
  209. NFCIP_ST_INIT_DEP_IDLE,
  210. NFCIP_ST_INIT_DEP_TX,
  211. NFCIP_ST_INIT_DEP_RX,
  212. NFCIP_ST_INIT_DEP_ATN,
  213. NFCIP_ST_INIT_DSL,
  214. NFCIP_ST_INIT_RLS,
  215. NFCIP_ST_TARG_WAIT_ATR,
  216. NFCIP_ST_TARG_WAIT_ACTV,
  217. NFCIP_ST_TARG_DEP_IDLE,
  218. NFCIP_ST_TARG_DEP_RX,
  219. NFCIP_ST_TARG_DEP_RTOX,
  220. NFCIP_ST_TARG_DEP_TX,
  221. NFCIP_ST_TARG_DEP_SLEEP
  222. } rfalNfcDepState;
  223. /*! NFCIP commands (Request, Response) */
  224. typedef enum{
  225. NFCIP_CMD_ATR_REQ = 0x00,
  226. NFCIP_CMD_ATR_RES = 0x01,
  227. NFCIP_CMD_WUP_REQ = 0x02,
  228. NFCIP_CMD_WUP_RES = 0x03,
  229. NFCIP_CMD_PSL_REQ = 0x04,
  230. NFCIP_CMD_PSL_RES = 0x05,
  231. NFCIP_CMD_DEP_REQ = 0x06,
  232. NFCIP_CMD_DEP_RES = 0x07,
  233. NFCIP_CMD_DSL_REQ = 0x08,
  234. NFCIP_CMD_DSL_RES = 0x09,
  235. NFCIP_CMD_RLS_REQ = 0x0A,
  236. NFCIP_CMD_RLS_RES = 0x0B
  237. } rfalNfcDepCmd;
  238. /*! Struct that holds all NFCIP data */
  239. typedef struct{
  240. rfalNfcDepConfigs cfg; /*!< Holds the current configuration to be used */
  241. rfalNfcDepState state; /*!< Current state of the NFCIP module */
  242. uint8_t pni; /*!< Packet Number Information (PNI) counter */
  243. uint8_t lastCmd; /*!< Last command sent */
  244. uint8_t lastPFB; /*!< Last PFB sent */
  245. uint8_t lastPFBnATN; /*!< Last PFB sent (excluding ATN) */
  246. uint8_t lastRTOX; /*!< Last RTOX value sent */
  247. uint8_t cntTxRetrys; /*!< Retransmissions counter */
  248. uint8_t cntTORetrys; /*!< Timeouts counter */
  249. uint8_t cntRTOXRetrys; /*!< RTOX counter */
  250. uint8_t cntNACKRetrys; /*!< NACK counter */
  251. uint8_t cntATNRetrys; /*!< Attention (ATN) counter */
  252. uint16_t fsc; /*!< Current Frame Size (FSC) to be used */
  253. bool isTxChaining; /*!< Flag for chaining on Transmission */
  254. bool isRxChaining; /*!< Flag for chaining on Reception */
  255. uint8_t* txBuf; /*!< Pointer to the Tx buffer to be sent */
  256. uint8_t* rxBuf; /*!< Pointer to the Rx buffer for incoming data */
  257. uint16_t txBufLen; /*!< Length of the data in the txBuf */
  258. uint16_t rxBufLen; /*!< Length of rxBuf buffer */
  259. uint16_t* rxRcvdLen; /*!< Length of the data in the rxBuf */
  260. uint8_t txBufPaylPos; /*!< Position in txBuf where data starts */
  261. uint8_t rxBufPaylPos; /*!< Position in rxBuf where data is to be placed */
  262. bool *isChaining; /*!< Flag for chaining on Reception */
  263. rfalNfcDepDevice *nfcDepDev; /*!< Pointer to NFC-DEP device info */
  264. uint32_t RTOXTimer; /*!< Timer used for RTOX */
  265. rfalNfcDepDeactCallback isDeactivating; /*!< Deactivating flag check callback */
  266. bool isReqPending; /*!< Flag pending REQ from Target activation */
  267. bool isTxPending; /*!< Flag pending DEP Block while waiting RTOX Ack */
  268. bool isWait4RTOX; /*!< Flag for waiting RTOX Ack */
  269. rfalNfcDepPduTxRxParam PDUParam; /*!< PDU TxRx params */
  270. uint16_t PDUTxPos; /*!< PDU Tx position */
  271. uint16_t PDURxPos; /*!< PDU Rx position */
  272. bool isPDURxChaining; /*!< PDU Transceive chaining flag */
  273. }rfalNfcDep;
  274. /*
  275. ******************************************************************************
  276. * LOCAL VARIABLES
  277. ******************************************************************************
  278. */
  279. static rfalNfcDep gNfcip; /*!< NFCIP module instance */
  280. /*
  281. ******************************************************************************
  282. * LOCAL FUNCTION PROTOTYPES
  283. ******************************************************************************
  284. */
  285. static ReturnCode nfcipTxRx( rfalNfcDepCmd cmd, uint8_t* txBuf, uint32_t fwt, uint8_t* paylBuf, uint8_t paylBufLen, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t *rxActLen );
  286. static ReturnCode nfcipTx( rfalNfcDepCmd cmd, uint8_t* txBuf, uint8_t *paylBuf, uint16_t paylLen, uint8_t pfbData, uint32_t fwt );
  287. static ReturnCode nfcipDEPControlMsg( uint8_t pfb, uint8_t RTOX );
  288. static ReturnCode nfcipInitiatorHandleDEP( ReturnCode rxRes, uint16_t rxLen, uint16_t *outActRxLen, bool *outIsChaining );
  289. static ReturnCode nfcipTargetHandleRX( ReturnCode rxRes, uint16_t *outActRxLen, bool *outIsChaining );
  290. static ReturnCode nfcipTargetHandleActivation( rfalNfcDepDevice *nfcDepDev, uint8_t *outBRS );
  291. /*!
  292. ******************************************************************************
  293. * \brief NFCIP Configure
  294. *
  295. * Configures the nfcip layer with the given configurations
  296. *
  297. * \param[in] cfg : nfcip configuration for following communication
  298. ******************************************************************************
  299. */
  300. static void nfcipConfig( const rfalNfcDepConfigs * cfg );
  301. /*!
  302. ******************************************************************************
  303. * \brief Set DEP parameters
  304. *
  305. * This method sets the parameters/configs for following Data Exchange
  306. * Sets the nfcip module state according to the role it is configured
  307. *
  308. *
  309. * \warning To be used only after proper Initiator/Target activation:
  310. * nfcipTargetHandleActivation() or nfcipInitiatorActivate() has
  311. * returned success
  312. *
  313. * This must be called before nfcipRun() in case of Target to pass
  314. * rxBuffer
  315. *
  316. * Everytime some data needs to be transmitted call this to set it and
  317. * call nfcipRun() until done or error
  318. *
  319. * \param[in] DEPParams : the parameters to be used during Data Exchange
  320. ******************************************************************************
  321. */
  322. static void nfcipSetDEPParams( const rfalNfcDepDEPParams *DEPParams );
  323. /*!
  324. ******************************************************************************
  325. * \brief NFCIP run protocol
  326. *
  327. * This method handles all the nfcip protocol during Data Exchange (DEP
  328. * requests and responses).
  329. *
  330. * A data exchange cycle is considered a DEP REQ and a DEP RES.
  331. *
  332. * In case of Tx chaining(MI) must signal it with nfcipSetDEPParams()
  333. * In case of Rx chaining(MI) outIsChaining will be set to true and the
  334. * current data returned
  335. *
  336. * \param[out] outActRxLen : data received length
  337. * \param[out] outIsChaining : true if other peer is performing chaining(MI)
  338. *
  339. * \return ERR_NONE : Data exchange cycle completed successfully
  340. * \return ERR_TIMEOUT : Timeout occurred
  341. * \return ERR_PROTO : Protocol error occurred
  342. * \return ERR_AGAIN : Other peer is doing chaining(MI), current block
  343. * was received successfully call again until complete
  344. *
  345. ******************************************************************************
  346. */
  347. static ReturnCode nfcipRun( uint16_t *outActRxLen, bool *outIsChaining );
  348. /*!
  349. ******************************************************************************
  350. * \brief Transmission method
  351. *
  352. * This method checks if the current communication is Active or Passive
  353. * and performs the necessary procedures for each communication type
  354. *
  355. * Transmits the data hold in txBuf
  356. *
  357. * \param[in] txBuf : buffer to transmit
  358. * \param[in] txBufLen : txBuffer capacity
  359. * \param[in] fwt : fwt for current Tx
  360. *
  361. * \return ERR_NONE : No error
  362. ******************************************************************************
  363. */
  364. static ReturnCode nfcipDataTx( uint8_t* txBuf, uint16_t txBufLen, uint32_t fwt );
  365. /*!
  366. ******************************************************************************
  367. * \brief Reception method
  368. *
  369. * This method checks if the current communication is Active or Passive
  370. * and calls the appropriate reception method
  371. *
  372. * Copies incoming data to rxBuf
  373. *
  374. * \param[in] blocking : reception is to be done blocking or non-blocking
  375. *
  376. * \return ERR_BUSY : Busy
  377. * \return ERR_NONE : No error
  378. ******************************************************************************
  379. */
  380. static ReturnCode nfcipDataRx( bool blocking );
  381. /*
  382. ******************************************************************************
  383. * LOCAL FUNCTIONS
  384. ******************************************************************************
  385. */
  386. /*******************************************************************************/
  387. /*******************************************************************************/
  388. static bool nfcipDxIsSupported( uint8_t Dx, uint8_t BRx, uint8_t BSx )
  389. {
  390. uint8_t Bx;
  391. /* Take the min of the possible bit rates, we'll use one for both directions */
  392. Bx = MIN(BRx, BSx);
  393. /* Lower bit rates must be supported for P2P */
  394. if( (Dx <= (uint8_t)RFAL_NFCDEP_Dx_04_424) )
  395. {
  396. return true;
  397. }
  398. if( (Dx == (uint8_t)RFAL_NFCDEP_Dx_08_848) && (Bx >= (uint8_t)RFAL_NFCDEP_Bx_08_848) )
  399. {
  400. return true;
  401. }
  402. return false;
  403. }
  404. /*******************************************************************************/
  405. static ReturnCode nfcipTxRx( rfalNfcDepCmd cmd, uint8_t* txBuf, uint32_t fwt, uint8_t* paylBuf, uint8_t paylBufLen, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t *rxActLen )
  406. {
  407. ReturnCode ret;
  408. if( (cmd == NFCIP_CMD_DEP_REQ) || (cmd == NFCIP_CMD_DEP_RES) ) /* this method cannot be used for DEPs */
  409. {
  410. return ERR_PARAM;
  411. }
  412. /* Assign the global params for this TxRx */
  413. gNfcip.rxBuf = rxBuf;
  414. gNfcip.rxBufLen = rxBufLen;
  415. gNfcip.rxRcvdLen = rxActLen;
  416. /*******************************************************************************/
  417. /* Transmission */
  418. /*******************************************************************************/
  419. if(txBuf != NULL) /* if nothing to Tx, just do Rx */
  420. {
  421. EXIT_ON_ERR( ret, nfcipTx( cmd, txBuf, paylBuf, paylBufLen, 0, fwt ) );
  422. }
  423. /*******************************************************************************/
  424. /* Reception */
  425. /*******************************************************************************/
  426. ret = nfcipDataRx( true );
  427. if( ret != ERR_NONE )
  428. {
  429. return ret;
  430. }
  431. /*******************************************************************************/
  432. *rxActLen = *rxBuf; /* Use LEN byte instead due to with/without CRC modes */
  433. return ERR_NONE; /* Tx and Rx completed successfully */
  434. }
  435. /*******************************************************************************/
  436. static ReturnCode nfcipDEPControlMsg( uint8_t pfb, uint8_t RTOX )
  437. {
  438. uint8_t ctrlMsg[20];
  439. rfalNfcDepCmd depCmd;
  440. uint32_t fwt;
  441. /*******************************************************************************/
  442. /* Calculate Cmd and fwt to be used */
  443. /*******************************************************************************/
  444. depCmd = ((gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET) ? NFCIP_CMD_DEP_RES : NFCIP_CMD_DEP_REQ);
  445. fwt = ((gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET) ? NFCIP_NO_FWT : (nfcip_PFBisSTO( pfb ) ? ( (RTOX*gNfcip.cfg.fwt) + gNfcip.cfg.dFwt) : (gNfcip.cfg.fwt + gNfcip.cfg.dFwt) ) );
  446. if( nfcip_PFBisSTO( pfb ) )
  447. {
  448. ctrlMsg[RFAL_NFCDEP_DEPREQ_HEADER_LEN] = RTOX;
  449. return nfcipTx( depCmd, ctrlMsg, &ctrlMsg[RFAL_NFCDEP_DEPREQ_HEADER_LEN], sizeof(uint8_t), pfb, fwt );
  450. }
  451. else
  452. {
  453. return nfcipTx( depCmd, ctrlMsg, NULL, 0, pfb, fwt );
  454. }
  455. }
  456. /*******************************************************************************/
  457. static void nfcipClearCounters( void )
  458. {
  459. gNfcip.cntATNRetrys = 0;
  460. gNfcip.cntNACKRetrys = 0;
  461. gNfcip.cntTORetrys = 0;
  462. gNfcip.cntTxRetrys = 0;
  463. gNfcip.cntRTOXRetrys = 0;
  464. }
  465. /*******************************************************************************/
  466. static ReturnCode nfcipInitiatorHandleDEP( ReturnCode rxRes, uint16_t rxLen, uint16_t *outActRxLen, bool *outIsChaining )
  467. {
  468. ReturnCode ret;
  469. uint8_t nfcDepLen;
  470. uint8_t rxMsgIt;
  471. uint8_t rxPFB;
  472. uint8_t rxRTOX;
  473. uint8_t optHdrLen;
  474. ret = ERR_INTERNAL;
  475. rxMsgIt = 0;
  476. optHdrLen = 0;
  477. *outActRxLen = 0;
  478. *outIsChaining = false;
  479. /*******************************************************************************/
  480. /* Handle reception errors */
  481. /*******************************************************************************/
  482. switch( rxRes )
  483. {
  484. /*******************************************************************************/
  485. /* Timeout -> Digital 1.0 14.15.5.6 */
  486. case ERR_TIMEOUT:
  487. nfcipLogI( " NFCIP(I) TIMEOUT TORetrys:%d \r\n", gNfcip.cntTORetrys );
  488. /* Digital 1.0 14.15.5.6 - If nTO >= Max raise protocol error */
  489. if( gNfcip.cntTORetrys++ >= RFAL_NFCDEP_TO_RETRYS )
  490. {
  491. return ERR_PROTO;
  492. }
  493. /*******************************************************************************/
  494. /* Upon Timeout error, if Deactivation is pending, no more error recovery
  495. * will be done #54.
  496. * This is used to address the issue some devices that havea big TO.
  497. * Normally LLCP layer has timeout already, and NFCIP layer is still
  498. * running error handling, retrying ATN/NACKs */
  499. /*******************************************************************************/
  500. if( nfcipIsDeactivationPending() )
  501. {
  502. nfcipLogI( " skipping error recovery due deactivation pending \r\n");
  503. return ERR_TIMEOUT;
  504. }
  505. /* Digital 1.0 14.15.5.6 1) If last PDU was NACK */
  506. if( nfcip_PFBisRNACK(gNfcip.lastPFB) )
  507. {
  508. /* Digital 1.0 14.15.5.6 2) if NACKs failed raise protocol error */
  509. if( gNfcip.cntNACKRetrys++ >= RFAL_NFCDEP_MAX_NACK_RETRYS )
  510. {
  511. return ERR_PROTO;
  512. }
  513. /* Send NACK */
  514. nfcipLogI( " NFCIP(I) Sending NACK retry: %d \r\n", gNfcip.cntNACKRetrys );
  515. EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBRPDU_NACK(gNfcip.pni), 0 ) );
  516. return ERR_BUSY;
  517. }
  518. nfcipLogI( " NFCIP(I) Checking if to send ATN ATNRetrys: %d \r\n", gNfcip.cntATNRetrys );
  519. /* Digital 1.0 14.15.5.6 3) Otherwise send ATN */
  520. if( gNfcip.cntATNRetrys++ >= RFAL_NFCDEP_MAX_NACK_RETRYS )
  521. {
  522. return ERR_PROTO;
  523. }
  524. /* Send ATN */
  525. nfcipLogI( " NFCIP(I) Sending ATN \r\n" );
  526. EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBSPDU_ATN(), 0 ) );
  527. return ERR_BUSY;
  528. /*******************************************************************************/
  529. /* Data rcvd with error -> Digital 1.0 14.12.5.4 */
  530. case ERR_CRC:
  531. case ERR_PAR:
  532. case ERR_FRAMING:
  533. case ERR_RF_COLLISION:
  534. nfcipLogI( " NFCIP(I) rx Error: %d \r\n", rxRes );
  535. /* Digital 1.0 14.12.5.4 Tx Error with data, ignore */
  536. if( rxLen < NFCIP_MIN_TXERROR_LEN )
  537. {
  538. nfcipLogI( " NFCIP(I) Transmission error w data \r\n" );
  539. #if 0
  540. if(gNfcip.cfg.commMode == RFAL_NFCDEP_COMM_PASSIVE)
  541. {
  542. nfcipLogI( " NFCIP(I) Transmission error w data -> reEnabling Rx \r\n" );
  543. nfcipReEnableRxTout( NFCIP_TRECOV );
  544. return ERR_BUSY;
  545. }
  546. #endif /* 0 */
  547. }
  548. /* Digital 1.1 16.12.5.4 if NACKs failed raise Transmission error */
  549. if( gNfcip.cntNACKRetrys++ >= RFAL_NFCDEP_MAX_NACK_RETRYS )
  550. {
  551. return ERR_FRAMING;
  552. }
  553. /* Send NACK */
  554. nfcipLogI( " NFCIP(I) Sending NACK \r\n" );
  555. EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBRPDU_NACK(gNfcip.pni), 0 ) );
  556. return ERR_BUSY;
  557. case ERR_NONE:
  558. break;
  559. case ERR_BUSY:
  560. return ERR_BUSY; /* Debug purposes */
  561. default:
  562. nfcipLogW( " NFCIP(I) Error: %d \r\n", rxRes );
  563. return rxRes;
  564. }
  565. /*******************************************************************************/
  566. /* Rx OK check if valid DEP PDU */
  567. /*******************************************************************************/
  568. /* Due to different modes on ST25R391x (with/without CRC) use NFC-DEP LEN instead of bytes retrieved */
  569. nfcDepLen = gNfcip.rxBuf[rxMsgIt++];
  570. nfcipLogD( " NFCIP(I) rx OK: %d bytes \r\n", nfcDepLen );
  571. /* Digital 1.0 14.15.5.5 Protocol Error */
  572. if( gNfcip.rxBuf[rxMsgIt++] != NFCIP_RES )
  573. {
  574. nfcipLogW( " NFCIP(I) error %02X instead of %02X \r\n", gNfcip.rxBuf[--rxMsgIt], NFCIP_RES );
  575. return ERR_PROTO;
  576. }
  577. /* Digital 1.0 14.15.5.5 Protocol Error */
  578. if( gNfcip.rxBuf[rxMsgIt++] != (uint8_t)NFCIP_CMD_DEP_RES )
  579. {
  580. nfcipLogW( " NFCIP(I) error %02X instead of %02X \r\n", gNfcip.rxBuf[--rxMsgIt], NFCIP_CMD_DEP_RES );
  581. return ERR_PROTO;
  582. }
  583. rxPFB = gNfcip.rxBuf[rxMsgIt++];
  584. /*******************************************************************************/
  585. /* Check for valid PFB type */
  586. if( !(nfcip_PFBisSPDU( rxPFB ) || nfcip_PFBisRPDU( rxPFB ) || nfcip_PFBisIPDU( rxPFB )) )
  587. {
  588. return ERR_PROTO;
  589. }
  590. /*******************************************************************************/
  591. /* Digital 1.0 14.8.2.1 check if DID is expected and match -> Protocol Error */
  592. if( gNfcip.cfg.did != RFAL_NFCDEP_DID_NO )
  593. {
  594. if( (gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.did) || !nfcip_PFBhasDID( rxPFB ) )
  595. {
  596. return ERR_PROTO;
  597. }
  598. optHdrLen++; /* Inc header optional field cnt*/
  599. }
  600. else if( nfcip_PFBhasDID( rxPFB ) ) /* DID not expected but rcv */
  601. {
  602. return ERR_PROTO;
  603. }
  604. else
  605. {
  606. /* MISRA 15.7 - Empty else */
  607. }
  608. /*******************************************************************************/
  609. /* Digital 1.0 14.6.2.8 & 14.6.3.11 NAD must not be used */
  610. if( gNfcip.cfg.nad != RFAL_NFCDEP_NAD_NO )
  611. {
  612. if( (gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.nad) || !nfcip_PFBhasNAD( rxPFB ) )
  613. {
  614. return ERR_PROTO;
  615. }
  616. optHdrLen++; /* Inc header optional field cnt*/
  617. }
  618. else if( nfcip_PFBhasNAD( rxPFB ) ) /* NAD not expected but rcv */
  619. {
  620. return ERR_PROTO;
  621. }
  622. else
  623. {
  624. /* MISRA 15.7 - Empty else */
  625. }
  626. /*******************************************************************************/
  627. /* Process R-PDU */
  628. /*******************************************************************************/
  629. if( nfcip_PFBisRPDU( rxPFB ) )
  630. {
  631. /*******************************************************************************/
  632. /* R ACK */
  633. /*******************************************************************************/
  634. if( nfcip_PFBisRACK( rxPFB ) )
  635. {
  636. nfcipLogI( " NFCIP(I) Rcvd ACK \r\n" );
  637. if( gNfcip.pni == nfcip_PBF_PNI( rxPFB ) )
  638. {
  639. /* 14.12.3.3 R-ACK with correct PNI -> Increment */
  640. gNfcip.pni = nfcip_PNIInc( gNfcip.pni );
  641. /* R-ACK while not performing chaining -> Protocol error*/
  642. if( !gNfcip.isTxChaining )
  643. {
  644. return ERR_PROTO;
  645. }
  646. nfcipClearCounters();
  647. gNfcip.state = NFCIP_ST_INIT_DEP_IDLE;
  648. return ERR_NONE; /* This block has been transmitted */
  649. }
  650. else /* Digital 1.0 14.12.4.5 ACK with wrong PNI Initiator may retransmit */
  651. {
  652. if( gNfcip.cntTxRetrys++ >= RFAL_NFCDEP_MAX_TX_RETRYS )
  653. {
  654. return ERR_PROTO;
  655. }
  656. /* Extended the MAY in Digital 1.0 14.12.4.5 to only reTransmit if the ACK
  657. * is for the previous DEP, otherwise raise Protocol immediately
  658. * If the PNI difference is more than 1 it is worthless to reTransmit 3x
  659. * and after raise the error */
  660. if( nfcip_PNIDec( gNfcip.pni ) == nfcip_PBF_PNI( rxPFB ) )
  661. {
  662. /* ReTransmit */
  663. nfcipLogI( " NFCIP(I) Rcvd ACK prev PNI -> reTx \r\n" );
  664. gNfcip.state = NFCIP_ST_INIT_DEP_TX;
  665. return ERR_BUSY;
  666. }
  667. nfcipLogI( " NFCIP(I) Rcvd ACK unexpected far PNI -> Error \r\n" );
  668. return ERR_PROTO;
  669. }
  670. }
  671. else /* Digital 1.0 - 14.12.5.2 Target must never send NACK */
  672. {
  673. return ERR_PROTO;
  674. }
  675. }
  676. /*******************************************************************************/
  677. /* Process S-PDU */
  678. /*******************************************************************************/
  679. if( nfcip_PFBisSPDU( rxPFB ) )
  680. {
  681. nfcipLogI( " NFCIP(I) Rcvd S-PDU \r\n" );
  682. /*******************************************************************************/
  683. /* S ATN */
  684. /*******************************************************************************/
  685. if( nfcip_PFBisSATN( rxPFB ) ) /* If is a S-ATN */
  686. {
  687. nfcipLogI( " NFCIP(I) Rcvd ATN \r\n" );
  688. if( nfcip_PFBisSATN( gNfcip.lastPFB ) ) /* Check if is expected */
  689. {
  690. gNfcip.cntATNRetrys = 0; /* Clear ATN counter */
  691. /* Although spec is not clear NFC Forum Digital test is expecting to
  692. * retransmit upon receiving ATN_RES */
  693. if( nfcip_PFBisSTO( gNfcip.lastPFBnATN ) )
  694. {
  695. nfcipLogI( " NFCIP(I) Rcvd ATN -> reTx RTOX_RES \r\n" );
  696. EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBSPDU_TO(), gNfcip.lastRTOX ) );
  697. }
  698. else
  699. {
  700. /* ReTransmit ? */
  701. if( gNfcip.cntTxRetrys++ >= RFAL_NFCDEP_MAX_TX_RETRYS )
  702. {
  703. return ERR_PROTO;
  704. }
  705. nfcipLogI( " NFCIP(I) Rcvd ATN -> reTx PNI: %d \r\n", gNfcip.pni );
  706. gNfcip.state = NFCIP_ST_INIT_DEP_TX;
  707. }
  708. return ERR_BUSY;
  709. }
  710. else /* Digital 1.0 14.12.4.4 & 14.12.4.8 */
  711. {
  712. return ERR_PROTO;
  713. }
  714. }
  715. /*******************************************************************************/
  716. /* S TO */
  717. /*******************************************************************************/
  718. else if( nfcip_PFBisSTO( rxPFB ) ) /* If is a S-TO (RTOX) */
  719. {
  720. nfcipLogI( " NFCIP(I) Rcvd TO \r\n" );
  721. rxRTOX = gNfcip.rxBuf[rxMsgIt++];
  722. /* Digital 1.1 16.12.4.3 - Initiator MAY stop accepting subsequent RTOX Req *
  723. * - RTOX request to an ATN -> Protocol error */
  724. if( (gNfcip.cntRTOXRetrys++ > RFAL_NFCDEP_MAX_RTOX_RETRYS) || nfcip_PFBisSATN( gNfcip.lastPFB ) )
  725. {
  726. return ERR_PROTO;
  727. }
  728. /* Digital 1.1 16.8.4.1 RTOX must be between [1,59] */
  729. if( (rxRTOX < NFCIP_INIT_MIN_RTOX) || (rxRTOX > NFCIP_INIT_MAX_RTOX) )
  730. {
  731. return ERR_PROTO;
  732. }
  733. EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBSPDU_TO(), rxRTOX ) );
  734. gNfcip.lastRTOX = rxRTOX;
  735. return ERR_BUSY;
  736. }
  737. else
  738. {
  739. /* Unexpected S-PDU */
  740. return ERR_PROTO; /* PRQA S 2880 # MISRA 2.1 - Guard code to prevent unexpected behavior */
  741. }
  742. }
  743. /*******************************************************************************/
  744. /* Process I-PDU */
  745. /*******************************************************************************/
  746. if( nfcip_PFBisIPDU( rxPFB ) )
  747. {
  748. if( gNfcip.pni != nfcip_PBF_PNI( rxPFB ) )
  749. {
  750. nfcipLogI( " NFCIP(I) Rcvd IPDU wrong PNI curPNI: %d rxPNI: %d \r\n", gNfcip.pni , nfcip_PBF_PNI( rxPFB ) );
  751. return ERR_PROTO;
  752. }
  753. nfcipLogD( " NFCIP(I) Rcvd IPDU OK PNI: %d \r\n", gNfcip.pni );
  754. /* 14.12.3.3 I-PDU with correct PNI -> Increment */
  755. gNfcip.pni = nfcip_PNIInc( gNfcip.pni );
  756. /* Successful data Exchange */
  757. nfcipClearCounters();
  758. *outActRxLen = ((uint16_t)nfcDepLen - RFAL_NFCDEP_DEP_HEADER - (uint16_t)optHdrLen);
  759. if( (&gNfcip.rxBuf[gNfcip.rxBufPaylPos] != &gNfcip.rxBuf[RFAL_NFCDEP_DEP_HEADER + optHdrLen]) && (*outActRxLen > 0U) )
  760. {
  761. ST_MEMMOVE( &gNfcip.rxBuf[gNfcip.rxBufPaylPos], &gNfcip.rxBuf[RFAL_NFCDEP_DEP_HEADER + optHdrLen], *outActRxLen );
  762. }
  763. /*******************************************************************************/
  764. /* Check if target is indicating chaining MI */
  765. /*******************************************************************************/
  766. if( nfcip_PFBisIMI( rxPFB ) )
  767. {
  768. gNfcip.isRxChaining = true;
  769. *outIsChaining = true;
  770. nfcipLogD( " NFCIP(I) Rcvd IPDU OK w MI -> ACK \r\n" );
  771. EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBRPDU_ACK( gNfcip.pni ), gNfcip.rxBuf[rxMsgIt++] ) );
  772. return ERR_AGAIN; /* Send Again signalling to run again, but some chaining data has arrived*/
  773. }
  774. else
  775. {
  776. gNfcip.isRxChaining = false;
  777. gNfcip.state = NFCIP_ST_INIT_DEP_IDLE;
  778. ret = ERR_NONE; /* Data exchange done */
  779. }
  780. }
  781. return ret;
  782. }
  783. /*******************************************************************************/
  784. static ReturnCode nfcipTargetHandleRX( ReturnCode rxRes, uint16_t *outActRxLen, bool *outIsChaining )
  785. {
  786. ReturnCode ret;
  787. uint8_t nfcDepLen;
  788. uint8_t rxMsgIt;
  789. uint8_t rxPFB;
  790. uint8_t optHdrLen;
  791. uint8_t resBuf[RFAL_NFCDEP_HEADER_PAD + NFCIP_TARGET_RES_MAX];
  792. ret = ERR_INTERNAL;
  793. rxMsgIt = 0;
  794. optHdrLen = 0;
  795. *outActRxLen = 0;
  796. *outIsChaining = false;
  797. /*******************************************************************************/
  798. /* Handle reception errors */
  799. /*******************************************************************************/
  800. switch( rxRes )
  801. {
  802. /*******************************************************************************/
  803. case ERR_NONE:
  804. break;
  805. case ERR_LINK_LOSS:
  806. nfcipLogW( " NFCIP(T) Error: %d \r\n", rxRes );
  807. return rxRes;
  808. case ERR_BUSY:
  809. return ERR_BUSY; /* Debug purposes */
  810. case ERR_TIMEOUT:
  811. case ERR_CRC:
  812. case ERR_PAR:
  813. case ERR_FRAMING:
  814. case ERR_PROTO:
  815. default:
  816. /* Digital 1.1 16.12.5.2 The Target MUST NOT attempt any error recovery. *
  817. * The Target MUST always stay in receive mode when a *
  818. * Transmission Error or a Protocol Error occurs. *
  819. * *
  820. * Do not push Transmission/Protocol Errors to upper layer in Listen Mode #766 */
  821. nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
  822. return ERR_BUSY;
  823. }
  824. /*******************************************************************************/
  825. /* Rx OK check if valid DEP PDU */
  826. /*******************************************************************************/
  827. /* Due to different modes on ST25R391x (with/without CRC) use NFC-DEP LEN instead of bytes retrieved */
  828. nfcDepLen = gNfcip.rxBuf[rxMsgIt++];
  829. nfcipLogD( " NFCIP(T) rx OK: %d bytes \r\n", nfcDepLen );
  830. if( gNfcip.rxBuf[rxMsgIt++] != NFCIP_REQ )
  831. {
  832. nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
  833. return ERR_BUSY; /* ERR_PROTO - Ignore bad request */
  834. }
  835. /*******************************************************************************/
  836. /* Check whether target rcvd a normal DEP or deactivation request */
  837. /*******************************************************************************/
  838. switch( gNfcip.rxBuf[rxMsgIt++] )
  839. {
  840. /*******************************************************************************/
  841. case (uint8_t)NFCIP_CMD_DEP_REQ:
  842. break; /* Continue to normal DEP processing */
  843. /*******************************************************************************/
  844. case (uint8_t)NFCIP_CMD_DSL_REQ:
  845. nfcipLogI( " NFCIP(T) rx DSL \r\n" );
  846. /* Digital 1.0 14.9.1.2 If DID is used and incorrect ignore it */
  847. /* [Digital 1.0, 16.9.1.2]: If DID == 0, Target SHALL ignore DSL_REQ with DID */
  848. if ( (((gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.did) || (nfcDepLen != RFAL_NFCDEP_DSL_RLS_LEN_DID)) && (gNfcip.cfg.did != RFAL_NFCDEP_DID_NO) )
  849. || ((gNfcip.cfg.did == RFAL_NFCDEP_DID_NO) && (nfcDepLen != RFAL_NFCDEP_DSL_RLS_LEN_NO_DID))
  850. )
  851. {
  852. nfcipLogI( " NFCIP(T) DSL wrong DID, ignoring \r\n" );
  853. return ERR_BUSY;
  854. }
  855. nfcipTx( NFCIP_CMD_DSL_RES, resBuf, NULL, 0, 0, NFCIP_NO_FWT );
  856. gNfcip.state = NFCIP_ST_TARG_DEP_SLEEP;
  857. return ERR_SLEEP_REQ;
  858. /*******************************************************************************/
  859. case (uint8_t)NFCIP_CMD_RLS_REQ:
  860. nfcipLogI( " NFCIP(T) rx RLS \r\n" );
  861. /* Digital 1.0 14.10.1.2 If DID is used and incorrect ignore it */
  862. /* [Digital 1.0, 16.10.2.2]: If DID == 0, Target SHALL ignore DSL_REQ with DID */
  863. if ( (((gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.did) || (nfcDepLen != RFAL_NFCDEP_DSL_RLS_LEN_DID)) && (gNfcip.cfg.did != RFAL_NFCDEP_DID_NO) )
  864. || ((gNfcip.cfg.did == RFAL_NFCDEP_DID_NO) && (nfcDepLen > RFAL_NFCDEP_DSL_RLS_LEN_NO_DID))
  865. )
  866. {
  867. nfcipLogI( " NFCIP(T) RLS wrong DID, ignoring \r\n" );
  868. return ERR_BUSY;
  869. }
  870. nfcipTx( NFCIP_CMD_RLS_RES, resBuf, NULL, 0, 0, NFCIP_NO_FWT );
  871. gNfcip.state = NFCIP_ST_TARG_DEP_IDLE;
  872. return ERR_RELEASE_REQ;
  873. /*******************************************************************************/
  874. /*case NFCIP_CMD_PSL_REQ: PSL must be handled in Activation only */
  875. /*case NFCIP_CMD_WUP_REQ: WUP not in NFC Forum Digital 1.0 */
  876. default:
  877. /* Don't go to NFCIP_ST_TARG_DEP_IDLE state as it needs to ignore this *
  878. * invalid frame, and keep waiting for more frames */
  879. nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
  880. return ERR_BUSY; /* ERR_PROTO - Ignore bad frame */
  881. }
  882. /*******************************************************************************/
  883. rxPFB = gNfcip.rxBuf[rxMsgIt++]; /* Store rcvd PFB */
  884. /*******************************************************************************/
  885. /* Check for valid PFB type */
  886. if( !(nfcip_PFBisSPDU( rxPFB ) || nfcip_PFBisRPDU( rxPFB ) || nfcip_PFBisIPDU( rxPFB )) )
  887. {
  888. nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
  889. return ERR_BUSY; /* ERR_PROTO - Ignore invalid PFB */
  890. }
  891. /*******************************************************************************/
  892. if( gNfcip.cfg.did != RFAL_NFCDEP_DID_NO )
  893. {
  894. if( !nfcip_PFBhasDID( rxPFB ) )
  895. {
  896. nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
  897. return ERR_BUSY; /* ERR_PROTO - Ignore bad/missing DID */
  898. }
  899. if( gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.did ) /* MISRA 13.5 */
  900. {
  901. nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
  902. return ERR_BUSY; /* ERR_PROTO - Ignore bad/missing DID */
  903. }
  904. optHdrLen++; /* Inc header optional field cnt*/
  905. }
  906. else if( nfcip_PFBhasDID( rxPFB ) ) /* DID not expected but rcv */
  907. {
  908. nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
  909. return ERR_BUSY; /* ERR_PROTO - Ignore unexpected DID */
  910. }
  911. else
  912. {
  913. /* MISRA 15.7 - Empty else */
  914. }
  915. /*******************************************************************************/
  916. if( gNfcip.cfg.nad != RFAL_NFCDEP_NAD_NO )
  917. {
  918. if( (gNfcip.rxBuf[rxMsgIt++] != gNfcip.cfg.did) || !nfcip_PFBhasDID( rxPFB ) )
  919. {
  920. nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
  921. return ERR_BUSY; /* ERR_PROTO - Ignore bad/missing DID */
  922. }
  923. optHdrLen++; /* Inc header optional field cnt*/
  924. }
  925. else if( nfcip_PFBhasNAD( rxPFB ) ) /* NAD not expected but rcv */
  926. {
  927. nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
  928. return ERR_BUSY; /* ERR_PROTO - Ignore unexpected NAD */
  929. }
  930. else
  931. {
  932. /* MISRA 15.7 - Empty else */
  933. }
  934. /*******************************************************************************/
  935. /* Process R-PDU */
  936. /*******************************************************************************/
  937. if( nfcip_PFBisRPDU( rxPFB ) )
  938. {
  939. nfcipLogD( " NFCIP(T) Rcvd R-PDU \r\n" );
  940. /*******************************************************************************/
  941. /* R ACK */
  942. /*******************************************************************************/
  943. if( nfcip_PFBisRACK( rxPFB ) )
  944. {
  945. nfcipLogI( " NFCIP(T) Rcvd ACK \r\n" );
  946. if( gNfcip.pni == nfcip_PBF_PNI( rxPFB ) )
  947. {
  948. /* R-ACK while not performing chaining -> Protocol error */
  949. if( !gNfcip.isTxChaining )
  950. {
  951. nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
  952. return ERR_BUSY; /* ERR_PROTO - Ignore unexpected ACK */
  953. }
  954. /* This block has been transmitted and acknowledged, perform RTOX until next data is provided */
  955. /* Digital 1.1 16.12.4.7 - If ACK rcvd continue with chaining or an RTOX */
  956. nfcipTimerStart( gNfcip.RTOXTimer, nfcipRTOXAdjust( nfcipConv1FcToMs( rfalNfcDepWT2RWT( gNfcip.cfg.to ) )) );
  957. gNfcip.state = NFCIP_ST_TARG_DEP_RTOX;
  958. return ERR_NONE; /* This block has been transmitted */
  959. }
  960. /* Digital 1.0 14.12.3.4 - If last send was ATN and rx PNI is minus 1 */
  961. else if( nfcip_PFBisSATN( gNfcip.lastPFB ) && (nfcip_PNIDec(gNfcip.pni) == nfcip_PBF_PNI( rxPFB )) )
  962. {
  963. nfcipLogI( " NFCIP(T) wrong PNI, last was ATN reTx \r\n" );
  964. /* Spec says to leave current PNI as is, but will be Inc after Tx, remaining the same */
  965. gNfcip.pni = nfcip_PNIDec( gNfcip.pni );
  966. gNfcip.state = NFCIP_ST_TARG_DEP_TX;
  967. return ERR_BUSY;
  968. }
  969. else
  970. {
  971. /* MISRA 15.7 - Empty else */
  972. }
  973. }
  974. /*******************************************************************************/
  975. /* R NACK */
  976. /*******************************************************************************/
  977. /* ISO 18092 12.6.1.3.3 When rcv NACK if PNI = prev PNI sent -> reTx */
  978. else if( nfcip_PFBisRNACK( rxPFB ) && (nfcip_PNIDec(gNfcip.pni) == nfcip_PBF_PNI( rxPFB ) ) )
  979. {
  980. nfcipLogI( " NFCIP(T) Rcvd NACK \r\n" );
  981. gNfcip.pni = nfcip_PNIDec( gNfcip.pni ); /* Dec so that has the prev PNI */
  982. gNfcip.state = NFCIP_ST_TARG_DEP_TX;
  983. return ERR_BUSY;
  984. }
  985. else
  986. {
  987. nfcipLogI( " NFCIP(T) Unexpected R-PDU \r\n" );
  988. nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
  989. return ERR_BUSY; /* ERR_PROTO - Ignore unexpected R-PDU */
  990. }
  991. }
  992. /*******************************************************************************/
  993. /* Process S-PDU */
  994. /*******************************************************************************/
  995. if( nfcip_PFBisSPDU( rxPFB ) )
  996. {
  997. nfcipLogD( " NFCIP(T) Rcvd S-PDU \r\n" );
  998. /*******************************************************************************/
  999. /* S ATN */
  1000. /*******************************************************************************/
  1001. /* ISO 18092 12.6.3 Attention */
  1002. if( nfcip_PFBisSATN( rxPFB ) ) /* If is a S-ATN */
  1003. {
  1004. nfcipLogI( " NFCIP(T) Rcvd ATN curPNI: %d \r\n", gNfcip.pni );
  1005. EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBSPDU_ATN(), 0 ) );
  1006. return ERR_BUSY;
  1007. }
  1008. /*******************************************************************************/
  1009. /* S TO */
  1010. /*******************************************************************************/
  1011. else if( nfcip_PFBisSTO( rxPFB ) ) /* If is a S-TO (RTOX) */
  1012. {
  1013. if( nfcip_PFBisSTO( gNfcip.lastPFBnATN ) )
  1014. {
  1015. nfcipLogI( " NFCIP(T) Rcvd TO \r\n" );
  1016. /* Digital 1.1 16.8.4.6 RTOX value in RES different that in REQ -> Protocol Error */
  1017. if( gNfcip.lastRTOX != gNfcip.rxBuf[rxMsgIt++] )
  1018. {
  1019. nfcipLogI( " NFCIP(T) Mismatched RTOX value \r\n" );
  1020. nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
  1021. return ERR_BUSY; /* ERR_PROTO - Ignore unexpected RTOX value */
  1022. }
  1023. /* Clear waiting for RTOX Ack Flag */
  1024. gNfcip.isWait4RTOX = false;
  1025. /* Check if a Tx is already pending */
  1026. if( gNfcip.isTxPending )
  1027. {
  1028. nfcipLogW( " NFCIP(T) Tx pending, go immediately to TX \r\n" );
  1029. gNfcip.state = NFCIP_ST_TARG_DEP_TX;
  1030. return ERR_BUSY;
  1031. }
  1032. /* Start RTOX timer and change to check state */
  1033. nfcipTimerStart( gNfcip.RTOXTimer, nfcipRTOXAdjust( nfcipConv1FcToMs( gNfcip.lastRTOX * rfalNfcDepWT2RWT(gNfcip.cfg.to ) ) ) );
  1034. gNfcip.state = NFCIP_ST_TARG_DEP_RTOX;
  1035. return ERR_BUSY;
  1036. }
  1037. }
  1038. else
  1039. {
  1040. /* Unexpected S-PDU */
  1041. nfcipLogI( " NFCIP(T) Unexpected S-PDU \r\n" ); /* PRQA S 2880 # MISRA 2.1 - Guard code to prevent unexpected behavior */
  1042. nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
  1043. return ERR_BUSY; /* ERR_PROTO - Ignore unexpected S-PDU */
  1044. }
  1045. }
  1046. /*******************************************************************************/
  1047. /* Process I-PDU */
  1048. /*******************************************************************************/
  1049. if( nfcip_PFBisIPDU( rxPFB ) )
  1050. {
  1051. if( gNfcip.pni != nfcip_PBF_PNI( rxPFB ) )
  1052. {
  1053. nfcipLogI( " NFCIP(T) Rcvd IPDU wrong PNI curPNI: %d rxPNI: %d \r\n", gNfcip.pni, nfcip_PBF_PNI( rxPFB ) );
  1054. /* Digital 1.1 16.12.3.4 - If last send was ATN and rx PNI is minus 1 */
  1055. if( nfcip_PFBisSATN(gNfcip.lastPFB ) && (nfcip_PNIDec(gNfcip.pni) == nfcip_PBF_PNI( rxPFB )) )
  1056. {
  1057. /* Spec says to leave current PNI as is, but will be Inc after Data Tx, remaining the same */
  1058. gNfcip.pni = nfcip_PNIDec(gNfcip.pni);
  1059. if( nfcip_PFBisIMI( rxPFB ) )
  1060. {
  1061. nfcipLogI( " NFCIP(T) PNI = prevPNI && ATN before && chaining -> send ACK \r\n" );
  1062. EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBRPDU_ACK( gNfcip.pni ), gNfcip.rxBuf[rxMsgIt++] ) );
  1063. /* Digital 1.1 16.12.3.4 (...) leave the current PNI unchanged afterwards */
  1064. gNfcip.pni = nfcip_PNIInc( gNfcip.pni );
  1065. }
  1066. else
  1067. {
  1068. nfcipLogI( " NFCIP(T) PNI = prevPNI && ATN before -> reTx last I-PDU \r\n" );
  1069. gNfcip.state = NFCIP_ST_TARG_DEP_TX;
  1070. }
  1071. return ERR_BUSY;
  1072. }
  1073. nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
  1074. return ERR_BUSY; /* ERR_PROTO - Ignore bad PNI value */
  1075. }
  1076. nfcipLogD( " NFCIP(T) Rcvd IPDU OK PNI: %d \r\n", gNfcip.pni );
  1077. /*******************************************************************************/
  1078. /* Successful data exchange */
  1079. /*******************************************************************************/
  1080. *outActRxLen = ((uint16_t)nfcDepLen - RFAL_NFCDEP_DEP_HEADER - (uint16_t)optHdrLen);
  1081. nfcipClearCounters();
  1082. if( (&gNfcip.rxBuf[gNfcip.rxBufPaylPos] != &gNfcip.rxBuf[RFAL_NFCDEP_DEP_HEADER + optHdrLen]) && (*outActRxLen > 0U) )
  1083. {
  1084. ST_MEMMOVE( &gNfcip.rxBuf[gNfcip.rxBufPaylPos], &gNfcip.rxBuf[RFAL_NFCDEP_DEP_HEADER + optHdrLen], *outActRxLen );
  1085. }
  1086. /*******************************************************************************/
  1087. /* Check if Initiator is indicating chaining MI */
  1088. /*******************************************************************************/
  1089. if( nfcip_PFBisIMI( rxPFB ) )
  1090. {
  1091. gNfcip.isRxChaining = true;
  1092. *outIsChaining = true;
  1093. nfcipLogD( " NFCIP(T) Rcvd IPDU OK w MI -> ACK \r\n" );
  1094. EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBRPDU_ACK( gNfcip.pni ), gNfcip.rxBuf[rxMsgIt++] ) );
  1095. gNfcip.pni = nfcip_PNIInc( gNfcip.pni );
  1096. return ERR_AGAIN; /* Send Again signalling to run again, but some chaining data has arrived*/
  1097. }
  1098. else
  1099. {
  1100. if(gNfcip.isRxChaining)
  1101. {
  1102. nfcipLogI( " NFCIP(T) Rcvd last IPDU chaining finished \r\n" );
  1103. }
  1104. /*******************************************************************************/
  1105. /* Reception done, send to DH and start RTOX timer */
  1106. /*******************************************************************************/
  1107. nfcipTimerStart( gNfcip.RTOXTimer, nfcipRTOXAdjust( nfcipConv1FcToMs( rfalNfcDepWT2RWT( gNfcip.cfg.to ) )) );
  1108. gNfcip.state = NFCIP_ST_TARG_DEP_RTOX;
  1109. gNfcip.isRxChaining = false;
  1110. ret = ERR_NONE; /* Data exchange done */
  1111. }
  1112. }
  1113. return ret;
  1114. }
  1115. /*******************************************************************************/
  1116. static ReturnCode nfcipTx( rfalNfcDepCmd cmd, uint8_t* txBuf, uint8_t *paylBuf, uint16_t paylLen, uint8_t pfbData, uint32_t fwt )
  1117. {
  1118. uint16_t txBufIt;
  1119. uint8_t *txBlock;
  1120. uint8_t *payloadBuf;
  1121. uint8_t pfb;
  1122. if( txBuf == NULL )
  1123. {
  1124. return ERR_PARAM;
  1125. }
  1126. payloadBuf = paylBuf; /* MISRA 17.8: Use intermediate variable */
  1127. if( (paylLen == 0U) || (payloadBuf == NULL) )
  1128. {
  1129. payloadBuf = (uint8_t*) &txBuf[RFAL_NFCDEP_DEPREQ_HEADER_LEN]; /* If not a DEP (no Data) ensure enough space for header */
  1130. }
  1131. txBufIt = 0;
  1132. pfb = pfbData; /* MISRA 17.8: Use intermediate variable */
  1133. txBlock = payloadBuf; /* Point to beginning of the Data, and go backwards */
  1134. gNfcip.lastCmd = (uint8_t)cmd; /* Store last cmd sent */
  1135. gNfcip.lastPFB = NFCIP_PFB_INVALID; /* Reset last pfb sent */
  1136. /*******************************************************************************/
  1137. /* Compute outgoing NFCIP message */
  1138. /*******************************************************************************/
  1139. switch( cmd )
  1140. {
  1141. /*******************************************************************************/
  1142. case NFCIP_CMD_ATR_RES:
  1143. case NFCIP_CMD_ATR_REQ:
  1144. rfalNfcDepSetNFCID( payloadBuf, gNfcip.cfg.nfcid, gNfcip.cfg.nfcidLen ); /* NFCID */
  1145. txBufIt += RFAL_NFCDEP_NFCID3_LEN;
  1146. payloadBuf[txBufIt++] = gNfcip.cfg.did; /* DID */
  1147. payloadBuf[txBufIt++] = gNfcip.cfg.bs; /* BS */
  1148. payloadBuf[txBufIt++] = gNfcip.cfg.br; /* BR */
  1149. if( cmd == NFCIP_CMD_ATR_RES )
  1150. {
  1151. payloadBuf[txBufIt++] = gNfcip.cfg.to; /* ATR_RES[ TO ] */
  1152. }
  1153. if( gNfcip.cfg.gbLen > 0U)
  1154. {
  1155. payloadBuf[txBufIt++] = nfcip_PPwGB( gNfcip.cfg.lr ); /* PP signalling GB */
  1156. ST_MEMCPY( &payloadBuf[txBufIt], gNfcip.cfg.gb, gNfcip.cfg.gbLen ); /* set General Bytes */
  1157. txBufIt += gNfcip.cfg.gbLen;
  1158. }
  1159. else
  1160. {
  1161. payloadBuf[txBufIt++] = rfalNfcDepLR2PP( gNfcip.cfg.lr ); /* PP without GB */
  1162. }
  1163. if( (txBufIt + RFAL_NFCDEP_CMDTYPE_LEN + RFAL_NFCDEP_CMD_LEN) > RFAL_NFCDEP_ATRREQ_MAX_LEN ) /* Check max ATR length (ATR_REQ = ATR_RES)*/
  1164. {
  1165. return ERR_PARAM;
  1166. }
  1167. break;
  1168. /*******************************************************************************/
  1169. case NFCIP_CMD_WUP_REQ: /* ISO 18092 - 12.5.2.1 */
  1170. rfalNfcDepSetNFCID( (payloadBuf), gNfcip.cfg.nfcid, gNfcip.cfg.nfcidLen ); /* NFCID */
  1171. txBufIt += RFAL_NFCDEP_NFCID3_LEN;
  1172. *(--txBlock) = gNfcip.cfg.did; /* DID */
  1173. break;
  1174. /*******************************************************************************/
  1175. case NFCIP_CMD_WUP_RES: /* ISO 18092 - 12.5.2.2 */
  1176. case NFCIP_CMD_PSL_REQ:
  1177. case NFCIP_CMD_PSL_RES:
  1178. *(--txBlock) = gNfcip.cfg.did; /* DID */
  1179. break;
  1180. /*******************************************************************************/
  1181. case NFCIP_CMD_RLS_REQ:
  1182. case NFCIP_CMD_RLS_RES:
  1183. case NFCIP_CMD_DSL_REQ:
  1184. case NFCIP_CMD_DSL_RES:
  1185. /* Digital 1.0 - 14.8.1.1 & 14.9.1.1 & 14.10.1.1 Only add DID if not 0 */
  1186. if( gNfcip.cfg.did != RFAL_NFCDEP_DID_NO )
  1187. {
  1188. *(--txBlock) = gNfcip.cfg.did; /* DID */
  1189. }
  1190. break;
  1191. /*******************************************************************************/
  1192. case NFCIP_CMD_DEP_REQ:
  1193. case NFCIP_CMD_DEP_RES:
  1194. /* Compute optional PFB bits */
  1195. if (gNfcip.cfg.did != RFAL_NFCDEP_DID_NO) { pfb |= NFCIP_PFB_DID_BIT; }
  1196. if (gNfcip.cfg.nad != RFAL_NFCDEP_NAD_NO) { pfb |= NFCIP_PFB_NAD_BIT; }
  1197. if ((gNfcip.isTxChaining) && (nfcip_PFBisIPDU(pfb)) ) { pfb |= NFCIP_PFB_MI_BIT; }
  1198. /* Store PFB for future handling */
  1199. gNfcip.lastPFB = pfb; /* store PFB sent */
  1200. if( !nfcip_PFBisSATN(pfb) )
  1201. {
  1202. gNfcip.lastPFBnATN = pfb; /* store last PFB different then ATN */
  1203. }
  1204. /* Add NAD if it is to be supported */
  1205. if( gNfcip.cfg.nad != RFAL_NFCDEP_NAD_NO )
  1206. {
  1207. *(--txBlock) = gNfcip.cfg.nad; /* NAD */
  1208. }
  1209. /* Digital 1.0 - 14.8.1.1 & 14.8.1.1 Only add DID if not 0 */
  1210. if( gNfcip.cfg.did != RFAL_NFCDEP_DID_NO )
  1211. {
  1212. *(--txBlock) = gNfcip.cfg.did; /* DID */
  1213. }
  1214. *(--txBlock) = pfb; /* PFB */
  1215. /* NCI 1.0 - Check if Empty frames are allowed */
  1216. if( (paylLen == 0U) && nfcipIsEmptyDEPDisabled(gNfcip.cfg.oper) && nfcip_PFBisIPDU(pfb) )
  1217. {
  1218. return ERR_PARAM;
  1219. }
  1220. break;
  1221. /*******************************************************************************/
  1222. default:
  1223. return ERR_PARAM;
  1224. }
  1225. /*******************************************************************************/
  1226. /* Prepend Header */
  1227. /*******************************************************************************/
  1228. *(--txBlock) = (uint8_t)cmd; /* CMD */
  1229. *(--txBlock) = (uint8_t)( nfcipCmdIsReq(cmd) ? NFCIP_REQ : NFCIP_RES ); /* CMDType */
  1230. txBufIt += paylLen + (uint16_t)((uint32_t)payloadBuf - (uint32_t)txBlock); /* Calculate overall buffer size */
  1231. if( txBufIt > gNfcip.fsc ) /* Check if msg length violates the maximum payload size FSC */
  1232. {
  1233. return ERR_NOTSUPP;
  1234. }
  1235. /*******************************************************************************/
  1236. return nfcipDataTx( txBlock, txBufIt, fwt );
  1237. }
  1238. /*
  1239. ******************************************************************************
  1240. * GLOBAL FUNCTIONS
  1241. ******************************************************************************
  1242. */
  1243. /*******************************************************************************/
  1244. static void nfcipConfig( const rfalNfcDepConfigs * cfg )
  1245. {
  1246. if (cfg == NULL)
  1247. {
  1248. return;
  1249. }
  1250. ST_MEMCPY(&gNfcip.cfg, cfg, sizeof(rfalNfcDepConfigs)); /* Copy given config to local */
  1251. gNfcip.cfg.to = MIN( RFAL_NFCDEP_WT_TRG_MAX, gNfcip.cfg.to); /* Ensure proper WT value */
  1252. gNfcip.cfg.did = nfcip_DIDMax( gNfcip.cfg.did ); /* Ensure proper DID value */
  1253. gNfcip.fsc = rfalNfcDepLR2FS( gNfcip.cfg.lr ); /* Calculate FSC based on given LR */
  1254. gNfcip.state = ( ( gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET) ? NFCIP_ST_TARG_WAIT_ATR : NFCIP_ST_INIT_IDLE );
  1255. }
  1256. /*******************************************************************************/
  1257. static ReturnCode nfcipRun( uint16_t *outActRxLen, bool *outIsChaining )
  1258. {
  1259. ReturnCode ret;
  1260. ret = ERR_SYNTAX;
  1261. nfcipLogD( " NFCIP Run() state: %d \r\n", gNfcip.state );
  1262. switch( gNfcip.state )
  1263. {
  1264. /*******************************************************************************/
  1265. case NFCIP_ST_IDLE:
  1266. case NFCIP_ST_INIT_DEP_IDLE:
  1267. case NFCIP_ST_TARG_DEP_IDLE:
  1268. case NFCIP_ST_TARG_DEP_SLEEP:
  1269. return ERR_NONE;
  1270. /*******************************************************************************/
  1271. case NFCIP_ST_INIT_DEP_TX:
  1272. nfcipLogD( " NFCIP(I) Tx PNI: %d txLen: %d \r\n", gNfcip.pni, gNfcip.txBufLen );
  1273. ret = nfcipTx( NFCIP_CMD_DEP_REQ, gNfcip.txBuf, &gNfcip.txBuf[gNfcip.txBufPaylPos], gNfcip.txBufLen, nfcip_PFBIPDU( gNfcip.pni ), (gNfcip.cfg.fwt + gNfcip.cfg.dFwt) );
  1274. switch( ret )
  1275. {
  1276. case ERR_NONE:
  1277. gNfcip.state = NFCIP_ST_INIT_DEP_RX;
  1278. break;
  1279. case ERR_PARAM:
  1280. default:
  1281. gNfcip.state = NFCIP_ST_INIT_DEP_IDLE;
  1282. return ret;
  1283. }
  1284. /* fall through */
  1285. /*******************************************************************************/
  1286. case NFCIP_ST_INIT_DEP_RX: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  1287. ret = nfcipDataRx( false );
  1288. if( ret != ERR_BUSY )
  1289. {
  1290. ret = nfcipInitiatorHandleDEP( ret, *gNfcip.rxRcvdLen, outActRxLen, outIsChaining );
  1291. }
  1292. break;
  1293. /*******************************************************************************/
  1294. case NFCIP_ST_TARG_DEP_RTOX:
  1295. if( !nfcipTimerisExpired( gNfcip.RTOXTimer ) ) /* Do nothing until RTOX timer has expired */
  1296. {
  1297. return ERR_BUSY;
  1298. }
  1299. /* If we cannot send a RTOX raise a Timeout error so that we do not
  1300. * hold the field On forever in AP2P */
  1301. if( nfcipIsRTOXReqDisabled(gNfcip.cfg.oper) )
  1302. {
  1303. /* We should reEnable Rx, and measure time between our field Off to
  1304. * either report link loss or recover #287 */
  1305. nfcipLogI( " NFCIP(T) RTOX not sent due to config, NOT reenabling Rx \r\n" );
  1306. return ERR_TIMEOUT;
  1307. }
  1308. if( gNfcip.cntRTOXRetrys++ > RFAL_NFCDEP_MAX_RTOX_RETRYS ) /* Check maximum consecutive RTOX requests */
  1309. {
  1310. return ERR_PROTO;
  1311. }
  1312. nfcipLogI( " NFCIP(T) RTOX sent \r\n" );
  1313. gNfcip.lastRTOX = nfcip_RTOXTargMax(gNfcip.cfg.to); /* Calculate requested RTOX value, and send it */
  1314. EXIT_ON_ERR( ret, nfcipDEPControlMsg( nfcip_PFBSPDU_TO(), gNfcip.lastRTOX ) );
  1315. /* Set waiting for RTOX Ack Flag */
  1316. gNfcip.isWait4RTOX = true;
  1317. gNfcip.state = NFCIP_ST_TARG_DEP_RX; /* Go back to Rx to process RTOX ack */
  1318. return ERR_BUSY;
  1319. /*******************************************************************************/
  1320. case NFCIP_ST_TARG_DEP_TX:
  1321. nfcipLogD( " NFCIP(T) Tx PNI: %d txLen: %d \r\n", gNfcip.pni, gNfcip.txBufLen );
  1322. ret = nfcipTx( NFCIP_CMD_DEP_RES, gNfcip.txBuf, &gNfcip.txBuf[gNfcip.txBufPaylPos], gNfcip.txBufLen, nfcip_PFBIPDU( gNfcip.pni ), NFCIP_NO_FWT );
  1323. /* Clear flags */
  1324. gNfcip.isTxPending = false;
  1325. gNfcip.isWait4RTOX = false;
  1326. /* Digital 1.0 14.12.3.4 Increment the current PNI after Tx */
  1327. gNfcip.pni = nfcip_PNIInc( gNfcip.pni );
  1328. switch( ret )
  1329. {
  1330. case ERR_NONE:
  1331. gNfcip.state = NFCIP_ST_TARG_DEP_RX; /* All OK, goto Rx state */
  1332. break;
  1333. case ERR_PARAM:
  1334. default:
  1335. gNfcip.state = NFCIP_ST_TARG_DEP_IDLE; /* Upon Tx error, goto IDLE state */
  1336. return ret;
  1337. }
  1338. /* fall through */
  1339. /*******************************************************************************/
  1340. case NFCIP_ST_TARG_DEP_RX: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  1341. if( gNfcip.isReqPending ) /* if already has Data should be from a DEP from nfcipTargetHandleActivation() */
  1342. {
  1343. nfcipLogD( " NFCIP(T) Skipping Rx Using DEP from Activation \r\n" );
  1344. gNfcip.isReqPending = false;
  1345. ret = ERR_NONE;
  1346. }
  1347. else
  1348. {
  1349. ret = nfcipDataRx( false );
  1350. }
  1351. if( ret != ERR_BUSY )
  1352. {
  1353. ret = nfcipTargetHandleRX( ret, outActRxLen, outIsChaining );
  1354. }
  1355. break;
  1356. /*******************************************************************************/
  1357. default:
  1358. /* MISRA 16.4: no empty default statement (a comment being enough) */
  1359. break;
  1360. }
  1361. return ret;
  1362. }
  1363. /*******************************************************************************/
  1364. void rfalNfcDepSetDeactivatingCallback( rfalNfcDepDeactCallback pFunc )
  1365. {
  1366. gNfcip.isDeactivating = pFunc;
  1367. }
  1368. /*******************************************************************************/
  1369. void rfalNfcDepInitialize( void )
  1370. {
  1371. nfcipLogD( " NFCIP Ini() \r\n" );
  1372. gNfcip.state = NFCIP_ST_IDLE;
  1373. gNfcip.isDeactivating = NULL;
  1374. gNfcip.isTxPending = false;
  1375. gNfcip.isWait4RTOX = false;
  1376. gNfcip.isReqPending = false;
  1377. gNfcip.cfg.oper = (RFAL_NFCDEP_OPER_FULL_MI_DIS | RFAL_NFCDEP_OPER_EMPTY_DEP_EN | RFAL_NFCDEP_OPER_ATN_EN | RFAL_NFCDEP_OPER_RTOX_REQ_EN);
  1378. gNfcip.cfg.did = RFAL_NFCDEP_DID_NO;
  1379. gNfcip.cfg.nad = RFAL_NFCDEP_NAD_NO;
  1380. gNfcip.cfg.br = RFAL_NFCDEP_Bx_NO_HIGH_BR;
  1381. gNfcip.cfg.bs = RFAL_NFCDEP_Bx_NO_HIGH_BR;
  1382. gNfcip.cfg.lr = RFAL_NFCDEP_LR_254;
  1383. gNfcip.fsc = rfalNfcDepLR2FS( gNfcip.cfg.lr );
  1384. gNfcip.cfg.gbLen = 0;
  1385. gNfcip.cfg.fwt = RFAL_NFCDEP_MAX_FWT;
  1386. gNfcip.cfg.dFwt = RFAL_NFCDEP_MAX_FWT;
  1387. gNfcip.pni = 0;
  1388. /* Destroy any ongoing RTOX timer*/
  1389. nfcipTimerDestroy( gNfcip.RTOXTimer );
  1390. gNfcip.RTOXTimer = 0U;
  1391. gNfcip.PDUTxPos = 0;
  1392. gNfcip.PDURxPos = 0;
  1393. gNfcip.PDUParam.rxLen = NULL;
  1394. gNfcip.PDUParam.rxBuf = NULL;
  1395. gNfcip.PDUParam.txBuf = NULL;
  1396. nfcipClearCounters();
  1397. }
  1398. /*******************************************************************************/
  1399. static void nfcipSetDEPParams( const rfalNfcDepDEPParams *DEPParams )
  1400. {
  1401. nfcipLogD( " NFCIP SetDEP() txLen: %d \r\n", DEPParams->txBufLen );
  1402. gNfcip.isTxChaining = DEPParams->txChaining;
  1403. gNfcip.txBuf = DEPParams->txBuf;
  1404. gNfcip.rxBuf = DEPParams->rxBuf;
  1405. gNfcip.txBufLen = DEPParams->txBufLen;
  1406. gNfcip.rxBufLen = DEPParams->rxBufLen;
  1407. gNfcip.txBufPaylPos = DEPParams->txBufPaylPos;
  1408. gNfcip.rxBufPaylPos = DEPParams->rxBufPaylPos;
  1409. if( DEPParams->did != RFAL_NFCDEP_DID_KEEP )
  1410. {
  1411. gNfcip.cfg.did = nfcip_DIDMax( DEPParams->did );
  1412. }
  1413. gNfcip.cfg.fwt = DEPParams->fwt;
  1414. gNfcip.cfg.dFwt = DEPParams->dFwt;
  1415. gNfcip.fsc = DEPParams->fsc;
  1416. if(gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET)
  1417. {
  1418. /* If there's any data to be sent go for Tx */
  1419. if(DEPParams->txBufLen > 0U)
  1420. {
  1421. /* Ensure that an RTOX Ack is not being expected at moment */
  1422. if( !gNfcip.isWait4RTOX )
  1423. {
  1424. gNfcip.state = NFCIP_ST_TARG_DEP_TX;
  1425. return;
  1426. }
  1427. else
  1428. {
  1429. /* If RTOX Ack is expected, signal a pending Tx to be transmitted right after */
  1430. gNfcip.isTxPending = true;
  1431. nfcipLogW( " NFCIP(T) Waiting RTOX, queueing outgoing DEP Block \r\n" );
  1432. }
  1433. }
  1434. /*Digital 1.0 14.12.4.1 In target mode the first PDU MUST be sent by the Initiator */
  1435. gNfcip.state = NFCIP_ST_TARG_DEP_RX;
  1436. return;
  1437. }
  1438. /* New data TxRx request clear previous error counters for consecutive TxRx without reseting communication/protocol layer*/
  1439. nfcipClearCounters();
  1440. gNfcip.state = NFCIP_ST_INIT_DEP_TX;
  1441. }
  1442. /*******************************************************************************/
  1443. bool rfalNfcDepTargetRcvdATR( void )
  1444. {
  1445. return ( (gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET) && nfcipIsTarget(gNfcip.state) && (gNfcip.state > NFCIP_ST_TARG_WAIT_ATR) );
  1446. }
  1447. /*******************************************************************************/
  1448. bool rfalNfcDepIsAtrReq( const uint8_t* buf, uint16_t bufLen, uint8_t* nfcid3 )
  1449. {
  1450. uint8_t msgIt;
  1451. msgIt = 0;
  1452. if ( (bufLen < RFAL_NFCDEP_ATRREQ_MIN_LEN) || (bufLen > RFAL_NFCDEP_ATRREQ_MAX_LEN) )
  1453. {
  1454. return false;
  1455. }
  1456. if ( buf[msgIt++] != NFCIP_REQ )
  1457. {
  1458. return false;
  1459. }
  1460. if( buf[msgIt++] != (uint8_t)NFCIP_CMD_ATR_REQ )
  1461. {
  1462. return false;
  1463. }
  1464. /* Output NFID3 if requested */
  1465. if( nfcid3 != NULL )
  1466. {
  1467. ST_MEMCPY( nfcid3, &buf[RFAL_NFCDEP_ATR_REQ_NFCID3_POS], RFAL_NFCDEP_NFCID3_LEN );
  1468. }
  1469. return true;
  1470. }
  1471. /*******************************************************************************/
  1472. static ReturnCode nfcipTargetHandleActivation( rfalNfcDepDevice *nfcDepDev, uint8_t *outBRS )
  1473. {
  1474. ReturnCode ret;
  1475. uint8_t msgIt;
  1476. uint8_t txBuf[RFAL_NFCDEP_HEADER_PAD + NFCIP_PSLRES_LEN];
  1477. /*******************************************************************************/
  1478. /* Check if we are in correct state */
  1479. /*******************************************************************************/
  1480. if( gNfcip.state != NFCIP_ST_TARG_WAIT_ACTV )
  1481. {
  1482. return ERR_WRONG_STATE;
  1483. }
  1484. /*******************************************************************************/
  1485. /* Check required parameters */
  1486. /*******************************************************************************/
  1487. if( outBRS == NULL )
  1488. {
  1489. return ERR_PARAM;
  1490. }
  1491. /*******************************************************************************/
  1492. /* Wait and process incoming cmd (PSL / DEP) */
  1493. /*******************************************************************************/
  1494. ret = nfcipDataRx( false );
  1495. if( ret != ERR_NONE )
  1496. {
  1497. return ret;
  1498. }
  1499. msgIt = 0;
  1500. *outBRS = RFAL_NFCDEP_BRS_MAINTAIN; /* set out BRS to be maintained */
  1501. msgIt++; /* Skip LEN byte */
  1502. if ( gNfcip.rxBuf[msgIt++] != NFCIP_REQ )
  1503. {
  1504. return ERR_PROTO;
  1505. }
  1506. if( gNfcip.rxBuf[msgIt] == (uint8_t)NFCIP_CMD_PSL_REQ )
  1507. {
  1508. msgIt++;
  1509. if( gNfcip.rxBuf[msgIt++] != gNfcip.cfg.did ) /* Checking DID */
  1510. {
  1511. return ERR_PROTO;
  1512. }
  1513. nfcipLogI( " NFCIP(T) PSL REQ rcvd \r\n" );
  1514. *outBRS = gNfcip.rxBuf[msgIt++]; /* assign output BRS value */
  1515. /* Store FSL(LR) and update current config */
  1516. gNfcip.cfg.lr = (gNfcip.rxBuf[msgIt++] & RFAL_NFCDEP_LR_VAL_MASK);
  1517. gNfcip.fsc = rfalNfcDepLR2FS( gNfcip.cfg.lr );
  1518. /*******************************************************************************/
  1519. /* Update NFC-DDE Device info */
  1520. if( nfcDepDev != NULL )
  1521. {
  1522. /* Update Bitrate info */
  1523. /* PRQA S 4342 2 # MISRA 10.5 - Layout of enum rfalBitRate and definition of rfalNfcDepBRS2DSI guarantee no invalid enum values to be created */
  1524. nfcDepDev->info.DSI = (rfalBitRate)rfalNfcDepBRS2DSI( *outBRS ); /* DSI codes the bit rate from Initiator to Target */
  1525. nfcDepDev->info.DRI = (rfalBitRate)rfalNfcDepBRS2DRI( *outBRS ); /* DRI codes the bit rate from Target to Initiator */
  1526. /* Update Length Reduction and Frame Size */
  1527. nfcDepDev->info.LR = gNfcip.cfg.lr;
  1528. nfcDepDev->info.FS = gNfcip.fsc;
  1529. /* Update PPi byte */
  1530. nfcDepDev->activation.Initiator.ATR_REQ.PPi &= ~RFAL_NFCDEP_PP_LR_MASK;
  1531. nfcDepDev->activation.Initiator.ATR_REQ.PPi |= rfalNfcDepLR2PP( gNfcip.cfg.lr );
  1532. }
  1533. rfalSetBitRate( RFAL_BR_KEEP, gNfcip.nfcDepDev->info.DSI );
  1534. EXIT_ON_ERR( ret, nfcipTx( NFCIP_CMD_PSL_RES, txBuf, NULL, 0, 0, NFCIP_NO_FWT ) );
  1535. }
  1536. else
  1537. {
  1538. if( gNfcip.rxBuf[msgIt] == (uint8_t)NFCIP_CMD_DEP_REQ )
  1539. {
  1540. msgIt++;
  1541. /*******************************************************************************/
  1542. /* Digital 1.0 14.12.3.1 PNI must be initialized to 0 */
  1543. if( nfcip_PBF_PNI( gNfcip.rxBuf[msgIt] ) != 0U )
  1544. {
  1545. return ERR_PROTO;
  1546. }
  1547. /*******************************************************************************/
  1548. /* Digital 1.0 14.8.2.1 check if DID is expected and match -> Protocol Error */
  1549. if( nfcip_PFBhasDID( gNfcip.rxBuf[ msgIt] ) )
  1550. {
  1551. if( gNfcip.rxBuf[++msgIt] != gNfcip.cfg.did )
  1552. {
  1553. return ERR_PROTO;
  1554. }
  1555. }
  1556. else if( gNfcip.cfg.did != RFAL_NFCDEP_DID_NO ) /* DID expected but not rcv */
  1557. {
  1558. return ERR_PROTO;
  1559. }
  1560. else
  1561. {
  1562. /* MISRA 15.7 - Empty else */
  1563. }
  1564. }
  1565. /* Signal Request pending to be digested on normal Handling (DEP_REQ, DSL_REQ, RLS_REQ) */
  1566. gNfcip.isReqPending = true;
  1567. }
  1568. gNfcip.state = NFCIP_ST_TARG_DEP_RX;
  1569. return ERR_NONE;
  1570. }
  1571. /*******************************************************************************/
  1572. ReturnCode rfalNfcDepATR( const rfalNfcDepAtrParam* param, rfalNfcDepAtrRes *atrRes, uint8_t* atrResLen )
  1573. {
  1574. ReturnCode ret;
  1575. rfalNfcDepConfigs cfg;
  1576. uint16_t rxLen;
  1577. uint8_t msgIt;
  1578. uint8_t txBuf[RFAL_NFCDEP_ATRREQ_MAX_LEN];
  1579. uint8_t rxBuf[NFCIP_ATRRES_BUF_LEN];
  1580. if( (param == NULL) || (atrRes == NULL) || (atrResLen == NULL) )
  1581. {
  1582. return ERR_PARAM;
  1583. }
  1584. /*******************************************************************************/
  1585. /* Configure NFC-DEP layer */
  1586. /*******************************************************************************/
  1587. cfg.did = param->DID;
  1588. cfg.nad = param->NAD;
  1589. cfg.fwt = RFAL_NFCDEP_MAX_FWT;
  1590. cfg.dFwt = RFAL_NFCDEP_MAX_FWT;
  1591. cfg.br = param->BR;
  1592. cfg.bs = param->BS;
  1593. cfg.lr = param->LR;
  1594. cfg.to = RFAL_NFCDEP_WT_TRG_MAX; /* Not used in Initiator mode */
  1595. cfg.gbLen = param->GBLen;
  1596. if( cfg.gbLen > 0U ) /* MISRA 21.18 */
  1597. {
  1598. ST_MEMCPY( cfg.gb, param->GB, cfg.gbLen );
  1599. }
  1600. cfg.nfcidLen = param->nfcidLen;
  1601. if( cfg.nfcidLen > 0U ) /* MISRA 21.18 */
  1602. {
  1603. ST_MEMCPY( cfg.nfcid, param->nfcid, cfg.nfcidLen );
  1604. }
  1605. cfg.role = RFAL_NFCDEP_ROLE_INITIATOR;
  1606. cfg.oper = param->operParam;
  1607. cfg.commMode = param->commMode;
  1608. rfalNfcDepInitialize();
  1609. nfcipConfig( &cfg );
  1610. /*******************************************************************************/
  1611. /* Send ATR_REQ */
  1612. /*******************************************************************************/
  1613. EXIT_ON_ERR( ret, nfcipTxRx(NFCIP_CMD_ATR_REQ, txBuf, nfcipRWTActivation(), NULL, 0, rxBuf, NFCIP_ATRRES_BUF_LEN, &rxLen ) );
  1614. /*******************************************************************************/
  1615. /* ATR sent, check response */
  1616. /*******************************************************************************/
  1617. msgIt = 0;
  1618. rxLen = ((uint16_t)rxBuf[msgIt++] - RFAL_NFCDEP_LEN_LEN); /* use LEN byte */
  1619. if( (rxLen < RFAL_NFCDEP_ATRRES_MIN_LEN) || (rxLen > RFAL_NFCDEP_ATRRES_MAX_LEN) ) /* Checking length: ATR_RES */
  1620. {
  1621. return ERR_PROTO;
  1622. }
  1623. if( rxBuf[msgIt++] != NFCIP_RES ) /* Checking if is a response*/
  1624. {
  1625. return ERR_PROTO;
  1626. }
  1627. if( rxBuf[msgIt++] != (uint8_t)NFCIP_CMD_ATR_RES ) /* Checking if is a ATR RES */
  1628. {
  1629. return ERR_PROTO;
  1630. }
  1631. ST_MEMCPY( (uint8_t*)atrRes, (rxBuf + RFAL_NFCDEP_LEN_LEN), rxLen );
  1632. *atrResLen = (uint8_t)rxLen;
  1633. return ERR_NONE;
  1634. }
  1635. /*******************************************************************************/
  1636. ReturnCode rfalNfcDepPSL( uint8_t BRS, uint8_t FSL )
  1637. {
  1638. ReturnCode ret;
  1639. uint16_t rxLen;
  1640. uint8_t msgIt;
  1641. uint8_t txBuf[NFCIP_PSLREQ_LEN + NFCIP_PSLPAY_LEN];
  1642. uint8_t rxBuf[NFCIP_PSLRES_LEN];
  1643. msgIt = NFCIP_PSLREQ_LEN;
  1644. txBuf[msgIt++] = BRS;
  1645. txBuf[msgIt++] = FSL;
  1646. /*******************************************************************************/
  1647. /* Send PSL REQ and wait for response */
  1648. /*******************************************************************************/
  1649. EXIT_ON_ERR( ret, nfcipTxRx( NFCIP_CMD_PSL_REQ, txBuf, nfcipRWTActivation(), &txBuf[NFCIP_PSLREQ_LEN], (msgIt - NFCIP_PSLREQ_LEN), rxBuf, NFCIP_PSLRES_LEN, &rxLen ) );
  1650. /*******************************************************************************/
  1651. /* PSL sent, check response */
  1652. /*******************************************************************************/
  1653. msgIt = 0;
  1654. rxLen = (uint16_t)(rxBuf[msgIt++]); /* use LEN byte */
  1655. if( rxLen < NFCIP_PSLRES_LEN ) /* Checking length: LEN + RLS_RES */
  1656. {
  1657. return ERR_PROTO;
  1658. }
  1659. if( rxBuf[msgIt++] != NFCIP_RES ) /* Checking if is a response */
  1660. {
  1661. return ERR_PROTO;
  1662. }
  1663. if( rxBuf[msgIt++] != (uint8_t)NFCIP_CMD_PSL_RES ) /* Checking if is a PSL RES */
  1664. {
  1665. return ERR_PROTO;
  1666. }
  1667. if( rxBuf[msgIt++] != gNfcip.cfg.did ) /* Checking DID */
  1668. {
  1669. return ERR_PROTO;
  1670. }
  1671. return ERR_NONE;
  1672. }
  1673. /*******************************************************************************/
  1674. ReturnCode rfalNfcDepDSL( void )
  1675. {
  1676. ReturnCode ret;
  1677. uint8_t txBuf[ RFAL_NFCDEP_HEADER_PAD + NFCIP_DSLREQ_LEN];
  1678. uint8_t rxBuf[NFCIP_DSLRES_LEN];
  1679. uint8_t rxMsgIt;
  1680. uint16_t rxLen = 0;
  1681. if( gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET )
  1682. {
  1683. return ERR_NONE; /* Target has no deselect procedure */
  1684. }
  1685. /* Repeating a DSL REQ is optional, not doing it */
  1686. EXIT_ON_ERR( ret, nfcipTxRx( NFCIP_CMD_DSL_REQ, txBuf, nfcipRWTActivation(), NULL, 0, rxBuf, (uint16_t)sizeof(rxBuf), &rxLen ) );
  1687. /*******************************************************************************/
  1688. rxMsgIt = 0;
  1689. if( rxBuf[rxMsgIt++] < NFCIP_DSLRES_MIN ) /* Checking length: LEN + DSL_RES */
  1690. {
  1691. return ERR_PROTO;
  1692. }
  1693. if( rxBuf[rxMsgIt++] != NFCIP_RES ) /* Checking if is a response */
  1694. {
  1695. return ERR_PROTO;
  1696. }
  1697. if( rxBuf[rxMsgIt++] != (uint8_t)NFCIP_CMD_DSL_RES ) /* Checking if is DSL RES */
  1698. {
  1699. return ERR_PROTO;
  1700. }
  1701. if( gNfcip.cfg.did != RFAL_NFCDEP_DID_NO )
  1702. {
  1703. if ( rxBuf[rxMsgIt++] != gNfcip.cfg.did )
  1704. {
  1705. return ERR_PROTO;
  1706. }
  1707. }
  1708. return ERR_NONE;
  1709. }
  1710. /*******************************************************************************/
  1711. ReturnCode rfalNfcDepRLS( void )
  1712. {
  1713. ReturnCode ret;
  1714. uint8_t txBuf[RFAL_NFCDEP_HEADER_PAD + NFCIP_RLSREQ_LEN];
  1715. uint8_t rxBuf[NFCIP_RLSRES_LEN];
  1716. uint8_t rxMsgIt;
  1717. uint16_t rxLen = 0;
  1718. if ( gNfcip.cfg.role == RFAL_NFCDEP_ROLE_TARGET ) /* Target has no release procedure */
  1719. {
  1720. return ERR_NONE;
  1721. }
  1722. /* Repeating a RLS REQ is optional, not doing it */
  1723. EXIT_ON_ERR( ret, nfcipTxRx( NFCIP_CMD_RLS_REQ, txBuf, nfcipRWTActivation(), NULL, 0, rxBuf, (uint16_t)sizeof(rxBuf), &rxLen ) );
  1724. /*******************************************************************************/
  1725. rxMsgIt = 0;
  1726. if( rxBuf[rxMsgIt++] < NFCIP_RLSRES_MIN ) /* Checking length: LEN + RLS_RES */
  1727. {
  1728. return ERR_PROTO;
  1729. }
  1730. if( rxBuf[rxMsgIt++] != NFCIP_RES ) /* Checking if is a response */
  1731. {
  1732. return ERR_PROTO;
  1733. }
  1734. if( rxBuf[rxMsgIt++] != (uint8_t)NFCIP_CMD_RLS_RES ) /* Checking if is RLS RES */
  1735. {
  1736. return ERR_PROTO;
  1737. }
  1738. if( gNfcip.cfg.did != RFAL_NFCDEP_DID_NO )
  1739. {
  1740. if ( rxBuf[rxMsgIt++] != gNfcip.cfg.did )
  1741. {
  1742. return ERR_PROTO;
  1743. }
  1744. }
  1745. return ERR_NONE;
  1746. }
  1747. /*******************************************************************************/
  1748. ReturnCode rfalNfcDepInitiatorHandleActivation( rfalNfcDepAtrParam* param, rfalBitRate desiredBR, rfalNfcDepDevice* nfcDepDev )
  1749. {
  1750. ReturnCode ret;
  1751. uint8_t maxRetyrs;
  1752. uint8_t PSL_BRS;
  1753. uint8_t PSL_FSL;
  1754. bool sendPSL;
  1755. if( (param == NULL) || (nfcDepDev == NULL) )
  1756. {
  1757. return ERR_PARAM;
  1758. }
  1759. param->NAD = RFAL_NFCDEP_NAD_NO; /* Digital 1.1 16.6.2.9 Initiator SHALL NOT use NAD */
  1760. maxRetyrs = NFCIP_ATR_RETRY_MAX;
  1761. /*******************************************************************************/
  1762. /* Send ATR REQ and wait for response */
  1763. /*******************************************************************************/
  1764. do{ /* Upon transmission error ATR REQ should be retried */
  1765. ret = rfalNfcDepATR( param, &nfcDepDev->activation.Target.ATR_RES, &nfcDepDev->activation.Target.ATR_RESLen );
  1766. if( nfcipIsTransmissionError(ret) )
  1767. {
  1768. continue;
  1769. }
  1770. break;
  1771. }
  1772. while( (maxRetyrs--) != 0U );
  1773. if( ret != ERR_NONE )
  1774. {
  1775. return ret;
  1776. }
  1777. /*******************************************************************************/
  1778. /* Compute NFC-DEP device with ATR_RES */
  1779. /*******************************************************************************/
  1780. nfcDepDev->info.GBLen = (nfcDepDev->activation.Target.ATR_RESLen - RFAL_NFCDEP_ATRRES_MIN_LEN);
  1781. nfcDepDev->info.DID = nfcDepDev->activation.Target.ATR_RES.DID;
  1782. nfcDepDev->info.NAD = RFAL_NFCDEP_NAD_NO; /* Digital 1.1 16.6.3.11 Initiator SHALL ignore b1 of PPt */
  1783. nfcDepDev->info.LR = rfalNfcDepPP2LR( nfcDepDev->activation.Target.ATR_RES.PPt );
  1784. nfcDepDev->info.FS = rfalNfcDepLR2FS( nfcDepDev->info.LR );
  1785. nfcDepDev->info.WT = (nfcDepDev->activation.Target.ATR_RES.TO & RFAL_NFCDEP_WT_MASK);
  1786. nfcDepDev->info.FWT = rfalNfcDepCalculateRWT( nfcDepDev->info.WT );
  1787. nfcDepDev->info.dFWT = RFAL_NFCDEP_WT_DELTA;
  1788. rfalGetBitRate( &nfcDepDev->info.DSI, &nfcDepDev->info.DRI );
  1789. /*******************************************************************************/
  1790. /* Check if a PSL needs to be sent */
  1791. /*******************************************************************************/
  1792. sendPSL = false;
  1793. PSL_BRS = rfalNfcDepDx2BRS( nfcDepDev->info.DSI ); /* Set current bit rate divisor on both directions */
  1794. PSL_FSL = nfcDepDev->info.LR; /* Set current Frame Size */
  1795. /* Activity 1.0 9.4.4.15 & 9.4.6.3 NFC-DEP Activation PSL
  1796. * Activity 2.0 9.4.4.17 & 9.4.6.6 NFC-DEP Activation PSL
  1797. *
  1798. * PSL_REQ shall only be sent if desired bit rate is different from current (Activity 1.0)
  1799. * PSL_REQ shall be sent to update LR or bit rate (Activity 2.0)
  1800. * */
  1801. #if 0 /* PSL due to LR is disabled, can be enabled if desired*/
  1802. /*******************************************************************************/
  1803. /* Check Frame Size */
  1804. /*******************************************************************************/
  1805. if( gNfcip.cfg.lr < nfcDepDev->info.LR ) /* If our Length reduction is smaller */
  1806. {
  1807. sendPSL = true;
  1808. nfcDepDev->info.LR = MIN( nfcDepDev->info.LR, gNfcip.cfg.lr );
  1809. gNfcip.cfg.lr = nfcDepDev->info.LR; /* Update nfcip LR to be used */
  1810. gNfcip.fsc = rfalNfcDepLR2FS( gNfcip.cfg.lr ); /* Update nfcip FSC to be used */
  1811. PSL_FSL = gNfcip.cfg.lr; /* Set LR to be sent */
  1812. nfcipLogI( " NFCIP(I) Frame Size differ, PSL new fsc: %d \r\n", gNfcip.fsc );
  1813. }
  1814. #endif
  1815. /*******************************************************************************/
  1816. /* Check Baud rates */
  1817. /*******************************************************************************/
  1818. if( (nfcDepDev->info.DSI != desiredBR) && (desiredBR != RFAL_BR_KEEP) ) /* if desired BR is different */
  1819. {
  1820. if( nfcipDxIsSupported( (uint8_t)desiredBR, nfcDepDev->activation.Target.ATR_RES.BRt, nfcDepDev->activation.Target.ATR_RES.BSt ) ) /* if desired BR is supported */ /* MISRA 13.5 */
  1821. {
  1822. sendPSL = true;
  1823. PSL_BRS = rfalNfcDepDx2BRS( desiredBR );
  1824. nfcipLogI( " NFCIP(I) BR differ, PSL BR: 0x%02X \r\n", PSL_BRS );
  1825. }
  1826. }
  1827. /*******************************************************************************/
  1828. if( sendPSL )
  1829. {
  1830. /*******************************************************************************/
  1831. /* Send PSL REQ and wait for response */
  1832. /*******************************************************************************/
  1833. EXIT_ON_ERR( ret, rfalNfcDepPSL(PSL_BRS, PSL_FSL) );
  1834. /* Check if bit rate has been changed */
  1835. if( nfcDepDev->info.DSI != desiredBR )
  1836. {
  1837. /* Check if device was in Passive NFC-A and went to higher bit rates, use NFC-F */
  1838. if( (nfcDepDev->info.DSI == RFAL_BR_106) && (gNfcip.cfg.commMode == RFAL_NFCDEP_COMM_PASSIVE) )
  1839. {
  1840. #if RFAL_FEATURE_NFCF
  1841. /* If Passive initialize NFC-F module */
  1842. rfalNfcfPollerInitialize( desiredBR );
  1843. #else /* RFAL_FEATURE_NFCF */
  1844. return ERR_NOTSUPP;
  1845. #endif /* RFAL_FEATURE_NFCF */
  1846. }
  1847. nfcDepDev->info.DRI = desiredBR; /* DSI Bit Rate coding from Initiator to Target */
  1848. nfcDepDev->info.DSI = desiredBR; /* DRI Bit Rate coding from Target to Initiator */
  1849. rfalSetBitRate( nfcDepDev->info.DSI, nfcDepDev->info.DRI );
  1850. }
  1851. return ERR_NONE; /* PSL has been sent */
  1852. }
  1853. return ERR_NONE; /* No PSL has been sent */
  1854. }
  1855. /*******************************************************************************/
  1856. uint32_t rfalNfcDepCalculateRWT( uint8_t wt )
  1857. {
  1858. /* Digital 1.0 14.6.3.8 & Digital 1.1 16.6.3.9 */
  1859. /* Digital 1.1 16.6.3.9 treat all RFU values as WT=14 */
  1860. uint8_t responseWaitTime = MIN( RFAL_NFCDEP_WT_INI_MAX, wt );
  1861. return (uint32_t)rfalNfcDepWT2RWT(responseWaitTime);
  1862. }
  1863. /*******************************************************************************/
  1864. static ReturnCode nfcipDataTx( uint8_t* txBuf, uint16_t txBufLen, uint32_t fwt )
  1865. {
  1866. return rfalTransceiveBlockingTx( txBuf, txBufLen, gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen, (RFAL_TXRX_FLAGS_DEFAULT | (uint32_t)RFAL_TXRX_FLAGS_NFCIP1_ON), ((fwt == NFCIP_NO_FWT) ? RFAL_FWT_NONE : fwt) );
  1867. }
  1868. /*******************************************************************************/
  1869. static ReturnCode nfcipDataRx( bool blocking )
  1870. {
  1871. ReturnCode ret;
  1872. /* Perform Rx either blocking or non-blocking */
  1873. if( blocking )
  1874. {
  1875. ret = rfalTransceiveBlockingRx();
  1876. }
  1877. else
  1878. {
  1879. ret = rfalGetTransceiveStatus();
  1880. }
  1881. if( ret != ERR_BUSY )
  1882. {
  1883. if( gNfcip.rxRcvdLen != NULL )
  1884. {
  1885. (*gNfcip.rxRcvdLen) = rfalConvBitsToBytes( *gNfcip.rxRcvdLen );
  1886. if( (ret == ERR_NONE) && (gNfcip.rxBuf != NULL) )
  1887. {
  1888. /* Digital 1.1 16.4.1.3 - Length byte LEN SHALL have a value between 3 and 255 -> otherwise treat as Transmission Error *
  1889. * - Ensure that actual received and frame length do match, otherwise treat as Transmission error */
  1890. if( (*gNfcip.rxRcvdLen != (uint16_t)*gNfcip.rxBuf) || (*gNfcip.rxRcvdLen < RFAL_NFCDEP_LEN_MIN) || (*gNfcip.rxRcvdLen > RFAL_NFCDEP_LEN_MAX) )
  1891. {
  1892. return ERR_FRAMING;
  1893. }
  1894. }
  1895. }
  1896. }
  1897. return ret;
  1898. }
  1899. /*******************************************************************************/
  1900. ReturnCode rfalNfcDepListenStartActivation( const rfalNfcDepTargetParam *param, const uint8_t *atrReq, uint16_t atrReqLength, rfalNfcDepListenActvParam rxParam )
  1901. {
  1902. ReturnCode ret;
  1903. rfalNfcDepConfigs cfg;
  1904. if( (param == NULL) || (atrReq == NULL) || (rxParam.rxLen == NULL) )
  1905. {
  1906. return ERR_PARAM;
  1907. }
  1908. /*******************************************************************************/
  1909. /* Check whether is a valid ATR_REQ Compute NFC-DEP device */
  1910. if( !rfalNfcDepIsAtrReq( atrReq, atrReqLength, NULL ) )
  1911. {
  1912. return ERR_PARAM;
  1913. }
  1914. rxParam.nfcDepDev->activation.Initiator.ATR_REQLen = (uint8_t)atrReqLength; /* nfcipIsAtrReq() is already checking Min and Max buffer lengths */
  1915. if( atrReqLength > 0U ) /* MISRA 21.18 */
  1916. {
  1917. ST_MEMCPY( (uint8_t*)&rxParam.nfcDepDev->activation.Initiator.ATR_REQ, atrReq, atrReqLength );
  1918. }
  1919. rxParam.nfcDepDev->info.GBLen = (uint8_t)(atrReqLength - RFAL_NFCDEP_ATRREQ_MIN_LEN);
  1920. rxParam.nfcDepDev->info.DID = rxParam.nfcDepDev->activation.Initiator.ATR_REQ.DID;
  1921. rxParam.nfcDepDev->info.NAD = RFAL_NFCDEP_NAD_NO; /* Digital 1.1 16.6.2.9 Initiator SHALL NOT use NAD */
  1922. rxParam.nfcDepDev->info.LR = rfalNfcDepPP2LR( rxParam.nfcDepDev->activation.Initiator.ATR_REQ.PPi );
  1923. rxParam.nfcDepDev->info.FS = rfalNfcDepLR2FS( rxParam.nfcDepDev->info.LR );
  1924. rxParam.nfcDepDev->info.WT = 0;
  1925. rxParam.nfcDepDev->info.FWT = NFCIP_NO_FWT;
  1926. rxParam.nfcDepDev->info.dFWT = NFCIP_NO_FWT;
  1927. rfalGetBitRate( &rxParam.nfcDepDev->info.DSI, &rxParam.nfcDepDev->info.DRI );
  1928. /* Store Device Info location, updated upon a PSL */
  1929. gNfcip.nfcDepDev = rxParam.nfcDepDev;
  1930. /*******************************************************************************/
  1931. cfg.did = rxParam.nfcDepDev->activation.Initiator.ATR_REQ.DID;
  1932. cfg.nad = RFAL_NFCDEP_NAD_NO;
  1933. cfg.fwt = RFAL_NFCDEP_MAX_FWT;
  1934. cfg.dFwt = RFAL_NFCDEP_MAX_FWT;
  1935. cfg.br = param->brt;
  1936. cfg.bs = param->bst;
  1937. cfg.lr = rfalNfcDepPP2LR(param->ppt);
  1938. cfg.gbLen = param->GBtLen;
  1939. if( cfg.gbLen > 0U ) /* MISRA 21.18 */
  1940. {
  1941. ST_MEMCPY(cfg.gb, param->GBt, cfg.gbLen);
  1942. }
  1943. cfg.nfcidLen = RFAL_NFCDEP_NFCID3_LEN;
  1944. ST_MEMCPY(cfg.nfcid, param->nfcid3, RFAL_NFCDEP_NFCID3_LEN);
  1945. cfg.to = param->to;
  1946. cfg.role = RFAL_NFCDEP_ROLE_TARGET;
  1947. cfg.oper = param->operParam;
  1948. cfg.commMode = param->commMode;
  1949. rfalNfcDepInitialize();
  1950. nfcipConfig( &cfg );
  1951. /*******************************************************************************/
  1952. /* Reply with ATR RES to Initiator */
  1953. /*******************************************************************************/
  1954. gNfcip.rxBuf = (uint8_t*)rxParam.rxBuf;
  1955. gNfcip.rxBufLen = sizeof(rfalNfcDepBufFormat);
  1956. gNfcip.rxRcvdLen = rxParam.rxLen;
  1957. gNfcip.rxBufPaylPos = RFAL_NFCDEP_DEPREQ_HEADER_LEN;
  1958. gNfcip.isChaining = rxParam.isRxChaining;
  1959. gNfcip.txBufPaylPos = RFAL_NFCDEP_DEPREQ_HEADER_LEN;
  1960. EXIT_ON_ERR( ret, nfcipTx( NFCIP_CMD_ATR_RES, (uint8_t*) gNfcip.rxBuf, NULL, 0, 0, NFCIP_NO_FWT ) );
  1961. gNfcip.state = NFCIP_ST_TARG_WAIT_ACTV;
  1962. return ERR_NONE;
  1963. }
  1964. /*******************************************************************************/
  1965. ReturnCode rfalNfcDepListenGetActivationStatus( void )
  1966. {
  1967. ReturnCode err;
  1968. uint8_t BRS;
  1969. BRS = RFAL_NFCDEP_BRS_MAINTAIN;
  1970. err = nfcipTargetHandleActivation( gNfcip.nfcDepDev, &BRS );
  1971. switch (err)
  1972. {
  1973. case ERR_NONE:
  1974. if( BRS != RFAL_NFCDEP_BRS_MAINTAIN )
  1975. {
  1976. /* DSI codes the bit rate from Initiator to Target */
  1977. /* DRI codes the bit rate from Target to Initiator */
  1978. if( gNfcip.cfg.commMode == RFAL_NFCDEP_COMM_ACTIVE )
  1979. {
  1980. EXIT_ON_ERR( err, rfalSetMode( RFAL_MODE_LISTEN_ACTIVE_P2P, gNfcip.nfcDepDev->info.DRI, gNfcip.nfcDepDev->info.DSI ));
  1981. }
  1982. else
  1983. {
  1984. EXIT_ON_ERR( err, rfalSetMode( ((RFAL_BR_106 == gNfcip.nfcDepDev->info.DRI) ? RFAL_MODE_LISTEN_NFCA : RFAL_MODE_LISTEN_NFCF), gNfcip.nfcDepDev->info.DRI, gNfcip.nfcDepDev->info.DSI ));
  1985. }
  1986. }
  1987. break;
  1988. case ERR_BUSY:
  1989. // do nothing
  1990. break;
  1991. case ERR_PROTO:
  1992. default:
  1993. // re-enable receiving of data
  1994. nfcDepReEnableRx( gNfcip.rxBuf, gNfcip.rxBufLen, gNfcip.rxRcvdLen );
  1995. break;
  1996. }
  1997. return err;
  1998. }
  1999. /*******************************************************************************/
  2000. ReturnCode rfalNfcDepStartTransceive( const rfalNfcDepTxRxParam *param )
  2001. {
  2002. rfalNfcDepDEPParams nfcDepParams;
  2003. nfcDepParams.txBuf = (uint8_t *)param->txBuf;
  2004. nfcDepParams.txBufLen = param->txBufLen;
  2005. nfcDepParams.txChaining = param->isTxChaining;
  2006. nfcDepParams.txBufPaylPos = RFAL_NFCDEP_DEPREQ_HEADER_LEN; /* position in txBuf where actual outgoing data is located */
  2007. nfcDepParams.did = RFAL_NFCDEP_DID_KEEP;
  2008. nfcDepParams.rxBufPaylPos = RFAL_NFCDEP_DEPREQ_HEADER_LEN;
  2009. nfcDepParams.rxBuf = (uint8_t *)param->rxBuf;
  2010. nfcDepParams.rxBufLen = sizeof(rfalNfcDepBufFormat);
  2011. nfcDepParams.fsc = param->FSx;
  2012. nfcDepParams.fwt = param->FWT;
  2013. nfcDepParams.dFwt = param->dFWT;
  2014. gNfcip.rxRcvdLen = param->rxLen;
  2015. gNfcip.isChaining = param->isRxChaining;
  2016. nfcipSetDEPParams(&nfcDepParams);
  2017. return ERR_NONE;
  2018. }
  2019. /*******************************************************************************/
  2020. ReturnCode rfalNfcDepGetTransceiveStatus( void )
  2021. {
  2022. return nfcipRun( gNfcip.rxRcvdLen, gNfcip.isChaining );
  2023. }
  2024. /*******************************************************************************/
  2025. static void rfalNfcDepPdu2BLockParam( rfalNfcDepPduTxRxParam pduParam, rfalNfcDepTxRxParam *blockParam, uint16_t txPos, uint16_t rxPos )
  2026. {
  2027. uint16_t maxInfLen;
  2028. NO_WARNING(rxPos); /* Keep this param for future use */
  2029. blockParam->DID = pduParam.DID;
  2030. blockParam->FSx = pduParam.FSx;
  2031. blockParam->FWT = pduParam.FWT;
  2032. blockParam->dFWT = pduParam.dFWT;
  2033. /* Calculate max INF/Payload to be sent to other device */
  2034. maxInfLen = (blockParam->FSx - (RFAL_NFCDEP_HEADER + RFAL_NFCDEP_DEP_PFB_LEN));
  2035. maxInfLen += ((blockParam->DID != RFAL_NFCDEP_DID_NO) ? RFAL_NFCDEP_DID_LEN : 0U);
  2036. if( (pduParam.txBufLen - txPos) > maxInfLen )
  2037. {
  2038. blockParam->isTxChaining = true;
  2039. blockParam->txBufLen = maxInfLen;
  2040. }
  2041. else
  2042. {
  2043. blockParam->isTxChaining = false;
  2044. blockParam->txBufLen = (pduParam.txBufLen - txPos);
  2045. }
  2046. /* TxBuf is moved to the beginning for every Block */
  2047. blockParam->txBuf = (rfalNfcDepBufFormat*)pduParam.txBuf; /* PRQA S 0310 # MISRA 11.3 - Intentional safe cast to avoiding large buffer duplication */
  2048. blockParam->rxBuf = pduParam.tmpBuf; /* Simply using the pdu buffer is not possible because of current ACK handling */
  2049. blockParam->isRxChaining = &gNfcip.isPDURxChaining;
  2050. blockParam->rxLen = pduParam.rxLen;
  2051. }
  2052. /*******************************************************************************/
  2053. ReturnCode rfalNfcDepStartPduTransceive( rfalNfcDepPduTxRxParam param )
  2054. {
  2055. rfalNfcDepTxRxParam txRxParam;
  2056. /* Initialize and store APDU context */
  2057. gNfcip.PDUParam = param;
  2058. gNfcip.PDUTxPos = 0;
  2059. gNfcip.PDURxPos = 0;
  2060. /* Convert PDU TxRxParams to Block TxRxParams */
  2061. rfalNfcDepPdu2BLockParam( gNfcip.PDUParam, &txRxParam, gNfcip.PDUTxPos, gNfcip.PDURxPos );
  2062. return rfalNfcDepStartTransceive( &txRxParam );
  2063. }
  2064. /*******************************************************************************/
  2065. ReturnCode rfalNfcDepGetPduTransceiveStatus( void )
  2066. {
  2067. ReturnCode ret;
  2068. rfalNfcDepTxRxParam txRxParam;
  2069. ret = rfalNfcDepGetTransceiveStatus();
  2070. switch( ret )
  2071. {
  2072. /*******************************************************************************/
  2073. case ERR_NONE:
  2074. /* Check if we are still doing chaining on Tx */
  2075. if( gNfcip.isTxChaining )
  2076. {
  2077. /* Add already Tx bytes */
  2078. gNfcip.PDUTxPos += gNfcip.txBufLen;
  2079. /* Convert APDU TxRxParams to I-Block TxRxParams */
  2080. rfalNfcDepPdu2BLockParam( gNfcip.PDUParam, &txRxParam, gNfcip.PDUTxPos, gNfcip.PDURxPos );
  2081. if( txRxParam.txBufLen > 0U ) /* MISRA 21.18 */
  2082. {
  2083. /* Move next Block to beginning of APDU Tx buffer */
  2084. ST_MEMCPY( gNfcip.PDUParam.txBuf->pdu, &gNfcip.PDUParam.txBuf->pdu[gNfcip.PDUTxPos], txRxParam.txBufLen );
  2085. }
  2086. EXIT_ON_ERR( ret, rfalNfcDepStartTransceive( &txRxParam ) );
  2087. return ERR_BUSY;
  2088. }
  2089. /* PDU TxRx is done */
  2090. /* fall through */
  2091. /*******************************************************************************/
  2092. case ERR_AGAIN: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
  2093. /* Check if no PDU transceive has been started before (data from rfalNfcDepListenStartActivation) */
  2094. if( gNfcip.PDUParam.rxLen == NULL )
  2095. {
  2096. /* In Listen mode first chained packet cannot be retrieved via APDU interface */
  2097. if( ret == ERR_AGAIN )
  2098. {
  2099. return ERR_NOTSUPP;
  2100. }
  2101. /* TxRx is complete and full data is already available */
  2102. return ERR_NONE;
  2103. }
  2104. if( (*gNfcip.PDUParam.rxLen) > 0U ) /* MISRA 21.18 */
  2105. {
  2106. /* Ensure that data in tmpBuf still fits into PDU buffer */
  2107. if( (uint16_t)((uint16_t)gNfcip.PDURxPos + (*gNfcip.PDUParam.rxLen)) > RFAL_FEATURE_NFC_DEP_PDU_MAX_LEN )
  2108. {
  2109. return ERR_NOMEM;
  2110. }
  2111. /* Copy chained packet from tmp buffer to PDU buffer */
  2112. ST_MEMCPY( &gNfcip.PDUParam.rxBuf->pdu[gNfcip.PDURxPos], gNfcip.PDUParam.tmpBuf->inf, *gNfcip.PDUParam.rxLen );
  2113. gNfcip.PDURxPos += *gNfcip.PDUParam.rxLen;
  2114. }
  2115. /* Update output param rxLen */
  2116. *gNfcip.PDUParam.rxLen = gNfcip.PDURxPos;
  2117. /* Wait for following Block or PDU TxRx is done */
  2118. return ((ret == ERR_AGAIN) ? ERR_BUSY : ERR_NONE);
  2119. /*******************************************************************************/
  2120. default:
  2121. /* MISRA 16.4: no empty default statement (a comment being enough) */
  2122. break;
  2123. }
  2124. return ret;
  2125. }
  2126. #endif /* RFAL_FEATURE_NFC_DEP */