rfal_nfc.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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_nfc.h
  27. *
  28. * \brief RFAL NFC device
  29. *
  30. * This module provides the required features to behave as an NFC Poller
  31. * or Listener device. It grants an easy to use interface for the following
  32. * activities: Technology Detection, Collision Resolution, Activation,
  33. * Data Exchange, and Deactivation
  34. *
  35. * This layer is influenced by (but not fully aligned with) the NFC Forum
  36. * specifications, in particular: Activity 2.0 and NCI 2.0
  37. *
  38. *
  39. *
  40. * \addtogroup RFAL
  41. * @{
  42. *
  43. * \addtogroup RFAL-HL
  44. * \brief RFAL Higher Layer
  45. * @{
  46. *
  47. * \addtogroup NFC
  48. * \brief RFAL NFC Device
  49. * @{
  50. *
  51. */
  52. #ifndef RFAL_NFC_H
  53. #define RFAL_NFC_H
  54. /*
  55. ******************************************************************************
  56. * INCLUDES
  57. ******************************************************************************
  58. */
  59. #include "platform.h"
  60. #include "st_errno.h"
  61. #include "rfal_rf.h"
  62. #include "rfal_nfca.h"
  63. #include "rfal_nfcb.h"
  64. #include "rfal_nfcf.h"
  65. #include "rfal_nfcv.h"
  66. #include "rfal_st25tb.h"
  67. #include "rfal_nfcDep.h"
  68. #include "rfal_isoDep.h"
  69. /*
  70. ******************************************************************************
  71. * GLOBAL DEFINES
  72. ******************************************************************************
  73. */
  74. #define RFAL_NFC_TECH_NONE 0x0000U /*!< No technology */
  75. #define RFAL_NFC_POLL_TECH_A 0x0001U /*!< NFC-A technology Flag */
  76. #define RFAL_NFC_POLL_TECH_B 0x0002U /*!< NFC-B technology Flag */
  77. #define RFAL_NFC_POLL_TECH_F 0x0004U /*!< NFC-F technology Flag */
  78. #define RFAL_NFC_POLL_TECH_V 0x0008U /*!< NFC-V technology Flag */
  79. #define RFAL_NFC_POLL_TECH_AP2P 0x0010U /*!< AP2P technology Flag */
  80. #define RFAL_NFC_POLL_TECH_ST25TB 0x0020U /*!< ST25TB technology Flag */
  81. #define RFAL_NFC_LISTEN_TECH_A 0x1000U /*!< NFC-V technology Flag */
  82. #define RFAL_NFC_LISTEN_TECH_B 0x2000U /*!< NFC-V technology Flag */
  83. #define RFAL_NFC_LISTEN_TECH_F 0x4000U /*!< NFC-V technology Flag */
  84. #define RFAL_NFC_LISTEN_TECH_AP2P 0x8000U /*!< NFC-V technology Flag */
  85. /*
  86. ******************************************************************************
  87. * GLOBAL MACROS
  88. ******************************************************************************
  89. */
  90. /*! Checks if a device is currently activated */
  91. #define rfalNfcIsDevActivated(st) \
  92. (((st) >= RFAL_NFC_STATE_ACTIVATED) && ((st) < RFAL_NFC_STATE_DEACTIVATION))
  93. /*! Checks if a device is in discovery */
  94. #define rfalNfcIsInDiscovery(st) \
  95. (((st) >= RFAL_NFC_STATE_START_DISCOVERY) && ((st) < RFAL_NFC_STATE_ACTIVATED))
  96. /*! Checks if remote device is in Poll mode */
  97. #define rfalNfcIsRemDevPoller(tp) \
  98. (((tp) >= RFAL_NFC_POLL_TYPE_NFCA) && ((tp) <= RFAL_NFC_POLL_TYPE_AP2P))
  99. /*! Checks if remote device is in Listen mode */
  100. #define rfalNfcIsRemDevListener(tp) \
  101. (((int16_t)(tp) >= (int16_t)RFAL_NFC_LISTEN_TYPE_NFCA) && ((tp) <= RFAL_NFC_LISTEN_TYPE_AP2P))
  102. /*
  103. ******************************************************************************
  104. * GLOBAL ENUMS
  105. ******************************************************************************
  106. */
  107. /*
  108. ******************************************************************************
  109. * GLOBAL TYPES
  110. ******************************************************************************
  111. */
  112. /*! Main state */
  113. typedef enum {
  114. RFAL_NFC_STATE_NOTINIT = 0, /*!< Not Initialized state */
  115. RFAL_NFC_STATE_IDLE = 1, /*!< Initialize state */
  116. RFAL_NFC_STATE_START_DISCOVERY = 2, /*!< Start Discovery loop state */
  117. RFAL_NFC_STATE_WAKEUP_MODE = 3, /*!< Wake-Up state */
  118. RFAL_NFC_STATE_POLL_TECHDETECT = 10, /*!< Technology Detection state */
  119. RFAL_NFC_STATE_POLL_COLAVOIDANCE = 11, /*!< Collision Avoidance state */
  120. RFAL_NFC_STATE_POLL_SELECT = 12, /*!< Wait for Selection state */
  121. RFAL_NFC_STATE_POLL_ACTIVATION = 13, /*!< Activation state */
  122. RFAL_NFC_STATE_LISTEN_TECHDETECT = 20, /*!< Listen Tech Detect */
  123. RFAL_NFC_STATE_LISTEN_COLAVOIDANCE = 21, /*!< Listen Collision Avoidance */
  124. RFAL_NFC_STATE_LISTEN_ACTIVATION = 22, /*!< Listen Activation state */
  125. RFAL_NFC_STATE_LISTEN_SLEEP = 23, /*!< Listen Sleep state */
  126. RFAL_NFC_STATE_ACTIVATED = 30, /*!< Activated state */
  127. RFAL_NFC_STATE_DATAEXCHANGE = 31, /*!< Data Exchange Start state */
  128. RFAL_NFC_STATE_DATAEXCHANGE_DONE = 33, /*!< Data Exchange terminated */
  129. RFAL_NFC_STATE_DEACTIVATION = 34 /*!< Deactivation state */
  130. } rfalNfcState;
  131. /*! Device type */
  132. typedef enum {
  133. RFAL_NFC_LISTEN_TYPE_NFCA = 0, /*!< NFC-A Listener device type */
  134. RFAL_NFC_LISTEN_TYPE_NFCB = 1, /*!< NFC-B Listener device type */
  135. RFAL_NFC_LISTEN_TYPE_NFCF = 2, /*!< NFC-F Listener device type */
  136. RFAL_NFC_LISTEN_TYPE_NFCV = 3, /*!< NFC-V Listener device type */
  137. RFAL_NFC_LISTEN_TYPE_ST25TB = 4, /*!< ST25TB Listener device type */
  138. RFAL_NFC_LISTEN_TYPE_AP2P = 5, /*!< AP2P Listener device type */
  139. RFAL_NFC_POLL_TYPE_NFCA = 10, /*!< NFC-A Poller device type */
  140. RFAL_NFC_POLL_TYPE_NFCB = 11, /*!< NFC-B Poller device type */
  141. RFAL_NFC_POLL_TYPE_NFCF = 12, /*!< NFC-F Poller device type */
  142. RFAL_NFC_POLL_TYPE_NFCV = 13, /*!< NFC-V Poller device type */
  143. RFAL_NFC_POLL_TYPE_AP2P = 15 /*!< AP2P Poller device type */
  144. } rfalNfcDevType;
  145. /*! Device interface */
  146. typedef enum {
  147. RFAL_NFC_INTERFACE_RF = 0, /*!< RF Frame interface */
  148. RFAL_NFC_INTERFACE_ISODEP = 1, /*!< ISO-DEP interface */
  149. RFAL_NFC_INTERFACE_NFCDEP = 2 /*!< NFC-DEP interface */
  150. } rfalNfcRfInterface;
  151. /*! Device struct containing all its details */
  152. typedef struct {
  153. rfalNfcDevType type; /*!< Device's type */
  154. union { /* PRQA S 0750 # MISRA 19.2 - Members of the union will not be used concurrently, only one technology at a time */
  155. rfalNfcaListenDevice nfca; /*!< NFC-A Listen Device instance */
  156. rfalNfcbListenDevice nfcb; /*!< NFC-B Listen Device instance */
  157. rfalNfcfListenDevice nfcf; /*!< NFC-F Listen Device instance */
  158. rfalNfcvListenDevice nfcv; /*!< NFC-V Listen Device instance */
  159. rfalSt25tbListenDevice st25tb; /*!< ST25TB Listen Device instance*/
  160. } dev; /*!< Device's instance */
  161. uint8_t* nfcid; /*!< Device's NFCID */
  162. uint8_t nfcidLen; /*!< Device's NFCID length */
  163. rfalNfcRfInterface rfInterface; /*!< Device's interface */
  164. union { /* PRQA S 0750 # MISRA 19.2 - Members of the union will not be used concurrently, only one protocol at a time */
  165. rfalIsoDepDevice isoDep; /*!< ISO-DEP instance */
  166. rfalNfcDepDevice nfcDep; /*!< NFC-DEP instance */
  167. } proto; /*!< Device's protocol */
  168. } rfalNfcDevice;
  169. /*! Discovery parameters */
  170. typedef struct {
  171. rfalComplianceMode compMode; /*!< Compliancy mode to be used */
  172. uint16_t techs2Find; /*!< Technologies to search for */
  173. uint16_t totalDuration; /*!< Duration of a whole Poll + Listen cycle */
  174. uint8_t devLimit; /*!< Max number of devices */
  175. rfalBitRate maxBR; /*!< Max Bit rate to be used for communications */
  176. rfalBitRate nfcfBR; /*!< Bit rate to poll for NFC-F */
  177. uint8_t
  178. nfcid3[RFAL_NFCDEP_NFCID3_LEN]; /*!< NFCID3 to be used on the ATR_REQ/ATR_RES */
  179. uint8_t GB[RFAL_NFCDEP_GB_MAX_LEN]; /*!< General bytes to be used on the ATR-REQ */
  180. uint8_t GBLen; /*!< Length of the General Bytes */
  181. rfalBitRate ap2pBR; /*!< Bit rate to poll for AP2P */
  182. rfalLmConfPA lmConfigPA; /*!< Configuration for Passive Listen mode NFC-A */
  183. rfalLmConfPF lmConfigPF; /*!< Configuration for Passive Listen mode NFC-A */
  184. void (*notifyCb)(rfalNfcState st); /*!< Callback to Notify upper layer */
  185. bool wakeupEnabled; /*!< Enable Wake-Up mode before polling */
  186. bool wakeupConfigDefault; /*!< Wake-Up mode default configuration */
  187. rfalWakeUpConfig wakeupConfig; /*!< Wake-Up mode configuration */
  188. bool activate_after_sak; // Set device to Active mode after SAK responce
  189. } rfalNfcDiscoverParam;
  190. /*! Buffer union, only one interface is used at a time */
  191. typedef union { /* PRQA S 0750 # MISRA 19.2 - Members of the union will not be used concurrently, only one interface at a time */
  192. uint8_t rfBuf[RFAL_FEATURE_NFC_RF_BUF_LEN]; /*!< RF buffer */
  193. rfalIsoDepApduBufFormat isoDepBuf; /*!< ISO-DEP buffer format (with header/prologue) */
  194. rfalNfcDepPduBufFormat nfcDepBuf; /*!< NFC-DEP buffer format (with header/prologue) */
  195. } rfalNfcBuffer;
  196. /*******************************************************************************/
  197. /*
  198. ******************************************************************************
  199. * GLOBAL FUNCTION PROTOTYPES
  200. ******************************************************************************
  201. */
  202. /*!
  203. *****************************************************************************
  204. * \brief RFAL NFC Worker
  205. *
  206. * It runs the internal state machine and runs the RFAL RF worker.
  207. *****************************************************************************
  208. */
  209. void rfalNfcWorker(void);
  210. /*!
  211. *****************************************************************************
  212. * \brief RFAL NFC Initialize
  213. *
  214. * It initializes this module and its dependencies
  215. *
  216. * \return ERR_WRONG_STATE : Incorrect state for this operation
  217. * \return ERR_IO : Generic internal error
  218. * \return ERR_NONE : No error
  219. *****************************************************************************
  220. */
  221. ReturnCode rfalNfcInitialize(void);
  222. /*!
  223. *****************************************************************************
  224. * \brief RFAL NFC Discovery
  225. *
  226. * It set the device in Discovery state.
  227. * In discovery it will Poll and/or Listen for the technologies configured,
  228. * and perform Wake-up mode if configured to do so.
  229. *
  230. * The device list passed on disParams must not be empty.
  231. * The number of devices on the list is indicated by the devLimit and shall
  232. * be at >= 1.
  233. *
  234. * \param[in] disParams : discovery configuration parameters
  235. *
  236. * \return ERR_WRONG_STATE : Incorrect state for this operation
  237. * \return ERR_PARAM : Invalid parameters
  238. * \return ERR_NONE : No error
  239. *****************************************************************************
  240. */
  241. ReturnCode rfalNfcDiscover(const rfalNfcDiscoverParam* disParams);
  242. /*!
  243. *****************************************************************************
  244. * \brief RFAL NFC Get State
  245. *
  246. * It returns the current state
  247. *
  248. * \return rfalNfcState : the current state
  249. *****************************************************************************
  250. */
  251. rfalNfcState rfalNfcGetState(void);
  252. /*!
  253. *****************************************************************************
  254. * \brief RFAL NFC Get Devices Found
  255. *
  256. * It returns the location of the device list and the number of
  257. * devices found.
  258. *
  259. * \param[out] devList : device list location
  260. * \param[out] devCnt : number of devices found
  261. *
  262. * \return ERR_WRONG_STATE : Incorrect state for this operation
  263. * Discovery still ongoing
  264. * \return ERR_PARAM : Invalid parameters
  265. * \return ERR_NONE : No error
  266. *****************************************************************************
  267. */
  268. ReturnCode rfalNfcGetDevicesFound(rfalNfcDevice** devList, uint8_t* devCnt);
  269. /*!
  270. *****************************************************************************
  271. * \brief RFAL NFC Get Active Device
  272. *
  273. * It returns the location of the device current Active device
  274. *
  275. * \param[out] dev : device info location
  276. *
  277. * \return ERR_WRONG_STATE : Incorrect state for this operation
  278. * No device activated
  279. * \return ERR_PARAM : Invalid parameters
  280. * \return ERR_NONE : No error
  281. *****************************************************************************
  282. */
  283. ReturnCode rfalNfcGetActiveDevice(rfalNfcDevice** dev);
  284. /*!
  285. *****************************************************************************
  286. * \brief RFAL NFC Select Device
  287. *
  288. * It selects the device to be activated.
  289. * It shall be called when more than one device has been identified to
  290. * indiacte which device shall be actived
  291. *
  292. * \param[in] devIdx : device index to be activated
  293. *
  294. * \return ERR_WRONG_STATE : Incorrect state for this operation
  295. * Not in select state
  296. * \return ERR_PARAM : Invalid parameters
  297. * \return ERR_NONE : No error
  298. *****************************************************************************
  299. */
  300. ReturnCode rfalNfcSelect(uint8_t devIdx);
  301. /*!
  302. *****************************************************************************
  303. * \brief RFAL NFC Start Data Exchange
  304. *
  305. * After a device has been activated, it starts a data exchange.
  306. * It handles automatically which interface/protocol to be used and acts accordingly.
  307. *
  308. * In Listen mode the first frame/data shall be sent by the Reader/Initiator
  309. * therefore this method must be called first with txDataLen set to zero
  310. * to retrieve the rxData and rcvLen locations.
  311. *
  312. *
  313. * \param[in] txData : data to be transmitted
  314. * \param[in] txDataLen : size of the data to be transmitted
  315. * \param[out] rxData : location of the received data after operation is completed
  316. * \param[out] rvdLen : location of thelength of the received data
  317. * \param[in] fwt : FWT to be used in case of RF interface.
  318. * If ISO-DEP or NFC-DEP interface is used, this will be ignored
  319. *
  320. * \return ERR_WRONG_STATE : Incorrect state for this operation
  321. * \return ERR_PARAM : Invalid parameters
  322. * \return ERR_NONE : No error
  323. *****************************************************************************
  324. */
  325. ReturnCode rfalNfcDataExchangeStart(
  326. uint8_t* txData,
  327. uint16_t txDataLen,
  328. uint8_t** rxData,
  329. uint16_t** rvdLen,
  330. uint32_t fwt,
  331. uint32_t tx_flag);
  332. ReturnCode rfalNfcDataExchangeCustomStart(
  333. uint8_t* txData,
  334. uint16_t txDataLen,
  335. uint8_t** rxData,
  336. uint16_t** rvdLen,
  337. uint32_t fwt,
  338. uint32_t flags);
  339. /*!
  340. *****************************************************************************
  341. * \brief RFAL NFC Get Data Exchange Status
  342. *
  343. * Gets current Data Exchange status
  344. *
  345. * \return ERR_NONE : Transceive done with no error
  346. * \return ERR_BUSY : Transceive ongoing
  347. * \return ERR_AGAIN : received one chaining block, copy received data
  348. * and continue to call this method to retrieve the
  349. * remaining blocks
  350. * \return ERR_XXXX : Error occurred
  351. * \return ERR_TIMEOUT : No response
  352. * \return ERR_FRAMING : Framing error detected
  353. * \return ERR_PAR : Parity error detected
  354. * \return ERR_CRC : CRC error detected
  355. * \return ERR_LINK_LOSS : Link Loss - External Field is Off
  356. * \return ERR_RF_COLLISION : Collision detected
  357. * \return ERR_IO : Internal error
  358. *****************************************************************************
  359. */
  360. ReturnCode rfalNfcDataExchangeGetStatus(void);
  361. /*!
  362. *****************************************************************************
  363. * \brief RFAL NFC Deactivate
  364. *
  365. * It triggers the deactivation procedure to terminate communications with
  366. * remote device. At the end the field will be turned off.
  367. *
  368. * \param[in] discovery : TRUE if after deactivation go back into discovery
  369. * : FALSE if after deactivation remain in idle
  370. *
  371. * \return ERR_WRONG_STATE : Incorrect state for this operation
  372. * \return ERR_NONE : No error
  373. *****************************************************************************
  374. */
  375. ReturnCode rfalNfcDeactivate(bool discovery);
  376. #endif /* RFAL_NFC_H */
  377. /**
  378. * @}
  379. *
  380. * @}
  381. *
  382. * @}
  383. */