]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/FreeRTOS-Plus-CLI/FreeRTOS_CLI.c
13f584d72de34fa1163f560e2ba1080d8397fde0
[freertos] / FreeRTOS-Plus / FreeRTOS-Plus-CLI / FreeRTOS_CLI.c
1 /*\r
2  * FreeRTOS+CLI V1.0.1 (C) 2012 Real Time Engineers ltd.\r
3  *\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
8  *\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
15  *\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
23  *\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
30  *\r
31  * 1 tab == 4 spaces!\r
32  *\r
33  * http://www.FreeRTOS.org\r
34  * http://www.FreeRTOS.org/FreeRTOS-Plus\r
35  *\r
36  */\r
37 \r
38 /* Standard includes. */\r
39 #include <string.h>\r
40 #include <stdint.h>\r
41 \r
42 /* FreeRTOS includes. */\r
43 #include "FreeRTOS.h"\r
44 #include "task.h"\r
45 \r
46 /* Utils includes. */\r
47 #include "FreeRTOS_CLI.h"\r
48 \r
49 typedef struct xCOMMAND_INPUT_LIST\r
50 {\r
51         const CLI_Command_Definition_t *pxCommandLineDefinition;\r
52         struct xCOMMAND_INPUT_LIST *pxNext;\r
53 } CLI_Definition_List_Item_t;\r
54 \r
55 /*\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
58  */\r
59 static portBASE_TYPE prvHelpCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
60 \r
61 /*\r
62  * Return the number of parameters that follow the command name.\r
63  */\r
64 static int8_t prvGetNumberOfParameters( const int8_t * pcCommandString );\r
65 \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
69 {\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
72         prvHelpCommand,\r
73         0\r
74 };\r
75 \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
79 {\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
82 };\r
83 \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
91 attempted. */\r
92 static int8_t cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];\r
93 \r
94 /*-----------------------------------------------------------*/\r
95 \r
96 portBASE_TYPE FreeRTOS_CLIRegisterCommand( const CLI_Command_Definition_t * const pxCommandToRegister )\r
97 {\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
101 \r
102         /* Check the parameter is not NULL. */\r
103         configASSERT( pxCommandToRegister );\r
104 \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
108 \r
109         if( pxNewListItem != NULL )\r
110         {\r
111                 taskENTER_CRITICAL();\r
112                 {\r
113                         /* Reference the command being registered from the newly created\r
114                         list item. */\r
115                         pxNewListItem->pxCommandLineDefinition = pxCommandToRegister;\r
116 \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
120 \r
121                         /* Add the newly created list item to the end of the already existing\r
122                         list. */\r
123                         pxLastCommandInList->pxNext = pxNewListItem;\r
124 \r
125                         /* Set the end of list marker to the new list item. */\r
126                         pxLastCommandInList = pxNewListItem;\r
127                 }\r
128                 taskEXIT_CRITICAL();\r
129 \r
130                 xReturn = pdPASS;\r
131         }\r
132 \r
133         return xReturn;\r
134 }\r
135 /*-----------------------------------------------------------*/\r
136 \r
137 portBASE_TYPE FreeRTOS_CLIProcessCommand( const int8_t * const pcCommandInput, int8_t * pcWriteBuffer, size_t xWriteBufferLen  )\r
138 {\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
143 \r
144         /* Note:  This function is not re-entrant.  It must not be called from more\r
145         thank one task. */\r
146 \r
147         if( pxCommand == NULL )\r
148         {\r
149                 /* Search for the command string in the list of registered commands. */\r
150                 for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )\r
151                 {\r
152                         pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand;\r
153                         xCommandStringLength = strlen( ( const char * ) pcRegisteredCommandString );\r
154 \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
158                         a parameter. */\r
159                         if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) )\r
160                         {\r
161                                 if( strncmp( ( const char * ) pcCommandInput, ( const char * ) pcRegisteredCommandString, xCommandStringLength ) == 0 )\r
162                                 {\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
166                                         check is made. */\r
167                                         if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 )\r
168                                         {\r
169                                                 if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters )\r
170                                                 {\r
171                                                         xReturn = pdFALSE;\r
172                                                 }\r
173                                         }\r
174 \r
175                                         break;\r
176                                 }\r
177                         }\r
178                 }\r
179         }\r
180 \r
181         if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) )\r
182         {\r
183                 /* The command was found, but the number of parameters with the command\r
184                 was incorrect. */\r
185                 strncpy( ( char * ) pcWriteBuffer, "Incorrect command parameter(s).  Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen );\r
186                 pxCommand = NULL;\r
187         }\r
188         else if( pxCommand != NULL )\r
189         {\r
190                 /* Call the callback function that is registered to this command. */\r
191                 xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput );\r
192 \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
197                 {\r
198                         pxCommand = NULL;\r
199                 }\r
200         }\r
201         else\r
202         {\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
205                 xReturn = pdFALSE;\r
206         }\r
207 \r
208         return xReturn;\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 int8_t *FreeRTOS_CLIGetOutputBuffer( void )\r
213 {\r
214         return cOutputBuffer;\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 const int8_t *FreeRTOS_CLIGetParameter( const int8_t *pcCommandString, unsigned portBASE_TYPE uxWantedParameter, portBASE_TYPE *pxParameterStringLength )\r
219 {\r
220 unsigned portBASE_TYPE uxParametersFound = 0;\r
221 const int8_t *pcReturn = NULL;\r
222 \r
223         *pxParameterStringLength = 0;\r
224 \r
225         while( uxParametersFound < uxWantedParameter )\r
226         {\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
230                 {\r
231                         pcCommandString++;\r
232                 }\r
233 \r
234                 /* Find the start of the next string. */\r
235                 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) == ' ' ) )\r
236                 {\r
237                         pcCommandString++;\r
238                 }\r
239 \r
240                 /* Was a string found? */\r
241                 if( *pcCommandString != 0x00 )\r
242                 {\r
243                         /* Is this the start of the required parameter? */\r
244                         uxParametersFound++;\r
245 \r
246                         if( uxParametersFound == uxWantedParameter )\r
247                         {\r
248                                 /* How long is the parameter? */\r
249                                 pcReturn = pcCommandString;\r
250                                 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )\r
251                                 {\r
252                                         ( *pxParameterStringLength )++;\r
253                                         pcCommandString++;\r
254                                 }\r
255 \r
256                                 if( *pxParameterStringLength == 0 )\r
257                                 {\r
258                                         pcReturn = NULL;\r
259                                 }\r
260 \r
261                                 break;\r
262                         }\r
263                 }\r
264                 else\r
265                 {\r
266                         break;\r
267                 }\r
268         }\r
269 \r
270         return pcReturn;\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 static portBASE_TYPE prvHelpCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
275 {\r
276 static const CLI_Definition_List_Item_t * pxCommand = NULL;\r
277 signed portBASE_TYPE xReturn;\r
278 \r
279         ( void ) pcCommandString;\r
280 \r
281         if( pxCommand == NULL )\r
282         {\r
283                 /* Reset the pxCommand pointer back to the start of the list. */\r
284                 pxCommand = &xRegisteredCommands;\r
285         }\r
286 \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
291 \r
292         if( pxCommand == NULL )\r
293         {\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
296                 xReturn = pdFALSE;\r
297         }\r
298         else\r
299         {\r
300                 xReturn = pdTRUE;\r
301         }\r
302 \r
303         return xReturn;\r
304 }\r
305 /*-----------------------------------------------------------*/\r
306 \r
307 static int8_t prvGetNumberOfParameters( const int8_t * pcCommandString )\r
308 {\r
309 int8_t cParameters = 0;\r
310 portBASE_TYPE xLastCharacterWasSpace = pdFALSE;\r
311 \r
312         /* Count the number of space delimited words in pcCommandString. */\r
313         while( *pcCommandString != 0x00 )\r
314         {\r
315                 if( ( *pcCommandString ) == ' ' )\r
316                 {\r
317                         if( xLastCharacterWasSpace != pdTRUE )\r
318                         {\r
319                                 cParameters++;\r
320                                 xLastCharacterWasSpace = pdTRUE;\r
321                         }\r
322                 }\r
323                 else\r
324                 {\r
325                         xLastCharacterWasSpace = pdFALSE;\r
326                 }\r
327 \r
328                 pcCommandString++;\r
329         }\r
330 \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
334         {\r
335                 cParameters--;\r
336         }\r
337 \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
341 }\r
342 \r