]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/Common/FreeRTOS_Plus_UDP_Demos/EchoClients/TwoEchoClients.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS-Plus / Demo / Common / FreeRTOS_Plus_UDP_Demos / EchoClients / TwoEchoClients.c
1 /*\r
2  * FreeRTOS Kernel V10.3.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.\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 /******************************************************************************\r
30  *\r
31  * See the following web page for essential TwoEchoClient.c usage and\r
32  * configuration details:\r
33  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/Common_Echo_Clients.shtml\r
34  *\r
35  ******************************************************************************/\r
36 \r
37 \r
38 /* Standard includes. */\r
39 #include <stdint.h>\r
40 #include <stdio.h>\r
41 #include <stdlib.h>\r
42 \r
43 /* FreeRTOS includes. */\r
44 #include "FreeRTOS.h"\r
45 #include "task.h"\r
46 \r
47 /* FreeRTOS+UDP includes. */\r
48 #include "FreeRTOS_UDP_IP.h"\r
49 #include "FreeRTOS_Sockets.h"\r
50 \r
51 /* Small delay used between attempts to obtain a zero copy buffer. */\r
52 #define echoTINY_DELAY  ( ( TickType_t ) 2 )\r
53 \r
54 /* The echo tasks create a socket, send out a number of echo requests\r
55 (listening for each echo reply), then close the socket again before\r
56 starting over.  This delay is used between each iteration to ensure the\r
57 network does not get too congested. */\r
58 #define echoLOOP_DELAY  ( ( TickType_t ) 250 / portTICK_RATE_MS )\r
59 \r
60 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1\r
61         /* When the trace recorder code is included user events are generated to\r
62         mark the sending and receiving of the echoed data (only in the zero copy\r
63         task. */\r
64         #define echoMARK_SEND_IN_TRACE_BUFFER( x ) vTraceUserEvent( x )\r
65         traceLabel xZeroCopySendEvent, xZeroCopyReceiveEvent;\r
66 \r
67 #else\r
68         /* When the trace recorder code is not included just #define away the call\r
69         to post the user event. */\r
70         #define echoMARK_SEND_IN_TRACE_BUFFER( x )\r
71         #define xZeroCopySendEvent 0\r
72         #define xZeroCopyReceiveEvent 0\r
73 #endif\r
74 \r
75 /* The echo server is assumed to be on port 7, which is the standard echo\r
76 protocol port. */\r
77 #define echoECHO_PORT   ( 7 )\r
78 \r
79 /*\r
80  * Uses a socket to send data to, then receive data from, the standard echo\r
81  * port number 7.  prvEchoClientTask() uses the standard interface.\r
82  * prvZeroCopyEchoClientTask() uses the zero copy interface.\r
83  */\r
84 static void prvEchoClientTask( void *pvParameters );\r
85 static void prvZeroCopyEchoClientTask( void *pvParameters );\r
86 \r
87 /* The receive timeout is set shorter when the windows simulator is used\r
88 because simulated time is slower than real time. */\r
89 #ifdef _WINDOWS_\r
90         const TickType_t xReceiveTimeOut = 50 / portTICK_RATE_MS;\r
91 #else\r
92         const TickType_t xReceiveTimeOut = 500 / portTICK_RATE_MS;\r
93 #endif\r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 void vStartEchoClientTasks( uint16_t usTaskStackSize, UBaseType_t uxTaskPriority )\r
98 {\r
99         /* Create the echo client task that does not use the zero copy interface. */\r
100         xTaskCreate(    prvEchoClientTask,      /* The function that implements the task. */\r
101                                         "Echo0",                        /* Just a text name for the task to aid debugging. */\r
102                                         usTaskStackSize,        /* The stack size is defined in FreeRTOSIPConfig.h. */\r
103                                         NULL,                           /* The task parameter, not used in this case. */\r
104                                         uxTaskPriority,         /* The priority assigned to the task is defined in FreeRTOSConfig.h. */\r
105                                         NULL );                         /* The task handle is not used. */\r
106 \r
107         /* Create the echo client task that does use the zero copy interface. */\r
108         xTaskCreate(    prvZeroCopyEchoClientTask,      /* The function that implements the task. */\r
109                                         "Echo1",                                        /* Just a text name for the task to aid debugging. */\r
110                                         usTaskStackSize,                        /* The stack size is defined in FreeRTOSIPConfig.h. */\r
111                                         NULL,                                           /* The task parameter, not used in this case. */\r
112                                         uxTaskPriority,                         /* The priority assigned to the task is defined in FreeRTOSConfig.h. */\r
113                                         NULL );                                         /* The task handle is not used. */\r
114 }\r
115 /*-----------------------------------------------------------*/\r
116 \r
117 static void prvEchoClientTask( void *pvParameters )\r
118 {\r
119 xSocket_t xSocket;\r
120 struct freertos_sockaddr xEchoServerAddress;\r
121 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
122 int32_t lLoopCount = 0UL;\r
123 const int32_t lMaxLoopCount = 50;\r
124 volatile uint32_t ulRxCount = 0UL, ulTxCount = 0UL;\r
125 uint32_t xAddressLength = sizeof( xEchoServerAddress );\r
126 \r
127         /* Remove compiler warning about unused parameters. */\r
128         ( void ) pvParameters;\r
129 \r
130         /* Echo requests are sent to the echo server.  The address of the echo\r
131         server is configured by the constants configECHO_SERVER_ADDR0 to\r
132         configECHO_SERVER_ADDR3 in FreeRTOSConfig.h. */\r
133         xEchoServerAddress.sin_port = FreeRTOS_htons( echoECHO_PORT );\r
134         xEchoServerAddress.sin_addr = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0,\r
135                                                                                                                         configECHO_SERVER_ADDR1,\r
136                                                                                                                         configECHO_SERVER_ADDR2,\r
137                                                                                                                         configECHO_SERVER_ADDR3 );\r
138 \r
139         for( ;; )\r
140         {\r
141                 /* Create a socket. */\r
142                 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
143                 configASSERT( xSocket != FREERTOS_INVALID_SOCKET );\r
144 \r
145                 /* Set a time out so a missing reply does not cause the task to block\r
146                 indefinitely. */\r
147                 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );\r
148 \r
149                 /* Send a number of echo requests. */\r
150                 for( lLoopCount = 0; lLoopCount < lMaxLoopCount; lLoopCount++ )\r
151                 {\r
152                         /* Create the string that is sent to the echo server. */\r
153                         sprintf( cTxString, "Message number %u\r\n", ( unsigned int ) ulTxCount );\r
154 \r
155                         /* Send the string to the socket.  ulFlags is set to 0, so the zero\r
156                         copy interface is not used.  That means the data from cTxString is\r
157                         copied into a network buffer inside FreeRTOS_sendto(), and cTxString\r
158                         can be reused as soon as FreeRTOS_sendto() has returned.  1 is added\r
159                         to ensure the NULL string terminator is sent as part of the message. */\r
160                         FreeRTOS_sendto( xSocket,                               /* The socket being sent to. */\r
161                                                         ( void * ) cTxString,   /* The data being sent. */\r
162                                                         strlen( cTxString ) + 1,/* The length of the data being sent. */\r
163                                                         0,                                              /* ulFlags with the FREERTOS_ZERO_COPY bit clear. */\r
164                                                         &xEchoServerAddress,    /* The destination address. */\r
165                                                         sizeof( xEchoServerAddress ) );\r
166 \r
167                         /* Keep a count of how many echo requests have been transmitted so\r
168                         it can be compared to the number of echo replies received.  It would\r
169                         be expected to loose at least one to an ARP message the first time\r
170                         the     connection is created. */\r
171                         ulTxCount++;\r
172 \r
173                         /* Receive data echoed back to the socket.  ulFlags is zero, so the\r
174                         zero copy option is not being used and the received data will be\r
175                         copied into the buffer pointed to by cRxString.  xAddressLength is\r
176                         not actually used (at the time of writing this comment, anyway) by\r
177                         FreeRTOS_recvfrom(), but is set appropriately in case future\r
178                         versions do use it. */\r
179                         memset( ( void * ) cRxString, 0x00, sizeof( cRxString ) );\r
180                         FreeRTOS_recvfrom(      xSocket,                                /* The socket being received from. */\r
181                                                                 cRxString,                              /* The buffer into which the received data will be written. */\r
182                                                                 sizeof( cRxString ),    /* The size of the buffer provided to receive the data. */\r
183                                                                 0,                                              /* ulFlags with the FREERTOS_ZERO_COPY bit clear. */\r
184                                                                 &xEchoServerAddress,    /* The address from where the data was sent (the source address). */\r
185                                                                 &xAddressLength );\r
186 \r
187                         /* Compare the transmitted string to the received string. */\r
188                         if( strcmp( cRxString, cTxString ) == 0 )\r
189                         {\r
190                                 /* The echo reply was received without error. */\r
191                                 ulRxCount++;\r
192                         }\r
193                 };\r
194 \r
195                 /* Pause for a short while to ensure the network is not too\r
196                 congested. */\r
197                 vTaskDelay( echoLOOP_DELAY );\r
198 \r
199                 /* Close this socket before looping back to create another. */\r
200                 FreeRTOS_closesocket( xSocket );\r
201         }\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 static void prvZeroCopyEchoClientTask( void *pvParameters )\r
206 {\r
207 xSocket_t xSocket;\r
208 struct freertos_sockaddr xEchoServerAddress;\r
209 static char cTxString[ 40 ];\r
210 int32_t lLoopCount = 0UL;\r
211 volatile uint32_t ulRxCount = 0UL, ulTxCount = 0UL;\r
212 uint32_t xAddressLength = sizeof( xEchoServerAddress );\r
213 int32_t lReturned;\r
214 uint8_t *pucUDPPayloadBuffer;\r
215 \r
216 const int32_t lMaxLoopCount = 50;\r
217 const char * const pcStringToSend = "Zero copy message number";\r
218 /* The buffer is large enough to hold the string, a number, and the string terminator. */\r
219 const size_t xBufferLength = strlen( pcStringToSend ) + 15;\r
220 \r
221         #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1\r
222         {\r
223                 /* When the trace recorder code is included user events are generated to\r
224                 mark the sending and receiving of the echoed data (only in the zero copy\r
225                 task). */\r
226                 xZeroCopySendEvent = xTraceOpenLabel( "ZeroCopyTx" );\r
227                 xZeroCopyReceiveEvent = xTraceOpenLabel( "ZeroCopyRx" );\r
228         }\r
229         #endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS */\r
230 \r
231         /* Remove compiler warning about unused parameters. */\r
232         ( void ) pvParameters;\r
233 \r
234         /* Delay for a little while to ensure the task is out of synch with the\r
235         other echo task implemented above. */\r
236         vTaskDelay( echoLOOP_DELAY >> 1 );\r
237 \r
238         /* Echo requests are sent to the echo server.  The address of the echo\r
239         server is configured by the constants configECHO_SERVER_ADDR0 to\r
240         configECHO_SERVER_ADDR3 in FreeRTOSConfig.h. */\r
241         xEchoServerAddress.sin_port = FreeRTOS_htons( echoECHO_PORT );\r
242         xEchoServerAddress.sin_addr = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0,\r
243                                                                                                                         configECHO_SERVER_ADDR1,\r
244                                                                                                                         configECHO_SERVER_ADDR2,\r
245                                                                                                                         configECHO_SERVER_ADDR3 );\r
246 \r
247         for( ;; )\r
248         {\r
249                 /* Create a socket. */\r
250                 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
251                 configASSERT( xSocket != FREERTOS_INVALID_SOCKET );\r
252 \r
253                 /* Set a time out so a missing reply does not cause the task to block\r
254                 indefinitely. */\r
255                 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );\r
256 \r
257                 /* Send a number of echo requests. */\r
258                 for( lLoopCount = 0; lLoopCount < lMaxLoopCount; lLoopCount++ )\r
259                 {\r
260                         /* This task is going to send using the zero copy interface.  The\r
261                         data being sent is therefore written directly into a buffer that is\r
262                         passed by reference into the FreeRTOS_sendto() function.  First\r
263                         obtain a buffer of adequate size from the IP stack.  Although a max\r
264                         delay is used, the actual delay will be capped to\r
265                         ipconfigMAX_SEND_BLOCK_TIME_TICKS, hence the test to ensure a buffer\r
266                         was actually obtained. */\r
267                         pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xBufferLength, portMAX_DELAY );\r
268 \r
269                         if( pucUDPPayloadBuffer != NULL )\r
270                         {\r
271                                 /* A buffer was successfully obtained.  Create the string that is\r
272                                 sent to the echo server.  Note the string is written directly\r
273                                 into the buffer obtained from the IP stack. */\r
274                                 sprintf( ( char * ) pucUDPPayloadBuffer, "%s %u\r\n", "Zero copy message number", ( unsigned int ) ulTxCount );\r
275 \r
276                                 /* Also copy the string into a local buffer so it can be compared\r
277                                 with the string that is later received back from the echo server. */\r
278                                 strcpy( cTxString, ( char * ) pucUDPPayloadBuffer );\r
279 \r
280                                 /* Pass the buffer into the send function.  ulFlags has the\r
281                                 FREERTOS_ZERO_COPY bit set so the IP stack will take control of\r
282                                 the     buffer, rather than copy data out of the buffer. */\r
283                                 echoMARK_SEND_IN_TRACE_BUFFER( xZeroCopySendEvent );\r
284                                 lReturned = FreeRTOS_sendto(    xSocket,                                        /* The socket being sent to. */\r
285                                                                                                 ( void * ) pucUDPPayloadBuffer, /* The buffer being passed into the IP stack. */\r
286                                                                                                 strlen( cTxString ) + 1,        /* The length of the data being sent.  Plus 1 to ensure the null terminator is part of the data. */\r
287                                                                                                 FREERTOS_ZERO_COPY,                     /* ulFlags with the zero copy bit is set. */\r
288                                                                                                 &xEchoServerAddress,            /* Where the data is being sent. */\r
289                                                                                                 sizeof( xEchoServerAddress ) );\r
290 \r
291                                 if( lReturned == 0 )\r
292                                 {\r
293                                         /* The send operation failed, so this task is still\r
294                                         responsible     for the buffer obtained from the IP stack.  To\r
295                                         ensure the buffer is not lost it must either be used again,\r
296                                         or, as in this case, returned to the IP stack using\r
297                                         FreeRTOS_ReleaseUDPPayloadBuffer().  pucUDPPayloadBuffer can\r
298                                         be safely re-used to receive from the socket below once the\r
299                                         buffer has been returned to the stack. */\r
300                                         FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );\r
301                                 }\r
302                                 else\r
303                                 {\r
304                                         /* The send was successful so the IP stack is now managing\r
305                                         the     buffer pointed to by pucUDPPayloadBuffer, and the IP\r
306                                         stack will return the buffer once it has been sent.\r
307                                         pucUDPPayloadBuffer can be safely re-used to receive from\r
308                                         the socket below. */\r
309                                 }\r
310 \r
311                                 /* Keep a count of how many echo requests have been transmitted\r
312                                 so it can be compared to the number of echo replies received.\r
313                                 It would be expected to loose at least one to an ARP message the\r
314                                 first time the connection is created. */\r
315                                 ulTxCount++;\r
316 \r
317                                 /* Receive data on the socket.  ulFlags has the zero copy bit set\r
318                                 (FREERTOS_ZERO_COPY) indicating to the stack that a reference to\r
319                                 the     received data should be passed out to this task using the\r
320                                 second parameter to the FreeRTOS_recvfrom() call.  When this is\r
321                                 done the IP stack is no longer responsible for releasing the\r
322                                 buffer, and     the task *must* return the buffer to the stack when\r
323                                 it is no longer needed.  By default the receive block time is\r
324                                 portMAX_DELAY. */\r
325                                 echoMARK_SEND_IN_TRACE_BUFFER( xZeroCopyReceiveEvent );\r
326                                 lReturned = FreeRTOS_recvfrom(  xSocket,                                        /* The socket to receive from. */\r
327                                                                                                 ( void * ) &pucUDPPayloadBuffer,  /* pucUDPPayloadBuffer will be set to point to the buffer that already contains the received data. */\r
328                                                                                                 0,                                                      /* Ignored because the zero copy interface is being used. */\r
329                                                                                                 FREERTOS_ZERO_COPY,                     /* ulFlags with the FREERTOS_ZERO_COPY bit set. */\r
330                                                                                                 &xEchoServerAddress,            /* The address from which the data was sent. */\r
331                                                                                                 &xAddressLength );\r
332 \r
333                                 if( lReturned > 0 )\r
334                                 {\r
335                                         /* Compare the string sent to the echo server with the string\r
336                                         received back from the echo server. */\r
337                                         if( strcmp( ( char * ) pucUDPPayloadBuffer, cTxString ) == 0 )\r
338                                         {\r
339                                                 /* The strings matched. */\r
340                                                 ulRxCount++;\r
341                                         }\r
342 \r
343                                         /* The buffer that contains the data passed out of the stack\r
344                                         *must* be returned to the stack. */\r
345                                         FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );\r
346                                 }\r
347                         }\r
348                 }\r
349 \r
350                 /* Pause for a short while to ensure the network is not too\r
351                 congested. */\r
352                 vTaskDelay( echoLOOP_DELAY );\r
353 \r
354                 /* Close this socket before looping back to create another. */\r
355                 FreeRTOS_closesocket( xSocket );\r
356         }\r
357 }\r
358 /*-----------------------------------------------------------*/\r
359 \r