]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0+_Atmel_SAMD20_XPlained/RTOSDemo/src/Sample-CLI-commands.c
5d12a66dc811a160754b8122113ff1c2be5c3a65
[freertos] / FreeRTOS / Demo / CORTEX_M0+_Atmel_SAMD20_XPlained / RTOSDemo / src / Sample-CLI-commands.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28  /******************************************************************************\r
29  *\r
30  * See the following URL for information on the commands defined in this file:\r
31  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/Ethernet_Related_CLI_Commands.shtml\r
32  *\r
33  ******************************************************************************/\r
34 \r
35 \r
36 /* FreeRTOS includes. */\r
37 #include "FreeRTOS.h"\r
38 #include "task.h"\r
39 \r
40 /* Standard includes. */\r
41 #include <stdint.h>\r
42 #include <stdio.h>\r
43 #include <string.h>\r
44 \r
45 /* FreeRTOS+CLI includes. */\r
46 #include "FreeRTOS_CLI.h"\r
47 \r
48 #ifndef  configINCLUDE_TRACE_RELATED_CLI_COMMANDS\r
49         #define configINCLUDE_TRACE_RELATED_CLI_COMMANDS 0\r
50 #endif\r
51 \r
52 \r
53 /*\r
54  * Implements the run-time-stats command.\r
55  */\r
56 static portBASE_TYPE prvTaskStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
57 \r
58 /*\r
59  * Implements the task-stats command.\r
60  */\r
61 static portBASE_TYPE prvRunTimeStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
62 \r
63 /*\r
64  * Implements the echo-three-parameters command.\r
65  */\r
66 static portBASE_TYPE prvThreeParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
67 \r
68 /*\r
69  * Implements the echo-parameters command.\r
70  */\r
71 static portBASE_TYPE prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
72 \r
73 /*\r
74  * Implements the "trace start" and "trace stop" commands;\r
75  */\r
76 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
77         static portBASE_TYPE prvStartStopTraceCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
78 #endif\r
79 \r
80 /* Structure that defines the "run-time-stats" command line command.   This\r
81 generates a table that shows how much run time each task has */\r
82 static const CLI_Command_Definition_t xRunTimeStats =\r
83 {\r
84         "run-time-stats", /* The command string to type. */\r
85         "\r\nrun-time-stats:\r\n Displays a table showing how much processing time each FreeRTOS task has used\r\n",\r
86         prvRunTimeStatsCommand, /* The function to run. */\r
87         0 /* No parameters are expected. */\r
88 };\r
89 \r
90 /* Structure that defines the "task-stats" command line command.  This generates\r
91 a table that gives information on each task in the system. */\r
92 static const CLI_Command_Definition_t xTaskStats =\r
93 {\r
94         "task-stats", /* The command string to type. */\r
95         "\r\ntask-stats:\r\n Displays a table showing the state of each FreeRTOS task\r\n",\r
96         prvTaskStatsCommand, /* The function to run. */\r
97         0 /* No parameters are expected. */\r
98 };\r
99 \r
100 /* Structure that defines the "echo_3_parameters" command line command.  This\r
101 takes exactly three parameters that the command simply echos back one at a\r
102 time. */\r
103 static const CLI_Command_Definition_t xThreeParameterEcho =\r
104 {\r
105         "echo-3-parameters",\r
106         "\r\necho-3-parameters <param1> <param2> <param3>:\r\n Expects three parameters, echos each in turn\r\n",\r
107         prvThreeParameterEchoCommand, /* The function to run. */\r
108         3 /* Three parameters are expected, which can take any value. */\r
109 };\r
110 \r
111 /* Structure that defines the "echo_parameters" command line command.  This\r
112 takes a variable number of parameters that the command simply echos back one at\r
113 a time. */\r
114 static const CLI_Command_Definition_t xParameterEcho =\r
115 {\r
116         "echo-parameters",\r
117         "\r\necho-parameters <...>:\r\n Take variable number of parameters, echos each in turn\r\n",\r
118         prvParameterEchoCommand, /* The function to run. */\r
119         -1 /* The user can enter any number of commands. */\r
120 };\r
121 \r
122 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
123         /* Structure that defines the "trace" command line command.  This takes a single\r
124         parameter, which can be either "start" or "stop". */\r
125         static const CLI_Command_Definition_t xStartStopTrace =\r
126         {\r
127                 "trace",\r
128                 "\r\ntrace [start | stop]:\r\n Starts or stops a trace recording for viewing in FreeRTOS+Trace\r\n",\r
129                 prvStartStopTraceCommand, /* The function to run. */\r
130                 1 /* One parameter is expected.  Valid values are "start" and "stop". */\r
131         };\r
132 #endif /* configINCLUDE_TRACE_RELATED_CLI_COMMANDS */\r
133 \r
134 /*-----------------------------------------------------------*/\r
135 \r
136 void vRegisterSampleCLICommands( void )\r
137 {\r
138         /* Register all the command line commands defined immediately above. */\r
139         FreeRTOS_CLIRegisterCommand( &xTaskStats );\r
140         FreeRTOS_CLIRegisterCommand( &xRunTimeStats );\r
141         FreeRTOS_CLIRegisterCommand( &xThreeParameterEcho );\r
142         FreeRTOS_CLIRegisterCommand( &xParameterEcho );\r
143 \r
144         #if( configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1 )\r
145         {\r
146                 FreeRTOS_CLIRegisterCommand( & xStartStopTrace );\r
147         }\r
148         #endif\r
149 }\r
150 /*-----------------------------------------------------------*/\r
151 \r
152 static portBASE_TYPE prvTaskStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
153 {\r
154 const char *const pcHeader = "Task          State  Priority  Stack      #\r\n************************************************\r\n";\r
155 \r
156         /* Remove compile time warnings about unused parameters, and check the\r
157         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
158         write buffer length is adequate, so does not check for buffer overflows. */\r
159         ( void ) pcCommandString;\r
160         ( void ) xWriteBufferLen;\r
161         configASSERT( pcWriteBuffer );\r
162 \r
163         /* Generate a table of task stats. */\r
164         strcpy( pcWriteBuffer, pcHeader );\r
165         vTaskList( pcWriteBuffer + strlen( pcHeader ) );\r
166 \r
167         /* There is no more data to return after this single string, so return\r
168         pdFALSE. */\r
169         return pdFALSE;\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 static portBASE_TYPE prvRunTimeStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
174 {\r
175 const char * const pcHeader = "Task            Abs Time      % Time\r\n****************************************\r\n";\r
176 \r
177         /* Remove compile time warnings about unused parameters, and check the\r
178         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
179         write buffer length is adequate, so does not check for buffer overflows. */\r
180         ( void ) pcCommandString;\r
181         ( void ) xWriteBufferLen;\r
182         configASSERT( pcWriteBuffer );\r
183 \r
184         /* Generate a table of task stats. */\r
185         strcpy( pcWriteBuffer, pcHeader );\r
186         vTaskGetRunTimeStats( pcWriteBuffer + strlen( pcHeader ) );\r
187 \r
188         /* There is no more data to return after this single string, so return\r
189         pdFALSE. */\r
190         return pdFALSE;\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 static portBASE_TYPE prvThreeParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
195 {\r
196 const char *pcParameter;\r
197 portBASE_TYPE xParameterStringLength, xReturn;\r
198 static portBASE_TYPE lParameterNumber = 0;\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         if( lParameterNumber == 0 )\r
208         {\r
209                 /* The first time the function is called after the command has been\r
210                 entered just a header string is returned. */\r
211                 sprintf( pcWriteBuffer, "The three parameters were:\r\n" );\r
212 \r
213                 /* Next time the function is called the first parameter will be echoed\r
214                 back. */\r
215                 lParameterNumber = 1L;\r
216 \r
217                 /* There is more data to be returned as no parameters have been echoed\r
218                 back yet. */\r
219                 xReturn = pdPASS;\r
220         }\r
221         else\r
222         {\r
223                 /* Obtain the parameter string. */\r
224                 pcParameter = FreeRTOS_CLIGetParameter\r
225                                                         (\r
226                                                                 pcCommandString,                /* The command string itself. */\r
227                                                                 lParameterNumber,               /* Return the next parameter. */\r
228                                                                 &xParameterStringLength /* Store the parameter string length. */\r
229                                                         );\r
230 \r
231                 /* Sanity check something was returned. */\r
232                 configASSERT( pcParameter );\r
233 \r
234                 /* Return the parameter string. */\r
235                 memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
236                 sprintf( pcWriteBuffer, "%d: ", ( int ) lParameterNumber );\r
237                 strncat( pcWriteBuffer, pcParameter, xParameterStringLength );\r
238                 strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );\r
239 \r
240                 /* If this is the last of the three parameters then there are no more\r
241                 strings to return after this one. */\r
242                 if( lParameterNumber == 3L )\r
243                 {\r
244                         /* If this is the last of the three parameters then there are no more\r
245                         strings to return after this one. */\r
246                         xReturn = pdFALSE;\r
247                         lParameterNumber = 0L;\r
248                 }\r
249                 else\r
250                 {\r
251                         /* There are more parameters to return after this one. */\r
252                         xReturn = pdTRUE;\r
253                         lParameterNumber++;\r
254                 }\r
255         }\r
256 \r
257         return xReturn;\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 static portBASE_TYPE prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
262 {\r
263 const char *pcParameter;\r
264 portBASE_TYPE xParameterStringLength, xReturn;\r
265 static portBASE_TYPE lParameterNumber = 0;\r
266 \r
267         /* Remove compile time warnings about unused parameters, and check the\r
268         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
269         write buffer length is adequate, so does not check for buffer overflows. */\r
270         ( void ) pcCommandString;\r
271         ( void ) xWriteBufferLen;\r
272         configASSERT( pcWriteBuffer );\r
273 \r
274         if( lParameterNumber == 0 )\r
275         {\r
276                 /* The first time the function is called after the command has been\r
277                 entered just a header string is returned. */\r
278                 sprintf( pcWriteBuffer, "The parameters were:\r\n" );\r
279 \r
280                 /* Next time the function is called the first parameter will be echoed\r
281                 back. */\r
282                 lParameterNumber = 1L;\r
283 \r
284                 /* There is more data to be returned as no parameters have been echoed\r
285                 back yet. */\r
286                 xReturn = pdPASS;\r
287         }\r
288         else\r
289         {\r
290                 /* Obtain the parameter string. */\r
291                 pcParameter = FreeRTOS_CLIGetParameter\r
292                                                         (\r
293                                                                 pcCommandString,                /* The command string itself. */\r
294                                                                 lParameterNumber,               /* Return the next parameter. */\r
295                                                                 &xParameterStringLength /* Store the parameter string length. */\r
296                                                         );\r
297 \r
298                 if( pcParameter != NULL )\r
299                 {\r
300                         /* Return the parameter string. */\r
301                         memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
302                         sprintf( pcWriteBuffer, "%d: ", ( int ) lParameterNumber );\r
303                         strncat( pcWriteBuffer, pcParameter, xParameterStringLength );\r
304                         strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );\r
305 \r
306                         /* There might be more parameters to return after this one. */\r
307                         xReturn = pdTRUE;\r
308                         lParameterNumber++;\r
309                 }\r
310                 else\r
311                 {\r
312                         /* No more parameters were found.  Make sure the write buffer does\r
313                         not contain a valid string. */\r
314                         pcWriteBuffer[ 0 ] = 0x00;\r
315 \r
316                         /* No more data to return. */\r
317                         xReturn = pdFALSE;\r
318 \r
319                         /* Start over the next time this command is executed. */\r
320                         lParameterNumber = 0;\r
321                 }\r
322         }\r
323 \r
324         return xReturn;\r
325 }\r
326 /*-----------------------------------------------------------*/\r
327 \r
328 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
329 \r
330         static portBASE_TYPE prvStartStopTraceCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
331         {\r
332         const char *pcParameter;\r
333         portBASE_TYPE lParameterStringLength;\r
334 \r
335                 /* Remove compile time warnings about unused parameters, and check the\r
336                 write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
337                 write buffer length is adequate, so does not check for buffer overflows. */\r
338                 ( void ) pcCommandString;\r
339                 ( void ) xWriteBufferLen;\r
340                 configASSERT( pcWriteBuffer );\r
341 \r
342                 /* Obtain the parameter string. */\r
343                 pcParameter = FreeRTOS_CLIGetParameter\r
344                                                         (\r
345                                                                 pcCommandString,                /* The command string itself. */\r
346                                                                 1,                                              /* Return the first parameter. */\r
347                                                                 &lParameterStringLength /* Store the parameter string length. */\r
348                                                         );\r
349 \r
350                 /* Sanity check something was returned. */\r
351                 configASSERT( pcParameter );\r
352 \r
353                 /* There are only two valid parameter values. */\r
354                 if( strncmp( pcParameter, "start", strlen( "start" ) ) == 0 )\r
355                 {\r
356                         /* Start or restart the trace. */\r
357                         vTraceStop();\r
358                         vTraceClear();\r
359                         vTraceStart();\r
360 \r
361                         sprintf( pcWriteBuffer, "Trace recording (re)started.\r\n" );\r
362                 }\r
363                 else if( strncmp( pcParameter, "stop", strlen( "stop" ) ) == 0 )\r
364                 {\r
365                         /* End the trace, if one is running. */\r
366                         vTraceStop();\r
367                         sprintf( pcWriteBuffer, "Stopping trace recording.\r\n" );\r
368                 }\r
369                 else\r
370                 {\r
371                         sprintf( pcWriteBuffer, "Valid parameters are 'start' and 'stop'.\r\n" );\r
372                 }\r
373 \r
374                 /* There is no more data to return after this single string, so return\r
375                 pdFALSE. */\r
376                 return pdFALSE;\r
377         }\r
378 \r
379 #endif /* configINCLUDE_TRACE_RELATED_CLI_COMMANDS */\r