/************************************/
/*Title:	alarmsnmp.c                                   */
/*Descr:	SNMP Interface                               */
/*          --MIB(WXC2-ALARM)                         */
/*Author:	Yi Zheng                                      */
/*Create:	2006-6-1                                         */
/************************************/
#include "alarmAgent.h"


#define DB_CONFIG_FILE					"./conf/omc_db.conf"
#define MAX_RECORD							100



#define ALARM_AGENT_OID				{1,3,6,1,4,1,1373,2,4,2} 
#define ALARM_AGENT_OIDLEN			10 
#define OMC_VERSION_OID				{1,3,6,1,4,1,1373,2,4,3} 
#define OMC_VERSION_OIDLEN			10 

/******************************************************/
/******            Heartbeat Parameter           ******/
/******************************************************/
DWORD PROC_HEARTBEAT_OID[12]={1,3,6,1,4,1,1373,2,4,10,9};
int PROC_HEARTBEAT_OIDLEN=11;
int sendHeartbeat();

/*----------------------------------------------------------*/
/*
static char* objlist[] = {"MTP2","8KE1","LAN","IPTR","MTP3","SCCP","TCAP","MAP","XAPP",
							 					"OMC" ,"MSC","HLR","AUC","SMSC","PPS","MNP","VMS","AAS","PCR","CNF"};
							 					
static WORD  objID[] = {113,111,200,200,200,200,200,200,200,
							 					0 ,320,330,340,350,360,370,390,311,130,160};

static DWORD	_8KE1_TRAN_OID[]	=	{1,3,6,1,4,1,1373,1,3,1,1,1};
#define 			_8KE1_TRAN_OIDLEN	12 
static DWORD	_LAN_TRAN_OID[]	=	{1,3,6,1,4,1,1373,1,3,1,1,2};
#define 			_LAN_TRAN_OIDLEN	12 
static DWORD	_MTP2_TRAN_OID[]=	{1,3,6,1,4,1,1373,1,3,1,1,3};
#define 			_MTP2_TRAN_OIDLEN	12 
static DWORD	_PCR_TRAN_OID[]	=	{1,3,6,1,4,1,1373,1,3,1,3};
#define 			_PCR_TRAN_OIDLEN	11 
static DWORD	_CNF_TRAN_OID[]	=	{1,3,6,1,4,1,1373,1,3,1,6};
#define 			_CNF_TRAN_OIDLEN	11 
static DWORD	_PLAT_TRAN_OID[] = 	{1,3,6,1,4,1,1373,1,3,2}; 
#define 			_PLAT_TRAN_OIDLEN	10
static DWORD	_MSC_TRAN_OID[]	=	{1,3,6,1,4,1,1373,1,3,3,2}; 
#define 			_MSC_TRAN_OIDLEN	11 
static DWORD	_HLR_TRAN_OID[]	=	{1,3,6,1,4,1,1373,1,3,3,3}; 
#define 			_HLR_TRAN_OIDLEN	11 
static DWORD	_AUC_TRAN_OID[] 	= 	{1,3,6,1,4,1,1373,1,3,3,4};
#define 			_AUC_TRAN_OIDLEN	11 
static DWORD	_SMSC_TRAN_OID[] = {1,3,6,1,4,1,1373,1,3,3,5};
#define 			_SMSC_TRAN_OIDLEN	11 
static DWORD	_PPS_TRAN_OID[] 	=  {1,3,6,1,4,1,1373,1,3,3,6};
#define 			_PPS_TRAN_OIDLEN	11 
static DWORD	_MNP_TRAN_OID[] 	=  {1,3,6,1,4,1,1373,1,3,3,32};
#define 			_MNP_TRAN_OIDLEN	11 
static DWORD	_EIR_TRAN_OID[] 	=  {1,3,6,1,4,1,1373,1,3,3,8};
#define 			_EIR_TRAN_OIDLEN	11 
static DWORD	_VMS_TRAN_OID[] 	=  {1,3,6,1,4,1,1373,1,3,3,9};
#define 			_VMS_TRAN_OIDLEN	11 
static DWORD	_IWF_TRAN_OID[] 	=  {1,3,6,1,4,1,1373,1,2,8};
#define 			_IWF_TRAN_OIDLEN	10 
static DWORD	_PPPRT_TRAN_OID[]= {1,3,6,1,4,1,1373,1,2,10};
#define 			_PPPRT_TRAN_OIDLEN  10
static DWORD	_AAS_TRAN_OID[]	=	{1,3,6,1,4,1,1373,1,3,3,10};
#define 			_AAS_TRAN_OIDLEN	11 
static DWORD	_OMC_TRAP_OID[]	=	{1,3,6,1,4,1,1373,1,4.0} ;
#define 			_OMC_TRAP_OIDLEN	10 
*/
/*----------------------------------------------------------*/


/*----------------------*/
static BYTE 		testflag = 0;
static WORD 	selDeviceID = 0;
static BYTE 		selAlarmLevel = 0;
static WORD 	selEvencode = 0;

//static char		szWhere[128];
/*----------------------*/

static BYTE loadflag = 1;
//static BYTE alarm_interval = 50;
static MYSQL *omcPub_conn;
static int alarm_get_record;
static char pre_time[32];
static char modify_time[32];
char omcVersion[16];

alarm_set AlarmSet[MAX_RECORD];

int getOmcConf();

int alarm_getnext_rsp(BYTE pre_oidlen, DWORD pre_oid[], BYTE *oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
int alarm_get_rsp(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
int omcVersion_get_rsp(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype);
//int alarm_set_rsp(BYTE oidlen, DWORD *oid, BYTE *pdata, WORD datalen);

int alarm_SnmpInit(void);
void alarm_Agent_timer(void);
/*-----------------------------*/
static int alarm_db_init(void);
static int getLastChangedTime(BYTE *pdata, BYTE *vartype);
static int getRemoveDelayTime(BYTE *pdata, BYTE *vartype);
static int getAlarmActiveRecord(BYTE *pdata, BYTE *vartype,int col,int row);
static int loadAlarmTable();
static int getFileModifyTime(char *File,char *szTime);
void logMessage(const char *fmt,...);
//static int testtimer();
//static void sendHeartbeat();
/*-----------------------------*/



int alarm_SnmpInit(void)
{
	DWORD	alarm_oid_prefix[20] = ALARM_AGENT_OID;
	DWORD	omc_version_oid[20] = OMC_VERSION_OID;
	
	logMessage("alarmAgent is starting...");
	
	snmp_init(ALARM_SNMP_PORT);	
	getOmcConf();
	
	inquire_getnextmsg(ALARM_AGENT_OIDLEN, alarm_oid_prefix, alarm_getnext_rsp);
	inquire_getmsg(ALARM_AGENT_OIDLEN, alarm_oid_prefix, alarm_get_rsp);
	inquire_getmsg(OMC_VERSION_OIDLEN, omc_version_oid, omcVersion_get_rsp);
	
#ifdef	_TEST_	
//	inquire_setmsg(ALARM_AGENT_OIDLEN, alarm_oid_prefix, alarm_set_rsp);
#endif	
	
	printf("Alarm agent on...\n");
	alarm_db_init();	
	testflag			=0;
	selDeviceID		=0xFFFF;
	selAlarmLevel	=0xFF;	
	selEvencode	=0xFFFF;
	return 1;
}


int alarm_db_init(void)
{
	omcPub_conn=mysql_conn("localhost","OMC_PUB");
	if(omcPub_conn == NULL)
	{
		printf("alarm_db_init:mysql_conn OMC_PUB error");
		logMessage("[ERR][alarm_db_init]:mysql connect OMC_PUB");
		return 0;
	}
	
	printf("Connect Datebase ok...\n");
	return 1;
}

/*************************************************
Function:        // sendHeartbeat
Description:     // Send process heartbeat to beatProc
Calls:           // snmp_send
Called By:       // on_Timer
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 1:OK; 0:Fail
Others:          // 
*************************************************/
int sendHeartbeat()
{
	snmp_pdu pdu;
	snmp_addr addr;
	char proc_name[16];
	
	memset(proc_name,0,16);
	sprintf(proc_name,"alarmAgent");
	
	//pdu
	memset(&pdu,0,sizeof(snmp_pdu));
	pdu.pdu_type=7;
	pdu.request_id=0;
	sprintf(pdu.community,"public");
	pdu.var_num=1;
	pdu.error_status=0;
	pdu.error_index=0;
	
	//Addr
	addr.local_port = 4957;
	//addr.remote_ip = inet_addr("127.0.0.1");
	addr.remote_ip = GetLocalIP();
	addr.broadcast = 0;
	addr.remote_port = 4957;
	
	//Varlist
	memcpy(pdu.var[0].oid,PROC_HEARTBEAT_OID,PROC_HEARTBEAT_OIDLEN*sizeof(DWORD));

	pdu.var[0].oidlen=PROC_HEARTBEAT_OIDLEN;
	pdu.var[0].vartype=0x04;
	pdu.var[0].msglen=16;
	memcpy(pdu.var[0].msg,proc_name,16);
	
	if(snmp_send(&pdu, &addr) <= 0)
	{
		logMessage("Send heartbeat fail");
		return 0;
	}
	return 1;
}
unsigned long heartbeat_timeout=0;
void alarm_Agent_timer(void)
{	
	static int m_state = 200;
	static BYTE	retry=5;
	
	
	if(time(NULL) - heartbeat_timeout > 5)
	{
		heartbeat_timeout=time(NULL);
		sendHeartbeat();
	}
	
	if(m_state++ > 200)
	{
		loadAlarmTable();
		m_state = 1;
		getFileModifyTime(ALARM_TABLE_FILE,modify_time);
		if(strcmp(modify_time,pre_time)&&loadflag)
		{
			retry=5;
			loadAlarmTable();
			sprintf(pre_time,"%s",modify_time);
		}
		//ooooo	
		else
		{		
			if(retry)
			{	
				retry--;
				loadAlarmTable();
			}
		}
		//ooooo	
		
	}
//	if(m_state%alarm_interval==0)
//	{
//		if(testflag)
//			testtimer();
//	}
}

int alarm_getnext_rsp(BYTE pre_oidlen, DWORD *pre_oid, BYTE *oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype)
{

	DWORD		*alarmOid = pre_oid + ALARM_AGENT_OIDLEN;
	DWORD		*rspOid 	  = oid + ALARM_AGENT_OIDLEN;
	int		ret;
			
	memcpy(oid,pre_oid,pre_oidlen*sizeof(DWORD));
	*oidlen  = pre_oidlen;
	loadflag = 1;
	switch(alarmOid[0])
	{
		case 0:																											//alarmlist
			rspOid[0] = 1;																							//->wxc2alarmActiveLastChanged
			*oidlen  = ALARM_AGENT_OIDLEN +1;
			return getLastChangedTime(pdata,vartype);
		case 1:																											//wxc2alarmActiveLastChanged
			rspOid[0] = 2;
			rspOid[1] = 1;
			rspOid[2] = 1;																							//->alarmSequenceNumber
			rspOid[3] = 1;
			*oidlen  = ALARM_AGENT_OIDLEN +4;
			ret =  getAlarmActiveRecord(pdata,vartype,rspOid[2],rspOid[3]);
			if(ret ==-1)
			{
				rspOid[0]	 =3;
				rspOid[1]	 =0;
				rspOid[2]	 =0;
				*oidlen  = ALARM_AGENT_OIDLEN +1;
				return getRemoveDelayTime(pdata,vartype);				
			}	
			return ret;
		case 2:																											//wxc2alarmActiveTable
			switch(alarmOid[1])
			{
				case 0:
					rspOid[1] = 1;
					rspOid[2] = 1;																					//->alarmSequenceNumber					
					rspOid[3] = 1;
					*oidlen  = ALARM_AGENT_OIDLEN +4;
					ret =  getAlarmActiveRecord(pdata,vartype,rspOid[2],rspOid[3]);
					if(ret ==-1)
					{
						rspOid[0]	 =3;
						rspOid[1]	 =0;
						rspOid[2]	 =0;
						*oidlen  = ALARM_AGENT_OIDLEN +1;
						return getRemoveDelayTime(pdata,vartype);				
					}	
					return ret;
				case 1:
					if(alarm_get_record==0)
					{
						rspOid[0]	 =3;
						rspOid[1]	 =0;
						rspOid[2]	 =0;
						*oidlen  = ALARM_AGENT_OIDLEN +1;
						return getRemoveDelayTime(pdata,vartype);						
					}
					if(alarmOid[2]	==0)
					{
						rspOid[1] = 1;
						rspOid[2] = 1;																				//->alarmSequenceNumber					
						rspOid[3] = 1;
						*oidlen  = ALARM_AGENT_OIDLEN +4;
						ret =  getAlarmActiveRecord(pdata,vartype,rspOid[2],rspOid[3]);
						if(ret ==-1)
						{
							rspOid[0]	 =3;
							rspOid[1]	 =0;
							rspOid[2]	 =0;
							*oidlen  = ALARM_AGENT_OIDLEN +1;
							return getRemoveDelayTime(pdata,vartype);				
						}	
						return ret;
					}
					else	if(alarmOid[2]<12)
					{					
						rspOid[3] = alarmOid[3]+1;
						*oidlen  = ALARM_AGENT_OIDLEN +4;
						if(rspOid[3]<=alarm_get_record)
						{	
							ret =  getAlarmActiveRecord(pdata,vartype,rspOid[2],rspOid[3]);
							if(ret ==-1)
							{
								rspOid[0]	 =3;
								rspOid[1]	 =0;
								rspOid[2]	 =0;
								*oidlen  = ALARM_AGENT_OIDLEN +1;
								return getRemoveDelayTime(pdata,vartype);				
							}	
							return ret;
						}
						else
						{
							rspOid[2] = alarmOid[2]+1;
							if(rspOid[2]<12)
							{
								rspOid[3] = 1;
								ret =  getAlarmActiveRecord(pdata,vartype,rspOid[2],rspOid[3]);
								if(ret ==-1)
								{
									rspOid[0]	 =3;
									rspOid[1]	 =0;
									rspOid[2]	 =0;
									*oidlen  = ALARM_AGENT_OIDLEN +1;
									return getRemoveDelayTime(pdata,vartype);				
								}	
								return ret;
							}
							else
							{
								rspOid[0]	 =3;
								rspOid[1]	 =0;
								rspOid[2]	 =0;
								*oidlen  = ALARM_AGENT_OIDLEN +1;
								return getRemoveDelayTime(pdata,vartype);
							}									
						}						
					}
					else
					{
						rspOid[0]	 =3;
						rspOid[1]	 =0;
						rspOid[2]	 =0;
						*oidlen  = ALARM_AGENT_OIDLEN +1;
						return getRemoveDelayTime(pdata,vartype);
					}
				default:
						rspOid[0]	 =3;
						rspOid[1]	 =0;
						rspOid[2]	 =0;
						*oidlen  = ALARM_AGENT_OIDLEN +1;
						return getRemoveDelayTime(pdata,vartype);
			}
			break;
		default:				
			loadflag = 1;
			return -1;			
	}
}

int alarm_get_rsp(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype)
{
	DWORD		*alarmOid = oid + ALARM_AGENT_OIDLEN;

	//printf("I Receive get message,oid:\n");
	loadflag = 1;
	switch(alarmOid[0])
	{
		case 1:																											//wxc2alarmActiveLastChanged
		 	return getLastChangedTime(pdata,vartype);
		case 2:																											//wxc2alarmActiveTable
			switch(alarmOid[1])
			{
				case 1:
					if(alarmOid[2]<13)
					{					
						return getAlarmActiveRecord(pdata,vartype,alarmOid[2],alarmOid[3]);
					}
					else
						return -1;	
				default:
					return -1;											
			}
			break;
		case 3:																											//wxc2alarmClearedRemoveDelay
			return getRemoveDelayTime(pdata,vartype);
		default:
			return -1;			
	}
	return 1;
}


int omcVersion_get_rsp(BYTE oidlen, DWORD *oid, BYTE *pdata,BYTE *vartype)
{
	*vartype=4;																									//octet string	
	sprintf((char*)pdata,"%s",omcVersion);
	return strlen((char*)pdata);
}
static int getLastChangedTime(BYTE *pdata, BYTE *vartype)
{
	loadflag = 1;
	
	*vartype=4;																								//octet string	
	sprintf((char*)pdata,"%s",modify_time);
	return strlen((char*)pdata);
}

static int getFileModifyTime(char *File,char *szTime)
{
	struct stat fileinfo;
	if (stat(File, &fileinfo) == 0) 
	{
		sprintf(szTime,"%s",ctime(&fileinfo.st_mtime));
		return strlen(szTime);
	}
	return -1;
}

static int getRemoveDelayTime(BYTE *pdata, BYTE *vartype)
{
	char s[80];
	char *p1;
	char sflag[]="OMC_PUB,dbAlarm,alarmTime,";
	FILE *fp;
	
	loadflag = 1;
	
	*vartype=2;  																									//integer
	fp = fopen(DB_CONFIG_FILE, "r");
	if (NULL == fp)
	{
		return -1;
	}	
	while(1)
	{
		if ((fgets(s, 1024, fp)) == NULL)
		{
			fclose(fp);	
			return -1;
		}
		p1=strstr(s,sflag);
		if (p1!=NULL)
		{	
			p1 = p1+sizeof(sflag)-1;
			if(p1==NULL)
				pdata[0] = 0;
			else		
				pdata[0] = atoi(p1);
			fclose(fp);
			return 1;	
		}
	}
}


static int loadAlarmTable()
{
	char 				sqlstr[2048];
	DWORD				res_num=0;
	alarm_set	 	*pAlarm;
	MYSQL_RES 		*alarm_res;
	MYSQL_ROW		 alarm_row;
	int					lel;
	
	sprintf(sqlstr,
			"SELECT level_1,a.sysNo,level_2,instanceNo,alarmTime,ip,EventCode,alarmLevel,alarmDesc_2,a.subSysNo,a.sysTypeNo,a.compCode,a.alarmCode\
					 FROM (sysAlarmLog	 a  ,sysAlarmConf b)\
					 LEFT OUTER JOIN sysInfo c\
					 ON a.sysTypeNo=c.sysTypeNo and a.sysNo=c.sysNo and a.subSysNo=c.subSysNo\
					 Where a.clearTime = '0000-00-00 00:00:00' and a.sysTypeNo=b.sysTypeNo and a.compCode=b.compCode and a.alarmCode=b.alarmCode \
					 order by alarmTime desc LIMIT %d",MAX_RECORD);

	alarm_res=mysql_getres(omcPub_conn,sqlstr);
	if(alarm_res == NULL)
	{
		logMessage("[ERR %d][loadAlarmTable]:%s",mysql_errno(omcPub_conn),sqlstr);
		return -1;
	}
	
	alarm_get_record = mysql_num_rows(alarm_res);
	while ((alarm_row = mysql_fetch_row(alarm_res)) != NULL) 
	{
		pAlarm = &AlarmSet[res_num];
				
		pAlarm->sq_Num = res_num;
		if(alarm_row[0] == NULL)
			sprintf(pAlarm->mag_ObjName," ");
		else
			sprintf(pAlarm->mag_ObjName,"%s",alarm_row[0]);
		
		if(atoi(alarm_row[10])==200)			//for platform   subSysNo=objid
		{
			if(alarm_row[9]==NULL)
				pAlarm->mag_ObjIns = 0;
			else	
				pAlarm->mag_ObjIns = atoi(alarm_row[9]);
		}
		else												//for others 	   sysNo = objid
		{	
			if(alarm_row[1]==NULL)
				pAlarm->mag_ObjIns = 0;
			else				
				pAlarm->mag_ObjIns = atoi(alarm_row[1]);		
		}
		if(alarm_row[2] == NULL)
			sprintf(pAlarm->mag_ComName," ");
		else
			sprintf(pAlarm->mag_ComName,"%s",alarm_row[2]);
		
		if(alarm_row[3]==NULL)
			pAlarm->mag_ComIns = 0;
		else			
			pAlarm->mag_ComIns = atoi(alarm_row[3]);
			
		if(alarm_row[4] == NULL)
			sprintf(pAlarm->date_Time,"0000-00-00 00:00:00");
		else
			sprintf(pAlarm->date_Time,"%s",alarm_row[4]);
			
		if(alarm_row[5] == NULL)
			sprintf(pAlarm->ip_Addr," ");
		else
			sprintf(pAlarm->ip_Addr,"%s",alarm_row[5]);
		
		if(alarm_row[6]==NULL)
			pAlarm->event_Code = 9999;//unknown
		else					
			pAlarm->event_Code = atoi(alarm_row[6]);		
			
		if(alarm_row[7]==NULL)
			lel = 5;
		else								
			lel = atoi(alarm_row[7]);
		
		pAlarm->severity_ID = lel;	
		if(alarm_row[8] == NULL)
			sprintf(pAlarm->active_Desc," ");
		else
			sprintf(pAlarm->active_Desc,"%s",alarm_row[8]);
		pAlarm->notifiy_ID = 0;
		if(alarm_row[10]!=NULL)
			pAlarm->sysTypeNo	=	atoi(alarm_row[10]);		//sysTypeNo
		if(alarm_row[11]!=NULL)	
			pAlarm->compCode	=	atoi(alarm_row[11]);		//compCode
		if(alarm_row[12]!=NULL)	
			pAlarm->alarmCode	=	atoi(alarm_row[12]);		//alarmCode
		
		res_num++;	
	}
	
	mysql_free_result(alarm_res);
//	printf("Loading %d record in  AlarmTable...\n",alarm_get_record);	
	return 0;
}


static int getAlarmActiveRecord(BYTE *pdata, BYTE *vartype,int col,int row)
{
	
	DWORD		*pDW;
	WORD		*pW;
	alarm_set	 *pAlarm;
	
	
	if(col>12 || row >alarm_get_record ||col < 1 || row< 1)
		return -1;
	pAlarm = 	&AlarmSet[row-1];
	switch	(col)
	{
		case 1:																							//			alarmSequenceNumber			Counter32,		
		{
			pDW = (DWORD *)pdata;
			pDW[0] = htonl(pAlarm->sq_Num);
			*vartype=2;
			return 4;
		}			
		case 2:																							//			alarmManagedObjName			OCTET STRING,	
																											//           	"MTP2,8KE1,LAN,IPTR,MTP3,SCCP,TCAP,MAP,XAPP,OMC ,MSC, HLR, AUC,SMSC,PPS, MNP, VMS,AAS, PCR, CNF,TEST,UNKOWN "
		{
			sprintf((char*)pdata,"%s ",pAlarm->mag_ObjName);
			*vartype=4;
			return strlen((char*)pdata);
		}			
				
		case 3:																							//			alarmManagedObjInstance		INTEGER,
		{
			pDW = (DWORD *)pdata;
			pDW[0] = htonl(pAlarm->mag_ObjIns);
			*vartype=2;
			return 4;
		}			
			
		case 4:																							//			alarmComponentName			OCTET STRING,
		{
			sprintf((char*)pdata,"%s ",pAlarm->mag_ComName);
			*vartype=4;
			return strlen((char*)pdata);			
		}			
				
		case 5:																							//			alarmComponentInstance		INTEGER,		
		{
			pW = (WORD *)pdata;
			pW[0] = htons(pAlarm->mag_ComIns);
			*vartype=2;
			return 2;			
		}			
			
		case 6:																							//			alarmDateAndTime			TimeTicks,
		{
			sprintf((char*)pdata,"%s ",pAlarm->date_Time);
			*vartype=4;
			return strlen((char*)pdata);						
		}			
				
		case 7:																							//			alarmEventEngineAddress		IpAddress,
		{
			sprintf((char*)pdata,"%s ",pAlarm->ip_Addr);
			*vartype=4;
			return strlen((char*)pdata);	
		}			
			
		case 8:																							//			alarmEventCode				INTEGER,
		{
			pW = (WORD *)pdata;
			pW[0] = htons(pAlarm->event_Code);
			*vartype=2;
			return 2;			
		}			
				
		case 9:																							//			alarmSeverity				INTEGER,
		{
			pW = (WORD *)pdata;
			pW[0] = htons(pAlarm->severity_ID);
			*vartype=2;
			return 2;			
		}			
			
		case 10:																						//			alarmActiveDescription		OCTET STRING,
		{
			sprintf((char*)pdata,"%s ",pAlarm->active_Desc);
			*vartype=4;
			return strlen((char*)pdata);
		}			
				
		case 11:																						//			alarmNotificationID			OBJECT IDENTIFIER			
		{
			pW = (WORD *)pdata;
			pW[0] = htons(pAlarm->notifiy_ID);
			*vartype=2;
			return 2;			
		}									

		case 12:																						//			sysTypeNo+compCode+alarmCode
		{
			pW = (WORD *)pdata;
			pW[0] = htons(pAlarm->sysTypeNo);
			pW = (WORD *)(pdata+2);
			pW[0] = htons(pAlarm->compCode);
			pdata[4]	=	pAlarm->alarmCode;
			*vartype=2;
			return 5;			
		}									
		
					
		default:
		{
			*vartype=2;
			return -1;			
		}				
	}
}


/*
int alarm_set_rsp(BYTE oidlen, DWORD *oid, BYTE *pdata, WORD datalen)
{	
	DWORD		*alarmOid = oid + ALARM_AGENT_OIDLEN;
	WORD		*pW;
	int 			p=0;
		
	if(alarmOid[0]==2&&alarmOid[1]==1&&alarmOid[2]==0)
	{
		if(pdata[0]==0xF1)
			testflag = 1;				//generate
		else if(pdata[0]==0x1F)
			testflag = 2;				//clear
		else
			testflag = 3;				//stop	
			
		selDeviceID		= pdata[1];
		selAlarmLevel = pdata[2];
		pW					= (WORD *)(pdata+3);
		selEvencode	= htons(pW[0]);	

		if(selDeviceID<20)
			p+=sprintf(szWhere+p,"AND level_1='%s' ",objlist[selDeviceID]);
//		if(selAlarmLevel<5)	
//			p+=sprintf(szWhere+p,"AND alarmLevel=%d ",selAlarmLevel);
//		if(selEvencode<9999)	
//			p+=sprintf(szWhere+p,"AND EventCode=%d ",selEvencode);			
//		printf("Command=%d %s\n",testflag,szWhere);
	}
	
	return datalen;
}


static void sendHeartbeat(int sysTypeNo,int compCode,int alarmCode)
{
	DWORD		*oid_prefix;
	int 			len;
	static 	int count=0;
	DWORD request_id;
	BYTE			*buf;
	time_t		cur_time;
	
	snmp_pdu pdu;
	snmp_addr request_addr;
			
	pdu.pdu_type = PDU_TRAP;
	pdu.error_status = 0;
	pdu.error_index = 0;
	pdu.request_id = 0;
	pdu.var_num = 1;
	sprintf(pdu.community, "public");
	
	buf = pdu.var[0].msg;
	buf[0] = 0x00;	// system id
	switch (sysTypeNo)
	{
		case 111:				//_8KE1_TRAN_OID[]	=	{1,3,6,1,4,1,1373,1,3,1,1,1};
			oid_prefix 	= _8KE1_TRAN_OID;
			len				= _8KE1_TRAN_OIDLEN;			
			break;			
		case 112:				//_LAN_TRAN_OID[]	=	{1,3,6,1,4,1,1373,1,3,1,1,2};
			oid_prefix 	= _LAN_TRAN_OID;
			len				= _LAN_TRAN_OIDLEN;			
			break;			
		case 113:				//_MTP2_TRAN_OID[]	=	{1,3,6,1,4,1,1373,1,3,1,1,3};
			oid_prefix 	= _MTP2_TRAN_OID;
			len				= _MTP2_TRAN_OIDLEN;			
			break;			
		case 130:				//_PCR_TRAN_OID[]	=	{1,3,6,1,4,1,1373,1,3,1,3};
			oid_prefix 	= _PCR_TRAN_OID;
			len				= _PCR_TRAN_OIDLEN;			
			break;
		case 160:				//_CNF_TRAN_OID[]	=	{1,3,6,1,4,1,1373,1,3,1,6};
			oid_prefix 	= _CNF_TRAN_OID;
			len				= _CNF_TRAN_OIDLEN;			
			break;			
		case 200:			//_PLAT_TRAN_OID[] = 	{1,3,6,1,4,1,1373,1,3,2}; 
			buf[0] = 0x09;				// system id
			oid_prefix 	= _PLAT_TRAN_OID;
			len				= _PLAT_TRAN_OIDLEN;			
			break;			
		case 320:			//_MSC_TRAN_OID[]	=	{1,3,6,1,4,1,1373,1,3,3,2}; 
			oid_prefix 	= _MSC_TRAN_OID;
			len				= _MSC_TRAN_OIDLEN;			
			break;						
		case 330:			//_HLR_TRAN_OID[]	=	{1,3,6,1,4,1,1373,1,3,3,3}; 
			oid_prefix 	= _HLR_TRAN_OID;
			len				= _HLR_TRAN_OIDLEN;			
			break;
		case 340:			//_AUC_TRAN_OID[] 	= 	{1,3,6,1,4,1,1373,1,3,3,4};
			oid_prefix 	= _AUC_TRAN_OID;
			len				= _AUC_TRAN_OIDLEN;			
			break;
		case 350:			//_SMSC_TRAN_OID[] = {1,3,6,1,4,1,1373,1,3,3,5};
			oid_prefix 	= _SMSC_TRAN_OID;
			len				= _SMSC_TRAN_OIDLEN;			
			break;
		case 360:			//_PPS_TRAN_OID[] 	=  {1,3,6,1,4,1,1373,1,3,3,6};
			oid_prefix 	= _PPS_TRAN_OID;
			len				= _PPS_TRAN_OIDLEN;			
			break;
		case 370:			//_MNP_TRAN_OID[] = {1,3,6,1,4,1,1373,1,3,3,32};
			oid_prefix 	= _MNP_TRAN_OID;
			len				= _MNP_TRAN_OIDLEN;			
			break;						
		case 380:			//_EIR_TRAN_OID[] = {1,3,6,1,4,1,1373,1,3,3,8};
			oid_prefix 	= _EIR_TRAN_OID;
			len				= _EIR_TRAN_OIDLEN;			
			break;						
		case 390:			//_VMS_TRAN_OID[] = {1,3,6,1,4,1,1373,1,3,3,9};
			oid_prefix 	= _VMS_TRAN_OID;
			len				= _VMS_TRAN_OIDLEN;			
			break;						
		case 280:			//_IWF_TRAN_OID[] = {1,3,6,1,4,1,1373,1,2,8};
			oid_prefix 	= _IWF_TRAN_OID;
			len				= _IWF_TRAN_OIDLEN;			
			break;
		case 210:			//_PPPRT_TRAN_OID[]= {1,3,6,1,4,1,1373,1,2,10};
			oid_prefix 	= _PPPRT_TRAN_OID;
			len				= _PPPRT_TRAN_OIDLEN;			
			break;																			
		case 311:			//AAS		--- 1.3.6.1.4.1.1373.1.3.3.10 
			oid_prefix 	= _AAS_TRAN_OID;
			len				= _AAS_TRAN_OIDLEN;			
			break;						
		case 0:				//OMC			---   1.3.6.1.4.1.1373.1.4 
			buf[0] = 0x01;	// system id
			oid_prefix 	= _OMC_TRAP_OID;
			len				= _OMC_TRAP_OIDLEN;
			break;
		default:
			return;
	}
	memcpy(pdu.var[0].oid, oid_prefix, len*4);

	pdu.var[0].oidlen = len;
	pdu.var[0].vartype = 4;
	
	buf[1] = 0;
	cur_time = time(NULL);
	buf[2] = cur_time >> 24;	// time stamp
	buf[3] = cur_time >> 16;
	buf[4] = cur_time >> 8;
	buf[5] = cur_time;
	buf[6] = 0x99;
	buf[7] = 90;
	buf[8] = 90;
	buf[9] = 0;
	buf[10] = 0;
	buf[11] = 0;
	buf[12] = 0;
	buf[13] = 0;		
	#ifdef	WXC2_R8
	buf[14] = compCode;
	buf[15] = alarmCode;	
	buf[16] = 0;		
	#else
	buf[14] = compCode>>8;
	buf[15] = compCode&255;	
	buf[16] = alarmCode;
	#endif
	buf[17] = 0;
	buf[18] = 0;//length 1

	buf[19] = 0;//component id
	buf[20] = 0;//component id

	buf[21] = 0;
	buf[22] = 20;//length 2	
	
	pdu.var[0].msglen = 23;
	
	request_addr.remote_ip		=inet_addr("127.0.0.1");
	request_addr.local_ip			=inet_addr("127.0.0.1");
	request_addr.remote_port	=4957;
	request_addr.local_port		=4961;
	request_addr.broadcast		=0;
	
	request_id = snmp_send(&pdu, &request_addr);
#ifdef	_TEST_		
//	if(request_id==0)
//		printf("\33[31m%d--->%d-%d-%d--Failed\n\33[37m",count,sysTypeNo,compCode,alarmCode);
//	else	
//		printf("%d--->%d-%d-%d--Succeed\n",count,sysTypeNo,compCode,alarmCode);
#endif
	count++;
}


static int testtimer()
{
	char 				sqlstr[2048];
	MYSQL_ROW		 get_row;	
	static MYSQL_RES 		*get_res;
	static int 						send_state = 0;	
	
	int i;
	
	if(testflag==1)			//generate
	{			
		switch (send_state) 
		{
			case 0:
				sprintf(sqlstr,
						"SELECT sysTypeNo,compCode,alarmCode
								 FROM sysAlarmConf
								 WHERE 2=2 %s",szWhere);							
				if (mysql_query(omcPub_conn, sqlstr) != 0)
					return -1;	
				if ((get_res = mysql_store_result(omcPub_conn)) == NULL)
					return -1;				
				send_state = 1;
				return 0;
			case 1:	
				if ((get_row = mysql_fetch_row(get_res)) != NULL) 
				{
					if(get_row[0]!=NULL&&get_row[1]!=NULL&&get_row[2]!=NULL)								
						sendHeartbeat(atoi(get_row[0]),atoi(get_row[1]),atoi(get_row[2]));
					return 0;
				}
				
				if(get_res!=NULL)
					mysql_free_result(get_res);
				send_state 	= 0;		
				testflag 		= 0;		
				return 0;
			default:
				if(get_res!=NULL)
					mysql_free_result(get_res);				
				send_state = 0;	
				testflag = 0;
				return 0;
		}
	}
	else if(testflag==2)			//clear
	{
		if(selDeviceID<20)
			sendHeartbeat(objID[selDeviceID],0,0);
		else
		{
			for(i=0;i<20;i++)
			{
				sendHeartbeat(objID[i],0,0);
				sleep(50);
			}			
		}				
		send_state = 0;	
		testflag = 0;		
		return 0;					
	}
	else if(testflag==3)			//stop
	{
		send_state = 0;	
		testflag = 0;
		if(get_res!=NULL)
			mysql_free_result(get_res);			
		return 0;		
	}
	return 0;
}

*/

/*************************************************
Function:        // getOmcVersion
Description:     // 
Calls:           // 
Called By:       // alarm_SnmpInit
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 1:OK,-1:error
Others:          // 
*************************************************/
int getOmcConf()
{
	int statfd;
	int buffSize = 1024;
	char buff[buffSize];
	char strBuf[64];
   	char *b=NULL;

	if ((statfd = open(OMCD_CONF_FILE, O_RDONLY, 0)) != -1){
		buff[buffSize-1] = 0;
		read(statfd, buff, buffSize-1);
		close(statfd);

		b = strstr(buff, "omcVersion=");
		if(b == NULL){
			fprintf(stderr,"[%s:%d] %s Error: Cannot get omcVersion!! \n",__FILE__,__LINE__,__FUNCTION__);
			exitLog("alarmAgent:getOmcConf, Can not get omcVersion");
			exit(1);
		}else{
			sscanf(b, "omcVersion=%s \n", strBuf);
			printf("omcVersion=%s \n",	strBuf);
		}
		sprintf(omcVersion,"%s",strBuf);
		
		return(1);
	}else{
		fprintf(stderr,"[%s:%d] %s Error: Can not open config file:%s!! \n",__FILE__,__LINE__,__FUNCTION__,OMCD_CONF_FILE);
		return(-1);
	}
}

void logMessage(const char *fmt,...)
{
	char logFile[64];
	FILE *fp;
	time_t l_time;
	va_list ap;
	char buf[8192];
	struct tm *t;
	char timestr[64];
	
	l_time=time(NULL);
	t=localtime(&l_time);
	sprintf(logFile,"/usr/local/omc/log/alarmAgent%04d%02d%02d.log",t->tm_year+1900,t->tm_mon+1,t->tm_mday);
	fp=fopen(logFile,"a");
	if(fp!=NULL)
	{
		va_start(ap,fmt);
		vsprintf(buf,fmt,ap);
		sprintf(timestr,"%02d:%02d:%02d: ",t->tm_hour,t->tm_min,t->tm_sec);
		fputs(timestr,fp);
		fputs(buf,fp);
		fputs("\n",fp);
		fflush(fp);
		va_end(ap);
		fclose(fp);
	}
}
