MySmartUSB-MK3-Interface

MySmartUSB MK3 Interface
git clone git://xatko.vsos.ethz.ch/MySmartUSB-MK3-Interface.git
Log | Files | Refs

commit d8a3e014a274b14957776241db1867a7ed3638d7
parent 2caeb4e80c863b7731b99245507be11d502fc337
Author: Dominik Schmidt <das1993@hotmail.com>
Date:   Sat, 15 Dec 2012 17:14:23 +0100

Global option handling improved

Diffstat:
include/cmd_dispatch.h | 4++--
include/com.h | 2+-
include/option.h | 15+++++++++++++--
src/cmd_dispatch.c | 3+++
src/engine.c | 21+++++++++++++++++----
src/log.c | 2+-
src/option.c | 62+++++++++++++++++++++++++++++++++++++++++++++++++-------------
7 files changed, 86 insertions(+), 23 deletions(-)

diff --git a/include/cmd_dispatch.h b/include/cmd_dispatch.h @@ -12,11 +12,11 @@ #include <cmd.h> struct command_def{ + struct option opt; + char *name; int (*callback)(struct command_exec *cmd, int fd); unsigned short int request_mymode; - - struct option opt; }; extern struct command_def commands[]; extern int executeCommand(struct command_exec *cmd, int fd); diff --git a/include/com.h b/include/com.h @@ -8,7 +8,7 @@ * */ #pragma once -#define BAUD_RATE_SWITCH_WAIT 5000 +#define BAUD_RATE_SWITCH_WAIT 7500 #define RECEIVE_TERM_STR "\n\n" extern char *readString(int fd, unsigned int length); extern int sendRawString(int fd, const char *str); diff --git a/include/option.h b/include/option.h @@ -27,7 +27,7 @@ struct conf{ speed_t baud; } oldmode; unsigned short int print_usage; - unsigned short int log_level; + int log_level; unsigned short int color; }; @@ -39,6 +39,17 @@ struct option{ char *usage; char *description; }; - +enum GLOPT_TYPE{ + GLOPT_INCR, + GLOPT_DECR, + GLOPT_SET, + GLOPT_COUNT +}; +struct global_option{ + struct option opt; + unsigned int type; + void *data; +}; +extern struct global_option global_options[]; extern void print_usage(FILE *stream); extern int parseArguments(int argc, char **argv, struct conf *config); diff --git a/src/cmd_dispatch.c b/src/cmd_dispatch.c @@ -52,6 +52,9 @@ int processCommands(struct command_exec **cmdlist, int fd){ } i++; } + if(i==0){ + configuration.print_usage=1; + } return 0; } struct command_def *getCmdDefByName(char *name){ diff --git a/src/engine.c b/src/engine.c @@ -16,6 +16,19 @@ void print_usage(FILE *stream){ fprintf(stream,"Usage of %s\n",configuration.filename); fprintf(stream,"Global Commands:\n"); + struct global_option *glopt=global_options; + while(glopt->type!=GLOPT_COUNT){ + if(glopt->opt.usage==NULL){ + fprintf(stream,"\t%s/%s\n", glopt->opt.short_option, glopt->opt.long_option); + } + else{ + fprintf(stream,"\t%s/%s %s\n", glopt->opt.short_option, glopt->opt.long_option, glopt->opt.usage); + } + if(glopt->opt.description!=NULL){ + fprintf(stream,"\t\t%s\n",glopt->opt.description); + } + glopt++; + } fprintf(stream,"MySmartUSB Commands:\n"); struct command_def *cmdptr=commands; while(cmdptr->name!=NULL){ @@ -37,13 +50,13 @@ int engine_autorun(int argc, char **argv){ int ret=engine_init(argc,argv); if(ret<-1){ do_log("engine_autorun", "Couldn't initialize engine",LOG_LEVEL_FATAL, LOG_TYPE_NORMAL); - return -1; } - if(ret>-1&&engine_run()<0){ + if(ret>=0&&engine_run()<0){ do_log("engine_autorun", "Couldn't run the command",LOG_LEVEL_FATAL, LOG_TYPE_NORMAL); - return -3; + ret=-2; } - return engine_deinit(); + engine_deinit(); + return ret; } int engine_init(int argc, char **argv){ configuration.filename=argv[0]; diff --git a/src/log.c b/src/log.c @@ -43,7 +43,7 @@ void log_string(char *str, unsigned short int level, char *force_color){ break; } } - if(configuration.log_level<=level){ + if((configuration.log_level>LOG_LEVEL_FATAL) ? LOG_LEVEL_FATAL : configuration.log_level<=level){ fprintf(stream,"%s%s%s\n",color, str, reset_color); } } diff --git a/src/option.c b/src/option.c @@ -14,8 +14,13 @@ #include <stdlib.h> #include <string.h> struct conf configuration={.device="/dev/ttyUSB0", .log_level=LOG_LEVEL_INFO, .color=1}; - -struct command_exec *optionAddCommand(struct conf *config, struct command_def *cmd_def, char **argv){ +struct global_option global_options[]={ + {.type=GLOPT_DECR, .data=&configuration.log_level, .opt={.long_option="--verbose", .short_option="-v", .arg_length=0, .usage=""}}, + {.type=GLOPT_INCR, .data=&configuration.log_level, .opt={.long_option="--quiet", .short_option="-q", .arg_length=0, .usage=""}}, + {.type=GLOPT_SET, .data=&configuration.device, .opt={.long_option="--device", .short_option="-d", .arg_length=1, .usage="/dev/ttyXY"}}, + {.type=GLOPT_COUNT} +}; +int optionAddCommand(struct conf *config, struct command_def *cmd_def, char **argv){ static unsigned int cmdlist_cnt; array_grow((void **) &config->cmdlist.data,&config->cmdlist.arr,sizeof(struct command_exec *)); @@ -29,32 +34,63 @@ struct command_exec *optionAddCommand(struct conf *config, struct command_def *c cmdlist_cnt++; return 0; } - +int optionExecGlobal(struct conf *config, struct global_option *opt, char **argv){ + printf("conf.dev=%s - conf.log=%d\n",configuration.device, configuration.log_level); + switch(opt->type){ + case GLOPT_INCR: + *(int *)opt->data=(*(int *)opt->data)+1; + break; + case GLOPT_DECR: + *(int *)opt->data=(*(int *)opt->data)-1; + break; + case GLOPT_SET: + *(char **)opt->data=argv[0]; + break; + } + printf("conf.dev=%s - conf.log=%d\n",configuration.device, configuration.log_level); + return 0; +} +struct global_option *getGlOptByOpt(char *opt){ + struct global_option *opt_ptr=global_options; + while(opt_ptr->type!=GLOPT_COUNT){ + if(strcmp(opt_ptr->opt.long_option, opt)==0||strcmp(opt_ptr->opt.short_option, opt)==0){ + return opt_ptr; + } + opt_ptr++; + } + return NULL; +} int parseArguments(int argc, char **argv, struct conf *config){ unsigned int i=0; struct command_def *cmd_def; + struct global_option *glopt; array_initiate((void **)&config->cmdlist.data,&config->cmdlist.arr, sizeof(struct command_exec)); for(i=0;i<argc;i++){ cmd_def=getCmdDefByOpt(argv[i]); + glopt=getGlOptByOpt(argv[i]); if(cmd_def!=NULL){ optionAddCommand(config,cmd_def,&argv[i+1]); i+=cmd_def->opt.arg_length; if(cmd_def->request_mymode==1){ config->need_mymode=1; } - continue; } - if(strcmp(argv[i],"-q")==0){ - if(config->log_level<LOG_LEVEL_FATAL){ - config->log_level++; + else if(glopt!=NULL){ + if(glopt->opt.arg_length<=(argc-i+1)){ + optionExecGlobal(config, glopt, &argv[i+1]); } - continue; + i+=glopt->opt.arg_length; } - if(strcmp(argv[i],"-v")==0){ - if(config->log_level>0){ - config->log_level--; - } - continue; + else{ + char buf[50]; + sprintf(buf, "Option %s unknown", argv[i]); + do_log("parseArguments", buf, LOG_LEVEL_ERROR, LOG_TYPE_NORMAL); + config->print_usage=1; + return -1; + } + if(i>=argc){ + do_log("parseArguments", "Too few Arguments omitted", LOG_LEVEL_ERROR, LOG_TYPE_NORMAL); + return -2; } } optionAddCommand(config, NULL, NULL);