]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-CLI/FreeRTOS_CLI.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-CLI / FreeRTOS_CLI.c
1 /*\r
2  * FreeRTOS+CLI V1.0.4\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
15  *\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
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* Standard includes. */\r
30 #include <string.h>\r
31 #include <stdint.h>\r
32 \r
33 /* FreeRTOS includes. */\r
34 #include "FreeRTOS.h"\r
35 #include "task.h"\r
36 \r
37 /* Utils includes. */\r
38 #include "FreeRTOS_CLI.h"\r
39 \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
45 */\r
46 #ifndef configAPPLICATION_PROVIDES_cOutputBuffer\r
47         #define configAPPLICATION_PROVIDES_cOutputBuffer 0\r
48 #endif\r
49 \r
50 typedef struct xCOMMAND_INPUT_LIST\r
51 {\r
52         const CLI_Command_Definition_t *pxCommandLineDefinition;\r
53         struct xCOMMAND_INPUT_LIST *pxNext;\r
54 } CLI_Definition_List_Item_t;\r
55 \r
56 /*\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
59  */\r
60 static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
61 \r
62 /*\r
63  * Return the number of parameters that follow the command name.\r
64  */\r
65 static int8_t prvGetNumberOfParameters( const char *pcCommandString );\r
66 \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
70 {\r
71         "help",\r
72         "\r\nhelp:\r\n Lists all the registered commands\r\n\r\n",\r
73         prvHelpCommand,\r
74         0\r
75 };\r
76 \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
80 {\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
83 };\r
84 \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
92 attempted.\r
93 \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
99 #else\r
100         extern char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];\r
101 #endif\r
102 \r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 BaseType_t FreeRTOS_CLIRegisterCommand( const CLI_Command_Definition_t * const pxCommandToRegister )\r
107 {\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
111 \r
112         /* Check the parameter is not NULL. */\r
113         configASSERT( pxCommandToRegister );\r
114 \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
118 \r
119         if( pxNewListItem != NULL )\r
120         {\r
121                 taskENTER_CRITICAL();\r
122                 {\r
123                         /* Reference the command being registered from the newly created\r
124                         list item. */\r
125                         pxNewListItem->pxCommandLineDefinition = pxCommandToRegister;\r
126 \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
130 \r
131                         /* Add the newly created list item to the end of the already existing\r
132                         list. */\r
133                         pxLastCommandInList->pxNext = pxNewListItem;\r
134 \r
135                         /* Set the end of list marker to the new list item. */\r
136                         pxLastCommandInList = pxNewListItem;\r
137                 }\r
138                 taskEXIT_CRITICAL();\r
139 \r
140                 xReturn = pdPASS;\r
141         }\r
142 \r
143         return xReturn;\r
144 }\r
145 /*-----------------------------------------------------------*/\r
146 \r
147 BaseType_t FreeRTOS_CLIProcessCommand( const char * const pcCommandInput, char * pcWriteBuffer, size_t xWriteBufferLen  )\r
148 {\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
153 \r
154         /* Note:  This function is not re-entrant.  It must not be called from more\r
155         thank one task. */\r
156 \r
157         if( pxCommand == NULL )\r
158         {\r
159                 /* Search for the command string in the list of registered commands. */\r
160                 for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )\r
161                 {\r
162                         pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand;\r
163                         xCommandStringLength = strlen( pcRegisteredCommandString );\r
164 \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
168                         a parameter. */\r
169                         if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) )\r
170                         {\r
171                                 if( strncmp( pcCommandInput, pcRegisteredCommandString, xCommandStringLength ) == 0 )\r
172                                 {\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
176                                         check is made. */\r
177                                         if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 )\r
178                                         {\r
179                                                 if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters )\r
180                                                 {\r
181                                                         xReturn = pdFALSE;\r
182                                                 }\r
183                                         }\r
184 \r
185                                         break;\r
186                                 }\r
187                         }\r
188                 }\r
189         }\r
190 \r
191         if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) )\r
192         {\r
193                 /* The command was found, but the number of parameters with the command\r
194                 was incorrect. */\r
195                 strncpy( pcWriteBuffer, "Incorrect command parameter(s).  Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen );\r
196                 pxCommand = NULL;\r
197         }\r
198         else if( pxCommand != NULL )\r
199         {\r
200                 /* Call the callback function that is registered to this command. */\r
201                 xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput );\r
202 \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
207                 {\r
208                         pxCommand = NULL;\r
209                 }\r
210         }\r
211         else\r
212         {\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
215                 xReturn = pdFALSE;\r
216         }\r
217 \r
218         return xReturn;\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 char *FreeRTOS_CLIGetOutputBuffer( void )\r
223 {\r
224         return cOutputBuffer;\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 const char *FreeRTOS_CLIGetParameter( const char *pcCommandString, UBaseType_t uxWantedParameter, BaseType_t *pxParameterStringLength )\r
229 {\r
230 UBaseType_t uxParametersFound = 0;\r
231 const char *pcReturn = NULL;\r
232 \r
233         *pxParameterStringLength = 0;\r
234 \r
235         while( uxParametersFound < uxWantedParameter )\r
236         {\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
240                 {\r
241                         pcCommandString++;\r
242                 }\r
243 \r
244                 /* Find the start of the next string. */\r
245                 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) == ' ' ) )\r
246                 {\r
247                         pcCommandString++;\r
248                 }\r
249 \r
250                 /* Was a string found? */\r
251                 if( *pcCommandString != 0x00 )\r
252                 {\r
253                         /* Is this the start of the required parameter? */\r
254                         uxParametersFound++;\r
255 \r
256                         if( uxParametersFound == uxWantedParameter )\r
257                         {\r
258                                 /* How long is the parameter? */\r
259                                 pcReturn = pcCommandString;\r
260                                 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )\r
261                                 {\r
262                                         ( *pxParameterStringLength )++;\r
263                                         pcCommandString++;\r
264                                 }\r
265 \r
266                                 if( *pxParameterStringLength == 0 )\r
267                                 {\r
268                                         pcReturn = NULL;\r
269                                 }\r
270 \r
271                                 break;\r
272                         }\r
273                 }\r
274                 else\r
275                 {\r
276                         break;\r
277                 }\r
278         }\r
279 \r
280         return pcReturn;\r
281 }\r
282 /*-----------------------------------------------------------*/\r
283 \r
284 static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
285 {\r
286 static const CLI_Definition_List_Item_t * pxCommand = NULL;\r
287 BaseType_t xReturn;\r
288 \r
289         ( void ) pcCommandString;\r
290 \r
291         if( pxCommand == NULL )\r
292         {\r
293                 /* Reset the pxCommand pointer back to the start of the list. */\r
294                 pxCommand = &xRegisteredCommands;\r
295         }\r
296 \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
301 \r
302         if( pxCommand == NULL )\r
303         {\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
306                 xReturn = pdFALSE;\r
307         }\r
308         else\r
309         {\r
310                 xReturn = pdTRUE;\r
311         }\r
312 \r
313         return xReturn;\r
314 }\r
315 /*-----------------------------------------------------------*/\r
316 \r
317 static int8_t prvGetNumberOfParameters( const char *pcCommandString )\r
318 {\r
319 int8_t cParameters = 0;\r
320 BaseType_t xLastCharacterWasSpace = pdFALSE;\r
321 \r
322         /* Count the number of space delimited words in pcCommandString. */\r
323         while( *pcCommandString != 0x00 )\r
324         {\r
325                 if( ( *pcCommandString ) == ' ' )\r
326                 {\r
327                         if( xLastCharacterWasSpace != pdTRUE )\r
328                         {\r
329                                 cParameters++;\r
330                                 xLastCharacterWasSpace = pdTRUE;\r
331                         }\r
332                 }\r
333                 else\r
334                 {\r
335                         xLastCharacterWasSpace = pdFALSE;\r
336                 }\r
337 \r
338                 pcCommandString++;\r
339         }\r
340 \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
344         {\r
345                 cParameters--;\r
346         }\r
347 \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
351 }\r
352 \r