]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_TCP_Minimal_Windows_Simulator/main.c
fae30bd74690936f5093d293410595857276ac2c
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_TCP_Minimal_Windows_Simulator / main.c
1 /*\r
2  * FreeRTOS Kernel V10.2.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  * This project is a cut down version of the project described on the following\r
30  * link.  Only the simple UDP client and server and the TCP echo clients are\r
31  * included in the build:\r
32  * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html\r
33  */\r
34 \r
35 /* Standard includes. */\r
36 #include <stdio.h>\r
37 #include <time.h>\r
38 \r
39 /* FreeRTOS includes. */\r
40 #include <FreeRTOS.h>\r
41 #include "task.h"\r
42 \r
43 /* Demo application includes. */\r
44 #include "FreeRTOS_IP.h"\r
45 #include "FreeRTOS_Sockets.h"\r
46 #include "SimpleUDPClientAndServer.h"\r
47 #include "SimpleTCPEchoServer.h"\r
48 #include "TCPEchoClient_SingleTasks.h"\r
49 #include "demo_logging.h"\r
50 \r
51 /* Simple UDP client and server task parameters. */\r
52 #define mainSIMPLE_UDP_CLIENT_SERVER_TASK_PRIORITY              ( tskIDLE_PRIORITY )\r
53 #define mainSIMPLE_UDP_CLIENT_SERVER_PORT                               ( 5005UL )\r
54 \r
55 /* Echo client task parameters - used for both TCP and UDP echo clients. */\r
56 #define mainECHO_CLIENT_TASK_STACK_SIZE                                 ( configMINIMAL_STACK_SIZE * 2 )        /* Not used in the Windows port. */\r
57 #define mainECHO_CLIENT_TASK_PRIORITY                                   ( tskIDLE_PRIORITY + 1 )\r
58 \r
59 /* Echo server task parameters. */\r
60 #define mainECHO_SERVER_TASK_STACK_SIZE                                 ( configMINIMAL_STACK_SIZE * 2 )        /* Not used in the Windows port. */\r
61 #define mainECHO_SERVER_TASK_PRIORITY                                   ( tskIDLE_PRIORITY + 1 )\r
62 \r
63 /* Define a name that will be used for LLMNR and NBNS searches. */\r
64 #define mainHOST_NAME                           "RTOSDemo"\r
65 #define mainDEVICE_NICK_NAME            "windows_demo"\r
66 \r
67 /* Set the following constants to 1 or 0 to define which tasks to include and\r
68 exclude:\r
69 \r
70 mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS:  When set to 1 two UDP client tasks\r
71 and two UDP server tasks are created.  The clients talk to the servers.  One set\r
72 of tasks use the standard sockets interface, and the other the zero copy sockets\r
73 interface.  These tasks are self checking and will trigger a configASSERT() if\r
74 they detect a difference in the data that is received from that which was sent.\r
75 As these tasks use UDP, and can therefore loose packets, they will cause\r
76 configASSERT() to be called when they are run in a less than perfect networking\r
77 environment.\r
78 \r
79 mainCREATE_TCP_ECHO_TASKS_SINGLE:  When set to 1 a set of tasks are created that\r
80 send TCP echo requests to the standard echo port (port 7), then wait for and\r
81 verify the echo reply, from within the same task (Tx and Rx are performed in the\r
82 same RTOS task).  The IP address of the echo server must be configured using the\r
83 configECHO_SERVER_ADDR0 to configECHO_SERVER_ADDR3 constants in\r
84 FreeRTOSConfig.h.\r
85 \r
86 mainCREATE_TCP_ECHO_SERVER_TASK:  When set to 1 a task is created that accepts\r
87 connections on the standard echo port (port 7), then echos back any data\r
88 received on that connection.\r
89 */\r
90 #define mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS       1\r
91 #define mainCREATE_TCP_ECHO_TASKS_SINGLE                        0\r
92 #define mainCREATE_TCP_ECHO_SERVER_TASK                         0\r
93 /*-----------------------------------------------------------*/\r
94 \r
95 /*\r
96  * Just seeds the simple pseudo random number generator.\r
97  */\r
98 static void prvSRand( UBaseType_t ulSeed );\r
99 \r
100 /*\r
101  * Miscellaneous initialisation including preparing the logging and seeding the\r
102  * random number generator.\r
103  */\r
104 static void prvMiscInitialisation( void );\r
105 \r
106 /* The default IP and MAC address used by the demo.  The address configuration\r
107 defined here will be used if ipconfigUSE_DHCP is 0, or if ipconfigUSE_DHCP is\r
108 1 but a DHCP server could not be contacted.  See the online documentation for\r
109 more information. */\r
110 static const uint8_t ucIPAddress[ 4 ] = { configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 };\r
111 static const uint8_t ucNetMask[ 4 ] = { configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 };\r
112 static const uint8_t ucGatewayAddress[ 4 ] = { configGATEWAY_ADDR0, configGATEWAY_ADDR1, configGATEWAY_ADDR2, configGATEWAY_ADDR3 };\r
113 static const uint8_t ucDNSServerAddress[ 4 ] = { configDNS_SERVER_ADDR0, configDNS_SERVER_ADDR1, configDNS_SERVER_ADDR2, configDNS_SERVER_ADDR3 };\r
114 \r
115 /* Set the following constant to pdTRUE to log using the method indicated by the\r
116 name of the constant, or pdFALSE to not log using the method indicated by the\r
117 name of the constant.  Options include to standard out (xLogToStdout), to a disk\r
118 file (xLogToFile), and to a UDP port (xLogToUDP).  If xLogToUDP is set to pdTRUE\r
119 then UDP messages are sent to the IP address configured as the echo server\r
120 address (see the configECHO_SERVER_ADDR0 definitions in FreeRTOSConfig.h) and\r
121 the port number set by configPRINT_PORT in FreeRTOSConfig.h. */\r
122 const BaseType_t xLogToStdout = pdTRUE, xLogToFile = pdFALSE, xLogToUDP = pdFALSE;\r
123 \r
124 /* Default MAC address configuration.  The demo creates a virtual network\r
125 connection that uses this MAC address by accessing the raw Ethernet data\r
126 to and from a real network connection on the host PC.  See the\r
127 configNETWORK_INTERFACE_TO_USE definition for information on how to configure\r
128 the real network connection to use. */\r
129 const uint8_t ucMACAddress[ 6 ] = { configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 };\r
130 \r
131 /* Use by the pseudo random number generator. */\r
132 static UBaseType_t ulNextRand;\r
133 \r
134 /*-----------------------------------------------------------*/\r
135 \r
136 int main( void )\r
137 {\r
138 const uint32_t ulLongTime_ms = pdMS_TO_TICKS( 1000UL );\r
139 \r
140         /*\r
141          * Instructions for using this project are provided on:\r
142          * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html\r
143          */\r
144 \r
145         /* Miscellaneous initialisation including preparing the logging and seeding\r
146         the random number generator. */\r
147         prvMiscInitialisation();\r
148 \r
149         /* Initialise the network interface.\r
150 \r
151         ***NOTE*** Tasks that use the network are created in the network event hook\r
152         when the network is connected and ready for use (see the definition of\r
153         vApplicationIPNetworkEventHook() below).  The address values passed in here\r
154         are used if ipconfigUSE_DHCP is set to 0, or if ipconfigUSE_DHCP is set to 1\r
155         but a DHCP server cannot be     contacted. */\r
156         FreeRTOS_debug_printf( ( "FreeRTOS_IPInit\n" ) );\r
157         FreeRTOS_IPInit( ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );\r
158 \r
159         /* Start the RTOS scheduler. */\r
160         FreeRTOS_debug_printf( ("vTaskStartScheduler\n") );\r
161         vTaskStartScheduler();\r
162 \r
163         /* If all is well, the scheduler will now be running, and the following\r
164         line will never be reached.  If the following line does execute, then\r
165         there was insufficient FreeRTOS heap memory available for the idle and/or\r
166         timer tasks     to be created.  See the memory management section on the\r
167         FreeRTOS web site for more details (this is standard text that is not not\r
168         really applicable to the Win32 simulator port). */\r
169         for( ;; )\r
170         {\r
171                 Sleep( ulLongTime_ms );\r
172         }\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 void vApplicationIdleHook( void )\r
177 {\r
178 const uint32_t ulMSToSleep = 1;\r
179 \r
180         /* This is just a trivial example of an idle hook.  It is called on each\r
181         cycle of the idle task if configUSE_IDLE_HOOK is set to 1 in\r
182         FreeRTOSConfig.h.  It must *NOT* attempt to block.  In this case the\r
183         idle task just sleeps to lower the CPU usage. */\r
184         Sleep( ulMSToSleep );\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 void vAssertCalled( const char *pcFile, uint32_t ulLine )\r
189 {\r
190 const uint32_t ulLongSleep = 1000UL;\r
191 volatile uint32_t ulBlockVariable = 0UL;\r
192 volatile char *pcFileName = ( volatile char *  ) pcFile;\r
193 volatile uint32_t ulLineNumber = ulLine;\r
194 \r
195         ( void ) pcFileName;\r
196         ( void ) ulLineNumber;\r
197 \r
198         FreeRTOS_debug_printf( ( "vAssertCalled( %s, %ld\n", pcFile, ulLine ) );\r
199 \r
200         /* Setting ulBlockVariable to a non-zero value in the debugger will allow\r
201         this function to be exited. */\r
202         taskDISABLE_INTERRUPTS();\r
203         {\r
204                 while( ulBlockVariable == 0UL )\r
205                 {\r
206                         Sleep( ulLongSleep );\r
207                 }\r
208         }\r
209         taskENABLE_INTERRUPTS();\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 /* Called by FreeRTOS+TCP when the network connects or disconnects.  Disconnect\r
214 events are only received if implemented in the MAC driver. */\r
215 void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )\r
216 {\r
217 uint32_t ulIPAddress, ulNetMask, ulGatewayAddress, ulDNSServerAddress;\r
218 char cBuffer[ 16 ];\r
219 static BaseType_t xTasksAlreadyCreated = pdFALSE;\r
220 \r
221         /* If the network has just come up...*/\r
222         if( eNetworkEvent == eNetworkUp )\r
223         {\r
224                 /* Create the tasks that use the IP stack if they have not already been\r
225                 created. */\r
226                 if( xTasksAlreadyCreated == pdFALSE )\r
227                 {\r
228                         /* See the comments above the definitions of these pre-processor\r
229                         macros at the top of this file for a description of the individual\r
230                         demo tasks. */\r
231                         #if( mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS == 1 )\r
232                         {\r
233                                 vStartSimpleUDPClientServerTasks( configMINIMAL_STACK_SIZE, mainSIMPLE_UDP_CLIENT_SERVER_PORT, mainSIMPLE_UDP_CLIENT_SERVER_TASK_PRIORITY );\r
234                         }\r
235                         #endif /* mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS */\r
236 \r
237                         #if( mainCREATE_TCP_ECHO_TASKS_SINGLE == 1 )\r
238                         {\r
239                                 vStartTCPEchoClientTasks_SingleTasks( mainECHO_CLIENT_TASK_STACK_SIZE, mainECHO_CLIENT_TASK_PRIORITY );\r
240                         }\r
241                         #endif /* mainCREATE_TCP_ECHO_TASKS_SINGLE */\r
242 \r
243                         #if( mainCREATE_TCP_ECHO_SERVER_TASK == 1 )\r
244                         {\r
245                                 vStartSimpleTCPServerTasks( mainECHO_SERVER_TASK_STACK_SIZE, mainECHO_SERVER_TASK_PRIORITY );\r
246                         }\r
247                         #endif\r
248 \r
249                         xTasksAlreadyCreated = pdTRUE;\r
250                 }\r
251 \r
252                 /* Print out the network configuration, which may have come from a DHCP\r
253                 server. */\r
254                 FreeRTOS_GetAddressConfiguration( &ulIPAddress, &ulNetMask, &ulGatewayAddress, &ulDNSServerAddress );\r
255                 FreeRTOS_inet_ntoa( ulIPAddress, cBuffer );\r
256                 FreeRTOS_printf( ( "\r\n\r\nIP Address: %s\r\n", cBuffer ) );\r
257 \r
258                 FreeRTOS_inet_ntoa( ulNetMask, cBuffer );\r
259                 FreeRTOS_printf( ( "Subnet Mask: %s\r\n", cBuffer ) );\r
260 \r
261                 FreeRTOS_inet_ntoa( ulGatewayAddress, cBuffer );\r
262                 FreeRTOS_printf( ( "Gateway Address: %s\r\n", cBuffer ) );\r
263 \r
264                 FreeRTOS_inet_ntoa( ulDNSServerAddress, cBuffer );\r
265                 FreeRTOS_printf( ( "DNS Server Address: %s\r\n\r\n\r\n", cBuffer ) );\r
266         }\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r
270 void vApplicationMallocFailedHook( void )\r
271 {\r
272         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
273         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
274         internally by FreeRTOS API functions that create tasks, queues, software\r
275         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
276         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
277         vAssertCalled( __FILE__, __LINE__ );\r
278 }\r
279 /*-----------------------------------------------------------*/\r
280 \r
281 UBaseType_t uxRand( void )\r
282 {\r
283 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;\r
284 \r
285         /* Utility function to generate a pseudo random number. */\r
286 \r
287         ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;\r
288         return( ( int ) ( ulNextRand >> 16UL ) & 0x7fffUL );\r
289 }\r
290 /*-----------------------------------------------------------*/\r
291 \r
292 static void prvSRand( UBaseType_t ulSeed )\r
293 {\r
294         /* Utility function to seed the pseudo random number generator. */\r
295         ulNextRand = ulSeed;\r
296 }\r
297 /*-----------------------------------------------------------*/\r
298 \r
299 static void prvMiscInitialisation( void )\r
300 {\r
301 time_t xTimeNow;\r
302 uint32_t ulLoggingIPAddress;\r
303 \r
304         ulLoggingIPAddress = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0, configECHO_SERVER_ADDR1, configECHO_SERVER_ADDR2, configECHO_SERVER_ADDR3 );\r
305         vLoggingInit( xLogToStdout, xLogToFile, xLogToUDP, ulLoggingIPAddress, configPRINT_PORT );\r
306 \r
307         /* Seed the random number generator. */\r
308         time( &xTimeNow );\r
309         FreeRTOS_debug_printf( ( "Seed for randomiser: %lu\n", xTimeNow ) );\r
310         prvSRand( ( uint32_t ) xTimeNow );\r
311         FreeRTOS_debug_printf( ( "Random numbers: %08X %08X %08X %08X\n", ipconfigRAND32(), ipconfigRAND32(), ipconfigRAND32(), ipconfigRAND32() ) );\r
312 }\r
313 /*-----------------------------------------------------------*/\r
314 \r
315 #if( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 ) || ( ipconfigDHCP_REGISTER_HOSTNAME == 1 )\r
316 \r
317         const char *pcApplicationHostnameHook( void )\r
318         {\r
319                 /* Assign the name "FreeRTOS" to this network node.  This function will\r
320                 be called during the DHCP: the machine will be registered with an IP\r
321                 address plus this name. */\r
322                 return mainHOST_NAME;\r
323         }\r
324 \r
325 #endif\r
326 /*-----------------------------------------------------------*/\r
327 \r
328 #if( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 )\r
329 \r
330         BaseType_t xApplicationDNSQueryHook( const char *pcName )\r
331         {\r
332         BaseType_t xReturn;\r
333 \r
334                 /* Determine if a name lookup is for this node.  Two names are given\r
335                 to this node: that returned by pcApplicationHostnameHook() and that set\r
336                 by mainDEVICE_NICK_NAME. */\r
337                 if( _stricmp( pcName, pcApplicationHostnameHook() ) == 0 )\r
338                 {\r
339                         xReturn = pdPASS;\r
340                 }\r
341                 else if( _stricmp( pcName, mainDEVICE_NICK_NAME ) == 0 )\r
342                 {\r
343                         xReturn = pdPASS;\r
344                 }\r
345                 else\r
346                 {\r
347                         xReturn = pdFAIL;\r
348                 }\r
349 \r
350                 return xReturn;\r
351         }\r
352 \r
353 #endif\r
354 \r
355 /*\r
356  * Callback that provides the inputs necessary to generate a randomized TCP\r
357  * Initial Sequence Number per RFC 6528.  THIS IS ONLY A DUMMY IMPLEMENTATION\r
358  * THAT RETURNS A PSEUDO RANDOM NUMBER SO IS NOT INTENDED FOR USE IN PRODUCTION\r
359  * SYSTEMS.\r
360  */\r
361 extern uint32_t ulApplicationGetNextSequenceNumber( uint32_t ulSourceAddress,\r
362                                                                                                         uint16_t usSourcePort,\r
363                                                                                                         uint32_t ulDestinationAddress,\r
364                                                                                                         uint16_t usDestinationPort )\r
365 {\r
366         ( void ) ulSourceAddress;\r
367         ( void ) usSourcePort;\r
368         ( void ) ulDestinationAddress;\r
369         ( void ) usDestinationPort;\r
370 \r
371         return uxRand();\r
372 }\r
373 \r
374 /*\r
375  * Supply a random number to FreeRTOS+TCP stack. \r
376  * THIS IS ONLY A DUMMY IMPLEMENTATION THAT RETURNS A PSEUDO RANDOM NUMBER \r
377  * SO IS NOT INTENDED FOR USE IN PRODUCTION SYSTEMS.\r
378  */\r
379 BaseType_t xApplicationGetRandomNumber(uint32_t* pulNumber)\r
380 {\r
381         *(pulNumber) = uxRand();\r
382         return pdTRUE;\r
383 }\r
384 \r