Skip to content

Commit d3ce35f

Browse files
htiboschHein Tibosch
andauthored
New helper function: FreeRTOS_get_tx_base (FreeRTOS#544)
* IPv4/single: new function: FreeRTOS_get_tx_base * Changed some code comments and repaired a typo. * Attempt to repair utest * Changes after CI checks * utest: Added tests for get_tx_base * Do not use const socket type in FreeRTOS_get_tx_base() * Removed comments from cmake file ut * Repaired UT * Removed a nested if/endif couple --------- Co-authored-by: Hein Tibosch <[email protected]>
1 parent ce11071 commit d3ce35f

File tree

4 files changed

+134
-8
lines changed

4 files changed

+134
-8
lines changed

source/FreeRTOS_Sockets.c

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4364,7 +4364,49 @@ void vSocketWakeUpUser( FreeRTOS_Socket_t * pxSocket )
43644364
#if ( ipconfigUSE_TCP == 1 )
43654365

43664366
/**
4367-
* @brief Get a direct pointer to the circular transmit buffer.
4367+
* @brief Get a direct pointer to the beginning of the circular transmit buffer.
4368+
*
4369+
* @param[in] xSocket: The socket owning the buffer.
4370+
*
4371+
* @return Address the first byte in the circular transmit buffer if all checks pass.
4372+
* Or else, NULL is returned.
4373+
*/
4374+
uint8_t * FreeRTOS_get_tx_base( Socket_t xSocket )
4375+
{
4376+
uint8_t * pucReturn = NULL;
4377+
FreeRTOS_Socket_t * pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
4378+
4379+
/* Confirm that this is a TCP socket before dereferencing structure
4380+
* member pointers. */
4381+
if( prvValidSocket( pxSocket, FREERTOS_IPPROTO_TCP, pdFALSE ) == pdTRUE )
4382+
{
4383+
StreamBuffer_t * pxBuffer = pxSocket->u.xTCP.txStream;
4384+
4385+
/* If the TX buffer hasn't been created yet,
4386+
* and if no malloc error has occurred on this socket yet. */
4387+
if( ( pxBuffer == NULL ) &&
4388+
( pxSocket->u.xTCP.bits.bMallocError == pdFALSE_UNSIGNED ) )
4389+
{
4390+
/* Create the outgoing stream only when it is needed */
4391+
( void ) prvTCPCreateStream( pxSocket, pdFALSE );
4392+
pxBuffer = pxSocket->u.xTCP.txStream;
4393+
}
4394+
4395+
if( pxBuffer != NULL )
4396+
{
4397+
pucReturn = pxBuffer->ucArray;
4398+
}
4399+
}
4400+
4401+
return pucReturn;
4402+
}
4403+
#endif /* ipconfigUSE_TCP */
4404+
/*-----------------------------------------------------------*/
4405+
4406+
#if ( ipconfigUSE_TCP == 1 )
4407+
4408+
/**
4409+
* @brief Get a direct pointer to the TX head of the circular transmit buffer.
43684410
*
43694411
* @param[in] xSocket The socket owning the buffer.
43704412
* @param[in] pxLength This will contain the number of bytes that may be written.
@@ -4387,7 +4429,10 @@ void vSocketWakeUpUser( FreeRTOS_Socket_t * pxSocket )
43874429
{
43884430
pxBuffer = pxSocket->u.xTCP.txStream;
43894431

4390-
if( ( pxBuffer == NULL ) && ( pxSocket->u.xTCP.bits.bMallocError != pdTRUE_UNSIGNED ) )
4432+
/* If the TX buffer hasn't been created yet,
4433+
* and if no malloc error has occurred on this socket yet. */
4434+
if( ( pxBuffer == NULL ) &&
4435+
( pxSocket->u.xTCP.bits.bMallocError == pdFALSE_UNSIGNED ) )
43914436
{
43924437
/* Create the outgoing stream only when it is needed */
43934438
( void ) prvTCPCreateStream( pxSocket, pdFALSE );

source/include/FreeRTOS_Sockets.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,15 +325,12 @@
325325
BaseType_t FreeRTOS_shutdown( Socket_t xSocket,
326326
BaseType_t xHow );
327327

328-
#if ( ipconfigUSE_TCP == 1 )
329-
330328
/* Release a TCP payload buffer that was obtained by
331329
* calling FreeRTOS_recv() with the FREERTOS_ZERO_COPY flag,
332330
* and a pointer to a void pointer. */
333-
BaseType_t FreeRTOS_ReleaseTCPPayloadBuffer( Socket_t xSocket,
334-
void const * pvBuffer,
335-
BaseType_t xByteCount );
336-
#endif /* ( ipconfigUSE_TCP == 1 ) */
331+
BaseType_t FreeRTOS_ReleaseTCPPayloadBuffer( Socket_t xSocket,
332+
void const * pvBuffer,
333+
BaseType_t xByteCount );
337334

338335
/* Returns the number of bytes available in the Rx buffer. */
339336
BaseType_t FreeRTOS_rx_size( ConstSocket_t xSocket );
@@ -367,6 +364,13 @@
367364
/* For internal use only: return the connection status. */
368365
BaseType_t FreeRTOS_connstatus( ConstSocket_t xSocket );
369366

367+
/* For advanced applications only:
368+
* Get a direct pointer to the beginning of the circular transmit buffer.
369+
* In case the buffer was not yet created, it will be created in
370+
* this call.
371+
*/
372+
uint8_t * FreeRTOS_get_tx_base( Socket_t xSocket );
373+
370374
/* For advanced applications only:
371375
* Get a direct pointer to the circular transmit buffer.
372376
* '*pxLength' will contain the number of bytes that may be written. */

test/unit-test/FreeRTOS_Sockets/FreeRTOS_Sockets_TCP_API_utest.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,3 +1826,66 @@ void test_prvTCPSendLoop_NullBuffer()
18261826

18271827
TEST_ASSERT_EQUAL( uxDataLength, xReturn );
18281828
}
1829+
1830+
/**
1831+
* @brief Invalid parameters passed to the function.
1832+
*/
1833+
void test_FreeRTOS_get_tx_base_InvalidParams( void )
1834+
{
1835+
uint8_t * pucReturn;
1836+
FreeRTOS_Socket_t xSocket;
1837+
BaseType_t xLength;
1838+
size_t uxLength = 128;
1839+
size_t uxMallocSize;
1840+
StreamBuffer_t * pxBuffer;
1841+
1842+
memset( &xSocket, 0, sizeof( xSocket ) );
1843+
xSocket.u.xTCP.uxTxStreamSize = uxLength;
1844+
1845+
/* Invalid Protocol. */
1846+
pucReturn = FreeRTOS_get_tx_base( &xSocket );
1847+
TEST_ASSERT_EQUAL( NULL, pucReturn );
1848+
1849+
/* NULL socket. */
1850+
pucReturn = FreeRTOS_get_tx_base( NULL );
1851+
TEST_ASSERT_EQUAL( NULL, pucReturn );
1852+
1853+
/* FAIL: Memory Mismatch. Byte 0 Expected 0xB0 Was 0xE0. */
1854+
/* Function pvPortMalloc Argument xSize. Function called with unexpected argument value. */
1855+
1856+
/* Add an extra 4 (or 8) bytes. */
1857+
uxLength += sizeof( size_t );
1858+
1859+
/* And make the length a multiple of sizeof( size_t ). */
1860+
uxLength &= ~( sizeof( size_t ) - 1U );
1861+
1862+
uxMallocSize = ( sizeof( *pxBuffer ) + uxLength ) - sizeof( pxBuffer->ucArray );
1863+
1864+
pvPortMalloc_ExpectAndReturn( uxMallocSize, NULL );
1865+
1866+
vTCPStateChange_Expect( &xSocket, eCLOSE_WAIT );
1867+
1868+
/* NULL stream. */
1869+
xSocket.ucProtocol = FREERTOS_IPPROTO_TCP;
1870+
pucReturn = FreeRTOS_get_tx_base( &xSocket );
1871+
TEST_ASSERT_EQUAL( NULL, pucReturn );
1872+
}
1873+
1874+
/**
1875+
* @brief All fields of the socket are NULL.
1876+
*/
1877+
void test_FreeRTOS_get_tx_base_AllNULL( void )
1878+
{
1879+
uint8_t * pucReturn;
1880+
FreeRTOS_Socket_t xSocket;
1881+
uint8_t ucStream[ ipconfigTCP_MSS ];
1882+
1883+
memset( &xSocket, 0, sizeof( xSocket ) );
1884+
memset( ucStream, 0, ipconfigTCP_MSS );
1885+
1886+
xSocket.ucProtocol = FREERTOS_IPPROTO_TCP;
1887+
xSocket.u.xTCP.txStream = ( StreamBuffer_t * ) ucStream;
1888+
1889+
pucReturn = FreeRTOS_get_tx_base( &xSocket );
1890+
TEST_ASSERT_EQUAL_PTR( ( ( StreamBuffer_t * ) ucStream )->ucArray, pucReturn );
1891+
}

test/unit-test/FreeRTOS_Sockets/ut.cmake

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ list(APPEND mock_list
2626
"${MODULE_ROOT_DIR}/test/unit-test/${project_name}/Sockets_list_macros.h"
2727
)
2828

29+
# Without 'FreeRTOS_Sockets.h':
30+
#
31+
# 1 - FreeRTOS_Sockets_GenericAPI_utest (Not Run)
32+
# 2 - FreeRTOS_Sockets_TCP_API_utest (Not Run)
33+
# 3 - FreeRTOS_Sockets_UDP_API_utest (Not Run)
34+
# 4 - FreeRTOS_Sockets_privates_utest (Not Run)
35+
#
36+
# With 'FreeRTOS_Sockets.h':
37+
#
38+
# 3 - FreeRTOS_Sockets_UDP_API_utest (Failed)
39+
#
40+
# FAIL:Function FreeRTOS_recvfrom. Called more times than expected.
41+
# FAIL:Function FreeRTOS_sendto. Called more times than expected.
42+
2943
set(mock_include_list "")
3044
# list the directories your mocks need
3145
list(APPEND mock_include_list

0 commit comments

Comments
 (0)