void
phFriNfc_ISO15693_FmtProcess (
    void        *pContext,
    NFCSTATUS   Status)
{
    phFriNfc_sNdefSmtCrdFmt_t      *psNdefSmtCrdFmt =
                                    (phFriNfc_sNdefSmtCrdFmt_t *)pContext;

    if((NFCSTATUS_SUCCESS & PHNFCSTBLOWER) == (Status & PHNFCSTBLOWER))
    {
        if (ISO15693_FORMAT == psNdefSmtCrdFmt->State)
        {
            /* Check for further formatting */
            Status = phFriNfc_ISO15693_H_ProFormat (psNdefSmtCrdFmt);
        }
        else
        {
            Status = PHNFCSTVAL (CID_FRI_NFC_NDEF_SMTCRDFMT,
                                NFCSTATUS_INVALID_DEVICE_REQUEST);
        }
    }
    else
    {
        Status = PHNFCSTVAL (CID_FRI_NFC_NDEF_SMTCRDFMT,
                            NFCSTATUS_FORMAT_ERROR);
    }

    /* Handle the all the error cases */
    if ((NFCSTATUS_PENDING & PHNFCSTBLOWER) != (Status & PHNFCSTBLOWER))
    {
        /* call respective CR */
        phFriNfc_SmtCrdFmt_HCrHandler (psNdefSmtCrdFmt, Status);
    }
}
void
phFriNfc_TopazFormat_Process(void* Context,
                             NFCSTATUS Status)
{
    NFCSTATUS wStatus = Status;
    phFriNfc_sNdefSmtCrdFmt_t* NdefSmtCrdFmt = (phFriNfc_sNdefSmtCrdFmt_t*)Context;

    PH_LOG_NDEF_FUNC_ENTRY();

    if (NdefSmtCrdFmt == NULL)
    {
        wStatus = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT, NFCSTATUS_INVALID_PARAMETER);
    }
    else if (wStatus == NFCSTATUS_SUCCESS)
    {
        if (*NdefSmtCrdFmt->SendRecvLength != 1 ||
            NdefSmtCrdFmt->SendRecvBuf[0] != c_staticTopazFormat[NdefSmtCrdFmt->AddInfo.Type1Info.CurrentByte])
        {
            PH_LOG_NDEF_WARN_STR("Unexpected response received. Formatting failed");
            wStatus = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT, NFCSTATUS_FORMAT_ERROR);
        }
        else
        {
            NdefSmtCrdFmt->AddInfo.Type1Info.CurrentByte++;
            if (NdefSmtCrdFmt->AddInfo.Type1Info.CurrentByte < ARRAYSIZE(c_staticTopazFormat))
            {
                wStatus = phFriNfc_TopazFormat_WriteByte(NdefSmtCrdFmt,
                                                         NdefSmtCrdFmt->AddInfo.Type1Info.CurrentBlock,
                                                         NdefSmtCrdFmt->AddInfo.Type1Info.CurrentByte,
                                                         c_staticTopazFormat[NdefSmtCrdFmt->AddInfo.Type1Info.CurrentByte]);
            }
        }
    }

    /* If status is not pending (i.e. we are waiting for a response from the card), then call the
    completion routine */
    if (wStatus != NFCSTATUS_PENDING)
    {
        phFriNfc_SmtCrdFmt_HCrHandler(NdefSmtCrdFmt, wStatus);
    }

    PH_LOG_NDEF_FUNC_EXIT();
}
void
phFriNfc_TopazDynamicFormat_Process(void* Context,
                                    NFCSTATUS Status)
{
    NFCSTATUS wStatus = Status;
    phFriNfc_sNdefSmtCrdFmt_t* NdefSmtCrdFmt = (phFriNfc_sNdefSmtCrdFmt_t*)Context;

    PH_LOG_NDEF_FUNC_ENTRY();

    if (NdefSmtCrdFmt == NULL)
    {
        wStatus = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT, NFCSTATUS_INVALID_PARAMETER);
    }
    else if (wStatus == NFCSTATUS_SUCCESS)
    {
        switch (NdefSmtCrdFmt->State)
        {
        case PH_FRINFC_TOPAZ_STATE_WRITE_CC_BYTES:
            wStatus = phFriNfc_TopazDynamicFormat_ProcessWritingCCBytes(NdefSmtCrdFmt);
            break;
        case PH_FRINFC_TOPAZ_STATE_WRITE_MEM_CTRL_BYTES:
            wStatus = phFriNfc_TopazDynamicFormat_ProcessWritingMemCtrlBytes(NdefSmtCrdFmt);
            break;
        case PH_FRINFC_TOPAZ_STATE_WRITE_TERM_TLV_BYTES:
            wStatus = phFriNfc_TopazDynamicFormat_ProcessWritingTermTlvBytes(NdefSmtCrdFmt);
            break;
        default:
            wStatus = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT, NFCSTATUS_INVALID_DEVICE_REQUEST);
            break;
        }
    }

    /* If status is not pending (i.e. we are waiting for a response from the card), then call the
       completion routine */
    if (wStatus != NFCSTATUS_PENDING)
    {
        phFriNfc_SmtCrdFmt_HCrHandler(NdefSmtCrdFmt, wStatus);
    }

    PH_LOG_NDEF_FUNC_EXIT();
}
void phFriNfc_MfUL_Process(void             *Context,
                           NFCSTATUS        Status)
{
    phFriNfc_sNdefSmtCrdFmt_t  *NdefSmtCrdFmt = (phFriNfc_sNdefSmtCrdFmt_t *)Context;
    PH_LOG_NDEF_FUNC_ENTRY();
    if(Status == NFCSTATUS_SUCCESS)
    {
        switch(NdefSmtCrdFmt->State)
        {
        case PH_FRINFC_MFUL_FMT_RD_16BYTES:
            Status = phFriNfc_MfUL_H_ProRd16Bytes(NdefSmtCrdFmt);
            break;

        case PH_FRINFC_MFUL_FMT_WR_OTPBYTES:
            Status = phFriNfc_MfUL_H_ProWrOTPBytes(NdefSmtCrdFmt);
            break;

        case PH_FRINFC_MFUL_FMT_WR_TLV:
            if (NdefSmtCrdFmt->CardType == PH_FRINFC_NDEFMAP_MIFARE_ULC_CARD)
            {
                /* Write NDEF TLV in block number 5 */
                NdefSmtCrdFmt->AddInfo.Type2Info.CurrentBlock =
                                                        PH_FRINFC_MFUL_FMT_VAL_5;
                /* Card already have the OTP bytes so write TLV */
                NdefSmtCrdFmt->State = PH_FRINFC_MFUL_FMT_WR_TLV1;

                Status = phFriNfc_MfUL_H_WrRd (NdefSmtCrdFmt);
            }
            break;

        case PH_FRINFC_MFUL_FMT_RO_RD_16BYTES:
        {
            if (MIFARE_UL_READ_MAX_SIZE == *NdefSmtCrdFmt->SendRecvLength)
            {
                uint8_t         otp_lock_page_size = 0;
                uint8_t         i = 0;

                otp_lock_page_size = sizeof (NdefSmtCrdFmt->AddInfo.Type2Info.LockBytes);
                (void)phOsalNfc_MemCopy ((void *)NdefSmtCrdFmt->AddInfo.Type2Info.LockBytes,
                            (void *)NdefSmtCrdFmt->SendRecvBuf,
                            sizeof(NdefSmtCrdFmt->AddInfo.Type2Info.LockBytes));

                NdefSmtCrdFmt->AddInfo.Type2Info.LockBytes[2] = (uint8_t)
                                    (NdefSmtCrdFmt->AddInfo.Type2Info.LockBytes[2]
                                    | MIFARE_UL_LOCK_BYTE1_VALUE);
                NdefSmtCrdFmt->AddInfo.Type2Info.LockBytes[3] = MIFARE_UL_LOCK_BYTE2_VALUE;
                i = (uint8_t)(i + otp_lock_page_size);

                otp_lock_page_size = sizeof (NdefSmtCrdFmt->AddInfo.Type2Info.OTPBytes);

                phOsalNfc_MemCopy ((void *)NdefSmtCrdFmt->AddInfo.Type2Info.OTPBytes,
                            (void *)(NdefSmtCrdFmt->SendRecvBuf + i),
                            sizeof(NdefSmtCrdFmt->AddInfo.Type2Info.OTPBytes));

                NdefSmtCrdFmt->AddInfo.Type2Info.OTPBytes[(otp_lock_page_size - 1)] =
                                                        READ_ONLY_VALUE_IN_OTP;

                switch (NdefSmtCrdFmt->AddInfo.Type2Info.OTPBytes[TYPE_2_MEM_SIZE_POSITION])
                {
                    case TYPE_2_STATIC_MEM_SIZE_VALUE:
                    {
                        NdefSmtCrdFmt->State = PH_FRINFC_MFUL_FMT_RO_WR_OTP_BYTES;
                        Status = phFriNfc_MfUL_H_WrRd (NdefSmtCrdFmt);
                        break;
                    }

                    case TYPE_2_DYNAMIC_MEM_SIZE_VALUE:
                    {
                        NdefSmtCrdFmt->State =
                                PH_FRINFC_MFUL_FMT_RO_NDEF_PARSE_RD_BYTES;

                        /* Start reading from block 4 */
                        NdefSmtCrdFmt->AddInfo.Type2Info.CurrentBlock = 4;
                        Status = phFriNfc_MfUL_H_WrRd (NdefSmtCrdFmt);
                        break;
                    }

                    default:
                    {
                        Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
                                            NFCSTATUS_INVALID_DEVICE_REQUEST);
                        break;
                    }
                }
            }
            else
            {
                Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
                                    NFCSTATUS_INVALID_RECEIVE_LENGTH);
            }
            break;
        }

        case PH_FRINFC_MFUL_FMT_RO_WR_OTP_BYTES:
        {
            switch (NdefSmtCrdFmt->AddInfo.Type2Info.OTPBytes[TYPE_2_MEM_SIZE_POSITION])
            {
                case TYPE_2_STATIC_MEM_SIZE_VALUE:
                case TYPE_2_DYNAMIC_MEM_SIZE_VALUE:
                {
                    NdefSmtCrdFmt->State = PH_FRINFC_MFUL_FMT_RO_WR_LOCK_BYTES;
                    Status = phFriNfc_MfUL_H_WrRd (NdefSmtCrdFmt);
                    break;
                }

                default:
                {
                    Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
                                        NFCSTATUS_INVALID_DEVICE_REQUEST);
                    break;
                }
            }
            break;
        }

        case PH_FRINFC_MFUL_FMT_RO_NDEF_PARSE_RD_BYTES:
        {
            if (MIFARE_UL_READ_MAX_SIZE == *NdefSmtCrdFmt->SendRecvLength)
            {
                Status = phFriNfc_MfUL_ParseTLVs (NdefSmtCrdFmt,
                                        NdefSmtCrdFmt->SendRecvBuf,
                                        (uint8_t)*NdefSmtCrdFmt->SendRecvLength);

                if (!Status)
                {
                    NdefSmtCrdFmt->AddInfo.Type2Info.CurrentBlock =
                        NdefSmtCrdFmt->AddInfo.Type2Info.LockBlockNumber;
                    Status = phFriNfc_MfUL_ReadWriteLockBytes (NdefSmtCrdFmt);
                }
            }
            else
            {
                Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
                                    NFCSTATUS_INVALID_RECEIVE_LENGTH);
            }
            break;
        }

        case PH_FRINFC_MFUL_FMT_RO_RD_DYN_LOCK_BYTES:
        {
            if (MIFARE_UL_READ_MAX_SIZE == *NdefSmtCrdFmt->SendRecvLength)
            {
                (void)phOsalNfc_MemCopy ((void *)NdefSmtCrdFmt->AddInfo.Type2Info.ReadData,
                            (void *)NdefSmtCrdFmt->SendRecvBuf,
                            sizeof(NdefSmtCrdFmt->AddInfo.Type2Info.ReadData));

                NdefSmtCrdFmt->AddInfo.Type2Info.ReadDataIndex = 0;

                Status = phFriNfc_MfUL_UpdateAndWriteLockBits (NdefSmtCrdFmt);

            }
            else
            {
                Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
                                    NFCSTATUS_INVALID_RECEIVE_LENGTH);
            }
            break;
        }

        case PH_FRINFC_MFUL_FMT_RO_WR_DYN_LOCK_BYTES:
        {
            NdefSmtCrdFmt->AddInfo.Type2Info.ReadDataIndex = (uint8_t)
                                    (NdefSmtCrdFmt->AddInfo.Type2Info.ReadDataIndex +
                                    MFUL_BLOCK_SIZE_IN_BYTES);

            if (!phFriNfc_MfUL_CalcRemainingLockBits (NdefSmtCrdFmt))
            {
                /* There is no lock bits to write, then write OTP bytes */
                NdefSmtCrdFmt->State = PH_FRINFC_MFUL_FMT_RO_WR_OTP_BYTES;
                Status = phFriNfc_MfUL_H_WrRd (NdefSmtCrdFmt);
            }
            else if ((NdefSmtCrdFmt->AddInfo.Type2Info.ReadDataIndex <
                MIFARE_UL_READ_MAX_SIZE)
                && (phFriNfc_MfUL_CalcRemainingLockBits (NdefSmtCrdFmt)))
            {
                /* If remaining lock bits has to be written and the data is already read */
                Status = phFriNfc_MfUL_UpdateAndWriteLockBits (NdefSmtCrdFmt);
            }
            else
            {
                /* Increment current block by 4 because if a data is read then 16
                    bytes will be given which is 4 blocks */
                NdefSmtCrdFmt->AddInfo.Type2Info.CurrentBlock = (uint8_t)
                            (NdefSmtCrdFmt->AddInfo.Type2Info.CurrentBlock + 4);
                Status = phFriNfc_MfUL_ReadWriteLockBytes (NdefSmtCrdFmt);
            }
            break;
        }

        case PH_FRINFC_MFUL_FMT_RO_WR_LOCK_BYTES:
        {
            /* Do nothing */
            break;
        }

        case PH_FRINFC_MFUL_FMT_WR_TLV1:
            break;

        default:
            Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
                                NFCSTATUS_INVALID_DEVICE_REQUEST);
            break;
        }
    }
    /* Status is not success then call completion routine */
    if(Status != NFCSTATUS_PENDING)
    {
        phFriNfc_SmtCrdFmt_HCrHandler(NdefSmtCrdFmt, Status);
    }
    PH_LOG_NDEF_FUNC_EXIT();
}