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