2 FreeRTOS V8.2.2 - Copyright (C) 2015 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 /******************************************************************************
\r
73 * See the following web page for essential TwoEchoClient.c usage and
\r
74 * configuration details:
\r
75 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/Common_Echo_Clients.shtml
\r
77 ******************************************************************************/
\r
80 /* Standard includes. */
\r
85 /* FreeRTOS includes. */
\r
86 #include "FreeRTOS.h"
\r
89 /* FreeRTOS+UDP includes. */
\r
90 #include "FreeRTOS_UDP_IP.h"
\r
91 #include "FreeRTOS_Sockets.h"
\r
93 /* Small delay used between attempts to obtain a zero copy buffer. */
\r
94 #define echoTINY_DELAY ( ( TickType_t ) 2 )
\r
96 /* The echo tasks create a socket, send out a number of echo requests
\r
97 (listening for each echo reply), then close the socket again before
\r
98 starting over. This delay is used between each iteration to ensure the
\r
99 network does not get too congested. */
\r
100 #define echoLOOP_DELAY ( ( TickType_t ) 250 / portTICK_RATE_MS )
\r
102 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1
\r
103 /* When the trace recorder code is included user events are generated to
\r
104 mark the sending and receiving of the echoed data (only in the zero copy
\r
106 #define echoMARK_SEND_IN_TRACE_BUFFER( x ) vTraceUserEvent( x )
\r
107 traceLabel xZeroCopySendEvent, xZeroCopyReceiveEvent;
\r
110 /* When the trace recorder code is not included just #define away the call
\r
111 to post the user event. */
\r
112 #define echoMARK_SEND_IN_TRACE_BUFFER( x )
\r
113 #define xZeroCopySendEvent 0
\r
114 #define xZeroCopyReceiveEvent 0
\r
117 /* The echo server is assumed to be on port 7, which is the standard echo
\r
119 #define echoECHO_PORT ( 7 )
\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 /* The receive timeout is set shorter when the windows simulator is used
\r
130 because simulated time is slower than real time. */
\r
132 const TickType_t xReceiveTimeOut = 50 / portTICK_RATE_MS;
\r
134 const TickType_t xReceiveTimeOut = 500 / portTICK_RATE_MS;
\r
137 /*-----------------------------------------------------------*/
\r
139 void vStartEchoClientTasks( uint16_t usTaskStackSize, UBaseType_t uxTaskPriority )
\r
141 /* Create the echo client task that does not use the zero copy interface. */
\r
142 xTaskCreate( prvEchoClientTask, /* The function that implements the task. */
\r
143 "Echo0", /* Just a text name for the task to aid debugging. */
\r
144 usTaskStackSize, /* The stack size is defined in FreeRTOSIPConfig.h. */
\r
145 NULL, /* The task parameter, not used in this case. */
\r
146 uxTaskPriority, /* The priority assigned to the task is defined in FreeRTOSConfig.h. */
\r
147 NULL ); /* The task handle is not used. */
\r
149 /* Create the echo client task that does use the zero copy interface. */
\r
150 xTaskCreate( prvZeroCopyEchoClientTask, /* The function that implements the task. */
\r
151 "Echo1", /* Just a text name for the task to aid debugging. */
\r
152 usTaskStackSize, /* The stack size is defined in FreeRTOSIPConfig.h. */
\r
153 NULL, /* The task parameter, not used in this case. */
\r
154 uxTaskPriority, /* The priority assigned to the task is defined in FreeRTOSConfig.h. */
\r
155 NULL ); /* The task handle is not used. */
\r
157 /*-----------------------------------------------------------*/
\r
159 static void prvEchoClientTask( void *pvParameters )
\r
162 struct freertos_sockaddr xEchoServerAddress;
\r
163 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
164 int32_t lLoopCount = 0UL;
\r
165 const int32_t lMaxLoopCount = 50;
\r
166 volatile uint32_t ulRxCount = 0UL, ulTxCount = 0UL;
\r
167 uint32_t xAddressLength = sizeof( xEchoServerAddress );
\r
169 /* Remove compiler warning about unused parameters. */
\r
170 ( void ) pvParameters;
\r
172 /* Echo requests are sent to the echo server. The address of the echo
\r
173 server is configured by the constants configECHO_SERVER_ADDR0 to
\r
174 configECHO_SERVER_ADDR3 in FreeRTOSConfig.h. */
\r
175 xEchoServerAddress.sin_port = FreeRTOS_htons( echoECHO_PORT );
\r
176 xEchoServerAddress.sin_addr = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0,
\r
177 configECHO_SERVER_ADDR1,
\r
178 configECHO_SERVER_ADDR2,
\r
179 configECHO_SERVER_ADDR3 );
\r
183 /* Create a socket. */
\r
184 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
185 configASSERT( xSocket != FREERTOS_INVALID_SOCKET );
\r
187 /* Set a time out so a missing reply does not cause the task to block
\r
189 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );
\r
191 /* Send a number of echo requests. */
\r
192 for( lLoopCount = 0; lLoopCount < lMaxLoopCount; lLoopCount++ )
\r
194 /* Create the string that is sent to the echo server. */
\r
195 sprintf( cTxString, "Message number %u\r\n", ( unsigned int ) ulTxCount );
\r
197 /* Send the string to the socket. ulFlags is set to 0, so the zero
\r
198 copy interface is not used. That means the data from cTxString is
\r
199 copied into a network buffer inside FreeRTOS_sendto(), and cTxString
\r
200 can be reused as soon as FreeRTOS_sendto() has returned. 1 is added
\r
201 to ensure the NULL string terminator is sent as part of the message. */
\r
202 FreeRTOS_sendto( xSocket, /* The socket being sent to. */
\r
203 ( void * ) cTxString, /* The data being sent. */
\r
204 strlen( cTxString ) + 1,/* The length of the data being sent. */
\r
205 0, /* ulFlags with the FREERTOS_ZERO_COPY bit clear. */
\r
206 &xEchoServerAddress, /* The destination address. */
\r
207 sizeof( xEchoServerAddress ) );
\r
209 /* Keep a count of how many echo requests have been transmitted so
\r
210 it can be compared to the number of echo replies received. It would
\r
211 be expected to loose at least one to an ARP message the first time
\r
212 the connection is created. */
\r
215 /* Receive data echoed back to the socket. ulFlags is zero, so the
\r
216 zero copy option is not being used and the received data will be
\r
217 copied into the buffer pointed to by cRxString. xAddressLength is
\r
218 not actually used (at the time of writing this comment, anyway) by
\r
219 FreeRTOS_recvfrom(), but is set appropriately in case future
\r
220 versions do use it. */
\r
221 memset( ( void * ) cRxString, 0x00, sizeof( cRxString ) );
\r
222 FreeRTOS_recvfrom( xSocket, /* The socket being received from. */
\r
223 cRxString, /* The buffer into which the received data will be written. */
\r
224 sizeof( cRxString ), /* The size of the buffer provided to receive the data. */
\r
225 0, /* ulFlags with the FREERTOS_ZERO_COPY bit clear. */
\r
226 &xEchoServerAddress, /* The address from where the data was sent (the source address). */
\r
229 /* Compare the transmitted string to the received string. */
\r
230 if( strcmp( cRxString, cTxString ) == 0 )
\r
232 /* The echo reply was received without error. */
\r
237 /* Pause for a short while to ensure the network is not too
\r
239 vTaskDelay( echoLOOP_DELAY );
\r
241 /* Close this socket before looping back to create another. */
\r
242 FreeRTOS_closesocket( xSocket );
\r
245 /*-----------------------------------------------------------*/
\r
247 static void prvZeroCopyEchoClientTask( void *pvParameters )
\r
250 struct freertos_sockaddr xEchoServerAddress;
\r
251 static char cTxString[ 40 ];
\r
252 int32_t lLoopCount = 0UL;
\r
253 volatile uint32_t ulRxCount = 0UL, ulTxCount = 0UL;
\r
254 uint32_t xAddressLength = sizeof( xEchoServerAddress );
\r
256 uint8_t *pucUDPPayloadBuffer;
\r
258 const int32_t lMaxLoopCount = 50;
\r
259 const char * const pcStringToSend = "Zero copy message number";
\r
260 /* The buffer is large enough to hold the string, a number, and the string terminator. */
\r
261 const size_t xBufferLength = strlen( pcStringToSend ) + 15;
\r
263 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1
\r
265 /* When the trace recorder code is included user events are generated to
\r
266 mark the sending and receiving of the echoed data (only in the zero copy
\r
268 xZeroCopySendEvent = xTraceOpenLabel( "ZeroCopyTx" );
\r
269 xZeroCopyReceiveEvent = xTraceOpenLabel( "ZeroCopyRx" );
\r
271 #endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS */
\r
273 /* Remove compiler warning about unused parameters. */
\r
274 ( void ) pvParameters;
\r
276 /* Delay for a little while to ensure the task is out of synch with the
\r
277 other echo task implemented above. */
\r
278 vTaskDelay( echoLOOP_DELAY >> 1 );
\r
280 /* Echo requests are sent to the echo server. The address of the echo
\r
281 server is configured by the constants configECHO_SERVER_ADDR0 to
\r
282 configECHO_SERVER_ADDR3 in FreeRTOSConfig.h. */
\r
283 xEchoServerAddress.sin_port = FreeRTOS_htons( echoECHO_PORT );
\r
284 xEchoServerAddress.sin_addr = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0,
\r
285 configECHO_SERVER_ADDR1,
\r
286 configECHO_SERVER_ADDR2,
\r
287 configECHO_SERVER_ADDR3 );
\r
291 /* Create a socket. */
\r
292 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
293 configASSERT( xSocket != FREERTOS_INVALID_SOCKET );
\r
295 /* Set a time out so a missing reply does not cause the task to block
\r
297 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );
\r
299 /* Send a number of echo requests. */
\r
300 for( lLoopCount = 0; lLoopCount < lMaxLoopCount; lLoopCount++ )
\r
302 /* This task is going to send using the zero copy interface. The
\r
303 data being sent is therefore written directly into a buffer that is
\r
304 passed by reference into the FreeRTOS_sendto() function. First
\r
305 obtain a buffer of adequate size from the IP stack. Although a max
\r
306 delay is used, the actual delay will be capped to
\r
307 ipconfigMAX_SEND_BLOCK_TIME_TICKS, hence the test to ensure a buffer
\r
308 was actually obtained. */
\r
309 pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xBufferLength, portMAX_DELAY );
\r
311 if( pucUDPPayloadBuffer != NULL )
\r
313 /* A buffer was successfully obtained. Create the string that is
\r
314 sent to the echo server. Note the string is written directly
\r
315 into the buffer obtained from the IP stack. */
\r
316 sprintf( ( char * ) pucUDPPayloadBuffer, "%s %u\r\n", "Zero copy message number", ( unsigned int ) ulTxCount );
\r
318 /* Also copy the string into a local buffer so it can be compared
\r
319 with the string that is later received back from the echo server. */
\r
320 strcpy( cTxString, ( char * ) pucUDPPayloadBuffer );
\r
322 /* Pass the buffer into the send function. ulFlags has the
\r
323 FREERTOS_ZERO_COPY bit set so the IP stack will take control of
\r
324 the buffer, rather than copy data out of the buffer. */
\r
325 echoMARK_SEND_IN_TRACE_BUFFER( xZeroCopySendEvent );
\r
326 lReturned = FreeRTOS_sendto( xSocket, /* The socket being sent to. */
\r
327 ( void * ) pucUDPPayloadBuffer, /* The buffer being passed into the IP stack. */
\r
328 strlen( cTxString ) + 1, /* The length of the data being sent. Plus 1 to ensure the null terminator is part of the data. */
\r
329 FREERTOS_ZERO_COPY, /* ulFlags with the zero copy bit is set. */
\r
330 &xEchoServerAddress, /* Where the data is being sent. */
\r
331 sizeof( xEchoServerAddress ) );
\r
333 if( lReturned == 0 )
\r
335 /* The send operation failed, so this task is still
\r
336 responsible for the buffer obtained from the IP stack. To
\r
337 ensure the buffer is not lost it must either be used again,
\r
338 or, as in this case, returned to the IP stack using
\r
339 FreeRTOS_ReleaseUDPPayloadBuffer(). pucUDPPayloadBuffer can
\r
340 be safely re-used to receive from the socket below once the
\r
341 buffer has been returned to the stack. */
\r
342 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );
\r
346 /* The send was successful so the IP stack is now managing
\r
347 the buffer pointed to by pucUDPPayloadBuffer, and the IP
\r
348 stack will return the buffer once it has been sent.
\r
349 pucUDPPayloadBuffer can be safely re-used to receive from
\r
350 the socket below. */
\r
353 /* Keep a count of how many echo requests have been transmitted
\r
354 so it can be compared to the number of echo replies received.
\r
355 It would be expected to loose at least one to an ARP message the
\r
356 first time the connection is created. */
\r
359 /* Receive data on the socket. ulFlags has the zero copy bit set
\r
360 (FREERTOS_ZERO_COPY) indicating to the stack that a reference to
\r
361 the received data should be passed out to this task using the
\r
362 second parameter to the FreeRTOS_recvfrom() call. When this is
\r
363 done the IP stack is no longer responsible for releasing the
\r
364 buffer, and the task *must* return the buffer to the stack when
\r
365 it is no longer needed. By default the receive block time is
\r
367 echoMARK_SEND_IN_TRACE_BUFFER( xZeroCopyReceiveEvent );
\r
368 lReturned = FreeRTOS_recvfrom( xSocket, /* The socket to receive from. */
\r
369 ( void * ) &pucUDPPayloadBuffer, /* pucUDPPayloadBuffer will be set to point to the buffer that already contains the received data. */
\r
370 0, /* Ignored because the zero copy interface is being used. */
\r
371 FREERTOS_ZERO_COPY, /* ulFlags with the FREERTOS_ZERO_COPY bit set. */
\r
372 &xEchoServerAddress, /* The address from which the data was sent. */
\r
375 if( lReturned > 0 )
\r
377 /* Compare the string sent to the echo server with the string
\r
378 received back from the echo server. */
\r
379 if( strcmp( ( char * ) pucUDPPayloadBuffer, cTxString ) == 0 )
\r
381 /* The strings matched. */
\r
385 /* The buffer that contains the data passed out of the stack
\r
386 *must* be returned to the stack. */
\r
387 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
\r
392 /* Pause for a short while to ensure the network is not too
\r
394 vTaskDelay( echoLOOP_DELAY );
\r
396 /* Close this socket before looping back to create another. */
\r
397 FreeRTOS_closesocket( xSocket );
\r
400 /*-----------------------------------------------------------*/
\r