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