#include "./include/sip_pub.h"
#include "./include/sip.h"
#include "./include/sip_const.h"
#include "./include/sip_struct.h"
#include "./include/sip_ext.h"
#include "../../debug/src/include/debug.h"

#define SIP_DEBUG_ID	29
#define	SIP_VER_DEBUG	"R9V2_01"

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

static BYTE	log_help[] = {
"SIP 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 sip on/off]                      display sip msg logs\n\r\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,SIP_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,SIP_DEBUG_ID + 2,2
	};
	
static DWORD	debug_ascin_id[20] = 
	{
		1,3,6,1,4,1,1373,1,1,2,3,1,2,SIP_DEBUG_ID + 2,3
	};

static DWORD	debug_ascout_id[20] = 
	{
		1,3,6,1,4,1,1373,1,1,2,3,1,2,SIP_DEBUG_ID + 2,4
	};
		
static	DWORD	debug_page_title[20] = 		
	{
		1,3,6,1,4,1,1373,1,1,2,3,1,2,SIP_DEBUG_ID + 2,1,1
	};
	
static	DWORD	debug_page_line[20] = 		
	{
		1,3,6,1,4,1,1373,1,1,2,3,1,2,SIP_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[] = 
{
	"    SIP    Page 01    Transaction 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 sip_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 *) &sipTrans[disp_page[page]];
			break;
		default:
			disp_length = 0;
			break;
	}
	
	return disp_length;
}

void sip_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 = sip_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	sip_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, SIP_VER_DEBUG, 10);
	debug_set_response(BASE_ID_LEN, debug_ascin_id, sipAsciInBuf, 4096);
	debug_set_response(BASE_ID_LEN, debug_ascout_id, sipAsciOutBuf, 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));
		sip_disp_page(page);
	}
	
	return 1;
}

int	sip_asciout_proc(BYTE *outStr)
{
	int outLen;
	
	outLen = strlen(outStr);
		
	if (outLen + strlen(sipAsciOutBuf) > SIP_MAX_ASCIIOUT_LEN - 2)
	{
		strcpy(sipAsciOutBuf, outStr);
	}
	else
		strcat(sipAsciOutBuf, outStr);
	
	return 1;
}

void sip_mon(void)
{
	BYTE	inPage, errFg = 0;
	BYTE	*asciiInPtr 	= NULL;
	BYTE	*strStart 		= NULL;
	WORD	strLen, num;
	
	if ((strLen = strlen(sipAsciInBuf)) > 0)
	{
		inPage = sipAsciInBuf[0] - 1;
		asciiInPtr = sipAsciInBuf + 1;
		
		if (inPage > 7)
			errFg = 1;
		else if (strcmp(asciiInPtr,"c") == 0)
		{
		}
		else if (strcmp(asciiInPtr,"log error on") == 0)
		{
			sipMonitorFg = sipMonitorFg | SIP_MONITOR_ERROR;
		}
		else if (strcmp(asciiInPtr,"log error off") == 0)
		{
			sipMonitorFg = sipMonitorFg & (~SIP_MONITOR_ERROR);			
		}
		else if (strcmp(asciiInPtr,"log sip on") == 0)
		{
			sipMonitorFg = sipMonitorFg | MONITOR_SIP_MSG;
		}
		else if (strcmp(asciiInPtr,"log sip off") == 0)
		{
			sipMonitorFg = sipMonitorFg & (~MONITOR_SIP_MSG);			
		}
		else if (strcmp(asciiInPtr,"log all") == 0)
		{
			sipMonitorFg = SIP_MONITOR_ALL;
		}
		else if (strcmp(asciiInPtr,"log none") == 0)
		{
			sipMonitorFg = SIP_MONITOR_NONE;
		}
		else if (strcmp(asciiInPtr,"help") == 0)
		{
			sip_asciout_proc(log_help);
		}
		else if (isdigit(asciiInPtr[0]))
		{
			num = strtoul(asciiInPtr, NULL, 10);
			disp_page[inPage] = num;
			sip_disp_page(inPage);
		}
		else if ((strStart = strstr(asciiInPtr, ">")) != NULL)
		{
			num = strtoul(strStart + 1, NULL, 10);
			disp_page[inPage] += num;
			sip_disp_page(inPage);
		}
		else if ((strStart = strstr(asciiInPtr, "<"))!= NULL)
		{
			num = strtoul(strStart + 1, NULL, 10);
			disp_page[inPage] -= num;
			sip_disp_page(inPage);
		}
		else
			errFg = 1;
		
		if (errFg == 0)
			sip_asciout_proc("Command OK!\n\r");
		else
			sip_asciout_proc("Command Error!\n\r");
		
		strcpy(sipAsciInBuf, "\0");
	}
}

int	sip_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 sip_log_err(BYTE *errMsg)
{
	if ((sipMonitorFg & SIP_MONITOR_ERROR) == SIP_MONITOR_ERROR)
	{
		sip_asciout_proc("\33[31m");
		if (strlen(errMsg) >= SIP_MAX_ASCITMP_LEN)
			sprintf(sipAsciTempBuf, "log msg is too long: %d\n\r", strlen(errMsg));
		else
			sprintf(sipAsciTempBuf, "%s\n\r", errMsg);
		strcat(sipAsciTempBuf, "\33[37m");
		sip_asciout_proc(sipAsciTempBuf);
	}
	
	return 1;
}
