]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_Windows_Simulator/DemoTasks/SelectServer.c
Roll up the minor changes checked into svn since V10.0.0 into new V10.0.1 ready for...
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_Windows_Simulator / DemoTasks / SelectServer.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * A number of sockets are created and added to a set. One task then blocks on\r
30  * the set while the other task sends data to a (pseudo) random member of the\r
31  * set.\r
32  */\r
33 \r
34 /* Standard includes. */\r
35 #include <stdint.h>\r
36 #include <stdio.h>\r
37 \r
38 /* FreeRTOS includes. */\r
39 #include "FreeRTOS.h"\r
40 #include "task.h"\r
41 \r
42 /* FreeRTOS+UDP includes. */\r
43 #include "FreeRTOS_UDP_IP.h"\r
44 #include "FreeRTOS_Sockets.h"\r
45 \r
46 #define selTINY_DELAY                           ( ( TickType_t ) 2 )\r
47 #define selNUMBER_OF_SOCKETS            ( 3 )\r
48 #define selSELECT_QUEUE_SIZE            ( selNUMBER_OF_SOCKETS * 2 )\r
49 \r
50 /*\r
51  * Sends data to multiple different sockets.\r
52  */\r
53 static void prvMultipleSocketTxTask( void *pvParameters );\r
54 \r
55 /*\r
56  * Uses the FreeRTOS_select() API function to receive from multiple sockets.\r
57  */\r
58 static void prvMultipleSocketRxTask( void *pvParameters );\r
59 \r
60 /*-----------------------------------------------------------*/\r
61 \r
62 static xSocket_t xRxSockets[ selNUMBER_OF_SOCKETS ] = { 0 };\r
63 \r
64 /*-----------------------------------------------------------*/\r
65 \r
66 void vStartSelectUDPServerTasks( uint16_t usStackSize, uint32_t ulFirstPortNumber, UBaseType_t uxPriority )\r
67 {\r
68         /* Create task that sends to multiple sockets, and the task that uses the\r
69         FreeRTOS_select() function to receive from multiple sockets.  The first\r
70         port number to use is passed into both tasks using the task's parameter.\r
71         Other port numbers are consecutive from the first. */\r
72         xTaskCreate( prvMultipleSocketTxTask, "MultiTx", usStackSize, ( void * ) ulFirstPortNumber, uxPriority, NULL );\r
73         xTaskCreate( prvMultipleSocketRxTask, "MultiRx", usStackSize, ( void * ) ulFirstPortNumber, uxPriority, NULL );\r
74 }\r
75 /*-----------------------------------------------------------*/\r
76 \r
77 static void prvMultipleSocketRxTask( void *pvParameters )\r
78 {\r
79 xSocketSet_t xFD_Set;\r
80 xSocket_t xSocket;\r
81 struct freertos_sockaddr xAddress;\r
82 uint32_t xClientLength = sizeof( struct freertos_sockaddr ), ulFirstRxPortNumber, x;\r
83 uint32_t ulReceivedValue = 0, ulExpectedValue = 0UL, ulReceivedCount[ selNUMBER_OF_SOCKETS ] = { 0 };\r
84 int32_t lBytes;\r
85 const TickType_t xRxBlockTime = 0;\r
86 \r
87         /* The number of the port the first Rx socket will be bound to is passed in\r
88         as the task parameter.  Other port numbers used are consecutive from this. */\r
89         ulFirstRxPortNumber = ( uint32_t ) pvParameters;\r
90 \r
91         /* Create the set of sockets that will be passed into FreeRTOS_select(). */\r
92         xFD_Set = FreeRTOS_CreateSocketSet( selSELECT_QUEUE_SIZE );\r
93 \r
94         for( x = 0; x < selNUMBER_OF_SOCKETS; x++ )\r
95         {\r
96                 /* Create the next Rx socket. */\r
97                 xRxSockets[ x ] = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
98                 configASSERT( xRxSockets[ x ] );\r
99 \r
100                 /* Bind to the next port number. */\r
101                 xAddress.sin_port = FreeRTOS_htons( ( uint16_t ) ( ulFirstRxPortNumber + x ) );\r
102                 FreeRTOS_bind( xRxSockets[ x ], &xAddress, sizeof( struct freertos_sockaddr ) );\r
103 \r
104                 /* There should always be data available on the socket returned from\r
105                 FreeRTOS_select() so blocking on a read should not be necessary. */\r
106                 FreeRTOS_setsockopt( xRxSockets[ x ], 0, FREERTOS_SO_RCVTIMEO, &xRxBlockTime, sizeof( xRxBlockTime ) );\r
107 \r
108                 /* Add the created socket to the set. */\r
109                 FreeRTOS_FD_SET( xRxSockets[ x ], xFD_Set );\r
110         }\r
111 \r
112         for( ;; )\r
113         {\r
114                 /* Wait for a socket from the set to become available for reading. */\r
115                 xSocket = FreeRTOS_select( xFD_Set, portMAX_DELAY );\r
116 \r
117                 /* xSocket should never be NULL because FreeRTOS_select() was called\r
118                 with an indefinite delay (assuming INCLUDE_vTaskSuspend is set to 1). */\r
119                 configASSERT( xSocket );\r
120 \r
121                 lBytes = FreeRTOS_recvfrom( xSocket, &( ulReceivedValue ), sizeof( uint32_t ), 0, &xAddress, &xClientLength );\r
122 \r
123                 /* It is possible that the first value received will not be zero\r
124                 because the first few transmitted packets may have been dropped to\r
125                 send an ARP and then wait the ARP reply. */\r
126                 if( ulExpectedValue == 0 )\r
127                 {\r
128                         if( ulExpectedValue != ulReceivedValue )\r
129                         {\r
130                                 /* Correct for packets lost to ARP traffic. */\r
131                                 ulExpectedValue = ulReceivedValue;\r
132                         }\r
133                 }\r
134 \r
135                 /* Data should always be available even though the block time was set\r
136                 to zero because the socket was returned from FreeRTOS_select(). */\r
137                 configASSERT( lBytes == 4 );\r
138                 configASSERT( ulReceivedValue == ulExpectedValue );\r
139 \r
140                 ulExpectedValue++;\r
141 \r
142                 /* Keep a record of the number of times each socket has been used so it\r
143                 can be verified (using the debugger) that they all get used. */\r
144                 for( x= 0; x < selNUMBER_OF_SOCKETS; x++ )\r
145                 {\r
146                         if( xSocket == xRxSockets[ x ] )\r
147                         {\r
148                                 ( ulReceivedCount[ x ] )++;\r
149                                 break;\r
150                         }\r
151                 }\r
152         }\r
153 }\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 static void prvMultipleSocketTxTask( void *pvParameters )\r
157 {\r
158 uint32_t ulTxValue = 0;\r
159 struct freertos_sockaddr xDestinationAddress;\r
160 uint32_t ulIPAddress, ulFirstDestinationPortNumber, xPortNumber;\r
161 xSocket_t xTxSocket;\r
162 const TickType_t xShortDelay = 100 / portTICK_RATE_MS, xSendBlockTime = 500 / portTICK_RATE_MS;\r
163 \r
164         srand( ( unsigned int ) &xPortNumber );\r
165 \r
166         /* The first destination port number is passed in as the task parameter.\r
167         Other destination port numbers used are consecutive from this. */\r
168         ulFirstDestinationPortNumber = ( uint32_t ) pvParameters;\r
169 \r
170         /* Create the socket used to send to the sockets created by the Rx task.\r
171         Let the IP stack select a port to bind to. */\r
172         xTxSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
173         FreeRTOS_bind( xTxSocket, NULL, sizeof( struct freertos_sockaddr ) );\r
174 \r
175         /* The Rx and Tx tasks execute at the same priority so it is possible that\r
176         the Tx task will fill up the send queue - set a Tx block time to ensure\r
177         flow control is managed if this happens. */\r
178         FreeRTOS_setsockopt( xTxSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendBlockTime, sizeof( xSendBlockTime ) );\r
179 \r
180         /* It is assumed that this task is not created until the network is up,\r
181         so the IP address can be obtained immediately.  Store the IP address being\r
182         used in ulIPAddress.  This is done so the socket can send to a different\r
183         port on the same IP address. */\r
184         FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );\r
185 \r
186         /* This test sends to itself, so data sent from here is received by a server\r
187         socket on the same IP address.  Setup the freertos_sockaddr structure with\r
188         this nodes IP address. */\r
189         xDestinationAddress.sin_addr = ulIPAddress;\r
190 \r
191         /* Block for a short time to ensure the task implemented by the\r
192         prvMultipleSocketRxTask() function has finished creating the Rx sockets. */\r
193         vTaskDelay( xShortDelay );\r
194 \r
195         for( ;; )\r
196         {\r
197                 /* Pseudo randomly select the destination port number from the range of\r
198                 valid destination port numbers. */\r
199                 xPortNumber = rand() % selNUMBER_OF_SOCKETS;\r
200                 xDestinationAddress.sin_port = ( uint16_t ) ( ulFirstDestinationPortNumber + xPortNumber );\r
201                 xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );\r
202 \r
203                 /* Send an incrementing value. */\r
204                 FreeRTOS_sendto( xTxSocket, &ulTxValue, sizeof( ulTxValue ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );\r
205                 ulTxValue++;\r
206 \r
207                 /* Delay here because in the Windows simulator the MAC interrupt\r
208                 simulator delays, so network trafic cannot be received any faster\r
209                 than this. */\r
210                 vTaskDelay( configWINDOWS_MAC_INTERRUPT_SIMULATOR_DELAY );\r
211         }\r
212 }\r
213 \r
214 \r
215 \r