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