MySmartUSB-MK3-Interface

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

commit 2d0eac04c695aa0571c83e23819edfdef8381d04
parent fd4bf3af79d6c508af15cb1a132200102b31e9e0
Author: Dominik Schmidt <das1993@hotmail.com>
Date:   Wed, 12 Dec 2012 22:19:36 +0100

Filehierarchy improved

Diffstat:
Makefile | 8++++++--
cmd.c | 98-------------------------------------------------------------------------------
cmd.h | 23-----------------------
cmd_dispatch.c | 100-------------------------------------------------------------------------------
cmd_dispatch.h | 28----------------------------
com.c | 94-------------------------------------------------------------------------------
com.h | 17-----------------
common.c | 31-------------------------------
common.h | 20--------------------
dev.c | 102-------------------------------------------------------------------------------
dev.h | 15---------------
engine.c | 62--------------------------------------------------------------
engine.h | 14--------------
include/cmd.h | 23+++++++++++++++++++++++
include/cmd_dispatch.h | 28++++++++++++++++++++++++++++
include/com.h | 17+++++++++++++++++
include/common | 0
include/common.h | 20++++++++++++++++++++
include/dev.h | 15+++++++++++++++
include/engine.h | 14++++++++++++++
include/log.h | 28++++++++++++++++++++++++++++
include/option.h | 39+++++++++++++++++++++++++++++++++++++++
log.c | 43-------------------------------------------
log.h | 28----------------------------
option.c | 47-----------------------------------------------
option.h | 39---------------------------------------
src/cmd.c | 98+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/cmd_dispatch.c | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/com.c | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/common.c | 31+++++++++++++++++++++++++++++++
src/dev.c | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/engine.c | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/log.c | 43+++++++++++++++++++++++++++++++++++++++++++
src/option.c | 47+++++++++++++++++++++++++++++++++++++++++++++++
34 files changed, 767 insertions(+), 763 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,11 +1,15 @@ CC = /usr/bin/gcc -CFLAGS = -O2 -I. -Wall +CFLAGS = -O2 -Iinclude -Wall TARGET = ctrl -LIBS = cmd.o dev.o log.o com.o option.o cmd_dispatch.o common.o engine.o +LIBS = cmd.lo dev.lo log.lo com.lo option.lo cmd_dispatch.lo common.lo engine.lo +SRCDIR=src .PHONY: $(TARGET) clean $(TARGET): $(LIBS) $(CC) $(CFLAGS) $(TARGET).c -o $@ $(LIBS) +%.lo: $(SRCDIR)/%.c + $(CC) -c $(CFLAGS) -o $@ $< + %.o: %.c $(CC) -c $(CFLAGS) -o $@ $< clean: diff --git a/cmd.c b/cmd.c @@ -1,98 +0,0 @@ -/* - * 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 <cmd.h> -#include <log.h> -#include <com.h> -#include <stdlib.h> -#include <string.h> -#include <stdio.h> - -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; - } - int ret=0; - char *buf=readString(fd,3); - if(parseMagicPacket(buf)=='r'){ - ret=-2; - } - free(buf); - return ret; -} -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; - } - char *ret=readString(fd, 0); - if(checkIfOk(ret)){ - do_log("cmd_reset","Couldn't reset device", LOG_LEVEL_ERROR, LOG_TYPE_NORMAL); - free(ret); - return -2; - } - free(ret); - return 0; -} -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; - } - char *ret=readString(fd, 3); - char c=parseMagicPacket(ret); - do_log("cmd_status", &c, LOG_LEVEL_INFO, LOG_TYPE_NORMAL); - free(ret); - return 0; -} -int cmd_reset_line(struct command_exec *cmd, int fd){ - char c=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="@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; - } - char *r=readString(fd,0); - printf("%s\n",r); - if(!checkIfOk(r)){ - do_log("cmd_reset_line","Couldn't set reset line", LOG_LEVEL_ERROR, LOG_TYPE_NORMAL); - free(r); - return -3; - } - free(r); - return 0; -} -int cmd_board_power(struct command_exec *cmd, int fd){ - char c=0; - if(*cmd->data[0]=='1'){ - c='+'; - } - else if(*cmd->data[0]=='0'){ - c='-'; - } - else{ - do_log("cmd_board_power","Value must be either 1 or 0",LOG_LEVEL_ERROR,LOG_TYPE_NORMAL); - return -1; - } - if(sendMagicPacket(fd,c)<0){ - do_log("cmd_board_reset","Couldn't send power string", LOG_LEVEL_ERROR, LOG_TYPE_NORMAL); - return -2; - } - char r=readMagicPacket(fd); - if(r!=c){ - return -3; - } - return 0; -} diff --git a/cmd.h b/cmd.h @@ -1,23 +0,0 @@ -/* - * 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 GENERATE_CALLBACK(name) int name(struct command_exec *cmd,int fd); - -struct command_exec{ - struct command_def *cmd_def; - char **data; -}; - - -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 @@ -1,100 +0,0 @@ -/* - * 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 @@ -1,28 +0,0 @@ -/* - * 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 @@ -1,94 +0,0 @@ -/* - * 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 <com.h> -#include <log.h> -#include <stdlib.h> -#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); - - unsigned int i=0; - char c; - int ret; - while((ret=read(fd,&c,sizeof(char)))){ - if(ret<=0){ - do_log("readString","Read Error",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); - free(str); - return NULL; - } - if(i+1>=size){ - size+=10; - str=realloc((void *)str,size); - } - if(length!=0&&i>=length){ - str[i]='\0'; - break; - } - if(c==0x0A){ - str[i]='\0'; - } - else if(c==0){ - continue; - } - else{ - str[i]=c; - } - i++; - } - return str; -} -char readMagicPacket(int fd){ - int ret; - char c,r; - r=0; - while((ret=read(fd,&c,sizeof(char)))){ - if(ret<=0){ - do_log("readString","Read Error",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); - return 0; - } - if(c==0x0A){ - break; - } - else if((c>='a'&&c<='z')||c=='+'||c=='-'){ - r=c; - } - } - return r; -} -int sendRawString(int fd, const char *str){ - return write(fd,str,strlen(str)); -} -int sendString(int fd, const char *str){ - unsigned int length=strlen(str); - char *newstr=malloc(length+1); - strcpy(newstr,str); - newstr[length]=0xA; - return write(fd,newstr,length+1); -} -int sendMagicPacket(int fd, char c){ - char magic[]={0xE6,0xB5,0xBA,0xB9,0xB2,0xB3,0xA9,c}; - return sendRawString(fd,magic); -} -char parseMagicPacket(char *str){ - unsigned int i=0; - for(i=0; i<strlen(str);i++){ - if((str[i]>='a'&&str[i]<='z')||str[i]=='+'||str[i]=='-'){ - return str[i]; - } - } - return 0; -} -int checkIfOk(char *str){ - return (str[0]=='o'&&str[1]=='k'); -} diff --git a/com.h b/com.h @@ -1,17 +0,0 @@ -/* - * 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 -extern char *readString(int fd, unsigned int length); -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); -extern char parseMagicPacket(char *str); -extern char readMagicPacket(int fd); diff --git a/common.c b/common.c @@ -1,31 +0,0 @@ -/* - * 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 @@ -1,20 +0,0 @@ -/* - * 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/dev.c b/dev.c @@ -1,102 +0,0 @@ -/* - * 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 <dev.h> -#include <termios.h> -#include <log.h> -#include <cmd.h> -#include <fcntl.h> -#include <sys/ioctl.h> -#include <stdlib.h> -#include <com.h> -#include <stdio.h> -#include <unistd.h> -#include <string.h> -int closeDevice(int dev){ - fsync(dev); - return close(dev); -} -int autoConfigBaud(int fd){ - speed_t speed[3]={B500000, B115200, B19200}; - unsigned int i=0; - for(i=0;i<=3; i++){ - sendMagicPacket(fd,'i'); - char c=readMagicPacket(fd); - if(c!=0){ - break; - } - - if(i==3){ - do_log("autoConfigBaud","Couldn't evaluate correct baudrate", LOG_LEVEL_FATAL,LOG_TYPE_NORMAL); - return -1; - } - if(setBaudRate(fd,speed[i])<0){ - do_log("autoConfigBaud","Couldn't set baudrate", LOG_LEVEL_FATAL,LOG_TYPE_NORMAL); - return -2; - } - } - - return 0; -} -int setBaudRate(int fd, speed_t baud){ - struct termios options; - if(tcgetattr(fd, &options)<0){ - do_log("setBaudRate","Couldn't load Options",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); - return -1; - } - if(cfsetispeed(&options, baud)<0){ - do_log("setBaudRate","Couldn't set InputSpeed",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); - closeDevice(fd); - return -2; - } - if(cfsetospeed(&options, baud)<0){ - do_log("setBaudRate","Couldn't set OutputSpeed",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); - closeDevice(fd); - return -3; - } - if(tcsetattr(fd, TCSANOW, &options)<0){ - do_log("setBaudRate","Couldn't set Options",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); - closeDevice(fd); - return -4; - } - return 0; - -} -int openDevice(char *dev){ - int fd = open(dev, O_RDWR | O_NOCTTY ); - if(fd<0){ - do_log("openDevice", "Couldn't open Device", LOG_LEVEL_FATAL,LOG_TYPE_SIGNAL); - return -1; - } - ioctl(fd, TIOCEXCL); - struct termios options; - tcgetattr(fd, &options); - - options.c_cflag|=(CLOCAL | CREAD); - options.c_cflag&=~CSIZE; - options.c_cflag|=CS8; - options.c_cflag&=~PARENB; - options.c_cflag&=~CSTOPB; - options.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG); - options.c_iflag &= ~(IXON | IXOFF | IXANY ); - options.c_oflag &= ~(OCRNL | ONLCR | ONLRET |ONOCR | OFILL | OLCUC | OPOST); - - options.c_cc[VTIME]=5; - options.c_cc[VMIN]=0; - - if(tcsetattr(fd, TCSANOW, &options)<0){ - do_log("openDevice","Couldn't set Options",LOG_LEVEL_FATAL,LOG_TYPE_SIGNAL); - closeDevice(fd); - return -4; - } - if(autoConfigBaud(fd)<0){ - return -5; - } - return fd; -} diff --git a/dev.h b/dev.h @@ -1,15 +0,0 @@ -/* - * 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 <termios.h> -extern int closeDevice(int dev); -extern int autoConfigBaud(int fd); -extern int openDevice(char *dev); -extern int setBaudRate(int fd, speed_t baud); diff --git a/engine.c b/engine.c @@ -1,62 +0,0 @@ -/* - * 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 @@ -1,14 +0,0 @@ -/* - * 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/include/cmd.h b/include/cmd.h @@ -0,0 +1,23 @@ +/* + * 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 GENERATE_CALLBACK(name) int name(struct command_exec *cmd,int fd); + +struct command_exec{ + struct command_def *cmd_def; + char **data; +}; + + +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/include/cmd_dispatch.h b/include/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/include/com.h b/include/com.h @@ -0,0 +1,17 @@ +/* + * 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 +extern char *readString(int fd, unsigned int length); +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); +extern char parseMagicPacket(char *str); +extern char readMagicPacket(int fd); diff --git a/include/common b/include/common Binary files differ. diff --git a/include/common.h b/include/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/include/dev.h b/include/dev.h @@ -0,0 +1,15 @@ +/* + * 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 <termios.h> +extern int closeDevice(int dev); +extern int autoConfigBaud(int fd); +extern int openDevice(char *dev); +extern int setBaudRate(int fd, speed_t baud); diff --git a/include/engine.h b/include/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/include/log.h b/include/log.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 +enum LOG_TYPE{ + LOG_TYPE_RESULT, + LOG_TYPE_NORMAL, + LOG_TYPE_SIGNAL +}; +enum LOG_LEVEL{ + LOG_LEVEL_DEBUG, + LOG_LEVEL_INFO, + LOG_LEVEL_WARNING, + LOG_LEVEL_ERROR, + LOG_LEVEL_FATAL, + LOG_LEVEL_COUNT +}; + +extern void log_string(char *str, unsigned short int level); +extern void do_log(char *src, char *msg, unsigned short int level, unsigned short int type); +#define LOG_LEVEL_names_length 8 +extern char LOG_LEVEL_names[LOG_LEVEL_COUNT][LOG_LEVEL_names_length]; diff --git a/include/option.h b/include/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); diff --git a/log.c b/log.c @@ -1,43 +0,0 @@ -/* - * 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 <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; - if(level>=LOG_LEVEL_WARNING){ - stream=stderr; - } - else{ - stream=stdout; - } - 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; - char *str=malloc(strlen("%s[%s]: %s (%s)")+strlen(src)+strlen(msg)+strlen(LOG_LEVEL_names[level])+strlen(strerror(error))); - if(type==LOG_TYPE_SIGNAL){ - sprintf(str,"%s[%s]: %s (%s)",LOG_LEVEL_names[level],src,msg,strerror(error)); - } - else if(type==LOG_TYPE_RESULT){ - sprintf(str,"%s",msg); - } - else{ - sprintf(str,"%s[%s]: %s",LOG_LEVEL_names[level],src,msg); - } - log_string(str,level); - free(str); -} diff --git a/log.h b/log.h @@ -1,28 +0,0 @@ -/* - * 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 -enum LOG_TYPE{ - LOG_TYPE_RESULT, - LOG_TYPE_NORMAL, - LOG_TYPE_SIGNAL -}; -enum LOG_LEVEL{ - LOG_LEVEL_DEBUG, - LOG_LEVEL_INFO, - LOG_LEVEL_WARNING, - LOG_LEVEL_ERROR, - LOG_LEVEL_FATAL, - LOG_LEVEL_COUNT -}; - -extern void log_string(char *str, unsigned short int level); -extern void do_log(char *src, char *msg, unsigned short int level, unsigned short int type); -#define LOG_LEVEL_names_length 8 -extern char LOG_LEVEL_names[LOG_LEVEL_COUNT][LOG_LEVEL_names_length]; diff --git a/option.c b/option.c @@ -1,47 +0,0 @@ -/* - * 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 @@ -1,39 +0,0 @@ -/* - * 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); diff --git a/src/cmd.c b/src/cmd.c @@ -0,0 +1,98 @@ +/* + * 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 <cmd.h> +#include <log.h> +#include <com.h> +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + +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; + } + int ret=0; + char *buf=readString(fd,3); + if(parseMagicPacket(buf)=='r'){ + ret=-2; + } + free(buf); + return ret; +} +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; + } + char *ret=readString(fd, 0); + if(checkIfOk(ret)){ + do_log("cmd_reset","Couldn't reset device", LOG_LEVEL_ERROR, LOG_TYPE_NORMAL); + free(ret); + return -2; + } + free(ret); + return 0; +} +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; + } + char *ret=readString(fd, 3); + char c=parseMagicPacket(ret); + do_log("cmd_status", &c, LOG_LEVEL_INFO, LOG_TYPE_NORMAL); + free(ret); + return 0; +} +int cmd_reset_line(struct command_exec *cmd, int fd){ + char c=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="@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; + } + char *r=readString(fd,0); + printf("%s\n",r); + if(!checkIfOk(r)){ + do_log("cmd_reset_line","Couldn't set reset line", LOG_LEVEL_ERROR, LOG_TYPE_NORMAL); + free(r); + return -3; + } + free(r); + return 0; +} +int cmd_board_power(struct command_exec *cmd, int fd){ + char c=0; + if(*cmd->data[0]=='1'){ + c='+'; + } + else if(*cmd->data[0]=='0'){ + c='-'; + } + else{ + do_log("cmd_board_power","Value must be either 1 or 0",LOG_LEVEL_ERROR,LOG_TYPE_NORMAL); + return -1; + } + if(sendMagicPacket(fd,c)<0){ + do_log("cmd_board_reset","Couldn't send power string", LOG_LEVEL_ERROR, LOG_TYPE_NORMAL); + return -2; + } + char r=readMagicPacket(fd); + if(r!=c){ + return -3; + } + return 0; +} diff --git a/src/cmd_dispatch.c b/src/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/src/com.c b/src/com.c @@ -0,0 +1,94 @@ +/* + * 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 <com.h> +#include <log.h> +#include <stdlib.h> +#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); + + unsigned int i=0; + char c; + int ret; + while((ret=read(fd,&c,sizeof(char)))){ + if(ret<=0){ + do_log("readString","Read Error",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); + free(str); + return NULL; + } + if(i+1>=size){ + size+=10; + str=realloc((void *)str,size); + } + if(length!=0&&i>=length){ + str[i]='\0'; + break; + } + if(c==0x0A){ + str[i]='\0'; + } + else if(c==0){ + continue; + } + else{ + str[i]=c; + } + i++; + } + return str; +} +char readMagicPacket(int fd){ + int ret; + char c,r; + r=0; + while((ret=read(fd,&c,sizeof(char)))){ + if(ret<=0){ + do_log("readString","Read Error",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); + return 0; + } + if(c==0x0A){ + break; + } + else if((c>='a'&&c<='z')||c=='+'||c=='-'){ + r=c; + } + } + return r; +} +int sendRawString(int fd, const char *str){ + return write(fd,str,strlen(str)); +} +int sendString(int fd, const char *str){ + unsigned int length=strlen(str); + char *newstr=malloc(length+1); + strcpy(newstr,str); + newstr[length]=0xA; + return write(fd,newstr,length+1); +} +int sendMagicPacket(int fd, char c){ + char magic[]={0xE6,0xB5,0xBA,0xB9,0xB2,0xB3,0xA9,c}; + return sendRawString(fd,magic); +} +char parseMagicPacket(char *str){ + unsigned int i=0; + for(i=0; i<strlen(str);i++){ + if((str[i]>='a'&&str[i]<='z')||str[i]=='+'||str[i]=='-'){ + return str[i]; + } + } + return 0; +} +int checkIfOk(char *str){ + return (str[0]=='o'&&str[1]=='k'); +} diff --git a/src/common.c b/src/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/src/dev.c b/src/dev.c @@ -0,0 +1,102 @@ +/* + * 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 <dev.h> +#include <termios.h> +#include <log.h> +#include <cmd.h> +#include <fcntl.h> +#include <sys/ioctl.h> +#include <stdlib.h> +#include <com.h> +#include <stdio.h> +#include <unistd.h> +#include <string.h> +int closeDevice(int dev){ + fsync(dev); + return close(dev); +} +int autoConfigBaud(int fd){ + speed_t speed[3]={B500000, B115200, B19200}; + unsigned int i=0; + for(i=0;i<=3; i++){ + sendMagicPacket(fd,'i'); + char c=readMagicPacket(fd); + if(c!=0){ + break; + } + + if(i==3){ + do_log("autoConfigBaud","Couldn't evaluate correct baudrate", LOG_LEVEL_FATAL,LOG_TYPE_NORMAL); + return -1; + } + if(setBaudRate(fd,speed[i])<0){ + do_log("autoConfigBaud","Couldn't set baudrate", LOG_LEVEL_FATAL,LOG_TYPE_NORMAL); + return -2; + } + } + + return 0; +} +int setBaudRate(int fd, speed_t baud){ + struct termios options; + if(tcgetattr(fd, &options)<0){ + do_log("setBaudRate","Couldn't load Options",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); + return -1; + } + if(cfsetispeed(&options, baud)<0){ + do_log("setBaudRate","Couldn't set InputSpeed",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); + closeDevice(fd); + return -2; + } + if(cfsetospeed(&options, baud)<0){ + do_log("setBaudRate","Couldn't set OutputSpeed",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); + closeDevice(fd); + return -3; + } + if(tcsetattr(fd, TCSANOW, &options)<0){ + do_log("setBaudRate","Couldn't set Options",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); + closeDevice(fd); + return -4; + } + return 0; + +} +int openDevice(char *dev){ + int fd = open(dev, O_RDWR | O_NOCTTY ); + if(fd<0){ + do_log("openDevice", "Couldn't open Device", LOG_LEVEL_FATAL,LOG_TYPE_SIGNAL); + return -1; + } + ioctl(fd, TIOCEXCL); + struct termios options; + tcgetattr(fd, &options); + + options.c_cflag|=(CLOCAL | CREAD); + options.c_cflag&=~CSIZE; + options.c_cflag|=CS8; + options.c_cflag&=~PARENB; + options.c_cflag&=~CSTOPB; + options.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG); + options.c_iflag &= ~(IXON | IXOFF | IXANY ); + options.c_oflag &= ~(OCRNL | ONLCR | ONLRET |ONOCR | OFILL | OLCUC | OPOST); + + options.c_cc[VTIME]=5; + options.c_cc[VMIN]=0; + + if(tcsetattr(fd, TCSANOW, &options)<0){ + do_log("openDevice","Couldn't set Options",LOG_LEVEL_FATAL,LOG_TYPE_SIGNAL); + closeDevice(fd); + return -4; + } + if(autoConfigBaud(fd)<0){ + return -5; + } + return fd; +} diff --git a/src/engine.c b/src/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/src/log.c b/src/log.c @@ -0,0 +1,43 @@ +/* + * 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 <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; + if(level>=LOG_LEVEL_WARNING){ + stream=stderr; + } + else{ + stream=stdout; + } + 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; + char *str=malloc(strlen("%s[%s]: %s (%s)")+strlen(src)+strlen(msg)+strlen(LOG_LEVEL_names[level])+strlen(strerror(error))); + if(type==LOG_TYPE_SIGNAL){ + sprintf(str,"%s[%s]: %s (%s)",LOG_LEVEL_names[level],src,msg,strerror(error)); + } + else if(type==LOG_TYPE_RESULT){ + sprintf(str,"%s",msg); + } + else{ + sprintf(str,"%s[%s]: %s",LOG_LEVEL_names[level],src,msg); + } + log_string(str,level); + free(str); +} diff --git a/src/option.c b/src/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; +}