FreeRTOS_sockets.h
BaseType_t FreeRTOS_select( SocketSet_t xSocketSet, TickType_t xBlockTimeTicks );
Block on a "socket set" until an event of interest occurs on a socket
within the set. ipconfigSUPPORT_SELECT_FUNCTION must be set to 1 in
FreeRTOSIPConfig.h
for FreeRTOS_select() (and its associated functions) to be available.
Socket Sets allow an application RTOS task to block on multiple sockets
simultaneously.
To use a socket set:
-
Create a socket set by calling FreeRTOS_CreateSocketSet().
A socket set is equivalent to the Berkeley sockets fd_set type.
-
Add one or more sockets to the set using calls to FreeRTOS_FD_SET().
FreeRTOS_FD_SET() is equivalent to the Berkeley sockets FD_SET() macro.
-
Call FreeRTOS_Select() to test the
sockets in the set to see if any of the sockets have an event
pending.
-
If FreeRTOS_select() returns a non-zero value then check all
sockets in the set using a call to FreeRTOS_FD_ISSET()
to determine which events are pending.
A socket can only be a member of one set at any time.
The FreeRTOS_FD_CLR() API function removes a
socket from a set.
Parameters:
xSocketSet
|
The socket set being tested.
|
xBlockTimeTicks
|
The maximum time, in ticks, that the calling RTOS task will
remain in the Blocked state (with other tasks executing)
to wait for a member of the socket set to get an event.
|
Returns:
If xBlockTimeTicks expired before a socket in the socket set had an event,
then zero is returned. Otherwise a non-zero value is returned. All sockets which
belong to the socket set must be checked by calling FreeRTOS_FD_ISSET()
If a socket in the set receives a signal,
causing the task that was blocked on the socket to abort its read operation,
then the call to FreeRTOS_select() will return eSELECT_INTR.
Example usage:
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "FreeRTOS_IP.h"
#include "FreeRTOS_Sockets.h"
static void prvMultipleSocketRxTask( void *pvParameters )
{
SocketSet_t xFD_Set;
BaseType_t xResult;
Socket_t xSockets[2];
struct freertos_sockaddr xAddress;
uint32_t xClientLength = sizeof( struct freertos_sockaddr ), x;
uint32_t ulReceivedValue = 0;
int32_t lBytes;
const TickType_t xRxBlockTime = 0;
xFD_Set = FreeRTOS_CreateSocketSet();
for( x = 0; x < 2; x++ )
{
xSockets[x] = FreeRTOS_socket( FREERTOS_AF_INET,
FREERTOS_SOCK_DGRAM,
FREERTOS_IPPROTO_UDP );
xAddress.sin_port = FreeRTOS_htons( 1000 + x );
FreeRTOS_bind( xSockets[x], &xAddress, sizeof( struct freertos_sockaddr ) );
FreeRTOS_setsockopt( xSockets[x], 0, FREERTOS_SO_RCVTIMEO,
&xRxBlockTime, sizeof( xRxBlockTime ) );
FreeRTOS_FD_SET( xSockets[x], xFD_Set, eSELECT_READ );
}
for( ;; )
{
xResult = FreeRTOS_select( xFD_Set, portMAX_DELAY );
if( xResult != 0 )
{
for( x = 0; x < 2; x++ )
{
if( FreeRTOS_FD_ISSET ( xSockets[x], xFD_Set ) )
{
lBytes = FreeRTOS_recvfrom( xSockets[x], &( ulReceivedValue ),
sizeof( uint32_t ), 0, &xAddress,
&xClientLength );
}
}
}
}
}
Example use of the FreeRTOS_select() API function
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.