]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/Common/FreeRTOS_Plus_CLI_Demos/Sample-CLI-commands.c
General maintenance - changing comments and correcting spellings only.
[freertos] / FreeRTOS-Plus / Demo / Common / FreeRTOS_Plus_CLI_Demos / Sample-CLI-commands.c
1 /*\r
2     FreeRTOS V8.0.1 - 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     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS 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 \r
67  /******************************************************************************\r
68  *\r
69  * See the following URL for information on the commands defined in this file:\r
70  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/Ethernet_Related_CLI_Commands.shtml\r
71  *\r
72  ******************************************************************************/\r
73 \r
74 \r
75 /* FreeRTOS includes. */\r
76 #include "FreeRTOS.h"\r
77 #include "task.h"\r
78 \r
79 /* Standard includes. */\r
80 #include <stdint.h>\r
81 #include <stdio.h>\r
82 #include <stdlib.h>\r
83 #include <string.h>\r
84 \r
85 /* FreeRTOS+CLI includes. */\r
86 #include "FreeRTOS_CLI.h"\r
87 \r
88 #ifndef  configINCLUDE_TRACE_RELATED_CLI_COMMANDS\r
89         #define configINCLUDE_TRACE_RELATED_CLI_COMMANDS 0\r
90 #endif\r
91 \r
92 /*\r
93  * The function that registers the commands that are defined within this file.\r
94  */\r
95 void vRegisterSampleCLICommands( void );\r
96 \r
97 /*\r
98  * Implements the task-stats command.\r
99  */\r
100 static BaseType_t prvTaskStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
101 \r
102 /*\r
103  * Implements the run-time-stats command.\r
104  */\r
105 static BaseType_t prvRunTimeStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
106 \r
107 /*\r
108  * Implements the echo-three-parameters command.\r
109  */\r
110 static BaseType_t prvThreeParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
111 \r
112 /*\r
113  * Implements the echo-parameters command.\r
114  */\r
115 static BaseType_t prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
116 \r
117 /*\r
118  * Implements the "trace start" and "trace stop" commands;\r
119  */\r
120 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
121         static BaseType_t prvStartStopTraceCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
122 #endif\r
123 \r
124 /* Structure that defines the "run-time-stats" command line command.   This\r
125 generates a table that shows how much run time each task has */\r
126 static const CLI_Command_Definition_t xRunTimeStats =\r
127 {\r
128         "run-time-stats", /* The command string to type. */\r
129         "\r\nrun-time-stats:\r\n Displays a table showing how much processing time each FreeRTOS task has used\r\n",\r
130         prvRunTimeStatsCommand, /* The function to run. */\r
131         0 /* No parameters are expected. */\r
132 };\r
133 \r
134 /* Structure that defines the "task-stats" command line command.  This generates\r
135 a table that gives information on each task in the system. */\r
136 static const CLI_Command_Definition_t xTaskStats =\r
137 {\r
138         "task-stats", /* The command string to type. */\r
139         "\r\ntask-stats:\r\n Displays a table showing the state of each FreeRTOS task\r\n",\r
140         prvTaskStatsCommand, /* The function to run. */\r
141         0 /* No parameters are expected. */\r
142 };\r
143 \r
144 /* Structure that defines the "echo_3_parameters" command line command.  This\r
145 takes exactly three parameters that the command simply echos back one at a\r
146 time. */\r
147 static const CLI_Command_Definition_t xThreeParameterEcho =\r
148 {\r
149         "echo-3-parameters",\r
150         "\r\necho-3-parameters <param1> <param2> <param3>:\r\n Expects three parameters, echos each in turn\r\n",\r
151         prvThreeParameterEchoCommand, /* The function to run. */\r
152         3 /* Three parameters are expected, which can take any value. */\r
153 };\r
154 \r
155 /* Structure that defines the "echo_parameters" command line command.  This\r
156 takes a variable number of parameters that the command simply echos back one at\r
157 a time. */\r
158 static const CLI_Command_Definition_t xParameterEcho =\r
159 {\r
160         "echo-parameters",\r
161         "\r\necho-parameters <...>:\r\n Take variable number of parameters, echos each in turn\r\n",\r
162         prvParameterEchoCommand, /* The function to run. */\r
163         -1 /* The user can enter any number of commands. */\r
164 };\r
165 \r
166 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
167         /* Structure that defines the "trace" command line command.  This takes a single\r
168         parameter, which can be either "start" or "stop". */\r
169         static const CLI_Command_Definition_t xStartStopTrace =\r
170         {\r
171                 "trace",\r
172                 "\r\ntrace [start | stop]:\r\n Starts or stops a trace recording for viewing in FreeRTOS+Trace\r\n",\r
173                 prvStartStopTraceCommand, /* The function to run. */\r
174                 1 /* One parameter is expected.  Valid values are "start" and "stop". */\r
175         };\r
176 #endif /* configINCLUDE_TRACE_RELATED_CLI_COMMANDS */\r
177 \r
178 /*-----------------------------------------------------------*/\r
179 \r
180 void vRegisterSampleCLICommands( void )\r
181 {\r
182         /* Register all the command line commands defined immediately above. */\r
183         FreeRTOS_CLIRegisterCommand( &xTaskStats );\r
184         FreeRTOS_CLIRegisterCommand( &xRunTimeStats );\r
185         FreeRTOS_CLIRegisterCommand( &xThreeParameterEcho );\r
186         FreeRTOS_CLIRegisterCommand( &xParameterEcho );\r
187 \r
188         #if( configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1 )\r
189         {\r
190                 FreeRTOS_CLIRegisterCommand( & xStartStopTrace );\r
191         }\r
192         #endif\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 static BaseType_t prvTaskStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
197 {\r
198 const char *const pcHeader = "Task          State  Priority  Stack      #\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         vTaskList( 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 BaseType_t prvRunTimeStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
218 {\r
219 const char * const pcHeader = "Task            Abs Time      % Time\r\n****************************************\r\n";\r
220 \r
221         /* Remove compile time warnings about unused parameters, and check the\r
222         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
223         write buffer length is adequate, so does not check for buffer overflows. */\r
224         ( void ) pcCommandString;\r
225         ( void ) xWriteBufferLen;\r
226         configASSERT( pcWriteBuffer );\r
227 \r
228         /* Generate a table of task stats. */\r
229         strcpy( pcWriteBuffer, pcHeader );\r
230         vTaskGetRunTimeStats( pcWriteBuffer + strlen( pcHeader ) );\r
231 \r
232         /* There is no more data to return after this single string, so return\r
233         pdFALSE. */\r
234         return pdFALSE;\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 static BaseType_t prvThreeParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
239 {\r
240 const char *pcParameter;\r
241 BaseType_t xParameterStringLength, xReturn;\r
242 static BaseType_t lParameterNumber = 0;\r
243 \r
244         /* Remove compile time warnings about unused parameters, and check the\r
245         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
246         write buffer length is adequate, so does not check for buffer overflows. */\r
247         ( void ) pcCommandString;\r
248         ( void ) xWriteBufferLen;\r
249         configASSERT( pcWriteBuffer );\r
250 \r
251         if( lParameterNumber == 0 )\r
252         {\r
253                 /* The first time the function is called after the command has been\r
254                 entered just a header string is returned. */\r
255                 sprintf( pcWriteBuffer, "The three parameters were:\r\n" );\r
256 \r
257                 /* Next time the function is called the first parameter will be echoed\r
258                 back. */\r
259                 lParameterNumber = 1L;\r
260 \r
261                 /* There is more data to be returned as no parameters have been echoed\r
262                 back yet. */\r
263                 xReturn = pdPASS;\r
264         }\r
265         else\r
266         {\r
267                 /* Obtain the parameter string. */\r
268                 pcParameter = FreeRTOS_CLIGetParameter\r
269                                                 (\r
270                                                         pcCommandString,                /* The command string itself. */\r
271                                                         lParameterNumber,               /* Return the next parameter. */\r
272                                                         &xParameterStringLength /* Store the parameter string length. */\r
273                                                 );\r
274 \r
275                 /* Sanity check something was returned. */\r
276                 configASSERT( pcParameter );\r
277 \r
278                 /* Return the parameter string. */\r
279                 memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
280                 sprintf( pcWriteBuffer, "%d: ", ( int ) lParameterNumber );\r
281                 strncat( pcWriteBuffer, pcParameter, xParameterStringLength );\r
282                 strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );\r
283 \r
284                 /* If this is the last of the three parameters then there are no more\r
285                 strings to return after this one. */\r
286                 if( lParameterNumber == 3L )\r
287                 {\r
288                         /* If this is the last of the three parameters then there are no more\r
289                         strings to return after this one. */\r
290                         xReturn = pdFALSE;\r
291                         lParameterNumber = 0L;\r
292                 }\r
293                 else\r
294                 {\r
295                         /* There are more parameters to return after this one. */\r
296                         xReturn = pdTRUE;\r
297                         lParameterNumber++;\r
298                 }\r
299         }\r
300 \r
301         return xReturn;\r
302 }\r
303 /*-----------------------------------------------------------*/\r
304 \r
305 static BaseType_t prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
306 {\r
307 const char *pcParameter;\r
308 BaseType_t xParameterStringLength, xReturn;\r
309 static BaseType_t lParameterNumber = 0;\r
310 \r
311         /* Remove compile time warnings about unused parameters, and check the\r
312         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
313         write buffer length is adequate, so does not check for buffer overflows. */\r
314         ( void ) pcCommandString;\r
315         ( void ) xWriteBufferLen;\r
316         configASSERT( pcWriteBuffer );\r
317 \r
318         if( lParameterNumber == 0 )\r
319         {\r
320                 /* The first time the function is called after the command has been\r
321                 entered just a header string is returned. */\r
322                 sprintf( pcWriteBuffer, "The parameters were:\r\n" );\r
323 \r
324                 /* Next time the function is called the first parameter will be echoed\r
325                 back. */\r
326                 lParameterNumber = 1L;\r
327 \r
328                 /* There is more data to be returned as no parameters have been echoed\r
329                 back yet. */\r
330                 xReturn = pdPASS;\r
331         }\r
332         else\r
333         {\r
334                 /* Obtain the parameter string. */\r
335                 pcParameter = FreeRTOS_CLIGetParameter\r
336                                                 (\r
337                                                         pcCommandString,                /* The command string itself. */\r
338                                                         lParameterNumber,               /* Return the next parameter. */\r
339                                                         &xParameterStringLength /* Store the parameter string length. */\r
340                                                 );\r
341 \r
342                 if( pcParameter != NULL )\r
343                 {\r
344                         /* Return the parameter string. */\r
345                         memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
346                         sprintf( pcWriteBuffer, "%d: ", ( int ) lParameterNumber );\r
347                         strncat( pcWriteBuffer, pcParameter, xParameterStringLength );\r
348                         strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );\r
349 \r
350                         /* There might be more parameters to return after this one. */\r
351                         xReturn = pdTRUE;\r
352                         lParameterNumber++;\r
353                 }\r
354                 else\r
355                 {\r
356                         /* No more parameters were found.  Make sure the write buffer does\r
357                         not contain a valid string. */\r
358                         pcWriteBuffer[ 0 ] = 0x00;\r
359 \r
360                         /* No more data to return. */\r
361                         xReturn = pdFALSE;\r
362 \r
363                         /* Start over the next time this command is executed. */\r
364                         lParameterNumber = 0;\r
365                 }\r
366         }\r
367 \r
368         return xReturn;\r
369 }\r
370 /*-----------------------------------------------------------*/\r
371 \r
372 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
373 \r
374         static BaseType_t prvStartStopTraceCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
375         {\r
376         const char *pcParameter;\r
377         BaseType_t lParameterStringLength;\r
378 \r
379                 /* Remove compile time warnings about unused parameters, and check the\r
380                 write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
381                 write buffer length is adequate, so does not check for buffer overflows. */\r
382                 ( void ) pcCommandString;\r
383                 ( void ) xWriteBufferLen;\r
384                 configASSERT( pcWriteBuffer );\r
385 \r
386                 /* Obtain the parameter string. */\r
387                 pcParameter = FreeRTOS_CLIGetParameter\r
388                                                 (\r
389                                                         pcCommandString,                /* The command string itself. */\r
390                                                         1,                                              /* Return the first parameter. */\r
391                                                         &lParameterStringLength /* Store the parameter string length. */\r
392                                                 );\r
393 \r
394                 /* Sanity check something was returned. */\r
395                 configASSERT( pcParameter );\r
396 \r
397                 /* There are only two valid parameter values. */\r
398                 if( strncmp( pcParameter, "start", strlen( "start" ) ) == 0 )\r
399                 {\r
400                         /* Start or restart the trace. */\r
401                         vTraceStop();\r
402                         vTraceClear();\r
403                         vTraceStart();\r
404 \r
405                         sprintf( pcWriteBuffer, "Trace recording (re)started.\r\n" );\r
406                 }\r
407                 else if( strncmp( pcParameter, "stop", strlen( "stop" ) ) == 0 )\r
408                 {\r
409                         /* End the trace, if one is running. */\r
410                         vTraceStop();\r
411                         sprintf( pcWriteBuffer, "Stopping trace recording.\r\n" );\r
412                 }\r
413                 else\r
414                 {\r
415                         sprintf( pcWriteBuffer, "Valid parameters are 'start' and 'stop'.\r\n" );\r
416                 }\r
417 \r
418                 /* There is no more data to return after this single string, so return\r
419                 pdFALSE. */\r
420                 return pdFALSE;\r
421         }\r
422 \r
423 #endif /* configINCLUDE_TRACE_RELATED_CLI_COMMANDS */\r