2 * FreeRTOS+CLI V1.0.1 (C) 2012 Real Time Engineers ltd.
\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 typedef struct xCOMMAND_INPUT_LIST
\r
56 const CLI_Command_Definition_t *pxCommandLineDefinition;
\r
57 struct xCOMMAND_INPUT_LIST *pxNext;
\r
58 } CLI_Definition_List_Item_t;
\r
61 * The callback function that is executed when "help" is entered. This is the
\r
62 * only default command that is always present.
\r
64 static portBASE_TYPE prvHelpCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
67 * Return the number of parameters that follow the command name.
\r
69 static int8_t prvGetNumberOfParameters( const int8_t * pcCommandString );
\r
71 /* The definition of the "help" command. This command is always at the front
\r
72 of the list of registered commands. */
\r
73 static const CLI_Command_Definition_t xHelpCommand =
\r
75 ( const int8_t * const ) "help",
\r
76 ( const int8_t * const ) "\r\nhelp:\r\n Lists all the registered commands\r\n\r\n",
\r
81 /* The definition of the list of commands. Commands that are registered are
\r
82 added to this list. */
\r
83 static CLI_Definition_List_Item_t xRegisteredCommands =
\r
85 &xHelpCommand, /* The first command in the list is always the help command, defined in this file. */
\r
86 NULL /* The next pointer is initialised to NULL, as there are no other registered commands yet. */
\r
89 /* A buffer into which command outputs can be written is declared here, rather
\r
90 than in the command console implementation, to allow multiple command consoles
\r
91 to share the same buffer. For example, an application may allow access to the
\r
92 command interpreter by UART and by Ethernet. Sharing a buffer is done purely
\r
93 to save RAM. Note, however, that the command console itself is not re-entrant,
\r
94 so only one command interpreter interface can be used at any one time. For that
\r
95 reason, no attempt at providing mutual exclusion to the cOutputBuffer array is
\r
97 static int8_t cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
\r
99 /*-----------------------------------------------------------*/
\r
101 portBASE_TYPE FreeRTOS_CLIRegisterCommand( const CLI_Command_Definition_t * const pxCommandToRegister )
\r
103 static CLI_Definition_List_Item_t *pxLastCommandInList = &xRegisteredCommands;
\r
104 CLI_Definition_List_Item_t *pxNewListItem;
\r
105 portBASE_TYPE xReturn = pdFAIL;
\r
107 /* Check the parameter is not NULL. */
\r
108 configASSERT( pxCommandToRegister );
\r
110 /* Create a new list item that will reference the command being registered. */
\r
111 pxNewListItem = ( CLI_Definition_List_Item_t * ) pvPortMalloc( sizeof( CLI_Definition_List_Item_t ) );
\r
112 configASSERT( pxNewListItem );
\r
114 if( pxNewListItem != NULL )
\r
116 taskENTER_CRITICAL();
\r
118 /* Reference the command being registered from the newly created
\r
120 pxNewListItem->pxCommandLineDefinition = pxCommandToRegister;
\r
122 /* The new list item will get added to the end of the list, so
\r
123 pxNext has nowhere to point. */
\r
124 pxNewListItem->pxNext = NULL;
\r
126 /* Add the newly created list item to the end of the already existing
\r
128 pxLastCommandInList->pxNext = pxNewListItem;
\r
130 /* Set the end of list marker to the new list item. */
\r
131 pxLastCommandInList = pxNewListItem;
\r
133 taskEXIT_CRITICAL();
\r
140 /*-----------------------------------------------------------*/
\r
142 portBASE_TYPE FreeRTOS_CLIProcessCommand( const int8_t * const pcCommandInput, int8_t * pcWriteBuffer, size_t xWriteBufferLen )
\r
144 static const CLI_Definition_List_Item_t *pxCommand = NULL;
\r
145 portBASE_TYPE xReturn = pdTRUE;
\r
146 const int8_t *pcRegisteredCommandString;
\r
147 size_t xCommandStringLength;
\r
149 /* Note: This function is not re-entrant. It must not be called from more
\r
152 if( pxCommand == NULL )
\r
154 /* Search for the command string in the list of registered commands. */
\r
155 for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )
\r
157 pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand;
\r
158 xCommandStringLength = strlen( ( const char * ) pcRegisteredCommandString );
\r
160 /* To ensure the string lengths match exactly, so as not to pick up
\r
161 a sub-string of a longer command, check the byte after the expected
\r
162 end of the string is either the end of the string or a space before
\r
164 if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) )
\r
166 if( strncmp( ( const char * ) pcCommandInput, ( const char * ) pcRegisteredCommandString, xCommandStringLength ) == 0 )
\r
168 /* The command has been found. Check it has the expected
\r
169 number of parameters. If cExpectedNumberOfParameters is -1,
\r
170 then there could be a variable number of parameters and no
\r
172 if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 )
\r
174 if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters )
\r
186 if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) )
\r
188 /* The command was found, but the number of parameters with the command
\r
190 strncpy( ( char * ) pcWriteBuffer, "Incorrect command parameter(s). Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen );
\r
193 else if( pxCommand != NULL )
\r
195 /* Call the callback function that is registered to this command. */
\r
196 xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput );
\r
198 /* If xReturn is pdFALSE, then no further strings will be returned
\r
199 after this one, and pxCommand can be reset to NULL ready to search
\r
200 for the next entered command. */
\r
201 if( xReturn == pdFALSE )
\r
208 /* pxCommand was NULL, the command was not found. */
\r
209 strncpy( ( char * ) pcWriteBuffer, ( const char * const ) "Command not recognised. Enter 'help' to view a list of available commands.\r\n\r\n", xWriteBufferLen );
\r
215 /*-----------------------------------------------------------*/
\r
217 int8_t *FreeRTOS_CLIGetOutputBuffer( void )
\r
219 return cOutputBuffer;
\r
221 /*-----------------------------------------------------------*/
\r
223 const int8_t *FreeRTOS_CLIGetParameter( const int8_t *pcCommandString, unsigned portBASE_TYPE uxWantedParameter, portBASE_TYPE *pxParameterStringLength )
\r
225 unsigned portBASE_TYPE uxParametersFound = 0;
\r
226 const int8_t *pcReturn = NULL;
\r
228 *pxParameterStringLength = 0;
\r
230 while( uxParametersFound < uxWantedParameter )
\r
232 /* Index the character pointer past the current word. If this is the start
\r
233 of the command string then the first word is the command itself. */
\r
234 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
\r
239 /* Find the start of the next string. */
\r
240 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) == ' ' ) )
\r
245 /* Was a string found? */
\r
246 if( *pcCommandString != 0x00 )
\r
248 /* Is this the start of the required parameter? */
\r
249 uxParametersFound++;
\r
251 if( uxParametersFound == uxWantedParameter )
\r
253 /* How long is the parameter? */
\r
254 pcReturn = pcCommandString;
\r
255 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
\r
257 ( *pxParameterStringLength )++;
\r
261 if( *pxParameterStringLength == 0 )
\r
277 /*-----------------------------------------------------------*/
\r
279 static portBASE_TYPE prvHelpCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
281 static const CLI_Definition_List_Item_t * pxCommand = NULL;
\r
282 signed portBASE_TYPE xReturn;
\r
284 ( void ) pcCommandString;
\r
286 if( pxCommand == NULL )
\r
288 /* Reset the pxCommand pointer back to the start of the list. */
\r
289 pxCommand = &xRegisteredCommands;
\r
292 /* Return the next command help string, before moving the pointer on to
\r
293 the next command in the list. */
\r
294 strncpy( ( char * ) pcWriteBuffer, ( const char * ) pxCommand->pxCommandLineDefinition->pcHelpString, xWriteBufferLen );
\r
295 pxCommand = pxCommand->pxNext;
\r
297 if( pxCommand == NULL )
\r
299 /* There are no more commands in the list, so there will be no more
\r
300 strings to return after this one and pdFALSE should be returned. */
\r
310 /*-----------------------------------------------------------*/
\r
312 static int8_t prvGetNumberOfParameters( const int8_t * pcCommandString )
\r
314 int8_t cParameters = 0;
\r
315 portBASE_TYPE xLastCharacterWasSpace = pdFALSE;
\r
317 /* Count the number of space delimited words in pcCommandString. */
\r
318 while( *pcCommandString != 0x00 )
\r
320 if( ( *pcCommandString ) == ' ' )
\r
322 if( xLastCharacterWasSpace != pdTRUE )
\r
325 xLastCharacterWasSpace = pdTRUE;
\r
330 xLastCharacterWasSpace = pdFALSE;
\r
336 /* If the command string ended with spaces, then there will have been too
\r
337 many parameters counted. */
\r
338 if( xLastCharacterWasSpace == pdTRUE )
\r
343 /* The value returned is one less than the number of space delimited words,
\r
344 as the first word should be the command itself. */
\r
345 return cParameters;
\r