#include <ctype.h>
#include "../../../../plat/snmp/src/include/snmp.h"
#include "../../../omcLib/c_program/omcLib/omcLib.h"
#include "mysql.h"

#include "errmsg.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>

#ifndef __CSTA_HEAD_FILE
#define __CSTA_HEAD_FILE

#ifndef _T_BYTE
#define _T_BYTE
typedef unsigned char BYTE;
#endif

#ifndef _T_BOOL
#define _T_BOOL
typedef unsigned char BOOL;
#endif

#ifndef _T_DWORD
#define _T_DWORD
typedef unsigned int DWORD;
#endif

#ifndef _T_WORD
#define _T_WORD
typedef unsigned short WORD;
#endif
//=======================================================
int csta_init();
void csta_timer();

#ifndef CSTA_DEBUG
#define CSTA_DEBUG 0
#endif

#define CSTA_DB "CSTA_DB"
#define CSTA_LOG_PATH	"../log"
#define LOG_FILE "../log/cstalog"
#define OUTPUT_FILE_TIME_HOUR 4
#define OUTPUT_FILE_TIME_MINUTE 30
#define RECORD_KEEP_DAY 30
#define CSTA_PORT 4954

#define MAX_SYSTEM_NUM 32
#define MAX_OBJECT_NUM 16
#define MAX_FIELD_NUM 512
#define MAX_CSTA_SRC_NUM 64
#define MAX_MSG_NUM 32

#define UPDATE_SYSTEM_INTERVAL 50*60
#define CHASE_INTERVAL 50*4

#define TIMETO_NONE 0
#define TIMETO_GET_CSTA 1
#define TIMETO_OUTPUT_FILE 2
#define TIMETO_CHANGE_LOG 3

#define STATE_NORMAL 0
#define STATE_CHASE 1

#define STATE_MSG_NONE 0
#define STATE_MSG_GET_REQUEST 1
#define STATE_MSG_WAIT_RESPONSE 2

typedef struct csta_sys
{
	DWORD request_id;
	int sysid;
	int subsysid;
	snmp_addr addr;
	int system_state;
	int msg_state;
	
	int instance;
	int objindex;
	int id1;
	int id2;
	
	//for retry
	int pre_instance;
	int pre_objindex;
	int pre_id1;
	int pre_id2;
	
	int timeout;
	int fail;
	int retry;
	BYTE cstamsg[MAX_OBJECT_NUM][4096];
	unsigned long msglen;
}csta_sys;

typedef struct csta_obj
{
	int csta_type;
	int field_width;
	
	int oidlen;
	DWORD oid[OID_MAXLEN];
	
	int id1_start;
	int id1_end;
	int id1_offset;
	char id1_field[32];
	
	int id2_start;
	int id2_end;
	int id2_offset;
	char id2_field[32];
	
	char sql_detail[8192];
	char detail_field_list[MAX_FIELD_NUM][32];
	int detail_field_count;
	
	char relate_table[64];
	
	char sql_sum[8192];
	char sum_field_list[MAX_FIELD_NUM][32];
	int sum_field_count;
	
	char detail_table[64];
	char sum_table[64];
}csta_obj;

typedef struct csta_src
{
	int sysTypeNo;
	int hassubsys;
	int sum_flag;
	int check_time_flag;
	int single_message_flag;
	csta_sys systems[MAX_SYSTEM_NUM];
	int systemCount;
	
	csta_obj objects[MAX_OBJECT_NUM];
	int objectCount;
	
	char csv_path[64];
}csta_src;


#endif
