forked from la5nta/pat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.go
321 lines (273 loc) · 7.32 KB
/
connect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2016 Martin Hebnes Pedersen (LA5NTA). All rights reserved.
// Use of this source code is governed by the MIT-license that can be
// found in the LICENSE file.
package main
import (
"context"
"errors"
"fmt"
"github.com/la5nta/pat/cfg"
"github.com/la5nta/pat/internal/debug"
"log"
"strconv"
"strings"
"time"
"github.com/harenber/ptc-go/v2/pactor"
"github.com/la5nta/wl2k-go/transport"
"github.com/la5nta/wl2k-go/transport/ardop"
"github.com/n8jja/Pat-Vara/vara"
// Register other dialers
_ "github.com/la5nta/wl2k-go/transport/ax25"
_ "github.com/la5nta/wl2k-go/transport/telnet"
)
var (
dialing *transport.URL // The connect URL currently being dialed (if any)
adTNC *ardop.TNC // Pointer to the ARDOP TNC used by Listen and Connect
pModem *pactor.Modem
varaHFModem *vara.Modem
varaFMModem *vara.Modem
// Context cancellation function for aborting while dialing.
dialCancelFunc func() = func() {}
)
func hasSSID(str string) bool { return strings.Contains(str, "-") }
func connectAny(connectStr ...string) bool {
for _, str := range connectStr {
if Connect(str) {
return true
}
}
return false
}
func Connect(connectStr string) (success bool) {
if connectStr == "" {
return false
} else if aliased, ok := config.ConnectAliases[connectStr]; ok {
return Connect(aliased)
}
// Hack around bug in frontend which may occur if the status updates too quickly.
defer func() { time.Sleep(time.Second); websocketHub.UpdateStatus() }()
debug.Printf("connectStr: %s", connectStr)
url, err := transport.ParseURL(connectStr)
if err != nil {
log.Println(err)
return false
}
// Init TNCs
switch url.Scheme {
case MethodArdop:
if err := initArdopTNC(); err != nil {
log.Println(err)
return
}
case MethodPactor:
ptCmdInit := ""
if val, ok := url.Params["init"]; ok {
ptCmdInit = strings.Join(val, "\n")
}
if err := initPactorModem(ptCmdInit); err != nil {
log.Println(err)
return
}
case MethodVaraHF:
if err := initVaraModem(varaHFModem, MethodVaraHF, config.VaraHF); err != nil {
log.Println(err)
return
}
case MethodVaraFM:
if err := initVaraModem(varaFMModem, MethodVaraFM, config.VaraFM); err != nil {
log.Println(err)
return
}
}
// Set default userinfo (mycall)
if url.User == nil {
url.SetUser(fOptions.MyCall)
}
// Set default host interface address
if url.Host == "" {
switch url.Scheme {
case MethodAX25:
url.Host = config.AX25.Port
case MethodSerialTNC:
url.Host = config.SerialTNC.Path
if hbaud := config.SerialTNC.HBaud; hbaud > 0 {
url.Params.Set("hbaud", fmt.Sprint(hbaud))
}
if sbaud := config.SerialTNC.SerialBaud; sbaud > 0 {
url.Params.Set("serial_baud", fmt.Sprint(sbaud))
}
}
}
// Radio Only?
radioOnly := fOptions.RadioOnly
if v := url.Params.Get("radio_only"); v != "" {
radioOnly, _ = strconv.ParseBool(v)
}
if radioOnly {
if hasSSID(fOptions.MyCall) {
log.Println("Radio Only does not support callsign with SSID")
return
}
switch url.Scheme {
case MethodAX25, MethodSerialTNC:
log.Printf("Radio-Only is not available for %s", url.Scheme)
return
default:
url.SetUser(url.User.Username() + "-T")
}
}
// QSY
var revertFreq func()
if freq := url.Params.Get("freq"); freq != "" {
revertFreq, err = qsy(url.Scheme, freq)
if err != nil {
log.Printf("Unable to QSY: %s", err)
return
}
defer revertFreq()
}
var currFreq Frequency
if vfo, _, ok, _ := VFOForTransport(url.Scheme); ok {
f, _ := vfo.GetFreq()
currFreq = Frequency(f)
}
// Wait for a clear channel
switch url.Scheme {
case MethodArdop:
waitBusy(adTNC)
}
ctx, cancel := context.WithCancel(context.Background())
dialCancelFunc = func() { dialing = nil; cancel() }
defer dialCancelFunc()
// Signal web gui that we are dialing a connection
dialing = url
websocketHub.UpdateStatus()
log.Printf("Connecting to %s (%s)...", url.Target, url.Scheme)
conn, err := transport.DialURLContext(ctx, url)
// Signal web gui that we are no longer dialing
dialing = nil
websocketHub.UpdateStatus()
eventLog.LogConn("connect "+connectStr, currFreq, conn, err)
switch {
case errors.Is(err, context.Canceled):
log.Printf("Connect cancelled")
return
case err != nil:
log.Printf("Unable to establish connection to remote: %s", err)
return
}
err = exchange(conn, url.Target, false)
if err != nil {
log.Printf("Exchange failed: %s", err)
} else {
log.Println("Disconnected.")
success = true
}
return
}
func qsy(method, addr string) (revert func(), err error) {
noop := func() {}
rig, rigName, ok, err := VFOForTransport(method)
if err != nil {
return noop, err
} else if !ok {
return noop, fmt.Errorf("hamlib rig '%s' not loaded", rigName)
}
log.Printf("QSY %s: %s", method, addr)
_, oldFreq, err := setFreq(rig, addr)
if err != nil {
return noop, err
}
time.Sleep(3 * time.Second)
return func() {
time.Sleep(time.Second)
log.Printf("QSX %s: %.3f", method, float64(oldFreq)/1e3)
rig.SetFreq(oldFreq)
}, nil
}
func waitBusy(b transport.BusyChannelChecker) {
printed := false
for b.Busy() {
if !printed && fOptions.IgnoreBusy {
log.Println("Ignoring busy channel!")
break
} else if !printed {
log.Println("Waiting for clear channel...")
printed = true
}
time.Sleep(300 * time.Millisecond)
}
}
func initArdopTNC() error {
if adTNC != nil && adTNC.Ping() == nil {
return nil
}
if adTNC != nil {
adTNC.Close()
}
var err error
adTNC, err = ardop.OpenTCP(config.Ardop.Addr, fOptions.MyCall, config.Locator)
if err != nil {
return fmt.Errorf("ARDOP TNC initialization failed: %w", err)
}
if !config.Ardop.ARQBandwidth.IsZero() {
if err := adTNC.SetARQBandwidth(config.Ardop.ARQBandwidth); err != nil {
return fmt.Errorf("unable to set ARQ bandwidth for ardop TNC: %w", err)
}
}
if err := adTNC.SetCWID(config.Ardop.CWID); err != nil {
return fmt.Errorf("unable to configure CWID for ardop TNC: %w", err)
}
if v, err := adTNC.Version(); err != nil {
return fmt.Errorf("ARDOP TNC initialization failed: %s", err)
} else {
log.Printf("ARDOP TNC (%s) initialized", v)
}
transport.RegisterDialer(MethodArdop, adTNC)
if !config.Ardop.PTTControl {
return nil
}
rig, ok := rigs[config.Ardop.Rig]
if !ok {
return fmt.Errorf("unable to set PTT rig '%s': Not defined or not loaded", config.Ardop.Rig)
}
adTNC.SetPTT(rig)
return nil
}
func initPactorModem(cmdlineinit string) error {
if pModem != nil {
pModem.Close()
}
var err error
pModem, err = pactor.OpenModem(config.Pactor.Path, config.Pactor.Baudrate, fOptions.MyCall, config.Pactor.InitScript, cmdlineinit)
if err != nil || pModem == nil {
return fmt.Errorf("pactor initialization failed: %w", err)
}
transport.RegisterDialer(MethodPactor, pModem)
return nil
}
func initVaraModem(vModem *vara.Modem, scheme string, conf cfg.VaraConfig) error {
if vModem != nil {
_ = vModem.Close()
}
vConf := vara.ModemConfig{
Host: conf.Host,
CmdPort: conf.CmdPort,
DataPort: conf.DataPort,
}
var err error
vModem, err = vara.NewModem(scheme, fOptions.MyCall, vConf)
if err != nil {
return fmt.Errorf("vara initialization failed: %w", err)
}
transport.RegisterDialer(scheme, vModem)
if !conf.PTTControl {
return nil
}
rig, ok := rigs[conf.Rig]
if !ok {
return fmt.Errorf("unable to set PTT rig '%s': not defined or not loaded", conf.Rig)
}
vModem.SetPTT(rig)
return nil
}