/*	Title:		SCF msgfunction c file	*/
/*	Writer: 	Liang Hongbin		*/
/*	Version:	1.0			*/
/*	Date:  		2003-09-18		*/
/*	------------------------------		*/
#include	<string.h>
//#include	"scfdef.h"
#include	"./include/scfmsg.h"


SCF_Buffer	scf_buffer;
/*-----------------------------------------------------------*/
/*  Functions invoked by SCSM to put/get message from/to buffer for PPS */
int PutScfOpen(CAP_Message  cap_message)
{
	unsigned char 		msgWriteSub;
	PSCF_Open_Buffer	popen_ptr;
	
	popen_ptr = &scf_buffer.openBuffer;	
	msgWriteSub = popen_ptr->msgWriteSub%SCF_OPEN_LEN;
	memcpy(&popen_ptr->msgList[msgWriteSub],
		&cap_message,
		sizeof(CAP_Message));
	popen_ptr->msgWriteSub =
		(popen_ptr->msgWriteSub+1)%SCF_OPEN_LEN;
	if(popen_ptr->msgWriteSub == popen_ptr->msgReadSub){
		popen_ptr->msgReadSub = 
			(popen_ptr->msgReadSub+1)%SCF_OPEN_LEN;
			
	}
	return 1;
}

int GetScfOpen(PCAP_Message pcap_message)
{
	unsigned char 		msgReadSub, msgWriteSub;
	PSCF_Open_Buffer	popen_ptr;
	
	popen_ptr = &scf_buffer.openBuffer;
	msgReadSub = popen_ptr->msgReadSub%SCF_OPEN_LEN;
	msgWriteSub = popen_ptr->msgWriteSub;
	
	if(msgReadSub == msgWriteSub)
		return 0;
	memcpy(pcap_message,
		&popen_ptr->msgList[msgReadSub],
		sizeof(CAP_Message));
	popen_ptr->msgReadSub = 
		(popen_ptr->msgReadSub + 1)%SCF_OPEN_LEN;
	return 1;
}

int PutScfMsg(CAP_Message cap_message)
{
	int			writesub,readsub;
	int			did;
	PSCF_HPort_Buffer	pOutBuf;

	did = (cap_message.dialogue_id[0] *256) +
		 cap_message.dialogue_id[1];
	if(did >= SCF_MAX_DLG_NUM) 
		return 0;
	if (cap_message.msgOperaCode == CAP_O_OPEN 
		&& cap_message.msgOperaType == 0x02)//INDICATE
	{		       
          //
	}
	pOutBuf = &scf_buffer.portBuffer[did].outBuffer;
	writesub = pOutBuf->msgWriteSub%SCF_PORT_LEN;
	readsub = pOutBuf->msgReadSub;
	memcpy(&pOutBuf->msgList[writesub],
		&cap_message,
		sizeof(CAP_Message));
	pOutBuf->msgWriteSub = 
			(pOutBuf->msgWriteSub + 1) % SCF_PORT_LEN;
	if (pOutBuf->msgReadSub == pOutBuf->msgWriteSub)
		pOutBuf->msgReadSub = 
			(pOutBuf->msgReadSub + 1) % SCF_PORT_LEN;
	return 1;
}

int GetScfMsg(PCAP_Message pcap_message,unsigned short dlgid)
{
	int			readsub,writesub;
	PSCF_HPort_Buffer	pInBuf;
	
	if(dlgid>=SCF_MAX_DLG_NUM) return 0;
	pInBuf = &scf_buffer.portBuffer[dlgid].inBuffer;	
	readsub  = pInBuf->msgReadSub % SCF_PORT_LEN;
	writesub = pInBuf->msgWriteSub;

	if(readsub == writesub)
		return 0;
	memcpy(pcap_message,
		&pInBuf->msgList[readsub],
		sizeof(CAP_Message));
    	pInBuf->msgReadSub = 
    		(pInBuf->msgReadSub + 1) % SCF_PORT_LEN;
	return 1;
}
/*  Functions invoked by SCSM to put/get message from/to buffer for PPS */
int ReadScfMsg(PCAP_Message pcap_message, unsigned short dlgid)
{
	PSCF_HPort_Buffer	pOutBuf;
	
	if(dlgid>=SCF_MAX_DLG_NUM) return 0;
	pOutBuf = &scf_buffer.portBuffer[dlgid].outBuffer;
	if (pOutBuf->msgReadSub == pOutBuf->msgWriteSub)
        	return 0;
    	memcpy(pcap_message,
    		&pOutBuf->msgList[pOutBuf->msgReadSub%SCF_PORT_LEN],
    		sizeof(CAP_Message));
    	pOutBuf->msgReadSub = 
    		(pOutBuf->msgReadSub + 1) % SCF_PORT_LEN;
	
	return 1;
}

int WriteScfOpen(CAP_Message cap_message)
{
	unsigned char 		msgReadSub, msgWriteSub;
	PSCF_Open_Buffer	popen_ptr;
	
	popen_ptr = &scf_buffer.openBuffer;
	msgReadSub = popen_ptr->msgReadSub;
	msgWriteSub = popen_ptr->msgWriteSub%SCF_OPEN_LEN;

	memcpy(&popen_ptr->msgList[msgWriteSub],
		&cap_message,
		sizeof(CAP_Message));
	popen_ptr->msgWriteSub = 
		(popen_ptr->msgWriteSub + 1)%SCF_OPEN_LEN;
	return 1;
}

int WriteScfMsg(CAP_Message cap_message)
{
	unsigned int		did;
	PSCF_HPort_Buffer	pInBuf;
	
	did = cap_message.dialogue_id[0]*256 +
		 cap_message.dialogue_id[1];
		 
	if(did >= SCF_MAX_DLG_NUM)
		return 0;
		
	pInBuf = &scf_buffer.portBuffer[did].inBuffer;
	
	memcpy(&pInBuf->msgList[pInBuf->msgWriteSub%SCF_PORT_LEN],
		&cap_message,
		sizeof(CAP_Message));
	pInBuf->msgWriteSub = 
		(pInBuf->msgWriteSub + 1) % SCF_PORT_LEN;
	if (pInBuf->msgReadSub == pInBuf->msgWriteSub)
		pInBuf->msgReadSub = 
			(pInBuf->msgReadSub + 1) % SCF_PORT_LEN;
	return 1;
}

int ClearPortSub(unsigned short dlgid)
{
	if(dlgid>=SCF_MAX_DLG_NUM) return 0;
	scf_buffer.portBuffer[dlgid].inBuffer.msgWriteSub = 0;
	scf_buffer.portBuffer[dlgid].inBuffer.msgReadSub = 0;
	scf_buffer.portBuffer[dlgid].outBuffer.msgWriteSub = 0;
	scf_buffer.portBuffer[dlgid].outBuffer.msgReadSub = 0;
	return 1;
}
/*-----------------------------------------------------------*/
