rfal_nfcDep.c 106 KB

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