void 
phLlcNfc_CreateTimers(void)
{
    uint8_t     index = 0;
    
    while (index < PH_LLCNFC_MAX_TIMER_USED)
    {
#ifdef LLC_TIMER_ENABLE
        gpphLlcNfc_Ctxt->s_timerinfo.timer_id[index] = 
                            phOsalNfc_Timer_Create();
#endif /* #ifdef LLC_TIMER_ENABLE */
        index++;
    }
    return;
}
/*******************************************************************************
 **
 ** Function         phNxpNciHal_TestMode_open
 **
 ** Description      It opens the physical connection with NFCC (pn547) and
 **                  creates required client thread for operation.
 **
 ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED.
 **
 *******************************************************************************/
NFCSTATUS phNxpNciHal_TestMode_open (void)
{
    /* Thread */
    pthread_t test_rx_thread;

    phOsalNfc_Config_t tOsalConfig;
    phTmlNfc_Config_t tTmlConfig;
    NFCSTATUS status = NFCSTATUS_SUCCESS;
    uint16_t read_len = 255;

    /* initialize trace level */
    phNxpLog_InitializeLogLevel();

    if (phNxpNciHal_init_monitor() == NULL)
    {
        NXPLOG_NCIHAL_E("Init monitor failed");
        return NFCSTATUS_FAILED;
    }

    CONCURRENCY_LOCK();

    memset(&tOsalConfig, 0x00, sizeof(tOsalConfig));
    memset(&tTmlConfig, 0x00, sizeof(tTmlConfig));

    gDrvCfg.nClientId = phDal4Nfc_msgget(0, 0600);
    gDrvCfg.nLinkType = ENUM_LINK_TYPE_I2C;/* For PN547 */
    tTmlConfig.pDevName = (int8_t *) "/dev/pn544";
    tOsalConfig.dwCallbackThreadId = (uint32_t) gDrvCfg.nClientId;
    tOsalConfig.pLogFile = NULL;
    tTmlConfig.dwGetMsgThreadId = (uint32_t) gDrvCfg.nClientId;
    nxpncihal_ctrl.gDrvCfg.nClientId = (uint32_t) gDrvCfg.nClientId;

    /* Initialize TML layer */
    status = phTmlNfc_Init(&tTmlConfig);
    if (status != NFCSTATUS_SUCCESS)
    {
        NXPLOG_NCIHAL_E("phTmlNfc_Init Failed");
        goto clean_and_return;
    }

    if (pthread_create(&test_rx_thread, NULL,
            phNxpNciHal_test_rx_thread, NULL) != 0)
    {
        NXPLOG_NCIHAL_E("pthread_create failed");
        phTmlNfc_Shutdown();
        goto clean_and_return;
    }

    timeoutTimerId = phOsalNfc_Timer_Create();

    if(timeoutTimerId == 0xFFFF)
    {
        NXPLOG_NCIHAL_E("phOsalNfc_Timer_Create failed");
    }
    else
    {
        NXPLOG_NCIHAL_D("phOsalNfc_Timer_Create SUCCESS");
    }
    CONCURRENCY_UNLOCK();

    return NFCSTATUS_SUCCESS;

clean_and_return:
    CONCURRENCY_UNLOCK();
    phNxpNciHal_cleanup_monitor();
    return NFCSTATUS_FAILED;
}
/*******************************************************************************
**
** Function         phNxpNciHal_NfcDep_rsp_ext
**
** Description      Implements algorithm for NFC-DEP protocol priority over
**                  ISO-DEP protocol.
**                  Following the algorithm:
**                  IF ISO-DEP detected first time,set the ISO-DEP detected flag
**                  and resume polling loop with 60ms timeout value.
**                      a) if than NFC-DEP detected than send the response to
**                       libnfc-nci stack and stop the timer.
**                      b) if NFC-DEP not detected with in 60ms, than restart the
**                          polling loop to give early chance to ISO-DEP with a
**                          cleanup timer.
**                      c) if ISO-DEP detected second time send the response to
**                          libnfc-nci stack and stop the cleanup timer.
**                      d) if ISO-DEP not detected with in cleanup timeout, than
**                          clear the ISO-DEP detection flag.
**
** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED
**
*******************************************************************************/
NFCSTATUS phNxpNciHal_NfcDep_rsp_ext(uint8_t *p_ntf, uint16_t *p_len)
{
    NFCSTATUS status = NFCSTATUS_INVALID_PARAMETER;

    NXPLOG_NCIHAL_D(">> p_ntf[0]=%02x , p_ntf[1]=%02x",p_ntf[0],p_ntf[1]);

    if(p_ntf[0] == 0x41 && p_ntf[1] == 0x04)
    {
        //Tag selected, Disable P2P Prio logic.
        bIgnoreIsoDep = 1;
        NXPLOG_NCIHAL_D(">> Tag selected, Disable P2P Prio logic.");

    }
    else if( ((p_ntf[0] == 0x61 && p_ntf[1] == 0x06) ||
            (p_ntf[0] == 0x41 && p_ntf[1] == 0x06) ) && bIgnoreIsoDep == 1
            )
    {
        //Tag deselected, enable P2P Prio logic.
        bIgnoreIsoDep = 0x00;
        NXPLOG_NCIHAL_D(">> Tag deselected, enable P2P Prio logic.");

    }
    if (bIgnoreIsoDep == 0x00 &&
            p_ntf[0] == 0x61 &&
            p_ntf[1] == 0x05 && *p_len > 5)
    {
        if (p_ntf[5] == 0x04 && p_ntf[6] < 0x80)
        {
            NXPLOG_NCIHAL_D(">> ISO DEP detected.");

            if (iso_dep_detected == 0x00)
            {
                NXPLOG_NCIHAL_D(
                        ">> ISO DEP detected first time. Resume polling loop");

                iso_dep_detected = 0x01;
                status = phNxpNciHal_resume_polling_loop();

                custom_poll_timer = phOsalNfc_Timer_Create();
                NXPLOG_NCIHAL_D("custom poll timer started - %d", custom_poll_timer);

                status = phOsalNfc_Timer_Start(custom_poll_timer,
                        CUSTOM_POLL_TIMEOUT,
                        &custom_poll_timer_handler,
                        NULL);

                if (NFCSTATUS_SUCCESS == status)
                {
                    NXPLOG_NCIHAL_D("custom poll timer started");
                }
                else
                {
                    NXPLOG_NCIHAL_E("custom poll timer not started!!!");
                    status  = NFCSTATUS_FAILED;
                }

                status = NFCSTATUS_FAILED;
            }
            else
            {
                NXPLOG_NCIHAL_D(">> ISO DEP detected second time.");
                /* Store notification */
                phNxpNciHal_NfcDep_store_ntf(p_ntf, *p_len);

                /* Stop Cleanup_timer */
                phOsalNfc_Timer_Stop(cleanup_timer);
                phOsalNfc_Timer_Delete(cleanup_timer);
                cleanup_timer=0;
                EnableP2P_PrioLogic = FALSE;
                iso_dep_detected = 0;
                status = NFCSTATUS_SUCCESS;
            }
        }
        else if (p_ntf[5] == 0x05)
        {
            NXPLOG_NCIHAL_D(">> NFC-DEP Detected - stopping the custom poll timer");

            phOsalNfc_Timer_Stop(custom_poll_timer);
            phOsalNfc_Timer_Delete(custom_poll_timer);
            EnableP2P_PrioLogic = FALSE;
            iso_dep_detected = 0;
            status = NFCSTATUS_SUCCESS;
        }
        else
        {
            NXPLOG_NCIHAL_D(">>  detected other technology- stopping the custom poll timer");
            phOsalNfc_Timer_Stop(custom_poll_timer);
            phOsalNfc_Timer_Delete(custom_poll_timer);
            EnableP2P_PrioLogic = FALSE;
            iso_dep_detected = 0;
            status = NFCSTATUS_INVALID_PARAMETER;
        }
    }
    else if( bIgnoreIsoDep == 0x00 &&
            ((p_ntf[0] == 0x41 && p_ntf[1] == 0x06) || (p_ntf[0] == 0x61
            && p_ntf[1] == 0x06))
            )
    {
        NXPLOG_NCIHAL_D(">> RF disabled");
        if (poll_timer_fired == 0x01)
        {
            poll_timer_fired = 0x00;

            NXPLOG_NCIHAL_D(">>restarting polling loop.");

            /* start polling loop */
            phNxpNciHal_start_polling_loop();
            EnableP2P_PrioLogic = FALSE;
            NXPLOG_NCIHAL_D (">> NFC DEP NOT  detected - custom poll timer expired - RF disabled");

            cleanup_timer = phOsalNfc_Timer_Create();

            /* Start cleanup_timer */
            NFCSTATUS status = phOsalNfc_Timer_Start(cleanup_timer,
                    CLEAN_UP_TIMEOUT,
                    &cleanup_timer_handler,
                    NULL);

            if (NFCSTATUS_SUCCESS == status)
            {
                NXPLOG_NCIHAL_D("cleanup timer started");
            }
            else
            {
                NXPLOG_NCIHAL_E("cleanup timer not started!!!");
                status  = NFCSTATUS_FAILED;
            }

            status = NFCSTATUS_FAILED;
        }
        else
        {
            status = NFCSTATUS_SUCCESS;
        }
    }
    if (bIgnoreIsoDep == 0x00 &&
            iso_dep_detected == 1)
    {
        if ((p_ntf[0] == 0x41 && p_ntf[1] == 0x06) || (p_ntf[0] == 0x61
                && p_ntf[1] == 0x06))
        {
            NXPLOG_NCIHAL_D(">>iso_dep_detected Disconnect related notification");
            status = NFCSTATUS_FAILED;
        }
        else
        {
            NXPLOG_NCIHAL_W("Never come here");
        }
    }

    return status;
}
static NFCSTATUS phNciNfc_ProcessInitRsp(void *pContext, NFCSTATUS Status)
{
    NFCSTATUS wStatus = Status;
    phNciNfc_sInitRspParams_t *pInitRsp = NULL;
    pphNciNfc_Context_t pNciContext = pContext;
    uint8_t Offset = 0;
    uint16_t wTemp = 0;

    PH_LOG_NCI_FUNC_ENTRY();
    if(NULL != pNciContext)
    {
        pInitRsp = &(pNciContext->InitRspParams);
        if (pNciContext->RspBuffInfo.wLen >= 19)
        {
            if (pNciContext->RspBuffInfo.pBuff[Offset++] == PH_NCINFC_STATUS_OK)
            {
                wStatus = NFCSTATUS_SUCCESS;

                /*NFCC Features*/
                pInitRsp->NfccFeatures.DiscConfSuprt =  pNciContext->RspBuffInfo.pBuff[Offset++];
                pInitRsp->NfccFeatures.RoutingType =  pNciContext->RspBuffInfo.pBuff[Offset++];
                pInitRsp->NfccFeatures.PwrOffState =  pNciContext->RspBuffInfo.pBuff[Offset++];
                pInitRsp->NfccFeatures.Byte3 =  pNciContext->RspBuffInfo.pBuff[Offset++];

                /*number of supported RF interfaces*/
                pInitRsp->NoOfRfIfSuprt = pNciContext->RspBuffInfo.pBuff[Offset++];

                if(pInitRsp->NoOfRfIfSuprt <= PH_NCINFC_CORE_MAX_SUP_RF_INTFS)
                {
                    /*Supported RF Interfaces */
                    phOsalNfc_MemCopy(pInitRsp->RfInterfaces, &(pNciContext->RspBuffInfo.pBuff[Offset]),
                                                                pInitRsp->NoOfRfIfSuprt);
                    Offset += pInitRsp->NoOfRfIfSuprt;

                    /*Max No of Logical Connection supported*/
                    pInitRsp->MaxLogicalCon = pNciContext->RspBuffInfo.pBuff[Offset++];

                    /*Routing Table Size*/
                    pInitRsp->RoutingTableSize = pNciContext->RspBuffInfo.pBuff[Offset++]; /*LSB*/
                    wTemp = pNciContext->RspBuffInfo.pBuff[Offset++];
                    pInitRsp->RoutingTableSize = (pInitRsp->RoutingTableSize) | (wTemp << 8);

                    /*Control Packet Payload Length*/
                    pInitRsp->CntrlPktPayloadLen = pNciContext->RspBuffInfo.pBuff[Offset++];

                    /*Max Size For Large parameter*/
                    pInitRsp->MaxSizeLarge = pNciContext->RspBuffInfo.pBuff[Offset++]; /*LSB*/
                    wTemp = (uint16_t)pNciContext->RspBuffInfo.pBuff[Offset++];
                    pInitRsp->MaxSizeLarge = (pInitRsp->MaxSizeLarge) | (wTemp << 8);

                    /*Manufacturer ID*/
                    pInitRsp->ManufacturerId = pNciContext->RspBuffInfo.pBuff[Offset++];
                    if(pInitRsp->ManufacturerId != 0x00)
                    {
                        /*Decided by Manufacturer*/
                        pInitRsp->ManufacturerInfo.Byte0 = pNciContext->RspBuffInfo.pBuff[Offset++];
                        pInitRsp->ManufacturerInfo.Byte1 = pNciContext->RspBuffInfo.pBuff[Offset++];
                        pInitRsp->ManufacturerInfo.Byte2 = pNciContext->RspBuffInfo.pBuff[Offset++];
                        pInitRsp->ManufacturerInfo.Byte3 = pNciContext->RspBuffInfo.pBuff[Offset++];
                    }

                    wStatus = phNciNfc_CoreIfSetMaxCtrlPacketSize(&(pNciContext->NciCoreContext),
                                                                     pInitRsp->CntrlPktPayloadLen);
                }
                else
                {
                    PH_LOG_NCI_CRIT_STR("Invalid number of supported Rf interfaces");
                    wStatus = NFCSTATUS_FAILED;
                }
            }
            else
            {
                wStatus = NFCSTATUS_FAILED;
            }
        }
        else
        {
            wStatus = NFCSTATUS_FAILED;
        }

        if((NFCSTATUS_SUCCESS == wStatus) &&
            (0 == pNciContext->tInitInfo.bSkipRegisterAllNtfs))
        {
            wStatus = phNciNfc_RegAllNtfs(pNciContext);
            if(NFCSTATUS_SUCCESS == wStatus)
            {
                pNciContext->dwNtfTimerId = phOsalNfc_Timer_Create();
                if(PH_OSALNFC_TIMER_ID_INVALID == pNciContext->dwNtfTimerId)
                {
                    PH_LOG_NCI_CRIT_STR("Notification Timer Create failed!!");
                    wStatus = NFCSTATUS_INSUFFICIENT_RESOURCES;
                }
                else
                {
                    PH_LOG_NCI_INFO_STR("Notification Timer Created Successfully");
                }
            }
        }
    }
    else
    {
        wStatus = NFCSTATUS_INVALID_PARAMETER;
    }
    PH_LOG_NCI_FUNC_EXIT();
    return wStatus;
}
NFCSTATUS phNciNfc_RegForConnCredits(
                                     uint8_t bConnId,
                                     pphNciNfc_ConnCreditsNtf_t pNotify,
                                     void *pContext,
                                     uint32_t CreditTo
                                     )
{
    NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
    uint8_t     bConnListIdx;
    uint32_t TimerId;

    PH_LOG_NCI_FUNC_ENTRY();

    if((NULL == pNotify) || (NULL == pContext))
    {
        wStatus = PHNFCSTVAL(CID_NFC_NCI, NFCSTATUS_INVALID_PARAMETER);
        PH_LOG_NCI_INFO_STR(" Invalid Caller Layer Param(s) received ..");
    }
    else
    {
        for(bConnListIdx = 0; bConnListIdx < (MAX_LOGICAL_CONNS+1) ; bConnListIdx++)
        {
            if(bConnId == gphNciNfc_ConnMgmtInt.tConnInfo.tConnList[bConnListIdx].tConn.bConnId)
            {
                if((TRUE == gphNciNfc_ConnMgmtInt.tConnInfo.tConnList[bConnListIdx].bIfActive) ||
                    (CONNTYPE_STATIC == gphNciNfc_ConnMgmtInt.tConnInfo.tConnList[bConnListIdx].tConn.bConnId))
                {
                    wStatus = NFCSTATUS_SUCCESS;
                }
                else
                {
                    PH_LOG_NCI_CRIT_STR(" Interface not active for this connection");
                    wStatus = NFCSTATUS_FAILED;
                }
                break;
            }
        }

        if(NFCSTATUS_SUCCESS == wStatus)
        {
            gphNciNfc_ConnMgmtInt.bConnId = bConnId;
            gphNciNfc_ConnMgmtInt.pCrdtsNotify = pNotify;
            gphNciNfc_ConnMgmtInt.pContext = pContext;

            TimerId = phOsalNfc_Timer_Create();

            if (0 == TimerId)
            {
                PH_LOG_NCI_WARN_STR("Credit wait Timer Create failed!!");
                wStatus = NFCSTATUS_INSUFFICIENT_RESOURCES;
            }
            else
            {
                PH_LOG_NCI_INFO_STR("Credit wait Timer Created Successfully");

                gphNciNfc_ConnMgmtInt.tCWTimerIf.dwTimerId = TimerId;
                gphNciNfc_ConnMgmtInt.tCWTimerIf.TimerStatus = 0;

                PH_LOG_NCI_INFO_STR("Credit timeout set to %d, for ConnId 0x%x",CreditTo, bConnId);

                wStatus = phOsalNfc_Timer_Start(gphNciNfc_ConnMgmtInt.tCWTimerIf.dwTimerId,
                                                CreditTo,
                                                &phNciNfc_WaitCreditTimerCb,
                                                &gphNciNfc_ConnMgmtInt);

                if (NFCSTATUS_SUCCESS == wStatus)
                {
                    PH_LOG_NCI_INFO_STR("Credit wait timer started..");
                    gphNciNfc_ConnMgmtInt.tCWTimerIf.TimerStatus = 1;
                    /* Set the CreditsAwaited flag to indicate credit notif function to call the registered CB */
                    gphNciNfc_ConnMgmtInt.bCreditsAwaited = TRUE;
                }
                else
                {
                     PH_LOG_NCI_WARN_STR("Failed to start credit ntf wait timer!");
                     (void )phOsalNfc_Timer_Delete(gphNciNfc_ConnMgmtInt.tCWTimerIf.dwTimerId);
                     gphNciNfc_ConnMgmtInt.tCWTimerIf.dwTimerId = 0;
                     wStatus = NFCSTATUS_FAILED;
                }
            }
        }
    }

    PH_LOG_NCI_FUNC_EXIT();

    return wStatus;
}
NFCSTATUS 
phHal4Nfc_Receive(                
                  phHal_sHwReference_t                  *psHwReference,
                  phHal4Nfc_TransactInfo_t              *psRecvInfo,
                  pphHal4Nfc_ReceiveCallback_t          pReceiveCallback,
                  void                                  *pContext
                 )
{
    NFCSTATUS RetStatus = NFCSTATUS_PENDING;
    phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
     /*NULL checks*/
    if((NULL == psHwReference) 
        ||( NULL == pReceiveCallback)
        ||( NULL == psRecvInfo))
    {
        phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
        RetStatus = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_INVALID_PARAMETER);
    }
    /*Check initialised state*/
    else if((NULL == psHwReference->hal_context)
                        || (((phHal4Nfc_Hal4Ctxt_t *)
                                psHwReference->hal_context)->Hal4CurrentState 
                                               < eHal4StateOpenAndReady)
                        || (((phHal4Nfc_Hal4Ctxt_t *)
                                psHwReference->hal_context)->Hal4NextState 
                                               == eHal4StateClosed))
    {
        RetStatus = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_NOT_INITIALISED);     
    }   
    else
    {
        Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)psHwReference->hal_context;
        if(NFC_EVT_ACTIVATED == Hal4Ctxt->sTgtConnectInfo.EmulationState)
        {
            /*Following condition gets satisfied only on target side,if receive
              is not already called*/
            if(NULL == Hal4Ctxt->psTrcvCtxtInfo)
            {
                Hal4Ctxt->psTrcvCtxtInfo= (pphHal4Nfc_TrcvCtxtInfo_t)
                    phOsalNfc_GetMemory((uint32_t)
                    (sizeof(phHal4Nfc_TrcvCtxtInfo_t)));
                if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
                {
                    (void)memset(Hal4Ctxt->psTrcvCtxtInfo,0,
                        sizeof(phHal4Nfc_TrcvCtxtInfo_t));
                    Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
                        = PH_OSALNFC_INVALID_TIMER_ID;
                    Hal4Ctxt->psTrcvCtxtInfo->RecvDataBufferStatus = NFCSTATUS_PENDING;
                }
            }
            if(NULL == Hal4Ctxt->psTrcvCtxtInfo)
            {
                phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,0);
                RetStatus= PHNFCSTVAL(CID_NFC_HAL , 
                    NFCSTATUS_INSUFFICIENT_RESOURCES);
            }
            else /*Store callback & Return status pending*/
            {
                Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt = pContext;     
                /*Register upper layer callback*/
                Hal4Ctxt->psTrcvCtxtInfo->pUpperTranceiveCb = NULL;
                Hal4Ctxt->psTrcvCtxtInfo->pP2PRecvCb = pReceiveCallback;
                if(NFCSTATUS_PENDING != 
                    Hal4Ctxt->psTrcvCtxtInfo->RecvDataBufferStatus)
                {               
                    /**Create a timer to send received data in the callback*/
                    if(Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
                        == PH_OSALNFC_INVALID_TIMER_ID)
                    {
                        PHDBG_INFO("HAL4: Transaction Timer Create for Receive");
                        Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId 
                            = phOsalNfc_Timer_Create();
                    }
                    if(Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
                        == PH_OSALNFC_INVALID_TIMER_ID)
                    {
                        RetStatus = PHNFCSTVAL(CID_NFC_HAL ,
                            NFCSTATUS_INSUFFICIENT_RESOURCES);                      
                    }
                    else/*start the timer*/
                    {
                        phOsalNfc_Timer_Start(
                            Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId,
                            PH_HAL4NFC_RECV_CB_TIMEOUT,
							 phHal4Nfc_P2PRecvTimerCb,
							 NULL
                            );
                    }
                }
            }
        }
        else/*deactivated*/
        {
            RetStatus= PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_DESELECTED);
        }
    }   
    return RetStatus;
}
/*******************************************************************************
**
** Function         phTmlNfc_Init
**
** Description      Provides initialization of TML layer and hardware interface
**                  Configures given hardware interface and sends handle to the caller
**
** Parameters       pConfig     - TML configuration details as provided by the upper layer
**
** Returns          NFC status:
**                  NFCSTATUS_SUCCESS            - initialization successful
**                  NFCSTATUS_INVALID_PARAMETER  - at least one parameter is invalid
**                  NFCSTATUS_FAILED             - initialization failed
**                                                 (for example, unable to open hardware interface)
**                  NFCSTATUS_INVALID_DEVICE     - device has not been opened or has been disconnected
**
*******************************************************************************/
NFCSTATUS phTmlNfc_Init(pphTmlNfc_Config_t pConfig)
{
    NFCSTATUS wInitStatus = NFCSTATUS_SUCCESS;
    NFCSTATUS setPidStatus = NFCSTATUS_SUCCESS;

    /* Check if TML layer is already Initialized */
    if (NULL != gpphTmlNfc_Context)
    {
        /* TML initialization is already completed */
        wInitStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_ALREADY_INITIALISED);
    }
    /* Validate Input parameters */
    else if ((NULL == pConfig)  ||
            (PH_TMLNFC_RESET_VALUE == pConfig->dwGetMsgThreadId))
    {
        /*Parameters passed to TML init are wrong */
        wInitStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {
        /* Allocate memory for TML context */
        gpphTmlNfc_Context = malloc(sizeof(phTmlNfc_Context_t));

        if (NULL == gpphTmlNfc_Context)
        {
            wInitStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_FAILED);
        }
        else
        {
            /* Initialise all the internal TML variables */
            memset(gpphTmlNfc_Context, PH_TMLNFC_RESET_VALUE, sizeof(phTmlNfc_Context_t));
            /* Make sure that the thread runs once it is created */
            gpphTmlNfc_Context->bThreadDone = 1;

            /* Open the device file to which data is read/written */
            wInitStatus = phTmlNfc_i2c_open_and_configure(pConfig, &(gpphTmlNfc_Context->pDevHandle));

            if (NFCSTATUS_SUCCESS != wInitStatus)
            {
                wInitStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_INVALID_DEVICE);
                gpphTmlNfc_Context->pDevHandle = (void *) NFCSTATUS_INVALID_DEVICE;
            }
            else
            {
                gpphTmlNfc_Context->tReadInfo.bEnable = 0;
                gpphTmlNfc_Context->tWriteInfo.bEnable = 0;
                gpphTmlNfc_Context->tReadInfo.bThreadBusy = FALSE;
                gpphTmlNfc_Context->tWriteInfo.bThreadBusy = FALSE;

                if(0 != sem_init(&gpphTmlNfc_Context->rxSemaphore, 0, 0))
                {
                    wInitStatus = NFCSTATUS_FAILED;
                }
                else if(0 != sem_init(&gpphTmlNfc_Context->txSemaphore, 0, 0))
                {
                    wInitStatus = NFCSTATUS_FAILED;
                }
                else if(0 != sem_init(&gpphTmlNfc_Context->postMsgSemaphore, 0, 0))
                {
                    wInitStatus = NFCSTATUS_FAILED;
                }
                else
                {
                    sem_post(&gpphTmlNfc_Context->postMsgSemaphore);
                    /* Start TML thread (to handle write and read operations) */
                    if (NFCSTATUS_SUCCESS != phTmlNfc_StartThread())
                    {
                        wInitStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_FAILED);
                    }
                    else
                    {
                        /* Create Timer used for Retransmission of NCI packets */
                        gpphTmlNfc_Context->dwTimerId = phOsalNfc_Timer_Create();
                        if (PH_OSALNFC_TIMER_ID_INVALID != gpphTmlNfc_Context->dwTimerId)
                        {
                            /* Store the Thread Identifier to which Message is to be posted */
                            gpphTmlNfc_Context->dwCallbackThreadId = pConfig->dwGetMsgThreadId;
                            /* Enable retransmission of Nci packet & set retry count to default */
                            gpphTmlNfc_Context->eConfig = phTmlNfc_e_DisableRetrans;
                            /** Retry Count = Standby Recovery time of NFCC / Retransmission time + 1 */
                            gpphTmlNfc_Context->bRetryCount = (2000 / PHTMLNFC_MAXTIME_RETRANSMIT) + 1;
                            gpphTmlNfc_Context->bWriteCbInvoked = FALSE;
                        }
                        else
                        {
                            wInitStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_FAILED);
                        }
                    }
                }
            }
        }
    }
    /* Clean up all the TML resources if any error */
    if (NFCSTATUS_SUCCESS != wInitStatus)
    {
        /* Clear all handles and memory locations initialized during init */
        phTmlNfc_CleanUp();
    }
#if(NFC_NXP_ESE == TRUE)
    else
    {
        nfc_service_pid = getpid();
        setPidStatus = phTmlNfc_IoCtl(phTmlNfc_e_SetNfcServicePid);
        if(setPidStatus == NFCSTATUS_SUCCESS)
       {
           NXPLOG_TML_D("nfc service set pid done");
       }
       else
       {
           NXPLOG_TML_D("nfc service set pid failed");
       }
    }
#endif

    return wInitStatus;
}