]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Source/FreeRTOS-Plus-CLI/FreeRTOS_CLI.c
Update libraries and sundry check-ins ready for the V10.3.0 kernel release.
[freertos] / FreeRTOS-Labs / Source / FreeRTOS-Plus-CLI / FreeRTOS_CLI.c
1 /*\r
2  * FreeRTOS+CLI V1.0.x Labs copy\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.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* Standard includes. */\r
29 #include <string.h>\r
30 #include <stdint.h>\r
31 \r
32 /* FreeRTOS includes. */\r
33 #include "FreeRTOS.h"\r
34 #include "task.h"\r
35 \r
36 /* Utils includes. */\r
37 #include "FreeRTOS_CLI.h"\r
38 \r
39 /* If the application writer needs to place the buffer used by the CLI at a\r
40 fixed address then set configAPPLICATION_PROVIDES_cOutputBuffer to 1 in\r
41 FreeRTOSConfig.h, then declare an array with the following name and size in \r
42 one of the application files:\r
43         char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];\r
44 */\r
45 #ifndef configAPPLICATION_PROVIDES_cOutputBuffer\r
46         #define configAPPLICATION_PROVIDES_cOutputBuffer 0\r
47 #endif\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 BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *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 char *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         "help",\r
71         "\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 \r
93 configAPPLICATION_PROVIDES_cOutputBuffer is provided to allow the application\r
94 writer to provide their own cOutputBuffer declaration in cases where the\r
95 buffer needs to be placed at a fixed address (rather than by the linker). */\r
96 #if( configAPPLICATION_PROVIDES_cOutputBuffer == 0 )\r
97         static char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];\r
98 #else\r
99         extern char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];\r
100 #endif\r
101 \r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 BaseType_t FreeRTOS_CLIRegisterCommand( const CLI_Command_Definition_t * const pxCommandToRegister )\r
106 {\r
107 static CLI_Definition_List_Item_t *pxLastCommandInList = &xRegisteredCommands;\r
108 CLI_Definition_List_Item_t *pxNewListItem;\r
109 BaseType_t xReturn = pdFAIL;\r
110 \r
111         /* Check the parameter is not NULL. */\r
112         configASSERT( pxCommandToRegister );\r
113 \r
114         /* Create a new list item that will reference the command being registered. */\r
115         pxNewListItem = ( CLI_Definition_List_Item_t * ) pvPortMalloc( sizeof( CLI_Definition_List_Item_t ) );\r
116         configASSERT( pxNewListItem );\r
117 \r
118         if( pxNewListItem != NULL )\r
119         {\r
120                 taskENTER_CRITICAL();\r
121                 {\r
122                         /* Reference the command being registered from the newly created\r
123                         list item. */\r
124                         pxNewListItem->pxCommandLineDefinition = pxCommandToRegister;\r
125 \r
126                         /* The new list item will get added to the end of the list, so\r
127                         pxNext has nowhere to point. */\r
128                         pxNewListItem->pxNext = NULL;\r
129 \r
130                         /* Add the newly created list item to the end of the already existing\r
131                         list. */\r
132                         pxLastCommandInList->pxNext = pxNewListItem;\r
133 \r
134                         /* Set the end of list marker to the new list item. */\r
135                         pxLastCommandInList = pxNewListItem;\r
136                 }\r
137                 taskEXIT_CRITICAL();\r
138 \r
139                 xReturn = pdPASS;\r
140         }\r
141 \r
142         return xReturn;\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 BaseType_t FreeRTOS_CLIProcessCommand( const char * const pcCommandInput, char * pcWriteBuffer, size_t xWriteBufferLen  )\r
147 {\r
148 static const CLI_Definition_List_Item_t *pxCommand = NULL;\r
149 BaseType_t xReturn = pdTRUE;\r
150 const char *pcRegisteredCommandString;\r
151 size_t xCommandStringLength;\r
152 \r
153         /* Note:  This function is not re-entrant.  It must not be called from more\r
154         thank one task. */\r
155 \r
156         if( pxCommand == NULL )\r
157         {\r
158                 /* Search for the command string in the list of registered commands. */\r
159                 for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )\r
160                 {\r
161                         pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand;\r
162                         xCommandStringLength = strlen( pcRegisteredCommandString );\r
163 \r
164                         /* To ensure the string lengths match exactly, so as not to pick up\r
165                         a sub-string of a longer command, check the byte after the expected\r
166                         end of the string is either the end of the string or a space before\r
167                         a parameter. */\r
168                         if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) )\r
169                         {\r
170                                 if( strncmp( pcCommandInput, pcRegisteredCommandString, xCommandStringLength ) == 0 )\r
171                                 {\r
172                                         /* The command has been found.  Check it has the expected\r
173                                         number of parameters.  If cExpectedNumberOfParameters is -1,\r
174                                         then there could be a variable number of parameters and no\r
175                                         check is made. */\r
176                                         if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 )\r
177                                         {\r
178                                                 if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters )\r
179                                                 {\r
180                                                         xReturn = pdFALSE;\r
181                                                 }\r
182                                         }\r
183 \r
184                                         break;\r
185                                 }\r
186                         }\r
187                 }\r
188         }\r
189 \r
190         if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) )\r
191         {\r
192                 /* The command was found, but the number of parameters with the command\r
193                 was incorrect. */\r
194                 strncpy( pcWriteBuffer, "Incorrect command parameter(s).  Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen );\r
195                 pxCommand = NULL;\r
196         }\r
197         else if( pxCommand != NULL )\r
198         {\r
199                 /* Call the callback function that is registered to this command. */\r
200                 xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput );\r
201 \r
202                 /* If xReturn is pdFALSE, then no further strings will be returned\r
203                 after this one, and     pxCommand can be reset to NULL ready to search\r
204                 for the next entered command. */\r
205                 if( xReturn == pdFALSE )\r
206                 {\r
207                         pxCommand = NULL;\r
208                 }\r
209         }\r
210         else\r
211         {\r
212                 /* pxCommand was NULL, the command was not found. */\r
213                 strncpy( pcWriteBuffer, "Command not recognised.  Enter 'help' to view a list of available commands.\r\n\r\n", xWriteBufferLen );\r
214                 xReturn = pdFALSE;\r
215         }\r
216 \r
217         return xReturn;\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 char *FreeRTOS_CLIGetOutputBuffer( void )\r
222 {\r
223         return cOutputBuffer;\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 const char *FreeRTOS_CLIGetParameter( const char *pcCommandString, UBaseType_t uxWantedParameter, BaseType_t *pxParameterStringLength )\r
228 {\r
229 UBaseType_t uxParametersFound = 0;\r
230 const char *pcReturn = NULL;\r
231 \r
232         *pxParameterStringLength = 0;\r
233 \r
234         while( uxParametersFound < uxWantedParameter )\r
235         {\r
236                 /* Index the character pointer past the current word.  If this is the start\r
237                 of the command string then the first word is the command itself. */\r
238                 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )\r
239                 {\r
240                         pcCommandString++;\r
241                 }\r
242 \r
243                 /* Find the start of the next string. */\r
244                 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) == ' ' ) )\r
245                 {\r
246                         pcCommandString++;\r
247                 }\r
248 \r
249                 /* Was a string found? */\r
250                 if( *pcCommandString != 0x00 )\r
251                 {\r
252                         /* Is this the start of the required parameter? */\r
253                         uxParametersFound++;\r
254 \r
255                         if( uxParametersFound == uxWantedParameter )\r
256                         {\r
257                                 /* How long is the parameter? */\r
258                                 pcReturn = pcCommandString;\r
259                                 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )\r
260                                 {\r
261                                         ( *pxParameterStringLength )++;\r
262                                         pcCommandString++;\r
263                                 }\r
264 \r
265                                 if( *pxParameterStringLength == 0 )\r
266                                 {\r
267                                         pcReturn = NULL;\r
268                                 }\r
269 \r
270                                 break;\r
271                         }\r
272                 }\r
273                 else\r
274                 {\r
275                         break;\r
276                 }\r
277         }\r
278 \r
279         return pcReturn;\r
280 }\r
281 /*-----------------------------------------------------------*/\r
282 \r
283 static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
284 {\r
285 static const CLI_Definition_List_Item_t * pxCommand = NULL;\r
286 BaseType_t xReturn;\r
287 \r
288         ( void ) pcCommandString;\r
289 \r
290         if( pxCommand == NULL )\r
291         {\r
292                 /* Reset the pxCommand pointer back to the start of the list. */\r
293                 pxCommand = &xRegisteredCommands;\r
294         }\r
295 \r
296         /* Return the next command help string, before moving the pointer on to\r
297         the next command in the list. */\r
298         strncpy( pcWriteBuffer, pxCommand->pxCommandLineDefinition->pcHelpString, xWriteBufferLen );\r
299         pxCommand = pxCommand->pxNext;\r
300 \r
301         if( pxCommand == NULL )\r
302         {\r
303                 /* There are no more commands in the list, so there will be no more\r
304                 strings to return after this one and pdFALSE should be returned. */\r
305                 xReturn = pdFALSE;\r
306         }\r
307         else\r
308         {\r
309                 xReturn = pdTRUE;\r
310         }\r
311 \r
312         return xReturn;\r
313 }\r
314 /*-----------------------------------------------------------*/\r
315 \r
316 static int8_t prvGetNumberOfParameters( const char *pcCommandString )\r
317 {\r
318 int8_t cParameters = 0;\r
319 BaseType_t xLastCharacterWasSpace = pdFALSE;\r
320 \r
321         /* Count the number of space delimited words in pcCommandString. */\r
322         while( *pcCommandString != 0x00 )\r
323         {\r
324                 if( ( *pcCommandString ) == ' ' )\r
325                 {\r
326                         if( xLastCharacterWasSpace != pdTRUE )\r
327                         {\r
328                                 cParameters++;\r
329                                 xLastCharacterWasSpace = pdTRUE;\r
330                         }\r
331                 }\r
332                 else\r
333                 {\r
334                         xLastCharacterWasSpace = pdFALSE;\r
335                 }\r
336 \r
337                 pcCommandString++;\r
338         }\r
339 \r
340         /* If the command string ended with spaces, then there will have been too\r
341         many parameters counted. */\r
342         if( xLastCharacterWasSpace == pdTRUE )\r
343         {\r
344                 cParameters--;\r
345         }\r
346 \r
347         /* The value returned is one less than the number of space delimited words,\r
348         as the first word should be the command itself. */\r
349         return cParameters;\r
350 }\r
351 \r