/*******************************************************************************
**
** Function         nfa_p2p_evt_hdlr
**
** Description      Processing event for NFA P2P
**
**
** Returns          TRUE if p_msg needs to be deallocated
**
*******************************************************************************/
static BOOLEAN nfa_p2p_evt_hdlr (BT_HDR *p_hdr)
{
    BOOLEAN delete_msg = TRUE;
    UINT16  event;

    tNFA_P2P_MSG *p_msg = (tNFA_P2P_MSG *)p_hdr;

#if (BT_TRACE_VERBOSE == TRUE)
    P2P_TRACE_DEBUG2 ("nfa_p2p_evt_hdlr (): LLCP State [%s], Event [%s]",
                       nfa_p2p_llcp_state_code (nfa_p2p_cb.llcp_state),
                       nfa_p2p_evt_code (p_msg->hdr.event));
#else
    P2P_TRACE_DEBUG2 ("nfa_p2p_evt_hdlr (): State 0x%02x, Event 0x%02x",
                       nfa_p2p_cb.llcp_state, p_msg->hdr.event);
#endif

    event = p_msg->hdr.event & 0x00ff;

    /* execute action functions */
    if (event < NFA_P2P_NUM_ACTIONS)
    {
        delete_msg = (*nfa_p2p_action[event]) (p_msg);
    }
    else
    {
        P2P_TRACE_ERROR0 ("Unhandled event");
    }

    return delete_msg;
}
/*******************************************************************************
**
** Function         nfa_p2p_disable_listening
**
** Description      Remove listen technologies and protocols for LLCP and
**                  deregister callback from NFA DM discovery if all of P2P/CHO/SNEP
**                  doesn't listen LLCP any more.
**                  If LLCP WKS is changed then ATR_RES will be updated.
**
** Returns          void
**
*******************************************************************************/
void nfa_p2p_disable_listening (tNFA_SYS_ID sys_id, BOOLEAN update_wks)
{

    P2P_TRACE_DEBUG2 ("nfa_p2p_disable_listening ()  sys_id = %d, update_wks = %d",
                       sys_id, update_wks);

    if (sys_id == NFA_ID_P2P)
        nfa_p2p_cb.is_p2p_listening = FALSE;
    else if (sys_id == NFA_ID_CHO)
        nfa_p2p_cb.is_cho_listening = FALSE;
    else if (sys_id == NFA_ID_SNEP)
        nfa_p2p_cb.is_snep_listening = FALSE;

    if (nfa_p2p_cb.dm_disc_handle != NFA_HANDLE_INVALID)
    {
        if (  (nfa_p2p_cb.is_p2p_listening == FALSE)
            &&(nfa_p2p_cb.is_cho_listening == FALSE)
            &&(nfa_p2p_cb.is_snep_listening == FALSE)  )
        {
            nfa_p2p_cb.llcp_state    = NFA_P2P_LLCP_STATE_IDLE;
            nfa_p2p_cb.rf_disc_state = NFA_DM_RFST_IDLE;

            nfa_dm_delete_rf_discover (nfa_p2p_cb.dm_disc_handle);
            nfa_p2p_cb.dm_disc_handle = NFA_HANDLE_INVALID;
        }
        else if (update_wks)
        {
            /* update LLCP Gen Bytes */
            nfa_p2p_set_config (NFA_DM_DISC_MASK_PA_NFC_DEP|NFA_DM_DISC_MASK_LA_NFC_DEP);
        }
    }
}
/*******************************************************************************
**
** Function         nfa_p2p_enable_listening
**
** Description      Configure listen technologies and protocols for LLCP
**                  If LLCP WKS is changed then LLCP Gen bytes will be updated.
**
** Returns          void
**
*******************************************************************************/
void nfa_p2p_enable_listening (tNFA_SYS_ID sys_id, BOOLEAN update_wks)
{
    tNFA_DM_DISC_TECH_PROTO_MASK p2p_listen_mask = 0;

    P2P_TRACE_DEBUG2 ("nfa_p2p_enable_listening () sys_id = %d, update_wks = %d",
                       sys_id, update_wks);

    if (sys_id == NFA_ID_P2P)
        nfa_p2p_cb.is_p2p_listening = TRUE;
    else if (sys_id == NFA_ID_CHO)
        nfa_p2p_cb.is_cho_listening = TRUE;
    else if (sys_id == NFA_ID_SNEP)
        nfa_p2p_cb.is_snep_listening = TRUE;

    if (nfa_p2p_cb.dm_disc_handle != NFA_HANDLE_INVALID)
    {
        /* if need to update WKS in LLCP Gen bytes */
        if (update_wks)
        {
            /* update LLCP Gen Bytes */
            nfa_p2p_set_config (NFA_DM_DISC_MASK_PA_NFC_DEP|NFA_DM_DISC_MASK_LA_NFC_DEP);
        }
        return;
    }

    /* collect listen technologies with NFC-DEP protocol */
    if (nfa_p2p_cb.listen_tech_mask & NFA_TECHNOLOGY_MASK_A)
        p2p_listen_mask |= NFA_DM_DISC_MASK_LA_NFC_DEP;

    if (nfa_p2p_cb.listen_tech_mask & NFA_TECHNOLOGY_MASK_F)
        p2p_listen_mask |= NFA_DM_DISC_MASK_LF_NFC_DEP;

    if (nfa_p2p_cb.listen_tech_mask & NFA_TECHNOLOGY_MASK_A_ACTIVE)
        p2p_listen_mask |= NFA_DM_DISC_MASK_LAA_NFC_DEP;

    if (nfa_p2p_cb.listen_tech_mask & NFA_TECHNOLOGY_MASK_F_ACTIVE)
        p2p_listen_mask |= NFA_DM_DISC_MASK_LFA_NFC_DEP;

    if (p2p_listen_mask)
    {
        /* Configure listen technologies and protocols and register callback to NFA DM discovery */
        nfa_p2p_cb.dm_disc_handle = nfa_dm_add_rf_discover (p2p_listen_mask,
                                                            NFA_DM_DISC_HOST_ID_DH,
                                                            nfa_p2p_discovery_cback);
    }
}
/*******************************************************************************
**
** Function         nfa_p2p_llcp_link_cback
**
** Description      Processing event from LLCP link management callback
**
**
** Returns          None
**
*******************************************************************************/
void nfa_p2p_llcp_link_cback (UINT8 event, UINT8 reason)
{
    tNFA_LLCP_ACTIVATED     llcp_activated;
    tNFA_LLCP_DEACTIVATED   llcp_deactivated;

    P2P_TRACE_DEBUG2 ("nfa_p2p_llcp_link_cback () event:0x%x, reason:0x%x", event, reason);

    if (event == LLCP_LINK_ACTIVATION_COMPLETE_EVT)
    {
        LLCP_GetLinkMIU (&nfa_p2p_cb.local_link_miu, &nfa_p2p_cb.remote_link_miu);
        nfa_p2p_cb.llcp_state = NFA_P2P_LLCP_STATE_ACTIVATED;

        if (nfa_p2p_cb.is_initiator)
        {
            /* notify NFA DM to send Activate Event to applicaiton with status  */
            nfa_dm_notify_activation_status (NFA_STATUS_OK, NULL);
        }

        llcp_activated.is_initiator    = nfa_p2p_cb.is_initiator;
        llcp_activated.local_link_miu  = nfa_p2p_cb.local_link_miu;
        llcp_activated.remote_link_miu = nfa_p2p_cb.remote_link_miu;
        llcp_activated.remote_lsc      = LLCP_GetRemoteLSC ();
        llcp_activated.remote_wks      = LLCP_GetRemoteWKS ();

        nfa_dm_act_conn_cback_notify (NFA_LLCP_ACTIVATED_EVT, (tNFA_CONN_EVT_DATA *) &llcp_activated);

    }
    else if (event == LLCP_LINK_ACTIVATION_FAILED_EVT)
    {
        nfa_p2p_cb.llcp_state = NFA_P2P_LLCP_STATE_IDLE;

        if (nfa_p2p_cb.is_initiator)
        {
            /* notify NFA DM to send Activate Event to applicaiton with status  */
            nfa_dm_notify_activation_status (NFA_STATUS_FAILED, NULL);
        }

        nfa_dm_rf_deactivate (NFA_DEACTIVATE_TYPE_DISCOVERY);
    }
    else if (event == LLCP_LINK_FIRST_PACKET_RECEIVED_EVT)
    {
        nfa_dm_act_conn_cback_notify (NFA_LLCP_FIRST_PACKET_RECEIVED_EVT, NULL);
    }
    else /* LLCP_LINK_DEACTIVATED_EVT       */
    {
        nfa_p2p_cb.llcp_state = NFA_P2P_LLCP_STATE_IDLE;

        /* if got RF link loss without any rx LLC PDU */
        if (reason == LLCP_LINK_RF_LINK_LOSS_NO_RX_LLC)
        {
            /* if it was active listen mode */
            if (  (nfa_p2p_cb.is_active_mode)
                &&(!nfa_p2p_cb.is_initiator))
            {
                /* if it didn't retry without active listen mode and passive mode is available */
                if (  (nfa_p2p_cb.listen_tech_mask_to_restore == 0x00)
                    &&(nfa_p2p_cb.listen_tech_mask & ( NFA_TECHNOLOGY_MASK_A
                                                      |NFA_TECHNOLOGY_MASK_F)))
                {
                    P2P_TRACE_DEBUG0 ("Retry without active listen mode");

                    /* retry without active listen mode */
                    nfa_p2p_update_active_listen ();
                }
            }
            else if (nfa_p2p_cb.listen_tech_mask_to_restore)
            {
                nfa_sys_start_timer (&nfa_p2p_cb.active_listen_restore_timer, 0, NFA_P2P_RESTORE_ACTIVE_LISTEN_TIMEOUT);
            }

            reason = LLCP_LINK_RF_LINK_LOSS_ERR;
        }
        else
        {
            if (nfa_p2p_cb.listen_tech_mask_to_restore)
            {
                /* restore active listen mode */
                nfa_p2p_update_active_listen ();
            }
        }

        llcp_deactivated.reason = reason;
        nfa_dm_act_conn_cback_notify (NFA_LLCP_DEACTIVATED_EVT, (tNFA_CONN_EVT_DATA *)&llcp_deactivated);

        if (reason != LLCP_LINK_RF_LINK_LOSS_ERR) /* if NFC link is still up */
        {
            if (nfa_p2p_cb.is_initiator)
            {
                nfa_dm_rf_deactivate (NFA_DEACTIVATE_TYPE_DISCOVERY);
            }
            else if ((nfa_p2p_cb.is_active_mode) && (reason == LLCP_LINK_TIMEOUT))
            {
                /*
                ** target needs to trun off RF in case of receiving invalid frame from initiator
                */
                P2P_TRACE_DEBUG0 ("Got LLCP_LINK_TIMEOUT in active mode on target");
                nfa_dm_rf_deactivate (NFA_DEACTIVATE_TYPE_DISCOVERY);
            }
        }
    }
}
/*******************************************************************************
**
** Function         nfa_p2p_enable_listening
**
** Description      Configure listen technologies and protocols for LLCP
**                  If LLCP WKS is changed then LLCP Gen bytes will be updated.
**
** Returns          void
**
*******************************************************************************/
void nfa_p2p_enable_listening (tNFA_SYS_ID sys_id, BOOLEAN update_wks)
{
    tNFA_DM_DISC_TECH_PROTO_MASK p2p_listen_mask = 0;

    P2P_TRACE_DEBUG2 ("nfa_p2p_enable_listening () sys_id = %d, update_wks = %d",
                       sys_id, update_wks);

    if (sys_id == NFA_ID_P2P)
        nfa_p2p_cb.is_p2p_listening = TRUE;
    else if (sys_id == NFA_ID_CHO)
        nfa_p2p_cb.is_cho_listening = TRUE;
    else if (sys_id == NFA_ID_SNEP)
        nfa_p2p_cb.is_snep_listening = TRUE;

    if (nfa_p2p_cb.dm_disc_handle != NFA_HANDLE_INVALID)
    {
        /* if need to update WKS in LLCP Gen bytes */
        if (update_wks)
        {
            /* update LLCP Gen Bytes */
            nfa_p2p_set_config (NFA_DM_DISC_MASK_PA_NFC_DEP|NFA_DM_DISC_MASK_LA_NFC_DEP);
        }
        return;
    }

    /* collect listen technologies with NFC-DEP protocol */
    if (nfa_p2p_cb.listen_tech_mask & NFA_TECHNOLOGY_MASK_A)
        p2p_listen_mask |= NFA_DM_DISC_MASK_LA_NFC_DEP;

    if (nfa_p2p_cb.listen_tech_mask & NFA_TECHNOLOGY_MASK_F)
        p2p_listen_mask |= NFA_DM_DISC_MASK_LF_NFC_DEP;

    if (nfa_p2p_cb.listen_tech_mask & NFA_TECHNOLOGY_MASK_A_ACTIVE)
        p2p_listen_mask |= NFA_DM_DISC_MASK_LAA_NFC_DEP;

    if (nfa_p2p_cb.listen_tech_mask & NFA_TECHNOLOGY_MASK_F_ACTIVE)
        p2p_listen_mask |= NFA_DM_DISC_MASK_LFA_NFC_DEP;

    if (p2p_listen_mask)
    {
#if(NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
        /*For P2P mode(Default DTA mode) open Raw channel to bypass LLCP layer. For LLCP DTA mode activate LLCP
         * Bypassing LLCP is handled in nfa_dm_poll_disc_cback*/
        if ((appl_dta_mode_flag == 1) && (nfa_dm_cb.eDtaMode == NFA_DTA_DEFAULT_MODE))
        {
            /* Configure listen technologies and protocols and register callback to NFA DM discovery */
            P2P_TRACE_DEBUG0 ("DTA mode2:Registering nfa_dm_poll_disc_cback to avoid LLCP in P2P ");
            nfa_p2p_cb.dm_disc_handle = nfa_dm_add_rf_discover (p2p_listen_mask,
                                                                NFA_DM_DISC_HOST_ID_DH,
                                                                nfa_dm_poll_disc_cback_dta_wrapper);
        }
        else
#endif
        {
            /* Configure listen technologies and protocols and register callback to NFA DM discovery */
            nfa_p2p_cb.dm_disc_handle = nfa_dm_add_rf_discover (p2p_listen_mask,
                                                                NFA_DM_DISC_HOST_ID_DH,
                                                                nfa_p2p_discovery_cback);
        }

    }
}