]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/Source/Full-Demo/Sample-CLI-commands.c
27200b3071a6709be40150f1835b7f57e8ce534d
[freertos] / FreeRTOS / Demo / CORTEX_A9_RZ_R7S72100_IAR_DS-5 / Source / Full-Demo / Sample-CLI-commands.c
1 /*\r
2     FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
6     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
7 \r
8     ***************************************************************************\r
9      *                                                                       *\r
10      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
11      *    Complete, revised, and edited pdf reference manuals are also       *\r
12      *    available.                                                         *\r
13      *                                                                       *\r
14      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
15      *    ensuring you get running as quickly as possible and with an        *\r
16      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
17      *    the FreeRTOS project to continue with its mission of providing     *\r
18      *    professional grade, cross platform, de facto standard solutions    *\r
19      *    for microcontrollers - completely free of charge!                  *\r
20      *                                                                       *\r
21      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
22      *                                                                       *\r
23      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
24      *                                                                       *\r
25     ***************************************************************************\r
26 \r
27 \r
28     This file is part of the FreeRTOS distribution.\r
29 \r
30     FreeRTOS is free software; you can redistribute it and/or modify it under\r
31     the terms of the GNU General Public License (version 2) as published by the\r
32     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
33 \r
34     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
35     distribute a combined work that includes FreeRTOS without being obliged to\r
36     provide the source code for proprietary components outside of the FreeRTOS\r
37     kernel.\r
38 \r
39     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
40     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
41     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
42     details. You should have received a copy of the GNU General Public License\r
43     and the FreeRTOS license exception along with FreeRTOS; if not itcan be\r
44     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
45     writing to Real Time Engineers Ltd., contact details for whom are available\r
46     on the FreeRTOS WEB site.\r
47 \r
48     1 tab == 4 spaces!\r
49 \r
50     ***************************************************************************\r
51      *                                                                       *\r
52      *    Having a problem?  Start by reading the FAQ "My application does   *\r
53      *    not run, what could be wrong?"                                     *\r
54      *                                                                       *\r
55      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
56      *                                                                       *\r
57     ***************************************************************************\r
58 \r
59 \r
60     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
61     license and Real Time Engineers Ltd. contact details.\r
62 \r
63     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
64     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
65     fully thread aware and reentrant UDP/IP stack.\r
66 \r
67     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
68     Integrity Systems, who sell the code with commercial support,\r
69     indemnification and middleware, under the OpenRTOS brand.\r
70 \r
71     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
72     engineered and independently SIL3 certified version for use in safety and\r
73     mission critical applications that require provable dependability.\r
74 */\r
75 \r
76  /******************************************************************************\r
77  *\r
78  * See the following URL for information on the commands defined in this file:\r
79  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/Ethernet_Related_CLI_Commands.shtml\r
80  *\r
81  ******************************************************************************/\r
82 \r
83 \r
84 /* FreeRTOS includes. */\r
85 #include "FreeRTOS.h"\r
86 #include "task.h"\r
87 \r
88 /* Standard includes. */\r
89 #include <stdint.h>\r
90 #include <stdio.h>\r
91 #include <string.h>\r
92 \r
93 /* FreeRTOS+CLI includes. */\r
94 #include "FreeRTOS_CLI.h"\r
95 \r
96 #ifndef  configINCLUDE_TRACE_RELATED_CLI_COMMANDS\r
97         #define configINCLUDE_TRACE_RELATED_CLI_COMMANDS 0\r
98 #endif\r
99 \r
100 \r
101 /*\r
102  * Implements the run-time-stats command.\r
103  */\r
104 static portBASE_TYPE prvTaskStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
105 \r
106 /*\r
107  * Implements the task-stats command.\r
108  */\r
109 static portBASE_TYPE prvRunTimeStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
110 \r
111 /*\r
112  * Implements the echo-three-parameters command.\r
113  */\r
114 static portBASE_TYPE prvThreeParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
115 \r
116 /*\r
117  * Implements the echo-parameters command.\r
118  */\r
119 static portBASE_TYPE prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
120 \r
121 /*\r
122  * Registers the CLI commands defined within this file with the FreeRTOS+CLI\r
123  * command line interface.\r
124  */\r
125 void vRegisterSampleCLICommands( void );\r
126 \r
127 /*\r
128  * Implements the "trace start" and "trace stop" commands;\r
129  */\r
130 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
131         static portBASE_TYPE prvStartStopTraceCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
132 #endif\r
133 \r
134 /* Structure that defines the "run-time-stats" command line command.   This\r
135 generates a table that shows how much run time each task has */\r
136 static const CLI_Command_Definition_t xRunTimeStats =\r
137 {\r
138         "run-time-stats", /* The command string to type. */\r
139         "\r\nrun-time-stats:\r\n Displays a table showing how much processing time each FreeRTOS task has used\r\n",\r
140         prvRunTimeStatsCommand, /* The function to run. */\r
141         0 /* No parameters are expected. */\r
142 };\r
143 \r
144 /* Structure that defines the "task-stats" command line command.  This generates\r
145 a table that gives information on each task in the system. */\r
146 static const CLI_Command_Definition_t xTaskStats =\r
147 {\r
148         "task-stats", /* The command string to type. */\r
149         "\r\ntask-stats:\r\n Displays a table showing the state of each FreeRTOS task\r\n",\r
150         prvTaskStatsCommand, /* The function to run. */\r
151         0 /* No parameters are expected. */\r
152 };\r
153 \r
154 /* Structure that defines the "echo_3_parameters" command line command.  This\r
155 takes exactly three parameters that the command simply echos back one at a\r
156 time. */\r
157 static const CLI_Command_Definition_t xThreeParameterEcho =\r
158 {\r
159         "echo-3-parameters",\r
160         "\r\necho-3-parameters <param1> <param2> <param3>:\r\n Expects three parameters, echos each in turn\r\n",\r
161         prvThreeParameterEchoCommand, /* The function to run. */\r
162         3 /* Three parameters are expected, which can take any value. */\r
163 };\r
164 \r
165 /* Structure that defines the "echo_parameters" command line command.  This\r
166 takes a variable number of parameters that the command simply echos back one at\r
167 a time. */\r
168 static const CLI_Command_Definition_t xParameterEcho =\r
169 {\r
170         "echo-parameters",\r
171         "\r\necho-parameters <...>:\r\n Take variable number of parameters, echos each in turn\r\n",\r
172         prvParameterEchoCommand, /* The function to run. */\r
173         -1 /* The user can enter any number of commands. */\r
174 };\r
175 \r
176 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
177         /* Structure that defines the "trace" command line command.  This takes a single\r
178         parameter, which can be either "start" or "stop". */\r
179         static const CLI_Command_Definition_t xStartStopTrace =\r
180         {\r
181                 "trace",\r
182                 "\r\ntrace [start | stop]:\r\n Starts or stops a trace recording for viewing in FreeRTOS+Trace\r\n",\r
183                 prvStartStopTraceCommand, /* The function to run. */\r
184                 1 /* One parameter is expected.  Valid values are "start" and "stop". */\r
185         };\r
186 #endif /* configINCLUDE_TRACE_RELATED_CLI_COMMANDS */\r
187 \r
188 /*-----------------------------------------------------------*/\r
189 \r
190 void vRegisterSampleCLICommands( void )\r
191 {\r
192         /* Register all the command line commands defined immediately above. */\r
193         FreeRTOS_CLIRegisterCommand( &xTaskStats );\r
194         FreeRTOS_CLIRegisterCommand( &xRunTimeStats );\r
195         FreeRTOS_CLIRegisterCommand( &xThreeParameterEcho );\r
196         FreeRTOS_CLIRegisterCommand( &xParameterEcho );\r
197 \r
198         #if( configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1 )\r
199         {\r
200                 FreeRTOS_CLIRegisterCommand( & xStartStopTrace );\r
201         }\r
202         #endif\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 static portBASE_TYPE prvTaskStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
207 {\r
208 const char *const pcHeader = "Task          State  Priority  Stack      #\r\n************************************************\r\n";\r
209 \r
210         /* Remove compile time warnings about unused parameters, and check the\r
211         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
212         write buffer length is adequate, so does not check for buffer overflows. */\r
213         ( void ) pcCommandString;\r
214         ( void ) xWriteBufferLen;\r
215         configASSERT( pcWriteBuffer );\r
216 \r
217         /* Generate a table of task stats. */\r
218         strcpy( pcWriteBuffer, pcHeader );\r
219         vTaskList( pcWriteBuffer + strlen( pcHeader ) );\r
220 \r
221         /* There is no more data to return after this single string, so return\r
222         pdFALSE. */\r
223         return pdFALSE;\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 static portBASE_TYPE prvRunTimeStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
228 {\r
229 const char * const pcHeader = "Task            Abs Time      % Time\r\n****************************************\r\n";\r
230 \r
231         /* Remove compile time warnings about unused parameters, and check the\r
232         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
233         write buffer length is adequate, so does not check for buffer overflows. */\r
234         ( void ) pcCommandString;\r
235         ( void ) xWriteBufferLen;\r
236         configASSERT( pcWriteBuffer );\r
237 \r
238         /* Generate a table of task stats. */\r
239         strcpy( pcWriteBuffer, pcHeader );\r
240         vTaskGetRunTimeStats( pcWriteBuffer + strlen( pcHeader ) );\r
241 \r
242         /* There is no more data to return after this single string, so return\r
243         pdFALSE. */\r
244         return pdFALSE;\r
245 }\r
246 /*-----------------------------------------------------------*/\r
247 \r
248 static portBASE_TYPE prvThreeParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
249 {\r
250 const char *pcParameter;\r
251 portBASE_TYPE xParameterStringLength, xReturn;\r
252 static portBASE_TYPE lParameterNumber = 0;\r
253 \r
254         /* Remove compile time warnings about unused parameters, and check the\r
255         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
256         write buffer length is adequate, so does not check for buffer overflows. */\r
257         ( void ) pcCommandString;\r
258         ( void ) xWriteBufferLen;\r
259         configASSERT( pcWriteBuffer );\r
260 \r
261         if( lParameterNumber == 0 )\r
262         {\r
263                 /* The first time the function is called after the command has been\r
264                 entered just a header string is returned. */\r
265                 sprintf( pcWriteBuffer, "The three parameters were:\r\n" );\r
266 \r
267                 /* Next time the function is called the first parameter will be echoed\r
268                 back. */\r
269                 lParameterNumber = 1L;\r
270 \r
271                 /* There is more data to be returned as no parameters have been echoed\r
272                 back yet. */\r
273                 xReturn = pdPASS;\r
274         }\r
275         else\r
276         {\r
277                 /* Obtain the parameter string. */\r
278                 pcParameter = FreeRTOS_CLIGetParameter\r
279                                                         (\r
280                                                                 pcCommandString,                /* The command string itself. */\r
281                                                                 lParameterNumber,               /* Return the next parameter. */\r
282                                                                 &xParameterStringLength /* Store the parameter string length. */\r
283                                                         );\r
284 \r
285                 /* Sanity check something was returned. */\r
286                 configASSERT( pcParameter );\r
287 \r
288                 /* Return the parameter string. */\r
289                 memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
290                 sprintf( pcWriteBuffer, "%d: ", ( int ) lParameterNumber );\r
291                 strncat( pcWriteBuffer, pcParameter, xParameterStringLength );\r
292                 strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );\r
293 \r
294                 /* If this is the last of the three parameters then there are no more\r
295                 strings to return after this one. */\r
296                 if( lParameterNumber == 3L )\r
297                 {\r
298                         /* If this is the last of the three parameters then there are no more\r
299                         strings to return after this one. */\r
300                         xReturn = pdFALSE;\r
301                         lParameterNumber = 0L;\r
302                 }\r
303                 else\r
304                 {\r
305                         /* There are more parameters to return after this one. */\r
306                         xReturn = pdTRUE;\r
307                         lParameterNumber++;\r
308                 }\r
309         }\r
310 \r
311         return xReturn;\r
312 }\r
313 /*-----------------------------------------------------------*/\r
314 \r
315 static portBASE_TYPE prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
316 {\r
317 const char *pcParameter;\r
318 portBASE_TYPE xParameterStringLength, xReturn;\r
319 static portBASE_TYPE lParameterNumber = 0;\r
320 \r
321         /* Remove compile time warnings about unused parameters, and check the\r
322         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
323         write buffer length is adequate, so does not check for buffer overflows. */\r
324         ( void ) pcCommandString;\r
325         ( void ) xWriteBufferLen;\r
326         configASSERT( pcWriteBuffer );\r
327 \r
328         if( lParameterNumber == 0 )\r
329         {\r
330                 /* The first time the function is called after the command has been\r
331                 entered just a header string is returned. */\r
332                 sprintf( pcWriteBuffer, "The parameters were:\r\n" );\r
333 \r
334                 /* Next time the function is called the first parameter will be echoed\r
335                 back. */\r
336                 lParameterNumber = 1L;\r
337 \r
338                 /* There is more data to be returned as no parameters have been echoed\r
339                 back yet. */\r
340                 xReturn = pdPASS;\r
341         }\r
342         else\r
343         {\r
344                 /* Obtain the parameter string. */\r
345                 pcParameter = FreeRTOS_CLIGetParameter\r
346                                                         (\r
347                                                                 pcCommandString,                /* The command string itself. */\r
348                                                                 lParameterNumber,               /* Return the next parameter. */\r
349                                                                 &xParameterStringLength /* Store the parameter string length. */\r
350                                                         );\r
351 \r
352                 if( pcParameter != NULL )\r
353                 {\r
354                         /* Return the parameter string. */\r
355                         memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
356                         sprintf( pcWriteBuffer, "%d: ", ( int ) lParameterNumber );\r
357                         strncat( pcWriteBuffer, pcParameter, xParameterStringLength );\r
358                         strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );\r
359 \r
360                         /* There might be more parameters to return after this one. */\r
361                         xReturn = pdTRUE;\r
362                         lParameterNumber++;\r
363                 }\r
364                 else\r
365                 {\r
366                         /* No more parameters were found.  Make sure the write buffer does\r
367                         not contain a valid string. */\r
368                         pcWriteBuffer[ 0 ] = 0x00;\r
369 \r
370                         /* No more data to return. */\r
371                         xReturn = pdFALSE;\r
372 \r
373                         /* Start over the next time this command is executed. */\r
374                         lParameterNumber = 0;\r
375                 }\r
376         }\r
377 \r
378         return xReturn;\r
379 }\r
380 /*-----------------------------------------------------------*/\r
381 \r
382 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
383 \r
384         static portBASE_TYPE prvStartStopTraceCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
385         {\r
386         char *pcParameter;\r
387         portBASE_TYPE lParameterStringLength;\r
388 \r
389                 /* Remove compile time warnings about unused parameters, and check the\r
390                 write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
391                 write buffer length is adequate, so does not check for buffer overflows. */\r
392                 ( void ) pcCommandString;\r
393                 ( void ) xWriteBufferLen;\r
394                 configASSERT( pcWriteBuffer );\r
395 \r
396                 /* Obtain the parameter string. */\r
397                 pcParameter = FreeRTOS_CLIGetParameter\r
398                                                         (\r
399                                                                 pcCommandString,                /* The command string itself. */\r
400                                                                 1,                                              /* Return the first parameter. */\r
401                                                                 &lParameterStringLength /* Store the parameter string length. */\r
402                                                         );\r
403 \r
404                 /* Sanity check something was returned. */\r
405                 configASSERT( pcParameter );\r
406 \r
407                 /* There are only two valid parameter values. */\r
408                 if( strncmp( pcParameter, "start", strlen( "start" ) ) == 0 )\r
409                 {\r
410                         /* Start or restart the trace. */\r
411                         vTraceStop();\r
412                         vTraceClear();\r
413                         vTraceStart();\r
414 \r
415                         sprintf( pcWriteBuffer, "Trace recording (re)started.\r\n" );\r
416                 }\r
417                 else if( strncmp( pcParameter, "stop", strlen( "stop" ) ) == 0 )\r
418                 {\r
419                         /* End the trace, if one is running. */\r
420                         vTraceStop();\r
421                         sprintf( pcWriteBuffer, "Stopping trace recording.\r\n" );\r
422                 }\r
423                 else\r
424                 {\r
425                         sprintf( pcWriteBuffer, "Valid parameters are 'start' and 'stop'.\r\n" );\r
426                 }\r
427 \r
428                 /* There is no more data to return after this single string, so return\r
429                 pdFALSE. */\r
430                 return pdFALSE;\r
431         }\r
432 \r
433 #endif /* configINCLUDE_TRACE_RELATED_CLI_COMMANDS */\r