phLibNfc_ndef_raw.c revision a6243755a9b438bfbfe0be331ece55953f8cf9eb
15d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*
25d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * Copyright (C) 2010 NXP Semiconductors
35d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
45d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * Licensed under the Apache License, Version 2.0 (the "License");
55d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * you may not use this file except in compliance with the License.
65d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * You may obtain a copy of the License at
75d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
85d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *      http://www.apache.org/licenses/LICENSE-2.0
95d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * Unless required by applicable law or agreed to in writing, software
115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * distributed under the License is distributed on an "AS IS" BASIS,
125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * See the License for the specific language governing permissions and
145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * limitations under the License.
155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \file phLibNfc_ndef_raw.c
195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * Project: NFC FRI 1.1
215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * $Date: Thu Apr 22 13:59:50 2010 $
235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * $Author: ing07385 $
245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * $Revision: 1.70 $
255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * $Aliases: NFC_FRI1.1_WK1014_SDK,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1019_SDK,NFC_FRI1.1_WK1024_SDK $
265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*
305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly************************* Header Files ****************************************
315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phLibNfcStatus.h>
345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phLibNfc.h>
355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phHal4Nfc.h>
365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phOsalNfc.h>
375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phLibNfc_Internal.h>
385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phLibNfc_ndef_raw.h>
395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phLibNfc_initiator.h>
405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phLibNfc_discovery.h>
415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phFriNfc_NdefReg.h>
425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*
445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*************************** Macro's  ****************************************
455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#ifndef STATIC_DISABLE
485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#define STATIC static
495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#else
505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly//#undef STATIC
515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#define STATIC
525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#endif
535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#define     TOPAZ_NDEF_BITMASK             0x10U
555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#define     TOPAZ_LEN_BITMASK              0x02U
565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#define     TOPAZ_DYNAMIC_LEN               460U
575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#define     TOPAZ_STATIC_CARD_LEN           128U
585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#define     MIFARE_STD_BLOCK_SIZE          0x10U
595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*
605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*************************** Global Variables **********************************
615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyphLibNfc_Ndef_Info_t NdefInfo;
635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyphFriNfc_NdefRecord_t *pNdefRecord=NULL;
645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*
655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*************************** Static Function Declaration ***********************
665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/* Response callback for Check Ndef */
695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC
705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_Ndef_CheckNdef_Cb(void *pContext, NFCSTATUS status);
715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/* Response callback for Ndef Write */
735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC
745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_Ndef_Write_Cb(void* Context,NFCSTATUS status);
755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/* Response callback for Ndef Read*/
775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC
785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_Ndef_Read_Cb(void* Context,NFCSTATUS status);
795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/* Response callback forNdef Format*/
815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC
825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_Ndef_format_Cb(void *Context,NFCSTATUS status);
835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen#ifdef LIBNFC_READONLY_NDEF
8580ee29b88ec489e2bbf6ae446692229d36755429Martijn CoenenSTATIC
8680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenenvoid
8780ee29b88ec489e2bbf6ae446692229d36755429Martijn CoenenphLibNfc_Ndef_ReadOnly_Cb (
8880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    void        *p_context,
8980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    NFCSTATUS   status);
9080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen#endif /* #ifdef LIBNFC_READONLY_NDEF */
9180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/* Response callback for Search Ndef Content */
935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC
945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_Ndef_SrchNdefCnt_Cb(void *context, NFCSTATUS status);
955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/* Response callback for Ndef Record Type Discovery */
975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC
985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_Ndef_Rtd_Cb( void *CallBackParam);
995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/* Response callback for Check Ndef timer callback */
1015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC void CheckNdef_timer_cb(uint32_t timer_id, void *pContext);
1025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*Callback for Presence check call from Chk Ndef*/
1045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC void phLibNfc_Ndef_ChkNdef_Pchk_Cb(void   *pContext,
1055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                NFCSTATUS  status
1065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                );
1075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*
1085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*************************** Function Definitions ******************************
1095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
1105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
1125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function reads an NDEF message from  already connected tag.
1135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* the NDEF message  is read starting after the position of the
1145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* last read operation of the same tag during current session.
1155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
1165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phLibNfc_Ndef_Read( phLibNfc_Handle                   hRemoteDevice,
1185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            phNfc_sData_t                      *psRd,
1195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            phLibNfc_Ndef_EOffset_t             Offset,
1205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            pphLibNfc_RspCb_t                   pNdefRead_RspCb,
1215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            void*                               pContext
1225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            )
1235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
1245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS RetVal = NFCSTATUS_FAILED;
1255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if((NULL == gpphLibContext)||
1265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))
1275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
1285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_NOT_INITIALISED;
1295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
1305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if((NULL == psRd) || (NULL == pNdefRead_RspCb)
1315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        || (NULL == psRd->buffer)
1325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        || (0 == psRd->length)
1335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        || (NULL == pContext)
1345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        || (0 == hRemoteDevice))
1355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
1365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal= NFCSTATUS_INVALID_PARAMETER;
1375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
1385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)
1395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
1405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_SHUTDOWN;
1415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
1425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(0 == gpphLibContext->Connected_handle)
1435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {   /*presently no target or tag is connected*/
1445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal=NFCSTATUS_TARGET_NOT_CONNECTED;
1455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
1465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(hRemoteDevice != gpphLibContext->Connected_handle)
1475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {   /*This handle of the device sent by application is not connected */
1485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal=NFCSTATUS_INVALID_HANDLE;
1495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
1505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if((TRUE == gpphLibContext->status.GenCb_pending_status)
1515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            ||(NULL!=gpphLibContext->CBInfo.pClientRdNdefCb)
1525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            ||(CHK_NDEF_NOT_DONE == gpphLibContext->ndef_cntx.is_ndef))
1535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
1545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*Previous callback is pending*/
1555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_REJECTED;
1565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
1575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(gpphLibContext->ndef_cntx.is_ndef == FALSE)
1585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
1595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*no Ndef Support in tag*/
1605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         RetVal = NFCSTATUS_NON_NDEF_COMPLIANT;
1615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
1625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if((gpphLibContext->ndef_cntx.is_ndef == TRUE)
1635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        &&(0 == gpphLibContext->ndef_cntx.NdefActualSize))
1645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
1655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*Card is empty- So Returning length as zero*/
1665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        psRd->length = 0;
1675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_SUCCESS;
1685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
169143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas#ifdef LLCP_TRANSACT_CHANGES
170143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas    else if ((LLCP_STATE_RESET_INIT != gpphLibContext->llcp_cntx.sLlcpContext.state)
171143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas            && (LLCP_STATE_CHECKED != gpphLibContext->llcp_cntx.sLlcpContext.state))
172143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas    {
173143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas        RetVal= NFCSTATUS_BUSY;
174143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas    }
175143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas#endif /* #ifdef LLCP_TRANSACT_CHANGES */
1765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
1775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
1785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->psRemoteDevList->psRemoteDevInfo->SessionOpened = SESSION_OPEN;
1795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.eLast_Call = NdefRd;
1805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if((((phHal_sRemoteDevInformation_t*)hRemoteDevice)->RemDevType ==
1815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            phHal_eMifare_PICC) && (((phHal_sRemoteDevInformation_t*)
1825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            hRemoteDevice)->RemoteDevInfo.Iso14443A_Info.Sak != 0)&&
1835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            ((NULL == gpphLibContext->psBufferedAuth)
1845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            ||(phHal_eMifareAuthentA == gpphLibContext->psBufferedAuth->cmd.MfCmd))
1855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            )
1865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
1875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(NULL != gpphLibContext->psBufferedAuth)
1885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
1895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                if(NULL != gpphLibContext->psBufferedAuth->sRecvData.buffer)
1905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
1915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    phOsalNfc_FreeMemory(
1925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        gpphLibContext->psBufferedAuth->sRecvData.buffer);
1935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                }
1945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                if(NULL != gpphLibContext->psBufferedAuth->sSendData.buffer)
1955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
1965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    phOsalNfc_FreeMemory(
1975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        gpphLibContext->psBufferedAuth->sSendData.buffer);
1985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                }
1995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                phOsalNfc_FreeMemory(gpphLibContext->psBufferedAuth);
2005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
2015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth
2025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                =(phLibNfc_sTransceiveInfo_t *)
2035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                phOsalNfc_GetMemory(sizeof(phLibNfc_sTransceiveInfo_t));
2045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->addr =
2055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly             (uint8_t)gpphLibContext->ndef_cntx.psNdefMap
2065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly             ->StdMifareContainer.currentBlock;
2075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->cmd.MfCmd = phHal_eMifareRead16;
2085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->sSendData.length
2095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                = 0;
2105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->sRecvData.length
2115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                = MIFARE_STD_BLOCK_SIZE;
2125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->sRecvData.buffer
2135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                = (uint8_t *)phOsalNfc_GetMemory(MIFARE_STD_BLOCK_SIZE);
2145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->sSendData.buffer
2155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly             = (uint8_t *)phOsalNfc_GetMemory(MIFARE_STD_BLOCK_SIZE);
2165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
2175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(eLibNfcHalStatePresenceChk !=
2185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->LibNfcState.next_state)
2195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
2205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            uint8_t     cr_index = 0;
2215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->ndef_cntx.psUpperNdefMsg = psRd;
2225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            for (cr_index = 0; cr_index < PH_FRINFC_NDEFMAP_CR; cr_index++)
2235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
2245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                RetVal= phFriNfc_NdefMap_SetCompletionRoutine(
2255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    gpphLibContext->ndef_cntx.psNdefMap,
2265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    cr_index,
2275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    phLibNfc_Ndef_Read_Cb,
2285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    (void *)gpphLibContext);
2295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
2315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->ndef_cntx.NdefContinueRead =(uint8_t) ((phLibNfc_Ndef_EBegin==Offset) ?
2325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                    PH_FRINFC_NDEFMAP_SEEK_BEGIN :
2335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                    PH_FRINFC_NDEFMAP_SEEK_CUR);
2345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* call below layer Ndef Read*/
2355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal = phFriNfc_NdefMap_RdNdef(gpphLibContext->ndef_cntx.psNdefMap,
2365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->ndef_cntx.psUpperNdefMsg->buffer,
2375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            (uint32_t*)&gpphLibContext->ndef_cntx.psUpperNdefMsg->length,
2385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->ndef_cntx.NdefContinueRead);
2395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal = PHNFCSTATUS(RetVal);
2415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(NFCSTATUS_INSUFFICIENT_STORAGE == RetVal)
2425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
2435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->ndef_cntx.psUpperNdefMsg->length = 0;
2445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                RetVal = NFCSTATUS_SUCCESS;
2455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
2465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
2475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
2485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
2495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly             gpphLibContext->CBInfo.pClientRdNdefCb= NULL;
2505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly             RetVal = NFCSTATUS_PENDING;
2515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
2525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(NFCSTATUS_PENDING == RetVal)
2535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
2545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->CBInfo.pClientRdNdefCb = pNdefRead_RspCb;
2555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->CBInfo.pClientRdNdefCntx = pContext;
2565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->status.GenCb_pending_status=TRUE;
2575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly			gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;
2585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
2605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else if (NFCSTATUS_SUCCESS == RetVal)
2615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
2625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal= NFCSTATUS_SUCCESS;
2635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
2645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
2655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
2665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /*Ndef read failed*/
2675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal = NFCSTATUS_FAILED;
2685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
2695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
2705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return RetVal;
2715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
2725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/* Response callback for phLibNfc_Ndef_Read */
2735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC
2745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_Ndef_Read_Cb(void* Context,NFCSTATUS status)
2755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
2765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS               RetStatus = NFCSTATUS_SUCCESS;
2775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    pphLibNfc_RspCb_t       pClientCb=NULL;
2785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phLibNfc_LibContext_t   *pLibNfc_Ctxt = (phLibNfc_LibContext_t *)Context;
2795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    void                    *pUpperLayerContext=NULL;
2805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phHal_sRemoteDevInformation_t   *ps_rem_dev_info = NULL;
2815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(pLibNfc_Ctxt != gpphLibContext)
2835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
2845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*wrong context returned*/
2855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
2865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
2875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
2885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
2895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
2905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {   /*shutdown called before completion of Ndef read allow
2915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly              shutdown to happen */
2925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            phLibNfc_Pending_Shutdown();
2935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetStatus = NFCSTATUS_SHUTDOWN;
2945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
2955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else if(eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)
2965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
2975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetStatus = NFCSTATUS_ABORTED;
2985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
2995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
3005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
3015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->status.GenCb_pending_status = FALSE;
302bcd4097d93d61a86839412879f9ccf4ed303ee15Martijn Coenen            if (gpphLibContext->psBufferedAuth != NULL && gpphLibContext->ndef_cntx.psNdefMap != NULL) {
303a6243755a9b438bfbfe0be331ece55953f8cf9ebMartijn Coenen                   gpphLibContext->psBufferedAuth->addr = (uint8_t)
304a6243755a9b438bfbfe0be331ece55953f8cf9ebMartijn Coenen                   gpphLibContext->ndef_cntx.psNdefMap->StdMifareContainer.currentBlock;
305bcd4097d93d61a86839412879f9ccf4ed303ee15Martijn Coenen            }
3065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(NFCSTATUS_FAILED == status )
3085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
3095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /*During Ndef read operation tag was not present in RF
3105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                field of reader*/
3115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                RetStatus = NFCSTATUS_FAILED;
3125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->LastTrancvSuccess = FALSE;
3135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->ndef_cntx.is_ndef = FALSE;
3145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                ps_rem_dev_info = (phHal_sRemoteDevInformation_t *)
3155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    gpphLibContext->Connected_handle;
3165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                if ((phHal_eMifare_PICC == ps_rem_dev_info->RemDevType) &&
3175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    (0x08 == (ps_rem_dev_info->RemoteDevInfo.Iso14443A_Info.Sak & 0x08)))
3185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
3195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    /* card type is mifare 1k/4k, then reconnect */
3215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    RetStatus = phHal4Nfc_Connect(gpphLibContext->psHwReference,
3225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                ps_rem_dev_info,
3235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                (pphHal4Nfc_ConnectCallback_t)
3245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                phLibNfc_Reconnect_Mifare_Cb,
3255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                (void *)gpphLibContext);
3265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                }
3275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
3295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            else if(status == NFCSTATUS_SUCCESS)
3305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
3315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->LastTrancvSuccess = TRUE;
3325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                RetStatus = NFCSTATUS_SUCCESS;
3335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
3345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly		    else
3355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly		    {
3365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->LastTrancvSuccess = FALSE;
3375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly				RetStatus = NFCSTATUS_FAILED;
3385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly			}
3395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
3405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*update the current state as connected*/
3415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phLibNfc_UpdateCurState(status,gpphLibContext);
3425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        pClientCb = gpphLibContext->CBInfo.pClientRdNdefCb;
3445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        pUpperLayerContext = gpphLibContext->CBInfo.pClientRdNdefCntx;
3455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->CBInfo.pClientRdNdefCb = NULL;
3475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->CBInfo.pClientRdNdefCntx = NULL;
3485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(NFCSTATUS_PENDING != RetStatus)
3495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
3505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if (NULL != pClientCb)
3515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
3525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /*Notify to upper layer status and read bytes*/
3535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pClientCb(pUpperLayerContext,RetStatus);
3545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
3555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
3565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
3575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return;
3585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
3595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
3615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* Write NDEF to a tag.
3625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
3635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function allows the user to write a NDEF data to already connected NFC
3645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* tag.Function writes   a complete NDEF message to a tag. If a NDEF message
3655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* already exists in the tag, it will be overwritten. When the transaction is
3665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* complete,a notification callback is notified.
3675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
3685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phLibNfc_Ndef_Write(
3695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            phLibNfc_Handle          hRemoteDevice,
3705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            phNfc_sData_t           *psWr,
3715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            pphLibNfc_RspCb_t        pNdefWrite_RspCb,
3725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            void*                    pContext
3735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            )
3745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
3755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS RetVal = NFCSTATUS_FAILED;
3765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint8_t             NdefWriteType=0xFF;
3775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /*LibNfc is initilized or not */
3785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if((NULL == gpphLibContext)||
3795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (gpphLibContext->LibNfcState.cur_state
3805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            == eLibNfcHalStateShutdown))
3815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
3825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_NOT_INITIALISED;
3835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }/*Check for application has sent the valid parameters*/
3845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if((NULL == psWr) || (NULL == pNdefWrite_RspCb)
3855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        || (NULL == psWr->buffer)
3865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        || (NULL == pContext)
3875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        || (0 ==hRemoteDevice))
3885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
3895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal= NFCSTATUS_INVALID_PARAMETER;
3905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
3915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(gpphLibContext->LibNfcState.next_state
3925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            == eLibNfcHalStateShutdown)
3935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {   /* Lib Nfc Shutdown*/
3945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal= NFCSTATUS_SHUTDOWN;
3955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
3965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(0 == gpphLibContext->Connected_handle)
3975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
3985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal=NFCSTATUS_TARGET_NOT_CONNECTED;
3995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
4005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(hRemoteDevice != gpphLibContext->Connected_handle)
4015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
4025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal=NFCSTATUS_INVALID_HANDLE;
4035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
4045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if((TRUE == gpphLibContext->status.GenCb_pending_status)||
4055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly           (gpphLibContext->ndef_cntx.is_ndef == CHK_NDEF_NOT_DONE))
4065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
4075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         /* Previous callback is pending or Tag is not NDEF tag*/
4085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_REJECTED;
4095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        PHDBG_INFO("LIbNfc:Previous Callback is Pending");
4105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
4115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(FALSE == gpphLibContext->ndef_cntx.is_ndef)
4125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
4135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_NON_NDEF_COMPLIANT;
4145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
4155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(psWr->length > gpphLibContext->ndef_cntx.NdefLength)
4165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
4175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_NOT_ENOUGH_MEMORY;
4185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
419143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas#ifdef LLCP_TRANSACT_CHANGES
420143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas    else if ((LLCP_STATE_RESET_INIT != gpphLibContext->llcp_cntx.sLlcpContext.state)
421143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas            && (LLCP_STATE_CHECKED != gpphLibContext->llcp_cntx.sLlcpContext.state))
422143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas    {
423143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas        RetVal= NFCSTATUS_BUSY;
424143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas    }
425143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas#endif /* #ifdef LLCP_TRANSACT_CHANGES */
4265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
4275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
4285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        uint8_t         cr_index = 0;
4295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.psUpperNdefMsg = psWr;
4305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.AppWrLength= psWr->length;
4315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.eLast_Call = NdefWr;
4325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->psRemoteDevList->psRemoteDevInfo->SessionOpened
4335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            = SESSION_OPEN;
4345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if((((phHal_sRemoteDevInformation_t*)hRemoteDevice)->RemDevType ==
4355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               phHal_eMifare_PICC) && (((phHal_sRemoteDevInformation_t*)
4365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               hRemoteDevice)->RemoteDevInfo.Iso14443A_Info.Sak != 0)&&
4375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               ((NULL == gpphLibContext->psBufferedAuth)
4385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                ||(phHal_eMifareAuthentA ==
4395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                   gpphLibContext->psBufferedAuth->cmd.MfCmd))
4405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               )
4415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
4425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(NULL != gpphLibContext->psBufferedAuth)
4435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
4445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                if(NULL != gpphLibContext->psBufferedAuth->sRecvData.buffer)
4455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
4465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    phOsalNfc_FreeMemory(
4475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        gpphLibContext->psBufferedAuth->sRecvData.buffer);
4485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                }
4495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                if(NULL != gpphLibContext->psBufferedAuth->sSendData.buffer)
4505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
4515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    phOsalNfc_FreeMemory(
4525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        gpphLibContext->psBufferedAuth->sSendData.buffer);
4535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                }
4545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                phOsalNfc_FreeMemory(gpphLibContext->psBufferedAuth);
4555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
4565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth
4575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                =(phLibNfc_sTransceiveInfo_t *)
4585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                phOsalNfc_GetMemory(sizeof(phLibNfc_sTransceiveInfo_t));
4595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->addr =
4605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly             (uint8_t)gpphLibContext->ndef_cntx.psNdefMap
4615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly             ->StdMifareContainer.currentBlock;
4625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->cmd.MfCmd = phHal_eMifareRead16;
4635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->sSendData.length
4645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                = 0;
4655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->sRecvData.length
4665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                = MIFARE_STD_BLOCK_SIZE;
4675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->sRecvData.buffer
4685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                = (uint8_t *)phOsalNfc_GetMemory(MIFARE_STD_BLOCK_SIZE);
4695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly             gpphLibContext->psBufferedAuth->sSendData.buffer
4705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                = (uint8_t *)phOsalNfc_GetMemory(MIFARE_STD_BLOCK_SIZE);
4715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
4725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(eLibNfcHalStatePresenceChk ==
4735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->LibNfcState.next_state)
4745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
4755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->CBInfo.pClientWrNdefCb = NULL;
4765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal = NFCSTATUS_PENDING;
4775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
4785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
4795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
4805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            for (cr_index = 0; cr_index < PH_FRINFC_NDEFMAP_CR; cr_index++)
4815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
4825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* Registering the Completion Routine.*/
4835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                RetVal= phFriNfc_NdefMap_SetCompletionRoutine(
4845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    gpphLibContext->ndef_cntx.psNdefMap,
4855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    cr_index,
4865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    phLibNfc_Ndef_Write_Cb,
4875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    (void *)gpphLibContext);
4885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
4905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(0 == psWr->length)
4915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
4925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                 /* Length of bytes to be written Zero- Erase the Tag  */
4935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                RetVal = phFriNfc_NdefMap_EraseNdef(gpphLibContext->ndef_cntx.psNdefMap);
4945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
4955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            else
4965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
4975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /*Write from beginning or current location*/
4985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefWriteType = PH_FRINFC_NDEFMAP_SEEK_BEGIN;
4995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /*Call FRI Ndef Write*/
5005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                RetVal=phFriNfc_NdefMap_WrNdef(gpphLibContext->ndef_cntx.psNdefMap,
5015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->ndef_cntx.psUpperNdefMsg->buffer,
5025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            (uint32_t*)&gpphLibContext->ndef_cntx.psUpperNdefMsg->length,
5035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            NdefWriteType);
5045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
5055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(NFCSTATUS_PENDING == RetVal)
5065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
5075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->CBInfo.pClientWrNdefCb = pNdefWrite_RspCb;
5085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->CBInfo.pClientWrNdefCntx = pContext;
5095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->status.GenCb_pending_status=TRUE;
5105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;
5115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
5125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            else
5135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
5145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                RetVal = NFCSTATUS_FAILED;
5155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
5165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
5175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
5185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return RetVal;
5195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
5205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/* Response callback for phLibNfc_Ndef_Write */
5225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC
5235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_Ndef_Write_Cb(void* Context,NFCSTATUS status)
5245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
5255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    pphLibNfc_RspCb_t       pClientCb=NULL;
5275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phLibNfc_LibContext_t   *pLibNfc_Ctxt = (phLibNfc_LibContext_t *)Context;
5285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    void                    *pUpperLayerContext=NULL;
5295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phHal_sRemoteDevInformation_t   *ps_rem_dev_info = NULL;
5305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(pLibNfc_Ctxt != gpphLibContext)
5325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {   /*wrong context returned*/
5335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
5345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
5355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
5365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
5375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
5385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {   /*shutdown called before completion of Ndef write allow
5395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly              shutdown to happen */
5405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            phLibNfc_Pending_Shutdown();
5415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            status = NFCSTATUS_SHUTDOWN;
5425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
5435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else if(eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)
5445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
5455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            status = NFCSTATUS_ABORTED;
5465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
5475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
5485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
5495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->status.GenCb_pending_status = FALSE;
550bcd4097d93d61a86839412879f9ccf4ed303ee15Martijn Coenen            if (gpphLibContext->psBufferedAuth != NULL && gpphLibContext->ndef_cntx.psNdefMap != NULL) {
551bcd4097d93d61a86839412879f9ccf4ed303ee15Martijn Coenen                gpphLibContext->psBufferedAuth->addr = (uint8_t)
552bcd4097d93d61a86839412879f9ccf4ed303ee15Martijn Coenen                    gpphLibContext->ndef_cntx.psNdefMap->TLVStruct.NdefTLVBlock;
553bcd4097d93d61a86839412879f9ccf4ed303ee15Martijn Coenen            }
5545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(status == NFCSTATUS_FAILED )
5555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
5565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly				status = NFCSTATUS_FAILED;
5575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->LastTrancvSuccess = FALSE;
5585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /*During Ndef write operation tag was not present in RF
5595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                field of reader*/
5605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                ps_rem_dev_info = (phHal_sRemoteDevInformation_t *)
5615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    gpphLibContext->Connected_handle;
5625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               if ((phHal_eMifare_PICC == ps_rem_dev_info->RemDevType) &&
5635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    (0x08 == (ps_rem_dev_info->RemoteDevInfo.Iso14443A_Info.Sak & 0x08)))
5645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
5655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    /* card type is mifare 1k/4k, then reconnect */
5685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    status = phHal4Nfc_Connect(gpphLibContext->psHwReference,
5695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                ps_rem_dev_info,
5705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                (pphHal4Nfc_ConnectCallback_t)
5715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                phLibNfc_Reconnect_Mifare_Cb,
5725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                (void *)gpphLibContext);
5735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                }
5745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
5755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            else if( status== NFCSTATUS_SUCCESS)
5765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
5775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->LastTrancvSuccess = TRUE;
5785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                status = NFCSTATUS_SUCCESS;
5795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                if(gpphLibContext->ndef_cntx.AppWrLength >
5805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                 gpphLibContext->ndef_cntx.NdefLength)
5815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
5825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    status = NFCSTATUS_NOT_ENOUGH_MEMORY;
583a6243755a9b438bfbfe0be331ece55953f8cf9ebMartijn Coenen                }
584a6243755a9b438bfbfe0be331ece55953f8cf9ebMartijn Coenen                else
585a6243755a9b438bfbfe0be331ece55953f8cf9ebMartijn Coenen                {
586a6243755a9b438bfbfe0be331ece55953f8cf9ebMartijn Coenen                    pLibNfc_Ctxt->ndef_cntx.NdefActualSize =
587a6243755a9b438bfbfe0be331ece55953f8cf9ebMartijn Coenen                                    pLibNfc_Ctxt->ndef_cntx.psUpperNdefMsg->length;
588a6243755a9b438bfbfe0be331ece55953f8cf9ebMartijn Coenen                }
589a6243755a9b438bfbfe0be331ece55953f8cf9ebMartijn Coenen            }
590a6243755a9b438bfbfe0be331ece55953f8cf9ebMartijn Coenen            else
591a6243755a9b438bfbfe0be331ece55953f8cf9ebMartijn Coenen            {
5925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->LastTrancvSuccess = FALSE;
5935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly				status = NFCSTATUS_FAILED;;
5945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly			}
5955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
5965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phLibNfc_UpdateCurState(status,gpphLibContext);
5975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        pClientCb = gpphLibContext->CBInfo.pClientWrNdefCb;
5995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        pUpperLayerContext = gpphLibContext->CBInfo.pClientWrNdefCntx;
6005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->CBInfo.pClientWrNdefCb = NULL;
6025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->CBInfo.pClientWrNdefCntx = NULL;
6035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(NFCSTATUS_PENDING !=status)
6045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
6055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if (NULL != pClientCb)
6065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
6075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /*Notify to upper layer status and No. of bytes
6085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                actually written */
6095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pClientCb(pUpperLayerContext, status);
6105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
6115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
6125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
6135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return;
6145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
6155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
6185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* Initialize structures needed for the Ndef
6195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* related operation such as Check Ndef, read, write
6205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* and Ndef foramt.only once allocation
6215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
6225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_Ndef_Init(void)
6235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
6245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(gpphLibContext->psTransInfo==NULL)
6255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
6265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*Allocate memory for Transceiveinformation Structure*/
6275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->psTransInfo = (phLibNfc_sTransceiveInfo_t *)
6285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            phOsalNfc_GetMemory(sizeof(phLibNfc_sTransceiveInfo_t));
6295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
6305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(gpphLibContext->psTransInfo==NULL)
6315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
6325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*exception: Not enough memory*/
6335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,1);
6345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
6365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(NULL == gpphLibContext->ndef_cntx.psNdefMap)
6375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
6385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*Allocate memory for NDEF Mapping Component Context Structure*/
6395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.psNdefMap = (phFriNfc_NdefMap_t *)
6405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    phOsalNfc_GetMemory(sizeof(phFriNfc_NdefMap_t));
6415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
6425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(NULL != gpphLibContext->ndef_cntx.psNdefMap)
6435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
6445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*Allocation successful*/
6455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (void)memset(gpphLibContext->ndef_cntx.psNdefMap,0,sizeof(phFriNfc_NdefMap_t));
6465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.NdefSendRecvLen = NDEF_SENDRCV_BUF_LEN;
6475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf =
6485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (uint8_t*) phOsalNfc_GetMemory(gpphLibContext->
6495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                ndef_cntx.NdefSendRecvLen);
6505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(NULL != gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf)
6525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
6535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            (void)memset(gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf,
6545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                0,
6555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->ndef_cntx.NdefSendRecvLen);
6565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psOverHalCtxt =(phFriNfc_OvrHal_t *)
6585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                phOsalNfc_GetMemory(sizeof(phFriNfc_OvrHal_t));
6595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
6605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
6615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(NULL == gpphLibContext->psOverHalCtxt)
6625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {   /*exception: Not enough memory*/
6635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,1);
6645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
6655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
6665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
6675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (void)memset(gpphLibContext->psOverHalCtxt,0,
6695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            sizeof(phFriNfc_OvrHal_t));
6705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Initialize the Overlapped hal structure*/
6725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->psOverHalCtxt->psHwReference =
6735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly             gpphLibContext->psHwReference;
6745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(NULL == gpphLibContext->psDevInputParam )
6755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
6765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psDevInputParam = (phHal_sDevInputParam_t *)
6775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                phOsalNfc_GetMemory(sizeof(phHal_sDevInputParam_t));
6785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
6795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.is_ndef = CHK_NDEF_NOT_DONE;
6805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
6815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(NULL == gpphLibContext->ndef_cntx.ndef_fmt)
6825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
6835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*Allocate memory for Ndef format structure*/
6845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.ndef_fmt = (phFriNfc_sNdefSmtCrdFmt_t *)
6855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                phOsalNfc_GetMemory(sizeof(phFriNfc_sNdefSmtCrdFmt_t));
6865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
6875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(NULL != gpphLibContext->ndef_cntx.ndef_fmt)
6885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
6895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (void)memset(gpphLibContext->ndef_cntx.ndef_fmt,
6905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        0,
6915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        sizeof(phFriNfc_sNdefSmtCrdFmt_t));
6925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
6935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
6945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
6955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*exception: Not enough memory*/
6965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,1);
6975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
6985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return;
6995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
7005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
7015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* Free the allocated memory used for Ndef operations
7025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
7035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_Ndef_DeInit(void)
7045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
7055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* If only allocated then only free the memory*/
7065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(gpphLibContext->ndef_cntx.psNdefMap !=NULL)
7075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
7085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf !=NULL)
7095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
7105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            phOsalNfc_FreeMemory(gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf);
7115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf=NULL;
7125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
7135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phOsalNfc_FreeMemory(gpphLibContext->ndef_cntx.psNdefMap);
7145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.psNdefMap =NULL;
7155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
7165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(NULL != gpphLibContext->ndef_cntx.ndef_fmt)
7185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
7195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phOsalNfc_FreeMemory(gpphLibContext->ndef_cntx.ndef_fmt);
7205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.ndef_fmt = NULL;
7215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
7225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(gpphLibContext->psOverHalCtxt !=NULL)
7245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
7255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phOsalNfc_FreeMemory(gpphLibContext->psOverHalCtxt);
7265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->psOverHalCtxt =NULL;
7275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
7285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(gpphLibContext->psDevInputParam !=NULL)
7295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
7305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phOsalNfc_FreeMemory(gpphLibContext->psDevInputParam);
7315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->psDevInputParam = NULL;
7325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
7335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(gpphLibContext->psTransInfo!=NULL)
7345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
7355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phOsalNfc_FreeMemory(gpphLibContext->psTransInfo);
7365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->psTransInfo= NULL;
7375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
7385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
7395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
7425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function allows  the user to check whether a particular Remote Device
7435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* is NDEF compliant or not
7445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
7455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phLibNfc_Ndef_CheckNdef(phLibNfc_Handle       hRemoteDevice,
7465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        pphLibNfc_ChkNdefRspCb_t        pCheckNdef_RspCb,
7475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        void*                           pContext)
7485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
7495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS RetVal = NFCSTATUS_FAILED;
7505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if((NULL == gpphLibContext)||
7535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))
7545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
7555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*Lib Nfc not initialized*/
7565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_NOT_INITIALISED;
7575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
7585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if((NULL == pCheckNdef_RspCb)||
7595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (NULL==pContext)||
7605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (hRemoteDevice == 0))
7615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
7625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*parameter sent by upper layer are not valid */
7635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal= NFCSTATUS_INVALID_PARAMETER;
7645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
7655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)
7665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
7675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_SHUTDOWN;
7685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
7695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(0 == gpphLibContext->Connected_handle)
7705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
7715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal=NFCSTATUS_TARGET_NOT_CONNECTED;
7725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
7735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(hRemoteDevice != gpphLibContext->Connected_handle)
7745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
7755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal=NFCSTATUS_INVALID_HANDLE;
7765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
777143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas#ifdef LLCP_TRANSACT_CHANGES
778143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas    else if ((LLCP_STATE_RESET_INIT != gpphLibContext->llcp_cntx.sLlcpContext.state)
779143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas            && (LLCP_STATE_CHECKED != gpphLibContext->llcp_cntx.sLlcpContext.state))
780143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas    {
781143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas        RetVal= NFCSTATUS_BUSY;
782143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas    }
783143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas#endif /* #ifdef LLCP_TRANSACT_CHANGES */
7845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
7855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
7865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        uint8_t     cr_index = 0;
7875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        static uint16_t     data_cnt = 0;
7885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Allocate memory for the ndef related structure */
7895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.NdefSendRecvLen=300;
7905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.eLast_Call = ChkNdef;
7915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Resets the component instance */
7935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = phFriNfc_NdefMap_Reset( gpphLibContext->ndef_cntx.psNdefMap,
7945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->psOverHalCtxt,
7955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            (phLibNfc_sRemoteDevInformation_t*)hRemoteDevice,
7965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->psDevInputParam,
7975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf,
7985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->ndef_cntx.NdefSendRecvLen,
7995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf,
8005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            &(gpphLibContext->ndef_cntx.NdefSendRecvLen),
8015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            &(data_cnt));
8025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        for (cr_index = 0; cr_index < PH_FRINFC_NDEFMAP_CR; cr_index++)
8055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
8065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Register the callback for the check ndef */
8075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal = phFriNfc_NdefMap_SetCompletionRoutine(
8085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                gpphLibContext->ndef_cntx.psNdefMap,
8095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                cr_index,
8105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                phLibNfc_Ndef_CheckNdef_Cb,
8115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                (void *)gpphLibContext);
8125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
8135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*call below layer check Ndef function*/
8145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = phFriNfc_NdefMap_ChkNdef(gpphLibContext->ndef_cntx.psNdefMap);
8155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal =PHNFCSTATUS(RetVal);
8165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(RetVal== NFCSTATUS_PENDING)
8185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
8195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal = NFCSTATUS_PENDING;
8205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
8215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else if((RetVal == NFCSTATUS_FAILED) || (RetVal ==(PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
8225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    NFCSTATUS_INVALID_REMOTE_DEVICE))))
8235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
8245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly			      RetVal= NFCSTATUS_FAILED;
8255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
8265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
8275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
8285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if((0x00 == gpphLibContext->ndef_cntx.Chk_Ndef_Timer_Id)||
8295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly              (PH_OSALNFC_INVALID_TIMER_ID == gpphLibContext->ndef_cntx.Chk_Ndef_Timer_Id))
8305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly	          {
8315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly		            gpphLibContext->ndef_cntx.Chk_Ndef_Timer_Id =
8325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly			          phOsalNfc_Timer_Create();
8335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly	          }
8345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly	          if((0x00 == gpphLibContext->ndef_cntx.Chk_Ndef_Timer_Id)||
8355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly              (PH_OSALNFC_INVALID_TIMER_ID == gpphLibContext->ndef_cntx.Chk_Ndef_Timer_Id))
8365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly	          {
8375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly		            RetVal = NFCSTATUS_FAILED;
8385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly	          }
8395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly	          else
8405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly	          {
8415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly	              phOsalNfc_Timer_Start(gpphLibContext->ndef_cntx.Chk_Ndef_Timer_Id,
8425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly			          CHK_NDEF_TIMER_TIMEOUT,CheckNdef_timer_cb,NULL);
8435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                RetVal = NFCSTATUS_PENDING;
8445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly	          }
8455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
8465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(RetVal== NFCSTATUS_PENDING)
8475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
8485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->CBInfo.pClientCkNdefCb = pCheckNdef_RspCb;
8495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->CBInfo.pClientCkNdefCntx = pContext;
8505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->status.GenCb_pending_status=TRUE;
8515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;
8525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
8535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
8555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return RetVal;
8565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
8575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/* Response callback for phLibNfc_Ndef_CheckNdef */
8595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC
8605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_Ndef_CheckNdef_Cb(void *pContext,NFCSTATUS status)
8615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
8625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phLibNfc_ChkNdef_Info_t    Ndef_Info;
8635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS RetStatus = NFCSTATUS_SUCCESS;
8645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    pphLibNfc_ChkNdefRspCb_t       pClientCb=NULL;
8655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phLibNfc_LibContext_t           *pLibNfc_Ctxt =
8665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    (phLibNfc_LibContext_t *)pContext;
8675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    void                    *pUpperLayerContext=NULL;
8685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phHal_sRemoteDevInformation_t   *ps_rem_dev_info = NULL;
8695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    Ndef_Info.ActualNdefMsgLength = 0;
8715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    Ndef_Info.MaxNdefMsgLength = 0;
87274216af142920e0bce72ec3d9609ec86db211c69Jan Brands    Ndef_Info.NdefCardState = PHLIBNFC_NDEF_CARD_INVALID;
8735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(pLibNfc_Ctxt != gpphLibContext)
8745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {    /*wrong context returned from below layer*/
8755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
8765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
8775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
8785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
8795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        ps_rem_dev_info = (phHal_sRemoteDevInformation_t *)
8805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    gpphLibContext->Connected_handle;
8815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
8825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {   /*shutdown called before completion of check Ndef, allow
8835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly              shutdown to happen */
8845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            phLibNfc_Pending_Shutdown();
8855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetStatus = NFCSTATUS_SHUTDOWN;
8865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
8875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else if(eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)
8885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
8895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetStatus = NFCSTATUS_ABORTED;
8905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
8915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
8925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
8935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(status == NFCSTATUS_SUCCESS)
8945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
8955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /*Tag is Ndef tag*/
8965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->ndef_cntx.is_ndef = TRUE;
8975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (void)phFriNfc_NdefMap_GetContainerSize(
8985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                pLibNfc_Ctxt->ndef_cntx.psNdefMap,
8995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                &(pLibNfc_Ctxt->ndef_cntx.NdefLength),
9005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                &(pLibNfc_Ctxt->ndef_cntx.NdefActualSize));
9015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /*Get the data size support by particular ndef card */
9025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                Ndef_Info.ActualNdefMsgLength = pLibNfc_Ctxt->ndef_cntx.NdefActualSize;
9035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                Ndef_Info.MaxNdefMsgLength = pLibNfc_Ctxt->ndef_cntx.NdefLength;
9045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->LastTrancvSuccess = TRUE;
9055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                RetStatus =NFCSTATUS_SUCCESS;
9065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
9075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            else if (PHNFCSTATUS(status) != NFCSTATUS_MORE_INFORMATION )
9085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
9095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /*Ndef check Failed.Issue a PresenceChk to ascertain if tag is
9105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                  still in the field*/
9115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                RetStatus = phHal4Nfc_PresenceCheck(
9125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    gpphLibContext->psHwReference,
9135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    phLibNfc_Ndef_ChkNdef_Pchk_Cb,
9145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    (void *)gpphLibContext
9155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    );
9165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
9175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            else
9185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
9195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly				RetStatus = NFCSTATUS_FAILED;
9205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->LastTrancvSuccess = FALSE;
9215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->ndef_cntx.is_ndef = FALSE;
9225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
9235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                if ((phHal_eMifare_PICC == ps_rem_dev_info->RemDevType) &&
9245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    (0x08 == (ps_rem_dev_info->RemoteDevInfo.Iso14443A_Info.Sak & 0x08)))
9255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
9265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
9275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    /* card type is mifare 1k/4k, then reconnect */
9285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    RetStatus = phHal4Nfc_Connect(gpphLibContext->psHwReference,
9295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                ps_rem_dev_info,
9305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                (pphHal4Nfc_ConnectCallback_t)
9315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                phLibNfc_Reconnect_Mifare_Cb,
9325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                (void *)gpphLibContext);
9335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                }
9345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                else
9355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
9365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                   if((phHal_eJewel_PICC == ps_rem_dev_info->RemDevType)
9375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                       &&(TOPAZ_NDEF_BITMASK &
9385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                          ps_rem_dev_info->RemoteDevInfo.Jewel_Info.HeaderRom0))
9395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    {
9405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        gpphLibContext->ndef_cntx.is_ndef = TRUE;
9415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        RetStatus = phFriNfc_NdefMap_GetContainerSize(
9425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        pLibNfc_Ctxt->ndef_cntx.psNdefMap,
9435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        &(pLibNfc_Ctxt->ndef_cntx.NdefLength),
9445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        &(pLibNfc_Ctxt->ndef_cntx.NdefActualSize));
9455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        /*Get the data size support by particular ndef card */
9465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        Ndef_Info.ActualNdefMsgLength =
9475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            pLibNfc_Ctxt->ndef_cntx.NdefActualSize;
9485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        Ndef_Info.MaxNdefMsgLength
9495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            = pLibNfc_Ctxt->ndef_cntx.NdefLength
9505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            = (TOPAZ_LEN_BITMASK &
9515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            ps_rem_dev_info->RemoteDevInfo.Jewel_Info.HeaderRom0?
9525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            TOPAZ_DYNAMIC_LEN:TOPAZ_STATIC_CARD_LEN);
9535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        RetStatus = NFCSTATUS_SUCCESS;
9545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    }
9555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                }
9565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
9575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->LibNfcState.cur_state=eLibNfcHalStateConnect;
9585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
9595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->status.GenCb_pending_status = FALSE;
9605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Update the current state */
9615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phLibNfc_UpdateCurState(RetStatus,gpphLibContext);
9625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(NFCSTATUS_PENDING != RetStatus)
9635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
9645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(((ps_rem_dev_info->RemDevType == phHal_eMifare_PICC)
9655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                && (ps_rem_dev_info->RemoteDevInfo.Iso14443A_Info.Sak != 0)&&
9665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               ((NULL == gpphLibContext->psBufferedAuth)
9675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                ||(phHal_eMifareAuthentA == gpphLibContext->psBufferedAuth->cmd.MfCmd)))
9685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               )
9695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
9705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                if(NULL != gpphLibContext->psBufferedAuth)
9715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
9725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    if(NULL != gpphLibContext->psBufferedAuth->sRecvData.buffer)
9735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    {
9745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        phOsalNfc_FreeMemory(
9755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->psBufferedAuth->sRecvData.buffer);
9765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    }
9775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    if(NULL != gpphLibContext->psBufferedAuth->sSendData.buffer)
9785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    {
9795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        phOsalNfc_FreeMemory(
9805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->psBufferedAuth->sSendData.buffer);
9815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    }
9825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    phOsalNfc_FreeMemory(gpphLibContext->psBufferedAuth);
9835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                }
9845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->psBufferedAuth
9855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    =(phLibNfc_sTransceiveInfo_t *)
9865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    phOsalNfc_GetMemory(sizeof(phLibNfc_sTransceiveInfo_t));
9875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->psBufferedAuth->addr =
9885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (uint8_t)gpphLibContext->ndef_cntx.psNdefMap
9895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                ->StdMifareContainer.currentBlock;
9905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->psBufferedAuth->cmd.MfCmd = phHal_eMifareRead16;
9915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->psBufferedAuth->sSendData.length
9925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    = 0;
9935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->psBufferedAuth->sRecvData.length
9945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    = MIFARE_STD_BLOCK_SIZE;
9955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->psBufferedAuth->sRecvData.buffer
9965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    = (uint8_t *)phOsalNfc_GetMemory(MIFARE_STD_BLOCK_SIZE);
9975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->psBufferedAuth->sSendData.buffer
9985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    = (uint8_t *)phOsalNfc_GetMemory(MIFARE_STD_BLOCK_SIZE);
9995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
10005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pClientCb = gpphLibContext->CBInfo.pClientCkNdefCb;
10015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pUpperLayerContext = gpphLibContext->CBInfo.pClientCkNdefCntx;
10025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->CBInfo.pClientCkNdefCb = NULL;
10035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->CBInfo.pClientCkNdefCntx = NULL;
10045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(NULL != pClientCb)
10055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
100674216af142920e0bce72ec3d9609ec86db211c69Jan Brands                if (!RetStatus)
100774216af142920e0bce72ec3d9609ec86db211c69Jan Brands                {
100874216af142920e0bce72ec3d9609ec86db211c69Jan Brands                    switch (pLibNfc_Ctxt->ndef_cntx.psNdefMap->CardState)
100974216af142920e0bce72ec3d9609ec86db211c69Jan Brands                    {
101074216af142920e0bce72ec3d9609ec86db211c69Jan Brands                        case PH_NDEFMAP_CARD_STATE_INITIALIZED:
101174216af142920e0bce72ec3d9609ec86db211c69Jan Brands                        {
101274216af142920e0bce72ec3d9609ec86db211c69Jan Brands                            Ndef_Info.NdefCardState =
101374216af142920e0bce72ec3d9609ec86db211c69Jan Brands                                            PHLIBNFC_NDEF_CARD_INITIALISED;
101474216af142920e0bce72ec3d9609ec86db211c69Jan Brands                            break;
101574216af142920e0bce72ec3d9609ec86db211c69Jan Brands                        }
101674216af142920e0bce72ec3d9609ec86db211c69Jan Brands
101774216af142920e0bce72ec3d9609ec86db211c69Jan Brands                        case PH_NDEFMAP_CARD_STATE_READ_ONLY:
101874216af142920e0bce72ec3d9609ec86db211c69Jan Brands                        {
101974216af142920e0bce72ec3d9609ec86db211c69Jan Brands                            Ndef_Info.NdefCardState =
102074216af142920e0bce72ec3d9609ec86db211c69Jan Brands                                            PHLIBNFC_NDEF_CARD_READ_ONLY;
102174216af142920e0bce72ec3d9609ec86db211c69Jan Brands                            break;
102274216af142920e0bce72ec3d9609ec86db211c69Jan Brands                        }
102374216af142920e0bce72ec3d9609ec86db211c69Jan Brands
102474216af142920e0bce72ec3d9609ec86db211c69Jan Brands                        case PH_NDEFMAP_CARD_STATE_READ_WRITE:
102574216af142920e0bce72ec3d9609ec86db211c69Jan Brands                        {
102674216af142920e0bce72ec3d9609ec86db211c69Jan Brands                            Ndef_Info.NdefCardState =
102774216af142920e0bce72ec3d9609ec86db211c69Jan Brands                                            PHLIBNFC_NDEF_CARD_READ_WRITE;
102874216af142920e0bce72ec3d9609ec86db211c69Jan Brands                            break;
102974216af142920e0bce72ec3d9609ec86db211c69Jan Brands                        }
103074216af142920e0bce72ec3d9609ec86db211c69Jan Brands
103174216af142920e0bce72ec3d9609ec86db211c69Jan Brands                        default:
103274216af142920e0bce72ec3d9609ec86db211c69Jan Brands                        {
103374216af142920e0bce72ec3d9609ec86db211c69Jan Brands                            Ndef_Info.NdefCardState =
103474216af142920e0bce72ec3d9609ec86db211c69Jan Brands                                            PHLIBNFC_NDEF_CARD_INVALID;
103574216af142920e0bce72ec3d9609ec86db211c69Jan Brands                            break;
103674216af142920e0bce72ec3d9609ec86db211c69Jan Brands                        }
103774216af142920e0bce72ec3d9609ec86db211c69Jan Brands                    }
103874216af142920e0bce72ec3d9609ec86db211c69Jan Brands                }
10395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* call the upper check ndef callback */
10405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pClientCb(pUpperLayerContext,Ndef_Info,RetStatus);
10415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
10425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
10435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
10445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return;
10455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
10465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*Callback for Presence check call from Chk Ndef*/
10485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC void phLibNfc_Ndef_ChkNdef_Pchk_Cb(void   *pContext,
10495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                NFCSTATUS  status
10505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                )
10515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
10525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phLibNfc_ChkNdef_Info_t    Ndef_Info = {0,0};
10535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS RetStatus = NFCSTATUS_SUCCESS;
10545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    pphLibNfc_ChkNdefRspCb_t       pClientCb=NULL;
10555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phLibNfc_LibContext_t           *pLibNfc_Ctxt =
10565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    (phLibNfc_LibContext_t *)pContext;
10575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    void                    *pUpperLayerContext=NULL;
10585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phHal_sRemoteDevInformation_t   *ps_rem_dev_info = NULL;
10595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(NFCSTATUS_SUCCESS == status)
10605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
10615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetStatus = NFCSTATUS_FAILED;
10625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.is_ndef = FALSE;
10635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
10645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
10655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
10665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetStatus = NFCSTATUS_TARGET_LOST;
10675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
10685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(pLibNfc_Ctxt != gpphLibContext)
10695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {    /*wrong context returned from below layer*/
10705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
10715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
10725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
10735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
10745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        ps_rem_dev_info = (phHal_sRemoteDevInformation_t *)
10755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    gpphLibContext->Connected_handle;
10765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(((ps_rem_dev_info->RemDevType == phHal_eMifare_PICC)
10775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            && (ps_rem_dev_info->RemoteDevInfo.Iso14443A_Info.Sak != 0)&&
10785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            ((NULL == gpphLibContext->psBufferedAuth)
10795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            ||(phHal_eMifareAuthentA == gpphLibContext->psBufferedAuth->cmd.MfCmd)))
10805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            )
10815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
10825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(NULL != gpphLibContext->psBufferedAuth)
10835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
10845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                if(NULL != gpphLibContext->psBufferedAuth->sRecvData.buffer)
10855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
10865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    phOsalNfc_FreeMemory(
10875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        gpphLibContext->psBufferedAuth->sRecvData.buffer);
10885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                }
10895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                if(NULL != gpphLibContext->psBufferedAuth->sSendData.buffer)
10905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
10915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    phOsalNfc_FreeMemory(
10925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        gpphLibContext->psBufferedAuth->sSendData.buffer);
10935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                }
10945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                phOsalNfc_FreeMemory(gpphLibContext->psBufferedAuth);
10955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
10965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth
10975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                =(phLibNfc_sTransceiveInfo_t *)
10985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                phOsalNfc_GetMemory(sizeof(phLibNfc_sTransceiveInfo_t));
10995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->addr =
11005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            (uint8_t)gpphLibContext->ndef_cntx.psNdefMap
11015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            ->StdMifareContainer.currentBlock;
11025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->cmd.MfCmd = phHal_eMifareRead16;
11035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->sSendData.length
11045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                = 0;
11055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->sRecvData.length
11065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                = MIFARE_STD_BLOCK_SIZE;
11075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->sRecvData.buffer
11085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                = (uint8_t *)phOsalNfc_GetMemory(MIFARE_STD_BLOCK_SIZE);
11095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->psBufferedAuth->sSendData.buffer
11105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                = (uint8_t *)phOsalNfc_GetMemory(MIFARE_STD_BLOCK_SIZE);
11115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
11125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        pClientCb = gpphLibContext->CBInfo.pClientCkNdefCb;
11135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        pUpperLayerContext = gpphLibContext->CBInfo.pClientCkNdefCntx;
11145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->CBInfo.pClientCkNdefCb = NULL;
11155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->CBInfo.pClientCkNdefCntx = NULL;
11165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(NULL != pClientCb)
11175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
111874216af142920e0bce72ec3d9609ec86db211c69Jan Brands            Ndef_Info.NdefCardState = PHLIBNFC_NDEF_CARD_INVALID;
11195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* call the upper check ndef callback */
11205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pClientCb(pUpperLayerContext,Ndef_Info,RetStatus);
11215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
11225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
11235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return;
11245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
11255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/* Check Ndef Timer Callback*/
11265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC void CheckNdef_timer_cb(uint32_t timer_id, void *pContext)
11275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
11285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly	PHNFC_UNUSED_VARIABLE(pContext);
11295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   phOsalNfc_Timer_Stop(timer_id);
11305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly	phOsalNfc_Timer_Delete(gpphLibContext->ndef_cntx.Chk_Ndef_Timer_Id);
11315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly	gpphLibContext->ndef_cntx.Chk_Ndef_Timer_Id = 0x00;
11325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly	phLibNfc_Ndef_CheckNdef_Cb((void *)gpphLibContext,NFCSTATUS_MORE_INFORMATION);
11335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
11345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
11355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_Reconnect_Mifare_Cb (
11365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    void                            *pContext,
11375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    phHal_sRemoteDevInformation_t   *psRemoteDevInfo,
11385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    NFCSTATUS                       status)
11395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
11405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phLibNfc_ChkNdef_Info_t     Ndef_Info;
11415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phLibNfc_LibContext_t       *pLibNfc_Ctxt =
11425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                (phLibNfc_LibContext_t *)pContext;
11435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    void                        *pUpperLayerContext = NULL;
11445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    switch(gpphLibContext->ndef_cntx.eLast_Call)
11455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
11465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case ChkNdef:
11475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
11485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pphLibNfc_ChkNdefRspCb_t    pClientCb=NULL;
11495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pClientCb = pLibNfc_Ctxt->CBInfo.pClientCkNdefCb;
11505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pUpperLayerContext = pLibNfc_Ctxt->CBInfo.pClientCkNdefCntx;
11515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLibNfc_Ctxt->CBInfo.pClientCkNdefCb = NULL;
11525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLibNfc_Ctxt->CBInfo.pClientCkNdefCntx = NULL;
11535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if (NULL != pClientCb)
11545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
11555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                status = (NFCSTATUS_SUCCESS == status?
11565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        NFCSTATUS_FAILED:NFCSTATUS_TARGET_LOST);
11575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                Ndef_Info.ActualNdefMsgLength = 0;
11585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                Ndef_Info.MaxNdefMsgLength = 0;
11595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* call the upper check ndef callback */
11605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pClientCb(pUpperLayerContext,Ndef_Info,status);
11615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
11625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
11635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
11645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case NdefRd:
11655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
11665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pphLibNfc_RspCb_t       pClientCb = pLibNfc_Ctxt->CBInfo.pClientRdNdefCb;
11675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pUpperLayerContext = pLibNfc_Ctxt->CBInfo.pClientRdNdefCntx;
11685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLibNfc_Ctxt->CBInfo.pClientRdNdefCb = NULL;
11695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLibNfc_Ctxt->CBInfo.pClientRdNdefCntx = NULL;
11705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if (NULL != pClientCb)
11715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
11725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                status = (NFCSTATUS_SUCCESS == status?
11735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        NFCSTATUS_FAILED:NFCSTATUS_TARGET_LOST);
11745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* call the upper ndef read callback */
11755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pClientCb(pUpperLayerContext,status);
11765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
11775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
11785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
11795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case NdefWr:
11805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
11815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pphLibNfc_RspCb_t       pClientCb =  pLibNfc_Ctxt->CBInfo.pClientWrNdefCb;
11825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pUpperLayerContext = pLibNfc_Ctxt->CBInfo.pClientWrNdefCntx;
11835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLibNfc_Ctxt->CBInfo.pClientWrNdefCb = NULL;
11845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLibNfc_Ctxt->CBInfo.pClientWrNdefCntx = NULL;
11855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if (NULL != pClientCb)
11865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
11875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                status = (NFCSTATUS_SUCCESS == status?
11885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        NFCSTATUS_FAILED:NFCSTATUS_TARGET_LOST);
11895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* call the upper ndef write callback */
11905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pClientCb(pUpperLayerContext,status);
11915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
11925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
11935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
11945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case NdefFmt:
119580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen#ifdef LIBNFC_READONLY_NDEF
119680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        case NdefReadOnly:
119780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen#endif /* #ifdef LIBNFC_READONLY_NDEF */
11985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
11995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pphLibNfc_RspCb_t       pClientCb =
12005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                           pLibNfc_Ctxt->ndef_cntx.pClientNdefFmtCb;
12015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pUpperLayerContext= pLibNfc_Ctxt->ndef_cntx.pClientNdefFmtCntx;
12025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLibNfc_Ctxt->ndef_cntx.pClientNdefFmtCb = NULL;
12035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLibNfc_Ctxt->ndef_cntx.pClientNdefFmtCntx = NULL;
12045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if (NULL != pClientCb)
12055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
12065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                status = (NFCSTATUS_SUCCESS == status?
12075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        NFCSTATUS_FAILED:NFCSTATUS_TARGET_LOST);
12085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* call the upper ndef format callback */
12095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pClientCb(pUpperLayerContext,status);
12105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
12115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
12125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
12135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case RawTrans:
12145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
12155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            phNfc_sData_t trans_resp;
12165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pphLibNfc_TransceiveCallback_t pClientCb =
12175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                           pLibNfc_Ctxt->CBInfo.pClientTransceiveCb;
12185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            trans_resp.length = 0;
12195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pUpperLayerContext = pLibNfc_Ctxt->CBInfo.pClientTranseCntx;
12205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLibNfc_Ctxt->CBInfo.pClientTranseCntx= NULL;
12215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLibNfc_Ctxt->CBInfo.pClientTransceiveCb= NULL;
12225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if (NULL != pClientCb)
12235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
12245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                status = (NFCSTATUS_SUCCESS == status?
12255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        NFCSTATUS_FAILED:NFCSTATUS_TARGET_LOST);
12265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* call the upper transceive callback */
12275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pClientCb(pUpperLayerContext,
12285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        (uint32_t)psRemoteDevInfo,
12295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        & trans_resp,
12305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        status);
12315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
12325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
12335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
12345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        default:
12355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
12365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
12375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
12385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
12395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
12405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
12415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
12425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* Target format to make it NDEF compliant
12435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
12445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phLibNfc_RemoteDev_FormatNdef(phLibNfc_Handle         hRemoteDevice,
12455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phNfc_sData_t*          pScrtKey,
12465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        pphLibNfc_RspCb_t       pNdefformat_RspCb,
12475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        void*                   pContext
12485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        )
12495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
12505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS RetVal = NFCSTATUS_FAILED;
12515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
12525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    static uint8_t       mif_std_key[6] ={0},
12535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                         Index = 0;
12545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if((NULL == gpphLibContext)
12555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        ||(gpphLibContext->LibNfcState.cur_state
12565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            == eLibNfcHalStateShutdown))
12575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
12585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*Lib Nfc not initialized*/
12595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_NOT_INITIALISED;
12605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
12615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if((NULL == pContext)
12625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        || (NULL == pNdefformat_RspCb)
12635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        ||(NULL == pScrtKey)
12645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        ||(0 == hRemoteDevice))
12655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
12665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal= NFCSTATUS_INVALID_PARAMETER;
12675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
12685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(gpphLibContext->LibNfcState.next_state
12695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            == eLibNfcHalStateShutdown)
12705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
12715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal= NFCSTATUS_SHUTDOWN;
12725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
12735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(0 == gpphLibContext->Connected_handle)
12745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
12755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal=NFCSTATUS_TARGET_NOT_CONNECTED;
12765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
12775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(hRemoteDevice != gpphLibContext->Connected_handle)
12785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
12795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal=NFCSTATUS_INVALID_HANDLE;
12805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
12815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if((TRUE == gpphLibContext->status.GenCb_pending_status)||
12825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (NULL != gpphLibContext->ndef_cntx.pClientNdefFmtCb)
12835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        ||(gpphLibContext->ndef_cntx.is_ndef == TRUE))
12845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
12855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*Previous Callback is Pending*/
12865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_REJECTED;
12875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        PHDBG_INFO("LIbNfc:Previous Callback is Pending");
12885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
1289143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas#ifdef LLCP_TRANSACT_CHANGES
1290143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas    else if ((LLCP_STATE_RESET_INIT != gpphLibContext->llcp_cntx.sLlcpContext.state)
1291143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas            && (LLCP_STATE_CHECKED != gpphLibContext->llcp_cntx.sLlcpContext.state))
1292143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas    {
1293143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas        RetVal= NFCSTATUS_BUSY;
1294143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas    }
1295143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas#endif /* #ifdef LLCP_TRANSACT_CHANGES */
12965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
12975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
12985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        uint8_t   fun_id;
12995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.eLast_Call = NdefFmt;
13005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.NdefSendRecvLen = NDEF_SENDRCV_BUF_LEN;
13015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
13025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Call ndef format reset, this will initialize the ndef
13035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        format structure, and appropriate values are filled */
13045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = phFriNfc_NdefSmtCrd_Reset(gpphLibContext->ndef_cntx.ndef_fmt,
13055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->psOverHalCtxt,
13065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            (phHal_sRemoteDevInformation_t*)hRemoteDevice,
13075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->psDevInputParam,
13085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf,
13095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            &(gpphLibContext->ndef_cntx.NdefSendRecvLen));
13105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        for(fun_id = 0; fun_id < PH_FRINFC_SMTCRDFMT_CR; fun_id++)
13115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
13125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Register for all the callbacks */
13135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal = phFriNfc_NdefSmtCrd_SetCR(gpphLibContext->ndef_cntx.ndef_fmt,
13145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        fun_id,
13155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        phLibNfc_Ndef_format_Cb,
13165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        gpphLibContext);
13175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
13185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* mif_std_key is required to format the mifare 1k/4k card */
13195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        for (Index =0 ;Index < (pScrtKey->length); Index++ )
13205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
13215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            mif_std_key[Index] = *(pScrtKey->buffer++);
13225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
13235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Start smart card formatting function   */
13245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = phFriNfc_NdefSmtCrd_Format(gpphLibContext->ndef_cntx.ndef_fmt,
13255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        mif_std_key);
13265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly		RetVal = PHNFCSTATUS(RetVal);
13275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(RetVal== NFCSTATUS_PENDING)
13285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
13295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->ndef_cntx.pClientNdefFmtCb = pNdefformat_RspCb;
13305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->ndef_cntx.pClientNdefFmtCntx = pContext;
13315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->status.GenCb_pending_status=TRUE;
13325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;
13335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
13345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
13355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
13365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal = NFCSTATUS_FAILED;
13375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
13385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
13395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return RetVal;
13405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
13415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
134280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen#ifdef LIBNFC_READONLY_NDEF
134380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
134480ee29b88ec489e2bbf6ae446692229d36755429Martijn CoenenNFCSTATUS
134580ee29b88ec489e2bbf6ae446692229d36755429Martijn CoenenphLibNfc_ConvertToReadOnlyNdef (
134680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    phLibNfc_Handle         hRemoteDevice,
134780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    pphLibNfc_RspCb_t       pNdefReadOnly_RspCb,
134880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    void*                   pContext
134980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    )
135080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen{
135180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    NFCSTATUS           ret_val = NFCSTATUS_FAILED;
135280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
135380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    if ((NULL == gpphLibContext)
135480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        || (gpphLibContext->LibNfcState.cur_state
135580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                            == eLibNfcHalStateShutdown))
135680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    {
135780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        /* LibNfc not initialized */
135880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        ret_val = NFCSTATUS_NOT_INITIALISED;
135980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    }
136080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    else if ((NULL == pContext)
136180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        || (NULL == pNdefReadOnly_RspCb)
136280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        || (0 == hRemoteDevice))
136380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    {
136480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        ret_val = NFCSTATUS_INVALID_PARAMETER;
136580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    }
136680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    else if (gpphLibContext->LibNfcState.next_state
136780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            == eLibNfcHalStateShutdown)
136880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    {
136980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        ret_val = NFCSTATUS_SHUTDOWN;
137080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    }
137180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    else if (0 == gpphLibContext->Connected_handle)
137280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    {
137380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        ret_val = NFCSTATUS_TARGET_NOT_CONNECTED;
137480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    }
137580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    else if (hRemoteDevice != gpphLibContext->Connected_handle)
137680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    {
137780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        ret_val = NFCSTATUS_INVALID_HANDLE;
137880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    }
137980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    else if ((TRUE == gpphLibContext->status.GenCb_pending_status)
138080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        || (NULL != gpphLibContext->ndef_cntx.pClientNdefFmtCb)
138180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        || (FALSE == gpphLibContext->ndef_cntx.is_ndef))
138280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    {
138380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        /* Previous Callback is Pending */
138480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        ret_val = NFCSTATUS_REJECTED;
138580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        PHDBG_INFO("LIbNfc:Previous Callback is Pending");
138680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    }
138780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    else if (PH_NDEFMAP_CARD_STATE_READ_WRITE !=
138880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            gpphLibContext->ndef_cntx.psNdefMap->CardState)
138980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    {
139080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        /* Tag is in different state */
139180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        ret_val = NFCSTATUS_REJECTED;
139280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    }
139380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    else
139480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    {
139580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        gpphLibContext->ndef_cntx.eLast_Call = NdefReadOnly;
139680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
139780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        if(eLibNfcHalStatePresenceChk != gpphLibContext->LibNfcState.next_state)
139880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        {
139980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            phHal_sRemoteDevInformation_t           *ps_rem_dev_info =
140080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                                (phHal_sRemoteDevInformation_t *)hRemoteDevice;
140180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            uint8_t                                 fun_id;
140280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
140380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            switch (ps_rem_dev_info->RemDevType)
140480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            {
140580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                case phHal_eMifare_PICC:
140680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                case phHal_eISO14443_A_PICC:
140780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                {
140880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    if ((phHal_eMifare_PICC == ps_rem_dev_info->RemDevType)
140980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                        && (0x00 != ps_rem_dev_info->RemoteDevInfo.Iso14443A_Info.Sak))
141080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    {
141180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                        /* Mifare classic 1k/4k not supported */
141280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                        ret_val = NFCSTATUS_REJECTED;
141380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    }
141480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    else
141580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    {
141680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                        gpphLibContext->ndef_cntx.NdefSendRecvLen = NDEF_SENDRCV_BUF_LEN;
141780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
141880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                        /* Call ndef format reset, this will initialize the ndef
141980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                        format structure, and appropriate values are filled */
142080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                        ret_val = phFriNfc_NdefSmtCrd_Reset (gpphLibContext->ndef_cntx.ndef_fmt,
142180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                                gpphLibContext->psOverHalCtxt,
142280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                                (phHal_sRemoteDevInformation_t*)hRemoteDevice,
142380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                                gpphLibContext->psDevInputParam,
142480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                                gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf,
142580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                                &(gpphLibContext->ndef_cntx.NdefSendRecvLen));
142680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
142780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                        for(fun_id = 0; fun_id < PH_FRINFC_SMTCRDFMT_CR; fun_id++)
142880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                        {
142980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                            /* Register for all the callbacks */
143080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                            ret_val = phFriNfc_NdefSmtCrd_SetCR (gpphLibContext->ndef_cntx.ndef_fmt,
143180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                                                fun_id, phLibNfc_Ndef_ReadOnly_Cb,
143280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                                                gpphLibContext);
143380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                        }
143480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
143580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                        /* Start smart card formatting function   */
143680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                        ret_val = phFriNfc_NdefSmtCrd_ConvertToReadOnly (
143780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                                        gpphLibContext->ndef_cntx.ndef_fmt);
143880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                        ret_val = PHNFCSTATUS(ret_val);
143980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    }
144080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    break;
144180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                }
144280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
144380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                case phHal_eJewel_PICC:
144480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                {
1445a6243755a9b438bfbfe0be331ece55953f8cf9ebMartijn Coenen// MC: Got the feedback this was #if 0'd because it was resetting the lock bits
1446a6243755a9b438bfbfe0be331ece55953f8cf9ebMartijn Coenen// read in check NDEF, and these should not be reset here already.
1447a6243755a9b438bfbfe0be331ece55953f8cf9ebMartijn Coenen#if 0
144880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    static uint16_t     data_cnt = 0;
144980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
145080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    /* Resets the component instance */
145180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    ret_val = phFriNfc_NdefMap_Reset (gpphLibContext->ndef_cntx.psNdefMap,
145280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                        gpphLibContext->psOverHalCtxt,
145380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                        (phLibNfc_sRemoteDevInformation_t*)hRemoteDevice,
145480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                        gpphLibContext->psDevInputParam,
145580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                        gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf,
145680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                        gpphLibContext->ndef_cntx.NdefSendRecvLen,
145780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                        gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf,
145880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                        &(gpphLibContext->ndef_cntx.NdefSendRecvLen),
145980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                        &(data_cnt));
1460a6243755a9b438bfbfe0be331ece55953f8cf9ebMartijn Coenen#endif /* #if 0 */
146180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
146280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
146380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    for (fun_id = 0; fun_id < PH_FRINFC_NDEFMAP_CR; fun_id++)
146480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    {
146580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                        /* Register the callback for the check ndef */
146680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                        ret_val = phFriNfc_NdefMap_SetCompletionRoutine (
146780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                            gpphLibContext->ndef_cntx.psNdefMap,
146880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                            fun_id, phLibNfc_Ndef_ReadOnly_Cb,
146980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                            (void *)gpphLibContext);
147080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    }
147180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
147280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    /* call below layer check Ndef function */
147380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    ret_val = phFriNfc_NdefMap_ConvertToReadOnly (
147480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                            gpphLibContext->ndef_cntx.psNdefMap);
147580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    ret_val = PHNFCSTATUS(ret_val);
147680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    break;
147780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                }
147880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
147980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                default:
148080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                {
148180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    /* Tag not supported */
148280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    ret_val = NFCSTATUS_REJECTED;
148380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                    break;
148480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                }
148580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            }
148680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        }
148780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        else
148880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        {
148980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen             gpphLibContext->ndef_cntx.pClientNdefFmtCb= NULL;
149080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen             ret_val = NFCSTATUS_PENDING;
149180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        }
149280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
149380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        if (NFCSTATUS_PENDING == ret_val)
149480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        {
149580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            gpphLibContext->ndef_cntx.pClientNdefFmtCb = pNdefReadOnly_RspCb;
149680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            gpphLibContext->ndef_cntx.pClientNdefFmtCntx = pContext;
149780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
149880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            gpphLibContext->status.GenCb_pending_status = TRUE;
149980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;
150080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        }
150180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        else
150280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        {
150380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            ret_val = NFCSTATUS_FAILED;
150480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        }
150580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    }
150680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    return ret_val;
150780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen}
150880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
150980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen#endif /* #ifdef LIBNFC_READONLY_NDEF */
151080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
15115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
15125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* Response callback for NDEF format.
15135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
15145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC
15155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_Ndef_format_Cb(void *Context,NFCSTATUS  status)
15165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
15175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS RetStatus = NFCSTATUS_SUCCESS;
15185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    pphLibNfc_RspCb_t       pClientCb=NULL;
15195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phLibNfc_LibContext_t   *pLibNfc_Ctxt = (phLibNfc_LibContext_t *)Context;
15205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    void                    *pUpperLayerContext=NULL;
15215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phHal_sRemoteDevInformation_t   *ps_rem_dev_info = NULL;
15225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(pLibNfc_Ctxt != gpphLibContext)
15235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {   /*wrong context returned*/
15245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
15255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
15265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
15275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
15285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
15295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
15305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /*shutdown is pending so issue shutdown*/
15315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            phLibNfc_Pending_Shutdown();
15325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetStatus = NFCSTATUS_SHUTDOWN;
15335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
15345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else if(eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)
15355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
15365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetStatus = NFCSTATUS_ABORTED;
15375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
15385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
15395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
15405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->status.GenCb_pending_status = FALSE;
15415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(NFCSTATUS_SUCCESS == status)
15425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
15435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                RetStatus = NFCSTATUS_SUCCESS;
15445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
15455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            else if(PHNFCSTATUS(status)==NFCSTATUS_FAILED)
15465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
15475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                RetStatus = NFCSTATUS_FAILED;
15485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                ps_rem_dev_info = (phHal_sRemoteDevInformation_t *)
15495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    gpphLibContext->Connected_handle;
15505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                if ((phHal_eMifare_PICC == ps_rem_dev_info->RemDevType) &&
15515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    (0x08 == (ps_rem_dev_info->RemoteDevInfo.Iso14443A_Info.Sak & 0x08)))
15525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
15535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
15545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    /* card type is mifare 1k/4k, then reconnect */
15555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    RetStatus = phHal4Nfc_Connect(gpphLibContext->psHwReference,
15565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                (phHal_sRemoteDevInformation_t *)
15575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                gpphLibContext->Connected_handle,
15585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                (pphHal4Nfc_ConnectCallback_t)
15595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                phLibNfc_Reconnect_Mifare_Cb,
15605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                (void *)gpphLibContext);
15615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                }
15625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
15635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly			else
15645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
15655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /*Target was removed during transaction*/
15665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                RetStatus = NFCSTATUS_FAILED;
15675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
15685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->LibNfcState.cur_state =eLibNfcHalStateConnect;
15695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
15705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phLibNfc_UpdateCurState(status,gpphLibContext);
15715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
15725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        pClientCb = gpphLibContext->ndef_cntx.pClientNdefFmtCb;
15735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        pUpperLayerContext= gpphLibContext->ndef_cntx.pClientNdefFmtCntx;
15745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.pClientNdefFmtCb = NULL;
15755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.pClientNdefFmtCntx = NULL;
15765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(NFCSTATUS_PENDING != RetStatus)
15775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
15785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if (NULL != pClientCb)
15795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
15805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* Call the tag format upper layer callback */
15815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pClientCb(pUpperLayerContext,RetStatus);
15825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
15835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
15845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
15855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return;
15865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
15875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
158880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen#ifdef LIBNFC_READONLY_NDEF
158980ee29b88ec489e2bbf6ae446692229d36755429Martijn CoenenSTATIC
159080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenenvoid
159180ee29b88ec489e2bbf6ae446692229d36755429Martijn CoenenphLibNfc_Ndef_ReadOnly_Cb (
159280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    void        *p_context,
159380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    NFCSTATUS   status)
159480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen{
159580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    NFCSTATUS                       ret_status = NFCSTATUS_SUCCESS;
159680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    pphLibNfc_RspCb_t               p_client_cb = NULL;
159780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    phLibNfc_LibContext_t           *pLibNfc_Ctxt = (phLibNfc_LibContext_t *)p_context;
159880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    void                            *p_upper_layer_ctxt = NULL;
159980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
160080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    if(pLibNfc_Ctxt != gpphLibContext)
160180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    {
160280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        /*wrong context returned*/
160380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
160480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    }
160580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    else
160680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    {
160780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
160880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        {
160980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            /*shutdown is pending so issue shutdown*/
161080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            phLibNfc_Pending_Shutdown();
161180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            ret_status = NFCSTATUS_SHUTDOWN;
161280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        }
161380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        else if(eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)
161480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        {
161580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            ret_status = NFCSTATUS_ABORTED;
161680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        }
161780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        else
161880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        {
161980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            gpphLibContext->status.GenCb_pending_status = FALSE;
162080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            if(NFCSTATUS_SUCCESS == status)
162180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            {
162280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                gpphLibContext->ndef_cntx.psNdefMap->CardState =
162380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                                                PH_NDEFMAP_CARD_STATE_READ_ONLY;
162480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                ret_status = NFCSTATUS_SUCCESS;
162580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            }
162680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            else
162780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            {
162880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                ret_status = NFCSTATUS_FAILED;
162980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            }
163080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            gpphLibContext->LibNfcState.cur_state =eLibNfcHalStateConnect;
163180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        }
163280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
163380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        phLibNfc_UpdateCurState(status, gpphLibContext);
163480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
163580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        p_client_cb = gpphLibContext->ndef_cntx.pClientNdefFmtCb;
163680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        p_upper_layer_ctxt = gpphLibContext->ndef_cntx.pClientNdefFmtCntx;
163780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        gpphLibContext->ndef_cntx.pClientNdefFmtCb = NULL;
163880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        gpphLibContext->ndef_cntx.pClientNdefFmtCntx = NULL;
163980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        if(NFCSTATUS_PENDING != ret_status)
164080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        {
164180ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            if (NULL != p_client_cb)
164280ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            {
164380ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                /* Call the tag format upper layer callback */
164480ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen                p_client_cb (p_upper_layer_ctxt, ret_status);
164580ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen            }
164680ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen        }
164780ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen    }
164880ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen}
164980ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen#endif /* #ifdef LIBNFC_READONLY_NDEF */
165080ee29b88ec489e2bbf6ae446692229d36755429Martijn Coenen
16515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC
16525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_Ndef_SrchNdefCnt_Cb(void *context, NFCSTATUS status)
16535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
16545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    static NFCSTATUS RegPrSt=FALSE;
16555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint8_t RegStatus=0;
16565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS RetVal = NFCSTATUS_SUCCESS ;
16575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint32_t Index=0;
16585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
16595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
16605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly	  PHNFC_UNUSED_VARIABLE(context);
16615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
16625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {   /*shutdown called before completion of Ndef read allow
16635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly              shutdown to happen */
16645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phLibNfc_Pending_Shutdown();
16655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_SHUTDOWN;
16665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
16675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)
16685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
16695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_ABORTED;
16705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
16715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(NFCSTATUS_SUCCESS != status)
16725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
16735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = status;
16745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
16755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
16765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
16775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     /* This conditional branch is for QMORE fix */
16785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
16795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    gpphLibContext->status.GenCb_pending_status = FALSE;
16805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
16815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phLibNfc_UpdateCurState(status,gpphLibContext);
16825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Read is not success send failed to upperlayer Call back*/
16835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if( RetVal!= NFCSTATUS_SUCCESS )
16845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
16855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if((RetVal!=NFCSTATUS_SHUTDOWN)&& (RetVal!=NFCSTATUS_ABORTED))
16865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
16875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal= NFCSTATUS_FAILED;
16885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
16895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->CBInfo.pClientNdefNtfRespCb(
16905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->CBInfo.pClientNdefNtfRespCntx,
16915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            NULL,
16925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->Connected_handle,
16935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            RetVal);
16945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->CBInfo.pClientNdefNtfRespCb = NULL;
16955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->CBInfo.pClientNdefNtfRespCntx = NULL;
16965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        return;
16975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
16985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
16995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /*Get the Number of records ( If Raw record parameter is null then API gives number of Records*/
17005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    RetVal = phFriNfc_NdefRecord_GetRecords(
17015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer,
17025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->phLib_NdefRecCntx.ndef_message.length,
17035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            NULL,
17045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->phLib_NdefRecCntx.IsChunked,
17055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            &(gpphLibContext->phLib_NdefRecCntx.NumberOfRawRecords));
17065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
17075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefInfo.pNdefMessage = gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer;
17085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefInfo.NdefMessageLengthActual = gpphLibContext->ndef_cntx.NdefActualSize;
17095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefInfo.NdefMessageLengthMaximum = gpphLibContext->ndef_cntx.NdefLength;
17105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefInfo.NdefRecordCount =0;
17115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
17125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /*Allocate memory to hold the records Read*/
17135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefInfo.pNdefRecord = phOsalNfc_GetMemory
17145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (sizeof(phFriNfc_NdefRecord_t)* gpphLibContext->phLib_NdefRecCntx.NumberOfRawRecords );
17155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(NULL==NdefInfo.pNdefRecord)
17165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
17175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->CBInfo.pClientNdefNtfRespCb(
17185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->CBInfo.pClientNdefNtfRespCntx,
17195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            NULL,
17205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->Connected_handle,
17215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            NFCSTATUS_FAILED);
17225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->CBInfo.pClientNdefNtfRespCb = NULL;
17235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->CBInfo.pClientNdefNtfRespCntx = NULL;
17245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        return;
17255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
17265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
17275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    pNdefRecord=NdefInfo.pNdefRecord;
17285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /*If phLibNfc_Ndef_SearchNdefContent Reg type is NULL return all the Records*/
17295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(gpphLibContext->ndef_cntx.pNdef_NtfSrch_Type==NULL)
17305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
17315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = phFriNfc_NdefRecord_GetRecords(
17325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer,
17335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        gpphLibContext->phLib_NdefRecCntx.ndef_message.length,
17345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        gpphLibContext->phLib_NdefRecCntx.RawRecords,
17355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        gpphLibContext->phLib_NdefRecCntx.IsChunked,
17365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        &(gpphLibContext->phLib_NdefRecCntx.NumberOfRawRecords));
17375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
17385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        for (Index = 0; Index < gpphLibContext->phLib_NdefRecCntx.NumberOfRawRecords; Index++)
17395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
17405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal = phFriNfc_NdefRecord_Parse(
17415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        pNdefRecord,
17425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        gpphLibContext->phLib_NdefRecCntx.RawRecords[Index]);
17435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pNdefRecord++;
17445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefInfo.NdefRecordCount++;
17455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
17465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
17475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
17485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
17495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
17505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Look for registerd TNF */
17515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = phFriNfc_NdefReg_DispatchPacket(
17525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    &(gpphLibContext->phLib_NdefRecCntx.NdefReg),
17535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer,
17545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    (uint16_t)gpphLibContext->phLib_NdefRecCntx.ndef_message.length);
17555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(NFCSTATUS_SUCCESS != RetVal)
17565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
17575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /*phFriNfc_NdefReg_DispatchPacket is failed call upper layer*/
17585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->CBInfo.pClientNdefNtfRespCb(gpphLibContext->CBInfo.pClientNdefNtfRespCntx,
17595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                    NULL,gpphLibContext->Connected_handle,NFCSTATUS_FAILED);
17605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->CBInfo.pClientNdefNtfRespCb = NULL;
17615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->CBInfo.pClientNdefNtfRespCntx = NULL;
17625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            return;
17635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
17645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
17655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        while(1 != RegStatus)
17665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
17675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Process the NDEF records, If match FOUND we will get Call back*/
17685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RegStatus = phFriNfc_NdefReg_Process(   &(gpphLibContext->phLib_NdefRecCntx.NdefReg),
17695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                &RegPrSt);
17705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(RegPrSt == TRUE)
17715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
17725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /*  Processing Done */
17735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                break;
17745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
17755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /*If match found the CbParam will be updated by lower layer, copy the record info*/
17765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            for(Index=0;Index<gpphLibContext->phLib_NdefRecCntx.CbParam.Count;Index++)
17775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
17785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pNdefRecord->Tnf  = gpphLibContext->phLib_NdefRecCntx.CbParam.Records[Index].Tnf;
17795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pNdefRecord->TypeLength  =
17805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    gpphLibContext->phLib_NdefRecCntx.CbParam.Records[Index].TypeLength;
17815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pNdefRecord->PayloadLength  =
17825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    gpphLibContext->phLib_NdefRecCntx.CbParam.Records[Index].PayloadLength;
17835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pNdefRecord->IdLength  =
17845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    gpphLibContext->phLib_NdefRecCntx.CbParam.Records[Index].IdLength;
17855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pNdefRecord->Flags =
17865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    gpphLibContext->phLib_NdefRecCntx.CbParam.Records[Index].Flags;
17875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
17885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pNdefRecord->Id = phOsalNfc_GetMemory(pNdefRecord->IdLength);
17895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pNdefRecord->Type = phOsalNfc_GetMemory(pNdefRecord->TypeLength);
17905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pNdefRecord->PayloadData = phOsalNfc_GetMemory(pNdefRecord->PayloadLength);
17915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
17925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (void)memcpy(pNdefRecord->Id,
17935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    gpphLibContext->phLib_NdefRecCntx.CbParam.Records[Index].Id,
17945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    pNdefRecord->IdLength);
17955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (void)memcpy(pNdefRecord->PayloadData,
17965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    gpphLibContext->phLib_NdefRecCntx.CbParam.Records[Index].PayloadData,
17975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    pNdefRecord->PayloadLength);
17985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (void)memcpy(pNdefRecord->Type,
17995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    gpphLibContext->phLib_NdefRecCntx.CbParam.Records[Index].Type,
18005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    pNdefRecord->TypeLength);
18015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
18025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pNdefRecord++;
18035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefInfo.NdefRecordCount++;
18045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
18055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
18065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
18075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* If no record found call upper layer with failed status*/
18085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(pNdefRecord == NdefInfo.pNdefRecord)
18095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
18105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefInfo.NdefRecordCount =0;
18115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->CBInfo.pClientNdefNtfRespCb(
18125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    gpphLibContext->CBInfo.pClientNdefNtfRespCntx,
18135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    &NdefInfo,gpphLibContext->Connected_handle,
18145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    NFCSTATUS_SUCCESS);
18155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
18165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
18175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
18185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
18195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*Call upperlayer Call back with match records*/
18205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
18215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->CBInfo.pClientNdefNtfRespCb(
18225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    gpphLibContext->CBInfo.pClientNdefNtfRespCntx,
18235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    &NdefInfo,gpphLibContext->Connected_handle,
18245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    NFCSTATUS_SUCCESS);
18255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*Remove entry from FRI*/
18265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = phFriNfc_NdefReg_RmCb(
18275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    &(gpphLibContext->phLib_NdefRecCntx.NdefReg),
18285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    gpphLibContext->phLib_NdefRecCntx.NdefCb );
18295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*Free the memory*/
18305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(gpphLibContext->ndef_cntx.pNdef_NtfSrch_Type!=NULL)
18315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
18325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pNdefRecord=NdefInfo.pNdefRecord;
18335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            for(Index=0;Index<gpphLibContext->phLib_NdefRecCntx.CbParam.Count;Index++)
18345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
18355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                phOsalNfc_FreeMemory(pNdefRecord->Id);
18365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                phOsalNfc_FreeMemory(pNdefRecord->PayloadData);
18375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                phOsalNfc_FreeMemory(pNdefRecord->Type);
18385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                pNdefRecord++;
18395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
18405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
18415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
18425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
18435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    gpphLibContext->CBInfo.pClientNdefNtfRespCb = NULL;
18445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    gpphLibContext->CBInfo.pClientNdefNtfRespCntx = NULL;
18455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
18465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
18475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
18485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC
18495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_Ndef_Rtd_Cb( void *CallBackParam)
18505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
18515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /*There will be single call back given to all match
18525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      It's processed in phLibNfc_Ndef_SrchNdefCnt_Cb*/
18535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    PHNFC_UNUSED_VARIABLE(CallBackParam);
18545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
18555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
18565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phLibNfc_Ndef_SearchNdefContent(
18575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                phLibNfc_Handle                 hRemoteDevice,
18585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                phLibNfc_Ndef_SrchType_t*       psSrchTypeList,
18595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                uint8_t                         uNoSrchRecords,
18605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                pphLibNfc_Ndef_Search_RspCb_t   pNdefNtfRspCb,
18615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                void *                          pContext
18625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                )
18635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
18645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
18655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     NFCSTATUS  RetVal =NFCSTATUS_SUCCESS;
18665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     uint32_t Index=0;
18675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     uint8_t     cr_index = 0;
18685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
18695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
18705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      if((NULL == gpphLibContext) ||
18715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (gpphLibContext->LibNfcState.cur_state
18725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            == eLibNfcHalStateShutdown))
18735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
18745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         RetVal = NFCSTATUS_NOT_INITIALISED;
18755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }
18765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     /* Check the state for DeInit is called or not,if yes return NFCSTATUS_SHUTDOWN*/
18775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      else if(gpphLibContext->LibNfcState.next_state
18785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            == eLibNfcHalStateShutdown)
18795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     {
18805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal= NFCSTATUS_SHUTDOWN;
18815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     }
18825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     else if( (NULL == pNdefNtfRspCb) ||
18835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (NULL == pContext ) ||
18845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (0 == hRemoteDevice))
18855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     {
18865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal= NFCSTATUS_INVALID_PARAMETER;
18875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     }
18885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     else if( (NULL != psSrchTypeList) && (0==uNoSrchRecords))
18895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     {
18905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal= NFCSTATUS_INVALID_PARAMETER;
18915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     }
18925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     else if(0 == gpphLibContext->Connected_handle)
18935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     {   /*presently no target or tag is connected*/
18945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal=NFCSTATUS_TARGET_NOT_CONNECTED;
18955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     }
18965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     else if(hRemoteDevice != gpphLibContext->Connected_handle)
18975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     {   /*This handle of the device sent by application is not connected */
18985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal=NFCSTATUS_INVALID_HANDLE;
18995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     }
19005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     else if((TRUE == gpphLibContext->status.GenCb_pending_status)
19015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        ||(NULL!=gpphLibContext->CBInfo.pClientNdefNtfRespCb))
19025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     {
19035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*Previous callback is pending*/
19045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_REJECTED;
19055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     }
19065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     else
19075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     {
19085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.pNdef_NtfSrch_Type = psSrchTypeList;
19095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
19105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(psSrchTypeList!=NULL)
19115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
19125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /*Maximum records supported*/
19135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->phLib_NdefRecCntx.NumberOfRecords = 255;
19145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /*Reset the FRI component to add the Reg type*/
19155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal = phFriNfc_NdefReg_Reset(
19165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            &(gpphLibContext->phLib_NdefRecCntx.NdefReg),
19175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->phLib_NdefRecCntx.NdefTypes_array,
19185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            &(gpphLibContext->phLib_NdefRecCntx.RecordsExtracted),
19195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            &(gpphLibContext->phLib_NdefRecCntx.CbParam),
19205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->phLib_NdefRecCntx.ChunkedRecordsarray,
19215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            gpphLibContext->phLib_NdefRecCntx.NumberOfRecords);
19225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
19235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->phLib_NdefRecCntx.NdefCb = phOsalNfc_GetMemory(sizeof(phFriNfc_NdefReg_Cb_t));
19245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(gpphLibContext->phLib_NdefRecCntx.NdefCb==NULL)
19255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
19265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /*exception: Not enough memory*/
19275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,1);
19285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
19295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->phLib_NdefRecCntx.NdefCb->NdefCallback = phLibNfc_Ndef_Rtd_Cb;
19305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /*Copy the TNF types to search in global structure*/
19315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->phLib_NdefRecCntx.NdefCb->NumberOfRTDs = uNoSrchRecords;
19325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            for(Index=0;Index<uNoSrchRecords;Index++)
19335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
19345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->phLib_NdefRecCntx.NdefCb->NdefType[Index] = psSrchTypeList->Type;
19355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->phLib_NdefRecCntx.NdefCb->Tnf[Index] = psSrchTypeList->Tnf ;
19365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->phLib_NdefRecCntx.NdefCb->NdeftypeLength[Index] = psSrchTypeList->TypeLength;
19375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                psSrchTypeList++;
19385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
19395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Add the TNF type to FRI component*/
19405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
19415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal = phFriNfc_NdefReg_AddCb(&(gpphLibContext->phLib_NdefRecCntx.NdefReg),
19425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                gpphLibContext->phLib_NdefRecCntx.NdefCb );
19435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
19445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
19455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer =
19465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            phOsalNfc_GetMemory(gpphLibContext->ndef_cntx.NdefActualSize);
19475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->phLib_NdefRecCntx.ndef_message.length =
19485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->ndef_cntx.NdefActualSize;
19495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*Set Complete routine for NDEF Read*/
19505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        for (cr_index = 0; cr_index < PH_FRINFC_NDEFMAP_CR; cr_index++)
19515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
19525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal= phFriNfc_NdefMap_SetCompletionRoutine(
19535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                gpphLibContext->ndef_cntx.psNdefMap,
19545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                cr_index,
19555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                phLibNfc_Ndef_SrchNdefCnt_Cb,
19565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                (void *)gpphLibContext);
19575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
19585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
19595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->ndef_cntx.NdefContinueRead = PH_FRINFC_NDEFMAP_SEEK_BEGIN;
19605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* call below layer Ndef Read*/
19615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = phFriNfc_NdefMap_RdNdef(gpphLibContext->ndef_cntx.psNdefMap,
19625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer,
19635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        (uint32_t*)&gpphLibContext->phLib_NdefRecCntx.ndef_message.length,
19645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        PH_FRINFC_NDEFMAP_SEEK_BEGIN);
19655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
19665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(NFCSTATUS_PENDING == RetVal)
19675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
19685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->CBInfo.pClientNdefNtfRespCb = pNdefNtfRspCb;
19695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->CBInfo.pClientNdefNtfRespCntx = pContext;
19705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->status.GenCb_pending_status=TRUE;
19715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;
19725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
19735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else if (NFCSTATUS_SUCCESS == RetVal)
19745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
19755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal= NFCSTATUS_SUCCESS;
19765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
19775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
19785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
19795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /*Ndef read failed*/
19805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal = NFCSTATUS_FAILED;
19815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
19825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
19835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return RetVal;
19845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
19855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
19865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1987