rfal_nfcDep.h 36 KB

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