#include "./include/inc.h"
#include "./include/var_ext.h"
#include "./include/biccif.h"
/**************CPCI*************************/
#define CPCI_IDLE   					0
#define CPCI_WAIT_CONTINUITY 			1
#define CPCI_WAIT_ACM					2
#define CPCI_WAIT_ANM					3
#define CPCI_ANSWERED					4
#define CPCI_SUSPENDED					5
#define CPCI_WAIT_RLC					6
#define CPCI_WAIT_REL_COMP				7
#define CPCI_WAIT_RESET					8
#define CPCI_WAIT_GROUP_RESET			9

/*************CPCO*************************/
#define CPCO_IDLE						0
#define CPCO_WAIT_CONTINUITY			1
#define CPCO_WAIT_ACM					2
#define CPCO_WAIT_ANM					3
#define CPCO_ANSWERED					4
#define CPCO_SUSPENDED					5
#define CPCO_WAIT_RLC					6
#define CPCO_WAIT_REL_COMP 				7
#define CPCO_WAIT_RESET					8
#define CPCO_WAIT_GROUP_RESET			9

/********************************************/
#define MGBS_IDLE						0
#define MGBS_WAIT_CGBA					1
#define MGBS_WAIT_CGUA					2
/********************************************/
#define MGBR_IDLE						0
#define MGBR_WAIT_BLOCKING_RESP			1
#define MGBR_WAIT_UNBLOCKING_RESP		2
/********************************************/
#define CRS_IDLE						0
#define CRS_WAIT_REL					1
/*******************************************/
#define CRR_IDLE						0
#define CRR_WAIT_RESPONSE				1
#define CRR_RESET						2
#define CRR_RESET_COMP					3
/*******************************************/
#define CGRS_IDLE						0
#define CGRS_WAIT_GRA					1
/*******************************************/
#define CGRR_IDLE						0
#define CGRR_WAIT_GROUP_RESET_COMP		1
/********************************************/
#define BLR_IDLE						0
#define BLR_WAIT_BLOCKING_RESP			1
#define BLR_REMOTE_LOCKED				2
#define BLR_WAIT_UNBLOCKING_RESP		3
/********************************************/
#define BLS_IDLE						0
#define BLS_WAIT_BLA					1
#define BLS_LOCAL_LOCKED				2
#define BLS_WAIT_UBA					3
/*******************************************/



BiccData_struct 				Bicc_Data;
const BiccTimer_struct 			*TIMER_PTR = &Timer_Data;


extern BiccReadPrim_union		readPrim_union[BICC_MAX_PORT];
extern BiccWritePrim_union 		writePrim_union[BICC_MAX_PORT];
//extern BICC_Resource_pond 		BiccResource;
extern BiccMsg_union			msg_union[BICC_MAX_PORT];
extern u8 						local_platid;

extern void bicc_send_apm(u32 pid);
extern void bicc_log_err(const char *fmt, ...);
extern void bicc_send_res(u32 pid);
extern void bicc_send_sus(u32 pid);
extern void bicc_send_sam(u32 pid);
extern void bicc_send_rsc(u32 pid);
extern void bicc_send_rlc(u32 pid);
extern void bicc_send_rel(u32 pid);
extern void bicc_send_iam(u32 pid);
extern void bicc_send_con(u32 pid);
extern void bicc_send_gra(u32 pid);
extern void bicc_send_grs(u32 pid);
extern void bicc_send_cgua(u32 pid);
extern void bicc_send_cgu(u32 pid);
extern void bicc_send_cgba(u32 pid);
extern void bicc_send_cgb(u32 pid);
extern void bicc_send_cpg(u32 pid);
extern void bicc_send_anm(u32 pid);
extern void bicc_send_acm(u32 pid);
extern BOOL check_crs_waiting(u32 pid);
extern int	check_outbound_route(BYTE nw, long dpc, BYTE sls);


void bicc_cpc_clear(u32 pid)
{
	memset(&Bicc_Data.port_data[pid], 0, sizeof(BICC_Port_struct));
}


BOOL bicc_cgrs_func(u32 start_pid, u8 range)
{
	int		i;
	u32		pid;
	
	if(start_pid+range >= BICC_MAX_PORT)
		return 0;
	for(i = 0; i <= range; i++)
	{
		pid = start_pid + i;
		Bicc_Data.port_data[pid].cic_state.call_state = BICC_RESET;

	}
	return 1;
}

BOOL bicc_cgrsack_func(u32 start_pid, const RangeStatus_struct *rs_ptr)
{
	int 		i;
	u32		pid;
	
	if(start_pid+rs_ptr->range >= BICC_MAX_PORT)
		return 0;
	for(i = 0; i <= rs_ptr->range; i++)
	{
		pid = start_pid + i;
		Bicc_Data.port_data[pid].cic_state.call_state = BICC_CIC_IDLE;
    	memset(&Bicc_Data.port_data[pid], 0, sizeof(BICC_Port_struct));

		if((rs_ptr->status[i>>3]>>(i&0x07)) & 0x01)
			Bicc_Data.port_data[pid].cic_state.blo_state |= M_REMOTE_BLOCK;
		else
			Bicc_Data.port_data[pid].cic_state.blo_state &= 0x0d;
	}
	//BiccResource.circuit_pond[start_pid/BICC_CIRCUIT_CIC].active_flag = 1;
	return 1;
}

BOOL bicc_cgrr_func(u32 start_pid, u8 range, RangeStatus_struct *rs_ptr)
{
	int		i;
	u32		pid;
	
	rs_ptr->range = range;
	memset(rs_ptr->status, 0, 4);
	if(start_pid+range >= BICC_MAX_PORT)
		return 0;
	for(i = 0; i <= range; i++)
	{
		pid = start_pid + i;
		Bicc_Data.port_data[pid].cic_state.blo_state &= 0x0d;
		if(Bicc_Data.port_data[pid].cic_state.blo_state & M_LOCAL_BLOCK)
		{
			if(Bicc_Data.port_data[pid].fsm_state.mgbs_state != MGBS_WAIT_CGUA)
				rs_ptr->status[i>>3]|= 0x01 << (i&0x07);
		}
	}
	return 1;
}


void bicc_set_cause(CauseInd_struct *cause_ptr, u8 val)
{
	cause_ptr->pres = 1;
	cause_ptr->codingStd = 0;	//CCITT standardized coding
	cause_ptr->location = 5;	//private network serving the remote user
	cause_ptr->causeVal = val;
}

void bicc_circuit_reset(u32 pid, const char *s)
{
	#ifdef BICC_DEBUG
		bicc_log_err("BICC circuit reset!, %s\n", s);
	#endif
	
	if(check_crs_waiting(pid))
		return;
	Bicc_Data.port_data[pid].primitive_cmd = BICC_RESET_REQ;
}


BOOL bicc_localblocking_func(u32 start_pid, const RangeStatus_struct *rs_ptr)
{
	int 		i;
	u32		pid;
	
	if(start_pid+rs_ptr->range >= BICC_MAX_PORT)
		return 0;
	for(i = 0; i <= rs_ptr->range; i++)
	{
		pid = start_pid + i;
		if((rs_ptr->status[i>>3]>>(i&0x07)) & 0x01)
			Bicc_Data.port_data[pid].cic_state.blo_state &= M_LOCAL_BLOCK;
	}
	return 1;
}

BOOL bicc_remoteblocking_func(u32 start_pid, const RangeStatus_struct *rs_ptr)
{
	int 	i;
	u32		pid;
	
	if(start_pid+rs_ptr->range >= BICC_MAX_PORT)
		return 0;
	for(i = 0; i <= rs_ptr->range; i++)
	{
		pid = start_pid + i;
		if((rs_ptr->status[i>>3]>>(i&0x07)) & 0x01)
			Bicc_Data.port_data[pid].cic_state.blo_state &= M_REMOTE_BLOCK;
	}
	return 1;
}

BOOL bicc_cpcoblocking_func(u32 start_pid, const RangeStatus_struct *rs_ptr)
{
	int		i;
	u32		pid;
	
	if(start_pid+rs_ptr->range >= BICC_MAX_PORT)
		return 0;
	for(i = 0; i <= rs_ptr->range; i++)
	{
		pid = start_pid + i;
		if((rs_ptr->status[i>>3]>>(i&0x07)) & 0x01)
		{
			if(Bicc_Data.port_data[pid].cic_state.call_state == BICC_OUTGOING)
				Bicc_Data.port_data[pid].internal_cmd = (BICC_CMD_BLO|BICC_SI_MGBR);
		}
	}
	return 1;
}

BOOL bicc_localunblocking_func(u32 start_pid, const RangeStatus_struct *rs_ptr)
{
	int		i;
	u32		pid;
	
	if(start_pid+rs_ptr->range >= BICC_MAX_PORT)
		return 0;
	for(i = 0; i <= rs_ptr->range; i++)
	{
		pid = start_pid + i;
		if((rs_ptr->status[i>>3]>>(i&0x07)) & 0x01)
			Bicc_Data.port_data[pid].cic_state.blo_state &= 0x0e;
	}
	return 1;
}

BOOL bicc_remoteunblocking_func(u32 start_pid, const RangeStatus_struct *rs_ptr)
{
	int		i;
	u32		pid;
	
	if(start_pid+rs_ptr->range >= BICC_MAX_PORT)
		return 0;
	for(i = 0; i <= rs_ptr->range; i++)
	{
		pid = start_pid + i;
		if((rs_ptr->status[i>>3]>>(i&0x07)) & 0x01)
		      Bicc_Data.port_data[pid].cic_state.blo_state &= 0x0d;
	}
	return 1;
}


BOOL bicc_event_handle(u32 pid, BiccReadPrim_union *prm_ptr, u8 primitive, u8 eventType)
{
	BICC_EventHandle_struct 		*eh_ptr = NULL;
	Pst 							pst;
	u32 							cic;
	int								circuit_id, offset, ret = -1;
	const pal_circuit_struct 		*pcircuit = NULL;
	const pal_cg_struct				*pcg = NULL;
	
	circuit_id = pid / BICC_CIRCUIT_CIC;
	offset = pid % BICC_CIRCUIT_CIC;
	pcircuit = pal_circuit_ptr(circuit_id);
	if(pcircuit == NULL)
		return BICC_CM_FAILED;
	cic = pcircuit->attrib.head_cic + offset;

	pst.cg_id = pcircuit->cg_id;
	pcg = pal_cg_ptr(pst.cg_id);
	if(pcg == NULL)
		return BICC_CM_FAILED;
	if(pcg->attrib.protocol != PROTO_BICC)
		return BICC_CM_FAILED;
	pst.tg_id = pcg->attrib.tg_id;
	pst.sp_proc_id = pid;
	pst.su_proc_id = Bicc_Data.port_data[pid].su_proc_id;
	pst.cic = cic;
	pst.trace_flag = 0;

	eh_ptr = (BICC_EventHandle_struct *)pal_get_handler(pst.cg_id);
	if(eh_ptr == NULL)
		return BICC_CM_FAILED;
	
	switch(primitive)
		{
			case BICC_SETUP_IND:
				if(eh_ptr->h_bicc_setup_ind != NULL)
				{
					ret = eh_ptr->h_bicc_setup_ind(&pst, prm_ptr);
					if(pst.trace_flag == 1)
					{
						//BiccResource.port_pond[pid].trace_flag = 1;
						Bicc_Data.port_data[pid].trace_flag = 1;
						monitor_bicc_msg(r_mtp_msg.msgList, r_mtp_msg.len, 0, cic, pid);
					}
				}
				else 
					bicc_log_err("Setup indicate handle is not registered.\n");
				if(ret != -1)
					Bicc_Data.port_data[pid].su_proc_id = pst.su_proc_id;
				break;
					
			case BICC_SETUP_CNF:
				if(eventType == EN_CON)
				{
					if(eh_ptr->h_bicc_setup_con_cnf != NULL)
					{
						if(!eh_ptr->h_bicc_setup_con_cnf(&pst, prm_ptr))
							return 0;
					}
					else 
						bicc_log_err("Setup confirm handle is not registered.\n");
						break;
						
				}
				else
				{
					if(eh_ptr->h_bicc_setup_anm_cnf != NULL)			
						eh_ptr->h_bicc_setup_anm_cnf(&pst, prm_ptr);
					else
						bicc_log_err("Setup confirm handle is not registered.\n");
				}
				
				break;
			
			case BICC_ALERT_IND:
				if(eh_ptr->h_bicc_alert_ind != NULL)
				{
					if(!eh_ptr->h_bicc_alert_ind(&pst, prm_ptr))
						return 0;
				}
				else 
					bicc_log_err("Alert indication handle is not registered.\n");
				break;
					
			case BICC_RELEASE_IND:
				if(eh_ptr->h_bicc_release_ind != NULL)
				{
					if(!eh_ptr->h_bicc_release_ind(&pst, prm_ptr))
						return 0;
				}
				else 
					bicc_log_err("Release indication is not registered.\n");
				break;
					
			case BICC_APM_IND:
				if(eh_ptr->h_bicc_apm_ind != NULL)
				{
					if(!eh_ptr->h_bicc_apm_ind(&pst, prm_ptr))
					return 0;
				}
				else 
					bicc_log_err("APM indication is not registered.\n");
				break;
					
			case BICC_RELEASE_CNF:
				if(eh_ptr->h_bicc_release_cnf != NULL)
				{
					if(!eh_ptr->h_bicc_release_cnf(&pst, prm_ptr))
						return 0;
				}
				else 
					bicc_log_err("Release confirm is not registered.\n");
				break;
					
			case BICC_INFO_IND:
				if(eh_ptr->h_bicc_info_ind != NULL)
				{
					if(!eh_ptr->h_bicc_info_ind(&pst, prm_ptr))
						return 0;
				}
				else
					bicc_log_err("info indication is not registered.\n");
				break;
					
			case BICC_SUSPEND_IND:
				if(eh_ptr->h_bicc_suspend_ind != NULL)
				{
					if(!eh_ptr->h_bicc_suspend_ind(&pst, prm_ptr))
						return 0;
				}
				else 
					bicc_log_err("Suspend indication is not registered.\n");
				break;
					
			case BICC_RESUME_IND:
				if(eh_ptr->h_bicc_resume_ind != NULL)
				{
					if(!eh_ptr->h_bicc_resume_ind(&pst, prm_ptr))
						return 0;
				}
				else
					bicc_log_err("Resume indication is not registered.\n");
				break;
					
			case BICC_PROG_IND:
				if(eh_ptr->h_bicc_prog_ind != NULL)
					eh_ptr->h_bicc_prog_ind(&pst, prm_ptr);
				else
					bicc_log_err("Call progress indication handle is not registered.\n");
				break;
			case BICC_RESET_IND:
				if(eh_ptr->h_bicc_reset_ind != NULL)
						eh_ptr->h_bicc_reset_ind(&pst, prm_ptr);

				else
					bicc_log_err("Reset indicate handle is not registered.\n");
				break;
			case BICC_RESET_CNF:
				if(eh_ptr->h_bicc_reset_cnf != NULL)
				{
				  if(eventType == EN_GROUP)
                  	eh_ptr->h_bicc_reset_cnf(&pst, prm_ptr);
                  else
                  	eh_ptr->h_bicc_reset_cnf(&pst, NULL);
				}
				else
					bicc_log_err("Reset cnf handle is not registered.\n");
				break;
		case BICC_FAILURE_IND:
				if(eh_ptr->h_bicc_failure_ind != NULL)
					eh_ptr->h_bicc_failure_ind(&pst, prm_ptr);
				else
					bicc_log_err("Failure indicate handle is not registered.\n");
				break;
		case BICC_REATTEMPT_IND:
				if(eh_ptr->h_bicc_reattempt_ind != NULL)
					eh_ptr->h_bicc_reattempt_ind(&pst, prm_ptr);
				else
					bicc_log_err("Reattempt indicate handle is not registered.\n");
				break;
		case BICC_BLOCK_IND:
                if(eh_ptr->h_bicc_block_ind != NULL)
                {
                	if(eventType == EN_GROUP)
                    	eh_ptr->h_bicc_block_ind(&pst, prm_ptr);                     
                    else
                    	eh_ptr->h_bicc_block_ind(&pst, NULL);
                }
                else
                   	bicc_log_err("Block indicate handle is not registered.\n");
                break;
                
       case BICC_BLOCK_CNF:
       			if(eh_ptr->h_bicc_block_cnf != NULL)
                {
        	        if(eventType == EN_GROUP)
            	        eh_ptr->h_bicc_block_cnf(&pst, prm_ptr);
                    else
                        eh_ptr->h_bicc_block_cnf(&pst, NULL);
                }
                else
                    bicc_log_err("Block confirm handle is not registered.\n");
                break;
       case BICC_UNBLOCK_IND:
                if(eh_ptr->h_bicc_unblock_ind != NULL)
                {
                	if(eventType == EN_GROUP)
                    	eh_ptr->h_bicc_unblock_ind(&pst, prm_ptr);
                    else
                    	eh_ptr->h_bicc_unblock_ind(&pst, NULL);
                }
                else
                    bicc_log_err("Unblock indicate handle is not registered.\n");
                break;
       case BICC_UNBLOCK_CNF:
                if(eh_ptr->h_bicc_unblock_cnf != NULL)
                {
                	if(eventType == EN_GROUP)
                    	eh_ptr->h_bicc_unblock_cnf(&pst, prm_ptr);
                   	else
                        eh_ptr->h_bicc_unblock_cnf(&pst, NULL);
                }
                else
                    bicc_log_err("Unblock confirm handle is not registered.\n");
                break;
		default:
				break;
	}
	return 1;
}


void bicc_dualseizure_func(u32 pid)
{
	int 						cg_id;
	const pal_circuit_struct 	*pcircuit = NULL;
	const pal_cg_struct 		*pcg = NULL;
	BiccTimer_struct 			*tempTimer = &Bicc_Data.timer_data[pid];
	BICC_Port_struct 			*port_ptr = &Bicc_Data.port_data[pid];
	pcircuit = pal_circuit_ptr(pid / BICC_CIRCUIT_CIC);
	if(pcircuit == NULL)
		return;
	cg_id = pcircuit->cg_id;
	pcg = pal_cg_ptr(cg_id);
	if(pcg == NULL)
		return;
	if(pcg->attrib.priority == 0)
		return;
	tempTimer->t7 = BICC_TIMER_STOP;
	bicc_event_handle(pid, NULL, BICC_REATTEMPT_IND, EN_NONE);
	bicc_cpc_clear(pid);
	Bicc_Data.port_data[pid].cic_state.call_state = BICC_INCOMING;
	port_ptr->sprc_cmd = M_IAM;
}


void bicc_cpci_proc(u32 pid)
{
	BICC_Port_struct 				*port_ptr = &Bicc_Data.port_data[pid];
	BiccTimer_struct				*tempTimer = &Bicc_Data.timer_data[pid];

	switch(port_ptr->fsm_state.cpc_state)
	{
		case CPCI_IDLE:
			if(port_ptr->sprc_cmd == M_IAM)
			{
				port_ptr->sprc_cmd = 0;
				if(readPrim_union[pid].setup_ind.callingPtyCat.val != 0x0d)                 //test call?
				{
					if(port_ptr->cic_state.blo_state & M_REMOTE_BLOCK)
						port_ptr->cic_state.blo_state &= 0x01;			//set remote unblocked
					if(port_ptr->cic_state.blo_state & M_LOCAL_BLOCK)
					{
						bicc_cpc_clear(pid);
						return;
					}

				}
				if(bicc_event_handle(pid, &readPrim_union[pid], BICC_SETUP_IND, EN_NONE))
				{
					port_ptr->fsm_state.cpc_state = CPCI_WAIT_ACM;
								
				}
				else
				{
					bicc_circuit_reset(pid, "setup ind rejected");
					bicc_cpc_clear(pid);
					return;
				}
			}
			else if(port_ptr->sprc_cmd == M_REL)
			{
				port_ptr->sprc_cmd = 0;
				writePrim_union[pid].rel_rsp.cause.pres = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_IND, EN_NONE);
				bicc_send_rlc(pid);
				bicc_cpc_clear(pid);
				return ;
			}
			else if(port_ptr->sprc_cmd == M_RLC)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_CNF, EN_NONE);
				bicc_cpc_clear(pid);
				return ;
			}
			else if(port_ptr->sprc_cmd !=0 )
			{
				port_ptr->sprc_cmd = 0;
				port_ptr->internal_cmd = (BICC_CMD_RESET|BICC_SI_CPCI);
			  	bicc_circuit_reset(pid, "unexpected message");
				bicc_cpc_clear(pid);
				return ;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRR))
			{
				port_ptr->internal_cmd = (BICC_CMD_RESET_COMP|BICC_SI_CRR);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRR))
			{
				port_ptr->internal_cmd = (BICC_CMD_RESET_COMP|BICC_SI_CGRR);
				bicc_cpc_clear(pid);
				return ;
			}
					
			break;
		case CPCI_WAIT_CONTINUITY:
			break;
		case CPCI_WAIT_ACM:
			if(port_ptr->primitive_cmd == BICC_ALERT_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_acm(pid);
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_ANM;
			}
			else if(port_ptr->primitive_cmd == BICC_SETUP_RSP)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_con(pid);
				port_ptr->fsm_state.cpc_state = CPCI_ANSWERED;
				tempTimer->t9 = TIMER_PTR->t9;
			}
			else if(port_ptr->primitive_cmd == BICC_RELEASE_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_rel(pid);
				
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->w_time = 0;

				port_ptr->fsm_state.cpc_state = CPCI_WAIT_RLC;
			}
			else if(port_ptr->sprc_cmd == M_REL)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_REL_COMP;
			}
			else if(port_ptr ->sprc_cmd == M_RLC)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_FAILURE_IND, EN_NONE);
				bicc_set_cause(&writePrim_union[pid].rel_req.cause, 0x10);	//Normal call clearing
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->w_time = 0;
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_RLC;
			}
/*			else if(port_ptr->sprc_cmd == M_SAM)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_INFO_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_ACM;
			}
*/
			else if(port_ptr->sprc_cmd != 0)
			{
				port_ptr->sprc_cmd = 0;
				port_ptr->internal_cmd = (BICC_CMD_RESET|BICC_SI_CPCI);
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_circuit_reset(pid, "unexpected message cpci wait acm");
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRR))
			{
				port_ptr->internal_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_RESET;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRR))
			{
				port_ptr->internal_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_GROUP_RESET;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_BLO|BICC_SI_MGBS))
			{
				port_ptr->internal_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_FAILURE_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRS))
			{
				port_ptr->internal_cmd= 0;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRS))
			{
				port_ptr->internal_cmd = 0;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			break;

		case CPCI_WAIT_ANM:
			if(port_ptr->primitive_cmd == BICC_SETUP_RSP)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_anm(pid);
				port_ptr->fsm_state.cpc_state = CPCI_ANSWERED;
			}
			else if(port_ptr->primitive_cmd == BICC_RELEASE_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->w_time = 0;
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_RLC;
			}
			else if(port_ptr->primitive_cmd == BICC_PROG_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_cpg(pid);
			}
			else if(port_ptr->sprc_cmd == M_REL)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_REL_COMP;
			}
			else if(port_ptr->sprc_cmd == M_RLC)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_FAILURE_IND, EN_NONE);
				bicc_set_cause(&writePrim_union[pid].rel_req.cause, 0x10);	//Normal call clearing
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->w_time = 0;
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_RLC;
			}
#ifdef BICC_TOLERANT
			// To avoid both side backoff //
			else if(port_ptr->sprc_cmd != 0)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_circuit_reset(pid, "unexpected message cpci wait anm");
				bicc_cpc_clear(pid);
				return;
			}
#endif
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRR))
			{
				port_ptr->internal_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_RESET;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRR))
			{
				port_ptr->internal_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_GROUP_RESET;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRS))
			{
				port_ptr->internal_cmd = 0;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRS))
			{
				port_ptr->internal_cmd = 0;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
		//	break;
			else if(tempTimer->t9-- == BICC_TIMER_EXPIRED)
            {
                bicc_event_handle(pid, &readPrim_union[pid], BICC_FAILURE_IND, EN_NONE);
                bicc_set_cause(&writePrim_union[pid].rel_req.cause, 0x13); 
                bicc_send_rel(pid);
                tempTimer->t5 = TIMER_PTR->t5;
                tempTimer->t1 = TIMER_PTR->t1;
               	port_ptr->fsm_state.cpc_state = CPCI_WAIT_RLC;
            }
			break;

		case CPCI_ANSWERED:
			if(port_ptr->primitive_cmd == BICC_SUSPEND_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_sus(pid);
				port_ptr->cic_state.sus_state = BICC_TERM_SUSPEND;
				port_ptr->fsm_state.cpc_state = CPCI_SUSPENDED;
			}
			else if(port_ptr->primitive_cmd == BICC_RELEASE_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->w_time = 0;
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_RLC;
			}
			else if(port_ptr->sprc_cmd == M_SUS)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_SUSPEND_IND, EN_NONE);
				port_ptr->cic_state.sus_state = BICC_ORIG_SUSPEND;
				port_ptr->fsm_state.cpc_state = CPCI_SUSPENDED;
			}
			else if(port_ptr->sprc_cmd == M_REL)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_REL_COMP;
			}
			else if(port_ptr->sprc_cmd == M_RLC)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_FAILURE_IND, EN_NONE);
				bicc_set_cause(&writePrim_union[pid].rel_req.cause, 0x10);	//Normal call clearing
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->w_time = 0;
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_RLC;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRR))
			{
				port_ptr->internal_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_RESET;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRR))
			{
				port_ptr->internal_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_GROUP_RESET;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRS))
			{
				port_ptr->internal_cmd = 0;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRS))
			{
				port_ptr->internal_cmd = 0;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			break;
		case CPCI_SUSPENDED:
			if(port_ptr->primitive_cmd == BICC_RESUME_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_res(pid);
				if(port_ptr->cic_state.sus_state == BICC_TERM_SUSPEND)
					port_ptr->fsm_state.cpc_state = CPCI_ANSWERED;
			}
			else if(port_ptr->primitive_cmd == BICC_SUSPEND_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_sus(pid);
			}
			else if(port_ptr->primitive_cmd == BICC_RELEASE_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->w_time = 0;
				port_ptr->fsm_state.cpc_state= CPCI_WAIT_RLC;
			}
			else if(port_ptr->sprc_cmd == M_RES)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESUME_IND, EN_NONE);
				if(port_ptr->cic_state.sus_state == BICC_ORIG_SUSPEND)
					port_ptr->fsm_state.cpc_state = CPCI_ANSWERED;
			}
			else if(port_ptr->sprc_cmd == M_SUS)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_SUSPEND_IND, EN_NONE);
			}
			else if(port_ptr->sprc_cmd == M_REL)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_REL_COMP;
			}
			else if(port_ptr->sprc_cmd == M_RLC)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_FAILURE_IND, EN_NONE);
				bicc_set_cause(&writePrim_union[pid].rel_req.cause, 0x10);	//Normal call clearing
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->w_time = 0;
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_RLC;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRR))
			{
				port_ptr->internal_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_RESET;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRR))
			{
				port_ptr->internal_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCI_WAIT_GROUP_RESET;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRS))
			{
				port_ptr->internal_cmd = 0;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRS))
			{
				port_ptr->internal_cmd = 0;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			break;

		case CPCI_WAIT_RLC:
			if(port_ptr->sprc_cmd== M_RLC)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_CNF, EN_NONE);
				tempTimer->t5 = BICC_TIMER_STOP;
				tempTimer->t1 = BICC_TIMER_STOP;	
				port_ptr->w_time = 0;
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->sprc_cmd == M_REL)
			{
				port_ptr->sprc_cmd = 0;
				writePrim_union[pid].rel_rsp.cause.pres = 0;
				bicc_send_rlc(pid);
				
			}
			else if(tempTimer->t1-- == BICC_TIMER_EXPIRED)
			{
				bicc_send_rel(pid);
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->w_time = 0;
				
			}
			else if(tempTimer->t5-- == BICC_TIMER_EXPIRED)
			{
				tempTimer->t1 = BICC_TIMER_STOP;
				port_ptr->w_time = 0;
				bicc_circuit_reset(pid, "t5 expired cpci wait rlc");				
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_CNF, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			//if(cpc_ptr->crr_cmd == BICC_RESET_IND)
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRR))
			{
				port_ptr->internal_cmd = 0;
				//port_ptr->fsm_state.cpc_state = BICC_RESET_RSP;
				port_ptr->internal_cmd = (BICC_CMD_RESET_COMP|BICC_SI_CPCI);
				tempTimer->t5 = BICC_TIMER_STOP;
				tempTimer->t1 = BICC_TIMER_STOP;
				port_ptr->w_time = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_CNF, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRR))
			{
				port_ptr->internal_cmd = 0;
				tempTimer->t5 = BICC_TIMER_STOP;
				tempTimer->t1 = BICC_TIMER_STOP;
				port_ptr->w_time = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_CNF, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRS))
			{
				port_ptr->internal_cmd = 0;
				tempTimer->t5 = BICC_TIMER_STOP;
				tempTimer->t1 = BICC_TIMER_STOP;
				port_ptr->w_time = 0;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRS))
			{
				port_ptr->internal_cmd = 0;
				tempTimer->t5 = BICC_TIMER_STOP;
				tempTimer->t1 = BICC_TIMER_STOP;
				port_ptr->w_time = 0;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid,&readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			break;
		case CPCI_WAIT_REL_COMP:
			if(port_ptr->primitive_cmd == BICC_RELEASE_RSP)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_rlc(pid);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRR))
			{
				port_ptr->internal_cmd = 0;
				//port_ptr->fsm_state.cpc_state = BICC_RESET_RSP;
				port_ptr->internal_cmd = (BICC_CMD_RESET_COMP|BICC_SI_CPCI);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRR))
			{
				port_ptr->internal_cmd = 0;
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRS))
			{
				port_ptr->internal_cmd = 0;
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRS))
			{
				port_ptr->internal_cmd = 0;
				bicc_cpc_clear(pid);
				return;
			}
#ifdef BICC_TOLERANT
			else if(port_ptr->sprc_cmd == M_REL)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_IND, EN_NONE);
			}

#endif
			break;
		case CPCI_WAIT_RESET:
			if(port_ptr->primitive_cmd == BICC_RESET_RSP)
			{
				port_ptr->primitive_cmd = 0;
				//port_ptr->fsm_state.cpc_state = BICC_RESET_RSP;
				port_ptr->internal_cmd = (BICC_CMD_RESET_COMP|BICC_SI_CPCI);
				bicc_cpc_clear(pid);
				return;
			}
			break;
		case CPCI_WAIT_GROUP_RESET:
			if(port_ptr->primitive_cmd == BICC_RESET_RSP)
			{
				port_ptr->primitive_cmd = 0;
				bicc_cpc_clear(pid);
				return;
			}
			break;
		default:
			break;
	}
}


inline void bicc_cpco_proc(u32 pid)
{
	BICC_Port_struct 	*port_ptr = &Bicc_Data.port_data[pid];
	BiccTimer_struct	*tempTimer = &Bicc_Data.timer_data[pid];
	
	switch(port_ptr->fsm_state.cpc_state)
	{
		case CPCO_IDLE:
			if(port_ptr->primitive_cmd== BICC_SETUP_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_iam(pid);
				tempTimer->t7 = TIMER_PTR->t7;
				port_ptr->w_time = 0;
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_ACM;
			}
			else
				bicc_cpc_clear(pid);
			break;
		case CPCO_WAIT_CONTINUITY:
			break;
		case CPCO_WAIT_ACM:
			if(port_ptr->primitive_cmd == BICC_RELEASE_REQ)
			{
				port_ptr->primitive_cmd = 0;
				tempTimer->t7 = BICC_TIMER_STOP;
				
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->w_time = 0;
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_RLC;
			}
			else if(port_ptr->primitive_cmd == BICC_INFO_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_sam(pid);
				tempTimer->t7 = TIMER_PTR->t7;
				port_ptr->w_time = 0;
			}
			else if(port_ptr->sprc_cmd == M_ACM)
			{
				port_ptr->sprc_cmd = 0;
				tempTimer->t7 = BICC_TIMER_STOP;
				tempTimer->t9 = TIMER_PTR->t9;
				port_ptr->w_time = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_ALERT_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_ANM;
			}
			else if(port_ptr->sprc_cmd == M_CON)
			{
				port_ptr->sprc_cmd = 0;
				tempTimer->t7 = BICC_TIMER_STOP;
				port_ptr->w_time = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_SETUP_CNF, EN_CON);
				port_ptr->fsm_state.cpc_state = CPCO_ANSWERED;
			}
/*			else if(port_ptr->sprc_cmd == M_ANM)
			{
				port_ptr->sprc_cmd = 0;
				tempTimer->t7 = BICC_TIMER_STOP;
				port_ptr->w_time = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_SETUP_CNF, EN_ANM);
				port_ptr->fsm_state.cpc_state = CPCO_ANSWERED;
			}		
*/
			else if(port_ptr->sprc_cmd == M_CPG)
			{
				port_ptr->sprc_cmd = 0;	//discard...
			}
			else if(port_ptr->sprc_cmd == M_REL)
			{
				port_ptr->sprc_cmd = 0;
				tempTimer->t7 = BICC_TIMER_STOP;
				port_ptr->w_time = 0;
				tempTimer->user_t10 = TIMER_PTR->user_t10;

			//	fsm_ptr->timer_data.t7 = BICC_TIMER_STOP;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_REL_COMP;
			}
			else if(port_ptr->sprc_cmd == M_RLC)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_CNF, EN_NONE);
				tempTimer->t7 = BICC_TIMER_STOP;
				
				bicc_set_cause(&writePrim_union[pid].rel_req.cause, 0x10);	//Normal call clearing
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->w_time = 0;
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_RLC;
			}
#ifdef BICC_REATTEMPT
			else if(port_ptr->sprc_cmd == M_IAM)
			{
				port_ptr->sprc_cmd = 0;
				bicc_dualseizure_func(pid);
			}
#endif
			else if(port_ptr->sprc_cmd != 0)
			{
				port_ptr->sprc_cmd = 0;
				tempTimer->t7 = BICC_TIMER_STOP;
				port_ptr->w_time = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_circuit_reset(pid, "unexpected message cpco wait acm");
				bicc_cpc_clear(pid);
				return;
			}
			else if(tempTimer->t7-- == BICC_TIMER_EXPIRED)
			{
				bicc_event_handle(pid, &readPrim_union[pid], BICC_FAILURE_IND, EN_NONE);
				bicc_set_cause(&writePrim_union[pid].rel_req.cause, 0x12);	//Normal call clearing
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->w_time = 0;
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_RLC;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_BLO|BICC_SI_MGBR))
			{
				port_ptr->internal_cmd = 0;
				tempTimer->t7 = BICC_TIMER_STOP;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_set_cause(&writePrim_union[pid].rel_req.cause, 0x10);	//Normal call clearing
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->w_time = 0;
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_RLC;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRR))
			{
				port_ptr->internal_cmd= 0;
				tempTimer->t7 = BICC_TIMER_STOP;
				port_ptr->w_time = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_RESET;
				tempTimer->user_t10 = TIMER_PTR->user_t10;

			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRR))
			{
				port_ptr->internal_cmd = 0;
				tempTimer->t7 = BICC_TIMER_STOP;
				port_ptr->w_time = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_GROUP_RESET;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRS))
			{
				port_ptr->internal_cmd= 0;
				tempTimer->t7 = BICC_TIMER_STOP;
				port_ptr->w_time = 0;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRS))
			{
				port_ptr->internal_cmd = 0;
				tempTimer->t7 = BICC_TIMER_STOP;
				port_ptr->w_time = 0;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			break;
		case CPCO_WAIT_ANM:
			if(port_ptr->primitive_cmd == BICC_RELEASE_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->w_time = 0;
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_RLC;
			}
			else if(port_ptr->sprc_cmd == M_ANM)
			{
				port_ptr->sprc_cmd = 0;
				tempTimer->t9 = BICC_TIMER_STOP;
				port_ptr->w_time = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_SETUP_CNF, EN_ANM);
				port_ptr->fsm_state.cpc_state = CPCO_ANSWERED;
			}
			else if(port_ptr->sprc_cmd == M_CPG)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_PROG_IND, EN_NONE);
			}
#ifdef BICC_TOLERANT
			else if(port_ptr->sprc_cmd == M_REL)
			{
				port_ptr->sprc_cmd = 0;
				tempTimer->t9 = BICC_TIMER_STOP;
				port_ptr->w_time = 0;
				tempTimer->user_t10 = TIMER_PTR->user_t10;

				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_REL_COMP;
			}
			else if(port_ptr->sprc_cmd == M_RLC)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_FAILURE_IND, EN_NONE);
				tempTimer->t9 = BICC_TIMER_STOP;
				bicc_set_cause(&writePrim_union[pid].rel_req.cause, 0x10);	//Normal call clearing
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->w_time = 0;
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_RLC;
			}
#endif
#ifdef BICC_TOLERANT
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRR))
			{
				port_ptr->internal_cmd = 0;
				tempTimer->t9 = BICC_TIMER_STOP;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_RESET;
				tempTimer->user_t10 = TIMER_PTR->user_t10;

			}
#endif
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRR))
			{
				port_ptr->internal_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_GROUP_RESET;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRS))
			{
				port_ptr->internal_cmd = 0;
				tempTimer->t9 = BICC_TIMER_STOP;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRS))
			{
				port_ptr->internal_cmd = 0;
				tempTimer->t9 = BICC_TIMER_STOP;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(tempTimer->t9-- == BICC_TIMER_EXPIRED)
            {
            	bicc_event_handle(pid, &readPrim_union[pid], BICC_FAILURE_IND, EN_NONE);
                bicc_set_cause(&writePrim_union[pid].rel_req.cause, 0x10); 
                bicc_send_rel(pid);
                tempTimer->t5 = TIMER_PTR->t5;
               	tempTimer->t1 = TIMER_PTR->t1;	
                port_ptr->fsm_state.cpc_state = CPCO_WAIT_RLC;
            }
			break;
		case CPCO_ANSWERED:
			if(port_ptr->primitive_cmd == BICC_SUSPEND_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_sus(pid);
				port_ptr->cic_state.sus_state = BICC_ORIG_SUSPEND;
				port_ptr->fsm_state.cpc_state = CPCO_SUSPENDED;
			}
			else if(port_ptr->primitive_cmd == BICC_RELEASE_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_RLC;
			}
			else if(port_ptr->sprc_cmd == M_SUS)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid,&readPrim_union[pid], BICC_SUSPEND_IND, EN_NONE);
				port_ptr->cic_state.sus_state = BICC_TERM_SUSPEND;
				port_ptr->fsm_state.cpc_state = CPCO_SUSPENDED;
			}
			else if(port_ptr->sprc_cmd == M_REL)
			{
				port_ptr->sprc_cmd = 0;
				tempTimer->user_t10 = TIMER_PTR->user_t10;

				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_REL_COMP;
			}
			else if(port_ptr->sprc_cmd == M_RLC)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_FAILURE_IND, EN_NONE);
				bicc_set_cause(&writePrim_union[pid].rel_req.cause, 0x10);	//Normal call clearing
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_RLC;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRR))
			{
				port_ptr->internal_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_RESET;
				tempTimer->user_t10 = TIMER_PTR->user_t10;

			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRR))
			{
				port_ptr->internal_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_GROUP_RESET;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRS))
			{
				port_ptr->internal_cmd = 0;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRS))
			{
				port_ptr->internal_cmd = 0;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			break;
		case CPCO_SUSPENDED:
			if(port_ptr->primitive_cmd== BICC_RESUME_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_res(pid);
				if(port_ptr->cic_state.sus_state == BICC_ORIG_SUSPEND)
					port_ptr->fsm_state.cpc_state = CPCO_ANSWERED;
			}
			else if(port_ptr->primitive_cmd == BICC_SUSPEND_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_sus(pid);
			}
			else if(port_ptr->primitive_cmd == BICC_RELEASE_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_RLC;
			}
			else if(port_ptr->sprc_cmd == M_RES)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESUME_IND, EN_NONE);
				if(port_ptr->cic_state.sus_state == BICC_TERM_SUSPEND)
					port_ptr->fsm_state.cpc_state = CPCO_ANSWERED;
			}
			else if(port_ptr->sprc_cmd == M_SUS)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_SUSPEND_IND, EN_NONE);
			}
			else if(port_ptr->sprc_cmd == M_REL)
			{
				port_ptr->sprc_cmd = 0;
				tempTimer->user_t10 = TIMER_PTR->user_t10;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_REL_COMP;
			}
			else if(port_ptr->sprc_cmd == M_RLC)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_FAILURE_IND, EN_NONE);
				bicc_set_cause(&writePrim_union[pid].rel_req.cause, 0x10);	//Normal call clearing
				bicc_send_rel(pid);
				tempTimer->t5 = TIMER_PTR->t5;
				tempTimer->t1 = TIMER_PTR->t1;
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_RLC;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRR))
			{
				port_ptr->internal_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_RESET;
				tempTimer->user_t10 = TIMER_PTR->user_t10;

			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRR))
			{
				port_ptr->internal_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				port_ptr->fsm_state.cpc_state = CPCO_WAIT_GROUP_RESET;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRS))
			{
				port_ptr->internal_cmd= 0;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRS))
			{
				port_ptr->internal_cmd= 0;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			break;
		case CPCO_WAIT_RLC:
			if(port_ptr->sprc_cmd == M_RLC)
			{
				port_ptr->sprc_cmd = 0;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_CNF, EN_NONE);
				tempTimer->t5 = BICC_TIMER_STOP;
				tempTimer->t5 = BICC_TIMER_STOP;
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->sprc_cmd == M_REL)
			{
				port_ptr->sprc_cmd = 0;
				writePrim_union[pid].rel_rsp.cause.pres = 0;
			//	bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_IND, EN_NONE);
				bicc_send_rlc(pid);
			}
			else if(tempTimer->t1-- == BICC_TIMER_EXPIRED)
			{
				bicc_send_rel(pid);
				tempTimer->t1 = TIMER_PTR->t1;
			}
			else if(tempTimer->t5-- == BICC_TIMER_EXPIRED)
			{
				tempTimer->t1 = BICC_TIMER_STOP;
				bicc_circuit_reset(pid, "t5 expired cpco wait rlc");
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_CNF, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRR))
			{
				port_ptr->internal_cmd = 0;
				//port_ptr->fsm_state.cpc_state = BICC_SETUP_RSP;
				port_ptr->internal_cmd = (BICC_CMD_RESET_COMP|BICC_SI_CPCO);
				tempTimer->t5  =  BICC_TIMER_STOP;
				tempTimer->t1  =  BICC_TIMER_STOP;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_CNF, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRR))
			{
				port_ptr->internal_cmd = 0;
				tempTimer->t5 = BICC_TIMER_STOP;
				tempTimer->t1 = BICC_TIMER_STOP;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_CNF, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRS))
			{
				port_ptr->internal_cmd= 0;
				tempTimer->t5 = BICC_TIMER_STOP;
				tempTimer->t1 = BICC_TIMER_STOP;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRS))
			{
				port_ptr->internal_cmd= 0;
				tempTimer->t5 = BICC_TIMER_STOP;
				tempTimer->t1 = BICC_TIMER_STOP;
				//The Ind must sent to CC because this Req is not by CC but BICC locally
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_IND, EN_NONE);
				bicc_cpc_clear(pid);
				return;
			}
			break;
		case CPCO_WAIT_REL_COMP:
			if(port_ptr->primitive_cmd == BICC_RELEASE_RSP)
			{
				port_ptr->primitive_cmd = 0;
				tempTimer->user_t10 = 0;

				bicc_send_rlc(pid);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRR))
			{
				port_ptr->internal_cmd = 0;
				//port_ptr->fsm_state.cpc_state = BICC_RESET_RSP;
				port_ptr->internal_cmd = (BICC_CMD_RESET_COMP|BICC_SI_CPCO);
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRR))
			{
				port_ptr->internal_cmd = 0;
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CRS))
			{
				port_ptr->internal_cmd = 0;
				bicc_cpc_clear(pid);
				return;
			}
			else if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRS))
			{
				port_ptr->internal_cmd = 0;
				bicc_cpc_clear(pid);
				return;
			}
			else if(tempTimer->user_t10-- == BICC_TIMER_EXPIRED)
   		{
     		tempTimer->user_t10 = BICC_TIMER_STOP;
     		bicc_circuit_reset(pid, "user t10 expired cpco wait release complete");
     		//bicc_event_handle(pid, &readPrim_union[pid], BICC_RELEASE_CNF, EN_NONE);
     		bicc_send_rlc(pid);
     		bicc_cpc_clear(pid);
     		return;
   		}

			break;
		case CPCO_WAIT_RESET:
			if(port_ptr->primitive_cmd == BICC_RESET_RSP)
			{
				port_ptr->primitive_cmd = 0;
				//port_ptr->fsm_state.cpc_state = BICC_RESET_RSP;
				port_ptr->internal_cmd = (BICC_CMD_RESET_COMP|BICC_SI_CPCO);
				bicc_cpc_clear(pid);
				return;
			}
			else if(tempTimer->user_t10-- == BICC_TIMER_EXPIRED)
	    	{
        		tempTimer->user_t10 = BICC_TIMER_STOP;
        		bicc_circuit_reset(pid, "user t10 expired cpco wait release complete");
        		bicc_cpc_clear(pid);
        		return;
      		}
			break;
		case CPCO_WAIT_GROUP_RESET:
			if(port_ptr->primitive_cmd == BICC_RESET_RSP)
			{
				port_ptr->primitive_cmd = 0;
				bicc_cpc_clear(pid);
				return;
			}
			break;
		default:
			break;
	}
}


/********************************************************************/
/*	Maintenance Oriented Circuit Group Blocking/Unbolcking Sending	*/
/********************************************************************/
inline void bicc_mgbs_proc(u32 pid)
{
	BICC_Port_struct			*port_ptr = &Bicc_Data.port_data[pid];
	BiccTimer_struct			*tempTimer = &Bicc_Data.timer_data[pid];
	
	u32 						circuit_id;
	const pal_circuit_struct	*pcircuit = NULL;

	circuit_id = pid / BICC_CIRCUIT_CIC;
	pcircuit = pal_circuit_ptr(circuit_id);
	if(pcircuit == NULL)
		return;
	if(pcircuit->attrib.plat_id != local_platid )
		if(Bicc_Data.dmp_data.active == 1)
		{
			if(port_ptr->fsm_state.mgbs_state != MGBS_IDLE)
			{
				port_ptr->fsm_state.mgbs_state = MGBS_IDLE;
				tempTimer->t18 = 0;
				tempTimer->t19 = 0;
			}	
			return ;
		}
	switch(port_ptr->fsm_state.mgbs_state)
	{
		case MGBS_IDLE:
			if(port_ptr->primitive_cmd == BICC_BLOCK_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_localblocking_func(pid, &writePrim_union[pid].cgb_req.rangeStatus);
				bicc_send_cgb(pid);
				tempTimer->t18 = TIMER_PTR->t18;
				tempTimer->t19 = TIMER_PTR->t19;
				port_ptr->fsm_state.mgbs_state = MGBS_WAIT_CGBA;
			}
			else if(port_ptr->primitive_cmd == BICC_UNBLOCK_REQ)
			{
				port_ptr->primitive_cmd = 0;
				bicc_send_cgu(pid);
				tempTimer->t20 = TIMER_PTR->t20;
				tempTimer->t21 = TIMER_PTR->t21;
				port_ptr->fsm_state.mgbs_state = MGBS_WAIT_CGUA;
			}
			if(port_ptr->sprc_cmd == M_CGBA)
			{
				port_ptr->sprc_cmd = 0;
				//discard
			}
			else if(port_ptr->sprc_cmd == M_CGUA)
			{
				port_ptr->sprc_cmd = 0;
				//discard
			}
			break;
		case MGBS_WAIT_CGBA:
			if(port_ptr->sprc_cmd == M_CGBA)
			{
				port_ptr->sprc_cmd = 0;
				if(readPrim_union[pid].cgb_cnf.rangeStatus.range >= 32)
					return;
				tempTimer->t18 = BICC_TIMER_STOP;
				tempTimer->t19 = BICC_TIMER_STOP;
				port_ptr->fsm_state.mgbs_state = MGBS_IDLE;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_BLOCK_CNF, EN_GROUP);
			}

			if(tempTimer->t18 -- == BICC_TIMER_EXPIRED)
			{
				bicc_send_cgb(pid);
				tempTimer->t18 = TIMER_PTR->t18;
			}
			if(tempTimer->t19 -- == BICC_TIMER_EXPIRED)
			{
				bicc_send_cgb(pid);
				tempTimer->t19 = TIMER_PTR->t19;	//ignore first T19 expired
			}
			if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRS))
			{
				port_ptr->internal_cmd = 0;
				tempTimer->t18 = BICC_TIMER_STOP;
				tempTimer->t19 = BICC_TIMER_STOP;
				port_ptr->fsm_state.mgbs_state = MGBS_IDLE;

			}
			break;
		case MGBS_WAIT_CGUA:
			if(port_ptr->sprc_cmd == M_CGUA)
			{
				port_ptr->sprc_cmd = 0;
				if(readPrim_union[pid].cgu_cnf.rangeStatus.range >= 32)
					return;
				tempTimer->t20 = BICC_TIMER_STOP;
				tempTimer->t21 = BICC_TIMER_STOP;
				bicc_localunblocking_func(pid, &readPrim_union[pid].cgu_cnf.rangeStatus);
				bicc_event_handle(pid, &readPrim_union[pid], BICC_UNBLOCK_CNF, EN_GROUP);
				port_ptr->fsm_state.mgbs_state = MGBS_IDLE;
			}

			if(tempTimer->t20 -- == BICC_TIMER_EXPIRED)
			{
				bicc_send_cgu(pid);
				tempTimer->t20 = TIMER_PTR->t20;
			}
			if(tempTimer->t21 -- == BICC_TIMER_EXPIRED)
			{
				bicc_send_cgb(pid);
				tempTimer->t21 = TIMER_PTR->t21;	//ignore first T21 expired
			}
			if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRS))
			{
				port_ptr->internal_cmd = 0;
				tempTimer->t20 = BICC_TIMER_STOP;
				tempTimer->t21 = BICC_TIMER_STOP;
				port_ptr->fsm_state.mgbs_state = MGBS_IDLE;
			}
			break;
		default:
			break;
	}
}

/************************************************************************/
/*	Maintenance Oriented Circuit Group Blocking/Unbolcking Reception	*/
/************************************************************************/
inline void bicc_mgbr_proc(u32 pid)
{
	
	BICC_Port_struct			*port_ptr = &Bicc_Data.port_data[pid];
	//BiccTimer_struct			*tempTimer = &Bicc_Data.timer_data[pid];
	switch(port_ptr->fsm_state.mgbr_state)
	{
		case MGBR_IDLE:
			if(port_ptr->sprc_cmd == M_CGB)
			{
				port_ptr->sprc_cmd = 0;
				if(readPrim_union[pid].cgb_ind.rangeStatus.range >= 32)
					return;
				bicc_cpcoblocking_func(pid, &readPrim_union[pid].cgb_ind.rangeStatus);
				port_ptr->fsm_state.mgbr_state = MGBR_WAIT_BLOCKING_RESP;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_BLOCK_IND, EN_GROUP);
			}
			else if(port_ptr->sprc_cmd == M_CGU)
			{
				port_ptr->sprc_cmd = 0;
				if(readPrim_union[pid].cgu_ind.rangeStatus.range >= 32)
					return;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_UNBLOCK_IND, EN_GROUP);
				port_ptr->fsm_state.mgbr_state = MGBR_WAIT_UNBLOCKING_RESP;
			}
			break;
		case MGBR_WAIT_BLOCKING_RESP:
			if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRR))
			{
				port_ptr->internal_cmd = 0;
				port_ptr->fsm_state.mgbr_state = MGBR_IDLE;
			}
			else
			{
				bicc_remoteblocking_func(pid, &readPrim_union[pid].cgb_ind.rangeStatus);
				memcpy(&writePrim_union[pid].cgb_rsp, &readPrim_union[pid].cgb_ind, sizeof(BiccCgb_Ind));
				bicc_send_cgba(pid);
				port_ptr->fsm_state.mgbr_state = MGBR_IDLE;
			}
			break;
		case MGBR_WAIT_UNBLOCKING_RESP:
			if(port_ptr->internal_cmd == (BICC_CMD_RESET|BICC_SI_CGRR))
			{
				port_ptr->internal_cmd = 0;
				port_ptr->fsm_state.mgbr_state = MGBR_IDLE;
			}
			else
			{
				bicc_remoteunblocking_func(pid, &readPrim_union[pid].cgu_ind.rangeStatus);
				memcpy(&writePrim_union[pid].cgu_rsp, &readPrim_union[pid].cgu_ind, sizeof(BiccCgu_Ind));
				bicc_send_cgua(pid);
				port_ptr->fsm_state.mgbr_state = MGBR_IDLE;
			}
			break;
		default:
			break;
	}
}

/********************************************/
/*			Circuit Reset Sending			*/
/********************************************/
BOOL check_crs_waiting(u32 pid)
{
	if(Bicc_Data.port_data[pid].fsm_state.crs_state == CRS_WAIT_REL)
		return 1;
	else
		return 0;
}
inline void bicc_crs_proc(u32 pid)
{
	BICC_Port_struct 			*port_ptr = &Bicc_Data.port_data[pid];
	BiccTimer_struct			*tempTimer = &Bicc_Data.timer_data[pid];
	
	u32 						circuit_id;
	const pal_circuit_struct	*pcircuit = NULL;

	circuit_id = pid / BICC_CIRCUIT_CIC;
	pcircuit = pal_circuit_ptr(circuit_id);
	if(pcircuit == NULL)
		return ;
	if(pcircuit->attrib.plat_id != local_platid )
		if(Bicc_Data.dmp_data.active == 1)
		{
			if(port_ptr->fsm_state.crs_state != CRS_IDLE)
			{
				port_ptr->fsm_state.crs_state = CRS_IDLE;
			
				tempTimer->t16 = 0;
			}	
			return ;
		}
	switch(port_ptr->fsm_state.crs_state)
	{
		case CRS_IDLE:
			if(port_ptr->primitive_cmd == BICC_RESET_REQ)
			{
				port_ptr->primitive_cmd = 0;
				port_ptr->cic_state.blo_state &= 0x0e;
				port_ptr->cic_state.blo_state &= 0x0d;
				bicc_send_rsc(pid);
				tempTimer->t16 = TIMER_PTR->t16;

				port_ptr->fsm_state.crs_state = CRS_WAIT_REL;
			}
			break;
		case CRS_WAIT_REL:
			if(port_ptr->sprc_cmd == M_RLC)
			{
				port_ptr->sprc_cmd = 0;
				tempTimer->t16 = BICC_TIMER_STOP;
				port_ptr->fsm_state.crs_state = CRS_IDLE;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_CNF, EN_NONE);

			}

			if(tempTimer->t16 -- == BICC_TIMER_EXPIRED)
			{
				bicc_send_rsc(pid);
				tempTimer->t16 = TIMER_PTR->t16;
			}
			break;
		default:
			break;
	}
}

/********************************************/
/*			Circuit Reset Reception			*/
/********************************************/
inline void bicc_crr_proc(u32 pid)
{
	BICC_Port_struct 			*port_ptr = &Bicc_Data.port_data[pid];
	
	
	switch(port_ptr->fsm_state.crr_state)
	{
		case CRR_IDLE:
			if(port_ptr->sprc_cmd == M_RSC)
			{
				port_ptr->sprc_cmd = 0;
				
				if(port_ptr->cic_state.call_state == BICC_CIC_IDLE)
					port_ptr->cic_state.call_state = BICC_INCOMING;
				
				port_ptr->internal_cmd = (BICC_CMD_RESET|BICC_SI_CRR);
				
				if(port_ptr->cic_state.blo_state & M_LOCAL_BLOCK)
					port_ptr->cic_state.blo_state &= 0x0e;
				port_ptr->cic_state.blo_state &= 0x0d;
				port_ptr->fsm_state.crr_state = CRR_WAIT_RESPONSE;
				port_ptr->internal_cmd = (BICC_CMD_RESET|BICC_SI_CRR);

			}
			break;
		case CRR_WAIT_RESPONSE:
			//if((port_ptr->internal_cmd & 0x0F) == CMD_RESET_COMP)
		//	if(port_ptr->primitive_cmd == BICC_RESET_RSP)
			{
				port_ptr->internal_cmd = 0;
				writePrim_union[pid].rel_rsp.cause.pres = 0;
				bicc_send_rlc(pid);
				port_ptr->fsm_state.crr_state = CRR_IDLE;
			}
			break;
		default:
			break;
	}
}

/************************************************/
/*			Circuit Group Reset Sending			*/
/************************************************/
inline void bicc_cgrs_proc(u32 pid)
{
	BICC_Port_struct 			*port_ptr = &Bicc_Data.port_data[pid];
	BiccTimer_struct 			*tempTimer = &Bicc_Data.timer_data[pid];
	u32 circuit_id;
	const pal_circuit_struct	*pcircuit = NULL;

	circuit_id = pid / BICC_CIRCUIT_CIC;
	pcircuit = pal_circuit_ptr(circuit_id);
	if(pcircuit == NULL)
		return ;
	if(pcircuit->attrib.plat_id != local_platid )
		if(Bicc_Data.dmp_data.active == 1)
		{
			if(port_ptr->fsm_state.cgrs_state != CGRS_IDLE)
			{
				port_ptr->fsm_state.cgrs_state = CGRS_IDLE;
			
				tempTimer->t22 = 0;
			}	
			return ;
		}
	switch(port_ptr->fsm_state.cgrs_state)
	{
		case CGRS_IDLE:
			if(port_ptr->primitive_cmd == BICC_GROUP_RESET_REQ)
			{
				port_ptr->primitive_cmd = 0;
				if(port_ptr->fsm_state.mgbs_state != MGBS_IDLE)
					port_ptr->internal_cmd = (BICC_CMD_RESET|BICC_SI_CGRS);
				bicc_cgrs_func(pid, writePrim_union[pid].grs_req.rangeStatus.range);
				bicc_send_grs(pid);
				tempTimer->t22 = TIMER_PTR->t22;
				port_ptr->fsm_state.cgrs_state = CGRS_WAIT_GRA;
			}
			break;
		case CGRS_WAIT_GRA:
			if(port_ptr->sprc_cmd == M_GRA)
			{
				port_ptr->sprc_cmd = 0;
				tempTimer->t22 = BICC_TIMER_STOP;
				port_ptr->fsm_state.cgrs_state = CGRS_IDLE;
				bicc_event_handle(pid, &readPrim_union[pid], BICC_RESET_CNF, EN_GROUP);
				bicc_cgrsack_func(pid, &readPrim_union[pid].grs_cnf.rangeStatus);
			}
						
			if(tempTimer->t22 -- == BICC_TIMER_EXPIRED)
			{
				bicc_send_grs(pid);
				tempTimer->t22 = TIMER_PTR->t22;
				
			}
			break;
		default:
			break;
	}
}

/****************************************************/
/*			Circuit Group Reset Reception			*/
/****************************************************/
inline void bicc_cgrr_proc(u32 pid)
{
	BICC_Port_struct 			*port_ptr = &Bicc_Data.port_data[pid];
	
	switch(port_ptr->fsm_state.cgrr_state)
	{
		case CGRR_IDLE:
			if(port_ptr->sprc_cmd == M_GRS)
			{
				port_ptr->sprc_cmd = 0;
				if((readPrim_union[pid].grs_ind.rangeStatus.range >= 32)|| (readPrim_union[pid].grs_ind.rangeStatus.range == 0))
					return;
				if(port_ptr->fsm_state.mgbr_state != MGBR_IDLE)
					port_ptr->internal_cmd = (BICC_CMD_RESET|BICC_SI_CGRR);
				bicc_cgrr_func(pid, readPrim_union[pid].grs_ind.rangeStatus.range, &msg_union[pid].gra.rangeStatus);
				port_ptr->fsm_state.cgrr_state = CGRR_WAIT_GROUP_RESET_COMP;
			}
			break;
		case CGRR_WAIT_GROUP_RESET_COMP:
			memcpy(&writePrim_union[pid].grs_rsp.rangeStatus, &msg_union[pid].gra.rangeStatus, sizeof(RangeStatus_struct));
			bicc_send_gra(pid);
			port_ptr->internal_cmd = 0;
			port_ptr->fsm_state.cgrr_state = CGRR_IDLE;
			break;
		default:
			break;
	}
}



BOOL bicc_check_blocking(u32 circuit_id, RangeStatus_struct *rs_ptr)
{
	int								i;
	BOOL							ret = 0;
	const pal_circuit_struct		*pcircuit = NULL;
	
	pcircuit = pal_circuit_ptr(circuit_id);
	if(pcircuit == NULL)
		return ret;
	memset(rs_ptr, 0, sizeof(RangeStatus_struct));
	rs_ptr->range = BICC_CIRCUIT_CIC- 1;
	for(i = pcircuit->attrib.head_cic; i < (pcircuit->attrib.head_cic + pcircuit->attrib.cic_range); i++)
	{
		if((Bicc_Data.port_data[i].cic_state.blo_state == 0) && (pcircuit->enable== 1))
		{
			rs_ptr->status[i>>3]|= 0x01 << (i&0x07);
			ret = 1;
		}
	}
	return ret;
}

BOOL bicc_check_unblocking(u32 circuit_id, RangeStatus_struct *rs_ptr)
{
	int								i;
	BOOL							ret = 0;
	const pal_circuit_struct		*pcircuit = NULL;
	
	pcircuit = pal_circuit_ptr(circuit_id);
	if(pcircuit == NULL)
		return ret;
	memset(rs_ptr, 0, sizeof(RangeStatus_struct));
	rs_ptr->range = BICC_CIRCUIT_CIC - 1;
	for(i = pcircuit->attrib.head_cic; i < (pcircuit->attrib.head_cic + pcircuit->attrib.cic_range); i++)
	{
		if((pcircuit->enable== 1) && (Bicc_Data.port_data[i].cic_state.blo_state == 1))
		{
			rs_ptr->status[i>>3]|= 0x01 << (i&0x07);
			ret = 1;
		}
	}
	return ret;
}

BOOL bicc_trunk_reachable(u32 cg_id)
{
        const pal_cg_struct 		*pcg;
        pcg = pal_cg_ptr(cg_id);
        if(pcg==NULL)
        	return 0;
        if(check_outbound_route(pcg->attrib.network_id, (long)pcg->attrib.dpc, 0) >= 0)
                return 1;
        else
                return 0;
}

