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