diff options
-rw-r--r-- | src/pira32ctl/command.go | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/src/pira32ctl/command.go b/src/pira32ctl/command.go index 5a8e860..390cb40 100644 --- a/src/pira32ctl/command.go +++ b/src/pira32ctl/command.go @@ -22,6 +22,7 @@ package main import ( + "errors" "fmt" "time" @@ -29,14 +30,24 @@ import ( ) const ( - REQ_PERSISTANT = '*' + REQ_PERSISTANT = "*" - RESP_OK = '+' - RESP_UNKNOWN_CMD = '!' - RESP_INVALID_ARG = '-' - RESP_PARTIAL = '/' + RESP_OK = "+" + RESP_UNKNOWN_CMD = "!" + RESP_INVALID_ARG = "-" + RESP_PARTIAL = "/" storeExecutionTime = 10 * time.Millisecond + + respTimeout = 500 * time.Millisecond +) + +var ( + ErrInvalidResp = errors.New("got invalid response") + ErrTimeout = errors.New("timeout") + ErrUnknownCmd = errors.New("unknown command") + ErrInvalidArg = errors.New("invalid argument") + ErrPartial = errors.New("partial success") ) type Command interface { @@ -81,8 +92,25 @@ func (c BasicCommand) Store(port *goserial.Port, params ...interface{}) error { if len(params) != 0 { return fmt.Errorf("this command takes no arguments") } - // TODO: implement this - return nil + port.TX <- REQ_PERSISTANT + c.code + t := time.NewTimer(respTimeout) + var resp string + select { + case resp = <-port.RX: + case <-t.C: + return ErrTimeout + } + switch resp { + case RESP_OK: + return nil + case RESP_UNKNOWN_CMD: + return ErrUnknownCmd + case RESP_INVALID_ARG: + return ErrUnknownCmd + case RESP_PARTIAL: + return ErrUnknownCmd + } + return ErrInvalidResp } func (c BasicCommand) AssignStore(port *goserial.Port, params ...interface{}) error { |