]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_Windows_Simulator/DemoTasks/SimpleClientAndServer.c
4154e2926445217dc4c315d8c85f087d16c2729d
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_Windows_Simulator / DemoTasks / SimpleClientAndServer.c
1 /*\r
2     FreeRTOS V7.4.2 - 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  * Creates two transmitting tasks and two receiving tasks.  The transmitting\r
77  * tasks send values that are received by the receiving tasks.  One set of tasks\r
78  * uses the standard API.  The other set of tasks uses the zero copy API.\r
79  */\r
80 \r
81 /* Standard includes. */\r
82 #include <stdint.h>\r
83 #include <stdio.h>\r
84 \r
85 /* FreeRTOS includes. */\r
86 #include "FreeRTOS.h"\r
87 #include "task.h"\r
88 \r
89 /* FreeRTOS+UDP includes. */\r
90 #include "FreeRTOS_UDP_IP.h"\r
91 #include "FreeRTOS_Sockets.h"\r
92 \r
93 #define simpTINY_DELAY  ( ( portTickType ) 2 )\r
94 \r
95 /*\r
96  * Uses a socket to send data without using the zero copy option.\r
97  * prvSimpleServerTask() will receive the data.\r
98  */\r
99 static void prvSimpleClientTask( void *pvParameters );\r
100 \r
101 /*\r
102  * Uses a socket to receive the data sent by the prvSimpleClientTask() task.\r
103  * Does not use the zero copy option.\r
104  */\r
105 static void prvSimpleServerTask( void *pvParameters );\r
106 \r
107 /*\r
108  * Uses a socket to send data using the zero copy option.\r
109  * prvSimpleZeroCopyServerTask() will receive the data.\r
110  */\r
111 static void prvSimpleZeroCopyUDPClientTask( void *pvParameters );\r
112 \r
113 /*\r
114  * Uses a socket to receive the data sent by the prvSimpleZeroCopyUDPClientTask()\r
115  * task.  Uses the zero copy option.\r
116  */\r
117 static void prvSimpleZeroCopyServerTask( void *pvParameters );\r
118 \r
119 /*-----------------------------------------------------------*/\r
120 \r
121 void vStartSimpleUDPClientServerTasks( uint16_t usStackSize, uint32_t ulPort, unsigned portBASE_TYPE uxPriority )\r
122 {\r
123         /* Create the client and server tasks that do not use the zero copy\r
124         interface. */\r
125         xTaskCreate( prvSimpleClientTask, "SimpCpyClnt", usStackSize, ( void * ) ulPort, uxPriority, NULL );\r
126         xTaskCreate( prvSimpleServerTask, "SimpCpySrv", usStackSize, ( void * ) ulPort, uxPriority + 1, NULL );\r
127 \r
128         /* Create the client and server tasks that do use the zero copy interface. */\r
129         xTaskCreate( prvSimpleZeroCopyUDPClientTask, "SimpZCpyClnt", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority, NULL );\r
130         xTaskCreate( prvSimpleZeroCopyServerTask, "SimpZCpySrv", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority + 1, NULL );\r
131 }\r
132 /*-----------------------------------------------------------*/\r
133 \r
134 static void prvSimpleClientTask( void *pvParameters )\r
135 {\r
136 xSocket_t xClientSocket;\r
137 struct freertos_sockaddr xDestinationAddress;\r
138 uint8_t cString[ 50 ];\r
139 portBASE_TYPE lReturned;\r
140 uint32_t ulCount = 0UL, ulIPAddress;\r
141 const uint32_t ulLoopsPerSocket = 10UL;\r
142 const portTickType x150ms = 150UL / portTICK_RATE_MS;\r
143 \r
144         /* Remove compiler warning about unused parameters. */\r
145         ( void ) pvParameters;\r
146 \r
147         /* It is assumed that this task is not created until the network is up,\r
148         so the IP address can be obtained immediately.  store the IP address being\r
149         used in ulIPAddress.  This is done so the socket can send to a different\r
150         port on the same IP address. */\r
151         FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );\r
152 \r
153         /* This test sends to itself, so data sent from here is received by a server\r
154         socket on the same IP address.  Setup the freertos_sockaddr structure with\r
155         this nodes IP address, and the port number being sent to.  The strange\r
156         casting is to try and remove compiler warnings on 32 bit machines. */\r
157         xDestinationAddress.sin_addr = ulIPAddress;\r
158         xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;\r
159         xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );\r
160 \r
161         for( ;; )\r
162         {\r
163                 /* Create the socket. */\r
164                 xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
165                 configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );\r
166 \r
167                 /* The count is used to differentiate between different messages sent to\r
168                 the server, and to break out of the do while loop below. */\r
169                 ulCount = 0UL;\r
170 \r
171                 do\r
172                 {\r
173                         /* Create the string that is sent to the server. */\r
174                         sprintf( ( char * ) cString, "Server received (not zero copy): Message number %lu\r\n", ulCount );\r
175 \r
176                         /* Send the string to the socket.  ulFlags is set to 0, so the zero\r
177                         copy option is not selected.  That means the data from cString[] is\r
178                         copied into a network buffer inside FreeRTOS_sendto(), and cString[]\r
179                         can be reused as soon as FreeRTOS_sendto() has returned. */\r
180                         lReturned = FreeRTOS_sendto( xClientSocket, ( void * ) cString, strlen( ( const char * ) cString ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );\r
181 \r
182                         ulCount++;\r
183 \r
184                 } while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );\r
185 \r
186                 FreeRTOS_closesocket( xClientSocket );\r
187 \r
188                 /* A short delay to prevent the messages printed by the server task\r
189                 scrolling off the screen too quickly, and to prevent reduce the network\r
190                 loading. */\r
191                 vTaskDelay( x150ms );\r
192         }\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 static void prvSimpleServerTask( void *pvParameters )\r
197 {\r
198 long lBytes;\r
199 uint8_t cReceivedString[ 60 ];\r
200 struct freertos_sockaddr xClient, xBindAddress;\r
201 uint32_t xClientLength = sizeof( xClient );\r
202 xSocket_t xListeningSocket;\r
203 \r
204         /* Just to prevent compiler warnings. */\r
205         ( void ) pvParameters;\r
206 \r
207         /* Attempt to open the socket. */\r
208         xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
209         configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );\r
210 \r
211         /* This test receives data sent from a different port on the same IP\r
212         address.  Configure the freertos_sockaddr structure with the address being\r
213         bound to.  The strange casting is to try and remove     compiler warnings on 32\r
214         bit machines.  Note that this task is only created after the network is up,\r
215         so the IP address is valid here. */\r
216         xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;\r
217         xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );\r
218 \r
219         /* Bind the socket to the port that the client task will send to. */\r
220         FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );\r
221 \r
222         for( ;; )\r
223         {\r
224                 /* Zero out the receive array so there is NULL at the end of the string\r
225                 when it is printed out. */\r
226                 memset( cReceivedString, 0x00, sizeof( cReceivedString ) );\r
227 \r
228                 /* Receive data on the socket.  ulFlags is zero, so the zero copy option\r
229                 is not set and the received data is copied into the buffer pointed to by\r
230                 cReceivedString.  By default the block time is portMAX_DELAY.\r
231                 xClientLength is not actually used by FreeRTOS_recvfrom(), but is set\r
232                 appropriately in case future versions do use it. */\r
233                 lBytes = FreeRTOS_recvfrom( xListeningSocket, cReceivedString, sizeof( cReceivedString ), 0, &xClient, &xClientLength );\r
234 \r
235                 /* Print the received characters. */\r
236                 if( lBytes > 0 )\r
237                 {\r
238                         vOutputString( ( char * ) cReceivedString );\r
239                 }\r
240 \r
241                 /* Error check. */\r
242                 configASSERT( lBytes == ( portBASE_TYPE ) strlen( ( const char * ) cReceivedString ) );\r
243         }\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 static void prvSimpleZeroCopyUDPClientTask( void *pvParameters )\r
248 {\r
249 xSocket_t xClientSocket;\r
250 uint8_t *pucUDPPayloadBuffer;\r
251 struct freertos_sockaddr xDestinationAddress;\r
252 portBASE_TYPE lReturned;\r
253 uint32_t ulCount = 0UL, ulIPAddress;\r
254 const uint32_t ulLoopsPerSocket = 10UL;\r
255 const uint8_t *pucStringToSend = ( const uint8_t * ) "Server received (using zero copy): Message number ";\r
256 const portTickType x150ms = 150UL / portTICK_RATE_MS;\r
257 /* 15 is added to ensure the number, \r\n and terminating zero fit. */\r
258 const size_t xStringLength = strlen( ( char * ) pucStringToSend ) + 15;\r
259 \r
260         /* Remove compiler warning about unused parameters. */\r
261         ( void ) pvParameters;\r
262 \r
263         /* It is assumed that this task is not created until the network is up,\r
264         so the IP address can be obtained immediately.  store the IP address being\r
265         used in ulIPAddress.  This is done so the socket can send to a different\r
266         port on the same IP address. */\r
267         FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );\r
268 \r
269         /* This test sends to itself, so data sent from here is received by a server\r
270         socket on the same IP address.  Setup the freertos_sockaddr structure with\r
271         this nodes IP address, and the port number being sent to.  The strange\r
272         casting is to try and remove compiler warnings on 32 bit machines. */\r
273         xDestinationAddress.sin_addr = ulIPAddress;\r
274         xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;\r
275         xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );\r
276 \r
277         for( ;; )\r
278         {\r
279                 /* Create the socket. */\r
280                 xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
281                 configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );\r
282 \r
283                 /* The count is used to differentiate between different messages sent to\r
284                 the server, and to break out of the do while loop below. */\r
285                 ulCount = 0UL;\r
286 \r
287                 do\r
288                 {\r
289                         /* This task is going to send using the zero copy interface.  The\r
290                         data being sent is therefore written directly into a buffer that is\r
291                         passed into, rather than copied into, the FreeRTOS_sendto()\r
292                         function.\r
293 \r
294                         First obtain a buffer of adequate length from the IP stack into which\r
295                         the string will be written.  Although a max delay is used, the actual\r
296                         delay will be capped to ipconfigMAX_SEND_BLOCK_TIME_TICKS, hence\r
297                         the do while loop is used to ensure a buffer is obtained. */\r
298                         do\r
299                         {\r
300                         } while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xStringLength, portMAX_DELAY ) ) == NULL );\r
301 \r
302                         /* A buffer was successfully obtained.  Create the string that is\r
303                         sent to the server.  First the string is filled with zeros as this will\r
304                         effectively be the null terminator when the string is received at the other\r
305                         end.  Note that the string is being written directly into the buffer\r
306                         obtained from the IP stack above. */\r
307                         memset( ( void * ) pucUDPPayloadBuffer, 0x00, xStringLength );\r
308                         sprintf( ( char * ) pucUDPPayloadBuffer, "%s%lu\r\n", ( char * ) pucStringToSend, ulCount );\r
309 \r
310                         /* Pass the buffer into the send function.  ulFlags has the\r
311                         FREERTOS_ZERO_COPY bit set so the IP stack will take control of the\r
312                         buffer rather than copy data out of the buffer. */\r
313                         lReturned = FreeRTOS_sendto( xClientSocket,                             /* The socket being sent to. */\r
314                                                                                 ( void * ) pucUDPPayloadBuffer, /* A pointer to the the data being sent. */\r
315                                                                                 strlen( ( const char * ) pucUDPPayloadBuffer ) + 1, /* The length of the data being sent - including the string's null terminator. */\r
316                                                                                 FREERTOS_ZERO_COPY,                     /* ulFlags with the FREERTOS_ZERO_COPY bit set. */\r
317                                                                                 &xDestinationAddress,                   /* Where the data is being sent. */\r
318                                                                                 sizeof( xDestinationAddress ) );\r
319 \r
320                         if( lReturned == 0 )\r
321                         {\r
322                                 /* The send operation failed, so this task is still responsible\r
323                                 for the buffer obtained from the IP stack.  To ensure the buffer\r
324                                 is not lost it must either be used again, or, as in this case,\r
325                                 returned to the IP stack using FreeRTOS_ReleaseUDPPayloadBuffer().\r
326                                 pucUDPPayloadBuffer can be safely re-used after this call. */\r
327                                 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );\r
328                         }\r
329                         else\r
330                         {\r
331                                 /* The send was successful so the IP stack is now managing the\r
332                                 buffer pointed to by pucUDPPayloadBuffer, and the IP stack will\r
333                                 return the buffer once it has been sent.  pucUDPPayloadBuffer can\r
334                                 be safely re-used. */\r
335                         }\r
336 \r
337                         ulCount++;\r
338 \r
339                 } while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );\r
340 \r
341                 FreeRTOS_closesocket( xClientSocket );\r
342 \r
343                 /* A short delay to prevent the messages scrolling off the screen too\r
344                 quickly. */\r
345                 vTaskDelay( x150ms );\r
346         }\r
347 }\r
348 /*-----------------------------------------------------------*/\r
349 \r
350 static void prvSimpleZeroCopyServerTask( void *pvParameters )\r
351 {\r
352 int32_t lBytes;\r
353 uint8_t *pucUDPPayloadBuffer;\r
354 struct freertos_sockaddr xClient, xBindAddress;\r
355 uint32_t xClientLength = sizeof( xClient ), ulIPAddress;\r
356 xSocket_t xListeningSocket;\r
357 \r
358         /* Just to prevent compiler warnings. */\r
359         ( void ) pvParameters;\r
360 \r
361         /* Attempt to open the socket. */\r
362         xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
363         configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );\r
364 \r
365         /* This test receives data sent from a different port on the same IP address.\r
366         Obtain the nodes IP address.  Configure the freertos_sockaddr structure with\r
367         the address being bound to.  The strange casting is to try and remove\r
368         compiler warnings on 32 bit machines.  Note that this task is only created\r
369         after the network is up, so the IP address is valid here. */\r
370         FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );\r
371         xBindAddress.sin_addr = ulIPAddress;\r
372         xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;\r
373         xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );\r
374 \r
375         /* Bind the socket to the port that the client task will send to. */\r
376         FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );\r
377 \r
378         for( ;; )\r
379         {\r
380                 /* Receive data on the socket.  ulFlags has the zero copy bit set\r
381                 (FREERTOS_ZERO_COPY) indicating to the stack that a reference to the\r
382                 received data should be passed out to this task using the second\r
383                 parameter to the FreeRTOS_recvfrom() call.  When this is done the\r
384                 IP stack is no longer responsible for releasing the buffer, and\r
385                 the task *must* return the buffer to the stack when it is no longer\r
386                 needed.  By default the block time is portMAX_DELAY. */\r
387                 lBytes = FreeRTOS_recvfrom( xListeningSocket, ( void * ) &pucUDPPayloadBuffer, 0, FREERTOS_ZERO_COPY, &xClient, &xClientLength );\r
388 \r
389                 /* It is expected to receive one more byte than the string length as\r
390                 the NULL terminator is also transmitted. */\r
391                 configASSERT( lBytes == ( ( portBASE_TYPE ) strlen( ( const char * ) pucUDPPayloadBuffer ) + 1 ) );\r
392 \r
393                 /* Print the received characters. */\r
394                 if( lBytes > 0 )\r
395                 {\r
396                         vOutputString( ( char * ) pucUDPPayloadBuffer );\r
397                 }\r
398 \r
399                 if( lBytes >= 0 )\r
400                 {\r
401                         /* The buffer *must* be freed once it is no longer needed. */\r
402                         FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );\r
403                 }\r
404         }\r
405 }\r
406 \r