2 FreeRTOS V8.1.0 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 /******************************************************************************
\r
69 * See the following web page for essential TwoEchoClient.c usage and
\r
70 * configuration details:
\r
71 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/Common_Echo_Clients.shtml
\r
73 ******************************************************************************/
\r
76 /* Standard includes. */
\r
81 /* FreeRTOS includes. */
\r
82 #include "FreeRTOS.h"
\r
85 /* FreeRTOS+UDP includes. */
\r
86 #include "FreeRTOS_UDP_IP.h"
\r
87 #include "FreeRTOS_Sockets.h"
\r
89 /* Small delay used between attempts to obtain a zero copy buffer. */
\r
90 #define echoTINY_DELAY ( ( TickType_t ) 2 )
\r
92 /* The echo tasks create a socket, send out a number of echo requests
\r
93 (listening for each echo reply), then close the socket again before
\r
94 starting over. This delay is used between each iteration to ensure the
\r
95 network does not get too congested. */
\r
96 #define echoLOOP_DELAY ( ( TickType_t ) 250 / portTICK_RATE_MS )
\r
98 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1
\r
99 /* When the trace recorder code is included user events are generated to
\r
100 mark the sending and receiving of the echoed data (only in the zero copy
\r
102 #define echoMARK_SEND_IN_TRACE_BUFFER( x ) vTraceUserEvent( x )
\r
103 traceLabel xZeroCopySendEvent, xZeroCopyReceiveEvent;
\r
106 /* When the trace recorder code is not included just #define away the call
\r
107 to post the user event. */
\r
108 #define echoMARK_SEND_IN_TRACE_BUFFER( x )
\r
109 #define xZeroCopySendEvent 0
\r
110 #define xZeroCopyReceiveEvent 0
\r
113 /* The echo server is assumed to be on port 7, which is the standard echo
\r
115 #define echoECHO_PORT ( 7 )
\r
118 * Uses a socket to send data to, then receive data from, the standard echo
\r
119 * port number 7. prvEchoClientTask() uses the standard interface.
\r
120 * prvZeroCopyEchoClientTask() uses the zero copy interface.
\r
122 static void prvEchoClientTask( void *pvParameters );
\r
123 static void prvZeroCopyEchoClientTask( void *pvParameters );
\r
125 /* The receive timeout is set shorter when the windows simulator is used
\r
126 because simulated time is slower than real time. */
\r
128 const TickType_t xReceiveTimeOut = 50 / portTICK_RATE_MS;
\r
130 const TickType_t xReceiveTimeOut = 500 / portTICK_RATE_MS;
\r
133 /*-----------------------------------------------------------*/
\r
135 void vStartEchoClientTasks( uint16_t usTaskStackSize, UBaseType_t uxTaskPriority )
\r
137 /* Create the echo client task that does not use the zero copy interface. */
\r
138 xTaskCreate( prvEchoClientTask, /* The function that implements the task. */
\r
139 "Echo0", /* Just a text name for the task to aid debugging. */
\r
140 usTaskStackSize, /* The stack size is defined in FreeRTOSIPConfig.h. */
\r
141 NULL, /* The task parameter, not used in this case. */
\r
142 uxTaskPriority, /* The priority assigned to the task is defined in FreeRTOSConfig.h. */
\r
143 NULL ); /* The task handle is not used. */
\r
145 /* Create the echo client task that does use the zero copy interface. */
\r
146 xTaskCreate( prvZeroCopyEchoClientTask, /* The function that implements the task. */
\r
147 "Echo1", /* 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 /*-----------------------------------------------------------*/
\r
155 static void prvEchoClientTask( void *pvParameters )
\r
158 struct freertos_sockaddr xEchoServerAddress;
\r
159 char cTxString[ 25 ], cRxString[ 25 ]; /* Make sure the stack is large enough to hold these. Turn on stack overflow checking during debug to be sure. */
\r
160 int32_t lLoopCount = 0UL;
\r
161 const int32_t lMaxLoopCount = 50;
\r
162 volatile uint32_t ulRxCount = 0UL, ulTxCount = 0UL;
\r
163 uint32_t xAddressLength = sizeof( xEchoServerAddress );
\r
165 /* Remove compiler warning about unused parameters. */
\r
166 ( void ) pvParameters;
\r
168 /* Echo requests are sent to the echo server. The address of the echo
\r
169 server is configured by the constants configECHO_SERVER_ADDR0 to
\r
170 configECHO_SERVER_ADDR3 in FreeRTOSConfig.h. */
\r
171 xEchoServerAddress.sin_port = FreeRTOS_htons( echoECHO_PORT );
\r
172 xEchoServerAddress.sin_addr = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0,
\r
173 configECHO_SERVER_ADDR1,
\r
174 configECHO_SERVER_ADDR2,
\r
175 configECHO_SERVER_ADDR3 );
\r
179 /* Create a socket. */
\r
180 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
181 configASSERT( xSocket != FREERTOS_INVALID_SOCKET );
\r
183 /* Set a time out so a missing reply does not cause the task to block
\r
185 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );
\r
187 /* Send a number of echo requests. */
\r
188 for( lLoopCount = 0; lLoopCount < lMaxLoopCount; lLoopCount++ )
\r
190 /* Create the string that is sent to the echo server. */
\r
191 sprintf( cTxString, "Message number %u\r\n", ( unsigned int ) ulTxCount );
\r
193 /* Send the string to the socket. ulFlags is set to 0, so the zero
\r
194 copy interface is not used. That means the data from cTxString is
\r
195 copied into a network buffer inside FreeRTOS_sendto(), and cTxString
\r
196 can be reused as soon as FreeRTOS_sendto() has returned. 1 is added
\r
197 to ensure the NULL string terminator is sent as part of the message. */
\r
198 FreeRTOS_sendto( xSocket, /* The socket being sent to. */
\r
199 ( void * ) cTxString, /* The data being sent. */
\r
200 strlen( cTxString ) + 1,/* The length of the data being sent. */
\r
201 0, /* ulFlags with the FREERTOS_ZERO_COPY bit clear. */
\r
202 &xEchoServerAddress, /* The destination address. */
\r
203 sizeof( xEchoServerAddress ) );
\r
205 /* Keep a count of how many echo requests have been transmitted so
\r
206 it can be compared to the number of echo replies received. It would
\r
207 be expected to loose at least one to an ARP message the first time
\r
208 the connection is created. */
\r
211 /* Receive data echoed back to the socket. ulFlags is zero, so the
\r
212 zero copy option is not being used and the received data will be
\r
213 copied into the buffer pointed to by cRxString. xAddressLength is
\r
214 not actually used (at the time of writing this comment, anyway) by
\r
215 FreeRTOS_recvfrom(), but is set appropriately in case future
\r
216 versions do use it. */
\r
217 memset( ( void * ) cRxString, 0x00, sizeof( cRxString ) );
\r
218 FreeRTOS_recvfrom( xSocket, /* The socket being received from. */
\r
219 cRxString, /* The buffer into which the received data will be written. */
\r
220 sizeof( cRxString ), /* The size of the buffer provided to receive the data. */
\r
221 0, /* ulFlags with the FREERTOS_ZERO_COPY bit clear. */
\r
222 &xEchoServerAddress, /* The address from where the data was sent (the source address). */
\r
225 /* Compare the transmitted string to the received string. */
\r
226 if( strcmp( cRxString, cTxString ) == 0 )
\r
228 /* The echo reply was received without error. */
\r
233 /* Pause for a short while to ensure the network is not too
\r
235 vTaskDelay( echoLOOP_DELAY );
\r
237 /* Close this socket before looping back to create another. */
\r
238 FreeRTOS_closesocket( xSocket );
\r
241 /*-----------------------------------------------------------*/
\r
243 static void prvZeroCopyEchoClientTask( void *pvParameters )
\r
246 struct freertos_sockaddr xEchoServerAddress;
\r
247 static char cTxString[ 40 ];
\r
248 int32_t lLoopCount = 0UL;
\r
249 volatile uint32_t ulRxCount = 0UL, ulTxCount = 0UL;
\r
250 uint32_t xAddressLength = sizeof( xEchoServerAddress );
\r
252 uint8_t *pucUDPPayloadBuffer;
\r
254 const int32_t lMaxLoopCount = 50;
\r
255 const char * const pcStringToSend = "Zero copy message number";
\r
256 /* The buffer is large enough to hold the string, a number, and the string terminator. */
\r
257 const size_t xBufferLength = strlen( pcStringToSend ) + 15;
\r
259 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1
\r
261 /* When the trace recorder code is included user events are generated to
\r
262 mark the sending and receiving of the echoed data (only in the zero copy
\r
264 xZeroCopySendEvent = xTraceOpenLabel( "ZeroCopyTx" );
\r
265 xZeroCopyReceiveEvent = xTraceOpenLabel( "ZeroCopyRx" );
\r
267 #endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS */
\r
269 /* Remove compiler warning about unused parameters. */
\r
270 ( void ) pvParameters;
\r
272 /* Delay for a little while to ensure the task is out of synch with the
\r
273 other echo task implemented above. */
\r
274 vTaskDelay( echoLOOP_DELAY >> 1 );
\r
276 /* Echo requests are sent to the echo server. The address of the echo
\r
277 server is configured by the constants configECHO_SERVER_ADDR0 to
\r
278 configECHO_SERVER_ADDR3 in FreeRTOSConfig.h. */
\r
279 xEchoServerAddress.sin_port = FreeRTOS_htons( echoECHO_PORT );
\r
280 xEchoServerAddress.sin_addr = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0,
\r
281 configECHO_SERVER_ADDR1,
\r
282 configECHO_SERVER_ADDR2,
\r
283 configECHO_SERVER_ADDR3 );
\r
287 /* Create a socket. */
\r
288 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
289 configASSERT( xSocket != FREERTOS_INVALID_SOCKET );
\r
291 /* Set a time out so a missing reply does not cause the task to block
\r
293 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );
\r
295 /* Send a number of echo requests. */
\r
296 for( lLoopCount = 0; lLoopCount < lMaxLoopCount; lLoopCount++ )
\r
298 /* This task is going to send using the zero copy interface. The
\r
299 data being sent is therefore written directly into a buffer that is
\r
300 passed by reference into the FreeRTOS_sendto() function. First
\r
301 obtain a buffer of adequate size from the IP stack. Although a max
\r
302 delay is used, the actual delay will be capped to
\r
303 ipconfigMAX_SEND_BLOCK_TIME_TICKS, hence the test to ensure a buffer
\r
304 was actually obtained. */
\r
305 pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xBufferLength, portMAX_DELAY );
\r
307 if( pucUDPPayloadBuffer != NULL )
\r
309 /* A buffer was successfully obtained. Create the string that is
\r
310 sent to the echo server. Note the string is written directly
\r
311 into the buffer obtained from the IP stack. */
\r
312 sprintf( ( char * ) pucUDPPayloadBuffer, "%s %u\r\n", "Zero copy message number", ( unsigned int ) ulTxCount );
\r
314 /* Also copy the string into a local buffer so it can be compared
\r
315 with the string that is later received back from the echo server. */
\r
316 strcpy( cTxString, ( char * ) pucUDPPayloadBuffer );
\r
318 /* Pass the buffer into the send function. ulFlags has the
\r
319 FREERTOS_ZERO_COPY bit set so the IP stack will take control of
\r
320 the buffer, rather than copy data out of the buffer. */
\r
321 echoMARK_SEND_IN_TRACE_BUFFER( xZeroCopySendEvent );
\r
322 lReturned = FreeRTOS_sendto( xSocket, /* The socket being sent to. */
\r
323 ( void * ) pucUDPPayloadBuffer, /* The buffer being passed into the IP stack. */
\r
324 strlen( cTxString ) + 1, /* The length of the data being sent. Plus 1 to ensure the null terminator is part of the data. */
\r
325 FREERTOS_ZERO_COPY, /* ulFlags with the zero copy bit is set. */
\r
326 &xEchoServerAddress, /* Where the data is being sent. */
\r
327 sizeof( xEchoServerAddress ) );
\r
329 if( lReturned == 0 )
\r
331 /* The send operation failed, so this task is still
\r
332 responsible for the buffer obtained from the IP stack. To
\r
333 ensure the buffer is not lost it must either be used again,
\r
334 or, as in this case, returned to the IP stack using
\r
335 FreeRTOS_ReleaseUDPPayloadBuffer(). pucUDPPayloadBuffer can
\r
336 be safely re-used to receive from the socket below once the
\r
337 buffer has been returned to the stack. */
\r
338 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );
\r
342 /* The send was successful so the IP stack is now managing
\r
343 the buffer pointed to by pucUDPPayloadBuffer, and the IP
\r
344 stack will return the buffer once it has been sent.
\r
345 pucUDPPayloadBuffer can be safely re-used to receive from
\r
346 the socket below. */
\r
349 /* Keep a count of how many echo requests have been transmitted
\r
350 so it can be compared to the number of echo replies received.
\r
351 It would be expected to loose at least one to an ARP message the
\r
352 first time the connection is created. */
\r
355 /* Receive data on the socket. ulFlags has the zero copy bit set
\r
356 (FREERTOS_ZERO_COPY) indicating to the stack that a reference to
\r
357 the received data should be passed out to this task using the
\r
358 second parameter to the FreeRTOS_recvfrom() call. When this is
\r
359 done the IP stack is no longer responsible for releasing the
\r
360 buffer, and the task *must* return the buffer to the stack when
\r
361 it is no longer needed. By default the receive block time is
\r
363 echoMARK_SEND_IN_TRACE_BUFFER( xZeroCopyReceiveEvent );
\r
364 lReturned = FreeRTOS_recvfrom( xSocket, /* The socket to receive from. */
\r
365 ( void * ) &pucUDPPayloadBuffer, /* pucUDPPayloadBuffer will be set to point to the buffer that already contains the received data. */
\r
366 0, /* Ignored because the zero copy interface is being used. */
\r
367 FREERTOS_ZERO_COPY, /* ulFlags with the FREERTOS_ZERO_COPY bit set. */
\r
368 &xEchoServerAddress, /* The address from which the data was sent. */
\r
371 if( lReturned > 0 )
\r
373 /* Compare the string sent to the echo server with the string
\r
374 received back from the echo server. */
\r
375 if( strcmp( ( char * ) pucUDPPayloadBuffer, cTxString ) == 0 )
\r
377 /* The strings matched. */
\r
381 /* The buffer that contains the data passed out of the stack
\r
382 *must* be returned to the stack. */
\r
383 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
\r
388 /* Pause for a short while to ensure the network is not too
\r
390 vTaskDelay( echoLOOP_DELAY );
\r
392 /* Close this socket before looping back to create another. */
\r
393 FreeRTOS_closesocket( xSocket );
\r
396 /*-----------------------------------------------------------*/
\r