2 * FreeRTOS Kernel V10.2.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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 * A set of tasks are created that send TCP echo requests to the standard echo
\r
30 * port (port 7) on the IP address set by the configECHO_SERVER_ADDR0 to
\r
31 * configECHO_SERVER_ADDR3 constants, then wait for and verify the reply
\r
32 * (another demo is avilable that demonstrates the reception being performed in
\r
33 * a task other than that from with the request was made).
\r
35 * See the following web page for essential demo usage and configuration
\r
37 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html
\r
40 /* Standard includes. */
\r
45 /* FreeRTOS includes. */
\r
46 #include "FreeRTOS.h"
\r
50 /* FreeRTOS+TCP includes. */
\r
51 #include "FreeRTOS_IP.h"
\r
52 #include "FreeRTOS_Sockets.h"
\r
54 /* Exclude the whole file if FreeRTOSIPConfig.h is configured to use UDP only. */
\r
55 #if( ipconfigUSE_TCP == 1 )
\r
57 /* The echo tasks create a socket, send out a number of echo requests, listen
\r
58 for the echo reply, then close the socket again before starting over. This
\r
59 delay is used between each iteration to ensure the network does not get too
\r
61 #define echoLOOP_DELAY ( ( TickType_t ) 150 / portTICK_PERIOD_MS )
\r
63 /* The echo server is assumed to be on port 7, which is the standard echo
\r
65 #define echoECHO_PORT ( 7 )
\r
67 /* The size of the buffers is a multiple of the MSS - the length of the data
\r
68 sent is a pseudo random size between 20 and echoBUFFER_SIZES. */
\r
69 #define echoBUFFER_SIZE_MULTIPLIER ( 3 )
\r
70 #define echoBUFFER_SIZES ( ipconfigTCP_MSS * echoBUFFER_SIZE_MULTIPLIER )
\r
72 /* The number of instances of the echo client task to create. */
\r
73 #define echoNUM_ECHO_CLIENTS ( 5 )
\r
75 /*-----------------------------------------------------------*/
\r
78 * Uses a socket to send data to, then receive data from, the standard echo
\r
81 static void prvEchoClientTask( void *pvParameters );
\r
84 * Creates a pseudo random sized buffer of data to send to the echo server.
\r
86 static BaseType_t prvCreateTxData( char *ucBuffer, uint32_t ulBufferLength );
\r
88 /*-----------------------------------------------------------*/
\r
90 /* Rx and Tx time outs are used to ensure the sockets do not wait too long for
\r
92 static const TickType_t xReceiveTimeOut = pdMS_TO_TICKS( 4000 );
\r
93 static const TickType_t xSendTimeOut = pdMS_TO_TICKS( 2000 );
\r
95 /* Counters for each created task - for inspection only. */
\r
96 static uint32_t ulTxRxCycles[ echoNUM_ECHO_CLIENTS ] = { 0 },
\r
97 ulTxRxFailures[ echoNUM_ECHO_CLIENTS ] = { 0 },
\r
98 ulConnections[ echoNUM_ECHO_CLIENTS ] = { 0 };
\r
100 /* Rx and Tx buffers for each created task. */
\r
101 static char cTxBuffers[ echoNUM_ECHO_CLIENTS ][ echoBUFFER_SIZES ],
\r
102 cRxBuffers[ echoNUM_ECHO_CLIENTS ][ echoBUFFER_SIZES ];
\r
104 /*-----------------------------------------------------------*/
\r
106 void vStartTCPEchoClientTasks_SingleTasks( uint16_t usTaskStackSize, UBaseType_t uxTaskPriority )
\r
110 /* Create the echo client tasks. */
\r
111 for( x = 0; x < echoNUM_ECHO_CLIENTS; x++ )
\r
113 xTaskCreate( prvEchoClientTask, /* The function that implements the task. */
\r
114 "Echo0", /* Just a text name for the task to aid debugging. */
\r
115 usTaskStackSize, /* The stack size is defined in FreeRTOSIPConfig.h. */
\r
116 ( void * ) x, /* The task parameter, not used in this case. */
\r
117 uxTaskPriority, /* The priority assigned to the task is defined in FreeRTOSConfig.h. */
\r
118 NULL ); /* The task handle is not used. */
\r
121 /*-----------------------------------------------------------*/
\r
123 static void prvEchoClientTask( void *pvParameters )
\r
126 struct freertos_sockaddr xEchoServerAddress;
\r
127 int32_t lLoopCount = 0UL;
\r
128 const int32_t lMaxLoopCount = 1;
\r
129 volatile uint32_t ulTxCount = 0UL;
\r
130 BaseType_t xReceivedBytes, xReturned, xInstance;
\r
131 BaseType_t lTransmitted, lStringLength;
\r
132 char *pcTransmittedString, *pcReceivedString;
\r
133 WinProperties_t xWinProps;
\r
134 TickType_t xTimeOnEntering;
\r
136 /* Fill in the buffer and window sizes that will be used by the socket. */
\r
137 xWinProps.lTxBufSize = 6 * ipconfigTCP_MSS;
\r
138 xWinProps.lTxWinSize = 3;
\r
139 xWinProps.lRxBufSize = 6 * ipconfigTCP_MSS;
\r
140 xWinProps.lRxWinSize = 3;
\r
142 /* This task can be created a number of times. Each instance is numbered
\r
143 to enable each instance to use a different Rx and Tx buffer. The number is
\r
144 passed in as the task's parameter. */
\r
145 xInstance = ( BaseType_t ) pvParameters;
\r
147 /* Point to the buffers to be used by this instance of this task. */
\r
148 pcTransmittedString = &( cTxBuffers[ xInstance ][ 0 ] );
\r
149 pcReceivedString = &( cRxBuffers[ xInstance ][ 0 ] );
\r
151 /* Echo requests are sent to the echo server. The address of the echo
\r
152 server is configured by the constants configECHO_SERVER_ADDR0 to
\r
153 configECHO_SERVER_ADDR3 in FreeRTOSConfig.h. */
\r
154 xEchoServerAddress.sin_port = FreeRTOS_htons( echoECHO_PORT );
\r
155 xEchoServerAddress.sin_addr = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0,
\r
156 configECHO_SERVER_ADDR1,
\r
157 configECHO_SERVER_ADDR2,
\r
158 configECHO_SERVER_ADDR3 );
\r
162 /* Create a TCP socket. */
\r
163 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP );
\r
164 configASSERT( xSocket != FREERTOS_INVALID_SOCKET );
\r
166 /* Set a time out so a missing reply does not cause the task to block
\r
168 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );
\r
169 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendTimeOut, sizeof( xSendTimeOut ) );
\r
171 /* Set the window and buffer sizes. */
\r
172 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_WIN_PROPERTIES, ( void * ) &xWinProps, sizeof( xWinProps ) );
\r
174 /* Connect to the echo server. */
\r
175 if( FreeRTOS_connect( xSocket, &xEchoServerAddress, sizeof( xEchoServerAddress ) ) == 0 )
\r
177 ulConnections[ xInstance ]++;
\r
179 /* Send a number of echo requests. */
\r
180 for( lLoopCount = 0; lLoopCount < lMaxLoopCount; lLoopCount++ )
\r
182 /* Create the string that is sent to the echo server. */
\r
183 lStringLength = prvCreateTxData( pcTransmittedString, echoBUFFER_SIZES );
\r
185 /* Add in some unique text at the front of the string. */
\r
186 sprintf( pcTransmittedString, "TxRx message number %u", ulTxCount );
\r
189 /* Send the string to the socket. */
\r
190 lTransmitted = FreeRTOS_send( xSocket, /* The socket being sent to. */
\r
191 ( void * ) pcTransmittedString, /* The data being sent. */
\r
192 lStringLength, /* The length of the data being sent. */
\r
193 0 ); /* No flags. */
\r
195 if( lTransmitted < 0 )
\r
201 /* Clear the buffer into which the echoed string will be
\r
203 memset( ( void * ) pcReceivedString, 0x00, echoBUFFER_SIZES );
\r
204 xReceivedBytes = 0;
\r
206 /* Receive data echoed back to the socket. */
\r
207 while( xReceivedBytes < lTransmitted )
\r
209 xReturned = FreeRTOS_recv( xSocket, /* The socket being received from. */
\r
210 &( pcReceivedString[ xReceivedBytes ] ),/* The buffer into which the received data will be written. */
\r
211 lStringLength - xReceivedBytes, /* The size of the buffer provided to receive the data. */
\r
212 0 ); /* No flags. */
\r
214 if( xReturned < 0 )
\r
216 /* Error occurred. Latch it so it can be detected
\r
218 xReceivedBytes = xReturned;
\r
221 else if( xReturned == 0 )
\r
228 /* Keep a count of the bytes received so far. */
\r
229 xReceivedBytes += xReturned;
\r
233 /* If an error occurred it will be latched in xReceivedBytes,
\r
234 otherwise xReceived bytes will be just that - the number of
\r
235 bytes received from the echo server. */
\r
236 if( xReceivedBytes > 0 )
\r
238 /* Compare the transmitted string to the received string. */
\r
239 configASSERT( strncmp( pcReceivedString, pcTransmittedString, lTransmitted ) == 0 );
\r
240 if( strncmp( pcReceivedString, pcTransmittedString, lTransmitted ) == 0 )
\r
242 /* The echo reply was received without error. */
\r
243 ulTxRxCycles[ xInstance ]++;
\r
247 /* The received string did not match the transmitted
\r
249 ulTxRxFailures[ xInstance ]++;
\r
253 else if( xReceivedBytes < 0 )
\r
255 /* FreeRTOS_recv() returned an error. */
\r
260 /* Timed out without receiving anything? */
\r
265 /* Finished using the connected socket, initiate a graceful close:
\r
266 FIN, FIN+ACK, ACK. */
\r
267 FreeRTOS_shutdown( xSocket, FREERTOS_SHUT_RDWR );
\r
269 /* Expect FreeRTOS_recv() to return an error once the shutdown is
\r
271 xTimeOnEntering = xTaskGetTickCount();
\r
274 xReturned = FreeRTOS_recv( xSocket, /* The socket being received from. */
\r
275 &( pcReceivedString[ 0 ] ), /* The buffer into which the received data will be written. */
\r
276 echoBUFFER_SIZES, /* The size of the buffer provided to receive the data. */
\r
279 if( xReturned < 0 )
\r
284 } while( ( xTaskGetTickCount() - xTimeOnEntering ) < xReceiveTimeOut );
\r
287 /* Close this socket before looping back to create another. */
\r
288 FreeRTOS_closesocket( xSocket );
\r
290 /* Pause for a short while to ensure the network is not too
\r
292 vTaskDelay( echoLOOP_DELAY );
\r
295 /*-----------------------------------------------------------*/
\r
297 static BaseType_t prvCreateTxData( char *cBuffer, uint32_t ulBufferLength )
\r
299 BaseType_t lCharactersToAdd, lCharacter;
\r
301 const BaseType_t lMinimumLength = 60;
\r
303 /* Randomise the number of characters that will be sent in the echo
\r
307 lCharactersToAdd = ipconfigRAND32() % ( ulBufferLength - 20UL );
\r
308 } while ( ( lCharactersToAdd == 0 ) || ( lCharactersToAdd < lMinimumLength ) ); /* Must be at least enough to add the unique text to the start of the string later. */
\r
310 /* Fill the buffer. */
\r
311 for( lCharacter = 0; lCharacter < lCharactersToAdd; lCharacter++ )
\r
313 cBuffer[ lCharacter ] = cChar;
\r
322 return lCharactersToAdd;
\r
324 /*-----------------------------------------------------------*/
\r
326 BaseType_t xAreSingleTaskTCPEchoClientsStillRunning( void )
\r
328 static uint32_t ulLastEchoSocketCount[ echoNUM_ECHO_CLIENTS ] = { 0 }, ulLastConnections[ echoNUM_ECHO_CLIENTS ] = { 0 };
\r
329 BaseType_t xReturn = pdPASS, x;
\r
331 /* Return fail is the number of cycles does not increment between
\r
332 consecutive calls. */
\r
333 for( x = 0; x < echoNUM_ECHO_CLIENTS; x++ )
\r
335 if( ulTxRxCycles[ x ] == ulLastEchoSocketCount[ x ] )
\r
341 ulLastEchoSocketCount[ x ] = ulTxRxCycles[ x ];
\r
344 if( ulConnections[ x ] == ulLastConnections[ x ] )
\r
350 ulConnections[ x ] = ulLastConnections[ x ];
\r
357 #endif /* ipconfigUSE_TCP */
\r