#include "../include/includes.h"
#include "../include/crd.h"
#include "../include/param.h"


extern	PPSParamStruct	ppsParam;

int SetCRDParam(u_char oidLen, DWORD *oid, 
				u_char *pdata, u_short dataLen)
{
	int				oid0;
	VmsRouteParam 	*pcrd;

	pcrd = &ppsParam.routeParam;
	oid0 = oid[13];	
	switch(oid0)
	{
		case 1:
			memset(pcrd->plmnServiceNumber,0,16);
			memcpy(pcrd->plmnServiceNumber,pdata,dataLen);
		break;
		case 2:
			memset(pcrd->pstnServiceNumber,0,16);
			memcpy(pcrd->pstnServiceNumber,pdata,dataLen);
			
		break;
		case 3:
			memset(pcrd->leaveMessageNumber,0,16);
			memcpy(pcrd->leaveMessageNumber,pdata,dataLen);
		break;
		case 4:
			pcrd->routeMethodOption = pdata[0];	
		break;
		case 5:
			pcrd->routeNumberOption = pdata[0];	
		break;
		case 6:			
			pcrd->isPostpaidAllowed = pdata[0];
		break;
		case 7:			
			pcrd->ring_back_service_key = pdata[0];
		break;
	}	
	return dataLen;
}

int GetCRDParam(u_char oidLen, DWORD *oid, 
				u_char *pdata, BYTE *vartype)
{
	int oid3;
	int vallen = 1;
	VmsRouteParam 	*pcrd;

	pcrd = &ppsParam.routeParam;
	oid3 = oid[13];		
	*vartype = 0x04;
	switch(oid3)
	{
		case 1:
			vallen = strlen((char *)pcrd->plmnServiceNumber);
			memcpy(pdata,pcrd->plmnServiceNumber,vallen);
		break;
		case 2:
			vallen = strlen((char *)pcrd->pstnServiceNumber);
			memcpy(pdata,pcrd->pstnServiceNumber,vallen);
		break;
		case 3:
			vallen = strlen((char *)pcrd->leaveMessageNumber);
			memcpy(pdata,pcrd->leaveMessageNumber,vallen);
		break;
		case 4:
			*vartype = INTEGER;
			pdata[0] = pcrd->routeMethodOption;
		break;
		case 5:
			*vartype = INTEGER;
			pdata[0] = pcrd->routeNumberOption;
		break;
		case 6:
			*vartype = INTEGER;
			pdata[0] = pcrd->isPostpaidAllowed;
		break;
		case 7:
			*vartype = INTEGER;
			vallen = 1;
			pdata[0] = pcrd->ring_back_service_key;
		break;
	}
	
	return vallen;
}

int IsCRDServiceNumber(char *number)
{
	int retval=0;
	int	len;
	VmsRouteParam 	*pcrd;

	pcrd = &ppsParam.routeParam;

	if(pcrd->routeMethodOption == ByWholeNumber)
	{
		if(strcmp((char *)pcrd->plmnServiceNumber,number)==0)
			retval = 1;	
	}
	else
	{
		if(pcrd->plmnServiceNumber[0] == 0) return 0;
		len = strlen((char *)pcrd->plmnServiceNumber);
		if(!strncasecmp(number, (char *)pcrd->plmnServiceNumber,len))
			retval = 1;
	}
	return retval;
}

int IsPSTNMaitenance(char *number)
{
	int retval=0;
	int	len;
	VmsRouteParam 	*pcrd;

	pcrd = &ppsParam.routeParam;

	if(pcrd->routeMethodOption == ByWholeNumber)
	{
		if(strcmp((char *)pcrd->pstnServiceNumber,number)==0)
			retval = 1;	
	}
	else
	{
		if(pcrd->pstnServiceNumber[0] == 0) return 0;
		len = strlen((char *)pcrd->pstnServiceNumber);
		if(!strncasecmp(number, (char *)pcrd->pstnServiceNumber,len))
			retval = 1;
	}
	return retval;
}

int IsVMSDeposit(char *number)
{
	int retval=0;
	int	len;
	VmsRouteParam 	*pcrd;

	pcrd = &ppsParam.routeParam;

	if(pcrd->routeMethodOption == ByWholeNumber)
	{
		if(strcmp((char *)pcrd->leaveMessageNumber,number)==0)
			retval = 1;	
	}
	else
	{
		if(pcrd->leaveMessageNumber[0] == 0) return 0;
		len = strlen((char *)pcrd->leaveMessageNumber);
		if(!strncasecmp(number, (char *)pcrd->leaveMessageNumber,len))
			retval = 1;
	}
	return retval;
}

int GetCRDDestination(char *src_number, char *dst_number,u_char	type)
{
	int retval=0;
	int loop=0;
	VmsUserSegment 	*crd_ptr;

	for(loop=0;loop<MaxVmsUserSegmentItem;loop++)
	{
		crd_ptr = &ppsParam.ppsTables.vmsUserSegment[loop];
		if(crd_ptr->existFlag == FALSE)
			continue;
		if(strcmp(src_number, (char *)crd_ptr->startMsisdn)>=0)
		if(strcmp(src_number, (char *)crd_ptr->endMsisdn)<=0)
		{
			switch(type)
			{
				case 0: /* PLMN maintenance */
					strcpy(dst_number, (char *)crd_ptr->vmsPLMNserviceNumber);
				break;
				case 1: /* PSTN maintenance */
					strcpy(dst_number, (char *)crd_ptr->vmsPSTNserviceNumber);
				break;
				case 2: /* deposit message */
					strcpy(dst_number, (char *)crd_ptr->leaveMessageNumber);
				break;
			}
			retval = 1;
			break;
		}
	}
	return retval;
}

int IsPostpaidAllowed()
{
	VmsRouteParam 	*pcrd;

	pcrd = &ppsParam.routeParam;
	
	return pcrd->isPostpaidAllowed;
}

int	CheckCRDRingBackSericeKey(int serviceKey)
{
	VmsRouteParam 	*pcrd;

	pcrd = &ppsParam.routeParam;
	
	if(pcrd->ring_back_service_key<2)
		return 0;
	if(pcrd->ring_back_service_key == serviceKey)
		return 1;
	return 0;	
}

