in lib/FreeRTOS/network_transport/freertos_plus_tcp/sockets_wrapper.c [47:138]
BaseType_t Sockets_Connect( Socket_t * pTcpSocket,
const char * pHostName,
uint16_t port,
uint32_t receiveTimeoutMs,
uint32_t sendTimeoutMs )
{
Socket_t tcpSocket = FREERTOS_INVALID_SOCKET;
BaseType_t socketStatus = 0;
struct freertos_sockaddr serverAddress = { 0 };
TickType_t transportTimeout = 0;
/* Create a new TCP socket. */
tcpSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP );
if( tcpSocket == FREERTOS_INVALID_SOCKET )
{
LogError( ( "Failed to create new socket." ) );
socketStatus = FREERTOS_SOCKETS_WRAPPER_NETWORK_ERROR;
}
else
{
LogDebug( ( "Created new TCP socket." ) );
/* Connection parameters. */
serverAddress.sin_family = FREERTOS_AF_INET;
serverAddress.sin_port = FreeRTOS_htons( port );
serverAddress.sin_addr = ( uint32_t ) FreeRTOS_gethostbyname( pHostName );
serverAddress.sin_len = ( uint8_t ) sizeof( serverAddress );
/* Check for errors from DNS lookup. */
if( serverAddress.sin_addr == 0U )
{
LogError( ( "Failed to connect to server: DNS resolution failed: Hostname=%s.",
pHostName ) );
socketStatus = FREERTOS_SOCKETS_WRAPPER_NETWORK_ERROR;
}
}
if( socketStatus == 0 )
{
/* Establish connection. */
LogDebug( ( "Creating TCP Connection to %s.", pHostName ) );
socketStatus = FreeRTOS_connect( tcpSocket, &serverAddress, sizeof( serverAddress ) );
if( socketStatus != 0 )
{
LogError( ( "Failed to connect to server: FreeRTOS_Connect failed: ReturnCode=%d,"
" Hostname=%s, Port=%u.",
socketStatus,
pHostName,
port ) );
}
}
if( socketStatus == 0 )
{
/* Set socket receive timeout. */
transportTimeout = pdMS_TO_TICKS( receiveTimeoutMs );
/* Setting the receive block time cannot fail. */
( void ) FreeRTOS_setsockopt( tcpSocket,
0,
FREERTOS_SO_RCVTIMEO,
&transportTimeout,
sizeof( TickType_t ) );
/* Set socket send timeout. */
transportTimeout = pdMS_TO_TICKS( sendTimeoutMs );
/* Setting the send block time cannot fail. */
( void ) FreeRTOS_setsockopt( tcpSocket,
0,
FREERTOS_SO_SNDTIMEO,
&transportTimeout,
sizeof( TickType_t ) );
}
/* Clean up on failure. */
if( socketStatus != 0 )
{
if( tcpSocket != FREERTOS_INVALID_SOCKET )
{
( void ) FreeRTOS_closesocket( tcpSocket );
}
}
else
{
/* Set the socket. */
*pTcpSocket = tcpSocket;
LogInfo( ( "Established TCP connection with %s.", pHostName ) );
}
return socketStatus;
}