-
Notifications
You must be signed in to change notification settings - Fork 20
/
kenwood.go
186 lines (165 loc) · 4.88 KB
/
kenwood.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
// Copyright 2015 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 ax25
import (
"fmt"
"log"
"net"
"os"
"strings"
"syscall"
"time"
"github.com/albenik/go-serial/v2"
"github.com/la5nta/wl2k-go/fbb"
)
// KenwoodConn implements net.Conn using a
// Kenwood (or similar) TNC in connected transparent mode.
//
// Tested with Kenwood TH-D72 and TM-D710 in "packet-mode".
//
// TODO: github.com/term/goserial does not support setting the
// line flow control. Thus, KenwoodConn is not suitable for
// sending messages > the TNC's internal buffer size.
//
// We should probably be using software flow control (XFLOW),
// as hardware flow is not supported by many USB->RS232 adapters
// including the adapter build into TH-D72 (at least, not using the
// current linux kernel module.
type KenwoodConn struct{ Conn }
// Dial a packet node using a Kenwood (or similar) radio over serial
func DialKenwood(dev, mycall, targetcall string, config Config, logger *log.Logger) (*KenwoodConn, error) {
if logger == nil {
logger = log.New(os.Stderr, "", log.LstdFlags)
}
localAddr, remoteAddr := tncAddrFromString(mycall), tncAddrFromString(targetcall)
conn := &KenwoodConn{Conn{
localAddr: AX25Addr{localAddr},
remoteAddr: AX25Addr{remoteAddr},
}}
if dev == "socket" {
c, err := net.Dial("tcp", "127.0.0.1:8081")
if err != nil {
panic(err)
}
conn.Conn.ReadWriteCloser = c
} else {
s, err := serial.Open(dev, serial.WithBaudrate(config.SerialBaud))
if err != nil {
return conn, err
} else {
conn.Conn.ReadWriteCloser = s
}
}
// Initialize the TNC (with timeout)
initErr := make(chan error, 1)
go func() {
defer close(initErr)
conn.Write([]byte{3, 3, 3}) // ETX
fmt.Fprint(conn, "\r\nrestart\r\n")
// Wait for prompt, then send all the init commands
for {
line, err := fbb.ReadLine(conn)
if err != nil {
conn.Close()
initErr <- err
return
}
if strings.HasPrefix(line, "cmd:") {
fmt.Fprint(conn, "ECHO OFF\r") // Don't echo commands
fmt.Fprint(conn, "FLOW OFF\r")
fmt.Fprint(conn, "XFLOW ON\r") // Enable software flow control
fmt.Fprint(conn, "LFIGNORE ON\r") // Ignore linefeed (\n)
fmt.Fprint(conn, "AUTOLF OFF\r") // Don't auto-insert linefeed
fmt.Fprint(conn, "CR ON\r")
fmt.Fprint(conn, "8BITCONV ON\r") // Use 8-bit characters
// Return to command mode if station of current I/O stream disconnects.
fmt.Fprint(conn, "NEWMODE ON\r")
time.Sleep(500 * time.Millisecond)
fmt.Fprintf(conn, "MYCALL %s\r", mycall)
fmt.Fprintf(conn, "HBAUD %d\r", config.HBaud)
fmt.Fprintf(conn, "PACLEN %d\r", config.PacketLength)
fmt.Fprintf(conn, "TXDELAY %d\r", config.TXDelay/_CONFIG_TXDELAY_UNIT)
fmt.Fprintf(conn, "PERSIST %d\r", config.Persist)
time.Sleep(500 * time.Millisecond)
fmt.Fprintf(conn, "SLOTTIME %d\r", config.SlotTime/_CONFIG_SLOT_TIME_UNIT)
fmt.Fprint(conn, "FULLDUP OFF\r")
fmt.Fprintf(conn, "MAXFRAME %d\r", config.MaxFrame)
fmt.Fprintf(conn, "FRACK %d\r", config.FRACK/_CONFIG_FRACK_UNIT)
fmt.Fprintf(conn, "RESPTIME %d\r", config.ResponseTime/_CONFIG_RESPONSE_TIME_UNIT)
fmt.Fprintf(conn, "NOMODE ON\r")
break
}
}
}()
select {
case <-time.After(3 * time.Second):
conn.Close()
return nil, fmt.Errorf("initialization failed: deadline exceeded")
case err := <-initErr:
if err != nil {
conn.Close()
return nil, fmt.Errorf("initialization failed: %w", err)
}
}
time.Sleep(2 * time.Second)
// Dial the connection (with timeout)
dialErr := make(chan error, 1)
go func() {
defer close(dialErr)
fmt.Fprintf(conn, "\rc %s\r", targetcall)
// Wait for connect acknowledgement
for {
line, err := fbb.ReadLine(conn)
if err != nil {
dialErr <- err
return
}
logger.Println(line)
line = strings.TrimSpace(line)
switch {
case strings.Contains(line, "*** DISCONNECTED"):
dialErr <- fmt.Errorf("got disconnect %d", int(line[len(line)-1]))
return
case strings.Contains(line, "*** CONNECTED to"):
return
}
}
}()
select {
case <-time.After(5 * time.Minute):
conn.Close()
return nil, fmt.Errorf("connect failed: deadline exceeded")
case err := <-initErr:
if err != nil {
conn.Close()
return nil, fmt.Errorf("connect failed: %w", err)
}
}
// Success! Switch to TRANSPARENT mode and return the connection
fmt.Fprint(conn, "TRANS\r\n")
return conn, nil
}
func (c *KenwoodConn) Close() error {
if !c.ok() {
return syscall.EINVAL
}
// Exit TRANS mode
time.Sleep(1 * time.Second)
for i := 0; i < 3; i++ {
c.Write([]byte{3}) // ETX
time.Sleep(200 * time.Millisecond)
}
// Wait for prompt
time.Sleep(1 * time.Second)
// Disconnect
fmt.Fprint(c, "\r\nD\r\n")
for {
line, _ := fbb.ReadLine(c)
if strings.Contains(line, `DISCONN`) {
log.Println(`Disconnected`)
break
}
}
return c.Conn.Close()
}