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