]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_Windows_Simulator/DemoTasks/SelectServer.c
Update version number to 8.1.1 for patch release that re-enables mutexes to be given...
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_Windows_Simulator / DemoTasks / SelectServer.c
1 /*\r
2     FreeRTOS V8.1.1 - Copyright (C) 2014 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67  * A number of sockets are created and added to a set. One task then blocks on\r
68  * the set while the other task sends data to a (pseudo) random member of the\r
69  * set.\r
70  */\r
71 \r
72 /* Standard includes. */\r
73 #include <stdint.h>\r
74 #include <stdio.h>\r
75 \r
76 /* FreeRTOS includes. */\r
77 #include "FreeRTOS.h"\r
78 #include "task.h"\r
79 \r
80 /* FreeRTOS+UDP includes. */\r
81 #include "FreeRTOS_UDP_IP.h"\r
82 #include "FreeRTOS_Sockets.h"\r
83 \r
84 #define selTINY_DELAY                           ( ( TickType_t ) 2 )\r
85 #define selNUMBER_OF_SOCKETS            ( 3 )\r
86 #define selSELECT_QUEUE_SIZE            ( selNUMBER_OF_SOCKETS * 2 )\r
87 \r
88 /*\r
89  * Sends data to multiple different sockets.\r
90  */\r
91 static void prvMultipleSocketTxTask( void *pvParameters );\r
92 \r
93 /*\r
94  * Uses the FreeRTOS_select() API function to receive from multiple sockets.\r
95  */\r
96 static void prvMultipleSocketRxTask( void *pvParameters );\r
97 \r
98 /*-----------------------------------------------------------*/\r
99 \r
100 static xSocket_t xRxSockets[ selNUMBER_OF_SOCKETS ] = { 0 };\r
101 \r
102 /*-----------------------------------------------------------*/\r
103 \r
104 void vStartSelectUDPServerTasks( uint16_t usStackSize, uint32_t ulFirstPortNumber, UBaseType_t uxPriority )\r
105 {\r
106         /* Create task that sends to multiple sockets, and the task that uses the\r
107         FreeRTOS_select() function to receive from multiple sockets.  The first\r
108         port number to use is passed into both tasks using the task's parameter.\r
109         Other port numbers are consecutive from the first. */\r
110         xTaskCreate( prvMultipleSocketTxTask, "MultiTx", usStackSize, ( void * ) ulFirstPortNumber, uxPriority, NULL );\r
111         xTaskCreate( prvMultipleSocketRxTask, "MultiRx", usStackSize, ( void * ) ulFirstPortNumber, uxPriority, NULL );\r
112 }\r
113 /*-----------------------------------------------------------*/\r
114 \r
115 static void prvMultipleSocketRxTask( void *pvParameters )\r
116 {\r
117 xSocketSet_t xFD_Set;\r
118 xSocket_t xSocket;\r
119 struct freertos_sockaddr xAddress;\r
120 uint32_t xClientLength = sizeof( struct freertos_sockaddr ), ulFirstRxPortNumber, x;\r
121 uint32_t ulReceivedValue = 0, ulExpectedValue = 0UL, ulReceivedCount[ selNUMBER_OF_SOCKETS ] = { 0 };\r
122 int32_t lBytes;\r
123 const TickType_t xRxBlockTime = 0;\r
124 \r
125         /* The number of the port the first Rx socket will be bound to is passed in\r
126         as the task parameter.  Other port numbers used are consecutive from this. */\r
127         ulFirstRxPortNumber = ( uint32_t ) pvParameters;\r
128 \r
129         /* Create the set of sockets that will be passed into FreeRTOS_select(). */\r
130         xFD_Set = FreeRTOS_CreateSocketSet( selSELECT_QUEUE_SIZE );\r
131 \r
132         for( x = 0; x < selNUMBER_OF_SOCKETS; x++ )\r
133         {\r
134                 /* Create the next Rx socket. */\r
135                 xRxSockets[ x ] = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
136                 configASSERT( xRxSockets[ x ] );\r
137 \r
138                 /* Bind to the next port number. */\r
139                 xAddress.sin_port = FreeRTOS_htons( ( uint16_t ) ( ulFirstRxPortNumber + x ) );\r
140                 FreeRTOS_bind( xRxSockets[ x ], &xAddress, sizeof( struct freertos_sockaddr ) );\r
141 \r
142                 /* There should always be data available on the socket returned from\r
143                 FreeRTOS_select() so blocking on a read should not be necessary. */\r
144                 FreeRTOS_setsockopt( xRxSockets[ x ], 0, FREERTOS_SO_RCVTIMEO, &xRxBlockTime, sizeof( xRxBlockTime ) );\r
145 \r
146                 /* Add the created socket to the set. */\r
147                 FreeRTOS_FD_SET( xRxSockets[ x ], xFD_Set );\r
148         }\r
149 \r
150         for( ;; )\r
151         {\r
152                 /* Wait for a socket from the set to become available for reading. */\r
153                 xSocket = FreeRTOS_select( xFD_Set, portMAX_DELAY );\r
154 \r
155                 /* xSocket should never be NULL because FreeRTOS_select() was called\r
156                 with an indefinite delay (assuming INCLUDE_vTaskSuspend is set to 1). */\r
157                 configASSERT( xSocket );\r
158 \r
159                 lBytes = FreeRTOS_recvfrom( xSocket, &( ulReceivedValue ), sizeof( uint32_t ), 0, &xAddress, &xClientLength );\r
160 \r
161                 /* It is possible that the first value received will not be zero\r
162                 because the first few transmitted packets may have been dropped to\r
163                 send an ARP and then wait the ARP reply. */\r
164                 if( ulExpectedValue == 0 )\r
165                 {\r
166                         if( ulExpectedValue != ulReceivedValue )\r
167                         {\r
168                                 /* Correct for packets lost to ARP traffic. */\r
169                                 ulExpectedValue = ulReceivedValue;\r
170                         }\r
171                 }\r
172 \r
173                 /* Data should always be available even though the block time was set\r
174                 to zero because the socket was returned from FreeRTOS_select(). */\r
175                 configASSERT( lBytes == 4 );\r
176                 configASSERT( ulReceivedValue == ulExpectedValue );\r
177 \r
178                 ulExpectedValue++;\r
179 \r
180                 /* Keep a record of the number of times each socket has been used so it\r
181                 can be verified (using the debugger) that they all get used. */\r
182                 for( x= 0; x < selNUMBER_OF_SOCKETS; x++ )\r
183                 {\r
184                         if( xSocket == xRxSockets[ x ] )\r
185                         {\r
186                                 ( ulReceivedCount[ x ] )++;\r
187                                 break;\r
188                         }\r
189                 }\r
190         }\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 static void prvMultipleSocketTxTask( void *pvParameters )\r
195 {\r
196 uint32_t ulTxValue = 0;\r
197 struct freertos_sockaddr xDestinationAddress;\r
198 uint32_t ulIPAddress, ulFirstDestinationPortNumber, xPortNumber;\r
199 xSocket_t xTxSocket;\r
200 const TickType_t xShortDelay = 100 / portTICK_RATE_MS, xSendBlockTime = 500 / portTICK_RATE_MS;\r
201 \r
202         srand( ( unsigned int ) &xPortNumber );\r
203 \r
204         /* The first destination port number is passed in as the task parameter.\r
205         Other destination port numbers used are consecutive from this. */\r
206         ulFirstDestinationPortNumber = ( uint32_t ) pvParameters;\r
207 \r
208         /* Create the socket used to send to the sockets created by the Rx task.\r
209         Let the IP stack select a port to bind to. */\r
210         xTxSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
211         FreeRTOS_bind( xTxSocket, NULL, sizeof( struct freertos_sockaddr ) );\r
212 \r
213         /* The Rx and Tx tasks execute at the same priority so it is possible that\r
214         the Tx task will fill up the send queue - set a Tx block time to ensure\r
215         flow control is managed if this happens. */\r
216         FreeRTOS_setsockopt( xTxSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendBlockTime, sizeof( xSendBlockTime ) );\r
217 \r
218         /* It is assumed that this task is not created until the network is up,\r
219         so the IP address can be obtained immediately.  Store the IP address being\r
220         used in ulIPAddress.  This is done so the socket can send to a different\r
221         port on the same IP address. */\r
222         FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );\r
223 \r
224         /* This test sends to itself, so data sent from here is received by a server\r
225         socket on the same IP address.  Setup the freertos_sockaddr structure with\r
226         this nodes IP address. */\r
227         xDestinationAddress.sin_addr = ulIPAddress;\r
228 \r
229         /* Block for a short time to ensure the task implemented by the\r
230         prvMultipleSocketRxTask() function has finished creating the Rx sockets. */\r
231         vTaskDelay( xShortDelay );\r
232 \r
233         for( ;; )\r
234         {\r
235                 /* Pseudo randomly select the destination port number from the range of\r
236                 valid destination port numbers. */\r
237                 xPortNumber = rand() % selNUMBER_OF_SOCKETS;\r
238                 xDestinationAddress.sin_port = ( uint16_t ) ( ulFirstDestinationPortNumber + xPortNumber );\r
239                 xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );\r
240 \r
241                 /* Send an incrementing value. */\r
242                 FreeRTOS_sendto( xTxSocket, &ulTxValue, sizeof( ulTxValue ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );\r
243                 ulTxValue++;\r
244 \r
245                 /* Delay here because in the Windows simulator the MAC interrupt\r
246                 simulator delays, so network trafic cannot be received any faster\r
247                 than this. */\r
248                 vTaskDelay( configWINDOWS_MAC_INTERRUPT_SIMULATOR_DELAY );\r
249         }\r
250 }\r
251 \r
252 \r
253 \r