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