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