/* FILE NAME:bufQueue.c AUTHOR:WJL DATE:2018 FUNCTION: *CREATE THE QUEUE APIS */ #include "bufQueue.h" #include #include #if defined ENABLE_QUE_TASK_LOCK #include "osi_api.h" #endif unsigned char PlyMsgBuffer[PLY_MSG_QUEUE_LEN_MAX]; SUT_MSG_QUEUE Ply_MsgQue; unsigned char RecMsgBuffer[REC_MSG_QUEUE_LEN_MAX]; SUT_MSG_QUEUE Rec_MsgQue; static unsigned char OuterMsgBuffer[OUTER_MSG_QUEUE_LEN_MAX]; static SUT_MSG_QUEUE Outer_MsgQue; static unsigned char InnerMsgBuffer[INNER_MSG_QUEUE_LEN_MAX]; static SUT_MSG_QUEUE Inner_MsgQue; static unsigned char LogMsgBuffer[LOG_MSG_QUEUE_LEN_MAX]; static SUT_MSG_QUEUE Log_MsgQue; static char msgQueueInit(QUEUE_DEF type){ int i; SUT_MSG_QUEUE *pMsgQueue; unsigned char *srcbuf; unsigned int bufMaxLen; char ret=0; #if defined ENABLE_QUE_TASK_LOCK uint32_t osFlag; osFlag=osiSchedulerSuspend(); #endif if(Q_OUTER==type){ pMsgQueue=&Outer_MsgQue; srcbuf=OuterMsgBuffer; bufMaxLen=sizeof(OuterMsgBuffer); }else if(Q_INNER==type){ pMsgQueue=&Inner_MsgQue; srcbuf=InnerMsgBuffer; bufMaxLen=sizeof(InnerMsgBuffer); }else if(Q_LOG==type){ pMsgQueue=&Log_MsgQue; srcbuf=LogMsgBuffer; bufMaxLen=sizeof(LogMsgBuffer); }else if(Q_VOICEP==type){ pMsgQueue=&Ply_MsgQue; srcbuf=PlyMsgBuffer; bufMaxLen=sizeof(PlyMsgBuffer); }else if(Q_VOICER==type){ pMsgQueue=&Rec_MsgQue; srcbuf=RecMsgBuffer; bufMaxLen=sizeof(RecMsgBuffer); }else{ ret=1; goto EXIT; } pMsgQueue->MsgIn=0; pMsgQueue->MsgOut=0; pMsgQueue->MsgNum=0; for(i=0;iMsgQueue[i].DataStartIndex=0; pMsgQueue->MsgQueue[i].MsgLen=0; } pMsgQueue->DataBuffer=srcbuf; for(i=0;iDataBuffer[i]=0; pMsgQueue->DataBufferLenMax=bufMaxLen; pMsgQueue->DataBufferIn=0; pMsgQueue->DataBufferOut=0; pMsgQueue->DataBufferLen=0; pMsgQueue->queLocker=0; EXIT: #if defined ENABLE_QUE_TASK_LOCK osiSchedulerResume(osFlag); #endif return ret; } char msgQueueSet(void){ char qret1,qret2,qret3,qret4,qret5; qret1=msgQueueInit(Q_OUTER); qret2=msgQueueInit(Q_INNER); qret3=msgQueueInit(Q_LOG); qret4=msgQueueInit(Q_VOICEP); qret5=msgQueueInit(Q_VOICER); return (qret1|qret2|qret3|qret4|qret5); } void msgQueueClear(QUEUE_DEF type){ msgQueueInit(type); } //return 0 queue post ok // 1 queue full // 2 queue buffer full // 3 queue invalid static char msgQueuePost(unsigned char *pData,unsigned short DataLen,QUEUE_DEF type, unsigned char subType){ SUT_MSG_QUEUE *pMsgQueue; unsigned int i; char ret; #if defined ENABLE_QUE_TASK_LOCK uint32_t osFlag; osFlag=osiSchedulerSuspend(); #endif if(Q_OUTER==type) pMsgQueue=&Outer_MsgQue; else if(Q_INNER==type) pMsgQueue=&Inner_MsgQue; else if(Q_LOG==type) pMsgQueue=&Log_MsgQue; else if(Q_VOICEP==type) pMsgQueue=&Ply_MsgQue; else if(Q_VOICER==type) pMsgQueue=&Rec_MsgQue; else{ ret=3; goto EXIT; } //queue full , reject to post queue anymore if(pMsgQueue->MsgNum>=MSG_QUEUE_NUM_MAX){ ret=1; goto EXIT; } //queue buffer full, rejiect to post queue anymore if((DataLen+pMsgQueue->DataBufferLen)>pMsgQueue->DataBufferLenMax){ ret=2; goto EXIT; } //start to queue post pMsgQueue->MsgQueue[pMsgQueue->MsgIn].type=subType; pMsgQueue->MsgQueue[pMsgQueue->MsgIn].DataStartIndex=pMsgQueue->DataBufferIn; pMsgQueue->MsgQueue[pMsgQueue->MsgIn].MsgLen=DataLen; //queue post to buffer for(i=0;iDataBuffer[pMsgQueue->DataBufferIn]=pData[i]; pMsgQueue->DataBufferIn++; if(pMsgQueue->DataBufferIn>=pMsgQueue->DataBufferLenMax)pMsgQueue->DataBufferIn=0; pMsgQueue->DataBufferLen++; } //add queue number pMsgQueue->MsgIn++; if(pMsgQueue->MsgIn>=MSG_QUEUE_NUM_MAX)pMsgQueue->MsgIn=0; pMsgQueue->MsgNum++; ret=0; EXIT: #if defined ENABLE_QUE_TASK_LOCK osiSchedulerResume(osFlag); #endif #if 1 //入列失败时,直接打印出来检测是否是入队列有问题 if(0 != ret){ char info[100]; snprintf(info, sizeof(info), "!!!Post failed:%d,%d,%d,%s",type,subType,DataLen,pData); outterInfo(info, strlen(info)); } #endif return ret; } //r=0 --no valid message queue //r>0 --got message queue and data is ready in pBuf //r<0 --got message buf accept pBuf is too small short msgQueueAccept(unsigned char *pBuf,unsigned short BufLen, QUEUE_DEF type, unsigned char *subType){ SUT_MSG_QUEUE *pMsgQueue; unsigned int i,j; char data; SUT_MESSAGE msg; short relen; #if defined ENABLE_QUE_TASK_LOCK uint32_t osFlag; osFlag=osiSchedulerSuspend(); #endif if(Q_OUTER==type) pMsgQueue=&Outer_MsgQue; else if(Q_INNER==type) pMsgQueue=&Inner_MsgQue; else if(Q_LOG==type) pMsgQueue=&Log_MsgQue; else if(Q_VOICEP==type) pMsgQueue=&Ply_MsgQue; else if(Q_VOICER==type) pMsgQueue=&Rec_MsgQue; else{ relen=0; goto EXIT; } if(pMsgQueue->MsgNum==0){ relen=0; goto EXIT; } msg.type = pMsgQueue->MsgQueue[pMsgQueue->MsgOut].type; msg.MsgLen = pMsgQueue->MsgQueue[pMsgQueue->MsgOut].MsgLen; msg.DataStartIndex = pMsgQueue->MsgQueue[pMsgQueue->MsgOut].DataStartIndex; j=0; for(i=0;iDataBufferLen)break; data=pMsgQueue->DataBuffer[pMsgQueue->DataBufferOut]; pMsgQueue->DataBufferOut++; if(pMsgQueue->DataBufferOut>=pMsgQueue->DataBufferLenMax)pMsgQueue->DataBufferOut=0; pMsgQueue->DataBufferLen--; if(jMsgOut++; if(pMsgQueue->MsgOut>=MSG_QUEUE_NUM_MAX)pMsgQueue->MsgOut=0; pMsgQueue->MsgNum--; if(i!=msg.MsgLen){ relen=-2; goto EXIT; } if(msg.MsgLen>BufLen){ relen=-1; }else{ *subType=msg.type; relen=msg.MsgLen; } EXIT: #if defined ENABLE_QUE_TASK_LOCK osiSchedulerResume(osFlag); #endif return relen; } unsigned short msgQueueNum(QUEUE_DEF type){ SUT_MSG_QUEUE *pMsgQueue; if(Q_OUTER==type) pMsgQueue=&Outer_MsgQue; else if(Q_INNER==type) pMsgQueue=&Inner_MsgQue; else if(Q_LOG==type) pMsgQueue=&Log_MsgQue; else if(Q_VOICEP==type) pMsgQueue=&Ply_MsgQue; else if(Q_VOICER==type) pMsgQueue=&Rec_MsgQue; else return 0; return pMsgQueue->MsgNum; } static char msgPlyPost(unsigned char *info, unsigned short len,unsigned char subType){ return msgQueuePost(info,len,Q_VOICEP, subType); } static char msgRecPost(unsigned char *info, unsigned short len,unsigned char subType){ return msgQueuePost(info,len,Q_VOICER, subType); } char logToOutter(char *info){ return msgQueuePost(info,strlen(info),Q_LOG, QSUB_COM_LOG); } char msgToOutter(char *info){//msg-->queue-->cmd need to send to outter(usart) MakeStringToUpperCap(info, strlen(info)); return msgQueuePost(info,strlen(info),Q_OUTER, QSUB_COM_TO_OUTTER); } char msgSToOutter(unsigned char *info, unsigned short len){//msg-->queue-->cmd need to send to outter(usart) return msgQueuePost(info,len,Q_OUTER, QSUB_COM_TO_OUTTER); } char msgToInner(char *info){//msg-->queue-->cmd need to send to inner(modem) return msgQueuePost(info,strlen(info),Q_INNER, QSUB_COM_TO_INNER); } char msgSToInner(unsigned char *info, unsigned short len){//msg-->queue-->cmd need to send to inner(modem) return msgQueuePost(info,len,Q_INNER, QSUB_COM_TO_INNER); } char msgToPlayer(unsigned char *info, unsigned short len){//msg-->queue-->voice to play return msgPlyPost(info, len, QSUB_VOICE_PLAY); } char msgToRecord(unsigned char *info, unsigned short len){//msg-->queue-->voice that record return msgRecPost(info, len, QSUB_VOICE_RECORD); } char msgFromInner(unsigned char *info, unsigned short len){//msg-->queue-->cmd get from inner(modem) return msgQueuePost(info,len,Q_INNER, QSUB_COM_FROM_INNER); } char msgFromOutter(unsigned char *info, unsigned short len){//msg-->queue-->cmd get from outter(usart) return msgQueuePost(info,len,Q_OUTER, QSUB_COM_FROM_OUTTER); }