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