#include "./include/var_ext.h"

//static u32		bicc_object_id[BICC_PREOID_LEN] = {1,3,6,1,4,1,1373,1,3,2,2,6};
//static u16		bicc_sub_page[BICC_DEBUG_PAGE] = {0};
//static u8		bicc_ascii_in[BICC_ASCIN_LEN] = {0};
//static u8		bicc_ascii_out[BICC_ASCOUT_LEN] = {0};

#define BICC_DEBUG_ID				17
#define	BICC_VER_DEBUG				"R91V0_01"

#define	MONITOR_NONE				0x0000
#define	MONITOR_ERROR				0x0010
#define	MONITOR_ALL				 	0xffff

#define TRACE_FLAG_ON				1
#define TRACE_FLAG_OFF				0

u8		AsciInBuf[4096];
u8		AsciOutBuf[4096];
u8		AsciTempBuf[4096];
WORD	MonitorFg;
u8 		info[1024];

static u8 log_help[] = {
	"BICC debug Help:\n\r\
	\n\r\
	1.help\n\r\
	2.log all/none\n\r\
	3.log error on/off\n\r\
  4.list cg[-cgNo] \n\r\
  5.list circuit[-circuitNo]\n\r\
	6.list port[-portNo]\n\r\
	7.list dmp\n\r\
	8.trace on-portNo\n\r\
	9.trace off[-portNo]\n\r\n\r\
"
};

static	WORD	disp_page[10];
static	u8		*disp_ptr = (u8 *) disp_page;

static 	DWORD	debug_status_id[20] = 
{
	1,3,6,1,4,1,1373,1,1,2,3,1,2,BICC_DEBUG_ID + 2,1
};

static	BYTE 	debug_status = 1;

static DWORD	debug_name_id[20] = 
{
	1,3,6,1,4,1,1373,1,1,2,3,1,2,BICC_DEBUG_ID + 2,2
};

static DWORD	debug_ascin_id[20] = 
{
	1,3,6,1,4,1,1373,1,1,2,3,1,2,BICC_DEBUG_ID + 2,3
};

static DWORD	debug_ascout_id[20] = 
{
	1,3,6,1,4,1,1373,1,1,2,3,1,2,BICC_DEBUG_ID + 2,4
};

static	DWORD	debug_page_title[20] = 		
{
	1,3,6,1,4,1,1373,1,1,2,3,1,2,BICC_DEBUG_ID + 2,1,1
};

static	DWORD	debug_page_line[20] = 		
{
	1,3,6,1,4,1,1373,1,1,2,3,1,2,BICC_DEBUG_ID + 2,1,2,1
};

const static u8	BASE_ID_LEN = 15;
const static u8	PAGE_POINT 	= 14;
const static u8 LINE_POINT 	= 15;

static u8	title1_p[] = 
{
"    BICC    Page 01    SAP Info\n\r\
		    0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15\n\r\
Subpage\n\r\
Enable\n\r\
CG num\n\r"
};

static u8	title2_p[] = 
{
"    BICC    Page 02    CG      Info\n\r\
	            0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15\n\r\
Subpage\n\r\
Enable\n\r\
SAP id\n\r\
NI\n\r\
DPC\n\r"
};

static u8	title3_p[] = 
{
"    BICC    Page 03    CIrcuit Info\n\r\
	            0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15\n\r\
Subpage\n\r\
CG id\n\r\
CIC range\n\r\
HEAD CIC\n\r"
};

static u8	title4_p[] = 
{
"    BICC    Page 04    Port Info\n\r\
	            0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15\n\r\
Subpage\n\r\
Enable\n\r\
FSM State\n\r\
CIC State\n\r"
};

static u8 debug_disp_line(u8 page, u8 line)
{
	u8	disp_length;
	disp_length = 0;
	disp_ptr 	= (u8 *) disp_page;
	switch (page)
	{
/*
		case 1:	// Page 1: sap Info
    		switch(line)
			{
				case 0:
					disp_ptr = (BYTE *) &disp_page[page]; 
					disp_length = 2;
					break;
				case 1:
					disp_ptr = (BYTE *) &Bicc_Data.debug_data.sap_enable;
					disp_length = 16;
					break;
				case 2:
					disp_ptr = (BYTE *) &Bicc_Data.debug_data.sap_cg_num;
					disp_length = 16;
					break;
				default:
					break;
			}
    		break;
	case 2:	// Page 2: cg  Info
		//disp_page[page] =       (disp_page[page]+1)%BICC_MAX_CG;
   		switch(line)
    		{
       			case 0:
           			disp_ptr        = (BYTE *) &disp_page[page];    //Subpage
           			disp_length = 2;
           			break;
       			case 1:                                                                                 //enable             
           			disp_ptr        = (BYTE *) &(BiccResource.cg_pond[disp_page[page]].used_flag);
           			disp_length = 16;
           			break;
       			case 2:                                                                                 //sap_id             
           			disp_ptr        = (BYTE *) &(BiccResource.cg_pond[disp_page[page]].sap_id);
           			disp_length = 16;
           			break;
       			case 3:                                                                                 //network_id         
           			disp_ptr        = (BYTE *) &(BiccResource.cg_pond[disp_page[page]].attrib.network_id);
           			disp_length = 16;
          			 break;
       			case 4:                                                                                 //dpc                
           			disp_ptr        = (BYTE *) &(BiccResource.cg_pond[disp_page[page]].attrib.dpc);
           			disp_length = 16;
           			break;
       			default:
       				break;
    		}
     	 	break;

	case 3:	// Page 3: circuit Info
    //disp_page[page] =       (disp_page[page]+1)%BICC_MAX_CIRCUIT;
    			switch(line)
    			{
     				case 0:
        				disp_ptr        = (BYTE *) &disp_page[page];    //Subpage
        				disp_length = 16;
        				break;
     				case 1:                                                                                 //cg_id              
        				disp_ptr        = (BYTE *) &(BiccResource.circuit_pond[disp_page[page]].cg_id);
        				disp_length = 16;
        				break;
     				case 2:                                                                                 //cic_range          
        				disp_ptr        = (BYTE *) &(BiccResource.circuit_pond[disp_page[page]].attrib.cic_range);
        				disp_length = 16;
        				break;
   				case 3:                                                                                 //head_cic           
        				disp_ptr        = (BYTE *) &(BiccResource.circuit_pond[disp_page[page]].attrib.head_cic);
        				disp_length = 16;
        				break;
   				default:
        				break;
   		}
   		break;

	case 4:	// Page 4: port Info
    //disp_page[page] =       (disp_page[page]+32)%BICC_MAX_PORT;
    			switch(line)
    			{
      			case 0:
      				disp_ptr        = (BYTE *) &disp_page[page];    //Subpage
      				disp_length = 16;
     				break;
      			case 1:                                                                                 //enable             
       				disp_ptr        = (BYTE *) &(BiccResource.port_pond[disp_page[page]].used_flag);
        			disp_length = 16;
        			break;
     			case 2:                                                                                 //fsm_state          
        			disp_ptr        = (BYTE *) &(BiccResource.port_pond[disp_page[page]].fsm_state);
        			disp_length = 16;
        			break;
      			case 3:                                                                                 //cic_state          
         			disp_ptr        = (BYTE *) &(BiccResource.port_pond[disp_page[page]].cic_state);
         			disp_length = 16;
         			break;
      			default:
      				break;
      	}
      	break;
*/
	default:
		disp_length = 0;
		break;
	}

	return disp_length;
}

static void debug_disp_page(u8 page)
{
	u8	disp_line;
	u8	disp_length;

	debug_page_line[PAGE_POINT] = page + 5;

	for (disp_line = 0; disp_line < 22; disp_line++)
	{
		disp_length = debug_disp_line(page, disp_line);
		debug_page_line[LINE_POINT] = disp_line + 2;
		debug_page_line[LINE_POINT + 1] = 2;	// Data Pointer
		debug_set_response(BASE_ID_LEN + 2, debug_page_line, disp_ptr, disp_length);
	}
}

int	bicc_debug_init(void)
{
	BYTE	page;
	BYTE	data[10];
	BYTE 	*ptr;

	strcpy(AsciInBuf, "\0");
    strcpy(AsciOutBuf, "\0");
    MonitorFg = MONITOR_NONE;
	bzero(disp_page, 20);

	ptr = data;

	debug_set_response(BASE_ID_LEN, debug_status_id, &debug_status, 1);
	debug_set_response(BASE_ID_LEN, debug_name_id, BICC_VER_DEBUG, 10);
	debug_set_response(BASE_ID_LEN, debug_ascin_id, AsciInBuf, 4096);
	debug_set_response(BASE_ID_LEN, debug_ascout_id, AsciOutBuf, 4096);

	for (page = 1; page < 5; page++)
	{
		switch (page)
		{
		case 1: //page 1
			ptr = title1_p;
			break;
		case 2: //page 2
			ptr = title2_p;
			break;
		case 3: //page 3
			ptr = title3_p;
			break;
		case 4: //page 4
			ptr = title4_p;
			break;
		default:
			break;				
		}

		debug_page_title[PAGE_POINT] = 5 + page;
		debug_set_response(BASE_ID_LEN + 1, debug_page_title, ptr, strlen(ptr));
		debug_disp_page(page);
	}

	return 1;
}

void bicc_asciout_proc(const char *fmt, ...)
{
        va_list         ap;
        char            buf[1024];

        va_start(ap, fmt);
        vsprintf(buf, fmt, ap);
        if((strlen(AsciOutBuf) + strlen(buf)) < 4096)
            strcat(AsciOutBuf, buf);
        else
            strcpy(AsciOutBuf, buf);
        va_end(ap);
}


void bicc_debug_refresh(void)
{
//	int i;
	#if 0
	for(i=0;i<16;i++)
	{
		Bicc_Data.debug_data.sap_enable[i] = BiccResource.sap_pond[i].used_flag;
		Bicc_Data.debug_data.sap_cg_num[i] = BiccResource.sap_pond[i].cg_num;
	}
	#endif
}


#if 0
void list_sap(WORD sapNo)
{
	//u8 sapinfo[1024];
	if(sapNo >= BICC_MAX_SAP)
		return;
	
	sprintf(info,"\
Enable:%d\n\r\
cg num:%d\n\r",
	BiccResource.sap_pond[sapNo].used_flag,BiccResource.sap_pond[sapNo].cg_num);
	bicc_asciout_proc(info);
}

void list_all_sap()
{
	int i;
	sprintf(info, "Index CG(s)\n\r");
	bicc_asciout_proc(info);
	for(i=0; i<BICC_MAX_SAP; i++)
	{
		if(BiccResource.sap_pond[i].used_flag == 1)
		{
			sprintf(info, "  %d    %d\n\r", i, BiccResource.sap_pond[i].cg_num);
			bicc_asciout_proc(info);
		}
	}
}
#endif
void list_cg(WORD cgNo)
{
	const pal_cg_struct *pcg;
	if(cgNo >= PAL_MAX_CG)
		return;
	pcg = pal_cg_ptr(cgNo);
	if(pcg == NULL)
            return;
	sprintf(info, "\
Enable:%d\n\r\
sap id:%d\n\r\
circuit num:%d\n\r\
variant_type:%d\n\r\
priority:%d\n\r\
network id:%d\n\r\
opc:%ld\n\r\
dpc:%ld\n\r\
tg_id:%ld\n\r",
		       pcg->enable,pcg->sap_id,pcg->circuit_num,pcg->attrib.variant,pcg->attrib.priority,pcg->attrib.network_id,pcg->attrib.opc,pcg->attrib.dpc,pcg->attrib.tg_id);
	bicc_asciout_proc(info);
}

void list_all_cg()
{
	int 				i = 0;
	const pal_cg_struct *pcg;
	sprintf(info, "Index  SAP  Circuit(s)  NetInd  DPC  OPC  TG\n\r");
	bicc_asciout_proc(info);	
	for(i=0; i<PAL_MAX_CG; i++)
    {
    	pcg = pal_cg_ptr(i);
        if(pcg == NULL)
        	continue;
    	if(pcg->enable==0)
      		continue;

    	if(pcg->attrib.protocol != PROTO_BICC)
        	continue;
		sprintf(info, "  %d     %d       %d        %d      %ld    %ld    %ld\n\r", i, pcg->sap_id, pcg->circuit_num, pcg->attrib.network_id, pcg->attrib.dpc, pcg->attrib.opc, pcg->attrib.tg_id);
		bicc_asciout_proc(info);
	}		
}


void list_circuit(WORD circuitNo)
{
	const pal_circuit_struct *pcircuit;
	if(circuitNo >= PAL_MAX_CIRCUIT)
		return;
	pcircuit = pal_circuit_ptr(circuitNo);
	if(pcircuit == NULL)
    		return ;
	
	sprintf(info,"\
Enable:%d\n\r\
cg id:%d\n\r\
head cic:%ld\n\r\
cic range:%d\n\r\
plat id:%d\n\r",
	pcircuit->enable,pcircuit->cg_id,pcircuit->attrib.head_cic,pcircuit->attrib.cic_range,pcircuit->attrib.plat_id);
	bicc_asciout_proc(info);
}


void list_all_circuit()
{
	int i = 0, j = 0;
	const pal_circuit_struct *pcircuit;
    const pal_cg_struct *pcg;
//  for(i = 0; i < BICC_MAX_SAP; i ++)
    //{
    sprintf(info, "Index  CG  PLAT     CIC(s)\n\r");
    bicc_asciout_proc(info);
	for(i=0; i<PAL_MAX_CG; i++)
	{
		pcg = pal_cg_ptr(i);
        if(pcg == NULL)
        	continue;
    	if(pcg->enable == 0)
      		continue;
    	if(pcg->attrib.protocol != PROTO_BICC)
        	continue;
		for(j = 0; j<PAL_MAX_CIRCUIT; j++)
		{
			pcircuit = pal_circuit_ptr(j);
			if(pcircuit == NULL)
            	continue;
			if(pcircuit->enable == 0 || pcircuit->cg_id != pcg->id)
            	continue;
            sprintf(info, "  %d     %d    %d        %d (%ld~~%ld)\n\r", i, pcircuit->cg_id, pcircuit->attrib.plat_id, pcircuit->attrib.cic_range, pcircuit->attrib.head_cic, pcircuit->attrib.head_cic+pcircuit->attrib.cic_range);
            bicc_asciout_proc(info);
			
		}

	}
}

void list_port(WORD portNo)
{
	if(portNo>=BICC_MAX_PORT)
		return;
	sprintf(info,"\
upper port:%ld\n\r\
sprc_cmd:%x\n\r\
primitive_cmd:%x\n\r\
internal_cmd:%x\n\r\
cic state{\n\r\
    		call state:%d\n\r\
    		suspend state:%d\n\r\
      		block state:%d\n\r\
         }\n\r\
fsm state{\n\r\
		cpc_state:%d\n\r\
		mgbs_state:%d\n\r\
		mgbr_state:%d\n\r\
		crs_state:%d\n\r\
		crr_state:%d\n\r\
		cgrs_state:%d\n\r\
		cgrr_state:%d\n\r\
	}\n\r",
		       Bicc_Data.port_data[portNo].su_proc_id,Bicc_Data.port_data[portNo].sprc_cmd,
		       Bicc_Data.port_data[portNo].primitive_cmd,Bicc_Data.port_data[portNo].internal_cmd,
		       Bicc_Data.port_data[portNo].cic_state.call_state,Bicc_Data.port_data[portNo].cic_state.sus_state,
		       Bicc_Data.port_data[portNo].cic_state.blo_state,Bicc_Data.port_data[portNo].fsm_state.cpc_state,
		       Bicc_Data.port_data[portNo].fsm_state.mgbs_state,Bicc_Data.port_data[portNo].fsm_state.mgbr_state,
		       Bicc_Data.port_data[portNo].fsm_state.crs_state,Bicc_Data.port_data[portNo].fsm_state.crr_state,
		       Bicc_Data.port_data[portNo].fsm_state.cgrs_state,Bicc_Data.port_data[portNo].fsm_state.cgrr_state);
	bicc_asciout_proc(info);
}

void list_busy_port()
{
	int i,j=0;
	sprintf(info, "Index  PortNo     CallState\n\r");
  	bicc_asciout_proc(info);	
	for(i=0;i<BICC_MAX_PORT;i++)
	{
		if(Bicc_Data.port_data[i].used_flag == 1)
		{
			sprintf(info,"%d    %d           %s\n\r",j++,i,(Bicc_Data.port_data[i].cic_state.call_state==1)?"INCOMING":"OUTGOING");
			bicc_asciout_proc(info);	
		}
	}
			
}

void list_dmp()
{
        sprintf(info, "\
dmp active: %d\n\r\
local plat id: %d\n\r\
remote ip: %lx\n\r", Bicc_Data.dmp_data.active,Bicc_Data.dmp_data.processor_id,Bicc_Data.dmp_data.remote_ip);
        bicc_asciout_proc(info);
}

void bicc_debug_timer(void)
{
	BYTE	in_page, err_fg = 0;
	BYTE	*ascii_in_ptr 	= NULL;
	BYTE	*str_start      = NULL, * tmpStr;
	WORD	str_len, portNo=0;
	WORD	num, cgNo, circuitNo;

	if ((str_len = strlen(AsciInBuf)) > 0)
	{
		in_page = AsciInBuf[0] - 1;
		ascii_in_ptr = AsciInBuf + 1;

		if (in_page > 4)
			err_fg = 1;
		else if (strcmp(ascii_in_ptr,"log error on") == 0)
		{
			MonitorFg = MonitorFg | MONITOR_ERROR;
		}
		else if (strcmp(ascii_in_ptr,"log error off") == 0)
		{
			MonitorFg = MonitorFg & (~MONITOR_ERROR);			
		}
		else if (strcmp(ascii_in_ptr,"log all") == 0)
		{
			MonitorFg = MONITOR_ALL;

		}
		else if (strcmp(ascii_in_ptr,"log none") == 0)
		{
			MonitorFg = MONITOR_NONE;

		}
		else if (strcmp(ascii_in_ptr,"help") == 0)
		{
			bicc_asciout_proc(log_help);
		}
		else if (isdigit(ascii_in_ptr[0]))
		{
			num = strtoul(ascii_in_ptr, NULL, 10);
			disp_page[in_page] = num;
			debug_disp_page(in_page);
		}
		else if ((str_start = strstr(ascii_in_ptr, ">")) != NULL)
		{
			num = strtoul(str_start + 1, NULL, 10);
			disp_page[in_page] += num;
			debug_disp_page(in_page);
		}
		else if ((str_start = strstr(ascii_in_ptr, "<"))!= NULL)
		{
			num = strtoul(str_start + 1, NULL, 10);
			disp_page[in_page] -= num;
			debug_disp_page(in_page);
		}
		#if 0
		else if ((strstr (ascii_in_ptr, "list sap")) != NULL)
		{
			if((tmpStr = strstr (ascii_in_ptr, "-")) != NULL)
			{
				sapNo = strtoul (tmpStr + 1, NULL, 10);
				list_sap(sapNo);
			}
			else
			{
				list_all_sap();
			}
		}
#endif
		else if ((strstr (ascii_in_ptr,"list cg")) != NULL)
        {
            if((tmpStr = strstr (ascii_in_ptr, "-")) != NULL)
		//	if((tmpStr = strsep(&&ascii_in_ptr, " \t")) != NULL)
            {
                cgNo = strtoul (tmpStr+1, NULL, 10);
			//	strsep(, " \t");
                list_cg(cgNo);
            }
			else
			{
				list_all_cg();
			}
        }
		else if ((strstr (ascii_in_ptr, "list circuit")) != NULL)
        {
            if((tmpStr = strstr (ascii_in_ptr, "-")) != NULL)
            {
                circuitNo = strtoul (tmpStr + 1, NULL, 10);
                list_circuit(circuitNo);
            }
			else
			{
				list_all_circuit();
			}
        }
		else if ((strstr (ascii_in_ptr, "list port")) != NULL)
		{
        	if((tmpStr = strstr (ascii_in_ptr, "-")) != NULL)
            {
            	portNo = strtoul (tmpStr + 1, NULL, 10);
                list_port(portNo);
            }
			else
			{
				list_busy_port();
			}
			
 	   }
		else if((strstr (ascii_in_ptr, "list dmp")) != NULL)
    	{
    		list_dmp();
    	}
		else if((strstr(ascii_in_ptr, "trace on")) != NULL)
		{
			if((tmpStr = strstr(ascii_in_ptr, "-")) != NULL)
			{
				portNo = strtoul(tmpStr + 1, NULL, 10);
				//trace_port(portNo);
				Bicc_Data.port_data[portNo].trace_flag = 1;
			}
			else
			{}
		}
		else if ((strstr (ascii_in_ptr, "trace off")) != NULL)
		{
        	if((tmpStr = strstr (ascii_in_ptr, "-")) != NULL)
          	{
            	portNo = strtoul (tmpStr + 1, NULL, 10);
            	Bicc_Data.port_data[portNo].trace_flag = 0;
          	}
			else
			{
				int i;
				for(i=0; i<BICC_MAX_PORT; i++)
					Bicc_Data.port_data[i].trace_flag = 0;
			}
			
 	   	}
		else
			err_fg = 1;

		if (err_fg == 0)
			bicc_asciout_proc("Command OK!\n\r");
		else
			bicc_asciout_proc("Command Error!\n\r");

		strcpy(AsciInBuf, "\0");
	}
	bicc_debug_refresh();
}


void bicc_log_err(const char *fmt, ...)
{
	if ((MonitorFg & MONITOR_ERROR) == MONITOR_ERROR)
	{
		va_list		ap;
		char		buf[1024];
		
		va_start(ap, fmt);
		vsprintf(buf, fmt, ap);
		if((strlen(AsciOutBuf) + strlen(buf)) < 4096-15)
		{
			strcat(AsciOutBuf, "\33[31m");
			strcat(AsciOutBuf, buf);
			strcat(AsciOutBuf, "\33[37m");
		}
		else
		{
			strcpy(AsciOutBuf, "\33[31m");
			strcat(AsciOutBuf, buf);
			strcat(AsciOutBuf, "\33[37m");
		}
		va_end(ap);		
	}
}


int bicc_log_procedure(WORD mgNo, WORD portIndex, WORD chnlIndex, char *msg)
{
	u8	monFlag = 0;

		monFlag = 1;

	if (monFlag == 1)
	{
		if (strlen(msg) >= 4096)
			sprintf(AsciTempBuf, "log msg is too long!\n\r");
		else
			sprintf(AsciTempBuf, "%s\n\r", msg);
		bicc_asciout_proc(AsciTempBuf);
		return 0;
	}

	return -1;
}

void display_bicc_msgtype(u8 msgtype, u32 cic)
{
	switch(msgtype)
	{
		case M_ACM:
			bicc_asciout_proc("ACM:Address complete");
			break;
		case M_ANM:
			bicc_asciout_proc("ANM:Answer");
			break;
		case M_BLO:
			bicc_asciout_proc("BLO:Blocking");
			break;
		case M_BLA:
			bicc_asciout_proc("BLA:Blocking acknowledgement");
			break;
		case M_CPG:
			bicc_asciout_proc("CPG:Call progress");
			break;
		case M_CGB:
			bicc_asciout_proc("CGB:Circuit/CIC group blocking");
			break;
		case M_CGBA:
			bicc_asciout_proc("CGBA:Circuit/CIC group blocking acknowledgement");
			break;
		case M_CQM:
			bicc_asciout_proc("CQM:Circuit/CIC group query (national use)");
			break;
		case M_CQR:
			bicc_asciout_proc("CQR:Circuit/CIC group query response (national use)");
			break;
		case M_GRS:
			bicc_asciout_proc("GRS:Circuit/CIC group reset");
			break;
		case M_GRA:
			bicc_asciout_proc("GRA:Circuit/CIC group reset acknowledgement");
			break;
		case M_CGU:
			bicc_asciout_proc("CGU:Circuit/CIC group unblocking");
			break;
		case M_CGUA:
			bicc_asciout_proc("CGUA:Circuit/CIC group unblocking acknowledgement");
			break;
		case M_CRG:
			bicc_asciout_proc("CRG:Charge information (national use)");
			break;
		case M_CFN:
			bicc_asciout_proc("CFN:Confusion");
			break;
		case M_CON:
			bicc_asciout_proc("CON:Connect");
			break;
		case M_COT:
			bicc_asciout_proc("COT:Continuity");
			break;
		case M_FAC:
			bicc_asciout_proc("FAC:Facility ");
			break;
		case M_FAA:
			bicc_asciout_proc("FAA:Facility accepted");
			break;
		case M_FRJ:
			bicc_asciout_proc("FRJ:Facility reject");
			break;
		case M_FAR:
			bicc_asciout_proc("FAR:Facility request");
			break;
		case M_FOT:
			bicc_asciout_proc("FOT:Forward transfer");
			break;
		case M_IDR:
			bicc_asciout_proc("IDR:Identification request");
			break;
		case M_IRS:
			bicc_asciout_proc("IRS:Identification response");
			break;
		case M_INF:
			bicc_asciout_proc("INF:Information (national use)");
			break;
		case M_INR:
			bicc_asciout_proc("INR:Information request (national use)");
			break;
		case M_IAM:
			bicc_asciout_proc("IAM:Initial address");
			break;
		case M_NRM:
			bicc_asciout_proc("NRM:Network resource management");
			break;
		case M_REL:	
			bicc_asciout_proc("REL:Release");
			break;
		case M_RLC:
			bicc_asciout_proc("RLC:Release complete");
			break;
		case M_RSC:	
			bicc_asciout_proc("RSC:Reset circuit/CIC");
			break;
		case M_RES:	
			bicc_asciout_proc("RES:Resume");
			break;
		case M_SGM:	
			bicc_asciout_proc("SGM:Segmentation");
			break;
		case M_SAM:	
			bicc_asciout_proc("SAM:Subsequent address");
			break;
		case M_SUS:	
			bicc_asciout_proc("SUS:Suspend");
			break;
		case M_UBL:	
			bicc_asciout_proc("UBL:Unblocking");
			break;
		case M_UBA:	
			bicc_asciout_proc("UBA:Unblocking acknowledgement");
			break;
		case M_UCIC:	
			bicc_asciout_proc("UCIC:Unequipped CIC (national use)");
			break;
		case M_APM:	
			bicc_asciout_proc("APM:Application Transport");
			break;
		case M_USR:	
			bicc_asciout_proc("USR:User-to-user information");
			break;
		default:	
			bicc_asciout_proc("Unknown message");
			
	}
	
	bicc_asciout_proc("(message type=0x%x  cic=%d)\r\n",msgtype,cic);	
}


void monitor_bicc_msg(BYTE *msg,BYTE buf_len, BYTE flag, u32 cic, u32 portNo)
{
	BYTE    i,temp_buf[1024];
	if(MonitorFg == MONITOR_ALL)
	{
		if(flag == 0)
 			bicc_asciout_proc("\33[33mReceive:\33[0m");
		else if(flag == 1)
			bicc_asciout_proc("\33[32mSend:\33[0m");
		else if(flag == 2)
    		bicc_asciout_proc("\33[34mRedirect:\33[0m");
    	display_bicc_msgtype(msg[0], cic);
    	
		if(flag != 2)
		{
			for (i = 0; i < buf_len; i ++)
			{
				sprintf(&temp_buf[3*i]," %02x", msg[i]);
			}
			sprintf(&temp_buf[3*i],"\n\r");	
			bicc_asciout_proc(temp_buf);
		}
	}
	if(MonitorFg == MONITOR_NONE)
	{}
	if(Bicc_Data.port_data[portNo].trace_flag == 1)
	{
		if(flag == 0)
 			bicc_asciout_proc("\33[33mReceive:\33[0m");
		else if(flag == 1)
			bicc_asciout_proc("\33[32mSend:\33[0m");
		else if(flag == 2)
    		bicc_asciout_proc("\33[34mRedirect:\33[0m");
    	display_bicc_msgtype(msg[0], cic);
    	
		if(flag != 2)
		{
			for (i = 0; i < buf_len; i ++)
			{
				sprintf(&temp_buf[3*i]," %02x", msg[i]);
			}
			sprintf(&temp_buf[3*i],"\n\r");	
			bicc_asciout_proc(temp_buf);
		}
	}
}

