2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
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
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
25 ***************************************************************************
\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
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
37 ***************************************************************************
\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
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
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
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
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
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
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
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
77 * See the following web page for essential demo usage and configuration
\r
79 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html
\r
82 /* Standard includes. */
\r
86 /* FreeRTOS includes. */
\r
87 #include "FreeRTOS.h"
\r
91 /* FreeRTOS+TCP includes. */
\r
92 #include "FreeRTOS_IP.h"
\r
93 #include "FreeRTOS_Sockets.h"
\r
95 /* Demo project includes. */
\r
96 #include "UDPSelectServer.h"
\r
98 /* Exclude the whole file if select() is not being supported. */
\r
99 #if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
\r
101 /* The numbers of sockets added to the set. */
\r
102 #define selNUMBER_OF_SOCKETS ( 3 )
\r
104 /* Each cycle of the demo sends the value 0 to selMAX_TX_VALUE to a socket in
\r
106 #define selMAX_TX_VALUE ( 100 )
\r
108 /*-----------------------------------------------------------*/
\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
114 static void prvMultipleSocketTxTask( void *pvParameters );
\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
122 static void prvMultipleSocketRxTask( void *pvParameters );
\r
124 /*-----------------------------------------------------------*/
\r
126 /* The sockets used in FreeRTOS_select(). */
\r
127 static Socket_t xRxSockets[ selNUMBER_OF_SOCKETS ] = { 0 };
\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
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
137 /* The Tx task needs to know the handle of the Rx task. */
\r
138 static TaskHandle_t xRxTaskHandle;
\r
140 /*-----------------------------------------------------------*/
\r
142 void vStartUDPSelectServerTasks( uint16_t usStackSize, uint32_t ulFirstPortNumber, UBaseType_t uxPriority )
\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
151 /*-----------------------------------------------------------*/
\r
153 static void prvMultipleSocketRxTask( void *pvParameters )
\r
155 SocketSet_t xFD_Set;
\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
162 const TickType_t xRxBlockTime = 0;
\r
163 BaseType_t xResult;
\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
169 /* Create the set for sockets that will be passed into FreeRTOS_select(). */
\r
170 xFD_Set = FreeRTOS_CreateSocketSet();
\r
172 /* Create the sockets and add them to the set. */
\r
173 for( x = 0; x < selNUMBER_OF_SOCKETS; x++ )
\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
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
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
187 /* Add the created socket to the set. */
\r
188 FreeRTOS_FD_SET( xRxSockets[ x ], xFD_Set, eSELECT_ALL );
\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
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
201 /* Expect to receive selMAX_TX_VALUE values. */
\r
204 while( ulCount < selMAX_TX_VALUE )
\r
206 /* Wait for a socket from the set to become available for
\r
208 xResult = FreeRTOS_select( xFD_Set, xReceiveBlockTime );
\r
212 /* See which sockets have data waiting to be read. */
\r
213 for( x = 0; x < selNUMBER_OF_SOCKETS; x++ )
\r
215 xSocket = xRxSockets[ x ];
\r
217 /* Find the expected value for this socket */
\r
218 if( FreeRTOS_FD_ISSET( xSocket, xFD_Set ) != 0 )
\r
220 while( ( lBytes = FreeRTOS_recvfrom( xSocket, &( ulReceivedValue ), sizeof( uint32_t ), 0, &xAddress, &xClientLength ) ) > 0 )
\r
222 /* Received another message. */
\r
225 /* It is always expected that the read will pass. */
\r
226 configASSERT( ( size_t ) lBytes == ( sizeof( uint32_t ) ) );
\r
228 /* Don't expect to receive anything greater than
\r
229 selMAX_TX_VALUE - 1. */
\r
230 configASSERT( ulReceivedValue < selMAX_TX_VALUE );
\r
232 /* Don't expect to receive any value twice. */
\r
233 configASSERT( ucReceivedValues[ ulReceivedValue ] != pdTRUE );
\r
234 if( ucReceivedValues[ ulReceivedValue ] != pdTRUE )
\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
242 ulErrorOccurred = pdTRUE;
\r
250 /* No value was received in time. */
\r
255 /* Were all values received? */
\r
256 if( ulCount == selMAX_TX_VALUE )
\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
264 configASSERT( ucReceivedValues[ ulCount ] == pdTRUE );
\r
266 if( ucReceivedValues[ ulCount ] != pdTRUE )
\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
280 /* Just for viewing in the debugger. */
\r
281 ulFailedRxCycles++;
\r
285 /*-----------------------------------------------------------*/
\r
287 static void prvMultipleSocketTxTask( void *pvParameters )
\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
295 memset( ulSendCount, '\0', sizeof( ulSendCount ) );
\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
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
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
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
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
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
326 vTaskDelay( xSendBlockTime );
\r
328 vTaskResume( xRxTaskHandle );
\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
339 /* Send an incrementing value to the pseudo randomly selected port. */
\r
340 FreeRTOS_sendto( xTxSocket, &ulTxValue, sizeof( ulTxValue ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );
\r
343 if( ulTxValue >= selMAX_TX_VALUE )
\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
352 if( ulSendCount[ xPortNumber ] == 0 )
\r
354 ulErrorOccurred = pdTRUE;
\r
357 ulSendCount[ xPortNumber ] = 0;
\r
360 /* Allow the Rx task to check it has received all the values. */
\r
361 while( eTaskGetState( xRxTaskHandle ) != eSuspended )
\r
363 vTaskDelay( xSendBlockTime );
\r
365 vTaskResume( xRxTaskHandle );
\r
367 /* Increment to show this task is still executing. */
\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
374 vTaskDelay( configWINDOWS_MAC_INTERRUPT_SIMULATOR_DELAY << 2 );
\r
377 /*-----------------------------------------------------------*/
\r
379 BaseType_t xAreUDPSelectTasksStillRunning( void )
\r
381 static uint32_t ulLastRxCycles = 0, ulLastTxCycles = 0;
\r
382 BaseType_t ulError;
\r
384 ulError = ulErrorOccurred;
\r
386 if( ulRxCycles == ulLastRxCycles )
\r
391 if( ulTxCycles == ulLastTxCycles )
\r
396 ulLastRxCycles = ulRxCycles;
\r
397 ulLastTxCycles = ulTxCycles;
\r
402 /* The whole file is excluded if select() is not being supported. */
\r
403 #endif /* ipconfigSUPPORT_SELECT_FUNCTION */
\r