]> begriffs open source - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-CLI/FreeRTOS_CLI.c
Change name of the CEC and MEC directory to CORTEX_MPU_CEC_MEC_17xx_51xx_Keil_GCC...
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-CLI / FreeRTOS_CLI.c
1 /*\r
2  * FreeRTOS+CLI V1.0.4 (C) 2014 Real Time Engineers ltd.  All rights reserved.\r
3  *\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
6  *\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
13  *\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
20  *\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
27  * changed.\r
28  *\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
35  *\r
36  * 1 tab == 4 spaces!\r
37  *\r
38  * http://www.FreeRTOS.org\r
39  * http://www.FreeRTOS.org/FreeRTOS-Plus\r
40  *\r
41  */\r
42 \r
43 /* Standard includes. */\r
44 #include <string.h>\r
45 #include <stdint.h>\r
46 \r
47 /* FreeRTOS includes. */\r
48 #include "FreeRTOS.h"\r
49 #include "task.h"\r
50 \r
51 /* Utils includes. */\r
52 #include "FreeRTOS_CLI.h"\r
53 \r
54 /* If the application writer needs to place the buffer used by the CLI at a\r
55 fixed address then set configAPPLICATION_PROVIDES_cOutputBuffer to 1 in\r
56 FreeRTOSConfig.h, then declare an array with the following name and size in \r
57 one of the application files:\r
58         char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];\r
59 */\r
60 #ifndef configAPPLICATION_PROVIDES_cOutputBuffer\r
61         #define configAPPLICATION_PROVIDES_cOutputBuffer 0\r
62 #endif\r
63 \r
64 typedef struct xCOMMAND_INPUT_LIST\r
65 {\r
66         const CLI_Command_Definition_t *pxCommandLineDefinition;\r
67         struct xCOMMAND_INPUT_LIST *pxNext;\r
68 } CLI_Definition_List_Item_t;\r
69 \r
70 /*\r
71  * The callback function that is executed when "help" is entered.  This is the\r
72  * only default command that is always present.\r
73  */\r
74 static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
75 \r
76 /*\r
77  * Return the number of parameters that follow the command name.\r
78  */\r
79 static int8_t prvGetNumberOfParameters( const char *pcCommandString );\r
80 \r
81 /* The definition of the "help" command.  This command is always at the front\r
82 of the list of registered commands. */\r
83 static const CLI_Command_Definition_t xHelpCommand =\r
84 {\r
85         "help",\r
86         "\r\nhelp:\r\n Lists all the registered commands\r\n\r\n",\r
87         prvHelpCommand,\r
88         0\r
89 };\r
90 \r
91 /* The definition of the list of commands.  Commands that are registered are\r
92 added to this list. */\r
93 static CLI_Definition_List_Item_t xRegisteredCommands =\r
94 {\r
95         &xHelpCommand,  /* The first command in the list is always the help command, defined in this file. */\r
96         NULL                    /* The next pointer is initialised to NULL, as there are no other registered commands yet. */\r
97 };\r
98 \r
99 /* A buffer into which command outputs can be written is declared here, rather\r
100 than in the command console implementation, to allow multiple command consoles\r
101 to share the same buffer.  For example, an application may allow access to the\r
102 command interpreter by UART and by Ethernet.  Sharing a buffer is done purely\r
103 to save RAM.  Note, however, that the command console itself is not re-entrant,\r
104 so only one command interpreter interface can be used at any one time.  For that\r
105 reason, no attempt at providing mutual exclusion to the cOutputBuffer array is\r
106 attempted.\r
107 \r
108 configAPPLICATION_PROVIDES_cOutputBuffer is provided to allow the application\r
109 writer to provide their own cOutputBuffer declaration in cases where the\r
110 buffer needs to be placed at a fixed address (rather than by the linker). */\r
111 #if( configAPPLICATION_PROVIDES_cOutputBuffer == 0 )\r
112         static char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];\r
113 #else\r
114         extern char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];\r
115 #endif\r
116 \r
117 \r
118 /*-----------------------------------------------------------*/\r
119 \r
120 BaseType_t FreeRTOS_CLIRegisterCommand( const CLI_Command_Definition_t * const pxCommandToRegister )\r
121 {\r
122 static CLI_Definition_List_Item_t *pxLastCommandInList = &xRegisteredCommands;\r
123 CLI_Definition_List_Item_t *pxNewListItem;\r
124 BaseType_t xReturn = pdFAIL;\r
125 \r
126         /* Check the parameter is not NULL. */\r
127         configASSERT( pxCommandToRegister );\r
128 \r
129         /* Create a new list item that will reference the command being registered. */\r
130         pxNewListItem = ( CLI_Definition_List_Item_t * ) pvPortMalloc( sizeof( CLI_Definition_List_Item_t ) );\r
131         configASSERT( pxNewListItem );\r
132 \r
133         if( pxNewListItem != NULL )\r
134         {\r
135                 taskENTER_CRITICAL();\r
136                 {\r
137                         /* Reference the command being registered from the newly created\r
138                         list item. */\r
139                         pxNewListItem->pxCommandLineDefinition = pxCommandToRegister;\r
140 \r
141                         /* The new list item will get added to the end of the list, so\r
142                         pxNext has nowhere to point. */\r
143                         pxNewListItem->pxNext = NULL;\r
144 \r
145                         /* Add the newly created list item to the end of the already existing\r
146                         list. */\r
147                         pxLastCommandInList->pxNext = pxNewListItem;\r
148 \r
149                         /* Set the end of list marker to the new list item. */\r
150                         pxLastCommandInList = pxNewListItem;\r
151                 }\r
152                 taskEXIT_CRITICAL();\r
153 \r
154                 xReturn = pdPASS;\r
155         }\r
156 \r
157         return xReturn;\r
158 }\r
159 /*-----------------------------------------------------------*/\r
160 \r
161 BaseType_t FreeRTOS_CLIProcessCommand( const char * const pcCommandInput, char * pcWriteBuffer, size_t xWriteBufferLen  )\r
162 {\r
163 static const CLI_Definition_List_Item_t *pxCommand = NULL;\r
164 BaseType_t xReturn = pdTRUE;\r
165 const char *pcRegisteredCommandString;\r
166 size_t xCommandStringLength;\r
167 \r
168         /* Note:  This function is not re-entrant.  It must not be called from more\r
169         thank one task. */\r
170 \r
171         if( pxCommand == NULL )\r
172         {\r
173                 /* Search for the command string in the list of registered commands. */\r
174                 for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )\r
175                 {\r
176                         pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand;\r
177                         xCommandStringLength = strlen( pcRegisteredCommandString );\r
178 \r
179                         /* To ensure the string lengths match exactly, so as not to pick up\r
180                         a sub-string of a longer command, check the byte after the expected\r
181                         end of the string is either the end of the string or a space before\r
182                         a parameter. */\r
183                         if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) )\r
184                         {\r
185                                 if( strncmp( pcCommandInput, pcRegisteredCommandString, xCommandStringLength ) == 0 )\r
186                                 {\r
187                                         /* The command has been found.  Check it has the expected\r
188                                         number of parameters.  If cExpectedNumberOfParameters is -1,\r
189                                         then there could be a variable number of parameters and no\r
190                                         check is made. */\r
191                                         if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 )\r
192                                         {\r
193                                                 if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters )\r
194                                                 {\r
195                                                         xReturn = pdFALSE;\r
196                                                 }\r
197                                         }\r
198 \r
199                                         break;\r
200                                 }\r
201                         }\r
202                 }\r
203         }\r
204 \r
205         if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) )\r
206         {\r
207                 /* The command was found, but the number of parameters with the command\r
208                 was incorrect. */\r
209                 strncpy( pcWriteBuffer, "Incorrect command parameter(s).  Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen );\r
210                 pxCommand = NULL;\r
211         }\r
212         else if( pxCommand != NULL )\r
213         {\r
214                 /* Call the callback function that is registered to this command. */\r
215                 xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput );\r
216 \r
217                 /* If xReturn is pdFALSE, then no further strings will be returned\r
218                 after this one, and     pxCommand can be reset to NULL ready to search\r
219                 for the next entered command. */\r
220                 if( xReturn == pdFALSE )\r
221                 {\r
222                         pxCommand = NULL;\r
223                 }\r
224         }\r
225         else\r
226         {\r
227                 /* pxCommand was NULL, the command was not found. */\r
228                 strncpy( pcWriteBuffer, "Command not recognised.  Enter 'help' to view a list of available commands.\r\n\r\n", xWriteBufferLen );\r
229                 xReturn = pdFALSE;\r
230         }\r
231 \r
232         return xReturn;\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 char *FreeRTOS_CLIGetOutputBuffer( void )\r
237 {\r
238         return cOutputBuffer;\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 const char *FreeRTOS_CLIGetParameter( const char *pcCommandString, UBaseType_t uxWantedParameter, BaseType_t *pxParameterStringLength )\r
243 {\r
244 UBaseType_t uxParametersFound = 0;\r
245 const char *pcReturn = NULL;\r
246 \r
247         *pxParameterStringLength = 0;\r
248 \r
249         while( uxParametersFound < uxWantedParameter )\r
250         {\r
251                 /* Index the character pointer past the current word.  If this is the start\r
252                 of the command string then the first word is the command itself. */\r
253                 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )\r
254                 {\r
255                         pcCommandString++;\r
256                 }\r
257 \r
258                 /* Find the start of the next string. */\r
259                 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) == ' ' ) )\r
260                 {\r
261                         pcCommandString++;\r
262                 }\r
263 \r
264                 /* Was a string found? */\r
265                 if( *pcCommandString != 0x00 )\r
266                 {\r
267                         /* Is this the start of the required parameter? */\r
268                         uxParametersFound++;\r
269 \r
270                         if( uxParametersFound == uxWantedParameter )\r
271                         {\r
272                                 /* How long is the parameter? */\r
273                                 pcReturn = pcCommandString;\r
274                                 while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )\r
275                                 {\r
276                                         ( *pxParameterStringLength )++;\r
277                                         pcCommandString++;\r
278                                 }\r
279 \r
280                                 if( *pxParameterStringLength == 0 )\r
281                                 {\r
282                                         pcReturn = NULL;\r
283                                 }\r
284 \r
285                                 break;\r
286                         }\r
287                 }\r
288                 else\r
289                 {\r
290                         break;\r
291                 }\r
292         }\r
293 \r
294         return pcReturn;\r
295 }\r
296 /*-----------------------------------------------------------*/\r
297 \r
298 static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
299 {\r
300 static const CLI_Definition_List_Item_t * pxCommand = NULL;\r
301 BaseType_t xReturn;\r
302 \r
303         ( void ) pcCommandString;\r
304 \r
305         if( pxCommand == NULL )\r
306         {\r
307                 /* Reset the pxCommand pointer back to the start of the list. */\r
308                 pxCommand = &xRegisteredCommands;\r
309         }\r
310 \r
311         /* Return the next command help string, before moving the pointer on to\r
312         the next command in the list. */\r
313         strncpy( pcWriteBuffer, pxCommand->pxCommandLineDefinition->pcHelpString, xWriteBufferLen );\r
314         pxCommand = pxCommand->pxNext;\r
315 \r
316         if( pxCommand == NULL )\r
317         {\r
318                 /* There are no more commands in the list, so there will be no more\r
319                 strings to return after this one and pdFALSE should be returned. */\r
320                 xReturn = pdFALSE;\r
321         }\r
322         else\r
323         {\r
324                 xReturn = pdTRUE;\r
325         }\r
326 \r
327         return xReturn;\r
328 }\r
329 /*-----------------------------------------------------------*/\r
330 \r
331 static int8_t prvGetNumberOfParameters( const char *pcCommandString )\r
332 {\r
333 int8_t cParameters = 0;\r
334 BaseType_t xLastCharacterWasSpace = pdFALSE;\r
335 \r
336         /* Count the number of space delimited words in pcCommandString. */\r
337         while( *pcCommandString != 0x00 )\r
338         {\r
339                 if( ( *pcCommandString ) == ' ' )\r
340                 {\r
341                         if( xLastCharacterWasSpace != pdTRUE )\r
342                         {\r
343                                 cParameters++;\r
344                                 xLastCharacterWasSpace = pdTRUE;\r
345                         }\r
346                 }\r
347                 else\r
348                 {\r
349                         xLastCharacterWasSpace = pdFALSE;\r
350                 }\r
351 \r
352                 pcCommandString++;\r
353         }\r
354 \r
355         /* If the command string ended with spaces, then there will have been too\r
356         many parameters counted. */\r
357         if( xLastCharacterWasSpace == pdTRUE )\r
358         {\r
359                 cParameters--;\r
360         }\r
361 \r
362         /* The value returned is one less than the number of space delimited words,\r
363         as the first word should be the command itself. */\r
364         return cParameters;\r
365 }\r
366 \r