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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* Standard includes. */
\r
33 /* FreeRTOS includes. */
\r
34 #include "FreeRTOS.h"
\r
37 /* Utils includes. */
\r
38 #include "FreeRTOS_CLI.h"
\r
40 /* If the application writer needs to place the buffer used by the CLI at a
\r
41 fixed address then set configAPPLICATION_PROVIDES_cOutputBuffer to 1 in
\r
42 FreeRTOSConfig.h, then declare an array with the following name and size in
\r
43 one of the application files:
\r
44 char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
\r
46 #ifndef configAPPLICATION_PROVIDES_cOutputBuffer
\r
47 #define configAPPLICATION_PROVIDES_cOutputBuffer 0
\r
50 typedef struct xCOMMAND_INPUT_LIST
\r
52 const CLI_Command_Definition_t *pxCommandLineDefinition;
\r
53 struct xCOMMAND_INPUT_LIST *pxNext;
\r
54 } CLI_Definition_List_Item_t;
\r
57 * The callback function that is executed when "help" is entered. This is the
\r
58 * only default command that is always present.
\r
60 static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
63 * Return the number of parameters that follow the command name.
\r
65 static int8_t prvGetNumberOfParameters( const char *pcCommandString );
\r
67 /* The definition of the "help" command. This command is always at the front
\r
68 of the list of registered commands. */
\r
69 static const CLI_Command_Definition_t xHelpCommand =
\r
72 "\r\nhelp:\r\n Lists all the registered commands\r\n\r\n",
\r
77 /* The definition of the list of commands. Commands that are registered are
\r
78 added to this list. */
\r
79 static CLI_Definition_List_Item_t xRegisteredCommands =
\r
81 &xHelpCommand, /* The first command in the list is always the help command, defined in this file. */
\r
82 NULL /* The next pointer is initialised to NULL, as there are no other registered commands yet. */
\r
85 /* A buffer into which command outputs can be written is declared here, rather
\r
86 than in the command console implementation, to allow multiple command consoles
\r
87 to share the same buffer. For example, an application may allow access to the
\r
88 command interpreter by UART and by Ethernet. Sharing a buffer is done purely
\r
89 to save RAM. Note, however, that the command console itself is not re-entrant,
\r
90 so only one command interpreter interface can be used at any one time. For that
\r
91 reason, no attempt at providing mutual exclusion to the cOutputBuffer array is
\r
94 configAPPLICATION_PROVIDES_cOutputBuffer is provided to allow the application
\r
95 writer to provide their own cOutputBuffer declaration in cases where the
\r
96 buffer needs to be placed at a fixed address (rather than by the linker). */
\r
97 #if( configAPPLICATION_PROVIDES_cOutputBuffer == 0 )
\r
98 static char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
\r
100 extern char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
\r
104 /*-----------------------------------------------------------*/
\r
106 BaseType_t FreeRTOS_CLIRegisterCommand( const CLI_Command_Definition_t * const pxCommandToRegister )
\r
108 static CLI_Definition_List_Item_t *pxLastCommandInList = &xRegisteredCommands;
\r
109 CLI_Definition_List_Item_t *pxNewListItem;
\r
110 BaseType_t xReturn = pdFAIL;
\r
112 /* Check the parameter is not NULL. */
\r
113 configASSERT( pxCommandToRegister );
\r
115 /* Create a new list item that will reference the command being registered. */
\r
116 pxNewListItem = ( CLI_Definition_List_Item_t * ) pvPortMalloc( sizeof( CLI_Definition_List_Item_t ) );
\r
117 configASSERT( pxNewListItem );
\r
119 if( pxNewListItem != NULL )
\r
121 taskENTER_CRITICAL();
\r
123 /* Reference the command being registered from the newly created
\r
125 pxNewListItem->pxCommandLineDefinition = pxCommandToRegister;
\r
127 /* The new list item will get added to the end of the list, so
\r
128 pxNext has nowhere to point. */
\r
129 pxNewListItem->pxNext = NULL;
\r
131 /* Add the newly created list item to the end of the already existing
\r
133 pxLastCommandInList->pxNext = pxNewListItem;
\r
135 /* Set the end of list marker to the new list item. */
\r
136 pxLastCommandInList = pxNewListItem;
\r
138 taskEXIT_CRITICAL();
\r
145 /*-----------------------------------------------------------*/
\r
147 BaseType_t FreeRTOS_CLIProcessCommand( const char * const pcCommandInput, char * pcWriteBuffer, size_t xWriteBufferLen )
\r
149 static const CLI_Definition_List_Item_t *pxCommand = NULL;
\r
150 BaseType_t xReturn = pdTRUE;
\r
151 const char *pcRegisteredCommandString;
\r
152 size_t xCommandStringLength;
\r
154 /* Note: This function is not re-entrant. It must not be called from more
\r
157 if( pxCommand == NULL )
\r
159 /* Search for the command string in the list of registered commands. */
\r
160 for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )
\r
162 pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand;
\r
163 xCommandStringLength = strlen( pcRegisteredCommandString );
\r
165 /* To ensure the string lengths match exactly, so as not to pick up
\r
166 a sub-string of a longer command, check the byte after the expected
\r
167 end of the string is either the end of the string or a space before
\r
169 if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) )
\r
171 if( strncmp( pcCommandInput, pcRegisteredCommandString, xCommandStringLength ) == 0 )
\r
173 /* The command has been found. Check it has the expected
\r
174 number of parameters. If cExpectedNumberOfParameters is -1,
\r
175 then there could be a variable number of parameters and no
\r
177 if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 )
\r
179 if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters )
\r
191 if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) )
\r
193 /* The command was found, but the number of parameters with the command
\r
195 strncpy( pcWriteBuffer, "Incorrect command parameter(s). Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen );
\r
198 else if( pxCommand != NULL )
\r
200 /* Call the callback function that is registered to this command. */
\r
201 xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput );
\r
203 /* If xReturn is pdFALSE, then no further strings will be returned
\r
204 after this one, and pxCommand can be reset to NULL ready to search
\r
205 for the next entered command. */
\r
206 if( xReturn == pdFALSE )
\r
213 /* pxCommand was NULL, the command was not found. */
\r
214 strncpy( pcWriteBuffer, "Command not recognised. Enter 'help' to view a list of available commands.\r\n\r\n", xWriteBufferLen );
\r
220 /*-----------------------------------------------------------*/
\r
222 char *FreeRTOS_CLIGetOutputBuffer( void )
\r
224 return cOutputBuffer;
\r
226 /*-----------------------------------------------------------*/
\r
228 const char *FreeRTOS_CLIGetParameter( const char *pcCommandString, UBaseType_t uxWantedParameter, BaseType_t *pxParameterStringLength )
\r
230 UBaseType_t uxParametersFound = 0;
\r
231 const char *pcReturn = NULL;
\r
233 *pxParameterStringLength = 0;
\r
235 while( uxParametersFound < uxWantedParameter )
\r
237 /* Index the character pointer past the current word. If this is the start
\r
238 of the command string then the first word is the command itself. */
\r
239 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
\r
244 /* Find the start of the next string. */
\r
245 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) == ' ' ) )
\r
250 /* Was a string found? */
\r
251 if( *pcCommandString != 0x00 )
\r
253 /* Is this the start of the required parameter? */
\r
254 uxParametersFound++;
\r
256 if( uxParametersFound == uxWantedParameter )
\r
258 /* How long is the parameter? */
\r
259 pcReturn = pcCommandString;
\r
260 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
\r
262 ( *pxParameterStringLength )++;
\r
266 if( *pxParameterStringLength == 0 )
\r
282 /*-----------------------------------------------------------*/
\r
284 static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
286 static const CLI_Definition_List_Item_t * pxCommand = NULL;
\r
287 BaseType_t xReturn;
\r
289 ( void ) pcCommandString;
\r
291 if( pxCommand == NULL )
\r
293 /* Reset the pxCommand pointer back to the start of the list. */
\r
294 pxCommand = &xRegisteredCommands;
\r
297 /* Return the next command help string, before moving the pointer on to
\r
298 the next command in the list. */
\r
299 strncpy( pcWriteBuffer, pxCommand->pxCommandLineDefinition->pcHelpString, xWriteBufferLen );
\r
300 pxCommand = pxCommand->pxNext;
\r
302 if( pxCommand == NULL )
\r
304 /* There are no more commands in the list, so there will be no more
\r
305 strings to return after this one and pdFALSE should be returned. */
\r
315 /*-----------------------------------------------------------*/
\r
317 static int8_t prvGetNumberOfParameters( const char *pcCommandString )
\r
319 int8_t cParameters = 0;
\r
320 BaseType_t xLastCharacterWasSpace = pdFALSE;
\r
322 /* Count the number of space delimited words in pcCommandString. */
\r
323 while( *pcCommandString != 0x00 )
\r
325 if( ( *pcCommandString ) == ' ' )
\r
327 if( xLastCharacterWasSpace != pdTRUE )
\r
330 xLastCharacterWasSpace = pdTRUE;
\r
335 xLastCharacterWasSpace = pdFALSE;
\r
341 /* If the command string ended with spaces, then there will have been too
\r
342 many parameters counted. */
\r
343 if( xLastCharacterWasSpace == pdTRUE )
\r
348 /* The value returned is one less than the number of space delimited words,
\r
349 as the first word should be the command itself. */
\r
350 return cParameters;
\r