diff --git a/peripheral/com.c b/peripheral/com.c index e9ed1ad..aa6c5e6 100644 --- a/peripheral/com.c +++ b/peripheral/com.c @@ -72,7 +72,8 @@ TComCtrl *rd_ComCreate(cbk_ComCheck _pfCheck, cbk_ComDecode _pfDecode, cbk_ComSe if (NULL == ptComCtrl) return NULL; ptComCtrl->m_iBufferSize = _iSize; ptComCtrl->m_pUserData = _pUserData; - //Rd_SemInit(&ptComCtrl->m_stComBuf.m_sem_Recv, 0, 0); + ptComCtrl->m_pSendTempBuffer = RD_CALLOC(1, _iSize); + Rd_SemInit(&ptComCtrl->m_stComBuf.m_sem_Recv, 0, 0); Rd_SemInit(&ptComCtrl->m_stComBuf.m_sem_Send, 0, 0); ptComCtrl->m_stComBuf.m_ptRecv = rd_RingbufferCreate(_iSize); rd_RingbufferReset(ptComCtrl->m_stComBuf.m_ptRecv); @@ -117,7 +118,8 @@ int rd_ComDelete(TComCtrl *_ptComCtrl) utarray_free(_ptComCtrl->m_pMutiDevArray); } - //Rd_SemDestroy(&_ptComCtrl->m_stComBuf.m_sem_Recv); + RD_FREE(_ptComCtrl->m_pSendTempBuffer); + Rd_SemDestroy(&_ptComCtrl->m_stComBuf.m_sem_Recv); Rd_SemDestroy(&_ptComCtrl->m_stComBuf.m_sem_Send); rd_RingbufferDestroy(_ptComCtrl->m_stComBuf.m_ptRecv); rd_RingbufferDestroy(_ptComCtrl->m_stComBuf.m_ptSend); @@ -167,16 +169,14 @@ int rd_ComSend(TComCtrl *_ptComCtrl, char *_pBuffer, uint32_t _iSize) void rd_ComSendProc(TComCtrl *_ptComCtrl) { if (NULL == _ptComCtrl) return; - char *pcBuffer = RD_CALLOC(1, _ptComCtrl->m_iBufferSize);; int iRet = 0; Rd_SemWait(&_ptComCtrl->m_stComBuf.m_sem_Send, __func__, NULL); - iRet = rd_RingbufferGet(_ptComCtrl->m_stComBuf.m_ptSend, pcBuffer, _ptComCtrl->m_iBufferSize); + iRet = rd_RingbufferGet(_ptComCtrl->m_stComBuf.m_ptSend, _ptComCtrl->m_pSendTempBuffer, _ptComCtrl->m_iBufferSize); if (iRet > 0) { - rd_ComSend(_ptComCtrl, pcBuffer, iRet); + rd_ComSend(_ptComCtrl, _ptComCtrl->m_pSendTempBuffer, iRet); } - RD_FREE(pcBuffer); } void rd_ComRecvProc(TComCtrl *_ptComCtrl, const char *_pBuffer, uint32_t _iSize) @@ -200,25 +200,6 @@ int rd_ComWrite(TComCtrl *_ptComCtrl, char *_pBuffer, uint32_t _iSize) return iRet; } -//static int rd_MutiDevIsconflict(TComCtrl *_ptComCtrl, char *_pBuffer, uint32_t _iSize, int _MyIdx) -//{ -// int iArrlen = utarray_len(_ptComCtrl->m_pMutiDevArray); -// for(int i = 0; i < iArrlen; i++) -// { -// TMutiDev **ppMutiDev = (TMutiDev**)utarray_eltptr(_ptComCtrl->m_pMutiDevArray, i); -// TMutiDev *ptMutiDev = *ppMutiDev; -// if (i == _MyIdx) continue; -// -// int result = ptMutiDev->m_pfCheck(_pBuffer, _iSize); -// if (result > 0) -// { -// return i; -// } -// } -// return RD_NULL; -//} - - /***************************************************************************** 函 数 名 : rd_ComRead 功能描述 : 读取一包数据,这里的入参用于存放校验通过的数据,如果buffer实际大小 @@ -279,7 +260,7 @@ int rd_ComRead(TComCtrl *_ptComCtrl, char *_pBuffer, uint32_t _iSize) iPackLen = ptMutiDev->m_pfCheck(pPeakData, iPeakLen); } - if (iPackLen > 0 && iPackLen <= iPeakLen) + if (iPackLen > 0) { _ptComCtrl->m_iDevIndex = i; bFound = 1; @@ -344,19 +325,29 @@ int rd_ComRead(TComCtrl *_ptComCtrl, char *_pBuffer, uint32_t _iSize) { iPackLen = ptMutiDev->m_pfCheck(pPeakData, iPeakLen); } - -// if (rd_MutiDevIsconflict(_ptComCtrl, pPeakData, iPeakLen, _ptComCtrl->m_iDevIndex) >= 0) -// { -// _ptComCtrl->m_iDevIndex = -1; -// char temp[1]; -// rd_RingbufferGet(_ptComCtrl->m_stComBuf.m_ptRecv, temp, 1); -// return RD_FAILURE; -// } if (iPackLen > 0 && iPackLen <= iPeakLen) { - int iLen = (_iSize > iPackLen) ? iPackLen : _iSize; - int iRet = rd_RingbufferGet(_ptComCtrl->m_stComBuf.m_ptRecv, _pBuffer, iLen); + if (_iSize < iPackLen) + { + // 用户 buffer 太小,直接丢弃完整包,避免污染后续解析 + char *dropBuf = RD_CALLOC(1, iPackLen); + if (dropBuf != NULL) + { + rd_RingbufferGet(_ptComCtrl->m_stComBuf.m_ptRecv, dropBuf, iPackLen); + RD_FREE(dropBuf); + } + else + { + char temp[1]; + rd_RingbufferGet(_ptComCtrl->m_stComBuf.m_ptRecv, temp, 1); + } + + _ptComCtrl->m_iDevIndex = -1; + ptMutiDev->m_uiLastRecvTime = 0; + return RD_INVALUE; + } + int iRet = rd_RingbufferGet(_ptComCtrl->m_stComBuf.m_ptRecv, _pBuffer, iPackLen); if (iRet > 0) { @@ -364,7 +355,7 @@ int rd_ComRead(TComCtrl *_ptComCtrl, char *_pBuffer, uint32_t _iSize) if(NULL != ptMutiDev->m_pfDecode) { ptMutiDev->m_uiLastRecvTime = 0; - ptMutiDev->m_pfDecode(_pBuffer, iLen); + ptMutiDev->m_pfDecode(_pBuffer, iRet); } return iRet; } @@ -374,11 +365,30 @@ int rd_ComRead(TComCtrl *_ptComCtrl, char *_pBuffer, uint32_t _iSize) return RD_FAILURE; } } + else if (iPackLen == 0 || iPackLen > iPeakLen) + { + if (ptMutiDev->m_uiLastRecvTime == 0) + { + ptMutiDev->m_uiLastRecvTime = u32CurrentTime; + } + + if ((u32CurrentTime - ptMutiDev->m_uiLastRecvTime) > RECV_TIMEOUT) + { + char temp[1]; + rd_RingbufferGet(_ptComCtrl->m_stComBuf.m_ptRecv, temp, 1); + ptMutiDev->m_uiLastRecvTime = 0; + _ptComCtrl->m_iDevIndex = -1; + return RD_FAILURE; + } + + return 0; + } else { // 长度错误或异常 char temp[1]; rd_RingbufferGet(_ptComCtrl->m_stComBuf.m_ptRecv, temp, 1); + ptMutiDev->m_uiLastRecvTime = 0; _ptComCtrl->m_iDevIndex = -1; return RD_FAILURE; } diff --git a/peripheral/include/com.h b/peripheral/include/com.h index bd1b230..a28064d 100644 --- a/peripheral/include/com.h +++ b/peripheral/include/com.h @@ -95,6 +95,7 @@ typedef struct typedef struct { TComBuffer m_stComBuf; + char *m_pSendTempBuffer; int m_iBufferSize; int m_iDevIndex; // 表示当前正在接收的设备索引,-1表示当前无设备在处理 UT_array *m_pMutiDevArray;