mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-07-30 02:18:11 -07:00
956788c09b
* nfc: clean up scenes * nfc worker: remove field on from worker * nfc worker: move full data exchange to furi hal * nfc_device: check UID length * nfc protocol: introduce mifare common API * nfc: move common data to furi hal nfc * nfc: rename emv_decoder -> emv * nfc: move emv data structure to emv lib * nfc: remove deactivate after detection * nfc: rework furi hal nfc detect * nfc: clean up CLI commands and type * nfc: remove unused includes and function * nfc: add TxRxType enum * nfc: read mifare ultralight refactoring * nfc: refactore mifare ultralight start * rfal: fix custom data exchange * nfc: refactor read bank card * nfc: refactor read emv application * nfc: refactor emv test emulation * nfc: refactor uid emulation * nfc: add limit to uid emulation logger * fix source formatting * furi_hal_nfc: fix data exchange full * nfc: fix mifare ultralight type load Co-authored-by: あく <alleteam@gmail.com>
3033 lines
122 KiB
C
3033 lines
122 KiB
C
|
|
/******************************************************************************
|
|
* \attention
|
|
*
|
|
* <h2><center>© COPYRIGHT 2020 STMicroelectronics</center></h2>
|
|
*
|
|
* Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
|
|
* You may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at:
|
|
*
|
|
* www.st.com/myliberty
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
|
|
* AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*
|
|
******************************************************************************/
|
|
|
|
/*
|
|
* PROJECT: NFCC firmware
|
|
* LANGUAGE: ISO C99
|
|
*/
|
|
|
|
/*! \file rfal_isoDep.c
|
|
*
|
|
* \author Gustavo Patricio
|
|
*
|
|
* \brief Implementation of ISO-DEP protocol
|
|
*
|
|
* This implementation was based on the following specs:
|
|
* - ISO/IEC 14443-4 2nd Edition 2008-07-15
|
|
* - NFC Forum Digital Protocol 1.1 2014-01-14
|
|
*
|
|
*/
|
|
|
|
/*
|
|
******************************************************************************
|
|
* INCLUDES
|
|
******************************************************************************
|
|
*/
|
|
|
|
#include "rfal_isoDep.h"
|
|
#include "rfal_rf.h"
|
|
#include "utils.h"
|
|
|
|
/*
|
|
******************************************************************************
|
|
* ENABLE SWITCH
|
|
******************************************************************************
|
|
*/
|
|
|
|
#if RFAL_FEATURE_ISO_DEP
|
|
|
|
#if(!RFAL_FEATURE_ISO_DEP_POLL && !RFAL_FEATURE_ISO_DEP_LISTEN)
|
|
#error \
|
|
" RFAL: Invalid ISO-DEP Configuration. Please select at least one mode: Poller and/or Listener. "
|
|
#endif
|
|
|
|
/* Check for valid I-Block length [RFAL_ISODEP_FSX_16 ; RFAL_ISODEP_FSX_4096]*/
|
|
#if((RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN > 4096) || (RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN < 16))
|
|
#error \
|
|
" RFAL: Invalid ISO-DEP IBlock Max length. Please change RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN. "
|
|
#endif
|
|
|
|
/* Check for valid APDU length. */
|
|
#if((RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN < RFAL_FEATURE_ISO_DEP_IBLOCK_MAX_LEN))
|
|
#error " RFAL: Invalid ISO-DEP APDU Max length. Please change RFAL_FEATURE_ISO_DEP_APDU_MAX_LEN. "
|
|
#endif
|
|
|
|
/*
|
|
******************************************************************************
|
|
* DEFINES
|
|
******************************************************************************
|
|
*/
|
|
#define ISODEP_CRC_LEN RFAL_CRC_LEN /*!< ISO1443 CRC Length */
|
|
|
|
#define ISODEP_PCB_POS (0U) /*!< PCB position on message header*/
|
|
#define ISODEP_SWTX_INF_POS (1U) /*!< INF position in a S-WTX */
|
|
|
|
#define ISODEP_DID_POS (1U) /*!< DID position on message header*/
|
|
#define ISODEP_SWTX_PARAM_LEN (1U) /*!< SWTX parameter length */
|
|
|
|
#define ISODEP_DSL_MAX_LEN \
|
|
(RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN) /*!< Deselect Req/Res length */
|
|
|
|
#define ISODEP_PCB_xBLOCK_MASK (0xC0U) /*!< Bit mask for Block type */
|
|
#define ISODEP_PCB_IBLOCK (0x00U) /*!< Bit mask indicating a I-Block */
|
|
#define ISODEP_PCB_RBLOCK (0x80U) /*!< Bit mask indicating a R-Block */
|
|
#define ISODEP_PCB_SBLOCK (0xC0U) /*!< Bit mask indicating a S-Block */
|
|
#define ISODEP_PCB_INVALID (0x40U) /*!< Bit mask of an Invalid PCB */
|
|
|
|
#define ISODEP_HDR_MAX_LEN \
|
|
(RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN + \
|
|
RFAL_ISODEP_NAD_LEN) /*!< Max header length (PCB + DID + NAD) */
|
|
|
|
#define ISODEP_PCB_IB_VALID_MASK \
|
|
(ISODEP_PCB_B6_BIT | ISODEP_PCB_B2_BIT) /*!< Bit mask for the MUST bits on I-Block */
|
|
#define ISODEP_PCB_IB_VALID_VAL \
|
|
(ISODEP_PCB_B2_BIT) /*!< Value for the MUST bits on I-Block */
|
|
#define ISODEP_PCB_RB_VALID_MASK \
|
|
(ISODEP_PCB_B6_BIT | ISODEP_PCB_B3_BIT | \
|
|
ISODEP_PCB_B2_BIT) /*!< Bit mask for the MUST bits on R-Block */
|
|
#define ISODEP_PCB_RB_VALID_VAL \
|
|
(ISODEP_PCB_B6_BIT | ISODEP_PCB_B2_BIT) /*!< Value for the MUST bits on R-Block */
|
|
#define ISODEP_PCB_SB_VALID_MASK \
|
|
(ISODEP_PCB_B3_BIT | ISODEP_PCB_B2_BIT | \
|
|
ISODEP_PCB_B1_BIT) /*!< Bit mask for the MUST bits on I-Block */
|
|
#define ISODEP_PCB_SB_VALID_VAL \
|
|
(ISODEP_PCB_B2_BIT) /*!< Value for the MUST bits on I-Block */
|
|
|
|
#define ISODEP_PCB_B1_BIT \
|
|
(0x01U) /*!< Bit mask for the RFU S Blocks */
|
|
#define ISODEP_PCB_B2_BIT \
|
|
(0x02U) /*!< Bit mask for the RFU bit2 in I,S,R Blocks */
|
|
#define ISODEP_PCB_B3_BIT \
|
|
(0x04U) /*!< Bit mask for the RFU bit3 in R Blocks */
|
|
#define ISODEP_PCB_B6_BIT \
|
|
(0x20U) /*!< Bit mask for the RFU bit2 in R Blocks */
|
|
#define ISODEP_PCB_CHAINING_BIT \
|
|
(0x10U) /*!< Bit mask for the chaining bit of an ISO DEP I-Block in PCB. */
|
|
#define ISODEP_PCB_DID_BIT \
|
|
(0x08U) /*!< Bit mask for the DID presence bit of an ISO DEP I,S,R Blocks PCB. */
|
|
#define ISODEP_PCB_NAD_BIT \
|
|
(0x04U) /*!< Bit mask for the NAD presence bit of an ISO DEP I,S,R Blocks in PCB */
|
|
#define ISODEP_PCB_BN_MASK \
|
|
(0x01U) /*!< Bit mask for the block number of an ISO DEP I,R Block in PCB */
|
|
|
|
#define ISODEP_SWTX_PL_MASK \
|
|
(0xC0U) /*!< Bit mask for the Power Level bits of the inf byte of an WTX request or response */
|
|
#define ISODEP_SWTX_WTXM_MASK \
|
|
(0x3FU) /*!< Bit mask for the WTXM bits of the inf byte of an WTX request or response */
|
|
|
|
#define ISODEP_RBLOCK_INF_LEN (0U) /*!< INF length of R-Block Digital 1.1 15.1.3 */
|
|
#define ISODEP_SDSL_INF_LEN (0U) /*!< INF length of S(DSL) Digital 1.1 15.1.3 */
|
|
#define ISODEP_SWTX_INF_LEN (1U) /*!< INF length of S(WTX) Digital 1.1 15.2.2 */
|
|
|
|
#define ISODEP_WTXM_MIN (1U) /*!< Minimum allowed value for the WTXM, Digital 1.0 13.2.2 */
|
|
#define ISODEP_WTXM_MAX (59U) /*!< Maximum allowed value for the WTXM, Digital 1.0 13.2.2 */
|
|
|
|
#define ISODEP_PCB_Sxx_MASK (0x30U) /*!< Bit mask for the S-Block type */
|
|
#define ISODEP_PCB_DESELECT (0x00U) /*!< Bit mask for S-Block indicating Deselect */
|
|
#define ISODEP_PCB_WTX (0x30U) /*!< Bit mask for S-Block indicating Waiting Time eXtension */
|
|
|
|
#define ISODEP_PCB_Rx_MASK (0x10U) /*!< Bit mask for the R-Block type */
|
|
#define ISODEP_PCB_ACK (0x00U) /*!< Bit mask for R-Block indicating ACK */
|
|
#define ISODEP_PCB_NAK (0x10U) /*!< Bit mask for R-Block indicating NAK */
|
|
|
|
/*! Maximum length of control message (no INF) */
|
|
#define ISODEP_CONTROLMSG_BUF_LEN \
|
|
(RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN + RFAL_ISODEP_NAD_LEN + ISODEP_SWTX_PARAM_LEN)
|
|
|
|
#define ISODEP_FWT_DEACTIVATION \
|
|
(71680U) /*!< FWT used for DESELECT Digital 2.2 B10 ISO1444-4 7.2 & 8.1 */
|
|
#define ISODEP_MAX_RERUNS (0x0FFFFFFFU) /*!< Maximum rerun retrys for a blocking protocol run*/
|
|
|
|
#define ISODEP_PCBSBLOCK \
|
|
(0x00U | ISODEP_PCB_SBLOCK | ISODEP_PCB_B2_BIT) /*!< PCB Value of a S-Block */
|
|
#define ISODEP_PCB_SDSL \
|
|
(ISODEP_PCBSBLOCK | ISODEP_PCB_DESELECT) /*!< PCB Value of a S-Block with DESELECT */
|
|
#define ISODEP_PCB_SWTX \
|
|
(ISODEP_PCBSBLOCK | ISODEP_PCB_WTX) /*!< PCB Value of a S-Block with WTX */
|
|
#define ISODEP_PCB_SPARAMETERS \
|
|
(ISODEP_PCB_SBLOCK | ISODEP_PCB_WTX) /*!< PCB Value of a S-Block with PARAMETERS */
|
|
|
|
#define ISODEP_FWI_LIS_MAX_NFC \
|
|
8U /*!< FWT Listener Max FWIT4ATmax FWIBmax Digital 1.1 A6 & A3 */
|
|
#define ISODEP_FWI_LIS_MAX_EMVCO \
|
|
7U /*!< FWT Listener Max FWIMAX EMVCo 2.6 A.5 */
|
|
#define ISODEP_FWI_LIS_MAX \
|
|
(uint8_t)( \
|
|
(gIsoDep.compMode == RFAL_COMPLIANCE_MODE_EMV) ? \
|
|
ISODEP_FWI_LIS_MAX_EMVCO : \
|
|
ISODEP_FWI_LIS_MAX_NFC) /*!< FWI Listener Max as NFC / EMVCo */
|
|
#define ISODEP_FWT_LIS_MAX \
|
|
rfalIsoDepFWI2FWT(ISODEP_FWI_LIS_MAX) /*!< FWT Listener Max */
|
|
|
|
#define ISODEP_FWI_MIN_10 (1U) /*!< Minimum value for FWI Digital 1.0 11.6.2.17 */
|
|
#define ISODEP_FWI_MIN_11 (0U) /*!< Default value for FWI Digital 1.1 13.6.2 */
|
|
#define ISODEP_FWI_MAX (14U) /*!< Maximum value for FWI Digital 1.0 11.6.2.17 */
|
|
#define ISODEP_SFGI_MIN (0U) /*!< Default value for FWI Digital 1.1 13.6.2.22 */
|
|
#define ISODEP_SFGI_MAX (14U) /*!< Maximum value for FWI Digital 1.1 13.6.2.22 */
|
|
|
|
#define RFAL_ISODEP_SPARAM_TVL_HDR_LEN (2U) /*!< S(PARAMETERS) TVL header length: Tag + Len */
|
|
#define RFAL_ISODEP_SPARAM_HDR_LEN \
|
|
(RFAL_ISODEP_PCB_LEN + \
|
|
RFAL_ISODEP_SPARAM_TVL_HDR_LEN) /*!< S(PARAMETERS) header length: PCB + Tag + Len */
|
|
|
|
/**********************************************************************************************************************/
|
|
/**********************************************************************************************************************/
|
|
#define RFAL_ISODEP_NO_PARAM (0U) /*!< No parameter flag for isoDepHandleControlMsg() */
|
|
|
|
#define RFAL_ISODEP_CMD_RATS (0xE0U) /*!< RATS command Digital 1.1 13.6.1 */
|
|
|
|
#define RFAL_ISODEP_ATS_MIN_LEN (1U) /*!< Minimum ATS length Digital 1.1 13.6.2 */
|
|
#define RFAL_ISODEP_ATS_HDR_LEN (5U) /*!< ATS headerlength Digital 1.1 13.6.2 */
|
|
#define RFAL_ISODEP_ATS_MAX_LEN \
|
|
(RFAL_ISODEP_ATS_HDR_LEN + \
|
|
RFAL_ISODEP_ATS_HB_MAX_LEN) /*!< Maximum ATS length Digital 1.1 13.6.2 */
|
|
#define RFAL_ISODEP_ATS_T0_FSCI_MASK (0x0FU) /*!< ATS T0's FSCI mask Digital 1.1 13.6.2 */
|
|
#define RFAL_ISODEP_ATS_TB_FWI_SHIFT (4U) /*!< ATS TB's FWI shift Digital 1.1 13.6.2 */
|
|
#define RFAL_ISODEP_ATS_FWI_MASK (0x0FU) /*!< ATS TB's FWI shift Digital 1.1 13.6.2 */
|
|
#define RFAL_ISODEP_ATS_TL_POS (0x00U) /*!< ATS TL's position Digital 1.1 13.6.2 */
|
|
|
|
#define RFAL_ISODEP_PPS_SB (0xD0U) /*!< PPS REQ PPSS's SB value (no CID) ISO14443-4 5.3 */
|
|
#define RFAL_ISODEP_PPS_MASK (0xF0U) /*!< PPS REQ PPSS's SB mask ISO14443-4 5.3 */
|
|
#define RFAL_ISODEP_PPS_SB_DID_MASK \
|
|
(0x0FU) /*!< PPS REQ PPSS's DID|CID mask ISO14443-4 5.3 */
|
|
#define RFAL_ISODEP_PPS_PPS0_PPS1_PRESENT \
|
|
(0x11U) /*!< PPS REQ PPS0 indicating that PPS1 is present */
|
|
#define RFAL_ISODEP_PPS_PPS1 (0x00U) /*!< PPS REQ PPS1 fixed value ISO14443-4 5.3 */
|
|
#define RFAL_ISODEP_PPS_PPS1_DSI_SHIFT \
|
|
(2U) /*!< PPS REQ PPS1 fixed value ISO14443-4 5.3 */
|
|
#define RFAL_ISODEP_PPS_PPS1_DXI_MASK \
|
|
(0x0FU) /*!< PPS REQ PPS1 fixed value ISO14443-4 5.3 */
|
|
#define RFAL_ISODEP_PPS_RES_LEN (1U) /*!< PPS Response length ISO14443-4 5.4 */
|
|
#define RFAL_ISODEP_PPS_STARTBYTE_POS \
|
|
(0U) /*!< PPS REQ PPSS's byte position ISO14443-4 5.4 */
|
|
#define RFAL_ISODEP_PPS_PPS0_POS (1U) /*!< PPS REQ PPS0's byte position ISO14443-4 5.4 */
|
|
#define RFAL_ISODEP_PPS_PPS1_POS (2U) /*!< PPS REQ PPS1's byte position ISO14443-4 5.4 */
|
|
#define RFAL_ISODEP_PPS0_VALID_MASK \
|
|
(0xEFU) /*!< PPS REQ PPS0 valid coding mask ISO14443-4 5.4 */
|
|
|
|
#define RFAL_ISODEP_CMD_ATTRIB (0x1DU) /*!< ATTRIB command Digital 1.1 14.6.1 */
|
|
#define RFAL_ISODEP_ATTRIB_PARAM2_DSI_SHIFT \
|
|
(6U) /*!< ATTRIB PARAM2 DSI shift Digital 1.1 14.6.1 */
|
|
#define RFAL_ISODEP_ATTRIB_PARAM2_DRI_SHIFT \
|
|
(4U) /*!< ATTRIB PARAM2 DRI shift Digital 1.1 14.6.1 */
|
|
#define RFAL_ISODEP_ATTRIB_PARAM2_DXI_MASK \
|
|
(0xF0U) /*!< ATTRIB PARAM2 DxI mask Digital 1.1 14.6.1 */
|
|
#define RFAL_ISODEP_ATTRIB_PARAM2_FSDI_MASK \
|
|
(0x0FU) /*!< ATTRIB PARAM2 FSDI mask Digital 1.1 14.6.1 */
|
|
#define RFAL_ISODEP_ATTRIB_PARAM4_DID_MASK \
|
|
(0x0FU) /*!< ATTRIB PARAM4 DID mask Digital 1.1 14.6.1 */
|
|
#define RFAL_ISODEP_ATTRIB_HDR_LEN (9U) /*!< ATTRIB REQ header length Digital 1.1 14.6.1 */
|
|
|
|
#define RFAL_ISODEP_ATTRIB_RES_HDR_LEN \
|
|
(1U) /*!< ATTRIB RES header length Digital 1.1 14.6.2 */
|
|
#define RFAL_ISODEP_ATTRIB_RES_MBLIDID_POS \
|
|
(0U) /*!< ATTRIB RES MBLI|DID position Digital 1.1 14.6.2 */
|
|
#define RFAL_ISODEP_ATTRIB_RES_DID_MASK \
|
|
(0x0FU) /*!< ATTRIB RES DID mask Digital 1.1 14.6.2 */
|
|
#define RFAL_ISODEP_ATTRIB_RES_MBLI_MASK \
|
|
(0x0FU) /*!< ATTRIB RES MBLI mask Digital 1.1 14.6.2 */
|
|
#define RFAL_ISODEP_ATTRIB_RES_MBLI_SHIFT \
|
|
(4U) /*!< ATTRIB RES MBLI shift Digital 1.1 14.6.2 */
|
|
|
|
#define RFAL_ISODEP_DID_MASK (0x0FU) /*!< ISODEP's DID mask */
|
|
#define RFAL_ISODEP_DID_00 (0U) /*!< ISODEP's DID value 0 */
|
|
|
|
#define RFAL_ISODEP_FSDI_MAX_NFC (8U) /*!< Max FSDI value Digital 2.0 14.6.1.9 & B7 & B8 */
|
|
#define RFAL_ISODEP_FSDI_MAX_NFC_21 \
|
|
(0x0CU) /*!< Max FSDI value Digital 2.1 14.6.1.9 & Table 72 */
|
|
#define RFAL_ISODEP_FSDI_MAX_EMV (0x0CU) /*!< Max FSDI value EMVCo 3.0 5.7.2.5 */
|
|
|
|
#define RFAL_ISODEP_RATS_PARAM_FSDI_MASK \
|
|
(0xF0U) /*!< Mask bits for FSDI in RATS */
|
|
#define RFAL_ISODEP_RATS_PARAM_FSDI_SHIFT \
|
|
(4U) /*!< Shift for FSDI in RATS */
|
|
#define RFAL_ISODEP_RATS_PARAM_DID_MASK \
|
|
(0x0FU) /*!< Mask bits for DID in RATS */
|
|
|
|
#define RFAL_ISODEP_ATS_TL_OFFSET \
|
|
(0x00U) /*!< Offset of TL on ATS */
|
|
#define RFAL_ISODEP_ATS_TA_OFFSET \
|
|
(0x02U) /*!< Offset of TA if it is present on ATS */
|
|
#define RFAL_ISODEP_ATS_TB_OFFSET \
|
|
(0x03U) /*!< Offset of TB if both TA and TB is present on ATS */
|
|
#define RFAL_ISODEP_ATS_TC_OFFSET \
|
|
(0x04U) /*!< Offset of TC if both TA,TB & TC are present on ATS */
|
|
#define RFAL_ISODEP_ATS_HIST_OFFSET \
|
|
(0x05U) /*!< Offset of Historical Bytes if TA, TB & TC are present on ATS */
|
|
#define RFAL_ISODEP_ATS_TC_ADV_FEAT \
|
|
(0x10U) /*!< Bit mask indicating support for Advanced protocol features: DID & NAD */
|
|
#define RFAL_ISODEP_ATS_TC_DID (0x02U) /*!< Bit mask indicating support for DID */
|
|
#define RFAL_ISODEP_ATS_TC_NAD (0x01U) /*!< Bit mask indicating support for NAD */
|
|
|
|
#define RFAL_ISODEP_PPS0_PPS1_PRESENT \
|
|
(0x11U) /*!< PPS0 byte indicating that PPS1 is present */
|
|
#define RFAL_ISODEP_PPS0_PPS1_NOT_PRESENT \
|
|
(0x01U) /*!< PPS0 byte indicating that PPS1 is NOT present */
|
|
#define RFAL_ISODEP_PPS1_DRI_MASK \
|
|
(0x03U) /*!< PPS1 byte DRI mask bits */
|
|
#define RFAL_ISODEP_PPS1_DSI_MASK \
|
|
(0x0CU) /*!< PPS1 byte DSI mask bits */
|
|
#define RFAL_ISODEP_PPS1_DSI_SHIFT \
|
|
(2U) /*!< PPS1 byte DSI shift */
|
|
#define RFAL_ISODEP_PPS1_DxI_MASK \
|
|
(0x03U) /*!< PPS1 byte DSI/DRS mask bits */
|
|
|
|
/*! Delta Time for polling during Activation (ATS) : 20ms Digital 1.0 11.7.1.1 & A.7 */
|
|
#define RFAL_ISODEP_T4T_DTIME_POLL_10 rfalConvMsTo1fc(20)
|
|
|
|
/*! Delta Time for polling during Activation (ATS) : 16.4ms Digital 1.1 13.8.1.1 & A.6
|
|
* Use 16 ms as testcase T4AT_BI_10_03 sends a frame exactly at the border */
|
|
#define RFAL_ISODEP_T4T_DTIME_POLL_11 216960U
|
|
|
|
/*! Activation frame waiting time FWT(act) = 71680/fc (~5286us) Digital 1.1 13.8.1.1 & A.6 */
|
|
#define RFAL_ISODEP_T4T_FWT_ACTIVATION (71680U + RFAL_ISODEP_T4T_DTIME_POLL_11)
|
|
|
|
/*! Delta frame waiting time = 16/fc Digital 1.0 11.7.1.3 & A.7*/
|
|
#define RFAL_ISODEP_DFWT_10 16U
|
|
|
|
/*! Delta frame waiting time = 16/fc Digital 2.0 14.8.1.3 & B.7*/
|
|
#define RFAL_ISODEP_DFWT_20 49152U
|
|
|
|
/*
|
|
******************************************************************************
|
|
* MACROS
|
|
******************************************************************************
|
|
*/
|
|
|
|
#define isoDep_PCBisIBlock(pcb) \
|
|
(((pcb) & (ISODEP_PCB_xBLOCK_MASK | ISODEP_PCB_IB_VALID_MASK)) == \
|
|
(ISODEP_PCB_IBLOCK | ISODEP_PCB_IB_VALID_VAL)) /*!< Checks if pcb is a I-Block */
|
|
#define isoDep_PCBisRBlock(pcb) \
|
|
(((pcb) & (ISODEP_PCB_xBLOCK_MASK | ISODEP_PCB_RB_VALID_MASK)) == \
|
|
(ISODEP_PCB_RBLOCK | ISODEP_PCB_RB_VALID_VAL)) /*!< Checks if pcb is a R-Block */
|
|
#define isoDep_PCBisSBlock(pcb) \
|
|
(((pcb) & (ISODEP_PCB_xBLOCK_MASK | ISODEP_PCB_SB_VALID_MASK)) == \
|
|
(ISODEP_PCB_SBLOCK | ISODEP_PCB_SB_VALID_VAL)) /*!< Checks if pcb is a S-Block */
|
|
|
|
#define isoDep_PCBisChaining(pcb) \
|
|
(((pcb)&ISODEP_PCB_CHAINING_BIT) == \
|
|
ISODEP_PCB_CHAINING_BIT) /*!< Checks if pcb is indicating chaining */
|
|
|
|
#define isoDep_PCBisDeselect(pcb) \
|
|
(((pcb)&ISODEP_PCB_Sxx_MASK) == \
|
|
ISODEP_PCB_DESELECT) /*!< Checks if pcb is indicating DESELECT */
|
|
#define isoDep_PCBisWTX(pcb) \
|
|
(((pcb)&ISODEP_PCB_Sxx_MASK) == ISODEP_PCB_WTX) /*!< Checks if pcb is indicating WTX */
|
|
|
|
#define isoDep_PCBisACK(pcb) \
|
|
(((pcb)&ISODEP_PCB_Rx_MASK) == ISODEP_PCB_ACK) /*!< Checks if pcb is indicating ACK */
|
|
#define isoDep_PCBisNAK(pcb) \
|
|
(((pcb)&ISODEP_PCB_Rx_MASK) == ISODEP_PCB_NAK) /*!< Checks if pcb is indicating ACK */
|
|
|
|
#define isoDep_PCBhasDID(pcb) \
|
|
(((pcb)&ISODEP_PCB_DID_BIT) == ISODEP_PCB_DID_BIT) /*!< Checks if pcb is indicating DID */
|
|
#define isoDep_PCBhasNAD(pcb) \
|
|
(((pcb)&ISODEP_PCB_NAD_BIT) == ISODEP_PCB_NAD_BIT) /*!< Checks if pcb is indicating NAD */
|
|
|
|
#define isoDep_PCBisIChaining(pcb) \
|
|
(isoDep_PCBisIBlock(pcb) && \
|
|
isoDep_PCBisChaining(pcb)) /*!< Checks if pcb is I-Block indicating chaining*/
|
|
|
|
#define isoDep_PCBisSDeselect(pcb) \
|
|
(isoDep_PCBisSBlock(pcb) && \
|
|
isoDep_PCBisDeselect(pcb)) /*!< Checks if pcb is S-Block indicating DESELECT*/
|
|
#define isoDep_PCBisSWTX(pcb) \
|
|
(isoDep_PCBisSBlock(pcb) && \
|
|
isoDep_PCBisWTX(pcb)) /*!< Checks if pcb is S-Block indicating WTX */
|
|
|
|
#define isoDep_PCBisRACK(pcb) \
|
|
(isoDep_PCBisRBlock(pcb) && \
|
|
isoDep_PCBisACK(pcb)) /*!< Checks if pcb is R-Block indicating ACK */
|
|
#define isoDep_PCBisRNAK(pcb) \
|
|
(isoDep_PCBisRBlock(pcb) && \
|
|
isoDep_PCBisNAK(pcb)) /*!< Checks if pcb is R-Block indicating NAK */
|
|
|
|
#define isoDep_PCBIBlock(bn) \
|
|
((uint8_t)(0x00U | ISODEP_PCB_IBLOCK | ISODEP_PCB_B2_BIT | ((bn)&ISODEP_PCB_BN_MASK))) /*!< Returns an I-Block with the given block number (bn) */
|
|
#define isoDep_PCBIBlockChaining(bn) \
|
|
((uint8_t)(isoDep_PCBIBlock(bn) | ISODEP_PCB_CHAINING_BIT)) /*!< Returns an I-Block with the given block number (bn) indicating chaining */
|
|
|
|
#define isoDep_PCBRBlock(bn) \
|
|
((uint8_t)(0x00U | ISODEP_PCB_RBLOCK | ISODEP_PCB_B6_BIT | ISODEP_PCB_B2_BIT | ((bn)&ISODEP_PCB_BN_MASK))) /*!< Returns an R-Block with the given block number (bn) */
|
|
#define isoDep_PCBRACK(bn) \
|
|
((uint8_t)(isoDep_PCBRBlock(bn) | ISODEP_PCB_ACK)) /*!< Returns an R-Block with the given block number (bn) indicating ACK */
|
|
#define isoDep_PCBRNAK(bn) \
|
|
((uint8_t)(isoDep_PCBRBlock(bn) | ISODEP_PCB_NAK)) /*!< Returns an R-Block with the given block number (bn) indicating NAK */
|
|
|
|
#define isoDep_GetBN(pcb) \
|
|
((uint8_t)((pcb)&ISODEP_PCB_BN_MASK)) /*!< Returns the block number (bn) from the given pcb */
|
|
#define isoDep_GetWTXM(inf) \
|
|
((uint8_t)(( \
|
|
inf)&ISODEP_SWTX_WTXM_MASK)) /*!< Returns the WTX value from the given inf byte */
|
|
#define isoDep_isWTXMValid(wtxm) \
|
|
(((wtxm) >= ISODEP_WTXM_MIN) && \
|
|
((wtxm) <= ISODEP_WTXM_MAX)) /*!< Checks if the given wtxm is valid */
|
|
|
|
#define isoDep_WTXMListenerMax(fwt) \
|
|
(MIN( \
|
|
(uint8_t)(ISODEP_FWT_LIS_MAX / (fwt)), \
|
|
ISODEP_WTXM_MAX)) /*!< Calculates the Max WTXM value for the given fwt as a Listener */
|
|
|
|
#define isoDepCalcdSGFT(s) \
|
|
(384U * ((uint32_t)1U \
|
|
<< (s))) /*!< Calculates the dSFGT with given SFGI Digital 1.1 13.8.2.1 & A.6*/
|
|
#define isoDepCalcSGFT(s) \
|
|
(4096U * ((uint32_t)1U \
|
|
<< (s))) /*!< Calculates the SFGT with given SFGI Digital 1.1 13.8.2 */
|
|
|
|
#define isoDep_PCBNextBN(bn) \
|
|
(((uint8_t)(bn) ^ 0x01U) & \
|
|
ISODEP_PCB_BN_MASK) /*!< Returns the value of the next block number based on bn */
|
|
#define isoDep_PCBPrevBN(bn) \
|
|
isoDep_PCBNextBN(bn) /*!< Returns the value of the previous block number based on bn */
|
|
#define isoDep_ToggleBN(bn) \
|
|
((bn) = \
|
|
(((bn) ^ 0x01U) & \
|
|
ISODEP_PCB_BN_MASK)) /*!< Toggles the block number value of the given bn */
|
|
|
|
#define isoDep_WTXAdjust(v) \
|
|
((v) - ((v) >> 3)) /*!< Adjust WTX timer value to a percentage of the total, current 88% */
|
|
|
|
/*! ISO 14443-4 7.5.6.2 & Digital 1.1 - 15.2.6.2 The CE SHALL NOT attempt error recovery and remains in Rx mode upon Transmission or a Protocol Error */
|
|
#define isoDepReEnableRx(rxB, rxBL, rxL) \
|
|
rfalTransceiveBlockingTx(NULL, 0, rxB, rxBL, rxL, RFAL_TXRX_FLAGS_DEFAULT, RFAL_FWT_NONE)
|
|
|
|
/*! Macro used for the blocking methods */
|
|
#define rfalIsoDepRunBlocking(e, fn) \
|
|
do { \
|
|
(e) = (fn); \
|
|
rfalWorker(); \
|
|
} while((e) == ERR_BUSY)
|
|
|
|
#define isoDepTimerStart(timer, time_ms) \
|
|
do { \
|
|
platformTimerDestroy(timer); \
|
|
(timer) = platformTimerCreate((uint16_t)(time_ms)); \
|
|
} while(0) /*!< Configures and starts the WTX timer */
|
|
#define isoDepTimerisExpired(timer) \
|
|
platformTimerIsExpired(timer) /*!< Checks WTX timer has expired */
|
|
#define isoDepTimerDestroy(timer) \
|
|
platformTimerDestroy(timer) /*!< Destroys WTX timer */
|
|
|
|
/*
|
|
******************************************************************************
|
|
* LOCAL DATA TYPES
|
|
******************************************************************************
|
|
*/
|
|
|
|
/*! Internal structure to be used in handling of S(PARAMETRS) only */
|
|
typedef struct {
|
|
uint8_t pcb; /*!< PCB byte */
|
|
rfalIsoDepSParameter sParam; /*!< S(PARAMETERS) */
|
|
} rfalIsoDepControlMsgSParam;
|
|
|
|
/*! Enumeration of the possible control message types */
|
|
typedef enum {
|
|
ISODEP_R_ACK, /*!< R-ACK Acknowledge */
|
|
ISODEP_R_NAK, /*!< R-NACK Negative acknowledge */
|
|
ISODEP_S_WTX, /*!< S-WTX Waiting Time Extension */
|
|
ISODEP_S_DSL /*!< S-DSL Deselect */
|
|
} rfalIsoDepControlMsg;
|
|
|
|
/*! Enumeration of the IsoDep roles */
|
|
typedef enum {
|
|
ISODEP_ROLE_PCD, /*!< Perform as Reader/PCD */
|
|
ISODEP_ROLE_PICC /*!< Perform as Card/PICC */
|
|
} rfalIsoDepRole;
|
|
|
|
/*! ISO-DEP layer states */
|
|
typedef enum {
|
|
ISODEP_ST_IDLE, /*!< Idle State */
|
|
ISODEP_ST_PCD_TX, /*!< PCD Transmission State */
|
|
ISODEP_ST_PCD_RX, /*!< PCD Reception State */
|
|
ISODEP_ST_PCD_WAIT_DSL, /*!< PCD Wait for DSL response */
|
|
|
|
ISODEP_ST_PICC_ACT_ATS, /*!< PICC has replied to RATS (ATS) */
|
|
ISODEP_ST_PICC_ACT_ATTRIB, /*!< PICC has replied to ATTRIB */
|
|
ISODEP_ST_PICC_RX, /*!< PICC Reception State */
|
|
ISODEP_ST_PICC_SWTX, /*!< PICC Waiting Time eXtension */
|
|
ISODEP_ST_PICC_SDSL, /*!< PICC S(DSL) response ongoing */
|
|
ISODEP_ST_PICC_TX, /*!< PICC Transmission State */
|
|
|
|
ISODEP_ST_PCD_ACT_RATS, /*!< PCD activation (RATS) */
|
|
ISODEP_ST_PCD_ACT_PPS, /*!< PCD activation (PPS) */
|
|
|
|
} rfalIsoDepState;
|
|
|
|
/*! Holds all ISO-DEP data(counters, buffers, ID, timeouts, frame size) */
|
|
typedef struct {
|
|
rfalIsoDepState state; /*!< ISO-DEP module state */
|
|
rfalIsoDepRole role; /*!< Current ISO-DEP role */
|
|
|
|
uint8_t blockNumber; /*!< Current block number */
|
|
uint8_t did; /*!< Current DID */
|
|
uint8_t nad; /*!< Current DID */
|
|
uint8_t cntIRetrys; /*!< I-Block retry counter */
|
|
uint8_t cntRRetrys; /*!< R-Block retry counter */
|
|
uint8_t cntSDslRetrys; /*!< S(DESELECT) retry counter */
|
|
uint8_t cntSWtxRetrys; /*!< Overall S(WTX) retry counter */
|
|
uint8_t cntSWtxNack; /*!< R(NACK) answered with S(WTX) counter */
|
|
uint32_t fwt; /*!< Current FWT (Frame Waiting Time) */
|
|
uint32_t dFwt; /*!< Current delta FWT */
|
|
uint16_t fsx; /*!< Current FSx FSC or FSD (max Frame size) */
|
|
bool isTxChaining; /*!< Flag for chaining on Tx */
|
|
bool isRxChaining; /*!< Flag for chaining on Rx */
|
|
uint8_t* txBuf; /*!< Tx buffer pointer */
|
|
uint8_t* rxBuf; /*!< Rx buffer pointer */
|
|
uint16_t txBufLen; /*!< Tx buffer length */
|
|
uint16_t rxBufLen; /*!< Rx buffer length */
|
|
uint8_t txBufInfPos; /*!< Start of payload in txBuf */
|
|
uint8_t rxBufInfPos; /*!< Start of payload in rxBuf */
|
|
|
|
uint16_t ourFsx; /*!< Our current FSx FSC or FSD (Frame size) */
|
|
uint8_t lastPCB; /*!< Last PCB sent */
|
|
uint8_t lastWTXM; /*!< Last WTXM sent */
|
|
uint8_t atsTA; /*!< TA on ATS */
|
|
uint8_t hdrLen; /*!< Current ISO-DEP length */
|
|
rfalBitRate txBR; /*!< Current Tx Bit Rate */
|
|
rfalBitRate rxBR; /*!< Current Rx Bit Rate */
|
|
uint16_t* rxLen; /*!< Output parameter ptr to Rx length */
|
|
bool* rxChaining; /*!< Output parameter ptr to Rx chaining flag */
|
|
uint32_t WTXTimer; /*!< Timer used for WTX */
|
|
bool lastDID00; /*!< Last PCD block had DID flag (for DID = 0) */
|
|
|
|
bool isTxPending; /*!< Flag pending Block while waiting WTX Ack */
|
|
bool isWait4WTX; /*!< Flag for waiting WTX Ack */
|
|
|
|
uint8_t maxRetriesI; /*!< Number of retries for a I-Block */
|
|
uint8_t maxRetriesR; /*!< Number of retries for a R-Block */
|
|
uint8_t maxRetriesSDSL; /*!< Number of retries for S(DESELECT) errors */
|
|
uint8_t maxRetriesSWTX; /*!< Number of retries for S(WTX) errors */
|
|
uint8_t maxRetriesSnWTX; /*!< Number of retries S(WTX) replied w NACK */
|
|
uint8_t maxRetriesRATS; /*!< Number of retries for RATS */
|
|
|
|
rfalComplianceMode compMode; /*!< Compliance mode */
|
|
|
|
uint8_t ctrlBuf[ISODEP_CONTROLMSG_BUF_LEN]; /*!< Control msg buf */
|
|
uint16_t ctrlRxLen; /*!< Control msg rcvd len */
|
|
|
|
union { /* PRQA S 0750 # MISRA 19.2 - Members of the union will not be used concurrently, only one frame at a time */
|
|
#if RFAL_FEATURE_NFCA
|
|
rfalIsoDepRats ratsReq;
|
|
rfalIsoDepPpsReq ppsReq;
|
|
#endif /* RFAL_FEATURE_NFCA */
|
|
|
|
#if RFAL_FEATURE_NFCB
|
|
rfalIsoDepAttribCmd attribReq;
|
|
#endif /* RFAL_FEATURE_NFCB */
|
|
} actv; /*!< Activation buffer */
|
|
|
|
uint8_t* rxLen8; /*!< Receive length (8-bit) */
|
|
rfalIsoDepDevice* actvDev; /*!< Activation Device Info */
|
|
rfalIsoDepListenActvParam actvParam; /*!< Listen Activation context */
|
|
|
|
rfalIsoDepApduTxRxParam APDUParam; /*!< APDU TxRx params */
|
|
uint16_t APDUTxPos; /*!< APDU Tx position */
|
|
uint16_t APDURxPos; /*!< APDU Rx position */
|
|
bool isAPDURxChaining; /*!< APDU Transceive chaining flag */
|
|
|
|
} rfalIsoDep;
|
|
|
|
/*
|
|
******************************************************************************
|
|
* LOCAL VARIABLES
|
|
******************************************************************************
|
|
*/
|
|
|
|
static rfalIsoDep gIsoDep; /*!< ISO-DEP Module instance */
|
|
|
|
/*
|
|
******************************************************************************
|
|
* LOCAL FUNCTION PROTOTYPES
|
|
******************************************************************************
|
|
*/
|
|
static void isoDepClearCounters(void);
|
|
static ReturnCode
|
|
isoDepTx(uint8_t pcb, const uint8_t* txBuf, uint8_t* infBuf, uint16_t infLen, uint32_t fwt);
|
|
static ReturnCode isoDepHandleControlMsg(rfalIsoDepControlMsg controlMsg, uint8_t param);
|
|
static void rfalIsoDepApdu2IBLockParam(
|
|
rfalIsoDepApduTxRxParam apduParam,
|
|
rfalIsoDepTxRxParam* iBlockParam,
|
|
uint16_t txPos,
|
|
uint16_t rxPos);
|
|
|
|
#if RFAL_FEATURE_ISO_DEP_POLL
|
|
static ReturnCode isoDepDataExchangePCD(uint16_t* outActRxLen, bool* outIsChaining);
|
|
static void rfalIsoDepCalcBitRate(
|
|
rfalBitRate maxAllowedBR,
|
|
uint8_t piccBRCapability,
|
|
rfalBitRate* dsi,
|
|
rfalBitRate* dri);
|
|
static uint32_t rfalIsoDepSFGI2SFGT(uint8_t sfgi);
|
|
|
|
#if RFAL_FEATURE_NFCA
|
|
static ReturnCode
|
|
rfalIsoDepStartRATS(rfalIsoDepFSxI FSDI, uint8_t DID, rfalIsoDepAts* ats, uint8_t* atsLen);
|
|
static ReturnCode rfalIsoDepGetRATSStatus(void);
|
|
static ReturnCode
|
|
rfalIsoDepStartPPS(uint8_t DID, rfalBitRate DSI, rfalBitRate DRI, rfalIsoDepPpsRes* ppsRes);
|
|
static ReturnCode rfalIsoDepGetPPSSTatus(void);
|
|
#endif /* RFAL_FEATURE_NFCA */
|
|
|
|
#if RFAL_FEATURE_NFCB
|
|
static ReturnCode rfalIsoDepStartATTRIB(
|
|
const uint8_t* nfcid0,
|
|
uint8_t PARAM1,
|
|
rfalBitRate DSI,
|
|
rfalBitRate DRI,
|
|
rfalIsoDepFSxI FSDI,
|
|
uint8_t PARAM3,
|
|
uint8_t DID,
|
|
const uint8_t* HLInfo,
|
|
uint8_t HLInfoLen,
|
|
uint32_t fwt,
|
|
rfalIsoDepAttribRes* attribRes,
|
|
uint8_t* attribResLen);
|
|
static ReturnCode rfalIsoDepGetATTRIBStatus(void);
|
|
#endif /* RFAL_FEATURE_NFCB */
|
|
|
|
#endif /* RFAL_FEATURE_ISO_DEP_POLL */
|
|
|
|
#if RFAL_FEATURE_ISO_DEP_LISTEN
|
|
static ReturnCode isoDepDataExchangePICC(void);
|
|
static ReturnCode isoDepReSendControlMsg(void);
|
|
#endif
|
|
|
|
/*
|
|
******************************************************************************
|
|
* LOCAL FUNCTIONS
|
|
******************************************************************************
|
|
*/
|
|
|
|
/*******************************************************************************/
|
|
static void isoDepClearCounters(void) {
|
|
gIsoDep.cntIRetrys = 0;
|
|
gIsoDep.cntRRetrys = 0;
|
|
gIsoDep.cntSDslRetrys = 0;
|
|
gIsoDep.cntSWtxRetrys = 0;
|
|
gIsoDep.cntSWtxNack = 0;
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
static ReturnCode
|
|
isoDepTx(uint8_t pcb, const uint8_t* txBuf, uint8_t* infBuf, uint16_t infLen, uint32_t fwt) {
|
|
uint8_t* txBlock;
|
|
uint16_t txBufLen;
|
|
uint8_t computedPcb;
|
|
rfalTransceiveContext ctx;
|
|
|
|
txBlock = infBuf; /* Point to beginning of the INF, and go backwards */
|
|
gIsoDep.lastPCB = pcb; /* Store the last PCB sent */
|
|
|
|
if(infLen > 0U) {
|
|
if(((uint32_t)infBuf - (uint32_t)txBuf) <
|
|
gIsoDep.hdrLen) /* Check that we can fit the header in the given space */
|
|
{
|
|
return ERR_NOMEM;
|
|
}
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
/* Compute optional PCB bits */
|
|
computedPcb = pcb;
|
|
if((gIsoDep.did != RFAL_ISODEP_NO_DID) ||
|
|
((gIsoDep.did == RFAL_ISODEP_DID_00) && gIsoDep.lastDID00)) {
|
|
computedPcb |= ISODEP_PCB_DID_BIT;
|
|
}
|
|
if(gIsoDep.nad != RFAL_ISODEP_NO_NAD) {
|
|
computedPcb |= ISODEP_PCB_NAD_BIT;
|
|
}
|
|
if((gIsoDep.isTxChaining) && (isoDep_PCBisIBlock(computedPcb))) {
|
|
computedPcb |= ISODEP_PCB_CHAINING_BIT;
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
/* Compute Payload on the given txBuf, start by the PCB | DID | NAD | before INF */
|
|
|
|
if(gIsoDep.nad != RFAL_ISODEP_NO_NAD) {
|
|
*(--txBlock) = gIsoDep.nad; /* NAD is optional */
|
|
}
|
|
|
|
if((gIsoDep.did != RFAL_ISODEP_NO_DID) ||
|
|
((gIsoDep.did == RFAL_ISODEP_DID_00) && gIsoDep.lastDID00)) {
|
|
*(--txBlock) = gIsoDep.did; /* DID is optional */
|
|
}
|
|
|
|
*(--txBlock) = computedPcb; /* PCB always present */
|
|
|
|
txBufLen =
|
|
(infLen +
|
|
(uint16_t)((uint32_t)infBuf - (uint32_t)txBlock)); /* Calculate overall buffer size */
|
|
|
|
if(txBufLen > (gIsoDep.fsx -
|
|
ISODEP_CRC_LEN)) /* Check if msg length violates the maximum frame size FSC */
|
|
{
|
|
return ERR_NOTSUPP;
|
|
}
|
|
|
|
rfalCreateByteFlagsTxRxContext(
|
|
ctx,
|
|
txBlock,
|
|
txBufLen,
|
|
gIsoDep.rxBuf,
|
|
gIsoDep.rxBufLen,
|
|
gIsoDep.rxLen,
|
|
RFAL_TXRX_FLAGS_DEFAULT,
|
|
((gIsoDep.role == ISODEP_ROLE_PICC) ? RFAL_FWT_NONE : fwt));
|
|
return rfalStartTransceive(&ctx);
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
static ReturnCode isoDepHandleControlMsg(rfalIsoDepControlMsg controlMsg, uint8_t param) {
|
|
uint8_t pcb;
|
|
uint8_t infLen;
|
|
uint32_t fwtTemp;
|
|
|
|
infLen = 0;
|
|
fwtTemp = (gIsoDep.fwt + gIsoDep.dFwt);
|
|
ST_MEMSET(gIsoDep.ctrlBuf, 0x00, ISODEP_CONTROLMSG_BUF_LEN);
|
|
|
|
switch(controlMsg) {
|
|
/*******************************************************************************/
|
|
case ISODEP_R_ACK:
|
|
|
|
if(gIsoDep.cntRRetrys++ > gIsoDep.maxRetriesR) {
|
|
return ERR_TIMEOUT; /* NFC Forum mandates timeout or transmission error depending on previous errors */
|
|
}
|
|
|
|
pcb = isoDep_PCBRACK(gIsoDep.blockNumber);
|
|
break;
|
|
|
|
/*******************************************************************************/
|
|
case ISODEP_R_NAK:
|
|
|
|
if((gIsoDep.cntRRetrys++ > gIsoDep.maxRetriesR) || /* Max R Block retries reached */
|
|
(gIsoDep.cntSWtxNack >=
|
|
gIsoDep
|
|
.maxRetriesSnWTX)) /* Max number PICC is allowed to respond with S(WTX) to R(NAK) */
|
|
{
|
|
return ERR_TIMEOUT;
|
|
}
|
|
|
|
pcb = isoDep_PCBRNAK(gIsoDep.blockNumber);
|
|
break;
|
|
|
|
/*******************************************************************************/
|
|
case ISODEP_S_WTX:
|
|
|
|
if((gIsoDep.cntSWtxRetrys++ > gIsoDep.maxRetriesSWTX) &&
|
|
(gIsoDep.maxRetriesSWTX != RFAL_ISODEP_MAX_WTX_RETRYS_ULTD)) {
|
|
return ERR_PROTO;
|
|
}
|
|
|
|
/* Check if WTXM is valid */
|
|
if(!isoDep_isWTXMValid(param)) {
|
|
return ERR_PROTO;
|
|
}
|
|
|
|
if(gIsoDep.role == ISODEP_ROLE_PCD) {
|
|
/* Calculate temp Wait Time eXtension */
|
|
fwtTemp = (gIsoDep.fwt * param);
|
|
fwtTemp = MIN(RFAL_ISODEP_MAX_FWT, fwtTemp);
|
|
fwtTemp += gIsoDep.dFwt;
|
|
}
|
|
|
|
pcb = ISODEP_PCB_SWTX;
|
|
gIsoDep.ctrlBuf[RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN + infLen++] = param;
|
|
break;
|
|
|
|
/*******************************************************************************/
|
|
case ISODEP_S_DSL:
|
|
|
|
if(gIsoDep.cntSDslRetrys++ > gIsoDep.maxRetriesSDSL) {
|
|
return ERR_TIMEOUT; /* NFC Forum mandates timeout or transmission error depending on previous errors */
|
|
}
|
|
|
|
if(gIsoDep.role == ISODEP_ROLE_PCD) {
|
|
/* Digital 1.0 - 13.2.7.3 Poller must wait fwtDEACTIVATION */
|
|
fwtTemp = ISODEP_FWT_DEACTIVATION;
|
|
gIsoDep.state = ISODEP_ST_PCD_WAIT_DSL;
|
|
}
|
|
pcb = ISODEP_PCB_SDSL;
|
|
break;
|
|
|
|
/*******************************************************************************/
|
|
default:
|
|
return ERR_INTERNAL;
|
|
}
|
|
|
|
return isoDepTx(
|
|
pcb,
|
|
gIsoDep.ctrlBuf,
|
|
&gIsoDep.ctrlBuf[RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN],
|
|
infLen,
|
|
fwtTemp);
|
|
}
|
|
|
|
#if RFAL_FEATURE_ISO_DEP_LISTEN
|
|
/*******************************************************************************/
|
|
static ReturnCode isoDepReSendControlMsg(void) {
|
|
if(isoDep_PCBisRACK(gIsoDep.lastPCB)) {
|
|
return isoDepHandleControlMsg(ISODEP_R_ACK, RFAL_ISODEP_NO_PARAM);
|
|
}
|
|
|
|
if(isoDep_PCBisRNAK(gIsoDep.lastPCB)) {
|
|
return isoDepHandleControlMsg(ISODEP_R_NAK, RFAL_ISODEP_NO_PARAM);
|
|
}
|
|
|
|
if(isoDep_PCBisSDeselect(gIsoDep.lastPCB)) {
|
|
return isoDepHandleControlMsg(ISODEP_S_DSL, RFAL_ISODEP_NO_PARAM);
|
|
}
|
|
|
|
if(isoDep_PCBisSWTX(gIsoDep.lastPCB)) {
|
|
return isoDepHandleControlMsg(ISODEP_S_WTX, gIsoDep.lastWTXM);
|
|
}
|
|
return ERR_WRONG_STATE;
|
|
}
|
|
#endif /* RFAL_FEATURE_ISO_DEP_LISTEN */
|
|
|
|
/*
|
|
******************************************************************************
|
|
* GLOBAL FUNCTIONS
|
|
******************************************************************************
|
|
*/
|
|
|
|
/*******************************************************************************/
|
|
void rfalIsoDepInitialize(void) {
|
|
gIsoDep.state = ISODEP_ST_IDLE;
|
|
gIsoDep.role = ISODEP_ROLE_PCD;
|
|
gIsoDep.did = RFAL_ISODEP_NO_DID;
|
|
gIsoDep.nad = RFAL_ISODEP_NO_NAD;
|
|
gIsoDep.blockNumber = 0;
|
|
gIsoDep.isTxChaining = false;
|
|
gIsoDep.isRxChaining = false;
|
|
gIsoDep.lastDID00 = false;
|
|
gIsoDep.lastPCB = ISODEP_PCB_INVALID;
|
|
gIsoDep.fsx = (uint16_t)RFAL_ISODEP_FSX_16;
|
|
gIsoDep.ourFsx = (uint16_t)RFAL_ISODEP_FSX_16;
|
|
gIsoDep.hdrLen = RFAL_ISODEP_PCB_LEN;
|
|
|
|
gIsoDep.rxLen = NULL;
|
|
gIsoDep.rxBuf = NULL;
|
|
gIsoDep.rxBufInfPos = 0U;
|
|
gIsoDep.txBufInfPos = 0U;
|
|
|
|
gIsoDep.isTxPending = false;
|
|
gIsoDep.isWait4WTX = false;
|
|
|
|
gIsoDep.compMode = RFAL_COMPLIANCE_MODE_NFC;
|
|
gIsoDep.maxRetriesR = RFAL_ISODEP_MAX_R_RETRYS;
|
|
gIsoDep.maxRetriesI = RFAL_ISODEP_MAX_I_RETRYS;
|
|
gIsoDep.maxRetriesSDSL = RFAL_ISODEP_MAX_DSL_RETRYS;
|
|
gIsoDep.maxRetriesSWTX = RFAL_ISODEP_MAX_WTX_RETRYS;
|
|
gIsoDep.maxRetriesSnWTX = RFAL_ISODEP_MAX_WTX_NACK_RETRYS;
|
|
gIsoDep.maxRetriesRATS = RFAL_ISODEP_RATS_RETRIES;
|
|
|
|
gIsoDep.APDURxPos = 0;
|
|
gIsoDep.APDUTxPos = 0;
|
|
gIsoDep.APDUParam.rxLen = NULL;
|
|
gIsoDep.APDUParam.rxBuf = NULL;
|
|
gIsoDep.APDUParam.txBuf = NULL;
|
|
|
|
isoDepClearCounters();
|
|
|
|
/* Destroy any ongoing WTX timer */
|
|
isoDepTimerDestroy(gIsoDep.WTXTimer);
|
|
gIsoDep.WTXTimer = 0U;
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
void rfalIsoDepInitializeWithParams(
|
|
rfalComplianceMode compMode,
|
|
uint8_t maxRetriesR,
|
|
uint8_t maxRetriesSnWTX,
|
|
uint8_t maxRetriesSWTX,
|
|
uint8_t maxRetriesSDSL,
|
|
uint8_t maxRetriesI,
|
|
uint8_t maxRetriesRATS) {
|
|
rfalIsoDepInitialize();
|
|
|
|
gIsoDep.compMode = compMode;
|
|
gIsoDep.maxRetriesR = maxRetriesR;
|
|
gIsoDep.maxRetriesSnWTX = maxRetriesSnWTX;
|
|
gIsoDep.maxRetriesSWTX = maxRetriesSWTX;
|
|
gIsoDep.maxRetriesSDSL = maxRetriesSDSL;
|
|
gIsoDep.maxRetriesI = maxRetriesI;
|
|
gIsoDep.maxRetriesRATS = maxRetriesRATS;
|
|
}
|
|
|
|
#if RFAL_FEATURE_ISO_DEP_POLL
|
|
/*******************************************************************************/
|
|
static ReturnCode isoDepDataExchangePCD(uint16_t* outActRxLen, bool* outIsChaining) {
|
|
ReturnCode ret;
|
|
uint8_t rxPCB;
|
|
|
|
/* Check out parameters */
|
|
if((outActRxLen == NULL) || (outIsChaining == NULL)) {
|
|
return ERR_PARAM;
|
|
}
|
|
|
|
*outIsChaining = false;
|
|
|
|
/* Calculate header required and check if the buffers InfPositions are suitable */
|
|
gIsoDep.hdrLen = RFAL_ISODEP_PCB_LEN;
|
|
if(gIsoDep.did != RFAL_ISODEP_NO_DID) {
|
|
gIsoDep.hdrLen += RFAL_ISODEP_DID_LEN;
|
|
}
|
|
if(gIsoDep.nad != RFAL_ISODEP_NO_NAD) {
|
|
gIsoDep.hdrLen += RFAL_ISODEP_NAD_LEN;
|
|
}
|
|
|
|
/* Check if there is enough space before the infPos to append ISO-DEP headers on rx and tx */
|
|
if((gIsoDep.rxBufInfPos < gIsoDep.hdrLen) || (gIsoDep.txBufInfPos < gIsoDep.hdrLen)) {
|
|
return ERR_PARAM;
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
switch(gIsoDep.state) {
|
|
/*******************************************************************************/
|
|
case ISODEP_ST_IDLE:
|
|
return ERR_NONE;
|
|
|
|
/*******************************************************************************/
|
|
case ISODEP_ST_PCD_TX:
|
|
ret = isoDepTx(
|
|
isoDep_PCBIBlock(gIsoDep.blockNumber),
|
|
gIsoDep.txBuf,
|
|
&gIsoDep.txBuf[gIsoDep.txBufInfPos],
|
|
gIsoDep.txBufLen,
|
|
(gIsoDep.fwt + gIsoDep.dFwt));
|
|
switch(ret) {
|
|
case ERR_NONE:
|
|
gIsoDep.state = ISODEP_ST_PCD_RX;
|
|
break;
|
|
|
|
default:
|
|
return ret;
|
|
}
|
|
/* fall through */
|
|
|
|
/*******************************************************************************/
|
|
case ISODEP_ST_PCD_WAIT_DSL: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
|
|
case ISODEP_ST_PCD_RX:
|
|
|
|
ret = rfalGetTransceiveStatus();
|
|
switch(ret) {
|
|
/* Data rcvd with error or timeout -> Send R-NAK */
|
|
case ERR_TIMEOUT:
|
|
case ERR_CRC:
|
|
case ERR_PAR:
|
|
case ERR_FRAMING: /* added to handle test cases scenario TC_POL_NFCB_T4AT_BI_82_x_y & TC_POL_NFCB_T4BT_BI_82_x_y */
|
|
case ERR_INCOMPLETE_BYTE: /* added to handle test cases scenario TC_POL_NFCB_T4AT_BI_82_x_y & TC_POL_NFCB_T4BT_BI_82_x_y */
|
|
|
|
if(gIsoDep
|
|
.isRxChaining) { /* Rule 5 - In PICC chaining when a invalid/timeout occurs -> R-ACK */
|
|
EXIT_ON_ERR(ret, isoDepHandleControlMsg(ISODEP_R_ACK, RFAL_ISODEP_NO_PARAM));
|
|
} else if(
|
|
gIsoDep.state ==
|
|
ISODEP_ST_PCD_WAIT_DSL) { /* Rule 8 - If s-Deselect response fails MAY retransmit */
|
|
EXIT_ON_ERR(ret, isoDepHandleControlMsg(ISODEP_S_DSL, RFAL_ISODEP_NO_PARAM));
|
|
} else { /* Rule 4 - When a invalid block or timeout occurs -> R-NACK */
|
|
EXIT_ON_ERR(ret, isoDepHandleControlMsg(ISODEP_R_NAK, RFAL_ISODEP_NO_PARAM));
|
|
}
|
|
return ERR_BUSY;
|
|
|
|
case ERR_NONE:
|
|
break;
|
|
|
|
case ERR_BUSY:
|
|
return ERR_BUSY; /* Debug purposes */
|
|
|
|
default:
|
|
return ret;
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
/* No error, process incoming msg */
|
|
/*******************************************************************************/
|
|
|
|
(*outActRxLen) = rfalConvBitsToBytes(*outActRxLen);
|
|
|
|
/* Check rcvd msg length, cannot be less then the expected header */
|
|
if(((*outActRxLen) < gIsoDep.hdrLen) || ((*outActRxLen) >= gIsoDep.ourFsx)) {
|
|
return ERR_PROTO;
|
|
}
|
|
|
|
/* Grab rcvd PCB */
|
|
rxPCB = gIsoDep.rxBuf[ISODEP_PCB_POS];
|
|
|
|
/* EMVCo doesn't allow usage of for CID or NAD EMVCo 2.6 TAble 10.2 */
|
|
if((gIsoDep.compMode == RFAL_COMPLIANCE_MODE_EMV) &&
|
|
(isoDep_PCBhasDID(rxPCB) || isoDep_PCBhasNAD(rxPCB))) {
|
|
return ERR_PROTO;
|
|
}
|
|
|
|
/* If we are expecting DID, check if PCB signals its presence and if device ID match*/
|
|
if((gIsoDep.did != RFAL_ISODEP_NO_DID) &&
|
|
(!isoDep_PCBhasDID(rxPCB) || (gIsoDep.did != gIsoDep.rxBuf[ISODEP_DID_POS]))) {
|
|
return ERR_PROTO;
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
/* Process S-Block */
|
|
/*******************************************************************************/
|
|
if(isoDep_PCBisSBlock(rxPCB)) {
|
|
/* Check if is a Wait Time eXtension */
|
|
if(isoDep_PCBisSWTX(rxPCB)) {
|
|
/* Check if PICC has requested S(WTX) as response to R(NAK) EMVCo 3.0 10.3.5.5 / Digital 2.0 16.2.6.5 */
|
|
if(isoDep_PCBisRNAK(gIsoDep.lastPCB)) {
|
|
gIsoDep.cntSWtxNack++; /* Count S(WTX) upon R(NAK) */
|
|
gIsoDep.cntRRetrys = 0; /* Reset R-Block counter has PICC has responded */
|
|
} else {
|
|
gIsoDep.cntSWtxNack = 0; /* Reset R(NACK)->S(WTX) counter */
|
|
}
|
|
|
|
/* Rule 3 - respond to S-block: get 1st INF byte S(STW): Power + WTXM */
|
|
EXIT_ON_ERR(
|
|
ret,
|
|
isoDepHandleControlMsg(
|
|
ISODEP_S_WTX, isoDep_GetWTXM(gIsoDep.rxBuf[gIsoDep.hdrLen])));
|
|
return ERR_BUSY;
|
|
}
|
|
|
|
/* Check if is a deselect response */
|
|
if(isoDep_PCBisSDeselect(rxPCB)) {
|
|
if(gIsoDep.state == ISODEP_ST_PCD_WAIT_DSL) {
|
|
rfalIsoDepInitialize(); /* Session finished reInit vars */
|
|
return ERR_NONE;
|
|
}
|
|
|
|
/* Deselect response not expected */
|
|
/* fall through to PROTO error */
|
|
}
|
|
/* Unexpected S-Block */
|
|
return ERR_PROTO;
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
/* Process R-Block */
|
|
/*******************************************************************************/
|
|
else if(isoDep_PCBisRBlock(rxPCB)) {
|
|
if(isoDep_PCBisRACK(rxPCB)) /* Check if is a R-ACK */
|
|
{
|
|
if(isoDep_GetBN(rxPCB) == gIsoDep.blockNumber) /* Expected block number */
|
|
{
|
|
/* Rule B - ACK with expected bn -> Increment block number */
|
|
gIsoDep.blockNumber = isoDep_PCBNextBN(gIsoDep.blockNumber);
|
|
|
|
/* R-ACK only allowed when PCD chaining */
|
|
if(!gIsoDep.isTxChaining) {
|
|
return ERR_PROTO;
|
|
}
|
|
|
|
/* Rule 7 - Chaining transaction done, continue chaining */
|
|
isoDepClearCounters();
|
|
return ERR_NONE; /* This block has been transmitted */
|
|
} else {
|
|
/* Rule 6 - R-ACK with wrong block number retransmit */
|
|
/* Digital 2.0 16.2.5.4 - Retransmit maximum two times */
|
|
/* EMVCo 3.0 10.3.4.3 - PCD may re-transmit the last I-Block or report error */
|
|
if(gIsoDep.cntIRetrys++ < gIsoDep.maxRetriesI) {
|
|
gIsoDep.cntRRetrys = 0; /* Clear R counter only */
|
|
gIsoDep.state = ISODEP_ST_PCD_TX;
|
|
return ERR_BUSY;
|
|
}
|
|
return ERR_TIMEOUT; /* NFC Forum mandates timeout or transmission error depending on previous errors */
|
|
}
|
|
} else /* Unexcpected R-Block */
|
|
{
|
|
return ERR_PROTO;
|
|
}
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
/* Process I-Block */
|
|
/*******************************************************************************/
|
|
else if(isoDep_PCBisIBlock(rxPCB)) {
|
|
/*******************************************************************************/
|
|
/* is PICC performing chaining */
|
|
if(isoDep_PCBisChaining(rxPCB)) {
|
|
gIsoDep.isRxChaining = true;
|
|
*outIsChaining = true;
|
|
|
|
if(isoDep_GetBN(rxPCB) == gIsoDep.blockNumber) {
|
|
/* Rule B - ACK with correct block number -> Increase Block number */
|
|
isoDep_ToggleBN(gIsoDep.blockNumber);
|
|
|
|
isoDepClearCounters(); /* Clear counters in case R counter is already at max */
|
|
|
|
/* Rule 2 - Send ACK */
|
|
EXIT_ON_ERR(ret, isoDepHandleControlMsg(ISODEP_R_ACK, RFAL_ISODEP_NO_PARAM));
|
|
|
|
/* Received I-Block with chaining, send current data to DH */
|
|
|
|
/* remove ISO DEP header, check is necessary to move the INF data on the buffer */
|
|
*outActRxLen -= gIsoDep.hdrLen;
|
|
if((gIsoDep.hdrLen != gIsoDep.rxBufInfPos) && (*outActRxLen > 0U)) {
|
|
ST_MEMMOVE(
|
|
&gIsoDep.rxBuf[gIsoDep.rxBufInfPos],
|
|
&gIsoDep.rxBuf[gIsoDep.hdrLen],
|
|
*outActRxLen);
|
|
}
|
|
|
|
isoDepClearCounters();
|
|
return ERR_AGAIN; /* Send Again signalling to run again, but some chaining data has arrived */
|
|
} else {
|
|
/* Rule 5 - PICC chaining invalid I-Block -> R-ACK */
|
|
EXIT_ON_ERR(ret, isoDepHandleControlMsg(ISODEP_R_ACK, RFAL_ISODEP_NO_PARAM));
|
|
}
|
|
return ERR_BUSY;
|
|
}
|
|
|
|
gIsoDep.isRxChaining = false; /* clear PICC chaining flag */
|
|
|
|
if(isoDep_GetBN(rxPCB) == gIsoDep.blockNumber) {
|
|
/* Rule B - I-Block with correct block number -> Increase Block number */
|
|
isoDep_ToggleBN(gIsoDep.blockNumber);
|
|
|
|
/* I-Block transaction done successfully */
|
|
|
|
/* remove ISO DEP header, check is necessary to move the INF data on the buffer */
|
|
*outActRxLen -= gIsoDep.hdrLen;
|
|
if((gIsoDep.hdrLen != gIsoDep.rxBufInfPos) && (*outActRxLen > 0U)) {
|
|
ST_MEMMOVE(
|
|
&gIsoDep.rxBuf[gIsoDep.rxBufInfPos],
|
|
&gIsoDep.rxBuf[gIsoDep.hdrLen],
|
|
*outActRxLen);
|
|
}
|
|
|
|
gIsoDep.state = ISODEP_ST_IDLE;
|
|
isoDepClearCounters();
|
|
return ERR_NONE;
|
|
} else {
|
|
if((gIsoDep.compMode != RFAL_COMPLIANCE_MODE_ISO)) {
|
|
/* Invalid Block (not chaining) -> Raise error Digital 1.1 15.2.6.4 EMVCo 2.6 10.3.5.4 */
|
|
return ERR_PROTO;
|
|
}
|
|
|
|
/* Rule 4 - Invalid Block -> R-NAK */
|
|
EXIT_ON_ERR(ret, isoDepHandleControlMsg(ISODEP_R_NAK, RFAL_ISODEP_NO_PARAM));
|
|
return ERR_BUSY;
|
|
}
|
|
} else /* not S/R/I - Block */
|
|
{
|
|
return ERR_PROTO;
|
|
}
|
|
/* fall through */
|
|
|
|
/*******************************************************************************/
|
|
default: /* PRQA S 2003 # MISRA 16.3 - Intentional fall through */
|
|
/* MISRA 16.4: no empty default (comment will suffice) */
|
|
break;
|
|
}
|
|
|
|
return ERR_INTERNAL;
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
ReturnCode rfalIsoDepDeselect(void) {
|
|
ReturnCode ret;
|
|
uint32_t cntRerun;
|
|
bool dummyB;
|
|
|
|
/*******************************************************************************/
|
|
/* Using local static vars and static config to cope with a Deselect after *
|
|
* RATS\ATTRIB without any I-Block exchanged */
|
|
gIsoDep.rxLen = &gIsoDep.ctrlRxLen;
|
|
gIsoDep.rxBuf = gIsoDep.ctrlBuf;
|
|
gIsoDep.rxBufLen = ISODEP_CONTROLMSG_BUF_LEN - (RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN);
|
|
gIsoDep.rxBufInfPos = (RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN);
|
|
gIsoDep.txBufInfPos = (RFAL_ISODEP_PCB_LEN + RFAL_ISODEP_DID_LEN);
|
|
|
|
/*******************************************************************************/
|
|
/* The Deselect process is being done blocking, Digital 1.0 - 13.2.7.1 MUST wait response and retry*/
|
|
/* Set the maximum reruns while we will wait for a response */
|
|
cntRerun = ISODEP_MAX_RERUNS;
|
|
|
|
/* Send DSL request and run protocol until get a response, error or "timeout" */
|
|
EXIT_ON_ERR(ret, isoDepHandleControlMsg(ISODEP_S_DSL, RFAL_ISODEP_NO_PARAM));
|
|
do {
|
|
ret = isoDepDataExchangePCD(gIsoDep.rxLen, &dummyB);
|
|
rfalWorker();
|
|
} while(((cntRerun--) != 0U) && (ret == ERR_BUSY));
|
|
|
|
rfalIsoDepInitialize();
|
|
return ((cntRerun == 0U) ? ERR_TIMEOUT : ret);
|
|
}
|
|
|
|
#endif /* RFAL_FEATURE_ISO_DEP_POLL */
|
|
|
|
/*******************************************************************************/
|
|
uint32_t rfalIsoDepFWI2FWT(uint8_t fwi) {
|
|
uint32_t result;
|
|
uint8_t tmpFWI;
|
|
|
|
tmpFWI = fwi;
|
|
|
|
/* RFU values -> take the default value
|
|
* Digital 1.0 11.6.2.17 FWI[1,14]
|
|
* Digital 1.1 7.6.2.22 FWI[0,14]
|
|
* EMVCo 2.6 Table A.5 FWI[0,14] */
|
|
if(tmpFWI > ISODEP_FWI_MAX) {
|
|
tmpFWI = RFAL_ISODEP_FWI_DEFAULT;
|
|
}
|
|
|
|
/* FWT = (256 x 16/fC) x 2^FWI => 2^(FWI+12) Digital 1.1 13.8.1 & 7.9.1 */
|
|
|
|
result = ((uint32_t)1U << (tmpFWI + 12U));
|
|
result = MIN(RFAL_ISODEP_MAX_FWT, result); /* Maximum Frame Waiting Time must be fulfilled */
|
|
|
|
return result;
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
uint16_t rfalIsoDepFSxI2FSx(uint8_t FSxI) {
|
|
uint16_t fsx;
|
|
uint8_t fsi;
|
|
|
|
/* Enforce maximum FSxI/FSx allowed - NFC Forum and EMVCo differ */
|
|
fsi =
|
|
((gIsoDep.compMode == RFAL_COMPLIANCE_MODE_EMV) ? MIN(FSxI, RFAL_ISODEP_FSDI_MAX_EMV) :
|
|
MIN(FSxI, RFAL_ISODEP_FSDI_MAX_NFC));
|
|
|
|
switch(fsi) {
|
|
case(uint8_t)RFAL_ISODEP_FSXI_16:
|
|
fsx = (uint16_t)RFAL_ISODEP_FSX_16;
|
|
break;
|
|
case(uint8_t)RFAL_ISODEP_FSXI_24:
|
|
fsx = (uint16_t)RFAL_ISODEP_FSX_24;
|
|
break;
|
|
case(uint8_t)RFAL_ISODEP_FSXI_32:
|
|
fsx = (uint16_t)RFAL_ISODEP_FSX_32;
|
|
break;
|
|
case(uint8_t)RFAL_ISODEP_FSXI_40:
|
|
fsx = (uint16_t)RFAL_ISODEP_FSX_40;
|
|
break;
|
|
case(uint8_t)RFAL_ISODEP_FSXI_48:
|
|
fsx = (uint16_t)RFAL_ISODEP_FSX_48;
|
|
break;
|
|
case(uint8_t)RFAL_ISODEP_FSXI_64:
|
|
fsx = (uint16_t)RFAL_ISODEP_FSX_64;
|
|
break;
|
|
case(uint8_t)RFAL_ISODEP_FSXI_96:
|
|
fsx = (uint16_t)RFAL_ISODEP_FSX_96;
|
|
break;
|
|
case(uint8_t)RFAL_ISODEP_FSXI_128:
|
|
fsx = (uint16_t)RFAL_ISODEP_FSX_128;
|
|
break;
|
|
case(uint8_t)RFAL_ISODEP_FSXI_256:
|
|
fsx = (uint16_t)RFAL_ISODEP_FSX_256;
|
|
break;
|
|
case(uint8_t)RFAL_ISODEP_FSXI_512:
|
|
fsx = (uint16_t)RFAL_ISODEP_FSX_512;
|
|
break;
|
|
case(uint8_t)RFAL_ISODEP_FSXI_1024:
|
|
fsx = (uint16_t)RFAL_ISODEP_FSX_1024;
|
|
break;
|
|
case(uint8_t)RFAL_ISODEP_FSXI_2048:
|
|
fsx = (uint16_t)RFAL_ISODEP_FSX_2048;
|
|
break;
|
|
case(uint8_t)RFAL_ISODEP_FSXI_4096:
|
|
fsx = (uint16_t)RFAL_ISODEP_FSX_4096;
|
|
break;
|
|
default:
|
|
fsx = (uint16_t)RFAL_ISODEP_FSX_256;
|
|
break;
|
|
}
|
|
return fsx;
|
|
}
|
|
|
|
#if RFAL_FEATURE_ISO_DEP_LISTEN
|
|
|
|
/*******************************************************************************/
|
|
bool rfalIsoDepIsRats(const uint8_t* buf, uint8_t bufLen) {
|
|
if(buf != NULL) {
|
|
if((RFAL_ISODEP_CMD_RATS == (uint8_t)*buf) && (sizeof(rfalIsoDepRats) == bufLen)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
bool rfalIsoDepIsAttrib(const uint8_t* buf, uint8_t bufLen) {
|
|
if(buf != NULL) {
|
|
if((RFAL_ISODEP_CMD_ATTRIB == (uint8_t)*buf) &&
|
|
(RFAL_ISODEP_ATTRIB_REQ_MIN_LEN <= bufLen) &&
|
|
((RFAL_ISODEP_ATTRIB_REQ_MIN_LEN + RFAL_ISODEP_ATTRIB_HLINFO_LEN) >= bufLen)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
ReturnCode rfalIsoDepListenStartActivation(
|
|
rfalIsoDepAtsParam* atsParam,
|
|
const rfalIsoDepAttribResParam* attribResParam,
|
|
const uint8_t* buf,
|
|
uint16_t bufLen,
|
|
rfalIsoDepListenActvParam actParam) {
|
|
uint8_t* txBuf;
|
|
uint8_t bufIt;
|
|
const uint8_t* buffer = buf;
|
|
|
|
/*******************************************************************************/
|
|
bufIt = 0;
|
|
txBuf =
|
|
(uint8_t*)actParam
|
|
.rxBuf; /* Use the rxBuf as TxBuf as well, the struct enforces a size enough MAX( NFCA_ATS_MAX_LEN, NFCB_ATTRIB_RES_MAX_LEN ) */
|
|
gIsoDep.txBR = RFAL_BR_106;
|
|
gIsoDep.rxBR = RFAL_BR_106;
|
|
|
|
/* Check for a valid buffer pointer */
|
|
if(buffer == NULL) {
|
|
return ERR_PARAM;
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
if(*buffer == RFAL_ISODEP_CMD_RATS) {
|
|
/* Check ATS parameters */
|
|
if(atsParam == NULL) {
|
|
return ERR_PARAM;
|
|
}
|
|
|
|
/* If requested copy RATS to device info */
|
|
if(actParam.isoDepDev != NULL) {
|
|
ST_MEMCPY(
|
|
(uint8_t*)&actParam.isoDepDev->activation.A.Poller.RATS,
|
|
buffer,
|
|
sizeof(rfalIsoDepRats)); /* Copy RATS' CMD + PARAM */
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
/* Process RATS */
|
|
buffer++;
|
|
gIsoDep.fsx = rfalIsoDepFSxI2FSx(
|
|
(((*buffer) & RFAL_ISODEP_RATS_PARAM_FSDI_MASK) >> RFAL_ISODEP_RATS_PARAM_FSDI_SHIFT));
|
|
gIsoDep.did = (*buffer & RFAL_ISODEP_DID_MASK);
|
|
|
|
/*******************************************************************************/
|
|
/* Digital 1.1 13.6.1.8 - DID as to between 0 and 14 */
|
|
if(gIsoDep.did > RFAL_ISODEP_DID_MAX) {
|
|
return ERR_PROTO;
|
|
}
|
|
|
|
/* Check if we are configured to support DID */
|
|
if((gIsoDep.did != RFAL_ISODEP_DID_00) && (!atsParam->didSupport)) {
|
|
return ERR_NOTSUPP;
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
/* Check RFAL supported bit rates */
|
|
if((!(RFAL_SUPPORT_BR_CE_A_212) &&
|
|
(((atsParam->ta & RFAL_ISODEP_ATS_TA_DPL_212) != 0U) ||
|
|
((atsParam->ta & RFAL_ISODEP_ATS_TA_DLP_212) != 0U))) ||
|
|
(!(RFAL_SUPPORT_BR_CE_A_424) &&
|
|
(((atsParam->ta & RFAL_ISODEP_ATS_TA_DPL_424) != 0U) ||
|
|
((atsParam->ta & RFAL_ISODEP_ATS_TA_DLP_424) != 0U))) ||
|
|
(!(RFAL_SUPPORT_BR_CE_A_848) &&
|
|
(((atsParam->ta & RFAL_ISODEP_ATS_TA_DPL_848) != 0U) ||
|
|
((atsParam->ta & RFAL_ISODEP_ATS_TA_DLP_848) != 0U)))) {
|
|
return ERR_NOTSUPP;
|
|
}
|
|
|
|
/* Enforce proper FWI configuration */
|
|
if(atsParam->fwi > ISODEP_FWI_LIS_MAX) {
|
|
atsParam->fwi = ISODEP_FWI_LIS_MAX;
|
|
}
|
|
|
|
gIsoDep.atsTA = atsParam->ta;
|
|
gIsoDep.fwt = rfalIsoDepFWI2FWT(atsParam->fwi);
|
|
gIsoDep.ourFsx = rfalIsoDepFSxI2FSx(atsParam->fsci);
|
|
|
|
/* Ensure proper/maximum Historical Bytes length */
|
|
atsParam->hbLen = MIN(RFAL_ISODEP_ATS_HB_MAX_LEN, atsParam->hbLen);
|
|
|
|
/*******************************************************************************/
|
|
/* Compute ATS */
|
|
|
|
txBuf[bufIt++] = (RFAL_ISODEP_ATS_HIST_OFFSET + atsParam->hbLen); /* TL */
|
|
txBuf[bufIt++] =
|
|
((RFAL_ISODEP_ATS_T0_TA_PRESENCE_MASK | RFAL_ISODEP_ATS_T0_TB_PRESENCE_MASK |
|
|
RFAL_ISODEP_ATS_T0_TC_PRESENCE_MASK) |
|
|
atsParam->fsci); /* T0 */
|
|
txBuf[bufIt++] = atsParam->ta; /* TA */
|
|
txBuf[bufIt++] =
|
|
((atsParam->fwi << RFAL_ISODEP_RATS_PARAM_FSDI_SHIFT) |
|
|
(atsParam->sfgi & RFAL_ISODEP_RATS_PARAM_FSDI_MASK)); /* TB */
|
|
txBuf[bufIt++] = (uint8_t)((atsParam->didSupport) ? RFAL_ISODEP_ATS_TC_DID : 0U); /* TC */
|
|
|
|
if(atsParam->hbLen > 0U) /* MISRA 21.18 */
|
|
{
|
|
ST_MEMCPY(&txBuf[bufIt], atsParam->hb, atsParam->hbLen); /* T1-Tk */
|
|
bufIt += atsParam->hbLen;
|
|
}
|
|
|
|
gIsoDep.state = ISODEP_ST_PICC_ACT_ATS;
|
|
|
|
}
|
|
/*******************************************************************************/
|
|
else if(*buffer == RFAL_ISODEP_CMD_ATTRIB) {
|
|
/* Check ATTRIB parameters */
|
|
if(attribResParam == NULL) {
|
|
return ERR_PARAM;
|
|
}
|
|
|
|
/* REMARK: ATTRIB handling */
|
|
NO_WARNING(attribResParam);
|
|
NO_WARNING(bufLen);
|
|
return ERR_NOT_IMPLEMENTED;
|
|
} else {
|
|
return ERR_PARAM;
|
|
}
|
|
|
|
gIsoDep.actvParam = actParam;
|
|
|
|
/*******************************************************************************/
|
|
/* If requested copy to ISO-DEP device info */
|
|
if(actParam.isoDepDev != NULL) {
|
|
actParam.isoDepDev->info.DID = gIsoDep.did;
|
|
actParam.isoDepDev->info.FSx = gIsoDep.fsx;
|
|
actParam.isoDepDev->info.FWT = gIsoDep.fwt;
|
|
actParam.isoDepDev->info.dFWT = 0;
|
|
actParam.isoDepDev->info.DSI = gIsoDep.txBR;
|
|
actParam.isoDepDev->info.DRI = gIsoDep.rxBR;
|
|
}
|
|
|
|
return rfalTransceiveBlockingTx(
|
|
txBuf,
|
|
bufIt,
|
|
(uint8_t*)actParam.rxBuf,
|
|
sizeof(rfalIsoDepBufFormat),
|
|
actParam.rxLen,
|
|
RFAL_TXRX_FLAGS_DEFAULT,
|
|
RFAL_FWT_NONE);
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
ReturnCode rfalIsoDepListenGetActivationStatus(void) {
|
|
ReturnCode err;
|
|
uint8_t* txBuf;
|
|
uint8_t bufIt;
|
|
|
|
rfalBitRate dsi;
|
|
rfalBitRate dri;
|
|
|
|
/* Check if Activation is running */
|
|
if(gIsoDep.state < ISODEP_ST_PICC_ACT_ATS) {
|
|
return ERR_WRONG_STATE;
|
|
}
|
|
|
|
/* Check if Activation has finished already */
|
|
if(gIsoDep.state >= ISODEP_ST_PICC_RX) {
|
|
return ERR_NONE;
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
/* Check for incoming msg */
|
|
err = rfalGetTransceiveStatus();
|
|
switch(err) {
|
|
/*******************************************************************************/
|
|
case ERR_NONE:
|
|
break;
|
|
|
|
/*******************************************************************************/
|
|
case ERR_LINK_LOSS:
|
|
ase |