/* help function */ static int help_command( int argc, char* argv[] ) { const command_t* cmd_ptr; cmd_err_t err = ERR_CMD_OK; uint32_t eg_sel = 0; int table_index; switch ( argc ) { case 0: case 1: printf( "Console Commands:\n" ); for ( table_index = 0; table_index < cons.command_table_count; table_index ++ ) { for ( cmd_ptr = cons.console_command_table_array[table_index]; cmd_ptr->name != NULL; cmd_ptr++ ) { if ( ( cmd_ptr->format != NULL ) || ( cmd_ptr->brief != NULL ) ) { if ( cmd_ptr->format != NULL ) { printf( " %s%c%s\n", cmd_ptr->name, cons.console_delimit_string[0], cmd_ptr->format ); } else { printf( " %s\n", cmd_ptr->name ); } if ( cmd_ptr->brief != NULL ) { printf( " - %s\n", cmd_ptr->brief ); } } } } break; default: /* greater than 2 */ eg_sel = generic_string_to_unsigned( argv[2] ); /* Disables Eclipse static analysis warning */ /* Intentional drop through */ /* no break */ case 2: err = ERR_UNKNOWN_CMD; if ( (cmd_ptr = console_lookup_command(argv[1])) != NULL) { if ( cmd_ptr->help_example != NULL ) { err = cmd_ptr->help_example( argv[1], eg_sel ); } else { err = ERR_CMD_OK; printf( "No example available for %s\n\n", argv[1] ); } } break; } return err; }
cmd_err_t console_parse_cmd( const char* line ) { const command_t* cmd_ptr = NULL; char* params[MAX_PARAMS]; uint32_t param_cnt = 0; char* saveptr = NULL; char copy[strlen(line) + 1]; /* Copy original buffer into local buffer, as tokenize will change the original string */ strcpy( copy, line ); cmd_err_t err = ERR_CMD_OK; /* First call to strtok. */ params[param_cnt++] = strtok_r( copy, cons.console_delimit_string, &saveptr ); if ( params[0] == NULL ) /* no command entered */ { err = ERR_NO_CMD; } else { /* find the command */ if ( (cmd_ptr = console_lookup_command(params[0])) == NULL ) { err = ERR_UNKNOWN_CMD; } else if ( strcmp(cmd_ptr->name, "loop") == 0 ) { loop_command( line ); } else { uint32_t i = 0; /* determine argument delimit string */ const char* delimit; if ( cmd_ptr->delimit != NULL ) { delimit = cmd_ptr->delimit; } else { delimit = cons.console_delimit_string; } /* parse arguments */ while ( saveptr != NULL && saveptr[ 0 ] != '\0' ) { wiced_bool_t encap_flag = WICED_FALSE; /* Check for delimiters first */ while ( saveptr[ 0 ] != '\0' ) { wiced_bool_t delimiter_flag = WICED_FALSE; for ( i = 0; i < strlen( delimit ); i++ ) { if ( saveptr[ 0 ] == delimit[ i ] ) { saveptr++; delimiter_flag = WICED_TRUE; break; } } if ( delimiter_flag == WICED_FALSE ) { break; } } /* Then check for encapsulation */ for ( i = 0; i < ( sizeof( console_encapsulate_string ) / sizeof( console_encapsulate_string[ 0 ] ) ); i++ ) { /* If encapsulated, get encapsulated token */ if ( saveptr[ 0 ] == console_encapsulate_string[ i ][ 0 ] ) { encap_flag = WICED_TRUE; params[ param_cnt ] = strtok_r( NULL, (char *) console_encapsulate_string[ i ], &saveptr ); break; } } /* Else not encapsulated, check for token */ if ( encap_flag == WICED_FALSE ) { params[ param_cnt ] = strtok_r( NULL, (char *) delimit, &saveptr ); } if ( params[ param_cnt ] != NULL ) { param_cnt++; if ( param_cnt > ( MAX_PARAMS - 1 ) ) { err = ERR_TOO_MANY_ARGS; break; } } } if ( err == ERR_CMD_OK ) { params[ param_cnt ] = NULL; } /* check arguments */ if ( ( param_cnt - 1 ) < cmd_ptr->arg_count ) { err = ERR_INSUFFICENT_ARGS; } /* run command */ if ( ( err == ERR_CMD_OK ) && ( cmd_ptr->command != NULL ) ) { err = (cmd_err_t) cmd_ptr->command( param_cnt, params ); } } /* process errors */ if ( err != ERR_CMD_OK ) { if ((err <=0) && (err > ERR_LAST_ERROR)) { printf( "ERROR: %s\r\n", console_error_strings[-err] ); } if ( err != ERR_UNKNOWN_CMD ) { printf("Usage: %s %s\r\n",cmd_ptr->name, cmd_ptr->format); } } } return err; }
void console_parse_command(char* command, size_t commandLen) { char *key = NULL, *value = NULL; uint8_t valueLen = 0; Serial *pSer = &g_serial; // A simple hack to command "!5" #if CFG_BEACON_TEST if(commandLen > 0 && command[0] == '!') { cmd_test_send(pSer, command+1,commandLen - 1); return; } #endif if(commandLen >0 && command[0] == '?') { cmd_info(pSer,0,0); #if CONSOLE_HELP_COMMAND_ENABLED cmd_help(pSer,0,0); #endif return; } //TinyAPRS AT Command Handler if(commandLen >=6 && (command[0] == 'A' || command[0] == 'a') && (command[1] == 'T' || command[1] == 't') && (command[2] == '+') ) { // looking for the '=' char* t = NULL; uint8_t i = 3; for(; i < commandLen; i++) { if(command[i] == '=') { t = command + i; break; } } if(t != NULL) { *t = 0; // split the key=value string into 2 strings. key = command + 3; value = t + 1; valueLen = strlen(value); } } // Compatible with OT2/Other TNCs KISS init command else if( (commandLen >=10) && (strcasecmp_P(command,PSTR("AMODE KISS")) == 0)) { // enter the kiss mode // reuse the existing command buffer key = command + 6; key[4] = 0; value = command; value[0] = '1'; value[1] = 0; valueLen = 1; } else if( (commandLen >=7) && (strcasecmp_P(command,PSTR("KISS ON")) == 0)) { key = command; key[4] = 0; value = command + 5; value[0] = '1'; value[1] = 0; valueLen = 1; } if(key == NULL && value == NULL) { // bail out SERIAL_PRINTF_P(pSer,PSTR("INVALID CMD: %.*s\r\n"),commandLen,command); return; } // look the command registry // convert to upper case strupr(key); PFUN_CMD_HANDLER fun = console_lookup_command(key); if(fun) { if(!fun(pSer, value, valueLen)) { // handle failure, should be invalid values SERIAL_PRINT_P(pSer,PSTR("INVALID CMD VALUE\r\n")); // user input command is parsed but the value is not valid } } else { SERIAL_PRINTF_P(pSer,PSTR("UNKNOWN CMD: %.*s\r\n"),commandLen,command); } return; }