/*************************************************
File name:	smeg_mib.c
Author: 	JianHui Zheng
Version:	9:00:00 
Date:  		2008-4-1
Description:This file include the set and get operation on the omc page
			
			
			
												
History:               
No. 		
Author:		  
Date:		 
Version:	   
Description:
*************************************************/
#include "include/smeg_smpp.h"
#include "include/smtp_const.h"
#include "include/smeg_pub.h"
//#include "../../plat/snmp/src/include/snmp.h"
//#include "../../plat/snmp/src/include/macro.h"
#include "snmp.h"
#include "macro.h"

#define SMEG_OID				{1,3,6,1,4,1,1373,2,3,3,11}
#define SMEG_OIDLEN				11
#define	SMEG_CONF_FILE			"./conf/smeg.conf"
#define SMEG_MODULE_VERSION		"R9V0_01"
/*
extern int license;              //oid 1
extern int  Num_sms_per_email;   //oid 2 Maximum number of SMS can be concatenated per long email

extern char Email_server_IP[16];  //oid 3 IP address of the host email server ////"202.108.5.82" smtp.163.com
extern char SMEG_DOMAIN_NAME[30];	//oid 4	"wanghebo" 
extern char smtp_tx_user_name[50];          //oid 5 SMTP Client authentication user name 
extern char smtp_tx_user_password[50];      //oid 6  SMTP Client authentication user password
extern char Gateway_Msisdn[30];              //oid 7  MSISDN of the UW SMS Email Gateway
extern char Country_code_sms[5];             //oid 8  Default country code of the local SMS

extern BYTE server_type_rx;                 //oid 9-1 server_type: 0-SMPP_TXENT; 1-SMPP_SERVER;
extern BYTE session_type_rx;                //oid 9-2 session_type: 0-BIND_TX; 1-BIND_RX; 2-BIND_TRX
extern BYTE service_number_rx[30];          //oid 9-3 service_number_ptr: poiter to remote service number.
extern BYTE message_mode_rx;                //oid 9-4 message_mode: 0-STORE_AND_FORWARD; 1-DATAGRAM; 2-TRANSACTION

extern BYTE server_type_tx;                 //oid 10-1             
extern BYTE session_type_tx;                //oid 10-2
extern BYTE service_number_tx[30];          //oid 10-3
extern BYTE message_mode_tx;                //oid 10-4

char SMS_MSISDN_prefixes_allowed;            //oid 11
static char Local_domain[40];                //oid 12
*/
void inqure_smeg_table();
extern int register_snmp_table(struct snmp_register *reg);
extern int save_smeg_param_flag;

/************************************************/
//MIB Interface=================================>
#define SMEG_MAX_LICENSE				200000
#define SMEG_MAX_SMS_PER_MAIL			16
typedef struct smeg_param_tag
{
	//SMPP Param
	BYTE 	service_number[17];
	
	//SMTP Param
	BYTE	mailserver_ip[16];
	BYTE	smtp_auth_flag;
	BYTE	smtp_auth_user[65];
	BYTE	smtp_auth_passwd[17];
	
	//Global Param
	int 	license;
	BYTE	cc[6];
	BYTE	ndc[6];
	BYTE	max_sms_per_email;
	BYTE  function_flag;
	BYTE	local_domain[33];
	BYTE	email_domain[33];
	struct allowed_prefix_tag
	{
		BYTE	flag; // 0-no use,1 use
		BYTE	prefix[17];
	}allowed_prefix[SMEG_MAX_ALLOWED_PREFIX_ENTRY];
}SMEG_PARAM;

typedef struct smeg_state_tag
{	
	BYTE	smtp;
	BYTE	smpp_tx;
	BYTE	smpp_rx;
}SMEG_STATE;

static SMEG_PARAM param;
static SMEG_STATE state;

/*******************************************************/
/****            Get smeg parameter                 ****/
/*******************************************************/
BYTE *get_service_number()
{
	return param.service_number;
}
BYTE* get_mailserver_ip()
{
	return param.mailserver_ip;
}
BYTE get_smtp_auth_flag()
{
	return param.smtp_auth_flag;
}
 BYTE* get_smtp_auth_user()
{
	return param.smtp_auth_user;
}
BYTE* get_smtp_auth_passwd()
{
	return param.smtp_auth_passwd;
}
int get_license()
{
	return param.license;
}
BYTE* get_cc()
{
	return param.cc;
}
BYTE* get_ndc()
{
	return param.ndc;
}
 BYTE get_max_sms_per_email()
{
	return param.max_sms_per_email;
}
BYTE get_function_flag()
{
	return param.function_flag;
}
BYTE* get_localdomain()
{
	return param.local_domain;
}
BYTE* get_emaildomain()
{
	return param.email_domain;
}
BYTE* get_allowed_prefixes(int i)
{
	return (BYTE *)&param.allowed_prefix[i].prefix;
}

BYTE get_allowed_prefixes_flag(int i)
{
	return param.allowed_prefix[i].flag;
}

/*******************************************************/
/****            Set smeg parameter                 ****/
/*******************************************************/
/*
	Set the service number parameter, oid=1.3.6.1.4.1.1373.1.3.3.11.2.1.1
*/
void set_service_number(const BYTE *pdata, int datalen)
{
	if(datalen > 16 || pdata == NULL){
		//print warning here
		return;
	}
	
	memcpy(param.service_number, pdata, datalen);
	param.service_number[datalen] = '\0';
}

/*
	Set the mail server ip parameter, oid=1.3.6.1.4.1.1373.1.3.3.11.2.2.1
*/
void set_mailserver_ip(const char *pdata, int datalen)
{
	if(datalen > 16 || pdata == NULL){
		//print warning here
		return;
	}
	memcpy(param.mailserver_ip, pdata, datalen);
	param.mailserver_ip[datalen] = '\0';
}

/*
	Set the smtp authentication flag, oid=1.3.6.1.4.1.1373.1.3.3.11.2.2.2
*/
void set_smtp_auth_flag(int i)
{
	param.smtp_auth_flag = i;	
}

/*
	Set the smtp authentication user name, oid=1.3.6.1.4.1.1373.1.3.3.11.2.2.3
*/
void set_smtp_auth_user(const BYTE *pdata, int datalen)
{
	if(datalen > 64 || pdata == NULL){
		//print warning here
		return;
	}
	memcpy(param.smtp_auth_user, pdata, datalen);
	param.smtp_auth_user[datalen] = '\0';
}

/*
	Set the smtp authentication user password, oid=1.3.6.1.4.1.1373.1.3.3.11.2.2.4
*/	
void set_smtp_auth_passwd(const BYTE *pdata, int datalen)
{
	if(datalen > 16 || pdata == NULL){
		//print warning here
		return;
	}
	memcpy(param.smtp_auth_passwd, pdata, datalen);
	param.smtp_auth_passwd[datalen] = '\0';
}

/*
	Set the license parameter, oid=1.3.6.1.4.1.1373.1.3.3.11.2.3.1
*/
void set_license(int n)
{
	if(n > SMEG_MAX_LICENSE){
		//print warning here
		param.license = SMEG_MAX_LICENSE;
	}
	else
		param.license = n;
}

/*
	Set country code parameter, oid=1.3.6.1.4.1.1373.1.3.3.11.2.3.2
*/
void set_cc(const BYTE *pdata, WORD datalen)
{
	if(datalen > 5 || pdata == NULL){
		return;
	}
	
	memcpy(param.cc, pdata, datalen);
	param.cc[datalen] = '\0';
}

/*
	Set ndc parameter, oid=1.3.6.1.4.1.1373.1.3.3.11.2.3.3
*/
void set_ndc(const BYTE *pdata, WORD datalen)
{
	if(datalen > 5 || pdata == NULL){
		return;
	}
	memcpy(param.ndc, pdata, datalen);
	param.ndc[datalen] = '\0';
}

/*
	Set max sms per mail parameter, oid=1.3.6.1.4.1.1373.1.3.3.11.2.3.4
*/
void set_max_sms_per_email(int n)
{
	if(n > SMEG_MAX_SMS_PER_MAIL){
		//print warning here
		param.max_sms_per_email = SMEG_MAX_SMS_PER_MAIL;
	}
	else
		param.max_sms_per_email = n;
}

/*
	Set the function flag, oid=1.3.6.1.4.1.1373.1.3.3.11.2.3.8
*/
void set_function_flag(int n)
{
	if(n)
	{
		param.function_flag=1;
	}
	else
	{
		param.function_flag=0;
	}
}

/*
	Set the local domain parameter, oid=1.3.6.1.4.1.1373.1.3.3.11.2.3.5
*/
void set_localdomain(const BYTE *pdata, int datalen)
{
	if(datalen > 32 || pdata == NULL){
		return;	
	}
	memcpy(param.local_domain, pdata, datalen);
	param.local_domain[datalen] = '\0';
}

/*
	Set email domain parameter, oid=1.3.6.1.4.1.1373.1.3.3.11.2.3.6
*/
void set_emaildomain(const BYTE *pdata, int datalen)
{
	if(datalen > 32 || pdata == NULL){
		return;
	}
	memcpy(param.email_domain, pdata, datalen);
	param.email_domain[datalen] = '\0';
}

/*
	Set the allowed prefixes, oid=1.3.6.1.4.1.1373.1.3.3.11.2.3.7.(index)
*/
void set_allowed_prefixes(int i, const BYTE *pdata, int datalen)
{
	if(datalen > 17 || pdata == NULL){
		return;
	}
	memcpy(&param.allowed_prefix[i].prefix, pdata, datalen);
	param.allowed_prefix[i].prefix[datalen] = '\0';
}

//State----------------------------------------------------------------------------------------------
//get state
int get_smtp_link_state()
{
	return state.smtp;
}
int get_smpp_tx_link_state()
{
	return state.smpp_tx;
}
int get_smpp_rx_link_state()
{
	return state.smpp_rx;
}


//set state
void set_smtp_link_state(int i)
{
	state.smtp = i;
}
void set_smpp_tx_link_state(int i)
{
	state.smpp_tx = i;
}
void set_smpp_rx_link_state(int i)
{
	state.smpp_rx = i;
}

/************************************************/
/*************************************************
Function:        // smeg_mib_init
Description:     // The initial function of the mib
Calls:           // smeg_loadParam
					inquire_setmsg
					inquire_getmsg
					inquire_trapmsg;
					smeg_trapcallback
Called By:       // smeg_init
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 
Others:          // 
*************************************************/
void smeg_mib_init()
{
//	DWORD oid[20] = SMEG_OID;

	inqure_smeg_table();
  //inquire_setmsg ( SMEG_OIDLEN, oid, smeg_setmib );
  //inquire_getmsg ( SMEG_OIDLEN, oid, smeg_getmib );
    
	inquire_trapmsg ( smeg_trapcallback ); 
	smeg_loadParam();
}

/*************************************************
Function:        // smeg_setmib
Description:     // The callback function when the set the parameter of the smeg
Calls:           // the set functions
Called By:       // smeg_mib_init
Table Accessed:  // 
Table Updated:   // 
Input:           // oidlen:the len of the oid
					pData:the data
					datalen:the length of the data
Output:          // 
Return:          // 
Others:          // 
*************************************************/
int smeg_setmib ( BYTE oidlen, DWORD * oid, BYTE * pData, WORD datalen )
{

	switch(oid[SMEG_OIDLEN])
	{
		case 1:	//Heartbeat
		/*
		case 2:	//Parameter
			switch(oid[SMEG_OIDLEN+1])
			{
				case 1:
					switch(oid[SMEG_OIDLEN+2])
					{
						case 1:
							set_service_number(pData, datalen);
							break;
						default:
							return (-1);
					}
					break;	
				case 2:
					switch(oid[SMEG_OIDLEN+2])
					{
						case 1:
							set_mailserver_ip(pData, datalen);
						break;
						case 2:
							set_smtp_auth_flag(*pData);
						break;
						case 3:
							set_smtp_auth_user(pData, datalen);
						break;
						case 4:
							set_smtp_auth_passwd(pData, datalen);
						break;
					}
					break;
				case 3:
					switch(oid[SMEG_OIDLEN+2])
					{
						case 1:
//							n = pData[0];
//							n = (n << 8) | pData[1];
//							n = (n << 8) | pData[2];
//							n = (n << 8) | pData[3];
//							set_license(n);
						break;
						case 2:
							set_cc(pData, datalen);
						break;
						case 3:
							set_ndc(pData, datalen);
						break;
						case 4:
							set_max_sms_per_email(*pData);
						break;
						case 5:
							set_localdomain(pData, datalen);
						break;
						case 6:
							set_emaildomain(pData, datalen);
						break;
						case 7:
							set_allowed_prefixes(oid[SMEG_OIDLEN+3], pData, datalen);
						break;
						default:
							return -1;
						break;
					}
					break;
				case 8:
					smeg_writeParam();					
					break;
				default:
					return (-1);
			}
			break;
		*/
		case 3: //CSTA
			break;
		case 4:	//CDR
			return (-1);
			break;
		
		default:
			return (-1);
			break;
	}
	
	return datalen;             
}

/*************************************************
Function:        // smeg_getmib
Description:     // The callback function when the get the parameter of the smeg
Calls:           // the get functions; GetCSTA_Record;
Called By:       // smeg_mib_init
Table Accessed:  // 
Table Updated:   // 
Input:           // oidlen:the len of the oid
					pData:the data
					vartype:the type of the data
Output:          // 
Return:          // 
Others:          // 
*************************************************/
int smeg_getmib ( BYTE oidlen, DWORD * oid, BYTE * pData, BYTE * vartype )
{
	int datalen = 0;
//	int temp = 0;
//	char *ch = NULL;               
	*vartype	= 4;
	memset(pData, 0, 64);

	switch(oid[SMEG_OIDLEN])
	{
		/*
		case 2:	// Parameter
			switch(oid[SMEG_OIDLEN+1])
			{
				case 1:	//SMPP
					switch(oid[SMEG_OIDLEN+2])
					{
						case 1:
							ch = get_service_number();
							datalen = 16;
							memcpy(pData, ch, datalen);
							pData[datalen] = '\0';
							return datalen;
						break;
						default:
							return -1;
					}
				break;
				case 2:	//SMTP
					switch(oid[SMEG_OIDLEN+2])
					{
						case 1:
							ch = get_mailserver_ip();
							strcpy(pData, ch);
							datalen = 15;
					//		*vartype = 0x40;
							pData[datalen] = '\0';
							break;
						case 2:
							pData[0] = get_smtp_auth_flag();
							*vartype = 2;
							datalen = 1;
							break;
						case 3:
							ch = get_smtp_auth_user();
							strcpy(pData, ch);
							datalen = 64;
							pData[datalen] = '\0';
							break;
						case 4:
							ch = get_smtp_auth_passwd();
							strcpy(pData, ch);
							datalen = 16;
							pData[datalen] = '\0';
							break;
						default:
							return -1;
							break;
					}
				break;
				case 3:	//Global
					switch(oid[SMEG_OIDLEN+2])
					{
						case 1:					
							temp = get_license();
							pData[0] = temp >> 24;
							pData[1] = (temp >> 16) & 0xFF;
							pData[2] = (temp >> 8) & 0xFF;
							pData[3] = temp & 0xFF;
							*vartype = 2;
							datalen = 4;
							pData[4] = '\0';
							break;
						case 2:
							ch = get_cc();
							strcpy(pData, ch);
							datalen = 5;
							pData[datalen] = '\0';
							break;
						case 3:
							ch = get_ndc();
							strcpy(pData, ch);
							datalen = 5;
							pData[datalen] = '\0';
							break;
						case 4:
							pData[0] = get_max_sms_per_email();
							pData[1] = '\0';
							*vartype = 2;
							datalen = 1;
							break;
						case 5:
							ch = get_localdomain();
							strcpy(pData, ch);
							datalen = 32;
							pData[datalen] = '\0';
							break;
						case 6:
							ch = get_emaildomain();
							strcpy(pData, ch);
							datalen = 32;	
							pData[datalen] = '\0';
							break;
						case 7:
							if(oid[SMEG_OIDLEN+3] > 15)
								return -1;	
							ch = get_allowed_prefixes(oid[SMEG_OIDLEN+3]);
							memcpy(pData, ch, 17);
							datalen = 17;
							pData[datalen] = '\0';
							break;
						default:
							return (-1);
					}
				break;
				case 8:
					pData[0] = 1;
					datalen = 1;		
					break;
				default:
					return (-1);
			}
			break;
		
		*/	
		case 3: // CSTA	
			datalen = GetCSTA_Record(oid[SMEG_OIDLEN+1], pData);
			*vartype	= 4;
			pData[datalen] = '\0';
			break;
		case 4:	// CDR			
			break;
		default:
			return -1;
			break;
	}
 
 	return datalen;
}

/*************************************************
Function:        // smeg_writeParam
Description:     // Write the parameter into the smeg.conf file
Calls:           // 
Called By:       // smeg_setmib
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 
Others:          // 
*************************************************/
int smeg_writeParam()
{
	FILE *fp;
	
	save_smeg_param_flag=0;

	if((fp = fopen(SMEG_CONF_FILE,"wb")) == NULL)
		return	0;

	fwrite((void *)&param, sizeof(param), 1, fp);
	
	fclose(fp);
	
	return 1;
}

/*************************************************
Function:        // smeg_loadParam
Description:     // Load the parameter into the param
Calls:           // 
Called By:       // smeg_mib_init
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 
Others:          // 
*************************************************/
int smeg_loadParam()
{
	FILE	*fp;
	
	fp = fopen(SMEG_CONF_FILE, "rb");
 	if (fp == NULL)
	{
		smeg_print("SMEG configuration file %s not existed, using default parameters\n", SMEG_CONF_FILE);
		return	0;
	}	
	
	fread((void*)&param, sizeof(param), 1, fp);
	
	fclose(fp);
	return TRUE;
}

#define OMC_HB_OID     		{1,3,6,1,4,1,1373,2,4,1}
#define OMC_HB_OIDLEN  		10
#define OMC_HB_SEQUEUE_NUM 39
DWORD smeg_ip[2];
int omc_status[2];
BYTE omc_sequeue[2][4]; 
int test_i=0;
extern BYTE *SequenceNum;
void smeg_trapcallback ( BYTE oidlen, DWORD * oid, BYTE * pdata, WORD len, snmp_addr * addr )
{
		//int i;
    DWORD OMCHLObjID[20] = OMC_HB_OID;

    if( memcmp ( &oid[7], &OMCHLObjID[7], ( OMC_HB_OIDLEN - 7 ) * 4 ) == 0 )    //omc heartbeat
    {
			if(*pdata < 2)
			{
				if(memcmp(SequenceNum,&pdata[OMC_HB_SEQUEUE_NUM],4) == 0)
				{
					smeg_ip[*pdata] = addr->remote_ip;
					omc_status[*pdata]=pdata[26];
					memset(omc_sequeue[*pdata],0,4);
					memcpy(omc_sequeue[*pdata],&pdata[OMC_HB_SEQUEUE_NUM],4);
					//omc_sequeue[*pdata][0]=1;
				}
			}
    }
}


//============================new mib file=========================
int smeg_set_system(u32 column, u32 *instance, u8 *pdata, u16 datalen)
{
	switch(column)
	{
		//license
		case 1:
			break;
		//country code
		case 2:
			if(datalen>5)
			{
				sprintf((char*)pdata,"length is too big");
				return -2;
			}
			set_cc(pdata, datalen);
			break;
		//national destination code
		case 3:
			set_ndc(pdata, datalen);
			break;
		//max sms per email
		case 4:
			set_max_sms_per_email(*pdata);
			break;
		//local domain name
		case 5:
			set_localdomain(pdata, datalen);
			break;
		//domain name for email
		case 6:
			set_emaildomain(pdata, datalen);
			break;
		//save parameter
		case 7:
			smeg_writeParam();					
			break;
		case 8:
			set_function_flag(*pdata);
			break;
	}
	save_smeg_param_flag=1;
	return datalen;
}

int smeg_get_system_resp(u32 column, u32 *instance, u8 *pdata, u8 *vartype)
{
	int datalen = 0;
	char *ch = NULL; 
	int temp=0;
	
	switch(column)
	{
		//license
		case 1:
			temp = get_license();
			pdata[0] = temp >> 24;
			pdata[1] = (temp >> 16) & 0xFF;
			pdata[2] = (temp >> 8) & 0xFF;
			pdata[3] = temp & 0xFF;
			*vartype = 2;
			datalen = 4;
			pdata[4] = '\0';
			break;
		//country code
		case 2:
			ch = (char*)get_cc();
			strcpy((char*)pdata, ch);
			datalen = 5;
			datalen = strlen(ch);			
			*vartype = 4;
			pdata[datalen] = '\0';
			break;
		//national destination code
		case 3:
			ch = (char*)get_ndc();
			strcpy((char*)pdata, ch);
			//datalen = 5;
			datalen = strlen(ch);
			*vartype = 4;
			pdata[datalen] = '\0';
			break;
		//max sms per email
		case 4:
			pdata[0] = get_max_sms_per_email();
			pdata[1] = '\0';
			*vartype = 2;
			datalen = 1;
			break;
		//local domain name
		case 5:
			ch = (char*)get_localdomain();
			strcpy((char*)pdata, ch);
			//datalen = 32;
			datalen = strlen(ch);
			*vartype = 4;
			pdata[datalen] = '\0';
			break;
		//domain name for email
		case 6:
			ch = (char*)get_emaildomain();
			strcpy((char*)pdata, ch);
			//datalen = 32;
			datalen = strlen(ch);	
			*vartype = 4;
			pdata[datalen] = '\0';	
			break;
		//save parameter
		case 7:
			pdata[0] = 1;
			datalen = 1;	
			*vartype = 2;
			break;
		//function flag
		case 8:
			pdata[0] = get_function_flag();
			pdata[1] = '\0';
			*vartype = 2;
			datalen = 1;
			break;
	}
	
	return datalen;
}

int smeg_set_protocol(u32 column, u32 *instance, u8 *pdata, u16 datalen)
{	
	char ipstr[16];
	switch(column)
	{
		//smpp link alias(service number)
		case 1:
			set_service_number(pdata, datalen);
			break;
		//email server ip
		case 2:
			
			sprintf(ipstr,"%d.%d.%d.%d",pdata[0],pdata[1],pdata[2],pdata[3]);
			set_mailserver_ip(ipstr, strlen(ipstr));
			break;
		//authentication flag	
		case 3:
			set_smtp_auth_flag(*pdata);
			break;
		//authentication user name
		case 4:
			set_smtp_auth_user(pdata, datalen);
			break;
		//authentication password
		case 5:
			set_smtp_auth_passwd(pdata, datalen);
			break;
	}
	save_smeg_param_flag=1;
	return datalen;
}

int smeg_get_protocol_resp(u32 column, u32 *instance, u8 *pdata, u8 *vartype)
{
	int datalen = 0;
	//int temp = 0;
	char *ch = NULL; 

	switch(column)
	{
		//smpp link alias
		case 1:
			ch = (char*)get_service_number();
			//datalen = 16;
			datalen = strlen(ch);
			memcpy(pdata, ch, datalen);
			*vartype = 4;
			pdata[datalen] = '\0';
			break;
		//email server ip
		case 2:
			ch = (char*)get_mailserver_ip();
			strcpy((char*)pdata, ch);
			//datalen = 15;
			datalen = strlen(ch);
			*vartype = 4;
			pdata[datalen] = '\0';
			break;
		//authentication flag
		case 3:
			pdata[0] = get_smtp_auth_flag();
			*vartype = 2;
			datalen = 1;
			break;
		//authentication user name
		case 4:
			ch = (char*)get_smtp_auth_user();
			strcpy((char*)pdata, ch);
			//datalen = 64;
			datalen = strlen(ch);
			*vartype = 4;
			pdata[datalen] = '\0';
			break;
		//authentication password
		case 5:
			ch = (char*)get_smtp_auth_passwd();
			strcpy((char*)pdata, ch);
			//datalen = 16;
			datalen = strlen(ch);
			*vartype = 4;
			pdata[datalen] = '\0';
			break;
	}
	return datalen;
}

static int smeg_set_aup(u32 column, u32 *instance, u8 *pdata, WORD datalen)
{
	u8 new_id;
	//u8 temp8;
	//int i;
	int index;

	save_smeg_param_flag=1;
	//create new aup
	if(column == 1)
	{
		//create by data
		new_id=pdata[0];
		if(new_id >0 && new_id<SMEG_MAX_ALLOWED_PREFIX_ENTRY)
		{
			if(param.allowed_prefix[new_id].flag == 0)
			{
				param.allowed_prefix[new_id].flag=1;
				memset(param.allowed_prefix[new_id].prefix,0,17);
				return datalen;
			}
			else
				return datalen;
		}
		else
			return -1;	

		/*
		//create auto
		for(i=0;i<SMEG_MAX_ALLOWED_PREFIX_ENTRY;i++)
		{
			if(param.allowed_prefix[i].flag == 0)
			{
				param.allowed_prefix[i].flag=1;
				memset(param.allowed_prefix[i].prefix,0,17);
				return datalen;
			}
		}
		return -1;
		*/
	}
	
	index=instance[0];
	if(index >0 && index<SMEG_MAX_ALLOWED_PREFIX_ENTRY)
	{
		//no such instance
		if(param.allowed_prefix[index].flag == 0)
			return -1;
	}
			
	switch(column)
	{
		//prefix
		case 2:
			set_allowed_prefixes(index, pdata, datalen);
			break;
		//delete
		case 3:
			if(pdata[0] == 6)
			{
				param.allowed_prefix[index].flag = 0; 
				memset(param.allowed_prefix[index].prefix,0,17);
			}
			break;
	}
	return datalen;
}

static int smeg_get_aup(u32 column, u32 *instance, u8 *pdata, u8 *vartype)
{
	int datalen = 0;
	char *ch=NULL;
	int index;

	index=instance[0];
	
	if(index >0 && index<SMEG_MAX_ALLOWED_PREFIX_ENTRY)
	{
		//no such instance
		if(param.allowed_prefix[index].flag == 0)
			return -1;
	}
	else
		return -1;
		
	switch(column)
	{
		//index
		case 1:
			pdata[0]=index;
			datalen=1;
			*vartype=2;
			break;
		//prefix
		case 2:
			ch = (char*)get_allowed_prefixes(index);
			//datalen = 17;
			datalen = strlen(ch);
			memcpy(pdata, ch, datalen);
			pdata[datalen] = '\0';
			*vartype=4;
			break;
		//row status
		case 3:
			pdata[0]=param.allowed_prefix[index].flag;
			datalen=1;
			*vartype=2;
			break;
	}
	return datalen;
}

int smeg_getnext_aup(u32 *id)
{
	int i;
	
	if(*id == 0)
		*id=1;
	for(i = *id; i < SMEG_MAX_ALLOWED_PREFIX_ENTRY; i++)
	{
		if(param.allowed_prefix[i].flag == 1)
		{
			*id = i;
			return 1;
		}
	}
	*id = 0;
	return 0;
}

static struct snmp_table_profile smeg_tables[] = {
	{
		.name = "system",
		.index.len = 13,
		.index.oid = {1, 3, 6, 1, 4, 1, 1373, 2, 3, 3, 11, 2, 1},
		.column = {0},
		.level = 0,
		.get_instance = NULL,
		.get_resp = smeg_get_system_resp,
		.set_resp = smeg_set_system
	},
	{
		.name = "protocol",
		.index.len = 13,
		.index.oid = {1, 3, 6, 1, 4, 1, 1373, 2, 3, 3, 11, 2, 2},
		.column = {0},
		.level = 0,
		.get_instance = NULL,
		.get_resp = smeg_get_protocol_resp,
		.set_resp = smeg_set_protocol
	},
	{
		.name = "allowedUserPrefix",
		.index.len = 14,
		.index.oid = {1, 3, 6, 1, 4, 1, 1373, 2, 3, 3, 11, 2, 3, 1},
		.column = {1, 2, 3, 0},
		.level = 1,
		.get_instance = smeg_getnext_aup,
		.get_resp = smeg_get_aup,
		.set_resp = smeg_set_aup
	}
};

static struct snmp_register smeg_register = {
	.name = "SMEG",
	.prefix_len = 11,
	.prefix_oid = {1, 3, 6, 1, 4, 1, 1373, 2, 3, 3, 11},
	.num_of_table = sizeof(smeg_tables)/sizeof(struct snmp_table_profile),
	.table = smeg_tables,
	.default_get_resp = smeg_getmib,
	.default_set_resp = smeg_setmib
};

void inqure_smeg_table()
{
	register_snmp_table(&smeg_register);
}

