/* 
** Written completely by Liang Hongbin

** Header file include by state machine
**
*/

#ifndef SMMP__H
#define SMMP__H
#include    <sys/types.h>
#include 	"ppsMibTable.h"
#include    "ramdata.h"
#include    "tariff.h"
#include	"cdrme.h"
#include    "../../../../plat/diameter/include/gy_message.h"
 
//#define OCS_USE_REDIS_DB 1
//#ifdef OCS_USE_REDIS_DB
//#include	"pps_redis.h"
//#endif
 
#define SMMP_IT_INTERVAL_SEC         0
#define SMMP_IT_INTERVAL_USEC        10000
#define SMMP_IT_VALUE_SEC            0
#define SMMP_IT_VALUE_USEC           10000
#define TIMER_ACTION_STATE           1
#define CYCLE_STATE_INTERVAL         (1000000/(SMMP_IT_INTERVAL_USEC*TIMER_ACTION_STATE))

#define WAITINGFOR_ANSWERED_DELAY    (2*60*CYCLE_STATE_INTERVAL)
#define SEND_ACTIVITYTEST_PERIODIC   (17*CYCLE_STATE_INTERVAL)
#define ACTIVITY_TEST_DELAY          (4*SEND_ACTIVITYTEST_PERIODIC)
#define ONE_MINUTE_STATE_TIME        (1*60*CYCLE_STATE_INTERVAL)

#define PERIODIC_ACTIONS			(CYCLE_STATE_INTERVAL)

#define	NORMAL_DELAY_TIMER			 (10*CYCLE_STATE_INTERVAL)
#define SSHORT_DELAY_TIMER			 (5*CYCLE_STATE_INTERVAL)
#define PROMPT_AND_COLLECT_TIMER	 (5*ONE_MINUTE_STATE_TIME)
#define SPECIAL_RESOURCE_REPORT_TIMER (5*ONE_MINUTE_STATE_TIME)

#define USSD_DELAY_TIMER			(3*ONE_MINUTE_STATE_TIME)

#define PPS_FSM_TIMEROUT			(1200*CYCLE_STATE_INTERVAL)
#define PC_LOOP_NUM					 16
// IP address and port defination

#define PPS_REALSYNC_PORT            (4971)
#define PPS_REALSYNC_PORT_ID		 (PPS_REALSYNC_PORT-4950)
#define HEART_BEATING_PORT           (4957)
#define PPS_SERVICE_PORT             (4955)

#define MAX_READ_PACKET_NUM       16 //8
#define MAX_RAM_INIT_RETRY           5
#define CONTEXT_BLOCK_SIZE           (256)

#define MAX_RO_FSM_PROCESS        2048		//2K Ro
#define MAX_GY_FSM_PROCESS        16384 // 16K Gy //8192		//8K Gy
#define MAX_FSM_PROCESS              (MAX_RO_FSM_PROCESS + MAX_GY_FSM_PROCESS)	//(1024+128), (2K Ro + 16K Gy)

#define TYPE_MO_CALL		         0x10 //added by lhb , 07/17
#define TYPE_MO_SM		             0x04

#define IVR_END_FLAG                 0xCC
#define IVR_CODE_LEN                 4

#define ACCOUNT_SERIES_LEN           2

#define SSN_SCF						 0x93

#define SERVICE_INPUT_NUM			 3

#define MAX_PREPAID_CALL_DURATION    3600

#define MAX_CALL_DURATION			 961 //seconds
#define PAN_KEY     "21436587123456"   // pan key to encrypt recharge card hrn

typedef enum _pps_service{
	SRV_IDLE = 0,
	SRV_UNKNOWN,
	SRV_NORMAL_MO_CALL,
	SRV_NORMAL_MT_CALL,
	SRV_NORMAL_MF_CALL,
	SRV_MO_SMS,
	SRV_CALL_HOTLINE,
	SRV_INQUIRY,
	SRV_SCRATCH_CARD_RECHARGE,
	SRV_CREDIT_CARD_RECHARGE,
	SRV_THIRD_PARTY_RECHARGE,
	SRV_RECHARGE_ONE_INPUT,
	SRV_RECHARGE_2TYPES,
	SRV_CRD,
	SRV_OTHER_VMS_MAITENANCE,
	SRV_VMS_DEPOSIT,
	SRV_DIRECT_ACCESS,
	SRV_OPR_MANAGE,
	SRV_CALLINGCARD_MANAGE,/*calling card		*/
	SRV_FEE_TRANSFER,
	SRV_POOL_ACCOUNT,
	SRV_FREE_SMS,
	SRV_CALL_ME_BACK,
	SRV_TELL_ME_NUMBER,
	SRV_FAVORITE_NUMBER,
	SRV_RESERVED_FUN,
	SRV_BUNDLE_PLAN,
	SRV_BUNDLE_QUERY,	
}_pps_service;

typedef enum _pps_retval{
	KEEP_STATE = 0,
}_pps_retval;

typedef enum _call_retval{
	RELEASE_CALL = 1,
}_call_retval;

typedef enum _sms_retval{
	RELEASE_SMS = 1,
}_sms_retval;

typedef enum _srf_retval{
	RELEASE_SRF = 1,
}_srf_retval;

typedef enum _call_state {
	S_IDLE,
	S_WAITINGFOR_ANSWERED,
	S_WAITINGFOR_RELEASED,
	S_ESTABLISH_IVR,
	S_WAITING_INITIALDP,
	S_SEND_MNP_ATI,
	S_WAIT_MNP_ATI_RES,
	S_CHECK_CALL_VALIDITY,
	S_SEND_CONTINUE,
	S_APPLY_CHARGING,
	S_APPLY_CHARGING_REAL,
	S_REQUEST_EVENTS,
	S_WAITINGFOR_EVENTS,
	S_SEND_RELEASE_CALL,
	S_RELEASE_ALL,
	S_SEND_SMS_MNP_ATI,
	S_SMS_WAIT_MNP_ATI_RES,
	S_SMS_CHECK_VALIDITY,
	S_REQUEST_SMS_EVENTS,
	S_RELEASE_SMS_CONTINUE,
	S_WAITINGFOR_SMS_EVENTS,        
	S_RECHARGE_OVER,
//	S_CONNECT_TO_RESOURCE,
	S_ESTABLISH_TEMPORARY_CONNECTION,
//	S_WAITING_ASSIST_REQUEST,
	S_SEND_PLAY_ANNOUNCEMENT,
	S_WAITING_SPECIAL_RESOURCE_REPORT,
	S_WAITING_RR_PCUI, 
	S_DISCONNECT_FORWARD_CONNECTION,
                //RR = Return result,
                //PCUI = PromptAndCollecrUserInformation 
	S_INQUIRY_MANAGE,
	S_RECHARGE_MANAGE,
	S_CREDIT_CARD_RECHARGE,
	S_FAVORITE_NUMBER_MANAGE,
	S_LANGUAGE_MANAGEMENT,
	S_PIN_MANAGEMENT,
	S_RELEASE_SRF,
	S_CRD_SERVICE,
	S_MAP_STATE,
	S_GPRS_STATE,
	S_VAS_STATE,
	
	S_OPR_MANAGE,	
	S_CALLING_CARD,/*calling card		*/
	S_CHARGE_OR_TRANSFER,
	S_FEE_TRANSFER,
	S_POOL_ACCOUNT,

	S_SMMP_RECHAGE_ACCOUNT,

	S_RO_EVENT_COMMON_INIT = 110,
	S_RO_EVENT_COMMON_PROCESS,
	S_RO_EVENT_SMS_INIT,
	S_RO_EVENT_SMS_PROCESS,

	S_RO_VOC_INIT=120,
	S_RO_VOC_CONNECTED,
	S_RO_VOC_RELEASE,
	S_GY_PS_INIT,
	S_GY_PS_CONNECTED,
	S_GY_PS_WAIT_TERM,
	S_GY_PS_RELEASE,
	S_GY_PS_POSTPAID,
	S_RO_VOC_GET_TARIFF_FROM_CRM,//S_GY_PS_GET_TARIFF_FROM_CRM,
	S_GY_PS_GET_TARIFF_FROM_CRM_RES,
	
	S_RO_ADAPTOR_CALL_INIT = 130,
	S_RO_CALL_WAIT_FIRST_CCA,
	S_RO_CALL_TERMIATED,
	S_RO_ADAPTOR_SMS_INIT,
	S_RO_SMS_WAIT_RESULT,
	S_RO_SMS_REFUND,

	S_ROUTING_HOME_PRN,
	S_SMPP_MAIN,

	S_CC_CHECK_CALL_VALIDITY,

	S_CRM_QUERY_TARIFF_TEST,	
	
} _call_state;

typedef enum _pps_map_state {
	S_MAP_SEND_OPENRSP,
	S_MAP_GET_PUSSDR,
	S_MAP_GET_USSDACK,
	S_MAP_RECHARGE_WAIT_PIN,
	S_MAP_SEND_PUSSR_ACK,
	S_MAP_CHECK_USSD_INPUT_CARD_VALIDITY,
	S_MAP_USSD_CHECK_CARD_DATA_RCM,
	S_MAP_USSD_CHECK_CARD_DATA_CRM,	
	S_MAP_USSD_INPUT_CARD_PWD_AGAIN,
	S_MAP_SEND_PUSSR_ACK_SEND_USSN,
	S_MAP_SEND_USSN_LOWBALANCE_PROMPT,
	S_MAP_WAIT_USSN_ACK,
	S_MAP_FEE_TRANSFER,
	S_MAP_CALL_ME_BACK,
	S_MAP_TELL_ME_NUMBER,
	S_MAP_FAVORITE_NUMBER,
	S_MAP_REST_QUERY,
	S_MAP_BUNDLE_PLAN,	
	S_MAP_BUNDLE_QUERY,	
	S_MAP_REST_TOPUP,
	S_MAP_THIRD_PARTY_HOTLINE,
	S_MAP_THIRD_PARTY_CHANNEL,
}_pps_map_state;

typedef enum _pps_smpp_state {
	S_SMPP_IDLE,
	S_SMPP_REST_QUERY,
	S_SMPP_REST_TOPUP,
	S_SMPP_REST_TRANSFER,	
}_pps_smpp_state;

/*
typedef enum ivr_state
{
	
}_ivr_state;

typedef enum ivr_mng_retval
{
	
}_ivr_mng_retval;
*/

typedef enum friend_family_call_type
{
	NONE_FRIEND_FAMILY_CALL = 0,
	FRIEND_FAMILY_CALL,
	FRIEND_FAMILY_FREE_CALL,
	FRIEND_FAMILY_FREE_SMS,
}_friend_family_call_type;

typedef enum inquiry_state
{
	S_INQ_SEND_PROMPT = 1,
	S_INQ_SRR0,
	S_INQ_SRR,
	S_INQ_PLAY_CREDIT_VAULT,
	S_INQ_WAIT_CREDIT_VAULT,
	S_INQ_PLAY_OPERATION_DONE,
	S_INQ_SRR_OPERATION_DONE,
	S_INQ_TO_RECHARGE,
	S_INQ_TO_RECHARGE_CONFIRM,
	S_INQ_REST_GET,
}_inquiry_state;

typedef enum inq_mng_retval
{
	inq_mng_keep_state = 0,
	inq_mng_to_recharge,
	inq_mng_exit,
}_inq_mng_retval;

typedef enum recharge_state
{
	S_REC_CHECK_ACCOUNT_STATUS = 1,
	S_REC_INPUT_ACCOUNT ,
	S_REC_WAIT_ACCOUNT,
	S_REC_INPUT_PIN,
	S_REC_WAIT_PIN,
	S_REC_ACCOUNT_READ,
	S_REC_ACCOUNT_CONFRIM,
	S_REC_PLAY_BALANCE,
	S_REC_WAIT_SRR0,
	S_REC_INQ_CHECK_MENU,
	S_REC_INPUT_RECHARGE_TYPE,
	S_REC_WAIT_TYPE,
	S_REC_INPUT_PWD,
	S_REC_CHECK_PWD,
	S_REC_WAIT_SRR,
	S_REC_CHECK_CARD_VALIDITY,
	S_REC_INPUT_PWD_AGAIN,
	S_REC_CHECK_CARD_WITH_RCM,
	S_REC_CHECK_CARD_WITH_REST,
	S_REC_CHECK_CARD_WITH_REST_GET,
	S_REC_CHECK_CARD_WITH_CRM,	
}_recharge_state;

typedef enum rec_mng_retval
{
	rec_mng_keep_state = 0,
	rec_mng_turn_to_ivr_scratch_card,
	rec_mng_turn_to_credit_recharge,
	rec_mng_exit,
}_rec_mng_retval;

typedef enum credit_recharge_state
{
	S_CCR_PLAY_INFO,
	S_CCR_SSR,
	S_CCR_CONNECT_TO_IVR_SCRATCH_CARD,
	S_CCR_CONNECT_TO_CREDIT_CENTER,
}_credit_recharge_state;

typedef enum ccr_mng_retval
{
	ccr_mng_keep_state = 0,
	ccr_mng_connect_to_credit_center,
	ccr_mng_exit,
}_ccr_mng_retval;

typedef enum fav_management{
	S_FAV_PLAY_MENU,
	S_FAV_SET_TYPE,
	S_FAV_INPUT_NEW_NUMBER,
	S_FAV_WAIT_NEW_NUMBER,
	S_FAV_SET_CONFIRM,
	S_FAV_WAIT_CONFIRM,
	S_FAV_INPUT_EDIT_NO,
	S_FAV_WAIT_EDIT_NO,
	S_FAV_INQUIRY,
	S_FAV_INQUIRY_READ,
}_fav_management;

typedef enum fav_mng_retval
{
	fav_mng_keep_state = 0,
	fav_mng_exit,
	fav_mng_exit_to_main_menu,
}_fav_mng_retval;

typedef enum language_management{
	S_LANGUAGE_MANAGE_MENU = 1,
	S_LANGUAGE_SET,
	S_LANGUAGE_SET_CONFIRM,
}_language_management;

typedef enum _language_mng_retval
{
	language_mng_keep_state = 0,
	language_mng_exit ,
	language_mng_exit_to_main_menu,
}_languange_mng_retval;

typedef enum pin_management{
	S_PIN_INPUT_OLD_PIN = 1,
	S_PIN_CHECK_OLD_PIN,
	S_PIN_MANAGE_MENU ,
	S_PIN_NEW_SET,
	S_PIN_SET_AGAIN,
	S_PIN_SET_OK,
	S_PIN_SET_OK_1,
}_pin_management;

#ifndef pin_mng_keep_state_value
#define pin_mng_keep_state_value
typedef enum pin_mng_retval
{
	pin_mng_keep_state = 0,
	pin_mng_exit,
	pin_mng_exit_to_main_menu,
}_pin_mng_retval;
#endif

typedef enum call_distribution{
	S_CRD_CHECK_CALLER_INFO = 1,
	S_CRD_INPUT_VOICEBOX,
	S_CRD_SET_DESTINATION,
	S_CRD_SET_DEPOSIT,
	S_CRD_CONNECT,
	S_CRD_SRR,
}_call_distribution;

typedef enum crd_retval{
	crd_keep_state = 0,
	crd_release_call,
	crd_wait_answer,
}_crd_retval;

enum _call_type {
    CT_LOCAL_CALL_OUT=PPS_CSTA_TYPE_BASE,
    CT_TRUNK_CALL_OUT,          // Local city call out
    CT_NDD_CALL_OUT,            // Nation l.d.tel
    CT_HMT_CALL_OUT,            // Hong Kong, Macao, TaiWan
    CT_IDD_CALL_OUT,            // International L.D.Tel
    CT_LOCAL_CALL_IN,
    CT_TRUNK_CALL_IN,
    CT_PERIODIC_CHARGE,
    CT_ASSISTED_UP,
    CT_ASSISTED_DOWN,
    CT_RECHARGE_SUCCESS,
    CT_RECHARGE_FAIL,
    CT_MO_SM,
    CT_MT_SM,
    CT_CONFERENCE_CALL,
    CT_THIRD_PARTY_CHARGE,
    CT_IDD_CALL_IN,
    CT_SERVICE,
    CT_FREE_SERVICE_CALL,
    CT_INVALID_CALL,
    CT_MO_CALL,
    CT_MT_CALL,
    CT_MF_CALL,
    CT_INVALID_MO_SMS,// 123
    CT_EMERGENCY_CALL,	

    CT_DIRECT_DEBITING = 138,
    CT_REFUND_ACCOUNT = 139,
    CT_TRANFER_OUT=140,
    CT_TRANFER_IN,
    CT_GPRS,    
    CT_VAS_SMS,
    CT_VAS_MMS_SEND,
    CT_VAS_MMS_RETRIEVE,
    CT_VAS_MMS_EMAIL,
    CT_BUNDLE_PLAN_CHARGE,
    CT_GPRS_REFUND,// 148
    CT_MO_CALLING_CARD_CALL, //149
    CT_RENT_CHARGE_TYPE0=150,
    CT_RENT_CHARGE_TYPE1=151,
    CT_RENT_CHARGE_TYPE2=152,
    CT_RENT_CHARGE_TYPE3=153,
    CT_RENT_CHARGE_TYPE4=154,
    CT_RENT_CHARGE_TYPE5=155,
    CT_RENT_CHARGE_TYPE6=156,
    CT_RENT_CHARGE_TYPE7=157,
    CT_FF_ACTIVATION,
    CT_FF_NUMBER_EDIT,   
    CT_USSD,
    CT_RO_DEBIT,

   CT_MO_CC_CALL,
   CT_MAX_CALL_TYPE,
};

#define	MAX_ASCII_NUMBER_LEN 24
typedef struct {
	u_char		nai;
	u_char		number[MAX_ASCII_NUMBER_LEN];
	u_char		IMSI[8];
	char		location;
	u_char		free_flag;
	unsigned int	valid_time;
	float			fee;
	float			charge;
	u_short		head;
	u_short		tail;
	u_char		status;
	u_short		cug_id;
	u_char		cos_id;
	u_char		idd_flag;
	u_char		expired_flag;
} _account_info;


typedef struct {
        u_char              state;
        u_char              transferMode;
		_account_info 		account;
        int   				fee;
		float				promotion;
        float  			 	validTime;
        u_char              smsTrigger;
        u_char  			inputSMS[MAX_INPUT_SMS+1];
}TRANSFER_FEE;

typedef struct {
        u_char              state;
        u_char  			destNumber[MAX_ASCII_NUMBER_LEN];
        u_char              smsTrigger;
        u_char  			inputSMS[MAX_INPUT_SMS+1];
}CMB;

typedef struct{
        u_char              state;
        u_char              choice;
		u_char				childNum;
}MASTER_SLAVE_ENQUIRY;

typedef struct{
        u_char                      state;
        u_char                      choice;
        u_char                      cosID;
        _account_info           	slaveInfo;
}MASTER_SLAVE_ADD;

typedef struct{
        u_char                      state;
        u_char                      choice;
        _account_info           	slaveInfo;
}MASTER_SLAVE_DEL;


typedef struct {
        u_char                              state;
        u_char                              choice;
        _prepaid_info   					*prepaidInfo;
        struct{
                MASTER_SLAVE_ENQUIRY    enquiry;
                MASTER_SLAVE_ADD        add;
                MASTER_SLAVE_DEL        del;
        }masterSlaveFunc;
}MASTER_SLAVE_CARD;

typedef struct RADIUS_REPORTED_INFO{
	RADIUS_VENDOR_3GPP2_PPAQ_SUB_ATTR_VQ	vq;
	RADIUS_VENDOR_3GPP2_PPAQ_SUB_ATTR_VT	vt;
	RADIUS_VENDOR_3GPP2_PPAQ_SUB_ATTR_DQ	dq;
	RADIUS_VENDOR_3GPP2_PPAQ_SUB_ATTR_DT	dt;
	RADIUS_ATTRIBUTE_NAS_IP_ADDRESS			nasIP;
	RADIUS_VENDOR_ATTRIBUTE_3GPP_SGSN_ADDR	sgsnIP;
}_RADIUS_REPORTED_INFO;


typedef struct _CHARGED_FEE{
	double balance;
	double promotion;
}CHARGED_FEE;

typedef struct gprs_process{
	u_char					state;
	u_char					postpaidState;
	u_char					prepaidState;
	
	u_short					qid;	
	float					       discount;
	u_char					radiusCommand;
	RADIUS_TLV_DECIMAL		vqAllocated;// Allocated Quota during the whole session
	RADIUS_TLV_DECIMAL		vthAllocated;	
	RADIUS_TLV_DECIMAL		vqConsumed;// Pre-charged Quota between each CDR
	ulong					vqAllocatedThisTime;
	ulong					dqAllocated;
	ulong					dqConsumed;// Comsumed Quota during the whole session
	ulong					dqRefund;
	ulong					dthAllocated;
	ulong					inactiveTimer;
	ulong					accessRequestTimer;
	DWORD					gsnIP;
	DWORD					sgsnIP;
	DWORD					destIP;
	u_char					location;
	CHARGED_FEE				chargedFee;
		
	WORD					radiusPid;
	u_char					userType;/*	1:prepaid user;0:postpaid user	*/
	ulong					volumeMeterUnit;
	_gprs_tariff				*gprsTariff;
	_RADIUS_REPORTED_INFO	radiusReportedInfo;
	RADIUS_TLV_TS			calling_station_id;
	RADIUS_TLV_TS			acct_session_id;
	u_char					calculateCount;
	u_char					calledType;
	int						update_quote_counter;
	RADIUS_ATTRIBUTE_CALLED_STATION_ID		*calledList;
}_gprs_process;

typedef struct _ussd_chnl_info{
	char scUrl[256];
	char prompt[256];
    char input[24];
    char sessId[64];
    u_char sess_state;
}ussd_chnl_info;

typedef struct {
	u_short 		portid;// handle port
    u_char			state;           /* state of the process */
	u_char			sub_state;
	u_char			sub_state_2;
	u_char			serviceType;
	u_char			serviceKey;
	u_short			correlationID;
    unsigned int	funnel;          /* valid time to talk */
    u_short			down;       
	u_short			Tassist;
    unsigned int	count;           /* count down value of process */
    unsigned int	duration;
    unsigned int	charged_duration;	
	unsigned int	fsm_timer;
	unsigned int	wait_timer;
	unsigned int	maxCallDuration;
    u_char			call_type;
	u_char			answerWanted;
    u_short			tcos;        /* terminate cause */
	u_char			oc;			 /* operation code */
	u_char			u_error;
	u_char			tcend;		/* tcend sent when release port */
	u_char			tariff_flag;
	u_char			roamingFlag; /* 0=home, 1=roaming */
	u_char			logMsisdn;
	u_char			dir;         /* 0=mo, 1= mt*/
	u_char			favoriteCall;
	u_char			cugCall;
	u_char			poolCall;
	u_short			voiceID;
	u_char			srfRelease;
	u_char			playAnnouncement;
	u_char			promptAndCollectUserInfo;
	u_char			maxPCUInfoLen;
	u_char			endOfReplyDigit[3];
	u_char			serviceInput;
	u_char			chargeFrom; /* 0=VPS,1=SRF,2=SM */
	u_char			chargeOther;
	u_char			rechargeByOther;
	char				choice_sn; 
	u_char			connFlag;  /* 0=no connectID, 1=connectID need, 2=correlation connected*/
	u_char			disconnectForwardConnection; 
	u_char			pre_warning;
	u_char			editFavNumber;
	u_char			callConnected;
	u_char			tmpNumber[12];
	char				tmpDigits[MAX_ASCII_NUMBER_LEN];
	u_short			chargedTime; /* initial tariff or not */
	u_int			realCallDuration;
	u_char			mnp_status;
	char				routing_number[MAX_ASCII_NUMBER_LEN];
	u_char			palim_auto_register;
	u_char			all_in_one_input_flag;
	u_char			cap_flag;
	u_char			bundle_flag;
	u_char			cap_plan;
	u_char			cap_seg;
	u_char			cap_notify_flag;
	u_char			currentTier;
	 _bill_tariff		tariff;
	_bill_tariff		*tariffMtReadOnly;/*	readonly,don't modify the content	*/
	_bill_tariff		*tariffMoReadOnly;/*	readonly,don't modify the content	*/
	_bill_sms			smsTariff;
	u_char			callingCardFunction;
	u_char			callingCardState;
	u_char			routing_home_flag;	
	u_char			ussd_transfer_one_input_flag;
	u_char			ccc_flag;
	union
	{
    	TRANSFER_FEE            feeTransfer;
    	CMB                     callMeBack;
    	MASTER_SLAVE_CARD       masterSlaveCard;
		_gprs_process			gprs_process;
        ussd_chnl_info chnl_info;
		char areaName[32];
    }ppsService;
	u_char		update_card_ack;
	u_char		update_card_result;
	u_char		send_warning_sms_flag;
	int			fd_port;
	struct 
	{
		u_char	gy_ro_enable;// Ro or Gy session
		u_char	adaptor_enabled;
		u_short   fd_port;
		u_char    request_type;
		u_char    request_action;
		u_char	recv_cmd;
		u_short	result;
		char		apn[64];
		long		granted_value;
		unsigned int	last_updated_duration;		
		u_char	is_final;
		u_char    no_more_ccr;
		//int           service_identifier;
		u_char	post_charge;
		u_char	last_ro_quota_type;// 0: bundle; 1: cap; for Ro refund
		unsigned int last_bundle_duration;// for Ro refund
		unsigned int last_cap_duration;// for Ro refund
		_gy_msg		gy_ccr_info;
	}ro_if;
	struct 
	{
		u_short	src_ref;
		u_short	dst_ref;
		u_short   timer;
		u_char	rest_src; /* 0/1/2=USSD, SMS, VOICE */
		u_char    rest_oc;	/* 0/1 = query, recharge */
		u_char	recv_cmd;		
		u_char    result;
		u_char    error_code;
		long		balance;
		long		mo_expiry_date;
		long		mt_expiry_date;
		u_char	query_tariff_status;
		int		unit;
		float 	unit_charge;
		int		tariff_discount;
		int		plan_id;
		int		bundle_plan_id;
		unsigned long long	total_plan_value;
		unsigned long	long	used_plan_value;
		int		sess_update_times;
		int		busicode;
		unsigned long	long	plan_value_add_this_time;		
	}rest_if;
	
} _process_info;




typedef struct {
    u_char			new_flag;
	u_char			callType;
    u_short			invoke_id;
    u_char			mtype;// operation type of session: Ro and Gy are CAP_O_OPEN
	u_short			dialogue_id;
	u_short			map_did; /* dialogue id for ATI */
	u_short			map_iid; /* invoke id for ATI */
	u_char			acn;
	u_char			acn_ver;
	u_char			map_acn;
	u_char			map_acn_ver;
    u_char			context[CONTEXT_BLOCK_SIZE];
} _context_info;

typedef struct _port_info{
     u_char			status;  /* 0=IDLE, 1=used */
     u_char			state;
}_port_info;


typedef struct _cdr_info
{
	u_char	msisdn[12];
	u_char	caller[12];
	u_char	called[12];
	u_char	cgi[7];
	char		locationNumber[24];
	char		msc_addr[24];
	char		smsc_addr[24];
	unsigned int	seizuretime; /* Time that system seizure the request */
    unsigned int	answertime;  /* Time that system answer the request */
    unsigned int	releasetime; /* Time that system release the call */  
}_cdr_info;

typedef struct 
{
     int		 cardhead;
     int		 cardtail; 
     float		 balance;
     float		 cc_call_charge;
}_card_info;

typedef struct {
    _process_info       process_info;
    _account_info       caller_info;
    _account_info       called_info;
    _account_info		charge_info;
    _account_info		recharge_info;
    _account_info		origin_info;
    _context_info          context_info;
    _cdr_info		      cdr_info;
    _card_info              card_info;
} _state_data;


/*
 * Primary PPS will send periodic Ram sync message,
 * and process periodic charging. 
 * Server will be set non-primary server as initiating,
 * server upgrade to primary server 
 * if it can't receive RAM sync message from another PPS in 5 seconds.
 */ 
#define PROC_FUNCTION_NUM     16

enum _PPS_CAUSE{
		NORMAL_RELEASE,
		/*
		NO_SUCH_SUBSCRIBER,
		ERROR_SUBSCRIBER_STATE,
		SUBSCRIBER_EXPIRED,
		LOW_BALALNCE,
		NO_TARIFF,
		TIMER_OUT,
		MISSING_HANDSHAKE,
		*/
		CALLER_STATUS_ERROR=1,
		CALLER_CREDIT_OUT,
		CALLER_EXPIRIED,
		CALLED_STATUS_ERROR,
		CALLED_CREDIT_OUT,
		CALLED_EXPIRIED,
		ACTIVITY_MISSED,
		TARIFF_NOT_SET,
		BOTH_USER_NOT_PREPAID,
		TIMER_OUT,
		CALLER_REACH_SMS_LIMIT,
		CALLER_REACH_CALL_LIMIT,
		CALLER_REACH_CHARGE_LIMIT,		
		SRVICE_NOT_SUBSCRIBERED,
};

enum _PPS_SELF_ERROR{
    _PPS_ERR_SERVICE_NOT_APPLIED=129,
    _PPS_ERR_MAX_ERROR,
};

enum _function_state{
	STATE_UPDATE_TARIFF,
	STATE_PERIODIC_TARIFF,
};//function_state;

typedef struct {
    u_long               state_i;
    u_long               count;
	u_long               timestamp;
} _proc_data;

typedef struct {
	u_long           total_recv_initdp;
	u_long           total_send_continue;
	u_long           total_send_release;
	u_long           total_recv_answer;
	u_long           total_recv_clear;
	u_long           total_recv_handshake;
	u_long           total_send_handshake;
	u_long           total_send_cdr;
	u_long           total_recv_other;
	u_long           total_send_other;
} _statistics;

#define PPS_FSM_RAMSIZE  (80*1024*1024)
#define	MAX_LOG_FUNC	32

typedef struct func_log{
	u_char	funcID;
	char	func_name[MAX_LOG_FUNC][64];
}_func_log;

typedef struct {
	_func_log			func_log;
	_statistics		statistics;
    _proc_data			proc_data[PROC_FUNCTION_NUM];
    _port_info			port_info[MAX_FSM_PROCESS];
    _state_data			state_data[MAX_FSM_PROCESS];
} _state_machine;

typedef struct _date_struct {
    int       year;
    int       month;
    int       day;
    int       hour;
    int       minute;
    int       second;
} _date_struct;

#define	MAX_LOADFILE_NUM	         16
#define MAX_READFILE_NUM	         10

#define PPS_USER_DATA_DB_OID	 (0x02)
#define PPS_PREPAID_CARD_DB_OID	 (0x03)

#define MAX_DEBUG_MESSAGES           8

#define  USER_FILE_NAME  \
"/usr/local/subsManage/ppsSubsCreate.csv"
#define  COMM_TARIFF_FILE_NAME \
"/tmp/pre_comm_tariff.csv"
#define  RENT_FILE_NAME \
"/tmp/pre_rent.csv"
#define  DISCOUNT_FILE_NAME \
"/tmp/pre_bill_discount.csv"
#define  HOLIDAY_FILE_NAME \
"/tmp/pre_bill_holiday.csv"
#define  TARIFF_FILE_NAME \
"/tmp/pre_mo_tariff.csv"
#define	 MT_TARIFF_FILE_NAME	\
"/tmp/pre_mt_tariff.csv"
#define  BILL_SMS_FILE_NAME \
"/tmp/pre_bill_sms.csv"
#define  BILL_GPRS_FILE_NAME \
"/tmp/pre_gprs_tariff.csv"
#define  BILL_VAS_MMS_SEND_FILE_NAME \
"/tmp/pre_vas_mms_send_tariff.csv"
#define  BILL_VAS_MMS_RETRIEVE_FILE_NAME \
"/tmp/pre_vas_mms_retrieve_tariff.csv"
#define  BILL_VAS_MMS_EMAIL_FILE_NAME \
"/tmp/pre_vas_mms_email_tariff.csv"


#define  USER_LOG_FILE \
"/usr/local/subsManage/ppsSubsCreate.log"
#define  PREPAID_CARD_FILE \
"/usr/local/subsManage/ppsCardCreate.csv"
#define  PREPAID_CARD_LOG_FILE \
"/usr/local/subsManage/ppsCardCreate.log"

typedef enum LOAD_FILE_ID
{
  _FILE_ID_USER_DATA,
  _FILE_ID_PREPAID_CARD,
  _FILE_ID_COMM_TARIFF,
  _FILE_ID_RENT_TARIFF,
  _FILE_ID_BILL_TARIFF,
  _FILE_ID_MT_TARIFF,
  _FILE_ID_DISCOUNT,
  _FILE_ID_HOLIDY,
  _FILE_ID_BILL_SMS,
  _FILE_ID_GPRS_TARIFF,
  _FILE_ID_VAS_MMS_SEND_TARIFF,
  _FILE_ID_VAS_MMS_RETRIEVE_TARIFF,
  _FILE_ID_VAS_MMS_EMAIL_TARIFF,
}_LOAD_FILE_ID;

typedef struct  _load_file
{
    u_char    flag[MAX_LOADFILE_NUM];
    FILE      *fp[MAX_LOADFILE_NUM];
    FILE      *log_fp[MAX_LOADFILE_NUM];  
/* index=0/1/2/3/4 file=USER/TPARAM/TARIFF/DSICOUNT/HOLIDAY
*/
}_load_file;

#define    PARAM_CSV \
"/usr/local/subsManage/rent.csv"
#define    BASICTARIFF_CSV \
"/usr/local/subsManage/mo_tariff.csv"
#define    BASICMTTARIFF_CSV \
"/usr/local/subsManage/mt_tariff.csv"
#define    DISCOUNT_CSV \
"/usr/local/subsManage/bill_discount.csv"
#define    HOLIDAY_CSV \
"/usr/local/subsManage/bill_holiday.csv"
#define    COMM_TARIFF \
"/usr/local/subsManage/comm_tariff.csv"
#define	   BILL_SMS_CSV \
"/usr/local/subsManage/bill_sms.csv"
#define	   GPRS_TARIFF_CSV \
"/usr/local/subsManage/gprs_tariff.csv"

#define  VAS_MMS_SEND_CSV \
"/usr/local/subsManage/vas_mms_send_tariff.csv"
#define  VAS_MMS_RETRIEVE_CSV \
"/usr/local/subsManage/vas_mms_retrieve_tariff.csv"
#define  VAS_MMS_EMAIL_CSV \
"/usr/local/subsManage/vas_mms_email_tariff.csv"


typedef enum OMC_OPERATION_CODE{
	OMC_OCODE_NULL,
	OMC_OCODE_READ,
	OMC_OCODE_EDIT,
	OMC_OCODE_ADD,
	OMC_OCODE_DEL,
	MAX_OMC_OCODE	
}_OMC_OPERATION_CODE;

#define    PPS_BACKUP_NAME \
"/usr/local/backup/ppsMemBak.dat"

#define    PPS_BACKUP_TEMP \
"/usr/local/backup/ppsMemBak.tmp"

#define		PPS_USER_BACKUP_NAME	\
"/usr/local/backup/ppsSubsBak.dat"
#define		PPS_USER_BACKUP_TEMP	\
"/usr/local/backup/ppsSubsBak.tmp"

#define		PPS_RECHARGE_CARD_BACKUP_NAME	\
"/usr/local/backup/ppsRechargeCardBak.dat"
#define		PPS_RECAHRGE_CARD_BACKUP_TEMP	\
"/usr/local/backup/ppsRechargeCardBak.tmp"

#define		PPS_TARIFF_DATA_NAME	\
"/usr/local/backup/ppsTariffBak.dat"
#define		PPS_TARIFF_DATA_TEMP	\
"/usr/local/backup/ppsTariffBak.tmp"

typedef enum DATA_SOURCE
{
   _SRC_NO_SRC,
   _SRC_PEER_SRV,
   _SRC_LOCAL_HD,
   _SRC_OMC_HD,
}_DATA_SOURCE;

typedef enum SYS_STATE
{
   _SYS_STATE_NORMAL,
   _SYS_STATE_START,
   _SYS_STATE_INIT,
   _SYS_STATE_SUSPEND,
   _SYS_STATE_SHUTDOWN,
}_SYS_STATE;

typedef enum INIT_STATE
{
  _INIT_CHECK_VERSION,
  _INIT_CHECK_SYNC,
  _INIT_READ_FROM_PEER,
  _INIT_READ_FROM_LOCAL_HD,
  _INIT_READ_FROM_OMC_HD,
  _INIT_OVER,
  _INIT_UNNORMAL,
}_INIT_STATE;

typedef enum RAM_OPER
{
  _RAM_OPER_READ=1,
  _RAM_OPER_SEND,
  _RAM_PERIODIC_SYNC,
  _RAM_REAL_SYNC,
}_RAM_OPER;

enum USSD_SMS{ 
	USSD_TRANSFER,
	SMS_TRANSFER,
};

enum SERVICE_RETAL{
	SERVICE_KEEP_STATE,
	SERVICE_RETURN_TO_PARENT,
	SERVICE_EXIT_PPS,	
};

enum WAIT_INPUT_RESULT{
	NO_INPUT,
	INPUT_SUCCESS,
	INPUT_ERROR_REINPUT,
	INPUT_ERROR_EXIT,
};

enum confirm_command{
	CONFIRM_CANCEL,
	CONFIRM_OK,
	CONFIRM_ENTER_AGAIN,
	CONFIRM_LISTEN_RE,
};

enum ChildOrParent{
	MASTER_SLAVE_NONE,
	MASTER_SLAVE_PARENT,
	MASTER_SLAVE_CHILD,
};

enum _TIER_RATING_NUMBER{
	BILL_TIERS_ONE = 0,
	BILL_TIERS_TWO,
	BILL_TIERS_TRREE,
};

enum CHECK_TRANSFERED_FEE_RESULT{
	CHECK_TRANSFERED_FEE_OK,
	CHECK_TRANSFERED_FEE_INSUFFIENT,
	CHECK_TRANSFERED_FEE_EXPIERED,
};

enum NOTIFY_CALLER_OR_CALLED{
	NOTIFY_CALLER,
	NOTIFY_CALLED,
};


enum CAP_TYPE{
	CAP_CALL,
	CAP_SMS,
	CAP_GPRS,
};
#define MAX_IVR_INPUT_LEN		32

int BillingSMSProc(_state_data *shmp, int sms_num);
int  load_tariff_main();
int  IdleProc(u_short);
int  EstablishProc(int invokeid);
int realSyncTcpSend(DWORD db_type, DWORD instance, DWORD ocode, BYTE *payload, WORD len);
int OCS_with_external_CRM();
void pps_send_rent_charge_to_crm(int head, int tail);
extern int pps_send_query_tariff_req(u_short portid);
extern int BillingProc_crm(_state_data *shmp, int sub_flag0);
extern int pps_send_update_plan_value_req(u_short portid);
int pps_send_selfcare_topup_req(u_short portid);
int RechargeSucceedwithCRM(u_short portid);
unsigned int ocs_get_ue_index(long long msisdn);
long long ocs_get_ue_by_index(int store_index);
void pps_send_update_subs_status_to_crm(_prepaid_info *user_ptr, int update_flag);


#endif  /* SMMP__H */ 
