]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_CLI_with_Trace_Windows_Simulator/CLI-commands.c
38274b342f10f14b5d04cfeb3b605123c54d6b5e
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_CLI_with_Trace_Windows_Simulator / CLI-commands.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 /* FreeRTOS includes. */\r
76 #include "FreeRTOS.h"\r
77 #include "task.h"\r
78 \r
79 /* FreeRTOS+CLI includes. */\r
80 #include "FreeRTOS_CLI.h"\r
81 \r
82 /* FreeRTOS+Trace includes. */\r
83 #include "trcUser.h"\r
84 \r
85 /*\r
86  * Writes trace data to a disk file when the trace recording is stopped.\r
87  * This function will simply overwrite any trace files that already exist.\r
88  */\r
89 static void prvSaveTraceFile( void );\r
90 \r
91 /*\r
92  * Defines a command that returns a table showing the state of each task at the\r
93  * time the command is called.\r
94  */\r
95 static portBASE_TYPE prvTaskStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
96 \r
97 /*\r
98  * Defines a command that returns a table showing how much time each task has\r
99  * spent in the Running state.\r
100  */\r
101 static portBASE_TYPE prvRunTimeStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
102 \r
103 /*\r
104  * Defines a command that expects exactly three parameters.  Each of the three\r
105  * parameter are echoed back one at a time.\r
106  */\r
107 static portBASE_TYPE prvThreeParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
108 \r
109 /*\r
110  * Defines a command that can take a variable number of parameters.  Each\r
111  * parameter is echoes back one at a time.\r
112  */\r
113 static portBASE_TYPE prvParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
114 \r
115 /*\r
116  * Defines a command that starts/stops events being recorded for offline viewing\r
117  * in FreeRTOS+Trace.\r
118  */\r
119 static portBASE_TYPE prvStartStopTraceCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
120 \r
121 /* Structure that defines the "run-time-stats" command line command. */\r
122 static const CLI_Command_Definition_t xRunTimeStats =\r
123 {\r
124         ( const int8_t * const ) "run-time-stats", /* The command string to type. */\r
125         ( 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
126         prvRunTimeStatsCommand, /* The function to run. */\r
127         0 /* No parameters are expected. */\r
128 };\r
129 \r
130 /* Structure that defines the "task-stats" command line command. */\r
131 static const CLI_Command_Definition_t xTaskStats =\r
132 {\r
133         ( const int8_t * const ) "task-stats", /* The command string to type. */\r
134         ( const int8_t * const ) "\r\ntask-stats:\r\n Displays a table showing the state of each FreeRTOS task\r\n\r\n",\r
135         prvTaskStatsCommand, /* The function to run. */\r
136         0 /* No parameters are expected. */\r
137 };\r
138 \r
139 /* Structure that defines the "echo_3_parameters" command line command.  This\r
140 takes exactly three parameters that the command simply echos back one at a\r
141 time. */\r
142 static const CLI_Command_Definition_t xThreeParameterEcho =\r
143 {\r
144         ( const int8_t * const ) "echo_3_parameters",\r
145         ( 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
146         prvThreeParameterEchoCommand, /* The function to run. */\r
147         3 /* Three parameters are expected, which can take any value. */\r
148 };\r
149 \r
150 /* Structure that defines the "echo_parameters" command line command.  This\r
151 takes a variable number of parameters that the command simply echos back one at\r
152 a time. */\r
153 static const CLI_Command_Definition_t xParameterEcho =\r
154 {\r
155         ( const int8_t * const ) "echo_parameters",\r
156         ( const int8_t * const ) "\r\necho_parameters <...>:\r\n Take variable number of parameters, echos each in turn\r\n\r\n",\r
157         prvParameterEchoCommand, /* The function to run. */\r
158         -1 /* The user can enter any number of commands. */\r
159 };\r
160 \r
161 /* Structure that defines the "trace" command line command.  This takes a single\r
162 parameter, which can be either "start" or "stop". */\r
163 static const CLI_Command_Definition_t xStartTrace =\r
164 {\r
165         ( const int8_t * const ) "trace",\r
166         ( 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
167         prvStartStopTraceCommand, /* The function to run. */\r
168         1 /* One parameter is expected.  Valid values are "start" and "stop". */\r
169 };\r
170 \r
171 /*-----------------------------------------------------------*/\r
172 \r
173 void vRegisterCLICommands( void )\r
174 {\r
175         /* Register all the command line commands defined immediately above. */\r
176         FreeRTOS_CLIRegisterCommand( &xTaskStats );\r
177         FreeRTOS_CLIRegisterCommand( &xRunTimeStats );\r
178         FreeRTOS_CLIRegisterCommand( &xThreeParameterEcho );\r
179         FreeRTOS_CLIRegisterCommand( &xParameterEcho );\r
180         FreeRTOS_CLIRegisterCommand( &xStartTrace );\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 static portBASE_TYPE prvTaskStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
185 {\r
186 const int8_t *const pcHeader = ( int8_t * ) "Task          State  Priority  Stack       #\r\n************************************************\r\n";\r
187 \r
188         /* Remove compile time warnings about unused parameters, and check the\r
189         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
190         write buffer length is adequate, so does not check for buffer overflows. */\r
191         ( void ) pcCommandString;\r
192         ( void ) xWriteBufferLen;\r
193         configASSERT( pcWriteBuffer );\r
194 \r
195         /* Generate a table of task stats. */\r
196         strcpy( ( char * ) pcWriteBuffer, ( char * ) pcHeader );\r
197         vTaskList( pcWriteBuffer + strlen( ( char * ) pcHeader ) );\r
198 \r
199         /* There is no more data to return after this single string, so return\r
200         pdFALSE. */\r
201         return pdFALSE;\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 static portBASE_TYPE prvRunTimeStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
206 {\r
207 const int8_t * const pcHeader = ( int8_t * ) "Task            Abs Time      % Time\r\n****************************************\r\n";\r
208 \r
209         /* Remove compile time warnings about unused parameters, and check the\r
210         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
211         write buffer length is adequate, so does not check for buffer overflows. */\r
212         ( void ) pcCommandString;\r
213         ( void ) xWriteBufferLen;\r
214         configASSERT( pcWriteBuffer );\r
215 \r
216         /* Generate a table of task stats. */\r
217         strcpy( ( char * ) pcWriteBuffer, ( char * ) pcHeader );\r
218         vTaskGetRunTimeStats( pcWriteBuffer + strlen( ( char * ) pcHeader ) );\r
219 \r
220         /* There is no more data to return after this single string, so return\r
221         pdFALSE. */\r
222         return pdFALSE;\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 static portBASE_TYPE prvThreeParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
227 {\r
228 int8_t *pcParameter;\r
229 portBASE_TYPE lParameterStringLength, xReturn;\r
230 static portBASE_TYPE lParameterNumber = 0;\r
231 \r
232         /* Remove compile time warnings about unused parameters, and check the\r
233         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
234         write buffer length is adequate, so does not check for buffer overflows. */\r
235         ( void ) pcCommandString;\r
236         ( void ) xWriteBufferLen;\r
237         configASSERT( pcWriteBuffer );\r
238 \r
239         if( lParameterNumber == 0 )\r
240         {\r
241                 /* The first time the function is called after the command has been\r
242                 entered just a header string is returned. */\r
243                 sprintf( ( char * ) pcWriteBuffer, "The three parameters were:\r\n" );\r
244 \r
245                 /* Next time the function is called the first parameter will be echoed\r
246                 back. */\r
247                 lParameterNumber = 1L;\r
248 \r
249                 /* There is more data to be returned as no parameters have been echoed\r
250                 back yet. */\r
251                 xReturn = pdPASS;\r
252         }\r
253         else\r
254         {\r
255                 /* Obtain the parameter string. */\r
256                 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter\r
257                                                                         (\r
258                                                                                 pcCommandString,                /* The command string itself. */\r
259                                                                                 lParameterNumber,               /* Return the next parameter. */\r
260                                                                                 &lParameterStringLength /* Store the parameter string length. */\r
261                                                                         );\r
262 \r
263                 /* Sanity check something was returned. */\r
264                 configASSERT( pcParameter );\r
265 \r
266                 /* Return the parameter string. */\r
267                 memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
268                 sprintf( ( char * ) pcWriteBuffer, "%d: ", lParameterNumber );\r
269                 strncat( ( char * ) pcWriteBuffer, ( const char * ) pcParameter, lParameterStringLength );\r
270                 strncat( ( char * ) pcWriteBuffer, "\r\n", strlen( "\r\n" ) );\r
271 \r
272                 /* If this is the last of the three parameters then there are no more\r
273                 strings to return after this one. */\r
274                 if( lParameterNumber == 3L )\r
275                 {\r
276                         /* If this is the last of the three parameters then there are no more\r
277                         strings to return after this one. */\r
278                         xReturn = pdFALSE;\r
279                         lParameterNumber = 0L;\r
280                 }\r
281                 else\r
282                 {\r
283                         /* There are more parameters to return after this one. */\r
284                         xReturn = pdTRUE;\r
285                         lParameterNumber++;\r
286                 }\r
287         }\r
288 \r
289         return xReturn;\r
290 }\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 static portBASE_TYPE prvParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
294 {\r
295 int8_t *pcParameter;\r
296 portBASE_TYPE lParameterStringLength, xReturn;\r
297 static portBASE_TYPE lParameterNumber = 0;\r
298 \r
299         /* Remove compile time warnings about unused parameters, and check the\r
300         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
301         write buffer length is adequate, so does not check for buffer overflows. */\r
302         ( void ) pcCommandString;\r
303         ( void ) xWriteBufferLen;\r
304         configASSERT( pcWriteBuffer );\r
305 \r
306         if( lParameterNumber == 0 )\r
307         {\r
308                 /* The first time the function is called after the command has been\r
309                 entered just a header string is returned. */\r
310                 sprintf( ( char * ) pcWriteBuffer, "The parameters were:\r\n" );\r
311 \r
312                 /* Next time the function is called the first parameter will be echoed\r
313                 back. */\r
314                 lParameterNumber = 1L;\r
315 \r
316                 /* There is more data to be returned as no parameters have been echoed\r
317                 back yet. */\r
318                 xReturn = pdPASS;\r
319         }\r
320         else\r
321         {\r
322                 /* Obtain the parameter string. */\r
323                 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter\r
324                                                                         (\r
325                                                                                 pcCommandString,                /* The command string itself. */\r
326                                                                                 lParameterNumber,               /* Return the next parameter. */\r
327                                                                                 &lParameterStringLength /* Store the parameter string length. */\r
328                                                                         );\r
329 \r
330                 if( pcParameter != NULL )\r
331                 {\r
332                         /* Return the parameter string. */\r
333                         memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
334                         sprintf( ( char * ) pcWriteBuffer, "%d: ", lParameterNumber );\r
335                         strncat( ( char * ) pcWriteBuffer, ( const char * ) pcParameter, lParameterStringLength );\r
336                         strncat( ( char * ) pcWriteBuffer, "\r\n", strlen( "\r\n" ) );\r
337 \r
338                         /* There might be more parameters to return after this one. */\r
339                         xReturn = pdTRUE;\r
340                         lParameterNumber++;\r
341                 }\r
342                 else\r
343                 {\r
344                         /* No more parameters were found.  Make sure the write buffer does\r
345                         not contain a valid string. */\r
346                         pcWriteBuffer[ 0 ] = 0x00;\r
347 \r
348                         /* No more data to return. */\r
349                         xReturn = pdFALSE;\r
350 \r
351                         /* Start over the next time this command is executed. */\r
352                         lParameterNumber = 0;\r
353                 }\r
354         }\r
355 \r
356         return xReturn;\r
357 }\r
358 /*-----------------------------------------------------------*/\r
359 \r
360 static portBASE_TYPE prvStartStopTraceCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
361 {\r
362 int8_t *pcParameter;\r
363 portBASE_TYPE lParameterStringLength;\r
364 \r
365         /* Remove compile time warnings about unused parameters, and check the\r
366         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
367         write buffer length is adequate, so does not check for buffer overflows. */\r
368         ( void ) pcCommandString;\r
369         ( void ) xWriteBufferLen;\r
370         configASSERT( pcWriteBuffer );\r
371 \r
372         /* Obtain the parameter string. */\r
373         pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter\r
374                                                                 (\r
375                                                                         pcCommandString,                /* The command string itself. */\r
376                                                                         1,                                              /* Return the first parameter. */\r
377                                                                         &lParameterStringLength /* Store the parameter string length. */\r
378                                                                 );\r
379 \r
380         /* Sanity check something was returned. */\r
381         configASSERT( pcParameter );\r
382 \r
383         /* There are only two valid parameter values. */\r
384         if( strncmp( ( const char * ) pcParameter, "start", strlen( "start" ) ) == 0 )\r
385         {\r
386                 /* Start or restart the trace. */\r
387                 vTraceStop();\r
388                 vTraceClear();\r
389                 uiTraceStart();\r
390 \r
391                 sprintf( ( char * ) pcWriteBuffer, "Trace recording (re)started.\r\n" );\r
392         }\r
393         else if( strncmp( ( const char * ) pcParameter, "stop", strlen( "stop" ) ) == 0 )\r
394         {\r
395                 /* End the trace, if one is running. */\r
396                 vTraceStop();\r
397                 sprintf( ( char * ) pcWriteBuffer, "Stopping trace recording and dumping log to disk.\r\n" );\r
398                 prvSaveTraceFile();\r
399         }\r
400         else\r
401         {\r
402                 sprintf( ( char * ) pcWriteBuffer, "Valid parameters are 'start' and 'stop'.\r\n" );\r
403         }\r
404 \r
405         /* There is no more data to return after this single string, so return\r
406         pdFALSE. */\r
407         return pdFALSE;\r
408 }\r
409 /*-----------------------------------------------------------*/\r
410 \r
411 static void prvSaveTraceFile( void )\r
412 {\r
413 FILE* pxOutputFile;\r
414 \r
415         fopen_s( &pxOutputFile, "Trace.dump", "wb");\r
416 \r
417         if( pxOutputFile != NULL )\r
418         {\r
419                 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );\r
420                 fclose( pxOutputFile );\r
421                 printf( "\r\nTrace output saved to Trace.dump\r\n" );\r
422         }\r
423         else\r
424         {\r
425                 printf( "\r\nFailed to create trace dump file\r\n" );\r
426         }\r
427 }\r
428 \r