/**
 * \ingroup grp_nfc_dal
 *
 * \brief DAL message control function
 * This function destroys the message queue. The cmd and buf parameters are ignored,
 * this is just to keep the same api as Linux queue.
 *
 * \param[in]       msqid     The handle of the message queue.
 *
 * \retval 0                                    If success.
 * \retval -1                                   Bad passed parameter
 */
int phDal4Nfc_msgctl ( int msqid, int cmd, void *buf )
{
   phDal4Nfc_message_queue_t * pQueue;
   phDal4Nfc_message_queue_item_t * p;

   if (msqid == 0)
      return -1;

   pQueue = (phDal4Nfc_message_queue_t *)msqid;
   pthread_mutex_lock(&pQueue->nCriticalSectionMutex);
   if (pQueue->pItems != NULL)
   {
      p = pQueue->pItems;
      while(p->pNext != NULL) { p = p->pNext; }
      while(p->pPrev != NULL)
      {
         p = p->pPrev;
         phOsalNfc_FreeMemory(p->pNext);
         p->pNext = NULL;
      }
      phOsalNfc_FreeMemory(p);
   }
   pQueue->pItems = NULL;
   pthread_mutex_unlock(&pQueue->nCriticalSectionMutex);
   pthread_mutex_destroy(&pQueue->nCriticalSectionMutex);
   phOsalNfc_FreeMemory(pQueue);
   return 0;
}
void phOsalNfc_Timer_DeferredCall(void *params)
{
   phOsalNfc_Timer_Msg_t *timer_msg;

   if(params == NULL)
      return;

   timer_msg = (phOsalNfc_Timer_Msg_t *)params;

   if((timer_msg != NULL) && (timer_msg->pCallBck != NULL))
      timer_msg->pCallBck(timer_msg->TimerId, timer_msg->pContext);

   if ((timer_msg->TimerId >= MAX_NO_TIMERS) || (timer_msg->TimerId < 0))
   {
      printf("Bad TimerId=%d, should be <= to %d\n", timer_msg->TimerId, MAX_NO_TIMERS);
   }
   else
   {
      if(timers[timer_msg->TimerId].ptr != NULL)
      {
         phOsalNfc_FreeMemory(timers[timer_msg->TimerId].ptr);
         timers[timer_msg->TimerId].ptr = NULL;
      }
   }
   phOsalNfc_FreeMemory(timer_msg);
}
void phLibNfc_P2pConfigParamsCb(void* pContext,NFCSTATUS status,void* pInfo)
{
    pphNciNfc_RfDiscConfigParams_t pRfDiscConfParam = (pphNciNfc_RfDiscConfigParams_t) pContext;
    NFCSTATUS wStatus = status;
    pphLibNfc_LibContext_t pLibContext = gpphLibNfc_Context;
    phLibNfc_Event_t TrigEvent = phLibNfc_EventReqCompleted;

    PH_LOG_LIBNFC_FUNC_ENTRY();
    if(NULL != pLibContext)
    {
        if(NULL != pRfDiscConfParam)
        {
            phOsalNfc_FreeMemory(pRfDiscConfParam);
        }

        (void)phLibNfc_StateHandler(pLibContext, TrigEvent, pInfo, NULL, NULL);
        if(NULL != pLibContext->CBInfo.pClientNfcIpCfgCb)
        {
            if(NFCSTATUS_SUCCESS != wStatus)
            {
                wStatus = NFCSTATUS_FAILED;
            }

            pLibContext->CBInfo.pClientNfcIpCfgCb(pLibContext->CBInfo.pClientNfcIpCfgCntx,
                                        wStatus);
         }
        pLibContext->CBInfo.pClientNfcIpCfgCb = NULL;
        pLibContext->CBInfo.pClientNfcIpCfgCntx = NULL;
    }
    PH_LOG_LIBNFC_FUNC_EXIT();
}
static void phLibNfc_DeferredReceiveCb(void *pContext)
{
    phNfc_sData_t tRecvBufferInfo;
    pphLibNfc_Receive_RspCb_t pUpperLayerCb = NULL;
    phLibNfc_LibContext_t *pLibContext = (phLibNfc_LibContext_t *)pContext;
    void *pUpperLayerCtx = NULL;
    phLibNfc_Event_t TrigEvent = phLibNfc_EventReqCompleted;
    PH_LOG_LIBNFC_FUNC_ENTRY();
    if(NULL != pLibContext)
    {
        pUpperLayerCb = pLibContext->CBInfo.pClientNfcIpRxCb;
        pUpperLayerCtx = pLibContext->CBInfo.pClientNfcIpRxCntx;
        pLibContext->CBInfo.pClientNfcIpRxCb = NULL;
        pLibContext->CBInfo.pClientNfcIpRxCntx = NULL;
        (void)phLibNfc_StateHandler(pLibContext, TrigEvent, NULL, NULL, NULL);
        if(NULL != pUpperLayerCb)
        {
            tRecvBufferInfo.buffer = pLibContext->HCE_FirstBuffer.buffer;
            tRecvBufferInfo.length = pLibContext->HCE_FirstBuffer.length;
            pUpperLayerCb(pUpperLayerCtx,&tRecvBufferInfo,NFCSTATUS_SUCCESS);
        }
        if(pLibContext->HCE_FirstBuffer.buffer )
        {
            phOsalNfc_FreeMemory(pLibContext->HCE_FirstBuffer.buffer);
            pLibContext->HCE_FirstBuffer.buffer = NULL;
            pLibContext->HCE_FirstBuffer.length = 0;
        }
    }
    PH_LOG_LIBNFC_FUNC_EXIT();
    return;
}
Exemple #5
0
NFCSTATUS phOsalNfc_ReceiveMsg(uint32_t *pSourceID, phOsalNfc_Message_t* pMsg)

{
    void * mms;
    MSG         msg;
    uint16_t     ret;

    if(NULL    ==  pMsg ||  NULL    ==  pSourceID)
        return PHNFCSTVAL(CID_NFC_NONE, NFCSTATUS_INVALID_PARAMETER);

    ret = (uint16_t)GetMessage(   &msg,
                                  NULL,
                                  PH_OSALNFC_MESSAGE_BASE,
                                  PH_OSALNFC_MESSAGE_BASE);

    *pSourceID  =   0;
    (void)memset(pMsg, 0x0, sizeof(*pMsg));

    if (ret == 0   ||  NULL    ==  (void*)msg.wParam)
    {
        return PHNFCSTVAL(CID_NFC_NONE, NFCSTATUS_INSUFFICIENT_RESOURCES);
    }

    *pSourceID  =   (uint32_t)(msg.lParam);
    *pMsg    =   *(phOsalNfc_Message_t *)msg.wParam;

    phOsalNfc_FreeMemory((void*)msg.wParam);

    return NFCSTATUS_SUCCESS;
}
static NFCSTATUS phNciNfc_SeSendCb(void* pContext, void *pInfo, NFCSTATUS wStatus)
{
    NFCSTATUS wStat = wStatus;
    pphNciNfc_Context_t pNciContext = pContext;

    UNUSED(pInfo);
    PH_LOG_NCI_FUNC_ENTRY();
    if(NULL != pNciContext)
    {
        if(NULL != pNciContext->tTranscvCtxt.tSendPld.pBuff)
        {
            PH_LOG_NCI_INFO_STR("De-allocating Send Request Payload Buffer...");
            phOsalNfc_FreeMemory(pNciContext->tTranscvCtxt.tSendPld.pBuff);
            pNciContext->tTranscvCtxt.tSendPld.pBuff = NULL;
            pNciContext->tTranscvCtxt.tSendPld.wLen = 0;
        }
        phNciNfc_Notify(pNciContext, wStatus, NULL);
    }
    else
    {
        PH_LOG_NCI_CRIT_STR("Invalid context received from RecvMgrHdlr!");
    }

    PH_LOG_NCI_FUNC_EXIT();
    return wStat;
}
static NFCSTATUS phNciNfc_SendCb(void* pContext, void *pInfo, NFCSTATUS wStatus)
{
    NFCSTATUS wStat = wStatus;
    pphNciNfc_Context_t pNciContext = pContext;
    UNUSED(pInfo);
    PH_LOG_NCI_FUNC_ENTRY();
    if(NULL != pNciContext)
    {
        /* Opening a dummy read request inorder not to loose data sent from remote device */
        (void )phNciNfc_DummyReadReq((void *)pNciContext);
        if(NULL != pNciContext->IfNtf)
        {
            pNciContext->IfNtf(pNciContext->IfNtfCtx,wStat,NULL);
        }
        if(NULL != pNciContext->tTranscvCtxt.tSendPld.pBuff)
        {
            PH_LOG_NCI_INFO_STR("De-allocating Send Request Payload Buffer...");
            phOsalNfc_FreeMemory(pNciContext->tTranscvCtxt.tSendPld.pBuff);
            pNciContext->tTranscvCtxt.tSendPld.pBuff = NULL;
            pNciContext->tTranscvCtxt.tSendPld.wLen = 0;
        }
    }
    else
    {
        PH_LOG_NCI_CRIT_STR("Invalid context received from RecvMgrHdlr!");
    }

    PH_LOG_NCI_FUNC_EXIT();
    return wStat;
}
/**
* \ingroup grp_fri_nfc
* \brief <b>Close a socket on a LLCP-connectionless device</b>.
*
* This function closes a LLCP socket previously created using phFriNfc_LlcpTransport_Socket.
*
* \param[in]  pLlcpSocket                    A pointer to a phFriNfc_LlcpTransport_Socket_t.

* \retval NFCSTATUS_SUCCESS                  Operation successful.
* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
*                                            could not be properly interpreted.
* \retval NFCSTATUS_FAILED                   Operation failed.
*/
NFCSTATUS phFriNfc_LlcpTransport_Connectionless_Close(phFriNfc_LlcpTransport_Socket_t*   pLlcpSocket)
{
   /* Reset the pointer to the socket closed */
   pLlcpSocket->eSocket_State                      = phFriNfc_LlcpTransportSocket_eSocketDefault;
   pLlcpSocket->eSocket_Type                       = phFriNfc_LlcpTransport_eDefaultType;
   pLlcpSocket->pContext                           = NULL;
   pLlcpSocket->pSocketErrCb                       = NULL;
   pLlcpSocket->socket_sSap                        = PHFRINFC_LLCP_SAP_DEFAULT;
   pLlcpSocket->socket_dSap                        = PHFRINFC_LLCP_SAP_DEFAULT;
   pLlcpSocket->bSocketRecvPending                 = FALSE;
   pLlcpSocket->bSocketSendPending                 = FALSE;
   pLlcpSocket->bSocketListenPending               = FALSE;
   pLlcpSocket->bSocketDiscPending                 = FALSE;
   pLlcpSocket->RemoteBusyConditionInfo            = FALSE;
   pLlcpSocket->ReceiverBusyCondition              = FALSE;
   pLlcpSocket->socket_VS                          = 0;
   pLlcpSocket->socket_VSA                         = 0;
   pLlcpSocket->socket_VR                          = 0;
   pLlcpSocket->socket_VRA                         = 0;

   phFriNfc_LlcpTransport_Connectionless_Abort(pLlcpSocket);

   memset(&pLlcpSocket->sSocketOption, 0x00, sizeof(phFriNfc_LlcpTransport_sSocketOptions_t));

   if (pLlcpSocket->sServiceName.buffer != NULL) {
       phOsalNfc_FreeMemory(pLlcpSocket->sServiceName.buffer);
   }
   pLlcpSocket->sServiceName.buffer = NULL;
   pLlcpSocket->sServiceName.length = 0;

   return NFCSTATUS_SUCCESS;
}
/*!
 * \brief System timer callback.
 *        This callback is called by Linux whenever one the timers expires.  It
 *        calls the corresponding registered callback.
 *
 * \param sv structure storing the expired timer ID.
 */
static void phOsalNfc_Timer_Expired(union sigval sv)
{
   uint32_t timerid = (uint32_t)(sv.sival_int);

   if((timerid < MAX_NO_TIMERS)&&(timers[timerid].nIsStopped == 1))
   {
      //printf("phOsalNfc_Timer_Expired : Expired but already stopped TimerId=%d\n", timerid);
      return;
   }

   if(timerid < MAX_NO_TIMERS)
   {
#ifndef CYCLIC_TIMER
      phOsalNfc_Timer_Stop(timerid);
#else

#endif
#ifdef NXP_MESSAGING
      phOsalNfc_Timer_Msg_t *timer_msg;
      phOsalNfc_DeferedCalldInfo_t *osal_defer_msg;
      phDal4Nfc_Message_Wrapper_t wrapper;

      timer_msg = phOsalNfc_GetMemory(sizeof(phOsalNfc_Timer_Msg_t));
      if(timer_msg == NULL)
      {
         phOsalNfc_RaiseException(phOsalNfc_e_NoMemory, 0);
	  return;
      }

      osal_defer_msg = phOsalNfc_GetMemory(sizeof(phOsalNfc_DeferedCalldInfo_t));
      if(osal_defer_msg == NULL)
      {
         phOsalNfc_FreeMemory(timer_msg);
         phOsalNfc_RaiseException(phOsalNfc_e_NoMemory, 0);
	  return;
      }

      timer_msg->TimerId = timerid;
      timer_msg->pCallBck = timers[timerid].callback;
      timer_msg->pContext = timers[timerid].pContext;

      osal_defer_msg->pCallback = phOsalNfc_Timer_DeferredCall;
      osal_defer_msg->pParameter = timer_msg;

      wrapper.mtype = 1;
      wrapper.msg.eMsgType = PH_OSALNFC_TIMER_MSG;
      wrapper.msg.pMsgData = osal_defer_msg;
      wrapper.msg.Size = sizeof(phOsalNfc_DeferedCalldInfo_t);

      timers[timerid].ptr = osal_defer_msg;
#ifndef G_IDLE_ADD_MSGQ
      phDal4Nfc_msgsnd(nDeferedCallMessageQueueId, (void *)&wrapper, sizeof(phOsalNfc_Message_t), 0);
#else
      PostMessage((void *)&wrapper, sizeof(phOsalNfc_Message_t), 0);
#endif
      //(timers[timerid].callback)(timerid, timers[timerid].pContext);
#endif
   }
}
Exemple #10
0
NFCSTATUS phOsalNfc_PostMsg(uint32_t SourceID, uint32_t DestID, phOsalNfc_Message_t * pMsg)
{

    phOsalNfc_Message_t*				 pMsg_i      = NULL;
    uint16_t									 retvalue=0;
    NFCSTATUS							 Status = NFCSTATUS_SUCCESS;

    if(NULL ==  pMsg)
    {
        Status =PHNFCSTVAL(CID_NFC_NONE, NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {
        pMsg_i    =   (void*) phOsalNfc_GetMemory(sizeof(phOsalNfc_Message_t));
        if(NULL ==  pMsg_i)
        {
            Status=PHNFCSTVAL(CID_NFC_NONE, NFCSTATUS_INSUFFICIENT_RESOURCES);
        }
        else
        {
            *pMsg_i    =   *pMsg;
            retvalue = (uint16_t)PostThreadMessage((DWORD)DestID,
                                                   (UINT)(PH_OSALNFC_MESSAGE_BASE),
                                                   (WPARAM)pMsg_i,
                                                   (LPARAM)SourceID);
            if(0 == retvalue)
            {

                Status= PHNFCSTVAL(CID_NFC_NONE, NFCSTATUS_INSUFFICIENT_RESOURCES);
            }
            else
            {
                Status= NFCSTATUS_SUCCESS;
            }
            phOsalNfc_FreeMemory(pMsg_i);
        }

    }
    return Status;

}
void phNciNfc_ListenMgmt_DeActivate(void* pContext,
                                    pphNciNfc_RemoteDevInformation_t pRemDevInfo)
{
    pphNciNfc_Context_t pNciCtx = (pphNciNfc_Context_t ) pContext;
    PH_LOG_NCI_FUNC_ENTRY();
    if((NULL != pNciCtx) && (NULL != pRemDevInfo))
    {
        switch(pRemDevInfo->eRFTechMode)
        {
            case phNciNfc_NFCA_Listen:
            case phNciNfc_NFCA_Active_Listen:
            case phNciNfc_NFCB_Listen:
            case phNciNfc_NFCF_Listen:
            case phNciNfc_NFCF_Active_Listen:
            {
                if(1 == pNciCtx->tLstnModeRecvInfo.bDataBuffEnable)
                {
                    /* Data has already been received but there is not RemDev_Receive call from upper layer.
                    Just free the temporary buffer and reset the 'bDataBuffEnable' flag */
                    pNciCtx->tLstnModeRecvInfo.bDataBuffEnable = 0;
                    if(NULL != pNciCtx->tLstnModeRecvInfo.pBuff)
                    {
                        phOsalNfc_FreeMemory(pNciCtx->tLstnModeRecvInfo.pBuff);
                        pNciCtx->tLstnModeRecvInfo.pBuff = NULL;
                    }
                    pNciCtx->tLstnModeRecvInfo.wBuffSize = 0;
                    pNciCtx->tLstnModeRecvInfo.wLstnCbStatus = NFCSTATUS_TARGET_LOST;
                }
            }
            break;
            default:
                /* Nothing to be done */
                break;
        }
    }
    PH_LOG_NCI_FUNC_EXIT();
    return ;
}
static NFCSTATUS phLibNfc_HCEQueueDeferredRecv(void *pContext)
{
    pphLibNfc_Context_t pLibContext = (pphLibNfc_Context_t)pContext;
    NFCSTATUS wStatus = NFCSTATUS_SUCCESS;

    pLibContext->HCE_FirstBuf = 0;
    wStatus = phOsalNfc_QueueDeferredCallback(phLibNfc_DeferredReceiveCb, pContext);
    if(NFCSTATUS_SUCCESS == wStatus)
    {
        wStatus = NFCSTATUS_PENDING;
    }
    else
    {
        if (NULL != pLibContext->HCE_FirstBuffer.buffer)
        {
            phOsalNfc_FreeMemory(pLibContext->HCE_FirstBuffer.buffer);
            pLibContext->HCE_FirstBuffer.buffer = NULL;
        }
        pLibContext->HCE_FirstBuffer.length = 0;
    }
    
    return wStatus;
}
/**
 * \ingroup grp_nfc_dal
 *
 * \brief DAL message receive function
 * The call to this function will get the older message from the queue. If the queue is empty the function waits
 * (blocks on a mutex) until a message is posted to the queue with phDal4Nfc_msgsnd.
 * The msgtyp and msgflg parameters are ignored.
 *
 * \param[in]       msqid     The handle of the message queue.
 * \param[out]      msgp      The received message.
 * \param[in]       msgsz     The message size.
 *
 * \retval 0                                    If success.
 * \retval -1                                   Bad passed parameter.
 */
int phDal4Nfc_msgrcv (int msqid, void * msgp, size_t msgsz, long msgtyp, int msgflg)
{
   phDal4Nfc_message_queue_t * pQueue;
   phDal4Nfc_message_queue_item_t * p;

   if ((msqid == 0) || (msgp == NULL))
      return -1;

   if (msgsz != sizeof(phLibNfc_Message_t))
      return -1;

   pQueue = (phDal4Nfc_message_queue_t *)msqid;
   sem_wait(&pQueue->nProcessSemaphore);
   pthread_mutex_lock(&pQueue->nCriticalSectionMutex);
   if (pQueue->pItems != NULL)
   {
      memcpy(&((phDal4Nfc_Message_Wrapper_t*)msgp)->msg, &(pQueue->pItems)->nMsg, sizeof(phLibNfc_Message_t));
      p = pQueue->pItems->pNext;
      phOsalNfc_FreeMemory(pQueue->pItems);
      pQueue->pItems = p;
   }
   pthread_mutex_unlock(&pQueue->nCriticalSectionMutex);
   return 0;
}
NFCSTATUS
phHciNfc_CoreSend(void                            *pHciContextHandle,
                  phHciNfc_SendParams_t           *pSendMsgParams,
                  pphHciNfc_UpperLayerSendCb_t    phHciNfcUpperLayerSendCb,
                  void                            *pContext
                 )
{
    NFCSTATUS wStatus;
    phHciNfc_HciContext_t *pHciContext = (phHciNfc_HciContext_t *) pHciContextHandle;
    uint8_t               *pBuiltHciPkt;
    uint32_t              dwTotalLenOfBuiltPkt=0;
    phNfc_sData_t         tSendData;

    if( (pHciContext == NULL) || (pSendMsgParams == NULL) )
    {
        PH_LOG_LIBNFC_CRIT_STR("Invalid parameter");
        wStatus = NFCSTATUS_INVALID_PARAMETER;
    }else
    {
        pBuiltHciPkt = phOsalNfc_GetMemory(PHHCI_HCP_MAX_PACKET_SIZE);

        if( pBuiltHciPkt == NULL )
        {
            /* Failed to allocate memory for HCI packet */
            wStatus = NFCSTATUS_FAILED;
        }else
        {
            /* Store Send Call back, Uppler Layer Context and received msg params in Hci context */
            phOsalNfc_MemCopy(&(pHciContext->pHciCoreContext.tHciCtxSendMsgParams),pSendMsgParams,sizeof(phHciNfc_SendParams_t));

            /* Check whether Fragmenation is required
            *  If fragmentation is required fragmentation is performed in phHciNfcLowerLayerSendCb callback
            *  which is invoked by NCI after transmission of first packet here
            */
            if( pSendMsgParams->dwLen <= ( PHHCI_HCI_MAX_PACKET_PAYLOAD_SIZE ) )
            {
                wStatus = phHciNfc_HciCoreBuildHcipacket(pSendMsgParams,
                                                         HCP_CHAINBIT_UN_CHAINED,
                                                         pSendMsgParams->dwLen,
                                                         &dwTotalLenOfBuiltPkt,
                                                         pBuiltHciPkt);
                pHciContext->pHciCoreContext.tHciFragInfo.dwTxdHcpPayloadNoOfDataBytes = pSendMsgParams->dwLen;

            }else
            {
                /* The HCI packets needs to be fragmented, send the First HCI packet and start
                *  HCI Fragmenation in the Send Call Back(after sucessfull transmission by lower layer)
                */
                wStatus = phHciNfc_HciCoreBuildHcipacket(pSendMsgParams,
                                                         HCP_CHAINBIT_CHAINED,
                                                         PHHCI_HCI_MAX_PACKET_PAYLOAD_SIZE,
                                                         &dwTotalLenOfBuiltPkt,
                                                         pBuiltHciPkt);
                pHciContext->pHciCoreContext.tHciFragInfo.dwTxdHcpPayloadNoOfDataBytes =
                                                                          PHHCI_HCI_MAX_PACKET_PAYLOAD_SIZE;
            }
            /* Send the HCI packet to lower layer */
            if( wStatus == NFCSTATUS_SUCCESS )
            {
                tSendData.length = dwTotalLenOfBuiltPkt;
                tSendData.buffer = pBuiltHciPkt;
                wStatus = phNciNfc_SeSendData(pHciContext->pNciContext,
                                  pHciContext->pSeHandle,
                                  (pphNciNfc_IfNotificationCb_t)&phHciNfc_HciCoreLowerLayerSendCb,
                                  pHciContext,
                                  &tSendData);
                if(wStatus == NFCSTATUS_PENDING)
                {
                    /* Store Send Call back, Uppler Layer Context in Hci context */
                    pHciContext->pHciCoreContext.phHciNfcCoreUpperLayerSendCb = phHciNfcUpperLayerSendCb;
                    pHciContext->pHciCoreContext.pUpperLayerContext           = pContext;

                    PH_LOG_LIBNFC_CRIT_STR(" HCP Packet Sent to NCI ");
                }
                else
                {
                    PH_LOG_LIBNFC_CRIT_STR(" Failed to send HCP to Lower ");
                }
            }
            else
            {
                PH_LOG_LIBNFC_CRIT_STR(" HCI packet Formation Failed ");
                wStatus = NFCSTATUS_INVALID_PARAMETER;
            }

            phOsalNfc_FreeMemory(pBuiltHciPkt);
        }
    }
    return wStatus;
}
NFCSTATUS
phHciNfc_CoreRecvCB(void                           *pContext,
                    void                           *pInfo,
                    NFCSTATUS                      wStatus
                   )
{
    uint8_t bChainBit = 0;
    pphHciNfc_sCoreRecvBuff_List_t pNode = NULL;
    phHciNfc_ReceiveParams_t tHciNfcRxdParams;
    phNciNfc_TransactInfo_t        *pHciCoreReceiveInfo;
    NFCSTATUS wStat;

    if(pContext != NULL)
    {
        if(pInfo != NULL)
        {
            phHciNfc_HciContext_t      *pHciContext     = (phHciNfc_HciContext_t *) pContext;
            phHciNfc_HciCoreContext_t  *pHciCoreContext = &(pHciContext->pHciCoreContext);
            pHciCoreReceiveInfo                         = (phNciNfc_TransactInfo_t*)pInfo;

            if(wStatus == NFCSTATUS_SUCCESS)
            {
                bChainBit = (uint8_t) GET_BITS8(*(pHciCoreReceiveInfo->pbuffer),
                                                PHHCINFC_HCP_CHAINBIT_OFFSET,
                                                PHHCINFC_HCP_CHAINBIT_LEN);

                /* Store/Buffer HCP Header along with Data irrespective of whether chained or Unchained */
                if(pHciCoreReceiveInfo->wLength <= PHHCI_HCP_MAX_RECEIVE_PACKET_SIZE)
                {
                    /* Create the Data Node and copy the Data */
                    pNode = phHciNfc_CoreGetNewNode(pHciCoreContext,pHciCoreReceiveInfo->wLength);
                    if(pNode != NULL)
                    {
                        phOsalNfc_MemCopy((uint8_t*)&(pNode->tMem.aBuffer[0]),
                                          (uint8_t*)(pHciCoreReceiveInfo->pbuffer),
                                          pHciCoreReceiveInfo->wLength);
                        pNode->tMem.wLen      = pHciCoreReceiveInfo->wLength;
                    }else
                    {
                        PH_LOG_LIBNFC_CRIT_STR(" HCI Core Receive- Failed To Allocate Memory for Node");
                    }
                }else
                {
                    /* HCP Packet should not be bigger than PHHCI_HCP_MAX_PACKET_SIZE */
                }
                /* Check Chaining Bit to asceratain whether complete Message is received or Not*/
                if(bChainBit == HCP_CHAINBIT_UN_CHAINED)
                {
                    /* HCI message is complete extract the Data from linked list*/
                    tHciNfcRxdParams.pData = phOsalNfc_GetMemory(pHciCoreContext->tReceiveInfo.wPayloadSize);
                    wStat = phHciNfc_HciCoreExtractData(pHciCoreContext,&tHciNfcRxdParams);
                    if(wStat == NFCSTATUS_SUCCESS)
                    {
                        /*Once the HCI Packets are Extracted ,HCI packet info is captured so delete the linked list */
                        phHciNfc_CoreDeleteList(pHciCoreContext);
                        /* Send the Received Data To upper layer*/
                        /* TO DO Registration mechanism to be implemented*/
                        phHciNfc_ReceiveHandler(pHciContext,
                                                &tHciNfcRxdParams,
                                                wStatus
                                                );
                        phOsalNfc_FreeMemory(tHciNfcRxdParams.pData);
                    }else
                    {
                            PH_LOG_LIBNFC_CRIT_STR(" HCI Core - HCI Packet Extraction Failed");
                    }
                }
                else
                {
                    /* Chained HCI packets which are already buffered
                    ** No Action here waits for complete HCI Msg to be Rxd
                    */
                }
            }else
            {
                /* Failed Status from lower layer- Call the upper layer call back with the returned status */
                /* Send the Received Data To upper layer*/
                phHciNfc_ReceiveHandler(pHciContext->pHciCoreContext.pUpperLayerContext,
                                        NULL,
                                        wStatus
                                        );
            }
        }
        else
        {
            /* Do Nothing if no information is received from lower layer */
            PH_LOG_LIBNFC_CRIT_STR(" Invalid Receive Info Pointer received from lower layer ");
        }
    }else
    {
        PH_LOG_LIBNFC_CRIT_STR(" Invalid HCI Context received from Lower Layer ");
    }
    return wStatus;

}
NFCSTATUS
phNciNfc_NfcIPollInit(
                          void *psContext,
                          pphNciNfc_RemoteDevInformation_t pRemDevInf,
                          uint8_t *pBuff,
                          uint16_t wLen
                         )
{
    NFCSTATUS                       wStatus = NFCSTATUS_SUCCESS;
    pphNciNfc_Context_t psNciCtxt = (pphNciNfc_Context_t)psContext;

    PH_LOG_NCI_FUNC_ENTRY();

    if((NULL == psNciCtxt) || (NULL == pBuff) || (NULL == pRemDevInf))
    {
        wStatus = NFCSTATUS_INVALID_PARAMETER;
        PH_LOG_NCI_INFO_STR(" Invalid Param(s)..");
    }
    else
    {
        gpphNciNfc_RdrDataXchgSequence = (phNciNfc_SequenceP_t *)phOsalNfc_GetMemory(
        (2 * sizeof(phNciNfc_SequenceP_t)));

        if(NULL != gpphNciNfc_RdrDataXchgSequence)
        {
            /* Extract info into RemDevInf structure */
            wStatus = phNciNfc_UpdateNfcIRemDevInfo(pRemDevInf,pBuff,wLen);

            if(NFCSTATUS_SUCCESS == wStatus)
            {
                /* Update Target specific info */
                switch(pRemDevInf->eRFTechMode)
                {
                    case phNciNfc_NFCA_Poll:
                    case phNciNfc_NFCA_Active_Poll:
                        wStatus = phNciNfc_NfcDepPollRdrAInit(pRemDevInf,pBuff,wLen);
                    break;

                    case phNciNfc_NFCF_Poll:
                    case phNciNfc_NFCF_Active_Poll:
                        wStatus = phNciNfc_NfcDepPollRdrFInit(pRemDevInf,pBuff,wLen);
                    break;

                    default:
                        PH_LOG_NCI_INFO_STR("Rf Technology and mode not supported");
                        wStatus = NFCSTATUS_FAILED;
                    break;
                }
            }

            if(NFCSTATUS_SUCCESS == wStatus)
            {
                (psNciCtxt->tActvDevIf.pDevInfo) = pRemDevInf;
                wStatus = phNciNfc_SetConnCredentials(psNciCtxt);
            }
        }
        else
        {
            wStatus = NFCSTATUS_INSUFFICIENT_RESOURCES;
            PH_LOG_NCI_INFO_STR(" DataXchg SequenceHandler pointer MemAlloc Failed..");
        }
    }

    if(NFCSTATUS_SUCCESS != wStatus)
    {
        if(NULL != gpphNciNfc_RdrDataXchgSequence)
        {
            PH_LOG_NCI_INFO_STR(" Freeing RdrDataXchgSeq Mem..");
            phOsalNfc_FreeMemory(gpphNciNfc_RdrDataXchgSequence);
            gpphNciNfc_RdrDataXchgSequence = NULL;
        }
    }
    PH_LOG_NCI_FUNC_EXIT();

    return wStatus;
}
STATIC  void phLibNfc_Ioctl_Mgmt_CB(void          *context,
                              phNfc_sData_t *pOutData,
                              NFCSTATUS      status )
{
    phLibNfc_Ioctl_Cntx_t *pIoctlCntx=NULL;

    if(PHNFCSTATUS(status)!=NFCSTATUS_SUCCESS)
    {
        status = NFCSTATUS_FAILED;
    }
	if(gpphLibContext!= NULL)
	{
		if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
		{
		    /*If shutdown called in between allow shutdown to happen*/
			phLibNfc_Pending_Shutdown();
			status = NFCSTATUS_SHUTDOWN;
		}
	}
    pIoctlCntx= (phLibNfc_Ioctl_Cntx_t*)context;
    if( pIoctlCntx !=NULL)
    {
        switch(pIoctlCntx->IoctlCode)
        {
            case NFC_FW_DOWNLOAD:
            {
                /*Release the hardware reference memory*/
                phOsalNfc_FreeMemory(pIoctlCntx->psHwReference);
            }break;
            case NFC_MEM_READ:
            {

            }break;
            case NFC_MEM_WRITE:
            {

            }break;

			case PHLIBNFC_ANTENNA_TEST:
            {
            
            }break; 
			case PHLIBNFC_SWP_TEST:
            {
            
            }break;	
			case PHLIBNFC_PRBS_TEST:
            {
            
            }break;
            default:
            {
            }
        }
        pIoctlCntx->CliRspCb(pIoctlCntx->pCliCntx,pOutData,status);
		if(gpphLibContext!= NULL)
		{
			gpphLibContext->status.GenCb_pending_status=FALSE;
		}
    }
}
Exemple #18
0
/*!
 * \brief Receives the HCI Response from the corresponding peripheral device.
 *
 * This function receives the HCI Command Response from the connected NFC
 * Pheripheral device.
 */
static
NFCSTATUS
phHciNfc_Recv_LinkMgmt_Response(
                        void                *psContext,
                        void                *pHwRef,
                        uint8_t             *pResponse,
#ifdef ONE_BYTE_LEN
                        uint8_t             length
#else
                        uint16_t            length
#endif
                    )
{
    phHciNfc_sContext_t         *psHciContext = 
                                    (phHciNfc_sContext_t *)psContext ;
    phHciNfc_LinkMgmt_Info_t    *p_link_mgmt_info=NULL;
    NFCSTATUS                   status = NFCSTATUS_SUCCESS;
    uint8_t                     prev_cmd = ANY_GET_PARAMETER;

    if( (NULL == psHciContext) || (NULL == pHwRef) )
    {
      status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
    }
    else if(  NULL == psHciContext->p_link_mgmt_info )
    {
        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
    }
    else
    {
        p_link_mgmt_info = (phHciNfc_LinkMgmt_Info_t *)
                            psHciContext->p_link_mgmt_info ;
        prev_cmd = p_link_mgmt_info->p_pipe_info->prev_msg ;
        switch(prev_cmd)
        {
            case ANY_GET_PARAMETER:
            {
                status = phHciNfc_LinkMgmt_InfoUpdate(psHciContext,
                            (phHal_sHwReference_t *)pHwRef,
                            p_link_mgmt_info->p_pipe_info->reg_index, 
                            &pResponse[HCP_HEADER_LEN],
                                (uint8_t)(length - HCP_HEADER_LEN));
                break;
            }
            case ANY_SET_PARAMETER:
            {
                status = PHNFCSTVAL(CID_NFC_HCI,
                                    NFCSTATUS_FEATURE_NOT_SUPPORTED);
                break;
            }
            case ANY_OPEN_PIPE:
            {
                break;
            }
            case ANY_CLOSE_PIPE:
            {
                phOsalNfc_FreeMemory(p_link_mgmt_info->p_pipe_info);
                p_link_mgmt_info->p_pipe_info = NULL;
                psHciContext->p_pipe_list[PIPETYPE_STATIC_LINK] = NULL;
                break;
            }
            default:
            {
                status = PHNFCSTVAL(CID_NFC_HCI,
                                        NFCSTATUS_INVALID_HCI_RESPONSE);
                break;
            }
        }
        if( NFCSTATUS_SUCCESS == status )
        {
            if( NULL != p_link_mgmt_info->p_pipe_info)
            {
                p_link_mgmt_info->p_pipe_info->prev_status = NFCSTATUS_SUCCESS;
            }
            p_link_mgmt_info->link_cur_seq = p_link_mgmt_info->link_next_seq;
        }

    }
    return status;
}
/*
 * This function called by the HAL4 when the initialization seq is completed.
 */
STATIC void phLibNfc_InitCb(void *pContext,NFCSTATUS status)
{
    pphLibNfc_LibContext_t   pLibContext=NULL;
    pphLibNfc_RspCb_t          pClientCb=NULL;
    void                        *pUpperLayerContext=NULL;


    /* Initialize the local variable */
    pLibContext  = (pphLibNfc_LibContext_t)pContext;

    pClientCb =pLibContext->CBInfo.pClientInitCb;
    pUpperLayerContext=pLibContext->CBInfo.pClientInitCntx;
    if(status == NFCSTATUS_SUCCESS)
    {
        /* Get the Lib context */
        pLibContext=(pphLibNfc_LibContext_t)gpphLibContext;
        gpphLibContext->sSeContext.eActivatedMode = phLibNfc_SE_ActModeOff;
        if(pLibContext->psHwReference->uicc_connected==TRUE)
        {
            /* populate state of the secured element */
            gpphLibContext->sSeContext.eActivatedMode = phLibNfc_SE_ActModeDefault;
            sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].eSE_CurrentState=phLibNfc_SE_Active;
            pLibContext->sSeContext.uUiccActivate=TRUE;
        }		
        if(pLibContext->psHwReference->smx_connected==TRUE)
        {
            /* populate state of the secured element */
            gpphLibContext->sSeContext.eActivatedMode = phLibNfc_SE_ActModeDefault;
            sSecuredElementInfo[LIBNFC_SE_SMARTMX_INDEX].eSE_CurrentState=phLibNfc_SE_Inactive; 
            pLibContext->sSeContext.uSmxActivate =FALSE;
        }

        phLibNfc_UpdateCurState(status,pLibContext);
        (void)phHal4Nfc_RegisterNotification(                                            
                                pLibContext->psHwReference,
                                eRegisterDefault,
                                phLibNfc_DefaultHandler,
                                (void*)pLibContext
                                ); 
        /* call the upper layer register function */
        (*pClientCb)(pUpperLayerContext,status);

    }
    else
    {
        /*Change the status code failed*/
        status = NFCSTATUS_FAILED;
        /* Get the Lib context */
        pLibContext=(pphLibNfc_LibContext_t)gpphLibContext;

        phLibNfc_UpdateCurState(status,pLibContext);



        /* Allocate the Memory for the Transceive info */
        if(pLibContext->psHwReference!= NULL)
        {
            phOsalNfc_FreeMemory(pLibContext->psHwReference);
            pLibContext->psHwReference = NULL;
        }
        (*pClientCb)(pUpperLayerContext, status);

        phOsalNfc_FreeMemory(pLibContext);
        pLibContext= NULL;
        gpphLibContext = NULL;
        
    }
    return;
}
NFCSTATUS
phLibNfc_Mgt_SetP2P_ConfigParams(phLibNfc_sNfcIPCfg_t *pConfigInfo,
                                 pphLibNfc_RspCb_t pConfigRspCb,
                                 void *  pContext)
{
    NFCSTATUS wStatus = NFCSTATUS_FAILED;
    pphLibNfc_LibContext_t pLibContext = gpphLibNfc_Context;
    pphNciNfc_RfDiscConfigParams_t pRfDiscConfParam = NULL;
    uint8_t bGeneralBytesLength = 0;
    phLibNfc_DummyInfo_t Info;
    phLibNfc_Event_t TrigEvent = phLibNfc_EventDummy;
    PH_LOG_LIBNFC_FUNC_ENTRY();
    Info.Evt = phLibNfc_DummyEventInvalid;
    if(NULL == pLibContext)
    {
        wStatus = NFCSTATUS_NOT_INITIALISED;
    }
    else if((NULL == pConfigInfo) ||(NULL == pConfigRspCb))
    {
        wStatus = NFCSTATUS_INVALID_PARAMETER;
    }
    else
    {
        bGeneralBytesLength = pConfigInfo->generalBytesLength;
        if((pLibContext->bDtaFlag || (0 != bGeneralBytesLength)) &&
           (PH_LIBNFC_INTERNAL_MAX_ATR_LENGTH >= bGeneralBytesLength))
        {
            /* Allocate memory for the input parameter that needs to be sent to
               'phNciNfc_SetConfigRfParameters' API */
            pRfDiscConfParam = (pphNciNfc_RfDiscConfigParams_t)phOsalNfc_GetMemory(
                                                sizeof(phNciNfc_RfDiscConfigParams_t));
            if(NULL != pRfDiscConfParam)
            {
                phOsalNfc_SetMemory(pRfDiscConfParam,0,sizeof(phNciNfc_RfDiscConfigParams_t));

                pLibContext->CBInfo.pClientNfcIpCfgCb = pConfigRspCb;
                pLibContext->CBInfo.pClientNfcIpCfgCntx = pContext;

                /* Set general bytes for Poll  Nfc-Dep parameters */
                switch(pConfigInfo->p2pMode)
                {
                    case NFC_DEP_POLL:
                        pRfDiscConfParam->tConfigInfo.PollNfcDepConfig = 1;
                        /* Configuring general bytes for ATR_REQ */
                        pRfDiscConfParam->tPollNfcDepDiscParams.PollNfcDepConfig.EnableConfig = 0;
                        pRfDiscConfParam->tPollNfcDepDiscParams.PollNfcDepConfig.Config.bSetGenBytes = 1;
                        pRfDiscConfParam->tPollNfcDepDiscParams.bAtrReqGeneBytesSize = bGeneralBytesLength;
                        phOsalNfc_MemCopy(pRfDiscConfParam->tPollNfcDepDiscParams.aAtrReqGenBytes,
                        pConfigInfo->generalBytes,bGeneralBytesLength);
                        /*Enable General Bytes to default values*/
                        /*Configure to default value = 0x30*/
                        pRfDiscConfParam->tPollNfcDepDiscParams.PollNfcDepConfig.Config.bSetAtrConfig = 1;
                        pRfDiscConfParam->tPollNfcDepDiscParams.AtrReqConfig.bDid = 0; /*0 For LLCP*/
                        pRfDiscConfParam->tPollNfcDepDiscParams.AtrReqConfig.bLr = 0x03;/*0x03 for LLCP*/
                    break;
                    case  NFC_DEP_LISTEN:
                        /* Set general bytes for Listen  Nfc-Dep parameters */
                        pRfDiscConfParam->tConfigInfo.LstnNfcDepConfig = 1;
                        /* Configuring general bytes for ATR_RES */
                        pRfDiscConfParam->tLstnNfcDepDiscParams.LstnNfcDepConfig.EnableConfig = 0;
                        pRfDiscConfParam->tLstnNfcDepDiscParams.LstnNfcDepConfig.Config.bSetGenBytes = 1;
                        pRfDiscConfParam->tLstnNfcDepDiscParams.bAtrResGenBytesSize = bGeneralBytesLength;
                        phOsalNfc_MemCopy(pRfDiscConfParam->tLstnNfcDepDiscParams.aAtrResGenBytes,
                        pConfigInfo->generalBytes,bGeneralBytesLength);
                        /*Enable General Bytes in response*/
                        /*Configure to defaule value*/
                        pRfDiscConfParam->tLstnNfcDepDiscParams.LstnNfcDepConfig.Config.bSetAtrRespConfig = 1;
                        pRfDiscConfParam->tLstnNfcDepDiscParams.AtrRespConfig.bLengthReduction = 0x03; /*For LLCP*/
                    break;
                    case NFC_DEP_DEFAULT:
                    default:
                        pRfDiscConfParam->tConfigInfo.PollNfcDepConfig = 1;
                        /* Configuring general bytes for ATR_REQ */
                        pRfDiscConfParam->tPollNfcDepDiscParams.PollNfcDepConfig.EnableConfig = 0;
                        pRfDiscConfParam->tPollNfcDepDiscParams.PollNfcDepConfig.Config.bSetGenBytes = 1;
                        pRfDiscConfParam->tPollNfcDepDiscParams.bAtrReqGeneBytesSize = bGeneralBytesLength;
                        phOsalNfc_MemCopy(pRfDiscConfParam->tPollNfcDepDiscParams.aAtrReqGenBytes,
                                          pConfigInfo->generalBytes,bGeneralBytesLength);
                        /*Enable General Bytes to default values*/
                        /*Configure to default value = 0x30*/
                        pRfDiscConfParam->tPollNfcDepDiscParams.PollNfcDepConfig.Config.bSetAtrConfig = 1;
                        pRfDiscConfParam->tPollNfcDepDiscParams.AtrReqConfig.bDid = 0; /*0 For LLCP*/
                        pRfDiscConfParam->tPollNfcDepDiscParams.AtrReqConfig.bLr = 0x03;/*0x03 for LLCP*/     
                        pRfDiscConfParam->tConfigInfo.LstnNfcDepConfig = 1;
                        /* Configuring general bytes for ATR_RES */
                        pRfDiscConfParam->tLstnNfcDepDiscParams.LstnNfcDepConfig.EnableConfig = 0;
                        pRfDiscConfParam->tLstnNfcDepDiscParams.LstnNfcDepConfig.Config.bSetGenBytes = 1;
                        pRfDiscConfParam->tLstnNfcDepDiscParams.bAtrResGenBytesSize = bGeneralBytesLength;
                        phOsalNfc_MemCopy(pRfDiscConfParam->tLstnNfcDepDiscParams.aAtrResGenBytes,
                                          pConfigInfo->generalBytes,bGeneralBytesLength);
                        /*Enable General Bytes in response*/
                        /*Configure to defaule value*/
                        pRfDiscConfParam->tLstnNfcDepDiscParams.LstnNfcDepConfig.Config.bSetAtrRespConfig = 1;
                        pRfDiscConfParam->tLstnNfcDepDiscParams.AtrRespConfig.bLengthReduction = 0x03; /*For LLCP*/		
                    break;
                }

                Info.Evt = phLibNfc_DummyEventSetP2PConfigs;
                Info.Params = (void *)pRfDiscConfParam;

                wStatus = phLibNfc_StateHandler(pLibContext,
                                                TrigEvent,
                                                NULL,
                                                &Info,
                                                NULL);
                if(NFCSTATUS_PENDING != wStatus)
                {
                    phOsalNfc_FreeMemory(pRfDiscConfParam);
                    pLibContext->CBInfo.pClientNfcIpCfgCb = NULL;
                    pLibContext->CBInfo.pClientNfcIpCfgCntx = NULL;
                }
            }
            else
            {
                wStatus = NFCSTATUS_INSUFFICIENT_RESOURCES;
            }
        }
        else
        {
            wStatus = NFCSTATUS_INVALID_PARAMETER;
        }
    }
    PH_LOG_LIBNFC_FUNC_EXIT();
    return wStatus;
}
/*Deactivation complete handler*/
void phHal4Nfc_HandleP2PDeActivate(
                               phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
                               void *pInfo
                               )
{
    pphHal4Nfc_ReceiveCallback_t pUpperRecvCb = NULL;
    pphHal4Nfc_SendCallback_t pUpperSendCb = NULL;
    phHal4Nfc_NotificationInfo_t uNotificationInfo;
    uNotificationInfo.psEventInfo = (phHal_sEventInfo_t *)pInfo;
    /*session is closed*/
    if(NULL != Hal4Ctxt->rem_dev_list[0])
    {
        Hal4Ctxt->rem_dev_list[0]->SessionOpened = FALSE;
        Hal4Ctxt->psADDCtxtInfo->nbr_of_devices = 0;
    }
    Hal4Ctxt->sTgtConnectInfo.psConnectedDevice = NULL;
    /*Update state*/
    Hal4Ctxt->Hal4CurrentState = eHal4StateOpenAndReady;
    Hal4Ctxt->Hal4NextState  = eHal4StateInvalid;
    Hal4Ctxt->sTgtConnectInfo.EmulationState = NFC_EVT_DEACTIVATED;
    /*If Trcv ctxt info is allocated ,free it here*/
    if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
    {
        if(PH_OSALNFC_INVALID_TIMER_ID != 
            Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId)
        {
            phOsalNfc_Timer_Stop(Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId);
            phOsalNfc_Timer_Delete(Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId);
        }
        pUpperRecvCb = Hal4Ctxt->psTrcvCtxtInfo->pP2PRecvCb;
        pUpperSendCb = Hal4Ctxt->psTrcvCtxtInfo->pP2PSendCb;
        /*Free Hal4 resources used by Target*/
        if (NULL != Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData.buffer)
        {
            phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo->
                sLowerRecvData.buffer);
        }
        if((NULL == Hal4Ctxt->sTgtConnectInfo.psConnectedDevice) 
            && (NULL != Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData))
        {
            phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData);
        }  
        phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo);
        Hal4Ctxt->psTrcvCtxtInfo = NULL;
    }
    /*if recv callback is pending*/
    if(NULL != pUpperRecvCb)
    {
        (*pUpperRecvCb)(
                        Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,
                        NULL,
                        NFCSTATUS_DESELECTED
                        );
    }  
    /*if send callback is pending*/
    else if(NULL != pUpperSendCb)
    {
        (*pUpperSendCb)(
                        Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,
                        NFCSTATUS_DESELECTED
                        );
    }  
    /*if pP2PNotification is registered*/
    else if(NULL != Hal4Ctxt->sUpperLayerInfo.pP2PNotification)
    {
        (*Hal4Ctxt->sUpperLayerInfo.pP2PNotification)(
                                Hal4Ctxt->sUpperLayerInfo.P2PDiscoveryCtxt,
                                NFC_EVENT_NOTIFICATION,
                                uNotificationInfo,
                                NFCSTATUS_DESELECTED
                                );
    }
    else/*Call Default event handler*/
    {
        if(NULL != Hal4Ctxt->sUpperLayerInfo.pDefaultEventHandler)
        {
            Hal4Ctxt->sUpperLayerInfo.pDefaultEventHandler(
                Hal4Ctxt->sUpperLayerInfo.DefaultListenerCtxt,
                NFC_EVENT_NOTIFICATION,
                uNotificationInfo,
                NFCSTATUS_DESELECTED
                );
        }
    }
}
NFCSTATUS
phNciNfc_Recv4BResp(
                        void                *psContext,
                        NFCSTATUS          wStatus
                       )
{
    NFCSTATUS                   status = NFCSTATUS_SUCCESS;
    phNciNfc_Context_t          *psNciContext = (phNciNfc_Context_t *)psContext;
    uint16_t                wRecvDataSz = 0;
    uint16_t CopyLen = 0;

    PH_LOG_NCI_FUNC_ENTRY();

    if(NULL == psNciContext)
    {
      status = PHNFCSTVAL(CID_NFC_NCI, NFCSTATUS_INVALID_PARAMETER);
      PH_LOG_NCI_INFO_STR(" Invalid Context Param..");
    }
    else
    {
        if((0 == (psNciContext->RspBuffInfo.wLen))
                || (PH_NCINFC_STATUS_OK != wStatus)
                || (NULL == (psNciContext->RspBuffInfo.pBuff))
                )
        {
            /* NOTE:- ( TODO) In this fail scenario,map the status code from response handler
            to the status code in Nci Context and use the same in the status below */
            status = PHNFCSTVAL(CID_NFC_NCI, NFCSTATUS_FAILED);
            PH_LOG_NCI_INFO_STR(" Data Receive Failed..");
        }
        else
        {
            wRecvDataSz = (psNciContext->tTranscvCtxt.tTranscvInfo.tRecvData.wLen);

            if((psNciContext->RspBuffInfo.wLen) > wRecvDataSz)
            {
                CopyLen = wRecvDataSz;
                status = NFCSTATUS_SUCCESS; // Change to this if handling is added NFCSTATUS_MORE_INFORMATION;
            }else
            {
                CopyLen = psNciContext->RspBuffInfo.wLen;
                status = NFCSTATUS_SUCCESS;
            }

            /* Extract the data part from pBuff[3] & fill it to be sent to upper layer */
                phOsalNfc_MemCopy((psNciContext->tTranscvCtxt.tTranscvInfo.tRecvData.pBuff),
                    (psNciContext->RspBuffInfo.pBuff),CopyLen);
                psNciContext->tTranscvCtxt.tTranscvInfo.tRecvData.wLen = CopyLen;
        }
        (psNciContext->tTranscvCtxt.wPrevStatus) = status;
    }

    if((NULL != psNciContext) && (NULL != (psNciContext->tTranscvCtxt.tSendPld.pBuff)))
    {
        (psNciContext->tTranscvCtxt.tSendPld.wLen) = 0;
        PH_LOG_NCI_INFO_STR(" Freeing Send Request Payload Buffer..");
        phOsalNfc_FreeMemory((psNciContext->tTranscvCtxt.tSendPld.pBuff));
        psNciContext->tTranscvCtxt.tSendPld.pBuff = NULL;
    }

    PH_LOG_NCI_FUNC_EXIT();

    return status;
}
/* shutdown callback -
  Free the allocated memory here */
STATIC void phLibNfc_ShutdownCb(void *pContext,NFCSTATUS status)
{
    pphLibNfc_RspCb_t           pClientCb=NULL;
    void                        *pUpperLayerContext=NULL;
    pphLibNfc_LibContext_t      pLibContext=NULL;

    PHNFC_UNUSED_VARIABLE(pContext);
    /* Get the Lib context */
    pLibContext=(pphLibNfc_LibContext_t)gpphLibContext;

    if(pLibContext == NULL)
    {
        status = NFCSTATUS_FAILED;
    }
    else
    {
        /* Initialize the local variable */
        pClientCb =pLibContext->CBInfo.pClientShutdownCb;
        pUpperLayerContext=pLibContext->CBInfo.pClientShtdwnCntx;
        if(status == NFCSTATUS_SUCCESS)
        {
            pLibContext->LibNfcState.cur_state = eLibNfcHalStateShutdown;
            phLibNfc_UpdateCurState(status,pLibContext);

            pLibContext->status.GenCb_pending_status=FALSE;
            
            /* Allocate the Memory for the Transceive info */
            if(pClientCb!=NULL)
            {
                (*pClientCb)(pUpperLayerContext, status);
            }
            if(pLibContext->psHwReference!=NULL)
            {
                phOsalNfc_FreeMemory(pLibContext->psHwReference);
                pLibContext->psHwReference = NULL;
            }
            if(NULL != gpphLibContext->psBufferedAuth)
            {
                if(NULL != gpphLibContext->psBufferedAuth->sRecvData.buffer)
                {
                    phOsalNfc_FreeMemory(
                        gpphLibContext->psBufferedAuth->sRecvData.buffer);
                }
                if(NULL != gpphLibContext->psBufferedAuth->sSendData.buffer)
                {
                    phOsalNfc_FreeMemory(
                        gpphLibContext->psBufferedAuth->sSendData.buffer);
                }
                phOsalNfc_FreeMemory(gpphLibContext->psBufferedAuth);
                gpphLibContext->psBufferedAuth = NULL;
            }
            /*Free the memory allocated during NDEF read,write
              and NDEF formatting*/
            phLibNfc_Ndef_DeInit();        
                phOsalNfc_FreeMemory(pLibContext);
                gpphLibContext=NULL;
                pLibContext= NULL;
       
        }
        else
        {
            /* shutdown sequence failed by HAL 4 */
            status= NFCSTATUS_FAILED;
            pLibContext=(pphLibNfc_LibContext_t)gpphLibContext;
            phLibNfc_UpdateCurState(status,pLibContext);
            pLibContext->status.GenCb_pending_status=FALSE;
            if(pClientCb!=NULL)
            {
                (*pClientCb)(pUpperLayerContext,status);
            }
        }
    }
}
/**
* De-Initialize the LIB NFC.
*/
NFCSTATUS phLibNfc_Mgt_DeInitialize(void *                      pDriverHandle,
                                   pphLibNfc_RspCb_t            pDeInitCb,
                                   void*                        pContext
                                   )
{
    NFCSTATUS Status = NFCSTATUS_SUCCESS;
    pphLibNfc_LibContext_t pLibContext = gpphLibContext;
    if(NULL==pDriverHandle)
    {
        /*Check for valid parameters */
        Status = NFCSTATUS_INVALID_PARAMETER;
    }
    else if((pLibContext==NULL)
        || (pLibContext->LibNfcState.cur_state
            == eLibNfcHalStateShutdown))
    {   /*Lib Nfc not initlized*/
        Status = NFCSTATUS_NOT_INITIALISED;
    }
    else
    {
        if(pDeInitCb==NULL)
        {
            phHal4Nfc_Hal4Reset(pLibContext->psHwReference,(void *)pLibContext);
            if(pLibContext->psHwReference!=NULL)
            {
                phOsalNfc_FreeMemory(pLibContext->psHwReference);
                pLibContext->psHwReference = NULL;
            }
            /*Free the memory allocated during NDEF read,write
              and NDEF formatting*/
            phLibNfc_Ndef_DeInit();
            phOsalNfc_FreeMemory(pLibContext);
            gpphLibContext=NULL;
            pLibContext= NULL;
        }
        else
        {
            if (NULL!= pLibContext->CBInfo.pClientShutdownCb)
            {
                /* Previous callback pending */
                Status = NFCSTATUS_BUSY;
            }
          Status = NFCSTATUS_PENDING;
          if(TRUE != pLibContext->status.GenCb_pending_status)
          {
              Status = phHal4Nfc_Close(pLibContext->psHwReference,
                                  phLibNfc_ShutdownCb,
                                  (void *)pLibContext);
          }
          if(Status== NFCSTATUS_PENDING)
          {
              pLibContext->CBInfo.pClientShutdownCb = pDeInitCb;
              pLibContext->CBInfo.pClientShtdwnCntx = pContext;
              pLibContext->status.GenCb_pending_status=TRUE;
              pLibContext->LibNfcState.next_state= eLibNfcHalStateShutdown;
          }
          else
          {
              Status =NFCSTATUS_FAILED;
          }
        }       
    }
    return Status;
}
Exemple #25
0
/**Handler for Target discovery completion for all remote device types*/
void phHal4Nfc_TargetDiscoveryComplete(
                                       phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
                                       void                  *pInfo
                                       )
{
    static phHal4Nfc_DiscoveryInfo_t sDiscoveryInfo; 
    NFCSTATUS status = NFCSTATUS_SUCCESS;
    /**SAK byte*/
    uint8_t Sak = 0;
    /*Union type to encapsulate and return the discovery info*/
    phHal4Nfc_NotificationInfo_t uNotificationInfo;
    /*All the following types will be discovered as type A ,and differentiation 
      will have to be done within this module based on SAK byte and UID info*/
    phHal_eRemDevType_t  aRemoteDevTypes[3] = {
                                               phHal_eISO14443_A_PICC,
                                               phHal_eNfcIP1_Target,
                                               phHal_eMifare_PICC                                                 
                                              };
    /*Count is used to add multiple info into remote dvice list for devices that
      support multiple protocols*/
    uint8_t Count = 0,
        NfcIpDeviceCount = 0;/**<Number of NfcIp devices discovered*/
    uint16_t nfc_id = 0;
    /*remote device info*/
    phHal_sRemoteDevInformation_t *psRemoteDevInfo = NULL;
    status = ((phNfc_sCompletionInfo_t *)pInfo)->status;
    /*Update Hal4 state*/
    Hal4Ctxt->Hal4CurrentState = eHal4StateTargetDiscovered;
    Hal4Ctxt->Hal4NextState  = eHal4StateInvalid;
     PHDBG_INFO("Hal4:Remotedevice Discovered"); 
    if(NULL != ((phNfc_sCompletionInfo_t *)pInfo)->info)
    {
        /*Extract Remote device Info*/
        psRemoteDevInfo = (phHal_sRemoteDevInformation_t *)
                                    ((phNfc_sCompletionInfo_t *)pInfo)->info;

        switch(psRemoteDevInfo->RemDevType)
        {
            case phHal_eISO14443_A_PICC:/*for TYPE A*/
            {
                Sak = psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak;
                if((Hal4Ctxt->psADDCtxtInfo->sCurrentPollConfig.EnableIso14443A)
                    || (TRUE == Hal4Ctxt->psADDCtxtInfo->smx_discovery))
                {
                    /*Check if Iso is Supported*/
                    if(Sak & ISO_14443_BITMASK)
                    {
                        Count++;
                    }
                    /*Check for Mifare Supported*/
                    switch( Sak )
                    {
                      case 0x01: // 1K Classic
                      case 0x09: // Mini
                      case 0x08: // 1K
                      case 0x18: // 4K
                      case 0x88: // Infineon 1K
                      case 0x98: // Pro 4K
                      case 0xB8: // Pro 4K
                      case 0x28: // 1K emulation
                      case 0x38: // 4K emulation
                        aRemoteDevTypes[Count] = phHal_eMifare_PICC;
                        Count++;
                        break;
                    }
                    if((0 == Sak)&& (0 == Count))
                    {
                        /*Mifare check*/
                        if((NXP_UID == 
                        psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Uid[0])
                        &&(NXP_MIN_UID_LEN <= 
                        psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.UidLength))
                        {
                            aRemoteDevTypes[Count] = phHal_eMifare_PICC;
                            Count++;
                        }
                    }
                    if ( !(Sak & NFCIP_BITMASK) )
                    {
                        // Always add a separate 3A target on a separate
                        // handle, so the upper layers can connect to it.
                        aRemoteDevTypes[Count] = phHal_eISO14443_3A_PICC;
                        Count++;
                    }
                }
                /*Check for P2P target passive*/
                if((Sak & NFCIP_BITMASK) && 
                    (NULL != Hal4Ctxt->sUpperLayerInfo.pP2PNotification)&&
                    (Hal4Ctxt->psADDCtxtInfo->sADDCfg.NfcIP_Mode 
                    & phHal_ePassive106))
                {
                  if( Sak == 0x53 // Fudan card incompatible to ISO18092
                      && psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AtqA[0] == 0x04
                      && psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AtqA[1] == 0x00
                    )
                  {
                    aRemoteDevTypes[Count] = phHal_eISO14443_3A_PICC;
                    Count++;
                  }
                  else
                  {
                    aRemoteDevTypes[Count] = phHal_eNfcIP1_Target;
                    Count++;
									}
                }
            }/*case phHal_eISO14443_A_PICC:*/
                break;
            case phHal_eNfcIP1_Target:/*P2P target detected*/
                aRemoteDevTypes[Count] = phHal_eNfcIP1_Target;
                Count++;
                break;
             case phHal_eISO14443_B_PICC: /*TYPE_B*/  
#ifdef TYPE_B
                aRemoteDevTypes[Count] = phHal_eISO14443_B_PICC;
                Count++;
                break;
#endif
            case phHal_eFelica_PICC: /*Felica*/
#ifdef TYPE_FELICA
            {
                /*nfc_id is used to differentiate between Felica and NfcIp target
                  discovered in Type F*/
                nfc_id = (((uint16_t)psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDm[0])
                    << BYTE_SIZE) | 
                    psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDm[1];
                /*check  for NfcIp target*/
                if(NXP_NFCIP_NFCID2_ID  == nfc_id)
                {
                    if((NULL != Hal4Ctxt->sUpperLayerInfo.pP2PNotification)
                        &&((Hal4Ctxt->psADDCtxtInfo->sADDCfg.NfcIP_Mode 
                             & phHal_ePassive212) || 
                            (Hal4Ctxt->psADDCtxtInfo->sADDCfg.NfcIP_Mode
                             & phHal_ePassive424)))
                    {
                        aRemoteDevTypes[Count] = phHal_eNfcIP1_Target;
                        Count++;
                    }
                }
                else/*Felica*/
                {
                    if(Hal4Ctxt->psADDCtxtInfo->sCurrentPollConfig.EnableFelica212
                    || Hal4Ctxt->psADDCtxtInfo->sCurrentPollConfig.EnableFelica424)
                    {
                        aRemoteDevTypes[Count] = phHal_eFelica_PICC;
                        Count++;                    
                    }
                }
                break;
            }
#endif             
            case phHal_eJewel_PICC: /*Jewel*/
#ifdef TYPE_JEWEL
            {
                /*Report Jewel tags only if TYPE A is enabled*/
                if(Hal4Ctxt->psADDCtxtInfo->sCurrentPollConfig.EnableIso14443A)
                {
                    aRemoteDevTypes[Count] = phHal_eJewel_PICC;
                    Count++;                    
                }
                break;
            }               
#endif
#ifdef  TYPE_ISO15693
            case phHal_eISO15693_PICC: /*ISO15693*/
            {
                if(Hal4Ctxt->psADDCtxtInfo->sCurrentPollConfig.EnableIso15693)
                {
                    aRemoteDevTypes[Count] = phHal_eISO15693_PICC;
                    Count++;                    
                }
                break;
            }               
#endif /* #ifdef    TYPE_ISO15693 */
            /*Types currently not supported*/
            case phHal_eISO14443_BPrime_PICC:
            default:
                PHDBG_WARNING("Hal4:Notification for Not supported types");
                break;
        }/*End of switch*/
        /*Update status code to success if atleast one device info is available*/
        status = (((NFCSTATUS_SUCCESS != status) 
                  && (NFCSTATUS_MULTIPLE_TAGS != status))
                   &&(Hal4Ctxt->psADDCtxtInfo->nbr_of_devices != 0))?
                    NFCSTATUS_SUCCESS:status;
        
        /*Update status to NFCSTATUS_MULTIPLE_PROTOCOLS if count > 1 ,and this
          is first discovery notification from Hci*/
        status = ((NFCSTATUS_SUCCESS == status)
                    &&(Hal4Ctxt->psADDCtxtInfo->nbr_of_devices == 0)
                    &&(Count > 1)?NFCSTATUS_MULTIPLE_PROTOCOLS:status);
         /*If multiple protocols are supported ,allocate separate remote device
          information for each protocol supported*/
        /*Allocate and copy Remote device info into Hal4 Context*/
        while(Count)
        {
            PHDBG_INFO("Hal4:Count is not zero"); 
            --Count;
            /*Allocate memory for each of Count number of 
              devices*/
            if(NULL == Hal4Ctxt->rem_dev_list[
                Hal4Ctxt->psADDCtxtInfo->nbr_of_devices])
            {
                Hal4Ctxt->rem_dev_list[
                    Hal4Ctxt->psADDCtxtInfo->nbr_of_devices] 
                = (phHal_sRemoteDevInformation_t *)
                    phOsalNfc_GetMemory(
                    (uint32_t)(
                    sizeof(phHal_sRemoteDevInformation_t))
                    );
            }
            if(NULL == Hal4Ctxt->rem_dev_list[
                Hal4Ctxt->psADDCtxtInfo->nbr_of_devices])
            {
                status =  PHNFCSTVAL(CID_NFC_HAL,
                    NFCSTATUS_INSUFFICIENT_RESOURCES);
                phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,0);
                break;
            }
            else
            {
                (void)memcpy(
                    (void *)Hal4Ctxt->rem_dev_list[
                        Hal4Ctxt->psADDCtxtInfo->nbr_of_devices],
                        (void *)psRemoteDevInfo,
                        sizeof(phHal_sRemoteDevInformation_t)
                        );
                /*Now copy appropriate device type from aRemoteDevTypes array*/
                Hal4Ctxt->rem_dev_list[
                        Hal4Ctxt->psADDCtxtInfo->nbr_of_devices]->RemDevType 
                            =   aRemoteDevTypes[Count];
                /*Increment number of devices*/
                Hal4Ctxt->psADDCtxtInfo->nbr_of_devices++;                            
            }/*End of else*/
        }/*End of while*/
        
        /*If Upper layer is interested only in P2P notifications*/
        if((NULL != Hal4Ctxt->sUpperLayerInfo.pP2PNotification)
           &&(((Hal4Ctxt->psADDCtxtInfo->nbr_of_devices == 1)
              &&(phHal_eNfcIP1_Target == Hal4Ctxt->rem_dev_list[0]->RemDevType))
              ||(NULL == Hal4Ctxt->sUpperLayerInfo.pTagDiscoveryNotification))
            )
        {
            PHDBG_INFO("Hal4:Trying to notify P2P Listener"); 
            /*NFCSTATUS_SUCCESS or NFCSTATUS_MULTIPLE_PROTOCOLS*/
            if((NFCSTATUS_SUCCESS == status) 
                ||(NFCSTATUS_MULTIPLE_PROTOCOLS == status))
            {
                /*Pick only the P2P target device info from the list*/
                for(Count = Hal4Ctxt->psADDCtxtInfo->nbr_of_devices;
                    Count > 0;--Count)
                {
                    /*Only one P2P target can be detected in one discovery*/
                    if(phHal_eNfcIP1_Target == 
                        Hal4Ctxt->rem_dev_list[Count-1]->RemDevType)
                    {
                        if (Count != 1)
                        {
                            (void)memcpy(
                                (void *)Hal4Ctxt->rem_dev_list[0],
                                (void *)Hal4Ctxt->rem_dev_list[Count-1],
                                        sizeof(phHal_sRemoteDevInformation_t)
                                        );
                        }
                        NfcIpDeviceCount = 1;                       
                        break;
                    }
                }
                /*If any P2p devices are discovered free other device info*/
                while(Hal4Ctxt->psADDCtxtInfo->nbr_of_devices > NfcIpDeviceCount)
                {
                    phOsalNfc_FreeMemory(Hal4Ctxt->rem_dev_list[
                        --Hal4Ctxt->psADDCtxtInfo->nbr_of_devices]);
                    Hal4Ctxt->rem_dev_list[
                        Hal4Ctxt->psADDCtxtInfo->nbr_of_devices] = NULL;
                }
                /*Issue P2P notification*/
                if(NfcIpDeviceCount == 1)
                {
                    sDiscoveryInfo.NumberOfDevices 
                        = Hal4Ctxt->psADDCtxtInfo->nbr_of_devices;
                    sDiscoveryInfo.ppRemoteDevInfo = Hal4Ctxt->rem_dev_list;
                    uNotificationInfo.psDiscoveryInfo = &sDiscoveryInfo;
                    PHDBG_INFO("Hal4:Calling P2P listener");
                    (*Hal4Ctxt->sUpperLayerInfo.pP2PNotification)(
                            (void *)(Hal4Ctxt->sUpperLayerInfo.P2PDiscoveryCtxt),
                            NFC_DISCOVERY_NOTIFICATION,
                            uNotificationInfo,
                            NFCSTATUS_SUCCESS
                            );
                }
                else/*Restart Discovery wheel*/ 
                {
                    PHDBG_INFO("Hal4:No P2P device in list");  
                    Hal4Ctxt->psADDCtxtInfo->nbr_of_devices = 0;
                    PHDBG_INFO("Hal4:Restart discovery1"); 
                    status = phHciNfc_Restart_Discovery (
                                        (void *)Hal4Ctxt->psHciHandle,
                                        (void *)gpphHal4Nfc_Hwref,      
                                        FALSE
                                        );
                    Hal4Ctxt->Hal4NextState = (NFCSTATUS_PENDING == status?
                                                        eHal4StateConfiguring:
                                                    Hal4Ctxt->Hal4NextState);
                }
            }
            /*More discovery info available ,get next info from HCI*/
            else if((NFCSTATUS_MULTIPLE_TAGS == status)
                &&(Hal4Ctxt->psADDCtxtInfo->nbr_of_devices 
                     < MAX_REMOTE_DEVICES))
            {
                status = phHciNfc_Select_Next_Target (
                    Hal4Ctxt->psHciHandle,
                    (void *)gpphHal4Nfc_Hwref
                    );
            }
            else/*Failed discovery ,restart discovery*/
            {
                Hal4Ctxt->psADDCtxtInfo->nbr_of_devices = 0;
                PHDBG_INFO("Hal4:Restart discovery2"); 
                status = phHciNfc_Restart_Discovery (
                                        (void *)Hal4Ctxt->psHciHandle,
                                        (void *)gpphHal4Nfc_Hwref,      
                                        FALSE
                                        );/*Restart Discovery wheel*/   
                Hal4Ctxt->Hal4NextState = (NFCSTATUS_PENDING == status?
                                                eHal4StateConfiguring:
                                                Hal4Ctxt->Hal4NextState);
            }
        }/*if((NULL != Hal4Ctxt->sUpperLayerInfo.pP2PNotification)...*/
        /*Notify if Upper layer is interested in tag notifications,also notify
          P2p if its in the list with other tags*/
        else if(NULL != Hal4Ctxt->sUpperLayerInfo.pTagDiscoveryNotification)
        {
            PHDBG_INFO("Hal4:Trying to notify Tag notification");
            /*Multiple tags in field, get discovery info a second time for the 
              other devices*/
            if((NFCSTATUS_MULTIPLE_TAGS == status)
                &&(Hal4Ctxt->psADDCtxtInfo->nbr_of_devices < MAX_REMOTE_DEVICES))
            {
                PHDBG_INFO("Hal4:select next target1"); 
                status = phHciNfc_Select_Next_Target (
                                                    Hal4Ctxt->psHciHandle,
                                                    (void *)gpphHal4Nfc_Hwref
                                                    );
            }
            /*Single tag multiple protocols scenario,Notify Multiple Protocols 
              status to upper layer*/
            else if(status == NFCSTATUS_MULTIPLE_PROTOCOLS) 
            {
                PHDBG_INFO("Hal4:Multiple Tags or protocols");
                sDiscoveryInfo.NumberOfDevices 
                    = Hal4Ctxt->psADDCtxtInfo->nbr_of_devices;
                sDiscoveryInfo.ppRemoteDevInfo = Hal4Ctxt->rem_dev_list;
                uNotificationInfo.psDiscoveryInfo = &sDiscoveryInfo;
                (*Hal4Ctxt->sUpperLayerInfo.pTagDiscoveryNotification)(
                            (void *)(Hal4Ctxt->sUpperLayerInfo.DiscoveryCtxt),
                            NFC_DISCOVERY_NOTIFICATION,
                            uNotificationInfo,
                            status
                            );
            }
            else /*NFCSTATUS_SUCCESS*/
            {                
                if(((Hal4Ctxt->psADDCtxtInfo->nbr_of_devices == 1)
                     &&(phHal_eNfcIP1_Target 
                     == Hal4Ctxt->rem_dev_list[0]->RemDevType))
                     ||(NFCSTATUS_SUCCESS != status)
                     || (Hal4Ctxt->psADDCtxtInfo->nbr_of_devices == 0)
                     )/*device detected but upper layer is not interested 
                       in the type(P2P) or activate next failed*/
                {
                    while(Hal4Ctxt->psADDCtxtInfo->nbr_of_devices > 0)
                    {
                        phOsalNfc_FreeMemory(Hal4Ctxt->rem_dev_list[
                            --Hal4Ctxt->psADDCtxtInfo->nbr_of_devices]);
                        Hal4Ctxt->rem_dev_list[
                            Hal4Ctxt->psADDCtxtInfo->nbr_of_devices] = NULL;
                    }
                    PHDBG_INFO("Hal4:Restart discovery3"); 
                    status = phHciNfc_Restart_Discovery (
                        (void *)Hal4Ctxt->psHciHandle,
                        (void *)gpphHal4Nfc_Hwref,      
                        FALSE
                        );/*Restart Discovery wheel*/ 
                    Hal4Ctxt->Hal4NextState = (
                        NFCSTATUS_PENDING == status?eHal4StateConfiguring
                                                    :Hal4Ctxt->Hal4NextState
                                                    );
                }
                else/*All remote device info available.Notify to upper layer*/
                {
                    /*Update status for MULTIPLE_TAGS here*/
                    status = (Hal4Ctxt->psADDCtxtInfo->nbr_of_devices > 1?
                                NFCSTATUS_MULTIPLE_TAGS:status);    
                    /*If listener is registered ,call it*/
                    sDiscoveryInfo.NumberOfDevices 
                        = Hal4Ctxt->psADDCtxtInfo->nbr_of_devices;
                    sDiscoveryInfo.ppRemoteDevInfo
                        = Hal4Ctxt->rem_dev_list;
                    uNotificationInfo.psDiscoveryInfo = &sDiscoveryInfo;                    
                    PHDBG_INFO("Hal4:Calling Discovery Handler1");
                    (*Hal4Ctxt->sUpperLayerInfo.pTagDiscoveryNotification)(
                        (void *)(Hal4Ctxt->sUpperLayerInfo.DiscoveryCtxt),
                        NFC_DISCOVERY_NOTIFICATION,
                        uNotificationInfo,
                        status
                        );
                }
            }       
        } /*else if(NULL != Hal4Ctxt->sUpperLayerInfo.pTagDiscoveryNotification)*/      
        else/*listener not registered ,Restart Discovery wheel*/ 
        {   
            PHDBG_INFO("Hal4:No listener registered.Ignoring Discovery  \
                        Notification");  
            Hal4Ctxt->psADDCtxtInfo->nbr_of_devices = 0;
            PHDBG_INFO("Hal4:Restart discovery4"); 
            status = phHciNfc_Restart_Discovery (
                                (void *)Hal4Ctxt->psHciHandle,
                                (void *)gpphHal4Nfc_Hwref,      
                                FALSE
                                );
            Hal4Ctxt->Hal4NextState = (NFCSTATUS_PENDING == status?
                                                eHal4StateConfiguring:
                                            Hal4Ctxt->Hal4NextState);
        }
    }/*if(NULL != ((phNfc_sCompletionInfo_t *)pInfo)->info)*/
/*!
 * \brief Receives the HCI Admin Commands from the corresponding peripheral device.
 *
 * This function receives  the HCI Admin Commands from the connected NFC Pheripheral
 * device
 */
static
 NFCSTATUS
 phHciNfc_Recv_Admin_Cmd (
                        void                *psContext,
                        void                *pHwRef,
                        uint8_t             *pCmd,
#ifdef ONE_BYTE_LEN
                        uint8_t             length
#else
                        uint16_t            length
#endif
                     )
{
    phHciNfc_sContext_t         *psHciContext = 
                                    (phHciNfc_sContext_t *)psContext ;
    phHciNfc_HCP_Packet_t       *hcp_packet = NULL;
    phHciNfc_HCP_Message_t      *hcp_message = NULL;
    phHciNfc_AdminGate_Info_t   *p_admin_info=NULL;
    phHciNfc_Pipe_Info_t        *p_pipe_info = NULL;
    uint8_t                     index=0;
    uint8_t                     pipe_id = (uint8_t) HCI_UNKNOWN_PIPE_ID;
    uint8_t                     cmd = (uint8_t) HCP_MSG_INSTRUCTION_INVALID;
    uint8_t                     response = (uint8_t) ANY_OK;
    NFCSTATUS                   status = NFCSTATUS_SUCCESS;

    if( (NULL == psHciContext) 
        || (NULL == pHwRef) 
        || (HCP_HEADER_LEN > length ) 
      )
    {
      status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {
        hcp_packet = (phHciNfc_HCP_Packet_t *)pCmd;
        hcp_message = &hcp_packet->msg.message;
        p_admin_info = psHciContext->p_admin_info;
        /* Get the Command instruction bits from the Message Header */
        cmd = (uint8_t) GET_BITS8( hcp_message->msg_header,
            HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);

        switch( cmd )
        {
            /* These are notifications sent by the Host Controller */
            case ADM_NOTIFY_PIPE_CREATED:
            {
                pipe_id = hcp_message->payload[RESPONSE_PIPEID_OFFSET];
                p_pipe_info = (phHciNfc_Pipe_Info_t *)
                        phOsalNfc_GetMemory(sizeof(phHciNfc_Pipe_Info_t));
                if(NULL != p_pipe_info)
                {
                    /* The Source Host is the UICC Host */
                    p_pipe_info->pipe.source.host_id = 
                                    hcp_message->payload[index++];
                    /* The Source Gate is same as the Destination Gate */
                    p_pipe_info->pipe.source.gate_id    = 
                                    hcp_message->payload[index++];
                    /* The Source Host is the Terminal Host */
                    p_pipe_info->pipe.dest.host_id = 
                                    hcp_message->payload[index++];
                    p_pipe_info->pipe.dest.gate_id  = 
                                    hcp_message->payload[index++];
                    p_pipe_info->pipe.pipe_id   = 
                                    hcp_message->payload[index++];
                }
                status = phHciNfc_Update_PipeInfo(psHciContext,
                    &(p_admin_info->pipe_seq), pipe_id, p_pipe_info);

                if( NFCSTATUS_SUCCESS == status )
                {
                    psHciContext->p_pipe_list[pipe_id] = p_pipe_info;
                    if (NULL != p_pipe_info)
                    {
                        p_pipe_info->pipe.pipe_id = pipe_id;
                    }
                }
                break;
            }
            case ADM_NOTIFY_PIPE_DELETED:
            {
                pipe_id = hcp_message->payload[index++];
                p_pipe_info = psHciContext->p_pipe_list[pipe_id];
                if ( NULL != p_pipe_info )
                {
                        status = phHciNfc_Update_PipeInfo(
                            psHciContext, &(p_admin_info->pipe_seq),
                             (uint8_t) HCI_UNKNOWN_PIPE_ID, p_pipe_info);
                    if(NFCSTATUS_SUCCESS == status )
                    {
                        phOsalNfc_FreeMemory(p_pipe_info);
                        psHciContext->p_pipe_list[pipe_id] = NULL;
                    }
                }
                break;
            }
            /* TODO: Since we receive the Host ID, we need to clear
             * all the pipes created with the host
             */
            case ADM_NOTIFY_ALL_PIPE_CLEARED:
            {
                break;
            }
            /* case ADM_CREATE_PIPE: */
            /* case ADM_DELETE_PIPE: */
            /* case ADM_CLEAR_ALL_PIPE: */
            default:
            {
                response = ANY_E_CMD_NOT_SUPPORTED;
                status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_COMMAND_NOT_SUPPORTED);
                break;
            }
        }
        hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
        phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
                                (uint8_t) HCI_ADMIN_PIPE_ID,
                                HCP_MSG_TYPE_RESPONSE, response );
        psHciContext->tx_total = HCP_HEADER_LEN;
        status = phHciNfc_Send_HCP( (void *)psHciContext, (void *)pHwRef );

        p_admin_info->admin_pipe_info->recv_msg_type = HCP_MSG_TYPE_COMMAND;
        p_admin_info->admin_pipe_info->sent_msg_type = HCP_MSG_TYPE_RESPONSE;
        p_admin_info->admin_pipe_info->prev_msg = response;
        p_admin_info->admin_pipe_info->prev_status = NFCSTATUS_PENDING;
    }
    return status;
}
static
NFCSTATUS
phHciNfc_Recv_Admin_Response(
                        void                *psContext,
                        void                *pHwRef,
                        uint8_t             *pResponse,
#ifdef ONE_BYTE_LEN
                        uint8_t             length
#else
                        uint16_t            length
#endif
                    )
{
    phHciNfc_sContext_t         *psHciContext = 
                                    (phHciNfc_sContext_t *)psContext ;
    phHciNfc_HCP_Packet_t       *hcp_packet = NULL;
    phHciNfc_HCP_Message_t      *hcp_message = NULL;
    phHciNfc_Pipe_Info_t        *p_pipe_info = NULL;
    phHciNfc_AdminGate_Info_t   *p_admin_info = NULL;
    NFCSTATUS                   status = NFCSTATUS_SUCCESS;
    uint8_t                     pipe_id = (uint8_t) HCI_UNKNOWN_PIPE_ID;
    uint8_t                     prev_cmd = 0;
    NFCSTATUS                   prev_status = NFCSTATUS_SUCCESS;

    if( (NULL == psHciContext) || (NULL == pHwRef) )
    {
      status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
    }
    else if ( NULL == psHciContext->p_admin_info )
    {
        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION);
    }
    else
    {
        hcp_packet = (phHciNfc_HCP_Packet_t *)pResponse;
        hcp_message = &hcp_packet->msg.message;
        p_admin_info = psHciContext->p_admin_info;
        prev_cmd = p_admin_info->admin_pipe_info->prev_msg ;
        prev_status = p_admin_info->admin_pipe_info->prev_status ;
        if(prev_status == NFCSTATUS_PENDING)
        {
            switch(prev_cmd)
            {
                case ANY_SET_PARAMETER:
                {
                    break;
                }
                case ANY_GET_PARAMETER:
                {
                    status = phHciNfc_Admin_InfoUpdate(psHciContext,
                                (phHal_sHwReference_t *)pHwRef,
                                p_admin_info->admin_pipe_info->reg_index, 
                                    &pResponse[HCP_HEADER_LEN],
                                        (uint8_t)(length - HCP_HEADER_LEN));
                    break;
                }
                case ANY_OPEN_PIPE:
                {
                    break;
                }
                case ANY_CLOSE_PIPE:
                {
                    phOsalNfc_FreeMemory(p_admin_info->admin_pipe_info);
                    p_admin_info->admin_pipe_info = NULL;
                    psHciContext->p_pipe_list[PIPETYPE_STATIC_ADMIN] = NULL;
                    break;
                }
                case ADM_CREATE_PIPE:
                {
                    p_pipe_info = (phHciNfc_Pipe_Info_t *)
                                        p_admin_info->admin_pipe_info->param_info;
                    pipe_id = hcp_message->payload[RESPONSE_PIPEID_OFFSET];
                    status = phHciNfc_Update_PipeInfo(psHciContext,
                        &(p_admin_info->pipe_seq), pipe_id, p_pipe_info);
                    if(NFCSTATUS_SUCCESS == status )
                    {
                        psHciContext->p_pipe_list[pipe_id] = p_pipe_info;
                        p_pipe_info->pipe.pipe_id = pipe_id;
                    }
                    break;
                }
                case ADM_DELETE_PIPE:
                {
                    p_pipe_info = (phHciNfc_Pipe_Info_t *)
                                    p_admin_info->admin_pipe_info->param_info;
                    if ( NULL != p_pipe_info )
                    {
                        pipe_id = p_pipe_info->pipe.pipe_id;
                        status = phHciNfc_Update_PipeInfo(
                            psHciContext, &(p_admin_info->pipe_seq),
                             (uint8_t) HCI_UNKNOWN_PIPE_ID, p_pipe_info);
                        if(NFCSTATUS_SUCCESS == status )
                        {
                            phOsalNfc_FreeMemory(p_pipe_info);
                            psHciContext->p_pipe_list[pipe_id] = NULL;
                        }
                    }
                    break;
                }
                case ADM_CLEAR_ALL_PIPE:
                {
                    break;
                }
                default:
                {
                    status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_RESPONSE);
                    HCI_DEBUG("%s: Default Statement Should Not Occur \n",
                                                    "phHciNfc_Recv_Admin_Response");
                    break;
                }
            }
        }
        if( NFCSTATUS_SUCCESS == status )
        {
            if( NULL != p_admin_info->admin_pipe_info)
            {
                p_admin_info->admin_pipe_info->prev_status = NFCSTATUS_SUCCESS;
            }
            p_admin_info->current_seq = p_admin_info->next_seq;
        }
    }
    return status;
}
NFCSTATUS
phHciNfc_Update_Pipe(
                        phHciNfc_sContext_t     *psHciContext,
                        void                    *pHwRef,
                        phHciNfc_PipeMgmt_Seq_t *p_pipe_seq
                    )
{
    static uint8_t pipe_index = HCI_DYNAMIC_PIPE_ID;
    phHciNfc_Pipe_Info_t  *p_pipe_info = NULL;
    NFCSTATUS       status = NFCSTATUS_SUCCESS;


    for (pipe_index = 0;
            (pipe_index +  HCI_DYNAMIC_PIPE_ID) <=
                (uint8_t)(sizeof(host_gate_list)/sizeof(phHciNfc_GateID_t) );
                    pipe_index++)
    {
        status = phHciNfc_Allocate_Resource((void **)&p_pipe_info,
                                            sizeof(phHciNfc_Pipe_Info_t));

        if( (NFCSTATUS_SUCCESS == status)
            && (NULL != p_pipe_info))
        {
            /* The Source Host is the Terminal Host */
            p_pipe_info->pipe.source.host_id  = (uint8_t) phHciNfc_TerminalHostID;

            /* The Source Gate is same as the Destination Gate */
            p_pipe_info->pipe.source.gate_id    =
                                    host_gate_list[pipe_index];
            p_pipe_info->pipe.dest.host_id =
                                    phHciNfc_HostControllerID;
            p_pipe_info->pipe.dest.gate_id  =
                                    host_gate_list[pipe_index];
            /* The Pipe ID is unknown until it is assigned */
            p_pipe_info->pipe.pipe_id   = (uint8_t) HCI_UNKNOWN_PIPE_ID;

            /* Initialise the Resources for the particular Gate */

            status = phHciNfc_Create_All_Pipes(psHciContext,
                                            pHwRef, p_pipe_seq );

            if( NFCSTATUS_SUCCESS == status )
            {
                uint8_t pipe_id = (uint8_t)(pipe_index + HCI_DYNAMIC_PIPE_ID);
                status = phHciNfc_Update_PipeInfo( psHciContext, p_pipe_seq ,
                                        pipe_id, p_pipe_info );
                if( NFCSTATUS_SUCCESS == status )
                {
                    p_pipe_info->pipe.pipe_id = pipe_id;
                    psHciContext->p_pipe_list[pipe_id] = p_pipe_info;
                }
                else
                {
                    phOsalNfc_FreeMemory(p_pipe_info);
                }
                p_pipe_info = NULL;
            }
            else
            {
                phOsalNfc_FreeMemory(p_pipe_info);
                p_pipe_info = NULL;
            }
        }
        else
        {
            status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INSUFFICIENT_RESOURCES);
        }
    }

    if( NFCSTATUS_SUCCESS == status )
    {
        status = phHciNfc_LinkMgmt_Initialise( psHciContext,pHwRef );
        if(NFCSTATUS_SUCCESS == status)
        {
            status = phHciNfc_ReaderMgmt_Initialise( psHciContext,pHwRef );
        }
        if(NFCSTATUS_SUCCESS == status)
        {
            status = phHciNfc_EmuMgmt_Initialise( psHciContext,pHwRef );
        }
    }

    return status;
}
/**
* Reset the LIB NFC.
*/
NFCSTATUS phLibNfc_Mgt_Reset(void  *pContext)
{
    NFCSTATUS Status = NFCSTATUS_SUCCESS;
    phLibNfc_LibContext_t   *pLibNfc_Ctxt = (phLibNfc_LibContext_t *)pContext;

    if((pLibNfc_Ctxt == NULL)
        || (gpphLibContext->LibNfcState.cur_state
            == eLibNfcHalStateShutdown))
    {   /*Lib Nfc not initlized*/
        Status = NFCSTATUS_NOT_INITIALISED;
    }  
    else if(NULL == pContext) 
    {
        Status = NFCSTATUS_INVALID_PARAMETER;
    }
    /* Check for valid state,If De initialize is called then
    return NFCSTATUS_SHUTDOWN */
    else if(gpphLibContext->LibNfcState.next_state
                            == eLibNfcHalStateShutdown)
    {
        Status = NFCSTATUS_SHUTDOWN;
    }
    else
    {                     
        /*Reset all callback status*/
        (void) memset(&(gpphLibContext->RegNtfType),0,
                        sizeof(phLibNfc_Registry_Info_t));
        (void) memset(&(gpphLibContext->sADDconfig),0,
                        sizeof(phLibNfc_sADD_Cfg_t));
        (void) memset(&(gpphLibContext->ndef_cntx),0,
                        sizeof(phLibNfc_NdefInfo_t));
        (void) memset(&(gpphLibContext->sNfcIp_Context),0,
                        sizeof(phLibNfc_NfcIpInfo_t));
        (void) memset(&(gpphLibContext->sCardEmulCfg),0,
                        sizeof(phHal_sEmulationCfg_t));
        (void) memset(&(gpphLibContext->Discov_handle),0,
                        MAX_REMOTE_DEVICES);

        /*Free memory allocated for NDEF records*/
        if(NULL != gpphLibContext->psBufferedAuth)
        {
            if(NULL != gpphLibContext->psBufferedAuth->sRecvData.buffer)
            {
                phOsalNfc_FreeMemory(
                    gpphLibContext->psBufferedAuth->sRecvData.buffer);
                gpphLibContext->psBufferedAuth->sRecvData.buffer = NULL;
            }
            if(NULL != gpphLibContext->psBufferedAuth->sSendData.buffer)
            {
                phOsalNfc_FreeMemory(
                    gpphLibContext->psBufferedAuth->sSendData.buffer);
                gpphLibContext->psBufferedAuth->sSendData.buffer = NULL;
            }
            phOsalNfc_FreeMemory(gpphLibContext->psBufferedAuth);
            gpphLibContext->psBufferedAuth = NULL;
        }
        if(NULL != gpphLibContext->psTransInfo)
        {
            phOsalNfc_FreeMemory(gpphLibContext->psTransInfo);
            gpphLibContext->psTransInfo = NULL;
        }
        if(NULL != gpphLibContext->ndef_cntx.psNdefMap)
        {
            if(NULL != gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf)
            {
                phOsalNfc_FreeMemory(gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf);
                gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf = NULL;
            }
            phOsalNfc_FreeMemory(gpphLibContext->ndef_cntx.psNdefMap);
            gpphLibContext->ndef_cntx.psNdefMap = NULL;
        }
        if(NULL != gpphLibContext->psOverHalCtxt)
        {
            phOsalNfc_FreeMemory(gpphLibContext->psOverHalCtxt);
            gpphLibContext->psTransInfo = NULL;
        }
        if(NULL != gpphLibContext->psDevInputParam)
        {
            phOsalNfc_FreeMemory(gpphLibContext->psDevInputParam);
            gpphLibContext->psDevInputParam = NULL;
        }
        if(NULL != gpphLibContext->ndef_cntx.ndef_fmt)
        {
            phOsalNfc_FreeMemory(gpphLibContext->ndef_cntx.ndef_fmt);
            gpphLibContext->ndef_cntx.ndef_fmt = NULL;
        }
        if(NULL != pNdefRecord) 
        {
            if(NULL != pNdefRecord->Id)
            {
                phOsalNfc_FreeMemory(pNdefRecord->Id);
                pNdefRecord->Id = NULL;
            }
            if(NULL != pNdefRecord->Type)
            {
                phOsalNfc_FreeMemory(pNdefRecord->Type);
                pNdefRecord->Type = NULL;
            }
            if(NULL != pNdefRecord->PayloadData)
            {
                phOsalNfc_FreeMemory(pNdefRecord->PayloadData);
                pNdefRecord->PayloadData = NULL;
            }
        }
        if(NULL != NdefInfo.pNdefRecord)
        {
            phOsalNfc_FreeMemory(NdefInfo.pNdefRecord);
            NdefInfo.pNdefRecord = NULL;
        }          
        if(NULL != gpphLibContext->phLib_NdefRecCntx.NdefCb)
        {
            phOsalNfc_FreeMemory(gpphLibContext->phLib_NdefRecCntx.NdefCb);
            gpphLibContext->phLib_NdefRecCntx.NdefCb = NULL;
        }
        if(NULL != gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer)
        {
            phOsalNfc_FreeMemory(gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer);
            gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer = NULL;
        }
        /* No device is connected */
        gpphLibContext->Connected_handle = 0x00;       
        gpphLibContext->Prev_Connected_handle = 0x00;
        gpphLibContext->ReleaseType = NFC_INVALID_RELEASE_TYPE;        
        gpphLibContext->eLibNfcCfgMode = NFC_DISCOVERY_STOP;
        /*Lib Nfc Stack is initilized and in idle state*/
        gpphLibContext->LibNfcState.cur_state = eLibNfcHalStateInitandIdle;

        /* Reset all callback status */
        gpphLibContext->CBInfo.pClientCkNdefCb = NULL;
        gpphLibContext->CBInfo.pClientCkNdefCntx = NULL;
        gpphLibContext->CBInfo.pClientConCntx = NULL;
        gpphLibContext->CBInfo.pClientConnectCb = NULL;
        gpphLibContext->CBInfo.pClientDConCntx = NULL;
        gpphLibContext->CBInfo.pClientDisCfgCntx = NULL;
        gpphLibContext->CBInfo.pClientDisConfigCb = NULL;
        gpphLibContext->CBInfo.pClientInitCb = NULL;
        gpphLibContext->CBInfo.pClientInitCntx = gpphLibContext;
        gpphLibContext->CBInfo.pClientNdefNtfRespCb = NULL;
        gpphLibContext->CBInfo.pClientNdefNtfRespCntx = NULL;
        gpphLibContext->CBInfo.pClientNtfRegRespCB = NULL;
        gpphLibContext->CBInfo.pClientNtfRegRespCntx = NULL;
        gpphLibContext->CBInfo.pClientPresChkCb = NULL;
        gpphLibContext->CBInfo.pClientPresChkCntx = NULL;
        gpphLibContext->CBInfo.pClientRdNdefCb = NULL;
        gpphLibContext->CBInfo.pClientRdNdefCntx = NULL;
        gpphLibContext->CBInfo.pClientShtdwnCntx = NULL;
        gpphLibContext->CBInfo.pClientShutdownCb = NULL;
        gpphLibContext->CBInfo.pClientTransceiveCb = NULL;
        gpphLibContext->CBInfo.pClientTranseCntx = NULL;
        gpphLibContext->CBInfo.pClientWrNdefCb = NULL;
        gpphLibContext->CBInfo.pClientWrNdefCntx = NULL;
        gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCb = NULL;
        gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCntx = NULL;
        gpphLibContext->sNfcIp_Context.pClientNfcIpRxCb = NULL;
        gpphLibContext->sNfcIp_Context.pClientNfcIpRxCntx = NULL;
        gpphLibContext->sNfcIp_Context.pClientNfcIpTxCb = NULL;
        gpphLibContext->sNfcIp_Context.pClientNfcIpTxCntx = NULL;
        gpphLibContext->sSeContext.sSeCallabackInfo.pSeListenerNtfCb = NULL;
        gpphLibContext->sSeContext.sSeCallabackInfo.pSeListenerCtxt = NULL;
        gpphLibContext->sSeContext.sSeCallabackInfo.pSEsetModeCb = NULL;
        gpphLibContext->sSeContext.sSeCallabackInfo.pSEsetModeCtxt = NULL;
        /*No callback is pending*/
        gpphLibContext->status.GenCb_pending_status = FALSE;        
                    
    }
    return Status;
}
/*Deactivation complete handler*/
void phHal4Nfc_HandleCEDeActivate(
                               phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
                               void *pInfo
                               )
{
    pphLibNfc_LibContext_t pLibContext=(pphLibNfc_LibContext_t)Hal4Ctxt->sUpperLayerInfo.HCEEventNotificationCtxt;

    pphHal4Nfc_TransceiveCallback_t pUpperRecvCb = NULL;
    pphHal4Nfc_SendCallback_t pUpperSendCb = NULL;
    phHal4Nfc_NotificationInfo_t uNotificationInfo;
    uNotificationInfo.psEventInfo = (phHal_sEventInfo_t *)pInfo;
    uint32_t handle=0;
    /*session is closed*/
    if(NULL != Hal4Ctxt->rem_dev_list[0])
    {
        if(Hal4Ctxt->rem_dev_list[0]->RemoteDevInfo.Iso14443_4_PCD_Info.buffer != NULL)
        {
          phOsalNfc_FreeMemory((void *)(Hal4Ctxt->rem_dev_list[0]->RemoteDevInfo.Iso14443_4_PCD_Info.buffer));
          Hal4Ctxt->rem_dev_list[0]->RemoteDevInfo.Iso14443_4_PCD_Info.buffer = NULL;
        }
        Hal4Ctxt->rem_dev_list[0]->RemoteDevInfo.Iso14443_4_PCD_Info.length = 0;
        Hal4Ctxt->rem_dev_list[0]->SessionOpened = FALSE;
        handle = (uint32_t)Hal4Ctxt->rem_dev_list[0];
        Hal4Ctxt->rem_dev_list[0] = NULL;
        Hal4Ctxt->psADDCtxtInfo->nbr_of_devices = 0;
    }
    Hal4Ctxt->sTgtConnectInfo.psConnectedDevice = NULL;
    gpphLibContext->Connected_handle = 0x0000;
    /*Update state*/
    Hal4Ctxt->Hal4CurrentState = eHal4StateOpenAndReady;
    Hal4Ctxt->Hal4NextState  = eHal4StateInvalid;
    Hal4Ctxt->sTgtConnectInfo.EmulationState = NFC_EVT_DEACTIVATED;
    /*If Trcv ctxt info is allocated ,free it here*/
    if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
    {
        if(PH_OSALNFC_INVALID_TIMER_ID !=
            Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId)
        {
            phOsalNfc_Timer_Stop(Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId);
            phOsalNfc_Timer_Delete(Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId);
        }
        pUpperRecvCb = (pphHal4Nfc_TransceiveCallback_t)Hal4Ctxt->psTrcvCtxtInfo->pP2PRecvCb;
        pUpperSendCb = Hal4Ctxt->psTrcvCtxtInfo->pP2PSendCb;
        /*Free Hal4 resources used by Target*/
        if (NULL != Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData.buffer)
        {
            phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo->
                sLowerRecvData.buffer);
        }
        if((NULL == Hal4Ctxt->sTgtConnectInfo.psConnectedDevice)
            && (NULL != Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData))
        {
            phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData);
        }
        phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo);
        Hal4Ctxt->psTrcvCtxtInfo = NULL;
    }
    /*if recv callback is pending*/
    if(NULL != pUpperRecvCb)
    {
        (*pUpperRecvCb)(
                        Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,
                        Hal4Ctxt->sTgtConnectInfo.psConnectedDevice,
                        NULL,
                        NFCSTATUS_DESELECTED
                        );
    }
    /*if send callback is pending*/
    else if(NULL != pUpperSendCb)
    {
        (*pUpperSendCb)(
                        Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,
                        NFCSTATUS_DESELECTED
                        );
    }
    /*if CENotification is registered*/
    else if(NULL != pLibContext->sCeContext.pCeListenerNtfCb)
    {
      if(uNotificationInfo.psEventInfo->eventSource==phHal_eISO14443_A_PICC)
      {
        (*pLibContext->sCeContext.pCeListenerNtfCb)(
                            pLibContext->sCeContext.pCeListenerCtxt,
                            phLibNfc_eCE_A_EvtDeActivated,
                            handle,
                            NFCSTATUS_SUCCESS);
      }
      else
      {
        (*pLibContext->sCeContext.pCeListenerNtfCb)(
                            pLibContext->sCeContext.pCeListenerCtxt,
                            phLibNfc_eCE_B_EvtDeActivated,
                            handle,
                            NFCSTATUS_SUCCESS);
      }
    }
}