commit fd4bf3af79d6c508af15cb1a132200102b31e9e0
parent a74e98c64b2f5b4716df820b94b242b8e35cf202
Author: Dominik Schmidt <das1993@hotmail.com>
Date: Wed, 12 Dec 2012 22:13:10 +0100
Structure improved, optionparsing is now dynamic, configuration is stored in a struct
Diffstat:
Makefile | | | 8 | +++++--- |
cmd.c | | | 78 | ++++++++++-------------------------------------------------------------------- |
cmd.h | | | 33 | +++++++++------------------------ |
cmd_dispatch.c | | | 100 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
cmd_dispatch.h | | | 28 | ++++++++++++++++++++++++++++ |
com.c | | | 2 | +- |
com.h | | | 3 | ++- |
common.c | | | 31 | +++++++++++++++++++++++++++++++ |
common.h | | | 20 | ++++++++++++++++++++ |
ctrl.c | | | 95 | ++----------------------------------------------------------------------------- |
dev.c | | | 2 | -- |
engine.c | | | 62 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
engine.h | | | 14 | ++++++++++++++ |
log.c | | | 5 | ++++- |
main.c | | | 0 | |
option.c | | | 47 | +++++++++++++++++++++++++++++++++++++++++++++++ |
option.h | | | 39 | +++++++++++++++++++++++++++++++++++++++ |
17 files changed, 374 insertions(+), 193 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,10 +1,12 @@
CC = /usr/bin/gcc
-CFLAGS = -O2 -I.
+CFLAGS = -O2 -I. -Wall
TARGET = ctrl
-LIBS = cmd.o dev.o log.o com.o
-.PHONY: $(TARGET)
+LIBS = cmd.o dev.o log.o com.o option.o cmd_dispatch.o common.o engine.o
+.PHONY: $(TARGET) clean
$(TARGET): $(LIBS)
$(CC) $(CFLAGS) $(TARGET).c -o $@ $(LIBS)
%.o: %.c
$(CC) -c $(CFLAGS) -o $@ $<
+clean:
+ rm -f $(LIBS) $(TARGET)
diff --git a/cmd.c b/cmd.c
@@ -13,9 +13,8 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
-char COMMAND_TYPE_names[COMMAND_COUNT][COMMAND_TYPE_names_length]={"Status","Reset","Board-Reset","Board-power","Reset-line","Rescue-clock"};
-int cmd_board_reset(struct command *cmd,int fd){
+int cmd_board_reset(struct command_exec *cmd,int fd){
if(sendMagicPacket(fd,'r')<0){
do_log("cmd_board_reset","Couldn't send reset packet", LOG_LEVEL_ERROR, LOG_TYPE_NORMAL);
return -1;
@@ -28,7 +27,7 @@ int cmd_board_reset(struct command *cmd,int fd){
free(buf);
return ret;
}
-int cmd_reset(struct command *cmd, int fd){
+int cmd_reset(struct command_exec *cmd, int fd){
if(sendString(fd,"@main reset")<0){
do_log("cmd_reset","Couldn't send reset string", LOG_LEVEL_ERROR, LOG_TYPE_NORMAL);
return -1;
@@ -42,7 +41,7 @@ int cmd_reset(struct command *cmd, int fd){
free(ret);
return 0;
}
-int cmd_status(struct command *cmd, int fd){
+int cmd_status(struct command_exec *cmd, int fd){
if(sendMagicPacket(fd,'i')<0){
do_log("cmd_board_reset","Couldn't send status request packet", LOG_LEVEL_ERROR, LOG_TYPE_NORMAL);
return -1;
@@ -53,13 +52,14 @@ int cmd_status(struct command *cmd, int fd){
free(ret);
return 0;
}
-int cmd_reset_line(struct command *cmd, int fd){
+int cmd_reset_line(struct command_exec *cmd, int fd){
char c=0;
- if(*(char*)cmd->data!='1'&&*(char*)cmd->data!='0'){
+ if(*(char*)cmd->data!='1'&&*cmd->data[0]!='0'){
do_log("cmd_reset_line","Value must be either 1 or 0",LOG_LEVEL_ERROR,LOG_TYPE_NORMAL);
return -1;
}
- char str[]={'@','l','i','n','e','s',' ','r','s','t',' ',c};
+ char *str="@lines rst ";
+ str[strlen(str)]=c;
if(sendString(fd,str)<0){
do_log("cmd_reset_line","Couldn't send power string", LOG_LEVEL_ERROR, LOG_TYPE_NORMAL);
return -2;
@@ -74,12 +74,12 @@ int cmd_reset_line(struct command *cmd, int fd){
free(r);
return 0;
}
-int cmd_board_power(struct command *cmd, int fd){
+int cmd_board_power(struct command_exec *cmd, int fd){
char c=0;
- if(*(char*)cmd->data=='1'){
+ if(*cmd->data[0]=='1'){
c='+';
}
- else if(*(char*)cmd->data=='0'){
+ else if(*cmd->data[0]=='0'){
c='-';
}
else{
@@ -96,61 +96,3 @@ int cmd_board_power(struct command *cmd, int fd){
}
return 0;
}
-int executeCommand(struct command *cmd, int fd){
- int (*func)(struct command *, int)=NULL;
- switch(cmd->type){
- case COMMAND_BOARD_RESET:
- func=&cmd_board_reset;
- break;
- case COMMAND_RESET:
- func=&cmd_reset;
- break;
- case COMMAND_STATUS:
- func=&cmd_status;
- break;
- case COMMAND_BOARD_POWER:
- func=&cmd_board_power;
- break;
- case COMMAND_RESET_LINE:
- func=&cmd_reset_line;
- break;
- default:
- do_log("executeCommand", "Commandtype unknown", LOG_LEVEL_ERROR, LOG_TYPE_NORMAL);
- return -1;
- break;
- }
- if(func(cmd,fd)<0){
- do_log("executeCommand","Couldn't execute command",LOG_LEVEL_ERROR,LOG_TYPE_NORMAL);
- }
- else{
- do_log("executeCommand","Executing successfull",LOG_LEVEL_DEBUG,LOG_TYPE_NORMAL);
- }
- return 0;
-}
-int processCommands(struct command **cmdlist, int fd){
- unsigned int i=0;
- char *str=malloc(strlen("Executing command %s failed")+COMMAND_TYPE_names_length);
- while(cmdlist[i]!=NULL){
- sprintf(str,"Executing command %s",COMMAND_TYPE_names[cmdlist[i]->type]);
- do_log("processCommands",str,LOG_LEVEL_DEBUG,LOG_TYPE_NORMAL);
- if(executeCommand(cmdlist[i], fd)<0){
- sprintf(str,"Executing command %s failed",COMMAND_TYPE_names[cmdlist[i]->type]);
- do_log("processCommands",str,LOG_LEVEL_ERROR,LOG_TYPE_NORMAL);
- }
- i++;
- }
- return 0;
-}
-struct command *newCommand(unsigned short type){
- struct command *cmd;
- unsigned int i=0;
- if(type>COMMAND_TYPEARGS_00){
- i=1;
- }
- else if(type>COMMAND_TYPEARGS_01){
- i=2;
- }
- cmd=(struct command *)malloc(sizeof(struct command)-1+i);
- cmd->type=type;
- return cmd;
-}
diff --git a/cmd.h b/cmd.h
@@ -8,31 +8,16 @@
*
*/
#pragma once
+#define GENERATE_CALLBACK(name) int name(struct command_exec *cmd,int fd);
-enum COMMAND_TYPE{
- COMMAND_STATUS=0,
- COMMAND_RESET,
- COMMAND_BOARD_RESET,
- COMMAND_TYPEARGS_00=COMMAND_BOARD_RESET,
- COMMAND_BOARD_POWER,
- COMMAND_RESET_LINE,
- COMMAND_RESCUE_CLOCK,
- COMMAND_TYPEARGS_01=COMMAND_RESCUE_CLOCK,
- COMMAND_TYPEARGS_02=COMMAND_TYPEARGS_01,
- COMMAND_COUNT
+struct command_exec{
+ struct command_def *cmd_def;
+ char **data;
};
-#define COMMAND_TYPE_names_length 18
-struct command{
- unsigned short type;
- void *data;
-};
-extern char COMMAND_TYPE_names[COMMAND_COUNT][COMMAND_TYPE_names_length];
-extern int cmd_board_reset(struct command *cmd,int fd);
-extern int cmd_reset(struct command *cmd, int fd);
-extern int cmd_status(struct command *cmd, int fd);
-extern int cmd_board_power(struct command *cmd, int fd);
-extern int executeCommand(struct command *cmd, int fd);
-extern int processCommands(struct command **cmdlist, int fd);
-extern struct command *newCommand(unsigned short type);
+extern GENERATE_CALLBACK(cmd_board_reset);
+extern int cmd_reset(struct command_exec *cmd, int fd);
+extern int cmd_status(struct command_exec *cmd, int fd);
+extern int cmd_board_power(struct command_exec *cmd, int fd);
+extern int cmd_reset_line(struct command_exec *cmd, int fd);
diff --git a/cmd_dispatch.c b/cmd_dispatch.c
@@ -0,0 +1,100 @@
+/*
+ * MySmartUSB MK3 Interface
+ *
+ * @copyright: Copyright (c) 2012, Dominik Schmidt
+ * @author: Dominik Schmidt <das1993@hotmail.com>
+ * @version: 0.0.0
+ * @license: CC-BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
+ *
+*/
+#include <log.h>
+#include <cmd_dispatch.h>
+#include <cmd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <option.h>
+#include <termios.h>
+#include <string.h>
+#include <com.h>
+#include <dev.h>
+struct command_def commands[]={
+ {.name="Status", .opt={.arg_length=0, .short_option="-s", .long_option="--status"}, .callback=&cmd_status},
+ {.name="Reset", .opt={.arg_length=0, .short_option="-R", .long_option="--reset"}, .callback=&cmd_reset},
+ {.name="Board-Reset", .opt={.arg_length=0, .short_option="-r", .long_option="--board-reset"}, .callback=&cmd_board_reset},
+ {.name="Reset-Line", .opt={.usage="[1/0]", .arg_length=1, .short_option="-rL", .long_option="--reset-line"}, .callback=&cmd_reset_line},
+ {.name="Board-Power", .opt={.usage="[1/0]", .arg_length=1, .short_option="-bP", .long_option="--board-power"}, .callback=&cmd_board_power},
+ {.name=NULL}
+};
+int executeCommand(struct command_exec *cmd, int fd){
+ if(cmd->cmd_def->callback(cmd,fd)<0){
+ do_log("executeCommand","Couldn't execute command",LOG_LEVEL_ERROR,LOG_TYPE_NORMAL);
+ }
+ else{
+ do_log("executeCommand","Executing successfull",LOG_LEVEL_DEBUG,LOG_TYPE_NORMAL);
+ }
+ return 0;
+}
+int processCommands(struct command_exec **cmdlist, int fd){
+ unsigned int i=0;
+ char str[40];
+ while(cmdlist[i]!=NULL){
+ sprintf(str,"Executing command %s",cmdlist[i]->cmd_def->name);
+ do_log("processCommands",str,LOG_LEVEL_DEBUG,LOG_TYPE_NORMAL);
+ if(executeCommand(cmdlist[i], fd)<0){
+ sprintf(str,"Executing command %s failed",cmdlist[i]->cmd_def->name);
+ do_log("processCommands",str,LOG_LEVEL_ERROR,LOG_TYPE_NORMAL);
+ }
+ i++;
+ }
+ return 0;
+}
+struct command_def *getCmdDefByName(char *name){
+ struct command_def *cmdptr=commands;
+ while(cmdptr->name!=NULL){
+ if(strcmp(name,cmdptr->name)==0){
+ return cmdptr;
+ }
+ cmdptr++;
+ }
+ return NULL;
+}
+struct command_def *getCmdDefByOpt(char *opt){
+ struct command_def *cmdptr=commands;
+ while(cmdptr->name!=NULL){
+ if(strcmp(opt,cmdptr->opt.long_option)==0||strcmp(opt,cmdptr->opt.short_option)==0){
+ return cmdptr;
+ }
+ cmdptr++;
+ }
+ return NULL;
+}
+struct command_exec *newCommand(struct command_def *cd){
+ struct command_exec *cmd=malloc(sizeof(struct command_exec));
+ cmd->cmd_def=cd;
+ return cmd;
+}
+
+
+int prepareCommandExecution(int fd, struct conf *config){
+ config->oldmode=0;
+ if(config->need_mymode){
+ sendMagicPacket(fd,'i');
+ config->oldmode=readMagicPacket(fd);
+ if(config->oldmode!='m'){
+ sendMagicPacket(fd,'m');
+ setBaudRate(fd,B500000);
+ if(readMagicPacket(fd)!='m'){
+ do_log("prepareCommandExecution","Couldn't switch to mymode",LOG_LEVEL_ERROR,LOG_TYPE_NORMAL);
+ return -1;
+ }
+ }
+
+ }
+ return 0;
+}
+int finishCommandExecution(int fd, struct conf *config){
+ if(config->oldmode>0){
+ sendMagicPacket(fd,config->oldmode);
+ }
+ return 0;
+}
diff --git a/cmd_dispatch.h b/cmd_dispatch.h
@@ -0,0 +1,28 @@
+/*
+ * MySmartUSB MK3 Interface
+ *
+ * @copyright: Copyright (c) 2012, Dominik Schmidt
+ * @author: Dominik Schmidt <das1993@hotmail.com>
+ * @version: 0.0.0
+ * @license: CC-BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
+ *
+*/
+#pragma once
+#include <option.h>
+#include <cmd.h>
+
+struct command_def{
+ char *name;
+ int (*callback)(struct command_exec *cmd, int fd);
+
+ struct option opt;
+};
+extern struct command_def commands[];
+extern int executeCommand(struct command_exec *cmd, int fd);
+extern int processCommands(struct command_exec **cmdlist, int fd);
+extern struct command_def *getCmdDefByName(char *name);
+extern struct command_def *getCmdDefByOpt(char *desc);
+extern struct command_exec *newCommand(struct command_def *cd);
+
+extern int prepareCommandExecution(int fd, struct conf *config);
+extern int finishCommandExecution(int fd, struct conf *config);
diff --git a/com.c b/com.c
@@ -13,6 +13,7 @@
#include <cmd.h>
#include <string.h>
#include <stdio.h>
+#include <unistd.h>
char *readString(int fd, unsigned int length){
unsigned int size=10;
char *str=malloc(size);
@@ -51,7 +52,6 @@ char readMagicPacket(int fd){
int ret;
char c,r;
r=0;
- unsigned int i=0;
while((ret=read(fd,&c,sizeof(char)))){
if(ret<=0){
do_log("readString","Read Error",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL);
diff --git a/com.h b/com.h
@@ -13,4 +13,5 @@ extern int sendRawString(int fd, const char *str);
extern int sendString(int fd, const char *str);
extern int sendMagicPacket(int fd, char c);
extern int checkIfOk(char *str);
-char parseMagicPacket(char *str);
+extern char parseMagicPacket(char *str);
+extern char readMagicPacket(int fd);
diff --git a/common.c b/common.c
@@ -0,0 +1,31 @@
+/*
+ * MySmartUSB MK3 Interface
+ *
+ * @copyright: Copyright (c) 2012, Dominik Schmidt
+ * @author: Dominik Schmidt <das1993@hotmail.com>
+ * @version: 0.0.0
+ * @license: CC-BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
+ *
+*/
+#include <common.h>
+#include <stdlib.h>
+
+int array_initiate(void *data, struct array *arr, unsigned short int size){
+ arr->alloc=10;
+ data=malloc(size);
+ return 0;
+}
+int array_grow(void **data, struct array *arr, unsigned short int by){
+ arr->count+=by;
+ while(arr->count>=arr->alloc){
+ arr->alloc+=ARRAY_CHUNK_SIZE;
+ }
+ *data=realloc(*data, arr->alloc);
+ return 0;
+}
+int array_destroy(void *data, struct array *arr){
+ free(data);
+ arr->count=0;
+ arr->alloc=0;
+ return 0;
+}
diff --git a/common.h b/common.h
@@ -0,0 +1,20 @@
+/*
+ * MySmartUSB MK3 Interface
+ *
+ * @copyright: Copyright (c) 2012, Dominik Schmidt
+ * @author: Dominik Schmidt <das1993@hotmail.com>
+ * @version: 0.0.0
+ * @license: CC-BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
+ *
+*/
+#pragma once
+#define ARRAY_CHUNK_SIZE 10;
+struct array{
+ unsigned short int alloc;
+ unsigned short int count;
+};
+
+
+int array_initiate(void *data, struct array *arr, unsigned short int size);
+int array_grow(void **data, struct array *arr, unsigned short int by);
+int array_destroy(void *data, struct array *arr);
diff --git a/ctrl.c b/ctrl.c
@@ -7,99 +7,8 @@
* @license: CC-BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
*
*/
-#include <stdio.h>
-#include <log.h>
-#include <cmd.h>
-#include <stdlib.h>
-#include <termios.h>
-#include <com.h>
-
-struct conf{
- struct command **cmdlist;
- unsigned short int need_mymode;
- char oldmode;
-};
-struct conf config;
-
-char *filename;
-unsigned short int log_level=LOG_LEVEL_DEBUG;
-
-int parseArguments(int argc, char **argv, struct conf *config){
- unsigned int i=0;
- unsigned int cmdcnt=0;
- unsigned int alloc=10;
- config->cmdlist=(struct command **)malloc(alloc*sizeof(struct command **));
- struct command *newcmd;
-
- for(i=0;i<argc;i++){
- newcmd=NULL;
- if(strcmp(argv[i],"-R")==0){
- newcmd=newCommand(COMMAND_RESET);
- config->need_mymode=1;
- }
- else if(strcmp(argv[i],"-r")==0){
- newcmd=newCommand(COMMAND_BOARD_RESET);
- }
- else if(strcmp("-s", argv[i])==0){
- newcmd=newCommand(COMMAND_STATUS);
- }
- else if(strcmp("-p", argv[i])==0){
- newcmd=newCommand(COMMAND_BOARD_POWER);
- newcmd->data=argv[++i];
- }
- else if(strcmp("-rL", argv[i])==0){
- newcmd=newCommand(COMMAND_RESET_LINE);
- newcmd->data=argv[++i];
- config->need_mymode=1;
- }
-
- if(newcmd!=NULL){
- if(cmdcnt+1>alloc){
- alloc+=10;
- config->cmdlist=realloc((void *)config->cmdlist, alloc*sizeof(struct command **));
- }
- config->cmdlist[cmdcnt]=newcmd;
- cmdcnt++;
- }
- }
- config->cmdlist[cmdcnt]=NULL;
- return 0;
-}
-int prepareCommandExecution(int fd, struct conf *config){
- config->oldmode=0;
- if(config->need_mymode){
- sendMagicPacket(fd,'i');
- config->oldmode=readMagicPacket(fd);
- if(config->oldmode!='m'){
- sendMagicPacket(fd,'m');
- setBaudRate(fd,B500000);
- if(readMagicPacket(fd)!='m'){
- do_log("prepareCommandExecution","Couldn't switch to mymode",LOG_LEVEL_ERROR,LOG_TYPE_NORMAL);
- return -1;
- }
- }
-
- }
- return 0;
-}
-int finishCommandExecution(int fd, struct conf *config){
- if(config->oldmode>0){
- sendMagicPacket(fd,config->oldmode);
- }
-}
+#include <engine.h>
int main(int argc, char **argv){
- filename=argv[0];
- parseArguments(argc-1, &argv[1],&config);
-
- int fd=openDevice("/dev/ttyUSB0");
- if(fd<0){
- do_log("main","Couldn't open device",LOG_LEVEL_FATAL,LOG_TYPE_NORMAL);
- return 1;
- }
- prepareCommandExecution(fd, &config);
- processCommands(config.cmdlist,fd);
- finishCommandExecution(fd, &config);
-
- closeDevice(fd);
+ engine_init(argc,argv);
return 0;
}
diff --git a/dev.c b/dev.c
@@ -25,8 +25,6 @@ int closeDevice(int dev){
int autoConfigBaud(int fd){
speed_t speed[3]={B500000, B115200, B19200};
unsigned int i=0;
- struct command query;
- query.type=COMMAND_STATUS;
for(i=0;i<=3; i++){
sendMagicPacket(fd,'i');
char c=readMagicPacket(fd);
diff --git a/engine.c b/engine.c
@@ -0,0 +1,62 @@
+/*
+ * MySmartUSB MK3 Interface
+ *
+ * @copyright: Copyright (c) 2012, Dominik Schmidt
+ * @author: Dominik Schmidt <das1993@hotmail.com>
+ * @version: 0.0.0
+ * @license: CC-BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
+ *
+*/
+#include <stdio.h>
+#include <option.h>
+#include <cmd_dispatch.h>
+#include <dev.h>
+#include <log.h>
+
+void print_usage(FILE *stream){
+ if(configuration.print_usage){
+ fprintf(stream,"Usage of %s\n",configuration.filename);
+ fprintf(stream,"Global Commands:\n");
+ fprintf(stream,"MySmartUSB Commands:\n");
+ struct command_def *cmdptr=commands;
+ while(cmdptr!=NULL){
+ if(cmdptr->opt.usage==NULL){
+ fprintf(stream,"\t%s: %s/%s\n", cmdptr->name, cmdptr->opt.short_option, cmdptr->opt.long_option);
+ }
+ else{
+ fprintf(stream,"\t%s: %s/%s %s\n", cmdptr->name, cmdptr->opt.short_option, cmdptr->opt.long_option, cmdptr->opt.usage);
+ }
+ if(cmdptr->opt.description!=NULL){
+ fprintf(stream,"\t\t%s\n",cmdptr->opt.description);
+ }
+ cmdptr++;
+ }
+
+
+ }
+}
+
+int engine_init(int argc, char **argv){
+ configuration.filename=argv[0];
+ parseArguments(argc-1, &argv[1],&configuration);
+
+ int fd=openDevice(configuration.device);
+ if(fd<0){
+ do_log("main","Couldn't open device",LOG_LEVEL_FATAL,LOG_TYPE_NORMAL);
+ return 1;
+ }
+ processCommands(configuration.cmdlist.data,fd);
+ /*
+ prepareCommandExecution(fd, &configuration);
+ processCommands(configuration.cmdlist.data,fd);
+ finishCommandExecution(fd, &configuration);
+ */
+ closeDevice(fd);
+
+ return 0;
+}
+
+int engine_run(void){
+ return 0;
+
+}
diff --git a/engine.h b/engine.h
@@ -0,0 +1,14 @@
+/*
+ * MySmartUSB MK3 Interface
+ *
+ * @copyright: Copyright (c) 2012, Dominik Schmidt
+ * @author: Dominik Schmidt <das1993@hotmail.com>
+ * @version: 0.0.0
+ * @license: CC-BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
+ *
+*/
+#pragma once
+#include <stdio.h>
+void print_usage(FILE *stream);
+int engine_init(int argc, char **argv);
+int engine_run(void);
diff --git a/log.c b/log.c
@@ -12,6 +12,7 @@
#include <errno.h>
#include <stdlib.h>
#include <string.h>
+#include <option.h>
char LOG_LEVEL_names[LOG_LEVEL_COUNT][8]={"DEBUG","INFO","WARNING","ERROR","FATAL"};
void log_string(char *str, unsigned short int level){
FILE *stream;
@@ -21,7 +22,9 @@ void log_string(char *str, unsigned short int level){
else{
stream=stdout;
}
- fprintf(stream,"%s\n",str);
+ if(configuration.log_level<=level){
+ fprintf(stream,"%s\n",str);
+ }
}
void do_log(char *src, char *msg, unsigned short int level, unsigned short int type){
int error=errno;
diff --git a/main.c b/main.c
diff --git a/option.c b/option.c
@@ -0,0 +1,47 @@
+/*
+ * MySmartUSB MK3 Interface
+ *
+ * @copyright: Copyright (c) 2012, Dominik Schmidt
+ * @author: Dominik Schmidt <das1993@hotmail.com>
+ * @version: 0.0.0
+ * @license: CC-BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
+ *
+*/
+#include <log.h>
+#include <stdio.h>
+#include <option.h>
+#include <cmd_dispatch.h>
+#include <stdlib.h>
+#include <string.h>
+struct conf configuration={.device="/dev/ttyUSB0", .log_level=LOG_LEVEL_DEBUG};
+
+struct command_exec *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 *));
+ if(cmd_def==NULL){
+ config->cmdlist.data[cmdlist_cnt]=NULL;
+ }
+ else{
+ config->cmdlist.data[cmdlist_cnt]=newCommand(cmd_def);
+ config->cmdlist.data[cmdlist_cnt]->data=argv;
+ }
+ cmdlist_cnt++;
+ return 0;
+}
+
+int parseArguments(int argc, char **argv, struct conf *config){
+ unsigned int i=0;
+ struct command_def *cmd_def;
+ array_initiate(&config->cmdlist.data,&config->cmdlist.arr, sizeof(struct command_exec));
+ for(i=0;i<argc;i++){
+ cmd_def=getCmdDefByOpt(argv[i]);
+ if(cmd_def!=NULL){
+ optionAddCommand(config,cmd_def,&argv[i+1]);
+ i+=cmd_def->opt.arg_length;
+ continue;
+ }
+ }
+ optionAddCommand(config, NULL, NULL);
+ return 0;
+}
diff --git a/option.h b/option.h
@@ -0,0 +1,39 @@
+/*
+ * MySmartUSB MK3 Interface
+ *
+ * @copyright: Copyright (c) 2012, Dominik Schmidt
+ * @author: Dominik Schmidt <das1993@hotmail.com>
+ * @version: 0.0.0
+ * @license: CC-BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
+ *
+*/
+#pragma once
+#include <common.h>
+#include <cmd.h>
+#include <stdio.h>
+
+extern struct conf configuration;
+struct conf{
+ char *filename;
+ char *device;
+ struct {
+ struct command_exec **data;
+ struct array arr;
+ } cmdlist;
+ unsigned short int need_mymode;
+ char oldmode;
+ unsigned short int print_usage;
+ unsigned short int log_level;
+};
+
+
+struct option{
+ char *long_option;
+ char *short_option;
+ unsigned short int arg_length;
+ char *usage;
+ char *description;
+};
+
+extern void print_usage(FILE *stream);
+extern int parseArguments(int argc, char **argv, struct conf *config);