MySmartUSB-MK3-Interface

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

cmd_dispatch.c (4516B)


      1 /*
      2  * MySmartUSB MK3 Interface
      3  * 
      4  * @copyright: 	Copyright (c) 2012, Dominik Schmidt
      5  * @author:		Dominik Schmidt <das1993@hotmail.com>
      6  * @version:	0.0.0 
      7  * @license: 	CC-BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
      8  * 
      9 */
     10 #include <log.h>
     11 #include <cmd_dispatch.h>
     12 #include <cmd.h>
     13 #include <stdlib.h>
     14 #include <option.h>
     15 #include <termios.h>
     16 #include <string.h>
     17 #include <com.h>
     18 #include <dev.h>
     19 #include <stdio.h>
     20 #include <unistd.h>
     21 struct command_def commands[]={
     22 	{.name="Status", .opt={.arg_length=0, .short_option="-s", .long_option="--status"}, .request_mymode=0, .callback=&cmd_status},
     23 	{.name="Reset", .opt={.arg_length=0, .short_option="-R", .long_option="--reset"}, .request_mymode=1, .callback=&cmd_reset},
     24 	{.name="Boardreset", .opt={.arg_length=0, .short_option="-r", .long_option="--board-reset"}, .request_mymode=0, .callback=&cmd_board_reset},
     25 	{.name="Resetline", .opt={.usage="[1/0]", .arg_length=1, .short_option="-rL", .long_option="--reset-line"}, .request_mymode=1, .callback=&cmd_reset_line},
     26 	{.name="Boardpower", .opt={.usage="[1/0]", .arg_length=1, .short_option="-bP", .long_option="--board-power"}, .request_mymode=0, .callback=&cmd_board_power},
     27 	{.name="Emulation Mode", .opt={.usage="[d/UART,s/STK500,a/AVR910,q/Quiet,m/MyMode,p/ProgMode]", .arg_length=1, .short_option="-eM", .long_option="--emulation-mode"}, .request_mymode=0, .callback=&cmd_set_emul_mode},
     28 	{.name="Rescue Clock", .opt={.usage="[1,0]", .arg_length=1, .short_option="-rC", .long_option="--rescue-clock"}, .request_mymode=0, .callback=&cmd_set_rescue_clock},
     29 	{.name="Programming Mode", .opt={.usage="[i/ISP, p/HVP, s/HVS, t/TPI, T/TPIHV]", .arg_length=1, .short_option="-pM", .long_option="--programming-mode"}, .request_mymode=1, .callback=&cmd_set_prog_mode},
     30 	{.name="ISP Speed", .opt={.usage="[<UINT>, A/Auto]", .arg_length=1, .short_option="-iS", .long_option="--isp-speed"}, .request_mymode=1, .callback=&cmd_set_isp_speed},
     31 	{.name="Par Speed", .opt={.usage="[<UINT>, A/Auto]", .arg_length=1, .short_option="-pS", .long_option="--par-speed"}, .request_mymode=1, .callback=&cmd_set_par_speed},
     32 	{.name=NULL}
     33 };
     34 int executeCommand(struct command_exec *cmd, int fd){
     35 	if(cmd->cmd_def->callback(cmd,fd)<0){
     36 		do_log("executeCommand","Couldn't execute command",LOG_LEVEL_ERROR,LOG_TYPE_NORMAL);
     37 	}
     38 	else{
     39 		do_log("executeCommand","Execution successfull",LOG_LEVEL_DEBUG,LOG_TYPE_NORMAL);
     40 	}
     41 	return 0;
     42 }
     43 int processCommands(struct command_exec **cmdlist, int fd){
     44 	unsigned int i=0;
     45 	char str[40];
     46 	while(cmdlist[i]!=NULL){
     47 		sprintf(str,"Executing command %s",cmdlist[i]->cmd_def->name);
     48 		do_log("processCommands",str,LOG_LEVEL_DEBUG,LOG_TYPE_NORMAL);
     49 		if(executeCommand(cmdlist[i], fd)<0){
     50 			sprintf(str,"Command execution \"%s\" failed",cmdlist[i]->cmd_def->name);
     51 			do_log("processCommands",str,LOG_LEVEL_ERROR,LOG_TYPE_NORMAL);
     52 		}
     53 		i++;
     54 	}
     55 	if(i==0){
     56 		configuration.print_usage=1;
     57 	}
     58 	return 0;
     59 }
     60 struct command_def *getCmdDefByName(char *name){
     61 	struct command_def *cmdptr=commands;
     62 	while(cmdptr->name!=NULL){
     63 		if(strcmp(name,cmdptr->name)==0){
     64 			return cmdptr;
     65 		}
     66 		cmdptr++;
     67 	}
     68 	return NULL;
     69 }
     70 struct command_def *getCmdDefByOpt(char *opt){
     71 	struct command_def *cmdptr=commands;
     72 	while(cmdptr->name!=NULL){
     73 		if(strcmp(opt,cmdptr->opt.long_option)==0||strcmp(opt,cmdptr->opt.short_option)==0){
     74 			return cmdptr;
     75 		}
     76 		cmdptr++;
     77 	}
     78 	return NULL;
     79 }
     80 struct command_exec *newCommand(struct command_def *cd){
     81 	struct command_exec *cmd=malloc(sizeof(struct command_exec));
     82 	cmd->cmd_def=cd;
     83 	return cmd;
     84 }
     85 
     86 
     87 int prepareCommandExecution(int fd, struct conf *config){
     88 	config->oldmode.mode=0;
     89 	if(config->need_mymode){
     90 		sendMagicPacket(fd,'i');
     91 		config->oldmode.mode=readMagicPacket(fd);
     92 		config->oldmode.baud=getBaudRate(fd);
     93 		if(config->oldmode.mode!='m'){
     94 			char str[50];
     95 			sprintf(str,"Switching from %c to m", config->oldmode.mode);
     96 			do_log("prepareCommandExecution",str,LOG_LEVEL_DEBUG,LOG_TYPE_NORMAL);
     97 			sendMagicPacket(fd,'m');
     98 			usleep(BAUD_RATE_SWITCH_WAIT);
     99 			setBaudRateExt(fd,B500000, BAUD_INPUT|BAUD_OUTPUT, TCSADRAIN);
    100 			char c=readMagicPacket(fd);
    101 			if(c!='m'){
    102 				do_log("prepareCommandExecution","Couldn't switch to mymode",LOG_LEVEL_ERROR,LOG_TYPE_NORMAL);
    103 				return -1;
    104 			}
    105 		}
    106 		
    107 	}
    108 	return 0;
    109 }
    110 int finishCommandExecution(int fd, struct conf *config){
    111 	if(config->oldmode.mode>0){
    112 		sendMagicPacket(fd,config->oldmode.mode);	
    113 		usleep(BAUD_RATE_SWITCH_WAIT);
    114 		setBaudRate(fd, config->oldmode.baud, BAUD_INPUT|BAUD_OUTPUT);
    115 	}
    116 	return 0;
    117 }