Proof of concept implementation of various ROTS features/requirements
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
tbm-go-rots/admin/main.go

245 lines
5.0 KiB

package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
"time"
)
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{
"echo": cmd_echo,
"jtag": cmd_jtag,
"buzzer": cmd_buzzer,
"led": cmd_led,
"reset": cmd_reset,
"flash_probe": cmd_flash_probe,
"flash_release": cmd_flash_release,
"flash_info": cmd_flash_info,
"flash_read": cmd_flash_read,
"flash_erase": cmd_flash_erase,
"ftl_probe": cmd_ftl_probe,
"date": cmd_date,
"time": cmd_time,
"set-date": cmd_set_date,
"set-time": cmd_set_time,
"sync-time" : cmd_sync_time,
"mount": cmd_mount,
"umount": cmd_umount,
"format": cmd_format,
"mkdir": cmd_mkdir,
"rmdir": cmd_rmdir,
"ls": cmd_ls,
"stat": cmd_stat,
"cat": cmd_cat,
"write": cmd_write,
"append": cmd_append,
"mv": cmd_mv,
"cp": cmd_cp,
"rm": cmd_rm,
}
)
func cmd_generic(tbm *TBM, command string, args []string, payload []byte) error {
cmd := &Command{command, args, payload}
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_echo(tbm *TBM, args []string) error {
return cmd_generic(tbm, "echo", args, nil);
}
func cmd_jtag(tbm *TBM, args []string) error {
return cmd_generic(tbm, "jtag", args, nil)
}
func cmd_buzzer(tbm *TBM, args []string) error {
return cmd_generic(tbm, "buzzer", args, nil)
}
func cmd_led(tbm *TBM, args []string) error {
return cmd_generic(tbm, "led", args, nil)
}
func cmd_reset(tbm *TBM, args []string) error {
return cmd_generic(tbm, "reset", args, nil)
}
func cmd_flash_probe(tbm *TBM, args []string) error {
return cmd_generic(tbm, "flash_probe", args, nil)
}
func cmd_flash_release(tbm *TBM, args []string) error {
return cmd_generic(tbm, "flash_release", args, nil)
}
func cmd_flash_info(tbm *TBM, args []string) error {
return cmd_generic(tbm, "flash_info", args, nil)
}
func cmd_flash_read(tbm *TBM, args []string) error {
return cmd_generic(tbm, "flash_read", args, nil)
}
func cmd_flash_erase(tbm *TBM, args []string) error {
return cmd_generic(tbm, "flash_erase", args, nil)
}
func cmd_ftl_probe(tbm *TBM, args []string) error {
return cmd_generic(tbm, "ftl_probe", args, nil)
}
func cmd_date(tbm *TBM, args []string) error {
return cmd_generic(tbm, "date", args, nil)
}
func cmd_time(tbm *TBM, args []string) error {
return cmd_generic(tbm, "time", args, nil)
}
func cmd_set_date(tbm *TBM, args []string) error {
return cmd_generic(tbm, "set_date", args, nil)
}
func cmd_set_time(tbm *TBM, args []string) error {
return cmd_generic(tbm, "set_time", args, nil)
}
func cmd_sync_time(tbm *TBM, args []string) error {
return cmd_generic(tbm, "set_time", []string{strconv.FormatInt(
time.Now().Unix(), 10)}, nil)
}
func cmd_mount(tbm *TBM, args []string) error {
return cmd_generic(tbm, "mount", args, nil)
}
func cmd_umount(tbm *TBM, args []string) error {
return cmd_generic(tbm, "umount", args, nil)
}
func cmd_format(tbm *TBM, args []string) error {
return cmd_generic(tbm, "format", args, nil)
}
func cmd_mkdir(tbm *TBM, args []string) error {
return cmd_generic(tbm, "mkdir", args, nil)
}
func cmd_rmdir(tbm *TBM, args []string) error {
return cmd_generic(tbm, "rmdir", args, nil)
}
func cmd_ls(tbm *TBM, args []string) error {
return cmd_generic(tbm, "ls", args, nil)
}
func cmd_stat(tbm *TBM, args []string) error {
return cmd_generic(tbm, "stat", args, nil)
}
func cmd_cat(tbm *TBM, args []string) error {
return cmd_generic(tbm, "cat", args, nil)
}
func cmd_write(tbm *TBM, args []string) error {
f, _ := os.Open(args[0])
defer f.Close()
buf := make([]byte, 256)
n, err := f.Read(buf)
if n == 0 {
return err
}
cmd_generic(tbm, "write", []string{args[1]}, buf)
for {
n, err = f.Read(buf)
if n == 0 {
return err
}
cmd_generic(tbm, "append", []string{args[1]}, buf)
}
}
func cmd_append(tbm *TBM, args []string) error {
var n int
var err error
f, _ := os.Open(args[0])
defer f.Close()
buf := make([]byte, 256)
for {
n, err = f.Read(buf)
if n == 0 {
return err
}
cmd_generic(tbm, "append", []string{args[1]}, buf)
}
}
func cmd_mv(tbm *TBM, args []string) error {
return cmd_generic(tbm, "mv", args, nil)
}
func cmd_cp(tbm *TBM, args []string) error {
return cmd_generic(tbm, "cp", args, nil)
}
func cmd_rm(tbm *TBM, args []string) error {
return cmd_generic(tbm, "rm", args, nil)
}
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(*serial_device, *serial_baudrate)
if err != nil {
log.Fatal(err)
}
t.Handle()
defer t.Close()
err = command_function(t, args[1:])
if err != nil {
log.Fatal(err)
}
}