[tuxdroid-svn] r1068 - software_suite_v2/middleware/tuxdriver/trunk/src
Status: Beta
Brought to you by:
ks156
From: eFfeM <c2m...@c2...> - 2008-05-02 10:06:50
|
Author: eFfeM Date: 2008-05-02 12:06:55 +0200 (Fri, 02 May 2008) New Revision: 1068 Modified: software_suite_v2/middleware/tuxdriver/trunk/src/tux_cmd_parser.c Log: cleanup up; some small optimisations Modified: software_suite_v2/middleware/tuxdriver/trunk/src/tux_cmd_parser.c =================================================================== --- software_suite_v2/middleware/tuxdriver/trunk/src/tux_cmd_parser.c 2008-05-02 09:58:21 UTC (rev 1067) +++ software_suite_v2/middleware/tuxdriver/trunk/src/tux_cmd_parser.c 2008-05-02 10:06:55 UTC (rev 1068) @@ -44,10 +44,10 @@ #define NRCMDS 256 -#define check_result(funct_res) if (funct_res) \ - return E_TUXDRV_NOERROR; \ - else \ - return E_TUXDRV_INVALIDPARAMETER; +#define check_result(funct_res) if (funct_res) \ + return E_TUXDRV_NOERROR; \ + else \ + return E_TUXDRV_INVALIDPARAMETER; typedef struct { double timeout; @@ -109,10 +109,10 @@ tux_cmd_parser_get_tokens(char *src_str, tokens_t *toks, int max_tokens, char *delimiters) { - char *p; - char *pnext; - int len; - int cnt = 0; + char *p; /* pointer to the next token */ + char *pnext; /* pointer to the next delimiter */ + int len; /* length of the next token */ + int cnt = 0; /* nr of tokens processed */ p = src_str; @@ -124,6 +124,7 @@ { while (1) { + /* get the next delimiter */ pnext = strpbrk(p, delimiters); if (pnext) { @@ -138,6 +139,7 @@ } else { + /* no next delimiter, so copy all the remaining strings */ strcpy((*toks)[cnt], p); cnt++; break; @@ -275,27 +277,15 @@ { unsigned char data[TUX_SEND_LENGTH] = {0, 0, 0, 0, 0}; int r = 0; - - if (hex_to_uint8(tokens[0], &data[0])) + int i; + + for (i = 0; i < TUX_SEND_LENGTH; i++) { - r++; + if (hex_to_uint8(tokens[i], &data[i])) + { + r++; + } } - if (hex_to_uint8(tokens[1], &data[1])) - { - r++; - } - if (hex_to_uint8(tokens[2], &data[2])) - { - r++; - } - if (hex_to_uint8(tokens[3], &data[3])) - { - r++; - } - if (hex_to_uint8(tokens[4], &data[4])) - { - r++; - } if (r < TUX_SEND_LENGTH) { @@ -851,16 +841,20 @@ /* Clear user cmd */ memset(&user_cmd_stack, 0, sizeof(cmd_stack_t)); - /* Clear system cmd */ + /* process all pending system commands */ for (i = 0; i < NRCMDS; i++) { if (strcmp(sys_cmd_stack.cmd_list[i].cmd, "")) { tux_cmd_parser_parse_command(sys_cmd_stack.cmd_list[i].cmd); - memset(&sys_cmd_stack.cmd_list[i], 0, sizeof(delay_cmd_t)); + /* no need to execute the following command as we are going + to clear the complete stack after this for loop + memset(&sys_cmd_stack.cmd_list[i], 0, sizeof(delay_cmd_t)); + */ } } + /* Clear system cmd */ memset(&sys_cmd_stack, 0, sizeof(cmd_stack_t)); #ifdef USE_MUTEX @@ -890,7 +884,12 @@ if (curtime >= user_cmd_stack.cmd_list[i].timeout) { tux_cmd_parser_parse_command(user_cmd_stack.cmd_list[i].cmd); - memset(&user_cmd_stack.cmd_list[i], 0, sizeof(delay_cmd_t)); + /* next two commands are faster than a memset + writing a null byte to the first char of cmd is sufficient + to make it an empty string + */ + user_cmd_stack.cmd_list[i].timeout = 0; + user_cmd_stack.cmd_list[i].cmd[0] = 0; } } if (strcmp(sys_cmd_stack.cmd_list[i].cmd, "")) @@ -898,7 +897,8 @@ if (curtime >= sys_cmd_stack.cmd_list[i].timeout) { tux_cmd_parser_parse_command(sys_cmd_stack.cmd_list[i].cmd); - memset(&sys_cmd_stack.cmd_list[i], 0, sizeof(delay_cmd_t)); + sys_cmd_stack.cmd_list[i].timeout = 0; + sys_cmd_stack.cmd_list[i].cmd[0] = 0; } } } @@ -937,21 +937,24 @@ { const char lex_ret[] = "\n"; char *line_tmp; - char macro[8192]; - TuxDrvError ret; - TuxDrvError result = E_TUXDRV_NOERROR; + char macro[CMDSIZE]; + TuxDrvError ret = E_TUXDRV_NOERROR; #ifdef USE_MUTEX mutex_lock(__macro_mutex); #endif + /* + strtok is used, this modifies the string, hence the copy + if it is ok to write 0 bytes into the argument no copy is needed + */ strcpy(macro, macro_str); if ((line_tmp = strtok(macro, lex_ret)) != NULL) { ret = parse_line(line_tmp); if (ret != E_TUXDRV_NOERROR) { - result = ret; + return(ret); } while ((line_tmp = strtok(NULL, lex_ret)) != NULL) @@ -959,7 +962,6 @@ ret = parse_line(line_tmp); if (ret != E_TUXDRV_NOERROR) { - result = ret; break; } } @@ -969,7 +971,7 @@ mutex_unlock(__macro_mutex); #endif - return result; + return ret; } /** @@ -980,8 +982,7 @@ { char line[CMDSIZE] = ""; FILE *macro_file; - TuxDrvError ret; - TuxDrvError result = E_TUXDRV_NOERROR; + TuxDrvError ret = E_TUXDRV_NOERROR; #ifdef USE_MUTEX mutex_lock(__macro_mutex); @@ -996,19 +997,19 @@ ret = parse_line(line); if (ret != E_TUXDRV_NOERROR) { - result = ret; + break; } } fclose(macro_file); } else { - result = E_TUXDRV_FILEERROR; + ret = E_TUXDRV_FILEERROR; } #ifdef USE_MUTEX mutex_unlock(__macro_mutex); #endif - return result; + return ret; } |