rfal_nfcDep.c 106 KB

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