Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update FreeRTOS_get_tx_head to create TX stream if not created already #1023

Merged
merged 4 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ) )
{
tony-josi-aws marked this conversation as resolved.
Show resolved Hide resolved
/* 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
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