bufQueue.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. FILE NAME:bufQueue.c
  3. AUTHOR:WJL
  4. DATE:2018
  5. FUNCTION:
  6. *CREATE THE QUEUE APIS
  7. */
  8. #include "bufQueue.h"
  9. #include <stdio.h>
  10. #include <string.h>
  11. #if defined ENABLE_QUE_TASK_LOCK
  12. #include "osi_api.h"
  13. #endif
  14. unsigned char PlyMsgBuffer[PLY_MSG_QUEUE_LEN_MAX];
  15. SUT_MSG_QUEUE Ply_MsgQue;
  16. unsigned char RecMsgBuffer[REC_MSG_QUEUE_LEN_MAX];
  17. SUT_MSG_QUEUE Rec_MsgQue;
  18. static unsigned char OuterMsgBuffer[OUTER_MSG_QUEUE_LEN_MAX];
  19. static SUT_MSG_QUEUE Outer_MsgQue;
  20. static unsigned char InnerMsgBuffer[INNER_MSG_QUEUE_LEN_MAX];
  21. static SUT_MSG_QUEUE Inner_MsgQue;
  22. static unsigned char LogMsgBuffer[LOG_MSG_QUEUE_LEN_MAX];
  23. static SUT_MSG_QUEUE Log_MsgQue;
  24. static char msgQueueInit(QUEUE_DEF type){
  25. int i;
  26. SUT_MSG_QUEUE *pMsgQueue;
  27. unsigned char *srcbuf;
  28. unsigned int bufMaxLen;
  29. char ret=0;
  30. #if defined ENABLE_QUE_TASK_LOCK
  31. uint32_t osFlag;
  32. osFlag=osiSchedulerSuspend();
  33. #endif
  34. if(Q_OUTER==type){
  35. pMsgQueue=&Outer_MsgQue;
  36. srcbuf=OuterMsgBuffer;
  37. bufMaxLen=sizeof(OuterMsgBuffer);
  38. }else if(Q_INNER==type){
  39. pMsgQueue=&Inner_MsgQue;
  40. srcbuf=InnerMsgBuffer;
  41. bufMaxLen=sizeof(InnerMsgBuffer);
  42. }else if(Q_LOG==type){
  43. pMsgQueue=&Log_MsgQue;
  44. srcbuf=LogMsgBuffer;
  45. bufMaxLen=sizeof(LogMsgBuffer);
  46. }else if(Q_VOICEP==type){
  47. pMsgQueue=&Ply_MsgQue;
  48. srcbuf=PlyMsgBuffer;
  49. bufMaxLen=sizeof(PlyMsgBuffer);
  50. }else if(Q_VOICER==type){
  51. pMsgQueue=&Rec_MsgQue;
  52. srcbuf=RecMsgBuffer;
  53. bufMaxLen=sizeof(RecMsgBuffer);
  54. }else{
  55. ret=1;
  56. goto EXIT;
  57. }
  58. pMsgQueue->MsgIn=0;
  59. pMsgQueue->MsgOut=0;
  60. pMsgQueue->MsgNum=0;
  61. for(i=0;i<MSG_QUEUE_NUM_MAX;i++){
  62. pMsgQueue->MsgQueue[i].DataStartIndex=0;
  63. pMsgQueue->MsgQueue[i].MsgLen=0;
  64. }
  65. pMsgQueue->DataBuffer=srcbuf;
  66. for(i=0;i<bufMaxLen;i++)pMsgQueue->DataBuffer[i]=0;
  67. pMsgQueue->DataBufferLenMax=bufMaxLen;
  68. pMsgQueue->DataBufferIn=0;
  69. pMsgQueue->DataBufferOut=0;
  70. pMsgQueue->DataBufferLen=0;
  71. pMsgQueue->queLocker=0;
  72. EXIT:
  73. #if defined ENABLE_QUE_TASK_LOCK
  74. osiSchedulerResume(osFlag);
  75. #endif
  76. return ret;
  77. }
  78. char msgQueueSet(void){
  79. char qret1,qret2,qret3,qret4,qret5;
  80. qret1=msgQueueInit(Q_OUTER);
  81. qret2=msgQueueInit(Q_INNER);
  82. qret3=msgQueueInit(Q_LOG);
  83. qret4=msgQueueInit(Q_VOICEP);
  84. qret5=msgQueueInit(Q_VOICER);
  85. return (qret1|qret2|qret3|qret4|qret5);
  86. }
  87. void msgQueueClear(QUEUE_DEF type){
  88. msgQueueInit(type);
  89. }
  90. //return 0 queue post ok
  91. // 1 queue full
  92. // 2 queue buffer full
  93. // 3 queue invalid
  94. static char msgQueuePost(unsigned char *pData,unsigned short DataLen,QUEUE_DEF type, unsigned char subType){
  95. SUT_MSG_QUEUE *pMsgQueue;
  96. unsigned int i;
  97. char ret;
  98. #if defined ENABLE_QUE_TASK_LOCK
  99. uint32_t osFlag;
  100. osFlag=osiSchedulerSuspend();
  101. #endif
  102. if(Q_OUTER==type) pMsgQueue=&Outer_MsgQue;
  103. else if(Q_INNER==type) pMsgQueue=&Inner_MsgQue;
  104. else if(Q_LOG==type) pMsgQueue=&Log_MsgQue;
  105. else if(Q_VOICEP==type) pMsgQueue=&Ply_MsgQue;
  106. else if(Q_VOICER==type) pMsgQueue=&Rec_MsgQue;
  107. else{
  108. ret=3;
  109. goto EXIT;
  110. }
  111. //queue full , reject to post queue anymore
  112. if(pMsgQueue->MsgNum>=MSG_QUEUE_NUM_MAX){
  113. ret=1;
  114. goto EXIT;
  115. }
  116. //queue buffer full, rejiect to post queue anymore
  117. if((DataLen+pMsgQueue->DataBufferLen)>pMsgQueue->DataBufferLenMax){
  118. ret=2;
  119. goto EXIT;
  120. }
  121. //start to queue post
  122. pMsgQueue->MsgQueue[pMsgQueue->MsgIn].type=subType;
  123. pMsgQueue->MsgQueue[pMsgQueue->MsgIn].DataStartIndex=pMsgQueue->DataBufferIn;
  124. pMsgQueue->MsgQueue[pMsgQueue->MsgIn].MsgLen=DataLen;
  125. //queue post to buffer
  126. for(i=0;i<DataLen;i++){
  127. pMsgQueue->DataBuffer[pMsgQueue->DataBufferIn]=pData[i];
  128. pMsgQueue->DataBufferIn++;
  129. if(pMsgQueue->DataBufferIn>=pMsgQueue->DataBufferLenMax)pMsgQueue->DataBufferIn=0;
  130. pMsgQueue->DataBufferLen++;
  131. }
  132. //add queue number
  133. pMsgQueue->MsgIn++;
  134. if(pMsgQueue->MsgIn>=MSG_QUEUE_NUM_MAX)pMsgQueue->MsgIn=0;
  135. pMsgQueue->MsgNum++;
  136. ret=0;
  137. EXIT:
  138. #if defined ENABLE_QUE_TASK_LOCK
  139. osiSchedulerResume(osFlag);
  140. #endif
  141. #if 1 //入列失败时,直接打印出来检测是否是入队列有问题
  142. if(0 != ret){
  143. char info[100];
  144. snprintf(info, sizeof(info), "!!!Post failed:%d,%d,%d,%s",type,subType,DataLen,pData);
  145. outterInfo(info, strlen(info));
  146. }
  147. #endif
  148. return ret;
  149. }
  150. //r=0 --no valid message queue
  151. //r>0 --got message queue and data is ready in pBuf
  152. //r<0 --got message buf accept pBuf is too small
  153. short msgQueueAccept(unsigned char *pBuf,unsigned short BufLen, QUEUE_DEF type, unsigned char *subType){
  154. SUT_MSG_QUEUE *pMsgQueue;
  155. unsigned int i,j;
  156. char data;
  157. SUT_MESSAGE msg;
  158. short relen;
  159. #if defined ENABLE_QUE_TASK_LOCK
  160. uint32_t osFlag;
  161. osFlag=osiSchedulerSuspend();
  162. #endif
  163. if(Q_OUTER==type) pMsgQueue=&Outer_MsgQue;
  164. else if(Q_INNER==type) pMsgQueue=&Inner_MsgQue;
  165. else if(Q_LOG==type) pMsgQueue=&Log_MsgQue;
  166. else if(Q_VOICEP==type) pMsgQueue=&Ply_MsgQue;
  167. else if(Q_VOICER==type) pMsgQueue=&Rec_MsgQue;
  168. else{
  169. relen=0;
  170. goto EXIT;
  171. }
  172. if(pMsgQueue->MsgNum==0){
  173. relen=0;
  174. goto EXIT;
  175. }
  176. msg.type = pMsgQueue->MsgQueue[pMsgQueue->MsgOut].type;
  177. msg.MsgLen = pMsgQueue->MsgQueue[pMsgQueue->MsgOut].MsgLen;
  178. msg.DataStartIndex = pMsgQueue->MsgQueue[pMsgQueue->MsgOut].DataStartIndex;
  179. j=0;
  180. for(i=0;i<msg.MsgLen;i++){
  181. if(0==pMsgQueue->DataBufferLen)break;
  182. data=pMsgQueue->DataBuffer[pMsgQueue->DataBufferOut];
  183. pMsgQueue->DataBufferOut++;
  184. if(pMsgQueue->DataBufferOut>=pMsgQueue->DataBufferLenMax)pMsgQueue->DataBufferOut=0;
  185. pMsgQueue->DataBufferLen--;
  186. if(j<BufLen)pBuf[j++]=data;
  187. }
  188. pMsgQueue->MsgOut++;
  189. if(pMsgQueue->MsgOut>=MSG_QUEUE_NUM_MAX)pMsgQueue->MsgOut=0;
  190. pMsgQueue->MsgNum--;
  191. if(i!=msg.MsgLen){
  192. relen=-2;
  193. goto EXIT;
  194. }
  195. if(msg.MsgLen>BufLen){
  196. relen=-1;
  197. }else{
  198. *subType=msg.type;
  199. relen=msg.MsgLen;
  200. }
  201. EXIT:
  202. #if defined ENABLE_QUE_TASK_LOCK
  203. osiSchedulerResume(osFlag);
  204. #endif
  205. return relen;
  206. }
  207. unsigned short msgQueueNum(QUEUE_DEF type){
  208. SUT_MSG_QUEUE *pMsgQueue;
  209. if(Q_OUTER==type) pMsgQueue=&Outer_MsgQue;
  210. else if(Q_INNER==type) pMsgQueue=&Inner_MsgQue;
  211. else if(Q_LOG==type) pMsgQueue=&Log_MsgQue;
  212. else if(Q_VOICEP==type) pMsgQueue=&Ply_MsgQue;
  213. else if(Q_VOICER==type) pMsgQueue=&Rec_MsgQue;
  214. else return 0;
  215. return pMsgQueue->MsgNum;
  216. }
  217. static char msgPlyPost(unsigned char *info, unsigned short len,unsigned char subType){
  218. return msgQueuePost(info,len,Q_VOICEP, subType);
  219. }
  220. static char msgRecPost(unsigned char *info, unsigned short len,unsigned char subType){
  221. return msgQueuePost(info,len,Q_VOICER, subType);
  222. }
  223. char logToOutter(char *info){
  224. return msgQueuePost(info,strlen(info),Q_LOG, QSUB_COM_LOG);
  225. }
  226. char msgToOutter(char *info){//msg-->queue-->cmd need to send to outter(usart)
  227. MakeStringToUpperCap(info, strlen(info));
  228. return msgQueuePost(info,strlen(info),Q_OUTER, QSUB_COM_TO_OUTTER);
  229. }
  230. char msgSToOutter(unsigned char *info, unsigned short len){//msg-->queue-->cmd need to send to outter(usart)
  231. return msgQueuePost(info,len,Q_OUTER, QSUB_COM_TO_OUTTER);
  232. }
  233. char msgToInner(char *info){//msg-->queue-->cmd need to send to inner(modem)
  234. return msgQueuePost(info,strlen(info),Q_INNER, QSUB_COM_TO_INNER);
  235. }
  236. char msgSToInner(unsigned char *info, unsigned short len){//msg-->queue-->cmd need to send to inner(modem)
  237. return msgQueuePost(info,len,Q_INNER, QSUB_COM_TO_INNER);
  238. }
  239. char msgToPlayer(unsigned char *info, unsigned short len){//msg-->queue-->voice to play
  240. return msgPlyPost(info, len, QSUB_VOICE_PLAY);
  241. }
  242. char msgToRecord(unsigned char *info, unsigned short len){//msg-->queue-->voice that record
  243. return msgRecPost(info, len, QSUB_VOICE_RECORD);
  244. }
  245. char msgFromInner(unsigned char *info, unsigned short len){//msg-->queue-->cmd get from inner(modem)
  246. return msgQueuePost(info,len,Q_INNER, QSUB_COM_FROM_INNER);
  247. }
  248. char msgFromOutter(unsigned char *info, unsigned short len){//msg-->queue-->cmd get from outter(usart)
  249. return msgQueuePost(info,len,Q_OUTER, QSUB_COM_FROM_OUTTER);
  250. }