/*************************************************
File name:	smcli.h
Author: 	
Version:	9:00:00 
Date:  		2007-8-4
Description:smcli.c的头文件，定义了unitOptDef,CmdBuf,PORT_STATUS等结构
			
			
			
			
												
History:               
No. 		
Author:		  
Date:		 
Version:	   
Description:
*************************************************/
#ifndef _SMCLI_H_
#define _SMCLI_H_

#include 	<assert.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<errno.h>
#include	<string.h>
#include	<ctype.h>
#include	<math.h>
#include	<memory.h>
#include	<sys/types.h>
#include	<netinet/in.h>
#include	<sys/socket.h>
#include	<arpa/inet.h>
#include	<netdb.h>
#include	<sys/wait.h>
#include	<unistd.h>
#include	<signal.h>
#include	<sys/ipc.h>
#include	<sys/shm.h>
#include	<sys/sem.h>
#include	<sys/msg.h>
#include	<sys/time.h>
#include	<time.h>
#include    <net/if.h>
#include	<syslog.h>
#include	<termio.h>
#include    <fcntl.h>
#include    <stdarg.h>
#include    <syslog.h>
#include    <sys/io.h>
#include    <error.h>


#include "mysql.h"
#include "../../../../plat/public/src/include/pub_include.h"
#include "../../../omcLib/c_program/omcLib/omcLib.h"
#include "../../../../plat/snmp/src/include/snmp.h"

#define		SMCLI_VERSION	"R9V0_1"
#define		MAX_CLIENTS		48
#define		SMCLI_PORT		4999

#define MaxCmdTypeNum		9

#define delayTime			500

#define CmdBufSize			12

//Command TypeNo define
#define CMD_READ			1
#define CMD_MODIFY			2
#define CMD_CREATE			3
#define CMD_DELETE			4
#define CMD_CONNECT			5
#define CMD_EXPORT			6
#define CMD_IMPORT			7
#define CMD_DISCONNECT		8
#define CMD_RECHARGE		9	//20041010
#define CMD_ALIVE			90
#define CMD_HELP			91

//Database table define
#define	CommandTable		"OMC_PUB.subscriberComm"
#define	paramCommandTable	"OMC_PUB.parameterComm"
#define	sysInfoTable		"OMC_PUB.sysInfo"
#define	sysSubsConfTable	"OMC_PUB.sysSubsConf"	//20041101
#define sysUserTable		"OMC_PUB.sysUser"	//operator table
#define sysOperLogTable		"OMC_PUB.sysOperLog"
#define smcliIndexTable		"OMC_PUB.smcliIndex"	//20041119

#define SMCLI_DEFAULT_VALUE_FILE "/usr/local/apache/htdocs/smcliDefaultValue.txt"

//Error display define
#define ERR_0000	"0000:Command successful"
#define ERR_0001	"0001:Unknown command"
#define ERR_0002	"0002:Invalid syntax"
#define ERR_0003	"0003:Invalid command"
#define ERR_0004	"0004:Incomplete command"
#define ERR_0005	"0005:No privilege command"
#define ERR_0010	"0010:Unknown parameter"
#define ERR_0011	"0011:Duplicate parameter"
#define ERR_0012	"0012:No privilege parameter"
#define ERR_0200	"0200:Command failed(General failed)"
#define ERR_0201	"0201:The value is too big"
#define ERR_0202	"0202:No such field"
#define ERR_0203	"0203:Error field value"
#define ERR_0204	"0204:The field is read only"
#define ERR_0205	"0205:General error (not defined error)"
#define ERR_0206	"0206:No access to the field"
#define ERR_0207	"0207:Wrong value type"
#define ERR_0208	"0208:Wrong value length"
#define ERR_0209	"0209:Wrong encoding of the value"
#define ERR_0210	"0210:Wrong value of the field"
#define ERR_0211	"0211:Cannot create the record"
#define ERR_0212	"0212:The value is incosistent"
#define ERR_0213	"0213:Resource unavailable"
#define ERR_0214	"0214:Commit failed(try again)"
#define ERR_0215	"0215:Undo failed"
#define ERR_0216	"0216:Error authorization info"
#define ERR_0217	"0217:The field is not writable"
#define ERR_0218	"0218:Inconsistent field name"
#define ERR_0251	"0251:Unknown error"
#define ERR_0252	"0252:Cannot send command to one object at same time"
#define ERR_0254	"0254:You must connect the server firstly"
#define ERR_0255	"0255:Execute time out"
#define ERR_0256	"0256:Cannot find this server"
#define ERR_0257	"0257:Password error"
#define ERR_0258	"0258:Cannot open the file"
#define ERR_0259	"0259:The subscriber status is invalid"
#define ERR_0260	"0260:Connection failure to"
#define ERR_0261	"0261:You have connected a server,please disconnect the server firstly"
#define ERR_0300	"0300:Time out,please login again"

//commState define (define in subsComm.h)
#define nProcFail		0
#define nWaitProc		1
#define nInProc			2
#define nOutProc		3

#define InputBufSize	8192
#define OutputBufSize   8192
#define MaxOptNum	    512

typedef struct unitOptDef {
	unsigned char optFlag[MaxCmdTypeNum + 1];
	//[0]:usedFlag;0=no use,1=used
	//[1-8]:match to command type no;0=mandatory,1~8=privilege1..........privilege8,9=nouse

	char optName[64];			//参数名称
	long int exMode;			//参数值的转换存储模式
	//const 
	char fieldName[64];	//参数在数据库里的名称
	char initFieldValue[256];	//参数的默认值

	char optValue[256];			//参数值
	char fieldValue[256];		//参数在数据库里存储的值
} unitOptDef;

typedef struct CmdBuf {
	unsigned char isConnected;
	//const 
	char serverStr[64];	//设备服务器名称
	//const 
	char objectStr[64];	//操作对象名称
	char ip[16];				//设备服务器IP地址

	//const 
	int sysTypeNo;		//设备号
	int sysNo;					//系统号
	int subSysNo;				//子系统号
	//const 
	int dataTypeNo; 		//用户数据类型号

	char srcTable[64];			//
	char tmpTable[64];			//20041101

	char paramStr[64];
	char paramTable[64];

	//const 
	char cmdFlag[MaxCmdTypeNum];//八种操作的权限
	//match to CmdList's sort,0=no support 1~8=privilege1............privilege8

	unsigned int optDefNum;		//命令可以操作的命令对象个数
	unitOptDef optDef[MaxOptNum];	//命令可以操作的所有参数对象
} CmdBuf;
/* by simon at 23/9/26 */
// CmdBuf defCmdBuf[CmdBufSize];

// struct sockaddr_in cliaddr;

typedef struct PORT_STATUS {
	int connfd;
	DWORD peerip;
	int peerport;
	char username[64];
	char passwd[64];
	int failNo;
	unsigned char loginFlag;
	unsigned char usernameFlag;
	unsigned char isConnected[CmdBufSize];
	char serverStr[CmdBufSize][64];
	char ip[CmdBufSize][16];
	int sysNo[CmdBufSize];
	int subSysNo[CmdBufSize];
	int cmdBufNum;
	unsigned char fieldNameFlag;
	char fieldNameList[2048];
	char exportFieldsStr[1024];
	FILE *expFile;
	FILE *impFile;
	FILE *impLogFile;
	long int exportedNum;
	long int exportNum;
	long int exportMaxNum;
	long int importedNum;
	long int importNum;
	BOOL expDoingFlag;
	BOOL impDoingFlag;
	char tmpInputBuf[InputBufSize];
	unsigned char privilege;
	long time;
	int paramOperationCount;

	int multiPacketFlag;
	char secondPacket[InputBufSize];
} _PORT_STATUS;

/* by simon at 23/9/26*/
// _PORT_STATUS port_status[MAX_CLIENTS];

// CmdBuf *cmdBuf;

//option Flag value define
#define	Op_N	0				//no use
#define Op_M	1				//mandatory
#define Op_O	2				//optional
#define Op_S	3				//subjoin

//default option define,Everyone CmdBuf must have those option
#define OptNo_ipaddress		0
#define OptNo_passwd		1
#define OptNo_expfile		2
#define OptNo_query			3
#define OptNo_fields		4
#define OptNo_max			5
#define OptNo_impfile		6
#define OptNo_srvip			7
//#define OptNo_index       8   //index field
//#define OptNo_indexSec    9   //if have second index field
/* by simon at 23/9/26 */
// int OptNo_index;
#define HLR_OptNo_indexSec	11	//if have second index field

#define OptNo_password		9
#define OptNo_privilege		11

//exchange mode define
#define exM_same			0
#define exM_undefined		255
#define	exM_byte0_bit0		100
#define	exM_byte0_bit1		101
#define	exM_byte0_bit2		102
#define exM_byte0_bit3		103
#define exM_byte0_bit4		104
#define	exM_byte0_bit5		105
#define	exM_byte0_bit6		106
#define	exM_byte0_bit7		107
#define	exM_byte1_bit0		110
#define exM_byte1_bit1      111
#define exM_byte1_bit2      112
#define exM_byte1_bit3      113
#define exM_byte1_bit4      114
#define	exM_byte2_bit0		120
#define	exM_byte2_bit1		121
#define	exM_byte2_bit3		123
#define	exM_byte2_bit4		124
#define	exM_byte3_bit0		130
#define	exM_byte4_bit0		140
#define	exM_byte4_bit1		141
#define	exM_byte4_bit5		145
/*
if (exchange mode ==x1*10000000+x2*100000+x3*1000+x4){
	x1:the optValue type;//0:unsigned digit,1:unsigned hex,2:signed digit,3:signed hex,4:unsigned digit and bofore check the length of the optValue,the optValue need change to hex,5:signed digit and bofore check the length of the optValue,the optValue need change to hex,9:string
	x2:the min length of the value
	x3:the max length of the value
	x4:exchange mode
}
*/
int exValueFieldToOpt(int optNo);
int exValueOptToField(int optNo);

int getBitValue(char *hexStr, int byteOffset, int bitOffset,
				unsigned char *bitValue);
int setBitValue(char *hexStr, int byteOffset, int bitOffset,
				unsigned char bitValue);
void fieldSync(int optNo);
int hex2dec(char *hexStr, char *decStr);

#endif
