Esempio n. 1
0
/*
 * lStreamBufferAdd( )
 * Adds data to a stream buffer.  If lOffset > 0, data will be written at
 * an offset from lHead while lHead will not be moved yet.  This possibility
 * will be used when TCP data is received while earlier data is still missing.
 * If 'pucData' equals NULL, the function is called to advance 'lHead' only.
 */
int32_t lStreamBufferAdd( xStreamBuffer *pxBuffer, int32_t lOffset, const uint8_t *pucData, int32_t lCount )
{
int32_t lSpace, lNextHead, lFirst;

	lSpace = lStreamBufferGetSpace( pxBuffer );

	/* If lOffset > 0, items can be placed in front of lHead */
	if( lSpace > lOffset )
	{
		lSpace -= lOffset;
	}
	else
	{
		lSpace = 0;
	}

	/* The number of bytes that can be written is the minimum of the number of
	bytes requested and the number available. */
	lCount = FreeRTOS_min_int32( lSpace, lCount );

	if( lCount != 0 )
	{
		lNextHead = pxBuffer->lHead;

		if( lOffset != 0 )
		{
			/* ( lOffset > 0 ) means: write in front if the lHead marker */
			lNextHead += lOffset;
			if( lNextHead >= pxBuffer->LENGTH )
			{
				lNextHead -= pxBuffer->LENGTH;
			}
		}

		if( pucData != NULL )
		{
			/* Calculate the number of bytes that can be added in the first
			write - which may be less than the total number of bytes that need
			to be added if the buffer will wrap back to the beginning. */
			lFirst = FreeRTOS_min_int32( pxBuffer->LENGTH - lNextHead, lCount );

			/* Write as many bytes as can be written in the first write. */
			memcpy( ( void* ) ( pxBuffer->ucArray + lNextHead ), pucData, ( size_t ) lFirst );

			/* If the number of bytes written was less than the number that
			could be written in the first write... */
			if( lCount > lFirst )
			{
				/* ...then write the remaining bytes to the start of the
				buffer. */
				memcpy( ( void * )pxBuffer->ucArray, pucData + lFirst, ( size_t )( lCount - lFirst ) );
			}
		}

		if( lOffset == 0 )
		{
			/* ( lOffset == 0 ) means: write at lHead position */
			lNextHead += lCount;
			if( lNextHead >= pxBuffer->LENGTH )
			{
				lNextHead -= pxBuffer->LENGTH;
			}
			pxBuffer->lHead = lNextHead;
		}

		if( xStreamBufferLessThenEqual( pxBuffer, pxBuffer->lFront, lNextHead ) != pdFALSE )
		{
			/* Advance the front pointer */
			pxBuffer->lFront = lNextHead;
		}
	}

	return lCount;
}
Esempio n. 2
0
/*
 * uxStreamBufferAdd( )
 * Adds data to a stream buffer.  If uxOffset > 0, data will be written at
 * an offset from uxHead while uxHead will not be moved yet.  This possibility
 * will be used when TCP data is received while earlier data is still missing.
 * If 'pucData' equals NULL, the function is called to advance 'uxHead' only.
 */
size_t uxStreamBufferAdd( StreamBuffer_t *pxBuffer, size_t uxOffset, const uint8_t *pucData, size_t uxCount )
{
size_t uxSpace, uxNextHead, uxFirst;

	uxSpace = uxStreamBufferGetSpace( pxBuffer );

	/* If uxOffset > 0, items can be placed in front of uxHead */
	if( uxSpace > uxOffset )
	{
		uxSpace -= uxOffset;
	}
	else
	{
		uxSpace = 0u;
	}

	/* The number of bytes that can be written is the minimum of the number of
	bytes requested and the number available. */
	uxCount = FreeRTOS_min_uint32( uxSpace, uxCount );

	if( uxCount != 0u )
	{
		uxNextHead = pxBuffer->uxHead;

		if( uxOffset != 0u )
		{
			/* ( uxOffset > 0 ) means: write in front if the uxHead marker */
			uxNextHead += uxOffset;
			if( uxNextHead >= pxBuffer->LENGTH )
			{
				uxNextHead -= pxBuffer->LENGTH;
			}
		}

		if( pucData != NULL )
		{
			/* Calculate the number of bytes that can be added in the first
			write - which may be less than the total number of bytes that need
			to be added if the buffer will wrap back to the beginning. */
			uxFirst = FreeRTOS_min_uint32( pxBuffer->LENGTH - uxNextHead, uxCount );

			/* Write as many bytes as can be written in the first write. */
			memcpy( ( void* ) ( pxBuffer->ucArray + uxNextHead ), pucData, uxFirst );

			/* If the number of bytes written was less than the number that
			could be written in the first write... */
			if( uxCount > uxFirst )
			{
				/* ...then write the remaining bytes to the start of the
				buffer. */
				memcpy( ( void * )pxBuffer->ucArray, pucData + uxFirst, uxCount - uxFirst );
			}
		}

		if( uxOffset == 0u )
		{
			/* ( uxOffset == 0 ) means: write at uxHead position */
			uxNextHead += uxCount;
			if( uxNextHead >= pxBuffer->LENGTH )
			{
				uxNextHead -= pxBuffer->LENGTH;
			}
			pxBuffer->uxHead = uxNextHead;
		}

		if( xStreamBufferLessThenEqual( pxBuffer, pxBuffer->uxFront, uxNextHead ) != pdFALSE )
		{
			/* Advance the front pointer */
			pxBuffer->uxFront = uxNextHead;
		}
	}

	return uxCount;
}