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