#include "./include/mgcp_pub.h"
#include "./include/mgcp.h"
#include "./include/mgcp_const.h"
#include "./include/mgcp_struct.h"
#include "./include/mgcp_ext.h"
#include "../../debug/src/include/debug.h"

#define MGCP_DEBUG_ID	19
#define	MGCP_VER_DEBUG	"R9V0_02"

extern char *mgcp_clrncpy(char *dst, char *src, WORD len);

static char ascii[16] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
						 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46};

static BYTE	log_help[] = {
"MGCP Debug Monitor Help:\n\r\
\n\r\
1.[help]                                display help menu\n\r\
2.[log all/none]                        display all/none logs\n\r\
3.[log error on/off]                    display error logs\n\r\
4.[log mg-mg_name on/off]                 display MGCP specific mg msg logs\n\r\
5.[list]                                display all mg which have been log\n\r"
};

static	WORD	disp_page[10];
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,MGCP_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,MGCP_DEBUG_ID + 2,2
	};
	
static DWORD	debug_ascin_id[20] = 
	{
		1,3,6,1,4,1,1373,1,1,2,3,1,2,MGCP_DEBUG_ID + 2,3
	};

static DWORD	debug_ascout_id[20] = 
	{
		1,3,6,1,4,1,1373,1,1,2,3,1,2,MGCP_DEBUG_ID + 2,4
	};
		
static	DWORD	debug_page_title[20] = 		
	{
		1,3,6,1,4,1,1373,1,1,2,3,1,2,MGCP_DEBUG_ID + 2,1,1
	};
	
static	DWORD	debug_page_line[20] = 		
	{
		1,3,6,1,4,1,1373,1,1,2,3,1,2,MGCP_DEBUG_ID + 2,1,2,1
	};
		
const static BYTE	PAGE_POINT 	= 14;
const static BYTE  	LINE_POINT 	= 15;
const static BYTE	BASE_ID_LEN = 15;

static BYTE	title1_p[] = 
{
	"    MGCP    Page 01    mgcpPort Status\n\r\
                   0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15\n\r\
Subpage\n\r\
Trans_Status\n\r"
};

BYTE mgcp_disp_line(BYTE page, BYTE line)
{
	BYTE	disp_length;
	
	disp_length = 16;
	disp_ptr 	= (BYTE *) disp_page;
	
	switch (page)
	{
		case 1:	// Page 1: SG Para
			if (line == 0)
			{
				disp_ptr 	= (BYTE *) &disp_page[page];
				disp_length = 2;
			}
			else
				disp_ptr 	= (BYTE *) &mgcpPort[disp_page[page]];
			break;
		default:
			disp_length = 0;
			break;
	}
	
	return disp_length;
}

void mgcp_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 = mgcp_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	mgcp_debug_set(void)
{
	BYTE	page;
	BYTE	data[10];
	BYTE 	*ptr;
	
	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, MGCP_VER_DEBUG, 10);
	debug_set_response(BASE_ID_LEN, debug_ascin_id, mgcpAsciInBuf, 4096);
	debug_set_response(BASE_ID_LEN, debug_ascout_id, mgcpAsciOutBuf, 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));
		mgcp_disp_page(page);
	}
	
	return 1;
}

int	mgcp_asciout_proc(BYTE *outStr)
{
	int outLen;
	
	outLen = strlen(outStr);
		
	if (outLen + strlen(mgcpAsciOutBuf) > MGCP_MAX_ASCIIOUT_LEN - 2)
	{
		strcpy(mgcpAsciOutBuf, outStr);
	}
	else
		strcat(mgcpAsciOutBuf, outStr);
	
	return 1;
}

void mgcp_mon(void)
{
	BYTE  inPage, errFg = 0;
	BYTE  *asciiInPtr = NULL;
    BYTE  *mgNM = NULL;
//  BYTE  *trkNM = NULL;
//  BYTE  *chlNM = NULL;
	BYTE  *poo = NULL;
	BYTE  *strStart = NULL;
	BYTE  tmp[128];
//	BYTE  tmptrkNum;						
//  WORD  tmpchlNum;
	WORD  strLen, num;
	
	if ((strLen = strlen(mgcpAsciInBuf)) > 0)
	{
		inPage = mgcpAsciInBuf[0] - 1;
		asciiInPtr = mgcpAsciInBuf + 1;
		
		if (inPage > 7)
			errFg = 1;
		else if (strcmp(asciiInPtr,"c") == 0)
		{
		}
		else if (strcmp(asciiInPtr,"log error on") == 0)
		{
			mgcpMonitorFg = mgcpMonitorFg | MONITOR_ERROR;
		}
		else if (strcmp(asciiInPtr,"log error off") == 0)
		{
			mgcpMonitorFg = mgcpMonitorFg & (~MONITOR_ERROR);			
		}
		else if (strncmp(asciiInPtr,"log mg-", 7) == 0)
		{ 
		  if (NULL == (poo = strrchr(asciiInPtr, 'o')))
		  {
		    errFg = 1;
		  }
		  else
		  {
		   mgNM = strchr(asciiInPtr, '-');
        
	     if (poo <= mgNM)
		   {
	    	errFg = 1;
  	   }
		   else
		   { 
		   	mgcp_clrncpy(tmp, mgNM+1, poo-mgNM-1);
		   	       
		    if (0 == strcmp(poo, "on"))
		    {	
		    	if(mgcpMonitorFg == MONITOR_ALL)
		    	{
		    		mgcp_asciout_proc("Status is log all\n\r");
		    	}		
		      else if (((mgcpMonitorFg & MONITOR_MG0_MSG) == MONITOR_MG0_MSG) 
		           && ((mgcpMonitorFg & MONITOR_MG1_MSG) == MONITOR_MG1_MSG) 
		           && ((mgcpMonitorFg & MONITOR_MG2_MSG) == MONITOR_MG2_MSG)
		           &&(mgcpMonitorFg != MONITOR_ALL))
		      
		      {
		        mgcp_asciout_proc("Can not log more MG\n\r");
		      }
		      else
		      {
		        if ((((mgcpMonitorFg & MONITOR_MG0_MSG) == MONITOR_MG0_MSG) && (0 == strcmp(dbgmgNamemg[0], tmp))) 
		             || (((mgcpMonitorFg & MONITOR_MG1_MSG) == MONITOR_MG1_MSG) && (0 == strcmp(dbgmgNamemg[1], tmp))) 
		             || (((mgcpMonitorFg & MONITOR_MG2_MSG) == MONITOR_MG2_MSG) && (0 == strcmp(dbgmgNamemg[2], tmp))))
		        {
		          mgcp_asciout_proc("MG already be on\n\r");
		        }
		        else
		        {
		        	if (((mgcpMonitorFg & MONITOR_MG0_MSG) == 0) && (0 == strcmp(dbgmgNamemg[0], tmp)))
		        	{
		        		mgcpMonitorFg |= MONITOR_MG0_MSG;
		        	}
		        	else if (((mgcpMonitorFg & MONITOR_MG1_MSG) == 0) && (0 == strcmp(dbgmgNamemg[1], tmp)))
		        	{
		        		mgcpMonitorFg |= MONITOR_MG1_MSG;
		        	}
		        	else if (((mgcpMonitorFg & MONITOR_MG2_MSG) == 0) && (0 == strcmp(dbgmgNamemg[2], tmp)))
		        	{
		        		mgcpMonitorFg |= MONITOR_MG2_MSG;
		        	}	
		          else if ((mgcpMonitorFg & MONITOR_MG0_MSG) == 0)
		          {	      
		            strcpy(dbgmgNamemg[0], tmp);
		      
		            mgcpMonitorFg |= MONITOR_MG0_MSG;
		          }
		          else if ((mgcpMonitorFg & MONITOR_MG1_MSG) == 0)
		          {
		            strcpy(dbgmgNamemg[1], tmp);
		      
		            mgcpMonitorFg |= MONITOR_MG1_MSG;		        
		          }
		          else
		          {
		            strcpy(dbgmgNamemg[2], tmp);
		      
		            mgcpMonitorFg |= MONITOR_MG2_MSG;		        
		          }
		        }
		      }
		    }
		    else if (0 == strcmp(poo, "off"))
		    {
		    	if (mgcpMonitorFg == MONITOR_NONE)
		    	{
		    		mgcp_asciout_proc("Status is log none\n\r");
		    	}		      
		      else if((mgcpMonitorFg == MONITOR_ALL)
		      	 ||((mgcpMonitorFg | MONITOR_MG0_MSG | MONITOR_MG1_MSG | MONITOR_MG2_MSG | MONITOR_ERROR) == MONITOR_ALL))
		      {
		      	if (mgcpMonitorFg == MONITOR_ALL)
		      	{
		      		mgcpMonitorFg = mgcpMonitorFg & (~MONITOR_MG0_MSG);
		      	  strcpy(dbgmgNamemg[0], tmp);
		      	}
		      	else if (((mgcpMonitorFg & MONITOR_MG0_MSG) == MONITOR_MG0_MSG)
		      		        ||((mgcpMonitorFg & MONITOR_MG1_MSG) == MONITOR_MG1_MSG)
		      		        ||((mgcpMonitorFg & MONITOR_MG2_MSG) == MONITOR_MG2_MSG))
		      	{
		      		if(((0 == strcmp(dbgmgNamemg[0], tmp)) && ((mgcpMonitorFg & MONITOR_MG0_MSG) == 0))
		      			 ||((0 == strcmp(dbgmgNamemg[1], tmp)) && ((mgcpMonitorFg & MONITOR_MG1_MSG) == 0))
		      			 ||((0 == strcmp(dbgmgNamemg[2], tmp)) && ((mgcpMonitorFg & MONITOR_MG2_MSG) == 0)))
		      		{
		      			mgcp_asciout_proc("MG already be off\n\r");
		      		}
		      		else if ((mgcpMonitorFg & MONITOR_MG0_MSG) == MONITOR_MG0_MSG)
		      		{
		      			mgcpMonitorFg = mgcpMonitorFg & (~MONITOR_MG0_MSG);
		      			strcpy(dbgmgNamemg[0], tmp);
		      		}
		      		else if ((mgcpMonitorFg & MONITOR_MG1_MSG) == MONITOR_MG1_MSG)
		      		{
		      			mgcpMonitorFg = mgcpMonitorFg & (~MONITOR_MG1_MSG);
		      			strcpy(dbgmgNamemg[1], tmp);
		      		}
		      		else
		      		{
		      			mgcpMonitorFg = mgcpMonitorFg & (~MONITOR_MG2_MSG);
		      			strcpy(dbgmgNamemg[2], tmp);
		      		}	
		      	}
		      	else
		      	{
		      		mgcp_asciout_proc("Can not off more MG\n\r");
		      	}	
		      }
		      else if (0 == strcmp(dbgmgNamemg[0], tmp))
		      {  
		        mgcpMonitorFg = mgcpMonitorFg & (~MONITOR_MG0_MSG);
		      }
		      else if (0 == strcmp(dbgmgNamemg[1], tmp))
		      {
		        mgcpMonitorFg = mgcpMonitorFg & (~MONITOR_MG1_MSG);
		      }
		      else if (0 == strcmp(dbgmgNamemg[2], tmp))
		      {
		        mgcpMonitorFg = mgcpMonitorFg & (~MONITOR_MG2_MSG);		      
		      }
		      else
		      {
		        mgcp_asciout_proc("MG has not been on\n\r");
		      }		      
		    }
		    else
		    {
		      errFg = 1;
		    }
		   } 
		  }
		}
/*		else if (strncmp(asciiInPtr,"log trk-", 8) == 0)
		{
		  if (NULL == (poo = strrchr(asciiInPtr, 'o')))
		  {
		     errFg = 1;
		  }
		  else
		  {

		    mgNM = strchr(asciiInPtr, '-');
           
        if (NULL == (trkNM = strchr(mgNM+1, '-')))
        {
          errFg = 1;
        }
        else
        {		      
		      mgcp_clrncpy(tmp, mgNM+1, trkNM-mgNM-1);
		         
		      tmptrkNum = strtoul(trkNM+1, NULL, 10);
		         
		      if (0 == strcmp(poo, "on"))
		      {
		        strcpy(dbgmgNametrk, tmp);
		        
		        dbgtrkNumtrk = tmptrkNum;
		       
            mgcpMonitorFg |= MONITOR_TRK_MSG;
          }
		      else if (0 == strcmp(poo, "off"))
		      {		       	  		      
		         if ((0 == strcmp(dbgmgNamemg, tmp))||((0 == strcmp(dbgmgNametrk, tmp))&&(dbgtrkNumtrk == tmptrkNum)))
		         {             
               strcpy(dbgmgNametrk, tmp);
             
               dbgtrkNumtrk = tmptrkNum;   
		            
		           mgcpMonitorFg &= (~MONITOR_TRK_MSG);
		         }
		         else
		         {
		           mgcp_asciout_proc("trk already be off!\n\r");
		         }
		      }
		      else
		      {
		        errFg = 1;
		      }                        
		    }
		  }
		}
		else if (strncmp(asciiInPtr,"log chl-", 8) == 0)
		{
      if (NULL == (poo = strrchr(asciiInPtr, 'o')))
		  {
		     errFg = 1;
		  }
		  else
		  {

		     mgNM = strchr(asciiInPtr, '-');
           
         if (NULL == (trkNM = strchr(mgNM+1, '-')))
         {
           errFg = 1;
         }
         else
         {
           if (NULL == (chlNM = strchr(trkNM, '-')))
           {
             errFg = 1;
           }
           else
           {
		      
	           mgcp_clrncpy(tmp, mgNM+1, trkNM-mgNM-1);
	           
	           tmptrkNum = strtoul(trkNM+1, NULL, 10);
		           
	           tmpchlNum = strtoul(chlNM+1, NULL, 10);

           	 if (0 == strcmp(poo, "on"))
		         {		         
		           strcpy(dbgmgNamechl, tmp);
		         
               dbgtrkNumchl = tmptrkNum;
               
               dbgchlNumchl = tmpchlNum;
		       
               mgcpMonitorFg |= MONITOR_TRK_MSG;
             }
		         else if (0 == strcmp(poo, "off"))
		         {
		           if ((0 == strcmp(dbgmgNamemg, tmp))||((0 == strcmp(dbgmgNametrk, tmp))&&(dbgtrkNumtrk == tmptrkNum))
		           ||((0 == strcmp(dbgmgNamechl, tmp))&&(dbgtrkNumchl == tmptrkNum)&&(dbgchlNumchl == tmpchlNum)))
		           {
		             strcpy(dbgmgNamechl, tmp);
		         
                 dbgtrkNumchl = tmptrkNum;
               
                 dbgchlNumchl = tmpchlNum;
                 
		             mgcpMonitorFg &= (~MONITOR_TRK_MSG);
		           }
		           else
		           {
		             mgcp_asciout_proc("chl already be off!\n\r");
		           }
		         }
		         else
		         {
		           errFg = 1;
		         }		                          
           }
         }
		  }		  
		}
		*/

    else if (strcmp(asciiInPtr, "list") == 0)
    {
       if ((mgcpMonitorFg & MONITOR_MG0_MSG) == MONITOR_MG0_MSG)
       {
         sprintf(mgcpAsciTempBuf, "MG :%s  on\r\n", dbgmgNamemg[0]);
      
         mgcp_asciout_proc(mgcpAsciTempBuf);
       }
       else
       {
         sprintf(mgcpAsciTempBuf, "MG :%s  off\r\n", dbgmgNamemg[0]);
      
         mgcp_asciout_proc(mgcpAsciTempBuf);       	
       }

       if ((mgcpMonitorFg & MONITOR_MG1_MSG) == MONITOR_MG1_MSG)
       {
         sprintf(mgcpAsciTempBuf, "MG :%s  on\r\n", dbgmgNamemg[1]);
      
         mgcp_asciout_proc(mgcpAsciTempBuf);
       }
       else
       {
         sprintf(mgcpAsciTempBuf, "MG :%s  off\r\n", dbgmgNamemg[1]);
      
         mgcp_asciout_proc(mgcpAsciTempBuf);       	
       }
      
       if ((mgcpMonitorFg & MONITOR_MG2_MSG) == MONITOR_MG2_MSG)
       {
         sprintf(mgcpAsciTempBuf, "MG :%s  on\r\n", dbgmgNamemg[2]);
      
         mgcp_asciout_proc(mgcpAsciTempBuf);
       }
       else
       {
         sprintf(mgcpAsciTempBuf, "MG :%s  off\r\n", dbgmgNamemg[2]);
      
         mgcp_asciout_proc(mgcpAsciTempBuf);       	
       }            
    }

		else if (strcmp(asciiInPtr,"log all") == 0)
		{
			strcpy(dbgmgNamemg[0], "");
			strcpy(dbgmgNamemg[1], "");
			strcpy(dbgmgNamemg[2], "");
			mgcpMonitorFg = MONITOR_ALL;
		}
		else if (strcmp(asciiInPtr,"log none") == 0)
		{
			strcpy(dbgmgNamemg[0], "");
			strcpy(dbgmgNamemg[1], "");
			strcpy(dbgmgNamemg[2], "");			
			mgcpMonitorFg = MONITOR_NONE;
		}
		else if (strcmp(asciiInPtr,"help") == 0)
		{
			mgcp_asciout_proc(log_help);
		}
		else if (isdigit(asciiInPtr[0]))
		{
			num = strtoul(asciiInPtr, NULL, 10);
			disp_page[inPage] = num;
			mgcp_disp_page(inPage);
		}
		else if ((strStart = strstr(asciiInPtr, ">")) != NULL)
		{
			num = strtoul(strStart + 1, NULL, 10);
			disp_page[inPage] += num;
			mgcp_disp_page(inPage);
		}
		else if ((strStart = strstr(asciiInPtr, "<"))!= NULL)
		{
			num = strtoul(strStart + 1, NULL, 10);
			disp_page[inPage] -= num;
			mgcp_disp_page(inPage);
		}
		else
			errFg = 1;
		
		if (errFg == 0)
			mgcp_asciout_proc("Command OK!\n\r");
		else
			mgcp_asciout_proc("Command Error!\n\r");
		
		strcpy(mgcpAsciInBuf, "\0");
	}
}

int	mgcp_hex_to_ascii(BYTE *fromHex, BYTE fromLen, BYTE *toAsc)
{
	int	i, j;
	
	j = 0;
	
	for (i = 0; i< fromLen; i++)
	{
		toAsc[j] = ascii[fromHex[i] >> 4];
		j++;
		toAsc[j] = ascii[fromHex[i] & 0x0F];
		j++;
		toAsc[j] = ' ';
		j++;
	}
	
	toAsc[j] = '\0';
	
	return 1;
}

int mgcp_log_err(BYTE *errMsg)
{
	if ((mgcpMonitorFg & MONITOR_ERROR) == MONITOR_ERROR)
	{
		mgcp_asciout_proc("\33[31m");
		if (strlen(errMsg) >= MGCP_MAX_ASCITMP_LEN)
			sprintf(mgcpAsciTempBuf, "log msg is too long: %d\n\r", strlen(errMsg));
		else
			sprintf(mgcpAsciTempBuf, "%s\n\r", errMsg);
		strcat(mgcpAsciTempBuf, "\33[37m");
		mgcp_asciout_proc(mgcpAsciTempBuf);
	}
	
	return 1;
}
