[tuxdroid-svn] r971 - software_suite_v2/middleware/tuxdriver/trunk/src
Status: Beta
Brought to you by:
ks156
From: eFfeM <c2m...@c2...> - 2008-04-19 17:10:35
|
Author: eFfeM Date: 2008-04-19 19:10:33 +0200 (Sat, 19 Apr 2008) New Revision: 971 Modified: software_suite_v2/middleware/tuxdriver/trunk/src/tux_cmd_parser.c software_suite_v2/middleware/tuxdriver/trunk/src/tux_cmd_parser.h Log: intermediate commit (cleanup, some optimisations) this one is fully functional but I will be working some more on it Modified: software_suite_v2/middleware/tuxdriver/trunk/src/tux_cmd_parser.c =================================================================== --- software_suite_v2/middleware/tuxdriver/trunk/src/tux_cmd_parser.c 2008-04-19 15:28:53 UTC (rev 970) +++ software_suite_v2/middleware/tuxdriver/trunk/src/tux_cmd_parser.c 2008-04-19 17:10:33 UTC (rev 971) @@ -18,35 +18,47 @@ * 02111-1307, USA. */ +#include <errno.h> +#include <stdlib.h> #include <stdio.h> -#include <stdlib.h> +#include <string.h> #include <time.h> -#include <string.h> -#include <errno.h> + #ifdef USE_MUTEX # include "threading_uniform.h" #endif -#include "tux_misc.h" + +#include "log.h" +#include "tux_audio.h" #include "tux_cmd_parser.h" -#include "tux_usb.h" +#include "tux_error.h" #include "tux_eyes.h" +#include "tux_leds.h" +#include "tux_misc.h" #include "tux_mouth.h" -#include "tux_wings.h" +#include "tux_sound_flash.h" #include "tux_spinning.h" +#include "tux_usb.h" #include "tux_user_inputs.h" -#include "tux_leds.h" -#include "tux_sound_flash.h" -#include "tux_audio.h" -#include "log.h" -#include "tux_error.h" +#include "tux_wings.h" +#define CMDSIZE 1024 +#define NRCMDS 256 + +#define match_token(t_num, str, t_count) (i >= t_count) &&\ + (!strcmp(tokens[t_num], str)) +#define check_result(funct_res) if (funct_res) \ + return E_TUXDRV_NOERROR; \ + else \ + return E_TUXDRV_INVALIDPARAMETER; + typedef struct { double timeout; - char cmd[1024]; + char cmd[CMDSIZE]; } delay_cmd_t; typedef struct { - delay_cmd_t cmd_list[256]; + delay_cmd_t cmd_list[NRCMDS]; int cmd_count; } cmd_stack_t; @@ -94,6 +106,10 @@ p = src_str; + /* the implementation below is build upon strncpy + it might be more efficient/cleaner to use strtok + */ + if (p) { while (1) @@ -128,15 +144,25 @@ int r = 0; if (hex_to_uint8(p0, &data[0])) + { r++; + } if (hex_to_uint8(p1, &data[1])) + { r++; + } if (hex_to_uint8(p2, &data[2])) + { r++; + } if (hex_to_uint8(p3, &data[3])) + { r++; + } if (hex_to_uint8(p4, &data[4])) + { r++; + } if (r < TUX_SEND_LENGTH) return false; @@ -144,13 +170,6 @@ return tux_usb_send_raw(data); } -#define match_token(t_num, str, t_count) (i >= t_count) &&\ - (!strcmp(tokens[t_num], str)) -#define check_result(funct_res) if (funct_res) \ - return E_TUXDRV_NOERROR; \ - else \ - return E_TUXDRV_INVALIDPARAMETER; - /** * Parse a command. */ @@ -162,152 +181,199 @@ /* If the parser is not enabled then fail */ if (!cmd_parser_enable) + { return E_TUXDRV_PARSERISDISABLED; + } log_debug("Parse command : [%s]", cmd_str); i = tux_cmd_parser_get_tokens(cmd_str, &tokens, 32); /* If no tokens in the command then fail */ - if (i <= 0) { + if (i <= 0) + { return E_TUXDRV_INVALIDCOMMAND; } /* Command to tux */ - if (match_token(0, "TUX_CMD", 1)) { + if (match_token(0, "TUX_CMD", 1)) + { /* EYES commands */ - if (match_token(1, "EYES", 2)) { + if (match_token(1, "EYES", 2)) + { /* EYES ON */ - if (match_token(2, "ON", 5)) { + if (match_token(2, "ON", 5)) + { check_result(tux_eyes_cmd_on(tokens[3], tokens[4])); /* EYES ON_DURING */ - } else if (match_token(2, "ON_DURING", 5)) { + } else if (match_token(2, "ON_DURING", 5)) + { check_result(tux_eyes_cmd_on_during(tokens[3], tokens[4])); /* EYES OPEN */ - } else if (match_token(2, "OPEN", 3)) { + } else if (match_token(2, "OPEN", 3)) + { check_result(tux_eyes_cmd_open()); /* EYES CLOSE */ - } else if (match_token(2, "CLOSE", 3)) { + } else if (match_token(2, "CLOSE", 3)) + { check_result(tux_eyes_cmd_close()); /* EYES OFF */ - } else if (match_token(2, "OFF", 3)) { + } else if (match_token(2, "OFF", 3)) + { check_result(tux_eyes_cmd_off()); } /* MOUTH commands */ - } else if (match_token(1, "MOUTH", 2)) { + } else if (match_token(1, "MOUTH", 2)) + { /* MOUTH ON */ - if (match_token(2, "ON", 5)) { + if (match_token(2, "ON", 5)) + { check_result(tux_mouth_cmd_on(tokens[3], tokens[4])); /* MOUTH ON_DURING */ - } else if (match_token(2, "ON_DURING", 5)) { + } else if (match_token(2, "ON_DURING", 5)) + { check_result(tux_mouth_cmd_on_during(tokens[3], tokens[4])); /* MOUTH OPEN */ - } else if (match_token(2, "OPEN", 3)) { + } else if (match_token(2, "OPEN", 3)) + { check_result(tux_mouth_cmd_open()); /* MOUTH CLOSE */ - } else if (match_token(2, "CLOSE", 3)) { + } else if (match_token(2, "CLOSE", 3)) + { check_result(tux_mouth_cmd_close()); /* MOUTH OFF */ - } else if (match_token(2, "OFF", 3)) { + } else if (match_token(2, "OFF", 3)) + { check_result(tux_mouth_cmd_off()); } /* WINGS commands */ - } else if (match_token(1, "WINGS", 2)) { + } else if (match_token(1, "WINGS", 2)) + { /* WINGS ON */ - if (match_token(2, "ON", 5)) { + if (match_token(2, "ON", 5)) + { check_result(tux_wings_cmd_on(tokens[3], tokens[4])); /* WINGS ON_DURING */ - } else if (match_token(2, "ON_DURING", 5)) { + } else if (match_token(2, "ON_DURING", 5)) + { check_result(tux_wings_cmd_on_during(tokens[3], tokens[4])); /* WINGS OPEN */ - } else if (match_token(2, "UP", 3)) { + } else if (match_token(2, "UP", 3)) + { check_result(tux_wings_cmd_up()); /* WINGS CLOSE */ - } else if (match_token(2, "DOWN", 3)) { + } else if (match_token(2, "DOWN", 3)) + { check_result(tux_wings_cmd_down()); /* WINGS OFF */ - } else if (match_token(2, "OFF", 3)) { + } else if (match_token(2, "OFF", 3)) + { check_result(tux_wings_cmd_off()); /* WINGS SPEED */ - } else if (match_token(2, "SPEED", 4)) { + } else if (match_token(2, "SPEED", 4)) + { check_result(tux_wings_cmd_speed(tokens[3])); } /* SPINNING commands */ - } else if (match_token(1, "SPINNING", 2)) { + } else if (match_token(1, "SPINNING", 2)) + { /* SPINNING SPEED */ - if (match_token(2, "SPEED", 4)) { + if (match_token(2, "SPEED", 4)) + { check_result(tux_spinning_cmd_speed(tokens[3])); /* SPINNING LEFT ON */ - } else if (match_token(2, "LEFT_ON", 4)) { + } else if (match_token(2, "LEFT_ON", 4)) + { check_result(tux_spinning_cmd_left_on(tokens[3])); /* SPINNING RIGHT ON */ - } else if (match_token(2, "RIGHT_ON", 4)) { + } else if (match_token(2, "RIGHT_ON", 4)) + { check_result(tux_spinning_cmd_right_on(tokens[3])); /* SPINNING LEFT ON DURING */ - } else if (match_token(2, "LEFT_ON_DURING", 4)) { + } else if (match_token(2, "LEFT_ON_DURING", 4)) + { check_result(tux_spinning_cmd_left_on_during(tokens[3])); /* SPINNING RIGHT ON DURING */ - } else if (match_token(2, "RIGHT_ON_DURING", 4)) { + } else if (match_token(2, "RIGHT_ON_DURING", 4)) + { check_result(tux_spinning_cmd_right_on_during(tokens[3])); /* SPINNING OFF */ - } else if (match_token(2, "OFF", 3)) { + } else if (match_token(2, "OFF", 3)) + { check_result(tux_spinning_cmd_off()); } /* IR commands */ - } else if (match_token(1, "IR", 2)) { + } else if (match_token(1, "IR", 2)) + { /* IR ON */ - if (match_token(2, "ON", 3)) { + if (match_token(2, "ON", 3)) + { check_result(tux_user_inputs_cmd_ir_on()); /* IR OFF */ - } else if (match_token(2, "OFF", 3)) { + } else if (match_token(2, "OFF", 3)) + { check_result(tux_user_inputs_cmd_ir_off()); /* IR SEND */ - } else if (match_token(2, "SEND", 5)) { + } else if (match_token(2, "SEND", 5)) + { check_result(tux_user_inputs_cmd_ir_send(tokens[3], tokens[4])); } /* LED commands */ - } else if (match_token(1, "LED", 2)) { + } else if (match_token(1, "LED", 2)) + { /* LED SET */ - if (match_token(2, "SET", 8)) { + if (match_token(2, "SET", 8)) + { check_result(tux_leds_cmd_set(tokens[3], tokens[4], tokens[5], tokens[6], tokens[7])); /* LED ON */ - } else if (match_token(2, "ON", 5)) { + } else if (match_token(2, "ON", 5)) + { check_result(tux_leds_cmd_set(tokens[3], tokens[4], "NONE", "0", "0")); /* LED OFF */ - } else if (match_token(2, "OFF", 4)) { + } else if (match_token(2, "OFF", 4)) + { check_result(tux_leds_cmd_set(tokens[3], "0.0", "NONE", "0", "0")); /* LED PULSE */ - } else if (match_token(2, "PULSE", 11)) { + } else if (match_token(2, "PULSE", 11)) + { check_result(tux_leds_cmd_pulse(tokens[3], tokens[4], tokens[5], tokens[6], tokens[7], tokens[8], tokens[9], tokens[10])); /* LED BLINK */ - } else if (match_token(2, "BLINK", 6)) { + } else if (match_token(2, "BLINK", 6)) + { check_result(tux_leds_cmd_pulse(tokens[3], "0.0", "1.0", tokens[4], tokens[5], "NONE", "0", "0")); } /* SOUND FLASH commands */ - } else if (match_token(1, "SOUND_FLASH", 2)) { + } else if (match_token(1, "SOUND_FLASH", 2)) + { /* SOUND FLASH PLAY */ - if (match_token(2, "PLAY", 5)) { + if (match_token(2, "PLAY", 5)) + { check_result(tux_sound_flash_cmd_play(tokens[3], tokens[4])); } /* AUDIO commands */ - } else if (match_token(1, "AUDIO", 2)) { + } else if (match_token(1, "AUDIO", 2)) + { /* CHANNEL GENERAL */ - if (match_token(2, "CHANNEL_GENERAL", 3)) { + if (match_token(2, "CHANNEL_GENERAL", 3)) + { check_result(tux_audio_cmd_channel_general()); /* CHANNEL TTS */ - } else if (match_token(2, "CHANNEL_TTS", 3)) { + } else if (match_token(2, "CHANNEL_TTS", 3)) + { check_result(tux_audio_cmd_channel_tts()); /* MUTE */ - } else if (match_token(2, "MUTE", 4)) { + } else if (match_token(2, "MUTE", 4)) + { check_result(tux_audio_cmd_mute(tokens[3])); } } /* Command raw to fux dongle or tux */ - } else if (match_token(0, "RAW_CMD", 6)) { + } else if (match_token(0, "RAW_CMD", 6)) + { check_result(tux_cmd_parser_parse_raw_cmd(tokens[1], tokens[2], tokens[3], tokens[4], tokens[5])); } @@ -321,39 +387,44 @@ tux_cmd_parser_insert_delay_command(float delay, char *cmd_str, bool sys_cmd) { TuxDrvError ret = E_TUXDRV_STACKOVERFLOW; - int free_idx = -1; int i; + double curtime = get_time(); + #ifdef USE_MUTEX mutex_lock(__stack_mutex); #endif - if (sys_cmd) { - for (i = 0; i < 256; i++) - if (!strcmp(sys_cmd_stack.cmd_list[i].cmd, "")) { - free_idx = i; + + if (sys_cmd) + { + for (i = 0; i < NRCMDS; i++) + { + if (!strcmp(sys_cmd_stack.cmd_list[i].cmd, "")) + { + sprintf(sys_cmd_stack.cmd_list[i].cmd, cmd_str); + sys_cmd_stack.cmd_list[i].timeout = delay + curtime; + ret = E_TUXDRV_NOERROR; break; } - - if (free_idx != -1) { - sprintf(sys_cmd_stack.cmd_list[free_idx].cmd, cmd_str); - sys_cmd_stack.cmd_list[free_idx].timeout = delay + get_time(); - ret = E_TUXDRV_NOERROR; - } - } else { - for (i = 0; i < 256; i++) - if (!strcmp(user_cmd_stack.cmd_list[i].cmd, "")) { - free_idx = i; + } + } + else + { + for (i = 0; i < NRCMDS; i++) + { + if (!strcmp(user_cmd_stack.cmd_list[i].cmd, "")) + { + sprintf(user_cmd_stack.cmd_list[i].cmd, cmd_str); + user_cmd_stack.cmd_list[i].timeout = delay + curtime; + ret = E_TUXDRV_NOERROR; break; } - - if (free_idx != -1) { - sprintf(user_cmd_stack.cmd_list[free_idx].cmd, cmd_str); - user_cmd_stack.cmd_list[free_idx].timeout = delay + get_time(); - ret = E_TUXDRV_NOERROR; - } + } } + #ifdef USE_MUTEX mutex_unlock(__stack_mutex); #endif + return ret; } @@ -368,18 +439,26 @@ #ifdef USE_MUTEX mutex_lock(__stack_mutex); #endif + /* Clear user cmd */ memset(&user_cmd_stack, 0, sizeof(cmd_stack_t)); + /* Clear system cmd */ - for (i = 0; i < 256; i++) - if (strcmp(sys_cmd_stack.cmd_list[i].cmd, "")) { + 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)); } + } + memset(&sys_cmd_stack, 0, sizeof(cmd_stack_t)); + #ifdef USE_MUTEX mutex_unlock(__stack_mutex); #endif + return true; } @@ -390,22 +469,32 @@ tux_cmd_parser_delay_stack_perform(void) { int i; + double curtime = get_time(); #ifdef USE_MUTEX mutex_lock(__stack_mutex); #endif - for (i = 0; i < 256; i++) { + + for (i = 0; i < NRCMDS; i++) + { if (strcmp(user_cmd_stack.cmd_list[i].cmd, "")) - if (get_time() >= user_cmd_stack.cmd_list[i].timeout) { + { + 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)); } + } if (strcmp(sys_cmd_stack.cmd_list[i].cmd, "")) - if (get_time() >= sys_cmd_stack.cmd_list[i].timeout) { + { + 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)); } + } } + #ifdef USE_MUTEX mutex_unlock(__stack_mutex); #endif @@ -418,13 +507,15 @@ parse_line(char *line_str) { float delay= 0.0; - char cmd_str[1024] = ""; + char cmd_str[CMDSIZE] = ""; int i; i = sscanf(line_str, "%f:%[^\n]", &delay, cmd_str); if (i == 2) + { return tux_cmd_parser_insert_delay_command(delay, cmd_str, false); + } return E_TUXDRV_NOERROR; } @@ -445,24 +536,31 @@ #ifdef USE_MUTEX mutex_lock(__macro_mutex); #endif + strcpy(macro, macro_str); - if ((line_tmp = strtok(macro, lex_ret)) != NULL) { - + if ((line_tmp = strtok(macro, lex_ret)) != NULL) + { ret = parse_line(line_tmp); if (ret != E_TUXDRV_NOERROR) + { result = ret; + } - while ((line_tmp = strtok(NULL, lex_ret)) != NULL) { + while ((line_tmp = strtok(NULL, lex_ret)) != NULL) + { ret = parse_line(line_tmp); - if (ret != E_TUXDRV_NOERROR) { + if (ret != E_TUXDRV_NOERROR) + { result = ret; break; } } } + #ifdef USE_MUTEX mutex_unlock(__macro_mutex); #endif + return result; } @@ -472,7 +570,7 @@ LIBLOCAL TuxDrvError tux_cmd_parser_parse_file(char *file_path) { - char line[1024] = ""; + char line[CMDSIZE] = ""; FILE *macro_file; TuxDrvError ret; TuxDrvError result = E_TUXDRV_NOERROR; @@ -482,19 +580,27 @@ #endif macro_file = fopen(file_path, "r"); - if (macro_file) { - while (!feof(macro_file)) { + if (macro_file) + { + while (!feof(macro_file)) + { fgets(line, sizeof(line)-2, macro_file); ret = parse_line(line); if (ret != E_TUXDRV_NOERROR) + { result = ret; + } } fclose(macro_file); - } else + } + else + { result = E_TUXDRV_FILEERROR; + } #ifdef USE_MUTEX mutex_unlock(__macro_mutex); #endif + return result; } Modified: software_suite_v2/middleware/tuxdriver/trunk/src/tux_cmd_parser.h =================================================================== --- software_suite_v2/middleware/tuxdriver/trunk/src/tux_cmd_parser.h 2008-04-19 15:28:53 UTC (rev 970) +++ software_suite_v2/middleware/tuxdriver/trunk/src/tux_cmd_parser.h 2008-04-19 17:10:33 UTC (rev 971) @@ -25,9 +25,12 @@ #include "tux_misc.h" #include "tux_error.h" -typedef char token_str_t[1024]; -typedef token_str_t tokens_t[256]; +#define TOKENSIZE 1024 +#define MAXNRTOKENS 256 +typedef char token_str_t[TOKENSIZE]; +typedef token_str_t tokens_t[MAXNRTOKENS]; + extern void tux_cmd_parser_init(void); extern void tux_cmd_parser_set_enable(bool value); extern int tux_cmd_parser_get_tokens(char *src_str, tokens_t *toks, |