]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_CLI_with_Trace_Windows_Simulator/CLI-commands.c
For consistency change the name of configINCLUDE_STATS_FORMATTING_FUNCTIONS to config...
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_CLI_with_Trace_Windows_Simulator / CLI-commands.c
1 /*\r
2     FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /* FreeRTOS includes. */\r
66 #include "FreeRTOS.h"\r
67 #include "task.h"\r
68 \r
69 /* FreeRTOS+CLI includes. */\r
70 #include "FreeRTOS_CLI.h"\r
71 \r
72 /* FreeRTOS+Trace includes. */\r
73 #include "trcUser.h"\r
74 \r
75 /*\r
76  * Writes trace data to a disk file when the trace recording is stopped.\r
77  * This function will simply overwrite any trace files that already exist.\r
78  */\r
79 static void prvSaveTraceFile( void );\r
80 \r
81 /*\r
82  * Defines a command that returns a table showing the state of each task at the\r
83  * time the command is called.\r
84  */\r
85 static portBASE_TYPE prvTaskStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
86 \r
87 /*\r
88  * Defines a command that returns a table showing how much time each task has\r
89  * spent in the Running state.\r
90  */\r
91 static portBASE_TYPE prvRunTimeStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
92 \r
93 /*\r
94  * Defines a command that expects exactly three parameters.  Each of the three\r
95  * parameter are echoed back one at a time.\r
96  */\r
97 static portBASE_TYPE prvThreeParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
98 \r
99 /*\r
100  * Defines a command that can take a variable number of parameters.  Each\r
101  * parameter is echoes back one at a time.\r
102  */\r
103 static portBASE_TYPE prvParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
104 \r
105 /*\r
106  * Defines a command that starts/stops events being recorded for offline viewing\r
107  * in FreeRTOS+Trace.\r
108  */\r
109 static portBASE_TYPE prvStartStopTraceCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
110 \r
111 /* Structure that defines the "run-time-stats" command line command. */\r
112 static const CLI_Command_Definition_t xRunTimeStats =\r
113 {\r
114         ( const int8_t * const ) "run-time-stats", /* The command string to type. */\r
115         ( const int8_t * const ) "\r\nrun-time-stats:\r\n Displays a table showing how much processing time each FreeRTOS task has used\r\n\r\n",\r
116         prvRunTimeStatsCommand, /* The function to run. */\r
117         0 /* No parameters are expected. */\r
118 };\r
119 \r
120 /* Structure that defines the "task-stats" command line command. */\r
121 static const CLI_Command_Definition_t xTaskStats =\r
122 {\r
123         ( const int8_t * const ) "task-stats", /* The command string to type. */\r
124         ( const int8_t * const ) "\r\ntask-stats:\r\n Displays a table showing the state of each FreeRTOS task\r\n\r\n",\r
125         prvTaskStatsCommand, /* The function to run. */\r
126         0 /* No parameters are expected. */\r
127 };\r
128 \r
129 /* Structure that defines the "echo_3_parameters" command line command.  This\r
130 takes exactly three parameters that the command simply echos back one at a\r
131 time. */\r
132 static const CLI_Command_Definition_t xThreeParameterEcho =\r
133 {\r
134         ( const int8_t * const ) "echo_3_parameters",\r
135         ( const int8_t * const ) "\r\necho_3_parameters <param1> <param2> <param3>:\r\n Expects three parameters, echos each in turn\r\n\r\n",\r
136         prvThreeParameterEchoCommand, /* The function to run. */\r
137         3 /* Three parameters are expected, which can take any value. */\r
138 };\r
139 \r
140 /* Structure that defines the "echo_parameters" command line command.  This\r
141 takes a variable number of parameters that the command simply echos back one at\r
142 a time. */\r
143 static const CLI_Command_Definition_t xParameterEcho =\r
144 {\r
145         ( const int8_t * const ) "echo_parameters",\r
146         ( const int8_t * const ) "\r\necho_parameters <...>:\r\n Take variable number of parameters, echos each in turn\r\n\r\n",\r
147         prvParameterEchoCommand, /* The function to run. */\r
148         -1 /* The user can enter any number of commands. */\r
149 };\r
150 \r
151 /* Structure that defines the "trace" command line command.  This takes a single\r
152 parameter, which can be either "start" or "stop". */\r
153 static const CLI_Command_Definition_t xStartTrace =\r
154 {\r
155         ( const int8_t * const ) "trace",\r
156         ( const int8_t * const ) "\r\ntrace [start | stop]:\r\n Starts or stops a trace recording for viewing in FreeRTOS+Trace\r\n\r\n",\r
157         prvStartStopTraceCommand, /* The function to run. */\r
158         1 /* One parameter is expected.  Valid values are "start" and "stop". */\r
159 };\r
160 \r
161 /*-----------------------------------------------------------*/\r
162 \r
163 void vRegisterCLICommands( void )\r
164 {\r
165         /* Register all the command line commands defined immediately above. */\r
166         FreeRTOS_CLIRegisterCommand( &xTaskStats );\r
167         FreeRTOS_CLIRegisterCommand( &xRunTimeStats );\r
168         FreeRTOS_CLIRegisterCommand( &xThreeParameterEcho );\r
169         FreeRTOS_CLIRegisterCommand( &xParameterEcho );\r
170         FreeRTOS_CLIRegisterCommand( &xStartTrace );\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 static portBASE_TYPE prvTaskStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
175 {\r
176 const int8_t *const pcHeader = ( int8_t * ) "Task          State  Priority  Stack       #\r\n************************************************\r\n";\r
177 \r
178         /* Remove compile time warnings about unused parameters, and check the\r
179         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
180         write buffer length is adequate, so does not check for buffer overflows. */\r
181         ( void ) pcCommandString;\r
182         ( void ) xWriteBufferLen;\r
183         configASSERT( pcWriteBuffer );\r
184 \r
185         /* Generate a table of task stats. */\r
186         strcpy( ( char * ) pcWriteBuffer, ( char * ) pcHeader );\r
187         vTaskList( pcWriteBuffer + strlen( ( char * ) pcHeader ) );\r
188 \r
189         /* There is no more data to return after this single string, so return\r
190         pdFALSE. */\r
191         return pdFALSE;\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 static portBASE_TYPE prvRunTimeStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
196 {\r
197 const int8_t * const pcHeader = ( int8_t * ) "Task            Abs Time      % Time\r\n****************************************\r\n";\r
198 \r
199         /* Remove compile time warnings about unused parameters, and check the\r
200         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
201         write buffer length is adequate, so does not check for buffer overflows. */\r
202         ( void ) pcCommandString;\r
203         ( void ) xWriteBufferLen;\r
204         configASSERT( pcWriteBuffer );\r
205 \r
206         /* Generate a table of task stats. */\r
207         strcpy( ( char * ) pcWriteBuffer, ( char * ) pcHeader );\r
208         vTaskGetRunTimeStats( pcWriteBuffer + strlen( ( char * ) pcHeader ) );\r
209 \r
210         /* There is no more data to return after this single string, so return\r
211         pdFALSE. */\r
212         return pdFALSE;\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 static portBASE_TYPE prvThreeParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
217 {\r
218 int8_t *pcParameter;\r
219 portBASE_TYPE lParameterStringLength, xReturn;\r
220 static portBASE_TYPE lParameterNumber = 0;\r
221 \r
222         /* Remove compile time warnings about unused parameters, and check the\r
223         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
224         write buffer length is adequate, so does not check for buffer overflows. */\r
225         ( void ) pcCommandString;\r
226         ( void ) xWriteBufferLen;\r
227         configASSERT( pcWriteBuffer );\r
228 \r
229         if( lParameterNumber == 0 )\r
230         {\r
231                 /* The first time the function is called after the command has been\r
232                 entered just a header string is returned. */\r
233                 sprintf( ( char * ) pcWriteBuffer, "The three parameters were:\r\n" );\r
234 \r
235                 /* Next time the function is called the first parameter will be echoed\r
236                 back. */\r
237                 lParameterNumber = 1L;\r
238 \r
239                 /* There is more data to be returned as no parameters have been echoed\r
240                 back yet. */\r
241                 xReturn = pdPASS;\r
242         }\r
243         else\r
244         {\r
245                 /* Obtain the parameter string. */\r
246                 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter\r
247                                                                         (\r
248                                                                                 pcCommandString,                /* The command string itself. */\r
249                                                                                 lParameterNumber,               /* Return the next parameter. */\r
250                                                                                 &lParameterStringLength /* Store the parameter string length. */\r
251                                                                         );\r
252 \r
253                 /* Sanity check something was returned. */\r
254                 configASSERT( pcParameter );\r
255 \r
256                 /* Return the parameter string. */\r
257                 memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
258                 sprintf( ( char * ) pcWriteBuffer, "%d: ", lParameterNumber );\r
259                 strncat( ( char * ) pcWriteBuffer, ( const char * ) pcParameter, lParameterStringLength );\r
260                 strncat( ( char * ) pcWriteBuffer, "\r\n", strlen( "\r\n" ) );\r
261 \r
262                 /* If this is the last of the three parameters then there are no more\r
263                 strings to return after this one. */\r
264                 if( lParameterNumber == 3L )\r
265                 {\r
266                         /* If this is the last of the three parameters then there are no more\r
267                         strings to return after this one. */\r
268                         xReturn = pdFALSE;\r
269                         lParameterNumber = 0L;\r
270                 }\r
271                 else\r
272                 {\r
273                         /* There are more parameters to return after this one. */\r
274                         xReturn = pdTRUE;\r
275                         lParameterNumber++;\r
276                 }\r
277         }\r
278 \r
279         return xReturn;\r
280 }\r
281 /*-----------------------------------------------------------*/\r
282 \r
283 static portBASE_TYPE prvParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
284 {\r
285 int8_t *pcParameter;\r
286 portBASE_TYPE lParameterStringLength, xReturn;\r
287 static portBASE_TYPE lParameterNumber = 0;\r
288 \r
289         /* Remove compile time warnings about unused parameters, and check the\r
290         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
291         write buffer length is adequate, so does not check for buffer overflows. */\r
292         ( void ) pcCommandString;\r
293         ( void ) xWriteBufferLen;\r
294         configASSERT( pcWriteBuffer );\r
295 \r
296         if( lParameterNumber == 0 )\r
297         {\r
298                 /* The first time the function is called after the command has been\r
299                 entered just a header string is returned. */\r
300                 sprintf( ( char * ) pcWriteBuffer, "The parameters were:\r\n" );\r
301 \r
302                 /* Next time the function is called the first parameter will be echoed\r
303                 back. */\r
304                 lParameterNumber = 1L;\r
305 \r
306                 /* There is more data to be returned as no parameters have been echoed\r
307                 back yet. */\r
308                 xReturn = pdPASS;\r
309         }\r
310         else\r
311         {\r
312                 /* Obtain the parameter string. */\r
313                 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter\r
314                                                                         (\r
315                                                                                 pcCommandString,                /* The command string itself. */\r
316                                                                                 lParameterNumber,               /* Return the next parameter. */\r
317                                                                                 &lParameterStringLength /* Store the parameter string length. */\r
318                                                                         );\r
319 \r
320                 if( pcParameter != NULL )\r
321                 {\r
322                         /* Return the parameter string. */\r
323                         memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
324                         sprintf( ( char * ) pcWriteBuffer, "%d: ", lParameterNumber );\r
325                         strncat( ( char * ) pcWriteBuffer, ( const char * ) pcParameter, lParameterStringLength );\r
326                         strncat( ( char * ) pcWriteBuffer, "\r\n", strlen( "\r\n" ) );\r
327 \r
328                         /* There might be more parameters to return after this one. */\r
329                         xReturn = pdTRUE;\r
330                         lParameterNumber++;\r
331                 }\r
332                 else\r
333                 {\r
334                         /* No more parameters were found.  Make sure the write buffer does\r
335                         not contain a valid string. */\r
336                         pcWriteBuffer[ 0 ] = 0x00;\r
337 \r
338                         /* No more data to return. */\r
339                         xReturn = pdFALSE;\r
340 \r
341                         /* Start over the next time this command is executed. */\r
342                         lParameterNumber = 0;\r
343                 }\r
344         }\r
345 \r
346         return xReturn;\r
347 }\r
348 /*-----------------------------------------------------------*/\r
349 \r
350 static portBASE_TYPE prvStartStopTraceCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
351 {\r
352 int8_t *pcParameter;\r
353 portBASE_TYPE lParameterStringLength;\r
354 \r
355         /* Remove compile time warnings about unused parameters, and check the\r
356         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
357         write buffer length is adequate, so does not check for buffer overflows. */\r
358         ( void ) pcCommandString;\r
359         ( void ) xWriteBufferLen;\r
360         configASSERT( pcWriteBuffer );\r
361 \r
362         /* Obtain the parameter string. */\r
363         pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter\r
364                                                                 (\r
365                                                                         pcCommandString,                /* The command string itself. */\r
366                                                                         1,                                              /* Return the first parameter. */\r
367                                                                         &lParameterStringLength /* Store the parameter string length. */\r
368                                                                 );\r
369 \r
370         /* Sanity check something was returned. */\r
371         configASSERT( pcParameter );\r
372 \r
373         /* There are only two valid parameter values. */\r
374         if( strncmp( ( const char * ) pcParameter, "start", strlen( "start" ) ) == 0 )\r
375         {\r
376                 /* Start or restart the trace. */\r
377                 vTraceStop();\r
378                 vTraceClear();\r
379                 uiTraceStart();\r
380 \r
381                 sprintf( ( char * ) pcWriteBuffer, "Trace recording (re)started.\r\n" );\r
382         }\r
383         else if( strncmp( ( const char * ) pcParameter, "stop", strlen( "stop" ) ) == 0 )\r
384         {\r
385                 /* End the trace, if one is running. */\r
386                 vTraceStop();\r
387                 sprintf( ( char * ) pcWriteBuffer, "Stopping trace recording and dumping log to disk.\r\n" );\r
388                 prvSaveTraceFile();\r
389         }\r
390         else\r
391         {\r
392                 sprintf( ( char * ) pcWriteBuffer, "Valid parameters are 'start' and 'stop'.\r\n" );\r
393         }\r
394 \r
395         /* There is no more data to return after this single string, so return\r
396         pdFALSE. */\r
397         return pdFALSE;\r
398 }\r
399 /*-----------------------------------------------------------*/\r
400 \r
401 static void prvSaveTraceFile( void )\r
402 {\r
403 FILE* pxOutputFile;\r
404 \r
405         fopen_s( &pxOutputFile, "Trace.dump", "wb");\r
406 \r
407         if( pxOutputFile != NULL )\r
408         {\r
409                 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );\r
410                 fclose( pxOutputFile );\r
411                 printf( "\r\nTrace output saved to Trace.dump\r\n" );\r
412         }\r
413         else\r
414         {\r
415                 printf( "\r\nFailed to create trace dump file\r\n" );\r
416         }\r
417 }\r
418 \r