// ***************************************************************
//  isup_main   version:  1.0   ¡¤  date: 04/06/2007
//  -------------------------------------------------------------
//  DESCRIPTION:	For isup module test
//
//  AUTHOR:	Zane
//
//  HISTORY:
//  -------------------------------------------------------------
//  Copyright (C) 2007 - All Rights Reserved
// ***************************************************************
// 
// ***************************************************************

#include <stdio.h> 
#include <assert.h> 
#include "./include/isup_if.h"
#include "../../plat/snmp/src/include/snmp.h"
#include "../../plat/public/src/include/license_id.h"

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

static char	serialNumber[18]="";
static int systemID=0,systemInited=0;
static int sys_timer10ms=0;

extern void m2ua_init(void);
extern void m2ua_timer(void);
extern void debug_init_wxc2_ver();

static void isup_param_init(void)
{
	Sap_Attrib_struct sap;
	Cg_Attrib_struct cg;
	Circuit_Attrib_struct trunk;
	int sap_id,cg_id,trunk_id;
	
	memset(&sap,0,sizeof(Sap_Attrib_struct));
	memset(&cg,0,sizeof(Cg_Attrib_struct));
	memset(&trunk,0,sizeof(Circuit_Attrib_struct));
	
	if((sap_id = isup_bind_sap(&sap)) == -1)
	{
		printf("add sap error!\r\n");
		return;
	}

	cg.opc = 1;
	cg.dpc = 2;
	cg.network_id = 0;
	cg.priority = 1;	//	0:Reattempt ind	 / 1:Discard
	cg.tg_id = 0;
	cg.variant_type = VARIANT_ITU;	
	
	if((cg_id = isup_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 = isup_add_circuit(cg_id, &trunk)) == -1)
	{
		printf("Failed to create isup trunk socket\r\n");
		return;
	}

	isup_set_run_mode(ISUP_SINGLE_MODE,0);
	printf("isup init ok.\r\n");
}


int	wxc2_get_license(int functionID)
{	
	return 1;
}

static void sysTimer_IRQ()
{
	sys_timer10ms = 1;
}


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 moduleInit()
{
	printf("\n\n¡¾R9 Platform Init¡¿\n\n");
	
	debug_init();
	iptrMainInit();
	
	m2ua_init();
	
	snmp_init(4957);
	
	mtp_shm_init();

	mtp3_init_serial(serialNumber);
	
//	sccp_init(systemID, 0, 0);
						
	isup_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();

//		sccp_timer();

		isup_timer();
	}


	debug_rt(); 
}

int main(int argc, char **argv)
{
  moduleInit();
	
	debug_init_wxc2_ver("ISUP TEST CASE v1.0 - Init...");
	
	SigactionSystem();
    SetFSMTimer();		

	isup_param_init();

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

    return 1;
}
