flipperzero-firmware/lib/ST25RFAL002/source/rfal_nfcf.c

586 lines
22 KiB
C
Raw Normal View History

/******************************************************************************
* \attention
*
* <h2><center>&copy; 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: ST25R391x firmware
* Revision:
* LANGUAGE: ISO C99
*/
/*! \file rfal_nfcf.c
*
* \author Gustavo Patricio
*
* \brief Implementation of NFC-F Poller (FeliCa PCD) device
*
* The definitions and helpers methods provided by this module are
* aligned with NFC-F (FeliCa - JIS X6319-4)
*
*/
/*
******************************************************************************
* INCLUDES
******************************************************************************
*/
#include "rfal_nfcf.h"
#include "utils.h"
/*
******************************************************************************
* ENABLE SWITCH
******************************************************************************
*/
#ifndef RFAL_FEATURE_NFCF
#define RFAL_FEATURE_NFCF false /* NFC-F module configuration missing. Disabled by default */
#endif
#if RFAL_FEATURE_NFCF
/*
******************************************************************************
* GLOBAL DEFINES
******************************************************************************
*/
#define RFAL_NFCF_SENSF_REQ_LEN_MIN \
5U /*!< SENSF_RES minimum length */
#define RFAL_NFCF_READ_WO_ENCRYPTION_MIN_LEN \
15U /*!< Minimum length for a Check Command T3T 5.4.1 */
#define RFAL_NFCF_WRITE_WO_ENCRYPTION_MIN_LEN \
31U /*!< Minimum length for an Update Command T3T 5.5.1 */
#define RFAL_NFCF_CHECK_RES_MIN_LEN \
11U /*!< CHECK Response minimum length T3T 1.0 Table 8 */
#define RFAL_NFCF_UPDATE_RES_MIN_LEN \
11U /*!< UPDATE Response minimum length T3T 1.0 Table 8 */
#define RFAL_NFCF_CHECK_REQ_MAX_LEN \
86U /*!< Max length of a Check request T3T 1.0 Table 7 */
#define RFAL_NFCF_CHECK_REQ_MAX_SERV \
15U /*!< Max Services number on Check request T3T 1.0 5.4.1.5 */
#define RFAL_NFCF_CHECK_REQ_MAX_BLOCK \
15U /*!< Max Blocks number on Check request T3T 1.0 5.4.1.10 */
#define RFAL_NFCF_UPDATE_REQ_MAX_SERV \
15U /*!< Max Services number Update request T3T 1.0 5.4.1.5 */
#define RFAL_NFCF_UPDATE_REQ_MAX_BLOCK \
13U /*!< Max Blocks number on Update request T3T 1.0 5.4.1.10 */
/*! MRT Check | Uupdate = (Tt3t x ((A+1) + n (B+1)) x 4^E) + dRWTt3t T3T 5.8
Max values used: A = 7 ; B = 7 ; E = 3 ; n = 15 (NFC Forum n = 15, JIS n = 32)
*/
#define RFAL_NFCF_MRT_CHECK_UPDATE ((4096 * (8 + (15 * 8)) * 64) + 16)
/*
******************************************************************************
* GLOBAL MACROS
******************************************************************************
*/
#define rfalNfcfSlots2CardNum(s) \
((uint8_t)(s) + 1U) /*!< Converts Time Slot Number (TSN) into num of slots */
/*
******************************************************************************
* GLOBAL TYPES
******************************************************************************
*/
/*! Structure/Buffer to hold the SENSF_RES with LEN byte prepended */
typedef struct {
uint8_t LEN; /*!< NFC-F LEN byte */
rfalNfcfSensfRes SENSF_RES; /*!< SENSF_RES */
} rfalNfcfSensfResBuf;
/*! Greedy collection for NFCF GRE_POLL_F Activity 1.0 Table 10 */
typedef struct {
uint8_t pollFound; /*!< Number of devices found by the Poll */
uint8_t pollCollision; /*!< Number of collisions detected */
rfalFeliCaPollRes POLL_F[RFAL_NFCF_POLL_MAXCARDS]; /*!< GRE_POLL_F Activity 1.0 Table 10 */
} rfalNfcfGreedyF;
/*! NFC-F SENSF_REQ format Digital 1.1 8.6.1 */
typedef struct {
uint8_t CMD; /*!< Command code: 00h */
uint8_t SC[RFAL_NFCF_SENSF_SC_LEN]; /*!< System Code */
uint8_t RC; /*!< Request Code */
uint8_t TSN; /*!< Time Slot Number */
} rfalNfcfSensfReq;
/*
******************************************************************************
* LOCAL VARIABLES
******************************************************************************
*/
static rfalNfcfGreedyF gRfalNfcfGreedyF; /*!< Activity's NFCF Greedy collection */
/*
******************************************************************************
* LOCAL FUNCTION PROTOTYPES
******************************************************************************
*/
static void rfalNfcfComputeValidSENF(
rfalNfcfListenDevice* outDevInfo,
uint8_t* curDevIdx,
uint8_t devLimit,
bool overwrite,
bool* nfcDepFound);
/*
******************************************************************************
* LOCAL VARIABLES
******************************************************************************
*/
/*******************************************************************************/
static void rfalNfcfComputeValidSENF(
rfalNfcfListenDevice* outDevInfo,
uint8_t* curDevIdx,
uint8_t