Download FreeRTOS
 

Quality RTOS & Embedded Software

LIBRARIES
WHAT'S NEW
Simplifying Authenticated Cloud Connectivity for Any Device.
Designing an energy efficient and cloud-connected IoT solution with CoAP.
Introducing FreeRTOS Kernel version 11.0.0:
FreeRTOS Roadmap and Code Contribution process.
OPC-UA over TSN with FreeRTOS.

Receiving UDP Data (standard interface)
Part of the FreeRTOS-Plus-TCP Networking Tutorial

The FreeRTOS_recvfrom() TCP/IP stack API function is used to receive from a UDP socket. Data can only be received after the socket has been created, configured, and bound to a local port number.

As detailed on the FreeRTOS_recvfrom() API reference page, FreeRTOS_recvfrom() can be used with standard calling semantics, or zero copy calling semantics. This page demonstrates the standard calling semantics.

The source code snippets below show a RTOS task that creates a socket before entering a loop that receives data using the standard (as opposed to zero copy) calling semantics. Both IPv4 and IPv6 use cases are shown.

IPv4

static void vUDPReceivingUsingStandardInterface( void *pvParameters )
{
long lBytes;
uint8_t cReceivedString[ 60 ];
struct freertos_sockaddr xClient, xBindAddress;
uint32_t xClientLength = sizeof( xClient );
Socket_t xListeningSocket;

/* Attempt to open the socket. */
xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET,
FREERTOS_SOCK_DGRAM, /*FREERTOS_SOCK_DGRAM for UDP.*/
FREERTOS_IPPROTO_UDP );

/* Check the socket was created. */
configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );

/* Bind to port 10000. */
xBindAddress.sin_port = FreeRTOS_htons( 10000 );
FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );

for( ;; )
{
/* Receive data from the socket. ulFlags is zero, so the standard
interface is used. By default the block time is portMAX_DELAY, but it
can be changed using FreeRTOS_setsockopt(). */

lBytes = FreeRTOS_recvfrom( xListeningSocket,
cReceivedString,
sizeof( cReceivedString ),
0,
&xClient,
&xClientLength );

if( lBytes > 0 )
{
/* Data was received and can be process here. */
}
}
}
Example using FreeRTOS_recvfrom() with the standard (as opposed to zero copy) calling semantics

IPv6

static void vUDPReceivingUsingStandardInterface( void *pvParameters )
{
long lBytes;
uint8_t cReceivedString[ 60 ];
struct freertos_sockaddr xClient, xBindAddress;
uint32_t xClientLength = sizeof( xClient );
Socket_t xListeningSocket;

/* Attempt to open the socket. */
xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET6, /* FREERTOS_AF_INET6 for IPv6 socket */
FREERTOS_SOCK_DGRAM, /*FREERTOS_SOCK_DGRAM for UDP.*/
FREERTOS_IPPROTO_UDP );

/* Check the socket was created. */
configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );

/* Bind to port 10000. */
xBindAddress.sin_port = FreeRTOS_htons( 10000 );
FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );

for( ;; )
{
/* Receive data from the socket. ulFlags is zero, so the standard

interface is used. By default the block time is portMAX_DELAY, but it

can be changed using FreeRTOS_setsockopt(). */

lBytes = FreeRTOS_recvfrom( xListeningSocket,
cReceivedString,
sizeof( cReceivedString ),
0,
&xClient,
&xClientLength );

if( lBytes > 0 )
{
/* Data was received and can be process here. */
}
}
}
Example using FreeRTOS_recvfrom() with the standard (as opposed to zero copy) calling semantics


<< Back to the RTOS TCP networking tutorial index


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