package main import ( "context" "flag" "fmt" "github.com/BurntSushi/toml" "github.com/naleek/gortty" "log" "os" "strconv" ) const ( verMajor = 0 verMinor = 1 verPatch = 0 ) type config struct { Baud float64 `toml:"baud"` StopBits float64 `toml:"stopbits"` DataBits int `toml:"databits"` SampleRate int `toml:"samplerate"` MarkFrequency int `toml:"markfrequency"` SpaceFrequency int `toml:"spacefrequency"` LocalEcho bool `toml:"localecho"` CodeSet string `toml:"codeset"` Registers map[string]string `toml:"registers"` } func main() { var configFile string flag.StringVar(&configFile, "c", "rttymodem.toml", "Configuration file path") showHelp := flag.Bool("h", false, "Show usage and exit") showVersion := flag.Bool("v", false, "Show version and exit") flag.Parse() if *showHelp { flag.PrintDefaults() os.Exit(0) } if *showVersion { fmt.Printf("%d.%d.%d\n", verMajor, verMinor, verPatch) os.Exit(0) } log.Printf("starting up with config from %s") conf := &config{} if _, err := toml.DecodeFile(configFile, &conf); err != nil { log.Fatal(err) } s := &gortty.ModemSettings{ Baud: conf.Baud, StopBits: conf.StopBits, DataBits: conf.DataBits, SampleRate: conf.SampleRate, OneFreq: conf.MarkFrequency, ZeroFreq: conf.SpaceFrequency, } if conf.CodeSet == "" { conf.CodeSet = "USTTY" } c, err := gortty.LoadCodeSet(conf.CodeSet) if err != nil { log.Fatal(err) } ctx, cancel := context.WithCancel(context.Background()) m, err := NewModem(ctx, s, c) if err != nil { log.Fatal(err) } m.SetLocalEcho(conf.LocalEcho) for i, r := range conf.Registers { n, err := strconv.Atoi(i) if err != nil { continue } m.SetRegister(n, r) } defer m.Close() m.run() cancel() }