]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_Windows_Simulator/DemoTasks/SelectServer.c
Update version numbers to V7.4.1.
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_Windows_Simulator / DemoTasks / SelectServer.c
1 /*\r
2     FreeRTOS V7.4.1 - 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 /*_RB_ See http://TBD for a description of these tasks. */\r
76 \r
77 /* Standard includes. */\r
78 #include <stdint.h>\r
79 #include <stdio.h>\r
80 \r
81 /* FreeRTOS includes. */\r
82 #include "FreeRTOS.h"\r
83 #include "task.h"\r
84 \r
85 /* FreeRTOS+UDP includes. */\r
86 #include "FreeRTOS_UDP_IP.h"\r
87 #include "FreeRTOS_Sockets.h"\r
88 \r
89 #define selTINY_DELAY                           ( ( portTickType ) 2 )\r
90 #define selNUMBER_OF_SOCKETS            ( 3 )\r
91 #define selSELECT_QUEUE_SIZE            ( selNUMBER_OF_SOCKETS * 2 )\r
92 \r
93 /*\r
94  * Sends data to multiple different sockets.\r
95  */\r
96 static void prvMultipleSocketTxTask( void *pvParameters );\r
97 \r
98 /*\r
99  * Uses the FreeRTOS_select() API function to receive from multiple sockets.\r
100  */\r
101 static void prvMultipleSocketRxTask( void *pvParameters );\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 static xSocket_t xRxSockets[ selNUMBER_OF_SOCKETS ] = { 0 };\r
106 \r
107 /*-----------------------------------------------------------*/\r
108 \r
109 void vStartSelectUDPServerTasks( uint16_t usStackSize, uint32_t ulFirstPortNumber, unsigned portBASE_TYPE uxPriority )\r
110 {\r
111         /* Create task that sends to multiple sockets, and the task that uses the\r
112         FreeRTOS_select() function to receive from multiple sockets.  The first\r
113         port number to use is passed into both tasks using the task's parameter.\r
114         Other port numbers are consecutive from the first. */\r
115         xTaskCreate( prvMultipleSocketTxTask, ( const signed char * const ) "MultiTx", usStackSize, ( void * ) ulFirstPortNumber, uxPriority, NULL );\r
116         xTaskCreate( prvMultipleSocketRxTask, ( const signed char * const ) "MultiRx", usStackSize, ( void * ) ulFirstPortNumber, uxPriority, NULL );\r
117 }\r
118 /*-----------------------------------------------------------*/\r
119 \r
120 static void prvMultipleSocketRxTask( void *pvParameters )\r
121 {\r
122 xSocketSet_t xFD_Set;\r
123 xSocket_t xSocket;\r
124 struct freertos_sockaddr xAddress;\r
125 uint32_t xClientLength = sizeof( struct freertos_sockaddr ), ulFirstRxPortNumber, x;\r
126 uint32_t ulReceivedValue = 0, ulExpectedValue = 0UL, ulReceivedCount[ selNUMBER_OF_SOCKETS ] = { 0 };\r
127 int32_t lBytes;\r
128 const portTickType xRxBlockTime = 0;\r
129 \r
130         /* The number of the port the first Rx socket will be bound to is passed in\r
131         as the task parameter.  Other port numbers used are consecutive from this. */\r
132         ulFirstRxPortNumber = ( uint32_t ) pvParameters;\r
133 \r
134         /* Create the set of sockets that will be passed into FreeRTOS_select(). */\r
135         xFD_Set = FreeRTOS_CreateSocketSet( selSELECT_QUEUE_SIZE );\r
136 \r
137         for( x = 0; x < selNUMBER_OF_SOCKETS; x++ )\r
138         {\r
139                 /* Create the next Rx socket. */\r
140                 xRxSockets[ x ] = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
141                 configASSERT( xRxSockets[ x ] );\r
142 \r
143                 /* Bind to the next port number. */\r
144                 xAddress.sin_port = FreeRTOS_htons( ( uint16_t ) ( ulFirstRxPortNumber + x ) );\r
145                 FreeRTOS_bind( xRxSockets[ x ], &xAddress, sizeof( struct freertos_sockaddr ) );\r
146 \r
147                 /* There should always be data available on the socket returned from\r
148                 FreeRTOS_select() so blocking on a read should not be necessary. */\r
149                 FreeRTOS_setsockopt( xRxSockets[ x ], 0, FREERTOS_SO_RCVTIMEO, &xRxBlockTime, sizeof( xRxBlockTime ) );\r
150 \r
151                 /* Add the created socket to the set. */\r
152                 FreeRTOS_FD_SET( xRxSockets[ x ], xFD_Set );\r
153         }\r
154 \r
155         for( ;; )\r
156         {\r
157                 /* Wait for a socket from the set to become available for reading. */\r
158                 xSocket = FreeRTOS_select( xFD_Set, portMAX_DELAY );\r
159 \r
160                 /* xSocket should never be NULL because FreeRTOS_select() was called\r
161                 with an indefinite delay (assuming INCLUDE_vTaskSuspend is set to 1). */\r
162                 configASSERT( xSocket );\r
163 \r
164                 lBytes = FreeRTOS_recvfrom( xSocket, &( ulReceivedValue ), sizeof( uint32_t ), 0, &xAddress, &xClientLength );\r
165 \r
166                 /* It is possible that the first value received will not be zero\r
167                 because the first few transmitted packets may have been dropped to\r
168                 send an ARP and then wait the ARP reply. */\r
169                 if( ulExpectedValue == 0 )\r
170                 {\r
171                         if( ulExpectedValue != ulReceivedValue )\r
172                         {\r
173                                 /* Correct for packets lost to ARP traffic. */\r
174                                 ulExpectedValue = ulReceivedValue;\r
175                         }\r
176                 }\r
177 \r
178                 /* Data should always be available even though the block time was set\r
179                 to zero because the socket was returned from FreeRTOS_select(). */\r
180                 configASSERT( lBytes == 4 );\r
181                 configASSERT( ulReceivedValue == ulExpectedValue );\r
182 \r
183                 ulExpectedValue++;\r
184 \r
185                 /* Keep a record of the number of times each socket has been used so it\r
186                 can be verified (using the debugger) that they all get used. */\r
187                 for( x= 0; x < selNUMBER_OF_SOCKETS; x++ )\r
188                 {\r
189                         if( xSocket == xRxSockets[ x ] )\r
190                         {\r
191                                 ( ulReceivedCount[ x ] )++;\r
192                                 break;\r
193                         }\r
194                 }\r
195         }\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 static void prvMultipleSocketTxTask( void *pvParameters )\r
200 {\r
201 uint32_t ulTxValue = 0;\r
202 struct freertos_sockaddr xDestinationAddress;\r
203 uint32_t ulIPAddress, ulFirstDestinationPortNumber, xPortNumber;\r
204 xSocket_t xTxSocket;\r
205 const portTickType xShortDelay = 100 / portTICK_RATE_MS, xSendBlockTime = 500 / portTICK_RATE_MS;\r
206 \r
207         srand( ( unsigned int ) &xPortNumber );\r
208 \r
209         /* The first destination port number is passed in as the task parameter.\r
210         Other destination port numbers used are consecutive from this. */\r
211         ulFirstDestinationPortNumber = ( uint32_t ) pvParameters;\r
212 \r
213         /* Create the socket used to send to the sockets created by the Rx task.\r
214         Let the IP stack select a port to bind to. */\r
215         xTxSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
216         FreeRTOS_bind( xTxSocket, NULL, sizeof( struct freertos_sockaddr ) );\r
217 \r
218         /* The Rx and Tx tasks execute at the same priority so it is possible that\r
219         the Tx task will fill up the send queue - set a Tx block time to ensure\r
220         flow control is managed if this happens. */\r
221         FreeRTOS_setsockopt( xTxSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendBlockTime, sizeof( xSendBlockTime ) );\r
222 \r
223         /* It is assumed that this task is not created until the network is up,\r
224         so the IP address can be obtained immediately.  Store the IP address being\r
225         used in ulIPAddress.  This is done so the socket can send to a different\r
226         port on the same IP address. */\r
227         FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );\r
228 \r
229         /* This test sends to itself, so data sent from here is received by a server\r
230         socket on the same IP address.  Setup the freertos_sockaddr structure with\r
231         this nodes IP address. */\r
232         xDestinationAddress.sin_addr = ulIPAddress;\r
233 \r
234         /* Block for a short time to ensure the task implemented by the\r
235         prvMultipleSocketRxTask() function has finished creating the Rx sockets. */\r
236         vTaskDelay( xShortDelay );\r
237 \r
238         for( ;; )\r
239         {\r
240                 /* Pseudo randomly select the destination port number from the range of\r
241                 valid destination port numbers. */\r
242                 xPortNumber = rand() % selNUMBER_OF_SOCKETS;\r
243                 xDestinationAddress.sin_port = ( uint16_t ) ( ulFirstDestinationPortNumber + xPortNumber );\r
244                 xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );\r
245 \r
246                 /* Send an incrementing value. */\r
247                 FreeRTOS_sendto( xTxSocket, &ulTxValue, sizeof( ulTxValue ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );\r
248                 ulTxValue++;\r
249 \r
250                 /* Delay here because in the Windows simulator the MAC interrupt\r
251                 simulator delays, so network trafic cannot be received any faster\r
252                 than this. */\r
253                 vTaskDelay( configWINDOWS_MAC_INTERRUPT_SIMULATOR_DELAY );\r
254         }\r
255 }\r
256 \r
257 \r
258 \r