123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592 |
- package main
-
- import (
- "fmt"
-
- "bufio"
- "bytes"
- "github.com/gordonklaus/portaudio"
- "github.com/naleek/gortty"
- "io"
- "net"
- "regexp"
- "strings"
- "unicode/utf8"
- "time"
- "os"
- "strconv"
- )
-
-
- func main() {
-
- m := &modem{}
-
- rttySettings := &gortty.ModemSettings{
- Baud: 45,
- StopBits: 2,
- DataBits: 5,
- SampleRate: 8000,
- OneFreq: 2125,
- ZeroFreq: 2295,
- }
-
- charset := gortty.LoadCharset(&gortty.USTTY)
-
- in := make([]int16, 64)
- out := make([]int16, 64)
- var stream *portaudio.Stream
-
- useStdio := false
-
- if !useStdio {
- // Set up audio with system default input and output
- portaudio.Initialize()
- defer portaudio.Terminate()
- var err error
- stream, err = portaudio.OpenDefaultStream(1, 1, float64(rttySettings.SampleRate), len(in), in, out)
- if err != nil {
- panic(err)
- }
- defer stream.Close()
- }
- modulatorToAudio := make(chan int16)
- audioToDemodulator := make(chan int16)
-
- if !useStdio {
- // Set up modulator pipeline
- ttyToEncoder := make(chan rune, 100)
- m.ttyWriter = NewModemWriter(ttyToEncoder)
- encoderToModulator := make(chan byte)
- m.encoder = gortty.NewEncoder(ttyToEncoder, encoderToModulator, charset)
- m.modulator = gortty.NewModulator(rttySettings, encoderToModulator, modulatorToAudio)
- m.modulator.SetIdleDelay(1000)
-
- // Set up demodulator pipeline
- demodulatorToDecoder := make(chan byte)
- m.demodulator = gortty.NewDemodulator(rttySettings, audioToDemodulator, demodulatorToDecoder)
- decoderToTTY := make(chan rune)
- m.decoder = gortty.NewDecoder(demodulatorToDecoder, decoderToTTY, charset)
- m.ttyReader = NewModemReader(decoderToTTY)
- } else {
- m.ttyReader = os.Stdin
- m.ttyWriter = os.Stdout
- }
-
- go m.cli()
-
- if !useStdio {
- // start audio
- stream.Start()
- defer stream.Stop()
- // Run audio output in goroutine
- go func() {
- l := 0
- for v := range modulatorToAudio {
- out[l] = v
- l++
- if l == 64 {
- l = 0
- stream.Write()
- }
- }
- }()
-
- // Run audio input
- for {
- stream.Read()
- for i := range in {
- audioToDemodulator <- in[i]
- }
- }
- }
-
- }
-
- type ModemReader struct {
- input chan rune
- }
-
- func NewModemReader(c chan rune) *ModemReader {
- m := &ModemReader{}
- m.input = c
- return m
- }
-
- func (m *ModemReader) Read(p []byte) (n int, err error) {
- r := <-m.input
-
- fmt.Print(string(r))
-
- l := utf8.EncodeRune(p, r)
- return l, nil
- }
-
- func (m *ModemReader) Add(b rune) {
- m.input <- b
- }
-
- type ModemWriter struct {
- output chan rune
- }
-
- func NewModemWriter(c chan rune) *ModemWriter {
- m := &ModemWriter{}
- m.output = c
- return m
- }
-
- func (m *ModemWriter) Write(p []byte) (n int, err error) {
-
- runes := bytes.Runes(p)
-
- fmt.Print(string(runes))
-
- for _, r := range runes {
- m.output <- r
- }
- return len(p), nil
- }
-
- func (m *ModemWriter) Get() rune {
- return <-m.output
- }
-
- type modem struct {
- ttyReader io.Reader
- ttyWriter io.Writer
- modulator *gortty.Modulator
- demodulator *gortty.Demodulator
- encoder *gortty.Encoder
- decoder *gortty.Decoder
- }
-
- func (m *modem) modulatorCallback(event int, data interface{}) {
- switch event {
- case gortty.EventActive:
- fmt.Println("EventActive")
- m.demodulator.Inhibit(true)
- case gortty.EventIdle:
- fmt.Println("EventIdle")
- m.demodulator.Inhibit(false)
- }
- }
-
- type configFunction func(action int, value string) string
-
- type configRegister struct {
- help string
- f configFunction
- }
-
- const (
- registerGet = iota
- registerSet
- )
-
- func (m *modem) cli() {
-
- fmt.Println("RTTY TCP Modem 1.0")
-
- autoAnswer := false
- escapeRune := '?'
- modulatorOn := true
- turnaroundDelay := 0
- usos := false
-
- settings := map[string]configRegister{
- "0": {
- help: "auto-answer enable",
- f: func(action int, value string) string {
- switch action {
- case registerGet:
- if autoAnswer {
- return "1"
- } else {
- return "0"
- }
- case registerSet:
- autoAnswer = (value == "1")
- return "OK"
- }
- return ""
- },
- },
- "2": {
- help: "escape character",
- f: func(action int, value string) string {
- switch action {
- case registerGet:
- return string(escapeRune)
- case registerSet:
- runes := []rune(value)
- escapeRune = runes[0]
- return "OK"
- }
- return ""
- },
- },
- "3": {
- help: "modulator enable",
- f: func(action int, value string) string {
- switch action {
- case registerGet:
- if modulatorOn {
- return "1"
- } else {
- return "0"
- }
- case registerSet:
- modulatorOn = (value == "1")
- if m.modulator != nil {
- if modulatorOn {
- m.modulator.SetAmplitude(16384)
- } else {
- m.modulator.SetAmplitude(0)
- }
- }
- return "OK"
- }
- return ""
- },
- },
- "4": {
- help: "turnaround delay",
- f: func(action int, value string) string {
- switch action {
- case registerGet:
- return fmt.Sprintln(turnaroundDelay)
- case registerSet:
- if d, err := strconv.Atoi(value); err == nil {
- turnaroundDelay = d
- return "OK"
- } else {
- return "ERROR"
- }
- }
- return ""
- },
- },
- "5": {
- help: "unshift on space",
- f: func(action int, value string) string {
- switch action {
- case registerGet:
- if usos {
- return "1"
- } else {
- return "0"
- }
- case registerSet:
- usos = (value == "1")
- m.decoder.UnshiftOnSpace(usos)
- return "OK"
- }
- return ""
- },
- },
- }
-
- localEcho := false
- ttyBufferedReader := bufio.NewReader(m.ttyReader)
-
- var netConn *NetConn
-
- if m.modulator != nil {
- m.modulator.SetCallback(m.modulatorCallback)
- }
-
-
- for {
-
- s, _ := ttyBufferedReader.ReadString('\n')
-
- //fmt.Println(s)
- s = strings.ToUpper(s)
- regex, _ := regexp.Compile(`^AT([SHOED?])([0-9])?(\?)?(?:[=|\:]([\S]*))?(?: (.*))?`)
-
- params := regex.FindStringSubmatch(s)
-
- if params == nil {
- continue
- }
-
- command := params[1]
-
- var register string
- if len(params) > 2 {
- register = params[2]
- }
-
- var registerQuery string
- if len(params) > 3 {
- registerQuery = params[3]
- }
-
- var value string
- if len(params) > 4 {
- value = params[4]
- }
-
- var parameter string
- if len(params) > 5 {
- parameter = params[5]
- }
-
- //fmt.Fprintf(m.ttyWriter, "command: '%s' register: '%s' query: '%s' value: '%s' parameter: '%s'\n", command, register, registerQuery, value, parameter)
- switch command {
- case "?":
- fmt.Fprintf(m.ttyWriter, "Setting Registers\n")
- for s, conf := range settings {
- fmt.Fprintf(m.ttyWriter, "%s: %s (current value: %s)\n", s, conf.help, conf.f(registerGet, ""))
- }
- case "O": // online
- if !netConn.Connected() {
- fmt.Fprintln(m.ttyWriter, "ERROR No active session")
- break
- }
- fmt.Fprintln(m.ttyWriter, "OK")
- dataMode(netConn, ttyBufferedReader, m.ttyWriter, escapeRune)
- case "D": // dial
- if netConn.Connected() {
- fmt.Fprintln(m.ttyWriter, "ERROR Session in progress")
- break
- }
- addr := strings.TrimSpace(parameter)
-
- var err error
- netConn, err = NewNetConn(addr)
- if err != nil {
- fmt.Fprintln(m.ttyWriter, "ERROR", err)
- break
- } else {
- fmt.Fprintln(m.ttyWriter, "CONNECTED")
- dataMode(netConn, ttyBufferedReader, m.ttyWriter, escapeRune)
- }
- case "H": // hangup
- if !netConn.Connected() {
- fmt.Fprintln(m.ttyWriter, "ERROR No active session")
- break
- }
- netConn.Hangup()
- fmt.Fprintln(m.ttyWriter, "OK")
- case "E": // local echo
- localEcho = (register == "1")
- fmt.Fprintln(m.ttyWriter, "local echo", localEcho)
- case "S": // local echo
- if conf, ok := settings[register]; ok {
- if registerQuery == "?" {
- fmt.Fprintln(m.ttyWriter, conf.f(registerGet, ""))
- } else if value != "" {
- fmt.Fprintln(m.ttyWriter, conf.f(registerSet, value))
- } else {
- fmt.Fprintln(m.ttyWriter, "ERROR")
- }
- }
- }
- }
-
- }
-
-
-
- func dataMode(netConn *NetConn, ttyBufferedReader *bufio.Reader, ttyWriter io.Writer, escapeRune rune) {
-
- ttyCancelReader := make(chan struct{})
- ttyReadChan := make(chan rune)
- ttyReadPending := make(chan struct{})
-
- var t *time.Timer
- escapeRunes := 0
-
-
- go func() {
- for {
- r, l, err := ttyBufferedReader.ReadRune()
- if err != nil && err != io.EOF {
- return
- }
- if l > 0 {
- select {
- case <-ttyCancelReader:
- ttyBufferedReader.UnreadRune() // oops this wasn't ours. let's put it back.
- close(ttyReadPending)
- return
- default:
- ttyReadChan <- r
- }
- }
- }
- }()
-
- netReadChan := netConn.OutputChannel()
- netConn.Resume()
- pipeLoop:
- for {
- var r rune
- if t == nil {
- select {
- case r = <-ttyReadChan:
- case q := <-netReadChan:
- ttyWriter.Write([]byte(string(q)))
- continue pipeLoop
- }
- } else {
- select {
- case r = <-ttyReadChan:
- case q := <-netReadChan:
- ttyWriter.Write([]byte(string(q)))
- continue pipeLoop
- case <-t.C:
- fmt.Fprintln(ttyWriter, "\nOK")
- close(ttyCancelReader)
- netConn.Pause()
- break pipeLoop
-
- }
- }
- if r == escapeRune && escapeRunes < 3 {
- escapeRunes++
- } else {
- if t != nil {
- t.Stop()
- }
- escapeRunes = 0
- }
- if escapeRunes == 3 {
- t = time.NewTimer(1*time.Second)
- }
- err := netConn.Write(r)
- if err != nil {
- fmt.Fprintln(ttyWriter, "NO CARRIER")
- close(ttyCancelReader)
- break pipeLoop
- }
- }
- <- ttyReadPending
-
- }
-
-
-
-
-
-
-
-
- type NetConn struct {
- netReader *bufio.Reader
- conn net.Conn
- netReadChan chan rune
- hangup chan struct{}
- paused bool
- connected bool
- }
-
- func NewNetConn(addr string) (*NetConn, error) {
- c := &NetConn{}
- var err error
- c.conn, err = net.Dial("tcp", addr)
- if err != nil {
- return nil, err
- }
- c.connected = true
-
- c.netReader = bufio.NewReader(c.conn)
-
- c.netReadChan = make(chan rune)
- c.hangup = make(chan struct{})
- c.paused = false
-
- go func() {
- for {
- r, l, err := c.netReader.ReadRune()
- if err != nil && err != io.EOF {
- c.cleanup()
- return
- }
- if c.paused || l == 0 {
- select {
- case <- c.hangup:
- c.cleanup()
- return
- default: // discard
- }
- } else {
- select {
- case c.netReadChan <- r:
- case <- c.hangup:
- c.cleanup()
- return
- }
- }
- }
-
- }()
-
-
- return c, nil
- }
-
- func (c *NetConn) Write(r rune) error {
- if c == nil {
- return fmt.Errorf("NetConn not initialized")
- }
- _, err := c.conn.Write([]byte(string(r)))
- if err != nil {
- close(c.hangup)
- }
- return err
- }
-
- func (c *NetConn) Hangup() {
- if c == nil {
- return
- }
- c.conn.Close()
- close(c.hangup)
- }
-
- func (c *NetConn) cleanup() {
- close(c.netReadChan)
- c.connected = false
- }
-
- func (c *NetConn) Pause() {
- if c == nil {
- return
- }
- c.paused = true
- }
-
- func (c *NetConn) Resume() {
- if c == nil {
- return
- }
- c.paused = false
- }
-
- func (c *NetConn) OutputChannel() chan rune {
- if c == nil {
- return nil
- }
- return c.netReadChan
- }
-
- func (c *NetConn) Connected() bool {
- if c == nil {
- return false
- }
- return c.connected
- }
-
-
-
-
-
-
-
-
-
-
|