/* 
** CDMA 2000 project, smpp module
**
** File name: smpp_debug.c
** Written by Li Long at 2004-11-25
** CVS $Id: smpp_debug.c,v0.1 2004/11/25 17:03:14 lilong Exp $
**
*/

#include "./include/smpp.h"
#include "./include/smpp_ext.h"

static	char	smpp_debug_status = 1;
static	WORD	disp_page[10];
static	char	*disp_ptr = (char *) disp_page;

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

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

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

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

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

static DWORD	smpp_page_line[20]	=
	{
		1,3,6,1,4,1,1373,1,1,2,3,1,2,SMPP_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 char	title1_p[] = 
{
	"    SMPP    R9    Page 01    link status (32 links)\n\r\
                   0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15\n\r\
link_status\n\r\
\n\r\
daemon sock\r\n\
tcp_client_flag\r\n\
\r\n\
sock_fd\r\n\
\r\n\
\r\n\
\r\n\
\r\n\
\r\n\
\r\n\
\r\n\
\r\n\
\tLink status:\r\n\
\t\t00 -- Not init\r\n\
\t\t01 -- IDLE\r\n\
\t\t02 -- WAITING\r\n\
\t\t03 -- ESTABLISHED\r\n\
\t\t04 -- SUSPEND"
};

static char	title2_p[] = 
{
	"    SMPP    R9    Page 02    debug struct (32 links)\n\r\
                   0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15\n\r\
debug_switch\n\r\
link_switch\n\r"
};

static char	title3_p[] = 
{
	"    SMPP    R9    Page 03    link type (link 0~21)\n\r\
                   type\t\t00 -- UDP,\t01 -- TCP\n\r\
link 0\n\r\
link 1\n\r\
link 2\n\r\
link 3\n\r\
link 4\n\r\
link 5\n\r\
link 6\n\r\
link 7\n\r\
link 8\n\r\
link 9\n\r\
link 10\n\r\
link 11\n\r\
link 12\n\r\
link 13\n\r\
link 14\n\r\
link 15\n\r\
link 16\n\r\
link 17\n\r\
link 18\n\r\
link 19\n\r\
link 20\n\r\
link 21\n\r"
};

static char	title4_p[] = 
{
	"    SMPP    R9    Page 04    link type (link 22~31)\n\r\
                   type\t\t00 -- UDP,\t01 -- TCP\n\r\
link 22\n\r\
link 23\n\r\
link 24\n\r\
link 25\n\r\
link 26\n\r\
link 27\n\r\
link 28\n\r\
link 29\n\r\
link 30\n\r\
link 31\n\r"
};

static char	title5_p[] = 
{
	"    SMPP    R9    Page 05    server_sock & client_sock\n\r\
	\r\n\
server_sock\n\r\
\n\r\
\n\r\
\n\r\
\n\r\
\n\r\
\n\r\
\n\r\
client_sock\n\r\
\n\r"
};

BYTE smpp_disp_line(BYTE page, BYTE line)
{
	BYTE	disp_length;
	
	disp_length = 16;
	disp_ptr 	= (char *) disp_page;
	
	switch (page)
	{
		case 1:
			switch (line)
			{
			case 0:
			case 1:
				disp_ptr 	= (char *) &smpp_state[disp_page[page]] + (line * 16);
				break;
			case 2:
				disp_ptr	= (char *) &(Mysock.DaemonSock);
				disp_length	= 1;
				break;
			case 3:
			case 4:
				disp_ptr	= (char *) &smpp_TCPClientFlag[disp_page[page]]+(line-3)*16;
				break;
			case 5:
			case 6:
			case 7:
			case 8:
			case 9:
			case 10:
			case 11:
			case 12:
				disp_ptr	= (char *) &(Mysock.Sockets[disp_page[page]]) + ((line-5)*16);
				break;
			default:
				disp_length = 0;
				break;
			}
			break;
		case 2:	// Page 2: link debug status
			if (line == 0)
			{
				disp_ptr 	= (char *) &smppDebug.debug_switch;
				disp_length = 1;
			}
			else if (line < 3)
				disp_ptr 	= (char *) &smppDebug.link_switch[disp_page[page]] + ((line - 1) * 16);
			else
				disp_length = 0;
			break;
		case 3:	//page 3: link type
			if (line<22)
			{
				disp_ptr = (char *) &smpp_param[line].link_type;
				disp_length = 1;
			}
			break;
		case 4:	//page 3: link type(0~21)
			if (line<10)
			{
				disp_ptr = (char *) &smpp_param[line+22].link_type;
				disp_length = 1;
			}else
				disp_length = 0;
			break;
		case 5: //page 5: server_socket and client_sockets
			switch (line) {
			case 0:
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
				disp_ptr	= (char *) &Mysock.server_sock[disp_page[page]] + line*16;
				break;
			case 8:
			case 9:
			case 10:
			case 11:
			case 12:
			case 13:
			case 14:
			case 15:
				disp_ptr	= (char *) &Mysock.client_sock[disp_page[page]] + (line-8)*16;
				break;
			default:
				disp_length = 0;
				break;
			}
			break;
		default:
			disp_length = 0;
			break;
	}
	
	return disp_length;
}

void smpp_disp_page(BYTE page)
{
	BYTE	disp_line;
	BYTE	disp_length;

	smpp_page_line[PAGE_POINT] = page + 5;
	
	for (disp_line = 0; disp_line < 22; disp_line++)
	{
		disp_length = smpp_disp_line(page, disp_line);
		smpp_page_line[LINE_POINT] = disp_line + 2;
		smpp_page_line[LINE_POINT + 1] = 2;	// Data Pointer
		debug_set_response(BASE_ID_LEN + 2, smpp_page_line, (BYTE *)disp_ptr, disp_length);
	}
}

void smpp_debugInit()
{
	int page;
	char* ptr;

	smppDebug.debug_switch		= 0;

	debug_set_response(15, smpp_status_id, (BYTE *)&smpp_debug_status, 1);//smpp status
	debug_set_response(15, smpp_version_id, (BYTE *)SMPP_MODULE_VERSION, strlen(SMPP_MODULE_VERSION));
	debug_set_response(15, smpp_ascii_in_id, (BYTE *)smpp_debug_asciiIn, MAX_ASCIN_BUF_LEN);
	debug_set_response(15, smpp_ascii_out_id, (BYTE *)smpp_debug_asciiOut, MAX_ASCOUT_BUF_LEN);

	for (page=1; page<=5; page++)
	{
		switch (page)
		{
		case 1:
			ptr	= title1_p;
			break;
		case 2:
			ptr	= title2_p;
			break;
		case 3:
			ptr	= title3_p;
			break;
		case 4:
			ptr	= title4_p;
			break;
		case 5:
			ptr = title5_p;
			break;
		default:
			break;
		}
		smpp_page_title[PAGE_POINT]	= 5 + page;
		debug_set_response(BASE_ID_LEN + 1, smpp_page_title, (BYTE *)ptr, strlen(ptr));
		smpp_disp_page(page);
	}
	
	#ifdef SMPP_DEBUG
	printf("smpp debug init.\n");
	#endif //end ifdef SMPP_DEBUG
}

void smpp_list_linkparam(int linkNo) {
	char	debug_str[256];
	struct in_addr	addr;

	if (linkNo >= MAX_SMPP_LINK)
	{	
		smpp_send_error("Link number is too big.\n\r");
		return;
	}
	
	sprintf(debug_str, "\r\nLink %d parameter:\r\n", linkNo);
	smpp_send_ascout(debug_str);
	sprintf(debug_str, "link_enable: %d\r\n", smpp_param[linkNo].link_enable);
	smpp_send_ascout(debug_str);
	sprintf(debug_str, "link_type: %d\r\n", smpp_param[linkNo].link_type);
	smpp_send_ascout(debug_str);
	sprintf(debug_str, "server_type: %d\r\n", smpp_param[linkNo].server_type);	
	smpp_send_ascout(debug_str);
	sprintf(debug_str, "session_type: %d\r\n", smpp_param[linkNo].session_type);
	smpp_send_ascout(debug_str);
	sprintf(debug_str, "system_id: %s\r\n", smpp_param[linkNo].sys_id);
	smpp_send_ascout(debug_str);
	sprintf(debug_str, "password: %s\r\n", smpp_param[linkNo].password);
	smpp_send_ascout(debug_str);
	sprintf(debug_str, "system_type: %s\r\n", smpp_param[linkNo].system_type);
	smpp_send_ascout(debug_str);
	sprintf(debug_str, "localGTT: %s\r\n", smpp_param[linkNo].localGTT);
	smpp_send_ascout(debug_str);
	sprintf(debug_str, "remoteGTT: %s\r\n", smpp_param[linkNo].remoteGTT);
	smpp_send_ascout(debug_str);
	addr.s_addr	= smpp_param[linkNo].local_ip;
	sprintf(debug_str, "local_ip: %s\r\n", inet_ntoa(addr));
	smpp_send_ascout(debug_str);
	addr.s_addr	= smpp_param[linkNo].remote_ip;
	sprintf(debug_str, "remote_ip: %s\r\n", inet_ntoa(addr));
	smpp_send_ascout(debug_str);
	sprintf(debug_str, "session_init_timer: %u\r\n", smpp_param[linkNo].session_init_timer);
	smpp_send_ascout(debug_str);
	sprintf(debug_str, "enquire_link_timer: %u\r\n", smpp_param[linkNo].enquire_link_timer);
	smpp_send_ascout(debug_str);
	sprintf(debug_str, "inactivity_timer: %u\r\n", smpp_param[linkNo].inactivity_timer);
	smpp_send_ascout(debug_str);
	sprintf(debug_str, "response_timer: %u\r\n", smpp_param[linkNo].response_timer);
	smpp_send_ascout(debug_str);
	sprintf(debug_str, "service_number: %s\r\n", smpp_param[linkNo].service_number);
	smpp_send_ascout(debug_str);
	sprintf(debug_str, "localSSN: %d\r\n", smpp_param[linkNo].localSSN);
	smpp_send_ascout(debug_str);
	sprintf(debug_str, "remoteSSN: %d\r\n", smpp_param[linkNo].remoteSSN);
	smpp_send_ascout(debug_str);
}

void smpp_debugTimer()
{
	int		i, linkNo = 0;
	char	debugBuf[1024];

	if (strlen(smpp_debug_asciiIn) > 0)
	{
		if (strcmp(smpp_debug_asciiIn+1, "help") == 0)
		{
			smpp_send_ascout("SMPP Debug Monitor Help:\n\r\n\r");
			smpp_send_ascout("log none               Turn off all log items\n\r");
			smpp_send_ascout("log all                Turn on all log items\n\r");
			smpp_send_ascout("log link=\x1b[34mxx\x1b[0m on/off     Turn on/off link x messages\n\r");
			smpp_send_ascout("list par.\x1b[34mxx\x1b[0m 		List link x parameter\r\n");
			sprintf(debugBuf, "ver. %s T%03d\r\n",SMPP_MODULE_VERSION,SMPP_MODULE_PATCH);
			smpp_send_ascout(debugBuf);			
		}else if (strcmp(smpp_debug_asciiIn+1, "log none") == 0)
		{
			smppDebug.debug_switch	= 0;
			for (i=0; i<MAX_SMPP_LINK; i++)
			{
				smppDebug.link_switch[i]	= 0;
			}
			smpp_send_ascout("smpp turn off all log items.\n\r");
		} 
		else if (strcmp(smpp_debug_asciiIn+1, "log all") == 0) {
			smppDebug.debug_switch	= 1;
			smpp_send_ascout("smpp turn on all log items.\n\r");
		}
		else if (strncmp(smpp_debug_asciiIn+1, "list par.", 9) == 0) {
			if ((smpp_debug_asciiIn[10] >= '0') && (smpp_debug_asciiIn[10] <= '9')) {
				linkNo	= smpp_debug_asciiIn[10] - '0';
				if ((smpp_debug_asciiIn[11] >= '0') && (smpp_debug_asciiIn[11] <= '9')) {
					linkNo = linkNo*10+(smpp_debug_asciiIn[11]-'0');
//					if (linkNo > MAX_SMPP_LINK)
//						smpp_send_error("Link number is too big.\n\r");
					smpp_list_linkparam(linkNo);
				}
				else 
					smpp_list_linkparam(linkNo);
			}
			else
				smpp_send_error("Link number is error");
		}
		else if (strncmp(smpp_debug_asciiIn+1, "save", 4) == 0) {
			smpp_writeParam();
		}
		else if (strncmp(smpp_debug_asciiIn+1, "log link=", 9) == 0) {
			if ((smpp_debug_asciiIn[10] >= '0') && (smpp_debug_asciiIn[10] <= '9'))
			{
				linkNo	= smpp_debug_asciiIn[10] - '0';
				if ((smpp_debug_asciiIn[11] >= '0') && (smpp_debug_asciiIn[11] <= '9'))
				{
					linkNo = linkNo*10+(smpp_debug_asciiIn[11]-'0');
					if (linkNo > MAX_SMPP_LINK)
					{
						smpp_send_error("Link number is too big.\n\r");
					}
					else if (strncmp(smpp_debug_asciiIn+13, "on", 2) == 0)
					{
						smppDebug.link_switch[linkNo]	= 1;
						sprintf(debugBuf, "Link %d debug on.\n\r", linkNo);
						smpp_send_ascout(debugBuf);
					}else if (strncmp(smpp_debug_asciiIn+13, "off", 3) == 0)
					{
						smppDebug.link_switch[linkNo]	= 0;
						sprintf(debugBuf, "Link %d debug off.\n\r", linkNo);
						smpp_send_ascout(debugBuf);
					}else
					{
						smpp_send_error("Command Error.\n\r");
					}
				}else if (strncmp(smpp_debug_asciiIn+12, "on", 2) == 0)
				{
					smppDebug.link_switch[linkNo]	= 1;
					sprintf(debugBuf, "Link %d debug on.\n\r", linkNo);
					smpp_send_ascout(debugBuf);
				}else if (strncmp(smpp_debug_asciiIn+12, "off", 3) == 0)
				{
					smppDebug.link_switch[linkNo]	= 0;
					sprintf(debugBuf, "Link %d debug off.\n\r", linkNo);
					smpp_send_ascout(debugBuf);
				}else
				{
					smpp_send_error("Command Error.\n\r");
				}
			}
			else
				smpp_send_error("Link number error.\n\r");
		}else
		{
			smpp_send_error("Command Error.\n\r");
		}

		smpp_debug_asciiIn[0]	= '\0';
	}
}

void smpp_send_ascout(char *sdBuf)
{
	if (strlen(sdBuf)+strlen(smpp_debug_asciiOut) < MAX_ASCOUT_BUF_LEN)
	{
		strcat(smpp_debug_asciiOut, sdBuf);
	}
}

void smpp_send_error(char *sdBuf)
{
	if (smppDebug.debug_switch == 1)
	{
		smpp_send_ascout("\x1b[31m");
		smpp_send_ascout(sdBuf);
		smpp_send_ascout("\x1b[0m");
	}
}
