MsgQueue.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /********************************************************************************
  2. * File Name: MsgQueue.c
  3. * Function Describe: 共享消息数据缓冲区的消息队列
  4. * Explain:
  5. * Writer: ShiLiangWen
  6. * Date: 2015-7-2
  7. *******************************************************************************/
  8. #include "includes.h"
  9. char DataBuffer[MSG_DATA_BUF_LEN];//消息数据缓冲区
  10. unsigned short DataBufferLen;//消息数据缓冲区 数据长度
  11. unsigned short DataBufferIn; //消息数据缓冲区 入数据索引
  12. unsigned short DataBufferOut;//消息数据缓冲区 出数据索引
  13. SUT_MSG_QUEUE ModemMsgQueue;//消息队列
  14. /********************************************************************************************
  15. *
  16. *********************************************************************************************/
  17. void MsgDataBufferInit(void)
  18. {
  19. int i;
  20. for(i=0;i<MSG_DATA_BUF_LEN;i++)DataBuffer[i]=0;
  21. DataBufferLen=0;
  22. DataBufferIn=0;
  23. DataBufferOut=0;
  24. }
  25. /********************************************************************************************
  26. *
  27. *********************************************************************************************/
  28. void MsgQueueInit(SUT_MSG_QUEUE *pMsgQueue)
  29. {
  30. int i;
  31. pMsgQueue->MsgIn=0;
  32. pMsgQueue->MsgNum=0;
  33. pMsgQueue->MsgOut=0;
  34. for(i=0;i<MSG_QUEUE_NUM_MAX;i++){
  35. pMsgQueue->MsgQueue[i].DataStartIndex=0;
  36. pMsgQueue->MsgQueue[i].MsgLen=0;
  37. }
  38. }
  39. /********************************************************************************************
  40. *
  41. *********************************************************************************************/
  42. void MsgQueuePost(SUT_MSG_QUEUE *pMsgQueue,char *pData,unsigned short DataLen)
  43. {
  44. unsigned short i;
  45. //队列满,不允许再入队列
  46. if(pMsgQueue->MsgNum>=MSG_QUEUE_NUM_MAX){
  47. //SlwTrace("MsgQueue full!\r\n");
  48. return;
  49. }
  50. //数据缓冲区不够,不允许入队列
  51. if((DataLen+DataBufferLen)>MSG_DATA_BUF_LEN){
  52. //SlwTrace("DataBuffer full!\r\n");
  53. return;
  54. }
  55. //消息入队列
  56. pMsgQueue->MsgQueue[pMsgQueue->MsgIn].DataStartIndex=DataBufferIn;
  57. pMsgQueue->MsgQueue[pMsgQueue->MsgIn].MsgLen=DataLen;
  58. //消息数据入缓冲区
  59. for(i=0;i<DataLen;i++){
  60. DataBuffer[DataBufferIn]=pData[i];
  61. if(++DataBufferIn>=MSG_DATA_BUF_LEN)DataBufferIn=0;
  62. DataBufferLen++;
  63. }
  64. //消息个数增加
  65. pMsgQueue->MsgIn++;
  66. if(pMsgQueue->MsgIn>=MSG_QUEUE_NUM_MAX)pMsgQueue->MsgIn=0;
  67. pMsgQueue->MsgNum++;
  68. }
  69. /********************************************************************************************
  70. DataBufLen
  71. retrun r:
  72. r=0 --无消息
  73. r=正数 --有消息,并已拷贝消息数据到pData,r为消息数据长度
  74. r=-1 --有消息,但接受数据缓冲区长度不够,并已拷贝部分消息
  75. *********************************************************************************************/
  76. int MsgQueueAccept(SUT_MSG_QUEUE *pMsgQueue,char *pBuf,unsigned short BufLen)
  77. {
  78. unsigned short i,j;
  79. char data;
  80. SUT_MESSAGE msg;
  81. if(pMsgQueue->MsgNum==0)return 0;
  82. msg.MsgLen = pMsgQueue->MsgQueue[pMsgQueue->MsgOut].MsgLen;
  83. msg.DataStartIndex = pMsgQueue->MsgQueue[pMsgQueue->MsgOut].DataStartIndex;
  84. j=0;
  85. for(i=0;i<msg.MsgLen;i++){
  86. if(0==DataBufferLen)break;
  87. data=DataBuffer[DataBufferOut];
  88. if(++DataBufferOut>=MSG_DATA_BUF_LEN)DataBufferOut=0;
  89. DataBufferLen--;
  90. if(j<BufLen)pBuf[j++]=data;
  91. }
  92. pMsgQueue->MsgOut++;
  93. if(pMsgQueue->MsgOut>=MSG_QUEUE_NUM_MAX)pMsgQueue->MsgOut=0;
  94. pMsgQueue->MsgNum--;
  95. if(i!=msg.MsgLen)return -2;//Databuffer len error!
  96. if(msg.MsgLen>BufLen)return -1;
  97. else return msg.MsgLen;
  98. }
  99. /********************************************************************************************
  100. *
  101. *********************************************************************************************/