#include "./include/8ecp.h"

#define _8ECP_DEBUG_ID 20
#define	_8ECP_VER_DEBUG	"R9V0_01p1"


BYTE _8ecp_asciin_buf[MAX_ASCIIIN_LEN];
BYTE _8ecp_asciout_buf[MAX_ASCIIOUT_LEN];
WORD monitor_flag;
BYTE _8ecp_sg_mon_flag[MAX_MG_NO];
BYTE _8ecp_trk_mon_flag[MAX_TRK_NO];
BYTE _8ecp_chl_mon_flag[MAX_CHL_NO];

BYTE tempstr[128];
BYTE  tempstr1[256];
static BYTE log_help[] = {"8ECP Debug Monitor Help:\n\r\
1.[help]\n\r\
2.[log all/none]\n\r\
3.[log error on/off]\n\r\
4.[log mg -mg_no on/off]\n\r\
5.[log trk -mg_no -trk_no on/off]\n\r\
6.[log chl -mg_no -trk_no -chnl_no on/off]\n\r\n\r"};

static	WORD	disp_page[128];
static	BYTE	*disp_ptr = (BYTE *) disp_page;

static DWORD debug_status_id[20] = 
	{1,3,6,1,4,1,1373,1,1,2,3,1,2, _8ECP_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,_8ECP_DEBUG_ID + 2,2};

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

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

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

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

static BYTE	title1_p[] = 
{	
	"				8ECP    Page\n\r\
				0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15\n\r\
Message Info\n\r\
"};

BYTE _8ecp_disp_line(BYTE page, BYTE line)
{	
	BYTE	disp_length;
	disp_length = 16;
	disp_ptr = (BYTE *) disp_page;	
	switch (page)	
	{
		case 1:	
			if (line == 0)
			{
				disp_ptr = (BYTE *) &disp_page[page];
				disp_length = 2;
			}
			else	
			{
				if(tempstr != NULL)
				disp_ptr = (BYTE *)tempstr;
			}
				
			break;
		default:
			disp_length = 0;
			break;
	}		
	return disp_length;
}

void _8ecp_disp_page(BYTE page)
{
	BYTE	disp_line;
	BYTE	disp_length;
	debug_page_line[PAGE_POINT] = page + 5;
	for (disp_line = 0; disp_line < 22; disp_line++)	
	{
		disp_length = _8ecp_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 _8ecp_asciout_proc (BYTE * out_ptr) 
{
	int out_len;
	out_len = strlen (out_ptr);
	if (out_len + strlen (_8ecp_asciout_buf) > MAX_ASCIIOUT_LEN - 2)  
	{
		strcpy (_8ecp_asciout_buf, out_ptr);
    }
	else
	{
		strcat (_8ecp_asciout_buf, out_ptr);
	}
    return 1;
}
int _8ecp_debug_set (void) 
{
	BYTE	page;
	BYTE	data[10];
	BYTE 	*ptr;
	bzero(disp_page,128);
	ptr = data;
	bzero(_8ecp_sg_mon_flag,MAX_MG_NO);
	bzero(_8ecp_trk_mon_flag,MAX_TRK_NO);
	bzero(_8ecp_chl_mon_flag,MAX_CHL_NO);
	bzero(tempstr, 40);
      
    debug_set_response(BASE_ID_LEN, debug_status_id, &debug_status, 1);
   	debug_set_response(BASE_ID_LEN, debug_name_id, _8ECP_VER_DEBUG, 10);
    debug_set_response (BASE_ID_LEN, debug_ascin_id, _8ecp_asciin_buf,  4096);
    debug_set_response (BASE_ID_LEN, debug_ascout_id, _8ecp_asciout_buf, 4096);
	
	for (page = 1; page < 2; page++)
	{
		switch (page)
		{
			case 1: //page 1
				ptr = title1_p;
				break;
			default:
				break;
		}
			
		debug_page_title[PAGE_POINT] = 5 + page;	
		debug_set_response(BASE_ID_LEN + 1, debug_page_title, ptr, strlen(ptr));
		_8ecp_disp_page(page);
	}
	return 1;
}

void log_procedure(BYTE flag, BYTE* tempstr)
{
	int i;
	if(flag == 0)
	{
		for(i = 0; i < 15; i ++)
			sprintf(&tempstr1[3*i], " %02x",tempstr[i]);
	}
	else if(flag == 1)
	{
		for(i = 0; i < 41; i ++)
	        sprintf(&tempstr1[3*i], " %02x",tempstr[i]);
	}
	if(monitor_flag == MONITOR_ALL)
		{
			if(flag == 0)
			{
				_8ecp_asciout_proc("\33[32mSend:\33[0m");
				_8ecp_asciout_proc(tempstr1);
				_8ecp_asciout_proc("\n");
			}
			else if(flag == 1)
			{
				_8ecp_asciout_proc("\33[33mReceive:\33[0m");
				_8ecp_asciout_proc(tempstr1);
				_8ecp_asciout_proc("\n");
			}
		}
	else
	{
		if(flag == 1)
		{
			for(i = 0; i < MAX_MG_NO; i ++)
			{
				if(_8ecp_sg_mon_flag[i] == 1)
					{
						if(tempstr[11] == i)
						_8ecp_asciout_proc(tempstr);
					}
			}
			for(i = 0; i < MAX_TRK_NO; i++)
			{
				if(_8ecp_trk_mon_flag[i] == 1)
				if(tempstr[12] / 32 == i)
					_8ecp_asciout_proc(tempstr);
			}
			for(i = 0; i < MAX_CHL_NO; i++)
			{
				if(_8ecp_chl_mon_flag[i] == 1)
				if(tempstr[12] == i)
					_8ecp_asciout_proc(tempstr);
			}
		}
	}
}

		
void _8ecp_mon (void) 
{
	WORD str_len;
    BYTE * ascii_in_ptr = NULL;
    BYTE * tmpStr;
    DWORD mgNo = 0, trkNo = 0, chlNo = 0;
    BYTE error_flag = 0;
    if ((str_len = strlen (_8ecp_asciin_buf)) > 0)  
	{
		ascii_in_ptr = _8ecp_asciin_buf + 1;
		if (strcmp (ascii_in_ptr, "log all") == 0)
	  	{
	    	monitor_flag = MONITOR_ALL;
	  	}
		else if (strcmp (ascii_in_ptr, "log none") == 0)
		{
	    	monitor_flag = MONITOR_NONE;
	  	}
		else if (strcmp (ascii_in_ptr, "help") == 0)
		{
	    	_8ecp_asciout_proc (log_help);
	  	}
		else if (strcmp (ascii_in_ptr, "log error on") == 0)
	  	{
	    	monitor_flag = MONITOR_ERROR_ON;
	  	}
		else if (strcmp (ascii_in_ptr, "log error off") == 0)
	  	{
	    	monitor_flag = MONITOR_ERROR_OFF;
	  	}
		else if ((strstr (ascii_in_ptr, "log mg")) != NULL)
	  	{
	    	if ((tmpStr = strstr (ascii_in_ptr, "-")) != NULL)
	      	{
				mgNo = strtoul (tmpStr + 1, NULL, 10);
				if (mgNo >= 0 && mgNo <= 31)
		  		{
		    		if (strstr (ascii_in_ptr, "on") != NULL)
		      		{
						_8ecp_sg_mon_flag[mgNo] = 1;
		      		}
		    		else if (strstr (ascii_in_ptr, "off") != NULL)
		      		{
						_8ecp_sg_mon_flag[mgNo] = 0;
		      		}
		  		}
				else
		  			error_flag = 1;	//the format of command is error
	      	}
	    else
			error_flag = 1;
	}
	else if (strstr (ascii_in_ptr, "log trk"))
	{
		if ((tmpStr = strstr (ascii_in_ptr, "-")) != NULL)
		{
			mgNo = strtoul (tmpStr + 1, NULL, 10);
			if (mgNo >= 0 && mgNo <= 31)
		  	{
		    	while (*++tmpStr != '\0')	//get the next char '-'
		      	{
					if (*tmpStr == '-')
			  		{
			    		trkNo = strtoul (tmpStr + 1, NULL, 10);
			    		if (trkNo >= 0 && trkNo <= 7)
			      		{
							if (strstr (tmpStr, "on") != NULL)
				  			{
				    			_8ecp_trk_mon_flag[mgNo * 8 + trkNo] = 1;
				  			}
							else if (strstr (tmpStr, "off") != NULL)
				  			{
				    			_8ecp_trk_mon_flag[mgNo * 8 + trkNo] = 0;
				  			}
			      		}
			    		else
			      			error_flag = 1;
			   				 break;
				}
			}
		    
		}
		else
			error_flag = 1;
		}
	else
	      error_flag = 1;
	}
	else if (strstr (ascii_in_ptr, "log chl"))
	{
		if ((tmpStr = strstr (ascii_in_ptr, "-")) != NULL)
		{
			mgNo = strtoul (tmpStr + 1, NULL, 10);
			if (mgNo >= 0 && mgNo <= 31)
		  	{
		    	while (*++tmpStr != '\0')	//get the next char '-'
		      	{
					if (*tmpStr == '-')
			  		{
			    		trkNo = strtoul (tmpStr + 1, NULL, 10);
			    		if (trkNo >= 0 && trkNo <= 7)
			      		{
							while (*++tmpStr != '\0')	//get the next char '-'
				  			{
				    			if (*tmpStr == '-')
				      			{
									chlNo = strtoul (tmpStr + 1, NULL, 10);
									if (chlNo >= 0 && chlNo <= 31)
					  				{
					    				if (strstr (tmpStr, "on") != NULL)
					      				{
											_8ecp_chl_mon_flag[mgNo * 8 * 32 + chlNo] = 1;
					      				}
					    
					    				else if (strstr (tmpStr, "off") != NULL)
					     				{
											_8ecp_chl_mon_flag[mgNo * 8 * 32 + chlNo] = 0;
					      				}
					  				}
					
									else
					  					error_flag = 1;
										break;
				      			}
				 			}
							break;
			      		}
					else
			      		error_flag = 1;
					}
				}
			}
			else
				error_flag = 1;
			}
		else
		{
			error_flag = 1;
		}
	}
	else
		error_flag = 1;

	if (error_flag == 0)
		_8ecp_asciout_proc("Command OK!\n\r");
	else
		_8ecp_asciout_proc("Command Error!\n\r");
	 
	strcpy (_8ecp_asciin_buf, "\0");
	}
    
}
