diff --git a/protocol/main.go b/protocol/main.go index 3352527..6c8cd1f 100644 --- a/protocol/main.go +++ b/protocol/main.go @@ -1,20 +1,82 @@ package main import ( + "flag" + "fmt" "log" ) +type CommandFunc func(tbm *TBM, args []string) error + +var ( + serial_device = flag.String("serial-device", "/dev/ttyUSB0", "Serial device to use") + serial_baudrate = flag.Int("serial-baud", 9600, "Serial baud rate") + + commands = map[string]CommandFunc{"hi": cmd_hi, "ls": cmd_ls, "cat": cmd_cat, "time": cmd_time, "bootversion": cmd_bootversion, "bootok": cmd_bootok} +) + +func cmd_generic(tbm *TBM, command string, args []string) error { + cmd := &Command{command, args} + tbm.Commands <- cmd + res := <-tbm.Results + + if res.errcode[0] != '0' { + return fmt.Errorf("Error: %s", res.errcode) + } + fmt.Printf("%s", res.output) + + return nil +} + +func cmd_hi(tbm *TBM, args []string) error { + return cmd_generic(tbm, "hi", []string{"v20170802 testing"}) +} + +func cmd_ls(tbm *TBM, args []string) error { + return cmd_generic(tbm, "ls", args) +} + +func cmd_cat(tbm *TBM, args []string) error { + return cmd_generic(tbm, "cat", args) +} + +func cmd_time(tbm *TBM, args []string) error { + return cmd_generic(tbm, "time", args) +} + +func cmd_bootversion(tbm *TBM, args []string) error { + return cmd_generic(tbm, "booting", args) +} + +func cmd_bootok(tbm *TBM, args []string) error { + return cmd_generic(tbm, "booting", []string{"ok"}) +} + func main() { + flag.Parse() + + args := flag.Args() + if len(args) == 0 { + log.Fatalf("Please specify at least one command") + } + command := args[0] + + command_function, ok := commands[command] + + if !ok { + log.Fatalf("Unknown command: %s", command) + } + t := &TBM{} - err := t.Connect("/dev/ttyUSB0") + err := t.Connect(*serial_device, *serial_baudrate) if err != nil { log.Fatal(err) } t.Handle() defer t.Close() - //cmd := &Command{"Hi", []string{"v20170802 testing"}} - //t.Commands <- cmd - res := <-t.Results - log.Printf("Got result: %q", res) + err = command_function(t, args[1:]) + if err != nil { + log.Fatal(err) + } } diff --git a/protocol/tbm.go b/protocol/tbm.go index 9242efe..dc10627 100644 --- a/protocol/tbm.go +++ b/protocol/tbm.go @@ -3,6 +3,7 @@ package main import ( "fmt" "io" + "log" "strings" "github.com/tarm/serial" @@ -24,11 +25,11 @@ type Result struct { errcode []byte } -func (t *TBM) Connect(device string) error { +func (t *TBM) Connect(device string, baud int) error { t.Commands = make(chan *Command) t.Results = make(chan *Result) - serialconfig := &serial.Config{Name: device, Baud: 9600} + serialconfig := &serial.Config{Name: device, Baud: baud} port, err := serial.OpenPort(serialconfig) if err != nil { @@ -72,14 +73,29 @@ func deserialise(in io.Reader, init []byte) (*Result, []byte, error) { data = append(data, buf[:n]...) for i := 0; i < n2; i++ { + // '\r' is really '\n' + if data[i] == '\r' { + data[i] = '\n' + } + if data[i] == 0x4 { if want_output { want_output = false output = data[:i] data = data[i+1:] + /* + // The TBM gives us our own commands back + for j := 0; j < len(output); j++ { + if output[j] == '\r' { + output = output[j+1:] + break + } + } + */ break } else { errorcode = data[:i] + rest = data[i+1:] return &Result{output, errorcode}, rest, nil } @@ -120,7 +136,8 @@ func serialise(out io.Writer, command string, args []string) error { out_string := strings.Join(quoted_args, " ") output := []byte(out_string) - output = append(output, 0x4) + output = append(output, '\r') + //output = append(output, 0x4) _, err := out.Write(output) // err is non nil if n != len(output)