/*
*********************************************************************************
*                                                                               *
* NOTICE:                                                                 *
*                                                                               *
*********************************************************************************
*   do not support now
*/
 
    
/*********************************************************************************
 *                              <mgc_8ecp.c>                                                              
 *     This file should include 8ecp related APIs used in MGC module, but now is not supported!!!!  	
 *
 *
 *    Author                         Date
 *    ------                        ------
 *    Sam Yao                     Aug 2008                                                                                              
 *********************************************************************************/





/*-----------------------------------------------------------------------*/
/*                        INCLUDE HEADER FILES                           */
/*-----------------------------------------------------------------------*/

#include "./include/mgc_8ecp.h"
#include "./include/mgc_mg_info.h"
#include "./include/mgc_phy_port.h"
#include "./include/mgc_debug.h"


static WORD    mgc_e1card0_mgNo;
static WORD    mgc_e1card1_mgNo;
static WORD    mgc_8k_tone_card_silent;
static MGC_8ECP_SAP	mgc8ecpSap;


/*-----------------------------------------------------------------------
              MGC - 8ECP     M A N A G E R
 ------------------------------------------------------------------------*/	  

int mgc_8ecp_update_mg_status(BYTE mgNo, BYTE status)
{
    MG_INFO *pMgInfo = NULL ;
	MG_INFO *pMgInfoE1 = NULL;

    pMgInfo = mgc_mg_info_get_index_mg(mgNo);
    pMgInfoE1 = mgc_mg_info_get_index_mg(mgc_e1card0_mgNo);
    if((pMgInfo == NULL)||(pMgInfoE1 == NULL))
        return -1;

    if(pMgInfo->created == FALSE)
    {
        MGC_ERROR("%s : mg[%d] not created" , __FUNCTION__, pMgInfo->id);
        return -1;
    }

    pMgInfo->enable = status;	

    if(pMgInfo->mgAttr.e1cardNo == 1)
    {
        if((status == 1)&&((0 == mgc_e1card0_mgNo)||(0 == pMgInfoE1->enable)))
        {
           mgc_8k_tone_card_silent = 1;        
        }
		else
		{
			mgc_8k_tone_card_silent = 0;
		
		}
    }
	else if(pMgInfo->mgAttr.e1cardNo == 0)
	{
        if ((0 == status)  
            && (0 != mgc_e1card1_mgNo) && (1 == pMgInfoE1->enable))
        {
            mgc_8k_tone_card_silent = 1;
        }
        else
        {
            mgc_8k_tone_card_silent = 0;
        }   	
	}
	
    return 0;

}

int mgc_8ecp_update_trk_status(BYTE mgNo, BYTE trkNo, BYTE status)
{
    MG_INFO *pMgInfo = NULL;
    PHY_PORT_INFO *pPhyPort = NULL;

    pMgInfo = mgc_mg_info_get_index_mg(mgNo);

    if(pMgInfo == NULL)
        return -1;
	
    if(pMgInfo->created == FALSE)
    {
        MGC_ERROR("%s : mg[%d] not created" , __FUNCTION__, pMgInfo->id);
        return -1;
    }

    if (trkNo >= MGC_8ECP_MAX_TRK_PER_MG)
    {
	    MGC_ERROR("%s : invalid trkNo, trkNo = %d" , __FUNCTION__ , trkNo);
        return -1;
    }

    pPhyPort = mgc_phy_port_find_port_of_mg(pMgInfo, trkNo);

    if(pPhyPort == NULL)
	    return -1;

    pPhyPort->status = status;

    return 0;
}


static int mgc_bind_8ecp(MGC_8ECP_SAP *pSap)
{
    if(pSap == NULL)
        return -1;

    sprintf(pSap->sap8ecp.name, "MGC");
	pSap->sap8ecp.update_mg_status = mgc_8ecp_update_mg_status;
	pSap->sap8ecp.update_trk_status = mgc_8ecp_update_trk_status;

	pSap->sapIndex= _8ecp_bind(&pSap->sap8ecp);
	if(pSap->sapIndex < 0)
	{
	    MGC_ERROR("8ecp %s bind failed!" , pSap->sap8ecp.name);	
	    return -1;
	}


    mgc_8k_tone_card_silent = 0;

    mgc_e1card0_mgNo = 0;
    mgc_e1card1_mgNo = 0;
	
    return pSap->sapIndex;	
}

int mgc_bind_8ecp_setup(void)
{
	MGC_8ECP_SAP *pSap = NULL;

	pSap = &mgc8ecpSap;

    if(mgc_bind_8ecp(pSap)<0)
	    return -1;

    return 0;	
}

BOOL mgc_8ecp_create(MG_INFO *pMgInfo , MG_ATTR *pMgAttr)
{
    BYTE cardNo;

    if((pMgInfo == NULL)||(pMgAttr == NULL))	
        return FALSE;		

    if(pMgAttr->ctrlType != MGC_MG_CTRL_TYPE_8ECP)
	    return FALSE;	

    cardNo = atoi(pMgAttr->domain);	
    if(cardNo > 31)
	    return FALSE;	
	
	_8ecp_createMG(pMgInfo->id, pMgAttr->ip, cardNo);

    if (0 == cardNo)
    {
        mgc_e1card0_mgNo = pMgInfo->id;
    }
    else if (1 == cardNo)
    {
        mgc_e1card1_mgNo = pMgInfo->id;
    }   	
	
	memcpy(&pMgInfo->mgAttr , pMgAttr , sizeof(MG_ATTR));
    return TRUE;	
}


BOOL mgc_8ecp_delete(MG_INFO *pMgInfo)
{
    BYTE cardNo;
	
    if(pMgInfo == NULL)
        return FALSE;		

    if(pMgInfo->mgAttr.ctrlType != MGC_MG_CTRL_TYPE_8ECP)
	    return FALSE;	

    cardNo = atoi(pMgInfo->mgAttr.domain);
    if(cardNo > 31)
	    return FALSE;

    _8ecp_deleteMG(pMgInfo->id, pMgInfo->mgAttr.ip, cardNo);

    if (0 == cardNo)
    {
        mgc_e1card0_mgNo = 0;
    }
    else if (1 == cardNo)
    {
        mgc_e1card1_mgNo = 0;
    }	
	
    return TRUE;	
}

BOOL mgc_8ecp_dlcx_chnl(CHNL chnl)
{
    MG_INFO *pMgInfo = NULL;
	WORD org, dest;

    pMgInfo = mgc_mg_info_get_index_mg(chnl.mgNo);
    if(pMgInfo == NULL)	
	    return FALSE;

    if(pMgInfo->mgAttr.ctrlType != MGC_MG_CTRL_TYPE_8ECP)
	    return FALSE;
	
    org = (pMgInfo->mgAttr.e1cardNo*8 + chnl.portNo)*32 + chnl.chlNo;	
	dest = (mgc_8k_tone_card_silent * 8 + 7) * 32 + 31;

    _8ecp_connect_chnl(org, dest, 0); 	

    return TRUE;	
}

BOOL mgc_8ecp_connect_chnl(CHNL orgChnl, CHNL destChnl, BYTE flag)
{
    MG_INFO *pOrgMgInfo = NULL;
	MG_INFO *pDestMgInfo = NULL;
	WORD org, dest;

    pOrgMgInfo = mgc_mg_info_get_index_mg(orgChnl.mgNo);
	pDestMgInfo = mgc_mg_info_get_index_mg(destChnl.mgNo);

    if((pOrgMgInfo == NULL)||(pDestMgInfo == NULL))
	    return FALSE;

    if(pOrgMgInfo->mgAttr.ctrlType != MGC_MG_CTRL_TYPE_8ECP)
	    return FALSE;

    if(pDestMgInfo->mgAttr.ctrlType != MGC_MG_CTRL_TYPE_8ECP)
	    return FALSE;

    org = (pOrgMgInfo->mgAttr.e1cardNo*8 + orgChnl.portNo)*32 + orgChnl.chlNo;	
    dest= (pDestMgInfo->mgAttr.e1cardNo*8 + destChnl.portNo)*32 + destChnl.chlNo;

	if((flag & MGC_CONNECT_SILENT_TONE) == MGC_CONNECT_SILENT_TONE)
        dest = (mgc_8k_tone_card_silent * 8 + 7) * 32 + 31;

    _8ecp_connect_chnl(org, dest, 0);	

    return TRUE;		
}


BOOL mgc_8ecp_connect_dtmf(CHNL orgChnl, CHNL destChnl, BYTE digit)
{
    MG_INFO *pOrgMgInfo = NULL;
	MG_INFO *pDestMgInfo = NULL;
	WORD org, dest;

    pOrgMgInfo = mgc_mg_info_get_index_mg(orgChnl.mgNo);
	pDestMgInfo = mgc_mg_info_get_index_mg(destChnl.mgNo);

    if((pOrgMgInfo == NULL)||(pDestMgInfo == NULL))
	    return FALSE;

    if(pOrgMgInfo->mgAttr.ctrlType != MGC_MG_CTRL_TYPE_8ECP)
	    return FALSE;

    if(pDestMgInfo->mgAttr.ctrlType != MGC_MG_CTRL_TYPE_8ECP)
	    return FALSE;

    org = (pOrgMgInfo->mgAttr.e1cardNo*8 + orgChnl.portNo)*32 + orgChnl.chlNo;	
    dest= (pDestMgInfo->mgAttr.e1cardNo*8 + destChnl.portNo)*32 + destChnl.chlNo;

    if(digit == 0)
	    digit = 10;	

    _8ecp_connect_chnl(org, dest, digit);	

    return TRUE;		
}


BOOL mgc_8ecp_connect_tone(CHNL orgChnl, CHNL destChnl, BYTE tone , BYTE duration)
{
    MG_INFO *pOrgMgInfo = NULL;
	MG_INFO *pDestMgInfo = NULL;
	WORD org, dest;

    pOrgMgInfo = mgc_mg_info_get_index_mg(orgChnl.mgNo);
	pDestMgInfo = mgc_mg_info_get_index_mg(destChnl.mgNo);

    if((pOrgMgInfo == NULL)||(pDestMgInfo == NULL))
	    return FALSE;

    if(pOrgMgInfo->mgAttr.ctrlType != MGC_MG_CTRL_TYPE_8ECP)
	    return FALSE;

    if(pDestMgInfo->mgAttr.ctrlType != MGC_MG_CTRL_TYPE_8ECP)
	    return FALSE;

    org = (pOrgMgInfo->mgAttr.e1cardNo*8 + orgChnl.portNo)*32 + orgChnl.chlNo;	
    dest= (pDestMgInfo->mgAttr.e1cardNo*8 + destChnl.portNo)*32 + destChnl.chlNo;

    _8ecp_connect_tone(org, dest, tone ,duration);	

    return TRUE;	
}


