2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
71 * Two tasks are created that send UDP echo requests to the standard echo port
\r
72 * (port 7). One task uses the standard socket interface, the other the zero
\r
73 * copy socket interface. The IP address of the echo server must be configured
\r
74 * using the configECHO_SERVER_ADDR0 to configECHO_SERVER_ADDR3 constants in
\r
75 * FreeRTOSConfig.h. These tasks are self checking and will trigger a
\r
76 * configASSERT() if the received echo reply does not match the transmitted echo
\r
77 * request. As these tasks use UDP, and can therefore loose packets, they will
\r
78 * cause configASSERT() to be called when they are run in a less than perfect
\r
79 * networking environment, or when connected to an echo server that
\r
80 * legitimately as UDP is used) opts not to reply to every echo request.
\r
82 * See the following web page for essential demo usage and configuration
\r
84 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html
\r
87 /* Standard includes. */
\r
92 /* FreeRTOS includes. */
\r
93 #include "FreeRTOS.h"
\r
96 /* FreeRTOS+TCP includes. */
\r
97 #include "FreeRTOS_IP.h"
\r
98 #include "FreeRTOS_Sockets.h"
\r
100 /* Small delay used between attempts to obtain a zero copy buffer. */
\r
101 #define echoTINY_DELAY ( ( TickType_t ) 2 )
\r
103 /* The echo tasks create a socket, send out a number of echo requests
\r
104 (listening for each echo reply), then close the socket again before
\r
105 starting over. This delay is used between each iteration to ensure the
\r
106 network does not get too congested. */
\r
107 #define echoLOOP_DELAY ( ( TickType_t ) 250 / portTICK_PERIOD_MS )
\r
109 /* When the trace recorder code is not included just #define away the call
\r
110 to post the user event. */
\r
111 #define echoMARK_SEND_IN_TRACE_BUFFER( x )
\r
112 #define xZeroCopySendEvent 0
\r
113 #define xZeroCopyReceiveEvent 0
\r
115 /* The echo server is assumed to be on port 7, which is the standard echo
\r
117 #define echoECHO_PORT ( 7 )
\r
119 /*-----------------------------------------------------------*/
\r
122 * Uses a socket to send data to, then receive data from, the standard echo
\r
123 * port number 7. prvEchoClientTask() uses the standard interface.
\r
124 * prvZeroCopyEchoClientTask() uses the zero copy interface.
\r
126 static void prvEchoClientTask( void *pvParameters );
\r
127 static void prvZeroCopyEchoClientTask( void *pvParameters );
\r
129 /*-----------------------------------------------------------*/
\r
131 /* Timeouts used to ensure the sockets don't wait forever. */
\r
132 static const TickType_t xReceiveTimeOut = 1500 / portTICK_PERIOD_MS;
\r
133 static const TickType_t xSendTimeOut = 1500 / portTICK_PERIOD_MS;
\r
135 /* Simple delay to ensure ARPs don't get in the way. */
\r
136 const TickType_t ulInitialARPReplyDelay = ( 500UL / portTICK_PERIOD_MS );
\r
138 /* Used for detecting errors. */
\r
139 static uint32_t ulStandardCycles = 0, ulZeroCopyCycles = 0;
\r
141 /*-----------------------------------------------------------*/
\r
143 void vStartUDPEchoClientTasks( uint16_t usTaskStackSize, UBaseType_t uxTaskPriority )
\r
145 /* Create the echo client task that does not use the zero copy interface. */
\r
146 xTaskCreate( prvEchoClientTask, /* The function that implements the task. */
\r
147 "UDPEcho0", /* Just a text name for the task to aid debugging. */
\r
148 usTaskStackSize, /* The stack size is defined in FreeRTOSIPConfig.h. */
\r
149 NULL, /* The task parameter, not used in this case. */
\r
150 uxTaskPriority, /* The priority assigned to the task is defined in FreeRTOSConfig.h. */
\r
151 NULL ); /* The task handle is not used. */
\r
153 /* Create the echo client task that does use the zero copy interface. */
\r
154 xTaskCreate( prvZeroCopyEchoClientTask, /* The function that implements the task. */
\r
155 "UDPEcho1", /* Just a text name for the task to aid debugging. */
\r
156 usTaskStackSize, /* The stack size is defined in FreeRTOSIPConfig.h. */
\r
157 NULL, /* The task parameter, not used in this case. */
\r
158 uxTaskPriority, /* The priority assigned to the task is defined in FreeRTOSConfig.h. */
\r
159 NULL ); /* The task handle is not used. */
\r
161 /*-----------------------------------------------------------*/
\r
163 static void prvEchoClientTask( void *pvParameters )
\r
166 struct freertos_sockaddr xEchoServerAddress;
\r
167 int8_t cTxString[ 50 ], cRxString[ 50 ]; /* Make sure the stack is large enough to hold these. Turn on stack overflow checking during debug to be sure. */
\r
168 int32_t lLoopCount = 0UL, lReturned;
\r
169 const int32_t lMaxLoopCount = 50;
\r
170 uint32_t xAddressLength = sizeof( xEchoServerAddress ), ulTxCount = 0;
\r
172 /* Remove compiler warning about unused parameters. */
\r
173 ( void ) pvParameters;
\r
175 /* Echo requests are sent to the echo server. The address of the echo
\r
176 server is configured by the constants configECHO_SERVER_ADDR0 to
\r
177 configECHO_SERVER_ADDR3 in FreeRTOSConfig.h. */
\r
178 xEchoServerAddress.sin_port = FreeRTOS_htons( echoECHO_PORT );
\r
179 xEchoServerAddress.sin_addr = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0,
\r
180 configECHO_SERVER_ADDR1,
\r
181 configECHO_SERVER_ADDR2,
\r
182 configECHO_SERVER_ADDR3 );
\r
184 /* Send a ping to elicit an ARP to ensure the MAC address is known before
\r
185 the first UDP packet is sent - otherwise the UDP packet lost to the ARP will
\r
186 trigger an assert. Wait enough time for the ARP reply before continuing. */
\r
187 #if ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
\r
189 FreeRTOS_SendPingRequest( xEchoServerAddress.sin_addr, 1, portMAX_DELAY );
\r
191 #endif /* ipconfigSUPPORT_OUTGOING_PINGS */
\r
193 /* Just ensure ARPS don't get in the way. */
\r
194 vTaskDelay( ulInitialARPReplyDelay );
\r
198 /* Create a socket. */
\r
199 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
200 configASSERT( xSocket != FREERTOS_INVALID_SOCKET );
\r
202 /* Set time outs so failed sends and missing replies do not cause the
\r
203 task to block indefinitely. */
\r
204 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );
\r
205 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendTimeOut, sizeof( xSendTimeOut ) );
\r
207 /* Send a number of echo requests. */
\r
208 for( lLoopCount = 0; lLoopCount < lMaxLoopCount; lLoopCount++ )
\r
210 /* Create the string that is sent to the echo server. */
\r
211 sprintf( ( char * ) cTxString, "Message number %u\r\n", ulTxCount );
\r
214 /* Send the string to the socket. ulFlags is set to 0, so the zero
\r
215 copy interface is not used. That means the data from cTxString is
\r
216 copied into a network buffer inside FreeRTOS_sendto(), and cTxString
\r
217 can be reused as soon as FreeRTOS_sendto() has returned. 1 is added
\r
218 to ensure the NULL string terminator is sent as part of the message. */
\r
219 lReturned = FreeRTOS_sendto( xSocket, /* The socket being sent to. */
\r
220 ( void * ) cTxString, /* The data being sent. */
\r
221 strlen( ( const char * ) cTxString ) + 1, /* The length of the data being sent. */
\r
222 0, /* ulFlags with the FREERTOS_ZERO_COPY bit clear. */
\r
223 &xEchoServerAddress, /* The destination address. */
\r
224 sizeof( xEchoServerAddress ) );
\r
225 configASSERT( lReturned > 0 );
\r
227 /* Receive data echoed back to the socket. ulFlags is zero, so the
\r
228 zero copy option is not being used and the received data will be
\r
229 copied into the buffer pointed to by cRxString. xAddressLength is
\r
230 not actually used (at the time of writing this comment, anyway) by
\r
231 FreeRTOS_recvfrom(), but is set appropriately in case future
\r
232 versions do use it. */
\r
233 memset( ( void * ) cRxString, 0x00, sizeof( cRxString ) );
\r
234 lReturned = FreeRTOS_recvfrom( xSocket, /* The socket being received from. */
\r
235 cRxString, /* The buffer into which the received data will be written. */
\r
236 sizeof( cRxString ), /* The size of the buffer provided to receive the data. */
\r
237 0, /* ulFlags with the FREERTOS_ZERO_COPY bit clear. */
\r
238 &xEchoServerAddress, /* The address from which the data was sent (the source address). */
\r
241 /* Compare the transmitted string to the received string. */
\r
242 if( lReturned > 1 )
\r
244 if( strcmp( ( char * ) cRxString, ( char * ) cTxString ) == 0 )
\r
246 /* The echo reply was received without error. */
\r
247 ulStandardCycles++;
\r
252 /* Pause for a short while to ensure the network is not too
\r
254 vTaskDelay( echoLOOP_DELAY );
\r
256 /* Close this socket before looping back to create another. */
\r
257 FreeRTOS_closesocket( xSocket );
\r
260 /*-----------------------------------------------------------*/
\r
262 static void prvZeroCopyEchoClientTask( void *pvParameters )
\r
265 struct freertos_sockaddr xEchoServerAddress;
\r
266 static int8_t cTxString[ 40 ];
\r
267 int32_t lLoopCount = 0UL;
\r
268 uint32_t xAddressLength = sizeof( xEchoServerAddress ), ulTxCount = 0;
\r
270 uint8_t *pucUDPPayloadBuffer;
\r
271 const int32_t lMaxLoopCount = 50;
\r
272 const char * const pcStringToSend = "Zero copy message number";
\r
273 /* The buffer is large enough to hold the string, a number, and the string terminator. */
\r
274 const size_t xBufferLength = strlen( pcStringToSend ) + 35;
\r
276 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1
\r
278 /* When the trace recorder code is included user events are generated to
\r
279 mark the sending and receiving of the echoed data (only in the zero copy
\r
281 xZeroCopySendEvent = xTraceOpenLabel( "ZeroCopyTx" );
\r
282 xZeroCopyReceiveEvent = xTraceOpenLabel( "ZeroCopyRx" );
\r
284 #endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS */
\r
286 /* Remove compiler warning about unused parameters. */
\r
287 ( void ) pvParameters;
\r
289 /* Delay for a little while to ensure the other task has sent out a ping
\r
290 in order for the ARP cache to contain the address of the echo server. */
\r
291 vTaskDelay( echoLOOP_DELAY >> 1 );
\r
293 /* Echo requests are sent to the echo server. The address of the echo
\r
294 server is configured by the constants configECHO_SERVER_ADDR0 to
\r
295 configECHO_SERVER_ADDR3 in FreeRTOSConfig.h. */
\r
296 xEchoServerAddress.sin_port = FreeRTOS_htons( echoECHO_PORT );
\r
297 xEchoServerAddress.sin_addr = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0,
\r
298 configECHO_SERVER_ADDR1,
\r
299 configECHO_SERVER_ADDR2,
\r
300 configECHO_SERVER_ADDR3 );
\r
304 /* Create a socket. */
\r
305 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
306 configASSERT( xSocket != FREERTOS_INVALID_SOCKET );
\r
308 /* Set a time outs so failed sends and missing replies do not cause the
\r
309 task to block indefinitely. */
\r
310 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );
\r
311 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendTimeOut, sizeof( xSendTimeOut ) );
\r
313 /* Send a number of echo requests. */
\r
314 for( lLoopCount = 0; lLoopCount < lMaxLoopCount; lLoopCount++ )
\r
316 /* This task is going to send using the zero copy interface. The
\r
317 data being sent is therefore written directly into a buffer that is
\r
318 passed by reference into the FreeRTOS_sendto() function. First
\r
319 obtain a buffer of adequate size from the IP stack. Although a max
\r
320 delay is used, the actual delay will be capped to
\r
321 ipconfigMAX_SEND_BLOCK_TIME_TICKS, hence the test to ensure a buffer
\r
322 was actually obtained. */
\r
323 pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xBufferLength, portMAX_DELAY );
\r
325 if( pucUDPPayloadBuffer != NULL )
\r
327 /* A buffer was successfully obtained. Create the string that is
\r
328 sent to the echo server. Note the string is written directly
\r
329 into the buffer obtained from the IP stack. */
\r
330 sprintf( ( char * ) pucUDPPayloadBuffer, "%s %u\r\n", ( const char * ) "Zero copy message number", ulTxCount );
\r
333 /* Also copy the string into a local buffer so it can be compared
\r
334 with the string that is later received back from the echo server. */
\r
335 strcpy( ( char * ) cTxString, ( char * ) pucUDPPayloadBuffer );
\r
337 /* Pass the buffer into the send function. ulFlags has the
\r
338 FREERTOS_ZERO_COPY bit set so the IP stack will take control of
\r
339 the buffer, rather than copy data out of the buffer. */
\r
340 echoMARK_SEND_IN_TRACE_BUFFER( xZeroCopySendEvent );
\r
341 lReturned = FreeRTOS_sendto( xSocket, /* The socket being sent to. */
\r
342 ( void * ) pucUDPPayloadBuffer, /* The buffer being passed into the IP stack. */
\r
343 strlen( ( const char * ) cTxString ) + 1, /* The length of the data being sent. Plus 1 to ensure the null terminator is part of the data. */
\r
344 FREERTOS_ZERO_COPY, /* ulFlags with the zero copy bit is set. */
\r
345 &xEchoServerAddress, /* Where the data is being sent. */
\r
346 sizeof( xEchoServerAddress ) );
\r
348 if( lReturned == 0 )
\r
350 /* The send operation failed, so this task is still
\r
351 responsible for the buffer obtained from the IP stack. To
\r
352 ensure the buffer is not lost it must either be used again,
\r
353 or, as in this case, returned to the IP stack using
\r
354 FreeRTOS_ReleaseUDPPayloadBuffer(). pucUDPPayloadBuffer can
\r
355 be safely re-used to receive from the socket below once the
\r
356 buffer has been returned to the stack. */
\r
357 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );
\r
361 /* The send was successful so the IP stack is now managing
\r
362 the buffer pointed to by pucUDPPayloadBuffer, and the IP
\r
363 stack will return the buffer once it has been sent.
\r
364 pucUDPPayloadBuffer can be safely re-used to receive from
\r
365 the socket below. */
\r
368 /* Receive data on the socket. ulFlags has the zero copy bit set
\r
369 (FREERTOS_ZERO_COPY) indicating to the stack that a reference to
\r
370 the received data should be passed out to this task using the
\r
371 second parameter to the FreeRTOS_recvfrom() call. When this is
\r
372 done the IP stack is no longer responsible for releasing the
\r
373 buffer, and the task *must* return the buffer to the stack when
\r
374 it is no longer needed. By default the receive block time is
\r
376 echoMARK_SEND_IN_TRACE_BUFFER( xZeroCopyReceiveEvent );
\r
377 lReturned = FreeRTOS_recvfrom( xSocket, /* The socket to receive from. */
\r
378 ( void * ) &pucUDPPayloadBuffer, /* pucUDPPayloadBuffer will be set to point to the buffer that already contains the received data. */
\r
379 0, /* Ignored because the zero copy interface is being used. */
\r
380 FREERTOS_ZERO_COPY, /* ulFlags with the FREERTOS_ZERO_COPY bit set. */
\r
381 &xEchoServerAddress, /* The address from which the data was sent. */
\r
384 if( lReturned > 0 )
\r
386 /* Compare the string sent to the echo server with the string
\r
387 received back from the echo server. */
\r
388 if( strcmp( ( char * ) pucUDPPayloadBuffer, ( char * ) cTxString ) == 0 )
\r
390 /* The strings matched. */
\r
391 ulZeroCopyCycles++;
\r
394 /* The buffer that contains the data passed out of the stack
\r
395 *must* be returned to the stack. */
\r
396 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
\r
401 /* Pause for a short while to ensure the network is not too
\r
403 vTaskDelay( echoLOOP_DELAY );
\r
405 /* Close this socket before looping back to create another. */
\r
406 FreeRTOS_closesocket( xSocket );
\r
409 /*-----------------------------------------------------------*/
\r
411 BaseType_t xAreUDPEchoClientsStillRunning( void )
\r
413 static uint32_t ulLastStandardCycles = 0, ulLastZeroCopyCycles = 0;
\r
414 BaseType_t xReturn = pdPASS;
\r
416 if( ulStandardCycles == ulLastStandardCycles )
\r
422 ulLastStandardCycles = ulStandardCycles;
\r
425 if( ulZeroCopyCycles == ulLastZeroCopyCycles )
\r
431 ulLastZeroCopyCycles = ulZeroCopyCycles;
\r