Real time embedded FreeRTOS RSS feed 
Homepage FreeRTOS+ Products FreeRTOS Labs Support Forum Contact / Enquiries
FreeRTOS+UDP was removed from the FreeRTOS kernel download from FreeRTOS V10.1.0. See the FreeRTOS+TCP stack, which can be configured for UDP only use, as an alternative.

FreeRTOS_select()

[FreeRTOS Embedded IP Stack API]

FreeRTOS_sockets.h
xSocket_t FreeRTOS_select( xSocketSet_t xSocketSet, TickType_t xBlockTimeTicks );
		

Block on a socket set until a member of the set contains data available for reading.

Socket Sets allow an application task to block when reading from multiple sockets at the same time. Instead of blocking on an individual socket, a task instead blocks on the set.

To use a socket set:

  1. Create a socket set by calling FreeRTOS_CreateSocketSet(). A socket set is equivalent to the Berkeley sockets fd_set type.

  2. 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.

  3. Call FreeRTOS_Select() to test the sockets in the set to see if any contain data that is waiting to be read.

  4. Read from the socket returned by FreeRTOS_select() (if any) using a call to FreeRTOS_recvfrom() as normal.
A socket can only be a member of one set at any time.

FreeRTOS_FD_CLR() removes a socket from a set.

Parameters:

xSocketSet   The socket set being tested.

xBlockTimeTicks   The maximum time, in ticks, that the calling task will remain in the Blocked state (with other tasks executing) to wait for a member of the socket set to have data queued ready for reading using a call to FreeRTOS_recvfrom().

Returns:

If xBlockTimeTicks expired before a socket in the socket set was ready to be read then NULL is returned. Otherwise a handle to a socket that contains received data is returned. A call to FreeRTOS_recvfrom() on the returned socket handle will always receive data even when the FreeRTOS_recvfrom() block time is zero.

Example usage:


/* FreeRTOS+UDP sockets include */
#define "FreeRTOS_sockets.h"

static void prvMultipleSocketRxTask( void *pvParameters )
{
xSocketSet_t xFD_Set;
xSocket_t xSocket;
struct freertos_sockaddr xAddress;
uint32_t xClientLength = sizeof( struct freertos_sockaddr ), x;
uint32_t ulReceivedValue = 0;
int32_t lBytes;
const TickType_t xRxBlockTime = 0;

    /* Create the set of sockets that will be passed into FreeRTOS_select(). */
    xFD_Set = FreeRTOS_CreateSocketSet( selSELECT_QUEUE_SIZE );

    /* Create two sockets to add to the set. */
    for( x = 0; x < 2; x++ )
    {
        /* Create the socket. */
        xSocket = FreeRTOS_socket( FREERTOS_AF_INET,
                                   FREERTOS_SOCK_DGRAM,
                                   FREERTOS_IPPROTO_UDP );

        /* Bind the socket to a port number. */
        xAddress.sin_port = FreeRTOS_htons( 1000 + x );
        FreeRTOS_bind( xSocket, &xAddress, sizeof( struct freertos_sockaddr ) );

        /* There should always be data available on the socket returned from
        FreeRTOS_select() so blocking on a read is never necessary.  Set the
        Rx block time to 0. */
        FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO,
                             &xRxBlockTime, sizeof( xRxBlockTime ) );

        /* Add the created socket to the set. */
        FreeRTOS_FD_SET( xSocket, xFD_Set );
    }

    for( ;; )
    {
        /* Wait for a socket from the set to become available for reading. */
        xSocket = FreeRTOS_select( xFD_Set, portMAX_DELAY );

        /* xSocket should never be NULL because FreeRTOS_select() was called
        with an indefinite delay (assuming INCLUDE_vTaskSuspend is set to 1). */
        configASSERT( xSocket );

        /* Read from the socket - this read is guaranteed to return data. */
        lBytes = FreeRTOS_recvfrom( xSocket, &( ulReceivedValue ),
                                    sizeof( uint32_t ), 0, &xAddress,
                                    &xClientLength );

        /* Process the received data here. */
    }
}
						
Example use of the FreeRTOS_close() API function


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ FreeRTOS+ Sitemap ]    [ Main FreeRTOS Sitemap ]    [ ]


Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.