/***********************************************************
Copyright (C), LGC Wireless.
File Name:		isdn_main.c
Description:	For isdn module test
Version:		v9.0.0
Author:			Gengxin Chen
Create Date:	2008-2-19
History:
	<author>	<date>		<version>		<desc>
	
************************************************************/
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>
#include <assert.h> 
#include <string.h>
#include "../src/include/isdn_rm.h"
#include "../src/include/isdn_ext.h"
#include "../src/include/isdn_inc.h"
#include "../../snmp/src/include/snmp.h"
#include "../../mtp3/src/include/mtp3.h"
#include "../../public/src/include/license_id.h"
#include "../../tcap/src/include/tcap_public.h"

#define SYS_IT_INTERVAL_SEC 0
#define SYS_IT_INTERVAL_USEC (20*1000)

static char serialNumber[18]="";
xua_up_msg_t iua_msg_src;
static int systemID=0,systemInited=0;
static int sys_timer10ms=0;
u8 ptemp[] = {0};

extern int test_len;

extern void test_isdn_msg_encode(u8 type);
extern void test_isdn_msg_decode(u8 type);
extern void m2ua_init(BYTE system_id);
extern void msc_isdn_init(int *sap_id);
extern void cc_timer();

static void isdn_param_init(void)
{
//	ISDN_Sap_Attrib_ sap;
	Cg_Attrib_struct cg;
	Circuit_Attrib_struct trunk;
	int sap_id,cg_id,trunk_id_0, trunk_id_1, trunk_id_2;
	
//	memset(&sap,0,sizeof(ISDN_Sap_Attrib_));
	memset(&cg,0,sizeof(Cg_Attrib_struct));
	memset(&trunk,0,sizeof(Circuit_Attrib_struct));
	
/*	if((sap_id = isdn_bind_sap(&sap)) == -1)
	{
		printf("add sap error!\r\n");
		return;
	}*/
	msc_isdn_init(&sap_id);


	cg.network_id = 0;
	cg.priority = 0;	//	0:Reattempt ind	 / 1:Discard
	cg.tg_id = 0;	

	if((cg_id = isdn_add_cg(sap_id, &cg)) == -1)
	{	
		printf("Failed to create isup trunk group socket\r\n");
		return;
	}

	trunk.cic_range = 32;
	trunk.head_cic = 0;
	trunk.plat_id = CTRL_PLAT_0;
	if((trunk_id_0 = isdn_add_circuit(cg_id, &trunk)) == -1)
	{
		printf("Failed to create isup trunk socket\r\n");
		return;
	}
	if((trunk_id_1 = isdn_add_circuit(cg_id, &trunk)) == -1)
	{
		printf("Failed to create isup trunk socket\r\n");
		return;
	}
	if((trunk_id_2 = isdn_add_circuit(cg_id, &trunk)) == -1)
	{
		printf("Failed to create isup trunk socket\r\n");
		return;
	}
	isdn_rs.cg_pond[cg_id].attrib.nfas_flag = 1;
	isdn_rs.cg_pond[cg_id].attrib.variant_type = VARIANT_ANSI;
	isdn_rs.cg_pond[cg_id].attrib.ds_ch[0] = trunk_id_0;
	isdn_rs.cg_pond[cg_id].attrib.ds_ch[1] = trunk_id_1;
	isdn_rs.circuit_pond[trunk_id_0].attrib.d_chnl = 16;
	isdn_rs.circuit_pond[trunk_id_0].attrib.head_cic = 0;
	isdn_rs.circuit_pond[trunk_id_0].attrib.cic_range =32;
	isdn_rs.circuit_pond[trunk_id_0].attrib.plat_id = 0;
	isdn_rs.circuit_pond[trunk_id_0].attrib.iua_id = 0;
	isdn_rs.circuit_pond[trunk_id_0].user_network_if = USER_SIDE;	//nt or lt
	isdn_rs.circuit_pond[trunk_id_1].attrib.d_chnl = 16;
	isdn_rs.circuit_pond[trunk_id_1].attrib.head_cic = 32;
	isdn_rs.circuit_pond[trunk_id_1].attrib.cic_range =32;
	isdn_rs.circuit_pond[trunk_id_1].attrib.plat_id = 0;
	isdn_rs.circuit_pond[trunk_id_1].attrib.iua_id = 1;
	isdn_rs.circuit_pond[trunk_id_1].user_network_if = USER_SIDE;	//nt or lt
	isdn_rs.circuit_pond[trunk_id_2].attrib.d_chnl = 16;
	isdn_rs.circuit_pond[trunk_id_2].attrib.head_cic = 64;
	isdn_rs.circuit_pond[trunk_id_2].attrib.cic_range =32;
	isdn_rs.circuit_pond[trunk_id_2].attrib.plat_id = 0;
	isdn_rs.circuit_pond[trunk_id_2].attrib.iua_id = 2;
	isdn_rs.circuit_pond[trunk_id_2].user_network_if = USER_SIDE;	//nt or lt
	
	printf("isdn init ok.\r\n");
}


int wxc2_get_license(int functionID)
{	
	return 1;
}

static void sysTimer_IRQ()
{
	sys_timer10ms = 1;	//test
}


static void SigactionSystem()
{
	struct sigaction action;//,actsegv;

	action.sa_handler = sysTimer_IRQ;
	sigemptyset(&action.sa_mask);
	action.sa_flags = SA_RESTART;
	if(sigaction(SIGALRM, &action, NULL)<0)
	{
		printf("sigactin sigalarm failed\n");
	}
}

static void sysDaemonInit()
{
	int		i;
	pid_t	pid;


	if ( (pid = fork()) < 0)		 /* parent terminates */
		printf("%s: %s: fork (%d)", 
				__FILE__, __FUNCTION__, __LINE__);
	else if (pid != 0)
		exit(0);		/* parent goes bye-bye */
	
	setsid();				// become session leader 

	for (i = 0; i<3; i++)
		close(i);
}

static void moduleInit()
{
	printf("\n\n¡¾R9 Platform Init¡¿\n\n");
	
	debug_init();
	heartbeat_init(0x0400);
	iptrMainInit();
	snmp_init(4957);
	m2ua_init(0);
	mtp_shm_init(0x0400);
	mtp3_init_serial(serialNumber);


	
	isdn_init(systemID,10);
								
	printf("\n\n¡¾R9 Platform Init completed¡¿\n\n");

}

static void SetFSMTimer()
{
	struct itimerval	*ptv, tv;

	ptv = (struct itimerval *) &tv;
	ptv->it_interval.tv_sec = SYS_IT_INTERVAL_SEC;
	ptv->it_interval.tv_usec = SYS_IT_INTERVAL_USEC;
	ptv->it_value. tv_sec = SYS_IT_INTERVAL_SEC;
	ptv->it_value.tv_usec = SYS_IT_INTERVAL_USEC;
	setitimer(ITIMER_REAL, ptv, NULL);
}

static void moduleProc()
{
	static int wait_sccp_normal_count = 0;

	iptrans_timer();
	snmp_timer();


	if(systemInited)
	{
		wait_sccp_normal_count=6001;

		heartbeat_timer();
		m2ua_timer();
		mtp3_proc();

		isdn_timer();
		cc_timer();
/*		test_isdn_msg_decode(MSG_SETUP);
		test_isdn_msg_encode(MSG_SETUP);
		test_isdn_msg_decode(MSG_SETUP_ACK);
		test_isdn_msg_encode(MSG_SETUP_ACK);
		test_isdn_msg_decode(MSG_CALL_PROC);
		test_isdn_msg_encode(MSG_CALL_PROC);
		test_isdn_msg_decode(MSG_ALERT);
		test_isdn_msg_encode(MSG_ALERT);
		test_isdn_msg_decode(MSG_CONN);
		test_isdn_msg_encode(MSG_CONN);
		test_isdn_msg_decode(MSG_CONNACK);
		test_isdn_msg_encode(MSG_CONNACK);
		test_isdn_msg_decode(MSG_DISC);
		test_isdn_msg_encode(MSG_DISC);
		test_isdn_msg_decode(MSG_REL);
		test_isdn_msg_encode(MSG_REL);
		test_isdn_msg_decode(MSG_REL_COMP);
		test_isdn_msg_encode(MSG_REL_COMP);*/
	}

	debug_rt();	
}

int CStrToHex(u8 dst[], u8 *src)
{
	int i, len, offset=0;
 
	len = strlen(src);
	while((len--) != 0)
	{
		if(*src >= '0' && *src <= '9')
			ptemp[offset++] = *src - 0x30;
		else if(*src >= 'A' && *src <= 'F')
			ptemp[offset++] = *src - 'A' + 10;        //*src - 0x37;
		else if(*src >= 'a' && *src <= 'f')
			ptemp[offset++] = *src - 'a' + 10;        //*src - 0x57;
		else
		{
			src++;
			continue;
		}
//		printf("src=%x,ptemp=%x\n",*src,*(ptemp+offset-1));
		src++;
	}

	test_len = offset/2;
	for(i = 0; i < test_len; i++)
	{
		dst[i] = ((ptemp[2*i]) << 4) | (ptemp[2*i+1]);
//		printf("set[%d]=0x%2x\n",i,dst[i]);
	}
//	printf("sizeof=%d,strlen=%d\n",sizeof(dst),strlen(dst));

	return 1;
}

static void isdn_set_val()
{
	iua_msg_src.msg_class = IUA_QPTM_MSG;
	iua_msg_src.msg.iua.link = 0x10;
	iua_msg_src.msg.iua.len = test_len;
}

int main(int argc, char **argv)
{
	int retval, err_flag=0, daemon_flag=0, no_sync=0;
	
	while ((retval = getopt(argc, argv, "dt")) != -1)
	{
		switch (retval) {
			case 'd':
				daemon_flag = 1;
				break;
			case 't':
				no_sync = 1;
				break;
			case '?':
				err_flag = 1;
				break;
		}
	}
	if (err_flag) {
		printf("Usage: %s [-d] \n", argv[0]);
		printf("	-d with daemon mode\n");
		exit(1);
	}

	if(daemon_flag)
		sysDaemonInit();
	
	moduleInit();
	
	SigactionSystem();
	SetFSMTimer();
    
	isdn_param_init();
//	isdn_set_val();

	sys_timer10ms = 1;
	
	while(1)
	{
		if(sys_timer10ms)
		{
			sys_timer10ms = 0;
			moduleProc();
			systemInited = 1;
		}  
		if (sys_timer10ms == 0)
			usleep(1);
	}

    return 1;
}

