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