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

#include "./include/smpp.h"

#define SD_PKT_NUM	10
#define STORE_MSG_ITEM	5

typedef struct	SMPP_MSG_STORE_STRUCT
{
	BYTE	short_message[SMPP_MSG_LEN];
	BYTE	message_id[65];
} SMPP_MSG_STORE_STRUCT;

typedef struct SMPP_LINK_ATTRIBUTE
{
	BYTE	enable;
	BYTE	session_type;		//BIND_TX/BIND_RX/BIND_TRX
	BYTE	message_mode;		//STORE AND FORWARD/DATAGRAM/TRANSACTION
	BYTE	terminal_type;	//SMPP_CLIENT/SMPP_SERVER
} SMPP_LINK_ATTRIBUTE;
static struct itimerval itimer,old_itimer;
static int		timer_counter[MAX_SMPP_LINK];
static int		stateFlag[MAX_SMPP_LINK];
static int		msg_count[MAX_SMPP_LINK];
static int		msg_pointer[MAX_SMPP_LINK];
static SMPP_MSG_STORE_STRUCT	store_msg_buf[MAX_SMPP_LINK][STORE_MSG_ITEM];
static SMPP_LINK_ATTRIBUTE		smpp_link_attr[MAX_SMPP_LINK];

static void On_Timer();
static void SetTimer();
static void smpp_test_init();
static void sendTestSM(int, int);
static int smpp_onDataRecv(BYTE linkNo, SMPP_MSG *pData, WORD dataLen);
static int smpp_onLinkStateChange(BYTE linkNo, BYTE linkstate);

void smpp_test_init()
{
	int i;

	for (i=0; i<MAX_SMPP_LINK; i++)
	{
		memset(store_msg_buf[i], 0, sizeof(SMPP_MSG_STORE_STRUCT)*STORE_MSG_ITEM);
		msg_pointer[i]	= 0;
	}
}

int main(int argc,char *argv[])
{
	pid_t		pid;
	int i, ch;
	BYTE	link, ret;
	unsigned char tx_service_number[8], rx_service_number[8];
	unsigned char	store_and_forward_service_number[8];

	heartbeat_init(0xFFFF);
//	#ifdef SMPP_DEBUG
	debug_init();
//	#endif 
	iptrMainInit();
	snmp_init(SMPP_PORT);
	mtp_shm_init();
	sccp_init();

	smpp_init_new();
	smpp_test_init();

	for (i=0; i<MAX_SMPP_LINK; i++)
	{
		stateFlag[i]	= FALSE;
		timer_counter[i]= 0;
		msg_count[i]	= 0;
	}

	opterr = 0;
	strcpy(tx_service_number, "9910");
	strcpy(rx_service_number, "9920");
	strcpy(store_and_forward_service_number, "6666");
	while( (ch = getopt(argc, argv, "scabd")) != -1 )
	{
		switch (ch)
		{
		case 's':
			ret = smpp_registerLink(SMPP_SERVER, BIND_TX, tx_service_number, DATAGRAM);
			if ((ret&0x80) == 0x80) {
				link = ret&0x7f;
				printf("(TX) session_type = %d, linkNo=%d\n", BIND_TX, link);
				smpp_attach_link(link, smpp_onDataRecv, smpp_onLinkStateChange);

				smpp_link_attr[link].enable				= 1;
				smpp_link_attr[link].session_type	= BIND_TX;
				smpp_link_attr[link].message_mode	= DATAGRAM;
				smpp_link_attr[link].terminal_type	= SMPP_SERVER;
			}
			break;
		case 'c':
			ret = smpp_registerLink(SMPP_CLIENT, BIND_TX, tx_service_number, DATAGRAM);
			if ((ret&0x80) == 0x80) {
				link = ret&0x7f;
				printf("(TX) session_type = %d, linkNo=%d\n", BIND_TX, link);
				smpp_attach_link(link, smpp_onDataRecv, smpp_onLinkStateChange);

				smpp_link_attr[link].enable				= 1;
				smpp_link_attr[link].session_type	= BIND_TX;
				smpp_link_attr[link].message_mode	= DATAGRAM;
				smpp_link_attr[link].terminal_type	= SMPP_CLIENT;
			}
			break;
		case 'a':	
			ret = smpp_registerLink(SMPP_CLIENT, BIND_RX, rx_service_number, DATAGRAM);
			if ((ret&0x80) == 0x80) {
				link = ret&0x7f;
				printf("(RX) session_type = %d, linkNo=%d\n", BIND_RX, link);
				smpp_attach_link(link, smpp_onDataRecv, smpp_onLinkStateChange);

				smpp_link_attr[link].enable				= 1;
				smpp_link_attr[link].session_type	= BIND_RX;
				smpp_link_attr[link].message_mode	= DATAGRAM;
				smpp_link_attr[link].terminal_type	= SMPP_CLIENT;
			}
			break;
		case 'b':	
			ret = smpp_registerLink(SMPP_SERVER, BIND_RX, rx_service_number, DATAGRAM);
			if ((ret&0x80) == 0x80) {
				link = ret&0x7f;
				printf("(RX) session_type = %d, linkNo=%d\n", BIND_RX, link);
				smpp_attach_link(link, smpp_onDataRecv, smpp_onLinkStateChange);

				smpp_link_attr[link].enable				= 1;
				smpp_link_attr[link].session_type	= BIND_RX;
				smpp_link_attr[link].message_mode	= DATAGRAM;
				smpp_link_attr[link].terminal_type	= SMPP_SERVER;
			}
			break;
		case 'd':
			if ((pid=fork())!=0) exit(0);
			setsid();
			break;
		default:
			break;
		}
	}

	SetTimer();
	while(1)
	{
		usleep(50);
	}
	return 1;
}

void On_Timer()
{
	int i;

	for (i=0; i<MAX_SMPP_LINK; i++)
	{	// 2 msgs per second
		if (timer_counter[i]==25)
			timer_counter[i]=0;
		if ((timer_counter[i]%25)==0)
		{
			if (stateFlag[i] == TRUE)
			{
				if ( (smpp_link_attr[i].session_type == BIND_TX) && (smpp_link_attr[i].terminal_type == SMPP_CLIENT) )
				{
					sendTestSM(i, 0);
					msg_count[i]++;
				}
				if ( (smpp_link_attr[i].session_type == BIND_RX) && (smpp_link_attr[i].terminal_type == SMPP_SERVER) )
				{
					sendTestSM(i, 1);
					msg_count[i]++;
				}
				if (smpp_link_attr[i].session_type == BIND_TRX)
				{
					sendTestSM(i, 2);
					msg_count[i]++;
				}
			}
		}
		timer_counter[i]++;

		if (msg_count[i] == SD_PKT_NUM)
		{
			smpp_close(i);
		}
	}

	iptrans_timer();
//	#ifdef SMPP_DEBUG
	debug_rt();
//	#endif //end ifdef SMPP_DEBUG
	snmp_timer();
	mtp3_proc();
	heartbeat_timer();
	sccp_timer();

	smpp_fsm();
}

void SetTimer()
{
	struct sigaction	act;
	act.sa_handler=On_Timer;
	sigemptyset(&act.sa_mask);
	act.sa_flags=0;
	if(sigaction(SIGALRM,&act,NULL)<0)
	{
		perror("Produce Sigaction");
		exit(1);
	}
	
	itimer.it_interval.tv_sec=0;
	itimer.it_interval.tv_usec=20*1000;
	itimer.it_value.tv_sec=0;
	itimer.it_value.tv_usec=20*1000;

	if (setitimer(ITIMER_REAL,&itimer,&old_itimer) !=0 )
	{
		printf("Setting Timer  error! \n");
		exit(1);
	}
}

int smpp_onDataRecv(BYTE linkNo, SMPP_MSG *pMsg, WORD dataLen)
{
	int sequence_number=100;
	SMPP_MSG	smpp_msg;
	int	pointer;
	int i;

	switch (pMsg->message_type)
	{
	case 0x0B:	/* SUBMIT SM */
		printf("[TEST PROGRAM]Link %d received SUBMIT SM:\n", linkNo);

		/* store smpp message */
		if (smpp_link_attr[linkNo].message_mode == STORE_AND_FORWARD)
		{
			//if (pMsg->pdu.submit_sm.sm_length < SMPP_MSG_LEN)
			{
				pointer	= (msg_pointer[linkNo]+1)%STORE_MSG_ITEM;
				memcpy(&store_msg_buf[linkNo][pointer].short_message, 
					pMsg->pdu.submit_sm.short_message, pMsg->pdu.submit_sm.sm_length);
				sprintf(store_msg_buf[linkNo][pointer].message_id, 
					"%ld", pMsg->pdu.submit_sm.head.sequence_number);
			}
		}
		
		/* fill with SUBMIT SM RESP structure. */
		smpp_msg.message_type	= 0x0C;
		smpp_msg.pdu.submit_sm_resp.head.command_status		= htonl(ESME_ROK);
		smpp_msg.pdu.submit_sm_resp.head.sequence_number	= pMsg->pdu.submit_sm.head.sequence_number;
		sprintf(smpp_msg.pdu.submit_sm_resp.message_id, "%ld", 
			smpp_msg.pdu.submit_sm_resp.head.sequence_number);
		smpp_send(linkNo, &smpp_msg, dataLen);
		break;
	case 0x0C:	/* SUBMIT SM RESP */
		printf("[TEST PROGRAM]Link %d received SUBMIT SM RESP.\n", linkNo);
		break;
	case 0x0D:	/* SUBMIT MULTI */
		printf("[TEST PROGRAM]Link %d received SUBMIT MULTI.\n", linkNo);
		/* store smpp message */
		if (smpp_link_attr[linkNo].message_mode == STORE_AND_FORWARD)
		{
			//if (pMsg->pdu.submit_multi.sm_length < SMPP_MSG_LEN)
			{
				pointer	= (msg_pointer[linkNo]+1)%STORE_MSG_ITEM;
				memcpy(&store_msg_buf[linkNo][pointer].short_message, 
					pMsg->pdu.submit_sm.short_message, pMsg->pdu.submit_multi.sm_length);
				sprintf(store_msg_buf[linkNo][pointer].message_id, 
					"%ld", pMsg->pdu.submit_multi.head.sequence_number);
			}
		}
		
		/* fill with SUBMIT MULTI RESP structure. */
		smpp_msg.message_type	= 0x0E;
		smpp_msg.pdu.submit_multi_resp.head.command_status	= htonl(ESME_ROK);
		smpp_msg.pdu.submit_multi_resp.head.sequence_number	= pMsg->pdu.submit_multi.head.sequence_number;
		sprintf(smpp_msg.pdu.submit_multi_resp.message_id, "%ld", 
			smpp_msg.pdu.submit_multi_resp.head.sequence_number);
		smpp_msg.pdu.submit_multi_resp.no_unsuccess	= 0;
		smpp_send(linkNo, &smpp_msg, dataLen);
		break;
	case 0x0E:	/* SUBMIT MULTI RESP */
		printf("[TEST PROGRAM]Link %d received SUBMIT MULTI RESP.\n", linkNo);
		break;
	case 0x0F:	/* DELIVER SM */
		printf("[TEST PROGRAM]Link %d received DELIVER SM.\n", linkNo);

		/* store smpp message */
		if (smpp_link_attr[linkNo].message_mode == STORE_AND_FORWARD)
		{
			//if (pMsg->pdu.deliver_sm.sm_length < SMPP_MSG_LEN)
			{
				pointer	= (msg_pointer[linkNo]+1)%STORE_MSG_ITEM;
				memcpy(&store_msg_buf[linkNo][pointer].short_message, 
					pMsg->pdu.deliver_sm.short_message, pMsg->pdu.deliver_sm.sm_length);
				sprintf(store_msg_buf[linkNo][pointer].message_id, 
					"%ld", pMsg->pdu.deliver_sm.head.sequence_number);
			}
		}

		/* fill with DELIVER SM RESP structure. */
		smpp_msg.message_type	= 0x10;
		smpp_msg.pdu.deliver_sm_resp.head.command_status	= htonl(ESME_ROK);
		smpp_msg.pdu.deliver_sm_resp.head.sequence_number	= pMsg->pdu.deliver_sm.head.sequence_number;
		smpp_msg.pdu.deliver_sm_resp.message_id				= 0x0;
		smpp_send(linkNo, &smpp_msg, dataLen);
		break;
	case 0x10:	/* DELIVER SM RESP */
		printf("[TEST PROGRAM]Link %d received DELIVER SM RESP.\n", linkNo);
		break;
	case 0x11:	/* DATA SM */
		printf("[TEST PROGRAM]Link %d received DATA SM.\n", linkNo);

		/* store smpp message */
		if (smpp_link_attr[linkNo].message_mode == STORE_AND_FORWARD)
		{
			if (strlen(pMsg->pdu.data_sm.message_payload) < SMPP_MSG_LEN)
			{
				pointer	= (msg_pointer[linkNo]+1)%STORE_MSG_ITEM;
				memcpy(&store_msg_buf[linkNo][pointer].short_message, 
					pMsg->pdu.data_sm.message_payload, strlen(pMsg->pdu.data_sm.message_payload));
				sprintf(store_msg_buf[linkNo][pointer].message_id, 
					"%ld", pMsg->pdu.data_sm.head.sequence_number);
			}
		}

		/* fill with DATA SM RESP structure. */
		smpp_msg.message_type	= 0x12;
		smpp_msg.pdu.data_sm_resp.head.command_status	= htonl(ESME_ROK);
		smpp_msg.pdu.data_sm_resp.head.sequence_number	= pMsg->pdu.data_sm.head.sequence_number;
		sprintf(smpp_msg.pdu.data_sm_resp.message_id, "%ld", 
			smpp_msg.pdu.data_sm_resp.head.sequence_number);
		smpp_send(linkNo, &smpp_msg, dataLen);
		break;
	case 0x12:	/* DATA SM RESP */
		printf("[TEST PROGRAM]Link %d received DATA SM RESP.\n", linkNo);
		break;
	case 0x13:	/* QUERY SM */
		printf("[TEST PROGRAM]Link %d received QUERY SM.\n", linkNo);

		pointer	= -1;
		for (i=0; i<STORE_MSG_ITEM; i++)
		{
			if (strcmp(store_msg_buf[linkNo][i].message_id, pMsg->pdu.query_sm.message_id) == 0)
			{
				pointer = i;
				break;
			}
		}

		/* fill with QUERY SM RESP structure */
		smpp_msg.message_type	= 0x14;
		smpp_msg.pdu.query_sm_resp.head.command_status	= htonl(ESME_ROK);
		smpp_msg.pdu.query_sm_resp.head.sequence_number	= pMsg->pdu.query_sm.head.sequence_number;
		sprintf(smpp_msg.pdu.query_sm_resp.message_id, "%ld", 
			smpp_msg.pdu.query_sm_resp.head.sequence_number);
		smpp_msg.pdu.query_sm_resp.final_date[0]	= 0x0;
		if (pointer == -1)
		{
			smpp_msg.pdu.query_sm_resp.message_state	= 0x04;
		}
		else
		{
			smpp_msg.pdu.query_sm_resp.message_state	= 0x02;
		}
		
		smpp_msg.pdu.query_sm_resp.error_code		= 0x0;
		smpp_send(linkNo, &smpp_msg, dataLen);
		break;
	case 0x14:	/* QUERY SM RESP */
		printf("[TEST PROGRAM]Link %d received QUERY SM RESP.\n", linkNo);
		break;
	case 0x15:	/* CANCEL SM */
		printf("[TEST PROGRAM]Link %d received CANCEL SM RESP.\n", linkNo);
		pointer	= -1;
		if (pMsg->pdu.cancel_sm.message_id[0] == 0){
			memset(store_msg_buf[linkNo], 0, sizeof(SMPP_MSG_STORE_STRUCT)*STORE_MSG_ITEM);
			msg_pointer[linkNo]	= 0;
			smpp_msg.pdu.cancel_sm_resp.head.command_status		= htonl(ESME_ROK);
		}else{
			for (i=0; i<STORE_MSG_ITEM; i++)
			{
				printf("[TEST program]message id in query sm=%s, message id in store message=%s",
					pMsg->pdu.cancel_sm.message_id, store_msg_buf[linkNo][i].message_id);
				if (strcmp(store_msg_buf[linkNo][i].message_id, pMsg->pdu.cancel_sm.message_id) == 0)
				{
					pointer = i;
					break;
				}
			}
			if (pointer == -1)
			{
				smpp_msg.pdu.cancel_sm_resp.head.command_status		= htonl(ESME_RCANCELFAIL);
			}else{
				smpp_msg.pdu.cancel_sm_resp.head.command_status		= htonl(ESME_ROK);
			}
		}

		/* fill with CANCEL SM RESP structure */
		smpp_msg.message_type	= 0x16;
		smpp_msg.pdu.cancel_sm_resp.head.sequence_number	= pMsg->pdu.cancel_sm.head.sequence_number;
		smpp_send(linkNo, &smpp_msg, dataLen);
		break;
	case 0x16:	/* CANCEL SM RESP */
		printf("[TEST PROGRAM]Link %d received CANCEL RESP.\n", linkNo);
		break;
	case 0x17:	/* REPLACE SM */
		printf("[TEST PROGRAM]Link %d received REPLACE SM.\n", linkNo);
		pointer	= -1;
		for (i=0; i<STORE_MSG_ITEM; i++)
		{
			if (strcmp(store_msg_buf[linkNo][i].message_id, pMsg->pdu.replace_sm.message_id) == 0)
			{
				pointer = i;
				break;
			}
		}

		/* store new smpp message */
		if (pointer != -1)
		{
			//if (pMsg->pdu.replace_sm.sm_length < SMPP_MSG_LEN)
			{
				memcpy(&store_msg_buf[linkNo][pointer].short_message, 
					pMsg->pdu.replace_sm.short_message, pMsg->pdu.replace_sm.sm_length);
			}
		}

		/* fill with REPLACE SM RESP structure */
		smpp_msg.message_type	= 0x18;
		if (pointer == -1)
		{
			smpp_msg.pdu.replace_sm_resp.head.command_status		= htonl(ESME_RREPLACEFAIL);
		}
		else
		{
			smpp_msg.pdu.replace_sm_resp.head.command_status		= htonl(ESME_ROK);
		}
		smpp_msg.pdu.replace_sm_resp.head.sequence_number	= pMsg->pdu.replace_sm.head.sequence_number;
		smpp_send(linkNo, &smpp_msg, dataLen);
		break;
	case 0x18:	/* REPLACE SM RESP */
		printf("[TEST PROGRAM]Link %d received REPLACE SM RESP.\n", linkNo);
		break;
	default:	
		return 0;
	}

	return sequence_number;
}

int smpp_onLinkStateChange(BYTE linkNo, BYTE linkstate)
{
	printf("[TEST PROGRAM]link %d state change to %d\n",linkNo, linkstate);
	if (linkstate == 1)
		stateFlag[linkNo]	= TRUE;
	else
		stateFlag[linkNo]	= FALSE;

	return linkNo;
}

//0-submit_sm, 1-deliver_sm, 2-data_sm 
void sendTestSM(int linkNo, int msg_type)
{
	SMPP_MSG	smpp_msg;
	WORD dataLen;

	memset(&smpp_msg, 0, sizeof(SMPP_MSG));
	switch (msg_type)
	{
	case 0:	//send submit sm
		smpp_msg.message_type	= 0x0B;
		smpp_msg.optional_param_flag1	= 0x0FF67FFF;
		smpp_msg.optional_param_flag2	= 0x0;
		
		//Mandatory parameters.
		strcpy(smpp_msg.pdu.submit_sm.service_type, "USSD");
		smpp_msg.pdu.submit_sm.source_addr_ton	= 0x2;	//National
		smpp_msg.pdu.submit_sm.source_addr_npi	= 0x1;	//ISDN
		strcpy(smpp_msg.pdu.submit_sm.source_addr, "172.18.166.1");
		smpp_msg.pdu.submit_sm.dest_addr_ton	= 0x2;	//National
		smpp_msg.pdu.submit_sm.dest_addr_npi	= 0x1;	//ISDN
		strcpy(smpp_msg.pdu.submit_sm.destination_addr,	"13923482085");
		smpp_msg.pdu.submit_sm.esm_class		= 0x00;
		smpp_msg.pdu.submit_sm.protocol_id		= 0x0;
		smpp_msg.pdu.submit_sm.priority_flag	= 0x3;
		strcpy(smpp_msg.pdu.submit_sm.schedule_delivery_time, "041231151200000R");
		strcpy(smpp_msg.pdu.submit_sm.validity_period, "041231151200000+");
		smpp_msg.pdu.submit_sm.registered_delivery	= 0x11;
		smpp_msg.pdu.submit_sm.replace_if_present_flag	= 0x01;
		smpp_msg.pdu.submit_sm.data_coding			= 0x07;
		smpp_msg.pdu.submit_sm.sm_default_msg_id	= 0x01;
		smpp_msg.pdu.submit_sm.sm_length			= 0x0;
		strcpy(smpp_msg.pdu.submit_sm.short_message, "");
		//Optional parameters.
		smpp_msg.pdu.submit_sm.user_message_reference	= 0x00;
		smpp_msg.pdu.submit_sm.source_port			= 0x6666;
		smpp_msg.pdu.submit_sm.source_addr_subunit	= 0x01;
		smpp_msg.pdu.submit_sm.destination_port		= 0x8888;
		smpp_msg.pdu.submit_sm.dest_addr_subunit	= 0x02;
		smpp_msg.pdu.submit_sm.sar_msg_ref_num		= 0x7700;
		smpp_msg.pdu.submit_sm.sar_total_segments	= 0x01;
		smpp_msg.pdu.submit_sm.sar_segment_seqnum	= 0x01;
		smpp_msg.pdu.submit_sm.more_messages_to_send	= 0x01;
		smpp_msg.pdu.submit_sm.payload_type			= 0x00;
		strcpy(smpp_msg.pdu.submit_sm.message_payload, "messae in submit sm optional");
		smpp_msg.pdu.submit_sm.privacy_indicator	= 0x03;
		
		//strcpy(smpp_msg.pdu.submit_sm.callback_num, "F0453245");
		smpp_msg.pdu.submit_sm.callback_num[0]	= 0x01;	//ASCII
		smpp_msg.pdu.submit_sm.callback_num[1]	= 0x02;	//National
		smpp_msg.pdu.submit_sm.callback_num[2]	= 0x03;	//Data
		strcpy(smpp_msg.pdu.submit_sm.callback_num+3, "F0453245");
		smpp_msg.pdu.submit_sm.callback_num_pres_ind	= 0x06;
		strcpy(smpp_msg.pdu.submit_sm.callback_num_atag, "callback_num_atag");
		//strcpy(smpp_msg.pdu.submit_sm.source_subaddress, "123");
		//smpp_msg.pdu.submit_sm.source_subaddress[0]	= 0x88;
		//strcpy(smpp_msg.pdu.submit_sm.source_subaddress+1,"123");
		
		//smpp_msg.pdu.submit_sm.dest_subaddress[0]	= 0x88;
		//strcpy(smpp_msg.pdu.submit_sm.dest_subaddress+1, "789");
		smpp_msg.pdu.submit_sm.user_response_code	= 0x03;
		smpp_msg.pdu.submit_sm.display_time			= 0x02;
		//smpp_msg.pdu.submit_sm.sms_signal			= 0x03;

		smpp_msg.pdu.submit_sm.ms_validity			= 0x03;
		smpp_msg.pdu.submit_sm.ms_msg_wait_facilities	= 0x03;
		smpp_msg.pdu.submit_sm.number_of_messages		= 0x03;
		smpp_msg.pdu.submit_sm.alert_on_message_delivery	= 0x03;
		
		smpp_msg.pdu.submit_sm.language_indicator		= 0x01;
		smpp_msg.pdu.submit_sm.its_reply_type			= 0x03;
		smpp_msg.pdu.submit_sm.its_session_info[0]		= 0x01;
		smpp_msg.pdu.submit_sm.its_session_info[1]		= 0x53;
		smpp_msg.pdu.submit_sm.ussd_service_op			= 0x03;
		
		break;
	case 1:	//send deliver sm
		smpp_msg.message_type	= 0x0F;
		smpp_msg.optional_param_flag1	= 0x000167FF;
		smpp_msg.optional_param_flag2	= 0x0;

		//Mandatory parameters.
		strcpy(smpp_msg.pdu.deliver_sm.service_type, "USSD");
		smpp_msg.pdu.deliver_sm.source_addr_ton	= 0x2;	//National
		smpp_msg.pdu.deliver_sm.source_addr_npi	= 0x1;	//ISDN
		strcpy(smpp_msg.pdu.deliver_sm.source_addr, "172.18.166.1");
		smpp_msg.pdu.deliver_sm.dest_addr_ton	= 0x2;	//National
		smpp_msg.pdu.deliver_sm.dest_addr_npi	= 0x1;	//ISDN
		strcpy(smpp_msg.pdu.deliver_sm.destination_addr,	"13923482085");
		smpp_msg.pdu.deliver_sm.esm_class		= 0x00;
		smpp_msg.pdu.deliver_sm.protocol_id		= 0x0;
		smpp_msg.pdu.deliver_sm.priority_flag	= 0x3;
		smpp_msg.pdu.deliver_sm.schedule_delivery_time	= 0x0;
		smpp_msg.pdu.deliver_sm.validity_period			= 0x0;
		smpp_msg.pdu.deliver_sm.registered_delivery	= 0x12;
		smpp_msg.pdu.deliver_sm.replace_if_present_flag	= 0x01;
		smpp_msg.pdu.deliver_sm.data_coding			= 0x07;
		smpp_msg.pdu.deliver_sm.sm_default_msg_id	= 0x01;
		smpp_msg.pdu.deliver_sm.sm_length			= 0x0;
		strcpy(smpp_msg.pdu.deliver_sm.short_message, "");

		//Optional parameters.
		smpp_msg.pdu.deliver_sm.user_message_reference	= 0x00;
		smpp_msg.pdu.deliver_sm.source_port				= 0x6666;
		smpp_msg.pdu.deliver_sm.destination_port		= 0x8888;
		smpp_msg.pdu.deliver_sm.sar_msg_ref_num			= 0x7700;

		smpp_msg.pdu.deliver_sm.sar_total_segments	= 0x01;
		smpp_msg.pdu.deliver_sm.sar_segment_seqnum	= 0x01;
		smpp_msg.pdu.deliver_sm.user_response_code	= 0x03;
		smpp_msg.pdu.deliver_sm.privacy_indicator	= 0x03;

		smpp_msg.pdu.deliver_sm.payload_type			= 0x00;
		strcpy(smpp_msg.pdu.deliver_sm.message_payload, "messae in deliver sm optional");
		smpp_msg.pdu.deliver_sm.callback_num[0]	= 0x01;	//ASCII
		smpp_msg.pdu.deliver_sm.callback_num[1]	= 0x02;	//National
		smpp_msg.pdu.deliver_sm.callback_num[2]	= 0x03;	//Data
		strcpy(smpp_msg.pdu.deliver_sm.callback_num+3, "F0453245");
		//smpp_msg.pdu.deliver_sm.source_subaddress[0]	= 0xA0;
		//smpp_msg.pdu.deliver_sm.source_subaddress[1]	= 0x01;
		//strcpy(smpp_msg.pdu.deliver_sm.source_subaddress+1, "123");

		//smpp_msg.pdu.deliver_sm.dest_subaddress[0]		= 0xA0;
		//smpp_msg.pdu.deliver_sm.dest_subaddress[1]		= 0x01;
		//strcpy(smpp_msg.pdu.deliver_sm.dest_subaddress+1, "321");
		smpp_msg.pdu.deliver_sm.language_indicator		= 0x01;
		smpp_msg.pdu.deliver_sm.its_session_info[0]		= 0x01;
		smpp_msg.pdu.deliver_sm.its_session_info[1]		= 0x53;
		//smpp_msg.pdu.deliver_sm.network_error_code[0]	= 0x03;
		//smpp_msg.pdu.deliver_sm.network_error_code[1]	= 0x03;
		//smpp_msg.pdu.deliver_sm.network_error_code[2]	= 0x00;

		smpp_msg.pdu.deliver_sm.message_state			= 0x01;
		//strcpy(smpp_msg.pdu.deliver_sm.receipted_message_id, "receipted message id in deliver sm");
		break;
	case 2:	//data_sm
		smpp_msg.message_type	= 0x11;
		smpp_msg.optional_param_flag1	= 0xB3EBFFFF;
		smpp_msg.optional_param_flag2	= 0x0000003F;
		//smpp_msg.optional_param_flag1	= 0x0C000000;
		//smpp_msg.optional_param_flag2	= 0x0;
		
		//Mandatory parameters.
		strcpy(smpp_msg.pdu.data_sm.service_type, "USSD");
		smpp_msg.pdu.data_sm.source_addr_ton	= 0x2;	//National
		smpp_msg.pdu.data_sm.source_addr_npi	= 0x1;	//ISDN
		strcpy(smpp_msg.pdu.data_sm.source_addr, "172.18.166.1");
		smpp_msg.pdu.data_sm.dest_addr_ton	= 0x2;	//National
		smpp_msg.pdu.data_sm.dest_addr_npi	= 0x1;	//ISDN
		strcpy(smpp_msg.pdu.data_sm.destination_addr,	"13923482085");
		smpp_msg.pdu.data_sm.esm_class		= 0x00;
		smpp_msg.pdu.data_sm.registered_delivery	= 0x11;
		smpp_msg.pdu.data_sm.data_coding			= 0x07;
		
		//Optional parameters.
		smpp_msg.pdu.data_sm.source_port	= 0x6666;
		smpp_msg.pdu.data_sm.source_addr_subunit	= 0x01;
		smpp_msg.pdu.data_sm.source_network_type	= 0x01;
		smpp_msg.pdu.data_sm.source_bearer_type		= 0x03;

		smpp_msg.pdu.data_sm.source_telematics_id	= 0x0003;
		smpp_msg.pdu.data_sm.destination_port		= 0x8888;
		smpp_msg.pdu.data_sm.dest_addr_subunit		= 0x03;
		smpp_msg.pdu.data_sm.dest_network_type		= 0x03;

		smpp_msg.pdu.data_sm.dest_bearer_type		= 0x03;
		smpp_msg.pdu.data_sm.dest_telematics_id		= 0x0003;
		smpp_msg.pdu.data_sm.sar_msg_ref_num		= 0x7700;
		smpp_msg.pdu.data_sm.sar_total_segments		= 0x01;

		smpp_msg.pdu.data_sm.sar_segment_seqnum		= 0x01;
		smpp_msg.pdu.data_sm.more_messages_to_send	= 0x01;
		smpp_msg.pdu.data_sm.qos_time_to_live		= 0x000000FF;
		smpp_msg.pdu.data_sm.payload_type			= 0x00;

		strcpy(smpp_msg.pdu.data_sm.message_payload, "smpp message in data sm optional");
		smpp_msg.pdu.data_sm.set_dpf				= 0x01;
		//strcpy(smpp_msg.pdu.data_sm.receipted_message_id, "receipted message id");
		smpp_msg.pdu.data_sm.message_state			= 0x01;

		//smpp_msg.pdu.data_sm.network_error_code[0]	= 0x03;
		//smpp_msg.pdu.data_sm.network_error_code[1]	= 0x03;
		//smpp_msg.pdu.data_sm.network_error_code[2]	= 0x00;
		smpp_msg.pdu.data_sm.user_message_reference	= 0x00;
		smpp_msg.pdu.data_sm.privacy_indicator		= 0x03;
		smpp_msg.pdu.data_sm.callback_num[0]		= 0x01;
		smpp_msg.pdu.data_sm.callback_num[1]		= 0x02;
		smpp_msg.pdu.data_sm.callback_num[2]		= 0x03;
		strcpy(smpp_msg.pdu.data_sm.callback_num+3, "call back num");

		smpp_msg.pdu.data_sm.callback_num_pres_ind	= 0x06;
		strcpy(smpp_msg.pdu.data_sm.callback_num_atag, "callback num atag in data sm");
		smpp_msg.pdu.data_sm.source_subaddress[0]	= 0xA0;
		smpp_msg.pdu.data_sm.source_subaddress[1]	= 0x01;
		//strcpy(smpp_msg.pdu.data_sm.source_subaddress+1, "123");
		smpp_msg.pdu.data_sm.dest_subaddress[0]		= 0xA0;
		smpp_msg.pdu.data_sm.dest_subaddress[1]		= 0x01;
		//strcpy(smpp_msg.pdu.data_sm.dest_subaddress+1, "321");

		smpp_msg.pdu.data_sm.user_response_code		= 0x03;
		smpp_msg.pdu.data_sm.display_time			= 0x02;
		//smpp_msg.pdu.data_sm.sms_signal				= 0x03;
		smpp_msg.pdu.data_sm.ms_validity			= 0x03;

		smpp_msg.pdu.data_sm.ms_msg_wait_facilities	= 0x03;
		smpp_msg.pdu.data_sm.number_of_messages		= 0x03;
		smpp_msg.pdu.data_sm.alert_on_message_delivery	= 0x03;
		smpp_msg.pdu.data_sm.language_indicator		= 0x01;

		smpp_msg.pdu.data_sm.its_reply_type			= 0x03;
		smpp_msg.pdu.data_sm.its_session_info[0]	= 0x01;
		smpp_msg.pdu.data_sm.its_session_info[1]	= 0x53;
		break;
	}

	smpp_send(linkNo, &smpp_msg, dataLen);
}
