Skip to content

Commit d700359

Browse files
htiboschHein TiboschAniruddhaKanhereamazonKamath
authored
IPv4/Single: Let send() stop blocking after a connection reset (#561)
* IPv4/Single: Let send() stop after a protocol error * Remove token need * Repaired unit-testing * Added the cunftion test_FreeRTOS_send_DisconnectionOccursDuringWait() * Added a comment for unit-test function test_FreeRTOS_send_DisconnectionOccursDuringWait() * Added an item to lexicon.txt * Restored original tcp_utilities * Restored original tcp_utilities, once more --------- Co-authored-by: Hein Tibosch <[email protected]> Co-authored-by: Aniruddha Kanhere <[email protected]> Co-authored-by: Nikhil Kamath <[email protected]>
1 parent f7e07f8 commit d700359

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

.github/lexicon.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,7 @@ prvtcpprepareconnect
720720
prvtcppreparesend
721721
prvtcpreturnpacket
722722
prvtcpsendchallengeack
723+
prvtcpsendcheck
723724
prvtcpsendpacket
724725
prvtcpsendrepeated
725726
prvtcpsendreset

source/FreeRTOS_Sockets.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3826,8 +3826,16 @@ void vSocketWakeUpUser( FreeRTOS_Socket_t * pxSocket )
38263826
( void ) xEventGroupWaitBits( pxSocket->xEventGroup, ( EventBits_t ) eSOCKET_SEND | ( EventBits_t ) eSOCKET_CLOSED,
38273827
pdTRUE /*xClearOnExit*/, pdFALSE /*xWaitAllBits*/, xRemainingTime );
38283828

3829+
xByteCount = ( BaseType_t ) prvTCPSendCheck( pxSocket, uxDataLength );
3830+
3831+
if( xByteCount < 0 )
3832+
{
3833+
/* In a meanwhile, the connection has dropped, stop iterating. */
3834+
break;
3835+
}
3836+
38293837
xByteCount = ( BaseType_t ) uxStreamBufferGetSpace( pxSocket->u.xTCP.txStream );
3830-
}
3838+
} /* while( ipTRUE_BOOL ) */
38313839

38323840
/* How much was actually sent? */
38333841
xByteCount = ( ( BaseType_t ) uxDataLength ) - xBytesLeft;

test/unit-test/FreeRTOS_Sockets/FreeRTOS_Sockets_TCP_API_utest.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ void test_FreeRTOS_send_LessSpaceInStreamBuffer_Timeout( void )
984984
xIsCallingFromIPTask_ExpectAndReturn( pdFALSE );
985985
vTaskSetTimeOutState_ExpectAnyArgs();
986986
xEventGroupWaitBits_ExpectAndReturn( xSocket.xEventGroup, eSOCKET_SEND | eSOCKET_CLOSED, pdTRUE, pdFALSE, 100, pdFALSE );
987+
listLIST_ITEM_CONTAINER_ExpectAnyArgsAndReturn( &xBoundTCPSocketsList );
987988

988989
/* Second Iteration. No space still. */
989990
uxStreamBufferGetSpace_ExpectAndReturn( xSocket.u.xTCP.txStream, 0 );
@@ -1026,6 +1027,7 @@ void test_FreeRTOS_send_LessSpaceInStreamBuffer_EventuallySpaceAvailable( void )
10261027
xIsCallingFromIPTask_ExpectAndReturn( pdFALSE );
10271028
vTaskSetTimeOutState_ExpectAnyArgs();
10281029
xEventGroupWaitBits_ExpectAndReturn( xSocket.xEventGroup, eSOCKET_SEND | eSOCKET_CLOSED, pdTRUE, pdFALSE, 100, pdFALSE );
1030+
listLIST_ITEM_CONTAINER_ExpectAnyArgsAndReturn( &xBoundTCPSocketsList );
10291031

10301032
/* Second Iteration. */
10311033
uxStreamBufferGetSpace_ExpectAndReturn( xSocket.u.xTCP.txStream, 20 );
@@ -1068,6 +1070,7 @@ void test_FreeRTOS_send_MultipleIterationsAndNoSuccess( void )
10681070
xIsCallingFromIPTask_ExpectAndReturn( pdFALSE );
10691071
vTaskSetTimeOutState_ExpectAnyArgs();
10701072
xEventGroupWaitBits_ExpectAndReturn( xSocket.xEventGroup, eSOCKET_SEND | eSOCKET_CLOSED, pdTRUE, pdFALSE, 100, pdFALSE );
1073+
listLIST_ITEM_CONTAINER_ExpectAnyArgsAndReturn( &xBoundTCPSocketsList );
10711074

10721075
/* Second Iteration. */
10731076
uxStreamBufferGetSpace_ExpectAndReturn( xSocket.u.xTCP.txStream, 10 );
@@ -1078,6 +1081,7 @@ void test_FreeRTOS_send_MultipleIterationsAndNoSuccess( void )
10781081
xTaskCheckForTimeOut_ExpectAnyArgsAndReturn( pdFALSE );
10791082

10801083
xEventGroupWaitBits_ExpectAndReturn( xSocket.xEventGroup, eSOCKET_SEND | eSOCKET_CLOSED, pdTRUE, pdFALSE, 100, pdFALSE );
1084+
listLIST_ITEM_CONTAINER_ExpectAnyArgsAndReturn( &xBoundTCPSocketsList );
10811085

10821086
/* Third iteration. No space still. */
10831087
uxStreamBufferGetSpace_ExpectAndReturn( xSocket.u.xTCP.txStream, 0 );
@@ -1089,6 +1093,46 @@ void test_FreeRTOS_send_MultipleIterationsAndNoSuccess( void )
10891093
TEST_ASSERT_EQUAL( uxDataLength - 10, xReturn );
10901094
}
10911095

1096+
/*
1097+
* @brief While waiting for space, the socket gets disconnected.
1098+
*/
1099+
void test_FreeRTOS_send_DisconnectionOccursDuringWait( void )
1100+
{
1101+
BaseType_t xReturn;
1102+
FreeRTOS_Socket_t xSocket;
1103+
uint8_t pvBuffer[ ipconfigTCP_MSS ];
1104+
size_t uxDataLength;
1105+
BaseType_t xFlags = 0;
1106+
StreamBuffer_t xLocalStreamBuffer;
1107+
1108+
memset( &xSocket, 0, sizeof( xSocket ) );
1109+
memset( pvBuffer, 0, ipconfigTCP_MSS );
1110+
1111+
xSocket.ucProtocol = FREERTOS_IPPROTO_TCP;
1112+
xSocket.u.xTCP.eTCPState = eESTABLISHED;
1113+
xSocket.u.xTCP.bits.bFinSent = pdFALSE_UNSIGNED;
1114+
xSocket.u.xTCP.txStream = &xLocalStreamBuffer;
1115+
xSocket.xSendBlockTime = 100;
1116+
1117+
uxDataLength = 100;
1118+
listLIST_ITEM_CONTAINER_ExpectAnyArgsAndReturn( &xBoundTCPSocketsList );
1119+
uxStreamBufferGetSpace_ExpectAndReturn( xSocket.u.xTCP.txStream, uxDataLength - 20 );
1120+
uxStreamBufferAdd_ExpectAndReturn( xSocket.u.xTCP.txStream, 0U, pvBuffer, uxDataLength - 20, uxDataLength - 20 );
1121+
xIsCallingFromIPTask_ExpectAndReturn( pdFALSE );
1122+
xSendEventToIPTask_ExpectAndReturn( eTCPTimerEvent, pdFALSE );
1123+
xIsCallingFromIPTask_ExpectAndReturn( pdFALSE );
1124+
vTaskSetTimeOutState_ExpectAnyArgs();
1125+
xEventGroupWaitBits_ExpectAndReturn( xSocket.xEventGroup, eSOCKET_SEND | eSOCKET_CLOSED, pdTRUE, pdFALSE, 100, pdFALSE );
1126+
1127+
/* Let `socketSOCKET_IS_BOUND()` return false, so that prvTCPSendCheck()
1128+
* returns en error, so that the loop is stopped. */
1129+
listLIST_ITEM_CONTAINER_ExpectAnyArgsAndReturn( NULL );
1130+
1131+
xReturn = FreeRTOS_send( &xSocket, pvBuffer, uxDataLength, xFlags );
1132+
1133+
TEST_ASSERT_EQUAL( uxDataLength - 20, xReturn );
1134+
}
1135+
10921136
/*
10931137
* @brief IP task is calling send function with a NULL buffer. Also there are 20 bytes worth of space
10941138
* less in the stream buffer as the data length.

0 commit comments

Comments
 (0)