rfal_nfcDep.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /******************************************************************************
  2. * \attention
  3. *
  4. * <h2><center>&copy; COPYRIGHT 2020 STMicroelectronics</center></h2>
  5. *
  6. * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
  7. * You may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at:
  9. *
  10. * www.st.com/myliberty
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
  15. * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. ******************************************************************************/
  21. /*
  22. * PROJECT: ST25R391x firmware
  23. * Revision:
  24. * LANGUAGE: ISO C99
  25. */
  26. /*! \file rfal_nfcDep.h
  27. *
  28. * \author Gustavo Patricio
  29. *
  30. * \brief Implementation of NFC-DEP protocol
  31. *
  32. * NFC-DEP is also known as NFCIP - Near Field Communication
  33. * Interface and Protocol
  34. *
  35. * This implementation was based on the following specs:
  36. * - NFC Forum Digital 1.1
  37. * - ECMA 340 3rd Edition 2013
  38. *
  39. *
  40. * \addtogroup RFAL
  41. * @{
  42. *
  43. * \addtogroup RFAL-AL
  44. * \brief RFAL Abstraction Layer
  45. * @{
  46. *
  47. * \addtogroup NFC-DEP
  48. * \brief RFAL NFC-DEP Module
  49. * @{
  50. */
  51. #ifndef RFAL_NFCDEP_H_
  52. #define RFAL_NFCDEP_H_
  53. /*
  54. ******************************************************************************
  55. * INCLUDES
  56. ******************************************************************************
  57. */
  58. #include "platform.h"
  59. #include "st_errno.h"
  60. #include "rfal_rf.h"
  61. /*
  62. ******************************************************************************
  63. * ENABLE SWITCH
  64. ******************************************************************************
  65. */
  66. #ifndef RFAL_FEATURE_NFC_DEP
  67. #define RFAL_FEATURE_NFC_DEP false /*!< NFC-DEP module configuration missing. Disabled by default */
  68. #endif
  69. /* If module is disabled remove the need for the user to set lengths */
  70. #if !RFAL_FEATURE_NFC_DEP
  71. #undef RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN
  72. #undef RFAL_FEATURE_NFC_DEP_PDU_MAX_LEN
  73. #define RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN 1U /*!< NFC-DEP Block/Payload length, set to "none" */
  74. #define RFAL_FEATURE_NFC_DEP_PDU_MAX_LEN 1U /*!< NFC-DEP PDU length, set to "none" */
  75. #endif /* !RFAL_FEATURE_NFC_DEP */
  76. /*
  77. ******************************************************************************
  78. * DEFINES
  79. ******************************************************************************
  80. */
  81. #define RFAL_NFCDEP_FRAME_SIZE_MAX_LEN 254U /*!< Maximum Frame Size Digital 2.0 Table 90 */
  82. #define RFAL_NFCDEP_DEPREQ_HEADER_LEN 5U /*!< DEP_REQ header length: CMD_TYPE + CMD_CMD + PBF + DID + NAD */
  83. /*! Length NFCIP DEP REQ or RES header (incl LEN) */
  84. #define RFAL_NFCDEP_DEP_HEADER ( RFAL_NFCDEP_LEN_LEN + RFAL_NFCDEP_CMDTYPE_LEN + RFAL_NFCDEP_CMD_LEN + RFAL_NFCDEP_DEP_PFB_LEN )
  85. #define RFAL_NFCDEP_HEADER ( RFAL_NFCDEP_CMDTYPE_LEN + RFAL_NFCDEP_CMD_LEN ) /*!< NFCIP header length */
  86. #define RFAL_NFCDEP_SB_LEN 1U /*!< SB length on NFCIP fram for NFC-A */
  87. #define RFAL_NFCDEP_LEN_LEN 1U /*!< LEN length on NFCIP frame */
  88. #define RFAL_NFCDEP_CMDTYPE_LEN 1U /*!< Length of the cmd type (REQ | RES) on NFCIP frame */
  89. #define RFAL_NFCDEP_CMD_LEN 1U /*!< Length of the cmd on NFCIP frame */
  90. #define RFAL_NFCDEP_DID_LEN 1U /*!< Length of did on NFCIP frame */
  91. #define RFAL_NFCDEP_DEP_PFB_LEN 1U /*!< Length of the PFB field on NFCIP frame */
  92. #define RFAL_NFCDEP_DSL_RLS_LEN_NO_DID (RFAL_NFCDEP_LEN_LEN + RFAL_NFCDEP_CMDTYPE_LEN + RFAL_NFCDEP_CMD_LEN) /*!< Length of DSL_REQ and RLS_REQ with no DID */
  93. #define RFAL_NFCDEP_DSL_RLS_LEN_DID (RFAL_NFCDEP_DSL_RLS_LEN_NO_DID + RFAL_NFCDEP_DID_LEN) /*!< Length of DSL_REQ and RLS_REQ with DID */
  94. #define RFAL_NFCDEP_FS_VAL_MIN 64U /*!< Minimum LR value */
  95. #define RFAL_NFCDEP_LR_VAL_MASK 0x03U /*!< Bit mask for a LR value */
  96. #define RFAL_NFCDEP_PP_LR_MASK 0x30U /*!< Bit mask for LR value in PP byte on a ATR REQ/RES */
  97. #define RFAL_NFCDEP_PP_LR_SHIFT 4U /*!< Position of LR value in PP byte on a ATR REQ/RES */
  98. #define RFAL_NFCDEP_DID_MAX 14U /*!< Max DID value Digital 14.6.2.3 */
  99. #define RFAL_NFCDEP_DID_KEEP 0xFFU /*!< Keep DID value already configured */
  100. #define RFAL_NFCDEP_DID_NO 0x00U /*!< No DID shall be used */
  101. #define RFAL_NFCDEP_NAD_NO 0x00U /*!< No NAD shall be used */
  102. #define RFAL_NFCDEP_OPER_RTOX_REQ_DIS 0x01U /*!< Operation config: RTOX REQ disable */
  103. #define RFAL_NFCDEP_OPER_RTOX_REQ_EN 0x00U /*!< Operation config: RTOX REQ enable */
  104. #define RFAL_NFCDEP_OPER_ATN_DIS 0x00U /*!< Operation config: ATN disable */
  105. #define RFAL_NFCDEP_OPER_ATN_EN 0x02U /*!< Operation config: ATN enable */
  106. #define RFAL_NFCDEP_OPER_EMPTY_DEP_DIS 0x04U /*!< Operation config: empty DEPs disable */
  107. #define RFAL_NFCDEP_OPER_EMPTY_DEP_EN 0x00U /*!< Operation config: empty DEPs enable */
  108. #define RFAL_NFCDEP_OPER_FULL_MI_DIS 0x00U /*!< Operation config: full chaining DEPs disable */
  109. #define RFAL_NFCDEP_OPER_FULL_MI_EN 0x08U /*!< Operation config: full chaining DEPs enable */
  110. #define RFAL_NFCDEP_BRS_MAINTAIN 0xC0U /*!< Value signalling that BR is to be maintained (no PSL) */
  111. #define RFAL_NFCDEP_BRS_Dx_MASK 0x07U /*!< Value signalling that BR is to be maintained (no PSL) */
  112. #define RFAL_NFCDEP_BRS_DSI_POS 3U /*!< Value signalling that BR is to be maintained (no PSL) */
  113. #define RFAL_NFCDEP_WT_DELTA (16U - RFAL_NFCDEP_WT_DELTA_ADJUST) /*!< NFC-DEP dWRT (adjusted) Digital 2.0 B.10 */
  114. #define RFAL_NFCDEP_WT_DELTA_ADJUST 4U /*!< dWRT value adjustment */
  115. #define RFAL_NFCDEP_ATR_REQ_NFCID3_POS 2U /*!< NFCID3 offset in ATR_REQ frame */
  116. #define RFAL_NFCDEP_NFCID3_LEN 10U /*!< NFCID3 Length */
  117. #define RFAL_NFCDEP_LEN_MIN 3U /*!< Minimum length byte LEN value */
  118. #define RFAL_NFCDEP_LEN_MAX 255U /*!< Maximum length byte LEN value */
  119. #define RFAL_NFCDEP_ATRRES_HEADER_LEN 2U /*!< ATR RES Header Len: CmdType: 0xD5 + Cod: 0x01 */
  120. #define RFAL_NFCDEP_ATRRES_MIN_LEN 17U /*!< Minimum length for an ATR RES */
  121. #define RFAL_NFCDEP_ATRRES_MAX_LEN 64U /*!< Maximum length for an ATR RES Digital 1.0 14.6.1 */
  122. #define RFAL_NFCDEP_ATRREQ_MIN_LEN 16U /*!< Minimum length for an ATR REQ */
  123. #define RFAL_NFCDEP_ATRREQ_MAX_LEN RFAL_NFCDEP_ATRRES_MAX_LEN /*!< Maximum length for an ATR REQ Digital 1.0 14.6.1 */
  124. #define RFAL_NFCDEP_GB_MAX_LEN (RFAL_NFCDEP_ATRREQ_MAX_LEN - RFAL_NFCDEP_ATRREQ_MIN_LEN) /*!< Maximum length the General Bytes on ATR Digital 1.1 16.6.3 */
  125. #define RFAL_NFCDEP_WT_INI_DEFAULT RFAL_NFCDEP_WT_INI_MAX /*!< WT Initiator default value Digital 1.0 14.6.3.8 */
  126. #define RFAL_NFCDEP_WT_INI_MIN 0U /*!< WT Initiator minimum value Digital 1.0 14.6.3.8 */
  127. #define RFAL_NFCDEP_WT_INI_MAX 14U /*!< WT Initiator maximum value Digital 1.0 14.6.3.8 A.10 */
  128. #define RFAL_NFCDEP_RWT_INI_MAX rfalNfcDepWT2RWT( RFAL_NFCDEP_WT_INI_MAX ) /*!< RWT Initiator maximum value */
  129. #define RFAL_NFCDEP_WT_TRG_MAX_D10 8U /*!< WT target max Digital 1.0 14.6.3.8 A.10 */
  130. #define RFAL_NFCDEP_WT_TRG_MAX_D11 14U /*!< WT target max Digital 1.1 16.6.3.9 A.9 */
  131. #define RFAL_NFCDEP_WT_TRG_MAX_L13 10U /*!< WT target max [LLCP] 1.3 6.2.1 */
  132. #define RFAL_NFCDEP_WT_TRG_MAX RFAL_NFCDEP_WT_TRG_MAX_D11 /*!< WT target max Digital x.x | LLCP x.x */
  133. #define RFAL_NFCDEP_RWT_TRG_MAX rfalNfcDepWT2RWT( RFAL_NFCDEP_WT_TRG_MAX ) /*!< RWT Initiator maximum value */
  134. /*! Maximum Frame Waiting Time = ((256 * 16/fc)*2^FWImax) = ((256*16/fc)*2^14) = (1048576 / 64)/fc = (100000h*64)/fc */
  135. #define RFAL_NFCDEP_MAX_FWT ((uint32_t)1U<<20)
  136. #define RFAL_NFCDEP_WT_MASK 0x0FU /*!< Bit mask for the Wait Time value */
  137. #define RFAL_NFCDEP_BR_MASK_106 0x01U /*!< Enable mask bit rate 106 */
  138. #define RFAL_NFCDEP_BR_MASK_212 0x02U /*!< Enable mask bit rate 242 */
  139. #define RFAL_NFCDEP_BR_MASK_424 0x04U /*!< Enable mask bit rate 424 */
  140. /*
  141. ******************************************************************************
  142. * GLOBAL MACROS
  143. ******************************************************************************
  144. */
  145. #define rfalNfcDepWT2RWT( wt ) ( (uint32_t)1U << (((uint32_t)(wt) & RFAL_NFCDEP_WT_MASK) + 12U) ) /*!< Converts WT value to RWT (1/fc) */
  146. /*! Returns the BRS value from the given bit rate */
  147. #define rfalNfcDepDx2BRS( br ) ( (((uint8_t)(br) & RFAL_NFCDEP_BRS_Dx_MASK) << RFAL_NFCDEP_BRS_DSI_POS) | ((uint8_t)(br) & RFAL_NFCDEP_BRS_Dx_MASK) )
  148. #define rfalNfcDepBRS2DRI( brs ) (uint8_t)( (uint8_t)(brs) & RFAL_NFCDEP_BRS_Dx_MASK ) /*!< Returns the DRI value from the given BRS byte */
  149. #define rfalNfcDepBRS2DSI( brs ) (uint8_t)( ((uint8_t)(brs) >> RFAL_NFCDEP_BRS_DSI_POS) & RFAL_NFCDEP_BRS_Dx_MASK ) /*!< Returns the DSI value from the given BRS byte */
  150. #define rfalNfcDepPP2LR( PPx ) ( ((uint8_t)(PPx) & RFAL_NFCDEP_PP_LR_MASK ) >> RFAL_NFCDEP_PP_LR_SHIFT) /*!< Returns the LR value from the given PPx byte */
  151. #define rfalNfcDepLR2PP( LRx ) ( ((uint8_t)(LRx) << RFAL_NFCDEP_PP_LR_SHIFT) & RFAL_NFCDEP_PP_LR_MASK) /*!< Returns the PP byte with the given LRx value */
  152. /*! Returns the Frame size value from the given LRx value */
  153. #define rfalNfcDepLR2FS( LRx ) (uint16_t)(MIN( (RFAL_NFCDEP_FS_VAL_MIN * ((uint16_t)(LRx) + 1U) ), RFAL_NFCDEP_FRAME_SIZE_MAX_LEN ))
  154. /*!
  155. * Despite DIGITAL 1.0 14.6.2.1 stating that the last two bytes may filled with
  156. * any value, some devices (Samsung Google Nexus) only accept when these are 0 */
  157. #define rfalNfcDepSetNFCID( dst, src, len ) ST_MEMSET( (dst), 0x00, RFAL_NFCDEP_NFCID3_LEN ); \
  158. if( (len) > 0U ) {ST_MEMCPY( (dst), (src), (len) );}
  159. /*
  160. ******************************************************************************
  161. * GLOBAL ENUMERATIONS
  162. ******************************************************************************
  163. */
  164. /*! Enumeration of NFC-DEP bit rate in ATR Digital 1.0 Table 93 and 94 */
  165. enum{
  166. RFAL_NFCDEP_Bx_NO_HIGH_BR = 0x00, /*!< Peer supports no high bit rates */
  167. RFAL_NFCDEP_Bx_08_848 = 0x01, /*!< Peer also supports 848 */
  168. RFAL_NFCDEP_Bx_16_1695 = 0x02, /*!< Peer also supports 1695 */
  169. RFAL_NFCDEP_Bx_32_3390 = 0x04, /*!< Peer also supports 3390 */
  170. RFAL_NFCDEP_Bx_64_6780 = 0x08 /*!< Peer also supports 6780 */
  171. };
  172. /*! Enumeration of NFC-DEP bit rate Dividor in PSL Digital 1.0 Table 100 */
  173. enum{
  174. RFAL_NFCDEP_Dx_01_106 = RFAL_BR_106, /*!< Divisor D = 1 : bit rate = 106 */
  175. RFAL_NFCDEP_Dx_02_212 = RFAL_BR_212, /*!< Divisor D = 2 : bit rate = 212 */
  176. RFAL_NFCDEP_Dx_04_424 = RFAL_BR_424, /*!< Divisor D = 4 : bit rate = 424 */
  177. RFAL_NFCDEP_Dx_08_848 = RFAL_BR_848, /*!< Divisor D = 8 : bit rate = 848 */
  178. RFAL_NFCDEP_Dx_16_1695 = RFAL_BR_1695, /*!< Divisor D = 16 : bit rate = 1695 */
  179. RFAL_NFCDEP_Dx_32_3390 = RFAL_BR_3390, /*!< Divisor D = 32 : bit rate = 3390 */
  180. RFAL_NFCDEP_Dx_64_6780 = RFAL_BR_6780 /*!< Divisor D = 64 : bit rate = 6780 */
  181. };
  182. /*! Enumeration of NFC-DEP Length Reduction (LR) Digital 1.0 Table 91 */
  183. enum{
  184. RFAL_NFCDEP_LR_64 = 0x00, /*!< Maximum payload size is 64 bytes */
  185. RFAL_NFCDEP_LR_128 = 0x01, /*!< Maximum payload size is 128 bytes */
  186. RFAL_NFCDEP_LR_192 = 0x02, /*!< Maximum payload size is 192 bytes */
  187. RFAL_NFCDEP_LR_254 = 0x03 /*!< Maximum payload size is 254 bytes */
  188. };
  189. /*
  190. ******************************************************************************
  191. * GLOBAL DATA TYPES
  192. ******************************************************************************
  193. */
  194. /*! NFC-DEP callback to check if upper layer has deactivation pending */
  195. typedef bool (* rfalNfcDepDeactCallback)(void);
  196. /*! Enumeration of the nfcip communication modes */
  197. typedef enum{
  198. RFAL_NFCDEP_COMM_PASSIVE, /*!< Passive communication mode */
  199. RFAL_NFCDEP_COMM_ACTIVE /*!< Active communication mode */
  200. } rfalNfcDepCommMode;
  201. /*! Enumeration of the nfcip roles */
  202. typedef enum{
  203. RFAL_NFCDEP_ROLE_INITIATOR, /*!< Perform as Initiator */
  204. RFAL_NFCDEP_ROLE_TARGET /*!< Perform as Target */
  205. } rfalNfcDepRole;
  206. /*! Struct that holds all NFCIP configs */
  207. typedef struct{
  208. rfalNfcDepRole role; /*!< Current NFCIP role */
  209. rfalNfcDepCommMode commMode; /*!< Current NFCIP communication mode */
  210. uint8_t oper; /*!< Operation config similar to NCI 1.0 Table 81 */
  211. uint8_t did; /*!< Current Device ID (DID) */
  212. uint8_t nad; /*!< Current Node Addressing (NAD) */
  213. uint8_t bs; /*!< Bit rate in Sending Direction */
  214. uint8_t br; /*!< Bit rate in Receiving Direction */
  215. uint8_t nfcid[RFAL_NFCDEP_NFCID3_LEN]; /*!< Pointer to the NFCID to be used */
  216. uint8_t nfcidLen; /*!< Length of the given NFCID in nfcid */
  217. uint8_t gb[RFAL_NFCDEP_GB_MAX_LEN]; /*!< Pointer General Bytes (GB) to be used */
  218. uint8_t gbLen; /*!< Length of the given GB in gb */
  219. uint8_t lr; /*!< Length Reduction (LR) to be used */
  220. uint8_t to; /*!< Timeout (TO) to be used */
  221. uint32_t fwt; /*!< Frame Waiting Time (FWT) to be used */
  222. uint32_t dFwt; /*!< Delta Frame Waiting Time (dFWT) to be used */
  223. } rfalNfcDepConfigs;
  224. /*! ATR_REQ command Digital 1.1 16.6.2 */
  225. typedef struct {
  226. uint8_t CMD1; /*!< Command format 0xD4 */
  227. uint8_t CMD2; /*!< Command Value */
  228. uint8_t NFCID3[RFAL_NFCDEP_NFCID3_LEN]; /*!< NFCID3 value */
  229. uint8_t DID; /*!< DID */
  230. uint8_t BSi; /*!< Sending Bitrate for Initiator */
  231. uint8_t BRi; /*!< Receiving Bitrate for Initiator */
  232. uint8_t PPi; /*!< Optional Parameters presence indicator */
  233. uint8_t GBi[RFAL_NFCDEP_GB_MAX_LEN]; /*!< General Bytes */
  234. } rfalNfcDepAtrReq;
  235. /*! ATR_RES response Digital 1.1 16.6.3 */
  236. typedef struct {
  237. uint8_t CMD1; /*!< Response Byte 0xD5 */
  238. uint8_t CMD2; /*!< Command Value */
  239. uint8_t NFCID3[RFAL_NFCDEP_NFCID3_LEN]; /*!< NFCID3 value */
  240. uint8_t DID; /*!< DID */
  241. uint8_t BSt; /*!< Sending Bitrate for Initiator */
  242. uint8_t BRt; /*!< Receiving Bitrate for Initiator */
  243. uint8_t TO; /*!< Timeout */
  244. uint8_t PPt; /*!< Optional Parameters presence indicator */
  245. uint8_t GBt[RFAL_NFCDEP_GB_MAX_LEN]; /*!< General Bytes */
  246. } rfalNfcDepAtrRes;
  247. /*! Structure of transmit I-PDU Buffer format from caller */
  248. typedef struct
  249. {
  250. uint8_t prologue[RFAL_NFCDEP_DEPREQ_HEADER_LEN]; /*!< Prologue space for NFC-DEP header*/
  251. uint8_t inf[RFAL_FEATURE_NFC_DEP_BLOCK_MAX_LEN]; /*!< INF | Data area of the buffer */
  252. } rfalNfcDepBufFormat;
  253. /*! Structure of APDU Buffer format from caller */
  254. typedef struct
  255. {
  256. uint8_t prologue[RFAL_NFCDEP_DEPREQ_HEADER_LEN]; /*!< Prologue/SoD buffer */
  257. uint8_t pdu[RFAL_FEATURE_NFC_DEP_PDU_MAX_LEN]; /*!< Complete PDU/Payload buffer */
  258. } rfalNfcDepPduBufFormat;
  259. /*! Activation info as Initiator and Target */
  260. typedef union { /* PRQA S 0750 # MISRA 19.2 - Both members of the union will not be used concurrently , device is only initiatior or target a time. No problem can occur. */
  261. struct {
  262. rfalNfcDepAtrRes ATR_RES; /*!< ATR RES (Initiator mode) */
  263. uint8_t ATR_RESLen; /*!< ATR RES length (Initiator mode) */
  264. }Target; /*!< Target */
  265. struct {
  266. rfalNfcDepAtrReq ATR_REQ; /*!< ATR REQ (Target mode) */
  267. uint8_t ATR_REQLen; /*!< ATR REQ length (Target mode) */
  268. }Initiator; /*!< Initiator */
  269. } rfalNfcDepActivation;
  270. /*! NFC-DEP device Info */
  271. typedef struct {
  272. uint8_t GBLen; /*!< General Bytes length */
  273. uint8_t WT; /*!< WT to be used (ignored in Listen Mode) */
  274. uint32_t FWT; /*!< FWT to be used (1/fc)(ignored Listen Mode) */
  275. uint32_t dFWT; /*!< Delta FWT to be used (1/fc) */
  276. uint8_t LR; /*!< Length Reduction coding the max payload */
  277. uint16_t FS; /*!< Frame Size */
  278. rfalBitRate DSI; /*!< Bit Rate coding from Initiator to Target */
  279. rfalBitRate DRI; /*!< Bit Rate coding from Target to Initiator */
  280. uint8_t DID; /*!< Device ID (RFAL_NFCDEP_DID_NO if no DID) */
  281. uint8_t NAD; /*!< Node ADdress (RFAL_NFCDEP_NAD_NO if no NAD)*/
  282. } rfalNfcDepInfo;
  283. /*! NFC-DEP Device structure */
  284. typedef struct {
  285. rfalNfcDepActivation activation; /*!< Activation Info */
  286. rfalNfcDepInfo info; /*!< NFC-DEP device Info */
  287. } rfalNfcDepDevice;
  288. /*! NFCIP Protocol structure for P2P Target
  289. *
  290. * operParam : derives from NFC-Forum NCI NFC-DEP Operation Parameter
  291. * NCI 1.1 Table 86: NFC-DEP Operation Parameter
  292. * and it's a bit mask composed as:
  293. * [ 0000b
  294. * | Chain SHALL use max. Transport Data Byte[1b]
  295. * | I-PDU with no Transport Data SHALL NOT be sent [1b]
  296. * | NFC-DEP Target SHALL NOT send RTOX request [1b]
  297. * ]
  298. *
  299. */
  300. typedef struct{
  301. rfalNfcDepCommMode commMode; /*!< Initiator in Active P2P or Passive P2P*/
  302. uint8_t operParam; /*!< NFC-DEP Operation Parameter */
  303. uint8_t* nfcid; /*!< Initiator's NFCID2 or NFCID3 */
  304. uint8_t nfcidLen; /*!< Initiator's NFCID length (NFCID2/3) */
  305. uint8_t DID; /*!< Initiator's Device ID DID */
  306. uint8_t NAD; /*!< Initiator's Node ID NAD */
  307. uint8_t BS; /*!< Initiator's Bit Rates supported in Tx */
  308. uint8_t BR; /*!< Initiator's Bit Rates supported in Rx */
  309. uint8_t LR; /*!< Initiator's Length reduction */
  310. uint8_t* GB; /*!< Initiator's General Bytes (Gi) */
  311. uint8_t GBLen; /*!< Initiator's General Bytes length */
  312. } rfalNfcDepAtrParam;
  313. /*! Structure of parameters to be passed in for nfcDepListenStartActivation */
  314. typedef struct
  315. {
  316. rfalNfcDepBufFormat *rxBuf; /*!< Receive Buffer struct reference */
  317. uint16_t *rxLen; /*!< Receive INF data length in bytes */
  318. bool *isRxChaining; /*!< Received data is not complete */
  319. rfalNfcDepDevice *nfcDepDev; /*!< NFC-DEP device info */
  320. } rfalNfcDepListenActvParam;
  321. /*! NFCIP Protocol structure for P2P Target
  322. *
  323. * operParam : derives from NFC-Forum NCI NFC-DEP Operation Parameter
  324. * NCI 1.1 Table 86: NFC-DEP Operation Parameter
  325. * and it's a bit mask composed as:
  326. * [ 0000b
  327. * | Chain SHALL use max. Transport Data Byte[1b]
  328. * | I-PDU with no Transport Data SHALL NOT be sent [1b]
  329. * | NFC-DEP Target SHALL NOT send RTOX request [1b]
  330. * ]
  331. *
  332. */
  333. typedef struct{
  334. rfalNfcDepCommMode commMode; /*!< Target in Active P2P or Passive P2P */
  335. uint8_t nfcid3[RFAL_NFCDEP_NFCID3_LEN]; /*!< Target's NFCID3 */
  336. uint8_t bst; /*!< Target's Bit Rates supported in Tx */
  337. uint8_t brt; /*!< Target's Bit Rates supported in Rx */
  338. uint8_t to; /*!< Target's timeout (TO) value */
  339. uint8_t ppt; /*!< Target's Presence optional Params(PPt)*/
  340. uint8_t GBt[RFAL_NFCDEP_GB_MAX_LEN]; /*!< Target's General Bytes (Gt) */
  341. uint8_t GBtLen; /*!< Target's General Bytes length */
  342. uint8_t operParam; /*!< NFC-DEP Operation Parameter */
  343. } rfalNfcDepTargetParam;
  344. /*! Structure of parameters to be passed in for nfcDepStartIpduTransceive */
  345. typedef struct
  346. {
  347. rfalNfcDepBufFormat *txBuf; /*!< Transmit Buffer struct reference */
  348. uint16_t txBufLen; /*!< Transmit Buffer INF field length in bytes */
  349. bool isTxChaining; /*!< Transmit data is not complete */
  350. rfalNfcDepBufFormat *rxBuf; /*!< Receive Buffer struct reference */
  351. uint16_t *rxLen; /*!< Receive INF data length */
  352. bool *isRxChaining; /*!< Received data is not complete */
  353. uint32_t FWT; /*!< FWT to be used (ignored in Listen Mode) */
  354. uint32_t dFWT; /*!< Delta FWT to be used */
  355. uint16_t FSx; /*!< Other device Frame Size (FSD or FSC) */
  356. uint8_t DID; /*!< Device ID (RFAL_ISODEP_NO_DID if no DID) */
  357. } rfalNfcDepTxRxParam;
  358. /*! Structure of parameters used on NFC DEP PDU Transceive */
  359. typedef struct
  360. {
  361. rfalNfcDepPduBufFormat *txBuf; /*!< Transmit Buffer struct reference */
  362. uint16_t txBufLen; /*!< Transmit Buffer INF field length in Bytes*/
  363. rfalNfcDepPduBufFormat *rxBuf; /*!< Receive Buffer struct reference in Bytes */
  364. uint16_t *rxLen; /*!< Received INF data length in Bytes */
  365. rfalNfcDepBufFormat *tmpBuf; /*!< Temp buffer for single PDUs (internal) */
  366. uint32_t FWT; /*!< FWT to be used (ignored in Listen Mode) */
  367. uint32_t dFWT; /*!< Delta FWT to be used */
  368. uint16_t FSx; /*!< Other device Frame Size (FSD or FSC) */
  369. uint8_t DID; /*!< Device ID (RFAL_ISODEP_NO_DID if no DID) */
  370. } rfalNfcDepPduTxRxParam;
  371. /*
  372. * *****************************************************************************
  373. * GLOBAL VARIABLE DECLARATIONS
  374. ******************************************************************************
  375. */
  376. /*
  377. ******************************************************************************
  378. * GLOBAL FUNCTION PROTOTYPES
  379. ******************************************************************************
  380. */
  381. /*!
  382. ******************************************************************************
  383. * \brief NFCIP Initialize
  384. *
  385. * This method resets all NFC-DEP inner states, counters and context and sets
  386. * default values
  387. *
  388. ******************************************************************************
  389. */
  390. void rfalNfcDepInitialize( void );
  391. /*!
  392. ******************************************************************************
  393. * \brief Set deactivating callback
  394. *
  395. * Sets the deactivating callback so that nfcip layer can check if upper layer
  396. * has a deactivation pending, and not perform error recovery upon specific
  397. * errors
  398. *
  399. * \param[in] pFunc : method pointer to deactivation flag check
  400. ******************************************************************************
  401. */
  402. void rfalNfcDepSetDeactivatingCallback( rfalNfcDepDeactCallback pFunc );
  403. /*!
  404. ******************************************************************************
  405. * \brief Calculate Response Waiting Time
  406. *
  407. * Calculates the Response Waiting Time (RWT) from the given Waiting Time (WT)
  408. *
  409. * \param[in] wt : the WT value to calculate RWT
  410. *
  411. * \return RWT value in 1/fc
  412. ******************************************************************************
  413. */
  414. uint32_t rfalNfcDepCalculateRWT( uint8_t wt );
  415. /*!
  416. ******************************************************************************
  417. * \brief NFC-DEP Initiator ATR (Attribute Request)
  418. *
  419. * This method configures the NFC-DEP layer with given parameters and then
  420. * sends an ATR to the Target with and checks for a valid response response
  421. *
  422. * \param[in] param : parameters to initialize and compose the ATR
  423. * \param[out] atrRes : location to store the ATR_RES
  424. * \param[out] atrResLen : length of the ATR_RES received
  425. *
  426. * \return ERR_NONE : No error
  427. * \return ERR_TIMEOUT : Timeout occurred
  428. * \return ERR_PROTO : Protocol error occurred
  429. ******************************************************************************
  430. */
  431. ReturnCode rfalNfcDepATR( const rfalNfcDepAtrParam* param, rfalNfcDepAtrRes *atrRes, uint8_t* atrResLen );
  432. /*!
  433. ******************************************************************************
  434. * \brief NFC-DEP Initiator PSL (Parameter Selection)
  435. *
  436. * This method sends a PSL to the Target with the given parameters and checks
  437. * for a valid response response
  438. *
  439. * The parameters must be coded according to Digital 1.1 16.7.1
  440. *
  441. * \param[in] BRS : the selected Bit Rates for Initiator and Target
  442. * \param[in] FSL : the maximum length of Commands and Responses
  443. *
  444. * \return ERR_NONE : No error
  445. * \return ERR_TIMEOUT : Timeout occurred
  446. * \return ERR_PROTO : Protocol error occurred
  447. ******************************************************************************
  448. */
  449. ReturnCode rfalNfcDepPSL( uint8_t BRS, uint8_t FSL );
  450. /*!
  451. ******************************************************************************
  452. * \brief NFC-DEP Initiator DSL (Deselect)
  453. *
  454. * This method checks if the NFCIP module is configured as initiator and if
  455. * so sends a DSL REQ, waits the target's response and checks it
  456. *
  457. * In case of performing as target no action is taken
  458. *
  459. * \return ERR_NONE : No error
  460. * \return ERR_TIMEOUT : Timeout occurred
  461. * \return ERR_MAX_RERUNS : Timeout occurred
  462. * \return ERR_PROTO : Protocol error occurred
  463. ******************************************************************************
  464. */
  465. ReturnCode rfalNfcDepDSL( void );
  466. /*!
  467. ******************************************************************************
  468. * \brief NFC-DEP Initiator RLS (Release)
  469. *
  470. * This method checks if the NFCIP module is configured as initiator and if
  471. * so sends a RLS REQ, waits target's response and checks it
  472. *
  473. * In case of performing as target no action is taken
  474. *
  475. * \return ERR_NONE : No error
  476. * \return ERR_TIMEOUT : Timeout occurred
  477. * \return ERR_MAX_RERUNS : Timeout occurred
  478. * \return ERR_PROTO : Protocol error occurred
  479. ******************************************************************************
  480. */
  481. ReturnCode rfalNfcDepRLS( void );
  482. /*!
  483. *****************************************************************************
  484. * \brief NFC-DEP Initiator Handle Activation
  485. *
  486. * This performs a Activation into NFC-DEP layer with the given
  487. * parameters. It sends ATR_REQ and if the higher bit rates are supported by
  488. * both devices it additionally sends PSL
  489. * Once Activated all details of the device are provided on nfcDepDev
  490. *
  491. * \param[in] param : required parameters to initialize and send ATR_REQ
  492. * \param[in] desiredBR : Desired bit rate supported by the Poller
  493. * \param[out] nfcDepDev : NFC-DEP information of the activated Listen device
  494. *
  495. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  496. * \return ERR_PARAM : Invalid parameters
  497. * \return ERR_IO : Generic internal error
  498. * \return ERR_TIMEOUT : Timeout error
  499. * \return ERR_PAR : Parity error detected
  500. * \return ERR_CRC : CRC error detected
  501. * \return ERR_FRAMING : Framing error detected
  502. * \return ERR_PROTO : Protocol error detected
  503. * \return ERR_NONE : No error, activation successful
  504. *****************************************************************************
  505. */
  506. ReturnCode rfalNfcDepInitiatorHandleActivation( rfalNfcDepAtrParam* param, rfalBitRate desiredBR, rfalNfcDepDevice* nfcDepDev );
  507. /*!
  508. ******************************************************************************
  509. * \brief Check if buffer contains valid ATR_REQ
  510. *
  511. * This method checks if the given ATR_REQ is valid
  512. *
  513. *
  514. * \param[in] buf : buffer holding Initiator's received request
  515. * \param[in] bufLen : size of the msg contained on the buf in Bytes
  516. * \param[out] nfcid3 : pointer to where the NFCID3 may be outputed,
  517. * nfcid3 has NFCF_SENSF_NFCID3_LEN as length
  518. * Pass NULL if output parameter not desired
  519. *
  520. * \return true : Valid ATR_REQ received, the ATR_RES has been computed in txBuf
  521. * \return false : Invalid protocol request
  522. *
  523. ******************************************************************************
  524. */
  525. bool rfalNfcDepIsAtrReq( const uint8_t* buf, uint16_t bufLen, uint8_t* nfcid3 );
  526. /*!
  527. ******************************************************************************
  528. * \brief Check is Target has received ATR
  529. *
  530. * This method checks if the NFCIP module is configured as target and if a
  531. * ATR REQ has been received ( whether is in activation or in data exchange)
  532. *
  533. * \return true : a ATR has already been received
  534. * \return false : no ATR has been received
  535. ******************************************************************************
  536. */
  537. bool rfalNfcDepTargetRcvdATR( void );
  538. /*!
  539. *****************************************************************************
  540. * \brief NFCDEP Start Listen Activation Handling
  541. *
  542. * Start Activation Handling and setup to receive first frame which may
  543. * contain complete or partial DEP-REQ after activation is completed
  544. *
  545. * Pass in ATR_REQ for NFC-DEP to handle ATR_RES. The Activation Handling
  546. * handles ATR_RES and PSL_RES if a PSL_REQ is received
  547. *
  548. * Activation is completed if PSL_RES is sent or if first I-PDU is received
  549. *
  550. * \ref rfalNfcDepListenGetActivationStatus() provide status of the
  551. * ongoing activation
  552. *
  553. * \warning nfcDepGetTransceiveStatus() shall be called right after activation
  554. * is completed (i.e. rfalNfcDepListenGetActivationStatus() return ERR_NONE)
  555. * to check for first received frame.
  556. *
  557. * \param[in] param : Target parameters to be used
  558. * \param[in] atrReq : reference to buffer containing ATR_REQ
  559. * \param[in] atrReqLength: Length of ATR_REQ
  560. * \param[out] rxParam : references to buffer, length and chaining indication
  561. * for first complete LLCP to be received
  562. *
  563. * \return ERR_NONE : ATR_REQ is valid and activation ongoing
  564. * \return ERR_PARAM : ATR_REQ or other params are invalid
  565. * \return ERR_LINK_LOSS : Remote Field is turned off
  566. *****************************************************************************
  567. */
  568. ReturnCode rfalNfcDepListenStartActivation( const rfalNfcDepTargetParam *param, const uint8_t *atrReq, uint16_t atrReqLength, rfalNfcDepListenActvParam rxParam );
  569. /*!
  570. *****************************************************************************
  571. * \brief Get the current NFC-DEP Activation Status
  572. *
  573. * \return ERR_NONE : Activation has completed successfully
  574. * \return ERR_BUSY : Activation is ongoing
  575. * \return ERR_LINK_LOSS : Remote Field was turned off
  576. *****************************************************************************
  577. */
  578. ReturnCode rfalNfcDepListenGetActivationStatus( void );
  579. /*!
  580. *****************************************************************************
  581. * \brief Start Transceive
  582. *
  583. * Transceives a complete or partial DEP block
  584. *
  585. * The txBuf contains complete or partial of DEP to be transmitted.
  586. * The Prologue field of the I-PDU is handled internally
  587. *
  588. * If the buffer contains partial LLCP and is not the last block, then
  589. * isTxChaining must be set to true
  590. *
  591. * \param[in] param: reference parameters to be used for the Transceive
  592. *
  593. * \return ERR_PARAM : Bad request
  594. * \return ERR_WRONG_STATE : The module is not in a proper state
  595. * \return ERR_NONE : The Transceive request has been started
  596. *****************************************************************************
  597. */
  598. ReturnCode rfalNfcDepStartTransceive( const rfalNfcDepTxRxParam *param );
  599. /*!
  600. *****************************************************************************
  601. * \brief Return the Transceive status
  602. *
  603. * Returns the status of the NFC-DEP Transceive
  604. *
  605. * \warning When the other device is performing chaining once a chained
  606. * block is received the error ERR_AGAIN is sent. At this point
  607. * caller must handle the received data immediately.
  608. * When ERR_AGAIN is returned an ACK has already been sent to
  609. * the other device and the next block might be incoming.
  610. * If rfalWorker() is called frequently it will place the next
  611. * block on the given buffer
  612. *
  613. * \return ERR_NONE : Transceive has been completed successfully
  614. * \return ERR_BUSY : Transceive is ongoing
  615. * \return ERR_PROTO : Protocol error occurred
  616. * \return ERR_TIMEOUT : Timeout error occurred
  617. * \return ERR_SLEEP_REQ : Deselect has been received and responded
  618. * \return ERR_NOMEM : The received I-PDU does not fit into the
  619. * receive buffer
  620. * \return ERR_LINK_LOSS : Communication is lost because Reader/Writer
  621. * has turned off its field
  622. * \return ERR_AGAIN : received one chaining block, continue to call
  623. * this method to retrieve the remaining blocks
  624. *****************************************************************************
  625. */
  626. ReturnCode rfalNfcDepGetTransceiveStatus( void );
  627. /*!
  628. *****************************************************************************
  629. * \brief Start PDU Transceive
  630. *
  631. * This method triggers a NFC-DEP Transceive containing a complete PDU
  632. * It transmits the given message and handles all protocol retransmitions,
  633. * error handling and control messages
  634. *
  635. * The txBuf contains a complete PDU to be transmitted
  636. * The Prologue field will be manipulated by the Transceive
  637. *
  638. * \warning the txBuf will be modified during the transmission
  639. * \warning the maximum RF frame which can be received is limited by param.tmpBuf
  640. *
  641. * \param[in] param: reference parameters to be used for the Transceive
  642. *
  643. * \return ERR_PARAM : Bad request
  644. * \return ERR_WRONG_STATE : The module is not in a proper state
  645. * \return ERR_NONE : The Transceive request has been started
  646. *****************************************************************************
  647. */
  648. ReturnCode rfalNfcDepStartPduTransceive( rfalNfcDepPduTxRxParam param );
  649. /*!
  650. *****************************************************************************
  651. * \brief Return the PSU Transceive status
  652. *
  653. * Returns the status of the NFC-DEP PDU Transceive
  654. *
  655. *
  656. * \return ERR_NONE : Transceive has been completed successfully
  657. * \return ERR_BUSY : Transceive is ongoing
  658. * \return ERR_PROTO : Protocol error occurred
  659. * \return ERR_TIMEOUT : Timeout error occurred
  660. * \return ERR_SLEEP_REQ : Deselect has been received and responded
  661. * \return ERR_NOMEM : The received I-PDU does not fit into the
  662. * receive buffer
  663. * \return ERR_LINK_LOSS : Communication is lost because Reader/Writer
  664. * has turned off its field
  665. *****************************************************************************
  666. */
  667. ReturnCode rfalNfcDepGetPduTransceiveStatus( void );
  668. #endif /* RFAL_NFCDEP_H_ */
  669. /**
  670. * @}
  671. *
  672. * @}
  673. *
  674. * @}
  675. */