#include "../include/includes.h"
#include "../include/ppsm.h"
#include "../include/ramdata.h"
#include "../include/ppsinf.h"
#include "../include/ppsoid.h"
#include "../include/param.h"
#include "../include/cdrme.h"
#include "../include/report.h"
#include "../include/message.h"
#include "../include/tariff.h"
#include "../include/precard.h"
#include "../include/mfunc.h"
#include "../include/plat_inc.h"
#include	"../include/ppsMibTable.h"
#include	"../include/mfunc.h"
#include   "../include/gprs.h"

// fix multiple definition====================================
_pps_cdr_buffer   pps_cdr_buffer;
_pps_csta_buffer  pps_csta_buffer;
//

DWORD    PPS_OID_PREFIX[]={1,3,6,1,4,1,1373,2,3,3,PPSOID};
DWORD    PPS_OID_PREFIX_USER[]={1,3,6,1,4,1,1373,2,3,3,PPSOID,2,2,2,1};         
DWORD    PPS_OID_THIRDPARTY[]={1,3,6,1,4,1,1373,2,3,3,PPSOID,5,5};

/*--------------------------------------------------*/
int SavePPSParam();
/*--------------------------------------------------*/
int pps_sub_sync_test();
int ocs_set_gy_report_data_method(int flag);
extern int snmpResponse(snmp_pdu *pdu, snmp_addr *addr);
extern void SaveSubsData();
extern void SaveTariffPlan();
extern int thirdPartyChargeProc(snmp_pdu *pdu, snmp_addr *addr);
extern int SetCRDParam(u_char oidLen, DWORD *oid, u_char *pdata, u_short dataLen);
extern int pps_exit(int flag);
extern int ReloadPPSSubscriberFromHD();
extern int ReadDailyReportRequest(u_char oidLen,DWORD *oid,u_char *pdata,BYTE *vartype);
extern int GetCRDParam(u_char oidLen, DWORD *oid, u_char *pdata, BYTE *vartype);
extern int GotSyncDailyReport(DWORD *oid,u_char *pdata,u_short dataLen);
extern int DisplayDailyReportIndex(char *ptr);
extern int FindPPSSubsStoreIndex(long long key);
extern int printfClimaxQuenceIndex();
extern int searchCardInfoByPWD(char *pwd);
extern int SetReplaceCardFlag(int flag);
extern int SetReplaceSubsDataFlag(int flag);
extern int SetSyncFlag(int flag);
extern int SetPeriodicSyncFlag(int flag);
extern int SetTcpSyncFlag(int flag);
extern int CheckCardPWD(char *pin);
extern int searchCardByIndex(int instance);
extern int RechargeRequestProc(u_short portid);
extern int ppsListAllIndexByOrder();
extern int CreatePPSDataIndexForAll();
extern int SetActivityTestTimer(char timer);
extern int ListAllPortStatus(int flag);
extern int pps_rest_test_process(int flag);
extern int ModifyPPSSubsData(_prepaid_info *shmp);
extern int AddPPSSubsData(_prepaid_info *shmp);
extern int GetPPSSubsRecord(char *msisdn, _prepaid_info *shmp);
extern int DeletePPSSubsData(_prepaid_info *shmp);
extern int IncreaseFavoriteNumberChangeCounter(int head, int tail);
extern int IncreaseFavoriteNumberChangeCounterByOmc(_prepaid_info *ptr);
extern int BillingFavoriteNumberChange(_state_data *shmp);
extern int AccountLatchFlagCheck(int head, int tail);
extern void initSMSTable();
int setPPSSmppNumber(char *number, int flag);
extern	int  	AddNewCardIntoClimaxQuence(_recharge_card_info *cardShmp);
extern int 		GetAccountInfoBySortIndex(int index, char *msisdn);
extern int start_pps_ocs_procedure(int flag, int nums);
extern	int 	ppsCleanCardFaceValue(int faceValue);
extern	void 	set_truncation_flag(int flag);
extern	int 	get_truncation_flag();
extern void		DebugMsg(void *ptr, const char *fmt, ...);
extern u_short			debug_messages;
extern int		  		logMsgFlag;
extern void SaveRechargeCard();
extern int pps_get_subid();
void SetSMSDelimiter(char *delimiter);
void ppsSetParameterAutoSaveFlag();
void ppsCheckParameterAutoSaveFlag();
int ConfigureDefaultSerivceNumber();
int ConfigureSerivceNumberPerCos(char *cmd, u8 type);
int GetSerivceNumberFlagPerCos(u8 cos_id, u8 service_type, u8 call_type);
void PrintCosServiceNumber(u8 cos_id, u8 type);
int pps_set_gy_delay_timer(int flag, int value);
#define			 PPS_ASCII_OUT_LEN	4096
int ocs_set_threshold_supported(int flag);
int	   	PPS_set_license(unsigned char *license);

/*--------------------------------------------------*/
int		logMsgFlag = 0x00;
char		logMsisdn[18] = "FFFFFFFFFFFFFFFFFF";

u_char		sumOfParameter;

extern _ram_data *ramshm_ptr;
extern DWORD	primary_data_server,second_data_server;

extern int	csta_now_instance,csta_old_instance;
extern int	cdr_head_ptr,cdr_tail_ptr ;
extern u_char	version_id[3];
extern u_char	peer_version_id[3];

extern char	masterFlag;
extern DWORD	peer_server_ip;
extern int	systemCapability;
extern char	alarm_code[2];

extern _state_machine	*smshm_ptr;
extern DWORD	LOCAL_IP;

extern struct	timeval tvnow;

u_char		uploadUserStatus[6]; /* status for upload */
u_char		uploadPrecardStatus[6];
extern _load_file	load_file; /* 0,1=user data/tariff data */

extern	PPSParamStruct	ppsParam;

extern char	serialNumber[8];
extern char	peer_server_in_init;
int		pps_peer_hb_flag = 0;
/*--------------------------------------------------*/
static int  	zonenum[MAX_ZONE_NUM]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
/*--------------------------------------------------*/
static  void 	ListData();
static  void 	ListSubsSegNo();
static  void 	ListCardsSegNo();
static  void 	SetDebugTariff(int, int, int);
static  int 	ToBeListed=0,ToBeListedSegNo=0;

#define		PPS_DEBUG_ID  10

//static	char	*disp_ptr ;

static	char	disp_data;
static	char	*disp_ptr = &disp_data;
/* ------------------------------------------------- */
static int	replaceableFlag = 0;
static	u_char	clearSubsFlag=0x00,
		clearCardFlag=0x00;
static int	waitClearSubsConfirmCounter = 0,
		waitClearCardConfirmCounter = 0;
			
/* ------------------------------------------------- */
static DWORD	debug_status_id[20] = 
{
		1,3,6,1,4,1,1373,1,1,2,3,1,2,PPS_DEBUG_ID + 2,1
};
static	BYTE debug_status = 1;

static DWORD	debug_ascin_id[20] = 
{
		1,3,6,1,4,1,1373,1,1,2,3,1,2,PPS_DEBUG_ID + 2,3
};

static DWORD	debug_ascout_id[20] = 
{
		1,3,6,1,4,1,1373,1,1,2,3,1,2,PPS_DEBUG_ID + 2,4
};

static DWORD	debug_name_id[20] = 
{
		1,3,6,1,4,1,1373,1,1,2,3,1,2,PPS_DEBUG_ID + 2,2
};
		
static	DWORD	debug_page_title[20] = 		
{
		1,3,6,1,4,1,1373,1,1,2,3,1,2,PPS_DEBUG_ID + 2,1,1
};
static	DWORD	debug_page_line[20] = 		
{
		1,3,6,1,4,1,1373,1,1,2,3,1,2,PPS_DEBUG_ID + 2,1,2,1
};	
const BYTE	PPS_PAGE_POINT = 14;
const BYTE  PPS_LINE_POINT = 15;
const BYTE	PPS_BASE_ID_LEN = 15;
	
static BYTE	title1_p[] = 
{
	"    PPS    Version R9V1_00                              Page 01 \n\r\
    Parameter List\n\r\
                   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n\r\
				   \n\r\
cdrFlag\n\r\
maxRechargeNum\n\r\
rcm ip\n\r\
currencyName\n\r\
srfNumber0\n\r\
srfNumber1\n\r\
reachable0\n\r\
reachable1\n\r\
hotline\n\r\
inquiryNumber\n\r\
directRecharge\n\r\
creditRecNumber\n\r\
3rdPartyRecNumber\n\r\
CustomerCare\n\r\
rechargeNumber1\n\r\
extern IVR\n\r\
MNP flag\n\r\
"
};

static BYTE	title2_p[] = 
{
	"    PPS    Version R9V1_00                                Page 02 \n\r\
    System Status \n\r\
                   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n\r\
				   \n\r\
Master\n\r\
System state\n\r\
Data source\n\r\
Init progress\n\r\
Master IP\n\r\
Peer IP\n\r\
peer_state\n\r\
Process used\n\r\
\n\r\
Ro&Gy used\n\r\
Define\n\r\
  State:0=NORMAL,1=START,2=INIT,3=SUSPEND,4=SHUTDOWN\n\r\
  Data source:0=NO DATA,1=PEER SERVER,2=LOCAL HD,3=OMC HD\n\r\
"
};

static BYTE	title3_p[] = 
{
	"    PPS    Version R9V1_00                            Page 03    \n\r\
    User info\n\r\
                   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n\r\
				   \n\r\
system capability\n\r\
license user\n\r\
user number\n\r\
free index\n\r\
peer user number\n\r\
\n\r\
send sync\n\r\
recv sync\n\r\
\n\r\
index status\n\r\
get subs. result\n\r\
"
};

static BYTE	title4_p[] = 
{
	"    PPS    Version R9V1_00                             Page 04    \n\r\
    Prepaid card info\n\r\
                   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n\r\
				   \n\r\
card number\n\r\
free card mem\n\r\
card prefix[00-03]\n\r\
card prefix[04-07]\n\r\
card prefix[08-11]\n\r\
card prefix[12-15]\n\r\
card prefix[16-19]\n\r\
card prefix[20-23]\n\r\
card prefix[24-27]\n\r\
card prefix[28-31]\n\r\
card prefix[32-35]\n\r\
card prefix[36-39]\n\r\
card prefix[40-43]\n\r\
card prefix[44-47]\n\r\
card prefix[48-51]\n\r\
card prefix[52-55]\n\r\
card prefix[56-59]\n\r\
card prefix[60-63]\n\r\
"
};

static BYTE	title5_p[] = 
{
	"    PPS    Version R9V1_00                            Page 05  \n\r\
    Message statistics\n\r\
                   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n\r\
                   Send/MO     Received/MT\n\r\
CapOpen\n\r\
InitialDP\n\r\
RequestBCSM\n\r\
ApplyCharging\n\r\
Continue\n\r\
ActivityTest\n\r\
ChargingReport\n\r\
ReportBCSM\n\r\
Answered\n\r\
Disconnect\n\r\
ReleaseCall\n\r\
Inquire\n\r\
Recharge\n\r\
ReadCard\n\r\
ReadCardRsp\n\r\
WriteCard\n\r\
WriteCardRsp\n\r\
PlayAnnounce\n\r\
SendAccInfo\n\r\
SendAccInfoAck\n\r\
"
};

static BYTE	title6_p[] = 
{
	"    PPS    Version R9V1_00                            Page 06  \n\r\
    Message statistics SMS\n\r\
                   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n\r\
                   Send        Received\n\r\
InitialDP_SMS\n\r\
Request_SMS\n\r\
Continue_SMS\n\r\
Report_SMS\n\r\
Release_SMS\n\r\
\n\r\
Conference\n\r\
\n\r\
ThirdParty\n\r\
\n\r\
Cnt2Resource\n\r\
PlayAnn\n\r\
EstTmpCnt\n\r\
AstInst\n\r\
PCUI\n\r\
PCUI_res\n\r\
SpeResRpt\n\r\
DiscntFwdCon\n\r\
smInquiry\n\r\
smRecharge\n\r\
"
};

static BYTE	title7_p[] = 
{
	"    PPS    Version  R9V1_00                             Page 07    \n\r\
    Measurement\n\r\
                   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n\r\
\n\r\
Watch Dog(00-16)\n\r\
Watch Dog(17-31)\n\r\
Watch Dog(32-47)\n\r\
Watch Dog(48-63)\n\r\
\n\r\
update req\n\r\
update ok\n\r\
update error\n\r\
update used\n\r\
update expired\n\r\
update sys\n\r\
"
};

static BYTE	title8_p[] = 
{
	"    PPS    Version  R9V1_00                             Page 08    \n\r\
    USSD statistics\n\r\
                   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n\r\
		   Amount      Duration    Amount      Duation\n\r\
Date\n\r\
Key1/2\n\r\
Key3/4\n\r\
Other/Total\n\r\
\n\r\
Date\n\r\
Key1/2\n\r\
Key3/4\n\r\
Other/Total\n\r\
\n\r\
Date\n\r\
Key1/2\n\r\
Key3/4\n\r\
Other/Total\n\r\
\n\r\
Date\n\r\
Key1/2\n\r\
Key3/4\n\r\
Other/Total\n\r\
\n\r\
"
};

static BYTE	title9_p[] = 
{
	"    PPS    Version  R9V1_00                             Page 08    \n\r\
    USSD statistics\n\r\
                   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n\r\
		   Amount      Duration    Amount      Duation\n\r\
Date\n\r\
Key1/2\n\r\
Key3/4\n\r\
Other/Total\n\r\
\n\r\
Date\n\r\
Key1/2\n\r\
Key3/4\n\r\
Other/Total\n\r\
\n\r\
Date\n\r\
Key1/2\n\r\
Key3/4\n\r\
Other/Total\n\r\
\n\r\
"
};

static char help_menu[]={
"PPS log help page\n\r\
=====================================================================\n\r\
| Command           |  Remark                                       |\n\r\
=====================================================================\n\r\
| help              |  help page                                    |\n\r\
| log all           |  all message will be displayed                |\n\r\
| log none          |  no message will be displayed                 |\n\r\
| log error         |  display error procedures and messages        |\n\r\
| log message       |  display all message(cap,vps)                 |\n\r\
| log procedure     |  only display process , no message presented  |\n\r\
| log msisdn=       |  only msisdn(xxx) concerned messages displayed|\n\r\
| log vps           |  only vps message displayed                   |\n\r\
| list tariff LAC=  |  display current tariff with LAC=xxx(0-15)    |\n\r\
| list tariff plan LAC= |  display tariff plan                      |\n\r\
| tariff msisdn=    |  list tariff with msisdn=xxx                  |\n\r\
| list subs segno=  |  list all subs. data of segment=xxx(0-999)    |\n\r\
| list card segno=  |  list all prepaid cards of segment=xxx(0-1999)|\n\r\
| clear sms table   |  clear SMS table                              |\n\r\
| load tariff   	|  load tariff file from tmp                    |\n\r\
| tariff take effect|  tariff take effect now                    	|\n\r\
| save param        |                                               |\n\r\
=====================================================================\n\r\
"
};


static PPSParamStruct defaultPARAM;
/*={
{0x0a,0x91,0x25,0x25,0x96,0x00,0x06,0xFF,0xFF,0xFF},
{0x0a,0x91,0x25,0x25,0x96,0x00,0x04,0xFF,0xFF,0xFF},
{0x0a,0x91,0x25,0x25,0x96,0x00,0x09,0xFF,0xFF,0xFF},
0x01,
0x05,
0x00,
0x00000000,
0x00,
1000000,
1000,
0x00,
100,
"$",
0x00,
0x00,
};
*/
int SetDefaultParam()
{
   memcpy(&ppsParam,&defaultPARAM,sizeof(ppsParam));
   return 1;
}

void CalcSumOfParameter()
{
   u_char *ptr = (u_char *)&ppsParam;
   int  i;

	PutLogFunID("CalcSumOfParameter");

   sumOfParameter = 0;
   
   for(i=0;i<sizeof(ppsParam);i++)
   {
      sumOfParameter +=*(ptr+i);
   }
}

int LoadPPSParam()
{
	FILE		*fp=NULL;
	char		buf[128];
	char		confFile[128] = "./conf/ppsConf.txt";

	PutLogFunID("LoadPPSParam");
	
	fp = fopen(confFile,"r");
	if(fp == NULL)
	{
		SetDefaultParam();
	}
	else
	{			
		fread(&ppsParam,sizeof(PPSParamStruct),1,fp);						
		fclose(fp);
	}
			
	if(ppsParam.serviceControl.vms_allways_go>1)
			ppsParam.serviceControl.vms_allways_go = 0;

	if(ppsParam.serviceControl.licenseduser<10) 
		ppsParam.serviceControl.licenseduser = 10;
	if(ppsParam.serviceControl.licenseduser>systemCapability) 
		ppsParam.serviceControl.licenseduser = systemCapability;
	sprintf(buf,"%08u",ppsParam.serviceControl.licenseduser);
	AsciiToBcd(debugmib.measurement.licensed_user,buf,8);

       if(ppsParam.max_call_duration < 1000 )
       	ppsParam.max_call_duration = MAX_PREPAID_CALL_DURATION;
       if(ppsParam.serviceControl.max_call_duration <= 0)
	   	ppsParam.serviceControl.max_call_duration = 1;
	if(ppsParam.serviceControl.answer_timer<40)
	  	ppsParam.serviceControl.answer_timer = 40;

	if(ppsParam.max_quota_per_request<=0)
		ppsParam.max_quota_per_request = 4;
	if(ppsParam.data_quota_as_postpaid<=0)
		ppsParam.data_quota_as_postpaid = 2;

      if(ppsParam.param_ver != 1)
       {
           int i;
		   
           ppsParam.param_ver = 1;
		   
           for(i=0;i<MaxCosTableItem;i++)
           {
           	ppsParam.ppsTables.cosTable[i].serviceControl[0].serviceNumberFlag = 0xFFFF;
		ppsParam.ppsTables.cosTable[i].serviceControl[0].smsServiceFlag = 0xFFFF;
		ppsParam.ppsTables.cosTable[i].serviceControl[0].ussdServiceFlag = 0xFFFF;
           }

	    SavePPSParam();
		
       }

#ifdef _ZED_NO_CALLER_NUM_AND_EXPIRY_DATE_
    if (ppsParam.scf_work_as_adaptor)
    {
      read_yx_ussd_param();
    }
#endif

	CalcSumOfParameter();   

	return 1;
}

int SavePPSParam()
{
	FILE *fp=NULL;
	PutLogFunID("SavePPSParam");   
	
	fp = fopen("./conf/ppsConf.txt","w");
	if(fp != NULL)
	{
		fwrite(&ppsParam,sizeof(ppsParam),1,fp);
		fclose(fp);
	}
   	
    CalcSumOfParameter();   
    return 1;
}


void checkLicense(char *lsn)
{
	char buf[32],sn[32];
	u_char bufdecrypt[32],raw_buf[32];
	int	value;

	AsciiToBcd(raw_buf,lsn,16);	
	DesCrypt(bufdecrypt,raw_buf,NULL,1);
	BcdToAscii(buf,bufdecrypt,16);
	memcpy(sn,buf,8);
	DebugMsg(debugmib.display.asciiOut,"raw value:%s - %s",lsn,buf);
	DebugMsg(debugmib.display.asciiOut,"SN: %s",sn);
	DebugMsg(debugmib.display.asciiOut,	"SSN: %d",bufdecrypt[4]);
	value = atoi(&buf[10]);
	DebugMsg(debugmib.display.asciiOut,"license: %d",value);
}

int PPS_set_license(unsigned char *pdata)
{
	u_char	bufdecrypt[128]="";
	char	buf[128];
	int	value;
	
	DesCrypt(bufdecrypt,pdata,NULL,1);
	BcdToAscii(buf,bufdecrypt,16);
	if(strncasecmp(buf,serialNumber,8))
	{
		DebugMsg(debugmib.display.asciiOut,
			"Error license!");
		return 0;
	}
	if(bufdecrypt[4] != 147)
	{
		DebugMsg(debugmib.display.asciiOut,
			"Error license!");
		return 0;
	}
	if(bufdecrypt[5] != 0)
	{
		DebugMsg(debugmib.display.asciiOut,"Error license type: %d, should be 0",
			bufdecrypt[5]);
		return 0;
	}
	value = bufdecrypt[6]*256+bufdecrypt[7];
	value = value * 100;
	
	if(value>systemCapability) 
	{
		DebugMsg(debugmib.display.asciiOut,"Error license:%d>capability:%d!",
			value,systemCapability);
		return 0;
	}
	if(value<ppsParam.serviceControl.licenseduser)
	{
		DebugMsg(debugmib.display.asciiOut,"Error license: %d<old one:%d!",
			value,ppsParam.serviceControl.licenseduser);
		return 0;
	}
	
	ppsParam.serviceControl.licenseduser = value; 
	sprintf(buf,"%08u",ppsParam.serviceControl.licenseduser);
	AsciiToBcd(debugmib.measurement.licensed_user,buf,8);
	SavePPSParam();
	DebugMsg(debugmib.display.asciiOut,"Succeed set license to %d!",value);
	return 1;
}

static  BYTE saveCommandFlag = 0;
static int setSaveCmdFlag(BYTE flag,BYTE *failReason)
{
	if(!saveCommandFlag)
		saveCommandFlag = flag;
	else
	{
		sprintf((char *)failReason,"pps is saving,flag is:%d",flag);
		pps_print_error(0,"pps is saving,flag is:%d",flag);
		return -2;
	}
	return 1;
}

static int retSaveCmdFlag()
{
	saveCommandFlag = 0;
	return 0;
}

static BYTE getSaveCmdFlag()
{
	return saveCommandFlag;
}

void saveCommand()
{
	switch(getSaveCmdFlag())
	{
		case 1:
			SaveSubsData();
			retSaveCmdFlag();
		break;					
		case 2:
			SaveTariffPlan();
			retSaveCmdFlag();
		break;
		case 3:
			SaveRechargeCard();
			retSaveCmdFlag();
		break;
		case 4:
			if(ppsBackupRamDataToFile())
				retSaveCmdFlag();			
		break;
		default:
			retSaveCmdFlag();
		break;
	} 	
}

int snmpSetPPS(BYTE oidLen, DWORD *oid,  BYTE *pdata, u_short dataLen)
{
	int    oid0,oid1,oid2,loop;
	char   buf[BUFSIZE];
	//struct tm tmnow;	
	int	   iValue=0;

	PutLogFunID("snmpSetPPS");

	oid0 = oid[11];
	switch(oid0)
	{
		case PPS_HEARTBEAT: 
			return dataLen;			
		break;
		case PPS_PARAMETER:
		break;
		case PPS_CSTA:
			return dataLen;
		break;
		case PPS_CDR: 
			return SetCDRParam(oidLen, oid, pdata, dataLen);
		break;
		case PPS_USERDATA: 
		return SetDatabase(oidLen, oid, pdata, dataLen);
		break;
	}

   if(oid0 != PPS_PARAMETER )  /* only parameter supported */
     return -1;

   oid1 = oid[12];
   oid2 = oid[13];
   
	switch(oid1)
	{
		case ServiceControl: /* parameter */
			ppsSetParameterAutoSaveFlag();
			switch(oid2)
			{			
				case 1: /* license */
					return -1;		
				break;
				case 2: /* cdr flag */
					ppsParam.serviceControl.cdrFlag = pdata[0];									
				break;
				case 3: /* recharge failure threshold*/																
					iValue = PPS_SET_INTEGER(pdata,dataLen);
					if(iValue<0 || iValue>255)
					{
						sprintf((char *)pdata,"Error value, should be 0-255");
						return -2;
					}
					ppsParam.serviceControl.maxRechargeFailedNum = iValue;
				break;
				case 4: /*  max balance */						
					for(loop = 0,ppsParam.serviceControl.maxBalance = 0;loop < dataLen;loop++)
					{
						ppsParam.serviceControl.maxBalance = ppsParam.serviceControl.maxBalance << 8;
						ppsParam.serviceControl.maxBalance	+= pdata[loop];				
					}	
				break;
				case 5: /* currency symbol */
					memset(ppsParam.serviceControl.currencyName,0,8);
					memcpy(ppsParam.serviceControl.currencyName,pdata,dataLen);
				break;
				case 6:/* cos option */
					ppsParam.serviceControl.cos_group_flag = pdata[0];					
				break;	
				case 7:
					ppsParam.serviceControl.support_mnp_flag = pdata[0];						
				break;
				case 8:
					ppsParam.serviceControl.callingCardVadilityDays = pdata[0];
				break;						
				case 9:
				case 10:
				case 11:
				case 12:
				case 13:
					ppsParam.serviceControl.languageSmEncodeType[oid2-9]= pdata[0];						
				break;
				case 14:
					ppsParam.serviceControl.external_vc = pdata[0];
				break;
				case 15:
					ppsParam.serviceControl.charge_by_lac_flag = pdata[0];
				break;
				case 16:
					ppsParam.serviceControl.not_change_expiry_date_when_transfer = pdata[0];
				break;
				case 17:
					ppsParam.serviceControl.ussd_callback_service_key = pdata[0];
				break;
				case 18:
					memcpy(ppsParam.serviceControl.ucbc_number,pdata,dataLen);
					ppsParam.serviceControl.ucbc_number[dataLen] = 0;					
				break;
				default:
					return -1;
				break;	
			}			
		break;
		case COSID: 
			return 0;
		break;
		case SrfParameter:
			return 0;
		break;
		case RouteVMS: 
			return SetCRDParam(oidLen,oid,pdata,dataLen);
		break;
		case PPSOID_FileOperation:					
			if(peer_server_in_init)
			{
				sprintf((char *)pdata,"Peer server is initializing , please wait!");
				return -1;
			}
			switch(oid2)
			{
				case 1: /* subs. data */
					if(pdata[0] != 0x01)
					{
						return 0;  /* not import */
					}
					if(dataLen != 0x07) 
					{
						return 0;
					}
					if(ppsParam.fileOperation.uploadUserData == 2)
						return 0;
					ppsParam.fileOperation.uploadUserData = pdata[0];
					memcpy(uploadUserStatus,&pdata[1],6);
					SetLoadFileFlag(1); /* user data */
				break;
				case 2: /* recharge card data */
					if(ppsParam.fileOperation.uploadPrepaidCard == 2)
						return 0;
					ppsParam.fileOperation.uploadPrepaidCard = pdata[0];
					memcpy(uploadPrecardStatus,&pdata[1],6);
					SetLoadFileFlag(2); /* prepaid card */
				break;
				case 3: /* tariff plan*/
					if(pdata[0] == 1){
						ppsParam.fileOperation.uploadTariffPlan = pdata[0];
						ppsParam.fileOperation.uploadTariffPlan = 0x02;
	
						SetLoadFileFlag(3); /* tariff data */
					}else if(pdata[0]==0){
						ppsParam.fileOperation.nextTariffStartTime = 0;
					}
				break;
				case 4: /* tariff time*/
					memset(buf,0,sizeof(buf));
					memcpy(buf,pdata,dataLen);
					ppsParam.fileOperation.nextTariffStartTime = MakeTime(NULL,buf);
				break;
				case 5:
					
					return setSaveCmdFlag(pdata[0],pdata);
				break;
			}
		break;						
		case Status: /* system action */
			if(oid2 == 1)
			{
				debugmib.display.sysState = pdata[0];
			}
			else if(oid2 == 2)
			{
				if(pdata[0]==1){
					SavePPSParam();
				}else if(pdata[0]==2){
					pps_exit(0);
				}
			}								
		break;			
		default:
			DebugMsgDelimiter();
			DebugMsg(debugmib.display.asciiOut,"Get pps data error: error oid[12]:%d",oid[12]);
			return 0;
		break;
	}
	return dataLen;   
}
 

 
 
int snmpGetPPS(BYTE oidLen, DWORD *oid, BYTE *pdata, BYTE *vartype)
{

	int oid0,oid1,oid2,vallen=1,instance;
	char  buf[BUFSIZE];
	
	PutLogFunID("snmpGetPPS");

	oid0 = oid[11];
	oid1 = oid[12];

	*vartype = 0x04;
	oid2 = oid[13];

	switch(oid0)
	{
		case PPS_HEARTBEAT:
			switch(oid1)
			{
				case 0x04:
				return ReadDailyReportRequest(oidLen,oid,pdata,vartype);
				break;
				default:
				break;
			}
		break;	
		case PPS_PARAMETER: 		
		switch(oid1)
		{
			case ServiceControl:
				switch(oid2)
				{				
					case 1: /* license */
						*vartype = INTEGER;
						vallen = 4;
						pdata[0] = ppsParam.serviceControl.licenseduser>>24;
						pdata[1] = ppsParam.serviceControl.licenseduser>>16;
						pdata[2] = ppsParam.serviceControl.licenseduser>>8;
						pdata[3] = ppsParam.serviceControl.licenseduser;					
					break;
					case 2: /* cdr flag */
						*vartype = INTEGER;
						pdata[0] = ppsParam.serviceControl.cdrFlag;										
					break;
					case 3: /* recharge failure threshold*/	
						*vartype = INTEGER;
						vallen = 1;						
						pdata[0] = ppsParam.serviceControl.maxRechargeFailedNum;			    										
					break;
					case 4: /*  max balance */
						*vartype = INTEGER;
						vallen = 4;
						pdata[0] = ppsParam.serviceControl.maxBalance>>24;
						pdata[1] = ppsParam.serviceControl.maxBalance>>16;
						pdata[2] = ppsParam.serviceControl.maxBalance>>8;
						pdata[3] = ppsParam.serviceControl.maxBalance;	
					break;
					case 5: /* currency symbol */
						vallen = strlen((char *)ppsParam.serviceControl.currencyName);
						memcpy(pdata,ppsParam.serviceControl.currencyName,vallen);
					break;
					case 6:/* cos option */
						*vartype = INTEGER;
						vallen = 1;
						pdata[0] = ppsParam.serviceControl.cos_group_flag;
					break;	
					case 7:
						*vartype = INTEGER;
						pdata[0] = ppsParam.serviceControl.support_mnp_flag;
						vallen = 1;
					break;
					case 8:
						*vartype = INTEGER;
						pdata[0] = ppsParam.serviceControl.callingCardVadilityDays;
						vallen = 1;						
					break;						
					case 9:
					case 10:
					case 11:
					case 12:
					case 13:
						*vartype = INTEGER;
						pdata[0] = ppsParam.serviceControl.languageSmEncodeType[oid2-9];
						vallen = 1;		
					break;
					case 14:
						*vartype = INTEGER;
						pdata[0] = ppsParam.serviceControl.external_vc;
						vallen = 1;	
					break;
					case 15:
						*vartype = INTEGER;
						pdata[0] = ppsParam.serviceControl.charge_by_lac_flag;
						vallen = 1;	
					break;
					case 16:
						*vartype = INTEGER;
						pdata[0] = ppsParam.serviceControl.not_change_expiry_date_when_transfer;
						vallen = 1;
					break;
					case 17:
						*vartype = INTEGER;
						pdata[0] = ppsParam.serviceControl.ussd_callback_service_key;
						vallen = 1;
					break;			
					case 18:
						vallen = strlen((char *)ppsParam.serviceControl.ucbc_number);
						memcpy(pdata,ppsParam.serviceControl.ucbc_number,vallen);						
					break;					
					default:
						return 0;
					break;	
				}			
			break;			
			case COSID: 
				return 0;
			break;
			case SrfParameter:
				return 0;
			break;
			case RouteVMS: 
				return GetCRDParam(oidLen,oid,pdata,vartype);
			break;
			case PPSOID_FileOperation:
				switch(oid2)
				{
					case 1: /* import subs data */
					pdata[0] = ppsParam.fileOperation.uploadUserData;
					memcpy(&pdata[1],uploadUserStatus,6);				
					*vartype = 0x44;
					vallen = 7;
					break;
					case 2: /* import recharge card data */
					pdata[0] = ppsParam.fileOperation.uploadPrepaidCard;
					memcpy(&pdata[1],uploadPrecardStatus,6);
					*vartype = 0x44;
					vallen = 7;
					break;
					case 3: /* tariff plan */
					pdata[0] = ppsParam.fileOperation.uploadTariffPlan;
					*vartype = 0x02;
					break;
					case 4: /* time for new tariff plan to take place */
					AsciiTime(buf,ppsParam.fileOperation.nextTariffStartTime);
					AsciiToBcd(pdata,buf,14);
					vallen = 7;
					break;
				}
			break;									
			case Status: /* system action */
				if(oid2 == 1)
				{
					vallen = 1;
					*vartype = INTEGER;
					pdata[0] = debugmib.display.sysState;
				}
			break;			
			case PPSOID_InitRequest:			
				return PeerServerRamInitRequest(oidLen,oid,pdata,vartype);
			break;
			default:
			vallen = -1;
			break;
		}
		break;
		case PPS_CSTA: /* csta */
			if(oidLen<13) return -1;
			instance = oid[12];		
			if(instance<0 || instance >= MAX_CSTA_LEN) return -1;
			vallen = GetCSTA_Record(instance,pdata);			
		break;
		case PPS_CDR: /* cdr */
			if(oid1 == 1 || oid1 == 2)
			return GetCDRParam(oid1,pdata);
			if(oidLen<14) return -1;
			instance = oid[13];
			vallen = GetCDR_Record(instance,pdata);  /* oid2=squence */			
		break;	
		case PPS_USERDATA:			
			return GetDatabase(oidLen,oid,pdata,vartype);									
		break;
		case 0x09:    /* status */
		vallen = 1;
		*vartype = INTEGER;
		pdata[0] = debugmib.display.sysState;
		break;
		default:
		vallen = -1;
		break;
	}   
	return vallen;
}

void snmpTrapPPS(BYTE oidLen, DWORD *oid, BYTE *pdata, u_short dataLen,snmp_addr *addr)
{
   	u_char peerParameterSum;

	PutLogFunID("snmpTrapPPS");

   	if(oid[8]==4)
   	{     /* OMC */
	   	unsigned char sNumber[5];
	   	AsciiToBcd(sNumber,serialNumber,8);
	   	if(memcmp(pdata+39,sNumber,4) !=0)
	   		return ;
		 switch(pdata[0])
		 {
		 	case 0:
				primary_data_server = addr->remote_ip;
			break;
			case 1:
				second_data_server = addr->remote_ip;
			break;
		 }
		 
		 return ;
   	}
	
   if(oid[10] == PPSOID)
   {/* pps peer server */
	 switch(oid[11])
	 {
		 case PPS_HEARTBEAT: /* heartbeat */	 
			 pps_peer_hb_flag = 1;
			 if(addr->remote_ip !=LOCAL_IP && pdata[1]  != pps_get_subid())
			 {
			   memcpy(peer_version_id,&pdata[23],3);
			   peer_server_ip = addr->remote_ip;
			 }
			 peerParameterSum = pdata[27];
			 if(peerParameterSum != sumOfParameter)
			 {
			  // alarm_code[0] = 0;
			  // alarm_code[1] = 5;
			 }
			 debugmib.display.peer_state = 0x10 | pdata[26];
			 /* a method to check peer server hb, and state , 
				as normal state = 0, so 0x10 is OR to the state */
			 debugmib.measurement.peer_usernumber =
				 pdata[35] * 256 * 65536 +
				 pdata[36] * 65536 +
				 pdata[37] * 256+
				 pdata[38];

			if((debugmib.measurement.peer_usernumber >
				debugmib.measurement.usernumber) && masterFlag)
			{
				debugmib.display.masterIP = addr->remote_ip;
				masterFlag = 0;
			}
			else if((debugmib.measurement.peer_usernumber <
				debugmib.measurement.usernumber) && !masterFlag)
			{
				debugmib.display.masterIP = LOCAL_IP;
				masterFlag = 1;
			}
			debugmib.display.peer_state_counter = 0;

#ifdef	CONVERT_16DIGIT_CARD
			if(memcmp(peer_version_id,version_id,3))
				stopReceivePeriodicSyncFlag = 1;	
			else
				stopReceivePeriodicSyncFlag = 0;				
#endif			 
		 break;
		 case PPS_CSTA: /* csta */
			GotSyncDailyReport(oid,pdata,dataLen);
		 break;
		 default:
		 break;
	 }
   }
   if(oid[10] == 26)
   {
       ppsParam.rcm_ip = addr->remote_ip;
   }
}

/*----------------CDR functions ---------------------------------------*/

int SetCDRParam(BYTE oidLen, DWORD *oid,  BYTE *pdata, u_short datalen)
{
	int subOID;
	int pWriteSub;

	PutLogFunID("SetCDRParam");

	subOID = oid[12];

	if(datalen == 1)
	{
		pWriteSub = pdata[0];
	}
	else if(datalen == 2 )
	{
		pWriteSub = pdata[1]  +pdata[0]* 256;
	}
	else if(datalen == 3)
	{
		pWriteSub = pdata[2] + (pdata[1] + pdata[0]*256)*65536;
	}
	else if(datalen ==4)
	{
		pWriteSub = (pdata[3] + pdata[2]*256) + (pdata[1] + pdata[0]*256)*65536;
	}
	else
	{
		return -1;
	}

	switch(subOID)
	{
	   case 0x01:  /* head */
	    cdr_head_ptr = pWriteSub;
		pps_cdr_buffer.pWriteSub = pWriteSub % MAX_CDR_LEN;
	   break;
	   case 0x02:  /* tail */
	     cdr_tail_ptr = pWriteSub;
	   break;
	   default:
	     return -1;
	   break;
	}
	return datalen;
}

int GetCDRParam(DWORD oid, BYTE *pdata)
{

    int pWriteSub;

	PutLogFunID("GetCDRParam");

	switch(oid)
	{
	   case 0x01:  /* head */
	     pWriteSub = cdr_head_ptr ;
	   break;
	   case 0x02:  /* tail */
	     pWriteSub = cdr_tail_ptr ;
	   break;
	   default:
	     return -1;
	   break;
	}
	pdata[0] = pWriteSub>>24;
	pdata[1] = pWriteSub>>16;
	pdata[2] = pWriteSub>>8;
	pdata[3] = pWriteSub;
	return 4;
}

int GetCDR_Record(int instance,BYTE *pdata)
{
	int	pReadSub,tmpLen = 0;
  
	PutLogFunID("GetCDR_Record");

	pReadSub = instance % MAX_CDR_LEN;
	if(pReadSub == cdr_head_ptr)
		return 0;
   	tmpLen = pps_cdr_buffer.cdrMsg[pReadSub][0]%254;
   	memcpy(pdata,&pps_cdr_buffer.cdrMsg[pReadSub][1],tmpLen);
   	return tmpLen;
}

/*
int GetCDR_Record(int instance,BYTE *pdata)
{
   int    recordType=0,pReadSub;
   _tocdr_report *pcdr = NULL;

	PutLogFunID("GetCDR_Record");

   pReadSub = instance % MAX_CDR_LEN;
   if(pReadSub == cdr_head_ptr)
	return 0;
   pcdr = &pps_cdr_buffer.cdr_record[pReadSub];
   if(pcdr->caller[0] == 0x00) return 0;
   
   recordType = pcdr->mtype;
   switch(recordType)
   {
      case MO_CDR_TYPE:
        return GetMO_CDR(pcdr, pdata);
	  break;
	  case MT_CDR_TYPE:
	    return GetMT_CDR(pcdr, pdata);
	  break;
	  default:
	  break;
   }
   return 0;
}
*/

int GetMO_CDR(_tocdr_report *pcdr, BYTE *pdata)
{
   int           len = 0, recordType=0;
   ASN_BUF       *asnbuf, asnbuf1;	
   u_char	buf[BUFSIZE]={""},charge[8],balance[8],duration[8];
   
	PutLogFunID("GetMO_CDR");

   asnbuf = &asnbuf1;
   asn_encode(pdata,asnbuf);

   recordType = pcdr->mtype;
   len = add_int("2-1",pcdr->squence,0x00,asnbuf);            
   len = add_tlv("0-2.0",1,(u_char *)&recordType,0x80,asnbuf);  /* record type */
   len = add_tlv("0-2.1",8,pcdr->imsi,0x80,asnbuf);   /* served IMSI */
   len = add_tlv("0-2.3",9,pcdr->msisdn,0x80,asnbuf); /* served MSISDN */
   len = add_tlv("0-2.4",9,pcdr->caller,0x80,asnbuf); /* calling number */
   len = add_tlv("0-2.5",12,pcdr->called,0x80,asnbuf); /* called number	 */
   len = add_tlv("0-2.9",10,ppsParam.ppsNumber,0x80,asnbuf); /* Recording Entity */

   CharToTimeStamp(buf, (char *)pcdr->seizuretime);
   len = add_tlv("0-2.22",8,buf,0x80,asnbuf);     /* seizure timestamp */
   CharToTimeStamp(buf, (char *)pcdr->answertime);
   len = add_tlv("0-2.23",8,buf,0x80,asnbuf);     /* answer timestamp*/
   CharToTimeStamp(buf, (char *)pcdr->releasetime);   
   len = add_tlv("0-2.24",8,buf,0x80,asnbuf);     /* release timestamp*/
   duration[0] = pcdr->duration[3];
   duration[1] = pcdr->duration[2];
   duration[2] = pcdr->duration[1];
   duration[3] = pcdr->duration[0];
   len = add_tlv("0-2.25",4,duration,0x80,asnbuf);/*call duration*/
   len = add_tlv("0-2.30",1,pcdr->tcos,0x80,asnbuf);      /* cause for term*/
   len = add_tlv("0-2.128",1,pcdr->type,0x80,asnbuf);          /* call type */
   charge[0] = pcdr->charge[3];
   charge[1] = pcdr->charge[2];
   charge[2] = pcdr->charge[1];
   charge[3] = pcdr->charge[0];
   len = add_tlv("0-2.129",4,charge,0x80,asnbuf);          /* charge  */
   balance[0] = pcdr->balance[3];
   balance[1] = pcdr->balance[2];
   balance[2] = pcdr->balance[1];
   balance[3] = pcdr->balance[0];
   len = add_tlv("0-2.130",4,balance,0x80,asnbuf);          /* balance */
   return len;
}


int GetMT_CDR(_tocdr_report *pcdr, BYTE *pdata)
{
   int           len = 0, recordType=0;
   ASN_BUF       *asnbuf, asnbuf1;

   u_char			 buf[BUFSIZE]={""},charge[8],balance[8],duration[8];

	PutLogFunID("GetMT_CDR");

   asnbuf = &asnbuf1;
   asn_encode(pdata,asnbuf);

   recordType = pcdr->mtype;
   len = add_int("2-1",pcdr->squence,0x00,asnbuf);            
   len = add_tlv("1-2.0",1,(u_char *)&recordType,0x80,asnbuf);  /* record type */
   len = add_tlv("1-2.1",8,pcdr->imsi,0x80,asnbuf);   /* served IMSI */
   len = add_tlv("1-2.3",9,pcdr->called,0x80,asnbuf); /* called number	 */
   len = add_tlv("1-2.4",9,pcdr->caller,0x80,asnbuf); /* calling number */
   len = add_tlv("1-2.5",9,pcdr->called,0x80,asnbuf); /* connected number */
   len = add_tlv("1-2.6",10,ppsParam.ppsNumber,0x80,asnbuf); /* Recording Entity */

   CharToTimeStamp(buf, (char *)pcdr->seizuretime);
   len = add_tlv("1-2.19",8,buf,0x80,asnbuf);     /* seizure timestamp */
   CharToTimeStamp(buf, (char *)pcdr->answertime);
   len = add_tlv("1-2.20",8,buf,0x80,asnbuf);     /* answer timestamp*/
   CharToTimeStamp(buf, (char *)pcdr->releasetime);   
   len = add_tlv("1-2.21",8,buf,0x80,asnbuf);     /* release timestamp*/
   duration[0] = pcdr->duration[3];
   duration[1] = pcdr->duration[2];
   duration[2] = pcdr->duration[1];
   duration[3] = pcdr->duration[0];
   len = add_tlv("1-2.22",4,duration,0x80,asnbuf);/*call duration*/
   len = add_tlv("1-2.27",1,pcdr->tcos,0x80,asnbuf);      /* cause for term*/
   len = add_tlv("1-2.128",1,pcdr->type,0x80,asnbuf);          /* call type */
   charge[0] = pcdr->charge[3];
   charge[1] = pcdr->charge[2];
   charge[2] = pcdr->charge[1];
   charge[3] = pcdr->charge[0];
   len = add_tlv("1-2.129",4,charge,0x80,asnbuf);          /* charge  */
   balance[0] = pcdr->balance[3];
   balance[1] = pcdr->balance[2];
   balance[2] = pcdr->balance[1];
   balance[3] = pcdr->balance[0];
   len = add_tlv("1-2.130",4,balance,0x80,asnbuf);          /* balance */
   return len;
}


int GetCSTA_Record(int instance, BYTE *pdata)
{
    int loop,totalsum,start;
    _pps_csta_record *pcsta = &pps_csta_buffer.csta_record[instance];

	PutLogFunID("GetCSTA_Record");

    pdata[0] = pcsta->timestamp>>24;
    pdata[1] = pcsta->timestamp>>16;
    pdata[2] = pcsta->timestamp>>8;
    pdata[3] = pcsta->timestamp;

	start = 4;
	totalsum = 0;
    for(loop=0;loop<PPS_CSTA_TYPES;loop++) /* short message not supported by 2003-03-19 */
	{
	  pdata[start++] = pcsta->csta_type[loop].number>>24;
	  pdata[start++] = pcsta->csta_type[loop].number>>16;
	  pdata[start++] = pcsta->csta_type[loop].number>>8;
      pdata[start++] = pcsta->csta_type[loop].number;
	  totalsum += pcsta->csta_type[loop].number;
    }
	totalsum = 0;
    for(loop=0;loop<PPS_CSTA_TYPES;loop++)
	{
	      pdata[start++] = pcsta->csta_type[loop].charge>>24;
	      pdata[start++] = pcsta->csta_type[loop].charge>>16;
	      pdata[start++] = pcsta->csta_type[loop].charge>>8;
	      pdata[start++] = pcsta->csta_type[loop].charge;
		  totalsum += pcsta->csta_type[loop].charge;
    }

	totalsum = 0;
    for(loop=0;loop<PPS_CSTA_TYPES;loop++)
	{
	      pdata[start++] = pcsta->csta_type[loop].duration>>24;
	      pdata[start++] = pcsta->csta_type[loop].duration>>16;
	      pdata[start++] = pcsta->csta_type[loop].duration>>8;
	      pdata[start++] = pcsta->csta_type[loop].duration;
		  totalsum += pcsta->csta_type[loop].duration;
    }	

	pdata[start++] = pcsta->gprsCsta.number>>24;
	pdata[start++] = pcsta->gprsCsta.number>>16;
	pdata[start++] = pcsta->gprsCsta.number>>8;
	pdata[start++] = pcsta->gprsCsta.number;
	
	pdata[start++] = pcsta->gprsCsta.charge>>24;
	pdata[start++] = pcsta->gprsCsta.charge>>16;
	pdata[start++] = pcsta->gprsCsta.charge>>8;
	pdata[start++] = pcsta->gprsCsta.charge;
	
	pdata[start++] = pcsta->gprsCsta.duration>>24;
	pdata[start++] = pcsta->gprsCsta.duration>>16;
	pdata[start++] = pcsta->gprsCsta.duration>>8;
	pdata[start++] = pcsta->gprsCsta.duration;
		
	pdata[start++] = pcsta->gprsCsta.gprsVolume>>24;
	pdata[start++] = pcsta->gprsCsta.gprsVolume>>16;
	pdata[start++] = pcsta->gprsCsta.gprsVolume>>8;
	pdata[start++] = pcsta->gprsCsta.gprsVolume;

	pdata[start++] = pcsta->vasCsta.smsNumber>>24;
	pdata[start++] = pcsta->vasCsta.smsNumber>>16;
	pdata[start++] = pcsta->vasCsta.smsNumber>>8;
	pdata[start++] = pcsta->vasCsta.smsNumber;

	pdata[start++] = pcsta->vasCsta.smsCharge>>24;
	pdata[start++] = pcsta->vasCsta.smsCharge>>16;
	pdata[start++] = pcsta->vasCsta.smsCharge>>8;
	pdata[start++] = pcsta->vasCsta.smsCharge;

	pdata[start++] = pcsta->vasCsta.mmsSendNumber>>24;
	pdata[start++] = pcsta->vasCsta.mmsSendNumber>>16;
	pdata[start++] = pcsta->vasCsta.mmsSendNumber>>8;
	pdata[start++] = pcsta->vasCsta.mmsSendNumber;

	pdata[start++] = pcsta->vasCsta.mmsSendCharge>>24;
	pdata[start++] = pcsta->vasCsta.mmsSendCharge>>16;
	pdata[start++] = pcsta->vasCsta.mmsSendCharge>>8;
	pdata[start++] = pcsta->vasCsta.mmsSendCharge;

	
	pdata[start++] = pcsta->vasCsta.mmsRetrieveNumber>>24;
	pdata[start++] = pcsta->vasCsta.mmsRetrieveNumber>>16;
	pdata[start++] = pcsta->vasCsta.mmsRetrieveNumber>>8;
	pdata[start++] = pcsta->vasCsta.mmsRetrieveNumber;

	pdata[start++] = pcsta->vasCsta.mmsRetrieveCharge>>24;
	pdata[start++] = pcsta->vasCsta.mmsRetrieveCharge>>16;
	pdata[start++] = pcsta->vasCsta.mmsRetrieveCharge>>8;
	pdata[start++] = pcsta->vasCsta.mmsRetrieveCharge;

	pdata[start++] = pcsta->vasCsta.mmsEmailNumber>>24;
	pdata[start++] = pcsta->vasCsta.mmsEmailNumber>>16;
	pdata[start++] = pcsta->vasCsta.mmsEmailNumber>>8;
	pdata[start++] = pcsta->vasCsta.mmsEmailNumber;

	pdata[start++] = pcsta->vasCsta.mmsEmailCharge>>24;
	pdata[start++] = pcsta->vasCsta.mmsEmailCharge>>16;
	pdata[start++] = pcsta->vasCsta.mmsEmailCharge>>8;
	pdata[start++] = pcsta->vasCsta.mmsEmailCharge;
	return start;
}

/*----------------CDR functions ---------------------------------------*/
/* ------------------ debug functions ---------------------------------*/

BYTE	pps_disp_line(BYTE page, BYTE line)
/* return display length,disp data ptr per line */
{
	BYTE	disp_length;
	
	disp_length = 1;
	
	
	switch (page)
	{
		case 1: /* page 1 */
			disp_length = 1;
        	switch (line-2)
			{
				case 0:
				  disp_ptr=(char *)&ppsParam.serviceControl.cdrFlag;
				break;
				case 1:
				  disp_ptr=(char *)&ppsParam.serviceControl.maxRechargeFailedNum;
				break;
				case 2:
				 disp_ptr=(char *)&ppsParam.rcm_ip;
				 disp_length = 4;
				break;
				case 3:
				  disp_length = 8;
				  disp_ptr=(char *)&ppsParam.serviceControl.currencyName;
				break;
				case 4:
				  disp_length = 16;
				  disp_ptr =(char *)&ppsParam.ppsTables.srfNumber[0].number;
				break;
				case 5:
				  disp_length = 16;
				  disp_ptr =(char *)&ppsParam.ppsTables.srfNumber[1].number;
				break;
				case 6:
				  disp_length = 1;
				  disp_ptr =(char *)&ppsParam.ppsTables.srfNumber[0].reachable;
				break;
				case 7:
				  disp_length = 1;
				  disp_ptr =(char *)&ppsParam.ppsTables.srfNumber[1].reachable;
				break;
				case 8:
				  disp_length = 16;
				  disp_ptr =(char *)&ppsParam.ppsTables.serviceNumber[InquiryNumberPosition].number;
				break;
				case 9:
				  disp_length = 16;
				  disp_ptr =(char *)&ppsParam.ppsTables.serviceNumber[ScratchCardRechargeNumberPosition].number;
				break;
				case 10:
				  disp_length = 16;
				  disp_ptr =(char *)&ppsParam.ppsTables.serviceNumber[CreditCardRechargeNumberPosition].number;
				break;
				case 11:
				  disp_length = 16;
				  disp_ptr =(char *)&ppsParam.ppsTables.serviceNumber[ThirdPartyRechargeNumberPosition].number;
				break;
				case 12:
				  disp_length = 16;
				  disp_ptr =(char *)&ppsParam.ppsTables.serviceNumber[CustomerCareCenterNumberPosition].number;
				break;
				case 13:
				  disp_length = 16;
				  disp_ptr =(char *)&ppsParam.ppsTables.serviceNumber[RechargeNumberPosition].number;
				break;
				case 14:
				  disp_length = 1;
				//  disp_ptr =(char *)&ppsParam.ppsTables.srf_param.srf_flag;
				break;
				case 15:
				  disp_length = 1;
				  disp_ptr =(char *)&ppsParam.serviceControl.support_mnp_flag;
				break;
				case 17:
				  disp_length = 3;
				  disp_ptr = (char *)&ppsParam.serviceControl.mt_release_for_no_charge;
				break;
				default:
				  disp_length = 0;
				break;
			}			
         break;
		 case 2:
		   disp_length = 2;
		   switch(line-2){
			 case 0:
			   disp_length = 1;
			   disp_ptr = (char *)&masterFlag;
			 break;
			 case 1:
			   disp_length = 1;
			   disp_ptr = &debugmib.display.sysState;
			 break;
			 case 2:
			   disp_length = 1;
			   disp_ptr = &debugmib.display.dataSrc;
			 break;
			 case 3:
			   disp_length = 1;
			   disp_ptr = &debugmib.display.initProgress;
			 break;
			 case 4:
			   disp_length = 4;
			   disp_ptr = (char *)&debugmib.display.masterIP;
			 break;
			 case 5:
			   disp_length = 4;
			   disp_ptr = (char *)&peer_server_ip;
			 break;
			 case 6:
			  disp_length = 1;
			  disp_ptr = (char *)&debugmib.display.peer_state;
			 break;
			 case 7:
			   disp_length = 16;
			   disp_ptr = (char *)&debugmib.display.proc_used;
			 break;
			 case 8:
				disp_length = 4;
			   disp_ptr = (char *)&debugmib.display.totalCharge;
			 break;
			 case 9:
			   disp_length = 16;
			   disp_ptr = (char *)&debugmib.display.ro_used;
			 break;
			 default:
			   disp_length = 0;
			 break;
		   }
		 break;
		 case 33:		   
		   switch(line-2)
		   {
		     case 0:
		   		disp_length = 2;
				//debugmib.display.proc_begin = 0;
				disp_ptr = (char *)&debugmib.display.proc_begin;
				break;
			 default:
			   disp_length = 16;
			   disp_ptr = (char *)&smshm_ptr->port_info[(line-2)*8];
			 break;
		   }
		 break;
		 case 3:
		   disp_length = 4;
                 switch(line-2)
		   {
		     case 0:
			  disp_ptr=(char *)&debugmib.measurement.sys_capability;
			 break;
                  case 1:
                      disp_ptr = (char *)&ppsParam.serviceControl.licenseduser;
                   break;
			 case 2:
			  disp_length = 4;
			  disp_ptr = (char *)&debugmib.measurement.user_number;
			 break;
			 case 3:
			  disp_length = 4;
			  disp_ptr = (char *)&debugmib.measurement.free_index;
			 break;
			 case 4:
			  disp_length = 4;
			  disp_ptr = (char *)&debugmib.measurement.peer_usernumber;
			 break;
			 case 6:
			  disp_length = 16;
			  disp_ptr = (char *)&debugmib.msgstatistics.send_sync[0];
			 break;
			 case 7:
			  disp_length = 16;
			  disp_ptr = (char *)&debugmib.msgstatistics.recv_sync[0];		  
			 break;
			 case 9:
				disp_length = 1;
				disp_ptr = (char *)&debugmib.measurement.index_alarm;
			 break;
			 case 10:
				disp_length = 16;
				disp_ptr = (char *)&debugmib.measurement.search_table_result;
			 break;			 
			 default:
			   disp_length = 0;
			 break;
		   }			
		 break;
		 case 4:
		   disp_length = 16;
           	   switch(line-2)
		   {
		     case 0:
			  disp_length = 4;
			  disp_ptr=(char *)&debugmib.measurement.card_number;
			 break;
			 case 1:
			 disp_length = 4;
			  disp_ptr=(char *)&debugmib.measurement.cardheadnumber;
			 break;
             		default:
			   memset(&debugmib.measurement.card_prefix[(line-4)*4],0,16);
			   disp_ptr = (char *)&debugmib.measurement.card_prefix[(line-4)*4][0];
			 break;
           }
		 break;
		 case 5:
			disp_length = 4;
			switch(line-2)
			{
			  case 0:
			    disp_ptr=(char *)&debugmib.msgstatistics.CapOpen;
			  break;
			  case 1:
			    disp_ptr=(char *)&debugmib.msgstatistics.Initialdp;
				disp_length = 8;
			  break;
			  case 2:
			    disp_ptr=(char *)&debugmib.msgstatistics.RequestBCSM;
				disp_length = 8;
			  break;
			  case 3:
			    disp_ptr=(char *)&debugmib.msgstatistics.ApplyCharging;
			    disp_length = 8;
			  break;
			  case 4:
			   // disp_ptr=(char *)&debugmib.msgstatistics.Continue;
			    disp_length = 8;
			  break;
			  case 5:
			    disp_ptr=(char *)&debugmib.msgstatistics.ActivityS;
				disp_length = 8;
			  break;
			  case 6:
			    disp_ptr=(char *)&debugmib.msgstatistics.ChargingReport;
			    disp_length = 8;
			  break;
			  case 7:
			    //disp_ptr=(char *)&debugmib.msgstatistics.ReportBCSM;
			    //disp_length = 8;
			  break;
			  case 8:
			    disp_ptr=(char *)&debugmib.msgstatistics.Answered;
			    disp_length = 8;
			  break;
			  case 9:
			    disp_ptr=(char *)&debugmib.msgstatistics.Disconnect;
			    disp_length = 8;
			  break;
			  case 10:
			    disp_ptr=(char *)&debugmib.msgstatistics.Releasecall;
			    disp_length = 8;
			  break;
			  case 11:
			    disp_ptr=(char *)&debugmib.msgstatistics.Inquire;
			  break;
			  case 12:
			    disp_ptr=(char *)&debugmib.msgstatistics.Recharge;
			  break;
			  case 13:
			    disp_ptr=(char *)&debugmib.msgstatistics.ReadCard;
			  break;
			  case 14:
			    disp_ptr=(char *)&debugmib.msgstatistics.ReadCardRsp;
			  break;
			  case 15:
			    disp_ptr=(char *)&debugmib.msgstatistics.WriteCard;
			  break;
			  case 16:
			    disp_ptr=(char *)&debugmib.msgstatistics.WriteCardRsp;
			  break;
			  case 17:
			    disp_ptr=(char *)&debugmib.msgstatistics.Playannounce;
			  break;
			  case 18:
			    disp_ptr=(char *)&debugmib.msgstatistics.Accountinfo;
			  break;
			  case 19:
			    disp_ptr=(char *)&debugmib.msgstatistics.Accinfoack;
			  break;
			  default:
			    disp_length = 0;
			  break;
			}
		 break;
		 case 6:
		   disp_length = 4;
		   switch(line-2){
		     case 0:
			   disp_ptr = (char *)&debugmib.msgstatistics.InitialDP_SMS;
			 break;
			 case 1:
			   disp_ptr = (char *)&debugmib.msgstatistics.Request_SMS;
			 break;
			 case 2:
			   disp_ptr = (char *)&debugmib.msgstatistics.Continue_SMS;
			 break;
			 case 3:
			   disp_ptr = (char *)&debugmib.msgstatistics.Report_SMS;
			 break;
			 case 4:
			   disp_ptr = (char *)&debugmib.msgstatistics.Release_SMS;
			 break;
			 case 6:
			   disp_ptr = (char *)&debugmib.msgstatistics.Conference;
			 break;
			 case 8:
			   disp_ptr = (char *)&debugmib.msgstatistics.ThirdParty;
			 break;
			 case 10:
			   disp_ptr = (char *)&debugmib.msgstatistics.ConnectToResource;
			 break;
			 case 11:
			   disp_ptr = (char *)&debugmib.msgstatistics.PlayAnnouncement;
			 break;
			 case 12:
				disp_ptr = (char *)&debugmib.msgstatistics.EstablishTemporaryConnection;
			 break;
			 case 13:
				disp_ptr = (char *)&debugmib.msgstatistics.AssistInstructions;
			 break;
			 case 14:
				disp_ptr = (char *)&debugmib.msgstatistics.PCUI;
			 break;
			 case 15:
				disp_ptr = (char *)&debugmib.msgstatistics.PCUI_rsp;
			 break;
			 case 16:
				disp_ptr = (char *)&debugmib.msgstatistics.SpecialResourceReport;
			 break;
			 case 17:
				disp_ptr = (char *)&debugmib.msgstatistics.DisconnectForwardConnection;
			 break;
			 case 18:
				disp_ptr = (char *)&debugmib.msgstatistics.smInquiry;
			 break;
			 case 19:
				disp_ptr = (char *)&debugmib.msgstatistics.smRecharge;
			 break;
			 default:
			   disp_length = 0;
			 break;
		   }
		 break;
		 case 7:
			disp_length = 0;
			switch(line-2)
			{
			  case 0:
			  case 1:
			  case 2:
			  case 3:
				disp_length = 16;
				disp_ptr=(char *)&debugmib.watch_dog[(line-5)*16];
			  break;
			  case 4:
			  break;
			  case 5:
			  	disp_length = 4;
			  	disp_ptr=(char *)&debugmib.msgstatistics.update_card_request;
			  break;
			  case 6:
			  	disp_length = 4;
			  	disp_ptr=(char *)&debugmib.msgstatistics.update_card_ok;
			  break;
			  case 7:
			  	disp_length = 4;
			  	disp_ptr=(char *)&debugmib.msgstatistics.update_card_error_msg;
			  break;
			  case 8:
			  	disp_length = 4;
			  	disp_ptr=(char *)&debugmib.msgstatistics.update_card_no_card;
			  break;
			  case 9:
			  	disp_length = 4;
			  	disp_ptr=(char *)&debugmib.msgstatistics.update_card_expired;
			  break;
			  case 10:
			  	disp_length = 4;
			  	disp_ptr=(char *)&debugmib.msgstatistics.update_card_sys_failure;
			  break;
			  default:
			    disp_length = 0;
			  break;
			}
		 break;
		 case 8:
		 	disp_length = 16;
			switch(line-2)
			{
			  case 0:
			  	disp_length = 4;
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation_date[0];
			  	break;
			  case 1:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[0][0];	
			  	break;
			  case 2:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[0][4];			  	
			  	break;
			  case 3:
				disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[0][8];	
			  break;
			  case 4:
			  	disp_length = 0;
			  break;
			  case 5:
			  	disp_length = 4;
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation_date[1];
			  	break;
			  case 6:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[1][0];	
			  	break;
			  case 7:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[1][4];	
			  break;
			  case 8:
				disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[1][8];
			  break;
			  case 9:
			  	disp_length = 0;
			  	break;
			  case 10:
			  	disp_length = 4;
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation_date[2];
			  	break;
			  case 11:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[2][0];			  	
			  	break;
			  case 12:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[2][4];
			  	break;
			  case 13:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[2][8];
			  	break;
			  case 14:
			  	disp_length = 0;
			  	break;
			  case 15:
			  	disp_length = 4;
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation_date[3];
			  	break;
			  case 16:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[3][0];
			  	break;
			  case 17:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[3][4];
			  	break;
			  case 18:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[3][8];
			  	break;
			  default:
			  	disp_length = 0;
			  	break;
			}
		 break;
		 case 9:
			disp_length = 16;
			switch(line-2)
			{
			  case 0:
			  	disp_length = 4;
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation_date[4];
			  	break;
			  case 1:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[4][0];	
			  	break;
			  case 2:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[4][4];			  	
			  	break;
			  case 3:
				disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[4][8];	
			  break;
			  case 4:
			  	disp_length = 0;
			  break;
			  case 5:
			  	disp_length = 4;
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation_date[5];
			  	break;
			  case 6:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[5][0];	
			  	break;
			  case 7:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[5][4];	
			  break;
			  case 8:
				disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[5][8];
			  break;
			  case 9:
			  	disp_length = 0;
			  	break;
			  case 10:
			  	disp_length = 4;
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation_date[6];
			  	break;
			  case 11:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[6][0];			  	
			  	break;
			  case 12:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[6][4];
			  	break;
			  case 13:
			  	disp_ptr=(char *)&debugmib.msgstatistics.ussd_operation[6][8];
			  	break;
			  default:
			  	disp_length = 0;
			  	break;
			}		 	
		 break;
		 default:
		   disp_length = 0;
		 break;
	}	 
	return disp_length;
}


void	pps_disp_page(BYTE page)
{
	BYTE	disp_line;
	BYTE	disp_length;

	
	debug_page_line[PPS_PAGE_POINT] = page + 5;/* page */
	for (disp_line = 2; disp_line < 22; disp_line++)
	{
		disp_length = pps_disp_line(page,disp_line);
		debug_page_line[PPS_LINE_POINT] = disp_line + 2;/* line */
		debug_page_line[PPS_LINE_POINT+1] = 2;/* data pointer */
		debug_set_response(PPS_BASE_ID_LEN+2,debug_page_line, (BYTE *)disp_ptr,disp_length);
	}
}



int	pps_debug_set()
{
	BYTE	page;
	BYTE	data[32];
	BYTE 	*ptr;
	char    version_info[32]={""};
	sprintf(version_info,"V%d.%d.%02d",version_id[0],version_id[1],version_id[2]);
	
	ptr = data;	
	
	debug_set_response(PPS_BASE_ID_LEN,debug_status_id,&debug_status,1);
	debug_set_response(PPS_BASE_ID_LEN,debug_name_id, (BYTE *)version_info,10);
	/* module name */
	debug_set_response(PPS_BASE_ID_LEN,debug_ascin_id, (BYTE *)debugmib.display.asciiIn,4096);
	/* ascii in buf */
	debug_set_response(PPS_BASE_ID_LEN,debug_ascout_id, (BYTE *)debugmib.display.asciiOut,4096);
	/* ascii out buf */
	
	for (page = 1; page < 10; page ++)
	{
		switch (page)
		{
			case 1:
				ptr = title1_p;
				break;
			case 2:
				ptr = title2_p;
				break;
			case 3:
				ptr = title3_p;
				break;
			case 4:
				ptr = title4_p;
				break;
			case 5:
			    ptr = title5_p;
				break;
			case 6:
			    ptr = title6_p;
				break;
            		case 7:
			    ptr = title7_p;
			    break;
			case 8:
			    ptr = title8_p;
			break;
			case 9:
		           ptr = title9_p;
			break;	
			default:
				ptr = NULL;
				continue;
				break;				
		}
		debug_page_title[PPS_PAGE_POINT] = 5 + page;
		debug_set_response(PPS_BASE_ID_LEN+1,debug_page_title,ptr,strlen((char *)ptr));
		pps_disp_page(page);
	}
	
	return 1;
}

void AddPPSStatistics(int index,int dir)
{
	PutLogFunID("AddPPSStatistics");

   switch(index)
   {
      case MIBPPS_CAPOPEN:
	    debugmib.msgstatistics.CapOpen ++;
	  break;
      case MIBPPS_INITIALDP:
	    debugmib.msgstatistics.Initialdp[dir] ++;
	  break;
   	  case MIBPPS_REQUESTBCSM:
	    debugmib.msgstatistics.RequestBCSM[dir] ++;
	  break;
	  case MIBPPS_APPLYCHARGING:
	    debugmib.msgstatistics.ApplyCharging[dir] ++;
	  break;
	  case MIBPPS_CONTINUE:
	    //debugmib.msgstatistics.Continue[dir] ++;
	  break;
	  case MIBPPS_ACTIVITYS:
	    debugmib.msgstatistics.ActivityS ++;
	  break;
	  case MIBPPS_ACTIVITYR:
	    debugmib.msgstatistics.ActivityR ++;
	  break;
	  case MIBPPS_CHARGINGREPORT:
	    debugmib.msgstatistics.ChargingReport[dir] ++;
	  break;
	  case MIBPPS_REPORTBCSM:
	    debugmib.msgstatistics.ReportBCSM[dir] ++;
	  break;
	  case MIBPPS_ANSWERED:
	    debugmib.msgstatistics.Answered[dir] ++;
	  break;
	  case MIBPPS_DISCONNECT:
	    debugmib.msgstatistics.Disconnect[dir] ++;
	  break;
	  case MIBPPS_RELEASECALL:
	    debugmib.msgstatistics.Releasecall[dir] ++;
	  break;
	  case MIBPPS_INQUIRE:
	    debugmib.msgstatistics.Inquire ++;
	  break;
	  case MIBPPS_RECHARGE:
	    debugmib.msgstatistics.Recharge ++;
	  break;
	  case MIBPPS_PLAYANNOUNCE:
	    debugmib.msgstatistics.Playannounce ++;
	  break;
	  case MIBPPS_READCARD:
	    debugmib.msgstatistics.ReadCard ++;
	  break;
	  case MIBPPS_READCARDRSP:
	    debugmib.msgstatistics.ReadCardRsp ++;
	  break;
	  case MIBPPS_WRITECARD:
	    debugmib.msgstatistics.WriteCard ++;
	  break;
	  case MIBPPS_WRITECARDRSP:
	    debugmib.msgstatistics.WriteCard ++;
	  break;
	  case MIBPPS_ACCOUNTINFO:
	    debugmib.msgstatistics.Accountinfo ++;
	  break;
	  case MIBPPS_ACCINFOACK:
	    debugmib.msgstatistics.Accinfoack ++;
	  break;
	  case MIBPPS_CDRNUMBER:
	    debugmib.measurement.cdrnumber ++;
	  break;
	  case MIBPPS_CSTANUMBER:
	    debugmib.measurement.cstanumber ++;
	  break;
	  case MIBPPS_CDROVERFLOW:
	    debugmib.measurement.cdroverflow ++;
	  break;
      case MIBPPS_BLACKLIST:
        debugmib.measurement.blacklist ++;
      break;
	  case MIBPPS_SUSPEND:
	    debugmib.measurement.suspend++;
	  break;
	  case MIBPPS_INITIALDP_SMS:
	    debugmib.msgstatistics.InitialDP_SMS++;
	  break;
	  case MIBPPS_REQUEST_SMS:
	    debugmib.msgstatistics.Request_SMS++;
	  break;
	  case MIBPPS_CONTINUE_SMS:
	    debugmib.msgstatistics.Continue_SMS++;
	  break;
	  case MIBPPS_REPORT_SMS:
	    debugmib.msgstatistics.Report_SMS++;
	  break;
	  case MIBPPS_RELEASE_SMS:
	    debugmib.msgstatistics.Release_SMS++;
	  break;
	  case MIBPPS_CONFERENCE:
	    debugmib.msgstatistics.Conference++;
	  break;
	  case MIBPPS_THIRD_PARTY:
	    debugmib.msgstatistics.ThirdParty++;
	  break;
	  case MIBPPS_CONNECT_TO_RESOURCE:
		debugmib.msgstatistics.ConnectToResource++;
	  break;
	  case MIBPPS_PLAY_ANNOUNCEMENT:
		debugmib.msgstatistics.PlayAnnouncement++;
	  break;
	  case MIBPPS_EST_TMP_CNT:
		debugmib.msgstatistics.EstablishTemporaryConnection++;
	  break;
	  case MIBPPS_AST_INS:
		debugmib.msgstatistics.AssistInstructions++;
	  break;
	  case MIBPPS_PCUI:
		debugmib.msgstatistics.PCUI++;
	  break;
	  case MIBPPS_PCUI_RES:
		debugmib.msgstatistics.PCUI_rsp++;
	  break;
	  case MIBPPS_SPE_RUS_RPT:
		debugmib.msgstatistics.SpecialResourceReport++;
	  break;		
	  case MIBPPS_DISCNT_FWD_CNT:
		debugmib.msgstatistics.DisconnectForwardConnection++;
	  break;
	  case MIBPPS_SM_INQUIRY:
		debugmib.msgstatistics.smInquiry++;
	  break;
	  case MIBPPS_SM_RECHARGE:
		debugmib.msgstatistics.smRecharge++;
	  break;
	  default:
	  break;
   }
}

int  ppsDebugRt()
{
    int   pageno, len;
    char  cmd[1024]={""};
    
	PutLogFunID("ppsDebugRt");

	if(clearSubsFlag)
	{
		if(waitClearSubsConfirmCounter++>1200)
		{
			DebugMsg(debugmib.display.asciiOut,"Command: clear all subs expired!");
			clearSubsFlag = 0;
		}
	}
	if(clearCardFlag)
	{
		if(waitClearCardConfirmCounter++>1200)
		{
			DebugMsg(debugmib.display.asciiOut,"Command: clear all card expired!");
			clearCardFlag = 0;
		}
	}
	
	SetDebugTariff(0,0,0);
    
    len = strlen(debugmib.display.asciiIn);
    if(len ==0){
		if(ToBeListed){
			ListData(ToBeListed);
		}
		return 0;
	}
    pageno = debugmib.display.asciiIn[0];
    memcpy(cmd,&debugmib.display.asciiIn[1],len-1);
 
    DecodePPSAsciiIn(pageno-1,cmd);
    memset(debugmib.display.asciiIn,0,len);
    return 1;
}

void ListParameter()
{
    char buf[4096];

	PutLogFunID("ListParameter");

	DebugMsg(debugmib.display.asciiOut,"PPS Parmeter list\n====================");
	DebugMsg(debugmib.display.asciiOut,"CDR Flag:    %02x",
				ppsParam.serviceControl.cdrFlag);
    DebugMsg(debugmib.display.asciiOut,"Max Recharge Attempt:  %d",
			ppsParam.serviceControl.maxRechargeFailedNum);
	BcdToAscii(buf, (BYTE *)debugmib.display.rechargeDigitals,16);
    DebugMsg(debugmib.display.asciiOut,"Recharge Digits:  %s",buf);
	BcdToAscii(buf, (BYTE *)debugmib.display.inquiryDigitals,16);

}

void ListData()
{
	PutLogFunID("ListData");

	if(ToBeListed==1){
		ListSubsSegNo(ToBeListedSegNo,0);
	}else{
		ListCardsSegNo(ToBeListedSegNo,0);
	}
	ToBeListed=0;
}

void ListSubInfo(char *msisdn)
{
	int					len,head,tail,instance;//,i;
	char				buf[BUFSIZE];
   	_prepaid_info  		*shmp=NULL;
	//PoolParent			*pParent;
	//PoolChild			*pChild;
	
	PutLogFunID("ListSubInfo");
	
	len = strlen(msisdn);
	if(len<=ACCOUNT_SERIES_LEN){
		DebugMsg(debugmib.display.asciiOut,"Error input of sub.: %s!",msisdn);
		return;
	}
	instance = FindPPSSubsStoreIndex(atoll(msisdn));
	if(instance<0)
	{
		DebugMsg(debugmib.display.asciiOut,"can not find msisdn: %s!",msisdn);
		return;
	}
	head = instance / UNIT_STORE_ACCOUNT;
	tail = instance % UNIT_STORE_ACCOUNT;
	
	shmp = &ramshm_ptr->prepaid_data.prepaid_info[head][tail];
	DebugMsg(debugmib.display.asciiOut,
			"<<----------- Sub. Info @ <%d> --------",instance);
	DebugMsg(debugmib.display.asciiOut,"msisdn:		%s",msisdn);
	DebugMsg(debugmib.display.asciiOut,"PIN:		%s",shmp->pin);
	DebugMsg(debugmib.display.asciiOut,"status:		%d",shmp->status);
	DebugMsg(debugmib.display.asciiOut,"balance:	%07.4f+%07.4f",
						shmp->balance,shmp->promo_balance);
    	AsciiTime(buf,shmp->mo_expiration_date);
	DebugMsg(debugmib.display.asciiOut,"mo expire date:	%s",buf);
   	AsciiTime(buf,shmp->mt_expiration_date);
	DebugMsg(debugmib.display.asciiOut,"mt expire date:	%s",buf);
	DebugMsg(debugmib.display.asciiOut,"cos id:		%d, CUG ID:	%d",shmp->cos_id, shmp->cug_id);
	if(OCS_with_external_CRM())
	{
		DebugMsg(debugmib.display.asciiOut,"CRM ID:	%d.%d",shmp->crm_info.account_id, shmp->crm_info.base_plan_id);
		DebugMsg(debugmib.display.asciiOut,"Rent:	%d",shmp->crm_info.rent_charge);
	}

	DebugMsg(debugmib.display.asciiOut,"free SMS status:		%d",shmp->freeSMSActiveDays);
	/*
	DebugMsg(debugmib.display.asciiOut,"fav numbers:	%d",shmp->favorite_numbers);
	DebugMsg(debugmib.display.asciiOut,"fav counter:	%d",shmp->fav_change_counter);
		
	for(i=0;i<5;i++)
	{
		len = shmp->favorite_number[i][MAX_MSISDN_BLEN-1];
		BcdToAscii(buf,shmp->favorite_number[i],16);
		buf[len] = 0;
		DebugMsg(debugmib.display.asciiOut,"fav number[%d]:		%s",i,buf);
	}

	if(shmp->poolAccount.role == MASTER_SLAVE_CHILD)
	{
		pChild = &shmp->poolAccount.roleParty.child;
		DebugMsg(debugmib.display.asciiOut,"pool account role:		child");
		
		len = pChild->parentMsisdn[MAX_MSISDN_BLEN-1];
		BcdToAscii(buf,pChild->parentMsisdn,16);
		buf[len] = 0;
		DebugMsg(debugmib.display.asciiOut,"parent number:		%s",buf);
		DebugMsg(debugmib.display.asciiOut,"calls today:		%d",pChild->callsToday);
		DebugMsg(debugmib.display.asciiOut,"sms today:			%d",pChild->smsToday);
		DebugMsg(debugmib.display.asciiOut,"charged today:		%f",pChild->chargedToday);
	}
	else if(shmp->poolAccount.role == MASTER_SLAVE_PARENT)
	{
		pParent = &shmp->poolAccount.roleParty.parent;
		DebugMsg(debugmib.display.asciiOut,"pool account role:		parent,has %d children",pParent->childNumber);
		DebugMsg(debugmib.display.asciiOut,"change times:			%d",pParent->changeTimes);
		for(i=0;i<POOL_MAX_CHILD;i++)
		{
			len = pParent->childMsisdn[i][MAX_MSISDN_BLEN-1];
			BcdToAscii(buf,pParent->childMsisdn[i],16);
			buf[len] = 0;
			DebugMsg(debugmib.display.asciiOut,"child number[%d]:		%s",i,buf);
		}		
	}
	else
		DebugMsg(debugmib.display.asciiOut,"pool account role:		Normal account");
	*/
	DebugMsg(debugmib.display.asciiOut,"Gprs volume:				%lld",shmp->gprsVolume);
	DebugMsg(debugmib.display.asciiOut,"Gprs balance:				%0.4f",shmp->gprsBalance);
	DebugMsg(debugmib.display.asciiOut,"Gprs volume ths month:			%lld",shmp->gprsVolumeThisMonth);
	DebugMsg(debugmib.display.asciiOut,"Gprs notification today:		%d",shmp->gprsNotificationTimes);
	DebugMsg(debugmib.display.asciiOut,"Call me back times:		%d",shmp->callMeBackRequestTimes);


	AsciiTimeYMD(buf,shmp->bundle_info.validity_date);
	DebugMsg(debugmib.display.asciiOut,"Bundle Info >>>");
	DebugMsg(debugmib.display.asciiOut,"	data (KB)	 : %lld", (shmp->bundle_info.data + 1023) / 1024);
	DebugMsg(debugmib.display.asciiOut,"	MO (S)     : %d", shmp->bundle_info.mo_voice);
	DebugMsg(debugmib.display.asciiOut,"	MT (S)     : %d", shmp->bundle_info.mt_voice);
	DebugMsg(debugmib.display.asciiOut,"	SMS        : %d", shmp->bundle_info.sms);
	DebugMsg(debugmib.display.asciiOut,"	Validity    : %s", buf);
	DebugMsg(debugmib.display.asciiOut,"	SMS Sent : %d", shmp->bundle_info.warning_sms_flag);

	DebugMsg(debugmib.display.asciiOut," ");
	
	DebugMsg(debugmib.display.asciiOut,"------------- The End ------------------->>");
}

void ListSubsSegNo(int segno,int flag)
{
   int  i,len;
   char buf[1024],tmpbuf[32];
   _prepaid_info  *shmp=NULL;

	PutLogFunID("ListSubsSegNo");

   if(segno>=MSISDN_HEAD_NUM){
     DebugMsg(debugmib.display.asciiOut,"Segno is too big ,should be (0-%d)",MSISDN_HEAD_NUM-1);
     return;
   }
   if(flag){
	  ToBeListed = 1;
	  ToBeListedSegNo = segno;
   	  DebugMsg(debugmib.display.asciiOut,"MSISDN			TYPE	STATUS	BALANCE	COS_ID	CUGID"); 
	}
   for(i=0;i<UNIT_STORE_ACCOUNT/2;i++){
     shmp = &ramshm_ptr->prepaid_data.prepaid_info[segno][i+(1-flag)*UNIT_STORE_ACCOUNT/2];
     len = shmp->msisdn[9];
	 if(len==0 || len>18) continue;
	 BcdToAscii(buf,shmp->msisdn,len);
	 sprintf(tmpbuf,"		%02x	",shmp->rent_type);
	 strcat(buf,tmpbuf);
	 sprintf(tmpbuf,"%d	",shmp->status);
	 strcat(buf,tmpbuf);
	 sprintf(tmpbuf,"%f	",(shmp->balance));
	 strcat(buf,tmpbuf);
	 sprintf(tmpbuf,"%d	",shmp->cos_id);
	 strcat(buf,tmpbuf);
	 sprintf(tmpbuf,"%d",shmp->cug_id);
	 strcat(buf,tmpbuf);
	 DebugMsg(debugmib.display.asciiOut,"%s",buf);
   }  
}

void ListCardsSegNo(unsigned int segno,int flag)
{
   int  i,len;
   char buf[1024],tmpbuf[32];
   _recharge_card_info  *shmp=NULL;

	PutLogFunID("ListCardsSegNo");

   if(segno>=MAX_CARD_SEGMENT){
     DebugMsg(debugmib.display.asciiOut,"Segno is too big ,should be (0-%d)",MAX_CARD_SEGMENT-1);
     return;
   }
   	if(flag){
		ToBeListed = 2;
		ToBeListedSegNo = segno;
   		DebugMsg(debugmib.display.asciiOut,"CARD NO				STATUS   VALUE     MSISDN"); 
	}
   for(i=0;i<MAX_CARD_NUM_PSEG/2;i++){
     shmp = &ramshm_ptr->card_data.prepaid_card[segno][i+(1-flag)*MAX_CARD_NUM_PSEG/2];
     len = shmp->card_no[MAX_CARD_LENGTH];
	 if(len!=12 && len!=16) continue;
	 BcdToAscii(buf,shmp->card_no,len);
	 StringCat(buf," ",16);
	 sprintf(tmpbuf," %02x	",shmp->card_status);
	 strcat(buf,tmpbuf);
	 sprintf(tmpbuf,"%07ld	",shmp->face_value);
	 strcat(buf,tmpbuf);
	 sprintf(tmpbuf,"%s",shmp->msisdn_used);
	 strcat(buf,tmpbuf);
	 DebugMsg(debugmib.display.asciiOut,"%s",buf);
   }
}

void ListUsedCardHead(int segno)
{
	int		i,j;
	PutLogFunID("ListUsedCardHead");

	if(segno>=MAX_CARD_SEGMENT) j=MAX_CARD_SEGMENT;
	else j = segno;

	for(i=j*MAX_CARD_NUM_PSEG;i<(j+1)*MAX_CARD_NUM_PSEG;i++)
	{
		if(ramshm_ptr->card_data.segment_used_flag[i])
		{
			DebugMsg(debugmib.display.asciiOut,"%04d: %lld",
						i,ramshm_ptr->card_data.prepaid_card_head[i]);
		}
	}
}

void ListUsedUserHead(int segno)
{
	int		i,j;

	PutLogFunID("ListUsedUserHead");	
	if(segno>=MSISDN_HEAD_NUM) j=MSISDN_HEAD_NUM;
	else j = segno;

	for(i=j*UNIT_STORE_ACCOUNT;i<(j+1)*UNIT_STORE_ACCOUNT;i++)
	{
		if(ramshm_ptr->prepaid_data.msisdn_head[i])
		{
			DebugMsg(debugmib.display.asciiOut,"%04d: %lld",
						i,ramshm_ptr->prepaid_data.msisdn_head[i]);
		}
	}
}

void cmdSetBalance(int balance)
{
	int  i,j,len;
	_prepaid_info  *shmp=NULL;

	PutLogFunID("cmdSetBalance");
	for(j=0;j<MSISDN_HEAD_NUM;j++)
	for(i=0;i<UNIT_STORE_ACCOUNT;i++)
	{
		shmp = &ramshm_ptr->prepaid_data.prepaid_info[j][i];
		len = shmp->msisdn[9];
		if(len==0) continue;
			shmp->balance = balance*1.0;
	}
}

void SumAccountBalance()
{
   int  i,j,len;
   double  balance=0;   
   _prepaid_info  *shmp=NULL;

   PutLogFunID("SumAccountBalance");
   for(j=0;j<MSISDN_HEAD_NUM;j++)
   for(i=0;i<UNIT_STORE_ACCOUNT;i++)
   {
     shmp = &ramshm_ptr->prepaid_data.prepaid_info[j][i];
     len = shmp->msisdn[9];
	 if(len==0) continue;
	 balance = balance + shmp->balance + shmp->promo_balance;
   }
   DebugMsg(debugmib.display.asciiOut,"Total balance: %.0f", balance);
}

int pps_set_all_balance(int balance)
{
	int  i,j,len;
	_prepaid_info  *shmp=NULL;

	for(j=0;j<MSISDN_HEAD_NUM;j++)
	for(i=0;i<UNIT_STORE_ACCOUNT;i++)
	{
		shmp = &ramshm_ptr->prepaid_data.prepaid_info[j][i];
		len = shmp->msisdn[9];
		if(len==0) continue;
		shmp->balance = balance;
   }
	
   DebugMsg(debugmib.display.asciiOut,"Set balance to : %d", balance);
   return 1;
}
/* ------------------------------------------*/
void ClearAllSubs()
{
	memset(&ramshm_ptr->prepaid_data,0,sizeof(_prepaid_data));
	debugmib.measurement.usernumber = 0;	
	memset(debugmib.measurement.user_number,0,8);
}

void ClearAllCard()
{
	memset(&ramshm_ptr->card_data,0,sizeof(_card_data));
	debugmib.measurement.cardnumber = 0;
	debugmib.measurement.cardheadnumber = 0;
	memset(debugmib.measurement.card_number,0,8);
	memset(debugmib.measurement.card_prefix,0,2000*4);
}

void SetDebugTariff(int tariff_id,int type,int operation_type)
{
	/* type: 0=current, 1=plan */
	/* operation type: 0=periodic check, 1=set parameter */	
	static int debug_tariff_flag = 0;
	static int debug_tariff_id=0, debug_tariff_type=0;
	
	if(operation_type)
	{
		debug_tariff_id = tariff_id;
		debug_tariff_type = type;
		debug_tariff_flag = 1;
	}
	else 
	{
		if(debug_tariff_flag)
		{
			if(debug_tariff_type==0)
			{
				if(ListCurrentTariff(debug_tariff_id))
					debug_tariff_flag = 0;
			}
			else
			{
				//if(ListTariffPlan(debug_tariff_id))
					debug_tariff_flag = 0;
			}
		}
	}
}

extern	_climax_quence		climaxQuence;
 
void CreatePrepaidCardAuto()
{
	int count = 0,len;
	_recharge_card_info	shmp;		
	static long	cardNoStart = 50000000;
	static long	cardPwdStart = 50000000;
	u_char	cardNo[20]={0};
	u_char	pwd[32]={0};
	
	while(climaxQuence.counter < MAX_CARD_RECORD)
	{
			memset(&shmp,0,sizeof(_recharge_card_info));
			/* card no	*/
			sprintf((char *)cardNo,"%ld",cardNoStart++);
			len = strlen((char *)cardNo);								
			AsciiToBcd(shmp.card_no, (char *)cardNo,len);
			shmp.card_no[MAX_CARD_LENGTH] = len;
			
			/* PWD */
			sprintf((char *)pwd,"%ld",cardPwdStart++);
			if(strlen((char *)pwd)!=16)
				return;
			EncryptCardHRN((char *)pwd,shmp.card_pwd);
			
			/* face value */
			shmp.face_value = 10;
			 /* card status */
			shmp.card_status = 1;
			/* expiry_date */
			shmp.expiry_date = atol("20080702054842");
			/* valid_time */
			shmp.valid_time =  atol("20080702054842");;
			/* msisdn_used:NULL */
			
			/* updated_date */
			shmp.updated_date = tvnow.tv_sec;
			if(AddNewCardIntoClimaxQuence(&shmp)<0)
			{
				DebugMsg(debugmib.display.asciiOut,"Error: buffer is full,no space to store new card!\n");    	
				DebugMsg(debugmib.display.asciiOut,"Total new created :%ld\n",count);
				return; 
			}
			else
				count++;
	}
	DebugMsg(debugmib.display.asciiOut,"Total new created :%ld\n",count);
}

int	jump =10;
void DeletePrepaidCardAuto()
{		
	int count = 0,i,j;	
	
	
	for(i=0;i<MAX_CARD_SEGMENT;i++)
	{
		for(j=0;j<MAX_CARD_NUM_PSEG;)
		{				
			count++	;
			MoveUsedCardToBuffer(i*MAX_CARD_NUM_PSEG+j);
			j += jump;
		}
	}

	DebugMsg(debugmib.display.asciiOut,"Total new deleted :%ld\n",count);	
} 

/* ------------------------------------------*/
void PrintCmdOK()
{
    DebugMsg(debugmib.display.asciiOut,"COMMAND OK!");
}

extern void ppsAddBalanceToAll(int balance);
extern int query_tariff_test(u_char call_type);
extern int  ocs_get_charge_prefix(char *dialed_number, char *prefix, char *area_name, int *call_type);
int DecodePPSAsciiIn(int pageno, char *cmd)
{
    char cvalue[128];
    int ivalue;

    PutLogFunID("DecodePPSAsciiIn");

    switch(pageno)
    {
        case 0: /* asciiout */
            if( strcasecmp(cmd,"help") ==0 || strcasecmp(cmd,"h") ==0)
            {
                PrintCmdOK();
                DebugMsg(debugmib.display.asciiOut,"%s",help_menu);
                return 1;
            }
            if(!strcasecmp(cmd,"log all"))
            {
                logMsgFlag = 0xFF;
                PrintCmdOK();
                return 1;
            }
            if(!strcasecmp(cmd,"log none"))
            {                
                logMsgFlag = 0x00;
                PrintCmdOK();
                return 1;
            }
            if(!strcasecmp(cmd,"log error"))
            {		  
                logMsgFlag |= 0x01;
                PrintCmdOK();
                return 1;
            }
            if(!strcasecmp(cmd,"log message"))
            {
                logMsgFlag |= 0x02;
                PrintCmdOK();
                return 1;
            }
            if(!strcasecmp(cmd,"log procedure"))
            {
                logMsgFlag |= 0x04;
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"log msisdn=",11))
            {
                logMsgFlag |= 0x08;
                strcpy(logMsisdn,&cmd[11]);
                PrintCmdOK();
                return 1;
            }
            if(!strcasecmp(cmd,"log errmsg"))
            {
                logMsgFlag |= 0x20;
                PrintCmdOK();
                return 1;
            }
            if(!strcasecmp(cmd,"log subs manage"))
            {
                logMsgFlag |=0x40;
                PrintCmdOK();
                return 1;
            }
            
            if(!strncasecmp(cmd,"list subs segno=",16))
            {
                strcpy(cvalue,&cmd[16]);
                ivalue = atoi(cvalue);
                ListSubsSegNo(ivalue,1);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"list sub=",9))
            {
                strcpy(cvalue,&cmd[9]);
                ListSubInfo(cvalue);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"list card segno=",16))
            {
                strcpy(cvalue,&cmd[16]);
                ivalue = atoi(cvalue);
                ListCardsSegNo(ivalue,1);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"list tariff plan LAC=",21))
            {
            //ListTariffPlan(atoi(&cmd[21]));
                SetDebugTariff(atoi(&cmd[21])%MAX_ZONE_NUM,1,1);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"list tariff LAC=",16))
            {
                strcpy(cvalue,&cmd[16]);
                ivalue = atoi(cvalue);
                //ListCurrentTariff(ivalue%MAX_ZONE_NUM);
                SetDebugTariff(ivalue%MAX_ZONE_NUM,0,1);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"tariff msisdn=",14))
            {
                strcpy(cvalue,&cmd[14]);
                ListCurrentTariffWithMsisdn(cvalue);
                PrintCmdOK();
                return 1;
            }

            if(!strcasecmp(cmd,"sum balance"))
            {
                SumAccountBalance();
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"card head=",10))
           {
                ListUsedCardHead(atoi(&cmd[10]));
                PrintCmdOK();
                return 1;                
            }
            if(!strncasecmp(cmd,"user head=",10))
            {
                ListUsedUserHead(atoi(&cmd[10]));
                PrintCmdOK();
                return 1;                
            }		

            if(!strncasecmp(cmd,"set ETC=",8))
            {
                if(!strncasecmp(&cmd[8],"separate",8))
                {
                    ppsParam.serviceControl.cdrFlag &=0x0f;
                }
                else if(!strncasecmp(&cmd[8],"embed",5))
                {
                    ppsParam.serviceControl.cdrFlag |=0x10;
                }
                SavePPSParam();
                PrintCmdOK();
                return 1;                
            }
            
            if(!strncasecmp(cmd,"list index",11))
            {
                ppsListAllIndexByOrder();
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"list all subs",13))
            {
                ppsListAllSubsMSISDN();
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"add balance to all=",19))		
	     {
                   ivalue = atoi(&cmd[19]);

			ppsAddBalanceToAll(ivalue);
	                PrintCmdOK();
	                return 1;
			
            }
            if(!strncasecmp(cmd,"recreate index",12))
            {
                CreatePPSDataIndexForAll();
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"pps reset",9))
            {
                LoadPPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"reload subs data"))
            {
                ReloadPPSSubscriberFromHD();
                PrintCmdOK();
                return 1;
            }		
            if(!strncasecmp(cmd,"save param",10))
            {
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }

            if(!strcmp(cmd,"mt release"))
            {
                debugmib.measurement.mt_release_flag = 1;
                ppsParam.serviceControl.mt_release_for_no_charge = 1;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"mt control"))
            {
                debugmib.measurement.mt_release_flag = 0;
                ppsParam.serviceControl.mt_release_for_no_charge = 0;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"mt cdr enable"))
            {
                ppsParam.serviceControl.mt_not_charged_for_cdr = 0;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }

            if(!strcmp(cmd,"pps mo release"))
            {
                ppsParam.serviceControl.mo_release_for_no_charge = 1;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"pps mo control"))
            {
                ppsParam.serviceControl.mo_release_for_no_charge = 0;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"mt voice=", 9))
            {
                ppsParam.serviceControl.mt_play_voice_flag = atoi(&cmd[9]);
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"vms allways go, yes"))
            {
                ppsParam.serviceControl.vms_allways_go = 0;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"vms allways go, no"))
            {
                ppsParam.serviceControl.vms_allways_go = 1;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"set log func on"))
            {
                ppsParam.serviceControl.log_func_flag = 1;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"set log func off"))
            {
                ppsParam.serviceControl.log_func_flag = 0;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"delay timer=",12))
            {
                ppsParam.serviceControl.delay_timer_for_roaming = atoi(&cmd[12]) % 11;
			/* maximum = 500ms */
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"activity timer=",15))
            {
                ivalue = atoi(&cmd[15]);
                if(ivalue <= 1) ivalue = 1;
                if(ivalue >120) ivalue = 120;
                SetActivityTestTimer(ivalue);
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"list all port status"))
            {
                ListAllPortStatus(0);
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"clear all port status"))
            {
                ListAllPortStatus(1);
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"list all fd status"))
            {
		  gx_print_all_port_status();
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"print fd stat"))
            {
		  gx_print_stat();
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"clean fd stat"))
            {
		  gx_stat_clean_all();
                PrintCmdOK();
                return 1;
            }

            if(!strncasecmp(cmd,"clean card faceValue=",20))
            {
                if(ppsCleanCardFaceValue(atoi(&cmd[21])))
                {
                    DebugMsg(debugmib.display.asciiOut,"Command complete, please check card");
                }
                else
                {
                    DebugMsg(debugmib.display.asciiOut,"Please input again:%s\n",cmd);
                }
                PrintCmdOK();
                return 1;
            }

            if(!strcmp(cmd,"truncation enable"))
            {
                set_truncation_flag(1);
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"truncation disable"))
            {
                set_truncation_flag(0);
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"truncation?"))
            {
                if(get_truncation_flag())
                    DebugMsg(debugmib.display.asciiOut,"Truncation is enabled\n");
                else
                    DebugMsg(debugmib.display.asciiOut,"Truncation is disabled\n");
                PrintCmdOK();
                return 1;
            }

            if(!strncasecmp(cmd,"set prom news=",14))	
            {
                SmsSentence		*smsSentenceItem;
                smsSentenceItem = &ppsParam.ppsTables.smsSentence[SMS_PROMOTION_NEWS];	
                memset(smsSentenceItem->title,0,PpsMaxTitleLen);
                memcpy(smsSentenceItem->title,"Promotion News ",PpsMaxTitleLen);
                memset(smsSentenceItem->english,0,MAX_SMS_LEN);
                strcpy((char *)smsSentenceItem->english,&cmd[14]);		
                smsSentenceItem->existFlag = TRUE;
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"set delimiter=",14))
            {
                SetSMSDelimiter(&cmd[14]);
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"reload pps param"))
            {
                LoadPPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"clear sms table"))
            {
                initSMSTable();	
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"load tariff"))
            {
                SetLoadFileFlag(3);
                PrintCmdOK();
                return 1;
            }	
            if(!strcmp(cmd,"tariff take effect"))
            {
                load_tariff_main();
                PrintCmdOK();
                return 1;
            }

            if(!strncasecmp(cmd,"idd prefix no",13))
            {
                ppsParam.serviceControl.transfer_idd_prefix = 0;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"idd prefix yes",14))
            {
                 ppsParam.serviceControl.transfer_idd_prefix = 1;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }            

            if(!strcasecmp(cmd,"climax"))
            {
                printfClimaxQuenceIndex();
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"get card=",9))
            {
                searchCardInfoByPWD(&cmd[9]);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"replace card on",15))
            {
                SetReplaceCardFlag(1);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"replace card off",16))
            {
                SetReplaceCardFlag(0);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"set replaceable on",18))
            {
                SetReplaceSubsDataFlag(1);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"set replaceable off",19))
            {
                SetReplaceSubsDataFlag(0);                
                PrintCmdOK();
                return 1;
            }
            /*
            if(!strncasecmp(cmd,"clear all subs",14))
            {
                if(clearSubsFlag == 0x00)
                {
                    DebugMsg(debugmib.display.asciiOut, "please input command again!");
                    clearSubsFlag = 0x01;
                    waitClearSubsConfirmCounter = 0;
                }
                else if(clearSubsFlag == 0x01)
                {
                    DebugMsg(debugmib.display.asciiOut,"clear subs. data and index...");
                    ClearAllSubs();
                    clearSubsFlag = 0x00;
                }
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"clear all card",14))
            {
                if(clearCardFlag == 0x00)
                {
                    DebugMsg(debugmib.display.asciiOut, "please input command again!");
                    clearCardFlag = 0x01;
                    waitClearCardConfirmCounter = 0;
                }
                else if(clearCardFlag == 0x01)
                {
                    DebugMsg(debugmib.display.asciiOut,"clear all scratch data and index...");
                    ClearAllCard();
                    clearCardFlag = 0x00;
                }
                PrintCmdOK();
                return 1;
            } */
            if(!strncasecmp(cmd,"set sync on",11))
            {
                SetSyncFlag(1);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"set sync off",12))
            {
                SetSyncFlag(0);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"set periodic sync on",20))
            {
                SetPeriodicSyncFlag(1);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"set periodic sync off",21))
            {
                SetPeriodicSyncFlag(0);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"set tcp sync on",15))
            {
                SetTcpSyncFlag(1);
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"set tcp sync off",16))
            {
                SetTcpSyncFlag(0);
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"check card=",11))
            {
                CheckCardPWD(&cmd[11]);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"get card index=",15))
            {
                searchCardByIndex(atoi(&cmd[15]));
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"set smpp caller=",16))
            {
                setPPSSmppNumber(&cmd[16], 0);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"set smpp called=",16))
            {
                setPPSSmppNumber(&cmd[16], 1);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"test smpp=",10))
            {
                sendPPSTestShortMessage(&cmd[10]);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"test ocs",8))
            {
            	  int nums=1;				  
		  int len= strlen(cmd);
		  
		  if(len>=10)
		  	nums = atoi(&cmd[9]);
		  
                start_pps_ocs_procedure(0, nums);
                PrintCmdOK();
                return 1;
            }
           if(!strncasecmp(cmd,"test smsocs",11))
            {
                start_pps_ocs_procedure(1, 1);
                PrintCmdOK();
                return 1;
            }
           if(!strncasecmp(cmd,"test rest",9))
            {
            	  int type = atoi(&cmd[10]);
				  
		  pps_rest_test_process(type);
		  
                PrintCmdOK();
                return 1;
            }

	    if(!strncasecmp(cmd,"set max duration=",17))
            {
                ppsParam.max_call_duration = atoi(&cmd[17]);
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            
            if(!strncasecmp(cmd,"card request",12))
            {
                RechargeRequestProc(0);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"with crm yes",12))
            {
                ppsParam.serviceControl.work_with_crm = 1;
				
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"with crm no",11))
            {
                ppsParam.serviceControl.work_with_crm = 0;

                SavePPSParam();
                PrintCmdOK();
                return 1;
            }

             if(!strncasecmp(cmd,"change expiry day",17))
            {
                ppsParam.serviceControl.not_change_expiry_date_when_transfer= 0;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
             if(!strncasecmp(cmd,"not change expiry day",21))
            {
                ppsParam.serviceControl.not_change_expiry_date_when_transfer= 1;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"onct=yes",8))
            {
                ppsParam.serviceControl.only_normal_can_transfer_balance = 1;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"onct=no",7))
            {
                ppsParam.serviceControl.only_normal_can_transfer_balance= 0;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }

            if(!strncasecmp(cmd,"import user data",16))
            {
		  SetLoadFileFlag(1);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"import tariff data",18))
            {
		  SetLoadFileFlag(3);
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"max duration=",13))
            {
		  ppsParam.serviceControl.max_call_duration = atoi(&cmd[13]);
		  SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"ans timer=",10))
            {
		  ppsParam.serviceControl.answer_timer = atoi(&cmd[10]);
		  if(ppsParam.serviceControl.answer_timer<20)
		  	ppsParam.serviceControl.answer_timer = 20;
		  
		  SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strncasecmp(cmd,"gy timer0=",10))
            {
		  pps_set_gy_delay_timer(0, atoi(&cmd[10]));
		  SavePPSParam();
                PrintCmdOK();
                return 1;
            }
           if(!strncasecmp(cmd,"gy timer1=",10))
            {
		  pps_set_gy_delay_timer(1, atoi(&cmd[10]));
		  SavePPSParam();
                PrintCmdOK();
                return 1;
            }
           if(!strncasecmp(cmd,"gy postpaid=",12))
            {
		  ppsParam.serviceControl.gy_postpaid_flag = atoi(&cmd[12]);
		  SavePPSParam();
                PrintCmdOK();
                return 1;
            }
          if(!strncasecmp(cmd,"st_llbn=",8))
            {
		  pps_set_all_balance(atoi(&cmd[8]));
                PrintCmdOK();
                return 1;
            }
          if(!strncasecmp(cmd,"set volume=",11))
            {
		  ppsParam.max_quota_per_request = atoi(&cmd[11]);
		  SavePPSParam();

                PrintCmdOK();
		  return 1;
          }
          if(!strncasecmp(cmd,"set post volume=",16))
            {
		  ppsParam.data_quota_as_postpaid = atoi(&cmd[16]);
		  SavePPSParam();

                PrintCmdOK();
		  return 1;
          }
          if(!strncasecmp(cmd,"adaptor=",8))
            {
		  ppsParam.scf_work_as_adaptor = atoi(&cmd[8]);
		  SavePPSParam();
#ifdef _ZED_NO_CALLER_NUM_AND_EXPIRY_DATE_
		  if (ppsParam.scf_work_as_adaptor)
		  {
            read_yx_ussd_param();
		  }
#endif
                PrintCmdOK();
	         return 1;
          }
          if(!strncasecmp(cmd,"vcs enable=",11))
            {
		  ppsParam.scf_work_as_adaptor_with_vcs = atoi(&cmd[11]);
		  SavePPSParam();
		  
                PrintCmdOK();
	         return 1;
          }
          if(!strncasecmp(cmd,"no gy threshold=",16))		  	
            {
		  ocs_set_threshold_supported(atoi(&cmd[16]));
		  SavePPSParam();
		  
                PrintCmdOK();
	         return 1;
          }
          if(!strncasecmp(cmd,"gy report data=",15))
            {
		  ocs_set_gy_report_data_method(atoi(&cmd[15]));
		  SavePPSParam();
		  
                PrintCmdOK();
	         return 1;
          }
		 
          if(!strncasecmp(cmd,"smsc addr=",10))
            {
		  strcpy(ppsParam.serviceControl.smsc_addr, &cmd[10]);
		  SavePPSParam();
		  
                PrintCmdOK();
  		  return 1;
          }
          if(!strncasecmp(cmd,"sub sync test",13))
          {

		  pps_sub_sync_test();
		  
                PrintCmdOK();
  		  return 1;
          }
          if(!strncasecmp(cmd,"bt res=", 7))
            {
		  ppsParam.serviceControl.balance_transfer_resolution = atoi(&cmd[7]);
		  SavePPSParam();
		  
                PrintCmdOK();
  		  return 1;
          }
          if(!strncasecmp(cmd,"nai flag=", 9))
            {
		  ppsParam.serviceControl.nai_for_diameter_proxy = atoi(&cmd[9]);
		  SavePPSParam();
		  
                PrintCmdOK();
  		  return 1;
          }
          if(!strncasecmp(cmd,"ctr state=", 10))
            {
		  debugmib.display.control_state = atoi(&cmd[10]);
		  if(debugmib.display.control_state)
		  	debugmib.display.control_state_ts = tvnow.tv_sec;
		  
                PrintCmdOK();
  		  return 1;
          }
	     if(!strncasecmp(cmd,"srv_no_default",14))
	     {
	         ConfigureDefaultSerivceNumber();
	         SavePPSParam();
                PrintCmdOK();
		  return 1;
	     }
            if(!strncasecmp(cmd,"srv_no_cfg ivr",14))
            {

                ConfigureSerivceNumberPerCos(cmd, 0);
		  return 1;
            }
            if(!strncasecmp(cmd,"srv_no_cfg sms",14))
            {

                ConfigureSerivceNumberPerCos(cmd, 1);
		  return 1;
            }            			
            if(!strncasecmp(cmd,"srv_no_cfg ussd",15))
            {

                ConfigureSerivceNumberPerCos(cmd, 2);			
		  return 1;
            }            			
	     if(!strncasecmp(cmd,"srv_no_chk ivr",14))
            {

                memcpy(cvalue, &cmd[15], 2);
		  cvalue[2] = 0;
		  ivalue = atoi(cvalue);

 		  PrintCosServiceNumber(ivalue, 0);
	
		  return 1;
            }
	     if(!strncasecmp(cmd,"srv_no_chk sms",14))
            {

                memcpy(cvalue, &cmd[15], 2);
		  cvalue[2] = 0;
		  ivalue = atoi(cvalue);
		  
		  PrintCosServiceNumber(ivalue, 1);
		  return 1;
            }
	     if(!strncasecmp(cmd,"srv_no_chk ussd",15))
            {

                memcpy(cvalue, &cmd[16], 2);
		  cvalue[2] = 0;
		  ivalue = atoi(cvalue);

		  PrintCosServiceNumber(ivalue, 2);
		  return 1;
            }
	     if(!strncasecmp(cmd,"srv_no_test",11))
            {

                u8 cos_id=0,srv_id=0,call_id=0;
		 
                memcpy(cvalue, &cmd[12], 2);
		  cvalue[2] = 0;
		  cos_id = atoi(cvalue);

		  memcpy(cvalue, &cmd[15], 2);
		  cvalue[2] = 0;
		  srv_id = atoi(cvalue);

		  memcpy(cvalue, &cmd[18], 1);
		  cvalue[1] = 0;
		  call_id = atoi(cvalue);
		  
		  ivalue = GetSerivceNumberFlagPerCos(cos_id, srv_id, call_id);
		  DebugMsg(debugmib.display.asciiOut,"result=%d", ivalue);
		  return 1;
            }
            if(!strcmp(cmd,"enable smpp over tcp"))
            {
		   ppsParam.serviceControl.smpp_over_tcp_flag = 1;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
            if(!strcmp(cmd,"disable smpp over tcp"))
            {
		   ppsParam.serviceControl.smpp_over_tcp_flag = 0;
                SavePPSParam();
                PrintCmdOK();
                return 1;
            }
           if(!strcmp(cmd,"check smpp link mode?"))
            {
            	   if(ppsParam.serviceControl.smpp_over_tcp_flag)
			DebugMsg(debugmib.display.asciiOut,"SMPP over TCP link");
		   else
		   	DebugMsg(debugmib.display.asciiOut,"SMPP over UDP link");
                return 1;
            } 
           if(!strncasecmp(cmd,"cc method=", 10))
            {
 		   ppsParam.serviceControl.calling_card_method = atoi(&cmd[10]);
                SavePPSParam();
                PrintCmdOK();           
                return 1;
            } 
           if(!strcmp(cmd,"check cc method?"))
            {
            	   if(ppsParam.serviceControl.calling_card_method == 0)
			DebugMsg(debugmib.display.asciiOut,"calling card with PPS account");
		   else
		   	DebugMsg(debugmib.display.asciiOut,"calling card with rechage card");
                return 1;
            } 

       	if(!strncasecmp(cmd,"cc cos id=", 10))
            {
 		   ppsParam.serviceControl.calling_card_cos_id = atoi(&cmd[10]);
                SavePPSParam();
                PrintCmdOK();           
                return 1;
            } 
       	if(!strncasecmp(cmd,"test crm tariff=", 16))
            {
            		u_char call_type = 1;/*1=mo call, 2=mo sms, 3=data*/
			if(cmd[16] == 'c')
				call_type =  1;
			else if(cmd[16] == 's')
				call_type =  2;
			else if(cmd[16] == 'd')
				call_type =  3;
				
			query_tariff_test(call_type);
			PrintCmdOK();           
                return 1;
            } 
       	if(strstr(cmd,"reload prefix definition"))
            {
			ocs_get_crm_prefix_definition();
			PrintCmdOK();           
	             return 1;
       	}
       	if(!strncasecmp(cmd,"free seconds=", 13))
            {
			 ppsParam.serviceControl.crm_free_seconds = atoi(&cmd[13]);
	             SavePPSParam();
			 
			PrintCmdOK();           
	             return 1;
       	}
       	if(!strncasecmp(cmd,"status vd=", 10))
            {
			 ppsParam.serviceControl.crm_status_validity = atoi(&cmd[10]);
	             SavePPSParam();
			 
			PrintCmdOK();           
	             return 1;
       	}
       	if(!strncasecmp(cmd,"days before vd=", 15))
            {
			 ppsParam.serviceControl.crm_days_before_validity = atoi(&cmd[15]);
	             SavePPSParam();
			 
			PrintCmdOK();           
	             return 1;
       	}			
       	if(!strncasecmp(cmd,"sms for event=", 14))
            {
			 ppsParam.serviceControl.crm_notification_for_event = atoi(&cmd[14]);
	             SavePPSParam();
			 
			PrintCmdOK();           
	             return 1;
       	}
       	if(!strncasecmp(cmd,"crm tariff=", 11))
            {
            		char prefix[128], areaName[32]="";
			int call_type;
			ocs_get_charge_prefix(&cmd[11], prefix, areaName, &call_type);
			 
			PrintCmdOK();           
	             return 1;
       	}
       	if(!strncasecmp(cmd,"cellid in ccr=", 14))
            {
 		   ppsParam.serviceControl.cellid_present_in_ccr = atoi(&cmd[14]);
                SavePPSParam();
                PrintCmdOK();           
                return 1;
            } 
		
		DebugMsg(debugmib.display.asciiOut,"ERROR COMMAND!");
        break;
        case 1: /* parameter */
        break;
        case 3:
            if(strcasecmp(cmd,"C")==0 || strcasecmp(cmd,"CLR")==0 || strcasecmp(cmd,"CLEAR")==0)
            {
                memset(debugmib.msgstatistics.send_sync,0,sizeof(int)*4);
                memset(debugmib.msgstatistics.recv_sync,0,sizeof(int)*4);
            }
        break;
        case 128: /* NOT USED  */
            if(strcasecmp(cmd,"C")==0)
            {
                debugmib.measurement.cdroverflow = 0;
                debugmib.measurement.blacklist = 0;
            }
        break;
        case 5: /* msg counter */
            if(strcasecmp(cmd,"C")==0 || strcasecmp(cmd,"CLR")==0 || strcasecmp(cmd,"CLEAR")==0)
            {
                debugmib.msgstatistics.CapOpen = 0;
                debugmib.msgstatistics.Initialdp[0] = 0;
                debugmib.msgstatistics.Initialdp[1] = 0;
                debugmib.msgstatistics.RequestBCSM[0] =0;
                debugmib.msgstatistics.RequestBCSM[1] =0;
                debugmib.msgstatistics.ApplyCharging[0] = 0;
                debugmib.msgstatistics.ApplyCharging[1] = 0;
	      //debugmib.msgstatistics.Continue[0]  = 0;
	      //debugmib.msgstatistics.Continue[1]  = 0;
                debugmib.msgstatistics.ActivityS = 0;
                debugmib.msgstatistics.ActivityR = 0;
                debugmib.msgstatistics.ChargingReport[0] = 0;
                debugmib.msgstatistics.ChargingReport[1] = 0;
                debugmib.msgstatistics.ReportBCSM[0] = 0;
                debugmib.msgstatistics.ReportBCSM[1] = 0;
                debugmib.msgstatistics.Answered[0]  = 0;
                debugmib.msgstatistics.Answered[1]  = 0;
                debugmib.msgstatistics.Disconnect[0] = 0;
                debugmib.msgstatistics.Disconnect[1] = 0;
                debugmib.msgstatistics.Releasecall[0] = 0;
                debugmib.msgstatistics.Releasecall[1] = 0;
                debugmib.msgstatistics.Inquire = 0;
                debugmib.msgstatistics.Recharge = 0;
                debugmib.msgstatistics.ReadCard = 0;
                debugmib.msgstatistics.ReadCardRsp = 0;
                debugmib.msgstatistics.WriteCard = 0;
                debugmib.msgstatistics.WriteCardRsp = 0;
                debugmib.msgstatistics.Playannounce = 0;
                debugmib.msgstatistics.Accountinfo = 0;
                debugmib.msgstatistics.Accinfoack = 0;
            }
        break;
        case 6:
            if(strcasecmp(cmd,"C")==0 || strcasecmp(cmd,"CLR")==0 || strcasecmp(cmd,"CLEAR")==0)
            {
                debugmib.msgstatistics.InitialDP_SMS = 0;
                debugmib.msgstatistics.Request_SMS = 0;
                debugmib.msgstatistics.Continue_SMS = 0;
                debugmib.msgstatistics.Report_SMS = 0;
                debugmib.msgstatistics.Release_SMS = 0;
                debugmib.msgstatistics.Conference = 0;
                debugmib.msgstatistics.ThirdParty = 0;
                debugmib.msgstatistics.ConnectToResource = 0;
                debugmib.msgstatistics.PlayAnnouncement = 0;
                debugmib.msgstatistics.EstablishTemporaryConnection = 0;
                debugmib.msgstatistics.AssistInstructions = 0;
                debugmib.msgstatistics.PCUI = 0;
                debugmib.msgstatistics.PCUI_rsp = 0;
                debugmib.msgstatistics.SpecialResourceReport = 0;
                debugmib.msgstatistics.DisconnectForwardConnection = 0;
                debugmib.msgstatistics.smInquiry=0;
                debugmib.msgstatistics.smRecharge=0;
            }
        break;
        case 7: /* cdr&csta counter */
            if(strcasecmp(cmd,"C")==0)
            {               
                debugmib.msgstatistics.update_card_request = 0;
                debugmib.msgstatistics.update_card_error_msg = 0;
                debugmib.msgstatistics.update_card_ok = 0;
                debugmib.msgstatistics.update_card_no_card = 0;
                debugmib.msgstatistics.update_card_used = 0;
                debugmib.msgstatistics.update_card_expired = 0;
                debugmib.msgstatistics.update_card_sys_failure = 0;
            }
        break;
        default:
        break;
    }
    
    return 1;
}

int
GetParamRechargeAttemptLimit()
{
    return ppsParam.serviceControl.maxRechargeFailedNum;
}

int
UserNumberIncrement()
{
  char buf[32]="";
  debugmib.measurement.usernumber ++;
  ramshm_ptr->index_data.free_index.user_counter++;
  
  sprintf(buf,"%08ld",debugmib.measurement.usernumber);
  AsciiToBcd(&debugmib.measurement.user_number[0],buf,8);
  return 1;
}

int
UserNumberDecrement()
{
  char buf[32]="";
  debugmib.measurement.usernumber --;
  ramshm_ptr->index_data.free_index.user_counter --;
  if(debugmib.measurement.usernumber<0)
  	debugmib.measurement.usernumber =0;
  if(ramshm_ptr->index_data.free_index.user_counter<0)
	  ramshm_ptr->index_data.free_index.user_counter = 0;
  sprintf(buf,"%08ld",debugmib.measurement.usernumber);
  AsciiToBcd(&debugmib.measurement.user_number[0],buf,8);
  return 1;
}


int  SetDatabase(BYTE oidLen, DWORD *oid, BYTE *pdata, u_short dataLen)
{
	int dbid;


	PutLogFunID("SetDatabase");
    
	dbid = oid[12];
	switch(dbid)
	{
		case 1: /* subs index */
			return GetRamData(oidLen,oid,pdata,dataLen);
		break;
		case 2: /* subs data */
			return SetUserDataByOMC(oidLen,oid,pdata,dataLen);
		break;
		case 3: /* recharge card index */
			return GetRamData(oidLen,oid,pdata,dataLen);
		break;
		case 4: /* recharge card data */
			return SetPrepaidCardByOMC(oidLen,oid,pdata,dataLen);
		break;
		case 5: /* third party recharge */
			//return thirdPartyChargeReqeust(oidLen,oid,pdata,dataLen);
		break;
		case 6: 
		break;
		case 7: /* daily report */
		break;
		case 8:
		break;
		case 9: /* conference */
			 return ConferenceRequest(oidLen,oid,pdata,dataLen);
		break;
	}

	return dataLen;
}

int GetDatabase(BYTE oidLen, DWORD *oid, BYTE *pdata, BYTE *vartype)
{
	PutLogFunID("GetDatabase");

	*vartype = 0x04;
	if (oidLen<14)
	{
		return -1;
	}
	switch(oid[12])
	{
		case 6:	
			return GetUsedPrepaidCard(oidLen,oid,pdata,vartype);	  
		break;
		case 7:
			return GetDailyReport(oidLen,oid,pdata,vartype);				
		break;
	}
	return -1;
}

/* ---------------------------------*/
int SetReplaceSubsDataFlag(int flag)
{
	if(flag<0) return 0;
	replaceableFlag = flag;
	return 1;
}

/* ---------------------------------*/
int ImportUserData(char *userdata)
{
	_prepaid_info	*shmp=NULL,
					*shmp_old=NULL,
					prepaid_info;
	char			buf[BUFSIZE]={""},*token,msisdn[32];
//	char			chead[64]={""},ctail[64]={""},bufhead[32]="";
	int				head,tail=0,retval=0,index=0;
	int				len, i;
	int				chargeFlag=0,difbalance=0,dif_promo_balance=0;
	int				favorite_numbers = 0;
	const  char		delimiter[]=",;";
	time_t			nowtime=0;
		
	PutLogFunID("ImportUserData");
	
    	shmp = &prepaid_info;
	memset(&prepaid_info,0,sizeof(_prepaid_info));

    	token  = buf;

	if(strlen(userdata)<10)
	{
		if(load_file.log_fp[_FILE_ID_USER_DATA] != NULL)
		{
			fprintf(load_file.log_fp[_FILE_ID_USER_DATA],
						"Error: Wrong record len=%zu : %s\n",
						strlen(userdata), userdata);
		}
		return 0;
	}

	int up_balance_only_flag = 0, first_id_not_nul = 0, invalid_record_flag = 0;
	while(index<MAX_PPS_USER_FIELD_NUMS)
	{

		token = strsep((char **)&(userdata),delimiter);
		if(token == NULL )
		{
			if((index == 0) || ((replaceableFlag == 0) && (index<21)))
			{
				if(load_file.log_fp[_FILE_ID_USER_DATA] != NULL)
				{
					fprintf(load_file.log_fp[_FILE_ID_USER_DATA],
						"Error: Wrong record  : %s,index=%d\n",userdata,index);
				}
				return 0;
			}
			else 
			{
				if (index < 21)// invalid record when replaceableFlag == 1
				{
					invalid_record_flag = 1;
				}
				index++;
				continue;
			}
		}

		if (index > 0)
		{
			if (first_id_not_nul == 0)
			{
				if (strlen(token) > 0){
					first_id_not_nul = index;
					if ((index == 0x03) && replaceableFlag)// first index is balance
					{
						up_balance_only_flag = 1;// set update balance only
					}
				}
			}
		}
		strcpy(buf,token);

		switch(index)
		{
			case 0x00: //msisdn
				len = strlen(buf);
				if(len>MAX_MSISDN_ALEN || len<MIN_MSISDN_ALEN) 
				{
					if(load_file.log_fp[_FILE_ID_USER_DATA] != NULL)
					{
						fprintf(load_file.log_fp[_FILE_ID_USER_DATA],
								"Error: Wrong msisdn length  : %s\n",buf);
						fprintf(load_file.log_fp[_FILE_ID_USER_DATA],
								"msisdn should be <=%d digits and >=%d digits\n",
								MAX_MSISDN_ALEN,MIN_MSISDN_ALEN);
					} 
					return 0;
				}
				for(i=0;i<len;i++)
				{
					if(buf[i]>'9' || buf[i]<'0' || buf[0] =='0') 
					{
						if(load_file.log_fp[_FILE_ID_USER_DATA] != NULL)
						{
							fprintf(load_file.log_fp[_FILE_ID_USER_DATA],
								 "Error: Wrong msisdn format:%s\n",buf);
						} 
						return 0;
					}
				}
				strcpy(msisdn,buf);

				msisdn[len] = 0;

				StringCat(buf,"F",MAX_MSISDN_ALEN);
				AsciiToBcd(shmp->msisdn,buf,MAX_MSISDN_ALEN);
				if(len %2 ==0)
					shmp->msisdn[len/2] = 0xFF;
				else
					shmp->msisdn[len/2] |= 0x0F;
				shmp->msisdn[MAX_MSISDN_BLEN-1] = len;         
			break;
			case 0x01:
				memcpy(shmp->pin,buf,6);
			break;
			case 0x02: //status
				shmp->status = atoi(buf);
			break;
			case 0x03: //balance
				shmp->balance = atoi(buf);
				if(shmp->balance>ppsParam.serviceControl.maxBalance)
				{
					if(load_file.log_fp[_FILE_ID_USER_DATA] != NULL)
					{
						fprintf(load_file.log_fp[_FILE_ID_USER_DATA],
							"Error: Balance=%.0f is too big  : should be 0-%d\n",
							shmp->balance,ppsParam.serviceControl.maxBalance);
					}
					return 0;
				}
			break;
			case 0x04:
				shmp->promo_balance = atoi(buf);
				if(shmp->promo_balance>ppsParam.serviceControl.maxBalance)
				{
					if(load_file.log_fp[_FILE_ID_USER_DATA] != NULL)
					{
						fprintf(load_file.log_fp[_FILE_ID_USER_DATA],
							"Error: Balance=%.0f is too big  : should be 0-%d\n",
							shmp->promo_balance,ppsParam.serviceControl.maxBalance);
					}
					return 0;
				}
			break;
			case 0x05: //overdraft
				shmp->overdraft = atoi(buf);
			break;
			case 0x06: //first used date
				string2ltime(&nowtime,buf);
				shmp->first_used_date = nowtime;
			break;
			case 0x07: //mo expiration date
				string2ltime(&nowtime,buf);
				shmp->mo_expiration_date = nowtime;
			break;
			case 0x08: //mt expiration date
				string2ltime(&nowtime,buf);
				shmp->mt_expiration_date = nowtime;
			break;
			case 0x09:
				string2ltime(&nowtime,buf);
				shmp->promo_date = nowtime;
			break;
			case 0x0a:
				shmp->cos_id = atoi(buf)%MaxCosTableItem;
				if(ppsParam.serviceControl.cos_group_flag)
					shmp->cos_id = atoi(buf)%MAX_ZONE_NUM_NEW;
			break;
			case 0x0b:
				shmp->rent_type = atoi(buf);
			break;
			case 0x0c:
			case 0x0d:
			case 0x0e:
			case 0x0f: /* should from 1- MAX_TAIRFF_LOCATION */
				shmp->home_location[index-12] = atoi(buf);
			break;
			case 0x10: //cug id
				shmp->cug_id = atoi(buf)%(MAX_CUG_ID+1);
			break;
			case 0x11: 
				shmp->language_type = atoi(buf);
			break;
			case 0x12: //free sm counter
				shmp->recharge_fail = atoi(buf);
			break; 
			case 0x13: 
				shmp->free_sm_counter = atoi(buf);
			break;
			case 0x14: //latch flag
				shmp->latch_flag = 0;
			break;
			case 0x15: //favorite number flag
				shmp->favorite_numbers = 0;
			break;
			case 0x16: //fav change counter
				shmp->fav_change_counter = 0;
			break;
			case 0x17:
			case 0x18:
			case 0x19:
			case 0x1a:
			case 0x1b:
				len = strlen(buf);
				if(len>MAX_MSISDN_ALEN || len<MIN_MSISDN_ALEN) 
				{
					index++;
					continue ;
				}
				StringCat(buf,"F",MAX_MSISDN_ALEN);
				buf[MAX_MSISDN_ALEN]=0;
				shmp->favorite_number[index-23][MAX_MSISDN_BLEN-1] = len;
				AsciiToBcd(shmp->favorite_number[index-23],buf,MAX_MSISDN_ALEN);
				favorite_numbers++;
				if(favorite_numbers>MAX_FAVORITE_NUMBERS)
				  favorite_numbers = MAX_FAVORITE_NUMBERS;
			break;
			case 61:
				shmp->bundle_info.data = atoll(buf);
			break;
			case 62:
				shmp->bundle_info.mo_voice = atoi(buf);
			break;
			case 63:
				shmp->bundle_info.mt_voice = atoi(buf);				
			break;
			case 64:
				shmp->bundle_info.sms = atoi(buf);				
			break;
			case 65:
				string2ltime(&nowtime,buf);
				shmp->bundle_info.validity_date= nowtime;				
			break;
			case 66:
				shmp->bundle_info.warning_sms_flag= atoi(buf);				
			break;
			default:
			break;
		}/* switch */
		index++;
	}//while

	if (invalid_record_flag && !up_balance_only_flag)
	{
		if(load_file.log_fp[_FILE_ID_USER_DATA] != NULL)
		{
			fprintf(load_file.log_fp[_FILE_ID_USER_DATA],
				"Error: Wrong record  : %s,index=%d\n",userdata,index);
		}
		return 0;
	}

	shmp->favorite_numbers = favorite_numbers;
	
	retval = FindPPSSubsStoreIndex(atoll(msisdn));
	
	if(retval>=0)
	{
		if(load_file.log_fp[_FILE_ID_USER_DATA] != NULL)
		{
			if(replaceableFlag == 0)
			{
				fprintf(load_file.log_fp[_FILE_ID_USER_DATA],
					"Error: account %s is in use, cann't be replaced!\n",msisdn);
			}
			else
			{
				fprintf(load_file.log_fp[_FILE_ID_USER_DATA],
					"Error: account %s is in use, replace it!\n",msisdn);
			}
		} 
		if(replaceableFlag == 0)
			return 0;			
		head = retval / UNIT_STORE_ACCOUNT;
		tail = retval % UNIT_STORE_ACCOUNT;
		shmp_old = &ramshm_ptr->prepaid_data.prepaid_info[head][tail];

		if (up_balance_only_flag)
		{
			if(shmp->balance != shmp_old->balance && masterFlag)
			{
				chargeFlag |= 0x01;
				difbalance = shmp->balance-shmp_old->balance;

				double new_balance = shmp->balance;
				memcpy(shmp, shmp_old, sizeof(_prepaid_info));
				shmp->balance = new_balance;
				ModifyPPSSubsData(shmp);
			}
		}
		else
		{
			if(shmp->balance != shmp_old->balance && masterFlag)
			{
				chargeFlag |= 0x01;
				difbalance = shmp->balance-shmp_old->balance;
			}
			if(shmp->promo_balance != shmp_old->promo_balance && masterFlag)
			{
				chargeFlag |= 0x02;
				dif_promo_balance = shmp->promo_balance - shmp_old->promo_balance;
			}
			
			ModifyPPSSubsData(shmp);
		}
	}
	else
	{
		if (up_balance_only_flag)
		{
			if(load_file.log_fp[_FILE_ID_USER_DATA] !=NULL)
			{
				fprintf(load_file.log_fp[_FILE_ID_USER_DATA],
				   "Error: can't create user from balance only record!\n");
			}
			return 0;
		}
		if(debugmib.measurement.usernumber>=ppsParam.serviceControl.licenseduser)
		{
			if(load_file.log_fp[_FILE_ID_USER_DATA] !=NULL)
			{
				fprintf(load_file.log_fp[_FILE_ID_USER_DATA],
				   "Error: exist users exceed lincensed user(%u)!\n",
				   ppsParam.serviceControl.licenseduser);
			}
			return 0;
		}

		retval = AddPPSSubsData(shmp);
		head = retval / UNIT_STORE_ACCOUNT;
		tail = retval % UNIT_STORE_ACCOUNT;

		if(shmp->balance !=0)
		{
			chargeFlag |= 0x01;
			difbalance = shmp->balance;
		}
		if(shmp->promo_balance !=0)
		{
			chargeFlag |= 0x02;
			dif_promo_balance = shmp->promo_balance;
		}
	}

	if(masterFlag && (chargeFlag & 0x01))
	{
		SendOperatorRechargeCDR(
			head,
			tail,
			difbalance);
	}
	if(masterFlag && (chargeFlag & 0x02))
	{
		SendOperatorRechargeCDR(
			head,
			tail,
			dif_promo_balance);
	}

	debugmib.display.sysState = _SYS_STATE_NORMAL;
	return 1;
}


/* ----------------+ Upload data procedure +---------------*/
/* ----------------+ Upload data procedure +---------------*/

int delblankspace(char *astr, char *bstr)
{
   int i,j=0,len ;

	PutLogFunID("delblankspace");

   len = strlen(bstr);
   for(i=0;i<len;i++)
   {
     if(bstr[i] !=' ')
	   astr[j++] = bstr[i];
   }
   astr[j] = 0;
   return 1;
}

int RentTariffSet(char *record)
{
	_tariff_data  *shmp=&ramshm_ptr->tariff_data;

	char			buf[BUFSIZE],*off,s[BUFSIZE],description[32];
	int				start=0,index=0,slen=0,len;
	int				id=0,ptariff=0,tariff_id,
					date=0;
	float			amount=0;

	PutLogFunID("RentTariffSet");

   if(strlen(record)<2) return 0;
   strcpy(s,record);
   while(index<6)
   {
     if((off=strchr(s,','))==NULL)
	  off = strchr(s,';');
	 slen = off - s;
	 if(slen>=strlen(record)) slen = strlen(record)-start;
	 memcpy(buf,s,slen);
	 buf[slen] = 0;
	 start += slen+1;
     strcpy(s,&record[start]);
	 switch(index)
	 {
	   case 0x00:
		 tariff_id = atoi(buf);
	   break;
	   case 0x01: /* id */
	     id = atoi(buf);
	   break;
	   case 0x02:	  /* description  */
		 len = strlen(buf);
		 if(len<=16)
		 {
		     strcpy(description,buf);
		 }else
		 {
			memcpy(description,buf,16);
			description[16]	= 0;
		 }

	   break;
	   case 0x03: /* type */
	     ptariff = atoi(buf);
	   break;
	   case 0x04: /* rent day */
	     date = atoi(buf);
	   break;
	   case 0x05:/* amount */
	     amount = atof(buf);
	   break;
	   default:
	   break;
	 }/* switch */
	 index++;
   }
	
   if(tariff_id>=MAX_ZONE_NUM) return 0;
   if(id>MAX_RENT_TARIFF || id<1) return 0;
   shmp->rent_tariff[tariff_id][id-1].id                   = id;
   strcpy(shmp->rent_tariff[tariff_id][id-1].description,description);
   shmp->rent_tariff[tariff_id][id-1].rent_type            = ptariff;
   shmp->rent_tariff[tariff_id][id-1].charge_date	 = date;
   shmp->rent_tariff[tariff_id][id-1].rent_amount          = amount;
   return 1;
}

int ImportRentTariffData(FILE *fp)
{
   char   record[512];

   while(fgets(record,512,fp) != (char *)0) 
   {
//	 fprintf(stderr,"record:%s\n",record);
	 RentTariffSet(record);
   }

   return 1;
}


void pps_print_error(u_char logMsisdnFlag,const char *fmt, ...)
{
	va_list 	ap;
	char 	buf[2048];

	if(logMsisdnFlag == 0)	
	{
		if(!(logMsgFlag & 0x01) || (logMsgFlag == 0x08))
			return;
	}
	else if(!(logMsgFlag & 0x08))
		return;
		
	sprintf(buf, "\33[31m");
	va_start(ap, fmt);
	
	vsprintf(buf+strlen(buf), fmt, ap);

	strcat(buf, "\33[0m\n\r");

	if((strlen(debugmib.display.asciiOut) + strlen(buf)) < PPS_ASCII_OUT_LEN)
		strcat(debugmib.display.asciiOut, buf);
	else
		strcpy(debugmib.display.asciiOut, buf);
	
	va_end(ap);
	
}


void pps_print_event(u_char logMsisdnFlag,const char *fmt, ...)
{
	va_list 	ap;
	char 	buf[2048];

	if(logMsisdnFlag == 0)	
	{
		if((logMsgFlag == 0x01) || (logMsgFlag == 0) || (logMsgFlag == 0x08))
			return;
	}	
	else if(!(logMsgFlag & 0x08))
		return;
	
	sprintf(buf, "\33[37m");
	va_start(ap, fmt);	
	
	vsprintf(buf+strlen(buf), fmt, ap);

	strcat(buf, "\33[0m\n\r");
	if((strlen(debugmib.display.asciiOut) + strlen(buf)) < PPS_ASCII_OUT_LEN)
		strcat(debugmib.display.asciiOut, buf);
	else
		strcpy(debugmib.display.asciiOut, buf);

	va_end(ap);

	
}

int BasicTariffSet(char *record)
{
   	_tariff_data	*shmp=&ramshm_ptr->tariff_data;
   	char			buf[BUFSIZE],*off,s[BUFSIZE],bill_id[128];
   	int				start=0,index=0,slen=0,len;
   	int 			tariffID=0,u_time=0,m_unit=0,rindex=0;
   	int				call_type,free_time,discountID,cap_flag=0;
	float		  	b_fee_a=0,long_fee_a=0, b_fee_b=0,long_fee_b=0, b_fee_c=0,long_fee_c=0,adjustment=0;
   	char			area[128];
   	u32				tierLevel=0,units_a = 0,units_b = 0,units_c = 0;
	TIER_RATING		*pTierRating;
	
	PutLogFunID("BasicTariffSet");

   if(strlen(record)<10) return 0;   
   strcpy(s,record);
   while(index <= 0x13)
   {
     if((off=strchr(s,','))==NULL)
	  off = strchr(s,';');
	 slen = off - s;
	 
	 if(slen>=strlen(record)) slen = strlen(record)-start;

	 memcpy(buf,s,slen);
	 buf[slen] = 0;
	 start += slen+1;
	 
     	strcpy(s,&record[start]);
	 switch(index)
	 {
	   case 0x00: /* bill */
	     delblankspace(bill_id,buf);
	   break;
	   case 0x01:	     /* area */
	     len = strlen(buf);
		 if(len<=TARIFF_AREA_LEN-1)
		   strcpy(area,buf);
		 else{
		   memcpy(area,buf,TARIFF_AREA_LEN-1);
                area[TARIFF_AREA_LEN-1] = 0;
		 }
	   break;
	   case 0x02: /* tariffID */
	     tariffID = atoi(buf)%MAX_ZONE_NUM;
	   break;
	   case 0x03:
		 call_type = atoi(buf) % MAX_MO_CALLTYPE;
	   break;
	   case 0x04:
			discountID = atoi(buf)%MAX_DISCOUNT_ID;
	   break;
	   case 0x05:
		free_time = atoi(buf);
	   break;
	   case 0x06: /* unit_time */
	     u_time = atoi(buf);
	   break;
	   case 0x07: /* mini_time */
	     m_unit = atoi(buf);
	   break;	   
	   case 0x08: /* adjustment */
	     adjustment = atof(buf);
	   break;
   	   case 0x09:
	     cap_flag = atoi(buf);
	   break;	
	   case 0x0a: /* tier level */
	     tierLevel = atoi(buf);
	   break;
	   case 0x0b: /*  uints a */
	     units_a = atol(buf);
	   break;	   
	   case 0x0c: /* base_fee a */
	     b_fee_a = atof(buf);
	   break;
	   case 0x0d: /* long distance fee  a*/
	     long_fee_a = atof(buf);
	   break;
	   case 0x0e: /*  uints b */
	     units_b = atol(buf);
	   break;	   
	   case 0x0f: /* base_fee b */
	     b_fee_b = atof(buf);
	   break;
	   case 0x10: /* long distance fee  b*/
	     long_fee_b = atof(buf);
	   break;
	   case 0x11: /*  uints c */
	     units_c = atol(buf);
	   break;	   
	   case 0x12: /* base_fee c */
	     b_fee_c = atof(buf);
	   break;
	   case 0x13: /* long distance fee  c*/
	     long_fee_c = atof(buf);
	   break;
	   default:
	   break;
	 }/* switch */
	 index++;
   }

   	if(ppsParam.serviceControl.cos_group_flag)
    {
        rindex = zonenum[tariffID] ++;
        tariffID = (tariffID * COS_GROUP_NUM );
        tariffID += rindex/BILLING_BASE_NUM;
        rindex = rindex%BILLING_BASE_NUM;
        tariffID = tariffID%MAX_ZONE_NUM;
   	}
    else
		rindex = (zonenum[tariffID] ++)%BILLING_BASE_NUM;
	strcpy(shmp->bill_tariff[tariffID][rindex].bill_id,bill_id);
	shmp->bill_tariff[tariffID][rindex].tariffID		= tariffID;
	shmp->bill_tariff[tariffID][rindex].call_type		= call_type;
	shmp->bill_tariff[tariffID][rindex].discountID		= discountID;
	strcpy(shmp->bill_tariff[tariffID][rindex].area,area);
	shmp->bill_tariff[tariffID][rindex].free_time		= free_time;
	shmp->bill_tariff[tariffID][rindex].unit_time		= u_time;
	shmp->bill_tariff[tariffID][rindex].mini_unit		= m_unit;
	shmp->bill_tariff[tariffID][rindex].adjustment		= adjustment;
	shmp->bill_tariff[tariffID][rindex].cap_flag		= cap_flag;

	pTierRating = &shmp->bill_tariff[tariffID][rindex].tierRating;
   	pTierRating->tierNumber = tierLevel;	
   	pTierRating->rating[0].units = units_a;
   	pTierRating->rating[0].base_fee = b_fee_a;
   	pTierRating->rating[0].long_distance_fee = long_fee_a;
   	pTierRating->rating[1].units = units_b;
   	pTierRating->rating[1].base_fee = b_fee_b;
   	pTierRating->rating[1].long_distance_fee = long_fee_b;
   	pTierRating->rating[2].units = units_c;
   	pTierRating->rating[2].base_fee = b_fee_c;
   	pTierRating->rating[2].long_distance_fee = long_fee_c;
	return 1;
}

int ImportBasicTariffData(FILE *fp)
{
   char   record[512];

   memset(zonenum,0,sizeof(zonenum));   
   while(fgets(record,512,fp) != (char *)0) 
   {
     BasicTariffSet(record);
   }

   return 1;
}


int gprsTariffSet(char *record) 
{
   char           buf[BUFSIZE],*off,s[BUFSIZE];
   int		      start=0,index=0,slen=0;
   int 			  id=0,tariffid=0;
   _gprs_tariff	  gprsTariff;

	PutLogFunID("BillGPRSSet");

   if(strlen(record)<2) return 0;
   strcpy(s,record);
   while(index <= 0x08)
   {
     if((off=strchr(s,','))==NULL)
	  off = strchr(s,';');
	 slen = off - s;
	 if(slen>=strlen(record)) slen = strlen(record)-start;
	 memcpy(buf,s,slen);
	 buf[slen] = 0;
	 start += slen+1;
	 strcpy(s,&record[start]);
	 switch(index)
	 {		
	   	case 0x00:
			tariffid = atoi(buf);
			if(tariffid>=MAX_ZONE_NUM || tariffid<0) 
				return 0;
			gprsTariff.tariffID = tariffid;
	   	break;
	   	case 0x01:
			id = atoi(buf);
			if(id>MAX_GPRS_PREFIX || id<1) 
				return 0;
			gprsTariff.gprsID = id;
	   	break;			
	   	case 0x02: 
			gprsTariff.destIp = inet_addr(buf);
		break;
	   	case 0x03:
	   		gprsTariff.chargeUnit = atoi(buf);
	   	break;	  
	   	case 0x04: 
			gprsTariff.basicFee = atof(buf);
	   	break;
	 	case 0x05: 
			gprsTariff.miniChargeUnit = atoi(buf);
	   	break;
	   	case 0x06:
			gprsTariff.initialAdjustment = atof(buf);
	   	break;	   
	   	case 0x07:
	  		gprsTariff.cap_flag = atoi(buf)%MaxcapPlanTableItem;
   	   	break;	
	   	case 0x08:
			gprsTariff.discountID = atoi(buf)%MAX_DISCOUNT_ID;
	   	break;	   	   	   
	   	default:
	   	break;
	 }/* switch */
	 index++;
	}
	memcpy(&ramshm_ptr->tariff_data.gprs_tariff[tariffid][id-1],&gprsTariff,sizeof(_gprs_tariff));
	return 1;
}

int ImportGprsTariff(FILE *fp)
{
   char   record[512];

   memset(zonenum,0,sizeof(zonenum));   
   while(fgets(record,512,fp) != (char *)0) 
   {
     gprsTariffSet(record);
   }

   return 1;
}

int MTTariffSet(char *record)
{
   _tariff_data  	*shmp=&ramshm_ptr->tariff_data;
   char          	 buf[BUFSIZE],*off,s[BUFSIZE],bill_id[128];
   int		      	start=0,index=0,slen=0,len;
   int 			  	call_type,tariffID=0,u_time=0,m_unit=0,rindex=0,discountID,free_time;
   float		  	b_fee_a=0,long_fee_a=0, b_fee_b=0,long_fee_b=0, b_fee_c=0,long_fee_c=0,adjustment=0,cap_flag=0;
   char			  	area[128];
   u32				tierLevel=0,units_a = 0,units_b = 0,units_c = 0;
	TIER_RATING		*pTierRating;
		
	PutLogFunID("MTTariffSet");

   if(strlen(record)<10) return 0;   
   strcpy(s,record);
   while(index <= 0x13)
   {
     if((off=strchr(s,','))==NULL)
	  off = strchr(s,';');
	 slen = off - s;
	 
	 if(slen>=strlen(record)) slen = strlen(record)-start;

	 memcpy(buf,s,slen);
	 buf[slen] = 0;
	 start += slen+1;
	 
	strcpy(s,&record[start]);
	 switch(index)
	 {
	   case 0x00: /* bill */
	     delblankspace(bill_id,buf);
	   break;
	   case 0x01:	     /* area */
	     len = strlen(buf);
		 if(len<=TARIFF_AREA_LEN-1)
		   strcpy(area,buf);
		 else{
		   memcpy(area,buf,TARIFF_AREA_LEN-1);
               area[TARIFF_AREA_LEN-1] = 0;
		 }
	   break;
	   case 0x02: /* tariffID */
	     tariffID = atoi(buf)%MAX_ZONE_NUM;
	   break;
	   case 0x03:
		 call_type = atoi(buf);
	   break;
	   case 0x04:
			discountID = atoi(buf)%MAX_DISCOUNT_ID;
	   break;
	   case 0x05:
			free_time = atoi(buf);
	   break;
	   case 0x06: /* unit_time */
			u_time = atoi(buf);
	   break;
	   case 0x07: /* mini_time */
			m_unit = atoi(buf);
	   break;	  
	   case 0x08: /* adjustment */
	     adjustment = atof(buf);
	   break;	   
	   
	   case 0x09: /* tier level */
	     tierLevel = atoi(buf);
	   break;
	   case 0x0a: /*  uints a */
	     units_a = atol(buf);
	   break;	   
	   case 0x0b: /* base_fee a */
	     b_fee_a = atof(buf);
	   break;
	   case 0x0c: /* long distance fee  a*/
	     long_fee_a = atof(buf);
	   break;
	   case 0x0d: /*  uints b */
	     units_b = atol(buf);
	   break;	   
	   case 0x0e: /* base_fee b */
	     b_fee_b = atof(buf);
	   break;
	   case 0x0f: /* long distance fee  b*/
	     long_fee_b = atof(buf);
	   break;
	   case 0x10: /*  uints c */
	     units_c = atol(buf);
	   break;	   
	   case 0x11: /* base_fee c */
	     b_fee_c = atof(buf);
	   break;
	   case 0x12: /* long distance fee  c*/
	     long_fee_c = atof(buf);
	   break;
	   case 0x13: /* cap flag */
	   	cap_flag = atoi(buf);
	   break;
	   default:	   
	   break;
	 }/* switch */
	 index++;
   }

   rindex = (zonenum[tariffID] ++)%CALLED_BASE_NUM;
   strcpy(shmp->called_tariff[tariffID][rindex].bill_id,bill_id);
   shmp->called_tariff[tariffID][rindex].tariffID		= tariffID;
   shmp->called_tariff[tariffID][rindex].call_type		= call_type;
   shmp->called_tariff[tariffID][rindex].discountID		= discountID;
   strcpy(shmp->called_tariff[tariffID][rindex].area,area);
   shmp->called_tariff[tariffID][rindex].free_time		= free_time;
   shmp->called_tariff[tariffID][rindex].unit_time		= u_time;
   shmp->called_tariff[tariffID][rindex].mini_unit		= m_unit;
   shmp->called_tariff[tariffID][rindex].adjustment		= adjustment;
   shmp->called_tariff[tariffID][rindex].cap_flag		= cap_flag;

   pTierRating = &shmp->called_tariff[tariffID][rindex].tierRating;
   pTierRating->tierNumber = tierLevel;
   pTierRating->rating[0].units = units_a;
   pTierRating->rating[0].base_fee = b_fee_a;
   pTierRating->rating[0].long_distance_fee = long_fee_a;
   pTierRating->rating[1].units = units_b;
   pTierRating->rating[1].base_fee = b_fee_b;
   pTierRating->rating[1].long_distance_fee = long_fee_b;
   pTierRating->rating[2].units = units_c;
   pTierRating->rating[2].base_fee = b_fee_c;
   pTierRating->rating[2].long_distance_fee = long_fee_c;
   return 1;
}

int ImportMTTariff(FILE *fp)
{
   char   record[512];

   memset(zonenum,0,sizeof(zonenum));   
   while(fgets(record,512,fp) != (char *)0) 
   {
     MTTariffSet(record);
   }

   return 1;
}
/* ------- bill tariff ----*/
int BillDiscountSet(char *record)
{
   _tariff_data  *shmp=&ramshm_ptr->tariff_data;

   char           buf[BUFSIZE],*off,s[BUFSIZE];
   int		      start=0,index=0,slen=0;
   int 			  did=0,hour=0,sun=0,mon=0,tue=0,wed=0,thu=0,fri=0,sat=0,hol=0;

	PutLogFunID("BillDiscountSet");

   if(strlen(record)<10) return 0;
   strcpy(s,record);
   while(index<10)
   {
     if((off=strchr(s,','))==NULL)
	  off = strchr(s,';');
	 slen = off - s;
	 if(slen>=strlen(record)) slen = strlen(record)-start;

	 memcpy(buf,s,slen);
	 buf[slen] = 0;
	 start += slen+1;
     strcpy(s,&record[start]);
	 switch(index)
	 {
	   case 0x00:
	     did = atoi(buf);
		 if(did>=MAX_DISCOUNT_ID) return 0;
	   break;
	   case 0x01:
	     hour = atoi(buf);
	   break;
	   case 0x02:
	     mon = atoi(buf);
	   break;
	   case 0x03:
	     tue = atoi(buf);
	   break;
	   case 0x04:
	     wed = atoi(buf);
	   break;
	   case 0x05:
	     thu = atoi(buf);
	   break;
	   case 0x06:
	     fri = atoi(buf);
	   break;
	   case 0x07:
	     sat = atoi(buf);
	   break;
	   case 0x08:
	     sun = atoi(buf);
	   break;
	   case 0x09:
	     hol = atoi(buf);
	   break;
	   default:
	   break;
	 }/* switch */
	 index++;
   }
   shmp->bill_discount[did][hour].discount_id = did;
   shmp->bill_discount[did][hour].hours       = hour;
   shmp->bill_discount[did][hour].days[0]     = sun;
   shmp->bill_discount[did][hour].days[1]     = mon;
   shmp->bill_discount[did][hour].days[2]     = tue;
   shmp->bill_discount[did][hour].days[3]     = wed;
   shmp->bill_discount[did][hour].days[4]     = thu;
   shmp->bill_discount[did][hour].days[5]     = fri;
   shmp->bill_discount[did][hour].days[6]     = sat;
   shmp->bill_discount[did][hour].days[7]     = hol;

   return 1;
}

int ImportDiscountTariffData(FILE *fp)
{
   char   record[512];

   while(fgets(record,512,fp) != (char *)0) 
   {
	// fprintf(stderr,"record:%s\n",record);
     BillDiscountSet(record);
   }

   return 1;
}


int HolidayTariffSet(char *record)
{
   _tariff_data  *shmp=&ramshm_ptr->tariff_data;

   char           buf[BUFSIZE],*off,s[BUFSIZE];
   int		      start=0,index=0,slen=0;
   int			  mon=0,day=0;

	PutLogFunID("HolidayTariffSet");

   if(strlen(record)<2) return 0;
   strcpy(s,record);
   while(index<2)
   {
     if((off=strchr(s,','))==NULL)
	  off = strchr(s,';');
	 slen = off - s;
	 if(slen>=strlen(record)) slen = strlen(record)-start;

	 memcpy(buf,s,slen);
	 buf[slen] = 0;
	 start += slen+1;
     strcpy(s,&record[start]);
	 switch(index)
	 {
	   case 0x00:
	     mon = atoi(buf);
	   break;
	   case 0x01:
	     day = atoi(buf);
	   break;
	   default:
	   break;
	 }/* switch */
	 index++;
   }
   
   if(mon>=1 && mon<13 && day<32 && day>=1)
   {
      shmp->holiday_flag[mon-1][day-1].month = mon;
	  shmp->holiday_flag[mon-1][day-1].day   = day;
	  shmp->holiday_flag[mon-1][day-1].mflag = 1;
   }
   return 1;
}

int ImportHolidayTariffData(FILE *fp)
{
   char   record[512];
   FILE  *fpW=NULL;
   
  // memset(&ramshm_ptr->tariff_data.holiday_flag,
  // 	0,
  // 	sizeof(_bill_holiday)*12*31);
   fpW = fopen(HOLIDAY_CSV,"w");
   
   while(fgets(record,512,fp) != (char *)0) 
   {
     HolidayTariffSet(record);
	 if(fpW !=NULL )
		 fprintf(fpW,"%s",record);
   }
	if(fpW != NULL)
			fclose(fpW);

   return 1;
}

int CommTariffSet(char *record)
{
   _tariff_data  *shmp=&ramshm_ptr->tariff_data;
   char           buf[BUFSIZE]={""},*token;
   int			  index=0,tariff_id=0;
   const  char    delimiter[]=",;";

	PutLogFunID("CommTariffSet");

   token  = buf;

   if(strlen(record)<10){
	 return 0;
   }

   while(index <= 0x18)
   {

     token = strsep((char **)&(record),delimiter);
     if(token == NULL){
	   return 0;	  
	 }
	 strcpy(buf,token);

	 switch(index)
	 {
	   case 0x00:
		 tariff_id = atoi(buf);
		 if(tariff_id>=MAX_ZONE_NUM || tariff_id<0) return 0;
	   break;
	   case 0x01: /* cf charge unit */
	     shmp->comm_tariff[tariff_id].cfChargeUnit = atoi(buf);
	   break;
	   case 0x02: /* cf charge amount */
	     shmp->comm_tariff[tariff_id].cfChargeAmount = atof(buf);
	   break;
	   case 0x03: /* roaming charge unit */
	     shmp->comm_tariff[tariff_id].roamingChargeUnit = atoi(buf);
	   break;
	   case 0x04: /* roaming charge amount */
	     shmp->comm_tariff[tariff_id].roamingChargeAmount = atof(buf);
	   break;
	   case 0x05: /* cc charge unit */
	     shmp->comm_tariff[tariff_id].ccChargeUnit = atoi(buf);
	   break;
	   case 0x06: /* cc charge amount */
	     shmp->comm_tariff[tariff_id].ccChargeAmount = atof(buf);
	   break;	   
	   case 0x07:
	     shmp->comm_tariff[tariff_id].cugChargeUnit = atoi(buf);
	   break;
	   case 0x08:
		 shmp->comm_tariff[tariff_id].cugChargeAmount = atof(buf);
	   break;
	   case 0x09:
		 shmp->comm_tariff[tariff_id].favEditChargeAmount = atof(buf);
	   break;
	   case 0x0a:
		 shmp->comm_tariff[tariff_id].favChargeUnit = atoi(buf);
	   break;
	   case 0x0b:
		 shmp->comm_tariff[tariff_id].favChargeAmount = atof(buf);
	   break;
	   case 0x0c:
		 shmp->comm_tariff[tariff_id].cf_cugChargeUnit = atoi(buf);
	   break;
	   case 0x0d:
		 shmp->comm_tariff[tariff_id].cf_cugChargeAmount = atof(buf);
	   break;
	   case 0x0e:
		 shmp->comm_tariff[tariff_id].cf_favChargeUnit = atoi(buf);
	   break;
	   case 0x0f:
		 shmp->comm_tariff[tariff_id].cf_favChargeAmount = atof(buf);
	   break;
	   case 0x10:
		 shmp->comm_tariff[tariff_id].cug_favChargeUnit = atoi(buf);
	   break;
	   case 0x11:
		 shmp->comm_tariff[tariff_id].cug_favChargeAmount = atof(buf);
	   break;
	   case 0x12:
		 shmp->comm_tariff[tariff_id].cf_cug_favChargeUnit = atoi(buf);
	   break;
	   case 0x13:
		 shmp->comm_tariff[tariff_id].cf_cug_favChargeAmount = atof(buf);
	   break;
	   case 0x14:
	   	 shmp->comm_tariff[tariff_id].childChangeChargeAmount = atof(buf);
		break;
		case 0x15:
			shmp->comm_tariff[tariff_id].poolAccountChargeUnit = atof(buf);
		break;
		case 0x16:
			shmp->comm_tariff[tariff_id].poolAccountChargeAmount = atof(buf);
		break;
		case 0x17:
			shmp->comm_tariff[tariff_id].activeFreeSMSCharegeAmount = atof(buf);
		break;
		case 0x18:
			shmp->comm_tariff[tariff_id].deactiveFreeSMSCharegeAmount = atof(buf);
		break;
	   default:
	   break;
	 }/* switch */
	 index++;
   }
   return 1;
}

int ImportCommTariffData(FILE *fp)
{
   char   record[512];

   while(fgets(record,512,fp) != (char *)0) 
   {
	 //fprintf(stderr,"record:%s\n",record);
     CommTariffSet(record);
   }

   return 1;
}


int BillSMSSet(char *record)
{
   char           buf[BUFSIZE],*off,s[BUFSIZE];
   int		      start=0,index=0,slen=0;
   int 			  id=0,tariffid=0;
   _bill_sms	  bill_sms;

	PutLogFunID("BillSMSSet");

   if(strlen(record)<2) return 0;
   strcpy(s,record);
   while(index <= 0x07)
   {
     if((off=strchr(s,','))==NULL)
	  off = strchr(s,';');
	 slen = off - s;
	 if(slen>=strlen(record)) slen = strlen(record)-start;
	 memcpy(buf,s,slen);
	 buf[slen] = 0;
	 start += slen+1;
	strcpy(s,&record[start]);
	 switch(index)
	 {
	   case 0x00:
			id = atoi(buf);
			if(id>MAX_SMS_PREFIX || id<1) return 0;
			bill_sms.id = id;
	   break;
	   case 0x01:
			strcpy(bill_sms.bill_id,buf);
	   break;
	   case 0x02:
			bill_sms.call_type = atoi(buf);
		break;
	   case 0x03:
			strcpy(bill_sms.area,buf);
	   break;
	   case 0x04:
			tariffid = atoi(buf);
			if(tariffid>=MAX_ZONE_NUM || tariffid<0) return 0;
			bill_sms.tariffID = tariffid;
	   break;
	   case 0x05:
			bill_sms.discountID = atoi(buf)%MAX_DISCOUNT_ID;
	   break;
	   case 0x06:
			bill_sms.base_fee = atof(buf);
	   break;
	   case 0x07:
	  		bill_sms.cap_flag = atoi(buf)%3;
   	   break;		   
	   default:
	   break;
	 }/* switch */
	 index++;
	}
	memcpy(&ramshm_ptr->tariff_data.bill_sms[tariffid][id-1],
			&bill_sms,sizeof(_bill_sms));
	return 1;
}

int ImportBillSMS(FILE *fp)
{
   char   record[512];

   while(fgets(record,512,fp) != (char *)0) 
   {
//	 fprintf(stderr,"record:%s",record);
     BillSMSSet(record);
   }

   return 1;
}

int vasMMSSendTariffSet(char *record) 
{
   char          		 buf[BUFSIZE],*off,s[BUFSIZE];
   int		     		 start=0,index=0,slen=0;
   int 			  		id=0,tariffid=0;
   VAS_MMS_TARIFF	 	 mmsTariff;

	PutLogFunID("vasMMSSenderTariffSet");

   if(strlen(record)<2) return 0;
   strcpy(s,record);
   while(index <= 0x08)
   {
     if((off=strchr(s,','))==NULL)
	  off = strchr(s,';');
	 slen = off - s;
	 if(slen>=strlen(record)) slen = strlen(record)-start;
	 memcpy(buf,s,slen);
	 buf[slen] = 0;
	 start += slen+1;
	 strcpy(s,&record[start]);
	 switch(index)
	 {		
	   	case 0x00:
			tariffid = atoi(buf);
			if(tariffid>=MAX_ZONE_NUM || tariffid<0) 
				return 0;
			mmsTariff.tariffID = tariffid;
	   	break;
	   	case 0x01:
			id = atoi(buf);
			if(id>MAX_VAS_MMS_SEND_TARIFF || id<1) 
				return 0;
			mmsTariff.mmsSendID= id;
	   	break;			
	   	case 0x02:
	   		mmsTariff.mmsType = atoi(buf);
	   	break;	  
	   	case 0x03: 
			strcpy((char *)mmsTariff.prefix, buf);
	   	break;
	 	case 0x04: 
			mmsTariff.callType= atoi(buf);
	   	break;
	   	case 0x05:
			strcpy((char *)mmsTariff.destName,buf);
	   	break;	  
		case 0x06:
			mmsTariff.discountID = atoi(buf)%MAX_DISCOUNT_ID;
	   	break;	
		case 0x07:
			mmsTariff.chargeFee = atof(buf);
	   	break;
	   	case 0x08:
	  		mmsTariff.capFlag= atoi(buf);
   	   	break;		   	   	   	   
	   	default:
	   	break;
	 }
	 index++;
	}
	memcpy(&ramshm_ptr->tariff_data.vas_tariff[tariffid].vasMmsSendTariff[id-1],&mmsTariff,sizeof(VAS_MMS_TARIFF));
	return 1;
}

int ImportVasMmsSendTariff(FILE *fp)
{
   char   record[512];

   memset(zonenum,0,sizeof(zonenum));   
   while(fgets(record,512,fp) != (char *)0) 
   {
     vasMMSSendTariffSet(record);
   }

   return 1;
}

int vasMMSTariffRetrieveSet(char *record) 
{
   char          		 		buf[BUFSIZE],*off,s[BUFSIZE];
   int		     		 		start=0,index=0,slen=0;
   int 			  				id=0,tariffid=0;
   VAS_MMS_RETRIEVE_TARIFF	 	 mmsTariff;

	PutLogFunID("vasMMSTariffRetrieveSet");

   if(strlen(record)<2) return 0;
   strcpy(s,record);
   while(index <= 0x07)
   {
     if((off=strchr(s,','))==NULL)
	  off = strchr(s,';');
	 slen = off - s;
	 if(slen>=strlen(record)) slen = strlen(record)-start;
	 memcpy(buf,s,slen);
	 buf[slen] = 0;
	 start += slen+1;
	 strcpy(s,&record[start]);
	 switch(index)
	 {		
	   	case 0x00:
			tariffid = atoi(buf);
			if(tariffid>=MAX_ZONE_NUM || tariffid<0) 
				return 0;
			mmsTariff.tariffID = tariffid;
	   	break;
	   	case 0x01:
			id = atoi(buf);
			if(id>MAX_VAS_MMS_RETRIEVE_TARIFF || id<1) 
				return 0;
			mmsTariff.mmsRetrieveID= id;
	   	break;				   
	   	case 0x02: 
			strcpy((char *)mmsTariff.prefix,buf);
	   	break;
	 	case 0x03: 
			mmsTariff.callType= atoi(buf);
	   	break;
	   	case 0x04:
			strcpy((char *)mmsTariff.destName,buf);
	   	break;	  
		case 0x05:
			mmsTariff.discountID = atoi(buf)%MAX_DISCOUNT_ID;
	   	break;	
		case 0x06:
			mmsTariff.chargeFee = atof(buf);
	   	break;
	   	case 0x07:
	  		mmsTariff.capFlag = atoi(buf);
   	   	break;		   	   	   	   
	   	default:
	   	break;
	 }
	 index++;
	}
	memcpy(&ramshm_ptr->tariff_data.vas_tariff[tariffid].vasMmsRetrieveTariff[id-1],&mmsTariff,sizeof(VAS_MMS_RETRIEVE_TARIFF));
	return 1;
}



int ImportVasMmsRetrieveTariff(FILE *fp)
{
   char   record[512];

   memset(zonenum,0,sizeof(zonenum));   
   while(fgets(record,512,fp) != (char *)0) 
   {
     vasMMSTariffRetrieveSet(record);
   }

   return 1;
}


int vasMMSTariffEmailSet(char *record) 
{
   char          		 			buf[BUFSIZE],*off,s[BUFSIZE];
   int		     		 			start=0,index=0,slen=0;
   int 			  					id=0,tariffid=0;
   VAS_MMSEMAIL_DELIVER_TARIFF	 	mmsTariff;

	PutLogFunID("vasMMSTariffEmailSet");

   if(strlen(record)<2) return 0;
   strcpy(s,record);
   while(index <= 0x07)
   {
     if((off=strchr(s,','))==NULL)
	  off = strchr(s,';');
	 slen = off - s;
	 if(slen>=strlen(record)) slen = strlen(record)-start;
	 memcpy(buf,s,slen);
	 buf[slen] = 0;
	 start += slen+1;
	 strcpy(s,&record[start]);
	 switch(index)
	 {		
	   	case 0x00:
			tariffid = atoi(buf);
			if(tariffid>=MAX_ZONE_NUM || tariffid<0) 
				return 0;
			mmsTariff.tariffID = tariffid;
	   	break;
	   	case 0x01:
			id = atoi(buf);
			if(id>MAX_VAS_MMSEMAIL_DELIVER_TARIFF || id<1) 
				return 0;
			mmsTariff.emailID= id;
	   	break;				   
	   	case 0x02: 
			strcpy((char *)mmsTariff.prefix,buf);
	   	break;
	 	case 0x03: 
			mmsTariff.callType= atoi(buf);
	   	break;
	   	case 0x04:
			strcpy((char *)mmsTariff.destName,buf);
	   	break;	  
		case 0x05:
			mmsTariff.discountID = atoi(buf)%MAX_DISCOUNT_ID;
	   	break;	
		case 0x06:
			mmsTariff.chargeFee = atof(buf);
	   	break;
	   	case 0x07:
	  		mmsTariff.capFlag = atoi(buf);
   	   	break;		   	   	   	   
	   	default:
	   	break;
	 }
	 index++;
	}
	memcpy(&ramshm_ptr->tariff_data.vas_tariff[tariffid].vasMmsEmailDeliverTariff[id-1],&mmsTariff,sizeof(VAS_MMSEMAIL_DELIVER_TARIFF));
	return 1;
}

int ImportVasEmailTariff(FILE *fp)
{
   char   record[512];

   memset(zonenum,0,sizeof(zonenum));   
   while(fgets(record,512,fp) != (char *)0) 
   {
     vasMMSTariffEmailSet(record);
   }

   return 1;
}

/* ----------------+ Upload data procedure end +---------------------*/

/* ----------------+ Sync configuration data procedures (recall) +---*/


/*message received from peer server,edit,add*/
int SetUserDataBySync(BYTE oidlen, DWORD *oid, BYTE *pdata, WORD datalen,int reflag)
{
	int			instance,ocode,len=0;
	//long	lmsisdn;
	int			head,tail,store_index;
	char		msisdn[64]={""};
  
	_prepaid_info *shmp=NULL,tmpSubsData;

#if DEBUG1
	int i;
	char buf[1024]={""},workbuf[32]={""};
  
	DebugMsg(debugmib.display.asciiOut,"received user data sync message");
 
	for(i=0;i<oidlen;i++)
	{
		sprintf(workbuf,"%ld.",oid[i]);
		strcat(buf,workbuf);
		memset(workbuf,0,6);
	}
	DebugMsg(debugmib.display.asciiOut,"oid: %s",buf);

	for(i=0;i<datalen;i++)
		sprintf(&buf[i*3],"%02x ",pdata[i]);
	DebugMsg(debugmib.display.asciiOut,"data: %s",buf);
#endif

	PutLogFunID("SetUserDataBySync");
	
	if(oidlen<17) 
	{
		debugmib.watch_dog[58]++;
		return 0;
	}
	if(datalen < sizeof(_prepaid_info))
	{
		debugmib.watch_dog[57]++;
		return 0;
	}	
	instance = oid[15];
	ocode = oid[16];
	if(ocode>4 || ocode<1) return 0;
 	debugmib.msgstatistics.recv_sync[ocode-1] ++;

	len = pdata[MAX_MSISDN_BLEN-1];  //len of msisdn
	if(len>MAX_MSISDN_ALEN || len<MIN_MSISDN_ALEN) 
	{
		debugmib.watch_dog[56]++;
		return 0;
	}
	if(debugmib.display.sysState == _SYS_STATE_INIT)
	{
		debugmib.watch_dog[55]++;
		head = instance / UNIT_STORE_ACCOUNT;;
		tail = instance % UNIT_STORE_ACCOUNT;;
		if(head>=MSISDN_HEAD_NUM)
			return 0;
		shmp = &ramshm_ptr->prepaid_data.prepaid_info[head][tail];
		if(ocode == 4)
			memset(shmp,0,sizeof(_prepaid_info));
		else
			memcpy(shmp,pdata,sizeof(_prepaid_info));
		return datalen;
   		
	} 
	
	BcdToAscii(msisdn,pdata,9*2);
	msisdn[len] = 0;
	//lmsisdn = atoll(msisdn);
	shmp = &tmpSubsData;

	if(ocode == 3)
	{
		store_index = GetPPSSubsRecord(msisdn,shmp);
		if(store_index<0) 
		{
			if(debugmib.measurement.usernumber>=ppsParam.serviceControl.licenseduser)
			{
				DebugMsg(debugmib.display.asciiOut,		
						"Error: received sync. messgae to add new subs. %s ",msisdn);
				DebugMsg(debugmib.display.asciiOut,		
					 "    But exist users %d >= licensed user(%ld)!" ,
					debugmib.measurement.usernumber,
					ppsParam.serviceControl.licenseduser);
				return 0;
			}	
			memcpy(shmp,pdata,sizeof(_prepaid_info));
			AddPPSSubsData(shmp);
		}
		else
		{
			if(replaceableFlag == 0)
			{
				DebugMsg(debugmib.display.asciiOut,	"Account %s is in use, cann't be replaced!",msisdn);
				return 0;
			}
			DebugMsg(debugmib.display.asciiOut,	"Account %s is in use, replaced it!",msisdn);
			memcpy(shmp,pdata,sizeof(_prepaid_info));
			ModifyPPSSubsData(shmp);
		}
	}
	else 
	{
		store_index = GetPPSSubsRecord(msisdn,shmp);

		if(store_index<0) 
		{
			/*
			if(ocode == 2)
			{
				memcpy(shmp,pdata,sizeof(_prepaid_info));
				AddPPSSubsData(shmp);
				return datalen;
			}
			*/
			/* create the subs if no subs. for update operation,2006-05-09*/
			debugmib.watch_dog[53]++;
			DebugMsg(debugmib.display.asciiOut,"Error sync message received: with instance: %d VS %d, not existed",
				store_index,instance);
			return 0;
		}
		if(store_index != instance)
		{
			debugmib.watch_dog[54]++;
			DebugMsg(debugmib.display.asciiOut,"Error sync message received: with instance: %d VS %d",
				store_index,instance);
			//return 0;
		}
		head = store_index / UNIT_STORE_ACCOUNT;
		tail = store_index % UNIT_STORE_ACCOUNT;
	
		if(ocode == 4)
		{
			debugmib.watch_dog[52]++;
			if(masterFlag)
				SendOperatorRechargeCDR(head,tail,-shmp->balance);
			
			DeletePPSSubsData(shmp);
		}
		else if(ocode == 2)
		{
			double balance,promotion, oldbalance,oldpromotion;

			shmp = &ramshm_ptr->prepaid_data.prepaid_info[head][tail];			
			oldbalance = shmp->balance;	
			oldpromotion = shmp->promo_balance;	
			
			if(datalen >= sizeof(_prepaid_info) + sizeof(double))
				memcpy(&balance,&pdata[sizeof(_prepaid_info)],sizeof(double));						
							
			if(datalen >= sizeof(_prepaid_info) + 2*sizeof(double))
				memcpy(&promotion,&pdata[sizeof(_prepaid_info)+sizeof(double)],sizeof(double));						
									
			memcpy(shmp,pdata,sizeof(_prepaid_info));
			if(balance != 0)
			{
				shmp->balance = oldbalance + balance;
				if (shmp->balance < 0.0001)
				{
					shmp->balance = 0;
				}
			}
			if(promotion != 0)
			{
				shmp->promo_balance = oldpromotion + promotion;
			}
			pps_real_update_user_file(head, tail);
			
			//ModifyPPSSubsData(shmp);
		}
	}

  //if(reflag)
//	SendAccountRealSync(oid[15],oid[16]);
	
	return datalen;
}
 

int SetUserDataByTcpSync(BYTE *pdata, WORD datalen, DWORD instance, DWORD ocode)
{
	int			len = 0;
	//long	lmsisdn;
	int			head,tail, store_index;
	char		msisdn[64] = {""};
  
	_prepaid_info *shmp = NULL, tmpSubsData;

	PutLogFunID("SetUserDataByTcpSync");

	// Check msg length
	if(datalen < sizeof(_prepaid_info))
	{
		debugmib.watch_dog[57]++;
		return 0;
	}

	// Check operation code
	if(ocode>=MAX_OMC_OCODE || ocode<=OMC_OCODE_NULL)
	{
		return 0;
	}
 	debugmib.msgstatistics.recv_sync[ocode-1] ++;

	// Check msisdn length
	len = pdata[MAX_MSISDN_BLEN-1];  //len of msisdn
	if(len>MAX_MSISDN_ALEN || len<MIN_MSISDN_ALEN) 
	{
		debugmib.watch_dog[56]++;
		return 0;
	}

	// start up as slaver
	if(debugmib.display.sysState == _SYS_STATE_INIT)
	{
		debugmib.watch_dog[55]++;
		head = instance / UNIT_STORE_ACCOUNT;
		tail = instance % UNIT_STORE_ACCOUNT;
		if(head>=MSISDN_HEAD_NUM)
		{
			return 0;
		}
		
		shmp = &ramshm_ptr->prepaid_data.prepaid_info[head][tail];
		if(ocode == OMC_OCODE_DEL)
		{
			memset(shmp, 0, sizeof(_prepaid_info));
		}
		else// modify or add
		{
			memcpy(shmp, pdata, sizeof(_prepaid_info));
		}
		return datalen;
	} 
	
	BcdToAscii(msisdn, pdata, 9*2);
	msisdn[len] = 0;
	//lmsisdn = atoll(msisdn);
	shmp = &tmpSubsData;

	if(ocode == OMC_OCODE_ADD)
	{
		store_index = GetPPSSubsRecord(msisdn, shmp);
		if(store_index < 0)
		{
			if(debugmib.measurement.usernumber >= ppsParam.serviceControl.licenseduser)
			{
				DebugMsg(debugmib.display.asciiOut,		
						"Error: received tcp sync. messgae to add new subs. %s ", msisdn);
				DebugMsg(debugmib.display.asciiOut,		
					 "    But exist users %d >= licensed user(%ld)!" ,
					debugmib.measurement.usernumber,
					ppsParam.serviceControl.licenseduser);
				return 0;
			}
			memcpy(shmp, pdata, sizeof(_prepaid_info));
			AddPPSSubsData(shmp);
		}
		else
		{
			if(replaceableFlag == 0)
			{
				DebugMsg(debugmib.display.asciiOut, "Account %s is in use, cann't be replaced!", msisdn);
				return 0;
			}
			DebugMsg(debugmib.display.asciiOut, "Account %s is in use, replaced it!", msisdn);
			memcpy(shmp, pdata, sizeof(_prepaid_info));
			ModifyPPSSubsData(shmp);
		}
	}
	else 
	{
		store_index = GetPPSSubsRecord(msisdn,shmp);

		if(store_index<0) 
		{
			/*
			if(ocode == OMC_OCODE_EDIT)
			{
				memcpy(shmp,pdata,sizeof(_prepaid_info));
				AddPPSSubsData(shmp);
				return datalen;
			}
			*/
			
			/* create the subs if no subs. for update operation, 2006-05-09*/
			debugmib.watch_dog[53]++;
			DebugMsg(debugmib.display.asciiOut, "Error sync message received: with instance: %d VS %d, not existed",
						store_index,instance);
			return 0;
		}
		
		if(store_index != instance)
		{
			debugmib.watch_dog[54]++;
			DebugMsg(debugmib.display.asciiOut, "Error sync message received: with instance: %d VS %d",
						store_index, instance);
			//return 0;
		}
		head = store_index / UNIT_STORE_ACCOUNT;
		tail = store_index % UNIT_STORE_ACCOUNT;
	
		if(ocode == OMC_OCODE_DEL)
		{
			debugmib.watch_dog[52]++;
			if(masterFlag)
				SendOperatorRechargeCDR(head, tail, -shmp->balance);
			
			DeletePPSSubsData(shmp);
		}
		else if(ocode == OMC_OCODE_EDIT)
		{
			double balance, promotion, oldbalance, oldpromotion;

			shmp = &ramshm_ptr->prepaid_data.prepaid_info[head][tail];			
			oldbalance = shmp->balance;	
			oldpromotion = shmp->promo_balance;	
			
			if(datalen >= sizeof(_prepaid_info) + sizeof(double))
				memcpy(&balance, &pdata[sizeof(_prepaid_info)], sizeof(double));						
							
			if(datalen >= sizeof(_prepaid_info) + 2*sizeof(double))
				memcpy(&promotion, &pdata[sizeof(_prepaid_info)+sizeof(double)], sizeof(double));						
									
			memcpy(shmp, pdata, sizeof(_prepaid_info));
			if(balance != 0)
			{
				shmp->balance = oldbalance + balance;
				if (shmp->balance < 0.0001)
				{
					shmp->balance = 0;
				}
			}
			if(promotion != 0)
			{
				shmp->promo_balance = oldpromotion + promotion;
			}
			pps_real_update_user_file(head, tail);
			
			//ModifyPPSSubsData(shmp);
		}
	}

  //if(reflag)
//	SendAccountRealSync(oid[15],oid[16]);
	
	return datalen;
}
 

extern void PrepaidCardTcpRealSyncProc(BYTE *pdata, WORD dataLen, DWORD instance, DWORD ocode);
int tcpRealSyncProc(WORD usr_port, BYTE *pdata, WORD datalen, DWORD db_type, DWORD instance, DWORD ocode)
{
	PutLogFunID("tcpRealSyncProc");

	switch(db_type){
	case PPS_USER_DATA_DB_OID:/* user data */
		SetUserDataByTcpSync(pdata, datalen, instance, ocode);
		break;
	case PPS_PREPAID_CARD_DB_OID:/* prepaid card */
		PrepaidCardTcpRealSyncProc(pdata, datalen, instance, ocode);
		break;
	default:
		break;
	}

	return 1;
}

int dataSyncCallPro(BYTE oidlen, DWORD *oid, BYTE *pdata, WORD len,int reflag)
{
   int /*oid0,*/oid1,oid2;

	PutLogFunID("dataSyncCallPro");

   if(oidlen<14) return 0;
   //oid0 = oid[11];

   oid1 = oid[12];
   oid2 = oid[13];

   switch(oid1)
   {
      case 0x01:
		  return 0;
	  break;
	  case 0x02:
	    switch(oid2){
		  case 2:/* user data */
		    SetUserDataBySync(oidlen,oid,pdata,len,reflag);
		  break;
		  case 3:/* prepaid */
		    //SetPrepaidDataBySync(oidlen,oid,pdata,len);
		  break;
		  case 4:/* tariff */
		  break;
		}
	  break;
	  default:
	  break;
   }
   return 1;
}

/*
int  RealSyncGlobalParam(PPSParamStruct globalparam)
{
    snmp_pdu         snmpbuf;
	snmp_addr		 snmpaddr;
    var_list		 *varlist;

	PutLogFunID("RealSyncGlobalParam");

	snmpbuf.pdu_type = 0x07; //trap 
	snmpbuf.var_num	 = 0x01;

	snmpaddr.remote_ip = peer_server_ip;
    snmpaddr.remote_port = PPS_REALSYNC_PORT;
	snmpaddr.local_port  = PPS_REALSYNC_PORT;
	snmpaddr.broadcast   = 0x00;

    varlist = snmpbuf.var;
    varlist->oidlen   = 14;
	varlist->vartype  = 0x04;

	memcpy(&varlist->oid,PPS_OID_PREFIX,11*4);
	varlist->oid[11] = 0x02; //configuration
	varlist->oid[12] = 0x01; //system parameter
    varlist->oid[13] = 0x00; //instance;

	varlist->msglen  = sizeof(ppsParam);
    memcpy(varlist->msg, 
                        (u_char *) &globalparam, 
                        sizeof(ppsParam));
    snmp_send(&snmpbuf,&snmpaddr);

#if DEBUG1
	DebugMsg(debugmib.display.asciiOut,"SendGlobalParam Sync message\n");
#endif
   return 1;
}
*/
int SyncTariffData()
{
   static  int sendindex = 0;
   if(sendindex ++>1) return 1;
   return 0;
}

/*message received from OMC, delete*/
int  SetUserDataByOMC(BYTE oidlen, DWORD *oid, BYTE *pdata, WORD datalen)
{
	int					ocode,send_f_f_sms_flag=0;
	int					head,tail,store_index;
	//long			lmsisdn;
	int					len;//,mslen;
	int					i,vallen,chargeFlag=0,/*addFlag=0,*/charge=0;
	char				buf[2048]="";
	char				valbuf[32]="";
	//struct tm 			tmnow;
	CosServiceControl	*serviceControlItem;
	CosFriendFamilyControl *friendFamilyControl;
	int					totalLen=0;
	unsigned int		tmpval;
	int					loop=0;
	double				balance,difbalance=0,oldbalance,dif_promo_balance=0,old_promo_balance;
	u_short				portid;
	BYTE				pdata1[1600],pdata2[1600],pflag;
	u_short				ptagcode;
	char				tagbuf[32],tmpBuf[32];
	_prepaid_info		*shmp=NULL,tmpSubsData;
	PoolParent			*pParent;
	PoolChild			*pChild;
	_state_data			*stat_shmp;


	ASN_BUF	*asnbuf, asnbuf1;
	ASN_BUF	*asnbuf2, asnbuf3;

	PutLogFunID("SetUserDataByOMC");

	if(debugmib.display.sysState != _SYS_STATE_NORMAL)
	{
		sprintf((char *)pdata,"operation not support for system in error state");
		return -1;
	}
	memset(pdata1,0,datalen+1);

	memcpy(pdata1,pdata,datalen);
	pdata1[datalen]=0;

	asnbuf = &asnbuf1;

	AsnEncode(pdata,1600,asnbuf);
	asnbuf2 = &asnbuf3;

	AsnDecode(pdata1, datalen, UNIT_STORE_ACCOUNT, NULL, asnbuf2);
	//BcdToAscii(buf,pdata1,datalen*2);
	//printf("dataLen:%d,data:%s\n",datalen,buf);

#if DEBUG1
	{	
   		if((logMsgFlag == 0x40))
		DebugMsg(debugmib.display.asciiOut,"received user data omc message");
 		if(logMsgFlag == 0x40)
		{
  			for(i=0;i<oidlen;i++)
  			{
    			sprintf(tmpBuf,"%ld.",oid[i]);
    			strcat(buf,tmpBuf);
				memset(tmpBuf,0,6);
  			}

			DebugMsg(debugmib.display.asciiOut,"oid: %s",buf);

			for(i=0;i<datalen;i++)
	 		sprintf(&buf[i*3],"%02x ",pdata1[i]);

			DebugMsg(debugmib.display.asciiOut,"data: %s",buf);
		}
	}
#endif

	if(oidlen<17)
	{
		return -1;
	}
	ocode = oid[16];
	if(ocode>0x04)
	{
		return -1;
	}
	if(oid[13]>0)	
		sprintf(buf,"%u%08u",oid[13],oid[14]);
	else
		sprintf(buf,"%u",oid[14]);	
	len = strlen(buf);

	if(len>MAX_MSISDN_ALEN || len<MIN_MSISDN_ALEN)
	{
		sprintf((char *)pdata,"msisdn length(%d) error, should be(%d-%d)",
			len,MIN_MSISDN_ALEN,MAX_MSISDN_ALEN);
		return -2;
	}
	
	//mslen = len;
	//lmsisdn = atoll(buf);
	shmp = &tmpSubsData;

	store_index = GetPPSSubsRecord(buf,shmp);
	if(logMsgFlag == 0x40)
		DebugMsg(debugmib.display.asciiOut,
			"omc operation: msisdn=%s, ocode: %d, store @ %d",
			buf,ocode,store_index);

	if(store_index<0) /* doesn't exists */
	{
		if(ocode != OMC_OCODE_ADD)
		{
			if(logMsgFlag == 0x40)
				DebugMsg(debugmib.display.asciiOut,
					"ocode: %d,record not found with msisdn=%s!",
						ocode, buf);
  			return -1;
		}
		if(debugmib.measurement.usernumber>=ppsParam.serviceControl.licenseduser)
		{
			sprintf((char *)pdata,"Too many msisdn %ld, license is %u",
				debugmib.measurement.usernumber,ppsParam.serviceControl.licenseduser);
			DebugMsg(debugmib.display.asciiOut,"Too many msisdn %ld, license is %ld",
				debugmib.measurement.usernumber,ppsParam.serviceControl.licenseduser);
			return -2;
		}
		debugmib.watch_dog[8]++;
		memset(shmp,0,sizeof(_prepaid_info));
		sprintf(shmp->pin,"123456");
		shmp->first_used_date = tvnow.tv_sec;
		shmp->mo_expiration_date = tvnow.tv_sec;
		shmp->mt_expiration_date = tvnow.tv_sec;
		shmp->promo_date = tvnow.tv_sec;		
	}
	else
	{
		if(ocode == OMC_OCODE_DEL){ //delete
			SendAccountRealSync(store_index,0.0,0.0,ocode); //delete
			return datalen;
		}
		if(ocode == OMC_OCODE_ADD)
		{
			debugmib.watch_dog[9]++;
			if(replaceableFlag == 0)
			{
				sprintf((char *)pdata,"account %s is in use, can not be replaced!",buf);
				if(logMsgFlag == 0x40)
				DebugMsg(debugmib.display.asciiOut,"account %s is in use, can not be replaced!",buf);
				return -2;
			}
			else
			{
				if(logMsgFlag == 0x40)
				DebugMsg(debugmib.display.asciiOut,"account %s is in use, replace it!",buf);
			}
		}
		else
		{
			head = store_index / UNIT_STORE_ACCOUNT;
			tail = store_index % UNIT_STORE_ACCOUNT;
		}
	}
   
	for(loop=1;loop<=MAX_PPS_USER_FIELD_NUMS;loop++)
	{     
		sprintf(tagbuf,"-%d",loop);
		if((vallen=GetAnyTLV(tagbuf,1024,pdata2,&pflag,&ptagcode,asnbuf2))<0){
			break;
		}
		sprintf(tagbuf,"%d",ptagcode);
		pdata2[vallen]=0;

		switch(ptagcode)
		{
			case 0x01: //msisdn
			if(ocode==OMC_OCODE_READ)
			{
				BcdToAscii(valbuf,shmp->msisdn,MAX_MSISDN_ALEN);
				len = shmp->msisdn[MAX_MSISDN_BLEN-1]; 
				if(len==0)
				{
					totalLen = add_null(tagbuf,0x80,asnbuf);	
					return -1;
				}
				else
				{
					totalLen = add_tlv(tagbuf,len, (u_char *)valbuf,0x80,asnbuf);
				}
			}
			else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
			{
				if(vallen == 0) continue;
				if(vallen > MAX_MSISDN_ALEN || vallen<MIN_MSISDN_ALEN)
				{
						sprintf((char *)pdata,"msisdn length(%d) error, should be(%d-%d)",
							vallen,MIN_MSISDN_ALEN,MAX_MSISDN_ALEN);
						if(logMsgFlag == 0x40)
						DebugMsg(debugmib.display.asciiOut,
							"msisdn length(%d) error, should be(%d-%d)",
							vallen,MIN_MSISDN_ALEN,MAX_MSISDN_ALEN);
						return -2;
				 }
				 for(i=0;i<vallen;i++)
				 {
					if(pdata2[i]>'9' || pdata2[i]<'0' || pdata2[0] =='0') 
					{
						sprintf((char *)pdata,"Error msisdn");
						DebugMsg(debugmib.display.asciiOut,"Error msisdn");
						return -2;
					}
				}			    
				if(shmp->msisdn[MAX_MSISDN_BLEN-1]==0) {/*addFlag = 1*/;}
					AsciiToBcd(shmp->msisdn, (char *)pdata2,(vallen+1)/2*2);
				if(vallen%2==0)
					shmp->msisdn[vallen/2] = 0xFF;
				else
					shmp->msisdn[vallen/2] |= 0x0F;
				shmp->msisdn[MAX_MSISDN_BLEN-1] = vallen;
				vallen = shmp->msisdn[MAX_MSISDN_BLEN-1];
				BcdToAscii(valbuf,shmp->msisdn,(vallen+1)/2*2);
				totalLen = add_tlv(tagbuf,vallen, (u_char *)valbuf,0x80,asnbuf);				
			}
			break;
			case 0x02:
			if(ocode == OMC_OCODE_READ)
			{
				totalLen = add_tlv(tagbuf,MAX_PIN_LEN, (u_char *)shmp->pin,0x80,asnbuf);
			}
			else
			{
				if(vallen != MAX_PIN_LEN)
				{
					sprintf((char *)pdata,"Error PIN length , should be 6 digits!");
					DebugMsg(debugmib.display.asciiOut,
							"Error PIN length , should be 6 digits!");
					return -2;
				}
				for(i=0;i<vallen;i++)
				{
					if(pdata2[i]>'9' || pdata2[i]<'0') 
					{
						sprintf((char *)pdata,"Error PIN format, only support digits of 0-9!");
						DebugMsg(debugmib.display.asciiOut,
							"Error PIN format, only support digits of 0-9!");
						return -2;
					}
				}
				memcpy(shmp->pin,pdata2,MAX_PIN_LEN);
				totalLen = add_tlv(tagbuf,MAX_PIN_LEN, (u_char *)shmp->pin,0x80,asnbuf);
			}
			break;
			case 0x03: //status
			if(ocode==OMC_OCODE_READ){
				len = 1;
				valbuf[0] = shmp->status;
				totalLen = add_tlv(tagbuf,1,&shmp->status,0x80,asnbuf);				
			}
			else if(ocode==OMC_OCODE_EDIT || ocode==OMC_OCODE_ADD)
			{
				if(GetInteger(tagbuf, (int *)&tmpval,pflag,asnbuf2)<=0) continue;
				if(tmpval>T_RELEASED)
				{
					sprintf((char *)pdata,"Error status!");
					return -2;
				}
				shmp->status = tmpval;
				totalLen = add_tlv(tagbuf,1,&shmp->status,0x80,asnbuf);							
			}
			break;
			case 0x04:  /* balance */
			if(ocode==OMC_OCODE_READ)
			{
				balance = shmp->balance;
				totalLen = AddInteger(tagbuf,(int)balance,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode==OMC_OCODE_ADD)
			{				
				int int_val;
				
				if(GetInteger(tagbuf,&int_val ,pflag,asnbuf2)<=0) continue;
				if(int_val>ppsParam.serviceControl.maxBalance){
					sprintf((char *)pdata,"Too big account balance(%d),should be(0-%d)",
								int_val,ppsParam.serviceControl.maxBalance);
					DebugMsg(debugmib.display.asciiOut,"Too big account balance(%d)",int_val);
					return -2;
				}
				
				balance = (double)int_val;
				oldbalance=(int)shmp->balance;
				shmp->balance = balance;
				if(oldbalance != balance)
				{
					chargeFlag |= 0x01;
					difbalance = balance-oldbalance;
				}
				totalLen = AddInteger(tagbuf,int_val,0x80,asnbuf);
			}
			break;
			case 0x05:  /* promotion balance */
			if(ocode==OMC_OCODE_READ)
			{
				balance = shmp->promo_balance;
				totalLen = AddInteger(tagbuf,(int)balance,0x80,asnbuf);
			}else if(ocode==OMC_OCODE_EDIT || ocode==OMC_OCODE_ADD)
			{			
				int	int_val1;
				
				if(GetInteger(tagbuf,&int_val1 ,pflag,asnbuf2)<=0) continue;
				if(int_val1>ppsParam.serviceControl.maxBalance){
					sprintf((char *)pdata,"Too big account balance(%d),should be(0-%d)",
							int_val1,ppsParam.serviceControl.maxBalance);
					DebugMsg(debugmib.display.asciiOut,"Too big account balance(%d)",int_val1);
					return -2;
				}
				
				balance = int_val1;
				old_promo_balance = shmp->promo_balance;
				shmp->promo_balance = (double) int_val1;
				if(old_promo_balance != balance){
					chargeFlag |= 0x02;
					dif_promo_balance = balance-old_promo_balance;
				}
				totalLen = AddInteger(tagbuf,int_val1,0x80,asnbuf);
			}
			break;
			case 0x06: //overdraft
			if(ocode==OMC_OCODE_READ)
			{
				len = 4;
				totalLen = AddInteger(tagbuf,shmp->overdraft,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode==OMC_OCODE_ADD)
			{			    
				if(GetInteger(tagbuf, (int *)&tmpval,pflag,asnbuf2)<=0) continue;		
				shmp->overdraft = tmpval;
				totalLen = AddInteger(tagbuf,shmp->overdraft,0x80,asnbuf);		
			}
			break;
			case 0x07: //first used date
			if(ocode==OMC_OCODE_READ)
			{
				AsciiTime(buf,shmp->first_used_date);
				AsciiToBcd((BYTE *)valbuf,buf,14);
				len = 7;
				totalLen = add_tlv(tagbuf,len, (u_char *)valbuf,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode==OMC_OCODE_ADD)
			{
				BcdToAscii(buf,pdata2,vallen*2);
				shmp->first_used_date = MakeTime(NULL,buf);
				if(ocode==OMC_OCODE_ADD)
				{
					shmp->first_used_date = tvnow.tv_sec;
				}
				AsciiTime(buf,shmp->first_used_date);
				AsciiToBcd((BYTE *)valbuf,buf,14);
				len = 7;
				totalLen = add_tlv(tagbuf,len, (u_char *)valbuf,0x80,asnbuf);
			}
			break;
			case 0x08: //mo expiration date
			if(ocode==OMC_OCODE_READ){
				AsciiTime(buf,shmp->mo_expiration_date);
				AsciiToBcd((BYTE *)valbuf,buf,14);
				len = 7;
				totalLen = add_tlv(tagbuf,len, (u_char *)valbuf,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode==OMC_OCODE_ADD)
			{
				BcdToAscii(buf,pdata2,vallen*2);		   
				shmp->mo_expiration_date = MakeTime(NULL,buf);

				AsciiTime(buf,shmp->mo_expiration_date);
				AsciiToBcd((BYTE *)valbuf,buf,14);
				len = 7;
				totalLen = add_tlv(tagbuf,len, (u_char *)valbuf,0x80,asnbuf);
			}
			break;
			case 0x09: //mt expiration date
			if(ocode==OMC_OCODE_READ)
			{
				AsciiTime(buf,shmp->mt_expiration_date);
				AsciiToBcd((BYTE *)valbuf,buf,14);
				len = 7;
				totalLen = add_tlv(tagbuf,len, (u_char *)valbuf,0x80,asnbuf);
			}else if(ocode==OMC_OCODE_EDIT || ocode==OMC_OCODE_ADD)
			{
				BcdToAscii(buf,pdata2,vallen*2);		   
				shmp->mt_expiration_date = MakeTime(NULL,buf);
				AsciiTime(buf,shmp->mt_expiration_date);
				AsciiToBcd((BYTE *)valbuf,buf,14);
				len = 7;
				totalLen = add_tlv(tagbuf,len, (u_char *)valbuf,0x80,asnbuf);
			}
			break;
			case 0x0a: //promotion balance expiration date
			if(ocode==OMC_OCODE_READ)
			{
				AsciiTime(buf,shmp->promo_date);
				AsciiToBcd((BYTE *)valbuf,buf,14);
				len = 7;
				totalLen = add_tlv(tagbuf,len, (u_char *)valbuf,0x80,asnbuf);
			}else if(ocode==OMC_OCODE_EDIT || ocode==OMC_OCODE_ADD)
			{
				BcdToAscii(buf,pdata2,vallen*2);		   
				shmp->promo_date = MakeTime(NULL,buf);

				AsciiTime(buf,shmp->promo_date);
				AsciiToBcd((BYTE *)valbuf,buf,14);
				len = 7;
				totalLen = add_tlv(tagbuf,len, (u_char *)valbuf,0x80,asnbuf);
			}
			break;
			case 0x0b:
			if(ocode == OMC_OCODE_READ)
			{
				totalLen = AddInteger(tagbuf,shmp->cos_id,0x80,asnbuf);
			}
			else
			{
				int	max_cos = MaxCosTableItem;
				if(GetInteger(tagbuf, (int *)&tmpval,pflag,asnbuf2)<=0) continue;
				if(ppsParam.serviceControl.cos_group_flag)
					max_cos = MAX_ZONE_NUM_NEW;
					
				if(tmpval >= max_cos)
				{
					sprintf((char *)pdata,"Error id of COS, should be 0-%d!",max_cos-1);
					DebugMsg(debugmib.display.asciiOut,
							"Error id of COS, should be 0-%d!",max_cos-1);
					return -2;
				}
				shmp->cos_id = tmpval;
				totalLen = AddInteger(tagbuf,shmp->cos_id,0x80,asnbuf);
			}
			break;
			case 0x0c: //rent_type
			if(ocode==OMC_OCODE_READ){
				totalLen = add_tlv(tagbuf,1,&shmp->rent_type,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode==OMC_OCODE_ADD)
			{
				if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
				/*
				if(tmpval>0xFF)
				{
					sprintf(pdata,"Error rent type value %d, should be less than 0xFF",tmpval);
					return -2;
				}
				*/
				shmp->rent_type = tmpval;
			    totalLen = add_tlv(tagbuf,1,&shmp->rent_type,0x80,asnbuf);
			}
			break;
			case 0x0d:
			case 0x0e:
			case 0x0f:
			case 0x10:
				if(ocode == OMC_OCODE_READ)
				{
					totalLen = add_tlv(tagbuf,1,&shmp->home_location[ptagcode-13],0x80,asnbuf);
				}
				else
				{
					if(GetInteger(tagbuf, (int *)&tmpval,pflag,asnbuf2)<=0) continue;
					if(tmpval>=MaxBasicTariffZoneItem)
					{
						sprintf((char *)pdata,"Error location id, should be 0-%d",MaxBasicTariffZoneItem-1);
						return -2;
					}
					shmp->home_location[ptagcode-13] = tmpval;
					totalLen = add_tlv(tagbuf,1,&shmp->home_location[ptagcode-13],0x80,asnbuf);
				}
			break;
			case 0x11: //cug id
			if(ocode ==OMC_OCODE_READ)
			{
				totalLen = AddInteger(tagbuf,shmp->cug_id,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
			{
				if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
				if(tmpval>MAX_CUG_ID)
				{
					sprintf((char *)pdata,"Too big cug id(%d),should be(0-%d)",
						tmpval,MAX_CUG_ID);
					return -2;
				}
				shmp->cug_id = tmpval;
				totalLen = AddInteger(tagbuf,shmp->cug_id,0x80,asnbuf);
			}
			break;
			case 0x12:
			if(ocode ==OMC_OCODE_READ)
			{
				totalLen = add_tlv(tagbuf,1,&shmp->language_type,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
			{
				if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
				if(tmpval>=MAX_LANGUAGE_TYPES)
				{
					sprintf((char *)pdata,"Error language type code, should be 0-%d!",MAX_LANGUAGE_TYPES);
					return -2;
				}
				shmp->language_type = tmpval;
				totalLen = add_tlv(tagbuf,1,&shmp->language_type,0x80,asnbuf);
			}
			break;
			case 0x13: //recharge failure
			if(ocode==OMC_OCODE_READ)
			{
				totalLen = add_tlv(tagbuf,1,&shmp->recharge_fail,0x80,asnbuf);				
			}
			else if(ocode==OMC_OCODE_EDIT || ocode==OMC_OCODE_ADD)
			{			    
				if(GetInteger(tagbuf, (int *)&tmpval,pflag,asnbuf2)<=0) continue;
				shmp->recharge_fail = tmpval;
				totalLen = add_tlv(tagbuf,1,&shmp->recharge_fail,0x80,asnbuf);
			}
			break;
			case 0x14: //free sm counter
			if(ocode ==OMC_OCODE_READ)
			{
				totalLen = add_tlv(tagbuf,1,&shmp->free_sm_counter,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
			{
				if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
				shmp->free_sm_counter = tmpval;
				totalLen = add_tlv(tagbuf,1,&shmp->free_sm_counter,0x80,asnbuf);
			}
			break;
			case 0x15: //latch flag
			totalLen = add_tlv(tagbuf,1,&shmp->latch_flag,0x80,asnbuf);
			break;
			case 0x16: //favorite numbers
			totalLen = AddInteger(tagbuf,shmp->favorite_numbers,0x80,asnbuf);
			break;
			case 0x17: //fav_change counter
			if(ocode ==OMC_OCODE_READ)
			{
				totalLen = add_tlv(tagbuf,1,&shmp->fav_change_counter,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
			{
				if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
				shmp->fav_change_counter = tmpval;
				totalLen = add_tlv(tagbuf,1,&shmp->fav_change_counter,0x80,asnbuf);
			}
			break;
			case 0x18:
			case 0x19:
			case 0x1a:
			case 0x1b:
			case 0x1c:
			if(ocode ==OMC_OCODE_READ)
			{
				len = shmp->favorite_number[ptagcode-24][MAX_MSISDN_BLEN-1]; 
				if(len<3 || len>MAX_MSISDN_ALEN)
				{
				    totalLen = add_null(tagbuf,0x80,asnbuf);	
				}
				else
				{
	  		        BcdToAscii(valbuf,shmp->favorite_number[ptagcode-24],MAX_MSISDN_ALEN);
					valbuf[len] = 0;
				    totalLen = add_tlv(tagbuf,len, (u_char *)valbuf,0x80,asnbuf);
				}
			}
			else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
			{ 								
				if(vallen == 0 && ocode == OMC_OCODE_EDIT)
				{
					if(shmp->favorite_number[ptagcode-24][MAX_MSISDN_BLEN-1]>3) 
					{
						if(shmp->favorite_numbers >0 ) 
						shmp->favorite_numbers --;		
						/*
						serviceControlItem = &ppsParam.ppsTables.cosTable[shmp->cos_id].serviceControl[0];
						if(serviceControlItem->friendFamilyChangeChargeFlag)
						{
							if(IncreaseFavoriteNumberChangeCounterByOmc(shmp)>
								serviceControlItem->friendFamilyFreeChangeTimes)
							{
								portid = AssignProcID();
								if(portid>0 && portid< MAX_FSM_PROCESS)
								{
								stat_shmp = (_state_data *)&smshm_ptr->state_data;
								stat_shmp += portid;
								
								stat_shmp->caller_info.head = head;
								stat_shmp->caller_info.tail = tail;
								
								BillingFavoriteNumberChange(stat_shmp);
								SendPeriodicChargeCDR(head,
													tail,
													stat_shmp->caller_info.charge,
													CT_PERIODIC_CHARGE);
								ReleasePortStatus(portid);
								}
							}
						}
						*/
					}
					memset(shmp->favorite_number[ptagcode-24],0,MAX_MSISDN_BLEN);
					//shmp->favorite_number[ptagcode-24][MAX_MSISDN_BLEN-1] = 0;
				    totalLen = add_null(tagbuf,0x80,asnbuf);	
					continue;
				}

				if((vallen > MAX_MSISDN_ALEN || vallen<3 )&& vallen!=0){
						sprintf((char *)pdata,"Friend and Family number length(%d) error, should be(3-16)",vallen);
						DebugMsg(debugmib.display.asciiOut,
						"Friends and Family number length(%d) error, should be(3-16)",vallen);
						return -2;
				 }
				
				 if(atoi((char *)pdata2)==0)
				 {
						//sprintf(pdata,"Error favorite number,all digits are 0");
						//DebugMsg(debugmib.display.asciiOut,"Error favorite number,all digits are 0");
						//return -2;
						continue;
				 }
				
		         	for(i=0;i<vallen;i++)
				 {					
					if(pdata2[i]>'9' || pdata2[i]<'0') 
					{
						sprintf((char *)pdata,"Error Friends and Family number");
						DebugMsg(debugmib.display.asciiOut,"Error Friends and Family number");
						return -2;
				    }
				}	

				friendFamilyControl = &ppsParam.ppsTables.cosTable[shmp->cos_id].friendFamilyControl[0];			
				if(!(friendFamilyControl->friendFamilyFlag))
				{
					sprintf((char *)pdata,"Friends and Family number service not applied!");
					return -2;
				}

				portid = AssignProcID(0);
				stat_shmp = (_state_data *)&smshm_ptr->state_data;
				stat_shmp += portid;
								
				stat_shmp->caller_info.head = head;
				stat_shmp->caller_info.tail = tail;
				stat_shmp->caller_info.cos_id = shmp->cos_id;				
				stat_shmp->caller_info.location = GetAccountZoneID(stat_shmp->caller_info.head, 
										stat_shmp->caller_info.tail);	
				if((ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD) ) 
				{
				       if(shmp->favorite_number[ptagcode-24][MAX_MSISDN_BLEN-1]<3)
				       {
						if(!IsEnableToAddEditFavoriteNumber(stat_shmp, 0))
						{
		  				    sprintf((char *)pdata,"Friends and Family number is not allowed for low balance or too many exsiting numbers!");
		  				    ReleasePortStatus(portid);
						    return -2;
						}
				       }
				       
					strcpy(stat_shmp->process_info.tmpDigits, (char *)pdata2);
					if(!IsFavoriteNumberTypeOk(stat_shmp))
					{
	  				    sprintf((char *)pdata,"Friends and Family number type is not allowed!");
	  				    ReleasePortStatus(portid);
					    return -2;				
					}
					
					 if(shmp->favorite_number[ptagcode-24][MAX_MSISDN_BLEN-1]<2)
					 {
					     shmp->favorite_numbers ++;
					     if(shmp->favorite_numbers>MAX_FAVORITE_NUMBERS)
					         shmp->favorite_numbers = MAX_FAVORITE_NUMBERS;
					 }
				}
				
				if(ocode == OMC_OCODE_EDIT)
				{
					len  = shmp->favorite_number[ptagcode-24][MAX_MSISDN_BLEN-1];
					BcdToAscii(tmpBuf,shmp->favorite_number[ptagcode-24],18);
					StringCut(tmpBuf,'F');
					tmpBuf[len] = 0;

					if(strcmp(tmpBuf, (char *)pdata2))
					{
					    strcpy(stat_shmp->process_info.tmpDigits, (char *)pdata2);
					    if(!IsFavoriteNumberTypeOk(stat_shmp))
					    {
	  				        sprintf((char *)pdata,"Friends and Family number type is not allowed!");
	  				        ReleasePortStatus(portid);
					        return -2;				
					    }
						if(friendFamilyControl->friendFamilyChangeChargeFlag)
						{
							if(IncreaseFavoriteNumberChangeCounterByOmc(shmp)>
								friendFamilyControl->friendFamilyFreeChangeTimes)
							{
							       charge = -BillingFavoriteNumberChange(stat_shmp);
								difbalance += charge;
								shmp->balance = shmp->balance + charge;
								chargeFlag |= 0x08;
							}
						}
					}//strcmp
				}

				ReleasePortStatus(portid);
				
				AsciiToBcd(shmp->favorite_number[ptagcode-24], (char *)pdata2,(vallen+1)/2*2);
				shmp->favorite_number[ptagcode-24][MAX_MSISDN_BLEN-1] = vallen;
				if(vallen %2 ==0)
					shmp->favorite_number[ptagcode-24][vallen/2] = 0xFF; 
				else
					shmp->favorite_number[ptagcode-24][vallen/2] |= 0x0F; 

				totalLen = add_tlv(tagbuf,vallen,pdata2,0x80,asnbuf);	
			}
			break;			
			case 0x01d:
			if(ocode==OMC_OCODE_READ){
				len = 1;
				totalLen = add_tlv(tagbuf,1,&shmp->last_call_type,0x80,asnbuf);
			}
			else
			{
				totalLen = add_null(tagbuf,0x80,asnbuf);	
			}
			break;
			case 0x1e:
			if(ocode==OMC_OCODE_READ && shmp->last_call_date>0)
			{
				AsciiTime(buf,shmp->last_call_date);
				AsciiToBcd((BYTE *)valbuf,buf,14);
				len = 7;
				totalLen = add_tlv(tagbuf,len, (u_char *)valbuf,0x80,asnbuf);
			}
			else
			{
				totalLen = add_null(tagbuf,0x80,asnbuf);	
			}
			break;
			case 0x1f:
			if(ocode ==OMC_OCODE_READ)
			{
				len = shmp->last_call_number[MAX_MSISDN_BLEN-1]; 
				if(len<3 || len>MAX_MSISDN_ALEN)
				{
					totalLen = add_null(tagbuf,0x80,asnbuf);	
				}
				else
				{
					if(len%2==1)
					{
						ppsBcdToAsciiR(buf,shmp->last_call_number,len+1);
						StringCut(buf,'F');
					}
					else
					{
						ppsBcdToAsciiR(buf,shmp->last_call_number,len);
					}
					totalLen = add_tlv(tagbuf,len, (u_char *)buf,0x80,asnbuf);
				}
			}			  
			break;
			case 32: /* in net cap SM balance */
			if(ocode==OMC_OCODE_READ)
			{
				balance = shmp->in_net_cap_sms_balance;
				totalLen = AddInteger(tagbuf,(int)balance,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode==OMC_OCODE_ADD)
			{			
				int int_val;
				if(GetInteger(tagbuf,&int_val ,pflag,asnbuf2)<=0) continue;
				if(int_val>ppsParam.serviceControl.maxBalance){
					sprintf((char *)pdata,"Too big balance(%d),should be(0-%d)",
								int_val,ppsParam.serviceControl.maxBalance);
					DebugMsg(debugmib.display.asciiOut,"Too big account balance(%d)",int_val);
					return -2;
				}
				balance = (double)int_val;
				shmp->in_net_cap_sms_balance = balance;
			
				totalLen = AddInteger(tagbuf,int_val,0x80,asnbuf);
			}
			break;
			case 33: /* in net cap SM counter */
			if(ocode ==OMC_OCODE_READ)
			{
				totalLen = AddInteger(tagbuf,shmp->in_net_cap_sms_counter,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
			{
				if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
				if(tmpval>50000)
				{
					sprintf((char *)pdata,"Too big value(%d),should be(0-%d)",
						tmpval,50000);
					return -2;
				}
				shmp->in_net_cap_sms_counter = tmpval;
				totalLen = AddInteger(tagbuf,shmp->in_net_cap_sms_counter,0x80,asnbuf);
			}
			break;
			case 34: /* out net cap SM balance */
			if(ocode==OMC_OCODE_READ)
			{
				balance = shmp->out_net_cap_sms_balance;
				totalLen = AddInteger(tagbuf,(int)balance,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode==OMC_OCODE_ADD)
			{			
				int int_val;
				if(GetInteger(tagbuf,&int_val ,pflag,asnbuf2)<=0) continue;
				if(int_val>ppsParam.serviceControl.maxBalance){
					sprintf((char *)pdata,"Too big  balance(%d),should be(0-%d)",
								int_val,ppsParam.serviceControl.maxBalance);
					DebugMsg(debugmib.display.asciiOut,"Too big account balance(%d)",int_val);
					return -2;
				}
				balance = (double)int_val;				
				shmp->out_net_cap_sms_balance = balance;
				totalLen = AddInteger(tagbuf,int_val,0x80,asnbuf);
			}
			break;
			case 35: /* out net cap SM counter */
			if(ocode ==OMC_OCODE_READ)
			{
				totalLen = AddInteger(tagbuf,shmp->out_net_cap_sms_counter,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
			{
				if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
				if(tmpval>50000)
				{
					sprintf((char *)pdata,"Too big value(%d),should be(0-%d)",
						tmpval,50000);
					return -2;
				}
				shmp->out_net_cap_sms_counter = tmpval;
				totalLen = AddInteger(tagbuf,shmp->out_net_cap_sms_counter,0x80,asnbuf);
			}
			break;	
			case 36: /* in_net_cap_call_balance */
			if(ocode==OMC_OCODE_READ)
			{
				balance = shmp->in_net_cap_call_balance;
				totalLen = AddInteger(tagbuf,(int)balance,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode==OMC_OCODE_ADD)
			{			
				int int_val;
				if(GetInteger(tagbuf,&int_val ,pflag,asnbuf2)<=0) continue;
				if(int_val>ppsParam.serviceControl.maxBalance){
					sprintf((char *)pdata,"Too big balance(%d),should be(0-%d)",
								int_val,ppsParam.serviceControl.maxBalance);
					DebugMsg(debugmib.display.asciiOut,"Too big account balance(%d)",int_val);
					return -2;
				}
				balance = (double)int_val;
				shmp->in_net_cap_call_balance = balance;
			
				totalLen = AddInteger(tagbuf,int_val,0x80,asnbuf);
			}
			break;
			case 37: /* in_net_cap_call_duration */
			if(ocode ==OMC_OCODE_READ)
			{
				totalLen = AddInteger(tagbuf,shmp->in_net_cap_call_duration,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
			{
				if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
				if(tmpval>50000)
				{
					sprintf((char *)pdata,"Too big value(%d),should be(0-%d)",
						tmpval,50000);
					return -2;
				}
				shmp->in_net_cap_call_duration = tmpval;
				totalLen = AddInteger(tagbuf,shmp->in_net_cap_call_duration,0x80,asnbuf);
			}
			break;	
			case 38: /* out_net_cap_call_balance */
			if(ocode==OMC_OCODE_READ)
			{
				balance = shmp->out_net_cap_call_balance;
				totalLen = AddInteger(tagbuf,(int)balance,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode==OMC_OCODE_ADD)
			{			
				int int_val;
				if(GetInteger(tagbuf,&int_val ,pflag,asnbuf2)<=0) continue;
				if(int_val>ppsParam.serviceControl.maxBalance){
					sprintf((char *)pdata,"Too big balance(%d),should be(0-%d)",
								int_val,ppsParam.serviceControl.maxBalance);
					DebugMsg(debugmib.display.asciiOut,"Too big account balance(%d)",int_val);
					return -2;
				}
				balance = (double)int_val;
				shmp->out_net_cap_call_balance = balance;
			
				totalLen = AddInteger(tagbuf,int_val,0x80,asnbuf);
			}
			break;
			case 39: /* out_net_cap_call_duration */
			if(ocode ==OMC_OCODE_READ)
			{
				totalLen = AddInteger(tagbuf,shmp->out_net_cap_call_duration,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
			{
				if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
				if(tmpval>50000)
				{
					sprintf((char *)pdata,"Too big value(%d),should be(0-%d)",
						tmpval,50000);
					return -2;
				}
				shmp->out_net_cap_call_duration = tmpval;
				totalLen = AddInteger(tagbuf,shmp->out_net_cap_call_duration,0x80,asnbuf);
			}
			break;						
			case 40: //freeSMSActiveDays
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = add_tlv(tagbuf,1,&shmp->freeSMSActiveDays,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
					shmp->freeSMSActiveDays = tmpval;
					totalLen = add_tlv(tagbuf,1,&shmp->freeSMSActiveDays,0x80,asnbuf);
				}
			break;
			case 41://pool role
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = add_tlv(tagbuf,1,&shmp->poolAccount.role,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
					shmp->poolAccount.role = tmpval;
					totalLen = add_tlv(tagbuf,1,&shmp->poolAccount.role,0x80,asnbuf);
				}
			break;
			case 42:
				pParent = &shmp->poolAccount.roleParty.parent;
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = add_tlv(tagbuf,1,&pParent->childNumber,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
					pParent->childNumber = tmpval;
					totalLen = add_tlv(tagbuf,1,&pParent->childNumber,0x80,asnbuf);
				}
			break;
			case 43:
				pParent = &shmp->poolAccount.roleParty.parent;
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = add_tlv(tagbuf,1,&pParent->changeTimes,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
					pParent->changeTimes = tmpval;
					totalLen = add_tlv(tagbuf,1,&pParent->changeTimes,0x80,asnbuf);
				}
			break;
			case 44:
			case 45:
			case 46:
			case 47:
			case 48:				
			ptagcode -= 44;
			pParent = &shmp->poolAccount.roleParty.parent;			
			if(ocode ==OMC_OCODE_READ)
			{
				len = pParent->childMsisdn[ptagcode][MAX_MSISDN_BLEN-1]; 
				if(len<3 || len>MAX_MSISDN_ALEN)
				{
				    totalLen = add_null(tagbuf,0x80,asnbuf);	
				}
				else
				{
	  		        BcdToAscii(valbuf,pParent->childMsisdn[ptagcode],MAX_MSISDN_ALEN);
					valbuf[len] = 0;
				    totalLen = add_tlv(tagbuf,len, (u_char *)valbuf,0x80,asnbuf);
				}
			}
			else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
			{
				int tmp;
				
				tmp = getHowManyChildren(shmp);
				
				if(vallen == 0 && ocode == OMC_OCODE_EDIT)
				{
					if(pParent->childMsisdn[ptagcode][MAX_MSISDN_BLEN-1]>3) 
					{
						decMasterChildren(shmp);
					}
					memset(pParent->childMsisdn[ptagcode],0,MAX_MSISDN_BLEN);
				    totalLen = add_null(tagbuf,0x80,asnbuf);	
					continue;
				}

				if((vallen > MAX_MSISDN_ALEN || vallen<3 )&& vallen!=0)
				{
						sprintf((char *)pdata,"child number length(%d) error, should be(3-16)",vallen);
						DebugMsg(debugmib.display.asciiOut,
						"child number length(%d) error, should be(3-16)",vallen);
						return -2;
				 }
				
				 if(atoi((char *)pdata2)==0)
				 {						
						continue;
				 }
				
		         for(i=0;i<vallen;i++)
				 {					
					if(pdata2[i]>'9' || pdata2[i]<'0') 
					{
						sprintf((char *)pdata,"Error child number");
						DebugMsg(debugmib.display.asciiOut,"Error child number");
						return -2;
				    }
				 }	

				serviceControlItem = &ppsParam.ppsTables.cosTable[shmp->cos_id].serviceControl[0];			
				if(!(serviceControlItem->poolAccountFlag))
				{
					sprintf((char *)pdata,"pool account service not applied!");
					return -2;
				}

				if(pParent->childMsisdn[ptagcode][MAX_MSISDN_BLEN-1]<2) 
				{
					if(tmp != ptagcode)
					{
						sprintf((char *)pdata,"pool child must be added one by one!");
						return -2;
					}
					increMasterChildren(shmp);
				}

				len  = pParent->childMsisdn[ptagcode][MAX_MSISDN_BLEN-1];
				BcdToAscii(tmpBuf,pParent->childMsisdn[ptagcode],18);
				StringCut(tmpBuf,'F');
				tmpBuf[len] = 0;

				if(strcmp(tmpBuf, (char *)pdata2))
				{
					if(serviceControlItem->poolAccountChangeChargeFlag)					
					{
						if(increMasterChangeCounter(shmp)>
							serviceControlItem->poolAccountFreeChangeTimes)
						{
							portid = AssignProcID(0);
							if(portid>=0 && portid< MAX_FSM_PROCESS)
							{
								stat_shmp = (_state_data *)&smshm_ptr->state_data;
								stat_shmp += portid;
								
								stat_shmp->caller_info.head = head;
								stat_shmp->caller_info.tail = tail;
								
								BillingChildNumberChange(stat_shmp);
								SendPeriodicChargeCDR(head,
													tail,
													stat_shmp->caller_info.charge,
													CT_PERIODIC_CHARGE);
								ReleasePortStatus(portid);
							}
						}
					}
				}//strcmp

				AsciiToBcd(pParent->childMsisdn[ptagcode], (char *)pdata2,(vallen+1)/2*2);
				pParent->childMsisdn[ptagcode][MAX_MSISDN_BLEN-1] = vallen;
				if(vallen %2 ==0)
					pParent->childMsisdn[ptagcode][vallen/2] = 0xFF; 
				else
					pParent->childMsisdn[ptagcode][vallen/2] |= 0x0F; 

				totalLen = add_tlv(tagbuf,vallen,pdata2,0x80,asnbuf);	
			}
			break;			
			case 49:
				pChild= &shmp->poolAccount.roleParty.child;	
				if(ocode==OMC_OCODE_READ)
				{
					len = pChild->parentMsisdn[MAX_MSISDN_BLEN-1]; 
					if(len<3 || len>MAX_MSISDN_ALEN)
					{
					    totalLen = add_null(tagbuf,0x80,asnbuf);	
					}
					else
					{
		  		        BcdToAscii(valbuf,pChild->parentMsisdn,MAX_MSISDN_ALEN);
						valbuf[len] = 0;
					    totalLen = add_tlv(tagbuf,len, (u_char *)valbuf,0x80,asnbuf);
					}
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(vallen == 0 && ocode == OMC_OCODE_EDIT)
					{						
						memset(pChild->parentMsisdn,0,MAX_MSISDN_BLEN);
					    totalLen = add_null(tagbuf,0x80,asnbuf);	
						continue;
					}
					if((vallen > MAX_MSISDN_ALEN || vallen<3 )&& vallen!=0)
					{
						sprintf((char *)pdata,"parent number length(%d) error, should be(3-16)",vallen);
						DebugMsg(debugmib.display.asciiOut,
						"parent number length(%d) error, should be(3-16)",vallen);
						return -2;
				 	}
				
					if(atoi((char *)pdata2)==0)
					{						
						continue;
					}
					
			        for(i=0;i<vallen;i++)
					{					
						if(pdata2[i]>'9' || pdata2[i]<'0') 
						{
							sprintf((char *)pdata,"Error parent number");
							DebugMsg(debugmib.display.asciiOut,"Error parent number");
							return -2;
					    }
					}	
					AsciiToBcd(pChild->parentMsisdn, (char *)pdata2,(vallen+1)/2*2);
					pChild->parentMsisdn[MAX_MSISDN_BLEN-1] = vallen;
					if(vallen %2 ==0)
						pChild->parentMsisdn[vallen/2] = 0xFF; 
					else
						pChild->parentMsisdn[vallen/2] |= 0x0F; 
					totalLen = add_tlv(tagbuf,vallen,pdata2,0x80,asnbuf);	
				}
			break;
			case 50:
				pChild= &shmp->poolAccount.roleParty.child;	
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = AddInteger(tagbuf,(int)pChild->callsToday,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
					pChild->callsToday = tmpval;
					totalLen = AddInteger(tagbuf,(int)pChild->callsToday,0x80,asnbuf);
				}
			break;
			case 51:
				pChild= &shmp->poolAccount.roleParty.child;	
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = AddInteger(tagbuf,(int)pChild->smsToday,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
					pChild->smsToday = tmpval;
					totalLen = AddInteger(tagbuf,(int)pChild->smsToday,0x80,asnbuf);
				}
			break;
			case 52:
				pChild= &shmp->poolAccount.roleParty.child;					
				if(ocode==OMC_OCODE_READ)
				{					
					balance = pChild->chargedToday;
					totalLen = AddInteger(tagbuf,(int)balance,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode==OMC_OCODE_ADD)
				{				
					int int_val;
					
					if(GetInteger(tagbuf,&int_val ,pflag,asnbuf2)<=0) continue;					
					
					pChild->chargedToday = (double)int_val;
					totalLen = AddInteger(tagbuf,int_val,0x80,asnbuf);
				}
			break;			
			case 53:/*	KB	*/
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = AddInteger(tagbuf,(u32)((shmp->gprsVolume+BYTES_PER_KB-1) >> 10),0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;					
					shmp->gprsVolume= (long)tmpval << 10;
					totalLen = AddInteger(tagbuf,(u32)(shmp->gprsVolume>>10),0x80,asnbuf);
					if (shmp->gprsVolume < 1)
					{
						shmp->cap_sms_notify_counter = 0;
					}
				}
			break;
			case 54:
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = AddInteger(tagbuf,(u32)shmp->gprsBalance,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{					
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
					if(tmpval>ppsParam.serviceControl.maxBalance)
					{
						sprintf((char *)pdata,"Too big value(%d),should be(0-%d)",
							tmpval,ppsParam.serviceControl.maxBalance);
						return -2;
					}
					shmp->gprsBalance= (double)tmpval;
					totalLen = AddInteger(tagbuf,tmpval,0x80,asnbuf);
				}
			break;
			case 55:/*	KB	*/				
				if(ocode ==OMC_OCODE_READ)
				{					
					totalLen = AddInteger(tagbuf,(u32)((shmp->gprsVolumeThisMonth+BYTES_PER_KB-1) >> 10),0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;					
					shmp->gprsVolumeThisMonth = (long)tmpval << 10;
					totalLen = AddInteger(tagbuf,(u32)(shmp->gprsVolumeThisMonth >> 10),0x80,asnbuf);
				}
			break;			
			case 56:
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = AddInteger(tagbuf,shmp->gprsNotificationTimes,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;					
					shmp->gprsNotificationTimes= tmpval;
					totalLen = AddInteger(tagbuf,shmp->gprsNotificationTimes,0x80,asnbuf);
				}				
			break;
			case 57:				
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = add_tlv(tagbuf,1,&shmp->callMeBackRequestTimes,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
					shmp->callMeBackRequestTimes = tmpval;
					totalLen = add_tlv(tagbuf,1,&shmp->callMeBackRequestTimes,0x80,asnbuf);
				}
			break;
			case 58:
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = add_tlv(tagbuf,1,&shmp->favorite_plan_id,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;
					if(tmpval>MAX_FAVORITE_PALN)
					{
						sprintf((char *)pdata,"Too big value(%d),should be(0-6)",tmpval);
						return -2;
					}
					if(tmpval != shmp->favorite_plan_id )
					{
						friendFamilyControl = &ppsParam.ppsTables.cosTable[shmp->cos_id].friendFamilyControl[0];			

						if(tmpval==0)
						{
						    unsigned char bValue=0x01;				                  
					           if(shmp->favorite_plan_id != 0)
						    {
						        shmp->rent_type ^= (bValue<<(friendFamilyControl->friendFamilyPlan[shmp->favorite_plan_id-1].rentID -1 ));
						    }
						    shmp->favorite_plan_state = 0;
						    shmp->free_favorite_call = 0;
						    shmp->free_favorite_sms = 0;
						}
						else
						{
							//if(friendFamilyControl->friendFamilyPlan[tmpval-1].rentCharge>0)
							{
								if(GetAccountBalance(head, tail)>=friendFamilyControl->friendFamilyPlan[tmpval-1].rentCharge)
								{
								       unsigned char bValue=0x01;
								       if(shmp->favorite_plan_id != 0)
								       {
								           shmp->rent_type ^= (bValue<<(friendFamilyControl->friendFamilyPlan[shmp->favorite_plan_id-1].rentID -1 ));
								       }

							              bValue = 0x01;
								       shmp->rent_type |= (bValue << (friendFamilyControl->friendFamilyPlan[tmpval-1].rentID-1));
								       								       
									portid = AssignProcID(0);
									stat_shmp = (_state_data *)&smshm_ptr->state_data;
									stat_shmp += portid;
													
									stat_shmp->caller_info.head = head;
									stat_shmp->caller_info.tail = tail;
									stat_shmp->caller_info.cos_id = shmp->cos_id;	
									stat_shmp->caller_info.charge = friendFamilyControl->friendFamilyPlan[tmpval-1].rentCharge;

									shmp->balance = shmp->balance -friendFamilyControl->friendFamilyPlan[tmpval-1].rentCharge;
								       shmp->free_favorite_call = 0;
								       shmp->free_favorite_sms = 0;
									shmp->favorite_plan_state = 0;
								       
								       chargeFlag |= 0x04;
								       difbalance += -friendFamilyControl->friendFamilyPlan[tmpval-1].rentCharge;

									ReleasePortStatus(portid);					
									send_f_f_sms_flag = 1;
									
								}
								else
							      {
							          if(ppsParam.ppsTables.cosTable[shmp->cos_id].smsNotificationInquiry[0].smsNotificationWhenFriendFamilyServiceReg)
		                                                       sendPPSNotificationMessage(head, tail, SMS_FRIEND_FAMILY_REG_FAILED);  
							          sprintf((char *)pdata,"Failed to provision Friends and Family service for account low balance");
							          return -2;
							      }
							}
						}
					}
					shmp->favorite_plan_id = tmpval;
					totalLen = add_tlv(tagbuf,1,&shmp->favorite_plan_id,0x80,asnbuf);
				}				
			break;
			case 59:
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = AddInteger(tagbuf,shmp->free_favorite_call,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;					
					shmp->free_favorite_call= tmpval;
					totalLen = AddInteger(tagbuf,shmp->free_favorite_call,0x80,asnbuf);
				}
			break;			
			case 60:
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = AddInteger(tagbuf,shmp->free_favorite_sms,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;					
					shmp->free_favorite_sms= tmpval;
					totalLen = AddInteger(tagbuf,shmp->free_favorite_sms,0x80,asnbuf);
				}
			break;
			case 61:
			if(ocode ==OMC_OCODE_READ)
			{
				totalLen = add_tlv(tagbuf,1,&shmp->favorite_plan_state,0x80,asnbuf);
			}
			else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
			{
				if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;				
				shmp->favorite_plan_state = tmpval;
				totalLen = add_tlv(tagbuf,1,&shmp->favorite_plan_state,0x80,asnbuf);
			}				
			break;
			case 62:	//bundle data
			if(ocode ==OMC_OCODE_READ)
				{					
					totalLen = AddInteger(tagbuf,(u32)((shmp->bundle_info.data+BYTES_PER_KB-1) >> 10),0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;					
					shmp->bundle_info.data = (long)tmpval << 10;
					totalLen = AddInteger(tagbuf,(u32)(shmp->bundle_info.data >> 10),0x80,asnbuf);
				}
	
			break;
			case 63: //mo voice
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = AddInteger(tagbuf,shmp->bundle_info.mo_voice,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;					
					shmp->bundle_info.mo_voice = tmpval;
					totalLen = AddInteger(tagbuf,shmp->bundle_info.mo_voice,0x80,asnbuf);
				}
			break;
			case 64: //mt voice
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = AddInteger(tagbuf,shmp->bundle_info.mt_voice,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;					
					shmp->bundle_info.mt_voice = tmpval;
					totalLen = AddInteger(tagbuf,shmp->bundle_info.mt_voice,0x80,asnbuf);
				}
			break;
			case 65: //sms
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = AddInteger(tagbuf,shmp->bundle_info.sms,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;					
					shmp->bundle_info.sms= tmpval;
					totalLen = AddInteger(tagbuf,shmp->bundle_info.sms,0x80,asnbuf);
				}
			break;
			case 66: //validity
				if(ocode==OMC_OCODE_READ)
				{
					AsciiTime(buf,shmp->bundle_info.validity_date);
					AsciiToBcd((BYTE *)valbuf,buf,14);
					len = 7;
					totalLen = add_tlv(tagbuf,len, (u_char *)valbuf,0x80,asnbuf);
				}else if(ocode==OMC_OCODE_EDIT || ocode==OMC_OCODE_ADD)
				{
					BcdToAscii(buf,pdata2,vallen*2);		   
					shmp->bundle_info.validity_date = MakeTime(NULL,buf);
					AsciiTime(buf,shmp->bundle_info.validity_date);
					AsciiToBcd((BYTE *)valbuf,buf,14);
					len = 7;
					totalLen = add_tlv(tagbuf,len, (u_char *)valbuf,0x80,asnbuf);
				}

			break;
			case 67:
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = AddInteger(tagbuf,shmp->bundle_info.warning_sms_flag,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;					
					shmp->bundle_info.warning_sms_flag= tmpval;
					totalLen = AddInteger(tagbuf,shmp->bundle_info.warning_sms_flag,0x80,asnbuf);
				}

			break;
			case 68:
				if(ocode ==OMC_OCODE_READ)
				{
					totalLen = AddInteger(tagbuf,shmp->cap_sms_notify_counter,0x80,asnbuf);
				}
				else if(ocode==OMC_OCODE_EDIT || ocode ==OMC_OCODE_ADD)
				{
					if(GetInteger(tagbuf, (int *)&tmpval ,pflag,asnbuf2)<=0) continue;					
					shmp->cap_sms_notify_counter = tmpval;
					totalLen = AddInteger(tagbuf,shmp->cap_sms_notify_counter, 0x80, asnbuf);
				}
			break;
			default:
				return 0;
			break;
		}//switch
	}//for
	
	if(ocode==OMC_OCODE_READ)
	{ //read
		datalen = totalLen;  
	}
	else
	{
		if(ocode == OMC_OCODE_ADD)
		{
			store_index = AddPPSSubsData(shmp);
			debugmib.watch_dog[7]++;
		}
		else
		{
			ModifyPPSSubsData(shmp);
		}
		head = store_index / UNIT_STORE_ACCOUNT;
		tail = store_index % UNIT_STORE_ACCOUNT;

		if(chargeFlag & 0x01)
		{
			SendOperatorRechargeCDR(head,tail,difbalance);
			AccountLatchFlagCheck(head,tail);
		}
		if(chargeFlag & 0x02){
			SendOperatorRechargeCDR(head,tail,dif_promo_balance);
		}
		if(chargeFlag & 0x04)
		{
		      SendPPSGeneralCDR(head, tail, difbalance, CT_FF_ACTIVATION);
		}
		if(chargeFlag & 0x08)
		{
		     SendPPSGeneralCDR(head, tail, difbalance, CT_FF_NUMBER_EDIT);
		}
		if(chargeFlag > 0)
			pps_real_update_user_file(head, tail);
		
		SendAccountRealSync(store_index,difbalance,dif_promo_balance,ocode); 
		if(send_f_f_sms_flag)
		{
		     if(ppsParam.ppsTables.cosTable[shmp->cos_id].smsNotificationInquiry[0].smsNotificationWhenFriendFamilyServiceReg)
	            	   sendPPSNotificationMessage(head, tail, SMS_FRIEND_FAMILY_REG);  

		}
	}
	
	return totalLen;
}
/* ----------------+ Sync configuration data procedure end +---------------*/

int
DeleteSubscriberDataHead(int head)
{
	int  i;
	_prepaid_info	*shmp=NULL;
	_prepaid_data	*shmp1=&ramshm_ptr->prepaid_data;

	PutLogFunID("DeleteSubscriberDataHead");

	shmp = &ramshm_ptr->prepaid_data.prepaid_info[head][0];
	for(i=0;i<UNIT_STORE_ACCOUNT;i++)
	{
 		if(shmp->msisdn[9] != 0 )  /*if whole segment buf ==0 then, clear the head*/
		{
			return 0;
		}
		shmp ++;
	}
	shmp1->msisdn_head[head] = 0;

	return 1;   
}

void DebugMsgDelimiter()
{
	char buf[64]="";

	sprintf(buf,"=====================================================");
	DebugMsg(debugmib.display.asciiOut,"%s",buf);
}

/*----------------------------------------*/
int GetRamData(BYTE oidLen,DWORD *oid,BYTE *pdata,u_short dataLen)
{
	int	totalLen=0,vallen=0;
	int	loop=0,ramMark,segno=0;
	BYTE	pdata1[1600],pdata2[1600],pflag;
	u_short	ptagcode;
	char	tagbuf[32],msisdnbuf[32]="",card_no[32];
 
	ASN_BUF       *asnbuf, asnbuf1;
	ASN_BUF       *asnbuf2, asnbuf3;

	PutLogFunID("GetRamData");


	memset(pdata1,0,dataLen+1);
	memset(pdata2,0,dataLen+1);

	memcpy(pdata1,pdata,dataLen);

	asnbuf = &asnbuf1;

	AsnEncode(pdata,1600,asnbuf);
	asnbuf2 = &asnbuf3;

	AsnDecode(pdata1, dataLen, UNIT_STORE_ACCOUNT, NULL, asnbuf2);
	//BcdToAscii(buf,pdata1,dataLen*2);
	//printf("dataLen:%d,data:%s\n",dataLen,buf);

	//if(oidLen<15) return 0;
	ramMark = oid[12];
  
	segno = oid[13]-1;
	switch(ramMark)
	{
		case 0x01:  /* MSISDN SEGMENT KEY */
		if(segno>=MSISDN_HEAD_NUM || segno<0 ) 
		{			
			return -1;
		}
		for(loop=1;loop<=UNIT_STORE_ACCOUNT;loop++)
		{     
			sprintf(tagbuf,"-%d",loop);
			if((vallen=GetAnyTLV(tagbuf,1600,pdata2,&pflag,&ptagcode,asnbuf2))<0)
			{
				break;
			}
			else
			{
				if(ptagcode<1 || ptagcode>UNIT_STORE_ACCOUNT) continue;
				sprintf(tagbuf,"%d",ptagcode);
				pdata2[vallen]=0;
			   
				if(GetAccountInfoBySortIndex(segno*UNIT_STORE_ACCOUNT+ptagcode-1,msisdnbuf))
				{
					totalLen = add_tlv(tagbuf,strlen(msisdnbuf), (u_char *)msisdnbuf,0x80,asnbuf);
				}
				else
				{
					totalLen = add_null(tagbuf,0x80,asnbuf);
				}
		
			}	
		}
		break;
		case 0x03:  /* prepaid card segment key */
		    //DebugMsg(debugmib.display.asciiOut,"Get Prepaid Card : segment is:%d",segno);
		
		if(segno>=MAX_CARD_SEGMENT){
			DebugMsgDelimiter();
			DebugMsg(debugmib.display.asciiOut,"Get Prepaid Card error: segment is too big:%d",segno);
			return -1;
		}
	
		for(loop=1;loop<=MAX_CARD_NUM_PSEG;loop++)
		{     
			sprintf(tagbuf,"-%d",loop);
			if((vallen=GetAnyTLV(tagbuf,1600,pdata2,&pflag,&ptagcode,asnbuf2))<0){
				break;
			}
			sprintf(tagbuf,"%d",ptagcode);
			pdata2[vallen]=0;
			if(ptagcode>0 && ptagcode <=MAX_CARD_NUM_PSEG )
			{
				if(GetRechargeCardByIndex(segno*MAX_CARD_NUM_PSEG+ptagcode-1,card_no))
				{
					totalLen = add_tlv(tagbuf,strlen(card_no), (u_char *)card_no,0x80,asnbuf);
				}
				else
				{									
					totalLen = add_null(tagbuf,0x80,asnbuf);
				}
			}
		}	   
		break;
		default:
		DebugMsgDelimiter();
		DebugMsg(debugmib.display.asciiOut,
					"Get Ram data error:  error oid[13]:%d",oid[13]);
		return -1;
		break;
	}
	if(totalLen==0){
		DebugMsgDelimiter();
		DebugMsg(debugmib.display.asciiOut,
			"Get Ram data error: return length=0 oid[14]:%d",oid[14]);
   	}
	return totalLen;
}

/* ----------------------------------------------------------  */


int IsPromotionTime(COSTable *cosTableItem,  struct		timeval tvn)
{
	if(cosTableItem->tariff[0].promotionUseControl.rechargePromotionStartDate<=tvn.tv_sec
		&&
		cosTableItem->tariff[0].promotionUseControl.rechargePromotionEndDate>=tvn.tv_sec)
	{
		return 1;
	}
	return 0;
}

int	GetPromotionAmount(COSTable	*cosTableItem,int charge)
{
	int loop;
		
	for(loop=0;loop<MaxRechargeCardType;loop++)
	{
		if(charge == cosTableItem->rechargePromotion[loop].rechargeCardCredit)
			return cosTableItem->rechargePromotion[loop].promotionBonus;
	}
	return 0;
}

int	GetPromotionDay(COSTable	*cosTableItem,int charge)
{
	int loop;
	for(loop=0;loop<MaxRechargeCardType;loop++)
	{
		if(charge == cosTableItem->rechargePromotion[loop].rechargeCardCredit)
			return cosTableItem->rechargePromotion[loop].promotionValidity;
	}
	return 0;
}

int IsMNPCalledNumber(char *number)
{
	int len=0,loop=0;
	
	if(ppsParam.serviceControl.support_mnp_flag==0)
		return 0;
	for(loop=0;loop<MaxMnpPrefixItem;loop++)
	{
		if(ppsParam.ppsTables.mnpPrefix[loop].existFlag == FALSE)
			continue;
		if(ppsParam.ppsTables.mnpPrefix[loop].prefix[0] == 0)
			continue;
		len = strlen((char *)ppsParam.ppsTables.mnpPrefix[loop].prefix);
		
		if(!memcmp(number,ppsParam.ppsTables.mnpPrefix[loop].prefix,len))
			return 1;
	}	
	return 0;
}

static int pps_parmaeter_auto_save_flag = 0;

void ppsSetParameterAutoSaveFlag()
{
	pps_parmaeter_auto_save_flag = 1;		
}

void ppsCheckParameterAutoSaveFlag()
{
	static int pps_parameter_auto_save_counter = 0;
	if(pps_parameter_auto_save_counter++ %200 == 0)
	{		
		if(pps_parmaeter_auto_save_flag)
		{
			pps_parmaeter_auto_save_flag = 0;
			SavePPSParam();
		}
	}
}

static char smsDelimiter[32]={"$value"};
void SetSMSDelimiter(char *delimiter)
{
	int len;
	if(delimiter == NULL) return;
	len = strlen(delimiter);
	if(len == 0 || len>32) return;
	strcpy(smsDelimiter,delimiter);
}

 char    realSentence[1024]="";
char* ppsParseSentence(char *sentence,int varNum, char *varStr[])
{
        char    buf[128]={""},*ptr1,*ptr2;
       
        int     length,tmplen=0,index=0,offset,loop=0;

        if(varNum<0 || varNum>8) return 0;
        if(sentence == NULL) return 0;
        length = strlen(sentence);
        if(length==6) return 0;

        ptr1 = sentence;
        memset(realSentence,0,1024);
        loop = 0;
        while(tmplen<length && loop++<8)
        {
                memset(buf,0,128);
                ptr2 = strstr(ptr1,smsDelimiter);
                if(ptr2 != NULL)
                {
                        offset = ptr2 - ptr1;

                        memcpy(buf,ptr1,offset);
                        strcat(realSentence,buf);

                        if(index<varNum)
                        {
                                strcat(realSentence,varStr[index]);
                                index ++;
                        }
                        ptr1 = ptr2+6;
                        tmplen += offset+6;
                }
                else
                {
                        strcat(realSentence,ptr1);
                        break;
                }
        }
        for(;index<varNum;index++)
        	strcat(realSentence, varStr[index]);
        return realSentence;
}

int UseExternalVC()
{
	return  ppsParam.serviceControl.external_vc;
}

int OCS_with_external_CRM()
{
	return  ppsParam.serviceControl.work_with_crm;
}

static int pps_week_index = 8;

int ppsGetWeekDay()
{
	char buf[32];
	
	pps_week_index = getweek(buf);
	return 1;
}

int CleanPPSUSSDStatisticsByDay();
int ppsSendUSSDStatistics(_state_data *shmp)
{
	int index=0;
	if(shmp == NULL)
		return 0;

	if(pps_week_index>=7)
	{
		CleanPPSUSSDStatisticsByDay();
	}

	if(shmp->process_info.serviceKey<=0 || shmp->process_info.serviceKey>=5)
		index = 4; //other
	else
		index = shmp->process_info.serviceKey - 1;
	
	debugmib.msgstatistics.ussd_operation[pps_week_index][index*2] ++;
	debugmib.msgstatistics.ussd_operation[pps_week_index][index*2+1] += 
		shmp->process_info.duration / 2;

	debugmib.msgstatistics.ussd_operation[pps_week_index][10] ++;
	debugmib.msgstatistics.ussd_operation[pps_week_index][11] += 
		shmp->process_info.duration / 2;
	
	return 1;
}

int CleanPPSUSSDStatisticsByDay()
{
	char buf[32];
	
	ppsGetWeekDay();

	sprintf(buf,"%04d%02d%02d", GetTmYear(), GetTmMonth(), GetTmDay());
	AsciiToBcd((BYTE *)debugmib.msgstatistics.ussd_operation_date[pps_week_index],buf,8);
	
	memset(debugmib.msgstatistics.ussd_operation[pps_week_index], 0, sizeof(int) * 3 * 4);
	return 0;
}

int pps_sub_sync_test()
{
	static int test_instance = 0;
	
	int head = 0, tail = 0;
	int balance = 10;
	
	_prepaid_data    	*shmp=&ramshm_ptr->prepaid_data;
	_prepaid_info		*sub_ptr = NULL;

	{
		head = test_instance / UNIT_STORE_ACCOUNT;
		tail = test_instance % UNIT_STORE_ACCOUNT;
		
		sub_ptr = &shmp->prepaid_info[head][tail];
		sub_ptr->balance = sub_ptr->balance - balance;
		sub_ptr->promo_balance = sub_ptr->promo_balance- balance;

		SendAccountRealSync(test_instance, 0-balance, 0-balance, 2);
		
	}
	
	return 1;
}

static char	pps_smpp_caller[32]="123";
static char	pps_smpp_called[32]="123";
int setPPSSmppNumber(char *number, int flag)
{
    if(flag == 0)
        strcpy(pps_smpp_caller, number);
    else
        strcpy(pps_smpp_called, number);
    return 1;
}

int sendPPSTestShortMessage(char *message)
{
    int len = strlen(message);
    
    smppSendMOSM(pps_smpp_caller, pps_smpp_called, (u_char *)message, len, 0);    
    return 1;
}

int get_not_change_transfer_expiry_date()
{
    return ppsParam.serviceControl.not_change_expiry_date_when_transfer;
}

int get_transfer_status_normal_flag()
{
    return ppsParam.serviceControl.only_normal_can_transfer_balance;
}

int get_transfer_resolution()
{
	return ppsParam.serviceControl.balance_transfer_resolution;
}

int pps_get_max_call_duration_in_second()
{
	return (ppsParam.serviceControl.max_call_duration);
}

int	pps_get_gy_postpaid_flag( )
{
	return ppsParam.serviceControl.gy_postpaid_flag;
}

int	pps_set_gy_delay_timer(int flag, int value)
{
	if(flag == 0)
		ppsParam.serviceControl.gy_delay_timer0 = value;
	else
		ppsParam.serviceControl.gy_delay_timer1 = value;
	return 1;
}

int pps_get_gy_delay_timer(int flag)
{
	if(flag == 0)
		return ppsParam.serviceControl.gy_delay_timer0;
	else
		return ppsParam.serviceControl.gy_delay_timer1;
}

int ocs_set_threshold_supported(int flag)
{
	ppsParam.serviceControl.gy_not_support_threshold = flag;
	return 1;
}

int ocs_get_threshold_supported()
{
	if(ppsParam.serviceControl.gy_not_support_threshold == 0)
		return 1;
	else
		return 0;
}

int ocs_set_gy_report_data_method(int flag)
{
	ppsParam.serviceControl.gy_report_data_method = flag;
	return 1;
}

int ocs_get_gy_report_data_metohd()
{
	return ppsParam.serviceControl.gy_report_data_method;
}

int pps_get_answer_timer_out()
{
	return ppsParam.serviceControl.answer_timer * CYCLE_STATE_INTERVAL;
}

int pps_get_control_state()
{
	return debugmib.display.control_state;
}

long pps_get_quota_per_request()
{
	if(pps_get_control_state())
	{
		int   FKB = (500 * 1024);
		long max_quota = 0;
		int   state_timer ;

		state_timer = (tvnow.tv_sec - debugmib.display.control_state_ts) / 300 + 1;
		if(state_timer > 9) state_timer = 9;
		
		max_quota = ppsParam.max_quota_per_request * 1024 * (10-state_timer) /10;
		
		if(max_quota < FKB)
			max_quota = FKB;
		
		return max_quota;
	}
	else
	return  (ppsParam.max_quota_per_request * 1024);// read from ppsConf.txt, can be modified by command line set volume=x
}

long pps_get_postpaid_quota_for_first_request()
{
	return  (ppsParam.data_quota_as_postpaid * 1024);// read from ppsConf.txt, can be modified by command line set volume=x
}

int pps_get_adaptor_flag()
{
	return  ppsParam.scf_work_as_adaptor;
}

int pps_get_adaptor_vcs_flag()
{
	return  ppsParam.scf_work_as_adaptor_with_vcs;
}

char *pps_get_smsc_addr()
{
	return ppsParam.serviceControl.smsc_addr;
}

int pps_get_ucbc_number(char *ucbc_number)
{
	strcpy(ucbc_number, (char *)ppsParam.serviceControl.ucbc_number);
	return 1;
}

int pps_get_ocs_proxy_nai_flag()
{
	return ppsParam.serviceControl.nai_for_diameter_proxy;
}

int pps_mt_play_voice_flag()
{
	return ppsParam.serviceControl.mt_play_voice_flag;
}

int pps_mt_cdr_enable_flag()
{
	return ppsParam.serviceControl.mt_not_charged_for_cdr;
}

int get_debug_log_flag();
int pps_debug_info(char *app_name, char *str_to_show)
{
	if(get_debug_log_flag() == 0)
		return 0;
	
	DebugMsg(debugmib.display.asciiOut, " ============= %s ==========\r\n", app_name);

	DebugMsg(debugmib.display.asciiOut, "%s",str_to_show);

	return 1;
}

int get_debug_log_flag()
{
	return logMsgFlag;
}

int pps_debug_flag_for_fd()
{
	return (logMsgFlag & 0xf7);
}

void PrintCosServiceNumber(u8 cos_id, u8 type)
{
    COSTable *cos_ptr = NULL;
    int i=0, srv_no_flag=0x0000;
    char service_type_name[3][8]={"voice","sms","ussd"};
    char service_number_name[16][64]={
		"HotLine",
		"Inquiry",
		"Scratch Card Recharge",
		"Credit Card Recharge",
		"ThirdParty Recharge",
		"Customer Care Center",
		"Recharge",
		"OPR",
		"Virtual Calling Card",
		"Palim Register",
		"Pool Account Service",
		"Credit Transfer Service",
		"Free SMS Service",
		"Call Me Back Service",
		"Friend&Family",
		"Tell Me My Number"		
	};
	
    if(cos_id >= MaxCosTableItem)
    {
        DebugMsg(debugmib.display.asciiOut,"Invalid COS ID [%02d]",cos_id);
	 return;
    }
	
    cos_ptr = &ppsParam.ppsTables.cosTable[cos_id];
    if(type == 0)
        srv_no_flag = cos_ptr->serviceControl[0].serviceNumberFlag;
    else if(type == 1)
	 srv_no_flag = cos_ptr->serviceControl[0].smsServiceFlag;
    else if(type == 2)
	 srv_no_flag = cos_ptr->serviceControl[0].ussdServiceFlag;
	
    DebugMsg(debugmib.display.asciiOut,"Service Number of [%6s ] for COS [%02d]",service_type_name[type], cos_id);
	
    for(i=0;i<16;i++)
    {
            if(srv_no_flag & (0x0001 << i))
            {
                DebugMsg(debugmib.display.asciiOut," %32s is enabled", service_number_name[i]);
            }
	     else
	    {
	        DebugMsg(debugmib.display.asciiOut," %32s is disabled", service_number_name[i]);
	    }
    }
		  
}

int ConfigureDefaultSerivceNumber()
{
    int cos_id;
    COSTable *cos_ptr=NULL;
	
    for(cos_id=0; cos_id<MaxCosTableItem;cos_id++)
    {
        cos_ptr = &ppsParam.ppsTables.cosTable[cos_id];
        cos_ptr->serviceControl[0].serviceNumberFlag = 0xffff;
        cos_ptr->serviceControl[0].smsServiceFlag = 0xffff;		
        cos_ptr->serviceControl[0].ussdServiceFlag = 0xffff;	
    }
	
    return 1;
}

int ConfigureSerivceNumberPerCos(char *cmd, u8 type)
{
    char cvalue[128];
    int ivalue, len, i, len_valid=0, cos_pos=0, value_pos=0;
    u8 cos_id=0;
    COSTable *cos_ptr=NULL;
				
    len = strlen(cmd);
	
    if(type == 0)
    {
        len_valid = 34;
        cos_pos = 15;
	 value_pos = 18;
    }
    else if(type == 1)
    {
        len_valid = 34;
        cos_pos = 15;
	 value_pos = 18;
    }
    else if(type == 2)
    {
        len_valid = 35;
        cos_pos = 16;
	 value_pos = 19;		
    }	

    if(len != len_valid)
    {
        DebugMsg(debugmib.display.asciiOut,"wrong command len (%d-%d): use srv_no_cfg ivr xx 0000000000000000", len, len_valid);
	 return 0;
    }
    		  
    memcpy(cvalue, &cmd[cos_pos], 2);
    cvalue[2] = 0;		  
    cos_id = atoi(cvalue);
    if(cos_id >= MaxCosTableItem)
    {
        DebugMsg(debugmib.display.asciiOut,"wrong cos id %d, should be (0-%d)!", cos_id, MaxCosTableItem-1);
        return 0;
    }

    ivalue = 0x0000;
	  
    for(i=value_pos;i<value_pos+16;i++)
    {
        if(cmd[i] == 0x30)
        {
            ;
        }
        else if(cmd[i] == 0x31)
        {
            ivalue |= 0x0001<<(i-value_pos);
        }
        else
        {
            DebugMsg(debugmib.display.asciiOut,"wrong command: use srv_no_cfg ivr xx 1111111111111111");
            return 0;
        }
    }
		  
    cos_ptr = &ppsParam.ppsTables.cosTable[cos_id];
    if(type == 0)
        cos_ptr->serviceControl[0].serviceNumberFlag = ivalue;
    else if(type== 1)
        cos_ptr->serviceControl[0].smsServiceFlag = ivalue;		
    else if(type== 2)
        cos_ptr->serviceControl[0].ussdServiceFlag = ivalue;		

    SavePPSParam();
    PrintCosServiceNumber(cos_id, type);

    return 1;
}

int GetSerivceNumberFlagPerCos(u8 cos_id, u8 service_type, u8 call_type)
{
    u8 retval = 0;
    u8 right_cycle = service_type;
    int flag_value=0;
	
    COSTable *cos_ptr=NULL;

    if(cos_id>=MaxCosTableItem || service_type>FriendFamilyPosition || call_type>2)
		return retval;
	
    cos_ptr = &ppsParam.ppsTables.cosTable[cos_id];	

    if(call_type == 0)
        flag_value = cos_ptr->serviceControl[0].serviceNumberFlag;
    else if(call_type== 1)
        flag_value = cos_ptr->serviceControl[0].smsServiceFlag;		
    else if(call_type== 2)
        flag_value = cos_ptr->serviceControl[0].ussdServiceFlag;	
    if(service_type == FriendFamilyPosition) right_cycle = 14;
	
    retval = (flag_value >> right_cycle) & (0x0001);
	
    return retval;	
}

int pps_get_smpp_link_type()
{
	return ppsParam.serviceControl.smpp_over_tcp_flag;
}

int ocs_get_calling_card_method()
{

	return ppsParam.serviceControl.calling_card_method;
}

int ocs_get_calling_card_cos_id()
{
	return ppsParam.serviceControl.calling_card_cos_id;

}

char *pps_get_service_number(unsigned int num_pos)
{
	char *ptr = NULL;

	if(num_pos < MAXServicePostion)
		ptr = (char *)ppsParam.ppsTables.serviceNumber[num_pos].number;

	return ptr;
}

char *ocs_get_crm_prefix_filename(char *ptr)
{
	if(ptr == NULL)
		return NULL;
	
//	strcpy(ptr , ppsParam.ocs_crm_control.base_prefix_file_name);
	strcpy(ptr, "/usr/local/agt/etc/tariff/prefix_table.txt");
	return ptr;
}

int ocs_get_crm_prefix_call_type_method()
{
	return ppsParam.ocs_crm_control.crm_call_type.equal_or_contain;
}

static 	char local_mobile_prefix_des[128][128]={"35","67235","0067235"};
static 	char local_pstn_prefix_des[128][128]={"32","67232","0067232"};
static 	int  local_mobile_index=0, local_pstn_index=0;

void 	ocs_get_crm_prefix_definition()
{
	char s[80],s1[80];
	int  len;
	unsigned char conf_state=0xFF;
  	FILE *fpConf;
      char file_name[128]={"/usr/local/agt/etc/tariff/prefix_definition.txt"};
	  
	fpConf = fopen(file_name, "rb");
	
	if(fpConf == NULL)
	{
		return ;
	}
   
	strcpy(s,"");
	strcpy(s1,"");
	while(fgets(s,1024,fpConf) !=(char *)0)
	{
		if( (int *)strchr(s,'#') !=NULL) continue;
		if( !strlen(s) ) continue;      
		len = strlen(s);
		if(len < 2) continue;
		
		if(s[len-1] == 0x0d ||s[len-1] == 0x0a)
		{
			if(s[len-2] == 0x0d ||s[len-2] == 0x0a)
			{
				s[len-2] = 0;
				len -= 2;			
			}
			else
			{
				s[len-1] = 0;
				len -= 1;
			}
		}

		if(strstr(s,"[local mobile]"))
		{
			conf_state = 0;
			local_mobile_index = 0;
			continue;
		}
		else if(strstr(s,"[local pstn]"))
		{
			conf_state = 1;
			local_pstn_index = 0;
			continue;
		}

		switch(conf_state)
		{	
			case 0:
				if(local_mobile_index<128)
					strcpy(local_mobile_prefix_des[local_mobile_index++], s);
			break;
			case 1:		
				if(local_pstn_index<128)
					strcpy(local_pstn_prefix_des[local_pstn_index++], s);
			break;    
      		}
	}
	
	fclose(fpConf);
		
}


int ocs_get_crm_prefix_call_type_des(const char *prefix)
{
	int len,index;
	
	if(prefix == NULL)
		return 0;
	
	for(index=0; index<=local_mobile_index; index++)
	{
		len = strlen(local_mobile_prefix_des[index]);
		if(len<=0) continue;
		if(strncasecmp(prefix, local_mobile_prefix_des[index],len) == 0)
			return 0; //local mobile
	}

	for(index=0; index<=local_pstn_index; index++)
	{
		len = strlen(local_pstn_prefix_des[index]);
		if(len<=0) continue;
		if(strncasecmp(prefix, local_pstn_prefix_des[index],len) == 0)
			return 1;//local pstn
	}

	return (index=4);//idd
}

int ocs_get_call_type_by_des(const char *prefix)
{
	int index = 0;
	//int match_method = ocs_get_crm_prefix_call_type_method();
	//char configured_des[128];

	index = ocs_get_crm_prefix_call_type_des(prefix);
	/*
	for(index=0; index<MAX_CALL_TYPE_DES; index++)
	{
			ocs_get_crm_prefix_call_type_des(index, configured_des);
			if(match_method == 0)
			{
				if(strcmp(des, configured_des) == 0)
					break;
			}
			else
			{
				if(strstr(des, configured_des) != NULL)
					break;
			}
	}
	*/
	return index;
}

int ocs_get_crm_free_seconds()
{
	return ppsParam.serviceControl.crm_free_seconds;
}

int pps_crm_get_notify_days()
{
	return ppsParam.serviceControl.crm_days_before_validity;
}

int pps_crm_get_status_vd()
{
	return ppsParam.serviceControl.crm_status_validity;
}

int pps_crm_get_sms_notification_for_event_flag()
{
	return ppsParam.serviceControl.crm_notification_for_event;
}

int ocs_get_cellid_present_flag()
{
	return ppsParam.serviceControl.cellid_present_in_ccr;
}

