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