123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- #include <string.h>
- #include <stdint.h>
- #include "FreeRTOS.h"
- #include "task.h"
- #include "FreeRTOS_CLI.h"
- typedef struct xCOMMAND_INPUT_LIST
- {
- const CLI_Command_Definition_t *pxCommandLineDefinition;
- struct xCOMMAND_INPUT_LIST *pxNext;
- } CLI_Definition_List_Item_t;
- static portBASE_TYPE prvHelpCommand(cli_state_t *cli_state, int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
- static const CLI_Command_Definition_t xHelpCommand =
- {
- ( const int8_t * const ) "help",
- ( const int8_t * const ) "\r\n\thelp: вывод краткого описания поддерживаемых команд\r\n",
- prvHelpCommand,
- 0
- };
- static CLI_Definition_List_Item_t xRegisteredCommands =
- {
- &xHelpCommand,
- NULL,
- };
- int8_t cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
- portBASE_TYPE FreeRTOS_CLIRegisterCommand( const CLI_Command_Definition_t * const pxCommandToRegister )
- {
- static CLI_Definition_List_Item_t *pxLastCommandInList = &xRegisteredCommands;
- CLI_Definition_List_Item_t *pxNewListItem;
- portBASE_TYPE xReturn = pdFAIL;
-
- configASSERT( pxCommandToRegister );
-
- pxNewListItem = ( CLI_Definition_List_Item_t * ) pvPortMalloc( sizeof( CLI_Definition_List_Item_t ) );
- configASSERT( pxNewListItem );
- if( pxNewListItem != NULL )
- {
- taskENTER_CRITICAL();
- {
-
- pxNewListItem->pxCommandLineDefinition = pxCommandToRegister;
-
- pxNewListItem->pxNext = NULL;
-
- pxLastCommandInList->pxNext = pxNewListItem;
-
- pxLastCommandInList = pxNewListItem;
- }
- taskEXIT_CRITICAL();
- xReturn = pdPASS;
- }
- return xReturn;
- }
- portBASE_TYPE FreeRTOS_CLIProcessCommand(cli_state_t *cli_state, const int8_t * const pcCommandInput, int8_t * pcWriteBuffer, size_t xWriteBufferLen )
- {
- static const CLI_Definition_List_Item_t *pxCommand = NULL;
- portBASE_TYPE xReturn = pdTRUE;
- const int8_t *pcRegisteredCommandString;
- size_t xCommandStringLength;
-
- if( pxCommand == NULL )
- {
-
- for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )
- {
- pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand;
- xCommandStringLength = strlen( ( const char * ) pcRegisteredCommandString );
-
- if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) )
- {
- if( strncmp( ( const char * ) pcCommandInput, ( const char * ) pcRegisteredCommandString, xCommandStringLength ) == 0 )
- {
-
- if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 )
- {
- if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters )
- {
- xReturn = pdFALSE;
- }
- }
- break;
- }
- }
- }
- }
- if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) )
- {
-
- strncpy( ( char * ) pcWriteBuffer, "Неправильно введены параметры команды. Введите \"help\" для просмотра списка поддерживаемых команд.\r\n\r\n", xWriteBufferLen );
- pxCommand = NULL;
- }
- else if( pxCommand != NULL )
- {
-
- xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter(cli_state, pcWriteBuffer, xWriteBufferLen, pcCommandInput );
-
- if( xReturn == pdFALSE )
- {
- pxCommand = NULL;
- }
- }
- else
- {
-
- strncpy( ( char * ) pcWriteBuffer, ( const char * const ) "Команда не поддерживается. Введите \"help\" для просмотра списка поддерживаемых команд.\r\n\r\n", xWriteBufferLen );
- xReturn = pdFALSE;
- }
- return xReturn;
- }
- int8_t *FreeRTOS_CLIGetOutputBuffer( void )
- {
- return cOutputBuffer;
- }
- const int8_t *FreeRTOS_CLIGetParameter( const int8_t *pcCommandString, unsigned portBASE_TYPE uxWantedParameter, unsigned portBASE_TYPE *pxParameterStringLength )
- {
- unsigned portBASE_TYPE uxParametersFound = 0;
- const int8_t *pcReturn = NULL;
- *pxParameterStringLength = 0;
- while( uxParametersFound < uxWantedParameter )
- {
-
- while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
- {
- pcCommandString++;
- }
-
- while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) == ' ' ) )
- {
- pcCommandString++;
- }
-
- if( *pcCommandString != 0x00 )
- {
-
- uxParametersFound++;
- if( uxParametersFound == uxWantedParameter )
- {
-
- pcReturn = pcCommandString;
- while( ( ( *pcCommandString ) != 0x00 ) && ( ( *pcCommandString ) != ' ' ) )
- {
- ( *pxParameterStringLength )++;
- pcCommandString++;
- }
- if( *pxParameterStringLength == 0 )
- {
- pcReturn = NULL;
- }
- break;
- }
- }
- else
- {
- break;
- }
- }
- return pcReturn;
- }
- static portBASE_TYPE prvHelpCommand(cli_state_t *cli_state, int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
- {
- static const CLI_Definition_List_Item_t * pxCommand = NULL;
- signed portBASE_TYPE xReturn;
- ( void ) pcCommandString;
-
- if( pxCommand == NULL )
- {
-
- pxCommand = &xRegisteredCommands;
- }
- memset(pcWriteBuffer, 0, configCOMMAND_INT_MAX_OUTPUT_SIZE);
-
- strncpy( ( char * ) pcWriteBuffer, ( const char * ) pxCommand->pxCommandLineDefinition->pcHelpString, xWriteBufferLen );
- pxCommand = pxCommand->pxNext;
- if( pxCommand == NULL )
- {
-
- xReturn = pdFALSE;
- }
- else
- {
- xReturn = pdTRUE;
- }
- return xReturn;
- }
- int8_t prvGetNumberOfParameters( const int8_t * pcCommandString )
- {
- int8_t cParameters = 0;
- portBASE_TYPE xLastCharacterWasSpace = pdFALSE;
-
- while( *pcCommandString != 0x00 )
- {
- if( ( *pcCommandString ) == ' ' )
- {
- if( xLastCharacterWasSpace != pdTRUE )
- {
- cParameters++;
- xLastCharacterWasSpace = pdTRUE;
- }
- }
- else
- {
- xLastCharacterWasSpace = pdFALSE;
- }
- pcCommandString++;
- }
-
- if( xLastCharacterWasSpace == pdTRUE )
- {
- cParameters--;
- }
-
- return cParameters;
- }
- portBASE_TYPE FreeRTOS_CLICompleteCMDCommand( const int8_t * const pcCommandInput, int8_t * pcWriteBuffer)
- {
- const CLI_Definition_List_Item_t *pxCommand = NULL;
- portBASE_TYPE xReturn = pdFALSE;
- const int8_t *pcRegisteredCommandString;
- size_t xCommandStringLength;
- int8_t args = 0;
- char* compl_world;
- uint8_t cnt_complete = 0 ;
- uint8_t len = 0;
- char* end_compl_word;
- char* end_compl_word2;
- args = prvGetNumberOfParameters(pcCommandInput);
- memset(pcWriteBuffer, 0, configCOMMAND_INT_MAX_OUTPUT_SIZE);
- if(args == 0){
- for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )
- {
- if (strstr ((char *)pxCommand->pxCommandLineDefinition->pcCommand, (char *)pcCommandInput) == (char *)pxCommand->pxCommandLineDefinition->pcCommand) {
- compl_world = (char *)pxCommand->pxCommandLineDefinition->pcCommand;
- compl_world += strlen((char *)pcCommandInput);
- len = strlen(compl_world);
- cnt_complete ++;
- }
- }
- }
- else{
-
- for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext )
- {
- len = 0;
- pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand;
- xCommandStringLength = strlen( ( const char * ) pcRegisteredCommandString );
-
- if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) )
- {
- if( strncmp( ( const char * ) pcCommandInput, ( const char * ) pcRegisteredCommandString, xCommandStringLength ) == 0 )
- {
- compl_world = strstr ((char *)pxCommand->pxCommandLineDefinition->pcHelpString, (char *)pcCommandInput);
- while (compl_world != NULL) {
- compl_world += (strlen((char *)pcCommandInput) - 1 );
- end_compl_word = strstr(compl_world, ":");
- if(end_compl_word != NULL){
- end_compl_word2 = strstr(compl_world, " ");
- if(end_compl_word2 == NULL){
- if(end_compl_word2 < end_compl_word){
- if(end_compl_word2[1] != '<'){
- len = end_compl_word2 - compl_world;
- }
- }
- }
- else
- if(strstr(compl_world, ">") == NULL)
- len = end_compl_word - compl_world;
- }
- compl_world = strstr ((char *)pxCommand->pxCommandLineDefinition->pcHelpString, compl_world);
- cnt_complete ++;
- }
- break;
- }
- }
- }
- }
- if(cnt_complete == 1 && len != 0){
- strncat((char *)pcWriteBuffer, compl_world, len);
- }
- return xReturn;
- }
|