]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/Common/FreeRTOS_Plus_UDP_Demos/EchoClients/TwoEchoClients.c
c6fca09d9fd14f7ea754876c2ef698a1b0aca42a
[freertos] / FreeRTOS-Plus / Demo / Common / FreeRTOS_Plus_UDP_Demos / EchoClients / TwoEchoClients.c
1 /*\r
2     FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 \r
76 /******************************************************************************\r
77  *\r
78  * See the following web page for essential TwoEchoClient.c usage and \r
79  * configuration details:\r
80  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/Common_Echo_Clients.shtml\r
81  *\r
82  ******************************************************************************/\r
83 \r
84 \r
85 /* Standard includes. */\r
86 #include <stdint.h>\r
87 #include <stdio.h>\r
88 #include <stdlib.h>\r
89 \r
90 /* FreeRTOS includes. */\r
91 #include "FreeRTOS.h"\r
92 #include "task.h"\r
93 \r
94 /* FreeRTOS+UDP includes. */\r
95 #include "FreeRTOS_UDP_IP.h"\r
96 #include "FreeRTOS_Sockets.h"\r
97 \r
98 /* Small delay used between attempts to obtain a zero copy buffer. */\r
99 #define echoTINY_DELAY  ( ( portTickType ) 2 )\r
100 \r
101 /* The echo tasks create a socket, send out a number of echo requests\r
102 (listening for each echo reply), then close the socket again before\r
103 starting over.  This delay is used between each iteration to ensure the\r
104 network does not get too congested.  The delay is shorter when the Windows\r
105 simulator is used because simulated time is slower than real time. */\r
106 #ifdef _WINDOWS_\r
107         #define echoLOOP_DELAY  ( ( portTickType ) 10 / portTICK_RATE_MS )\r
108 #else\r
109         #define echoLOOP_DELAY  ( ( portTickType ) 150 / portTICK_RATE_MS )\r
110 #endif /* _WINDOWS_ */\r
111 \r
112 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1\r
113         /* When the trace recorder code is included user events are generated to\r
114         mark the sending and receiving of the echoed data (only in the zero copy\r
115         task. */\r
116         #define echoMARK_SEND_IN_TRACE_BUFFER( x ) vTraceUserEvent( x )\r
117         traceLabel xZeroCopySendEvent, xZeroCopyReceiveEvent;\r
118 \r
119 #else\r
120         /* When the trace recorder code is not included just #define away the call\r
121         to post the user event. */\r
122         #define echoMARK_SEND_IN_TRACE_BUFFER( x )\r
123         #define xZeroCopySendEvent 0\r
124         #define xZeroCopyReceiveEvent 0\r
125 #endif\r
126 \r
127 /* The echo server is assumed to be on port 7, which is the standard echo\r
128 protocol port. */\r
129 #define echoECHO_PORT   ( 7 )\r
130 \r
131 /*\r
132  * Uses a socket to send data to, then receive data from, the standard echo\r
133  * port number 7.  prvEchoClientTask() uses the standard interface.\r
134  * prvZeroCopyEchoClientTask() uses the zero copy interface.\r
135  */\r
136 static void prvEchoClientTask( void *pvParameters );\r
137 static void prvZeroCopyEchoClientTask( void *pvParameters );\r
138 \r
139 /* The receive timeout is set shorter when the windows simulator is used\r
140 because simulated time is slower than real time. */\r
141 #ifdef _WINDOWS_\r
142         const portTickType xReceiveTimeOut = 50 / portTICK_RATE_MS;\r
143 #else\r
144         const portTickType xReceiveTimeOut = 500 / portTICK_RATE_MS;\r
145 #endif\r
146 \r
147 /*-----------------------------------------------------------*/\r
148 \r
149 void vStartEchoClientTasks( uint16_t usTaskStackSize, unsigned portBASE_TYPE uxTaskPriority )\r
150 {\r
151         /* Create the echo client task that does not use the zero copy interface. */\r
152         xTaskCreate(    prvEchoClientTask,                                              /* The function that implements the task. */\r
153                                         ( const signed char * const ) "Echo0",  /* Just a text name for the task to aid debugging. */\r
154                                         usTaskStackSize,                                                /* The stack size is defined in FreeRTOSIPConfig.h. */\r
155                                         NULL,                                                                   /* The task parameter, not used in this case. */\r
156                                         uxTaskPriority,                                                 /* The priority assigned to the task is defined in FreeRTOSConfig.h. */\r
157                                         NULL );                                                                 /* The task handle is not used. */\r
158 \r
159         /* Create the echo client task that does use the zero copy interface. */\r
160         xTaskCreate(    prvZeroCopyEchoClientTask,                              /* The function that implements the task. */\r
161                                         ( const signed char * const ) "Echo1",  /* Just a text name for the task to aid debugging. */\r
162                                         usTaskStackSize,                                                /* The stack size is defined in FreeRTOSIPConfig.h. */\r
163                                         NULL,                                                                   /* The task parameter, not used in this case. */\r
164                                         uxTaskPriority,                                                 /* The priority assigned to the task is defined in FreeRTOSConfig.h. */\r
165                                         NULL );                                                                 /* The task handle is not used. */\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 static void prvEchoClientTask( void *pvParameters )\r
170 {\r
171 xSocket_t xSocket;\r
172 struct freertos_sockaddr xEchoServerAddress;\r
173 int8_t 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
174 int32_t lLoopCount = 0UL;\r
175 const int32_t lMaxLoopCount = 50;\r
176 volatile uint32_t ulRxCount = 0UL, ulTxCount = 0UL;\r
177 uint32_t xAddressLength = sizeof( xEchoServerAddress );\r
178 \r
179         /* Remove compiler warning about unused parameters. */\r
180         ( void ) pvParameters;\r
181 \r
182         /* Echo requests are sent to the echo server.  The address of the echo\r
183         server is configured by the constants configECHO_SERVER_ADDR0 to\r
184         configECHO_SERVER_ADDR3 in FreeRTOSConfig.h. */\r
185         xEchoServerAddress.sin_port = FreeRTOS_htons( echoECHO_PORT );\r
186         xEchoServerAddress.sin_addr = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0,\r
187                                                                                                                         configECHO_SERVER_ADDR1,\r
188                                                                                                                         configECHO_SERVER_ADDR2,\r
189                                                                                                                         configECHO_SERVER_ADDR3 );\r
190 \r
191         for( ;; )\r
192         {\r
193                 /* Create a socket. */\r
194                 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
195                 configASSERT( xSocket != FREERTOS_INVALID_SOCKET );\r
196 \r
197                 /* Set a time out so a missing reply does not cause the task to block\r
198                 indefinitely. */\r
199                 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );\r
200 \r
201                 /* Send a number of echo requests. */\r
202                 for( lLoopCount = 0; lLoopCount < lMaxLoopCount; lLoopCount++ )\r
203                 {\r
204                         /* Create the string that is sent to the echo server. */\r
205                         sprintf( ( char * ) cTxString, "Message number %u\r\n", ulTxCount );\r
206 \r
207                         /* Send the string to the socket.  ulFlags is set to 0, so the zero\r
208                         copy interface is not used.  That means the data from cTxString is\r
209                         copied into a network buffer inside FreeRTOS_sendto(), and cTxString\r
210                         can be reused as soon as FreeRTOS_sendto() has returned.  1 is added\r
211                         to ensure the NULL string terminator is sent as part of the message. */\r
212                         FreeRTOS_sendto( xSocket,                               /* The socket being sent to. */\r
213                                                         ( void * ) cTxString,   /* The data being sent. */\r
214                                                         strlen( ( const char * ) cTxString ) + 1, /* The length of the data being sent. */\r
215                                                         0,                                              /* ulFlags with the FREERTOS_ZERO_COPY bit clear. */\r
216                                                         &xEchoServerAddress,    /* The destination address. */\r
217                                                         sizeof( xEchoServerAddress ) );\r
218 \r
219                         /* Keep a count of how many echo requests have been transmitted so\r
220                         it can be compared to the number of echo replies received.  It would\r
221                         be expected to loose at least one to an ARP message the first time\r
222                         the     connection is created. */\r
223                         ulTxCount++;\r
224 \r
225                         /* Receive data echoed back to the socket.  ulFlags is zero, so the\r
226                         zero copy option is not being used and the received data will be\r
227                         copied into the buffer pointed to by cRxString.  xAddressLength is\r
228                         not actually used (at the time of writing this comment, anyway) by\r
229                         FreeRTOS_recvfrom(), but is set appropriately in case future\r
230                         versions do use it. */\r
231                         memset( ( void * ) cRxString, 0x00, sizeof( cRxString ) );\r
232                         FreeRTOS_recvfrom(      xSocket,                                /* The socket being received from. */\r
233                                                                 cRxString,                              /* The buffer into which the received data will be written. */\r
234                                                                 sizeof( cRxString ),    /* The size of the buffer provided to receive the data. */\r
235                                                                 0,                                              /* ulFlags with the FREERTOS_ZERO_COPY bit clear. */\r
236                                                                 &xEchoServerAddress,    /* The address from where the data was sent (the source address). */\r
237                                                                 &xAddressLength );\r
238 \r
239                         /* Compare the transmitted string to the received string. */\r
240                         if( strcmp( ( char * ) cRxString, ( char * ) cTxString ) == 0 )\r
241                         {\r
242                                 /* The echo reply was received without error. */\r
243                                 ulRxCount++;\r
244                         }\r
245                 };\r
246 \r
247                 /* Pause for a short while to ensure the network is not too\r
248                 congested. */\r
249                 vTaskDelay( echoLOOP_DELAY );\r
250 \r
251                 /* Close this socket before looping back to create another. */\r
252                 FreeRTOS_closesocket( xSocket );\r
253         }\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 static void prvZeroCopyEchoClientTask( void *pvParameters )\r
258 {\r
259 xSocket_t xSocket;\r
260 struct freertos_sockaddr xEchoServerAddress;\r
261 static int8_t cTxString[ 40 ];\r
262 int32_t lLoopCount = 0UL;\r
263 volatile uint32_t ulRxCount = 0UL, ulTxCount = 0UL;\r
264 uint32_t xAddressLength = sizeof( xEchoServerAddress );\r
265 int32_t lReturned;\r
266 uint8_t *pucUDPPayloadBuffer;\r
267 \r
268 const int32_t lMaxLoopCount = 50;\r
269 const uint8_t * const pucStringToSend = ( const uint8_t * const ) "Zero copy message number";\r
270 /* The buffer is large enough to hold the string, a number, and the string terminator. */\r
271 const size_t xBufferLength = strlen( ( char * ) pucStringToSend ) + 15;\r
272 \r
273         #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1\r
274         {\r
275                 /* When the trace recorder code is included user events are generated to\r
276                 mark the sending and receiving of the echoed data (only in the zero copy\r
277                 task). */\r
278                 xZeroCopySendEvent = xTraceOpenLabel( "ZeroCopyTx" );\r
279                 xZeroCopyReceiveEvent = xTraceOpenLabel( "ZeroCopyRx" );\r
280         }\r
281         #endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS */\r
282 \r
283         /* Remove compiler warning about unused parameters. */\r
284         ( void ) pvParameters;\r
285 \r
286         /* Delay for a little while to ensure the task is out of synch with the\r
287         other echo task implemented above. */\r
288         vTaskDelay( echoLOOP_DELAY >> 1 );\r
289 \r
290         /* Echo requests are sent to the echo server.  The address of the echo\r
291         server is configured by the constants configECHO_SERVER_ADDR0 to\r
292         configECHO_SERVER_ADDR3 in FreeRTOSConfig.h. */\r
293         xEchoServerAddress.sin_port = FreeRTOS_htons( echoECHO_PORT );\r
294         xEchoServerAddress.sin_addr = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0,\r
295                                                                                                                         configECHO_SERVER_ADDR1,\r
296                                                                                                                         configECHO_SERVER_ADDR2,\r
297                                                                                                                         configECHO_SERVER_ADDR3 );\r
298 \r
299         for( ;; )\r
300         {\r
301                 /* Create a socket. */\r
302                 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
303                 configASSERT( xSocket != FREERTOS_INVALID_SOCKET );\r
304 \r
305                 /* Set a time out so a missing reply does not cause the task to block\r
306                 indefinitely. */\r
307                 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );\r
308 \r
309                 /* Send a number of echo requests. */\r
310                 for( lLoopCount = 0; lLoopCount < lMaxLoopCount; lLoopCount++ )\r
311                 {\r
312                         /* This task is going to send using the zero copy interface.  The\r
313                         data being sent is therefore written directly into a buffer that is\r
314                         passed by reference into the FreeRTOS_sendto() function.  First\r
315                         obtain a buffer of adequate size from the IP stack.  Although a max\r
316                         delay is used, the actual delay will be capped to\r
317                         ipconfigMAX_SEND_BLOCK_TIME_TICKS, hence the test to ensure a buffer\r
318                         was actually obtained. */\r
319                         pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xBufferLength, portMAX_DELAY );\r
320 \r
321                         if( pucUDPPayloadBuffer != NULL )\r
322                         {\r
323                                 /* A buffer was successfully obtained.  Create the string that is\r
324                                 sent to the echo server.  Note the string is written directly\r
325                                 into the buffer obtained from the IP stack. */\r
326                                 sprintf( ( char * ) pucUDPPayloadBuffer, "%s %u\r\n", ( const char * ) "Zero copy message number", ulTxCount );\r
327 \r
328                                 /* Also copy the string into a local buffer so it can be compared\r
329                                 with the string that is later received back from the echo server. */\r
330                                 strcpy( ( char * ) cTxString, ( char * ) pucUDPPayloadBuffer );\r
331 \r
332                                 /* Pass the buffer into the send function.  ulFlags has the\r
333                                 FREERTOS_ZERO_COPY bit set so the IP stack will take control of\r
334                                 the     buffer, rather than copy data out of the buffer. */\r
335                                 echoMARK_SEND_IN_TRACE_BUFFER( xZeroCopySendEvent );\r
336                                 lReturned = FreeRTOS_sendto(    xSocket,                                        /* The socket being sent to. */\r
337                                                                                                 ( void * ) pucUDPPayloadBuffer, /* The buffer being passed into the IP stack. */\r
338                                                                                                 strlen( ( const char * ) cTxString ) + 1, /* The length of the data being sent.  Plus 1 to ensure the null terminator is part of the data. */\r
339                                                                                                 FREERTOS_ZERO_COPY,                     /* ulFlags with the zero copy bit is set. */\r
340                                                                                                 &xEchoServerAddress,            /* Where the data is being sent. */\r
341                                                                                                 sizeof( xEchoServerAddress ) );\r
342 \r
343                                 if( lReturned == 0 )\r
344                                 {\r
345                                         /* The send operation failed, so this task is still\r
346                                         responsible     for the buffer obtained from the IP stack.  To\r
347                                         ensure the buffer is not lost it must either be used again,\r
348                                         or, as in this case, returned to the IP stack using\r
349                                         FreeRTOS_ReleaseUDPPayloadBuffer().  pucUDPPayloadBuffer can\r
350                                         be safely re-used to receive from the socket below once the\r
351                                         buffer has been returned to the stack. */\r
352                                         FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );\r
353                                 }\r
354                                 else\r
355                                 {\r
356                                         /* The send was successful so the IP stack is now managing\r
357                                         the     buffer pointed to by pucUDPPayloadBuffer, and the IP\r
358                                         stack will return the buffer once it has been sent.\r
359                                         pucUDPPayloadBuffer can be safely re-used to receive from\r
360                                         the socket below. */\r
361                                 }\r
362 \r
363                                 /* Keep a count of how many echo requests have been transmitted\r
364                                 so it can be compared to the number of echo replies received.\r
365                                 It would be expected to loose at least one to an ARP message the\r
366                                 first time the connection is created. */\r
367                                 ulTxCount++;\r
368 \r
369                                 /* Receive data on the socket.  ulFlags has the zero copy bit set\r
370                                 (FREERTOS_ZERO_COPY) indicating to the stack that a reference to\r
371                                 the     received data should be passed out to this task using the\r
372                                 second parameter to the FreeRTOS_recvfrom() call.  When this is\r
373                                 done the IP stack is no longer responsible for releasing the\r
374                                 buffer, and     the task *must* return the buffer to the stack when\r
375                                 it is no longer needed.  By default the receive block time is\r
376                                 portMAX_DELAY. */\r
377                                 echoMARK_SEND_IN_TRACE_BUFFER( xZeroCopyReceiveEvent );\r
378                                 lReturned = FreeRTOS_recvfrom(  xSocket,                                        /* The socket to receive from. */\r
379                                                                                                 ( void * ) &pucUDPPayloadBuffer,  /* pucUDPPayloadBuffer will be set to point to the buffer that already contains the received data. */\r
380                                                                                                 0,                                                      /* Ignored because the zero copy interface is being used. */\r
381                                                                                                 FREERTOS_ZERO_COPY,                     /* ulFlags with the FREERTOS_ZERO_COPY bit set. */\r
382                                                                                                 &xEchoServerAddress,            /* The address from which the data was sent. */\r
383                                                                                                 &xAddressLength );\r
384 \r
385                                 if( lReturned > 0 )\r
386                                 {\r
387                                         /* Compare the string sent to the echo server with the string\r
388                                         received back from the echo server. */\r
389                                         if( strcmp( ( char * ) pucUDPPayloadBuffer, ( char * ) cTxString ) == 0 )\r
390                                         {\r
391                                                 /* The strings matched. */\r
392                                                 ulRxCount++;\r
393                                         }\r
394 \r
395                                         /* The buffer that contains the data passed out of the stack\r
396                                         *must* be returned to the stack. */\r
397                                         FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );\r
398                                 }\r
399                         }\r
400                 }\r
401 \r
402                 /* Pause for a short while to ensure the network is not too\r
403                 congested. */\r
404                 vTaskDelay( echoLOOP_DELAY );\r
405 \r
406                 /* Close this socket before looping back to create another. */\r
407                 FreeRTOS_closesocket( xSocket );\r
408         }\r
409 }\r
410 /*-----------------------------------------------------------*/\r
411 \r