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_discovery.c
195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * Project: NFC FRI 1.1
215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * $Date: Mon Mar  1 19:02:41 2010 $
235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * $Author: ing07385 $
245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * $Revision: 1.36 $
255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * $Aliases: NFC_FRI1.1_WK1008_SDK,NFC_FRI1.1_WK1007_R33_4,NFC_FRI1.1_WK1007_SDK,NFC_FRI1.1_WK1014_SDK,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1019_SDK,NFC_FRI1.1_WK1024_SDK $
265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*
305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly************************* Header Files ****************************************
315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phLibNfcStatus.h>
345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phLibNfc.h>
355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phHal4Nfc.h>
365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phOsalNfc.h>
375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phLibNfc_Internal.h>
385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phLibNfc_ndef_raw.h>
395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phLibNfc_initiator.h>
405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phLibNfc_discovery.h>
415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*
435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*************************** Macro's  ****************************************
445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#ifndef STATIC_DISABLE
475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#define STATIC static
485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#else
495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#define STATIC
505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#endif
515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*
535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*************************** Global Variables **********************************
545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*
595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*************************** Static Function Declaration ***********************
605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*Remote device Presence check callback*/
645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC void phLibNfc_RemoteDev_CheckPresence_Cb(void  *context,
655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                NFCSTATUS status);
665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**Used for presence chk incase of mifare std tags*/
685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC void phLibNfc_ChkPresence_Trcv_Cb(
695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            void *context,
705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            phHal_sRemoteDevInformation_t *psRemoteDevInfo,
715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            phNfc_sData_t *response,
725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            NFCSTATUS status
735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            );
745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*
765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*************************** Function Definitions ******************************
775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_config_discovery_cb(void     *context,
795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                  NFCSTATUS status)
805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if((phLibNfc_LibContext_t *)context == gpphLibContext)
835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {   /*check for same context*/
845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /*If shutdown called in between allow shutdown to happen*/
885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            phLibNfc_Pending_Shutdown();
895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            status = NFCSTATUS_SHUTDOWN;
905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->status.GenCb_pending_status = FALSE;
945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->status.DiscEnbl_status = FALSE;
955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            phLibNfc_UpdateCurState(status,gpphLibContext);
965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#ifdef RESTART_CFG
975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(gpphLibContext->status.Discovery_pending_status == TRUE)
985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NFCSTATUS RetStatus = NFCSTATUS_FAILED;
1005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* Application has called discovery before receiving this callback,
1015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                so NO notification to the upper layer, instead lower layer
1025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                discovery is called */
1035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->status.Discovery_pending_status = FALSE;
1045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                RetStatus =  phHal4Nfc_ConfigureDiscovery(
1055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        gpphLibContext->psHwReference,
1065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        gpphLibContext->eLibNfcCfgMode,
1075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        &gpphLibContext->sADDconfig,
1085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        (pphLibNfc_RspCb_t)
1095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        phLibNfc_config_discovery_cb,
1105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        (void *)gpphLibContext);
1115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                if (NFCSTATUS_PENDING == RetStatus)
1125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
1135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    (void)phLibNfc_UpdateNextState(gpphLibContext,
1145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            eLibNfcHalStateConfigReady);
1155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    gpphLibContext->status.GenCb_pending_status = TRUE;
1165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    gpphLibContext->status.DiscEnbl_status = TRUE;
1175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                }
1185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                else
1195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
1205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    status = NFCSTATUS_FAILED;
1215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                }
1225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
1235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#endif /* #ifdef RESTART_CFG */
1245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
1255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    } /*End of if-context check*/
1265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
1275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {   /*exception: wrong context pointer returned*/
1285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
1295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        status = NFCSTATUS_FAILED;
1305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
1315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(gpphLibContext->CBInfo.pClientDisConfigCb!=NULL)
1325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
1335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->CBInfo.pClientDisConfigCb(gpphLibContext->CBInfo.pClientDisCfgCntx,status);
1345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->CBInfo.pClientDisConfigCb=NULL;
1355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
1365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return;
1375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
1385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
1395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* Configure Discovery Modes.
1405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function is used to configure ,start and stop the discovery wheel.
1415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
1425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phLibNfc_Mgt_ConfigureDiscovery (
1435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        phLibNfc_eDiscoveryConfigMode_t DiscoveryMode,
1445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        phLibNfc_sADD_Cfg_t             sADDSetup,
1455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        pphLibNfc_RspCb_t               pConfigDiscovery_RspCb,
1465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        void*                           pContext
1475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        )
1485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly {
1495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS RetVal = NFCSTATUS_FAILED;
1505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phHal_sADD_Cfg_t           *psADDConfig;
1515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    psADDConfig = (phHal_sADD_Cfg_t *)&(sADDSetup);
1525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if((NULL == gpphLibContext) ||
1555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (gpphLibContext->LibNfcState.cur_state
1565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            == eLibNfcHalStateShutdown))
1575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
1585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*Lib Nfc not initialized*/
1595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_NOT_INITIALISED;
1605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
1615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Check for Valid parameters*/
1625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if((NULL == pContext) || (NULL == pConfigDiscovery_RspCb))
1635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
1645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal= NFCSTATUS_INVALID_PARAMETER;
1655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
1665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(gpphLibContext->LibNfcState.next_state
1675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            == eLibNfcHalStateShutdown)
1685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
1695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal= NFCSTATUS_SHUTDOWN;
1705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
1715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
1725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
1735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->eLibNfcCfgMode =DiscoveryMode;
1745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        gpphLibContext->sADDconfig = sADDSetup;
1755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(gpphLibContext->status.DiscEnbl_status != TRUE)
1765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
1775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* call lower layer config API for the discovery
1795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            configuration sent by the application */
1805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal = phHal4Nfc_ConfigureDiscovery ( gpphLibContext->psHwReference,
1815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        DiscoveryMode,
1825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        psADDConfig,
1835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        (pphLibNfc_RspCb_t)
1845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phLibNfc_config_discovery_cb,
1855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        (void*)gpphLibContext);
1865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(PHNFCSTATUS(RetVal) == NFCSTATUS_PENDING)
1875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
1885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->status.DiscEnbl_status = TRUE;
1895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* Copy discovery callback and its context */
1905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->CBInfo.pClientDisConfigCb = pConfigDiscovery_RspCb;
1915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->CBInfo.pClientDisCfgCntx = pContext;
1925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                gpphLibContext->status.GenCb_pending_status = TRUE;
1935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly				gpphLibContext->LibNfcState.next_state = eLibNfcHalStateConfigReady;
1945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
1955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            else
1965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
1975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                RetVal=NFCSTATUS_FAILED;
1985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
1995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
2015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
2025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
2035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal=NFCSTATUS_BUSY;
2045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
2055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
2065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return RetVal;
2075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly }
2085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
2105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* Check for target presence.
2115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* Checks given target is present in RF filed or not
2125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
2135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phLibNfc_RemoteDev_CheckPresence( phLibNfc_Handle     hTargetDev,
2145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            pphLibNfc_RspCb_t   pPresenceChk_RspCb,
2155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            void*               pRspCbCtx
2165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                           )
2175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
2185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS RetVal = NFCSTATUS_FAILED;
2195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phHal_sRemoteDevInformation_t *ps_rem_dev_info = NULL;
2205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Check for valid sate */
2215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if((NULL == gpphLibContext) ||
2225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (gpphLibContext->LibNfcState.cur_state
2235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            == eLibNfcHalStateShutdown))
2245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
2255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_NOT_INITIALISED;
2265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
2275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Check for valid parameters*/
2285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if((NULL == pRspCbCtx) || (NULL == pPresenceChk_RspCb)
2295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        || (hTargetDev == 0) )
2305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
2315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal= NFCSTATUS_INVALID_PARAMETER;
2325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
2335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Check for DeInit call*/
2345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(gpphLibContext->LibNfcState.next_state
2355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            == eLibNfcHalStateShutdown)
2365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
2375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_SHUTDOWN;
2385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
2395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Check target is connected or not */
2405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if( gpphLibContext->Connected_handle == 0)
2415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
2425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_TARGET_NOT_CONNECTED;
2435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
2445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Check given handle is valid or not*/
2455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(hTargetDev != gpphLibContext->Connected_handle)
2465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
2475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        RetVal = NFCSTATUS_INVALID_HANDLE;
2485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
249143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas#ifdef LLCP_TRANSACT_CHANGES
250143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas    else if ((LLCP_STATE_RESET_INIT != gpphLibContext->llcp_cntx.sLlcpContext.state)
251143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas            && (LLCP_STATE_CHECKED != gpphLibContext->llcp_cntx.sLlcpContext.state))
252143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas    {
253143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas        RetVal= NFCSTATUS_BUSY;
254143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas    }
255143acf0b768ce3c2f61de2add5ca339479defd57daniel_Tomas#endif /* #ifdef LLCP_TRANSACT_CHANGES */
2565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
2575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
2585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        ps_rem_dev_info = (phHal_sRemoteDevInformation_t *)
2595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    gpphLibContext->Connected_handle;
2605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if((phHal_eMifare_PICC == ps_rem_dev_info->RemDevType)
2615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            &&(0 != ps_rem_dev_info->RemoteDevInfo.Iso14443A_Info.Sak)
2625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            &&(TRUE == gpphLibContext->LastTrancvSuccess))
2635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
2645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Call HAL4 API */
2655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal =  phHal4Nfc_Transceive(
2665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                gpphLibContext->psHwReference,
2675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                gpphLibContext->psBufferedAuth,
2685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                (phHal_sRemoteDevInformation_t *)
2695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    gpphLibContext->Connected_handle,
2705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                (pphHal4Nfc_TransceiveCallback_t )
2715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    phLibNfc_ChkPresence_Trcv_Cb,
2725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                (void *)gpphLibContext
2735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                );
2745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
2765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
2775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
2785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Call lower layer PresenceCheck function */
2795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            RetVal = phHal4Nfc_PresenceCheck(gpphLibContext->psHwReference,
2805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phLibNfc_RemoteDev_CheckPresence_Cb,
2815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        (void *)gpphLibContext);
2825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
2835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if( NFCSTATUS_PENDING == PHNFCSTATUS(RetVal))
2845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
2855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->CBInfo.pClientPresChkCb = pPresenceChk_RspCb;
2865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->CBInfo.pClientPresChkCntx = pRspCbCtx;
2875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Mark General callback pending status as TRUE*/
2885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            gpphLibContext->status.GenCb_pending_status = TRUE;
2895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Update the state machine*/
2915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly			gpphLibContext->LibNfcState.next_state = eLibNfcHalStatePresenceChk;
2925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
2935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else /* If return value is internal error(other than pending ) return NFCSTATUS_FAILED*/
2945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
2955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly          RetVal = NFCSTATUS_FAILED;
2965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
2975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
2985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return RetVal;
2995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
3005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
3025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* Response Callback for Remote device Presence Check.
3035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
3045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC
3055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phLibNfc_RemoteDev_CheckPresence_Cb(void     *context,
3065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        NFCSTATUS status)
3075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
3085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    void                    *pUpperLayerContext=NULL;
3095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    pphLibNfc_RspCb_t       pClientCb=NULL;
3105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /*check valid context is returned or not*/
3125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if((phLibNfc_LibContext_t *)context != gpphLibContext)
3135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
3145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /*exception: wrong context pointer returned*/
3155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
3165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
3175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Mark general callback pending status as FALSE*/
3185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    gpphLibContext->status.GenCb_pending_status = FALSE;
3195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    pClientCb =gpphLibContext->CBInfo.pClientPresChkCb ;
3205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    pUpperLayerContext = gpphLibContext->CBInfo.pClientPresChkCntx;
3215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    gpphLibContext->CBInfo.pClientPresChkCntx = NULL;
3225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    gpphLibContext->CBInfo.pClientPresChkCb =NULL;
3235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Check DeInit call is called, if yes call pending
3245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    shutdown and return NFCSTATUS_SHUTDOWN */
3255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
3265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
3275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phLibNfc_Pending_Shutdown();
3285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        status = NFCSTATUS_SHUTDOWN;
3295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
3305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
3315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
3325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if (status != NFCSTATUS_SUCCESS)
3335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
3345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly           /*If status is other than SUCCESS (Internal error) return
3355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly           NFCSTATUS_TARGET_LOST */
3365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            status= NFCSTATUS_TARGET_LOST;
3375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
3385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
3395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
3405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            status = NFCSTATUS_SUCCESS;
3415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
3425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
3435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     /* Update the current state */
3445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phLibNfc_UpdateCurState(status,gpphLibContext);
3455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(NULL != pClientCb)
3465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
3475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* call the upper layer callback */
3485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        pClientCb(pUpperLayerContext,status);
3495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
3505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return;
3515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
3525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**Used for presence chk incase of mifare std tags*/
3545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellySTATIC void phLibNfc_ChkPresence_Trcv_Cb(
3555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            void *context,
3565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            phHal_sRemoteDevInformation_t *psRemoteDevInfo,
3575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            phNfc_sData_t *response,
3585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            NFCSTATUS status
3595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            )
3605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
3615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    PHNFC_UNUSED_VARIABLE(psRemoteDevInfo);
3625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    PHNFC_UNUSED_VARIABLE(response);
3635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phLibNfc_RemoteDev_CheckPresence_Cb(context,status);
3645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return;
3655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
3665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
369