]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Demo/FreeRTOS_Plus_TCP_and_FAT_Windows_Simulator/main.c
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Demo / FreeRTOS_Plus_TCP_and_FAT_Windows_Simulator / main.c
1 /*\r
2     FreeRTOS V9.0.0 - 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  * Instructions for using this project are provided on:\r
72  * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html\r
73  *\r
74  * NOTE: Some versions of Visual Studio will generate erroneous compiler\r
75  * warnings about variables being used before they are set.\r
76  */\r
77 \r
78 /* Standard includes. */\r
79 #include <stdio.h>\r
80 #include <time.h>\r
81 \r
82 /* FreeRTOS includes. */\r
83 #include <FreeRTOS.h>\r
84 #include "task.h"\r
85 #include "timers.h"\r
86 #include "queue.h"\r
87 #include "semphr.h"\r
88 \r
89 /* FreeRTOS+TCP includes. */\r
90 #include "FreeRTOS_IP.h"\r
91 #include "FreeRTOS_Sockets.h"\r
92 #include "FreeRTOS_TCP_server.h"\r
93 #include "FreeRTOS_DHCP.h"\r
94 \r
95 /* FreeRTOS+FAT includes. */\r
96 #include "ff_headers.h"\r
97 #include "ff_stdio.h"\r
98 #include "ff_ramdisk.h"\r
99 \r
100 /* Demo application includes. */\r
101 #include "SimpleUDPClientAndServer.h"\r
102 #include "TwoUDPEchoClients.h"\r
103 #include "TCPEchoClient_SingleTasks.h"\r
104 #include "TCPEchoClient_SeparateTasks.h"\r
105 #include "UDPCommandConsole.h"\r
106 #include "TCPCommandConsole.h"\r
107 #include "UDPSelectServer.h"\r
108 #include "SimpleTCPEchoServer.h"\r
109 #include "TFTPServer.h"\r
110 #include "demo_logging.h"\r
111 \r
112 /* UDP command server task parameters. */\r
113 #define mainUDP_CLI_TASK_PRIORITY                                               ( tskIDLE_PRIORITY )\r
114 #define mainUDP_CLI_PORT_NUMBER                                                 ( 5001UL )\r
115 \r
116 /* TCP command server task parameters.  The standard telnet port is used even\r
117 though this is not implementing a real telnet server. */\r
118 #define mainTCP_CLI_TASK_PRIORITY                                               ( tskIDLE_PRIORITY )\r
119 #define mainTCP_CLI_PORT_NUMBER                                                 ( 23UL )\r
120 \r
121 /* Simple UDP client and server task parameters. */\r
122 #define mainSIMPLE_UDP_CLIENT_SERVER_TASK_PRIORITY              ( tskIDLE_PRIORITY )\r
123 #define mainSIMPLE_UDP_CLIENT_SERVER_PORT                               ( 5005UL )\r
124 \r
125 /* Select UDP server task parameters. */\r
126 #define mainUDP_SELECT_SERVER_TASK_PRIORITY                             ( tskIDLE_PRIORITY )\r
127 #define mainUDP_SELECT_SERVER_PORT                                              ( 30001UL )\r
128 \r
129 /* Echo client task parameters - used for both TCP and UDP echo clients. */\r
130 #define mainECHO_CLIENT_TASK_STACK_SIZE                                 ( configMINIMAL_STACK_SIZE * 2 )\r
131 #define mainECHO_CLIENT_TASK_PRIORITY                                   ( tskIDLE_PRIORITY + 1 )\r
132 \r
133 /* FTP and HTTP servers execute in the TCP server work task. */\r
134 #define mainTCP_SERVER_TASK_PRIORITY                                    ( tskIDLE_PRIORITY + 2 )\r
135 #define mainTCP_SERVER_STACK_SIZE                                               1400 /* Not used in the Win32 simulator. */\r
136 \r
137 /* TFTP server parameters. */\r
138 #define mainTFTP_SERVER_PRIORITY                                                ( tskIDLE_PRIORITY + 1 )\r
139 #define mainTFTP_SERVER_STACK_SIZE                                              1400 /* Not used in the Win32 simulator. */\r
140 \r
141 /* Dimensions the buffer used to send UDP print and debug messages. */\r
142 #define cmdPRINTF_BUFFER_SIZE           512\r
143 \r
144 /* The number and size of sectors that will make up the RAM disk.  The RAM disk\r
145 is huge to allow some verbose FTP tests. */\r
146 #define mainRAM_DISK_SECTOR_SIZE        512UL /* Currently fixed! */\r
147 #define mainRAM_DISK_SECTORS            ( ( 5UL * 1024UL * 1024UL ) / mainRAM_DISK_SECTOR_SIZE ) /* 5M bytes. */\r
148 #define mainIO_MANAGER_CACHE_SIZE       ( 15UL * mainRAM_DISK_SECTOR_SIZE )\r
149 \r
150 /* Where the RAM disk is mounted. */\r
151 #define mainRAM_DISK_NAME                       "/ram"\r
152 \r
153 /* Define a name that will be used for LLMNR and NBNS searches. */\r
154 #define mainHOST_NAME                           "RTOSDemo"\r
155 #define mainDEVICE_NICK_NAME            "windows_demo"\r
156 \r
157 /* Set to 0 to run the STDIO examples once only, or 1 to create multiple tasks\r
158 that run the tests continuously. */\r
159 #define mainRUN_STDIO_TESTS_IN_MULTIPLE_TASK 0\r
160 \r
161 /* Set the following constants to 1 or 0 to define which tasks to include and\r
162 exclude:\r
163 \r
164 mainCREATE_FTP_SERVER:  When set to 1 the TCP server task will include an FTP\r
165 server.\r
166 See http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/FTP_Server.html\r
167 \r
168 mainCREATE_HTTP_SERVER:  When set to 1 the TCP server task will include a basic\r
169 HTTP server.\r
170 See http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/HTTP_web_Server.html\r
171 \r
172 mainCREATE_UDP_CLI_TASKS:  When set to 1 a command console that uses a UDP port\r
173 for input and output is created using FreeRTOS+CLI.  The port number used is set\r
174 by the mainUDP_CLI_PORT_NUMBER constant above.  A dumb UDP terminal such as YAT\r
175 can be used to connect.\r
176 See http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/UDP_CLI.html\r
177 \r
178 mainCREATE_TCP_CLI_TASKS:  When set to 1 a command console that uses a TCP port\r
179 for input and output is created using FreeRTOS+CLI.  The port number used is set\r
180 by the mainTCP_CLI_PORT_NUMBER constant above.  A dumb UDP terminal such as YAT\r
181 can be used to connect.\r
182 \r
183 mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS:  When set to 1 two UDP client tasks\r
184 and two UDP server tasks are created.  The clients talk to the servers.  One set\r
185 of tasks use the standard sockets interface, and the other the zero copy sockets\r
186 interface.  These tasks are self checking and will trigger a configASSERT() if\r
187 they detect a difference in the data that is received from that which was sent.\r
188 As these tasks use UDP, and can therefore loose packets, they will cause\r
189 configASSERT() to be called when they are run in a less than perfect networking\r
190 environment.\r
191 See http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/UDP_client_server.html\r
192 \r
193 mainCREATE_SELECT_UDP_SERVER_TASKS: Uses two tasks to demonstrate the use of the\r
194 FreeRTOS_select() function.\r
195 \r
196 mainCREATE_UDP_ECHO_TASKS:  When set to 1 a two tasks are created that send\r
197 UDP echo requests to the standard echo port (port 7).  One task uses the\r
198 standard socket interface, the other the zero copy socket interface.  The IP\r
199 address of the echo server must be configured using the configECHO_SERVER_ADDR0\r
200 to configECHO_SERVER_ADDR3 constants in FreeRTOSConfig.h.  These tasks are self\r
201 checking and will trigger a configASSERT() if the received echo reply does not\r
202 match the transmitted echo request.  As these tasks use UDP, and can therefore\r
203 loose packets, they will cause configASSERT() to be called when they are run in\r
204 a less than perfect networking environment, or when connected to an echo server\r
205 that (legitimately as UDP is used) opts not to reply to every echo request.\r
206 See http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/UDP_Echo_Clients.html\r
207 \r
208 mainCREATE_TCP_ECHO_TASKS_SINGLE:  When set to 1 a set of tasks are created that\r
209 send TCP echo requests to the standard echo port (port 7), then wait for and\r
210 verify the echo reply, from within the same task (Tx and Rx are performed in the\r
211 same RTOS task).  The IP address of the echo server must be configured using the\r
212 configECHO_SERVER_ADDR0 to configECHO_SERVER_ADDR3 constants in\r
213 FreeRTOSConfig.h.\r
214 See http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/TCP_Echo_Clients.html\r
215 \r
216 mainCREATE_TCP_ECHO_TASKS_SEPARATE:  As per the description for the\r
217 mainCREATE_TCP_ECHO_TASKS_SINGLE constant above, except this time separate tasks\r
218 are used to send data to and receive data from the echo server (one task is used\r
219 for Tx and another task for Rx, using the same socket).\r
220 See http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/TCP_Echo_Clients_Separate.html\r
221 \r
222 mainCREATE_SIMPLE_TCP_ECHO_SERVER:  When set to 1 FreeRTOS tasks are used with\r
223 FreeRTOS+TCP to create a TCP echo server.  Echo clients are also created, but\r
224 the echo clients use Windows threads (as opposed to FreeRTOS tasks) and use the\r
225 Windows TCP library (Winsocks).  This creates a communication between the\r
226 FreeRTOS+TCP TCP/IP stack and the Windows TCP/IP stack.\r
227 See http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/TCP_Echo_Server.html\r
228 */\r
229 #define mainCREATE_UDP_CLI_TASKS                                        1\r
230 #define mainCREATE_TCP_CLI_TASKS                                        1\r
231 #define mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS       1\r
232 #define mainCREATE_SELECT_UDP_SERVER_TASKS                      0 /* _RB_ Requires retest. */\r
233 #define mainCREATE_UDP_ECHO_TASKS                                       1\r
234 #define mainCREATE_TCP_ECHO_TASKS_SINGLE                        1\r
235 #define mainCREATE_TCP_ECHO_TASKS_SEPARATE                      1\r
236 #define mainCREATE_SIMPLE_TCP_ECHO_SERVER                       1\r
237 #define mainCREATE_FTP_SERVER                                           1\r
238 #define mainCREATE_HTTP_SERVER                                          1\r
239 #define mainCREATE_TFTP_SERVER                                          1\r
240 \r
241 /* Set the following constant to pdTRUE to log using the method indicated by the\r
242 name of the constant, or pdFALSE to not log using the method indicated by the\r
243 name of the constant.  Options include to standard out (mainLOG_TO_STDOUT), to a\r
244 disk file (mainLOG_TO_DISK_FILE), and to a UDP port (mainLOG_TO_UDP).  If\r
245 mainLOG_TO_UDP is set to pdTRUE then UDP messages are sent to the IP address\r
246 configured as the echo server address (see the configECHO_SERVER_ADDR0\r
247 definitions in FreeRTOSConfig.h) and the port number set by configPRINT_PORT in\r
248 FreeRTOSConfig.h. */\r
249 #define mainLOG_TO_STDOUT               pdTRUE\r
250 #define mainLOG_TO_DISK_FILE    pdFALSE\r
251 #define mainLOG_TO_UDP                  pdFALSE\r
252 \r
253 /*-----------------------------------------------------------*/\r
254 \r
255 /*\r
256  * Register commands that can be used with FreeRTOS+CLI through the UDP socket.\r
257  * The commands are defined in CLI-commands.c and File-related-CLI-commands.c\r
258  * respectively.\r
259  */\r
260 extern void vRegisterCLICommands( void );\r
261 extern void vRegisterFileSystemCLICommands( void );\r
262 \r
263 /*\r
264  * A software timer is created that periodically checks that some of the TCP/IP\r
265  * demo tasks are still functioning as expected.  This is the timer's callback\r
266  * function.\r
267  */\r
268 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
269 \r
270 /*\r
271  * Just seeds the simple pseudo random number generator.\r
272  */\r
273 static void prvSRand( UBaseType_t ulSeed );\r
274 \r
275 /*\r
276  * Miscellaneous initialisation including preparing the logging and seeding the\r
277  * random number generator.\r
278  */\r
279 static void prvMiscInitialisation( void );\r
280 \r
281 /*\r
282  * Creates a RAM disk, then creates files on the RAM disk.  The files can then\r
283  * be viewed via the FTP server and the command line interface.\r
284  */\r
285 static void prvCreateDiskAndExampleFiles( void );\r
286 \r
287 /*\r
288  * Functions used to create and then test files on a disk.\r
289  */\r
290 extern void vCreateAndVerifyExampleFiles( const char *pcMountPath );\r
291 extern void vStdioWithCWDTest( const char *pcMountPath );\r
292 extern void vMultiTaskStdioWithCWDTest( const char *const pcMountPath, uint16_t usStackSizeWords );\r
293 \r
294 /*\r
295  * The task that runs the FTP and HTTP servers.\r
296  */\r
297 #if( ( mainCREATE_FTP_SERVER == 1 ) || ( mainCREATE_HTTP_SERVER == 1 ) )\r
298         static void prvServerWorkTask( void *pvParameters );\r
299 #endif\r
300 \r
301 \r
302 /* The default IP and MAC address used by the demo.  The address configuration\r
303 defined here will be used if ipconfigUSE_DHCP is 0, or if ipconfigUSE_DHCP is\r
304 1 but a DHCP server could not be contacted.  See the online documentation for\r
305 more information. */\r
306 static const uint8_t ucIPAddress[ 4 ] = { configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 };\r
307 static const uint8_t ucNetMask[ 4 ] = { configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 };\r
308 static const uint8_t ucGatewayAddress[ 4 ] = { configGATEWAY_ADDR0, configGATEWAY_ADDR1, configGATEWAY_ADDR2, configGATEWAY_ADDR3 };\r
309 static const uint8_t ucDNSServerAddress[ 4 ] = { configDNS_SERVER_ADDR0, configDNS_SERVER_ADDR1, configDNS_SERVER_ADDR2, configDNS_SERVER_ADDR3 };\r
310 \r
311 /* Default MAC address configuration.  The demo creates a virtual network\r
312 connection that uses this MAC address by accessing the raw Ethernet data\r
313 to and from a real network connection on the host PC.  See the\r
314 configNETWORK_INTERFACE_TO_USE definition for information on how to configure\r
315 the real network connection to use. */\r
316 const uint8_t ucMACAddress[ 6 ] = { configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 };\r
317 \r
318 /* The UDP address to which print messages are sent. */\r
319 static struct freertos_sockaddr xPrintUDPAddress;\r
320 \r
321 /* Use by the pseudo random number generator. */\r
322 static UBaseType_t ulNextRand;\r
323 \r
324 /* Handle of the task that runs the FTP and HTTP servers. */\r
325 static TaskHandle_t xServerWorkTaskHandle = NULL;\r
326 \r
327 /*-----------------------------------------------------------*/\r
328 \r
329 /*\r
330  * NOTE: Some versions of Visual Studio will generate erroneous compiler\r
331  * warnings about variables being used before they are set.\r
332  */\r
333 int main( void )\r
334 {\r
335 const uint32_t ulLongTime_ms = 250UL, ulCheckTimerPeriod_ms = 15000UL;\r
336 TimerHandle_t xCheckTimer;\r
337 \r
338         /*\r
339          * Instructions for using this project are provided on:\r
340          * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html\r
341          *\r
342          * NOTE: Some versions of Visual Studio will generate erroneous compiler\r
343          * warnings about variables being used before they are set.\r
344          */\r
345 \r
346         /* Miscellaneous initialisation including preparing the logging and seeding\r
347         the random number generator. */\r
348         prvMiscInitialisation();\r
349 \r
350         /* Initialise the network interface.\r
351 \r
352         ***NOTE*** Tasks that use the network are created in the network event hook\r
353         when the network is connected and ready for use (see the definition of\r
354         vApplicationIPNetworkEventHook() below).  The address values passed in here\r
355         are used if ipconfigUSE_DHCP is set to 0, or if ipconfigUSE_DHCP is set to 1\r
356         but a DHCP server cannot be     contacted. */\r
357         FreeRTOS_debug_printf( ( "FreeRTOS_IPInit\n" ) );\r
358         FreeRTOS_IPInit( ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );\r
359 \r
360         /* A timer is used to periodically check the example tasks are functioning\r
361         as expected.  First create the software timer ... */\r
362         xCheckTimer = xTimerCreate( "Check",                            /* Text name used for debugging only. */\r
363                                                                 pdMS_TO_TICKS( ulCheckTimerPeriod_ms ),\r
364                                                                 pdTRUE,                                 /* This is an auto-reload timer. */\r
365                                                                 NULL,                                   /* Parameter not used. */\r
366                                                                 prvCheckTimerCallback ); /* The timer callback function. */\r
367 \r
368         /* ... assert if the timer was not created, ... */\r
369         configASSERT( xCheckTimer );\r
370 \r
371         /* ... then start the timer. */\r
372         xTimerStart( xCheckTimer, 0 );\r
373 \r
374         #if( ( mainCREATE_FTP_SERVER == 1 ) || ( mainCREATE_HTTP_SERVER == 1 ) )\r
375         {\r
376                 /* Create the task that handles the FTP and HTTP servers.  This will\r
377                 initialise the file system then wait for a notification from the network\r
378                 event hook before creating the servers.  The task is created at the idle\r
379                 priority, and sets itself to mainTCP_SERVER_TASK_PRIORITY after the file\r
380                 system has initialised. */\r
381                 xTaskCreate( prvServerWorkTask, "SvrWork", mainTCP_SERVER_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xServerWorkTaskHandle );\r
382         }\r
383         #endif\r
384 \r
385         /* Start the RTOS scheduler. */\r
386         FreeRTOS_debug_printf( ("vTaskStartScheduler\n") );\r
387         vTaskStartScheduler();\r
388 \r
389         /* If all is well, the scheduler will now be running, and the following\r
390         line will never be reached.  If the following line does execute, then\r
391         there was insufficient FreeRTOS heap memory available for the idle and/or\r
392         timer tasks     to be created.  See the memory management section on the\r
393         FreeRTOS web site for more details (this is standard text that is not not\r
394         really applicable to the Win32 simulator port). */\r
395         for( ;; )\r
396         {\r
397                 Sleep( ulLongTime_ms );\r
398         }\r
399 }\r
400 /*-----------------------------------------------------------*/\r
401 \r
402 static void prvCreateDiskAndExampleFiles( void )\r
403 {\r
404 static uint8_t ucRAMDisk[ mainRAM_DISK_SECTORS * mainRAM_DISK_SECTOR_SIZE ];\r
405 FF_Disk_t *pxDisk;\r
406 \r
407         /* Create the RAM disk. */\r
408         pxDisk = FF_RAMDiskInit( mainRAM_DISK_NAME, ucRAMDisk, mainRAM_DISK_SECTORS, mainIO_MANAGER_CACHE_SIZE );\r
409         configASSERT( pxDisk );\r
410 \r
411         /* Print out information on the disk. */\r
412         FF_RAMDiskShowPartition( pxDisk );\r
413 \r
414         /* Create a few example files on the disk.  These are not deleted again. */\r
415         vCreateAndVerifyExampleFiles( mainRAM_DISK_NAME );\r
416 \r
417         /* A few sanity checks only - can only be called after\r
418         vCreateAndVerifyExampleFiles(). */\r
419         #if( mainRUN_STDIO_TESTS_IN_MULTIPLE_TASK == 1 )\r
420         {\r
421                 /* Note the stack size is not actually used in the Windows port. */\r
422                 vMultiTaskStdioWithCWDTest( mainRAM_DISK_NAME, configMINIMAL_STACK_SIZE * 2U );\r
423         }\r
424         #else\r
425         {\r
426                 vStdioWithCWDTest( mainRAM_DISK_NAME );\r
427         }\r
428         #endif\r
429 }\r
430 /*-----------------------------------------------------------*/\r
431 \r
432 #if( ( mainCREATE_FTP_SERVER == 1 ) || ( mainCREATE_HTTP_SERVER == 1 ) )\r
433 \r
434         static void prvServerWorkTask( void *pvParameters )\r
435         {\r
436         TCPServer_t *pxTCPServer = NULL;\r
437         const TickType_t xInitialBlockTime = pdMS_TO_TICKS( 200UL );\r
438 \r
439         /* A structure that defines the servers to be created.  Which servers are\r
440         included in the structure depends on the mainCREATE_HTTP_SERVER and\r
441         mainCREATE_FTP_SERVER settings at the top of this file. */\r
442         static const struct xSERVER_CONFIG xServerConfiguration[] =\r
443         {\r
444                 #if( mainCREATE_HTTP_SERVER == 1 )\r
445                                 /* Server type,         port number,    backlog,        root dir. */\r
446                                 { eSERVER_HTTP,         80,                     12,             configHTTP_ROOT },\r
447                 #endif\r
448 \r
449                 #if( mainCREATE_FTP_SERVER == 1 )\r
450                                 /* Server type,         port number,    backlog,        root dir. */\r
451                                 { eSERVER_FTP,          21,                     12,             "" }\r
452                 #endif\r
453         };\r
454 \r
455                 /* Remove compiler warning about unused parameter. */\r
456                 ( void ) pvParameters;\r
457 \r
458                 /* Create the RAM disk used by the FTP and HTTP servers. */\r
459                 prvCreateDiskAndExampleFiles();\r
460 \r
461                 /* The priority of this task can be raised now the disk has been\r
462                 initialised. */\r
463                 vTaskPrioritySet( NULL, mainTCP_SERVER_TASK_PRIORITY );\r
464 \r
465                 /* If the CLI is included in the build then register commands that allow\r
466                 the file system to be accessed. */\r
467                 #if( ( mainCREATE_UDP_CLI_TASKS == 1 ) || ( mainCREATE_TCP_CLI_TASKS == 1 ) )\r
468                 {\r
469                         vRegisterFileSystemCLICommands();\r
470                 }\r
471                 #endif /* mainCREATE_UDP_CLI_TASKS */\r
472 \r
473 \r
474                 /* Wait until the network is up before creating the servers.  The\r
475                 notification is given from the network event hook. */\r
476                 ulTaskNotifyTake( pdTRUE, portMAX_DELAY );\r
477 \r
478                 /* Create the servers defined by the xServerConfiguration array above. */\r
479                 pxTCPServer = FreeRTOS_CreateTCPServer( xServerConfiguration, sizeof( xServerConfiguration ) / sizeof( xServerConfiguration[ 0 ] ) );\r
480                 configASSERT( pxTCPServer );\r
481 \r
482                 for( ;; )\r
483                 {\r
484                         FreeRTOS_TCPServerWork( pxTCPServer, xInitialBlockTime );\r
485                 }\r
486         }\r
487 \r
488 #endif /* ( ( mainCREATE_FTP_SERVER == 1 ) || ( mainCREATE_HTTP_SERVER == 1 ) ) */\r
489 /*-----------------------------------------------------------*/\r
490 \r
491 void vApplicationIdleHook( void )\r
492 {\r
493 const uint32_t ulMSToSleep = 1;\r
494 \r
495         /* This is just a trivial example of an idle hook.  It is called on each\r
496         cycle of the idle task if configUSE_IDLE_HOOK is set to 1 in\r
497         FreeRTOSConfig.h.  It must *NOT* attempt to block.  In this case the\r
498         idle task just sleeps to lower the CPU usage. */\r
499         Sleep( ulMSToSleep );\r
500 }\r
501 /*-----------------------------------------------------------*/\r
502 \r
503 void vAssertCalled( const char *pcFile, uint32_t ulLine )\r
504 {\r
505 const uint32_t ulLongSleep = 1000UL;\r
506 volatile uint32_t ulBlockVariable = 0UL;\r
507 volatile char *pcFileName = ( volatile char *  ) pcFile;\r
508 volatile uint32_t ulLineNumber = ulLine;\r
509 \r
510         ( void ) pcFileName;\r
511         ( void ) ulLineNumber;\r
512 \r
513         FreeRTOS_printf( ( "vAssertCalled( %s, %ld\n", pcFile, ulLine ) );\r
514 \r
515         /* Setting ulBlockVariable to a non-zero value in the debugger will allow\r
516         this function to be exited. */\r
517         taskDISABLE_INTERRUPTS();\r
518         {\r
519                 while( ulBlockVariable == 0UL )\r
520                 {\r
521                         Sleep( ulLongSleep );\r
522                 }\r
523         }\r
524         taskENABLE_INTERRUPTS();\r
525 }\r
526 /*-----------------------------------------------------------*/\r
527 \r
528 /* Called by FreeRTOS+TCP when the network connects or disconnects.  Disconnect\r
529 events are only received if implemented in the MAC driver. */\r
530 void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )\r
531 {\r
532 uint32_t ulIPAddress, ulNetMask, ulGatewayAddress, ulDNSServerAddress;\r
533 char cBuffer[ 16 ];\r
534 static BaseType_t xTasksAlreadyCreated = pdFALSE;\r
535 \r
536         /* If the network has just come up...*/\r
537         if( eNetworkEvent == eNetworkUp )\r
538         {\r
539                 /* Create the tasks that use the IP stack if they have not already been\r
540                 created. */\r
541                 if( xTasksAlreadyCreated == pdFALSE )\r
542                 {\r
543                         /* See the comments above the definitions of these pre-processor\r
544                         macros at the top of this file for a description of the individual\r
545                         demo tasks. */\r
546                         #if( mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS == 1 )\r
547                         {\r
548                                 vStartSimpleUDPClientServerTasks( configMINIMAL_STACK_SIZE, mainSIMPLE_UDP_CLIENT_SERVER_PORT, mainSIMPLE_UDP_CLIENT_SERVER_TASK_PRIORITY );\r
549                         }\r
550                         #endif /* mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS */\r
551 \r
552                         #if( mainCREATE_SELECT_UDP_SERVER_TASKS == 1 )\r
553                         {\r
554                                 vStartUDPSelectServerTasks( configMINIMAL_STACK_SIZE, mainUDP_SELECT_SERVER_PORT, mainUDP_SELECT_SERVER_TASK_PRIORITY );\r
555                         }\r
556                         #endif /* mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS */\r
557 \r
558                         #if( mainCREATE_UDP_ECHO_TASKS == 1 )\r
559                         {\r
560                                 vStartUDPEchoClientTasks( mainECHO_CLIENT_TASK_STACK_SIZE, mainECHO_CLIENT_TASK_PRIORITY );\r
561                         }\r
562                         #endif /* mainCREATE_UDP_ECHO_TASKS */\r
563 \r
564                         #if( mainCREATE_TCP_ECHO_TASKS_SINGLE == 1 )\r
565                         {\r
566                                 vStartTCPEchoClientTasks_SingleTasks( mainECHO_CLIENT_TASK_STACK_SIZE, mainECHO_CLIENT_TASK_PRIORITY );\r
567                         }\r
568                         #endif /* mainCREATE_TCP_ECHO_TASKS_SINGLE */\r
569 \r
570                         #if( mainCREATE_TCP_ECHO_TASKS_SEPARATE == 1 )\r
571                         {\r
572                                 vStartTCPEchoClientTasks_SeparateTasks( mainECHO_CLIENT_TASK_STACK_SIZE, mainECHO_CLIENT_TASK_PRIORITY );\r
573                         }\r
574                         #endif /* mainCREATE_TCP_ECHO_TASKS_SEPARATE */\r
575 \r
576                         #if( mainCREATE_TFTP_SERVER == 1 )\r
577                         {\r
578                                 vStartTFTPServerTask( mainTFTP_SERVER_STACK_SIZE, mainTFTP_SERVER_PRIORITY );\r
579                         }\r
580                         #endif /* mainCREATE_TFTP_SERVER */\r
581 \r
582                         #if( mainCREATE_UDP_CLI_TASKS == 1 )\r
583                         {\r
584                                 /* Register example commands with the FreeRTOS+CLI command\r
585                                 interpreter via the UDP port specified by the\r
586                                 mainUDP_CLI_PORT_NUMBER constant. */\r
587                                 vRegisterCLICommands();\r
588                                 vStartUDPCommandInterpreterTask( configMINIMAL_STACK_SIZE, mainUDP_CLI_PORT_NUMBER, mainUDP_CLI_TASK_PRIORITY );\r
589                         }\r
590                         #endif /* mainCREATE_UDP_CLI_TASKS */\r
591 \r
592                         #if( mainCREATE_TCP_CLI_TASKS == 1 )\r
593                         {\r
594                                 /* Register example commands with the FreeRTOS+CLI command\r
595                                 interpreter via the TCP port specified by the\r
596                                 mainTCP_CLI_PORT_NUMBER constant. */\r
597                                 vRegisterCLICommands();\r
598                                 vStartTCPCommandInterpreterTask( configMINIMAL_STACK_SIZE, mainTCP_CLI_PORT_NUMBER, mainTCP_CLI_TASK_PRIORITY );\r
599                         }\r
600                         #endif /* mainCREATE_TCPP_CLI_TASKS */\r
601 \r
602                         #if( mainCREATE_SIMPLE_TCP_ECHO_SERVER == 1 )\r
603                         {\r
604                                 /* TCP server on port 5001, using multiple threads */\r
605                                 vStartSimpleTCPServerTasks( configMINIMAL_STACK_SIZE, mainSIMPLE_UDP_CLIENT_SERVER_TASK_PRIORITY );\r
606                         }\r
607                         #endif /* mainCREATE_SIMPLE_TCP_ECHO_SERVER */\r
608 \r
609                         #if( ( mainCREATE_FTP_SERVER == 1 ) || ( mainCREATE_HTTP_SERVER == 1 ) )\r
610                         {\r
611                                 /* See TBD.\r
612                                 Let the server work task now it can now create the servers. */\r
613                                 xTaskNotifyGive( xServerWorkTaskHandle );\r
614                         }\r
615                         #endif\r
616 \r
617                         xTasksAlreadyCreated = pdTRUE;\r
618                 }\r
619 \r
620                 /* Print out the network configuration, which may have come from a DHCP\r
621                 server. */\r
622                 FreeRTOS_GetAddressConfiguration( &ulIPAddress, &ulNetMask, &ulGatewayAddress, &ulDNSServerAddress );\r
623                 FreeRTOS_inet_ntoa( ulIPAddress, cBuffer );\r
624                 FreeRTOS_printf( ( "\r\n\r\nIP Address: %s\r\n", cBuffer ) );\r
625 \r
626                 FreeRTOS_inet_ntoa( ulNetMask, cBuffer );\r
627                 FreeRTOS_printf( ( "Subnet Mask: %s\r\n", cBuffer ) );\r
628 \r
629                 FreeRTOS_inet_ntoa( ulGatewayAddress, cBuffer );\r
630                 FreeRTOS_printf( ( "Gateway Address: %s\r\n", cBuffer ) );\r
631 \r
632                 FreeRTOS_inet_ntoa( ulDNSServerAddress, cBuffer );\r
633                 FreeRTOS_printf( ( "DNS Server Address: %s\r\n\r\n\r\n", cBuffer ) );\r
634         }\r
635 }\r
636 /*-----------------------------------------------------------*/\r
637 \r
638 /* Called automatically when a reply to an outgoing ping is received. */\r
639 void vApplicationPingReplyHook( ePingReplyStatus_t eStatus, uint16_t usIdentifier )\r
640 {\r
641 static const char *pcSuccess = "Ping reply received - ";\r
642 static const char *pcInvalidChecksum = "Ping reply received with invalid checksum - ";\r
643 static const char *pcInvalidData = "Ping reply received with invalid data - ";\r
644 \r
645         switch( eStatus )\r
646         {\r
647                 case eSuccess   :\r
648                         FreeRTOS_printf( ( pcSuccess ) );\r
649                         break;\r
650 \r
651                 case eInvalidChecksum :\r
652                         FreeRTOS_printf( ( pcInvalidChecksum ) );\r
653                         break;\r
654 \r
655                 case eInvalidData :\r
656                         FreeRTOS_printf( ( pcInvalidData ) );\r
657                         break;\r
658 \r
659                 default :\r
660                         /* It is not possible to get here as all enums have their own\r
661                         case. */\r
662                         break;\r
663         }\r
664 \r
665         FreeRTOS_printf( ( "identifier %d\r\n", ( int ) usIdentifier ) );\r
666 \r
667         /* Prevent compiler warnings in case FreeRTOS_debug_printf() is not defined. */\r
668         ( void ) usIdentifier;\r
669 }\r
670 /*-----------------------------------------------------------*/\r
671 \r
672 void vApplicationMallocFailedHook( void )\r
673 {\r
674         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
675         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
676         internally by FreeRTOS API functions that create tasks, queues, software\r
677         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
678         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
679         vAssertCalled( __FILE__, __LINE__ );\r
680 }\r
681 /*-----------------------------------------------------------*/\r
682 \r
683 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
684 {\r
685 static volatile uint32_t ulEchoClientErrors_Single = 0, ulEchoClientErrors_Separate = 0, ulEchoServerErrors = 0, ulUDPEchoClientErrors = 0, ulUDPSelectServerErrors = 0;\r
686 \r
687         ( void ) xTimer;\r
688 \r
689         /* Not all the demo tasks contain a check function yet - although an\r
690         assert() will be triggered if a task fails. */\r
691 \r
692         #if( mainCREATE_TCP_ECHO_TASKS_SINGLE == 1 )\r
693         {\r
694                 if( xAreSingleTaskTCPEchoClientsStillRunning() != pdPASS )\r
695                 {\r
696                         ulEchoClientErrors_Single++;\r
697                 }\r
698         }\r
699         #endif\r
700 \r
701         #if( mainCREATE_TCP_ECHO_TASKS_SEPARATE == 1 )\r
702         {\r
703                 if( xAreSeparateTaskTCPEchoClientsStillRunning() != pdPASS )\r
704                 {\r
705                         ulEchoClientErrors_Separate++;\r
706                 }\r
707         }\r
708         #endif\r
709 \r
710         #if( mainCREATE_SIMPLE_TCP_ECHO_SERVER == 1 )\r
711         {\r
712                 if( xAreTCPEchoServersStillRunning() != pdPASS )\r
713                 {\r
714                         ulEchoServerErrors++;\r
715                 }\r
716         }\r
717         #endif\r
718 \r
719         #if( mainCREATE_UDP_ECHO_TASKS == 1 )\r
720         {\r
721                 if( xAreUDPEchoClientsStillRunning() != pdPASS )\r
722                 {\r
723                         ulUDPEchoClientErrors++;\r
724                 }\r
725         }\r
726         #endif\r
727 \r
728         #if( mainCREATE_SELECT_UDP_SERVER_TASKS == 1 )\r
729         {\r
730                 if( xAreUDPSelectTasksStillRunning() != pdPASS )\r
731                 {\r
732                         ulUDPSelectServerErrors++;\r
733                 }\r
734         }\r
735         #endif\r
736 }\r
737 /*-----------------------------------------------------------*/\r
738 \r
739 UBaseType_t uxRand( void )\r
740 {\r
741 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;\r
742 \r
743         /* Utility function to generate a pseudo random number. */\r
744 \r
745         ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;\r
746         return( ( int ) ( ulNextRand >> 16UL ) & 0x7fffUL );\r
747 }\r
748 /*-----------------------------------------------------------*/\r
749 \r
750 static void prvSRand( UBaseType_t ulSeed )\r
751 {\r
752         /* Utility function to seed the pseudo random number generator. */\r
753     ulNextRand = ulSeed;\r
754 }\r
755 /*-----------------------------------------------------------*/\r
756 \r
757 static void prvMiscInitialisation( void )\r
758 {\r
759 time_t xTimeNow;\r
760 uint32_t ulLoggingIPAddress;\r
761 \r
762         ulLoggingIPAddress = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0, configECHO_SERVER_ADDR1, configECHO_SERVER_ADDR2, configECHO_SERVER_ADDR3 );\r
763         vLoggingInit( mainLOG_TO_STDOUT, mainLOG_TO_DISK_FILE, mainLOG_TO_UDP, ulLoggingIPAddress, configPRINT_PORT );\r
764 \r
765         /* Seed the random number generator. */\r
766         time( &xTimeNow );\r
767         FreeRTOS_debug_printf( ( "Seed for randomiser: %lu\n", xTimeNow ) );\r
768         prvSRand( ( uint32_t ) xTimeNow );\r
769         FreeRTOS_debug_printf( ( "Random numbers: %08X %08X %08X %08X\n", ipconfigRAND32(), ipconfigRAND32(), ipconfigRAND32(), ipconfigRAND32() ) );\r
770 }\r
771 /*-----------------------------------------------------------*/\r
772 \r
773 struct tm *gmtime_r( const time_t *pxTime, struct tm *tmStruct )\r
774 {\r
775         /* Dummy time functions to keep the file system happy in the absence of\r
776         target support. */\r
777         memcpy( tmStruct, gmtime( pxTime ), sizeof( * tmStruct ) );\r
778         return tmStruct;\r
779 }\r
780 /*-----------------------------------------------------------*/\r
781 \r
782 time_t FreeRTOS_time( time_t *pxTime )\r
783 {\r
784 time_t xReturn;\r
785 \r
786         xReturn = time( &xReturn );\r
787 \r
788         if( pxTime != NULL )\r
789         {\r
790                 *pxTime = xReturn;\r
791         }\r
792 \r
793         return xReturn;\r
794 }\r
795 /*-----------------------------------------------------------*/\r
796 \r
797 /*\r
798  * Callback that provides the inputs necessary to generate a randomized TCP\r
799  * Initial Sequence Number per RFC 6528.  THIS IS ONLY A DUMMY IMPLEMENTATION\r
800  * THAT RETURNS A PSEUDO RANDOM NUMBER SO IS NOT INTENDED FOR USE IN PRODUCTION\r
801  * SYSTEMS.\r
802  */\r
803 extern uint32_t ulApplicationGetNextSequenceNumber(uint32_t ulSourceAddress,\r
804         uint16_t usSourcePort,\r
805         uint32_t ulDestinationAddress,\r
806         uint16_t usDestinationPort)\r
807 {\r
808         (void)ulSourceAddress;\r
809         (void)usSourcePort;\r
810         (void)ulDestinationAddress;\r
811         (void)usDestinationPort;\r
812 \r
813         return uxRand();\r
814 }\r
815 /*-----------------------------------------------------------*/\r
816 \r
817 #if( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 ) || ( ipconfigDHCP_REGISTER_HOSTNAME != 0 )\r
818 \r
819         const char *pcApplicationHostnameHook( void )\r
820         {\r
821                 /* Assign the name "FreeRTOS" to this network node.  This function will\r
822                 be called during the DHCP: the machine will be registered with an IP\r
823                 address plus this name. */\r
824                 return mainHOST_NAME;\r
825         }\r
826 \r
827 #endif\r
828 /*-----------------------------------------------------------*/\r
829 \r
830 #if( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 )\r
831 \r
832         BaseType_t xApplicationDNSQueryHook( const char *pcName )\r
833         {\r
834         BaseType_t xReturn;\r
835 \r
836                 /* Determine if a name lookup is for this node.  Two names are given\r
837                 to this node: that returned by pcApplicationHostnameHook() and that set\r
838                 by mainDEVICE_NICK_NAME. */\r
839                 if( FF_stricmp( pcName, pcApplicationHostnameHook() ) == 0 )\r
840                 {\r
841                         xReturn = pdPASS;\r
842                 }\r
843                 else if( FF_stricmp( pcName, mainDEVICE_NICK_NAME ) == 0 )\r
844                 {\r
845                         xReturn = pdPASS;\r
846                 }\r
847                 else\r
848                 {\r
849                         xReturn = pdFAIL;\r
850                 }\r
851 \r
852                 return xReturn;\r
853         }\r
854 \r
855 #endif\r
856 /*-----------------------------------------------------------*/\r
857 \r
858 \r