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