FreeRTOS_CLI.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * FreeRTOS+CLI V1.0.4
  3. * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * http://www.FreeRTOS.org
  23. * http://aws.amazon.com/freertos
  24. *
  25. * 1 tab == 4 spaces!
  26. */
  27. /* Standard includes. */
  28. #include <string.h>
  29. #include <stdint.h>
  30. /* FreeRTOS includes. */
  31. #include "FreeRTOS.h"
  32. #include "task.h"
  33. /* Utils includes. */
  34. #include "FreeRTOS_CLI.h"
  35. /* If the application writer needs to place the buffer used by the CLI at a
  36. fixed address then set configAPPLICATION_PROVIDES_cOutputBuffer to 1 in
  37. FreeRTOSConfig.h, then declare an array with the following name and size in
  38. one of the application files:
  39. char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
  40. */
  41. #ifndef configAPPLICATION_PROVIDES_cOutputBuffer
  42. #define configAPPLICATION_PROVIDES_cOutputBuffer 0
  43. #endif
  44. typedef struct xCOMMAND_INPUT_LIST
  45. {
  46. const CLI_Command_Definition_t *pxCommandLineDefinition;
  47. struct xCOMMAND_INPUT_LIST *pxNext;
  48. } CLI_Definition_List_Item_t;
  49. /*
  50. * The callback function that is executed when "help" is entered. This is the
  51. * only default command that is always present.
  52. */
  53. static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
  54. /*
  55. * Return the number of parameters that follow the command name.
  56. */
  57. static int8_t prvGetNumberOfParameters( const char *pcCommandString );
  58. /* The definition of the "help" command. This command is always at the front
  59. of the list of registered commands. */
  60. static const CLI_Command_Definition_t xHelpCommand =
  61. {
  62. "help",
  63. "\r\nhelp:\r\n Lists all the registered commands\r\n\r\n",
  64. prvHelpCommand,
  65. 0
  66. };
  67. /* The definition of the list of commands. Commands that are registered are
  68. added to this list. */
  69. static CLI_Definition_List_Item_t xRegisteredCommands =
  70. {
  71. &xHelpCommand, /* The first command in the list is always the help command, defined in this file. */
  72. NULL /* The next pointer is initialised to NULL, as there are no other registered commands yet. */
  73. };
  74. /* A buffer into which command outputs can be written is declared here, rather
  75. than in the command console implementation, to allow multiple command consoles
  76. to share the same buffer. For example, an application may allow access to the
  77. command interpreter by UART and by Ethernet. Sharing a buffer is done purely
  78. to save RAM. Note, however, that the command console itself is not re-entrant,
  79. so only one command interpreter interface can be used at any one time. For that
  80. reason, no attempt at providing mutual exclusion to the cOutputBuffer array is
  81. attempted.
  82. configAPPLICATION_PROVIDES_cOutputBuffer is provided to allow the application
  83. writer to provide their own cOutputBuffer declaration in cases where the
  84. buffer needs to be placed at a fixed address (rather than by the linker). */
  85. #if( configAPPLICATION_PROVIDES_cOutputBuffer == 0 )
  86. static char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
  87. #else
  88. extern char cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
  89. #endif
  90. /*-----------------------------------------------------------*/
  91. BaseType_t FreeRTOS_CLIRegisterCommand( const CLI_Command_Definition_t * const pxCommandToRegister )
  92. {
  93. static CLI_Definition_List_Item_t *pxLastCommandInList = &xRegisteredCommands;
  94. CLI_Definition_List_Item_t *pxNewListItem;
  95. BaseType_t xReturn = pdFAIL;
  96. /* Check the parameter is not NULL. */
  97. configASSERT( pxCommandToRegister );
  98. /* Create a new list item that will reference the command being registered. */
  99. pxNewListItem = ( CLI_Definition_List_Item_t * ) pvPortMalloc( sizeof( CLI_Definition_List_Item_t ) );
  100. configASSERT( pxNewListItem );
  101. if( pxNewListItem != NULL )
  102. {
  103. taskENTER_CRITICAL();
  104. {
  105. /* Reference the command being registered from the newly created
  106. list item. */
  107. pxNewListItem->pxCommandLineDefinition = pxCommandToRegister;
  108. /* The new list item will get added to the end of the list, so
  109. pxNext has nowhere to point. */
  110. pxNewListItem->pxNext = NULL;
  111. /* Add the newly created list item to the end of the already existing
  112. list. */
  113. pxLastCommandInList->pxNext = pxNewListItem;
  114. /* Set the end of list marker to the new list item. */
  115. pxLastCommandInList = pxNewListItem;
  116. }
  117. taskEXIT_CRITICAL();
  118. xReturn = pdPASS;
  119. }
  120. return xReturn;
  121. }
  122. /*-----------------------------------------------------------*/
  123. BaseType_t FreeRTOS_CLIProcessCommand( const char * const pcCommandInput, char * pcWriteBuffer, size_t xWriteBufferLen )
  124. {
  125. static const CLI_Definition_List_Item_t *pxCommand = NULL;
  126. BaseType_t xReturn = pdTRUE;
  127. const char *pcRegisteredCommandString;
  128. size_t xCommandStringLength;
  129. /* Note: This function is not re-entrant. It must not be called from more
  130. thank one task. */
  131. if( pxCommand == NULL )
  132. {
  133. /* Search for the command string in the list of registered commands. */
  134. for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )
  135. {
  136. pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand;
  137. xCommandStringLength = strlen( pcRegisteredCommandString );
  138. /* To ensure the string lengths match exactly, so as not to pick up
  139. a sub-string of a longer command, check the byte after the expected
  140. end of the string is either the end of the string or a space before
  141. a parameter. */
  142. if( strncmp( pcCommandInput, pcRegisteredCommandString, xCommandStringLength ) == 0 )
  143. {
  144. if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) )
  145. {
  146. /* The command has been found. Check it has the expected
  147. number of parameters. If cExpectedNumberOfParameters is -1,
  148. then there could be a variable number of parameters and no
  149. check is made. */
  150. if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 )
  151. {
  152. if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters )
  153. {
  154. xReturn = pdFALSE;
  155. }
  156. if (pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters == 0) {
  157. xReturn = pdTRUE;
  158. }
  159. }
  160. break;
  161. }
  162. }
  163. }
  164. }
  165. if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) )
  166. {
  167. /* The command was found, but the number of parameters with the command
  168. was incorrect. */
  169. strncpy( pcWriteBuffer, "Incorrect command parameter(s). Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen );
  170. pxCommand = NULL;
  171. }
  172. else if( pxCommand != NULL )
  173. {
  174. /* Call the callback function that is registered to this command. */
  175. xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput );
  176. /* If xReturn is pdFALSE, then no further strings will be returned
  177. after this one, and pxCommand can be reset to NULL ready to search
  178. for the next entered command. */
  179. if( xReturn == pdFALSE )
  180. {
  181. pxCommand = NULL;
  182. }
  183. }
  184. else
  185. {
  186. /* pxCommand was NULL, the command was not found. */
  187. strncpy( pcWriteBuffer, "Command not recognised. Enter 'help' to view a list of available commands.\r\n\r\n", xWriteBufferLen );
  188. xReturn = pdFALSE;
  189. }
  190. return xReturn;
  191. }
  192. /*-----------------------------------------------------------*/
  193. char *FreeRTOS_CLIGetOutputBuffer( void )
  194. {
  195. return cOutputBuffer;
  196. }
  197. /*-----------------------------------------------------------*/
  198. const char *FreeRTOS_CLIGetParameter( const char *pcCommandString, UBaseType_t uxWantedParameter, BaseType_t *pxParameterStringLength )
  199. {
  200. UBaseType_t uxParametersFound = 0;
  201. const char *pcReturn = NULL;
  202. *pxParameterStringLength = 0;
  203. while( uxParametersFound < uxWantedParameter )
  204. {
  205. /* Index the character pointer past the current word. If this is the start
  206. of the command string then the first word is the command itself. */
  207. while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
  208. {
  209. pcCommandString++;
  210. }
  211. /* Find the start of the next string. */
  212. while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) == ' ' ) )
  213. {
  214. pcCommandString++;
  215. }
  216. /* Was a string found? */
  217. if( *pcCommandString != 0x00 )
  218. {
  219. /* Is this the start of the required parameter? */
  220. uxParametersFound++;
  221. if( uxParametersFound == uxWantedParameter )
  222. {
  223. /* How long is the parameter? */
  224. pcReturn = pcCommandString;
  225. while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
  226. {
  227. ( *pxParameterStringLength )++;
  228. pcCommandString++;
  229. }
  230. if( *pxParameterStringLength == 0 )
  231. {
  232. pcReturn = NULL;
  233. }
  234. break;
  235. }
  236. }
  237. else
  238. {
  239. break;
  240. }
  241. }
  242. return pcReturn;
  243. }
  244. /*-----------------------------------------------------------*/
  245. static BaseType_t prvHelpCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
  246. {
  247. static const CLI_Definition_List_Item_t * pxCommand = NULL;
  248. BaseType_t xReturn;
  249. ( void ) pcCommandString;
  250. if( pxCommand == NULL )
  251. {
  252. /* Reset the pxCommand pointer back to the start of the list. */
  253. pxCommand = &xRegisteredCommands;
  254. }
  255. /* Return the next command help string, before moving the pointer on to
  256. the next command in the list. */
  257. strncpy( pcWriteBuffer, pxCommand->pxCommandLineDefinition->pcHelpString, xWriteBufferLen );
  258. pxCommand = pxCommand->pxNext;
  259. if( pxCommand == NULL )
  260. {
  261. /* There are no more commands in the list, so there will be no more
  262. strings to return after this one and pdFALSE should be returned. */
  263. xReturn = pdFALSE;
  264. }
  265. else
  266. {
  267. xReturn = pdTRUE;
  268. }
  269. return xReturn;
  270. }
  271. /*-----------------------------------------------------------*/
  272. static int8_t prvGetNumberOfParameters( const char *pcCommandString )
  273. {
  274. int8_t cParameters = 0;
  275. BaseType_t xLastCharacterWasSpace = pdFALSE;
  276. /* Count the number of space delimited words in pcCommandString. */
  277. while( *pcCommandString != 0x00 )
  278. {
  279. if( ( *pcCommandString ) == ' ' )
  280. {
  281. if( xLastCharacterWasSpace != pdTRUE )
  282. {
  283. cParameters++;
  284. xLastCharacterWasSpace = pdTRUE;
  285. }
  286. }
  287. else
  288. {
  289. xLastCharacterWasSpace = pdFALSE;
  290. }
  291. pcCommandString++;
  292. }
  293. /* If the command string ended with spaces, then there will have been too
  294. many parameters counted. */
  295. if( xLastCharacterWasSpace == pdTRUE )
  296. {
  297. cParameters--;
  298. }
  299. /* The value returned is one less than the number of space delimited words,
  300. as the first word should be the command itself. */
  301. return cParameters;
  302. }