diff options
author | Guodong Xu <guodong.xu@linaro.org> | 2012-06-13 16:28:01 +0800 |
---|---|---|
committer | Guodong Xu <guodong.xu@linaro.org> | 2012-06-13 16:28:01 +0800 |
commit | a7236aace25aca192bfd6b227cc94a1a2ba25296 (patch) | |
tree | 086773fdc4831800be44e7034776153056d342ce | |
parent | 26b144677fc59aaa63d5082f6a273a7f88408a72 (diff) |
Use linked list to manage test results
-rw-r--r-- | main.c | 270 | ||||
-rw-r--r-- | test_commands.h | 7 |
2 files changed, 235 insertions, 42 deletions
@@ -122,6 +122,11 @@ static char *config_cmds[] = { [CFGID_CHECKSUM_DISABLE] = "checksum_disable" }; +static int command_sequencial_count = 0; /* to record total number of commands executed */ +static struct stm_test_run_node_type *stm_test_node_list = NULL; /* list head for test result */ +static struct stm_test_run_node_type *ptr_stm_test_node_last = NULL; /* list tail for test result */ +static bool is_commandline_mode = false; + /* to check and return command index of stm_test_command_list[] stm_channel_config * Input: * - command_name: null terminated string of STM test command_name @@ -738,7 +743,8 @@ int stm_send_msg(int param_num, char** param_array, command_result_type* ptr_com ret = read_channel_config(*(param_array + 1), &tmp_command_result); /* parse transmit byte count */ if (ret == 0) /* when channel config is read correctly */ - ret = parse_transmit_byte_count(tmp_command_result.private_data, &ptr_private_send_msg->prev_tx_count); + ret = parse_transmit_byte_count(tmp_command_result.private_data, + &ptr_private_send_msg->prev_tx_count); FREE_COMMAND_RESULT(&tmp_command_result); /* compose channel_filename */ @@ -753,6 +759,12 @@ int stm_send_msg(int param_num, char** param_array, command_result_type* ptr_com } TDBG("channel_filename is %s\n", channel_filename); + /* check config file exist or not */ + if (check_file_exist(channel_filename) == -1) { + printf("Error: channel file, %s, doesn't exist.\n", channel_filename); + return -1; + } + /* open the channel file for appending */ fp = fopen(channel_filename, "ab"); if(fp == NULL) { /* failure */ @@ -928,22 +940,16 @@ verify_result: return -4; /* should never been here*/ } -/* to log command and test result into stdout. +/* to log one test node result * Input: - * - ptr_command_result: pointer to command result to be logged. + * - ptr_command_result: command result + * - fp_logging: where to printf * Return: * - 0: success * - -1: failure */ -int result_logging(const command_result_type *ptr_command_result) +static int result_logging_one_test_node(command_result_type *ptr_command_result, FILE *fp_logging) { - int ret = -1; - FILE* fp_logging = stdout; /* using stdout as default */ - - if(ptr_command_result == NULL) { /* invalid input */ - TDBG("Invalid input parameter in %s().\n", __FUNCTION__); - return ret; - } /* print delimiter */ fprintf(fp_logging, "%s", STR_COMMAND_RESULT_DELIMITER); @@ -995,6 +1001,7 @@ int result_logging(const command_result_type *ptr_command_result) } else { fprintf(fp_logging, "async_veri_result: N/A\n"); } + #if 0 /* print private data */ /* CAUSION: @@ -1011,10 +1018,30 @@ int result_logging(const command_result_type *ptr_command_result) /* flush */ fflush(fp_logging); - ret = 0; - return ret; + return 0; +} + +/* to log command and test result. + * Return: + * - 0: success + * - -1: failure + */ +int result_logging() +{ + struct stm_test_run_node_type *ptr_test_node = NULL; + FILE* fp_logging = stdout; /* using stdout as default */ + + ptr_test_node = stm_test_node_list; + + while (ptr_test_node != NULL) { /* dump each node */ + result_logging_one_test_node(ptr_test_node->command_result, fp_logging); + ptr_test_node = ptr_test_node->next; + } + + return 0; } + /* to send command to channel's config file * Input: * - param_num: total number of parameters, incl. command_name, in param_array @@ -1058,43 +1085,202 @@ int stm_config_channel(int param_num, char ** param_array, command_result_type* return ret; /* carry back return value from send_request() */ } -static int command_sequencial_count = 0; /* to record total number of commands executed */ - -int main(int argc, char** argv) +/* stm test command execution and verification + * Input: + * - param_num: total number of parameters, incl. command_name, in param_array + * - param_array: an char string array to host all parameters, w/ command_name + * being the first. + * SYNOPSIS: STM_TEST_COMMAND [PARAMETERS_AS_PER_COMMAND_REQUIRED] + * Output: + * - ptr_command_result: command result + * Return: + * - 0: success + * - negartive value: error + */ +int stm_command_processing(int param_num, char** param_array, command_result_type* ptr_command_result) { - int ret = -1; int cmd_index = -1; command_list* current_command = NULL; - command_result_type current_command_result; -/* - pthread_mutex_init(&mux, NULL); - pthread_mutex_lock(&mux); - TDBG("pthread_mutex testing.\n"); - pthread_mutex_unlock(&mux); -*/ + TDBG("In %s, line %d\n", __FUNCTION__, __LINE__); + + /* at least one param is needed */ + if (param_num < 1) + TDBG("Invalid number of param_num, %d. \n", param_num); + + /* check index of test command */ + cmd_index = check_command_index(*param_array); + if (cmd_index < 0) { + printf("Invaild test command: %s\n", *param_array); + return -1; + } + + current_command = &stm_test_command_list[cmd_index]; + + ptr_command_result->current_command = &stm_test_command_list[cmd_index]; + ptr_command_result->sequencial_id = command_sequencial_count; + + /* increase the global command seq count by 1 */ + command_sequencial_count ++; + + /* send command to STM Framework/driver */ + (* current_command->command_consumer_callback)(param_num, param_array, ptr_command_result); + + /* to call sync_verification API if proper */ + if (ptr_command_result->command_execution_result == COMMAND_EXEC_SUCCESS && + current_command->is_sync == true && + current_command->sync_verification_callback != NULL) { + TDBG("to call sync_verification API.\n"); + (* current_command->sync_verification_callback)(param_num, param_array, ptr_command_result); + } + + /* to call async_verification API if proper */ + if (ptr_command_result->command_execution_result == COMMAND_EXEC_SUCCESS && + current_command->is_async == true && + current_command->async_verification_callback != NULL) { + TDBG("to call async_verification API.\n"); + (* current_command->async_verification_callback)(param_num, param_array, ptr_command_result); + } + + return 0; +} + +/* append test result to stm_test_node_list, ptr_stm_test_node_last */ +int append_test_node(int param_num, char** param_array, command_result_type* ptr_command_result) +{ + struct stm_test_run_node_type *ptr_test_node = NULL; + + /* allocate stm_test_run_node */ + ptr_test_node = (struct stm_test_run_node_type *)malloc(sizeof(struct stm_test_run_node_type)); + if (ptr_test_node == NULL) { + printf("Error: memory allocation failure in line %d, %s()\n", __LINE__, __FUNCTION__); + /* TODO free ptr_command_result */ + if (ptr_command_result->private_data != NULL) { + free(ptr_command_result->private_data); + ptr_command_result->private_data = NULL; + } + /* TODO free param_array */ + return -1; + } + + ptr_test_node->param_num = param_num; + ptr_test_node->param_array = param_array; + ptr_test_node->command_result = ptr_command_result; + ptr_test_node->next = NULL; + + /* when not adding the first node */ + if (ptr_stm_test_node_last != NULL) + ptr_stm_test_node_last->next = ptr_test_node; + ptr_stm_test_node_last = ptr_test_node; + + /* at the first test, need to change list head */ + if (stm_test_node_list == NULL) + stm_test_node_list = ptr_stm_test_node_last; + + return 0; +} + +/* to free memory before exit */ +int free_resources() +{ + int i = 0; + struct stm_test_run_node_type *ptr_test_node = NULL; - /* check command line information */ + ptr_test_node = stm_test_node_list; + + if (is_commandline_mode) { /* there is only one node to release */ + /* and no need to free param_array */ + /* free prvate data */ + if (ptr_test_node->command_result->private_data != NULL) { + free(ptr_test_node->command_result->private_data); + ptr_test_node->command_result->private_data = NULL; + } + /* free command result */ + free(ptr_test_node->command_result); + /* free test node */ + free(ptr_test_node); + return 0; + } + + while (stm_test_node_list != NULL) { /* free each node */ + ptr_test_node = stm_test_node_list; + stm_test_node_list = ptr_test_node->next; + /* free prvate data */ + if (ptr_test_node->command_result->private_data != NULL) { + free(ptr_test_node->command_result->private_data); + ptr_test_node->command_result->private_data = NULL; + } + /* free command result */ + free(ptr_test_node->command_result); + /* free test node's param_array */ + for (i = 0; i < ptr_test_node->param_num; i ++) + free(*(ptr_test_node->param_array + i)); + /* free test node */ + free(ptr_test_node); + } + + return 0; +} + +int main(int argc, char** argv) +{ + int ret = -1; + command_result_type *ptr_command_result = NULL; + + /* check and set is_commandline_mode */ if (argc == 1) { + is_commandline_mode = false; print_help(); /* print help information */ - TDBG("\n or \nTest commands come from standard input.\n"); + TDBG("Taking commands from standard input.\n"); /* read test commands from standard input */ /* TODO */ TDBG("Do nothing. \n"); - return ret; + return 0; + } else { + is_commandline_mode = true; } - /* check index of test command */ - cmd_index = check_command_index(argv[1]); - if (cmd_index < 0) { - printf("Invaild test command. Type in \" %s help\" to get guidance.\n", argv[0]); - return ret; + /* allocate for command_result_type */ + ptr_command_result = (command_result_type *)malloc(sizeof(command_result_type)); + if (ptr_command_result == NULL) { + printf("Error: memory allocation failure in line %d, %s()\n", __LINE__, __FUNCTION__); + return -1; } - current_command = &stm_test_command_list[cmd_index]; - /* initialize current_command_result */ - INIT_COMMAND_RESULT(¤t_command_result); + INIT_COMMAND_RESULT(ptr_command_result); + + /* stm test command processing */ + ret = stm_command_processing(argc - 1, argv + 1, ptr_command_result); + + TDBG("In line %d, %s()\n", __LINE__, __FUNCTION__); + + /* append to test node list */ + ret = append_test_node(argc - 1, argv + 1, ptr_command_result); + + TDBG("In line %d, %s()\n", __LINE__, __FUNCTION__); + + /* to print out the command result */ + result_logging(); + + TDBG("In line %d, %s()\n", __LINE__, __FUNCTION__); + + /* to free memory before exit */ + free_resources(); + + TDBG("In line %d, %s()\n", __LINE__, __FUNCTION__); + + /* return */ + return 0; +} + + + +/* + FREE_COMMAND_RESULT(¤t_command_result); +*/ + +#if 0 current_command_result.current_command = &stm_test_command_list[cmd_index]; current_command_result.sequencial_id = command_sequencial_count; @@ -1123,12 +1309,12 @@ int main(int argc, char** argv) (* current_command->async_verification_callback)(); } - /* to record the command result */ - result_logging(¤t_command_result); - - /* to free memory before exit */ - FREE_COMMAND_RESULT(¤t_command_result); +#endif - return 0; -} +/* + pthread_mutex_init(&mux, NULL); + pthread_mutex_lock(&mux); + TDBG("pthread_mutex testing.\n"); + pthread_mutex_unlock(&mux); +*/ diff --git a/test_commands.h b/test_commands.h index 54a11d3..9ea064d 100644 --- a/test_commands.h +++ b/test_commands.h @@ -48,6 +48,13 @@ typedef struct command_result_type { */ } command_result_type; +struct stm_test_run_node_type { + int param_num; /* number of parameters */ + char **param_array; /* array of parameters */ + command_result_type *command_result; /* command execution and verification result */ + struct stm_test_run_node_type *next; /* pointer to next node, as a linked list */ +}; + /* private_data for send_msg */ typedef struct private_send_msg { int prev_tx_count; /* channel's tx byte count, before sending this message */ |