WILC_Uint32 FIFO_ReadBytes(tHANDLE hFifo,WILC_Uint8 *pu8Buffer,WILC_Uint32 u32BytesToRead,WILC_Uint32 *pu32BytesRead) { WILC_Uint32 u32Error = 0; tstrFifoHandler * pstrFifoHandler = (tstrFifoHandler*)hFifo; if(pstrFifoHandler && pu32BytesRead) { if(pstrFifoHandler->u32TotalBytes) { if(WILC_SemaphoreAcquire(&pstrFifoHandler->SemBuffer, WILC_NULL) == WILC_SUCCESS) { if(u32BytesToRead > pstrFifoHandler->u32TotalBytes) { *pu32BytesRead = pstrFifoHandler->u32TotalBytes; } else { *pu32BytesRead = u32BytesToRead; } if((pstrFifoHandler->u32ReadOffset + u32BytesToRead) <= pstrFifoHandler->u32BufferLength) { WILC_memcpy(pu8Buffer,pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32ReadOffset, *pu32BytesRead); //update read offset and total bytes pstrFifoHandler->u32ReadOffset += u32BytesToRead; pstrFifoHandler->u32TotalBytes -= u32BytesToRead; } else { WILC_Uint32 u32FirstPart = pstrFifoHandler->u32BufferLength - pstrFifoHandler->u32ReadOffset; WILC_memcpy(pu8Buffer,pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32ReadOffset, u32FirstPart); WILC_memcpy(pu8Buffer + u32FirstPart,pstrFifoHandler->pu8Buffer, u32BytesToRead - u32FirstPart); //update read offset and total bytes pstrFifoHandler->u32ReadOffset = u32BytesToRead - u32FirstPart; pstrFifoHandler->u32TotalBytes -= u32BytesToRead; } WILC_SemaphoreRelease(&pstrFifoHandler->SemBuffer, WILC_NULL); } else { u32Error = 1; } } else { u32Error = 1; } } else { u32Error = 1; } return u32Error; }
u32 FIFO_WriteBytes(tHANDLE hFifo, u8 *pu8Buffer, u32 u32BytesToWrite, bool bForceOverWrite) { u32 u32Error = 0; tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo; if (pstrFifoHandler) { if (u32BytesToWrite < pstrFifoHandler->u32BufferLength) { if ((pstrFifoHandler->u32TotalBytes + u32BytesToWrite) <= pstrFifoHandler->u32BufferLength || bForceOverWrite) { down(&pstrFifoHandler->SemBuffer); if ((pstrFifoHandler->u32WriteOffset + u32BytesToWrite) <= pstrFifoHandler->u32BufferLength) { WILC_memcpy(pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32WriteOffset, pu8Buffer, u32BytesToWrite); /* update read offset and total bytes */ pstrFifoHandler->u32WriteOffset += u32BytesToWrite; pstrFifoHandler->u32TotalBytes += u32BytesToWrite; } else { u32 u32FirstPart = pstrFifoHandler->u32BufferLength - pstrFifoHandler->u32WriteOffset; WILC_memcpy(pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32WriteOffset, pu8Buffer, u32FirstPart); WILC_memcpy(pstrFifoHandler->pu8Buffer, pu8Buffer + u32FirstPart, u32BytesToWrite - u32FirstPart); /* update read offset and total bytes */ pstrFifoHandler->u32WriteOffset = u32BytesToWrite - u32FirstPart; pstrFifoHandler->u32TotalBytes += u32BytesToWrite; } /* if data overwriten */ if (pstrFifoHandler->u32TotalBytes > pstrFifoHandler->u32BufferLength) { /* adjust read offset to the oldest data available */ pstrFifoHandler->u32ReadOffset = pstrFifoHandler->u32WriteOffset; /* data availabe is the buffer length */ pstrFifoHandler->u32TotalBytes = pstrFifoHandler->u32BufferLength; } up(&pstrFifoHandler->SemBuffer); } else { u32Error = 1; } } else { u32Error = 1; } } else { u32Error = 1; } return u32Error; }
/*! * @author syounan * @date 1 Sep 2010 * @note copied from FLO glue implementatuion * @version 1.0 */ WILC_ErrNo WILC_MsgQueueSend(WILC_MsgQueueHandle *pHandle, const void *pvSendBuffer, WILC_Uint32 u32SendBufferSize, tstrWILC_MsgQueueAttrs *pstrAttrs) { WILC_ErrNo s32RetStatus = WILC_SUCCESS; unsigned long flags; Message *pstrMessage = NULL; if ((pHandle == NULL) || (u32SendBufferSize == 0) || (pvSendBuffer == NULL)) { WILC_ERRORREPORT(s32RetStatus, WILC_INVALID_ARGUMENT); } if (pHandle->bExiting == WILC_TRUE) { WILC_ERRORREPORT(s32RetStatus, WILC_FAIL); } spin_lock_irqsave(&pHandle->strCriticalSection, flags); /* construct a new message */ pstrMessage = WILC_NEW(Message, 1); WILC_NULLCHECK(s32RetStatus, pstrMessage); pstrMessage->u32Length = u32SendBufferSize; pstrMessage->pstrNext = NULL; pstrMessage->pvBuffer = WILC_MALLOC(u32SendBufferSize); WILC_NULLCHECK(s32RetStatus, pstrMessage->pvBuffer); WILC_memcpy(pstrMessage->pvBuffer, pvSendBuffer, u32SendBufferSize); /* add it to the message queue */ if (pHandle->pstrMessageList == NULL) { pHandle->pstrMessageList = pstrMessage; } else { Message *pstrTailMsg = pHandle->pstrMessageList; while (pstrTailMsg->pstrNext != NULL) { pstrTailMsg = pstrTailMsg->pstrNext; } pstrTailMsg->pstrNext = pstrMessage; } spin_unlock_irqrestore(&pHandle->strCriticalSection, flags); WILC_SemaphoreRelease(&pHandle->hSem, WILC_NULL); WILC_CATCH(s32RetStatus) { /* error occured, free any allocations */ if (pstrMessage != NULL) { if (pstrMessage->pvBuffer != NULL) { WILC_FREE(pstrMessage->pvBuffer); } WILC_FREE(pstrMessage); } } return s32RetStatus; }
u32 FIFO_ReadBytes(tHANDLE hFifo, u8 *pu8Buffer, u32 u32BytesToRead, u32 *pu32BytesRead) { u32 u32Error = 0; tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo; if (pstrFifoHandler && pu32BytesRead) { if (pstrFifoHandler->u32TotalBytes) { down(&pstrFifoHandler->SemBuffer); if (u32BytesToRead > pstrFifoHandler->u32TotalBytes) *pu32BytesRead = pstrFifoHandler->u32TotalBytes; else *pu32BytesRead = u32BytesToRead; if ((pstrFifoHandler->u32ReadOffset + u32BytesToRead) <= pstrFifoHandler->u32BufferLength) { WILC_memcpy(pu8Buffer, pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32ReadOffset, *pu32BytesRead); /* update read offset and total bytes */ pstrFifoHandler->u32ReadOffset += u32BytesToRead; pstrFifoHandler->u32TotalBytes -= u32BytesToRead; } else { u32 u32FirstPart = pstrFifoHandler->u32BufferLength - pstrFifoHandler->u32ReadOffset; WILC_memcpy(pu8Buffer, pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32ReadOffset, u32FirstPart); WILC_memcpy(pu8Buffer + u32FirstPart, pstrFifoHandler->pu8Buffer, u32BytesToRead - u32FirstPart); /* update read offset and total bytes */ pstrFifoHandler->u32ReadOffset = u32BytesToRead - u32FirstPart; pstrFifoHandler->u32TotalBytes -= u32BytesToRead; } up(&pstrFifoHandler->SemBuffer); } else { u32Error = 1; } } else { u32Error = 1; } return u32Error; }
WILC_Uint32 FIFO_WriteBytes(tHANDLE hFifo,WILC_Uint8 *pu8Buffer,WILC_Uint32 u32BytesToWrite,WILC_Bool bForceOverWrite) { WILC_Uint32 u32Error = 0; tstrFifoHandler * pstrFifoHandler = (tstrFifoHandler*)hFifo; if(pstrFifoHandler) { if(u32BytesToWrite < pstrFifoHandler->u32BufferLength) { if((pstrFifoHandler->u32TotalBytes + u32BytesToWrite) <= pstrFifoHandler->u32BufferLength || bForceOverWrite) { if(WILC_SemaphoreAcquire(&pstrFifoHandler->SemBuffer, WILC_NULL) == WILC_SUCCESS) { if((pstrFifoHandler->u32WriteOffset + u32BytesToWrite) <= pstrFifoHandler->u32BufferLength) { WILC_memcpy(pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32WriteOffset,pu8Buffer, u32BytesToWrite); //update read offset and total bytes pstrFifoHandler->u32WriteOffset += u32BytesToWrite; pstrFifoHandler->u32TotalBytes += u32BytesToWrite; } else { WILC_Uint32 u32FirstPart = pstrFifoHandler->u32BufferLength - pstrFifoHandler->u32WriteOffset; WILC_memcpy(pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32WriteOffset,pu8Buffer, u32FirstPart); WILC_memcpy(pstrFifoHandler->pu8Buffer,pu8Buffer + u32FirstPart, u32BytesToWrite - u32FirstPart); //update read offset and total bytes pstrFifoHandler->u32WriteOffset = u32BytesToWrite - u32FirstPart; pstrFifoHandler->u32TotalBytes += u32BytesToWrite; } //if data overwriten if(pstrFifoHandler->u32TotalBytes > pstrFifoHandler->u32BufferLength) { //adjust read offset to the oldest data available pstrFifoHandler->u32ReadOffset = pstrFifoHandler->u32WriteOffset; //data availabe is the buffer length pstrFifoHandler->u32TotalBytes = pstrFifoHandler->u32BufferLength; } WILC_SemaphoreRelease(&pstrFifoHandler->SemBuffer, WILC_NULL); } } else { u32Error = 1; } } else { u32Error = 1; } } else { u32Error = 1; } return u32Error; }
/*! * @author syounan * @date 1 Sep 2010 * @note copied from FLO glue implementatuion * @version 1.0 */ WILC_ErrNo WILC_MsgQueueRecv(WILC_MsgQueueHandle *pHandle, void *pvRecvBuffer, WILC_Uint32 u32RecvBufferSize, WILC_Uint32 *pu32ReceivedLength, tstrWILC_MsgQueueAttrs *pstrAttrs) { Message *pstrMessage; WILC_ErrNo s32RetStatus = WILC_SUCCESS; tstrWILC_SemaphoreAttrs strSemAttrs; unsigned long flags; if ((pHandle == NULL) || (u32RecvBufferSize == 0) || (pvRecvBuffer == NULL) || (pu32ReceivedLength == NULL)) { WILC_ERRORREPORT(s32RetStatus, WILC_INVALID_ARGUMENT); } if (pHandle->bExiting == WILC_TRUE) { WILC_ERRORREPORT(s32RetStatus, WILC_FAIL); } spin_lock_irqsave(&pHandle->strCriticalSection, flags); pHandle->u32ReceiversCount++; spin_unlock_irqrestore(&pHandle->strCriticalSection, flags); WILC_SemaphoreFillDefault(&strSemAttrs); #ifdef CONFIG_WILC_MSG_QUEUE_TIMEOUT if (pstrAttrs != WILC_NULL) { strSemAttrs.u32TimeOut = pstrAttrs->u32Timeout; } #endif s32RetStatus = WILC_SemaphoreAcquire(&(pHandle->hSem), &strSemAttrs); if (s32RetStatus == WILC_TIMEOUT) { /* timed out, just exit without consumeing the message */ spin_lock_irqsave(&pHandle->strCriticalSection, flags); pHandle->u32ReceiversCount--; spin_unlock_irqrestore(&pHandle->strCriticalSection, flags); } else { /* other non-timeout scenarios */ WILC_ERRORCHECK(s32RetStatus); if (pHandle->bExiting) { WILC_ERRORREPORT(s32RetStatus, WILC_FAIL); } spin_lock_irqsave(&pHandle->strCriticalSection, flags); pstrMessage = pHandle->pstrMessageList; if (pstrMessage == NULL) { spin_unlock_irqrestore(&pHandle->strCriticalSection, flags); WILC_ERRORREPORT(s32RetStatus, WILC_FAIL); } /* check buffer size */ if (u32RecvBufferSize < pstrMessage->u32Length) { spin_unlock_irqrestore(&pHandle->strCriticalSection, flags); WILC_SemaphoreRelease(&pHandle->hSem, WILC_NULL); WILC_ERRORREPORT(s32RetStatus, WILC_BUFFER_OVERFLOW); } /* consume the message */ pHandle->u32ReceiversCount--; WILC_memcpy(pvRecvBuffer, pstrMessage->pvBuffer, pstrMessage->u32Length); *pu32ReceivedLength = pstrMessage->u32Length; pHandle->pstrMessageList = pstrMessage->pstrNext; WILC_FREE(pstrMessage->pvBuffer); WILC_FREE(pstrMessage); spin_unlock_irqrestore(&pHandle->strCriticalSection, flags); } WILC_CATCH(s32RetStatus) { } return s32RetStatus; }