#include "./include/mgc_sess_ctl.h"

 
MGC_SESS_REC mgcSess[MGC_SESS_RECORD];

void mgc_sess_rec_init(MGC_SESS_REC *pSess , int id)
{
    if(pSess == NULL)
	    return;

    memset(pSess , 0 , sizeof(MGC_SESS_REC));
	pSess->status = MGC_SESS_STATUS_IDLE;
	pSess->id = id;
	return; 
}

void mgc_sess_init(void)
{
    MGC_SESS_REC *pSess;
    int i;
	
    for(i=0 ; i<MGC_SESS_RECORD ; i++)
    {
        pSess = &mgcSess[i];
		mgc_sess_rec_init(pSess , i);
    }

    return;
}

void mgc_sess_set_status(MGC_SESS_REC *pSess , MGC_SESS_STATUS status)
{
    if(pSess == NULL)
	    return;

    pSess->status = status;
}

MGC_SESS_STATUS mgc_sess_get_status(MGC_SESS_REC *pSess)
{
    if(pSess == NULL)
	    return MGC_SESS_STATUS_UNDEF;

    return pSess->status;
}


MGC_SESS_REC *mgc_sess_get_unused(void)
{
    int i;
    MGC_SESS_REC *pSess;	

    for(i=0 ; i<MGC_SESS_RECORD ; i++)
    {
        pSess = &mgcSess[i];

        if(mgc_sess_get_status(pSess) != MGC_SESS_STATUS_IDLE)
		    continue;

		return pSess;
    }
	
    return NULL;
}

BOOL mgc_sess_is_chnl_equal(CHNL orgChnl , CHNL dstChnl)
{
    if(orgChnl.portNo != dstChnl.portNo)
	    return FALSE;

    if(orgChnl.mgNo != dstChnl.mgNo)
	    return FALSE;

    if(orgChnl.connectNo != dstChnl.connectNo)
	    return FALSE;

    if(orgChnl.chlNo != dstChnl.chlNo)
	    return FALSE;

    return TRUE;	
}

MGC_SESS_REC *mgc_sess_is_already_rec(CHNL chnl)
{
    int i;
    MGC_SESS_REC *pSess;

    for(i=0 ;  i<MGC_SESS_RECORD; i++)
    {
        pSess = &mgcSess[i];

		if(mgc_sess_get_status(pSess) != MGC_SESS_STATUS_USED)
		    continue;

		if(mgc_sess_is_chnl_equal(pSess->chnl , chnl) == FALSE)
		    continue;

		return pSess;
    }

    return NULL;	
}


BOOL mgc_sess_rec_chnl(MGC_SESS_REC *pSess ,CHNL chnl)
{
    if(pSess == NULL)
        return FALSE;   

    memcpy(&pSess->chnl , &chnl , sizeof(CHNL));
    mgc_sess_set_status(pSess,MGC_SESS_STATUS_USED);
    return TRUE;	
}

BOOL mgc_sess_rec_org_and_dst_chnl(CHNL orgChnl , CHNL destChnl)
{
    MGC_SESS_REC *pOrgSess, *pDestSess;

    pOrgSess = mgc_sess_is_already_rec(orgChnl);
	pDestSess = mgc_sess_is_already_rec(destChnl);

    if(pOrgSess == NULL)
    {
	    pOrgSess = mgc_sess_get_unused();
	    if(pOrgSess == NULL)
		    return FALSE;	    
    }

    mgc_sess_rec_chnl(pOrgSess, orgChnl);	

    if(pDestSess == NULL)
    {
	    pDestSess = mgc_sess_get_unused();
	    if(pDestSess == NULL)
		    return FALSE;	    
    }

    mgc_sess_rec_chnl(pDestSess, destChnl);	
	return TRUE;
}


BOOL mgc_sess_is_media_info_same(MGC_SDP_TAG *pSdpTag , PUB_SDP_MSG *pSdp)
{
    if((pSdpTag == NULL)||(pSdp == NULL))
        return FALSE;

    if(pSdpTag->m.m.port != pSdp->medias.medias[0].m.port)
	    return FALSE;

    if(strcmp(pSdpTag->c.addr , pSdp->c.addr) != 0)
	    return FALSE;
	
    if(pSdpTag->m.m.payloads[0] != pSdp->medias.medias[0].m.payloads[0])
	    return FALSE;

    return TRUE;	
}


BOOL mgc_sess_is_chnl_need_update(CHNL chnl, MGCP_PARA *pPara)
{
	PUB_SDP_MSG *pSdp;
	MGC_SDP_TAG *pSdpTag;
    MGC_SESS_REC *pSess;

    pSess = mgc_sess_is_already_rec(chnl);

    if((pSess == NULL)||(pPara == NULL))
	    return TRUE;

    pSdp = &pPara->sdp;
    pSdpTag = &pSess->sdpTag;


    if(pSdpTag->mode!= pPara->m.mode)
	{
        pSdpTag->mode = pPara->m.mode;
        memcpy(&pSdpTag->c, &pSdp->c , sizeof(PUB_SDP_C)); 
    memcpy(&pSdpTag->m, &(pSdp->medias.medias[0]) , sizeof(PUB_SDP_MEDIA));
	return TRUE;
}

    if((pPara->flag & MGCP_PARA_FLAG_SDP) != MGCP_PARA_FLAG_SDP)
	    return TRUE;		

	if(mgc_sess_is_media_info_same(&pSess->sdpTag , &(pPara->sdp)) == TRUE)
{
		return FALSE;
	}

	pSdpTag->mode = pPara->m.mode;
	memcpy(&pSdpTag->c, &pSdp->c , sizeof(PUB_SDP_C)); 
	memcpy(&pSdpTag->m, &(pSdp->medias.medias[0]) , sizeof(PUB_SDP_MEDIA)); 

		return TRUE;
    }
	

