blob: a2291fc62d8209666b6b48dd47258679b817e24d [file] [log] [blame]
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* NFC Ndef Mapping For Remote Devices.
*
*/
#include <phFriNfc_MifStdFormat.h>
#include <phNfcCompId.h>
#include <phNxpExtns_MifareStd.h>
#include <phFriNfc_MifareStdMap.h>
/**************** local methods used in this file only ************************/
static NFCSTATUS phFriNfc_MifStd_H_RdABlock (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_WrABlock (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_AuthSector (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd4k_H_CheckNdef (phFriNfc_NdefMap_t *NdefMap);
static void phFriNfc_MifStd_H_fillAIDarray (phFriNfc_NdefMap_t *NdefMap);
static uint8_t phFriNfc_MifStd_H_GetSect (uint8_t BlockNumber);
static NFCSTATUS phFriNfc_MifStd_H_BlkChk (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_RdAcsBit (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_ChkAcsBit (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_ChkRdWr (phFriNfc_NdefMap_t *NdefMap);
static void phFriNfc_MifStd_H_ChkNdefCmpltSects (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_RemainTLV (phFriNfc_NdefMap_t *NdefMap,
uint8_t *Flag,
uint8_t *Temp16Bytes);
static NFCSTATUS phFriNfc_MifStd_H_ChkIntLen (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_IntLenWioutNdef (phFriNfc_NdefMap_t *NdefMap,
uint8_t *Flag,
uint8_t *TempintBytes);
static uint8_t phFriNfc_MifStd_H_UpdateTLV (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_WriteNdefLen (phFriNfc_NdefMap_t *NdefMap);
static void phFriNfc_MifStd_H_SetNdefBlkAuth (phFriNfc_NdefMap_t *NdefMap);
static void phFriNfc_MifStd_H_RdWrReset (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_RdtoWrNdefLen (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_GetActCardLen (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_ChkTLVs (phFriNfc_NdefMap_t *NdefMap,
uint8_t *CRFlag);
static NFCSTATUS phFriNfc_MifStd_H_GetNxtTLV (phFriNfc_NdefMap_t *NdefMap,
uint16_t *TempLength,
uint8_t *TL4bytesFlag);
static NFCSTATUS phFriNfc_MifStd_H_Chk16Bytes (phFriNfc_NdefMap_t *NdefMap,
uint16_t TempLength);
static NFCSTATUS phFriNfc_MifStd_H_ChkRemainTLVs (phFriNfc_NdefMap_t *NdefMap,
uint8_t *CRFlag,
uint8_t *NDEFFlag);
static void phFriNfc_MifStd_H_Complete (phFriNfc_NdefMap_t *NdefMap,
NFCSTATUS Result);
static void phFriNfc_MifStd_H_Get1kStTrail (phFriNfc_NdefMap_t *NdefMap);
static void phFriNfc_MifStd_H_Get4kStTrail (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_ProChkNdef (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_ProAuth (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_Rd16Bytes (phFriNfc_NdefMap_t *NdefMap,
uint8_t BlockNo);
static NFCSTATUS phFriNfc_MifStd_H_ProAcsBits (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_GPBChk (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_ProStatNotValid (phFriNfc_NdefMap_t *NdefMap,
NFCSTATUS status);
static NFCSTATUS phFriNfc_MifStd_H_RdBeforeWr (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_ProBytesToWr (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_fillSendBuf (phFriNfc_NdefMap_t *NdefMap,
uint8_t Length);
static NFCSTATUS phFriNfc_MifStd_H_WrTLV (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_ProWrTLV (phFriNfc_NdefMap_t *NdefMap);
static uint8_t phFriNfc_MifStd_H_UpdRemTLV (phFriNfc_NdefMap_t *NdefMap);
static void phFriNfc_MifStd_H_fillTLV1 (phFriNfc_NdefMap_t *NdefMap);
static void phFriNfc_MifStd_H_fillTLV2 (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_CallWrNdefLen (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_BlkChk_1 (phFriNfc_NdefMap_t *NdefMap);
static void phFriNfc_MifStd_H_fillTLV1_1 (phFriNfc_NdefMap_t *NdefMap);
static void phFriNfc_MifStd_H_fillTLV2_1 (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_RdTLV (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_ProRdTLV (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_WrTermTLV (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_ProWrABlock (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_CallConnect (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_CallDisCon (phFriNfc_NdefMap_t *NdefMap);
static void phFriNfc_MifStd1k_H_BlkChk (phFriNfc_NdefMap_t *NdefMap,
uint8_t SectorID,
uint8_t *callbreak);
static uint8_t phFriNfc_MifStd_H_GetSectorTrailerBlkNo (uint8_t SectorID);
static NFCSTATUS phFriNfc_MifStd_H_ProSectorTrailorAcsBits (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_WrSectorTrailorBlock (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MifStd_H_ProWrSectorTrailor (phFriNfc_NdefMap_t *NdefMap);
static NFCSTATUS phFriNfc_MapTool_ChkSpcVer (const phFriNfc_NdefMap_t *NdefMap,
uint8_t VersionIndex) __attribute__((unused));
/* Mifare Standard Mapping - Constants */
#define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT1 0xA0 /* internal Authenticate Command for MAD Sector */
#define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT2 0xA1 /* internal Authenticate Command for MAD Sector */
#define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT3 0xA2 /* internal Authenticate Command for MAD Sector */
#define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT4 0xA3 /* internal Authenticate Command for MAD Sector */
#define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT5 0xA4 /* internal Authenticate Command for MAD Sector */
#define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT6 0xA5 /* internal Authenticate Command for MAD Sector */
#define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT1 0xD3 /* internal Authenticate Command for NDEF Sectors 1 */
#define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT2 0xF7 /* internal Authenticate Command for NDEF Sectors 2 */
#define PH_FRINFC_NDEFMAP_MIFARESTD_NDEF_COMPVAL2 0x03 /* internal Ndef Compliant command 1 */
#define PH_FRINFC_NDEFMAP_MIFARESTD_NDEF_COMPVAL1 0xE1 /* internal Ndef Compliant command 2 */
/* Enable access bits check for the MAD sector
#define ENABLE_ACS_BIT_CHK_FOR_MAD */
#define PH_FRINFC_NDEFMAP_MFUL_VAL0 0
/******************************************************************************
* Function phFriNfc_MapTool_SetCardState
*
* Description This function sets the appropriate card state.
*
* Returns This function return NFCSTATUS_SUCCESS in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
NFCSTATUS phFriNfc_MapTool_SetCardState (phFriNfc_NdefMap_t *NdefMap,
uint32_t Length)
{
NFCSTATUS Result = NFCSTATUS_SUCCESS;
if (Length == PH_FRINFC_NDEFMAP_MFUL_VAL0)
{
/* As the NDEF LEN / TLV Len is Zero, irrespective of any state the card
shall be set to INITIALIZED STATE*/
NdefMap->CardState =(uint8_t) (((NdefMap->CardState ==
PH_NDEFMAP_CARD_STATE_READ_ONLY) ||
(NdefMap->CardState ==
PH_NDEFMAP_CARD_STATE_INVALID))?
PH_NDEFMAP_CARD_STATE_INVALID:
PH_NDEFMAP_CARD_STATE_INITIALIZED);
}
else
{
switch (NdefMap->CardState)
{
case PH_NDEFMAP_CARD_STATE_INITIALIZED:
NdefMap->CardState =(uint8_t) ((NdefMap->CardState ==
PH_NDEFMAP_CARD_STATE_INVALID)?
NdefMap->CardState:
PH_NDEFMAP_CARD_STATE_READ_WRITE);
break;
case PH_NDEFMAP_CARD_STATE_READ_ONLY:
NdefMap->CardState = (uint8_t) ((NdefMap->CardState ==
PH_NDEFMAP_CARD_STATE_INVALID)?
NdefMap->CardState:
PH_NDEFMAP_CARD_STATE_READ_ONLY);
break;
case PH_NDEFMAP_CARD_STATE_READ_WRITE:
NdefMap->CardState = (uint8_t) ((NdefMap->CardState ==
PH_NDEFMAP_CARD_STATE_INVALID)?
NdefMap->CardState:
PH_NDEFMAP_CARD_STATE_READ_WRITE);
if (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD ||
NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD ||
NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD)
{
if(NdefMap->StdMifareContainer.ReadOnlySectorIndex &&
NdefMap->StdMifareContainer.SectorTrailerBlockNo == NdefMap->StdMifareContainer.currentBlock )
{
NdefMap->CardState = (uint8_t) ((NdefMap->CardState ==
PH_NDEFMAP_CARD_STATE_INVALID)?
NdefMap->CardState:
PH_NDEFMAP_CARD_STATE_READ_ONLY);
}
}
break;
default:
NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID;
Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_NO_NDEF_SUPPORT);
break;
}
}
Result = ((NdefMap->CardState ==
PH_NDEFMAP_CARD_STATE_INVALID)?
PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_NO_NDEF_SUPPORT):
Result);
return Result;
}
/******************************************************************************
* Function phFriNfc_MifareStdMap_H_Reset
*
* Description This function resets the component instance to the initial
* state and lets the component forget about the list of
* registered items. Moreover, the lower device is set.
*
* Returns This function return NFCSTATUS_SUCCESS in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
NFCSTATUS phFriNfc_MifareStdMap_H_Reset (phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS status = NFCSTATUS_SUCCESS;
uint8_t index = PH_FRINFC_MIFARESTD_VAL0;
if (NdefMap == NULL)
{
status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
}
else
{
/* Current Block stores the present block accessed in the card */
NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_VAL0;
for (index = PH_FRINFC_MIFARESTD_VAL0; index < PH_FRINFC_NDEFMAP_MIFARESTD_ST15_BYTES; index++)
{
/* internal buffer to store the odd bytes of length < 15 */
NdefMap->StdMifareContainer.internalBuf[index] = PH_FRINFC_MIFARESTD_VAL0;
}
for (index = 0; index < PH_FRINFC_NDEFMAP_MIFARESTD_TOTALNO_BLK; index++)
{
/* aid buffer reset to non ndef compliant */
NdefMap->StdMifareContainer.aid[index] = PH_FRINFC_MIFARESTD_NON_NDEF_COMP;
}
/* odd bytes length stored in the internal buffer */
NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INITIALIZED;
/* Flag to get that last few bytes are taken from the user buffer */
NdefMap->StdMifareContainer.RemainingBufFlag = PH_FRINFC_MIFARESTD_FLAG0;
/* Flag to find that the read/write operation has reached the end of the card.
Further reading/writing is not possible */
NdefMap->StdMifareContainer.ReadWriteCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0;
/* Flag to get that last few bytes are taken from the internal buffer */
NdefMap->StdMifareContainer.internalBufFlag = PH_FRINFC_MIFARESTD_FLAG0;
/* Authentication Flag for every sector */
NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0;
/* Used in Check Ndef for storing the sector ID */
NdefMap->StdMifareContainer.SectorIndex = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->StdMifareContainer.NdefBlocks = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->StdMifareContainer.NoOfNdefCompBlocks = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->StdMifareContainer.ReadAcsBitFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.remSizeUpdFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->TLVStruct.NoLbytesinTLV = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->TLVStruct.prevLenByteValue = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->TLVStruct.NdefTLVBlock = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->StdMifareContainer.remainingSize = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->StdMifareContainer.ReadNdefFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.WrNdefFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.ChkNdefFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.aidCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.NFCforumSectFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.ProprforumSectFlag = PH_FRINFC_MIFARESTD_PROP_1ST_CONFIG;
NdefMap->StdMifareContainer.ReadCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.FirstReadFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.WrLength = PH_FRINFC_MIFARESTD_VAL1;
NdefMap->StdMifareContainer.ChkNdefCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.ReadOnlySectorIndex = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.TotalNoSectors = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.SectorTrailerBlockNo = PH_FRINFC_MIFARESTD_FLAG0;
}
return status;
}
/******************************************************************************
* Function phFriNfc_MifareStdMap_ChkNdef
*
* Description The function checks whether the peer device is NDEF compliant.
*
* Returns This function return NFCSTATUS_PENDING in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
NFCSTATUS phFriNfc_MifareStdMap_ChkNdef ( phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS status = NFCSTATUS_PENDING;
uint8_t atq, sak;
if (NdefMap == NULL)
{
status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
}
else
{
NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_CHECK_OPE;
NdefMap->StdMifareContainer.CRIndex = PH_FRINFC_NDEFMAP_CR_CHK_NDEF;
/* Get the Select Response and Sense Response to get
the exact Card Type either Mifare 1k or 4k */
sak = NdefMap->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak;
atq = NdefMap->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AtqA[0];
if (0x08 == (sak & 0x18))
{
/* Total Number of Blocks in Mifare 1k Card */
NdefMap->StdMifareContainer.NoOfNdefCompBlocks =
PH_FRINFC_NDEFMAP_MIFARESTD_1KNDEF_COMPBLOCK;
NdefMap->StdMifareContainer.remainingSize =
((NdefMap->CardType == PH_FRINFC_MIFARESTD_VAL0) ?
(PH_FRINFC_NDEFMAP_MIFARESTD_1KNDEF_COMPBLOCK *
PH_FRINFC_MIFARESTD_BLOCK_BYTES) :
NdefMap->StdMifareContainer.remainingSize);
NdefMap->CardType = PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD;
}
else if (0x19 == (sak & 0x19))
{
/* Total Number of Blocks in Mifare 2k Card */
NdefMap->StdMifareContainer.NoOfNdefCompBlocks =
PH_FRINFC_NDEFMAP_MIFARESTD_2KNDEF_COMPBLOCK;
NdefMap->StdMifareContainer.remainingSize =
((NdefMap->CardType == PH_FRINFC_MIFARESTD_VAL0) ?
(PH_FRINFC_NDEFMAP_MIFARESTD_2KNDEF_COMPBLOCK *
PH_FRINFC_MIFARESTD_BLOCK_BYTES) :
NdefMap->StdMifareContainer.remainingSize);
NdefMap->CardType = PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD;
}
else
{
/* Total Number of Blocks in Mifare 4k Card */
NdefMap->StdMifareContainer.NoOfNdefCompBlocks =
PH_FRINFC_NDEFMAP_MIFARESTD_4KNDEF_COMPBLOCK;
NdefMap->StdMifareContainer.remainingSize =
((NdefMap->CardType == PH_FRINFC_MIFARESTD_VAL0) ?
(PH_FRINFC_NDEFMAP_MIFARESTD_4KNDEF_COMPBLOCK *
PH_FRINFC_MIFARESTD_BLOCK_BYTES) :
NdefMap->StdMifareContainer.remainingSize);
NdefMap->CardType = PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD;
}
/* phFriNfc_MifareStdMap_ChkNdef should be called only
when currentBlock is 0 OR 64,65 and 66 (for Mifare 4k).
Otherwise return error */
/* and also Check the Authentication Flag */
if ((NdefMap->StdMifareContainer.currentBlock != 0) &&
(NdefMap->StdMifareContainer.currentBlock != 1) &&
(NdefMap->StdMifareContainer.currentBlock != 2) &&
(NdefMap->StdMifareContainer.currentBlock != 64) &&
(NdefMap->StdMifareContainer.currentBlock != 65) &&
(NdefMap->StdMifareContainer.currentBlock != 66))
{
status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
}
else if ( NdefMap->StdMifareContainer.AuthDone == 0)
{
/* Block 0 contains Manufacturer information and
also other informaton. So go for block 1 which
contains AIDs. Authenticating any of the block
in a sector, Authenticates the whole sector */
if (NdefMap->StdMifareContainer.currentBlock == 0)
{
NdefMap->StdMifareContainer.currentBlock = 1;
}
status = phFriNfc_MifStd_H_AuthSector (NdefMap);
}
else
{
/**
* Mifare 1k, sak = 0x08 atq = 0x04
* Mifare 2k, sak = 0x19 atq = 0x02
* Mifare 4k, sak = 0x18 atq = 0x02
*/
if ((NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD) ||
(NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD) ||
(NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD))
{
/* Change the state to Check Ndef Compliant */
NdefMap->State = PH_FRINFC_NDEFMAP_STATE_CHK_NDEF_COMP;
NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_CHECK_OPE;
NdefMap->StdMifareContainer.ChkNdefFlag = PH_FRINFC_MIFARESTD_FLAG1;
NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process;
NdefMap->MapCompletionInfo.Context = NdefMap;
NdefMap->Cmd.MfCmd = phHal_eMifareRead;
*NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
NdefMap->SendRecvBuf [0] = NdefMap->StdMifareContainer.currentBlock;
NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_READ;
/* Call the Overlapped HAL Transceive function */
status = phFriNfc_ExtnsTransceive (NdefMap->pTransceiveInfo,
NdefMap->Cmd,
NdefMap->SendRecvBuf,
NdefMap->SendLength,
NdefMap->SendRecvLength);
}
else
{
/* Since we have decided temporarily not to go
for any new error codes we are using
NFCSTATUS_INVALID_PARAMETER even though it is not
the relevant error code here TBD */
status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
}
}
}
return status;
}
/******************************************************************************
* Function phFriNfc_MifareStdMap_RdNdef
*
* Description The function initiates the reading of NDEF information from
* a Remote Device. It performs a reset of the state and starts
* the action (state machine). A periodic call of the
* phFriNfcNdefMap_Process has to be done once the action
* has been triggered.
*
* Returns This function return NFCSTATUS_PENDING in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
NFCSTATUS phFriNfc_MifareStdMap_RdNdef (phFriNfc_NdefMap_t *NdefMap,
uint8_t *PacketData,
uint32_t *PacketDataLength,
uint8_t Offset)
{
NFCSTATUS status = NFCSTATUS_PENDING;
NdefMap->ApduBufferSize = *PacketDataLength;
NdefMap->NumOfBytesRead = PacketDataLength;
*NdefMap->NumOfBytesRead = 0;
NdefMap->ApduBuffIndex = 0;
NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_READ_OPE;
NdefMap->StdMifareContainer.CRIndex = PH_FRINFC_NDEFMAP_CR_RD_NDEF;
if ((NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID)
|| (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INITIALIZED))
{
/* Card state is not correct */
status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_INVALID_PARAMETER);
}
else
{
if ((Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN) || ( NdefMap->PrevOperation ==
PH_FRINFC_NDEFMAP_WRITE_OPE))
{
phFriNfc_MifStd_H_RdWrReset (NdefMap);
NdefMap->StdMifareContainer.ReadNdefFlag = PH_FRINFC_MIFARESTD_FLAG1;
NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0;
}
/* Offset = Current, but the read has reached the End of Card */
if ((Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) &&
(NdefMap->StdMifareContainer.ReadWriteCompleteFlag ==
PH_FRINFC_MIFARESTD_FLAG1))
{
status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_EOF_NDEF_CONTAINER_REACHED);
}
else
{
NdefMap->Offset = (((Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN) &&
(NdefMap->PrevOperation ==
PH_FRINFC_NDEFMAP_WRITE_OPE)) ?
PH_FRINFC_NDEFMAP_SEEK_BEGIN :
Offset);
status = phFriNfc_MifStd_H_BlkChk (NdefMap);
if (status == NFCSTATUS_SUCCESS)
{
NdefMap->ApduBuffer = PacketData;
/* Read Operation in Progress */
NdefMap->StdMifareContainer.ReadWriteCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0;
/* Check Authentication Flag */
status =
((NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1) ?
phFriNfc_MifStd_H_RdABlock(NdefMap) :
phFriNfc_MifStd_H_AuthSector(NdefMap));
}
}
}
return status;
}
/******************************************************************************
* Function phFriNfc_MifareStdMap_WrNdef
*
* Description The function initiates the writing of NDEF information to
* a Remote Device. It performs a reset of the state and starts
* the action (state machine). A periodic call of the
* phFriNfcNdefMap_Process has to be done once the action
* has been triggered.
*
* Returns This function return NFCSTATUS_PENDING in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
NFCSTATUS phFriNfc_MifareStdMap_WrNdef (phFriNfc_NdefMap_t *NdefMap,
uint8_t *PacketData,
uint32_t *PacketDataLength,
uint8_t Offset)
{
NFCSTATUS status = NFCSTATUS_PENDING;
NdefMap->ApduBuffer = PacketData;
NdefMap->ApduBufferSize = *PacketDataLength;
NdefMap->ApduBuffIndex = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->WrNdefPacketLength = PacketDataLength;
*NdefMap->WrNdefPacketLength = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_WRITE_OPE;
NdefMap->StdMifareContainer.CRIndex = PH_FRINFC_NDEFMAP_CR_WR_NDEF;
if ((NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID)
|| (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY))
{
/* Card state is not correct */
status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_INVALID_PARAMETER);
}
else
{
if ((Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN) ||
(NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_READ_OPE))
{
NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.RdBeforeWrFlag = PH_FRINFC_MIFARESTD_FLAG1;
NdefMap->StdMifareContainer.WrNdefFlag = PH_FRINFC_MIFARESTD_FLAG1;
NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->StdMifareContainer.RdAfterWrFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->TLVStruct.NdefTLVAuthFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.FirstReadFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.remainingSize =
(NdefMap->StdMifareContainer.NoOfNdefCompBlocks *
PH_FRINFC_MIFARESTD_BLOCK_BYTES);
NdefMap->StdMifareContainer.currentBlock =
PH_FRINFC_MIFARESTD_BLK4;
NdefMap->StdMifareContainer.NdefBlocks = PH_FRINFC_MIFARESTD_VAL1;
NdefMap->StdMifareContainer.NFCforumSectFlag = PH_FRINFC_MIFARESTD_FLAG0;
/* This macro is added, to be compliant with the previous HAL 2.0
For HAL 2.0, polling is done before writing data to the mifare
std (if the offset is BEGIN), because if an error is reported
during read or write and again write is called, the PN531 state is
unchanged (so write will fail), to bring the PN531 to the correct
state, polling is done.
Changed on 13th Jan 2009
*/
NdefMap->StdMifareContainer.PollFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.WrLength = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->StdMifareContainer.FirstWriteFlag = PH_FRINFC_MIFARESTD_FLAG1;
}
if (((Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) &&
(NdefMap->StdMifareContainer.ReadWriteCompleteFlag ==
PH_FRINFC_MIFARESTD_FLAG1)) || ((NdefMap->StdMifareContainer.PollFlag ==
PH_FRINFC_MIFARESTD_FLAG1) && (Offset == PH_FRINFC_NDEFMAP_SEEK_CUR)))
{
/* Offset = Current, but the read has reached the End of Card */
status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_EOF_NDEF_CONTAINER_REACHED);
}
else
{
NdefMap->Offset = (((Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN) &&
(NdefMap->PrevOperation ==
PH_FRINFC_NDEFMAP_READ_OPE)) ?
PH_FRINFC_NDEFMAP_SEEK_BEGIN :
Offset);
NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0;
status = phFriNfc_MifStd_H_BlkChk (NdefMap);
NdefMap->StdMifareContainer.ReadWriteCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0;
if (status == NFCSTATUS_SUCCESS)
{
if (NdefMap->StdMifareContainer.PollFlag ==
PH_FRINFC_MIFARESTD_FLAG1)
{
/* if poll flag is set then call disconnect because the authentication
has failed so reactivation of card is required */
status = phFriNfc_MifStd_H_CallDisCon (NdefMap);
}
/* Check Authentication Flag */
else if (NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1)
{
status = ((NdefMap->Offset ==
PH_FRINFC_NDEFMAP_SEEK_BEGIN) ?
phFriNfc_MifStd_H_RdBeforeWr (NdefMap) :
phFriNfc_MifStd_H_WrABlock (NdefMap));
}
else
{
status = phFriNfc_MifStd_H_AuthSector (NdefMap);
}
}
}
}
return status;
}
/******************************************************************************
* Function phFriNfc_MifareStdMap_Process
*
* Description This function is a Completion Routine, Processing function,
* needed to avoid long blocking.
* This function as a Completion Routine in order to be able
* to notify the component that an I/O has finished and data
* are ready to be processed.
* Returns void
*
******************************************************************************/
void phFriNfc_MifareStdMap_Process (void *Context,
NFCSTATUS Status)
{
phFriNfc_NdefMap_t *NdefMap;
uint8_t NDEFFlag = 0,
CRFlag = 0,
Temp16Bytes = 0,
i = 0;
NdefMap = (phFriNfc_NdefMap_t *)Context;
if ((Status & PHNFCSTBLOWER) == (NFCSTATUS_SUCCESS & PHNFCSTBLOWER))
{
switch (NdefMap->State)
{
case PH_FRINFC_NDEFMAP_STATE_CHK_NDEF_COMP:
Status = phFriNfc_MifStd_H_ProChkNdef (NdefMap);
CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
break;
case PH_FRINFC_NDEFMAP_STATE_READ:
/* Receive Length for read shall always be equal to 16 */
if ((*NdefMap->SendRecvLength == PH_FRINFC_MIFARESTD_BYTES_READ) &&
(NdefMap->ApduBuffIndex < (uint16_t) NdefMap->ApduBufferSize))
{
Temp16Bytes = PH_FRINFC_MIFARESTD_VAL0;
NDEFFlag = (uint8_t) PH_FRINFC_MIFARESTD_FLAG1;
if (NdefMap->TLVStruct.BytesRemainLinTLV != 0)
{
NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0;
CRFlag = PH_FRINFC_MIFARESTD_FLAG0;
/* To read the remaining length (L) in TLV */
Status = phFriNfc_MifStd_H_RemainTLV (NdefMap, &NDEFFlag, &Temp16Bytes);
CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
}
/* check the NDEFFlag is set. if this is not set, then
in the above RemainTLV function all the 16 bytes has been
read */
}
else
{
Status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_RECEIVE_LENGTH);
CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
}
break;
case PH_FRINFC_NDEFMAP_STATE_WRITE:
Status = phFriNfc_MifStd_H_ProWrABlock (NdefMap);
CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
/* Call Completion Routine if CR Flag is Set to 1 */
if (CRFlag == PH_FRINFC_MIFARESTD_FLAG1)
{
*NdefMap->WrNdefPacketLength = NdefMap->ApduBuffIndex;
}
break;
case PH_FRINFC_NDEFMAP_STATE_AUTH:
NdefMap->StdMifareContainer.FirstReadFlag = PH_FRINFC_MIFARESTD_FLAG0;
Status = phFriNfc_MifStd_H_ProAuth (NdefMap);
CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
break;
case PH_FRINFC_NDEFMAP_STATE_RD_ACS_BIT:
Status = phFriNfc_MifStd_H_ProAcsBits (NdefMap);
CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
break;
case PH_FRINFC_NDEFMAP_STATE_WR_NDEF_LEN:
if (NdefMap->StdMifareContainer.RdAfterWrFlag ==
PH_FRINFC_MIFARESTD_FLAG1)
{
Status = phFriNfc_MifStd_H_CallWrNdefLen (NdefMap);
CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
}
else
{
/* Check this */
if (NdefMap->StdMifareContainer.TempBlockNo ==
NdefMap->StdMifareContainer.currentBlock)
{
memcpy (NdefMap->StdMifareContainer.internalBuf,
NdefMap->StdMifareContainer.Buffer,
NdefMap->StdMifareContainer.internalLength);
}
*NdefMap->WrNdefPacketLength = NdefMap->ApduBuffIndex;
NdefMap->StdMifareContainer.currentBlock =
NdefMap->StdMifareContainer.TempBlockNo;
NdefMap->CardState = (uint8_t) ((NdefMap->CardState ==
PH_NDEFMAP_CARD_STATE_INITIALIZED) ?
PH_NDEFMAP_CARD_STATE_READ_WRITE :
NdefMap->CardState);
CRFlag = (uint8_t) PH_FRINFC_MIFARESTD_FLAG1;
}
break;
case PH_FRINFC_NDEFMAP_STATE_RD_TO_WR_NDEF_LEN:
CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
Status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_INVALID_DEVICE_REQUEST);
if (*NdefMap->SendRecvLength == PH_FRINFC_MIFARESTD_BYTES_READ)
{
/* Size of NdefMap->SendRecvBuf is set by phLibNfc_Gen_NdefMapReset to PH_LIBNFC_GEN_MAX_BUFFER */
/* We don't have to check memory here */
for (i = PH_FRINFC_MIFARESTD_BYTES_READ; i > 0; i--)
{
NdefMap->SendRecvBuf [i] = NdefMap->SendRecvBuf [i-1];
}
NdefMap->SendRecvBuf [PH_FRINFC_MIFARESTD_VAL0] =
NdefMap->StdMifareContainer.currentBlock;
Status = phFriNfc_MifStd_H_WriteNdefLen (NdefMap);
CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
}
break;
case PH_FRINFC_NDEFMAP_STATE_GET_ACT_CARDSIZE:
NDEFFlag = PH_FRINFC_MIFARESTD_FLAG1;
if (NdefMap->TLVStruct.NoLbytesinTLV > PH_FRINFC_MIFARESTD_VAL0)
{
NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0;
Status = phFriNfc_MifStd_H_ChkRemainTLVs (NdefMap, &CRFlag, &NDEFFlag);
NdefMap->TLVStruct.NoLbytesinTLV =
PH_FRINFC_MIFARESTD_VAL0;
}
if ((NDEFFlag == PH_FRINFC_MIFARESTD_FLAG1) &&
(CRFlag != PH_FRINFC_MIFARESTD_FLAG1))
{
Status = phFriNfc_MifStd_H_ChkTLVs (NdefMap, &CRFlag);
}
if (((NdefMap->StdMifareContainer.ReadNdefFlag ==
PH_FRINFC_MIFARESTD_FLAG1) ||
(NdefMap->StdMifareContainer.WrNdefFlag ==
PH_FRINFC_MIFARESTD_FLAG1)) &&
(Status != NFCSTATUS_PENDING))
{
NdefMap->StdMifareContainer.NFCforumSectFlag =
PH_FRINFC_MIFARESTD_FLAG1;
CRFlag = PH_FRINFC_MIFARESTD_FLAG0;
/* if the card state has changed to initialised and
read ndef is called then error is returned */
if (((NdefMap->StdMifareContainer.WrNdefFlag ==
PH_FRINFC_MIFARESTD_FLAG1) &&
(NdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY)) ||
((NdefMap->StdMifareContainer.ReadNdefFlag ==
PH_FRINFC_MIFARESTD_FLAG1) &&
(NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INITIALIZED)))
{
Status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_NO_NDEF_SUPPORT);
}
if (NdefMap->StdMifareContainer.AuthDone ==
PH_FRINFC_MIFARESTD_FLAG0)
{
Status = phFriNfc_MifStd_H_AuthSector (NdefMap);
}
else
{
Status = ((NdefMap->StdMifareContainer.ReadNdefFlag ==
PH_FRINFC_MIFARESTD_FLAG1) ?
phFriNfc_MifStd_H_RdTLV (NdefMap) :
phFriNfc_MifStd_H_RdBeforeWr (NdefMap));
}
NdefMap->StdMifareContainer.ReadNdefFlag =
PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.WrNdefFlag =
PH_FRINFC_MIFARESTD_FLAG0;
}
if (NdefMap->StdMifareContainer.ChkNdefFlag ==
PH_FRINFC_MIFARESTD_FLAG1)
{
CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
}
break;
case PH_FRINFC_NDEFMAP_STATE_RD_BEF_WR:
/* Read flag says that already part of TLV has been written */
Status = phFriNfc_MifStd_H_ProBytesToWr (NdefMap);
CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
break;
case PH_FRINFC_NDEFMAP_STATE_WR_TLV:
Status = phFriNfc_MifStd_H_ProWrTLV (NdefMap);
CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
break;
case PH_FRINFC_NDEFMAP_STATE_RD_TLV:
Status = phFriNfc_MifStd_H_ProRdTLV (NdefMap);
CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
break;
case PH_FRINFC_NDEFMAP_STATE_TERM_TLV:
phFriNfc_MifStd_H_SetNdefBlkAuth(NdefMap);
NdefMap->StdMifareContainer.currentBlock =
NdefMap->TLVStruct.NdefTLVBlock;
Status = phFriNfc_MifStd_H_RdtoWrNdefLen (NdefMap);
CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
break;
case PH_FRINFC_NDEFMAP_STATE_DISCONNECT:
NdefMap->StdMifareContainer.PollFlag = PH_FRINFC_MIFARESTD_FLAG0;
Status = phFriNfc_MifStd_H_CallConnect (NdefMap);
CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
break;
case PH_FRINFC_NDEFMAP_STATE_CONNECT:
if (NdefMap->StdMifareContainer.FirstReadFlag == PH_FRINFC_MIFARESTD_FLAG1)
{
NdefMap->StdMifareContainer.FirstReadFlag = PH_FRINFC_MIFARESTD_FLAG0;
Status = phFriNfc_MifStd_H_AuthSector (NdefMap);
}
else if ((NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD ||
NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD ||
NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD) &&
(NdefMap->StdMifareContainer.ReadOnlySectorIndex &&
NdefMap->StdMifareContainer.SectorTrailerBlockNo == NdefMap->StdMifareContainer.currentBlock))
{
NdefMap->StdMifareContainer.ReadOnlySectorIndex =
PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.SectorTrailerBlockNo =
PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_FLAG0;
Status = NFCSTATUS_FAILED;
}
else
{
Status = ((((NdefMap->Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) &&
(NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_WRITE_OPE)) ||
(NdefMap->StdMifareContainer.WrLength >
PH_FRINFC_MIFARESTD_VAL0)) ?
phFriNfc_MifStd_H_ProStatNotValid (NdefMap, Status) :
phFriNfc_MifStd_H_AuthSector (NdefMap));
}
CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
break;
case PH_FRINFC_NDEFMAP_STATE_RD_SEC_ACS_BIT:
Status = phFriNfc_MifStd_H_ProSectorTrailorAcsBits (NdefMap);
CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING)?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
if ((CRFlag == PH_FRINFC_MIFARESTD_FLAG1) &&
(NdefMap->StdMifareContainer.WriteAcsBitFlag == PH_FRINFC_MIFARESTD_FLAG0))
{
Status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_INVALID_DEVICE_REQUEST);
}
break;
case PH_FRINFC_NDEFMAP_STATE_WRITE_SEC:
/* Set flag for writing of Acs bit */
NdefMap->StdMifareContainer.WriteAcsBitFlag = PH_FRINFC_MIFARESTD_FLAG1;
/* The first NDEF sector is already made read only,
set card state to read only and proceed*/
if (NdefMap->CardState != PH_NDEFMAP_CARD_STATE_READ_ONLY)
{
Status = phFriNfc_MapTool_SetCardState (NdefMap, NdefMap->TLVStruct.BytesRemainLinTLV);
if (Status != NFCSTATUS_SUCCESS)
{
CRFlag = (uint8_t) PH_FRINFC_MIFARESTD_FLAG1;
}
}
if (CRFlag != PH_FRINFC_MIFARESTD_FLAG1)
{
Status = phFriNfc_MifStd_H_ProWrSectorTrailor (NdefMap);
CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
}
break;
default:
Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_INVALID_DEVICE_REQUEST);
CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
break;
}
}
else if (NdefMap->State == PH_FRINFC_NDEFMAP_STATE_AUTH)
{
NdefMap->StdMifareContainer.PollFlag = PH_FRINFC_MIFARESTD_FLAG1;
if(NdefMap->StdMifareContainer.FirstWriteFlag ==
PH_FRINFC_MIFARESTD_FLAG1)
{
NdefMap->StdMifareContainer.FirstWriteFlag =
PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.WrLength =
((NdefMap->StdMifareContainer.NFCforumSectFlag ==
PH_FRINFC_MIFARESTD_FLAG0) ?
PH_FRINFC_MIFARESTD_VAL1 :
NdefMap->StdMifareContainer.WrLength);
}
if (NdefMap->StdMifareContainer.WrLength == PH_FRINFC_MIFARESTD_VAL0)
{
Status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_EOF_NDEF_CONTAINER_REACHED);
CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
}
else
{
/* Authentication has failed */
CRFlag = PH_FRINFC_MIFARESTD_FLAG1; /* Call Completion Routine */
Status = NFCSTATUS_FAILED;/* Update Status Flag */
}
}
else
{
Status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_INVALID_DEVICE_REQUEST);
CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
}
/* Call Completion Routine if CR Flag is Set to 1 */
if (CRFlag == PH_FRINFC_MIFARESTD_FLAG1)
{
phFriNfc_MifStd_H_Complete (NdefMap, Status);
}
return;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_RdABlock
*
* Description This function is a Helper function for Mifare Std. It Reads
* a block from the card.
*
* Returns This function return NFCSTATUS_PENDING in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
static NFCSTATUS phFriNfc_MifStd_H_RdABlock (phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS status = NFCSTATUS_PENDING;
NdefMap->State = PH_FRINFC_NDEFMAP_STATE_READ;
NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_READ_OPE;
NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process;
NdefMap->MapCompletionInfo.Context = NdefMap;
if (NdefMap->ApduBuffIndex < (uint16_t) NdefMap->ApduBufferSize)
{
if (NdefMap->StdMifareContainer.internalLength > PH_FRINFC_MIFARESTD_VAL0)
{
status = phFriNfc_MifStd_H_ChkIntLen (NdefMap);
} /* internal Length Check */
else
{
NdefMap->SendRecvBuf [PH_FRINFC_MIFARESTD_VAL0] =
NdefMap->StdMifareContainer.currentBlock;
NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_READ;
*NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
NdefMap->Cmd.MfCmd = phHal_eMifareRead;
/* Call the Overlapped HAL Transceive function */
status = phFriNfc_ExtnsTransceive (NdefMap->pTransceiveInfo,
NdefMap->Cmd,
NdefMap->SendRecvBuf,
NdefMap->SendLength,
NdefMap->SendRecvLength);
}
}
else
{
/* Check for the Card Size */
if((((NdefMap->StdMifareContainer.NoOfNdefCompBlocks -
NdefMap->StdMifareContainer.NdefBlocks) *
PH_FRINFC_MIFARESTD_BYTES_READ) == 0) ||
(NdefMap->ApduBufferSize == NdefMap->ApduBuffIndex))
{
NdefMap->StdMifareContainer.ReadWriteCompleteFlag =
(uint8_t) ((((NdefMap->StdMifareContainer.NoOfNdefCompBlocks -
NdefMap->StdMifareContainer.NdefBlocks) *
PH_FRINFC_MIFARESTD_BYTES_READ) == 0) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
*NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex;
status = PHNFCSTVAL (CID_NFC_NONE, NFCSTATUS_SUCCESS);
}
else
{
/* Error: The control should not ideally come here. Return Error. */
status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_FAILED);
}
}
return status;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_WrABlock
*
* Description This function writes into a block of the card.
*
* Returns This function return NFCSTATUS_PENDING in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
static NFCSTATUS phFriNfc_MifStd_H_WrABlock (phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS status = NFCSTATUS_PENDING;
uint16_t RemainingBytes = 0,
BytesRemained = 0,
index = 0;
uint8_t Temp16Bytes = 0;
NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process;
NdefMap->MapCompletionInfo.Context = NdefMap;
NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_WRITE_OPE;
NdefMap->State = PH_FRINFC_NDEFMAP_STATE_WRITE;
/* User Buffer Check */
if( NdefMap->ApduBuffIndex < (uint16_t) NdefMap->ApduBufferSize)
{
RemainingBytes = (((uint16_t) (NdefMap->ApduBufferSize -
NdefMap->ApduBuffIndex) <
NdefMap->StdMifareContainer.remainingSize) ?
(uint16_t)(NdefMap->ApduBufferSize -
NdefMap->ApduBuffIndex) :
NdefMap->StdMifareContainer.remainingSize);
NdefMap->SendRecvBuf[0] = NdefMap->StdMifareContainer.currentBlock;
Temp16Bytes += PH_FRINFC_MIFARESTD_INC_1;
/* Check for internal bytes */
if (NdefMap->StdMifareContainer.internalLength > 0)
{
/* copy the bytes previously written in the internal Buffer */
memcpy (&(NdefMap->SendRecvBuf [Temp16Bytes]),
NdefMap->StdMifareContainer.internalBuf,
NdefMap->StdMifareContainer.internalLength);
Temp16Bytes += (uint8_t) (NdefMap->StdMifareContainer.internalLength);
if (RemainingBytes >= (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes))
{
/* Copy the Remaining bytes from the user buffer to make the send
data and length = 16 */
memcpy (&(NdefMap->SendRecvBuf [Temp16Bytes]),
NdefMap->ApduBuffer,
(MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes));
NdefMap->NumOfBytesWritten =
(MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes);
Temp16Bytes += (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes);
*NdefMap->DataCount = (Temp16Bytes - PH_FRINFC_MIFARESTD_VAL1);
}
else
{
memcpy (&(NdefMap->SendRecvBuf [Temp16Bytes]),
NdefMap->ApduBuffer,
RemainingBytes);
NdefMap->StdMifareContainer.internalBufFlag = PH_FRINFC_MIFARESTD_FLAG1;
NdefMap->NumOfBytesWritten = RemainingBytes;
Temp16Bytes += (uint8_t) (RemainingBytes);
*NdefMap->DataCount = (Temp16Bytes - PH_FRINFC_MIFARESTD_VAL1);
BytesRemained = (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes);
/* Pad empty bytes with Zeroes to complete 16 bytes*/
for (index = 0; index < BytesRemained; index++)
{
NdefMap->SendRecvBuf [(Temp16Bytes + index)] =
(uint8_t) ((index == PH_FRINFC_MIFARESTD_VAL0) ?
PH_FRINFC_MIFARESTD_TERMTLV_T :
PH_FRINFC_MIFARESTD_NULLTLV_T);
NdefMap->TLVStruct.SetTermTLVFlag = PH_FRINFC_MIFARESTD_FLAG1;
}
Temp16Bytes += (uint8_t) (BytesRemained);
}
}
else
{
if (RemainingBytes >= (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes))
{
/* Bytes left to write < 16, copy remaining bytes */
memcpy (&(NdefMap->SendRecvBuf [
Temp16Bytes]),
&(NdefMap->ApduBuffer [
NdefMap->ApduBuffIndex]),
(MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes));
NdefMap->NumOfBytesWritten = (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes);
Temp16Bytes += (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes);
*NdefMap->DataCount = (Temp16Bytes - PH_FRINFC_MIFARESTD_VAL1);
}
else
{
/* Bytes left to write < 16, copy remaining bytes */
memcpy (&(NdefMap->SendRecvBuf [
Temp16Bytes]),
&(NdefMap->ApduBuffer [
NdefMap->ApduBuffIndex]),
RemainingBytes);
NdefMap->StdMifareContainer.RemainingBufFlag = PH_FRINFC_MIFARESTD_FLAG1;
NdefMap->NumOfBytesWritten = RemainingBytes;
Temp16Bytes += (uint8_t) (RemainingBytes);
*NdefMap->DataCount = (Temp16Bytes - PH_FRINFC_MIFARESTD_VAL1);
/* Pad empty bytes with Zeroes to complete 16 bytes */
for (index = Temp16Bytes; index < MIFARE_MAX_SEND_BUF_TO_WRITE; index++)
{
NdefMap->SendRecvBuf [index] = (uint8_t) ((index ==
Temp16Bytes) ?
PH_FRINFC_MIFARESTD_TERMTLV_T :
PH_FRINFC_MIFARESTD_NULLTLV_T);
NdefMap->TLVStruct.SetTermTLVFlag = PH_FRINFC_MIFARESTD_FLAG1;
}
}
}
/* Buffer to store 16 bytes which is writing to the present block */
memcpy (NdefMap->StdMifareContainer.Buffer,
&(NdefMap->SendRecvBuf [PH_FRINFC_MIFARESTD_INC_1]),
PH_FRINFC_MIFARESTD_BLOCK_BYTES);
/* Write from here */
NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_WRITE;
NdefMap->Cmd.MfCmd = phHal_eMifareWrite16;
*NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
/* Call the Overlapped HAL Transceive function */
status = phFriNfc_ExtnsTransceive (NdefMap->pTransceiveInfo,
NdefMap->Cmd,
NdefMap->SendRecvBuf,
NdefMap->SendLength,
NdefMap->SendRecvLength);
}
else /* Check User Buffer */
{
if (NdefMap->StdMifareContainer.NdefBlocks >
NdefMap->StdMifareContainer.NoOfNdefCompBlocks)
{
NdefMap->StdMifareContainer.ReadWriteCompleteFlag =
PH_FRINFC_MIFARESTD_FLAG1;
status = PHNFCSTVAL (CID_NFC_NONE,
NFCSTATUS_SUCCESS);
}
else if (NdefMap->ApduBuffIndex == (uint16_t) NdefMap->ApduBufferSize)
{
status = PHNFCSTVAL (CID_NFC_NONE, NFCSTATUS_SUCCESS);
}
else
{
/* Error: The control should not ideally come here. Return Error. */
status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_FAILED);
}
}
return status;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_AuthSector
*
* Description This function authenticates one sector at a time.
*
* Returns This function return NFCSTATUS_PENDING in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
static NFCSTATUS phFriNfc_MifStd_H_AuthSector (phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS status = NFCSTATUS_PENDING;
NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process;
NdefMap->MapCompletionInfo.Context = NdefMap;
*NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
NdefMap->State = PH_FRINFC_NDEFMAP_STATE_AUTH;
/* Authenticate */
NdefMap->Cmd.MfCmd = phHal_eMifareAuthentA;
NdefMap->SendRecvBuf [PH_FRINFC_MIFARESTD_VAL0] =
((NdefMap->TLVStruct.NdefTLVAuthFlag ==
PH_FRINFC_MIFARESTD_FLAG1) ?
NdefMap->TLVStruct.NdefTLVBlock :
NdefMap->StdMifareContainer.currentBlock);
/* if MAD blocks then authentication key is
0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 else
0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 */
if (((NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK0) &&
(NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK1) &&
(NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK2) &&
(NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK64) &&
(NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK65) &&
(NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK66)) ||
(NdefMap->TLVStruct.NdefTLVAuthFlag ==
(uint8_t) PH_FRINFC_MIFARESTD_FLAG1))
{
NdefMap->SendRecvBuf [1] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT1; /* 0xD3 */
NdefMap->SendRecvBuf [2] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT2; /* 0xF7 */
NdefMap->SendRecvBuf [3] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT1; /* 0xD3 */
NdefMap->SendRecvBuf [4] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT2; /* 0xF7 */
NdefMap->SendRecvBuf [5] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT1; /* 0xD3 */
NdefMap->SendRecvBuf [6] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT2; /* 0xF7 */
}
else
{
NdefMap->SendRecvBuf [1] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT1; /* 0xA0 */
NdefMap->SendRecvBuf [2] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT2; /* 0xA1 */
NdefMap->SendRecvBuf [3] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT3; /* 0xA2 */
NdefMap->SendRecvBuf [4] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT4; /* 0xA3 */
NdefMap->SendRecvBuf [5] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT5; /* 0xA4 */
NdefMap->SendRecvBuf [6] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT6; /* 0xA5 */
}
if (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD ||
NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD ||
NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD)
{
if (NdefMap->StdMifareContainer.ReadOnlySectorIndex &&
NdefMap->StdMifareContainer.SectorTrailerBlockNo == NdefMap->StdMifareContainer.currentBlock)
{
memcpy (&NdefMap->SendRecvBuf [1], &NdefMap->StdMifareContainer.UserScrtKeyB [0], PH_FRINFC_MIFARESTD_KEY_LEN);
/* Authenticate with KeyB */
NdefMap->Cmd.MfCmd = phHal_eMifareAuthentB;
}
}
NdefMap->SendLength = MIFARE_AUTHENTICATE_CMD_LENGTH;
*NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
/* Call the Overlapped HAL Transceive function */
status = phFriNfc_ExtnsTransceive (NdefMap->pTransceiveInfo,
NdefMap->Cmd,
NdefMap->SendRecvBuf,
NdefMap->SendLength,
NdefMap->SendRecvLength);
return status;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_Complete
*
* Description It is used to call the Completion Routine
*
* Returns void
*
******************************************************************************/
static void phFriNfc_MifStd_H_Complete (phFriNfc_NdefMap_t *NdefMap,
NFCSTATUS Result)
{
/* set the state back to the Reset_Init state */
NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RESET_INIT;
/* set the completion routine */
NdefMap->CompletionRoutine [NdefMap->StdMifareContainer.CRIndex].
CompletionRoutine (NdefMap->CompletionRoutine->Context, Result);
return;
}
/******************************************************************************
* Function phFriNfc_MifStd4k_H_CheckNdef
*
* Description This function is used for Mifare 4k Check Ndef to
* get the next AID blocks.
*
* Returns This function return NFCSTATUS_SUCCESS in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
static NFCSTATUS phFriNfc_MifStd4k_H_CheckNdef (phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS Result = NFCSTATUS_SUCCESS;
/* Get the AID Block */
if (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK2)
{
NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_MAD_BLK64;
NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0;
}
else if (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK64)
{
NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_MAD_BLK65;
}
else
{
NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_MAD_BLK66;
}
Result = phFriNfc_MifareStdMap_ChkNdef (NdefMap);
return Result;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_fillAIDarray
*
* Description This function storew the AIDs for check ndef.
*
* Returns void
*
******************************************************************************/
static void phFriNfc_MifStd_H_fillAIDarray (phFriNfc_NdefMap_t *NdefMap)
{
uint8_t byteindex = 0;
if ((NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK1) ||
(NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK64))
{
/* The First Two Bytes in Receive Buffer
are CRC bytes so it is not copied
instead, 0 is copied in AID[0] & AID[1] */
NdefMap->StdMifareContainer.aid [NdefMap->StdMifareContainer.SectorIndex] =
PH_FRINFC_MIFARESTD_NON_NDEF_COMP;
NdefMap->StdMifareContainer.SectorIndex++;
byteindex = 2;
}
while (byteindex < PH_FRINFC_MIFARESTD_BYTES_READ)
{
if ((NdefMap->SendRecvBuf [byteindex] == PH_FRINFC_NDEFMAP_MIFARESTD_NDEF_COMPVAL2) &&
(NdefMap->SendRecvBuf [(byteindex + 1)] == PH_FRINFC_NDEFMAP_MIFARESTD_NDEF_COMPVAL1))
{
/* This flag is set when a NFC forum sector is found in a
MAD block for the first time */
NdefMap->StdMifareContainer.NFCforumSectFlag = PH_FRINFC_MIFARESTD_FLAG1;
NdefMap->StdMifareContainer.aid[NdefMap->StdMifareContainer.SectorIndex] =
PH_FRINFC_MIFARESTD_NDEF_COMP;
NdefMap->StdMifareContainer.SectorIndex++;
}
else
{
NdefMap->StdMifareContainer.aid [NdefMap->StdMifareContainer.SectorIndex] =
PH_FRINFC_MIFARESTD_NON_NDEF_COMP;
NdefMap->StdMifareContainer.SectorIndex++;
/* AID complete flag is set when a non NFC forum sector is found in a
MAD block after the NFC forum sector. After setting this, all other
values are ignored and are NOT NDEF compliant */
NdefMap->StdMifareContainer.aidCompleteFlag =
((NdefMap->StdMifareContainer.NFCforumSectFlag ==
PH_FRINFC_MIFARESTD_FLAG1) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
NdefMap->StdMifareContainer.NFCforumSectFlag = PH_FRINFC_MIFARESTD_FLAG0;
if (NdefMap->StdMifareContainer.aidCompleteFlag == PH_FRINFC_MIFARESTD_FLAG1)
{
break;
}
}
byteindex += 2;
}
/* If "aidCompleteFlag" is set then the remaining sectors are made NOT
NDEF compliant */
if ((NdefMap->StdMifareContainer.aidCompleteFlag == PH_FRINFC_MIFARESTD_FLAG1) &&
(NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD))
{
/* for Mifare 1k there are 16 sectors, till this number all sectors
are made NOT NDEF compliant */
for (byteindex = NdefMap->StdMifareContainer.SectorIndex;
byteindex < PH_FRINFC_MIFARESTD1K_TOTAL_SECTOR;
byteindex++)
{
NdefMap->StdMifareContainer.aid [byteindex] =
PH_FRINFC_MIFARESTD_NON_NDEF_COMP;
}
}
else if ((NdefMap->StdMifareContainer.aidCompleteFlag == PH_FRINFC_MIFARESTD_FLAG1) &&
(NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD))
{
/* for Mifare 2k there are 32 sectors, till this number all sectors
are made NOT NDEF compliant */
for (byteindex = NdefMap->StdMifareContainer.SectorIndex;
byteindex < PH_FRINFC_MIFARESTD2K_TOTAL_SECTOR;
byteindex++)
{
NdefMap->StdMifareContainer.aid[byteindex] =
PH_FRINFC_MIFARESTD_NON_NDEF_COMP;
}
}
else
{
/* for Mifare 4k there are 40 sectors, till this number all sectors
are made NOT NDEF compliant */
if ((NdefMap->StdMifareContainer.aidCompleteFlag == PH_FRINFC_MIFARESTD_FLAG1) &&
(NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD))
{
for(byteindex = NdefMap->StdMifareContainer.SectorIndex;
byteindex < PH_FRINFC_MIFARESTD4K_TOTAL_SECTOR;
byteindex++)
{
NdefMap->StdMifareContainer.aid [byteindex] =
PH_FRINFC_MIFARESTD_NON_NDEF_COMP;
}
}
}
return;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_BlkChk
*
* Description This function is to check the Ndef compliance of the
* current block, if the block is not Ndef Compliant,
* increment the block till the next Ndef compliant block
* using the Get Sector Helper function
*
* Returns This function return NFCSTATUS_SUCCESS in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
static NFCSTATUS phFriNfc_MifStd_H_BlkChk (phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS Result = NFCSTATUS_SUCCESS;
uint8_t SectorID = 0, callbreak = 0;
for (;;)
{
/* Get a Sector ID for the Current Block */
SectorID = phFriNfc_MifStd_H_GetSect (NdefMap->StdMifareContainer.currentBlock);
/* Check the card Type 1k or 4k */
/* enter if Mifare 1k card. For Mifare 4k go to else */
if (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD)
{
/* if Sector Id > 15 No Sectors to write */
if (SectorID > 15)
{
SectorID = phFriNfc_MifStd_H_GetSect (NdefMap->StdMifareContainer.currentBlock);
/*Error: No Ndef Compliant Sectors present */
Result = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_INVALID_PARAMETER);
callbreak = 1;
}
else
{
phFriNfc_MifStd1k_H_BlkChk (NdefMap, SectorID, &callbreak);
}
} /* End of if */ /* End of Mifare 1k check */
else if (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD) /* Mifare 2k check starts here */
{
/* Sector > 39 no ndef compliant sectors found*/
if (SectorID > PH_FRINFC_MIFARESTD_SECTOR_NO31)
{
/*Error: No Ndef Compliant Sectors present */
Result = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_INVALID_PARAMETER);
callbreak = 1;
}
else if (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK64)
{
NdefMap->StdMifareContainer.currentBlock += PH_FRINFC_MIFARESTD_BLK4;
}
else if (SectorID < PH_FRINFC_MIFARESTD_SECTOR_NO32) /* sector < 32 contains 4 blocks in each sector */
{
/* If the block checked is 63, the 3 blocks after this
are AID(MAD) blocks so its need to be skipped */
if (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK63)
{
NdefMap->StdMifareContainer.currentBlock += PH_FRINFC_MIFARESTD_BLK4;
}
else
{
phFriNfc_MifStd1k_H_BlkChk (NdefMap, SectorID, &callbreak);
}
}
else
{
phFriNfc_MifStd1k_H_BlkChk (NdefMap, SectorID, &callbreak);
}
}/* End of if*/ /* End of Mifare 2k check*/
else /* Mifare 4k check starts here */
{
/* Sector > 39 no ndef compliant sectors found*/
if (SectorID > PH_FRINFC_MIFARESTD_SECTOR_NO39)
{
/*Error: No Ndef Compliant Sectors present */
Result = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
callbreak = 1;
}
else if (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK64)
{
NdefMap->StdMifareContainer.currentBlock += PH_FRINFC_MIFARESTD_BLK4;
}
else if (SectorID < PH_FRINFC_MIFARESTD_SECTOR_NO32) /* sector < 32 contains 4 blocks in each sector */
{
/* If the block checked is 63, the 3 blocks after this
are AID(MAD) blocks so its need to be skipped */
if (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK63)
{
NdefMap->StdMifareContainer.currentBlock += PH_FRINFC_MIFARESTD_BLK4;
}
else
{
phFriNfc_MifStd1k_H_BlkChk (NdefMap, SectorID, &callbreak);
}
}
else
{
/* every last block of a sector needs to be skipped */
if (((NdefMap->StdMifareContainer.currentBlock + 1) %
PH_FRINFC_MIFARESTD_BLOCK_BYTES) == 0)
{
NdefMap->StdMifareContainer.currentBlock++;
}
else
{
if (NdefMap->StdMifareContainer.aid [SectorID] ==
PH_FRINFC_MIFARESTD_NDEF_COMP)
{
/* Check whether the block is first block of a (next)new sector and
also check if it is first block then internal length is zero
or not. Because once Authentication is done for the sector again
we should not authenticate it again */
/* In this case 32 sectors contains 4 blocks and next remaining 8 sectors
contains 16 blocks that is why (32 * 4) + (sectorID - 32) *16*/
if ((NdefMap->StdMifareContainer.currentBlock ==
((PH_FRINFC_MIFARESTD_SECTOR_NO32 * PH_FRINFC_MIFARESTD_BLK4) +
((SectorID - PH_FRINFC_MIFARESTD_SECTOR_NO32) * PH_FRINFC_MIFARESTD_BLOCK_BYTES))) &&
(NdefMap->StdMifareContainer.internalLength == 0))
{
NdefMap->StdMifareContainer.AuthDone = 0;
}
callbreak = 1;
}
else
{
NdefMap->StdMifareContainer.currentBlock += 16;
}
}
}
}
if (callbreak == 1)
{
break;
}
}
return Result;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_GetSect
*
* Description This function to get the Sector from the current block
*
* Returns uint8_t SectorID
*
******************************************************************************/
static uint8_t phFriNfc_MifStd_H_GetSect (uint8_t BlockNumber)
{
uint8_t SectorID = 0;
if (BlockNumber >= PH_FRINFC_MIFARESTD4K_BLK128)
{
SectorID = (uint8_t) (PH_FRINFC_MIFARESTD_SECTOR_NO32 +
((BlockNumber - PH_FRINFC_MIFARESTD4K_BLK128)/
PH_FRINFC_MIFARESTD_BLOCK_BYTES));
}
else
{
SectorID = (BlockNumber/PH_FRINFC_MIFARESTD_BLK4);
}
return SectorID;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_RdAcsBit
*
* Description It read the access bits of each sector.
* NCI messages.
*
* Returns This function return NFCSTATUS_SUCCESS in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
static NFCSTATUS phFriNfc_MifStd_H_RdAcsBit (phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS Result = NFCSTATUS_SUCCESS;
NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RD_ACS_BIT;
if (NdefMap->StdMifareContainer.ReadOnlySectorIndex &&
NdefMap->StdMifareContainer.currentBlock == NdefMap->StdMifareContainer.SectorTrailerBlockNo)
{
NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RD_SEC_ACS_BIT;
}
if (NdefMap->StdMifareContainer.ReadAcsBitFlag == PH_FRINFC_MIFARESTD_FLAG1)
{
/* Get the sector trailer */
((NdefMap->StdMifareContainer.currentBlock > 127) ?
phFriNfc_MifStd_H_Get4kStTrail(NdefMap) :
phFriNfc_MifStd_H_Get1kStTrail(NdefMap));
}
else
{
/* Give the current block to read */
NdefMap->SendRecvBuf [PH_FRINFC_MIFARESTD_VAL0] =
NdefMap->StdMifareContainer.currentBlock;
}
Result = phFriNfc_MifStd_H_Rd16Bytes(NdefMap,
NdefMap->SendRecvBuf [PH_FRINFC_MIFARESTD_VAL0]);
return Result;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_ChkAcsBit
*
* Description This function check the access bits of each sector.
*
* Returns This function return NFCSTATUS_SUCCESS in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
static NFCSTATUS phFriNfc_MifStd_H_ChkAcsBit (phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS Result = NFCSTATUS_SUCCESS;
/* Blocks from 0 to 3 and from 64 to 67(MAD blocks) */
if ((NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK0) ||
(NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK1) ||
(NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK2) ||
(NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK3) ||
(NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK64) ||
(NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK65) ||
(NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK66) )
{
/* Access bits check removed for the MAD blocks */
#ifdef ENABLE_ACS_BIT_CHK_FOR_MAD
if (((NdefMap->SendRecvBuf [
PH_FRINFC_MIFARESTD_VAL6] &
PH_FRINFC_MIFARESTD_MASK_FF) ==
PH_FRINFC_MIFARESTD_MADSECT_ACS_BYTE6) &&
((NdefMap->SendRecvBuf [
PH_FRINFC_MIFARESTD_VAL7] &
PH_FRINFC_MIFARESTD_MASK_FF) ==
PH_FRINFC_MIFARESTD_MADSECT_ACS_BYTE7) &&
((NdefMap->SendRecvBuf [
PH_FRINFC_MIFARESTD_VAL8] &
PH_FRINFC_MIFARESTD_MASK_FF) ==
PH_FRINFC_MIFARESTD_ACS_BYTE8))
{
NdefMap->StdMifareContainer.WriteFlag =
PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.ReadFlag =
PH_FRINFC_MIFARESTD_FLAG1;
}
else
{
NdefMap->StdMifareContainer.WriteFlag =
PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.ReadFlag =
PH_FRINFC_MIFARESTD_FLAG0;
}
#else /* #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD */
NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INITIALIZED;
#endif /* #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD */
}
else
{
/* Check for Access bytes 6, 7 and 8 value =
0x7F, 0x07, 0x88 NFC forum sectors*/
if (((NdefMap->SendRecvBuf [
PH_FRINFC_MIFARESTD_VAL6] &
PH_FRINFC_MIFARESTD_MASK_FF) ==
PH_FRINFC_MIFARESTD_NFCSECT_ACS_BYTE6) &&
((NdefMap->SendRecvBuf [
PH_FRINFC_MIFARESTD_VAL7] &
PH_FRINFC_MIFARESTD_MASK_FF) ==
PH_FRINFC_MIFARESTD_NFCSECT_ACS_BYTE7) &&
((NdefMap->SendRecvBuf [
PH_FRINFC_MIFARESTD_VAL8] &
PH_FRINFC_MIFARESTD_MASK_FF) ==
PH_FRINFC_MIFARESTD_ACS_BYTE8))
{
NdefMap->StdMifareContainer.WriteFlag =
PH_FRINFC_MIFARESTD_FLAG1;
NdefMap->StdMifareContainer.ReadFlag =
PH_FRINFC_MIFARESTD_FLAG1;
}
else if (((NdefMap->SendRecvBuf [
PH_FRINFC_MIFARESTD_VAL6] &
PH_FRINFC_MIFARESTD_MASK_FF) ==
PH_FRINFC_MIFARESTD_NFCSECT_RDACS_BYTE6) &&
((NdefMap->SendRecvBuf [
PH_FRINFC_MIFARESTD_VAL7] &
PH_FRINFC_MIFARESTD_MASK_FF) ==
PH_FRINFC_MIFARESTD_NFCSECT_RDACS_BYTE7) &&
((NdefMap->SendRecvBuf [
PH_FRINFC_MIFARESTD_VAL8] &
PH_FRINFC_MIFARESTD_MASK_FF) ==
PH_FRINFC_MIFARESTD_NFCSECT_RDACS_BYTE8))
{
/* Read Only state */
/* Check for Access bytes 6, 7 and 8 value =
0x55, 0xAD, 0x2A NFC forum Sectors */
NdefMap->StdMifareContainer.WriteFlag =
PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.ReadFlag =
PH_FRINFC_MIFARESTD_FLAG1;
}
else
{
NdefMap->StdMifareContainer.WriteFlag =
PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.ReadFlag =
PH_FRINFC_MIFARESTD_FLAG0;
}
#ifdef ENABLE_ACS_BIT_CHK_FOR_MAD
/* Do nothing */
#else /* #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD */
Result = phFriNfc_MifStd_H_GPBChk (NdefMap);
#endif /* #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD */
}
#ifdef ENABLE_ACS_BIT_CHK_FOR_MAD
Result = phFriNfc_MifStd_H_GPBChk (NdefMap);
#endif /* #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD */
return Result;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_ChkRdWr
*
* Description This function is for read access bits, depending
* on the read/write/check ndef function called.
*
* Returns This function return NFCSTATUS_SUCCESS in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
static NFCSTATUS phFriNfc_MifStd_H_ChkRdWr (phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS Result = NFCSTATUS_SUCCESS;
switch (NdefMap->PrevOperation)
{
case PH_FRINFC_NDEFMAP_CHECK_OPE:
if (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID)
{
/* No permission to read */
Result = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_READ_FAILED);
}
else if ((NdefMap->StdMifareContainer.currentBlock > 3) &&
(NdefMap->StdMifareContainer.ChkNdefCompleteFlag ==
PH_FRINFC_MIFARESTD_FLAG1) &&
(NdefMap->StdMifareContainer.currentBlock !=
PH_FRINFC_MIFARESTD_MAD_BLK65) &&
(NdefMap->StdMifareContainer.currentBlock !=
PH_FRINFC_MIFARESTD_MAD_BLK66))
{
Result = ((NdefMap->StdMifareContainer.ReadAcsBitFlag ==
PH_FRINFC_MIFARESTD_FLAG0) ?
phFriNfc_MifStd_H_RdAcsBit(NdefMap) :
phFriNfc_MifStd_H_AuthSector(NdefMap));
}
else
{
Result = phFriNfc_MifareStdMap_ChkNdef (NdefMap);
}
break;
case PH_FRINFC_NDEFMAP_READ_OPE:
if (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID)
{
/* No permission to Read */
Result = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_READ_FAILED);
}
else if (NdefMap->StdMifareContainer.ReadNdefFlag == PH_FRINFC_MIFARESTD_FLAG1)
{
Result = phFriNfc_MifStd_H_GetActCardLen (NdefMap);
}
else
{
Result = phFriNfc_MifStd_H_RdABlock (NdefMap);
}
break;
case PH_FRINFC_NDEFMAP_WRITE_OPE:
if ((NdefMap->CardState ==
PH_NDEFMAP_CARD_STATE_INVALID) ||
(NdefMap->CardState ==
PH_NDEFMAP_CARD_STATE_READ_ONLY))
{
/* No permission to Read */
Result = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_WRITE_FAILED);
}
else if (NdefMap->StdMifareContainer.WrNdefFlag ==
PH_FRINFC_MIFARESTD_FLAG1)
{
Result = phFriNfc_MifStd_H_GetActCardLen (NdefMap);
}
else if (NdefMap->StdMifareContainer.RdBeforeWrFlag ==
PH_FRINFC_MIFARESTD_FLAG1)
{
/*NdefMap->StdMifareContainer.ReadFlag =
PH_FRINFC_MIFARESTD_FLAG0;*/
Result = phFriNfc_MifStd_H_RdBeforeWr (NdefMap);
}
else if (NdefMap->StdMifareContainer.RdAfterWrFlag ==
PH_FRINFC_MIFARESTD_FLAG1)
{
Result = phFriNfc_MifStd_H_RdtoWrNdefLen (NdefMap);
}
else
{
Result = (((NdefMap->TLVStruct.NdefTLVBlock ==
NdefMap->StdMifareContainer.currentBlock) &&
(NdefMap->Offset ==
PH_FRINFC_NDEFMAP_SEEK_BEGIN)) ?
phFriNfc_MifStd_H_RdBeforeWr (NdefMap) :
phFriNfc_MifStd_H_WrABlock (NdefMap));
}
break;
case PH_FRINFC_NDEFMAP_GET_ACTSIZE_OPE:
Result = ((NdefMap->StdMifareContainer.ReadFlag ==
PH_FRINFC_MIFARESTD_FLAG0) ?
(PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_READ_FAILED)) :
phFriNfc_MifStd_H_GetActCardLen (NdefMap));
break;
default:
/* Operation is not correct */
Result = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP,
NFCSTATUS_INVALID_PARAMETER);
break;
}
return Result;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_ChkNdefCmpltSects
*
* Description This function is used to check ndef to check the
* ndef compliant sectors.
*
* Returns void
*
******************************************************************************/
static void phFriNfc_MifStd_H_ChkNdefCmpltSects (phFriNfc_NdefMap_t *NdefMap)
{
uint8_t index = 0;
uint8_t index_max_4k_2k= 0;
if (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD)
{
index_max_4k_2k = PH_FRINFC_MIFARESTD4K_TOTAL_SECTOR;
}
else
{
index_max_4k_2k = PH_FRINFC_MIFARESTD2K_TOTAL_SECTOR;
}
if (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD || NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD)
{
for (index = PH_FRINFC_MIFARESTD_SECTOR_NO1; index < index_max_4k_2k; index++)/*Block 0 is MAD block, so it should start with 1*/
{
/* For Mifare 4k, Block 0 to 31 contains 4 blocks */
/* sector 0 and 15 are aid blocks */
if (index != PH_FRINFC_MIFARESTD_SECTOR_NO16)
{
if (((index < 32) && (index != PH_FRINFC_MIFARESTD_SECTOR_NO0))
&& (NdefMap->StdMifareContainer.aid [index] ==
PH_FRINFC_MIFARESTD_NON_NDEF_COMP))
{
/* Only 3 blocks can be read/written till sector 31 */
NdefMap->StdMifareContainer.NoOfNdefCompBlocks -=
PH_FRINFC_MIFARESTD_MAD_BLK3;
}
else
{
/* For Mifare 4k, Block 32 to 39 contains 16 blocks */
if(NdefMap->StdMifareContainer.aid [index] ==
PH_FRINFC_MIFARESTD_NON_NDEF_COMP)
{
/* Only 15 blocks can be read/written from sector 31 */
NdefMap->StdMifareContainer.NoOfNdefCompBlocks -=
PH_FRINFC_MIFARESTD_BLK15;
}
}
}
} /* For index > 40 */
}
else
{
for (index = PH_FRINFC_MIFARESTD_SECTOR_NO1;
index < PH_FRINFC_MIFARESTD_SECTOR_NO16; index++)
{
if (NdefMap->StdMifareContainer.aid [index] ==
PH_FRINFC_MIFARESTD_NON_NDEF_COMP)
{
/* Only three blocks can be read/written in
a sector. So if a sector is non-ndef
compliant, decrement 3 */
NdefMap->StdMifareContainer.NoOfNdefCompBlocks -=
PH_FRINFC_MIFARESTD_MAD_BLK3;
}
}
}
return;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_RemainTLV
*
* Description This function is used for read ndef to process the
* remaining bytes of length (L) in the TLV.
*
* Returns This function return NFCSTATUS_PENDING in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
static NFCSTATUS phFriNfc_MifStd_H_RemainTLV (phFriNfc_NdefMap_t *NdefMap,
uint8_t *Flag,
uint8_t *Temp16Bytes)
{
NFCSTATUS Result = NFCSTATUS_SUCCESS;
uint8_t CRFlag = 0;
uint16_t RemainingBytes = 0;
RemainingBytes = ((uint16_t) NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex);
if (NdefMap->StdMifareContainer.remainingSize < RemainingBytes)
{
/* If the user Buffer is greater than the Card Size
set LastBlockFlag = 1. This Flag is used to read bytes
till the end of the card only */
RemainingBytes = NdefMap->StdMifareContainer.remainingSize;
}
/* Remaining Bytes of length (L) in TLV <= 16 */
if ((NdefMap->TLVStruct.BytesRemainLinTLV <=
(PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))) &&
(RemainingBytes <= NdefMap->TLVStruct.BytesRemainLinTLV))
{
/* Copy data to user buffer */
memcpy (&(NdefMap->ApduBuffer [NdefMap->ApduBuffIndex]),
&(NdefMap->SendRecvBuf [(*Temp16Bytes)]),
RemainingBytes);
NdefMap->ApduBuffIndex += RemainingBytes;
NdefMap->StdMifareContainer.remainingSize -= RemainingBytes;
/* copy the bytes to internal buffer, that are read,
but not used for the user buffer */
if (RemainingBytes != NdefMap->TLVStruct.BytesRemainLinTLV)
{
memcpy (NdefMap->StdMifareContainer.internalBuf,
&(NdefMap->SendRecvBuf [((*Temp16Bytes) + RemainingBytes)]),
((PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)) - RemainingBytes));
/* internal buffer length */
NdefMap->StdMifareContainer.internalLength =
((PH_FRINFC_MIFARESTD_BYTES_READ -
(*Temp16Bytes)) - RemainingBytes);
}
*Temp16Bytes += ((uint8_t)RemainingBytes);
/* Remaining Bytes of length value in TLV */
NdefMap->TLVStruct.BytesRemainLinTLV -= RemainingBytes;
if (NdefMap->StdMifareContainer.internalLength == PH_FRINFC_MIFARESTD_VAL0)
{
NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t)
(((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) ||
(NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0)) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
/* internal length bytes completed */
NdefMap->StdMifareContainer.currentBlock++;
NdefMap->StdMifareContainer.NdefBlocks++;
}
if (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0)
{
/* Remaining Bytes of length (L) in TLV is Zero means that the next
coming bytes are containing type (T), length (L) in TLV */
NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1;
NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
}
/* call completion routine */
CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
*Flag = PH_FRINFC_MIFARESTD_FLAG0;
}
else if ((NdefMap->TLVStruct.BytesRemainLinTLV <=
(PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))) &&
(RemainingBytes > NdefMap->TLVStruct.BytesRemainLinTLV))
{
/* Copy data to user buffer */
memcpy (&(NdefMap->ApduBuffer [NdefMap->ApduBuffIndex]),
&(NdefMap->SendRecvBuf [(*Temp16Bytes)]),
NdefMap->TLVStruct.BytesRemainLinTLV);
NdefMap->ApduBuffIndex += NdefMap->TLVStruct.BytesRemainLinTLV;
NdefMap->StdMifareContainer.remainingSize -= NdefMap->TLVStruct.BytesRemainLinTLV;
NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1;
*Temp16Bytes += ((uint8_t) NdefMap->TLVStruct.BytesRemainLinTLV);
NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_VAL0;
*Flag = PH_FRINFC_MIFARESTD_FLAG1;
NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
/* 16 bytes completed */
if (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_BYTES_READ)
{
*Flag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0;
NdefMap->StdMifareContainer.currentBlock++;
NdefMap->StdMifareContainer.NdefBlocks++;
Result = phFriNfc_MifStd_H_BlkChk (NdefMap);
if (Result == NFCSTATUS_SUCCESS)
{
if (NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1)
{
Result = phFriNfc_MifStd_H_RdABlock (NdefMap);
}
else
{
Result = phFriNfc_MifStd_H_AuthSector (NdefMap);
}
}
}
else
{
NdefMap->TLVStruct.BytesRemainLinTLV =
PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0;
/* The read operation has finished. so, completion routine
can be called. set the Completion routine(CR) flag */
CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
}
}
else if ((NdefMap->TLVStruct.BytesRemainLinTLV >
(PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))) &&
(RemainingBytes <= (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))))
{
/* Copy data to user buffer */
memcpy (&(NdefMap->ApduBuffer [NdefMap->ApduBuffIndex]),
&(NdefMap->SendRecvBuf [(*Temp16Bytes)]),
RemainingBytes);
NdefMap->ApduBuffIndex += RemainingBytes;
NdefMap->StdMifareContainer.remainingSize -= RemainingBytes;
/* Remaining Bytes of length (L) in TLV */
NdefMap->TLVStruct.BytesRemainLinTLV -= RemainingBytes;
/* copy the bytes to internal buffer, that are read,
but not used for the user buffer */
memcpy (NdefMap->StdMifareContainer.internalBuf,
&(NdefMap->SendRecvBuf[(RemainingBytes + (*Temp16Bytes))]),
((PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))
- RemainingBytes));
/* internal buffer length */
NdefMap->StdMifareContainer.internalLength =
((PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)) - RemainingBytes);
if (RemainingBytes == (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)))
{
NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t)
(((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) ||
(NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0)) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
/* internal length bytes completed */
NdefMap->StdMifareContainer.currentBlock++;
NdefMap->StdMifareContainer.NdefBlocks++;
}
*Temp16Bytes += ((uint8_t) RemainingBytes);
NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0;
CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
*Flag = PH_FRINFC_MIFARESTD_FLAG0;
}
else
{
if ((NdefMap->TLVStruct.BytesRemainLinTLV >
(PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))) &&
(RemainingBytes > (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))))
{
*Flag = PH_FRINFC_MIFARESTD_FLAG0;
/* Copy data to user buffer */
memcpy (&(NdefMap->ApduBuffer [NdefMap->ApduBuffIndex]),
&(NdefMap->SendRecvBuf [(*Temp16Bytes)]),
(PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)));
NdefMap->ApduBuffIndex += (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes));
NdefMap->StdMifareContainer.remainingSize -=
(PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes));
NdefMap->TLVStruct.BytesRemainLinTLV -= (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes));
*Temp16Bytes += (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes));
if (NdefMap->TLVStruct.BytesRemainLinTLV != PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0)
{
NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0;
}
/* 16 bytes completed */
NdefMap->StdMifareContainer.currentBlock++;
NdefMap->StdMifareContainer.NdefBlocks++;
Result = phFriNfc_MifStd_H_BlkChk (NdefMap);
if (Result == NFCSTATUS_SUCCESS)
{
Result = ((NdefMap->StdMifareContainer.AuthDone ==
PH_FRINFC_MIFARESTD_FLAG1) ?
phFriNfc_MifStd_H_RdABlock(NdefMap) :
phFriNfc_MifStd_H_AuthSector(NdefMap));
}
}
}
if (CRFlag == PH_FRINFC_MIFARESTD_FLAG1)
{
*NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex;
NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t)
(((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) ||
(NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0)) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
}
return Result;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_ChkIntLen
*
* Description This function reads ndef to process the internal bytes.
*
* Returns This function return NFCSTATUS_SUCCESS in case of success,
* In case of failure returns other failure value.
*
******************************************************************************/
static NFCSTATUS phFriNfc_MifStd_H_ChkIntLen(phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS Result = NFCSTATUS_SUCCESS;
uint8_t NDEFFlag = PH_FRINFC_MIFARESTD_FLAG1;
uint8_t TempintBytes = 0;
if(NdefMap->TLVStruct.BytesRemainLinTLV != 0)
{
NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0;
/* To read the remaining length (L) in TLV */
Result = phFriNfc_MifStd_H_IntLenWioutNdef(NdefMap, &NDEFFlag, &TempintBytes);
}
NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0;
/* check the NDEFFlag is set. if this is not set, then
in the above RemainTLV function all the 16 bytes has been
read */
return Result;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_IntLenWioutNdef
*
* Description This function reads ndef to check the internal bytes
* without ndef tlv flag.
*
* Returns This function return NFCSTATUS_SUCCESS in case of success,
* In case of failure returns other failure value.
*
******************************************************************************/
static NFCSTATUS phFriNfc_MifStd_H_IntLenWioutNdef (phFriNfc_NdefMap_t *NdefMap,
uint8_t *Flag,
uint8_t *TempintBytes)
{
NFCSTATUS Result = NFCSTATUS_SUCCESS;
uint8_t CRFlag = 0;
uint16_t RemainingBytes = 0;
RemainingBytes = ((uint16_t)NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex);
if (NdefMap->StdMifareContainer.remainingSize < RemainingBytes)
{
/* If the user Buffer is greater than the Card Size
set LastBlockFlag = 1. This Flag is used to read bytes
till the end of the card only */
RemainingBytes = NdefMap->StdMifareContainer.remainingSize;
}
/* Remaining Bytes of length (L) in TLV <= internal length */
if ((NdefMap->TLVStruct.BytesRemainLinTLV <=
NdefMap->StdMifareContainer.internalLength) &&
(RemainingBytes <= NdefMap->TLVStruct.BytesRemainLinTLV))
{
memcpy (&(NdefMap->ApduBuffer [NdefMap->ApduBuffIndex]),
&(NdefMap->StdMifareContainer.internalBuf [(*TempintBytes)]),
RemainingBytes);
NdefMap->ApduBuffIndex += RemainingBytes;
NdefMap->StdMifareContainer.remainingSize -= RemainingBytes;
*TempintBytes += ((uint8_t) RemainingBytes);
/* copy the bytes to internal buffer, that are read,
but not used for the user buffer */
memcpy (NdefMap->StdMifareContainer.internalBuf,
&(NdefMap->StdMifareContainer.internalBuf [RemainingBytes]),
(NdefMap->StdMifareContainer.internalLength - RemainingBytes));
/* internal buffer length */
NdefMap->StdMifareContainer.internalLength -= RemainingBytes;
NdefMap->TLVStruct.BytesRemainLinTLV -= RemainingBytes;
if (NdefMap->StdMifareContainer.internalLength == PH_FRINFC_MIFARESTD_VAL0)
{
NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t)
(((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) ||
(NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0)) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
/* internal length bytes completed */
NdefMap->StdMifareContainer.currentBlock++;
NdefMap->StdMifareContainer.NdefBlocks++;
}
/* Remaining Bytes of length value in TLV */
if (NdefMap->TLVStruct.BytesRemainLinTLV == 0)
{
/* Remaining Bytes of length (L) in TLV is Zero means that the next
coming bytes are containing type (T), length (L) in TLV */
NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1;
NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
}
/* call completion routine */
CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
*Flag = PH_FRINFC_MIFARESTD_FLAG0;
}
else if ((NdefMap->TLVStruct.BytesRemainLinTLV <=
NdefMap->StdMifareContainer.internalLength) &&
(RemainingBytes > NdefMap->TLVStruct.BytesRemainLinTLV))
{
memcpy (&(NdefMap->ApduBuffer [NdefMap->ApduBuffIndex]),
&(NdefMap->StdMifareContainer.internalBuf [(*TempintBytes)]),
NdefMap->TLVStruct.BytesRemainLinTLV);
NdefMap->ApduBuffIndex += NdefMap->TLVStruct.BytesRemainLinTLV;
NdefMap->StdMifareContainer.remainingSize -= NdefMap->TLVStruct.BytesRemainLinTLV;
NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1;
*TempintBytes += ((uint8_t) NdefMap->TLVStruct.BytesRemainLinTLV);
*Flag = PH_FRINFC_MIFARESTD_FLAG1;
NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t)
(((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) ||
(NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0)) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
if (PH_FRINFC_MIFARESTD_FLAG1 == NdefMap->StdMifareContainer.ReadWriteCompleteFlag)
{
CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
}
if (NdefMap->TLVStruct.BytesRemainLinTLV == NdefMap->StdMifareContainer.internalLength)
{
/* Remaining Bytes in Length (L) field of TLV is 0 */
NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0;
NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0;
*Flag = PH_FRINFC_MIFARESTD_FLAG0;
/* internal length bytes completed */
NdefMap->StdMifareContainer.currentBlock++;
NdefMap->StdMifareContainer.NdefBlocks++;
Result = phFriNfc_MifStd_H_BlkChk (NdefMap);
if (Result == NFCSTATUS_SUCCESS)
{
Result =
((NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1) ?
phFriNfc_MifStd_H_RdABlock (NdefMap) :
phFriNfc_MifStd_H_AuthSector (NdefMap));
}
}
else
{
/* Remaining Bytes in Length (L) field of TLV is 0 */
NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0;
*Flag = PH_FRINFC_MIFARESTD_FLAG1;
}
}
else if ((NdefMap->TLVStruct.BytesRemainLinTLV >
NdefMap->StdMifareContainer.internalLength) &&
(RemainingBytes <= NdefMap->StdMifareContainer.internalLength))
{
memcpy(&(NdefMap->ApduBuffer [NdefMap->ApduBuffIndex]),
&(NdefMap->StdMifareContainer.internalBuf [(*TempintBytes)]),
RemainingBytes);
NdefMap->ApduBuffIndex += RemainingBytes;
NdefMap->StdMifareContainer.remainingSize -= RemainingBytes;
*TempintBytes += ((uint8_t) RemainingBytes);
/* Remaining Bytes of length (L) in TLV */
NdefMap->TLVStruct.BytesRemainLinTLV -= RemainingBytes;
/* copy the bytes to internal buffer, that are read,
but not used for the user buffer */
memcpy (NdefMap->StdMifareContainer.internalBuf,
&(NdefMap->StdMifareContainer.internalBuf [RemainingBytes]),
(NdefMap->StdMifareContainer.internalLength - RemainingBytes));
/* internal buffer length */
NdefMap->StdMifareContainer.internalLength -= RemainingBytes;
if (NdefMap->StdMifareContainer.internalLength == PH_FRINFC_MIFARESTD_VAL0)
{
NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t)
(((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) ||
(NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0)) ?
PH_FRINFC_MIFARESTD_FLAG1 :
PH_FRINFC_MIFARESTD_FLAG0);
/* internal length bytes completed */
NdefMap->StdMifareContainer.currentBlock++;
NdefMap->StdMifareContainer.NdefBlocks++;
}
NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0;
CRFlag = PH_FRINFC_MIFARESTD_FLAG1;
*Flag = PH_FRINFC_MIFARESTD_FLAG0;
}
else
{
if ((NdefMap->TLVStruct.BytesRemainLinTLV >
NdefMap->StdMifareContainer.internalLength) &&
(RemainingBytes > NdefMap->StdMifareContainer.internalLength))
{
memcpy (&(NdefMap->ApduBuffer [NdefMap->ApduBuffIndex]),
&(NdefMap->StdMifareContainer.internalBuf [(*TempintBytes)]),
NdefMap->StdMifareContainer.internalLength);
*Flag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->ApduBuffIndex += NdefMap->StdMifareContainer.internalLength;
NdefMap->StdMifareContainer.remainingSize -=
NdefMap->StdMifareContainer.internalLength;
NdefMap->TLVStruct.BytesRemainLinTLV -= NdefMap->StdMifareContainer.internalLength;
if(NdefMap->TLVStruct.BytesRemainLinTLV != PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0)
{
NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0;
}
NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0;
/* internal length bytes completed */
NdefMap->StdMifareContainer.currentBlock++;
NdefMap->StdMifareContainer.NdefBlocks++;
Result = phFriNfc_MifStd_H_BlkChk(NdefMap);
if(Result == NFCSTATUS_SUCCESS)
{
Result = ((NdefMap->StdMifareContainer.AuthDone ==
PH_FRINFC_MIFARESTD_FLAG1)?
phFriNfc_MifStd_H_RdABlock(NdefMap):
phFriNfc_MifStd_H_AuthSector(NdefMap));
}
}
}
if(CRFlag == PH_FRINFC_MIFARESTD_FLAG1)
{
NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t)
(((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) ||
(NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0))?
PH_FRINFC_MIFARESTD_FLAG1:
PH_FRINFC_MIFARESTD_FLAG0);
*NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex;
}
return Result;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_WriteNdefLen
*
* Description This function is Helper function for write ndef
* to write the Length TLV.
*
* Returns This function return NFCSTATUS_PENDING in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
static NFCSTATUS phFriNfc_MifStd_H_WriteNdefLen(phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS Result = NFCSTATUS_SUCCESS;
NdefMap->State = PH_FRINFC_NDEFMAP_STATE_WR_NDEF_LEN;
/* If Current block = Ndef TLV block then the starting point
is writing from type of TLV
Else */
if(NdefMap->StdMifareContainer.currentBlock ==
NdefMap->TLVStruct.NdefTLVBlock)
{
if(NdefMap->TLVStruct.NULLTLVCount >=
PH_FRINFC_MIFARESTD_VAL2)
{
phFriNfc_MifStd_H_fillTLV1(NdefMap);
}
else
{
phFriNfc_MifStd_H_fillTLV2(NdefMap);
}
}
else
{
if(NdefMap->TLVStruct.NULLTLVCount >=
PH_FRINFC_MIFARESTD_VAL2)
{
phFriNfc_MifStd_H_fillTLV1_1(NdefMap);
}
else
{
phFriNfc_MifStd_H_fillTLV2_1(NdefMap);
}
}
memcpy( NdefMap->StdMifareContainer.Buffer,
&(NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL1]),
PH_FRINFC_MIFARESTD_BYTES_READ);
/* Write from here */
NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_WRITE;
NdefMap->Cmd.MfCmd = phHal_eMifareWrite16;
*NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
/* Call the Overlapped HAL Transceive function */
Result = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo,
NdefMap->Cmd,
NdefMap->SendRecvBuf,
NdefMap->SendLength,
NdefMap->SendRecvLength);
return Result;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_RdWrReset
*
* Description It resets ndef TLV values. This is used when the offset
* is BEGIN.
*
* Returns void
*
******************************************************************************/
static void phFriNfc_MifStd_H_RdWrReset(phFriNfc_NdefMap_t *NdefMap)
{
NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_BLK4;
NdefMap->StdMifareContainer.NdefBlocks = PH_FRINFC_MIFARESTD_VAL1;
NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->TLVStruct.NdefTLVAuthFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->TLVStruct.NdefTLVBlock = PH_FRINFC_MIFARESTD_MAD_BLK0;
NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->TLVStruct.NoLbytesinTLV = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0;
NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.NFCforumSectFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.FirstReadFlag = PH_FRINFC_MIFARESTD_FLAG1;
NdefMap->StdMifareContainer.ReadWriteCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0;
NdefMap->StdMifareContainer.remainingSize = (uint16_t)
(NdefMap->StdMifareContainer.NoOfNdefCompBlocks *
PH_FRINFC_MIFARESTD_BLOCK_BYTES);
NdefMap->StdMifareContainer.WrLength = PH_FRINFC_MIFARESTD_VAL1;
return;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_RdtoWrNdefLen
*
* Description This function is used to read the first ndef compliant
* block to change the length.
*
* Returns This function return NFCSTATUS_PENDING in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
static NFCSTATUS phFriNfc_MifStd_H_RdtoWrNdefLen(phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS Result = NFCSTATUS_SUCCESS;
NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RD_TO_WR_NDEF_LEN;
if(NdefMap->TLVStruct.NdefTLVAuthFlag == PH_FRINFC_MIFARESTD_FLAG1)
{
NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0;
Result = phFriNfc_MifStd_H_AuthSector(NdefMap);
}
else
{
NdefMap->SendRecvBuf[0] = NdefMap->StdMifareContainer.currentBlock;
NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_READ;
*NdefMap->SendRecvLength = NdefMap->TempReceiveLength;
NdefMap->Cmd.MfCmd = phHal_eMifareRead;
/* Call the Overlapped HAL Transceive function */
Result = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo,
NdefMap->Cmd,
NdefMap->SendRecvBuf,
NdefMap->SendLength,
NdefMap->SendRecvLength);
}
return Result;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_SetNdefBlkAuth
*
* Description This function is used to set the authentication flag
* for the ndef TLV block.
*
* Returns void
*
******************************************************************************/
static void phFriNfc_MifStd_H_SetNdefBlkAuth(phFriNfc_NdefMap_t *NdefMap)
{
NdefMap->TLVStruct.NdefTLVAuthFlag =
((phFriNfc_MifStd_H_GetSect(NdefMap->TLVStruct.NdefTLVBlock)
== phFriNfc_MifStd_H_GetSect(NdefMap->StdMifareContainer.currentBlock))?
PH_FRINFC_MIFARESTD_FLAG0:
PH_FRINFC_MIFARESTD_FLAG1);
return;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_GetActCardLen
*
* Description Helper function to get the actual length of card.
*
* Returns This function return NFCSTATUS_SUCCESS in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
static NFCSTATUS phFriNfc_MifStd_H_GetActCardLen(phFriNfc_NdefMap_t *NdefMap)
{
NFCSTATUS Result = NFCSTATUS_SUCCESS;
NdefMap->State = PH_FRINFC_NDEFMAP_STATE_GET_ACT_CARDSIZE;
NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_GET_ACTSIZE_OPE;
Result = ((NdefMap->StdMifareContainer.AuthDone ==
PH_FRINFC_MIFARESTD_FLAG0)?
phFriNfc_MifStd_H_AuthSector(NdefMap):
phFriNfc_MifStd_H_Rd16Bytes(NdefMap,
NdefMap->StdMifareContainer.currentBlock));
return Result;
}
/******************************************************************************
* Function phFriNfc_MifStd_H_ChkTLVs
*
* Description Helper function to check all the TLVs.
*
* Returns This function return NFCSTATUS_SUCCESS in case of success
* In case of failure returns other failure value.
*
******************************************************************************/
static NFCSTATUS phFriNfc_MifStd_H_ChkTLVs(phFriNfc_NdefMap_t *NdefMap,
uint8_t *CRFlag)