rfal_nfcDep.h 36 KB

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