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