Skip to content

Commit

Permalink
Update FreeRTOS_get_tx_head to create TX stream if not created already (
Browse files Browse the repository at this point in the history
#1023)

* TCP zero copy update FreeRTOS_get_tx_head()

* fix unit tests

* Uncrustify: triggered by comment

* updating with review feedback

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
tony-josi-aws and actions-user authored Sep 13, 2023
1 parent b3289a7 commit 5e55153
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
13 changes: 10 additions & 3 deletions source/FreeRTOS_Sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -4372,11 +4372,11 @@ void vSocketWakeUpUser( FreeRTOS_Socket_t * pxSocket )
* @return Head of the circular transmit buffer if all checks pass. Or else, NULL
* is returned.
*/
uint8_t * FreeRTOS_get_tx_head( ConstSocket_t xSocket,
uint8_t * FreeRTOS_get_tx_head( Socket_t xSocket,
BaseType_t * pxLength )
{
uint8_t * pucReturn = NULL;
const FreeRTOS_Socket_t * pxSocket = ( const FreeRTOS_Socket_t * ) xSocket;
FreeRTOS_Socket_t * pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
StreamBuffer_t * pxBuffer = NULL;

*pxLength = 0;
Expand All @@ -4387,6 +4387,13 @@ void vSocketWakeUpUser( FreeRTOS_Socket_t * pxSocket )
{
pxBuffer = pxSocket->u.xTCP.txStream;

if( ( pxBuffer == NULL ) && ( pxSocket->u.xTCP.bits.bMallocError != pdTRUE_UNSIGNED ) )
{
/* Create the outgoing stream only when it is needed */
( void ) prvTCPCreateStream( pxSocket, pdFALSE );
pxBuffer = pxSocket->u.xTCP.txStream;
}

if( pxBuffer != NULL )
{
size_t uxSpace = uxStreamBufferGetSpace( pxBuffer );
Expand Down Expand Up @@ -5050,7 +5057,7 @@ void vSocketWakeUpUser( FreeRTOS_Socket_t * pxSocket )
if( pxBuffer == NULL )
{
FreeRTOS_debug_printf( ( "prvTCPCreateStream: malloc failed\n" ) );
pxSocket->u.xTCP.bits.bMallocError = pdTRUE;
pxSocket->u.xTCP.bits.bMallocError = pdTRUE_UNSIGNED;
vTCPStateChange( pxSocket, eCLOSE_WAIT );
}
else
Expand Down
2 changes: 1 addition & 1 deletion source/include/FreeRTOS_Sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@
/* For advanced applications only:
* Get a direct pointer to the circular transmit buffer.
* '*pxLength' will contain the number of bytes that may be written. */
uint8_t * FreeRTOS_get_tx_head( ConstSocket_t xSocket,
uint8_t * FreeRTOS_get_tx_head( Socket_t xSocket,
BaseType_t * pxLength );

/* For the web server: borrow the circular Rx buffer for inspection
Expand Down
39 changes: 38 additions & 1 deletion test/unit-test/FreeRTOS_Sockets/FreeRTOS_Sockets_TCP_API_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -794,11 +794,48 @@ void test_FreeRTOS_get_tx_head_InvalidParams( void )
/* NULL socket. */
pucReturn = FreeRTOS_get_tx_head( NULL, &xLength );
TEST_ASSERT_EQUAL( NULL, pucReturn );
}

/**
* @brief Socket with stream not yet created is passed to the function.
*/
void test_FreeRTOS_get_tx_head_NoStream( void )
{
uint8_t * pucReturn;
FreeRTOS_Socket_t xSocket;
BaseType_t xLength;
uint8_t ucStream[ ipconfigTCP_MSS ];
const size_t uxRemainingSize = 5;

memset( &xSocket, 0, sizeof( xSocket ) );
memset( ucStream, 0, ipconfigTCP_MSS );

/* NULL stream. */
xSocket.ucProtocol = FREERTOS_IPPROTO_TCP;
pvPortMalloc_ExpectAnyArgsAndReturn( ucStream );
uxStreamBufferGetSpace_ExpectAndReturn( ( StreamBuffer_t * ) ucStream, uxRemainingSize );
pucReturn = FreeRTOS_get_tx_head( &xSocket, &xLength );
TEST_ASSERT_EQUAL( NULL, pucReturn );
TEST_ASSERT_EQUAL_PTR( &( ( ( StreamBuffer_t * ) ucStream )->ucArray[ 0 ] ), pucReturn );
TEST_ASSERT_EQUAL( uxRemainingSize, xLength );
}

/**
* @brief Socket with stream not created but malloc failed previously.
*/
void test_FreeRTOS_get_tx_head_NoStreamMallocError( void )
{
uint8_t * pucReturn;
FreeRTOS_Socket_t xSocket;
BaseType_t xLength;

memset( &xSocket, 0, sizeof( xSocket ) );

/* NULL stream. */
xSocket.ucProtocol = FREERTOS_IPPROTO_TCP;
xSocket.u.xTCP.bits.bMallocError = pdTRUE_UNSIGNED;
pucReturn = FreeRTOS_get_tx_head( &xSocket, &xLength );
TEST_ASSERT_EQUAL_PTR( NULL, pucReturn );
TEST_ASSERT_EQUAL( 0, xLength );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2296,14 +2296,14 @@ void test_prvTCPSendCheck_InvalidValues( void )
TEST_ASSERT_EQUAL( -pdFREERTOS_ERRNO_EINVAL, lReturn );

/* No memory. */
xSocket.u.xTCP.bits.bMallocError = pdTRUE;
xSocket.u.xTCP.bits.bMallocError = pdTRUE_UNSIGNED;
xSocket.ucProtocol = FREERTOS_IPPROTO_TCP;
listLIST_ITEM_CONTAINER_ExpectAnyArgsAndReturn( &xBoundTCPSocketsList );
lReturn = prvTCPSendCheck( &xSocket, uxDataLength );
TEST_ASSERT_EQUAL( -pdFREERTOS_ERRNO_ENOMEM, lReturn );

/* Invalid states. */
xSocket.u.xTCP.bits.bMallocError = pdFALSE;
xSocket.u.xTCP.bits.bMallocError = pdFALSE_UNSIGNED;
xSocket.ucProtocol = FREERTOS_IPPROTO_TCP;

for( unsigned int i = 0; i < sizeof( array ) / sizeof( eIPTCPState_t ); i++ )
Expand Down

0 comments on commit 5e55153

Please sign in to comment.