forked from la5nta/pat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.go
296 lines (244 loc) · 6.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
// 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 (
"fmt"
"log"
"strconv"
"strings"
"time"
"github.com/la5nta/wl2k-go/transport"
"github.com/la5nta/wl2k-go/transport/ardop"
"github.com/la5nta/wl2k-go/transport/winmor"
// Register ax25 and telnet dialers
_ "github.com/la5nta/wl2k-go/transport/ax25"
_ "github.com/la5nta/wl2k-go/transport/telnet"
)
var (
wmTNC *winmor.TNC // Pointer to the WINMOR TNC used by Listen and Connect
adTNC *ardop.TNC // Pointer to the ARDOP TNC used by Listen and Connect
)
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)
}
url, err := transport.ParseURL(connectStr)
if err != nil {
log.Println(err)
return false
}
// Init TNCs
switch url.Scheme {
case "ardop":
if err := initArdopTNC(); err != nil {
log.Println(err)
return
}
case "winmor":
if err := initWinmorTNC(); 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 "ax25":
url.Host = config.AX25.Port
case "serial-tnc":
url.Host = config.SerialTNC.Path
if config.SerialTNC.Baudrate > 0 {
url.Params.Set("hbaud", fmt.Sprint(config.SerialTNC.Baudrate))
}
}
}
// 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 "ax25", "serial-tnc":
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 "ardop":
waitBusy(adTNC)
case "winmor":
waitBusy(wmTNC)
}
// Catch interrupts (signals) while dialing, so users can abort ardop/winmor connects.
doneHandleInterrupt := handleInterrupt()
log.Printf("Connecting to %s (%s)...", url.Target, url.Scheme)
conn, err := transport.DialURL(url)
close(doneHandleInterrupt)
eventLog.LogConn("connect "+connectStr, currFreq, conn, err)
if 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() {}
var rigName string
switch method {
case MethodWinmor:
rigName = config.Winmor.Rig
case MethodArdop:
rigName = config.Ardop.Rig
case MethodAX25:
rigName = config.AX25.Rig
default:
return noop, fmt.Errorf("Not supported with transport '%s'", method)
}
if rigName == "" {
return noop, fmt.Errorf("Missing rig reference in config section for %s, don't know which rig to qsy", method)
}
var ok bool
rig, ok := rigs[rigName]
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 initWinmorTNC() error {
if wmTNC != nil && wmTNC.Ping() == nil {
return nil
}
if wmTNC != nil {
wmTNC.Close()
}
var err error
wmTNC, err = winmor.Open(config.Winmor.Addr, fOptions.MyCall, config.Locator)
if err != nil {
return fmt.Errorf("WINMOR TNC initialization failed: %s", err)
}
if config.Winmor.DriveLevel != 0 {
if err := wmTNC.SetDriveLevel(config.Winmor.DriveLevel); err != nil {
log.Println("Failed to set WINMOR drive level:", err)
}
}
if v, err := wmTNC.Version(); err != nil {
return fmt.Errorf("WINMOR TNC initialization failed: %s", err)
} else {
log.Printf("WINMOR TNC v%s initialized", v)
}
transport.RegisterDialer("winmor", wmTNC)
if !config.Winmor.PTTControl {
return nil
}
rig, ok := rigs[config.Winmor.Rig]
if !ok {
return fmt.Errorf("Unable to set PTT rig '%s': Not defined or not loaded.", config.Winmor.Rig)
}
wmTNC.SetPTT(rig)
return nil
}
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: %s", 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: %s", err)
}
}
if err := adTNC.SetCWID(config.Ardop.CWID); err != nil {
return fmt.Errorf("Unable to configure CWID for ardop TNC: %s", 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("ardop", 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
}