| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- /********************************************************************************
- * File Name: MsgQueue.c
- * Function Describe: 共享消息数据缓冲区的消息队列
- * Explain:
- * Writer: ShiLiangWen
- * Date: 2015-7-2
- *******************************************************************************/
- #include "includes.h"
- char DataBuffer[MSG_DATA_BUF_LEN];//消息数据缓冲区
- unsigned short DataBufferLen;//消息数据缓冲区 数据长度
- unsigned short DataBufferIn; //消息数据缓冲区 入数据索引
- unsigned short DataBufferOut;//消息数据缓冲区 出数据索引
- SUT_MSG_QUEUE ModemMsgQueue;//消息队列
- /********************************************************************************************
- *
- *********************************************************************************************/
- void MsgDataBufferInit(void)
- {
- int i;
- for(i=0;i<MSG_DATA_BUF_LEN;i++)DataBuffer[i]=0;
- DataBufferLen=0;
- DataBufferIn=0;
- DataBufferOut=0;
- }
- /********************************************************************************************
- *
- *********************************************************************************************/
- void MsgQueueInit(SUT_MSG_QUEUE *pMsgQueue)
- {
- int i;
- 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;
- }
- }
- /********************************************************************************************
- *
- *********************************************************************************************/
- void MsgQueuePost(SUT_MSG_QUEUE *pMsgQueue,char *pData,unsigned short DataLen)
- {
- unsigned short i;
- //队列满,不允许再入队列
- if(pMsgQueue->MsgNum>=MSG_QUEUE_NUM_MAX){
- //SlwTrace("MsgQueue full!\r\n");
- return;
- }
- //数据缓冲区不够,不允许入队列
- if((DataLen+DataBufferLen)>MSG_DATA_BUF_LEN){
- //SlwTrace("DataBuffer full!\r\n");
- return;
- }
- //消息入队列
- pMsgQueue->MsgQueue[pMsgQueue->MsgIn].DataStartIndex=DataBufferIn;
- pMsgQueue->MsgQueue[pMsgQueue->MsgIn].MsgLen=DataLen;
- //消息数据入缓冲区
- for(i=0;i<DataLen;i++){
- DataBuffer[DataBufferIn]=pData[i];
- if(++DataBufferIn>=MSG_DATA_BUF_LEN)DataBufferIn=0;
- DataBufferLen++;
- }
- //消息个数增加
- pMsgQueue->MsgIn++;
- if(pMsgQueue->MsgIn>=MSG_QUEUE_NUM_MAX)pMsgQueue->MsgIn=0;
- pMsgQueue->MsgNum++;
- }
- /********************************************************************************************
- DataBufLen
- retrun r:
- r=0 --无消息
- r=正数 --有消息,并已拷贝消息数据到pData,r为消息数据长度
- r=-1 --有消息,但接受数据缓冲区长度不够,并已拷贝部分消息
- *********************************************************************************************/
- int MsgQueueAccept(SUT_MSG_QUEUE *pMsgQueue,char *pBuf,unsigned short BufLen)
- {
- unsigned short i,j;
- char data;
- SUT_MESSAGE msg;
- if(pMsgQueue->MsgNum==0)return 0;
-
- 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==DataBufferLen)break;
- data=DataBuffer[DataBufferOut];
- if(++DataBufferOut>=MSG_DATA_BUF_LEN)DataBufferOut=0;
- 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;
- }
- /********************************************************************************************
- *
- *********************************************************************************************/
|