/*=========================================*/
/*                                         */
/*                                         */
/*  Author:     Zane Yi                    */
/*  Date:       2007.02.01                 */
/*                                         */
/*=========================================*/
#include "./include/inc.h"
#include "./include/isup_if.h"
#include "./include/isup_struct.h"
#include "./include/isup_debug.h"
#include "./include/isup_def.h"
#include "./include/isup_const.h"

up_message    			isup_rv_msg,isup_sd_msg,isup_raw_msg;
SiAllPdus						isup_rv_pdus,isup_sd_pdus;
ISUP_Timer_Struct   isup_timer_var;
ISUP_Resource_Pond  isup_db;

extern int	check_outbound_route(BYTE ni, long dpc, BYTE sls);//mtp3 interface

static void timer_init(u32 interval_ms)
{
		memset(&isup_timer_var,0,sizeof(isup_timer_var));
    isup_timer_var.t1 = ISUP_T1*1000/interval_ms;
    isup_timer_var.t5 = ISUP_T5*1000/interval_ms;
    isup_timer_var.t7 = ISUP_T7*1000/interval_ms;
    isup_timer_var.t8 = ISUP_T8*1000/interval_ms;
    isup_timer_var.t9 = ISUP_T9*1000/interval_ms;
    isup_timer_var.t12 = ISUP_T12*1000/interval_ms;
    isup_timer_var.t13 = ISUP_T13*1000/interval_ms;
    isup_timer_var.t14 = ISUP_T14*1000/interval_ms;
    isup_timer_var.t15 = ISUP_T15*1000/interval_ms;
    isup_timer_var.t16 = ISUP_T16*1000/interval_ms;
    isup_timer_var.t17 = ISUP_T17*1000/interval_ms;
    isup_timer_var.t18 = ISUP_T18*1000/interval_ms;
    isup_timer_var.t19 = ISUP_T19*1000/interval_ms;
    isup_timer_var.t20 = ISUP_T20*1000/interval_ms;
    isup_timer_var.t21 = ISUP_T21*1000/interval_ms;
    isup_timer_var.t22 = ISUP_T22*1000/interval_ms;    
    isup_timer_var.t23 = ISUP_T23*1000/interval_ms;
    isup_timer_var.t24 = ISUP_T24*1000/interval_ms;
    isup_timer_var.t25 = ISUP_T25*1000/interval_ms;
    isup_timer_var.t26 = ISUP_T26*1000/interval_ms;
    isup_timer_var.t27 = ISUP_T27*1000/interval_ms;
    isup_timer_var.t34 = ISUP_T34*1000/interval_ms;
    isup_timer_var.t37 = ISUP_T37*1000/interval_ms;    
}


static int find_port_by_sap(Pst *pst)
{	
	const pal_circuit_struct* circuit_str;
	
	circuit_str = pal_locate_circuit(pst->cg_id, pst->cic);
	if(circuit_str == NULL)
	{
		isup_log_err(0,"illegal cic value!  cg_id=%d,cic=%d\r\n",pst->cg_id,pst->cic);
		return -1;
	}	

	if(isup_db.attrib.isup_run_mode == ISUP_DUAL_MODE && 
		circuit_str->attrib.plat_id != isup_db.attrib.systemID)
	{
		isup_log_err(0,"error!plat_id=%d,systemID=%d,run_mode=%d,cg_id=%d,cic=%d\r\n",
								circuit_str->attrib.plat_id,
								isup_db.attrib.systemID,
								isup_db.attrib.isup_run_mode,
								pst->cg_id,
								pst->cic);			
		return -1;
	}	
				
	pst->sp_proc_id = ISUP_CIRCUIT_CIC*circuit_str->id + 
		(pst->cic - circuit_str->attrib.head_cic);
	return 1;
}


//static void isup_check_L3status(void)
//{
//	int i;
//	for (i = 0; i < ISUP_MAX_CG; i++)
//	{
//		if (isup_db.cg_pond[i].enable)
//		{
//			if (check_outbound_route((isup_db.cg_pond[i].attrib.network_id & 3), isup_db.cg_pond[i].attrib.dpc, 0) >= 0)
//			{
//				//L3 OK
//			}
//			else
//			{
//				//L3 Failed
//			}
//		}
//	}
//}
void isup_init(u8 systemID, u32 interval_ms)
{
    u32     stack_size;
    if (interval_ms==0)
        interval_ms = 10;    
    printf("Start ISUP module init...\n");
    memset(&isup_db, 0, sizeof(ISUP_Resource_Pond));
    memset(&isup_timer_var, 1, sizeof(ISUP_Timer_Struct));
	memset(&isup_rv_msg, 0, sizeof(up_message));
	memset(&isup_sd_msg, 0, sizeof(up_message));
	memset(&isup_rv_pdus, 0, sizeof(SiAllPdus));
	memset(&isup_sd_pdus, 0, sizeof(SiAllPdus));
	timer_init(interval_ms);
	isup_db.attrib.systemID  =	systemID;
	isup_db.attrib.isup_remote_ip = 0;
	isup_db.attrib.isup_run_mode = 0;
    stack_size = sizeof(ISUP_Resource_Pond) + sizeof(ISUP_Timer_Struct);
	isup_debug_init();
    printf("ISUP module init completed, stack size: %ld bytes\n", stack_size);
}

void isup_timer()
{
  u16             i,j;
  u32             offset, pid;
	static DWORD cntT50ms = 0;
	const pal_circuit_struct* circuit_str;
	const pal_cg_struct* cg_str;

	if (++cntT50ms >= 5)
	{	// 50 mini seconds period
		cntT50ms = 0;
		isup_debug_timer();
//		isup_check_L3status();
	}

  for (i = 0; i <64; i++)
  {
      if (isup_mdsc_proc()==0)
          break;      //no message in lower layer
  }

  for (i = 0; i <ISUP_MAX_CIRCUIT; i++)		//for timer
  {
  	circuit_str = pal_circuit_ptr(i);
  	if(circuit_str == NULL)
  		continue;
    if(circuit_str->enable==0)
      continue;
      
    cg_str = pal_cg_ptr(circuit_str->cg_id);	 
  	if(cg_str == NULL)
  		continue;    
    if(cg_str->attrib.protocol!=1)//isup
    	continue;
    	
		if(isup_db.attrib.isup_run_mode == ISUP_DUAL_MODE && 
			circuit_str->attrib.plat_id != isup_db.attrib.systemID)
			continue;
	
    offset = ISUP_CIRCUIT_CIC*i;
    for (j = 0; j <ISUP_CIRCUIT_CIC; j++)
    {
        pid = offset + j;
        isup_csc_timer(pid);
        isup_cpc_timer(pid);
    }
  }

}

int isup_setup_req(Pst *pst, SiPduIAM *ptr, u8 *raw_data, u8 len)
{
	if(find_port_by_sap(pst)==-1)
	{	
		isup_trace_func(0,"APP => function trace: %s\r\n", __FUNCTION__);
		return ISUP_CM_FAILED;
	}
	isup_db.port_pond[pst->sp_proc_id].su_proc_id = pst->su_proc_id;
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_SETUP_REQ;
	isup_db.port_pond[pst->sp_proc_id].trace_flag = pst->trace_flag;
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	
	if(isup_db.port_pond[pst->sp_proc_id].cic_state.call_state == IDLE_FLAG)
	{	
		isup_db.port_pond[pst->sp_proc_id].cic_state.call_state = CALLOUT_FLAG;
		memcpy(&isup_sd_pdus,ptr,sizeof(SiPduIAM));
		
		memset(&isup_raw_msg,0,sizeof(up_message));
		isup_raw_msg.len	=	len;
		memcpy(isup_raw_msg.msgList,raw_data,isup_raw_msg.len);
		
		isup_cpc_proc(pst->sp_proc_id);
		if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
		{	
			isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
			isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
		}
		return ISUP_CM_OK;
	}
	else
	{
		isup_log_err(pst->sp_proc_id,"Error,call_state=%d\r\n",isup_db.port_pond[pst->sp_proc_id].cic_state.call_state);
		return ISUP_CM_FAILED;	
	}
}

int isup_setup_anm_rsp(Pst *pst, SiPduANM *ptr_anm, u8 *raw_data, u8 len)
{
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	if(pst->sp_proc_id >= ISUP_MAX_PORT)
	{	
		isup_log_err(0,"[Error] sp_proc_id == %d. ",pst->sp_proc_id);
		return ISUP_CM_FAILED;
	}
	
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_SETUP_RSP;
	memcpy(&isup_sd_pdus,ptr_anm,sizeof(SiPduANM));
	
	memset(&isup_raw_msg,0,sizeof(up_message));
	isup_raw_msg.len	=	len;
	memcpy(isup_raw_msg.msgList,raw_data,isup_raw_msg.len);	
	isup_cpc_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}

int isup_setup_con_rsp(Pst *pst, SiPduCON *ptr_con, u8 *raw_data, u8 len)
{
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	if(pst->sp_proc_id >= ISUP_MAX_PORT)
	{	
		isup_log_err(0,"[Error] sp_proc_id == %d. ",pst->sp_proc_id);
		return ISUP_CM_FAILED;
	}
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_SETUP_RSP;
	memcpy(&isup_sd_pdus,ptr_con,sizeof(SiPduCON));
	
	memset(&isup_raw_msg,0,sizeof(up_message));
	isup_raw_msg.len	=	len;
	memcpy(isup_raw_msg.msgList,raw_data,isup_raw_msg.len);	
	isup_cpc_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}


int isup_release_req(Pst *pst, SiPduREL *ptr, u8 *raw_data, u8 len )
{
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	if(pst->sp_proc_id >= ISUP_MAX_PORT)
	{	
		isup_log_err(0,"[Error] sp_proc_id == %d. ",pst->sp_proc_id);
		return ISUP_CM_FAILED;
	}
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_RELEASE_REQ;
	memcpy(&isup_sd_pdus,ptr,sizeof(SiPduREL));
	
	memset(&isup_raw_msg,0,sizeof(up_message));
	isup_raw_msg.len	=	len;
	memcpy(isup_raw_msg.msgList,raw_data,isup_raw_msg.len);		
	isup_cpc_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}

int isup_release_rsp(Pst *pst, SiPduRLC *ptr, u8 *raw_data, u8 len)
{
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	if(pst->sp_proc_id >= ISUP_MAX_PORT)
	{	
		isup_log_err(0,"[Error] sp_proc_id == %d. ",pst->sp_proc_id);
		return ISUP_CM_FAILED;
	}
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_RELEASE_RSP;
	memcpy(&isup_sd_pdus,ptr,sizeof(SiPduRLC));
	
	memset(&isup_raw_msg,0,sizeof(up_message));
	isup_raw_msg.len	=	len;
	memcpy(isup_raw_msg.msgList,raw_data,isup_raw_msg.len);		
	isup_cpc_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}

int isup_alert_req(Pst *pst,  SiPduACM *ptr, u8 *raw_data, u8 len)
{
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	if(pst->sp_proc_id >= ISUP_MAX_PORT)
		return ISUP_CM_FAILED;
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_ALERT_REQ;
	memcpy(&isup_sd_pdus,ptr,sizeof(SiPduACM));
	
	memset(&isup_raw_msg,0,sizeof(up_message));
	isup_raw_msg.len	=	len;
	memcpy(isup_raw_msg.msgList,raw_data,isup_raw_msg.len);		
	
	isup_cpc_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}

int isup_info_req(Pst *pst,  SiPduSAM *ptr, u8 *raw_data, u8 len)
{
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	if(pst->sp_proc_id >= ISUP_MAX_PORT)
		return ISUP_CM_FAILED;
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_INFO_REQ;
	memcpy(&isup_sd_pdus,ptr,sizeof(SiPduSAM));
	
	memset(&isup_raw_msg,0,sizeof(up_message));
	isup_raw_msg.len	=	len;
	memcpy(isup_raw_msg.msgList,raw_data,isup_raw_msg.len);		
	isup_cpc_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}

int isup_prog_req(Pst *pst,  SiPduCPG *ptr, u8 *raw_data, u8 len)
{
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	if(pst->sp_proc_id >= ISUP_MAX_PORT)
		return ISUP_CM_FAILED;
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_PROG_REQ;
	memcpy(&isup_sd_pdus,ptr,sizeof(SiPduCPG));
	
	memset(&isup_raw_msg,0,sizeof(up_message));
	isup_raw_msg.len	=	len;
	memcpy(isup_raw_msg.msgList,raw_data,isup_raw_msg.len);		
	isup_cpc_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}

int isup_suspend_req(Pst *pst,  SiPduSUS *ptr, u8 *raw_data, u8 len)
{
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	if(pst->sp_proc_id >= ISUP_MAX_PORT)
		return ISUP_CM_FAILED;
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_SUSPEND_REQ;
	memcpy(&isup_sd_pdus,ptr,sizeof(SiPduSUS));
	
	memset(&isup_raw_msg,0,sizeof(up_message));
	isup_raw_msg.len	=	len;
	memcpy(isup_raw_msg.msgList,raw_data,isup_raw_msg.len);		
	isup_cpc_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}

int isup_resume_req(Pst *pst, SiPduRES *ptr, u8 *raw_data, u8 len)
{
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	if(pst->sp_proc_id >= ISUP_MAX_PORT)
		return ISUP_CM_FAILED;
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_RESUME_REQ;
	memcpy(&isup_sd_pdus,ptr,sizeof(SiPduRES));
	
	memset(&isup_raw_msg,0,sizeof(up_message));
	isup_raw_msg.len	=	len;
	memcpy(isup_raw_msg.msgList,raw_data,isup_raw_msg.len);		
	isup_cpc_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}

/*Maintenance*/
int isup_block_req(Pst *pst, SiPduCGB *ptr )
{
	if(find_port_by_sap(pst)==-1)
	{
		isup_trace_func(0,"APP => function trace: %s\r\n", __FUNCTION__);		
		return ISUP_CM_FAILED;
	}
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);		
	isup_db.port_pond[pst->sp_proc_id].sm_proc_id = pst->su_proc_id;
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_BLOCK_REQ;
	if(ptr != NULL)
	{	
		memcpy(&isup_sd_pdus,ptr,sizeof(SiPduCGB));
		isup_mgbs_proc(pst->sp_proc_id);
	}
	else
		isup_bls_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}

int isup_block_rsp(Pst *pst,  SiPduCGBA *ptr)
{
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	if(pst->sp_proc_id >= ISUP_MAX_PORT)
	{	
		isup_log_err(0,"[Error] sp_proc_id == %d. ",pst->sp_proc_id);
		return ISUP_CM_FAILED;
	}
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_BLOCK_RSP;
	if(ptr != NULL)
	{	
		memcpy(&isup_sd_pdus,ptr,sizeof(SiPduCGBA));
		isup_mgbr_proc(pst->sp_proc_id);
	}
	else
		isup_blr_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}

int isup_unblock_req(Pst *pst, SiPduCGU *ptr )
{	
	if(find_port_by_sap(pst)==-1)
	{
		isup_trace_func(0,"APP => function trace: %s\r\n", __FUNCTION__);			
		return ISUP_CM_FAILED;
	}
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);		
	isup_db.port_pond[pst->sp_proc_id].sm_proc_id = pst->su_proc_id;
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_UNBLOCK_REQ;
	if(ptr != NULL)
	{	
		memcpy(&isup_sd_pdus,ptr,sizeof(SiPduCGU));
		isup_mgbs_proc(pst->sp_proc_id);
	}
	else
		isup_bls_proc(pst->sp_proc_id);			
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}

int isup_unblock_rsp(Pst *pst,  SiPduCGUA *ptr)
{
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	if(pst->sp_proc_id >= ISUP_MAX_PORT)
	{	
		isup_log_err(0,"[Error] sp_proc_id == %d. ",pst->sp_proc_id);
		return ISUP_CM_FAILED;
	}
	
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_UNBLOCK_RSP;
	if(ptr != NULL)
	{	
		memcpy(&isup_sd_pdus,ptr,sizeof(SiPduCGUA));
		isup_mgbr_proc(pst->sp_proc_id);
	}
	else
		isup_blr_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}

int isup_reset_req(Pst *pst, SiPduGRS *ptr )
{
	
	if(find_port_by_sap(pst)==-1)
	{
		isup_trace_func(0,"APP => function trace: %s\r\n", __FUNCTION__);
		return ISUP_CM_FAILED;
	}
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);		
	isup_db.port_pond[pst->sp_proc_id].sm_proc_id = pst->su_proc_id;
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_RESET_REQ;
	if(ptr != NULL)
	{	
		memcpy(&isup_sd_pdus,ptr,sizeof(SiPduGRS));
		isup_cgrs_proc(pst->sp_proc_id);
	}
	else
		isup_crs_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}

int isup_reset_rsp(Pst *pst,  SiPduGRA *ptr)
{
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	if(pst->sp_proc_id >= ISUP_MAX_PORT)
	{	
		isup_log_err(0,"[Error] sp_proc_id == %d. ",pst->sp_proc_id);
		return ISUP_CM_FAILED;
	}
	
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_RESET_RSP;
	if(ptr != NULL)
	{	
		memcpy(&isup_sd_pdus,ptr,sizeof(SiPduGRA));
	}		
	isup_cpc_proc(pst->sp_proc_id);		
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}


int isup_bls_stop_req(Pst *pst)
{
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	if(pst->sp_proc_id == 0 || pst->sp_proc_id >= ISUP_MAX_PORT)
	{	
		isup_log_err(0,"[Error] sp_proc_id == %d. ",pst->sp_proc_id);
		return ISUP_CM_FAILED;
	}
	
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_STOP_REQ;	
	isup_bls_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}


int isup_mgbs_stop_req(Pst *pst)
{
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	if(pst->sp_proc_id >= ISUP_MAX_PORT)
	{	
		isup_log_err(0,"[Error] sp_proc_id == %d. ",pst->sp_proc_id);
		return ISUP_CM_FAILED;
	}
	
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_STOP_REQ;
	isup_mgbs_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}

int isup_crs_stop_req(Pst *pst)
{
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	if(pst->sp_proc_id >= ISUP_MAX_PORT)
	{	
		isup_log_err(0,"[Error] sp_proc_id == %d. ",pst->sp_proc_id);
		return ISUP_CM_FAILED;
	}
	
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_STOP_REQ;
	isup_crs_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}

int isup_cgrs_stop_req(Pst *pst)
{
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s\r\n", __FUNCTION__);
	if(pst->sp_proc_id >= ISUP_MAX_PORT)
	{	
		isup_log_err(0,"[Error] sp_proc_id == %d. ",pst->sp_proc_id);
		return ISUP_CM_FAILED;
	}
	isup_db.port_pond[pst->sp_proc_id].primitive_cmd = ISUP_STOP_REQ;
	isup_cgrs_proc(pst->sp_proc_id);
	if(isup_db.port_pond[pst->sp_proc_id].primitive_cmd != 0)
	{	
		isup_trace_func(pst->sp_proc_id,"[waring]primitive cmd discard.(cmd=%d)",isup_db.port_pond[pst->sp_proc_id].primitive_cmd);
		isup_db.port_pond[pst->sp_proc_id].primitive_cmd = 0;
	}

	return ISUP_CM_OK;
}


int isup_data_req(Pst *pst,  u8 msgType, SiAllPdus *ptr, u8 *raw_data, u8 len)
{
//	if(find_port_by_sap(pst)==-1)
//	{
//		isup_trace_func(0,"APP => function trace: %s(msgType=%d)\r\n", __FUNCTION__,msgType);
//		return ISUP_CM_FAILED;
//	}
	isup_trace_func(pst->sp_proc_id,"APP => function trace: %s(msgType=%d)\r\n", __FUNCTION__,msgType);
	memcpy(&isup_sd_pdus,ptr,sizeof(SiAllPdus));
	
	memset(&isup_raw_msg,0,sizeof(up_message));
	isup_raw_msg.len	=	len;
	memcpy(isup_raw_msg.msgList,raw_data,isup_raw_msg.len);		
	if(isup_send_msg(pst->sp_proc_id,msgType)==-1)
		return ISUP_CM_FAILED;
	else
		return ISUP_CM_OK;
}



int isup_set_run_mode (u8 run_mode, u32 alter_ip)
{
	isup_trace_func(0,"APP => function trace: %s\r\n", __FUNCTION__);
	isup_db.attrib.isup_run_mode = run_mode % 2;
	isup_db.attrib.isup_remote_ip = alter_ip;
	return ISUP_CM_OK;
}

int isup_inquire_cic (u16 cg_id, u32 cic)
{
	Pst pst;
	const pal_cg_struct* cg_str;
	
	pst.cg_id = cg_id;
	pst.cic = cic;
	
	cg_str = pal_cg_ptr(cg_id);	
	if(cg_str == NULL)
		return -3;
	
	if (check_outbound_route((cg_str->attrib.network_id & 3), cg_str->attrib.dpc, 0) < 0)
		return -1;	
		
	if(find_port_by_sap(&pst)==-1)
		return -2;

	if(isup_db.port_pond[pst.sp_proc_id].cic_state.call_state == IDLE_FLAG &&
		!(isup_db.port_pond[pst.sp_proc_id].cic_state.blo_state & M_REMOTE_BLOCK))
		return 1;
	else
		return 0;
}


