Hi.
My application is an ethernet to UART converter with 3 ports.
It runs 4 TCP listening tasks (the 4th task is for configuration) that send received data to UART ports (there are 4 additional UART listening tasks that send received data to connected TCP ports).
All TCP tasks run with the same code (the difference is the parameter which defines to which port to listen), here it is:
void TcpTask(void *param)
{
int pn = (int) param;
int port;
struct freertos_sockaddr xClient, xBindAddress;
socklen_t xSize = sizeof( xClient );
Socket_t xConnectedSocket = 0, xListeningSocket = 0;
BaseType_t lBytesReceived;
BaseType_t xSocketReuse = pdTRUE;
Buffer *pbufUartTx, *pbufTcpRx;
port=pn;
pbufUartTx = &bufUartTX[port];
pbufTcpRx = &bufTcpRX[port];
while(1)
{
if(xListeningSocket == 0)
{
xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET4, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP );
if( xListeningSocket == FREERTOS_INVALID_SOCKET )
{
xListeningSocket = 0;
vTaskDelay(100);
continue;
}
FreeRTOS_setsockopt(xListeningSocket, 0, FREERTOS_SO_REUSE_LISTEN_SOCKET, &xSocketReuse, sizeof(xSocketReuse));
memset( &xBindAddress, 0, sizeof(xBindAddress) );
xBindAddress.sin_port = ( uint16_t ) TCP_PORT_BASE + pn;
xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
xBindAddress.sin_family = FREERTOS_AF_INET4;
FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
FreeRTOS_listen( xListeningSocket, 1 );
}
xConnectedSocket = FreeRTOS_accept( xListeningSocket, &xClient, &xSize );
if((xConnectedSocket != NULL) && (xConnectedSocket != FREERTOS_INVALID_SOCKET))
{
xServerSocket[port] = xConnectedSocket;
while(xConnectedSocket)
{
lBytesReceived = FreeRTOS_recv( xConnectedSocket, pbufTcpRx->buf, BUFFER_SIZE, 0 );
if( lBytesReceived > 0 )
{
/* Data was received, process it here. */
/* Check whether the previous packet was sent */
if(pbufUartTx->count == 0)
{
pbufUartTx->count = lBytesReceived;
memcpy(pbufUartTx->buf, pbufTcpRx->buf, lBytesReceived);
if(port != PORT_3) UartSendData(port, pbufUartTx->buf, lBytesReceived);
else
{
PerformGetSetConfig(pbufUartTx->buf, PORT_3, 0, lBytesReceived);
Led3 = LED_OFF;
pbufUartTx->count = 0;
}
}
}
else if( lBytesReceived == 0 )
{
/* No data was received, but FreeRTOS\_recv() did not return an error.
Timeout? */
}
else
{
/* Error (maybe the connected socket already shut down the socket?).
Attempt graceful shutdown. */
FreeRTOS_shutdown( xConnectedSocket, FREERTOS_SHUT_RDWR );
FreeRTOS_closesocket( xConnectedSocket );
xConnectedSocket = 0;
xListeningSocket = 0;
xServerSocket[port] = 0;
break;
}
}
}
}
}
My problem is as following:
when the program runs, an outside application connects to port #2 and transfers data to and from UART #2. Then I use Putty and connect to port #3 (which is a configuration port, it is not related to any UART). As long as I send “wrong” data through this port (the port doesn’t reply to wrong data), nothing happens. But the moment I send a recognized command (like - get version), I get the right reply, but then the whole device stops replying to ping and to the 2 previously connected ports.
Here is what I see at +TCP debug printout:
10:02:13.599 --> .FreeRTOS_AddEndPoint: MAC: 48-5c IPv4: ac1601e9ip
10:02:17.224 --> FreeRTOS_NetworkDown is called
10:02:17.224 --> xPhyReset: phyBMCR_RESET 0 ready
10:02:17.308 --> +TCP: advertise: 0101 config 3100
10:02:17.308 --> prvEthernetUpdateConfig: LS mask 00 Force 1
10:02:17.308 --> xSTM32F_NetworkInterfaceInitialise returns 0
10:02:18.774 --> xPhyCheckLinkStatus: PHY LS now 01
10:02:18.774 --> prvEMACHandlerTask LS has changed
10:02:18.774 --> prvEthernetUpdateConfig: LS mask 01 Force 0
10:02:18.843 --> Network buffers: 59 lowest 59
10:02:18.843 --> Heap: current 6464 lowest 6464
10:02:20.331 --> FreeRTOS_NetworkDown is called
10:02:20.331 --> Link Status is high
10:02:20.331 --> xSTM32F_NetworkInterfaceInitialise returns 1
10:02:20.331 --> Heap: current 4912 lowest 4912
10:02:20.394 --> Heap: current 3280 lowest 3280
10:02:21.518 --> Heap: current 4160 lowest 2608
10:02:23.625 --> Heap: current 1056 lowest 1056
10:02:41.531 --> Network buffers: 57 lowest 57
10:02:41.531 --> Network buffers: 57 lowest 56
/* 30 seconds after stop replying to ping */
10:03:50.015 --> vTCPStateChange: Closing (Queued 0, Accept 0 Reuse 1)
10:03:50.015 --> vTCPStateChange: me 2000edd8 parent 2000edd8 peer 00000000 clear 0
10:03:50.031 --> vTCPStateChange: xHasCleared = 0
/* 3 minutes after */
10:06:34.875 --> vTCPStateChange: Closing (Queued 0, Accept 0 Reuse 1)
10:06:34.875 --> vTCPStateChange: me 2000ef48 parent 2000ef48 peer 00000000 clear 0
10:06:34.906 --> vTCPStateChange: xHasCleared = 0
It takes the +TCP 3 minutes to start replying again (after it notifies of closing the second port).
Can anyone help solve this issue?
Thanks.