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