|
@@ -1,444 +1,101 @@
|
1
|
1
|
package main
|
2
|
2
|
|
3
|
3
|
import (
|
|
4
|
+ "context"
|
|
5
|
+ "flag"
|
4
|
6
|
"fmt"
|
5
|
|
-
|
6
|
|
- "bufio"
|
7
|
|
- "github.com/gordonklaus/portaudio"
|
|
7
|
+ "github.com/BurntSushi/toml"
|
8
|
8
|
"github.com/naleek/gortty"
|
9
|
|
- "io"
|
10
|
|
- "regexp"
|
11
|
|
- "strings"
|
12
|
|
- "time"
|
|
9
|
+ "log"
|
|
10
|
+ "os"
|
13
|
11
|
"strconv"
|
14
|
12
|
)
|
15
|
13
|
|
|
14
|
+const (
|
|
15
|
+ verMajor = 0
|
|
16
|
+ verMinor = 1
|
|
17
|
+ verPatch = 0
|
|
18
|
+)
|
|
19
|
+
|
|
20
|
+type config struct {
|
|
21
|
+ Baud float64 `toml:"baud"`
|
|
22
|
+ StopBits float64 `toml:"stopbits"`
|
|
23
|
+ DataBits int `toml:"databits"`
|
|
24
|
+ SampleRate int `toml:"samplerate"`
|
|
25
|
+ MarkFrequency int `toml:"markfrequency"`
|
|
26
|
+ SpaceFrequency int `toml:"spacefrequency"`
|
|
27
|
+ LocalEcho bool `toml:"localecho"`
|
|
28
|
+ CodeSet string `toml:"codeset"`
|
|
29
|
+ Registers map[string]string `toml:"registers"`
|
|
30
|
+}
|
16
|
31
|
|
17
|
32
|
func main() {
|
18
|
33
|
|
19
|
|
- m := &modem{}
|
20
|
|
-
|
21
|
|
- rttySettings := &gortty.ModemSettings{
|
22
|
|
- Baud: 45,
|
23
|
|
- StopBits: 2,
|
24
|
|
- DataBits: 5,
|
25
|
|
- SampleRate: 8000,
|
26
|
|
- OneFreq: 2125,
|
27
|
|
- ZeroFreq: 2295,
|
28
|
|
- }
|
|
34
|
+ var configFile string
|
29
|
35
|
|
30
|
|
- charset := gortty.LoadCharset(&gortty.USTTY)
|
|
36
|
+ flag.StringVar(&configFile, "c", "rttymodem.toml", "Configuration file path")
|
31
|
37
|
|
32
|
|
- in := make([]int16, 64)
|
33
|
|
- out := make([]int16, 64)
|
34
|
|
- var stream *portaudio.Stream
|
35
|
|
-
|
36
|
|
- useStdio := false
|
37
|
|
-
|
38
|
|
- if !useStdio {
|
39
|
|
- // Set up audio with system default input and output
|
40
|
|
- portaudio.Initialize()
|
41
|
|
- defer portaudio.Terminate()
|
42
|
|
- var err error
|
43
|
|
- stream, err = portaudio.OpenDefaultStream(1, 1, float64(rttySettings.SampleRate), len(in), in, out)
|
44
|
|
- if err != nil {
|
45
|
|
- panic(err)
|
46
|
|
- }
|
47
|
|
- defer stream.Close()
|
48
|
|
- }
|
49
|
|
- modulatorToAudio := make(chan int16)
|
50
|
|
- audioToDemodulator := make(chan int16)
|
|
38
|
+ showHelp := flag.Bool("h", false, "Show usage and exit")
|
|
39
|
+ showVersion := flag.Bool("v", false, "Show version and exit")
|
51
|
40
|
|
52
|
|
- if !useStdio {
|
53
|
|
- // Set up modulator pipeline
|
54
|
|
- ttyToEncoder := make(chan rune, 100)
|
55
|
|
- encoderToModulator := make(chan byte)
|
56
|
|
- m.encoder = gortty.NewEncoder(ttyToEncoder, encoderToModulator, charset)
|
57
|
|
- m.modulator = gortty.NewModulator(rttySettings, encoderToModulator, modulatorToAudio)
|
58
|
|
- m.modulator.SetIdleDelay(1000)
|
59
|
|
-
|
60
|
|
- // Set up demodulator pipeline
|
61
|
|
- demodulatorToDecoder := make(chan byte)
|
62
|
|
- m.demodulator = gortty.NewDemodulator(rttySettings, audioToDemodulator, demodulatorToDecoder)
|
63
|
|
- decoderToTTY := make(chan rune)
|
64
|
|
- m.decoder = gortty.NewDecoder(demodulatorToDecoder, decoderToTTY, charset)
|
65
|
|
- }
|
66
|
|
-
|
67
|
|
- go m.cli()
|
|
41
|
+ flag.Parse()
|
68
|
42
|
|
69
|
|
- if !useStdio {
|
70
|
|
- // start audio
|
71
|
|
- stream.Start()
|
72
|
|
- defer stream.Stop()
|
73
|
|
- // Run audio output in goroutine
|
74
|
|
- go func() {
|
75
|
|
- l := 0
|
76
|
|
- for v := range modulatorToAudio {
|
77
|
|
- out[l] = v
|
78
|
|
- l++
|
79
|
|
- if l == 64 {
|
80
|
|
- l = 0
|
81
|
|
- stream.Write()
|
82
|
|
- }
|
83
|
|
- }
|
84
|
|
- }()
|
85
|
|
-
|
86
|
|
- // Run audio input
|
87
|
|
- for {
|
88
|
|
- stream.Read()
|
89
|
|
- for i := range in {
|
90
|
|
- audioToDemodulator <- in[i]
|
91
|
|
- }
|
92
|
|
- }
|
|
43
|
+ if *showHelp {
|
|
44
|
+ flag.PrintDefaults()
|
|
45
|
+ os.Exit(0)
|
93
|
46
|
}
|
94
|
|
-
|
95
|
|
-}
|
96
|
|
-
|
97
|
|
-//type ModemReader struct {
|
98
|
|
-// input chan rune
|
99
|
|
-//}
|
100
|
|
-//
|
101
|
|
-//func NewModemReader(c chan rune) *ModemReader {
|
102
|
|
-// m := &ModemReader{}
|
103
|
|
-// m.input = c
|
104
|
|
-// return m
|
105
|
|
-//}
|
106
|
|
-//
|
107
|
|
-//func (m *ModemReader) Read(p []byte) (n int, err error) {
|
108
|
|
-// r := <-m.input
|
109
|
|
-// l := utf8.EncodeRune(p, r)
|
110
|
|
-// return l, nil
|
111
|
|
-//}
|
112
|
47
|
|
113
|
|
-
|
114
|
|
-type modem struct {
|
115
|
|
- ttyReader io.Reader
|
116
|
|
- ttyWriter io.Writer
|
117
|
|
- modulator *gortty.Modulator
|
118
|
|
- demodulator *gortty.Demodulator
|
119
|
|
- encoder *gortty.Encoder
|
120
|
|
- decoder *gortty.Decoder
|
121
|
|
-}
|
122
|
|
-
|
123
|
|
-func (m *modem) modulatorCallback(event int, data interface{}) {
|
124
|
|
- switch event {
|
125
|
|
- case gortty.EventActive:
|
126
|
|
- fmt.Println("EventActive")
|
127
|
|
- m.demodulator.Inhibit(true)
|
128
|
|
- case gortty.EventIdle:
|
129
|
|
- fmt.Println("EventIdle")
|
130
|
|
- m.demodulator.Inhibit(false)
|
|
48
|
+ if *showVersion {
|
|
49
|
+ fmt.Printf("%d.%d.%d\n", verMajor, verMinor, verPatch)
|
|
50
|
+ os.Exit(0)
|
131
|
51
|
}
|
132
|
|
-}
|
133
|
52
|
|
134
|
|
-type configFunction func(action int, value string) string
|
|
53
|
+ log.Printf("starting up with config from %s")
|
135
|
54
|
|
136
|
|
-type configRegister struct {
|
137
|
|
- help string
|
138
|
|
- f configFunction
|
139
|
|
-}
|
140
|
|
-
|
141
|
|
-const (
|
142
|
|
- registerGet = iota
|
143
|
|
- registerSet
|
144
|
|
-)
|
145
|
|
-
|
146
|
|
-func (m *modem) cli() {
|
|
55
|
+ conf := &config{}
|
147
|
56
|
|
148
|
|
- fmt.Println("RTTY TCP Modem 1.0")
|
149
|
|
-
|
150
|
|
- autoAnswer := false
|
151
|
|
- escapeRune := '?'
|
152
|
|
- modulatorOn := true
|
153
|
|
- turnaroundDelay := 0
|
154
|
|
- usos := false
|
155
|
|
-
|
156
|
|
- settings := map[string]configRegister{
|
157
|
|
- "0": {
|
158
|
|
- help: "auto-answer enable",
|
159
|
|
- f: func(action int, value string) string {
|
160
|
|
- switch action {
|
161
|
|
- case registerGet:
|
162
|
|
- if autoAnswer {
|
163
|
|
- return "1"
|
164
|
|
- } else {
|
165
|
|
- return "0"
|
166
|
|
- }
|
167
|
|
- case registerSet:
|
168
|
|
- autoAnswer = (value == "1")
|
169
|
|
- return "OK"
|
170
|
|
- }
|
171
|
|
- return ""
|
172
|
|
- },
|
173
|
|
- },
|
174
|
|
- "2": {
|
175
|
|
- help: "escape character",
|
176
|
|
- f: func(action int, value string) string {
|
177
|
|
- switch action {
|
178
|
|
- case registerGet:
|
179
|
|
- return string(escapeRune)
|
180
|
|
- case registerSet:
|
181
|
|
- runes := []rune(value)
|
182
|
|
- escapeRune = runes[0]
|
183
|
|
- return "OK"
|
184
|
|
- }
|
185
|
|
- return ""
|
186
|
|
- },
|
187
|
|
- },
|
188
|
|
- "3": {
|
189
|
|
- help: "modulator enable",
|
190
|
|
- f: func(action int, value string) string {
|
191
|
|
- switch action {
|
192
|
|
- case registerGet:
|
193
|
|
- if modulatorOn {
|
194
|
|
- return "1"
|
195
|
|
- } else {
|
196
|
|
- return "0"
|
197
|
|
- }
|
198
|
|
- case registerSet:
|
199
|
|
- modulatorOn = (value == "1")
|
200
|
|
- if m.modulator != nil {
|
201
|
|
- if modulatorOn {
|
202
|
|
- m.modulator.SetAmplitude(16384)
|
203
|
|
- } else {
|
204
|
|
- m.modulator.SetAmplitude(0)
|
205
|
|
- }
|
206
|
|
- }
|
207
|
|
- return "OK"
|
208
|
|
- }
|
209
|
|
- return ""
|
210
|
|
- },
|
211
|
|
- },
|
212
|
|
- "4": {
|
213
|
|
- help: "turnaround delay",
|
214
|
|
- f: func(action int, value string) string {
|
215
|
|
- switch action {
|
216
|
|
- case registerGet:
|
217
|
|
- return fmt.Sprintln(turnaroundDelay)
|
218
|
|
- case registerSet:
|
219
|
|
- if d, err := strconv.Atoi(value); err == nil {
|
220
|
|
- turnaroundDelay = d
|
221
|
|
- return "OK"
|
222
|
|
- } else {
|
223
|
|
- return "ERROR"
|
224
|
|
- }
|
225
|
|
- }
|
226
|
|
- return ""
|
227
|
|
- },
|
228
|
|
- },
|
229
|
|
- "5": {
|
230
|
|
- help: "unshift on space",
|
231
|
|
- f: func(action int, value string) string {
|
232
|
|
- switch action {
|
233
|
|
- case registerGet:
|
234
|
|
- if usos {
|
235
|
|
- return "1"
|
236
|
|
- } else {
|
237
|
|
- return "0"
|
238
|
|
- }
|
239
|
|
- case registerSet:
|
240
|
|
- usos = (value == "1")
|
241
|
|
- m.decoder.UnshiftOnSpace(usos)
|
242
|
|
- return "OK"
|
243
|
|
- }
|
244
|
|
- return ""
|
245
|
|
- },
|
246
|
|
- },
|
247
|
|
- }
|
248
|
|
-
|
249
|
|
- localEcho := false
|
250
|
|
- ttyBufferedReader := bufio.NewReader(m.decoder)
|
251
|
|
-
|
252
|
|
- var netConn *NetConn
|
253
|
|
-
|
254
|
|
- if m.modulator != nil {
|
255
|
|
- m.modulator.SetCallback(m.modulatorCallback)
|
|
57
|
+ if _, err := toml.DecodeFile(configFile, &conf); err != nil {
|
|
58
|
+ log.Fatal(err)
|
256
|
59
|
}
|
257
|
|
-
|
258
|
|
-
|
259
|
|
- for {
|
260
|
|
-
|
261
|
|
- s, _ := ttyBufferedReader.ReadString('\n')
|
262
|
|
-
|
263
|
|
- //fmt.Println(s)
|
264
|
|
- s = strings.ToUpper(s)
|
265
|
|
- regex, _ := regexp.Compile(`^AT([SHOED?])([0-9])?(\?)?(?:[=|\:]([\S]*))?(?: (.*))?`)
|
266
|
|
-
|
267
|
|
- params := regex.FindStringSubmatch(s)
|
268
|
|
-
|
269
|
|
- if params == nil {
|
270
|
|
- continue
|
271
|
|
- }
|
272
|
|
-
|
273
|
|
- command := params[1]
|
274
|
|
-
|
275
|
|
- var register string
|
276
|
|
- if len(params) > 2 {
|
277
|
|
- register = params[2]
|
278
|
|
- }
|
279
|
60
|
|
280
|
|
- var registerQuery string
|
281
|
|
- if len(params) > 3 {
|
282
|
|
- registerQuery = params[3]
|
283
|
|
- }
|
284
|
|
-
|
285
|
|
- var value string
|
286
|
|
- if len(params) > 4 {
|
287
|
|
- value = params[4]
|
288
|
|
- }
|
289
|
|
-
|
290
|
|
- var parameter string
|
291
|
|
- if len(params) > 5 {
|
292
|
|
- parameter = params[5]
|
293
|
|
- }
|
294
|
|
-
|
295
|
|
- //fmt.Fprintf(m.encoder, "command: '%s' register: '%s' query: '%s' value: '%s' parameter: '%s'\n", command, register, registerQuery, value, parameter)
|
296
|
|
- switch command {
|
297
|
|
- case "?":
|
298
|
|
- fmt.Fprintf(m.encoder, "Setting Registers\n")
|
299
|
|
- for s, conf := range settings {
|
300
|
|
- fmt.Fprintf(m.encoder, "%s: %s (current value: %s)\n", s, conf.help, conf.f(registerGet, ""))
|
301
|
|
- }
|
302
|
|
- case "O": // online
|
303
|
|
- if !netConn.Connected() {
|
304
|
|
- fmt.Fprintln(m.encoder, "ERROR No active session")
|
305
|
|
- break
|
306
|
|
- }
|
307
|
|
- fmt.Fprintln(m.encoder, "OK")
|
308
|
|
- dataMode(netConn, ttyBufferedReader, m.encoder, escapeRune)
|
309
|
|
- case "D": // dial
|
310
|
|
- if netConn.Connected() {
|
311
|
|
- fmt.Fprintln(m.encoder, "ERROR Session in progress")
|
312
|
|
- break
|
313
|
|
- }
|
314
|
|
- addr := strings.TrimSpace(parameter)
|
315
|
|
-
|
316
|
|
- var err error
|
317
|
|
- netConn, err = NewNetConn(addr)
|
318
|
|
- if err != nil {
|
319
|
|
- fmt.Fprintln(m.encoder, "ERROR", err)
|
320
|
|
- break
|
321
|
|
- } else {
|
322
|
|
- fmt.Fprintln(m.encoder, "CONNECTED")
|
323
|
|
- dataMode(netConn, ttyBufferedReader, m.encoder, escapeRune)
|
324
|
|
- }
|
325
|
|
- case "H": // hangup
|
326
|
|
- if !netConn.Connected() {
|
327
|
|
- fmt.Fprintln(m.encoder, "ERROR No active session")
|
328
|
|
- break
|
329
|
|
- }
|
330
|
|
- netConn.Hangup()
|
331
|
|
- fmt.Fprintln(m.encoder, "OK")
|
332
|
|
- case "E": // local echo
|
333
|
|
- localEcho = (register == "1")
|
334
|
|
- fmt.Fprintln(m.encoder, "local echo", localEcho)
|
335
|
|
- case "S": // local echo
|
336
|
|
- if conf, ok := settings[register]; ok {
|
337
|
|
- if registerQuery == "?" {
|
338
|
|
- fmt.Fprintln(m.encoder, conf.f(registerGet, ""))
|
339
|
|
- } else if value != "" {
|
340
|
|
- fmt.Fprintln(m.encoder, conf.f(registerSet, value))
|
341
|
|
- } else {
|
342
|
|
- fmt.Fprintln(m.encoder, "ERROR")
|
343
|
|
- }
|
344
|
|
- }
|
345
|
|
- }
|
|
61
|
+ s := &gortty.ModemSettings{
|
|
62
|
+ Baud: conf.Baud,
|
|
63
|
+ StopBits: conf.StopBits,
|
|
64
|
+ DataBits: conf.DataBits,
|
|
65
|
+ SampleRate: conf.SampleRate,
|
|
66
|
+ OneFreq: conf.MarkFrequency,
|
|
67
|
+ ZeroFreq: conf.SpaceFrequency,
|
346
|
68
|
}
|
347
|
69
|
|
348
|
|
-}
|
349
|
|
-
|
|
70
|
+ if conf.CodeSet == "" {
|
|
71
|
+ conf.CodeSet = "USTTY"
|
|
72
|
+ }
|
350
|
73
|
|
|
74
|
+ c, err := gortty.LoadCodeSet(conf.CodeSet)
|
|
75
|
+ if err != nil {
|
|
76
|
+ log.Fatal(err)
|
|
77
|
+ }
|
351
|
78
|
|
352
|
|
-func dataMode(netConn *NetConn, ttyBufferedReader *bufio.Reader, ttyWriter io.Writer, escapeRune rune) {
|
353
|
|
-
|
354
|
|
- ttyCancelReader := make(chan struct{})
|
355
|
|
- ttyReadChan := make(chan rune)
|
356
|
|
- ttyReadPending := make(chan struct{})
|
357
|
|
-
|
358
|
|
- var t *time.Timer
|
359
|
|
- escapeRunes := 0
|
|
79
|
+ ctx, cancel := context.WithCancel(context.Background())
|
360
|
80
|
|
|
81
|
+ m, err := NewModem(ctx, s, c)
|
|
82
|
+ if err != nil {
|
|
83
|
+ log.Fatal(err)
|
|
84
|
+ }
|
361
|
85
|
|
362
|
|
- go func() {
|
363
|
|
- for {
|
364
|
|
- r, l, err := ttyBufferedReader.ReadRune()
|
365
|
|
- if err != nil && err != io.EOF {
|
366
|
|
- return
|
367
|
|
- }
|
368
|
|
- if l > 0 {
|
369
|
|
- select {
|
370
|
|
- case <-ttyCancelReader:
|
371
|
|
- ttyBufferedReader.UnreadRune() // oops this wasn't ours. let's put it back.
|
372
|
|
- close(ttyReadPending)
|
373
|
|
- return
|
374
|
|
- default:
|
375
|
|
- ttyReadChan <- r
|
376
|
|
- }
|
377
|
|
- }
|
378
|
|
- }
|
379
|
|
- }()
|
380
|
|
-
|
381
|
|
- netReadChan := netConn.OutputChannel()
|
382
|
|
- netConn.Resume()
|
383
|
|
-pipeLoop:
|
384
|
|
- for {
|
385
|
|
- var r rune
|
386
|
|
- if t == nil {
|
387
|
|
- select {
|
388
|
|
- case r = <-ttyReadChan:
|
389
|
|
- case q := <-netReadChan:
|
390
|
|
- ttyWriter.Write([]byte(string(q)))
|
391
|
|
- continue pipeLoop
|
392
|
|
- }
|
393
|
|
- } else {
|
394
|
|
- select {
|
395
|
|
- case r = <-ttyReadChan:
|
396
|
|
- case q := <-netReadChan:
|
397
|
|
- ttyWriter.Write([]byte(string(q)))
|
398
|
|
- continue pipeLoop
|
399
|
|
- case <-t.C:
|
400
|
|
- fmt.Fprintln(ttyWriter, "\nOK")
|
401
|
|
- close(ttyCancelReader)
|
402
|
|
- netConn.Pause()
|
403
|
|
- break pipeLoop
|
|
86
|
+ m.SetLocalEcho(conf.LocalEcho)
|
404
|
87
|
|
405
|
|
- }
|
406
|
|
- }
|
407
|
|
- if r == escapeRune && escapeRunes < 3 {
|
408
|
|
- escapeRunes++
|
409
|
|
- } else {
|
410
|
|
- if t != nil {
|
411
|
|
- t.Stop()
|
412
|
|
- }
|
413
|
|
- escapeRunes = 0
|
414
|
|
- }
|
415
|
|
- if escapeRunes == 3 {
|
416
|
|
- t = time.NewTimer(1*time.Second)
|
417
|
|
- }
|
418
|
|
- err := netConn.Write(r)
|
|
88
|
+ for i, r := range conf.Registers {
|
|
89
|
+ n, err := strconv.Atoi(i)
|
419
|
90
|
if err != nil {
|
420
|
|
- fmt.Fprintln(ttyWriter, "NO CARRIER")
|
421
|
|
- close(ttyCancelReader)
|
422
|
|
- break pipeLoop
|
|
91
|
+ continue
|
423
|
92
|
}
|
|
93
|
+ m.SetRegister(n, r)
|
424
|
94
|
}
|
425
|
|
- <- ttyReadPending
|
426
|
|
-
|
427
|
|
-}
|
428
|
|
-
|
429
|
|
-
|
430
|
|
-
|
431
|
|
-
|
432
|
|
-
|
433
|
|
-
|
434
|
|
-
|
435
|
|
-
|
436
|
|
-
|
437
|
|
-
|
438
|
|
-
|
439
|
|
-
|
440
|
|
-
|
441
|
|
-
|
442
|
95
|
|
|
96
|
+ defer m.Close()
|
443
|
97
|
|
|
98
|
+ m.run()
|
444
|
99
|
|
|
100
|
+ cancel()
|
|
101
|
+}
|