Skip to content

Commit 5e55153

Browse files
Update FreeRTOS_get_tx_head to create TX stream if not created already (#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]>
1 parent b3289a7 commit 5e55153

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

source/FreeRTOS_Sockets.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -4372,11 +4372,11 @@ void vSocketWakeUpUser( FreeRTOS_Socket_t * pxSocket )
43724372
* @return Head of the circular transmit buffer if all checks pass. Or else, NULL
43734373
* is returned.
43744374
*/
4375-
uint8_t * FreeRTOS_get_tx_head( ConstSocket_t xSocket,
4375+
uint8_t * FreeRTOS_get_tx_head( Socket_t xSocket,
43764376
BaseType_t * pxLength )
43774377
{
43784378
uint8_t * pucReturn = NULL;
4379-
const FreeRTOS_Socket_t * pxSocket = ( const FreeRTOS_Socket_t * ) xSocket;
4379+
FreeRTOS_Socket_t * pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
43804380
StreamBuffer_t * pxBuffer = NULL;
43814381

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

4390+
if( ( pxBuffer == NULL ) && ( pxSocket->u.xTCP.bits.bMallocError != pdTRUE_UNSIGNED ) )
4391+
{
4392+
/* Create the outgoing stream only when it is needed */
4393+
( void ) prvTCPCreateStream( pxSocket, pdFALSE );
4394+
pxBuffer = pxSocket->u.xTCP.txStream;
4395+
}
4396+
43904397
if( pxBuffer != NULL )
43914398
{
43924399
size_t uxSpace = uxStreamBufferGetSpace( pxBuffer );
@@ -5050,7 +5057,7 @@ void vSocketWakeUpUser( FreeRTOS_Socket_t * pxSocket )
50505057
if( pxBuffer == NULL )
50515058
{
50525059
FreeRTOS_debug_printf( ( "prvTCPCreateStream: malloc failed\n" ) );
5053-
pxSocket->u.xTCP.bits.bMallocError = pdTRUE;
5060+
pxSocket->u.xTCP.bits.bMallocError = pdTRUE_UNSIGNED;
50545061
vTCPStateChange( pxSocket, eCLOSE_WAIT );
50555062
}
50565063
else

source/include/FreeRTOS_Sockets.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@
370370
/* For advanced applications only:
371371
* Get a direct pointer to the circular transmit buffer.
372372
* '*pxLength' will contain the number of bytes that may be written. */
373-
uint8_t * FreeRTOS_get_tx_head( ConstSocket_t xSocket,
373+
uint8_t * FreeRTOS_get_tx_head( Socket_t xSocket,
374374
BaseType_t * pxLength );
375375

376376
/* For the web server: borrow the circular Rx buffer for inspection

test/unit-test/FreeRTOS_Sockets/FreeRTOS_Sockets_TCP_API_utest.c

+38-1
Original file line numberDiff line numberDiff line change
@@ -794,11 +794,48 @@ void test_FreeRTOS_get_tx_head_InvalidParams( void )
794794
/* NULL socket. */
795795
pucReturn = FreeRTOS_get_tx_head( NULL, &xLength );
796796
TEST_ASSERT_EQUAL( NULL, pucReturn );
797+
}
798+
799+
/**
800+
* @brief Socket with stream not yet created is passed to the function.
801+
*/
802+
void test_FreeRTOS_get_tx_head_NoStream( void )
803+
{
804+
uint8_t * pucReturn;
805+
FreeRTOS_Socket_t xSocket;
806+
BaseType_t xLength;
807+
uint8_t ucStream[ ipconfigTCP_MSS ];
808+
const size_t uxRemainingSize = 5;
809+
810+
memset( &xSocket, 0, sizeof( xSocket ) );
811+
memset( ucStream, 0, ipconfigTCP_MSS );
797812

798813
/* NULL stream. */
799814
xSocket.ucProtocol = FREERTOS_IPPROTO_TCP;
815+
pvPortMalloc_ExpectAnyArgsAndReturn( ucStream );
816+
uxStreamBufferGetSpace_ExpectAndReturn( ( StreamBuffer_t * ) ucStream, uxRemainingSize );
800817
pucReturn = FreeRTOS_get_tx_head( &xSocket, &xLength );
801-
TEST_ASSERT_EQUAL( NULL, pucReturn );
818+
TEST_ASSERT_EQUAL_PTR( &( ( ( StreamBuffer_t * ) ucStream )->ucArray[ 0 ] ), pucReturn );
819+
TEST_ASSERT_EQUAL( uxRemainingSize, xLength );
820+
}
821+
822+
/**
823+
* @brief Socket with stream not created but malloc failed previously.
824+
*/
825+
void test_FreeRTOS_get_tx_head_NoStreamMallocError( void )
826+
{
827+
uint8_t * pucReturn;
828+
FreeRTOS_Socket_t xSocket;
829+
BaseType_t xLength;
830+
831+
memset( &xSocket, 0, sizeof( xSocket ) );
832+
833+
/* NULL stream. */
834+
xSocket.ucProtocol = FREERTOS_IPPROTO_TCP;
835+
xSocket.u.xTCP.bits.bMallocError = pdTRUE_UNSIGNED;
836+
pucReturn = FreeRTOS_get_tx_head( &xSocket, &xLength );
837+
TEST_ASSERT_EQUAL_PTR( NULL, pucReturn );
838+
TEST_ASSERT_EQUAL( 0, xLength );
802839
}
803840

804841
/**

test/unit-test/FreeRTOS_Sockets/FreeRTOS_Sockets_privates_utest.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2296,14 +2296,14 @@ void test_prvTCPSendCheck_InvalidValues( void )
22962296
TEST_ASSERT_EQUAL( -pdFREERTOS_ERRNO_EINVAL, lReturn );
22972297

22982298
/* No memory. */
2299-
xSocket.u.xTCP.bits.bMallocError = pdTRUE;
2299+
xSocket.u.xTCP.bits.bMallocError = pdTRUE_UNSIGNED;
23002300
xSocket.ucProtocol = FREERTOS_IPPROTO_TCP;
23012301
listLIST_ITEM_CONTAINER_ExpectAnyArgsAndReturn( &xBoundTCPSocketsList );
23022302
lReturn = prvTCPSendCheck( &xSocket, uxDataLength );
23032303
TEST_ASSERT_EQUAL( -pdFREERTOS_ERRNO_ENOMEM, lReturn );
23042304

23052305
/* Invalid states. */
2306-
xSocket.u.xTCP.bits.bMallocError = pdFALSE;
2306+
xSocket.u.xTCP.bits.bMallocError = pdFALSE_UNSIGNED;
23072307
xSocket.ucProtocol = FREERTOS_IPPROTO_TCP;
23082308

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

0 commit comments

Comments
 (0)