]> git.sur5r.net Git - freertos/blob
378f2ff787d4f319e3d0d4744c1b2a66589d04e6
[freertos] /
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
15  *\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
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\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
35  *\r
36  * See the following web page for essential demo usage and configuration\r
37  * details:\r
38  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html\r
39  */\r
40 \r
41 /* Standard includes. */\r
42 #include <stdint.h>\r
43 #include <stdio.h>\r
44 #include <stdlib.h>\r
45 \r
46 /* FreeRTOS includes. */\r
47 #include "FreeRTOS.h"\r
48 #include "task.h"\r
49 #include "queue.h"\r
50 \r
51 /* FreeRTOS+TCP includes. */\r
52 #include "FreeRTOS_IP.h"\r
53 #include "FreeRTOS_Sockets.h"\r
54 \r
55 /* Exclude the whole file if FreeRTOSIPConfig.h is configured to use UDP only. */\r
56 #if( ipconfigUSE_TCP == 1 )\r
57 \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
61 congested. */\r
62 #define echoLOOP_DELAY  ( ( TickType_t ) 150 / portTICK_PERIOD_MS )\r
63 \r
64 /* The echo server is assumed to be on port 7, which is the standard echo\r
65 protocol port. */\r
66 #define echoECHO_PORT   ( 7 )\r
67 \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
72 \r
73 /* The number of instances of the echo client task to create. */\r
74 #define echoNUM_ECHO_CLIENTS            ( 5 )\r
75 \r
76 /*-----------------------------------------------------------*/\r
77 \r
78 /*\r
79  * Uses a socket to send data to, then receive data from, the standard echo\r
80  * port number 7.\r
81  */\r
82 static void prvEchoClientTask( void *pvParameters );\r
83 \r
84 /*\r
85  * Creates a pseudo random sized buffer of data to send to the echo server.\r
86  */\r
87 static BaseType_t prvCreateTxData( char *ucBuffer, uint32_t ulBufferLength );\r
88 \r
89 /*-----------------------------------------------------------*/\r
90 \r
91 /* Rx and Tx time outs are used to ensure the sockets do not wait too long for\r
92 missing data. */\r
93 static const TickType_t xReceiveTimeOut = pdMS_TO_TICKS( 4000 );\r
94 static const TickType_t xSendTimeOut = pdMS_TO_TICKS( 2000 );\r
95 \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
100 \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
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 void vStartTCPEchoClientTasks_SingleTasks( uint16_t usTaskStackSize, UBaseType_t uxTaskPriority )\r
108 {\r
109 BaseType_t x;\r
110 \r
111         /* Create the echo client tasks. */\r
112         for( x = 0; x < echoNUM_ECHO_CLIENTS; x++ )\r
113         {\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
120         }\r
121 }\r
122 /*-----------------------------------------------------------*/\r
123 \r
124 static void prvEchoClientTask( void *pvParameters )\r
125 {\r
126 Socket_t xSocket;\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
136 \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
142 \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
147 \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
151 \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
160 \r
161         for( ;; )\r
162         {\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
166 \r
167                 /* Set a time out so a missing reply does not cause the task to block\r
168                 indefinitely. */\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
171 \r
172                 /* Set the window and buffer sizes. */\r
173                 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_WIN_PROPERTIES, ( void * ) &xWinProps,     sizeof( xWinProps ) );\r
174 \r
175                 /* Connect to the echo server. */\r
176                 if( FreeRTOS_connect( xSocket, &xEchoServerAddress, sizeof( xEchoServerAddress ) ) == 0 )\r
177                 {\r
178                         ulConnections[ xInstance ]++;\r
179 \r
180                         /* Send a number of echo requests. */\r
181                         for( lLoopCount = 0; lLoopCount < lMaxLoopCount; lLoopCount++ )\r
182                         {\r
183                                 /* Create the string that is sent to the echo server. */\r
184                                 lStringLength = prvCreateTxData( pcTransmittedString, echoBUFFER_SIZES );\r
185 \r
186                                 /* Add in some unique text at the front of the string. */\r
187                                 sprintf( pcTransmittedString, "TxRx message number %u", ulTxCount );\r
188                                 ulTxCount++;\r
189 \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
195 \r
196                                 if( lTransmitted < 0 )\r
197                                 {\r
198                                         /* Error? */\r
199                                         break;\r
200                                 }\r
201 \r
202                                 /* Clear the buffer into which the echoed string will be\r
203                                 placed. */\r
204                                 memset( ( void * ) pcReceivedString, 0x00, echoBUFFER_SIZES );\r
205                                 xReceivedBytes = 0;\r
206 \r
207                                 /* Receive data echoed back to the socket. */\r
208                                 while( xReceivedBytes < lTransmitted )\r
209                                 {\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
214 \r
215                                         if( xReturned < 0 )\r
216                                         {\r
217                                                 /* Error occurred.  Latch it so it can be detected\r
218                                                 below. */\r
219                                                 xReceivedBytes = xReturned;\r
220                                                 break;\r
221                                         }\r
222                                         else if( xReturned == 0 )\r
223                                         {\r
224                                                 /* Timed out. */\r
225                                                 break;\r
226                                         }\r
227                                         else\r
228                                         {\r
229                                                 /* Keep a count of the bytes received so far. */\r
230                                                 xReceivedBytes += xReturned;\r
231                                         }\r
232                                 }\r
233 \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
238                                 {\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
242                                         {\r
243                                                 /* The echo reply was received without error. */\r
244                                                 ulTxRxCycles[ xInstance ]++;\r
245                                         }\r
246                                         else\r
247                                         {\r
248                                                 /* The received string did not match the transmitted\r
249                                                 string. */\r
250                                                 ulTxRxFailures[ xInstance ]++;\r
251                                                 break;\r
252                                         }\r
253                                 }\r
254                                 else if( xReceivedBytes < 0 )\r
255                                 {\r
256                                         /* FreeRTOS_recv() returned an error. */\r
257                                         break;\r
258                                 }\r
259                                 else\r
260                                 {\r
261                                         /* Timed out without receiving anything? */\r
262                                         break;\r
263                                 }\r
264                         }\r
265 \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
269 \r
270                         /* Expect FreeRTOS_recv() to return an error once the shutdown is\r
271                         complete. */\r
272                         xTimeOnEntering = xTaskGetTickCount();\r
273                         do\r
274                         {\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
278                                         0 );\r
279 \r
280                                 if( xReturned < 0 )\r
281                                 {\r
282                                         break;\r
283                                 }\r
284 \r
285                         } while( ( xTaskGetTickCount() - xTimeOnEntering ) < xReceiveTimeOut );\r
286                 }\r
287 \r
288                 /* Close this socket before looping back to create another. */\r
289                 FreeRTOS_closesocket( xSocket );\r
290 \r
291                 /* Pause for a short while to ensure the network is not too\r
292                 congested. */\r
293                 vTaskDelay( echoLOOP_DELAY );\r
294         }\r
295 }\r
296 /*-----------------------------------------------------------*/\r
297 \r
298 static BaseType_t prvCreateTxData( char *cBuffer, uint32_t ulBufferLength )\r
299 {\r
300 BaseType_t lCharactersToAdd, lCharacter;\r
301 char cChar = '0';\r
302 const BaseType_t lMinimumLength = 60;\r
303 \r
304         /* Randomise the number of characters that will be sent in the echo\r
305         request. */\r
306         do\r
307         {\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
310 \r
311         /* Fill the buffer. */\r
312         for( lCharacter = 0; lCharacter < lCharactersToAdd; lCharacter++ )\r
313         {\r
314                 cBuffer[ lCharacter ] = cChar;\r
315                 cChar++;\r
316 \r
317                 if( cChar > '~' )\r
318                 {\r
319                         cChar = '0';\r
320                 }\r
321         }\r
322 \r
323         return lCharactersToAdd;\r
324 }\r
325 /*-----------------------------------------------------------*/\r
326 \r
327 BaseType_t xAreSingleTaskTCPEchoClientsStillRunning( void )\r
328 {\r
329 static uint32_t ulLastEchoSocketCount[ echoNUM_ECHO_CLIENTS ] = { 0 }, ulLastConnections[ echoNUM_ECHO_CLIENTS ] = { 0 };\r
330 BaseType_t xReturn = pdPASS, x;\r
331 \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
335         {\r
336                 if( ulTxRxCycles[ x ] == ulLastEchoSocketCount[ x ] )\r
337                 {\r
338                         xReturn = pdFAIL;\r
339                 }\r
340                 else\r
341                 {\r
342                         ulLastEchoSocketCount[ x ] = ulTxRxCycles[ x ];\r
343                 }\r
344 \r
345                 if( ulConnections[ x ] == ulLastConnections[ x ] )\r
346                 {\r
347                         xReturn = pdFAIL;\r
348                 }\r
349                 else\r
350                 {\r
351                         ulConnections[ x ] = ulLastConnections[ x ];\r
352                 }\r
353         }\r
354 \r
355         return xReturn;\r
356 }\r
357 \r
358 #endif /* ipconfigUSE_TCP */\r
359 \r