diff --git a/groundStation/src/cli/cli.c b/groundStation/src/cli/cli.c index 5aa3082909e1856ed281091bae4663b1f6c09949..ebde95590a1250cb280f6e8329a81d465d8ecd65 100644 --- a/groundStation/src/cli/cli.c +++ b/groundStation/src/cli/cli.c @@ -52,9 +52,8 @@ int main(int argc, char **argv) // recognize which cli command the user has entered if (cmdID == -1) { command = argv[1]; - for(i = 0; i < MAX_COMMANDS; ++i) { - if (strncmp(command, commandNames[i], strlen(commandNames[i])) == 0) - { + for (i = 0; i < MAX_COMMANDS; ++i) { + if (strncmp(command, commandNames[i], strlen(commandNames[i])) == 0) { cmdID = i; } } @@ -100,7 +99,7 @@ int main(int argc, char **argv) // Call the appropriate function if (useSymlink) { (*cli_functions[cmdID]) (conn, argc, &argv[0]); - }else { + } else { (*cli_functions[cmdID]) (conn, argc-1, &argv[1]); } @@ -111,7 +110,6 @@ int main(int argc, char **argv) return 0; } - /* This function is called by other cli functions to check for a help condition */ int help_check(int argc, char ** argv) { int c; @@ -138,4 +136,102 @@ int help_check(int argc, char ** argv) { } } return needHelp; +} + +int isNumber(char *number) { + int i = 0; + //checking for negative numbers + if (number[0] == '-') + i = 1; + for (; number[i] != 0; i++) + { + //if (number[i] > '9' || number[i] < '0') + if (!isdigit(number[i])) + return 0; + } + return 1; +} + +int convert_to_id(struct backend_conn * conn, char **argv, struct convert_data * convert_data, int conversion_requirement) { + /* variables used to search for id values */ + size_t num_nodes = 0, i; + struct frontend_node_data* search_data; + int search_1 = 0, search_2 = 0; + const struct graph_node_type * node_definition; + + + + if (!isNumber(argv[1])) { + search_1 = 1; + } else { + convert_data->val_1 = atoi(argv[1]); + } + + if (!isNumber(argv[2])) { + search_2 = 1; + } else { + convert_data->val_2 = atoi(argv[2]); + } + + if (!search_1 && !search_2) { + return 0; + } + + search_data = malloc(sizeof((*search_data)) * num_nodes); + + if (frontend_getnodes(conn, &search_data, &num_nodes)) { + return 1; + } + + if (search_1) { + for (i = 0; i < num_nodes; ++i) { + if (strncasecmp(search_data[i].name, argv[1], strlen(search_data[i].name)) == 0) { + convert_data->val_1 = search_data[i].block; + node_definition = blockDefs[search_data[i].type]; + break; + } + } + } + + if (i == num_nodes) + return 1; + + if (search_2) { + const int *num_s2_options; + const char* const* s2_options_arr; + + if (!search_1) { + node_definition = blockDefs[search_data[convert_data->val_1].type]; + } + + switch (conversion_requirement) { + case PARAM: + num_s2_options = &node_definition->n_params; + s2_options_arr = node_definition->param_names; + break; + case INPUT: + num_s2_options = &node_definition->n_inputs; + s2_options_arr = node_definition->input_names; + break; + case OUTPUT: + num_s2_options = &node_definition->n_outputs; + s2_options_arr = node_definition->output_names; + break; + } + + for (i = 0; i < (size_t)*num_s2_options; ++i) { + if (strncasecmp(s2_options_arr[i], + argv[2], + strlen(s2_options_arr[i])) == 0) { + convert_data->val_2 = i; + search_2 = 0; + break; + } + } + } + for(i = 0; i < num_nodes; ++i) { + free(search_data[i].name); + } + free(search_data); + return search_2; } \ No newline at end of file diff --git a/groundStation/src/cli/cli.h b/groundStation/src/cli/cli.h index 7a7d4fdf832f0c516398707f8484c09aeb1857f8..8a14201024f41c39a921beea10f19e36428047af 100644 --- a/groundStation/src/cli/cli.h +++ b/groundStation/src/cli/cli.h @@ -40,7 +40,43 @@ static char* commandNames[MAX_COMMANDS] = { "getnodes", "addnode" }; + +/** + * generic two short struct for use with 'convert_to_id()' + */ +struct convert_data { + int16_t val_1; + int16_t val_2; +}; + +enum conversion_requirements { + INPUT, + PARAM, + OUTPUT +}; +/** + * isNumber takes a char * such as argv[n] and checks to see if it is a number. + * @param number null terminating string (ex. argv[1]) + * @return 1 if 'number' is a number + * 0 if 'number' is not a number + */ +int isNumber(char *number); + +/** + * convert_to_id will take in the convert_data struct and either use the values from argv or + * find the correct values by using the optional strings passed in from argv + * @param conn backend connection struct + * @param argv argv from the cmd line + * @param convert_data saves the necissary converted data in this struct for later use. + * @return returns an error integer + */ +int convert_to_id(struct backend_conn * conn, char **argv, struct convert_data * convert_data, int conversion_requirement); + /* This function is called by other cli functions to check for a help condition */ +/** + * determine if the user asked for help + * @return 1 if help is needed, else 0. + */ int help_check(int argc, char ** argv); #endif /* _CLI_H */ \ No newline at end of file diff --git a/groundStation/src/cli/cli_nodes.c b/groundStation/src/cli/cli_nodes.c index 341a8f05933369847150a9b97d6715a07e454ab9..281ee6fb60dfd66b29c60515748a7fc6c908a344 100644 --- a/groundStation/src/cli/cli_nodes.c +++ b/groundStation/src/cli/cli_nodes.c @@ -37,11 +37,11 @@ int cli_getnodes(struct backend_conn * conn, int argc, char ** argv) { return 1; } - printf("------------------------------------------------\n"); + printf("--------------------------------------------------------------------------------------------------------------\n"); printf("The following %lu Nodes have been found:\n", num_nodes); - printf("------------------------------------------------\n"); - printf("\tBLOCK\t\tTYPE\t\tNAME\n"); - printf("------------------------------------------------\n"); + printf("--------------------------------------------------------------------------------------------------------------\n"); + printf("\tBLOCK\t\tTYPE\t\tNAME\t\tINPUTS\t\tPARAMS\t\tOUTPUTS\n"); + printf("--------------------------------------------------------------------------------------------------------------\n"); for(i = 0; i < num_nodes; ++i) { printf("\t%3d\t\t%3d\t\t%s\t", node_data[i].block, node_data[i].type, node_data[i].name); diff --git a/groundStation/src/cli/cli_output.c b/groundStation/src/cli/cli_output.c index 273b749c3ab085fcdd56d4f1d78963997b895fad..9f4a5fb5fc90c3209c0c3bb3cd81b497503ca302 100644 --- a/groundStation/src/cli/cli_output.c +++ b/groundStation/src/cli/cli_output.c @@ -12,8 +12,8 @@ int cli_getoutput(struct backend_conn * conn, int argc, char ** argv) { if ((needHelp = help_check(argc, argv))) { printf("getoutput gets the output value of a specified block_id and output_id\n"); - printf("Usage Syntax : \n\t./Cli getoutput block_id output_id\n"); - printf("Symlink Usage Syntax : \n\t./getoutput block_id output_id\n\n"); + printf("Usage Syntax : \n\t./Cli getoutput <block_id|'block_name'> <output_id|'output_name'>\n"); + printf("Symlink Usage Syntax : \n\t./getoutput <block_id|'block_name'> <output_id|'output_name'>\n\n"); return 0; } @@ -22,8 +22,9 @@ int cli_getoutput(struct backend_conn * conn, int argc, char ** argv) { return 1; } - output_data.block = atoi(argv[1]); - output_data.output = atoi(argv[2]); + if (convert_to_id(conn, argv, (struct convert_data *)&output_data, OUTPUT)) { + return 1; + } if (frontend_getoutput(conn, &output_data)) { return 1; diff --git a/groundStation/src/cli/cli_param.c b/groundStation/src/cli/cli_param.c index 4cc50b3f6b968181c56cc5ab6d36295e90b52273..4b3b802c40760d3b3e4323761f6cd2bb8d4699e4 100644 --- a/groundStation/src/cli/cli_param.c +++ b/groundStation/src/cli/cli_param.c @@ -1,4 +1,5 @@ #include <stdio.h> +#include <stdlib.h> #include <unistd.h> #include <inttypes.h> @@ -12,8 +13,8 @@ int cli_getparam(struct backend_conn * conn, int argc, char ** argv) { if ((needHelp = help_check(argc, argv))) { printf("getparam gets the param_val for a specified block_id and param_id\n"); - printf("Usage Syntax : \n\t./Cli getparam block_id param_id\n"); - printf("Symlink Usage Syntax : \n\t./getparam block_id param_id\n\n"); + printf("Usage Syntax : \n\t./Cli getparam <block_id|'block_name'> <param_id|'param_name'>\n"); + printf("Symlink Usage Syntax : \n\t./getparam <block_id|'block_name'> <param_id|'param_name'>\n\n"); return 0; } @@ -22,7 +23,7 @@ int cli_getparam(struct backend_conn * conn, int argc, char ** argv) { return 1; } - if (convert_to_id(conn, argv, ¶m_data)) { + if (convert_to_id(conn, argv, (struct convert_data *)¶m_data, PARAM)) { return 1; } @@ -44,8 +45,8 @@ int cli_setparam(struct backend_conn * conn, int argc, char ** argv) { if ((needHelp = help_check(argc, argv))) { printf("setparam sets the param_val for a specified block_id and param_id\n"); - printf("Usage Syntax : \n\t./Cli setparam block_id param_id value\n"); - printf("Symlink Usage Syntax : \n\t./setparam block_id param_id value\n\n"); + printf("Usage Syntax : \n\t./Cli setparam <block_id|'block_name'> <param_id|'param_name'> value\n"); + printf("Symlink Usage Syntax : \n\t./setparam <block_id|'block_name'> <param_id|'param_name'> value\n\n"); return 0; } @@ -55,11 +56,11 @@ int cli_setparam(struct backend_conn * conn, int argc, char ** argv) { } - if (convert_to_id(conn, argv, ¶m_data)) { + if (convert_to_id(conn, argv, (struct convert_data *)¶m_data, PARAM)) { return 1; } - param_data.value = atoi(argv[3]); + param_data.value = atof(argv[3]); if (frontend_setparam(conn, ¶m_data)) { return 1; diff --git a/groundStation/src/cli/cli_param.h b/groundStation/src/cli/cli_param.h index f80b7829257c8ff9a3ea8517fa5a5f6227b604f5..b48f033b8fb38c84a062dcd53f15097f2cf4e001 100644 --- a/groundStation/src/cli/cli_param.h +++ b/groundStation/src/cli/cli_param.h @@ -8,86 +8,6 @@ #include "frontend_nodes.h" #include "graph_blocks.h" - -static int isNumber(char *number) -{ - int i = 0; - //checking for negative numbers - if (number[0] == '-') - i = 1; - for (; number[i] != 0; i++) - { - //if (number[i] > '9' || number[i] < '0') - if (!isdigit(number[i])) - return 0; - } - return 1; -} - -static int convert_to_id(struct backend_conn * conn, char **argv, struct frontend_param_data * param_data) { - /* variables used to search for id values */ - size_t num_nodes = 0, i; - struct frontend_node_data* search_data; - int search_for_block = 0, search_for_param = 0; - const struct graph_node_type * node_definition; - - - if (!isNumber(argv[1])) { - search_for_block = 1; - } else { - param_data->block = atoi(argv[1]); - } - - if (!isNumber(argv[2])) { - search_for_param = 1; - } else { - param_data->param = atoi(argv[2]); - } - - if (!search_for_block && !search_for_param) { - return 0; - } - - search_data = malloc(sizeof((*search_data)) * num_nodes); - - if (frontend_getnodes(conn, &search_data, &num_nodes)) { - return 1; - } - - if (search_for_block) { - for (i = 0; i < num_nodes; ++i) { - if (strncmp(search_data[i].name, argv[1], strlen(search_data[i].name)) == 0) { - param_data->block = search_data[i].block; - node_definition = blockDefs[search_data[i].type]; - break; - } - } - } - - if (i == num_nodes) - return 1; - - if (search_for_param) { - if (!search_for_block) { - node_definition = blockDefs[search_data[param_data->block].type]; - } - for (i = 0; i < (size_t)node_definition->n_params; ++i) { - if (strncmp(node_definition->param_names[i], - argv[2], - strlen(node_definition->param_names[i])) == 0) { - param_data->param = i; - search_for_param = 0; - break; - } - } - } - for(i = 0; i < num_nodes; ++i) { - free(search_data[i].name); - } - free(search_data); - return search_for_param; -} - int cli_getparam(struct backend_conn * conn, int argc, char ** argv); int cli_setparam(struct backend_conn * conn, int argc, char ** argv); diff --git a/groundStation/src/cli/cli_source.c b/groundStation/src/cli/cli_source.c index c7168a6fb25065598f5899db7bfe163ed39e127e..214174224e35d7411e112bbfa77176fed9adb042 100644 --- a/groundStation/src/cli/cli_source.c +++ b/groundStation/src/cli/cli_source.c @@ -22,8 +22,9 @@ int cli_getsource(struct backend_conn * conn, int argc, char ** argv) { return 1; } - source_data.dst_block = atoi(argv[1]); - source_data.dst_input = atoi(argv[2]); + if (convert_to_id(conn, argv, (struct convert_data *)&source_data, INPUT)) { + return 1; + } if (frontend_getsource(conn, &source_data)) { return 1;