]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Demo/FreeRTOS_Plus_TCP_and_FAT_Windows_Simulator/DemoTasks/UDPSelectServer.c
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Demo / FreeRTOS_Plus_TCP_and_FAT_Windows_Simulator / DemoTasks / UDPSelectServer.c
1 /*\r
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /*\r
71  * A number of sockets are created and added to a set. One task then blocks on\r
72  * the set while the other task sends data to a (pseudo) random member of the\r
73  * set.  The value sent increments from 0 to selMAX_TX_VALUE, and when all the\r
74  * values have been sent a check is made that each expected value has indeed\r
75  * been received before the cycle re-starts.\r
76  *\r
77  * See the following web page for essential demo usage and configuration\r
78  * details:\r
79  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html\r
80  */\r
81 \r
82 /* Standard includes. */\r
83 #include <stdint.h>\r
84 #include <stdio.h>\r
85 \r
86 /* FreeRTOS includes. */\r
87 #include "FreeRTOS.h"\r
88 #include "task.h"\r
89 #include "semphr.h"\r
90 \r
91 /* FreeRTOS+TCP includes. */\r
92 #include "FreeRTOS_IP.h"\r
93 #include "FreeRTOS_Sockets.h"\r
94 \r
95 /* Demo project includes. */\r
96 #include "UDPSelectServer.h"\r
97 \r
98 /* Exclude the whole file if select() is not being supported. */\r
99 #if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )\r
100 \r
101 /* The numbers of sockets added to the set. */\r
102 #define selNUMBER_OF_SOCKETS            ( 3 )\r
103 \r
104 /* Each cycle of the demo sends the value 0 to selMAX_TX_VALUE to a socket in\r
105 the set. */\r
106 #define selMAX_TX_VALUE                         ( 100 )\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 \r
110 /*\r
111  * The Tx task that sends the data to the sockets created by the Rx task and\r
112  * added to the select set.\r
113  */\r
114 static void prvMultipleSocketTxTask( void *pvParameters );\r
115 \r
116 /*\r
117  * Uses the FreeRTOS_select() API function to receive from multiple sockets.\r
118  * This task expects to receive every value from 0 to selMAX_TX_VALUE during\r
119  * each cycle of the demo.  An array with an index for each value it expects to\r
120  * receive is used to record which values were and were not received.\r
121  */\r
122 static void prvMultipleSocketRxTask( void *pvParameters );\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 /* The sockets used in FreeRTOS_select(). */\r
127 static Socket_t xRxSockets[ selNUMBER_OF_SOCKETS ] = { 0 };\r
128 \r
129 /* Used to monitor the status of the task. */\r
130 static volatile uint32_t ulTxCycles = 0, ulRxCycles = 0, ulFailedRxCycles = 0, ulErrorOccurred = pdFALSE;\r
131 \r
132 /* The block time used by the Tx task when sending - other delays are derived\r
133 from this value. */\r
134 const TickType_t xSendBlockTime = pdMS_TO_TICKS( 400 );\r
135 const TickType_t xReceiveBlockTime = pdMS_TO_TICKS( 600 );\r
136 \r
137 /* The Tx task needs to know the handle of the Rx task. */\r
138 static TaskHandle_t xRxTaskHandle;\r
139 \r
140 /*-----------------------------------------------------------*/\r
141 \r
142 void vStartUDPSelectServerTasks( uint16_t usStackSize, uint32_t ulFirstPortNumber, UBaseType_t uxPriority )\r
143 {\r
144         /* Create a task that sends to multiple sockets, and the task that uses the\r
145         FreeRTOS_select() function to receive from multiple sockets.  The first port\r
146         number to use is passed into both tasks using the task's parameter.     Other\r
147         port numbers are consecutive from the first. */\r
148         xTaskCreate( prvMultipleSocketTxTask, "MultiTx", usStackSize, ( void * ) ulFirstPortNumber, uxPriority, NULL );\r
149         xTaskCreate( prvMultipleSocketRxTask, "MultiRx", usStackSize, ( void * ) ulFirstPortNumber, uxPriority, &xRxTaskHandle );\r
150 }\r
151 /*-----------------------------------------------------------*/\r
152 \r
153 static void prvMultipleSocketRxTask( void *pvParameters )\r
154 {\r
155 SocketSet_t xFD_Set;\r
156 Socket_t xSocket;\r
157 struct freertos_sockaddr xAddress;\r
158 uint32_t xClientLength = sizeof( struct freertos_sockaddr ), ulFirstRxPortNumber, x;\r
159 uint32_t ulReceivedValue = 0, ulCount;\r
160 uint8_t ucReceivedValues[ selMAX_TX_VALUE ]; /* If the array position is pdTRUE then the corresponding value has been received. */\r
161 int32_t lBytes;\r
162 const TickType_t xRxBlockTime = 0;\r
163 BaseType_t xResult;\r
164 \r
165         /* The number of the port the first Rx socket will be bound to is passed in\r
166         as the task parameter.  Other port numbers used are consecutive from this. */\r
167         ulFirstRxPortNumber = ( uint32_t ) pvParameters;\r
168 \r
169         /* Create the set for sockets that will be passed into FreeRTOS_select(). */\r
170         xFD_Set = FreeRTOS_CreateSocketSet();\r
171 \r
172         /* Create the sockets and add them to the set. */\r
173         for( x = 0; x < selNUMBER_OF_SOCKETS; x++ )\r
174         {\r
175                 /* Create the next Rx socket. */\r
176                 xRxSockets[ x ] = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
177                 configASSERT( xRxSockets[ x ] != FREERTOS_INVALID_SOCKET );\r
178 \r
179                 /* Bind to the next port number. */\r
180                 xAddress.sin_port = FreeRTOS_htons( ( uint16_t ) ( ulFirstRxPortNumber + x ) );\r
181                 FreeRTOS_bind( xRxSockets[ x ], &xAddress, sizeof( struct freertos_sockaddr ) );\r
182 \r
183                 /* There should always be data available after FreeRTOS_select() so\r
184                 blocking on a read should not be necessary. */\r
185                 FreeRTOS_setsockopt( xRxSockets[ x ], 0, FREERTOS_SO_RCVTIMEO, &xRxBlockTime, sizeof( xRxBlockTime ) );\r
186 \r
187                 /* Add the created socket to the set. */\r
188                 FreeRTOS_FD_SET( xRxSockets[ x ], xFD_Set, eSELECT_ALL );\r
189         }\r
190 \r
191         for( ;; )\r
192         {\r
193                 /* No values have yet been received so set each array position to\r
194                 pdFALSE.  Each expected Rx value has a corresponding array position. */\r
195                 memset( ( void * ) ucReceivedValues, pdFALSE, sizeof( ucReceivedValues ) );\r
196 \r
197                 /* Wait for the other task to resume this task - indicating that it is\r
198                 about to start sending. */\r
199                 vTaskSuspend( NULL );\r
200 \r
201                 /* Expect to receive selMAX_TX_VALUE values. */\r
202                 ulCount = 0;\r
203 \r
204                 while( ulCount < selMAX_TX_VALUE )\r
205                 {\r
206                         /* Wait for a socket from the set to become available for\r
207                         reading. */\r
208                         xResult = FreeRTOS_select( xFD_Set, xReceiveBlockTime );\r
209 \r
210                         if( xResult != 0 )\r
211                         {\r
212                                 /* See which sockets have data waiting to be read. */\r
213                                 for( x = 0; x < selNUMBER_OF_SOCKETS; x++ )\r
214                                 {\r
215                                         xSocket = xRxSockets[ x ];\r
216 \r
217                                         /* Find the expected value for this socket */\r
218                                         if( FreeRTOS_FD_ISSET( xSocket, xFD_Set ) != 0 )\r
219                                         {\r
220                                                 while( ( lBytes = FreeRTOS_recvfrom( xSocket, &( ulReceivedValue ), sizeof( uint32_t ), 0, &xAddress, &xClientLength ) ) > 0 )\r
221                                                 {\r
222                                                         /* Received another message. */\r
223                                                         ulCount++;\r
224 \r
225                                                         /* It is always expected that the read will pass. */\r
226                                                         configASSERT( ( size_t ) lBytes == ( sizeof( uint32_t ) ) );\r
227 \r
228                                                         /* Don't expect to receive anything greater than\r
229                                                         selMAX_TX_VALUE - 1. */\r
230                                                         configASSERT( ulReceivedValue < selMAX_TX_VALUE );\r
231 \r
232                                                         /* Don't expect to receive any value twice. */\r
233                                                         configASSERT( ucReceivedValues[ ulReceivedValue ] != pdTRUE );\r
234                                                         if( ucReceivedValues[ ulReceivedValue ] != pdTRUE )\r
235                                                         {\r
236                                                                 /* Mark the value as received by setting its\r
237                                                                 index in the received array to pdTRUE. */\r
238                                                                 ucReceivedValues[ ulReceivedValue ] = pdTRUE;\r
239                                                         }\r
240                                                         else\r
241                                                         {\r
242                                                                 ulErrorOccurred = pdTRUE;\r
243                                                         }\r
244                                                 }\r
245                                         }\r
246                                 }\r
247                         }\r
248                         else\r
249                         {\r
250                                 /* No value was received in time. */\r
251                                 break;\r
252                         }\r
253                 }\r
254 \r
255                 /* Were all values received? */\r
256                 if( ulCount == selMAX_TX_VALUE )\r
257                 {\r
258                         /* Check all selMAX_TX_VALUE values are present and correct\r
259                         before starting a new cycle.  It is valid for a few values at\r
260                         the beginning of the array to be missing as they may have been\r
261                         dropped for ARP messages, so start a few indexes in. */\r
262                         for( ulCount = 4; ulCount < selMAX_TX_VALUE; ulCount++ )\r
263                         {\r
264                                 configASSERT( ucReceivedValues[ ulCount ] == pdTRUE );\r
265 \r
266                                 if( ucReceivedValues[ ulCount ] != pdTRUE )\r
267                                 {\r
268                                         /* The value corresponding to this array position was\r
269                                         never received.  In a real application UDP is not\r
270                                         reliable, but in this tightly controlled test it is\r
271                                         unusual for a packet to be dropped. */\r
272                                         ulErrorOccurred = pdTRUE;\r
273                                 }\r
274                         }\r
275 \r
276                         ulRxCycles++;\r
277                 }\r
278                 else\r
279                 {\r
280                         /* Just for viewing in the debugger. */\r
281                         ulFailedRxCycles++;\r
282                 }\r
283         }\r
284 }\r
285 /*-----------------------------------------------------------*/\r
286 \r
287 static void prvMultipleSocketTxTask( void *pvParameters )\r
288 {\r
289 uint32_t ulTxValue = 0;\r
290 struct freertos_sockaddr xDestinationAddress;\r
291 uint32_t ulIPAddress, ulFirstDestinationPortNumber, xPortNumber;\r
292 Socket_t xTxSocket;\r
293 uint32_t ulSendCount[ selNUMBER_OF_SOCKETS ];\r
294 \r
295         memset( ulSendCount, '\0', sizeof( ulSendCount ) );\r
296 \r
297         /* The first destination port number is passed in as the task parameter.\r
298         Other destination port numbers used are consecutive from this. */\r
299         ulFirstDestinationPortNumber = ( uint32_t ) pvParameters;\r
300 \r
301         /* Create the socket used to send to the sockets created by the Rx task.\r
302         Let the IP stack select a port to bind to. */\r
303         xTxSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
304         FreeRTOS_bind( xTxSocket, NULL, sizeof( struct freertos_sockaddr ) );\r
305 \r
306         /* The Rx and Tx tasks execute at the same priority so it is possible that\r
307         the Tx task will fill up the send queue - set a Tx block time to ensure\r
308         flow control is managed if this happens. */\r
309         FreeRTOS_setsockopt( xTxSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendBlockTime, sizeof( xSendBlockTime ) );\r
310 \r
311         /* It is assumed that this task is not created until the network is up,\r
312         so the IP address can be obtained immediately.  Store the IP address being\r
313         used in ulIPAddress.  This is done so the socket can send to a different\r
314         port on the same IP address. */\r
315         FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );\r
316 \r
317         /* This test sends to itself, so data sent from here is received by a server\r
318         socket on the same IP address.  Setup the freertos_sockaddr structure with\r
319         this nodes IP address. */\r
320         xDestinationAddress.sin_addr = ulIPAddress;\r
321 \r
322         /* Block for a short time to ensure the task implemented by the\r
323         prvMultipleSocketRxTask() function has finished creating the Rx sockets. */\r
324         while( eTaskGetState( xRxTaskHandle ) != eSuspended )\r
325         {\r
326                 vTaskDelay( xSendBlockTime );\r
327         }\r
328         vTaskResume( xRxTaskHandle );\r
329 \r
330         for( ;; )\r
331         {\r
332                 /* Pseudo randomly select the destination port number from the range of\r
333                 valid destination port numbers. */\r
334                 xPortNumber = ipconfigRAND32() % selNUMBER_OF_SOCKETS;\r
335                 ulSendCount[ xPortNumber ]++;\r
336                 xDestinationAddress.sin_port = ( uint16_t ) ( ulFirstDestinationPortNumber + xPortNumber );\r
337                 xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );\r
338 \r
339                 /* Send an incrementing value to the pseudo randomly selected port. */\r
340                 FreeRTOS_sendto( xTxSocket, &ulTxValue, sizeof( ulTxValue ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );\r
341                 ulTxValue++;\r
342 \r
343                 if( ulTxValue >= selMAX_TX_VALUE )\r
344                 {\r
345                         /* Start over. */\r
346                         ulTxValue = 0;\r
347 \r
348                         /* As a sanity check that this demo is valid, ensure each socket has\r
349                         been used at least once. */\r
350                         for( xPortNumber = 0; xPortNumber < selNUMBER_OF_SOCKETS; xPortNumber++ )\r
351                         {\r
352                                 if( ulSendCount[ xPortNumber ] == 0 )\r
353                                 {\r
354                                         ulErrorOccurred = pdTRUE;\r
355                                 }\r
356 \r
357                                 ulSendCount[ xPortNumber ] = 0;\r
358                         }\r
359 \r
360                         /* Allow the Rx task to check it has received all the values. */\r
361                         while( eTaskGetState( xRxTaskHandle ) != eSuspended )\r
362                         {\r
363                                 vTaskDelay( xSendBlockTime );\r
364                         }\r
365                         vTaskResume( xRxTaskHandle );\r
366 \r
367                         /* Increment to show this task is still executing. */\r
368                         ulTxCycles++;\r
369                 }\r
370 \r
371                 /* Delay here because in the Windows simulator the MAC interrupt\r
372                 simulator delays, so network traffic cannot be received any faster than\r
373                 this. */\r
374                 vTaskDelay( configWINDOWS_MAC_INTERRUPT_SIMULATOR_DELAY << 2 );\r
375         }\r
376 }\r
377 /*-----------------------------------------------------------*/\r
378 \r
379 BaseType_t xAreUDPSelectTasksStillRunning( void )\r
380 {\r
381 static uint32_t ulLastRxCycles = 0, ulLastTxCycles = 0;\r
382 BaseType_t ulError;\r
383 \r
384         ulError = ulErrorOccurred;\r
385 \r
386         if( ulRxCycles == ulLastRxCycles )\r
387         {\r
388                 ulError |= pdTRUE;\r
389         }\r
390 \r
391         if( ulTxCycles == ulLastTxCycles )\r
392         {\r
393                 ulError |= pdTRUE;\r
394         }\r
395 \r
396         ulLastRxCycles = ulRxCycles;\r
397         ulLastTxCycles = ulTxCycles;\r
398 \r
399         return !ulError;\r
400 }\r
401 \r
402 /* The whole file is excluded if select() is not being supported. */\r
403 #endif /* ipconfigSUPPORT_SELECT_FUNCTION */\r
404 \r