2 * FreeRTOS+CLI V1.0.1 (C) 2012 Real Time Engineers ltd.
\r
4 * FreeRTOS+CLI is an add-on component to FreeRTOS. It is not, in itself, part
\r
5 * of the FreeRTOS kernel. FreeRTOS+CLI is licensed separately from FreeRTOS,
\r
6 * and uses a different license to FreeRTOS. FreeRTOS+CLI uses a dual license
\r
7 * model, information on which is provided below:
\r
9 * - Open source licensing -
\r
10 * FreeRTOS+CLI is a free download and may be used, modified and distributed
\r
11 * without charge provided the user adheres to version two of the GNU General
\r
12 * Public license (GPL) and does not remove the copyright notice or this text.
\r
13 * The GPL V2 text is available on the gnu.org web site, and on the following
\r
14 * URL: http://www.FreeRTOS.org/gpl-2.0.txt
\r
16 * - Commercial licensing -
\r
17 * Businesses and individuals who wish to incorporate FreeRTOS+CLI into
\r
18 * proprietary software for redistribution in any form must first obtain a
\r
19 * (very) low cost commercial license - and in-so-doing support the maintenance,
\r
20 * support and further development of the FreeRTOS+CLI product. Commercial
\r
21 * licenses can be obtained from http://shop.freertos.org and do not require any
\r
22 * source files to be changed.
\r
24 * FreeRTOS+CLI is distributed in the hope that it will be useful. You cannot
\r
25 * use FreeRTOS+CLI unless you agree that you use the software 'as is'.
\r
26 * FreeRTOS+CLI is provided WITHOUT ANY WARRANTY; without even the implied
\r
27 * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
28 * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they
\r
29 * implied, expressed, or statutory.
\r
31 * 1 tab == 4 spaces!
\r
33 * http://www.FreeRTOS.org
\r
34 * http://www.FreeRTOS.org/FreeRTOS-Plus
\r
38 /* Standard includes. */
\r
42 /* FreeRTOS includes. */
\r
43 #include "FreeRTOS.h"
\r
46 /* Utils includes. */
\r
47 #include "FreeRTOS_CLI.h"
\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 portBASE_TYPE prvHelpCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
62 * Return the number of parameters that follow the command name.
\r
64 static int8_t prvGetNumberOfParameters( const int8_t * 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
70 ( const int8_t * const ) "help",
\r
71 ( const int8_t * const ) "\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
92 static int8_t cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
\r
94 /*-----------------------------------------------------------*/
\r
96 portBASE_TYPE FreeRTOS_CLIRegisterCommand( const CLI_Command_Definition_t * const pxCommandToRegister )
\r
98 static CLI_Definition_List_Item_t *pxLastCommandInList = &xRegisteredCommands;
\r
99 CLI_Definition_List_Item_t *pxNewListItem;
\r
100 portBASE_TYPE xReturn = pdFAIL;
\r
102 /* Check the parameter is not NULL. */
\r
103 configASSERT( pxCommandToRegister );
\r
105 /* Create a new list item that will reference the command being registered. */
\r
106 pxNewListItem = ( CLI_Definition_List_Item_t * ) pvPortMalloc( sizeof( CLI_Definition_List_Item_t ) );
\r
107 configASSERT( pxNewListItem );
\r
109 if( pxNewListItem != NULL )
\r
111 taskENTER_CRITICAL();
\r
113 /* Reference the command being registered from the newly created
\r
115 pxNewListItem->pxCommandLineDefinition = pxCommandToRegister;
\r
117 /* The new list item will get added to the end of the list, so
\r
118 pxNext has nowhere to point. */
\r
119 pxNewListItem->pxNext = NULL;
\r
121 /* Add the newly created list item to the end of the already existing
\r
123 pxLastCommandInList->pxNext = pxNewListItem;
\r
125 /* Set the end of list marker to the new list item. */
\r
126 pxLastCommandInList = pxNewListItem;
\r
128 taskEXIT_CRITICAL();
\r
135 /*-----------------------------------------------------------*/
\r
137 portBASE_TYPE FreeRTOS_CLIProcessCommand( const int8_t * const pcCommandInput, int8_t * pcWriteBuffer, size_t xWriteBufferLen )
\r
139 static const CLI_Definition_List_Item_t *pxCommand = NULL;
\r
140 portBASE_TYPE xReturn = pdTRUE;
\r
141 const int8_t *pcRegisteredCommandString;
\r
142 size_t xCommandStringLength;
\r
144 /* Note: This function is not re-entrant. It must not be called from more
\r
147 if( pxCommand == NULL )
\r
149 /* Search for the command string in the list of registered commands. */
\r
150 for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )
\r
152 pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand;
\r
153 xCommandStringLength = strlen( ( const char * ) pcRegisteredCommandString );
\r
155 /* To ensure the string lengths match exactly, so as not to pick up
\r
156 a sub-string of a longer command, check the byte after the expected
\r
157 end of the string is either the end of the string or a space before
\r
159 if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) )
\r
161 if( strncmp( ( const char * ) pcCommandInput, ( const char * ) pcRegisteredCommandString, xCommandStringLength ) == 0 )
\r
163 /* The command has been found. Check it has the expected
\r
164 number of parameters. If cExpectedNumberOfParameters is -1,
\r
165 then there could be a variable number of parameters and no
\r
167 if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 )
\r
169 if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters )
\r
181 if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) )
\r
183 /* The command was found, but the number of parameters with the command
\r
185 strncpy( ( char * ) pcWriteBuffer, "Incorrect command parameter(s). Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen );
\r
188 else if( pxCommand != NULL )
\r
190 /* Call the callback function that is registered to this command. */
\r
191 xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput );
\r
193 /* If xReturn is pdFALSE, then no further strings will be returned
\r
194 after this one, and pxCommand can be reset to NULL ready to search
\r
195 for the next entered command. */
\r
196 if( xReturn == pdFALSE )
\r
203 /* pxCommand was NULL, the command was not found. */
\r
204 strncpy( ( char * ) pcWriteBuffer, ( const char * const ) "Command not recognised. Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen );
\r
210 /*-----------------------------------------------------------*/
\r
212 int8_t *FreeRTOS_CLIGetOutputBuffer( void )
\r
214 return cOutputBuffer;
\r
216 /*-----------------------------------------------------------*/
\r
218 const int8_t *FreeRTOS_CLIGetParameter( const int8_t *pcCommandString, unsigned portBASE_TYPE uxWantedParameter, portBASE_TYPE *pxParameterStringLength )
\r
220 unsigned portBASE_TYPE uxParametersFound = 0;
\r
221 const int8_t *pcReturn = NULL;
\r
223 *pxParameterStringLength = 0;
\r
225 while( uxParametersFound < uxWantedParameter )
\r
227 /* Index the character pointer past the current word. If this is the start
\r
228 of the command string then the first word is the command itself. */
\r
229 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
\r
234 /* Find the start of the next string. */
\r
235 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) == ' ' ) )
\r
240 /* Was a string found? */
\r
241 if( *pcCommandString != 0x00 )
\r
243 /* Is this the start of the required parameter? */
\r
244 uxParametersFound++;
\r
246 if( uxParametersFound == uxWantedParameter )
\r
248 /* How long is the parameter? */
\r
249 pcReturn = pcCommandString;
\r
250 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
\r
252 ( *pxParameterStringLength )++;
\r
256 if( *pxParameterStringLength == 0 )
\r
272 /*-----------------------------------------------------------*/
\r
274 static portBASE_TYPE prvHelpCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
276 static const CLI_Definition_List_Item_t * pxCommand = NULL;
\r
277 signed portBASE_TYPE xReturn;
\r
279 ( void ) pcCommandString;
\r
281 if( pxCommand == NULL )
\r
283 /* Reset the pxCommand pointer back to the start of the list. */
\r
284 pxCommand = &xRegisteredCommands;
\r
287 /* Return the next command help string, before moving the pointer on to
\r
288 the next command in the list. */
\r
289 strncpy( ( char * ) pcWriteBuffer, ( const char * ) pxCommand->pxCommandLineDefinition->pcHelpString, xWriteBufferLen );
\r
290 pxCommand = pxCommand->pxNext;
\r
292 if( pxCommand == NULL )
\r
294 /* There are no more commands in the list, so there will be no more
\r
295 strings to return after this one and pdFALSE should be returned. */
\r
305 /*-----------------------------------------------------------*/
\r
307 static int8_t prvGetNumberOfParameters( const int8_t * pcCommandString )
\r
309 int8_t cParameters = 0;
\r
310 portBASE_TYPE xLastCharacterWasSpace = pdFALSE;
\r
312 /* Count the number of space delimited words in pcCommandString. */
\r
313 while( *pcCommandString != 0x00 )
\r
315 if( ( *pcCommandString ) == ' ' )
\r
317 if( xLastCharacterWasSpace != pdTRUE )
\r
320 xLastCharacterWasSpace = pdTRUE;
\r
325 xLastCharacterWasSpace = pdFALSE;
\r
331 /* If the command string ended with spaces, then there will have been too
\r
332 many parameters counted. */
\r
333 if( xLastCharacterWasSpace == pdTRUE )
\r
338 /* The value returned is one less than the number of space delimited words,
\r
339 as the first word should be the command itself. */
\r
340 return cParameters;
\r