MySmartUSB-MK3-Interface

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

commit 5e2d2324b03141a907f2895f9f3a073e89e27d4a
parent 858071fffa6beae8dc7debe3c37cc63620d9ae69
Author: Dominik Schmidt <das1993@hotmail.com>
Date:   Thu, 13 Dec 2012 21:54:23 +0100

Emulation Mode setting added, Logging color functionality implemented

Diffstat:
ctrl.c | 1+
include/cmd.h | 11++++++-----
include/dev.h | 6+++++-
include/engine.h | 7++++---
include/log.h | 1+
include/option.h | 1+
src/cmd.c | 89++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
src/cmd_dispatch.c | 7++++---
src/dev.c | 39++++++++++++++++++++++++++++-----------
src/engine.c | 7+++++++
src/log.c | 28++++++++++++++++++++++++++--
src/option.c | 2+-
12 files changed, 156 insertions(+), 43 deletions(-)

diff --git a/ctrl.c b/ctrl.c @@ -14,5 +14,6 @@ int main(int argc, char **argv){ engine_init(argc,argv); engine_run(); + engine_deinit(); return 0; } diff --git a/include/cmd.h b/include/cmd.h @@ -8,7 +8,7 @@ * */ #pragma once -#define GENERATE_CALLBACK(name) int name(struct command_exec *cmd,int fd); +#define GENERATE_CALLBACK(name) int name(struct command_exec *cmd,int fd) struct command_exec{ struct command_def *cmd_def; @@ -17,7 +17,8 @@ struct command_exec{ 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); +extern GENERATE_CALLBACK(cmd_reset); +extern GENERATE_CALLBACK(cmd_status); +extern GENERATE_CALLBACK(cmd_board_power); +extern GENERATE_CALLBACK(cmd_reset_line); +extern GENERATE_CALLBACK(cmd_set_emul_mode); diff --git a/include/dev.h b/include/dev.h @@ -12,4 +12,8 @@ extern int closeDevice(int dev); extern int autoConfigBaud(int fd); extern int openDevice(char *dev); -extern int setBaudRate(int fd, speed_t baud); +extern int setBaudRate(int fd, speed_t baud, unsigned int baud_select); +enum BAUD_SELECT{ + BAUD_INPUT=(1<<0), + BAUD_OUTPUT=(1<<1) +}; diff --git a/include/engine.h b/include/engine.h @@ -9,6 +9,7 @@ */ #pragma once #include <stdio.h> -void print_usage(FILE *stream); -int engine_init(int argc, char **argv); -int engine_run(void); +extern void print_usage(FILE *stream); +extern int engine_init(int argc, char **argv); +extern int engine_run(void); +extern int engine_deinit(void); diff --git a/include/log.h b/include/log.h @@ -16,6 +16,7 @@ enum LOG_TYPE{ enum LOG_LEVEL{ LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, + LOG_LEVEL_SUCCESS, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR, LOG_LEVEL_FATAL, diff --git a/include/option.h b/include/option.h @@ -24,6 +24,7 @@ struct conf{ char oldmode; unsigned short int print_usage; unsigned short int log_level; + unsigned short int color; }; diff --git a/src/cmd.c b/src/cmd.c @@ -14,8 +14,10 @@ #include <string.h> #include <stdio.h> #include <unistd.h> - -int cmd_board_reset(struct command_exec *cmd,int fd){ +#include <option.h> +#include <termios.h> +#include <dev.h> +GENERATE_CALLBACK(cmd_board_reset){ if(sendMagicPacket(fd,'r')<0){ do_log("cmd_board_reset","Couldn't send reset packet", LOG_LEVEL_ERROR, LOG_TYPE_NORMAL); return -1; @@ -25,7 +27,7 @@ int cmd_board_reset(struct command_exec *cmd,int fd){ } return 0; } -int cmd_reset(struct command_exec *cmd, int fd){ +GENERATE_CALLBACK(cmd_reset){ if(sendString(fd,"@main reset")<0){ do_log("cmd_reset","Couldn't send reset string", LOG_LEVEL_ERROR, LOG_TYPE_NORMAL); return -1; @@ -41,29 +43,31 @@ int cmd_reset(struct command_exec *cmd, int fd){ free(ret); return 0; } -int cmd_status(struct command_exec *cmd, int fd){ +GENERATE_CALLBACK(cmd_status){ 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); + char c=readMagicPacket(fd); + char str[]={c,'\0'}; + do_log("cmd_status", str, LOG_LEVEL_INFO, LOG_TYPE_RESULT); return 0; } -int cmd_reset_line(struct command_exec *cmd, int fd){ - char c=0; - if(*(char*)cmd->data!='1'&&*cmd->data[0]!='0'){ +GENERATE_CALLBACK(cmd_reset_line){ + do_log("cmd_reset_line","Due to segfault, command not implemented yet", LOG_LEVEL_WARNING, LOG_TYPE_NORMAL); + /* + if(*cmd->data[0]!='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; + str[5]=*cmd->data[0]-'0'; + 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)){ @@ -71,11 +75,11 @@ int cmd_reset_line(struct command_exec *cmd, int fd){ free(r); return -3; } - free(r); + free(r);*/ return 0; } -int cmd_board_power(struct command_exec *cmd, int fd){ - char c=0; +GENERATE_CALLBACK(cmd_board_power){ + char c; if(*cmd->data[0]=='1'){ c='+'; } @@ -91,10 +95,61 @@ int cmd_board_power(struct command_exec *cmd, int fd){ return -2; } char r=readMagicPacket(fd); - putc(r,stdout); - putc('\n',stdout); if(r!=c){ return -3; } return 0; } + +GENERATE_CALLBACK(cmd_set_emul_mode){ + char mode; + speed_t newBaud=0; + if(strcmp(cmd->data[0],"STK500")==0||*cmd->data[0]=='s'){ + mode='s'; + newBaud=B115200; + } + else if(strcmp(cmd->data[0],"AVR910")==0||*cmd->data[0]=='a'){ + mode='a'; + newBaud=B19200; + } + else if(strcmp(cmd->data[0],"Quiet")==0||*cmd->data[0]=='q'){ + mode='q'; + newBaud=B500000; + } + else if(strcmp(cmd->data[0],"UART")==0||*cmd->data[0]=='d'){ + mode='d'; + newBaud=B500000; + } + else if(strcmp(cmd->data[0],"MyMode")==0||*cmd->data[0]=='m'){ + mode='m'; + newBaud=B500000; + } + else if(strcmp(cmd->data[0],"ProgMode")==0||*cmd->data[0]=='p'){ + mode='p'; + } + else{ + configuration.print_usage=1; + char str[50]; + sprintf(str,"Mode %s unknown",cmd->data[0]); + do_log("cmd_set_emul_mode",str,LOG_LEVEL_ERROR,LOG_TYPE_NORMAL); + return -1; + } + char str[50]; + sprintf(str,"Setting mode to %c",mode); + do_log("cmd_set_emul_mode",str,LOG_LEVEL_DEBUG,LOG_TYPE_NORMAL); + if(newBaud!=0){ + setBaudRate(fd,newBaud,BAUD_INPUT); + } + if(sendMagicPacket(fd,mode)<0){ + do_log("cmd_set_emul_mode","Couldn't send magic packet",LOG_LEVEL_ERROR,LOG_TYPE_NORMAL); + return -2; + } + if(newBaud!=0){ + setBaudRate(fd,newBaud,BAUD_OUTPUT); + } + if(readMagicPacket(fd)!=mode){ + do_log("cmd_set_emul_mode","Couldn't set Mode",LOG_LEVEL_ERROR,LOG_TYPE_NORMAL); + return -4; + } + return 0; +} diff --git a/src/cmd_dispatch.c b/src/cmd_dispatch.c @@ -23,7 +23,8 @@ struct command_def commands[]={ {.name="Boardreset", .opt={.arg_length=0, .short_option="-r", .long_option="--board-reset"}, .request_mymode=0, .callback=&cmd_board_reset}, {.name="Resetline", .opt={.usage="[1/0]", .arg_length=1, .short_option="-rL", .long_option="--reset-line"}, .request_mymode=1, .callback=&cmd_reset_line}, {.name="Boardpower", .opt={.usage="[1/0]", .arg_length=1, .short_option="-bP", .long_option="--board-power"}, .request_mymode=0, .callback=&cmd_board_power}, - {.name="Set Mode", .opt={.usage="[1/0]", .arg_length=1, .short_option="-bP", .long_option="--board-power"}, .request_mymode=0, .callback=&cmd_board_power}, + {.name="Set Emulation Mode", .opt={.usage="[UART,STK500,AVR910,Quiet,MyMode,ProgMode]", .arg_length=1, .short_option="-sE", .long_option="--set-emulation"}, .request_mymode=0, .callback=&cmd_set_emul_mode}, + {.name="Rescue Clock", .opt={.usage="[1,0]", .arg_length=1, .short_option="-rC", .long_option="--rescue-clock"}, .request_mymode=0, .callback=&cmd_set_emul_mode}, {.name=NULL} }; int executeCommand(struct command_exec *cmd, int fd){ @@ -42,7 +43,7 @@ int processCommands(struct command_exec **cmdlist, int fd){ 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); + sprintf(str,"Command execution \"%s\" failed",cmdlist[i]->cmd_def->name); do_log("processCommands",str,LOG_LEVEL_ERROR,LOG_TYPE_NORMAL); } i++; @@ -83,7 +84,7 @@ int prepareCommandExecution(int fd, struct conf *config){ config->oldmode=readMagicPacket(fd); if(config->oldmode!='m'){ sendMagicPacket(fd,'m'); - setBaudRate(fd,B500000); + setBaudRate(fd,B500000, BAUD_INPUT|BAUD_OUTPUT); if(readMagicPacket(fd)!='m'){ do_log("prepareCommandExecution","Couldn't switch to mymode",LOG_LEVEL_ERROR,LOG_TYPE_NORMAL); return -1; diff --git a/src/dev.c b/src/dev.c @@ -25,6 +25,8 @@ int closeDevice(int dev){ int autoConfigBaud(int fd){ speed_t speed[3]={B500000, B115200, B19200}; unsigned int i=0; + char str[50]; + do_log("autoConfigBaud","Testing preset Baud",LOG_LEVEL_DEBUG,LOG_TYPE_NORMAL); for(i=0;i<=3; i++){ sendMagicPacket(fd,'i'); char c=readMagicPacket(fd); @@ -36,30 +38,45 @@ int autoConfigBaud(int fd){ do_log("autoConfigBaud","Couldn't evaluate correct baudrate", LOG_LEVEL_FATAL,LOG_TYPE_NORMAL); return -1; } - if(setBaudRate(fd,speed[i])<0){ + if(setBaudRate(fd,speed[i],BAUD_INPUT|BAUD_OUTPUT)<0){ do_log("autoConfigBaud","Couldn't set baudrate", LOG_LEVEL_FATAL,LOG_TYPE_NORMAL); return -2; } - printf("Testing baud %u\n",speed[i]); + sprintf(str,"Testing baud %u",speed[i]); + do_log("autoConfigBaud",str,LOG_LEVEL_DEBUG,LOG_TYPE_NORMAL); } + do_log("autoConfigBaud","BaudRate autoconfiguration successfull",LOG_LEVEL_DEBUG,LOG_TYPE_NORMAL); return 0; } -int setBaudRate(int fd, speed_t baud){ +int setBaudRate(int fd, speed_t baud, unsigned int baud_select){ struct termios options; + char str[50]; 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((baud_select&BAUD_INPUT)>0){ + if(cfsetispeed(&options, baud)<0){ + do_log("setBaudRate","Couldn't set InputSpeed",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); + closeDevice(fd); + return -2; + } + else{ + sprintf(str, "InputBaud set to %u", baud); + do_log("setBaudRate", str, LOG_LEVEL_DEBUG, LOG_TYPE_NORMAL); + } } - if(cfsetospeed(&options, baud)<0){ - do_log("setBaudRate","Couldn't set OutputSpeed",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); - closeDevice(fd); - return -3; + if((baud_select&BAUD_OUTPUT)>0){ + if(cfsetospeed(&options, baud)<0){ + do_log("setBaudRate","Couldn't set OutputSpeed",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); + closeDevice(fd); + return -3; + } + else{ + sprintf(str, "OutputBaud set to %u", baud); + do_log("setBaudRate", str, LOG_LEVEL_DEBUG, LOG_TYPE_NORMAL); + } } if(tcsetattr(fd, TCSANOW, &options)<0){ do_log("setBaudRate","Couldn't set Options",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); diff --git a/src/engine.c b/src/engine.c @@ -57,3 +57,10 @@ int engine_run(void){ return 0; } + +int engine_deinit(void){ + if(configuration.print_usage){ + print_usage(stderr); + } + return 0; +} diff --git a/src/log.c b/src/log.c @@ -13,7 +13,7 @@ #include <stdlib.h> #include <string.h> #include <option.h> -char LOG_LEVEL_names[LOG_LEVEL_COUNT][8]={"DEBUG","INFO","WARNING","ERROR","FATAL"}; +char LOG_LEVEL_names[LOG_LEVEL_COUNT][8]={"DEBUG","INFO", "SUCCESS","WARNING","ERROR","FATAL"}; void log_string(char *str, unsigned short int level){ FILE *stream; if(level>=LOG_LEVEL_WARNING){ @@ -22,8 +22,32 @@ void log_string(char *str, unsigned short int level){ else{ stream=stdout; } + char *color=""; + char *reset_color="\e[0m"; + if(configuration.color){ + switch(level){ + case LOG_LEVEL_DEBUG: + color="\e[2;37;40m"; + break; + case LOG_LEVEL_INFO: + color="\e[0;37;40m"; + break; + case LOG_LEVEL_WARNING: + color="\e[1;33;40m"; + break; + case LOG_LEVEL_ERROR: + color="\e[1;31;40m"; + break; + case LOG_LEVEL_FATAL: + color="\e[1;33;41m"; + break; + case LOG_LEVEL_SUCCESS: + color="\e[1;37;43m"; + break; + } + } if(configuration.log_level<=level){ - fprintf(stream,"%s\n",str); + fprintf(stream,"%s%s\n%s",color, str, reset_color); } } void do_log(char *src, char *msg, unsigned short int level, unsigned short int type){ diff --git a/src/option.c b/src/option.c @@ -13,7 +13,7 @@ #include <cmd_dispatch.h> #include <stdlib.h> #include <string.h> -struct conf configuration={.device="/dev/ttyUSB0", .log_level=LOG_LEVEL_DEBUG}; +struct conf configuration={.device="/dev/ttyUSB0", .log_level=LOG_LEVEL_DEBUG, .color=1}; struct command_exec *optionAddCommand(struct conf *config, struct command_def *cmd_def, char **argv){ static unsigned int cmdlist_cnt;