]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MSVC-lwIP/main.c
Update license information text files for the CLI, TCP and UDP products to be correct...
[freertos] / FreeRTOS / Demo / WIN32-MSVC-lwIP / 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  *******************************************************************************\r
31  * -NOTE- The Win32 port is a simulation (or is that emulation?) only!  Do not\r
32  * expect to get real time behaviour from the Win32 port or this demo\r
33  * application.  It is provided as a convenient development and demonstration\r
34  * test bed only.  This was tested using Windows XP on a dual core laptop.\r
35  *\r
36  * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -\r
37  * - http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html\r
38  * - Note that the above linked page describes the simulator environment.  It\r
39  * - is not the correct page to view for information on using this lwIP demo.\r
40  *******************************************************************************\r
41  *\r
42  * This project demonstrates use of the lwIP stack.  The lwIP raw API is\r
43  * demonstrated by a simple http server that comes as part of the lwIP\r
44  * distribution - and executes in the tcpip task.  The lwIP sockets API\r
45  * is demonstrated by a simple command line interpreter interface, which\r
46  * executes in its own task.\r
47  *\r
48  * Both the http and command line server can be used to view task stats, and\r
49  * run time stats.  Task stats give a snapshot of the state of each task in\r
50  * the system.  Run time stats show how much processing time has been allocated\r
51  * to each task.  A few of the standard demo tasks are created, just to ensure\r
52  * there is some data to be viewed.\r
53  *\r
54  * Finally, a check timer is created.  The check timer is a software timer that\r
55  * inspects the few standard demo tasks that are created to ensure they are\r
56  * executing as expected.  It maintains a status string that can be viewed on\r
57  * the "task stats" page served by the web server.\r
58  *\r
59  * More information about this demo, including details of how to set up the\r
60  * network interface, and the command line commands that are available, is\r
61  * available on the documentation page for this demo on the\r
62  * http://www.FreeRTOS.org web site.\r
63  *\r
64  */\r
65 \r
66 \r
67 /* Standard includes. */\r
68 #include <stdio.h>\r
69 \r
70 /* Kernel includes. */\r
71 #include <FreeRTOS.h>\r
72 #include "task.h"\r
73 #include "timers.h"\r
74 \r
75 /* Standard demo includes. */\r
76 #include "GenQTest.h"\r
77 \r
78 /* lwIP includes. */\r
79 #include "lwip/tcpip.h"\r
80 #include "lwIP_Apps.h"\r
81 \r
82 /* Utils includes. */\r
83 #include "CommandInterpreter.h"\r
84 \r
85 /* Priorities at which the tasks are created. */\r
86 #define mainGEN_QUEUE_TASK_PRIORITY     ( tskIDLE_PRIORITY )\r
87 \r
88 /* The period at which the check timer will expire, in ms, provided no errors\r
89 have been reported by any of the standard demo tasks.  ms are converted to the\r
90 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
91 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
92 \r
93 /* Check timer callback function. */\r
94 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
95 \r
96 /* Defined in lwIPApps.c. */\r
97 extern void lwIPAppsInit( void *pvArguments );\r
98 \r
99 /* Callbacks to handle the command line commands defined by the xTaskStats and\r
100 xRunTimeStats command definitions respectively.  These functions are not\r
101 necessarily reentrant!  They must be used from one task only - or at least by\r
102 only one task at a time. */\r
103 static portBASE_TYPE prvTaskStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString );\r
104 static portBASE_TYPE prvRunTimeStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString );\r
105 \r
106 /* The string that latches the current demo status. */\r
107 static char *pcStatusMessage = "All tasks running without error";\r
108 \r
109 /* Variables used in the creation of the run time stats time base.  Run time\r
110 stats record how much time each task spends in the Running state. */\r
111 long long llInitialRunTimeCounterValue = 0LL, llRunTimeStatsDivisor = 0LL;\r
112 \r
113 /* The check timer.  This uses prvCheckTimerCallback() as its callback\r
114 function. */\r
115 static TimerHandle_t xCheckTimer = NULL;\r
116 \r
117 /* Structure that defines the "run-time-stats" command line command. */\r
118 static const xCommandLineInput xRunTimeStats =\r
119 {\r
120         "run-time-stats",\r
121         "run-time-stats: Displays a table showing how much processing time each FreeRTOS task has used\r\n",\r
122         prvRunTimeStatsCommand,\r
123         0\r
124 };\r
125 \r
126 /* Structure that defines the "task-stats" command line command. */\r
127 static const xCommandLineInput xTaskStats =\r
128 {\r
129         "task-stats",\r
130         "task-stats: Displays a table showing the state of each FreeRTOS task\r\n",\r
131         prvTaskStatsCommand,\r
132         0\r
133 };\r
134 \r
135 /*-----------------------------------------------------------*/\r
136 \r
137 int main( void )\r
138 {\r
139 const unsigned long ulLongTime_ms = 1000UL;\r
140 \r
141         /* This call creates the TCP/IP thread. */\r
142         tcpip_init( lwIPAppsInit, NULL );\r
143 \r
144         /* Create and start the check timer, as described at the top of this file. */\r
145         xCheckTimer = xTimerCreate( "CheckTimer",/* A text name, purely to help debugging. */\r
146                                                                 ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
147                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
148                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
149                                                                 prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
150                                                           );\r
151 \r
152         /* Sanity check that the timer was created. */\r
153         configASSERT( xCheckTimer );\r
154 \r
155         /* Start the check timer. */\r
156         xTimerStart( xCheckTimer, 0UL );\r
157 \r
158         /* Create a few standard demo tasks, just so there are tasks running to\r
159         view on the web server and via the command line command interpreter. */\r
160         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
161 \r
162         /* Register two command line commands to show task stats and run time stats\r
163         respectively. */\r
164         xCmdIntRegisterCommand( &xTaskStats );\r
165         xCmdIntRegisterCommand( &xRunTimeStats );\r
166 \r
167         /* Start the scheduler itself. */\r
168         vTaskStartScheduler();\r
169 \r
170         /* This line should never be reached.  If it does execute then there was\r
171         insufficient FreeRTOS heap memory available for the idle and/or timer\r
172         tasks to be created. */\r
173         for( ;; )\r
174         {\r
175                 Sleep( ulLongTime_ms );\r
176         }\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
181 {\r
182         /* The parameter is not used in this case. */\r
183         ( void ) xTimer;\r
184 \r
185         /* Check the standard demo tasks are running without error.   Latch the\r
186         latest reported error in the pcStatusMessage character pointer.  The latched\r
187         string can be viewed using the embedded web server and the command line\r
188         interpreter.  This project is really to demonstrate the lwIP stack - so very\r
189         few tasks are created - and those that are created are created purely so\r
190         there is something to view. */\r
191         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
192         {\r
193                 pcStatusMessage = "Error: The GenQueue test reported an error.";\r
194         }\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 void vApplicationIdleHook( void )\r
199 {\r
200 const unsigned long ulMSToSleep = 5;\r
201 \r
202         /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are\r
203         tasks waiting to be terminated by the idle task. */\r
204         Sleep( ulMSToSleep );\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 void vApplicationMallocFailedHook( void )\r
209 {\r
210 const unsigned long ulLongSleep = 1000UL;\r
211 \r
212         /* Can be implemented if required, but probably not required in this\r
213         environment and running this demo. */\r
214         taskDISABLE_INTERRUPTS();\r
215         for( ;; )\r
216         {\r
217                 Sleep( ulLongSleep );\r
218         }\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 void vApplicationStackOverflowHook( void )\r
223 {\r
224 const unsigned long ulLongSleep = 1000UL;\r
225 \r
226         /* Can be implemented if required, but probably not required in this\r
227         environment and running this demo. */\r
228         taskDISABLE_INTERRUPTS();\r
229         for( ;; )\r
230         {\r
231                 Sleep( ulLongSleep );\r
232         }\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 void vAssertCalled( void )\r
237 {\r
238 const unsigned long ulLongSleep = 1000UL;\r
239 \r
240         taskDISABLE_INTERRUPTS();\r
241         for( ;; )\r
242         {\r
243                 Sleep( ulLongSleep );\r
244         }\r
245 }\r
246 /*-----------------------------------------------------------*/\r
247 \r
248 char *pcMainGetTaskStatusMessage( void )\r
249 {\r
250         return pcStatusMessage;\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r
254 void vMainConfigureTimerForRunTimeStats( void )\r
255 {\r
256 LARGE_INTEGER liPerformanceCounterFrequency, liInitialRunTimeValue;\r
257 \r
258         /* Initialise the variables used to create the run time stats time base.\r
259         Run time stats record how much time each task spends in the Running\r
260         state. */\r
261 \r
262         if( QueryPerformanceFrequency( &liPerformanceCounterFrequency ) == 0 )\r
263         {\r
264                 llRunTimeStatsDivisor = 1;\r
265         }\r
266         else\r
267         {\r
268                 /* How many times does the performance counter increment in 10ms? */\r
269                 llRunTimeStatsDivisor = liPerformanceCounterFrequency.QuadPart / 1000LL;\r
270 \r
271                 /* What is the performance counter value now, this will be subtracted\r
272                 from readings taken at run time. */\r
273                 QueryPerformanceCounter( &liInitialRunTimeValue );\r
274                 llInitialRunTimeCounterValue = liInitialRunTimeValue.QuadPart;\r
275         }\r
276 }\r
277 /*-----------------------------------------------------------*/\r
278 \r
279 unsigned long ulMainGetRunTimeCounterValue( void )\r
280 {\r
281 LARGE_INTEGER liCurrentCount;\r
282 unsigned long ulReturn;\r
283 \r
284         /* What is the performance counter value now? */\r
285         QueryPerformanceCounter( &liCurrentCount );\r
286 \r
287         /* Subtract the performance counter value reading taken when the\r
288         application started to get a count from that reference point, then\r
289         scale to a 32 bit number. */\r
290         ulReturn = ( unsigned long ) ( ( liCurrentCount.QuadPart - llInitialRunTimeCounterValue ) / llRunTimeStatsDivisor );\r
291 \r
292         return ulReturn;\r
293 }\r
294 /*-----------------------------------------------------------*/\r
295 \r
296 static portBASE_TYPE prvTaskStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString )\r
297 {\r
298 const char *const pcHeader = "Task          State  Priority  Stack      #\r\n************************************************\r\n";\r
299 \r
300         configASSERT( pcWriteBuffer );\r
301 \r
302         /* This function assumes the buffer length is adequate and does not look\r
303         for parameters. */\r
304         ( void ) xWriteBufferLen;\r
305         ( void ) pcCommandString;\r
306 \r
307         /* Generate a table of task stats. */\r
308         strcpy( pcWriteBuffer, pcHeader );\r
309         vTaskList( pcWriteBuffer + strlen( pcHeader ) );\r
310 \r
311         /* There is no more data to return after this single string, so return\r
312         pdFALSE. */\r
313         return pdFALSE;\r
314 }\r
315 /*-----------------------------------------------------------*/\r
316 \r
317 static portBASE_TYPE prvRunTimeStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString )\r
318 {\r
319 const char * const pcHeader = "Task            Abs Time      % Time\r\n****************************************\r\n";\r
320 \r
321         configASSERT( pcWriteBuffer );\r
322 \r
323         /* This function assumes the buffer length is adequate and does not look\r
324         for parameters. */\r
325         ( void ) xWriteBufferLen;\r
326         ( void ) pcCommandString;\r
327 \r
328         /* Generate a table of task stats. */\r
329         strcpy( pcWriteBuffer, pcHeader );\r
330         vTaskGetRunTimeStats( ( char * ) pcWriteBuffer + strlen( pcHeader ) );\r
331 \r
332         /* There is no more data to return after this single string, so return\r
333         pdFALSE. */\r
334         return pdFALSE;\r
335 }\r
336 \r
337 \r