2 * FreeRTOS+CLI V1.0.4 (C) 2014 Real Time Engineers ltd. All rights reserved.
\r
4 * This file is part of the FreeRTOS+CLI distribution. The FreeRTOS+CLI license
\r
5 * terms are different to the FreeRTOS license terms.
\r
7 * FreeRTOS+CLI uses a dual license model that allows the software to be used
\r
8 * under a standard GPL open source license, or a commercial license. The
\r
9 * standard GPL license (unlike the modified GPL license under which FreeRTOS
\r
10 * itself is distributed) requires that all software statically linked with
\r
11 * FreeRTOS+CLI is also distributed under the same GPL V2 license terms.
\r
12 * Details of both license options follow:
\r
14 * - Open source licensing -
\r
15 * FreeRTOS+CLI is a free download and may be used, modified, evaluated and
\r
16 * distributed without charge provided the user adheres to version two of the
\r
17 * GNU General Public License (GPL) and does not remove the copyright notice or
\r
18 * this text. The GPL V2 text is available on the gnu.org web site, and on the
\r
19 * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.
\r
21 * - Commercial licensing -
\r
22 * Businesses and individuals that for commercial or other reasons cannot comply
\r
23 * with the terms of the GPL V2 license must obtain a low cost commercial
\r
24 * license before incorporating FreeRTOS+CLI into proprietary software for
\r
25 * distribution in any form. Commercial licenses can be purchased from
\r
26 * http://shop.freertos.org/cli and do not require any source files to be
\r
29 * FreeRTOS+CLI is distributed in the hope that it will be useful. You cannot
\r
30 * use FreeRTOS+CLI unless you agree that you use the software 'as is'.
\r
31 * FreeRTOS+CLI is provided WITHOUT ANY WARRANTY; without even the implied
\r
32 * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
33 * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they
\r
34 * implied, expressed, or statutory.
\r
36 * 1 tab == 4 spaces!
\r
38 * http://www.FreeRTOS.org
\r
39 * http://www.FreeRTOS.org/FreeRTOS-Plus
\r
43 /* Standard includes. */
\r
47 /* FreeRTOS includes. */
\r
48 #include "FreeRTOS.h"
\r
51 /* Utils includes. */
\r
52 #include "FreeRTOS_CLI.h"
\r
54 /* If the application writer needs to place the buffer used by the CLI at a
\r
55 fixed address then set configAPPLICATION_PROVIDES_cOutputBuffer to 1 in
\r
56 FreeRTOSConfig.h, then declare an array with the following name and size in
\r
57 one of the application files:
\r
58 char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
\r
60 #ifndef configAPPLICATION_PROVIDES_cOutputBuffer
\r
61 #define configAPPLICATION_PROVIDES_cOutputBuffer 0
\r
64 typedef struct xCOMMAND_INPUT_LIST
\r
66 const CLI_Command_Definition_t *pxCommandLineDefinition;
\r
67 struct xCOMMAND_INPUT_LIST *pxNext;
\r
68 } CLI_Definition_List_Item_t;
\r
71 * The callback function that is executed when "help" is entered. This is the
\r
72 * only default command that is always present.
\r
74 static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
77 * Return the number of parameters that follow the command name.
\r
79 static int8_t prvGetNumberOfParameters( const char *pcCommandString );
\r
81 /* The definition of the "help" command. This command is always at the front
\r
82 of the list of registered commands. */
\r
83 static const CLI_Command_Definition_t xHelpCommand =
\r
86 "\r\nhelp:\r\n Lists all the registered commands\r\n\r\n",
\r
91 /* The definition of the list of commands. Commands that are registered are
\r
92 added to this list. */
\r
93 static CLI_Definition_List_Item_t xRegisteredCommands =
\r
95 &xHelpCommand, /* The first command in the list is always the help command, defined in this file. */
\r
96 NULL /* The next pointer is initialised to NULL, as there are no other registered commands yet. */
\r
99 /* A buffer into which command outputs can be written is declared here, rather
\r
100 than in the command console implementation, to allow multiple command consoles
\r
101 to share the same buffer. For example, an application may allow access to the
\r
102 command interpreter by UART and by Ethernet. Sharing a buffer is done purely
\r
103 to save RAM. Note, however, that the command console itself is not re-entrant,
\r
104 so only one command interpreter interface can be used at any one time. For that
\r
105 reason, no attempt at providing mutual exclusion to the cOutputBuffer array is
\r
108 configAPPLICATION_PROVIDES_cOutputBuffer is provided to allow the application
\r
109 writer to provide their own cOutputBuffer declaration in cases where the
\r
110 buffer needs to be placed at a fixed address (rather than by the linker). */
\r
111 #if( configAPPLICATION_PROVIDES_cOutputBuffer == 0 )
\r
112 static char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
\r
114 extern char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
\r
118 /*-----------------------------------------------------------*/
\r
120 BaseType_t FreeRTOS_CLIRegisterCommand( const CLI_Command_Definition_t * const pxCommandToRegister )
\r
122 static CLI_Definition_List_Item_t *pxLastCommandInList = &xRegisteredCommands;
\r
123 CLI_Definition_List_Item_t *pxNewListItem;
\r
124 BaseType_t xReturn = pdFAIL;
\r
126 /* Check the parameter is not NULL. */
\r
127 configASSERT( pxCommandToRegister );
\r
129 /* Create a new list item that will reference the command being registered. */
\r
130 pxNewListItem = ( CLI_Definition_List_Item_t * ) pvPortMalloc( sizeof( CLI_Definition_List_Item_t ) );
\r
131 configASSERT( pxNewListItem );
\r
133 if( pxNewListItem != NULL )
\r
135 taskENTER_CRITICAL();
\r
137 /* Reference the command being registered from the newly created
\r
139 pxNewListItem->pxCommandLineDefinition = pxCommandToRegister;
\r
141 /* The new list item will get added to the end of the list, so
\r
142 pxNext has nowhere to point. */
\r
143 pxNewListItem->pxNext = NULL;
\r
145 /* Add the newly created list item to the end of the already existing
\r
147 pxLastCommandInList->pxNext = pxNewListItem;
\r
149 /* Set the end of list marker to the new list item. */
\r
150 pxLastCommandInList = pxNewListItem;
\r
152 taskEXIT_CRITICAL();
\r
159 /*-----------------------------------------------------------*/
\r
161 BaseType_t FreeRTOS_CLIProcessCommand( const char * const pcCommandInput, char * pcWriteBuffer, size_t xWriteBufferLen )
\r
163 static const CLI_Definition_List_Item_t *pxCommand = NULL;
\r
164 BaseType_t xReturn = pdTRUE;
\r
165 const char *pcRegisteredCommandString;
\r
166 size_t xCommandStringLength;
\r
168 /* Note: This function is not re-entrant. It must not be called from more
\r
171 if( pxCommand == NULL )
\r
173 /* Search for the command string in the list of registered commands. */
\r
174 for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )
\r
176 pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand;
\r
177 xCommandStringLength = strlen( pcRegisteredCommandString );
\r
179 /* To ensure the string lengths match exactly, so as not to pick up
\r
180 a sub-string of a longer command, check the byte after the expected
\r
181 end of the string is either the end of the string or a space before
\r
183 if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) )
\r
185 if( strncmp( pcCommandInput, pcRegisteredCommandString, xCommandStringLength ) == 0 )
\r
187 /* The command has been found. Check it has the expected
\r
188 number of parameters. If cExpectedNumberOfParameters is -1,
\r
189 then there could be a variable number of parameters and no
\r
191 if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 )
\r
193 if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters )
\r
205 if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) )
\r
207 /* The command was found, but the number of parameters with the command
\r
209 strncpy( pcWriteBuffer, "Incorrect command parameter(s). Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen );
\r
212 else if( pxCommand != NULL )
\r
214 /* Call the callback function that is registered to this command. */
\r
215 xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput );
\r
217 /* If xReturn is pdFALSE, then no further strings will be returned
\r
218 after this one, and pxCommand can be reset to NULL ready to search
\r
219 for the next entered command. */
\r
220 if( xReturn == pdFALSE )
\r
227 /* pxCommand was NULL, the command was not found. */
\r
228 strncpy( pcWriteBuffer, "Command not recognised. Enter 'help' to view a list of available commands.\r\n\r\n", xWriteBufferLen );
\r
234 /*-----------------------------------------------------------*/
\r
236 char *FreeRTOS_CLIGetOutputBuffer( void )
\r
238 return cOutputBuffer;
\r
240 /*-----------------------------------------------------------*/
\r
242 const char *FreeRTOS_CLIGetParameter( const char *pcCommandString, UBaseType_t uxWantedParameter, BaseType_t *pxParameterStringLength )
\r
244 UBaseType_t uxParametersFound = 0;
\r
245 const char *pcReturn = NULL;
\r
247 *pxParameterStringLength = 0;
\r
249 while( uxParametersFound < uxWantedParameter )
\r
251 /* Index the character pointer past the current word. If this is the start
\r
252 of the command string then the first word is the command itself. */
\r
253 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
\r
258 /* Find the start of the next string. */
\r
259 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) == ' ' ) )
\r
264 /* Was a string found? */
\r
265 if( *pcCommandString != 0x00 )
\r
267 /* Is this the start of the required parameter? */
\r
268 uxParametersFound++;
\r
270 if( uxParametersFound == uxWantedParameter )
\r
272 /* How long is the parameter? */
\r
273 pcReturn = pcCommandString;
\r
274 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
\r
276 ( *pxParameterStringLength )++;
\r
280 if( *pxParameterStringLength == 0 )
\r
296 /*-----------------------------------------------------------*/
\r
298 static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
300 static const CLI_Definition_List_Item_t * pxCommand = NULL;
\r
301 BaseType_t xReturn;
\r
303 ( void ) pcCommandString;
\r
305 if( pxCommand == NULL )
\r
307 /* Reset the pxCommand pointer back to the start of the list. */
\r
308 pxCommand = &xRegisteredCommands;
\r
311 /* Return the next command help string, before moving the pointer on to
\r
312 the next command in the list. */
\r
313 strncpy( pcWriteBuffer, pxCommand->pxCommandLineDefinition->pcHelpString, xWriteBufferLen );
\r
314 pxCommand = pxCommand->pxNext;
\r
316 if( pxCommand == NULL )
\r
318 /* There are no more commands in the list, so there will be no more
\r
319 strings to return after this one and pdFALSE should be returned. */
\r
329 /*-----------------------------------------------------------*/
\r
331 static int8_t prvGetNumberOfParameters( const char *pcCommandString )
\r
333 int8_t cParameters = 0;
\r
334 BaseType_t xLastCharacterWasSpace = pdFALSE;
\r
336 /* Count the number of space delimited words in pcCommandString. */
\r
337 while( *pcCommandString != 0x00 )
\r
339 if( ( *pcCommandString ) == ' ' )
\r
341 if( xLastCharacterWasSpace != pdTRUE )
\r
344 xLastCharacterWasSpace = pdTRUE;
\r
349 xLastCharacterWasSpace = pdFALSE;
\r
355 /* If the command string ended with spaces, then there will have been too
\r
356 many parameters counted. */
\r
357 if( xLastCharacterWasSpace == pdTRUE )
\r
362 /* The value returned is one less than the number of space delimited words,
\r
363 as the first word should be the command itself. */
\r
364 return cParameters;
\r