/*******************************************************************************
**
** Function         nfa_p2p_update_listen_tech
**
** Description      Update P2P listen technologies. If there is change then
**                  restart or stop P2P listen.
**
** Returns          void
**
*******************************************************************************/
void nfa_p2p_update_listen_tech (tNFA_TECHNOLOGY_MASK tech_mask)
{
    P2P_TRACE_DEBUG1 ("nfa_p2p_update_listen_tech ()  tech_mask = 0x%x", tech_mask);

    if (nfa_p2p_cb.listen_tech_mask_to_restore)
    {
        nfa_p2p_cb.listen_tech_mask_to_restore = 0;
        nfa_sys_stop_timer (&nfa_p2p_cb.active_listen_restore_timer);
    }

    if (nfa_p2p_cb.listen_tech_mask != tech_mask)
    {
        nfa_p2p_cb.listen_tech_mask = tech_mask;

        if (nfa_p2p_cb.dm_disc_handle != NFA_HANDLE_INVALID)
        {
            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;
        }

        /* restart discovery without updating sub-module status */
        if (nfa_p2p_cb.is_p2p_listening)
            nfa_p2p_enable_listening (NFA_ID_P2P, FALSE);
        else if (nfa_p2p_cb.is_cho_listening)
            nfa_p2p_enable_listening (NFA_ID_CHO, FALSE);
        else if (nfa_p2p_cb.is_snep_listening)
            nfa_p2p_enable_listening (NFA_ID_SNEP, FALSE);
    }
}
/*******************************************************************************
**
** Function         nfa_p2p_update_active_listen
**
** Description      Remove active listen mode temporarily or restore it
**
**
** Returns          None
**
*******************************************************************************/
static void nfa_p2p_update_active_listen (void)
{
    tNFA_DM_DISC_TECH_PROTO_MASK p2p_listen_mask = 0;
    BT_HDR *p_msg;

    P2P_TRACE_DEBUG1 ("nfa_p2p_update_active_listen (): listen_tech_mask_to_restore:0x%x",
                       nfa_p2p_cb.listen_tech_mask_to_restore);

    /* if active listen mode was removed temporarily */
    if (nfa_p2p_cb.listen_tech_mask_to_restore)
    {
        /* restore listen technologies */
        nfa_p2p_cb.listen_tech_mask = nfa_p2p_cb.listen_tech_mask_to_restore;
        nfa_p2p_cb.listen_tech_mask_to_restore = 0;
        nfa_sys_stop_timer (&nfa_p2p_cb.active_listen_restore_timer);
    }
    else
    {
        /* start timer in case of no passive activation */
        nfa_p2p_cb.active_listen_restore_timer.p_cback = (TIMER_CBACK *)nfa_p2p_update_active_listen_timeout_cback;
        nfa_sys_start_timer (&nfa_p2p_cb.active_listen_restore_timer, 0, NFA_P2P_RESTORE_ACTIVE_LISTEN_TIMEOUT);

        /* save listen techonologies */
        nfa_p2p_cb.listen_tech_mask_to_restore = nfa_p2p_cb.listen_tech_mask;

        /* remove active listen mode */
        nfa_p2p_cb.listen_tech_mask &= ~( NFA_TECHNOLOGY_MASK_A_ACTIVE|NFA_TECHNOLOGY_MASK_F_ACTIVE);
    }

    if (nfa_p2p_cb.dm_disc_handle != NFA_HANDLE_INVALID)
    {
        nfa_dm_delete_rf_discover (nfa_p2p_cb.dm_disc_handle);
        nfa_p2p_cb.dm_disc_handle = NFA_HANDLE_INVALID;
    }

    /* 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;

    /* 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);

    /* restart RF discovery to update RF technologies */
    if ((p_msg = (BT_HDR *) GKI_getbuf (sizeof(BT_HDR))) != NULL)
    {
        p_msg->event = NFA_P2P_INT_RESTART_RF_DISC_EVT;
        nfa_sys_sendmsg (p_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_update_active_listen
**
** Description      Remove active listen mode temporarily or restore it
**
**
** Returns          None
**
*******************************************************************************/
static void nfa_p2p_update_active_listen (void)
{
    tNFA_DM_DISC_TECH_PROTO_MASK p2p_listen_mask = 0;
    BT_HDR *p_msg;

    P2P_TRACE_DEBUG1 ("nfa_p2p_update_active_listen (): listen_tech_mask_to_restore:0x%x",
                       nfa_p2p_cb.listen_tech_mask_to_restore);

    /* if active listen mode was removed temporarily */
    if (nfa_p2p_cb.listen_tech_mask_to_restore)
    {
        /* restore listen technologies */
        nfa_p2p_cb.listen_tech_mask = nfa_p2p_cb.listen_tech_mask_to_restore;
        nfa_p2p_cb.listen_tech_mask_to_restore = 0;
        nfa_sys_stop_timer (&nfa_p2p_cb.active_listen_restore_timer);
    }
    else
    {
        /* start timer in case of no passive activation */
        nfa_p2p_cb.active_listen_restore_timer.p_cback = (TIMER_CBACK *)nfa_p2p_update_active_listen_timeout_cback;
        nfa_sys_start_timer (&nfa_p2p_cb.active_listen_restore_timer, 0, NFA_P2P_RESTORE_ACTIVE_LISTEN_TIMEOUT);

        /* save listen techonologies */
        nfa_p2p_cb.listen_tech_mask_to_restore = nfa_p2p_cb.listen_tech_mask;

        /* remove active listen mode */
        nfa_p2p_cb.listen_tech_mask &= ~( NFA_TECHNOLOGY_MASK_A_ACTIVE|NFA_TECHNOLOGY_MASK_F_ACTIVE);
    }

    if (nfa_p2p_cb.dm_disc_handle != NFA_HANDLE_INVALID)
    {
        nfa_dm_delete_rf_discover (nfa_p2p_cb.dm_disc_handle);
        nfa_p2p_cb.dm_disc_handle = NFA_HANDLE_INVALID;
    }

    /* 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(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 mode1: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);
    }

    /* restart RF discovery to update RF technologies */
    if ((p_msg = (BT_HDR *) GKI_getbuf (sizeof(BT_HDR))) != NULL)
    {
        p_msg->event = NFA_P2P_INT_RESTART_RF_DISC_EVT;
        nfa_sys_sendmsg (p_msg);
    }
}