#include "./include/mgc_sur_info.h"
#include "./include/mgc_debug.h"
#include "./include/mgc_mgcp.h"
#include "./include/mgc_conn_info.h"
#include "./include/mgc_chnl_info.h"
#include "./include/mgc_mg_info.h"
#include "./include/mgc_phy_port.h"
#include "./include/mgc_tandem_info.h"





static CONNECT_INFO mgcSurvillanceConn[MGC_MAX_NUM_OF_SURVEILLANCE_CONN];
static CHNL_INFO mgcSurvillianceChnl[MGC_MAX_NUM_OF_SURVEILLIANCE_CHNL];
static PHY_PORT_INFO	mgcSurvilliancePort[MGC_MAX_NUM_OF_SURVEILLANCE_NUM];
static MGC_SUR_INFO_NODE mgcSurvillianceNode[MGC_MAX_NUM_OF_SURVEILLANCE_NODE];


extern 	MGC_SAP *mgc_sap_get_index_sap(int index);


static void mgc_sur_info_node_init(MGC_SUR_INFO_NODE *pNode , int id)
{
    if(pNode == NULL)
	    return ;

    pNode->id = id;
	pNode->pConn = NULL;
	pNode->pNext = NULL;
	pNode->pPrior = NULL;
	return;
}


static MGC_SUR_INFO_NODE *mgc_sur_info_get_unused_node(void)
{
    int i;
	static int index = 0;
	MGC_SUR_INFO_NODE *pNode = NULL;
	MGC_SUR_INFO_NODE *pNodeTmp = NULL;

    i = index;	
    do
    {
        i++;
		if(i == MGC_MAX_NUM_OF_SURVEILLANCE_NODE)
		    i = 0;

        pNode = &mgcSurvillianceNode[i];
		if(pNode->pConn != NULL)
		    continue;

		index = i;
		pNodeTmp = pNode;
		break;
    }while(i != index);

	return pNodeTmp;
}


void mgc_sur_info_list_init(MGC_SUR_INFO_LIST *pList)
{
    if(pList == NULL)
	    return ;

	pList->len = 0;
	pList->pHead = NULL;
	return;
}

MGC_SUR_INFO_NODE *mgc_sur_info_get_list_tail(MGC_SUR_INFO_LIST *pList)
{
	MGC_SUR_INFO_NODE *pNode = NULL;
	MGC_SUR_INFO_NODE *pNodeHead = NULL;

    if((pList == NULL)||(pList->pHead == NULL))
	    return NULL;

    pNode = NULL;
	pNodeHead = pList->pHead;
	pNode = pNodeHead->pNext;
    while(pNode->pNext != pNodeHead)
    {
        pNode = pNode->pNext;		
    }

    return pNode;
}

MGC_SUR_INFO_NODE *mgc_sur_info_get_list_node(MGC_SUR_INFO_LIST *pList , WORD index)
{
	MGC_SUR_INFO_NODE *pNode = NULL;
	MGC_SUR_INFO_NODE *pNodeTmp = NULL;
	int i;

    if((pList == NULL) || (pList->pHead == NULL))
	    return NULL;

    if(index >= pList->len)
    {
        MGC_WARN("list len %d <= %d" , pList->len , index);
		return NULL;
    }

    i = 0;
    pNode = pList->pHead;
    do
    {
        if(i == index)
        {
            pNodeTmp = pNode;
            break;
        }
		
        i++;
		pNode = pNode->pNext;
    }while(pNode != pList->pHead);

    return pNodeTmp;    	
}


MGC_SUR_INFO_NODE *mgc_sur_info_find_conn_in_list(MGC_SUR_INFO_LIST *pList , CONNECT_INFO *pConn)
{
    MGC_SUR_INFO_NODE *pNode = NULL;
    BOOL result = FALSE;	

    if((pList == NULL)||(pConn == NULL)||(pList->pHead == NULL))
	    return NULL;		

    pNode = pList->pHead;
    do
    {
        if(pNode->pConn == pConn)
        {
      		result = TRUE;	
			//MGC_DEBUG("find conn[%d] in the list" , pConn->id);
            break;  
        }

        pNode = pNode->pNext;
    }while(pNode != pList->pHead);
	
    if(result == TRUE)
	    return pNode;	
    	
    return NULL;
}


BOOL mgc_sur_info_add_conn_to_list(MGC_SUR_INFO_LIST *pList , CONNECT_INFO *pConn)
{
    MGC_SUR_INFO_NODE *pNode = NULL;
	MGC_SUR_INFO_NODE *pNodeTail = NULL;

    if((pList == NULL)||(pConn == NULL))
	    return FALSE;	

    pNode = mgc_sur_info_get_unused_node();
	if(pNode == NULL)
	    return FALSE;

    pNode->pConn = pConn;
	pNode->pPrior = pNode;
	pNode->pNext = pNode;

    if(pList->pHead == NULL)
        pList->pHead = pNode;
    
    
    pNodeTail = mgc_sur_info_get_list_tail(pList);
	if(pNodeTail == NULL)
	{
	    MGC_ERROR("lost list tail");
	    return FALSE;	
	}

    pNode->pPrior = pNodeTail;
	pNode->pNext = pNodeTail->pNext;
	pNodeTail->pNext = pNode;	   
    pList->pHead->pPrior = pNode;
	
	pList->len++;	
    MGC_DEBUG("conn[%d] is add to node[%d] , list len %d" , pConn->id , pNode->id , pList->len);		
	return TRUE;	
}



BOOL mgc_sur_info_remove_conn_to_list(MGC_SUR_INFO_LIST *pList , CONNECT_INFO *pConn)
{
    MGC_SUR_INFO_NODE *pNode = NULL;
	MGC_SUR_INFO_NODE *pPrior = NULL;
	MGC_SUR_INFO_NODE *pNext = NULL;

    if((pList == NULL)||(pConn == NULL)||(pList->pHead == NULL))
	    return FALSE;	

    pNode = mgc_sur_info_find_conn_in_list(pList, pConn);
    if(pNode == NULL)
    {
        MGC_ERROR("conn[%d] is not find in the list" , pConn->id);
        return FALSE;
    }

	pPrior = pNode->pPrior;
	pNext = pNode->pNext;

    pPrior->pNext = pNext;
	pNext->pPrior = pPrior;	

    pList->len--;

    if(pList->pHead == pNode)
    {
        pList->pHead = pNext;
    }

    if((pPrior == pNode)&&(pNext == pNode))
    {
        if(pList->len != 0)
        {
			MGC_DEBUG("list shoulde be empty, len %d" , pList->len);        
        }
		pList->pHead = NULL;
    }
	
    MGC_DEBUG("conn[%d] is removed from node[%d] list len %d" , pConn->id , pNode->id , pList->len);
	mgc_sur_info_node_init(pNode, pNode->id);
	return TRUE;	
}



void mgc_sur_info_clear_list(MGC_SUR_INFO_LIST *pList)
{
	MGC_SUR_INFO_NODE  *pNodeTail = NULL;
	CONNECT_INFO *pConn = NULL;
	MGCP_PARA mgcpPara;

    if((pList == NULL)||(pList->pHead == NULL))
	    return ;	

	pNodeTail = mgc_sur_info_get_list_tail(pList);
    while(pNodeTail != NULL)
    {
        pConn = pNodeTail->pConn;
		
        if((mgc_connect_get_status(pConn) == MGC_CONNECT_STATUS_CREATED)||(mgc_connect_get_status(pConn) == MGC_CONNECT_STATUS_CREATE))
        {
            if(mgc_connect_dlcx_para_create(pConn , &mgcpPara) == TRUE)
		        MGCP_req(mgc_mgcp_get_sap_index(), MGCP_CMD_DLCX , 0xFFFF , &mgcpPara);        
        }
        mgc_tandem_info_unset_conn(pConn->pTandem, pConn);
		mgc_chnl_info_detach_connect(pConn->pChnlInfo, pConn);
		mgc_connect_init(pConn, pConn->id);
		mgc_sur_info_remove_conn_to_list(pList , pConn);
		pNodeTail = mgc_sur_info_get_list_tail(pList);
    }

    if(pList->len != 0)
    {
        MGC_ERROR("list clear failed!");
    }

    return;        		
}



static CHNL_INFO *mgc_sur_info_get_unsed_chnl(void)
{
    int i;
    CHNL_INFO *pChnlInfo = NULL;

    for(i=0 ; i<MGC_MAX_NUM_OF_CHNL ; i++)
    {
        pChnlInfo = &mgcSurvillianceChnl[i];
		
		if(pChnlInfo->status != MGC_CHNL_INFO_STATUS_UNDEF)
		    continue;

		return pChnlInfo;
    }

    return NULL;		
}



static CONNECT_INFO *mgc_sur_info_get_unsed_conn(void)
{
    int i;
	CONNECT_INFO *pConnect = NULL;

    for(i=0 ; i<MGC_MAX_NUM_OF_CON ; i++)
    {
        pConnect = &mgcSurvillanceConn[i];

		if(pConnect->pChnlInfo != NULL)
		    continue;	

		return pConnect;
    }

    return NULL;	
}

#if 0
static BOOL mgc_sur_info_add_conn_to_chnl(CHNL_INFO *pChnlInfo)
{
    int i;
    CONNECT_INFO *pConn;
	
    if(pChnlInfo == NULL)
	    return FALSE;	

    for(i=0 ; i<pChnlInfo->maxConnectNum ; i++)
    {
        pConn = mgc_sur_info_get_unsed_conn();
		if(pConn == NULL)
		    return FALSE;

		if(mgc_chnl_info_attach_connect(pChnlInfo, pConn, i) == FALSE)
		    return FALSE;		

		//MGC_DEBUG("surConn[%d] add to chnlInfo[%d]", pConn->id , pChnlInfo->id);
    }

    return TRUE;	
}
#endif


static BOOL mgc_sur_info_add_chnl_to_port(PHY_PORT_INFO *pPhyPort)
{
    int i;
    CHNL_INFO *pChnlInfo = NULL;

    if(pPhyPort == NULL)
        return FALSE;

    for(i=0 ; i<pPhyPort->chnlNum ; i++)	
    {
        pChnlInfo = mgc_sur_info_get_unsed_chnl();
		if(pChnlInfo == NULL)
		    return FALSE;

		pPhyPort->pChnlInfo[i] = pChnlInfo;
		
		if(mgc_chnl_info_attach_to_phy_port(pChnlInfo , pPhyPort) == FALSE)
		    return FALSE;	

//		if(mgc_sur_info_add_conn_to_chnl(pChnlInfo) == FALSE)
//		    return FALSE;

		mgc_chnl_info_status_set(pChnlInfo , MGC_CHNL_INFO_STATUS_IDLE);	
		//MGC_DEBUG("chnlInfo[%d] add to phyPort[%d]" , pChnlInfo->id, pPhyPort->id);
    }

    return TRUE;	
}


static PHY_PORT_INFO *mgc_sur_info_get_unused_phy_port(void)
{
    int i;
    PHY_PORT_INFO *pPhyPort = NULL;

    for(i=0 ; i<MGC_MAX_NUM_OF_PHY_PORT ; i++)
    {
        pPhyPort = &mgcSurvilliancePort[i];
		if((pPhyPort->chnlNum == 0)&&(pPhyPort->pMgInfo == NULL))
		    return pPhyPort;
    }	
    return NULL;
}




BOOL mgc_sur_info_add_survillance_res(BYTE sapIndex, WORD mgNo)
{
    MG_INFO *pMgInfo;
	MGC_SAP *pMgcSap;
    int i;	
    BOOL result = FALSE;	
    PHY_PORT_INFO *pPhyPort = NULL;

	pMgInfo = mgc_mg_info_get_index_mg(mgNo);
    pMgcSap = mgc_sap_get_index_sap(sapIndex);
    if((pMgInfo == NULL)||(pMgcSap == NULL))
	    return FALSE;

	if(pMgInfo->mgAttr.mgType != MGC_MG_TYPE_TANDEM)
	    return FALSE;	

    pPhyPort = mgc_sur_info_get_unused_phy_port();
    if(pPhyPort == NULL)
	    return FALSE;	

    for(i=0 ; i<MGC_MAX_PHY_PORT_PER_MG ; i++)
    {
        if(pMgInfo->pPhyPort[i] != NULL)
		    continue;
        result = TRUE;
		break;
    }

    if(result == FALSE)
    {
        MGC_DEBUG("mg[%d] is full of phyport" , pMgInfo->id);
        return FALSE;
    }
    pPhyPort->portType = MGC_PHY_VIRTUAL_TYPE_SURVEILLANCE;
    pPhyPort->portNo = i;
    pPhyPort->pMgcSap = pMgcSap;
    pPhyPort->pMgInfo = pMgInfo;

    switch(pMgInfo->mgAttr.ctrlType)
    {
	    case MGC_MG_CTRL_TYPE_MGCP:
		    mgc_phy_port_set_chnl_num(pPhyPort , pPhyPort->portType);
		    break;
	    default:
		    mgc_phy_port_clear(pPhyPort);
		    MGC_ERROR("mgcMgInfo[%d] unsupport ctrl type %d" , pMgInfo->id , pMgInfo->mgAttr.ctrlType);
		    return FALSE;
    }


    if(mgc_sur_info_add_chnl_to_port(pPhyPort) == FALSE)
    {
        mgc_phy_port_clear(pPhyPort);
		return FALSE; 
    }

    if(mgc_mg_info_attach_phy_port(pMgInfo , pPhyPort, pMgcSap)== FALSE)
    {
        mgc_phy_port_clear(pPhyPort);
		return FALSE;    
    }	

    return TRUE;    	

}


void mgc_sur_info_setup(void)
{
    int i;
	CONNECT_INFO *pConn = NULL;
	CHNL_INFO *pChnl = NULL;
    PHY_PORT_INFO *pPhyPort = NULL;
	MGC_SUR_INFO_NODE *pNode = NULL;

    for(i=0; i<MGC_MAX_NUM_OF_SURVEILLANCE_CONN ; i++)	
    {
        pConn = &mgcSurvillanceConn[i];
	    mgc_connect_init(pConn , i + MGC_SURVEILLIANCE_CONN_STAR_ID);		
    }

    for(i=0 ; i<MGC_MAX_NUM_OF_SURVEILLANCE_NODE; i++)
    {
        pNode = &mgcSurvillianceNode[i];
		mgc_sur_info_node_init(pNode, i);
    }

    for(i=0 ; i<MGC_MAX_NUM_OF_SURVEILLIANCE_CHNL; i++)
    {
        pChnl = &mgcSurvillianceChnl[i];
		mgc_chnl_info_init(pChnl, i + MGC_SURVEILLIANCE_CHNL_STAR_ID);
    }

    for(i=0 ; i<MGC_MAX_NUM_OF_SURVEILLANCE_NUM ; i++)
    {
        pPhyPort = &mgcSurvilliancePort[i];
		mgc_phy_port_init(pPhyPort, i+MGC_SURVEILLIANCE_PHY_PORT_STAR_ID);
    }
}


static CONNECT_INFO *mgc_sur_info_find_unattached_conn_in_phy_port(PHY_PORT_INFO *pPhyPort)
{
    int i,j;
    CONNECT_INFO *pConnect = NULL;
    CHNL_INFO *pChnlInfo = NULL;
	
    if(pPhyPort == NULL)
	    return NULL;	

    for(i=0 ; i<pPhyPort->chnlNum ; i++)
    {
        pChnlInfo = pPhyPort->pChnlInfo[i];
		if(pChnlInfo == NULL)
		{
		    MGC_WARN("phyPort[%d] lost chnl[%d] info" , pPhyPort->id , i);
			continue;
		}

        for(j=0 ; j<pChnlInfo->maxConnectNum ; j++)
        {
            pConnect = pChnlInfo->pConnection[j];

            if(pConnect == NULL)
            {
                pConnect = mgc_sur_info_get_unsed_conn();
				if(pConnect == NULL)
				    continue;

				if(pConnect->pTandem != NULL)
				{
				    MGC_WARN("surConn[%d] attached illegal tandem[%d]", pConnect->id, pConnect->pTandem->id);
					mgc_connect_init(pConnect, pConnect->id);
					continue;
				}

				if(mgc_chnl_info_attach_connect(pChnlInfo, pConnect, j) == FALSE)
				    continue;

				return pConnect;
            }
        }
    }

    MGC_DEBUG("no unattached conn find in phyport[%d]" , pPhyPort->id);
    return NULL;
}

CONNECT_INFO *mgc_sur_info_find_sur_conn_in_mg(MG_INFO *pMgInfo)
{
    int i;
	CONNECT_INFO *pConnect = NULL;
    PHY_PORT_INFO *pPhyPort = NULL;

    if((pMgInfo == NULL)||(pMgInfo->mgAttr.mgType != MGC_MG_TYPE_TANDEM))
	    return NULL;

    for(i=0 ; i<MGC_MAX_PHY_PORT_PER_MG ; i++)
    {
        pPhyPort = pMgInfo->pPhyPort[i];

		if(pPhyPort == NULL)
		    continue;

        if(pPhyPort->portType != MGC_PHY_VIRTUAL_TYPE_SURVEILLANCE)
		    continue;

		break;
    }

    if(pPhyPort == NULL)
    {
        MGC_ERROR("no surveillant port find in mg[%d]" , pMgInfo->id);
        return NULL;
    }

    pConnect = mgc_sur_info_find_unattached_conn_in_phy_port(pPhyPort);
    return pConnect;	
}
