]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/Common/FreeRTOS_Plus_CLI_Demos/Sample-CLI-commands.c
Kernel updates:
[freertos] / FreeRTOS-Plus / Demo / Common / FreeRTOS_Plus_CLI_Demos / Sample-CLI-commands.c
1 /*\r
2     FreeRTOS V8.2.0 - Copyright (C) 2015 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     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13         ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18         ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40         the FAQ page "My application does not run, what could be wrong?".  Have you\r
41         defined configASSERT()?\r
42 \r
43         http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44         embedded software for free we request you assist our global community by\r
45         participating in the support forum.\r
46 \r
47         http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48         be as productive as possible as early as possible.  Now you can receive\r
49         FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50         Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71  /******************************************************************************\r
72  *\r
73  * See the following URL for information on the commands defined in this file:\r
74  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/Ethernet_Related_CLI_Commands.shtml\r
75  *\r
76  ******************************************************************************/\r
77 \r
78 \r
79 /* FreeRTOS includes. */\r
80 #include "FreeRTOS.h"\r
81 #include "task.h"\r
82 \r
83 /* Standard includes. */\r
84 #include <stdint.h>\r
85 #include <stdio.h>\r
86 #include <stdlib.h>\r
87 #include <string.h>\r
88 \r
89 /* FreeRTOS+CLI includes. */\r
90 #include "FreeRTOS_CLI.h"\r
91 \r
92 #ifndef  configINCLUDE_TRACE_RELATED_CLI_COMMANDS\r
93         #define configINCLUDE_TRACE_RELATED_CLI_COMMANDS 0\r
94 #endif\r
95 \r
96 #ifndef configINCLUDE_QUERY_HEAP_COMMAND\r
97         #define configINCLUDE_QUERY_HEAP_COMMAND 0\r
98 #endif\r
99 \r
100 /*\r
101  * The function that registers the commands that are defined within this file.\r
102  */\r
103 void vRegisterSampleCLICommands( void );\r
104 \r
105 /*\r
106  * Implements the task-stats command.\r
107  */\r
108 static BaseType_t prvTaskStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
109 \r
110 /*\r
111  * Implements the run-time-stats command.\r
112  */\r
113 static BaseType_t prvRunTimeStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
114 \r
115 /*\r
116  * Implements the echo-three-parameters command.\r
117  */\r
118 static BaseType_t prvThreeParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
119 \r
120 /*\r
121  * Implements the echo-parameters command.\r
122  */\r
123 static BaseType_t prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
124 \r
125 /*\r
126  * Implements the "query heap" command.\r
127  */\r
128 #if( configINCLUDE_QUERY_HEAP_COMMAND == 1 )\r
129         static BaseType_t prvQueryHeapCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
130 #endif\r
131 \r
132 /*\r
133  * Implements the "trace start" and "trace stop" commands;\r
134  */\r
135 #if( configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1 )\r
136         static BaseType_t prvStartStopTraceCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
137 #endif\r
138 \r
139 /* Structure that defines the "run-time-stats" command line command.   This\r
140 generates a table that shows how much run time each task has */\r
141 static const CLI_Command_Definition_t xRunTimeStats =\r
142 {\r
143         "run-time-stats", /* The command string to type. */\r
144         "\r\nrun-time-stats:\r\n Displays a table showing how much processing time each FreeRTOS task has used\r\n",\r
145         prvRunTimeStatsCommand, /* The function to run. */\r
146         0 /* No parameters are expected. */\r
147 };\r
148 \r
149 /* Structure that defines the "task-stats" command line command.  This generates\r
150 a table that gives information on each task in the system. */\r
151 static const CLI_Command_Definition_t xTaskStats =\r
152 {\r
153         "task-stats", /* The command string to type. */\r
154         "\r\ntask-stats:\r\n Displays a table showing the state of each FreeRTOS task\r\n",\r
155         prvTaskStatsCommand, /* The function to run. */\r
156         0 /* No parameters are expected. */\r
157 };\r
158 \r
159 /* Structure that defines the "echo_3_parameters" command line command.  This\r
160 takes exactly three parameters that the command simply echos back one at a\r
161 time. */\r
162 static const CLI_Command_Definition_t xThreeParameterEcho =\r
163 {\r
164         "echo-3-parameters",\r
165         "\r\necho-3-parameters <param1> <param2> <param3>:\r\n Expects three parameters, echos each in turn\r\n",\r
166         prvThreeParameterEchoCommand, /* The function to run. */\r
167         3 /* Three parameters are expected, which can take any value. */\r
168 };\r
169 \r
170 /* Structure that defines the "echo_parameters" command line command.  This\r
171 takes a variable number of parameters that the command simply echos back one at\r
172 a time. */\r
173 static const CLI_Command_Definition_t xParameterEcho =\r
174 {\r
175         "echo-parameters",\r
176         "\r\necho-parameters <...>:\r\n Take variable number of parameters, echos each in turn\r\n",\r
177         prvParameterEchoCommand, /* The function to run. */\r
178         -1 /* The user can enter any number of commands. */\r
179 };\r
180 \r
181 #if( configINCLUDE_QUERY_HEAP_COMMAND == 1 )\r
182         /* Structure that defines the "query_heap" command line command. */\r
183         static const CLI_Command_Definition_t xQueryHeap =\r
184         {\r
185                 "query-heap",\r
186                 "\r\nquery-heap:\r\n Displays the free heap space, and minimum ever free heap space.\r\n",\r
187                 prvQueryHeapCommand, /* The function to run. */\r
188                 0 /* The user can enter any number of commands. */\r
189         };\r
190 #endif /* configQUERY_HEAP_COMMAND */\r
191 \r
192 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
193         /* Structure that defines the "trace" command line command.  This takes a single\r
194         parameter, which can be either "start" or "stop". */\r
195         static const CLI_Command_Definition_t xStartStopTrace =\r
196         {\r
197                 "trace",\r
198                 "\r\ntrace [start | stop]:\r\n Starts or stops a trace recording for viewing in FreeRTOS+Trace\r\n",\r
199                 prvStartStopTraceCommand, /* The function to run. */\r
200                 1 /* One parameter is expected.  Valid values are "start" and "stop". */\r
201         };\r
202 #endif /* configINCLUDE_TRACE_RELATED_CLI_COMMANDS */\r
203 \r
204 /*-----------------------------------------------------------*/\r
205 \r
206 void vRegisterSampleCLICommands( void )\r
207 {\r
208         /* Register all the command line commands defined immediately above. */\r
209         FreeRTOS_CLIRegisterCommand( &xTaskStats );\r
210         FreeRTOS_CLIRegisterCommand( &xRunTimeStats );\r
211         FreeRTOS_CLIRegisterCommand( &xThreeParameterEcho );\r
212         FreeRTOS_CLIRegisterCommand( &xParameterEcho );\r
213 \r
214         #if( configINCLUDE_QUERY_HEAP_COMMAND == 1 )\r
215         {\r
216                 FreeRTOS_CLIRegisterCommand( &xQueryHeap );\r
217         }\r
218         #endif\r
219 \r
220         #if( configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1 )\r
221         {\r
222                 FreeRTOS_CLIRegisterCommand( &xStartStopTrace );\r
223         }\r
224         #endif\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 static BaseType_t prvTaskStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
229 {\r
230 const char *const pcHeader = "       State  Priority  Stack    #\r\n************************************************\r\n";\r
231 BaseType_t xSpacePadding;\r
232 \r
233         /* Remove compile time warnings about unused parameters, and check the\r
234         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
235         write buffer length is adequate, so does not check for buffer overflows. */\r
236         ( void ) pcCommandString;\r
237         ( void ) xWriteBufferLen;\r
238         configASSERT( pcWriteBuffer );\r
239 \r
240         /* Generate a table of task stats. */\r
241         strcpy( pcWriteBuffer, "Task" );\r
242         pcWriteBuffer += strlen( pcWriteBuffer );\r
243 \r
244         /* Minus three for the null terminator and half the number of characters in\r
245         "Task" so the column lines up with the centre of the heading. */\r
246         configASSERT( configMAX_TASK_NAME_LEN > 3 );\r
247         for( xSpacePadding = strlen( "Task" ); xSpacePadding < ( configMAX_TASK_NAME_LEN - 3 ); xSpacePadding++ )\r
248         {\r
249                 /* Add a space to align columns after the task's name. */\r
250                 *pcWriteBuffer = ' ';\r
251                 pcWriteBuffer++;\r
252 \r
253                 /* Ensure always terminated. */\r
254                 *pcWriteBuffer = 0x00;\r
255         }\r
256         strcpy( pcWriteBuffer, pcHeader );\r
257         vTaskList( pcWriteBuffer + strlen( pcHeader ) );\r
258 \r
259         /* There is no more data to return after this single string, so return\r
260         pdFALSE. */\r
261         return pdFALSE;\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 #if( configINCLUDE_QUERY_HEAP_COMMAND == 1 )\r
266 \r
267         static BaseType_t prvQueryHeapCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
268         {\r
269                 /* Remove compile time warnings about unused parameters, and check the\r
270                 write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
271                 write buffer length is adequate, so does not check for buffer overflows. */\r
272                 ( void ) pcCommandString;\r
273                 ( void ) xWriteBufferLen;\r
274                 configASSERT( pcWriteBuffer );\r
275 \r
276                 sprintf( pcWriteBuffer, "Current free heap %d bytes, minimum ever free heap %d bytes\r\n", ( int ) xPortGetFreeHeapSize(), ( int ) xPortGetMinimumEverFreeHeapSize() );\r
277 \r
278                 /* There is no more data to return after this single string, so return\r
279                 pdFALSE. */\r
280                 return pdFALSE;\r
281         }\r
282 \r
283 #endif /* configINCLUDE_QUERY_HEAP */\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 static BaseType_t prvRunTimeStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
287 {\r
288 const char * const pcHeader = "  Abs Time      % Time\r\n****************************************\r\n";\r
289 BaseType_t xSpacePadding;\r
290 \r
291         /* Remove compile time warnings about unused parameters, and check the\r
292         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
293         write buffer length is adequate, so does not check for buffer overflows. */\r
294         ( void ) pcCommandString;\r
295         ( void ) xWriteBufferLen;\r
296         configASSERT( pcWriteBuffer );\r
297 \r
298         /* Generate a table of task stats. */\r
299         strcpy( pcWriteBuffer, "Task" );\r
300         pcWriteBuffer += strlen( pcWriteBuffer );\r
301 \r
302         /* Pad the string "task" with however many bytes necessary to make it the\r
303         length of a task name.  Minus three for the null terminator and half the \r
304         number of characters in "Task" so the column lines up with the centre of \r
305         the heading. */\r
306         for( xSpacePadding = strlen( "Task" ); xSpacePadding < ( configMAX_TASK_NAME_LEN - 3 ); xSpacePadding++ )\r
307         {\r
308                 /* Add a space to align columns after the task's name. */\r
309                 *pcWriteBuffer = ' ';\r
310                 pcWriteBuffer++;\r
311 \r
312                 /* Ensure always terminated. */\r
313                 *pcWriteBuffer = 0x00;\r
314         }\r
315 \r
316         strcpy( pcWriteBuffer, pcHeader );\r
317         vTaskGetRunTimeStats( pcWriteBuffer + strlen( pcHeader ) );\r
318 \r
319         /* There is no more data to return after this single string, so return\r
320         pdFALSE. */\r
321         return pdFALSE;\r
322 }\r
323 /*-----------------------------------------------------------*/\r
324 \r
325 static BaseType_t prvThreeParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
326 {\r
327 const char *pcParameter;\r
328 BaseType_t xParameterStringLength, xReturn;\r
329 static BaseType_t lParameterNumber = 0;\r
330 \r
331         /* Remove compile time warnings about unused parameters, and check the\r
332         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
333         write buffer length is adequate, so does not check for buffer overflows. */\r
334         ( void ) pcCommandString;\r
335         ( void ) xWriteBufferLen;\r
336         configASSERT( pcWriteBuffer );\r
337 \r
338         if( lParameterNumber == 0 )\r
339         {\r
340                 /* The first time the function is called after the command has been\r
341                 entered just a header string is returned. */\r
342                 sprintf( pcWriteBuffer, "The three parameters were:\r\n" );\r
343 \r
344                 /* Next time the function is called the first parameter will be echoed\r
345                 back. */\r
346                 lParameterNumber = 1L;\r
347 \r
348                 /* There is more data to be returned as no parameters have been echoed\r
349                 back yet. */\r
350                 xReturn = pdPASS;\r
351         }\r
352         else\r
353         {\r
354                 /* Obtain the parameter string. */\r
355                 pcParameter = FreeRTOS_CLIGetParameter\r
356                                                 (\r
357                                                         pcCommandString,                /* The command string itself. */\r
358                                                         lParameterNumber,               /* Return the next parameter. */\r
359                                                         &xParameterStringLength /* Store the parameter string length. */\r
360                                                 );\r
361 \r
362                 /* Sanity check something was returned. */\r
363                 configASSERT( pcParameter );\r
364 \r
365                 /* Return the parameter string. */\r
366                 memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
367                 sprintf( pcWriteBuffer, "%d: ", ( int ) lParameterNumber );\r
368                 strncat( pcWriteBuffer, pcParameter, xParameterStringLength );\r
369                 strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );\r
370 \r
371                 /* If this is the last of the three parameters then there are no more\r
372                 strings to return after this one. */\r
373                 if( lParameterNumber == 3L )\r
374                 {\r
375                         /* If this is the last of the three parameters then there are no more\r
376                         strings to return after this one. */\r
377                         xReturn = pdFALSE;\r
378                         lParameterNumber = 0L;\r
379                 }\r
380                 else\r
381                 {\r
382                         /* There are more parameters to return after this one. */\r
383                         xReturn = pdTRUE;\r
384                         lParameterNumber++;\r
385                 }\r
386         }\r
387 \r
388         return xReturn;\r
389 }\r
390 /*-----------------------------------------------------------*/\r
391 \r
392 static BaseType_t prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
393 {\r
394 const char *pcParameter;\r
395 BaseType_t xParameterStringLength, xReturn;\r
396 static BaseType_t lParameterNumber = 0;\r
397 \r
398         /* Remove compile time warnings about unused parameters, and check the\r
399         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
400         write buffer length is adequate, so does not check for buffer overflows. */\r
401         ( void ) pcCommandString;\r
402         ( void ) xWriteBufferLen;\r
403         configASSERT( pcWriteBuffer );\r
404 \r
405         if( lParameterNumber == 0 )\r
406         {\r
407                 /* The first time the function is called after the command has been\r
408                 entered just a header string is returned. */\r
409                 sprintf( pcWriteBuffer, "The parameters were:\r\n" );\r
410 \r
411                 /* Next time the function is called the first parameter will be echoed\r
412                 back. */\r
413                 lParameterNumber = 1L;\r
414 \r
415                 /* There is more data to be returned as no parameters have been echoed\r
416                 back yet. */\r
417                 xReturn = pdPASS;\r
418         }\r
419         else\r
420         {\r
421                 /* Obtain the parameter string. */\r
422                 pcParameter = FreeRTOS_CLIGetParameter\r
423                                                 (\r
424                                                         pcCommandString,                /* The command string itself. */\r
425                                                         lParameterNumber,               /* Return the next parameter. */\r
426                                                         &xParameterStringLength /* Store the parameter string length. */\r
427                                                 );\r
428 \r
429                 if( pcParameter != NULL )\r
430                 {\r
431                         /* Return the parameter string. */\r
432                         memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
433                         sprintf( pcWriteBuffer, "%d: ", ( int ) lParameterNumber );\r
434                         strncat( pcWriteBuffer, pcParameter, xParameterStringLength );\r
435                         strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );\r
436 \r
437                         /* There might be more parameters to return after this one. */\r
438                         xReturn = pdTRUE;\r
439                         lParameterNumber++;\r
440                 }\r
441                 else\r
442                 {\r
443                         /* No more parameters were found.  Make sure the write buffer does\r
444                         not contain a valid string. */\r
445                         pcWriteBuffer[ 0 ] = 0x00;\r
446 \r
447                         /* No more data to return. */\r
448                         xReturn = pdFALSE;\r
449 \r
450                         /* Start over the next time this command is executed. */\r
451                         lParameterNumber = 0;\r
452                 }\r
453         }\r
454 \r
455         return xReturn;\r
456 }\r
457 /*-----------------------------------------------------------*/\r
458 \r
459 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
460 \r
461         static BaseType_t prvStartStopTraceCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
462         {\r
463         const char *pcParameter;\r
464         BaseType_t lParameterStringLength;\r
465 \r
466                 /* Remove compile time warnings about unused parameters, and check the\r
467                 write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
468                 write buffer length is adequate, so does not check for buffer overflows. */\r
469                 ( void ) pcCommandString;\r
470                 ( void ) xWriteBufferLen;\r
471                 configASSERT( pcWriteBuffer );\r
472 \r
473                 /* Obtain the parameter string. */\r
474                 pcParameter = FreeRTOS_CLIGetParameter\r
475                                                 (\r
476                                                         pcCommandString,                /* The command string itself. */\r
477                                                         1,                                              /* Return the first parameter. */\r
478                                                         &lParameterStringLength /* Store the parameter string length. */\r
479                                                 );\r
480 \r
481                 /* Sanity check something was returned. */\r
482                 configASSERT( pcParameter );\r
483 \r
484                 /* There are only two valid parameter values. */\r
485                 if( strncmp( pcParameter, "start", strlen( "start" ) ) == 0 )\r
486                 {\r
487                         /* Start or restart the trace. */\r
488                         vTraceStop();\r
489                         vTraceClear();\r
490                         vTraceStart();\r
491 \r
492                         sprintf( pcWriteBuffer, "Trace recording (re)started.\r\n" );\r
493                 }\r
494                 else if( strncmp( pcParameter, "stop", strlen( "stop" ) ) == 0 )\r
495                 {\r
496                         /* End the trace, if one is running. */\r
497                         vTraceStop();\r
498                         sprintf( pcWriteBuffer, "Stopping trace recording.\r\n" );\r
499                 }\r
500                 else\r
501                 {\r
502                         sprintf( pcWriteBuffer, "Valid parameters are 'start' and 'stop'.\r\n" );\r
503                 }\r
504 \r
505                 /* There is no more data to return after this single string, so return\r
506                 pdFALSE. */\r
507                 return pdFALSE;\r
508         }\r
509 \r
510 #endif /* configINCLUDE_TRACE_RELATED_CLI_COMMANDS */\r