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