]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_Windows_Simulator/main.c
Update version number to V8.0.0 (without the release candidate number).
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_Windows_Simulator / main.c
1 /*\r
2     FreeRTOS V8.0.0 - Copyright (C) 2014 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* Standard includes. */\r
67 #include <stdio.h>\r
68 #include <stdint.h>\r
69 \r
70 /* FreeRTOS includes. */\r
71 #include <FreeRTOS.h>\r
72 #include "task.h"\r
73 #include "queue.h"\r
74 #include "semphr.h"\r
75 \r
76 /* Demo application includes. */\r
77 #include "FreeRTOS_UDP_IP.h"\r
78 #include "FreeRTOS_Sockets.h"\r
79 #include "SimpleClientAndServer.h"\r
80 #include "TwoEchoClients.h"\r
81 #include "UDPCommandInterpreter.h"\r
82 #include "SelectServer.h"\r
83 \r
84 /* UDP command server task parameters. */\r
85 #define mainUDP_CLI_TASK_PRIORITY                                       ( tskIDLE_PRIORITY )\r
86 #define mainUDP_CLI_PORT_NUMBER                                         ( 5001UL )\r
87 #define mainUDP_CLI_TASK_STACK_SIZE                                     ( configMINIMAL_STACK_SIZE )\r
88 \r
89 /* Simple UDP client and server task parameters. */\r
90 #define mainSIMPLE_CLIENT_SERVER_TASK_PRIORITY          ( tskIDLE_PRIORITY )\r
91 #define mainSIMPLE_CLIENT_SERVER_PORT                           ( 5005UL )\r
92 #define mainSIMPLE_CLIENT_SERVER_TASK_STACK_SIZE        ( configMINIMAL_STACK_SIZE )\r
93 \r
94 /* Select UDP server task parameters. */\r
95 #define mainSELECT_SERVER_TASK_PRIORITY                         ( tskIDLE_PRIORITY )\r
96 #define mainSELECT_SERVER_PORT                                          ( 10001UL )\r
97 #define mainSELECT_SERVER_TASK_STACK_SIZE                       ( configMINIMAL_STACK_SIZE )\r
98 \r
99 /* Echo client task parameters. */\r
100 #define mainECHO_CLIENT_TASK_STACK_SIZE                         ( configMINIMAL_STACK_SIZE * 2 )\r
101 #define mainECHO_CLIENT_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
102 \r
103 /* Set the following constants to 1 or 0 to define which tasks to include and \r
104 exclude. */\r
105 #define mainCREATE_UDP_CLI_TASKS                                        1\r
106 #define mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS       0\r
107 #define mainCREATE_SELECT_UDP_SERVER_TASKS                      0\r
108 #define mainCREATE_UDP_ECHO_TASKS                                       1\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /*\r
113  * Register commands that can be used with FreeRTOS+CLI through the UDP socket.\r
114  * The commands are defined in CLI-commands.c.\r
115  */\r
116 extern void vRegisterCLICommands( void );\r
117 \r
118 /* The default IP and MAC address used by the demo.  The address configuration\r
119 defined here will be used if ipconfigUSE_DHCP is 0, or if ipconfigUSE_DHCP is\r
120 1 but a DHCP server could not be contacted.  See the online documentation for\r
121 more information. */\r
122 static const uint8_t ucIPAddress[ 4 ] = { configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 };\r
123 static const uint8_t ucNetMask[ 4 ] = { configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 };\r
124 static const uint8_t ucGatewayAddress[ 4 ] = { configGATEWAY_ADDR0, configGATEWAY_ADDR1, configGATEWAY_ADDR2, configGATEWAY_ADDR3 };\r
125 static const uint8_t ucDNSServerAddress[ 4 ] = { configDNS_SERVER_ADDR0, configDNS_SERVER_ADDR1, configDNS_SERVER_ADDR2, configDNS_SERVER_ADDR3 };\r
126 \r
127 /* Default MAC address configuration.  The demo creates a virtual network\r
128 connection that uses this MAC address by accessing the raw Ethernet data\r
129 to and from a real network connection on the host PC.  See the\r
130 configNETWORK_INTERFACE_TO_USE definition for information on how to configure\r
131 the real network connection to use. */\r
132 const uint8_t ucMACAddress[ 6 ] = { configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 };\r
133 \r
134 /* Used to guard prints to the console. */\r
135 static xSemaphoreHandle xConsoleMutex = NULL;\r
136 \r
137 /*-----------------------------------------------------------*/\r
138 \r
139 \r
140 \r
141 /******************************************************************************\r
142  *\r
143  * See the following web page for information on using this demo.\r
144  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/RTOS_UDP_CLI_Windows_Simulator.shtml\r
145  *\r
146  ******************************************************************************/\r
147 \r
148 \r
149 int main( void )\r
150 {\r
151 const uint32_t ulLongTime_ms = 250UL;\r
152 \r
153         /* Create a mutex that is used to guard against the console being accessed \r
154         by more than one task simultaniously. */\r
155         xConsoleMutex = xSemaphoreCreateMutex();\r
156 \r
157         /* Initialise the network interface.  Tasks that use the network are\r
158         created in the network event hook when the network is connected and ready\r
159         for use.  The address values passed in here are used if ipconfigUSE_DHCP is\r
160         set to 0, or if ipconfigUSE_DHCP is set to 1 but a DHCP server cannot be\r
161         contacted. */\r
162         FreeRTOS_IPInit( ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );\r
163 \r
164         /* Register commands with the FreeRTOS+CLI command interpreter. */\r
165         vRegisterCLICommands();\r
166 \r
167         /* Start the RTOS scheduler. */\r
168         vTaskStartScheduler();\r
169 \r
170         /* If all is well, the scheduler will now be running, and the following\r
171         line will never be reached.  If the following line does execute, then\r
172         there was insufficient FreeRTOS heap memory available for the idle and/or\r
173         timer tasks     to be created.  See the memory management section on the\r
174         FreeRTOS web site for more details (this is standard text that is not not\r
175         really applicable to the Win32 simulator port). */\r
176         for( ;; )\r
177         {\r
178                 Sleep( ulLongTime_ms );\r
179         }\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 void vApplicationIdleHook( void )\r
184 {\r
185 const unsigned long ulMSToSleep = 5;\r
186 \r
187         /* This function is called on each cycle of the idle task if\r
188         configUSE_IDLE_HOOK is set to 1 in FreeRTOSConfig.h.  Sleep to reduce CPU\r
189         load. */\r
190         Sleep( ulMSToSleep );\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 void vAssertCalled( void )\r
195 {\r
196 const unsigned long ulLongSleep = 1000UL;\r
197 volatile uint32_t ulBlockVariable = 0UL;\r
198 \r
199         /* Setting ulBlockVariable to a non-zero value in the debugger will allow\r
200         this function to be exited. */\r
201         taskDISABLE_INTERRUPTS();\r
202         {\r
203                 while( ulBlockVariable == 0UL )\r
204                 {\r
205                         Sleep( ulLongSleep );\r
206                 }\r
207         }\r
208         taskENABLE_INTERRUPTS();\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 void vOutputString( char *pcMessage )\r
213 {\r
214         /* Wrap the standard windows console output (as opposed to the FreeRTOS+CLI\r
215         console) with a mutex to ensure it can only be accessed by one task at a\r
216         time. */\r
217         xSemaphoreTake( xConsoleMutex, portMAX_DELAY );\r
218                 printf( pcMessage );\r
219         xSemaphoreGive( xConsoleMutex );\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 /* Called by FreeRTOS+UDP when the network connects. */\r
224 void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )\r
225 {\r
226 uint32_t ulIPAddress, ulNetMask, ulGatewayAddress, ulDNSServerAddress;\r
227 int8_t cBuffer[ 16 ];\r
228 static portBASE_TYPE xTasksAlreadyCreated = pdFALSE;\r
229 \r
230         if( eNetworkEvent == eNetworkUp )\r
231         {\r
232                 /* Create the tasks that use the IP stack if they have not already been\r
233                 created. */\r
234                 if( xTasksAlreadyCreated == pdFALSE )\r
235                 {\r
236                         #if( mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS == 1 )\r
237                         {\r
238                                 /* Create tasks that demonstrate sending and receiving in both\r
239                                 standard and zero copy mode. */\r
240                                 vStartSimpleUDPClientServerTasks( mainSIMPLE_CLIENT_SERVER_TASK_STACK_SIZE, mainSIMPLE_CLIENT_SERVER_PORT, mainSIMPLE_CLIENT_SERVER_TASK_PRIORITY );\r
241                         }\r
242                         #endif /* mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS */\r
243 \r
244                         #if( mainCREATE_SELECT_UDP_SERVER_TASKS == 1 )\r
245                         {\r
246                                 /* Create tasks that demonstrate sending and receiving in both\r
247                                 standard and zero copy mode. */\r
248                                 vStartSelectUDPServerTasks( mainSELECT_SERVER_TASK_STACK_SIZE, mainSELECT_SERVER_PORT, mainSELECT_SERVER_TASK_PRIORITY );\r
249                         }\r
250                         #endif /* mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS */\r
251 \r
252 \r
253                         #if( mainCREATE_UDP_ECHO_TASKS == 1 )\r
254                         {\r
255                                 /* Create the tasks that transmit to and receive from a standard\r
256                                 echo server (see the web documentation for this port) in both\r
257                                 standard and zero copy mode. */\r
258                                 vStartEchoClientTasks( mainECHO_CLIENT_TASK_STACK_SIZE, mainECHO_CLIENT_TASK_PRIORITY );\r
259                         }\r
260                         #endif /* mainCREATE_UDP_ECHO_TASKS */\r
261 \r
262                         #if( mainCREATE_UDP_CLI_TASKS == 1 )\r
263                         {\r
264                                 /* Create the task that handles the CLI on a UDP port.  The port number\r
265                                 is set using the configUDP_CLI_PORT_NUMBER setting in FreeRTOSConfig.h. */\r
266                                 vStartUDPCommandInterpreterTask( mainUDP_CLI_TASK_STACK_SIZE, mainUDP_CLI_PORT_NUMBER, mainUDP_CLI_TASK_PRIORITY );\r
267                         }\r
268                         #endif /* mainCREATE_UDP_CLI_TASKS */\r
269 \r
270                         xTasksAlreadyCreated = pdTRUE;\r
271                 }\r
272 \r
273                 /* Print out the network configuration, which may have come from a DHCP\r
274                 server. */\r
275                 FreeRTOS_GetAddressConfiguration( &ulIPAddress, &ulNetMask, &ulGatewayAddress, &ulDNSServerAddress );\r
276                 vOutputString( "IP Address: " );\r
277                 FreeRTOS_inet_ntoa( ulIPAddress, cBuffer );\r
278                 vOutputString( ( char * ) cBuffer );\r
279                 vOutputString( "\r\nSubnet Mask: " );\r
280                 FreeRTOS_inet_ntoa( ulNetMask, cBuffer );\r
281                 vOutputString( ( char * ) cBuffer );\r
282                 vOutputString( "\r\nGateway Address: " );\r
283                 FreeRTOS_inet_ntoa( ulGatewayAddress, cBuffer );\r
284                 vOutputString( ( char * ) cBuffer );\r
285                 vOutputString( "\r\nDNS Server Address: " );\r
286                 FreeRTOS_inet_ntoa( ulDNSServerAddress, cBuffer );\r
287                 vOutputString( ( char * ) cBuffer );\r
288                 vOutputString( "\r\n\r\n" );\r
289         }\r
290 }\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 /* Called automatically when a reply to an outgoing ping is received. */\r
294 void vApplicationPingReplyHook( ePingReplyStatus_t eStatus, uint16_t usIdentifier )\r
295 {\r
296 static const uint8_t *pcSuccess = ( uint8_t * ) "Ping reply received - ";\r
297 static const uint8_t *pcInvalidChecksum = ( uint8_t * ) "Ping reply received with invalid checksum - ";\r
298 static const uint8_t *pcInvalidData = ( uint8_t * ) "Ping reply received with invalid data - ";\r
299 static uint8_t cMessage[ 50 ];\r
300 \r
301 \r
302         switch( eStatus )\r
303         {\r
304                 case eSuccess   :\r
305                         vOutputString( ( char * ) pcSuccess );\r
306                         break;\r
307 \r
308                 case eInvalidChecksum :\r
309                         vOutputString( ( char * ) pcInvalidChecksum );\r
310                         break;\r
311 \r
312                 case eInvalidData :\r
313                         vOutputString( ( char * ) pcInvalidData );\r
314                         break;\r
315 \r
316                 default :\r
317                         /* It is not possible to get here as all enums have their own\r
318                         case. */\r
319                         break;\r
320         }\r
321 \r
322         sprintf( ( char * ) cMessage, "identifier %d\r\n", ( int ) usIdentifier );\r
323         vOutputString( ( char * ) cMessage );\r
324 }\r
325 /*-----------------------------------------------------------*/\r
326 \r
327 void vApplicationMallocFailedHook( void )\r
328 {\r
329         /* vApplicationMallocFailedHook() will only be called if\r
330         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
331         function that will get called if a call to pvPortMalloc() fails.\r
332         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
333         timer or semaphore is created.  It is also called by various parts of the\r
334         demo application.  If heap_1.c, heap_2.c or heap_4.c are used, then the \r
335         size of the heap available to pvPortMalloc() is defined by \r
336         configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize() \r
337         API function can be used to query the size of free heap space that remains \r
338         (although it does not provide information on how the remaining heap might \r
339         be fragmented). */\r
340         taskDISABLE_INTERRUPTS();\r
341         for( ;; );\r
342 }\r
343 \r
344 \r
345 \r