2 * FreeRTOS+CLI V1.0.4
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* Standard includes. */
\r
32 /* FreeRTOS includes. */
\r
33 #include "FreeRTOS.h"
\r
36 /* Utils includes. */
\r
37 #include "FreeRTOS_CLI.h"
\r
39 /* If the application writer needs to place the buffer used by the CLI at a
\r
40 fixed address then set configAPPLICATION_PROVIDES_cOutputBuffer to 1 in
\r
41 FreeRTOSConfig.h, then declare an array with the following name and size in
\r
42 one of the application files:
\r
43 char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
\r
45 #ifndef configAPPLICATION_PROVIDES_cOutputBuffer
\r
46 #define configAPPLICATION_PROVIDES_cOutputBuffer 0
\r
49 typedef struct xCOMMAND_INPUT_LIST
\r
51 const CLI_Command_Definition_t *pxCommandLineDefinition;
\r
52 struct xCOMMAND_INPUT_LIST *pxNext;
\r
53 } CLI_Definition_List_Item_t;
\r
56 * The callback function that is executed when "help" is entered. This is the
\r
57 * only default command that is always present.
\r
59 static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
62 * Return the number of parameters that follow the command name.
\r
64 static int8_t prvGetNumberOfParameters( const char *pcCommandString );
\r
66 /* The definition of the "help" command. This command is always at the front
\r
67 of the list of registered commands. */
\r
68 static const CLI_Command_Definition_t xHelpCommand =
\r
71 "\r\nhelp:\r\n Lists all the registered commands\r\n\r\n",
\r
76 /* The definition of the list of commands. Commands that are registered are
\r
77 added to this list. */
\r
78 static CLI_Definition_List_Item_t xRegisteredCommands =
\r
80 &xHelpCommand, /* The first command in the list is always the help command, defined in this file. */
\r
81 NULL /* The next pointer is initialised to NULL, as there are no other registered commands yet. */
\r
84 /* A buffer into which command outputs can be written is declared here, rather
\r
85 than in the command console implementation, to allow multiple command consoles
\r
86 to share the same buffer. For example, an application may allow access to the
\r
87 command interpreter by UART and by Ethernet. Sharing a buffer is done purely
\r
88 to save RAM. Note, however, that the command console itself is not re-entrant,
\r
89 so only one command interpreter interface can be used at any one time. For that
\r
90 reason, no attempt at providing mutual exclusion to the cOutputBuffer array is
\r
93 configAPPLICATION_PROVIDES_cOutputBuffer is provided to allow the application
\r
94 writer to provide their own cOutputBuffer declaration in cases where the
\r
95 buffer needs to be placed at a fixed address (rather than by the linker). */
\r
96 #if( configAPPLICATION_PROVIDES_cOutputBuffer == 0 )
\r
97 static char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
\r
99 extern char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
\r
103 /*-----------------------------------------------------------*/
\r
105 BaseType_t FreeRTOS_CLIRegisterCommand( const CLI_Command_Definition_t * const pxCommandToRegister )
\r
107 static CLI_Definition_List_Item_t *pxLastCommandInList = &xRegisteredCommands;
\r
108 CLI_Definition_List_Item_t *pxNewListItem;
\r
109 BaseType_t xReturn = pdFAIL;
\r
111 /* Check the parameter is not NULL. */
\r
112 configASSERT( pxCommandToRegister );
\r
114 /* Create a new list item that will reference the command being registered. */
\r
115 pxNewListItem = ( CLI_Definition_List_Item_t * ) pvPortMalloc( sizeof( CLI_Definition_List_Item_t ) );
\r
116 configASSERT( pxNewListItem );
\r
118 if( pxNewListItem != NULL )
\r
120 taskENTER_CRITICAL();
\r
122 /* Reference the command being registered from the newly created
\r
124 pxNewListItem->pxCommandLineDefinition = pxCommandToRegister;
\r
126 /* The new list item will get added to the end of the list, so
\r
127 pxNext has nowhere to point. */
\r
128 pxNewListItem->pxNext = NULL;
\r
130 /* Add the newly created list item to the end of the already existing
\r
132 pxLastCommandInList->pxNext = pxNewListItem;
\r
134 /* Set the end of list marker to the new list item. */
\r
135 pxLastCommandInList = pxNewListItem;
\r
137 taskEXIT_CRITICAL();
\r
144 /*-----------------------------------------------------------*/
\r
146 BaseType_t FreeRTOS_CLIProcessCommand( const char * const pcCommandInput, char * pcWriteBuffer, size_t xWriteBufferLen )
\r
148 static const CLI_Definition_List_Item_t *pxCommand = NULL;
\r
149 BaseType_t xReturn = pdTRUE;
\r
150 const char *pcRegisteredCommandString;
\r
151 size_t xCommandStringLength;
\r
153 /* Note: This function is not re-entrant. It must not be called from more
\r
156 if( pxCommand == NULL )
\r
158 /* Search for the command string in the list of registered commands. */
\r
159 for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )
\r
161 pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand;
\r
162 xCommandStringLength = strlen( pcRegisteredCommandString );
\r
164 /* To ensure the string lengths match exactly, so as not to pick up
\r
165 a sub-string of a longer command, check the byte after the expected
\r
166 end of the string is either the end of the string or a space before
\r
168 if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) )
\r
170 if( strncmp( pcCommandInput, pcRegisteredCommandString, xCommandStringLength ) == 0 )
\r
172 /* The command has been found. Check it has the expected
\r
173 number of parameters. If cExpectedNumberOfParameters is -1,
\r
174 then there could be a variable number of parameters and no
\r
176 if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 )
\r
178 if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters )
\r
190 if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) )
\r
192 /* The command was found, but the number of parameters with the command
\r
194 strncpy( pcWriteBuffer, "Incorrect command parameter(s). Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen );
\r
197 else if( pxCommand != NULL )
\r
199 /* Call the callback function that is registered to this command. */
\r
200 xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput );
\r
202 /* If xReturn is pdFALSE, then no further strings will be returned
\r
203 after this one, and pxCommand can be reset to NULL ready to search
\r
204 for the next entered command. */
\r
205 if( xReturn == pdFALSE )
\r
212 /* pxCommand was NULL, the command was not found. */
\r
213 strncpy( pcWriteBuffer, "Command not recognised. Enter 'help' to view a list of available commands.\r\n\r\n", xWriteBufferLen );
\r
219 /*-----------------------------------------------------------*/
\r
221 char *FreeRTOS_CLIGetOutputBuffer( void )
\r
223 return cOutputBuffer;
\r
225 /*-----------------------------------------------------------*/
\r
227 const char *FreeRTOS_CLIGetParameter( const char *pcCommandString, UBaseType_t uxWantedParameter, BaseType_t *pxParameterStringLength )
\r
229 UBaseType_t uxParametersFound = 0;
\r
230 const char *pcReturn = NULL;
\r
232 *pxParameterStringLength = 0;
\r
234 while( uxParametersFound < uxWantedParameter )
\r
236 /* Index the character pointer past the current word. If this is the start
\r
237 of the command string then the first word is the command itself. */
\r
238 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
\r
243 /* Find the start of the next string. */
\r
244 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) == ' ' ) )
\r
249 /* Was a string found? */
\r
250 if( *pcCommandString != 0x00 )
\r
252 /* Is this the start of the required parameter? */
\r
253 uxParametersFound++;
\r
255 if( uxParametersFound == uxWantedParameter )
\r
257 /* How long is the parameter? */
\r
258 pcReturn = pcCommandString;
\r
259 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
\r
261 ( *pxParameterStringLength )++;
\r
265 if( *pxParameterStringLength == 0 )
\r
281 /*-----------------------------------------------------------*/
\r
283 static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
285 static const CLI_Definition_List_Item_t * pxCommand = NULL;
\r
286 BaseType_t xReturn;
\r
288 ( void ) pcCommandString;
\r
290 if( pxCommand == NULL )
\r
292 /* Reset the pxCommand pointer back to the start of the list. */
\r
293 pxCommand = &xRegisteredCommands;
\r
296 /* Return the next command help string, before moving the pointer on to
\r
297 the next command in the list. */
\r
298 strncpy( pcWriteBuffer, pxCommand->pxCommandLineDefinition->pcHelpString, xWriteBufferLen );
\r
299 pxCommand = pxCommand->pxNext;
\r
301 if( pxCommand == NULL )
\r
303 /* There are no more commands in the list, so there will be no more
\r
304 strings to return after this one and pdFALSE should be returned. */
\r
314 /*-----------------------------------------------------------*/
\r
316 static int8_t prvGetNumberOfParameters( const char *pcCommandString )
\r
318 int8_t cParameters = 0;
\r
319 BaseType_t xLastCharacterWasSpace = pdFALSE;
\r
321 /* Count the number of space delimited words in pcCommandString. */
\r
322 while( *pcCommandString != 0x00 )
\r
324 if( ( *pcCommandString ) == ' ' )
\r
326 if( xLastCharacterWasSpace != pdTRUE )
\r
329 xLastCharacterWasSpace = pdTRUE;
\r
334 xLastCharacterWasSpace = pdFALSE;
\r
340 /* If the command string ended with spaces, then there will have been too
\r
341 many parameters counted. */
\r
342 if( xLastCharacterWasSpace == pdTRUE )
\r
347 /* The value returned is one less than the number of space delimited words,
\r
348 as the first word should be the command itself. */
\r
349 return cParameters;
\r