/********************************************************************************
 * File Name:			MsgQueue.c
 * Function Describe:   ������Ϣ���ݻ���������Ϣ����
 * Explain:             
	����Ϣ������һ���������ݽṹ�����Խ�����Ϣ����У�����ȡ��Ϣ�����У�������������MSG_QUEUE_NUM_MAX����Ϣ
	ÿ����Ϣ����ʹ��һ�����νṹ����Ϣ���ݻ�����������Ϣ�����ڵ�������Ϣ�����ô����ݻ�����
	�����ڴ��ڽ��յ����ݴ�����������������ģ��Ĵ��ڽ������ݴ�����
�÷���
    �ȶ�����Ϣ����SUT_MSG_QUEUEʵ��������������Ϣ���ݻ�����ʵ��
	Ȼ����MsgQueueInit��ʼ����Ϣ����
	��MsgQueuePost����Ϣ����У������ʱ��������Ϣ���������ݵ�SUT_MSG_QUEUEʵ�������ݻ������С�
	��MsgQueueAccept���Ӷ�����ȡ��Ϣ��ȡ��Ϣʱ�������ݻ������п������ݵ�ָ���������ǻ��λ��������Ա㴦����Ϣ
 * Writer:				ShiLiangWen
 * Date:				2015-7-2
 *******************************************************************************/
#define THIS_FILE_ID 5
/********************************************************************************/
#include "includes.h"

unsigned char msgBuffer[MSG_DATA_BUF_LEN];
SUT_MSG_QUEUE msgQue;

void MsgQueueSet(void)
{
	MsgQueueInit((void *)msgBuffer,MSG_DATA_BUF_LEN);
}
/********************************************************************************************
*
*********************************************************************************************/
void MsgQueueInit(char *pDataBuffer,unsigned short DataBufferLenMax)
{
	int i;
	SUT_MSG_QUEUE *pMsgQueue = &msgQue;
	pMsgQueue->MsgIn=0;
	pMsgQueue->MsgNum=0;
	pMsgQueue->MsgOut=0;
	for(i=0;i<MSG_QUEUE_NUM_MAX;i++){
		pMsgQueue->MsgQueue[i].DataStartIndex=0;
		pMsgQueue->MsgQueue[i].MsgLen=0;
	}
	//
	pMsgQueue->DataBuffer=pDataBuffer;
	for(i=0;i<DataBufferLenMax;i++)pMsgQueue->DataBuffer[i]=0;
	pMsgQueue->DataBufferLenMax=DataBufferLenMax;
	pMsgQueue->DataBufferIn=0;
	pMsgQueue->DataBufferOut=0;
	pMsgQueue->DataBufferLen=0;
}
/********************************************************************************************
*
*********************************************************************************************/
int MsgQueuePost(char *pData,unsigned short DataLen)
{
	SUT_MSG_QUEUE *pMsgQueue = &msgQue;
	unsigned short i;
	//���������������������
	//SlwTrace(INF, "in MSQ\r\n");
	if(pMsgQueue->MsgNum>=MSG_QUEUE_NUM_MAX){
		//SlwTrace(INF, "MsgQueue full!\r\n");
		return -1;
	}
	//���ݻ����������������������
	if((DataLen+pMsgQueue->DataBufferLen)>pMsgQueue->DataBufferLenMax){
		//SlwTrace(INF,"DataBuffer full!\r\n");
		return -2;
	}
	//��Ϣ�����
	pMsgQueue->MsgQueue[pMsgQueue->MsgIn].DataStartIndex=pMsgQueue->DataBufferIn;
	pMsgQueue->MsgQueue[pMsgQueue->MsgIn].MsgLen=DataLen;
	//��Ϣ�����뻺����
	for(i=0;i<DataLen;i++){
		pMsgQueue->DataBuffer[pMsgQueue->DataBufferIn]=pData[i];
		pMsgQueue->DataBufferIn++;
		if(pMsgQueue->DataBufferIn>=MSG_DATA_BUF_LEN)pMsgQueue->DataBufferIn=0;
		pMsgQueue->DataBufferLen++;
	}
	//��Ϣ��������
	pMsgQueue->MsgIn++;
	if(pMsgQueue->MsgIn>=MSG_QUEUE_NUM_MAX)pMsgQueue->MsgIn=0;
	pMsgQueue->MsgNum++;
	return 0;
}

/********************************************************************************************
��ȡpMsgQueue��Ϣ��������pBuf�У�����pBuf�ռ䳤��ΪBufLen
����r:
r=0   --����Ϣ
r>0   --����Ϣ�����ѿ�����Ϣ���ݵ�pData,rΪ��Ϣ���ݳ���
r<0   --����Ϣ,���������ݻ��������Ȳ���
*********************************************************************************************/

int MsgQueueAccept(char *pBuf,unsigned short BufLen)
{
	SUT_MSG_QUEUE *pMsgQueue = &msgQue;
	unsigned short i,j;
	char data;
	SUT_MESSAGE msg;
	
	if(g_ucModemTaskEn==0)
		{
			printf("STOP GO GT\r\n");
			Modem_Stopinit();
		}
	
	if(pMsgQueue->MsgNum==0)return 0;
	//SlwTrace(INF, "out MSQ\r\n");
	msg.MsgLen = pMsgQueue->MsgQueue[pMsgQueue->MsgOut].MsgLen;
	msg.DataStartIndex = pMsgQueue->MsgQueue[pMsgQueue->MsgOut].DataStartIndex;
	j=0;
	for(i=0;i<msg.MsgLen;i++){
		if(0==pMsgQueue->DataBufferLen)break;
		data=pMsgQueue->DataBuffer[pMsgQueue->DataBufferOut];
		pMsgQueue->DataBufferOut++;
		if(pMsgQueue->DataBufferOut>=pMsgQueue->DataBufferLenMax)pMsgQueue->DataBufferOut=0;
		pMsgQueue->DataBufferLen--;
		if(j<BufLen)pBuf[j++]=data;
	}
	pMsgQueue->MsgOut++;
	if(pMsgQueue->MsgOut>=MSG_QUEUE_NUM_MAX)pMsgQueue->MsgOut=0;
	pMsgQueue->MsgNum--;
	
	if(i!=msg.MsgLen){
		return -2;//Databuffer len error!
	}
	
	if(msg.MsgLen>BufLen){
		return -1;
	}else{
		return msg.MsgLen;
	}
}

/********************************************************************************************
*
*********************************************************************************************/