#include "./include/snmp.h"
#include "./include/heartbeat.h"

static struct itimerval itimer,old_itimer;
int		intval;
char	strval[50]="hello!";
static void On_Timer();
static void SetTimer();
static int get_rsp(BYTE object_id_len, DWORD *object_id, BYTE *data_ptr,BYTE *vartype);
static int getnext_rsp(BYTE in_oidlen, DWORD in_oid[], BYTE *oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
static int set_rsp(BYTE object_id_len, DWORD *object_id, BYTE *data_ptr,WORD len);
static void pro_rsp(snmp_pdu *pdata,snmp_addr *addr);
//static int atoi_hex(char* input);

	static DWORD	oid[10] ={1,3,6,1,4,1,12189,2,2,50};
//static DWORD	oid[11] ={1,3,6,1,4,1,12189,2,1,50,6};	
	static int table_len = 9;
	static int row_len = 11;
	static int table_id = 50;

void showbuf(BYTE *buf,int len)
{
	int i;
	for(i=0;i<len;i++)
	{
		printf("%02X",buf[i]);
		if(i>0 && i%25==0)
			printf("\n");
		else
			printf(" ");
	}
	printf("\n");
}

static void showid(DWORD *buf,int len)
{
	int i;
	for(i=0;i<len;i++)
	{
		printf("%1ld.",buf[i]);
		if(i>0 && i%25==0)
			printf("\n");
	}
	printf("\n");
}
static void trap_callback(BYTE oidlen, DWORD *oid, BYTE *pdata,WORD len,snmp_addr *addr)
{
	//printf("Receive trap message,oid:");
	//showid(oid,oidlen);
	//showbuf(pdata,len);
}


int main()
{
	intval=123456;
	snmp_init(4961);
	heartbeat_init(0x1234);
	SetTimer();
	inquire_trapmsg(trap_callback);
	inquire_setmsg(1,oid,set_rsp);
	inquire_getmsg(1,oid,get_rsp);
	inquire_rspmsg(10,oid,pro_rsp);
	inquire_getnextmsg(10,oid,getnext_rsp);
	//cls;
	set_led(0,8);
	set_alarm(0,1);
	
{

	DWORD request_id;
	snmp_pdu request_msg;
	snmp_addr request_addr;
//	iptrans_init();
	iptrMainInit();
	
	request_msg.pdu_type = PDU_GETBULK;
	request_msg.max_repetitions = 199900;
	request_msg.non_repeaters = 0;
	request_msg.request_id = 10;
	request_msg.var_num = 1;
	sprintf(request_msg.community, "public");
	
	memcpy(request_msg.var[0].oid, oid, 4 * 10);
		
	request_msg.var[0].oidlen = 10;
	


	request_addr.remote_ip		=inet_addr("127.0.0.1");
	request_addr.local_ip			=inet_addr("172.48.234.128");
	request_addr.remote_port	=4961;
	request_addr.local_port		=4961;
	request_addr.broadcast		=1;
	

	request_id = snmp_send(&request_msg, &request_addr);
	printf("%ld\n",request_id);
	sleep(2);
	
}		
	while(1)
	{
		usleep(50);
	}

	return 1;

}


int getnext_rsp(BYTE in_oidlen, DWORD *in_oid, BYTE *oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype)
{
	memcpy(oid,in_oid,in_oidlen*sizeof(DWORD));
	//printf("receive command\n");
	if(in_oidlen<12)
	{
		oid[10]=1;
		oid[11]=1;
		oid[12]=0;
		in_oidlen=13;
	}
	else
	{
		oid[in_oidlen-1] =in_oid[in_oidlen-1]+1;
	}
	if(oid[in_oidlen-1]>12)
	{
		oid[in_oidlen-1]=0;
		oid[in_oidlen-2]=in_oid[in_oidlen-2]+1;
		if(oid[in_oidlen-2]>20)
			return -1;
	}
	pdata[0] = oid[in_oidlen-1];
	*vartype=2;
	*oidlen = in_oidlen;
	return 1;
}


void pro_rsp(snmp_pdu *pdata,snmp_addr *addr)
{
	int i,getflag,value=0;
	static int old_oid=0;
	DWORD request_id;
	snmp_pdu request_msg;
	snmp_addr request_addr;
	getflag = 1;
	for(i=0;i<pdata->var_num;i++)
	{
		if(pdata->var[i].oid[table_len]!=table_id||pdata->var[i].vartype==EndOfMibView)
		{
			printf("\n");
			getflag = 0;
			break;
		}
		if(pdata->var[i].oid[row_len]!=old_oid)
			printf("\n");

		old_oid = pdata->var[i].oid[row_len];
		value = decode_integer(pdata->var[i].msg,pdata->var[i].msglen);
		printf("%-5d",value);		
	}	
	
	if(getflag)
	{
		request_msg.pdu_type = PDU_GETBULK;
		request_msg.max_repetitions = 10;
		request_msg.non_repeaters = 0;
		request_msg.var_num = 1;
		sprintf(request_msg.community, "public");
		memcpy(request_msg.var[0].oid, pdata->var[pdata->var_num-1].oid, 4 * pdata->var[pdata->var_num-1].oidlen);
		request_msg.var[0].oidlen = pdata->var[pdata->var_num-1].oidlen;		

		request_addr.remote_ip		=addr->remote_ip; 
		request_addr.remote_port	=addr->remote_port; 
		request_addr.local_port		=addr->local_port; 		
		request_id = snmp_send(&request_msg, &request_addr);
	}
	return;
}

void On_Timer()
{

	snmp_timer();
	iptrans_timer();
	//heartbeat_timer();
}

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=1000;
	itimer.it_value.tv_sec=0;
	itimer.it_value.tv_usec=1000;

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



int get_rsp(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype)
{
	printf("I Receive get message,oid:");
	showid(oid,oidlen);
	/*
	if(object_id[1]==2)
	{
		memcpy(data_ptr,(BYTE *)&intval,4);
		return 4;
	}
	else
	{
		memcpy(data_ptr,(BYTE *)&strval,strlen(strval));
		return strlen(strval);
	}
	*/
	return -1;
}

int set_rsp(BYTE oidlen, DWORD *oid, BYTE *pdata,WORD len)
{
	printf("I Receive set message,oid:");
	showid(oid,oidlen);
	showbuf(pdata,len);
	/*
	if(object_id[1]==2)
	{
		memcpy((BYTE *)&intval,data_ptr,4);
		return 4;
	}
	else
	{
		memcpy((BYTE *)&strval,data_ptr,len);
		return len;
	}
	*/
	return -1;
}
