/*	TCAP structure head file			*/
/*	written by Liu Zhiguo 2002-08-06	*/
/*	Version 2.0							*/
/*	-------------------------------		*/

#ifndef	_TCAP_STRUCT
#define _TCAP_STRUCT

#define	TCAP_KEY		0xa0100000
#define TCAP_PERM		0664

typedef struct	tcapdebug_struct	// tcap debug structure
{
	u8		default_invtime;	// default invoke time, init is 10s
	u8		default_rejtime;	// default reject time, init is 1s
	u8		error_switch;		// show error record switch
	u8		sccp_switch;		// show receive data from sccp switch
	u8		tcu_switch;			// show send data to TC-User switch
	u8		send_control;		// control if can send out tcap message
	u8		start_time[6];
	u8		current_time[6];
	u32		monitor_did;
	u32		local_ip;
	u32		peer_ip;
	u8		watch_dog[256];
	u8		ascin_buf[TCAP_ASCIN_LEN];
	u8		ascout_buf[TCAP_ASCOUT_LEN];
	u8		current_csta;
	u32		csta_time[TCAP_UTILIZE_COUNT];
	u32		tc_utilize[TCAP_UTILIZE_COUNT][TCAP_UTILIZE_LEN];
	u8		hb_status[TCAP_HB_LEN];
}	tcapdebug_struct;

/*	+++++++++++++++++++++++++++++++	*/
/*	Transaction sub-layer structure	*/
/*	+++++++++++++++++++++++++++++++	*/
typedef struct TRUni_struct	// transaction unidirectional struct
{
	u8		dialogue_len;
	u8		components_len;
	u8		dialogue[TCAP_DLG_LEN];
	u8		components[TCAP_CMP_LEN];
}	TRUni_struct;

typedef struct TRBegin_struct	// transaction begin struct
{
	u8		tid_flag;
	u32		transaction_id;
	u8		dialogue_len;
	u8		components_len;
	u8		dialogue[TCAP_DLG_LEN];
	u8		components[TCAP_CMP_LEN];
}	TRBegin_struct;

typedef struct TRContinue_struct	// transaction continue struct
{
	u8		ltid_flag;
	u8		ptid_flag;
	u32		local_tid;
	u32		peer_tid;
	u8		dialogue_len;
	u8		components_len;
	u8		dialogue[TCAP_DLG_LEN];
	u8		components[TCAP_CMP_LEN];
}	TRContinue_struct;

typedef struct TREnd_struct	// transaction end struct
{
	u8		tid_flag;
	u32		transaction_id;
	u8		termination;
	u8		dialogue_len;
	u8		components_len;
	u8		dialogue[TCAP_DLG_LEN];
	u8		components[TCAP_CMP_LEN];
}	TREnd_struct;

typedef struct TRUAbort_struct	// transaction u-abort struct
{
	u8		tid_flag;
	u32		transaction_id;
	u8		dialogue_len;
	u8		dialogue[TCAP_DLG_LEN];
	u8		uabort_info_len;
	u8		uabort_info[TCAP_UABORT_INFO_LEN];
}	TRUAbort_struct;

typedef struct TRPAbort_struct	// transaction p-abort struct
{
	u8		tid_flag;
	u32		transaction_id;
	u8		abort_reason;
}	TRPAbort_struct;

typedef struct TRNotice_struct	// transcation notice struct
{
	u8		tid_flag;
	u32		transaction_id;
	u8		report_cause;
}	TRNotice_struct;

typedef struct TSL_struct
{
	u8		message_type;
	u8		message_flag;
	u8		peer_tid_len;  /* added by Pierre, 2006-08-19 */
	struct	SCCP_ADDRESS		local_add;
	struct	SCCP_ADDRESS		peer_add;
	union
	{
		struct	TRUni_struct		tr_uni;
		struct	TRBegin_struct		tr_begin;
		struct	TRContinue_struct	tr_continue;
		struct	TREnd_struct		tr_end;
		struct	TRUAbort_struct		tr_uabort;
		struct	TRPAbort_struct		tr_pabort;
		struct	TRNotice_struct		tr_notice;
	}	tsl_prim;
}	TSL_struct;

/*	+++++++++++++++++++++++++++++++	*/
/*	Component sub-layer structure	*/
/*	+++++++++++++++++++++++++++++++	*/

/*	define dialogue handing primitive structure	*/
typedef struct	AARQ_struct
{
	u8		version[2];
	u8		acn_len;
	u8		acn[TCAP_ACN_LEN];
	u8		user_info_len;
	u8		user_info[TCAP_USERINFO_LEN];
}	AARQ_struct;

typedef struct	AARE_struct
{
	u8		version[2];
	u8		acn_len;
	u8		acn[TCAP_ACN_LEN];
	u8		result;
	u8		diagnostic_flag;	// user or provider
	u8		diagnostic_type;	// reason
	u8		user_info_len;
	u8		user_info[TCAP_USERINFO_LEN];
}	AARE_struct;

typedef struct	ABRT_struct
{
	u8		abort_source;
	u8		user_info_len;
	u8		user_info[TCAP_USERINFO_LEN];
}	ABRT_struct;

typedef struct	ASUDT_struct
{
	u8		version;
	u8		acn_type;
	u8		acn_len;
	u8		acn[TCAP_ACN_LEN];
	u8		user_info_len;
	u8		user_info[TCAP_USERINFO_LEN];
	u8		security_context_type;
	u8		security_context_len;
	u8		security_context[TCAP_SECURITY_CONTEXT_LEN];
	u8		confidential_info_len;
	u8		confidential_info[TCAP_CONFIDENTIAL_INFO_LEN];
}	ASUDT_struct;

typedef struct	dlgPDU_struct
{
	u8		dialogue_type;
	union
	{
		struct	AARQ_struct		aarq;
		struct	AARE_struct		aare;
		struct	ABRT_struct		abrt;
		struct	ASUDT_struct	asudt;
	}	pdu_union;
}	dlgPDU_struct;

typedef struct	dlgport_struct	// dialogue portion in the TC
{
	u8		acn_type;
	u8		acn_len;
	u8		acn[TCAP_ACN_LEN];
	u8		user_info_len;
	u8		user_info[TCAP_USERINFO_LEN];
	u8		security_context_type;
	u8		security_context_len;
	u8		security_context[TCAP_SECURITY_CONTEXT_LEN];
	u8		confidential_info_len;
	u8		confidential_info[TCAP_CONFIDENTIAL_INFO_LEN];
}	dlgport_struct;

typedef struct	TCUni_struct	// component unidirectional struct
{
	struct	SCCP_ADDRESS	local_add;
	struct	SCCP_ADDRESS	peer_add;
	u8		dialogue_flag;
	struct	dlgport_struct	dlg_data;
	u8		component_present;
}	TCUni_struct;

typedef struct	TCBegin_struct
{
	struct	SCCP_ADDRESS	local_add;
	struct	SCCP_ADDRESS	peer_add;
	u8		dialogue_flag;
	struct	dlgport_struct	dlg_data;
	u8		component_present;
}	TCBegin_struct;

typedef struct	TCContinue_struct
{
	u8		dialogue_flag;
	struct	dlgport_struct	dlg_data;
	u8		component_present;
}	TCContinue_struct;

typedef struct	TCEnd_struct
{
	u8		dialogue_flag;
	struct	dlgport_struct	dlg_data;
	u8		component_present;
	u8		termination;
}	TCEnd_struct;

typedef struct	TCUAbort_struct
{
	u8		uabort_reason;
	u8		dialogue_flag;
	struct	dlgport_struct	dlg_data;
	u8		uabort_info_len;
	u8		uabort_info[TCAP_UABORT_INFO_LEN];
}	TCUAbort_struct;

typedef struct	TCPAbort_struct
{
	u8		pabort_reason;
}	TCPAbort_struct;

typedef struct	TCNotice_struct
{
	u8		report_reason;
}	TCNotice_struct;

typedef struct	CSLdlg_struct	// dialogue primitive in CSL
{
	u32		dialogue_id;
	u8		message_type;
	u8		message_flag;
	union
	{
		struct	TCUni_struct		tc_uni;
		struct	TCBegin_struct		tc_begin;
		struct	TCContinue_struct	tc_continue;
		struct	TCEnd_struct		tc_end;
		struct	TCUAbort_struct		tc_uabort;
		struct	TCPAbort_struct		tc_pabort;
		struct	TCNotice_struct		tc_notice;
	}	dlg_prim;
}	CSLdlg_struct;

/*	define component handing primitive structure	*/
typedef struct	TCInvoke_struct
{
	u8		linkedid_flag;
	u8		linked_id;
	u8		operation_domain;
	u8		operation_family;
	u8		operation_code;
	u8		parameter_len;
	u8		parameter[TCAP_CMP_LEN];
	u8		operation_class;
	u32		timer;
}	TCInvoke_struct;

typedef struct	TCResult_struct
{
	u8		operation_flag;	// the flag indicate if has operation code
	u8		operation_domain;
	u8		operation_family;
	u8		operation_code;
	u8		parameter_len;
	u8		parameter[TCAP_CMP_LEN];
}	TCResult_struct;

typedef struct	TCUError_struct
{
	u8		error_type;		// error tag
	u8		error_code;		// error code
	u8		parameter_len;
	u8		parameter[TCAP_CMP_LEN];
}	TCUError_struct;

typedef struct	TCReject_struct
{
	u8		problem_type;
	u8		problem_code;
	u8		parameter_len;
	u8		parameter[TCAP_CMP_LEN];
}	TCReject_struct;

typedef struct	CSLcmp_struct
{
	u32		dialogue_id;	// dialogue id
	u8		invoke_id;		// invoke id
	u8		message_type;
	u8		message_flag;
	u8		last_flag;
	union
	{
		struct	TCInvoke_struct		tc_invoke;
		struct	TCResult_struct		tc_result;
		struct	TCUError_struct		tc_uerror;
		struct	TCReject_struct		tc_reject;
	}	cmp_prim;
}	CSLcmp_struct;

/*	+++++++++++++++++++++++++++++++	*/
/*	TCAP virtue machine structure	*/
/*	+++++++++++++++++++++++++++++++	*/
typedef struct	ismvm_data
{
	u8		used_flag;
	u8		linked_iid;
	u8		ism_state;
	u8		ism_msgtype;
	u8		ism_msgflag;
	u8		ism_msgclass;
	u8		ism_oper_domain;
	u8		ism_oper_family;
	u8		ism_oper_code;
	u32		ism_timer;
}	ismvm_data;

typedef struct	tcapvm_struct
{
	struct	SCCP_ADDRESS		local_add;
	struct	SCCP_ADDRESS		peer_add;
	u8		peer_tid_len;
	u32		peer_tid;
	u32		tcap_timer;
	u8		tcap_state;
	u8		ac_mode;
	u8		proto_type;
	u8		tcaprecv_head;
	u8		tcaprecv_tail;
	struct	ismvm_data			ism_data[MAX_TEMPINVOKEID];
	struct	TSL_struct			tsl_data[MAX_TSL_BUF];
}	tcapvm_struct;

typedef struct	tcapint_struct		// tcap interface structure
{
	u8		dlgrecv_head;
	u8		dlgrecv_tail;
	u8		dlgsend_head;
	u8		dlgsend_tail;
	struct	CSLdlg_struct		dlg_recv[MAX_TSL_BUF];
	struct	CSLdlg_struct		dlg_send[MAX_TSL_BUF];
	u8		cmprecv_head;
	u8		cmprecv_tail;
	u8		cmpsend_head;
	u8		cmpsend_tail;
	struct	CSLcmp_struct		cmp_recv[MAX_CSL_BUF];
	struct	CSLcmp_struct		cmp_send[MAX_CSL_BUF];
}	tcapint_struct;

typedef struct	tcapbuf_struct	// TCAP buffer structure
{
	int     grantdid;
	struct	tcapvm_struct		tcapvm_data[MAX_DIALOGUEID];
	struct	tcapint_struct		tcapint_data[MAX_DIALOGUEID];
	struct	tcapdebug_struct	tcap_debug;
}	tcapbuf_struct;

struct	tcapbuf_struct			*tcap_ptr;

u8 tsl_message_decode(struct SCLC_MSG *sccp_ptr,struct TSL_struct *dst_ptr,u8 *err_code);// tsl decode
u8 tsl_message_encode(struct SCLC_MSG *sccp_ptr,struct TSL_struct *tsl_ptr,u8 flag);// tsl encode
u8 extract_dp(struct dlgPDU_struct *pdu_ptr,u8 *databuf,int datalen,u8 proto_type);	// extract dialogue portion
u8 build_dp(u8 *content,struct dlgPDU_struct *dlg_ptr,u8 un_flag,u8 proto_type);	// build dialogue portion
int extract_component(struct CSLcmp_struct *cmp_ptr,u8 *data_buf,u32 proc,int *section_len);	// extract component portion
int assemble_component(u8 *string,struct CSLcmp_struct *cha_ptr);	// assemble component portion
void tcapvm_init();		// init share memory
u8 write_ism(CSLcmp_struct *cmp_ptr);
void tco_program(void);	// transaction coordinator
void tsm_program(u32 proc);
void ism_program(u32 proc);	// invocation state machine

#endif
