Real time embedded FreeRTOS RSS feed 
Homepage FreeRTOS+ Products FreeRTOS Labs Support Forum Contact / Enquiries

FreeRTOS_select()

[FreeRTOS+TCP API Reference]

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:

  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 of the sockets have an event pending.

  4. 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 -pdFREERTOS_ERRNO_EINTR.

Example usage:


/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

/* FreeRTOS+TCP includes. */
#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;

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

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

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

        /* Once it has been tested that a socket has a eSELECT_READ
        event, blocking on a read call is not necessary any more.  Set the
        Rx block time to 0. */
        FreeRTOS_setsockopt( xSockets[x], 0, FREERTOS_SO_RCVTIMEO,
                             &xRxBlockTime, sizeof( xRxBlockTime ) );

        /* Add the created socket to the set for the READ event only. */
        FreeRTOS_FD_SET( xSockets[x], xFD_Set, eSELECT_READ );
    }

    for( ;; )
    {
        /* Wait for any event within the socket set. */
        xResult = FreeRTOS_select( xFD_Set, portMAX_DELAY );
        if( xResult != 0 )
        {
            /* The return value should never be zero because FreeRTOS_select() was called
            with an indefinite delay (assuming INCLUDE_vTaskSuspend is set to 1).
            Now check each socket which belongs to the set if it had an event */

            for( x = 0; x < 2; x++ )
            {
                if( FreeRTOS_FD_ISSET ( xSockets[x], xFD_Set ) )
                {
                    /* Read from the socket. */
                    lBytes = FreeRTOS_recvfrom( xSockets[x], &( ulReceivedValue ),
                                        sizeof( uint32_t ), 0, &xAddress,
                                        &xClientLength );
                    /* Process the received data here. */
                }
            }
        }
    }
}
						
Example use of the FreeRTOS_select() API function


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




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