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