2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * A set of tasks are created that send TCP echo requests to the standard echo
\r
31 * port (port 7) on the IP address set by the configECHO_SERVER_ADDR0 to
\r
32 * configECHO_SERVER_ADDR3 constants, then wait for and verify the reply
\r
33 * (another demo is avilable that demonstrates the reception being performed in
\r
34 * a task other than that from with the request was made).
\r
36 * See the following web page for essential demo usage and configuration
\r
38 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html
\r
41 /* Standard includes. */
\r
46 /* FreeRTOS includes. */
\r
47 #include "FreeRTOS.h"
\r
51 /* FreeRTOS+TCP includes. */
\r
52 #include "FreeRTOS_IP.h"
\r
53 #include "FreeRTOS_Sockets.h"
\r
55 /* Exclude the whole file if FreeRTOSIPConfig.h is configured to use UDP only. */
\r
56 #if( ipconfigUSE_TCP == 1 )
\r
58 /* The echo tasks create a socket, send out a number of echo requests, listen
\r
59 for the echo reply, then close the socket again before starting over. This
\r
60 delay is used between each iteration to ensure the network does not get too
\r
62 #define echoLOOP_DELAY ( ( TickType_t ) 150 / portTICK_PERIOD_MS )
\r
64 /* The echo server is assumed to be on port 7, which is the standard echo
\r
66 #define echoECHO_PORT ( 7 )
\r
68 /* The size of the buffers is a multiple of the MSS - the length of the data
\r
69 sent is a pseudo random size between 20 and echoBUFFER_SIZES. */
\r
70 #define echoBUFFER_SIZE_MULTIPLIER ( 3 )
\r
71 #define echoBUFFER_SIZES ( ipconfigTCP_MSS * echoBUFFER_SIZE_MULTIPLIER )
\r
73 /* The number of instances of the echo client task to create. */
\r
74 #define echoNUM_ECHO_CLIENTS ( 5 )
\r
76 /*-----------------------------------------------------------*/
\r
79 * Uses a socket to send data to, then receive data from, the standard echo
\r
82 static void prvEchoClientTask( void *pvParameters );
\r
85 * Creates a pseudo random sized buffer of data to send to the echo server.
\r
87 static BaseType_t prvCreateTxData( char *ucBuffer, uint32_t ulBufferLength );
\r
89 /*-----------------------------------------------------------*/
\r
91 /* Rx and Tx time outs are used to ensure the sockets do not wait too long for
\r
93 static const TickType_t xReceiveTimeOut = pdMS_TO_TICKS( 4000 );
\r
94 static const TickType_t xSendTimeOut = pdMS_TO_TICKS( 2000 );
\r
96 /* Counters for each created task - for inspection only. */
\r
97 static uint32_t ulTxRxCycles[ echoNUM_ECHO_CLIENTS ] = { 0 },
\r
98 ulTxRxFailures[ echoNUM_ECHO_CLIENTS ] = { 0 },
\r
99 ulConnections[ echoNUM_ECHO_CLIENTS ] = { 0 };
\r
101 /* Rx and Tx buffers for each created task. */
\r
102 static char cTxBuffers[ echoNUM_ECHO_CLIENTS ][ echoBUFFER_SIZES ],
\r
103 cRxBuffers[ echoNUM_ECHO_CLIENTS ][ echoBUFFER_SIZES ];
\r
105 /*-----------------------------------------------------------*/
\r
107 void vStartTCPEchoClientTasks_SingleTasks( uint16_t usTaskStackSize, UBaseType_t uxTaskPriority )
\r
111 /* Create the echo client tasks. */
\r
112 for( x = 0; x < echoNUM_ECHO_CLIENTS; x++ )
\r
114 xTaskCreate( prvEchoClientTask, /* The function that implements the task. */
\r
115 "Echo0", /* Just a text name for the task to aid debugging. */
\r
116 usTaskStackSize, /* The stack size is defined in FreeRTOSIPConfig.h. */
\r
117 ( void * ) x, /* The task parameter, not used in this case. */
\r
118 uxTaskPriority, /* The priority assigned to the task is defined in FreeRTOSConfig.h. */
\r
119 NULL ); /* The task handle is not used. */
\r
122 /*-----------------------------------------------------------*/
\r
124 static void prvEchoClientTask( void *pvParameters )
\r
127 struct freertos_sockaddr xEchoServerAddress;
\r
128 int32_t lLoopCount = 0UL;
\r
129 const int32_t lMaxLoopCount = 1;
\r
130 volatile uint32_t ulTxCount = 0UL;
\r
131 BaseType_t xReceivedBytes, xReturned, xInstance;
\r
132 BaseType_t lTransmitted, lStringLength;
\r
133 char *pcTransmittedString, *pcReceivedString;
\r
134 WinProperties_t xWinProps;
\r
135 TickType_t xTimeOnEntering;
\r
137 /* Fill in the buffer and window sizes that will be used by the socket. */
\r
138 xWinProps.lTxBufSize = 6 * ipconfigTCP_MSS;
\r
139 xWinProps.lTxWinSize = 3;
\r
140 xWinProps.lRxBufSize = 6 * ipconfigTCP_MSS;
\r
141 xWinProps.lRxWinSize = 3;
\r
143 /* This task can be created a number of times. Each instance is numbered
\r
144 to enable each instance to use a different Rx and Tx buffer. The number is
\r
145 passed in as the task's parameter. */
\r
146 xInstance = ( BaseType_t ) pvParameters;
\r
148 /* Point to the buffers to be used by this instance of this task. */
\r
149 pcTransmittedString = &( cTxBuffers[ xInstance ][ 0 ] );
\r
150 pcReceivedString = &( cRxBuffers[ xInstance ][ 0 ] );
\r
152 /* Echo requests are sent to the echo server. The address of the echo
\r
153 server is configured by the constants configECHO_SERVER_ADDR0 to
\r
154 configECHO_SERVER_ADDR3 in FreeRTOSConfig.h. */
\r
155 xEchoServerAddress.sin_port = FreeRTOS_htons( echoECHO_PORT );
\r
156 xEchoServerAddress.sin_addr = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0,
\r
157 configECHO_SERVER_ADDR1,
\r
158 configECHO_SERVER_ADDR2,
\r
159 configECHO_SERVER_ADDR3 );
\r
163 /* Create a TCP socket. */
\r
164 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP );
\r
165 configASSERT( xSocket != FREERTOS_INVALID_SOCKET );
\r
167 /* Set a time out so a missing reply does not cause the task to block
\r
169 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );
\r
170 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendTimeOut, sizeof( xSendTimeOut ) );
\r
172 /* Set the window and buffer sizes. */
\r
173 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_WIN_PROPERTIES, ( void * ) &xWinProps, sizeof( xWinProps ) );
\r
175 /* Connect to the echo server. */
\r
176 if( FreeRTOS_connect( xSocket, &xEchoServerAddress, sizeof( xEchoServerAddress ) ) == 0 )
\r
178 ulConnections[ xInstance ]++;
\r
180 /* Send a number of echo requests. */
\r
181 for( lLoopCount = 0; lLoopCount < lMaxLoopCount; lLoopCount++ )
\r
183 /* Create the string that is sent to the echo server. */
\r
184 lStringLength = prvCreateTxData( pcTransmittedString, echoBUFFER_SIZES );
\r
186 /* Add in some unique text at the front of the string. */
\r
187 sprintf( pcTransmittedString, "TxRx message number %u", ulTxCount );
\r
190 /* Send the string to the socket. */
\r
191 lTransmitted = FreeRTOS_send( xSocket, /* The socket being sent to. */
\r
192 ( void * ) pcTransmittedString, /* The data being sent. */
\r
193 lStringLength, /* The length of the data being sent. */
\r
194 0 ); /* No flags. */
\r
196 if( lTransmitted < 0 )
\r
202 /* Clear the buffer into which the echoed string will be
\r
204 memset( ( void * ) pcReceivedString, 0x00, echoBUFFER_SIZES );
\r
205 xReceivedBytes = 0;
\r
207 /* Receive data echoed back to the socket. */
\r
208 while( xReceivedBytes < lTransmitted )
\r
210 xReturned = FreeRTOS_recv( xSocket, /* The socket being received from. */
\r
211 &( pcReceivedString[ xReceivedBytes ] ),/* The buffer into which the received data will be written. */
\r
212 lStringLength - xReceivedBytes, /* The size of the buffer provided to receive the data. */
\r
213 0 ); /* No flags. */
\r
215 if( xReturned < 0 )
\r
217 /* Error occurred. Latch it so it can be detected
\r
219 xReceivedBytes = xReturned;
\r
222 else if( xReturned == 0 )
\r
229 /* Keep a count of the bytes received so far. */
\r
230 xReceivedBytes += xReturned;
\r
234 /* If an error occurred it will be latched in xReceivedBytes,
\r
235 otherwise xReceived bytes will be just that - the number of
\r
236 bytes received from the echo server. */
\r
237 if( xReceivedBytes > 0 )
\r
239 /* Compare the transmitted string to the received string. */
\r
240 configASSERT( strncmp( pcReceivedString, pcTransmittedString, lTransmitted ) == 0 );
\r
241 if( strncmp( pcReceivedString, pcTransmittedString, lTransmitted ) == 0 )
\r
243 /* The echo reply was received without error. */
\r
244 ulTxRxCycles[ xInstance ]++;
\r
248 /* The received string did not match the transmitted
\r
250 ulTxRxFailures[ xInstance ]++;
\r
254 else if( xReceivedBytes < 0 )
\r
256 /* FreeRTOS_recv() returned an error. */
\r
261 /* Timed out without receiving anything? */
\r
266 /* Finished using the connected socket, initiate a graceful close:
\r
267 FIN, FIN+ACK, ACK. */
\r
268 FreeRTOS_shutdown( xSocket, FREERTOS_SHUT_RDWR );
\r
270 /* Expect FreeRTOS_recv() to return an error once the shutdown is
\r
272 xTimeOnEntering = xTaskGetTickCount();
\r
275 xReturned = FreeRTOS_recv( xSocket, /* The socket being received from. */
\r
276 &( pcReceivedString[ 0 ] ), /* The buffer into which the received data will be written. */
\r
277 echoBUFFER_SIZES, /* The size of the buffer provided to receive the data. */
\r
280 if( xReturned < 0 )
\r
285 } while( ( xTaskGetTickCount() - xTimeOnEntering ) < xReceiveTimeOut );
\r
288 /* Close this socket before looping back to create another. */
\r
289 FreeRTOS_closesocket( xSocket );
\r
291 /* Pause for a short while to ensure the network is not too
\r
293 vTaskDelay( echoLOOP_DELAY );
\r
296 /*-----------------------------------------------------------*/
\r
298 static BaseType_t prvCreateTxData( char *cBuffer, uint32_t ulBufferLength )
\r
300 BaseType_t lCharactersToAdd, lCharacter;
\r
302 const BaseType_t lMinimumLength = 60;
\r
304 /* Randomise the number of characters that will be sent in the echo
\r
308 lCharactersToAdd = ipconfigRAND32() % ( ulBufferLength - 20UL );
\r
309 } while ( ( lCharactersToAdd == 0 ) || ( lCharactersToAdd < lMinimumLength ) ); /* Must be at least enough to add the unique text to the start of the string later. */
\r
311 /* Fill the buffer. */
\r
312 for( lCharacter = 0; lCharacter < lCharactersToAdd; lCharacter++ )
\r
314 cBuffer[ lCharacter ] = cChar;
\r
323 return lCharactersToAdd;
\r
325 /*-----------------------------------------------------------*/
\r
327 BaseType_t xAreSingleTaskTCPEchoClientsStillRunning( void )
\r
329 static uint32_t ulLastEchoSocketCount[ echoNUM_ECHO_CLIENTS ] = { 0 }, ulLastConnections[ echoNUM_ECHO_CLIENTS ] = { 0 };
\r
330 BaseType_t xReturn = pdPASS, x;
\r
332 /* Return fail is the number of cycles does not increment between
\r
333 consecutive calls. */
\r
334 for( x = 0; x < echoNUM_ECHO_CLIENTS; x++ )
\r
336 if( ulTxRxCycles[ x ] == ulLastEchoSocketCount[ x ] )
\r
342 ulLastEchoSocketCount[ x ] = ulTxRxCycles[ x ];
\r
345 if( ulConnections[ x ] == ulLastConnections[ x ] )
\r
351 ulConnections[ x ] = ulLastConnections[ x ];
\r
358 #endif /* ipconfigUSE_TCP */
\r