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