From 3e92a8add6d68dfa5c1b6496d0edc95d9f8bf095 Mon Sep 17 00:00:00 2001 From: Nonoo Date: Sun, 18 Oct 2020 10:33:47 +0200 Subject: [PATCH] Clarify type names --- audiostream.go | 25 +++++++ portcontrol.go => controlstream.go | 98 ++++++++++++++-------------- main.go | 10 +-- portaudio.go | 25 ------- portcommon.go => streamconnection.go | 14 ++-- 5 files changed, 86 insertions(+), 86 deletions(-) create mode 100644 audiostream.go rename portcontrol.go => controlstream.go (75%) delete mode 100644 portaudio.go rename portcommon.go => streamconnection.go (83%) diff --git a/audiostream.go b/audiostream.go new file mode 100644 index 0000000..788f06e --- /dev/null +++ b/audiostream.go @@ -0,0 +1,25 @@ +package main + +import ( + "encoding/binary" + + "github.com/nonoo/kappanhang/log" +) + +type audioStream struct { + stream streamConnection +} + +func (p *audioStream) Start() { + p.stream.open(50003) + + p.stream.sendPkt3() + + // Expecting a Pkt4 answer. + // Example answer from radio: 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x8c, 0x7d, 0x45, 0x7a, 0x1d, 0xf6, 0xe9, 0x0b + r := p.stream.expect(16, []byte{0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00}) + p.stream.remoteSID = binary.BigEndian.Uint32(r[8:12]) + p.stream.sendPkt6() + + log.Debugf("got remote session id %.8x", p.stream.remoteSID) +} diff --git a/portcontrol.go b/controlstream.go similarity index 75% rename from portcontrol.go rename to controlstream.go index a9f91eb..ea9421a 100644 --- a/portcontrol.go +++ b/controlstream.go @@ -10,8 +10,8 @@ import ( "github.com/nonoo/kappanhang/log" ) -type portControl struct { - port portCommon +type controlStream struct { + stream streamConnection authSendSeq uint16 authInnerSendSeq uint16 authID [6]byte @@ -20,7 +20,7 @@ type portControl struct { lastReauthAt time.Time } -func (p *portControl) sendPkt7(replyID []byte, seq uint16) { +func (p *controlStream) sendPkt7(replyID []byte, seq uint16) { // Example request from PC: 0x15, 0x00, 0x00, 0x00, 0x07, 0x00, 0x09, 0x00, 0xbe, 0xd9, 0xf2, 0x63, 0xe4, 0x35, 0xdd, 0x72, 0x00, 0x78, 0x40, 0xf6, 0x02 // Example reply from radio: 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x09, 0x00, 0xe4, 0x35, 0xdd, 0x72, 0xbe, 0xd9, 0xf2, 0x63, 0x01, 0x78, 0x40, 0xf6, 0x02 var replyFlag byte @@ -39,24 +39,24 @@ func (p *portControl) sendPkt7(replyID []byte, seq uint16) { replyFlag = 0x01 } - p.expectedPkt7ReplySeq = p.port.sendSeq + p.expectedPkt7ReplySeq = p.stream.sendSeq - p.port.send([]byte{0x15, 0x00, 0x00, 0x00, 0x07, 0x00, byte(seq), byte(seq >> 8), - byte(p.port.localSID >> 24), byte(p.port.localSID >> 16), byte(p.port.localSID >> 8), byte(p.port.localSID), - byte(p.port.remoteSID >> 24), byte(p.port.remoteSID >> 16), byte(p.port.remoteSID >> 8), byte(p.port.remoteSID), + p.stream.send([]byte{0x15, 0x00, 0x00, 0x00, 0x07, 0x00, byte(seq), byte(seq >> 8), + byte(p.stream.localSID >> 24), byte(p.stream.localSID >> 16), byte(p.stream.localSID >> 8), byte(p.stream.localSID), + byte(p.stream.remoteSID >> 24), byte(p.stream.remoteSID >> 16), byte(p.stream.remoteSID >> 8), byte(p.stream.remoteSID), replyFlag, replyID[0], replyID[1], replyID[2], replyID[3]}) } -func (p *portControl) sendPktLogin() { +func (p *controlStream) sendPktLogin() { // The reply to the login packet will contain a 6 bytes long auth ID with the first 2 bytes set to our randID. var randID [2]byte _, err := rand.Read(randID[:]) if err != nil { log.Fatal(err) } - p.port.send([]byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, - byte(p.port.localSID >> 24), byte(p.port.localSID >> 16), byte(p.port.localSID >> 8), byte(p.port.localSID), - byte(p.port.remoteSID >> 24), byte(p.port.remoteSID >> 16), byte(p.port.remoteSID >> 8), byte(p.port.remoteSID), + p.stream.send([]byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, + byte(p.stream.localSID >> 24), byte(p.stream.localSID >> 16), byte(p.stream.localSID >> 8), byte(p.stream.localSID), + byte(p.stream.remoteSID >> 24), byte(p.stream.remoteSID >> 16), byte(p.stream.remoteSID >> 8), byte(p.stream.remoteSID), 0x00, 0x00, 0x00, 0x70, 0x01, 0x00, 0x00, byte(p.authInnerSendSeq), byte(p.authInnerSendSeq >> 8), 0x00, randID[0], randID[1], 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -75,7 +75,7 @@ func (p *portControl) sendPktLogin() { p.authInnerSendSeq++ } -func (p *portControl) sendPktReauth(firstReauthSend bool) { +func (p *controlStream) sendPktReauth(firstReauthSend bool) { var magic byte if firstReauthSend { @@ -100,9 +100,9 @@ func (p *portControl) sendPktReauth(firstReauthSend bool) { // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - p.port.send([]byte{0x40, 0x00, 0x00, 0x00, 0x00, 0x00, byte(p.authSendSeq), byte(p.authSendSeq >> 8), - byte(p.port.localSID >> 24), byte(p.port.localSID >> 16), byte(p.port.localSID >> 8), byte(p.port.localSID), - byte(p.port.remoteSID >> 24), byte(p.port.remoteSID >> 16), byte(p.port.remoteSID >> 8), byte(p.port.remoteSID), + p.stream.send([]byte{0x40, 0x00, 0x00, 0x00, 0x00, 0x00, byte(p.authSendSeq), byte(p.authSendSeq >> 8), + byte(p.stream.localSID >> 24), byte(p.stream.localSID >> 16), byte(p.stream.localSID >> 8), byte(p.stream.localSID), + byte(p.stream.remoteSID >> 24), byte(p.stream.remoteSID >> 16), byte(p.stream.remoteSID >> 8), byte(p.stream.remoteSID), 0x00, 0x00, 0x00, 0x30, 0x01, magic, 0x00, byte(p.authInnerSendSeq), byte(p.authInnerSendSeq >> 8), 0x00, p.authID[0], p.authID[1], p.authID[2], p.authID[3], p.authID[4], p.authID[5], 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -114,26 +114,26 @@ func (p *portControl) sendPktReauth(firstReauthSend bool) { p.lastReauthAt = time.Now() } -func (p *portControl) SendDisconnect() { - p.port.send([]byte{0x40, 0x00, 0x00, 0x00, 0x00, 0x00, byte(p.port.sendSeq), byte(p.port.sendSeq >> 8), - byte(p.port.localSID >> 24), byte(p.port.localSID >> 16), byte(p.port.localSID >> 8), byte(p.port.localSID), - byte(p.port.remoteSID >> 24), byte(p.port.remoteSID >> 16), byte(p.port.remoteSID >> 8), byte(p.port.remoteSID), +func (p *controlStream) SendDisconnect() { + p.stream.send([]byte{0x40, 0x00, 0x00, 0x00, 0x00, 0x00, byte(p.stream.sendSeq), byte(p.stream.sendSeq >> 8), + byte(p.stream.localSID >> 24), byte(p.stream.localSID >> 16), byte(p.stream.localSID >> 8), byte(p.stream.localSID), + byte(p.stream.remoteSID >> 24), byte(p.stream.remoteSID >> 16), byte(p.stream.remoteSID >> 8), byte(p.stream.remoteSID), 0x00, 0x00, 0x00, 0x30, 0x01, 0x01, 0x00, byte(p.authInnerSendSeq), byte(p.authInnerSendSeq >> 8), 0x00, p.authID[0], p.authID[1], p.authID[2], p.authID[3], p.authID[4], p.authID[5], 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) - p.port.send([]byte{0x10, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, - byte(p.port.localSID >> 24), byte(p.port.localSID >> 16), byte(p.port.localSID >> 8), byte(p.port.localSID), - byte(p.port.remoteSID >> 24), byte(p.port.remoteSID >> 16), byte(p.port.remoteSID >> 8), byte(p.port.remoteSID)}) + p.stream.send([]byte{0x10, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, + byte(p.stream.localSID >> 24), byte(p.stream.localSID >> 16), byte(p.stream.localSID >> 8), byte(p.stream.localSID), + byte(p.stream.remoteSID >> 24), byte(p.stream.remoteSID >> 16), byte(p.stream.remoteSID >> 8), byte(p.stream.remoteSID)}) } -func (p *portControl) sendRequestSerialAndAudio() { +func (p *controlStream) sendRequestSerialAndAudio() { log.Print("requesting serial and audio stream") - p.port.send([]byte{0x90, 0x00, 0x00, 0x00, 0x00, 0x00, byte(p.authSendSeq), byte(p.authSendSeq >> 8), - byte(p.port.localSID >> 24), byte(p.port.localSID >> 16), byte(p.port.localSID >> 8), byte(p.port.localSID), - byte(p.port.remoteSID >> 24), byte(p.port.remoteSID >> 16), byte(p.port.remoteSID >> 8), byte(p.port.remoteSID), + p.stream.send([]byte{0x90, 0x00, 0x00, 0x00, 0x00, 0x00, byte(p.authSendSeq), byte(p.authSendSeq >> 8), + byte(p.stream.localSID >> 24), byte(p.stream.localSID >> 16), byte(p.stream.localSID >> 8), byte(p.stream.localSID), + byte(p.stream.remoteSID >> 24), byte(p.stream.remoteSID >> 16), byte(p.stream.remoteSID >> 8), byte(p.stream.remoteSID), 0x00, 0x00, 0x00, 0x80, 0x01, 0x03, 0x00, byte(p.authInnerSendSeq), byte(p.authInnerSendSeq >> 8), 0x00, p.authID[0], p.authID[1], p.authID[2], p.authID[3], p.authID[4], p.authID[5], 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, @@ -154,32 +154,32 @@ func (p *portControl) sendRequestSerialAndAudio() { p.authInnerSendSeq++ } -func (p *portControl) StartStream() { - p.port.open(50001) +func (p *controlStream) Start() { + p.stream.open(50001) - p.port.sendPkt3() - p.port.sendSeq = 1 - p.sendPkt7(nil, p.port.sendSeq) - p.port.sendSeq = 0 - p.port.sendPkt3() + p.stream.sendPkt3() + p.stream.sendSeq = 1 + p.sendPkt7(nil, p.stream.sendSeq) + p.stream.sendSeq = 0 + p.stream.sendPkt3() // Expecting a Pkt4 answer. // Example answer from radio: 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x8c, 0x7d, 0x45, 0x7a, 0x1d, 0xf6, 0xe9, 0x0b - r := p.port.expect(16, []byte{0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00}) - p.port.remoteSID = binary.BigEndian.Uint32(r[8:12]) + r := p.stream.expect(16, []byte{0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00}) + p.stream.remoteSID = binary.BigEndian.Uint32(r[8:12]) - log.Debugf("got remote session id %.8x", p.port.remoteSID) + log.Debugf("got remote session id %.8x", p.stream.remoteSID) p.authSendSeq = 1 p.authInnerSendSeq = 0x50 - p.port.sendPkt6() + p.stream.sendPkt6() // Expecting a Pkt6 answer. - r = p.port.expect(16, []byte{0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00}) - p.port.remoteSID = binary.BigEndian.Uint32(r[8:12]) // TODO + r = p.stream.expect(16, []byte{0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00}) + p.stream.remoteSID = binary.BigEndian.Uint32(r[8:12]) // TODO p.sendPktLogin() - p.port.sendSeq = 5 + p.stream.sendSeq = 5 // Example success auth packet: 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, // 0xe6, 0xb2, 0x7b, 0x7b, 0xbb, 0x41, 0x3f, 0x2b, @@ -193,7 +193,7 @@ func (p *portControl) StartStream() { // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - r = p.port.expect(96, []byte{0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00}) + r = p.stream.expect(96, []byte{0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00}) if bytes.Equal(r[48:52], []byte{0xff, 0xff, 0xff, 0xfe}) { log.Fatal("invalid user/password") } @@ -213,7 +213,7 @@ func (p *portControl) StartStream() { } for { - r, err := p.port.read() + r, err := p.stream.read() if err != nil { errCount++ if errCount > 5 { @@ -249,9 +249,9 @@ func (p *portControl) StartStream() { // Example request from radio: 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0xe4, 0x35, 0xdd, 0x72, 0xbe, 0xd9, 0xf2, 0x63 // Example answer from PC: 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0xbe, 0xd9, 0xf2, 0x63, 0xe4, 0x35, 0xdd, 0x72 gotSeq := binary.LittleEndian.Uint16(r[6:8]) - p.port.send([]byte{0x10, 0x00, 0x00, 0x00, 0x00, 0x00, byte(gotSeq), byte(gotSeq >> 8), - byte(p.port.localSID >> 24), byte(p.port.localSID >> 16), byte(p.port.localSID >> 8), byte(p.port.localSID), - byte(p.port.remoteSID >> 24), byte(p.port.remoteSID >> 16), byte(p.port.remoteSID >> 8), byte(p.port.remoteSID)}) + p.stream.send([]byte{0x10, 0x00, 0x00, 0x00, 0x00, 0x00, byte(gotSeq), byte(gotSeq >> 8), + byte(p.stream.localSID >> 24), byte(p.stream.localSID >> 16), byte(p.stream.localSID >> 8), byte(p.stream.localSID), + byte(p.stream.remoteSID >> 24), byte(p.stream.remoteSID >> 16), byte(p.stream.remoteSID >> 8), byte(p.stream.remoteSID)}) } if len(r) == 80 && bytes.Equal(r[:6], []byte{0x50, 0x00, 0x00, 0x00, 0x00, 0x00}) && bytes.Equal(r[48:51], []byte{0xff, 0xff, 0xff}) { // Example answer from radio: 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, @@ -290,13 +290,13 @@ func (p *portControl) StartStream() { // 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x03, 0x03, // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 log.Print("serial and audio request success") - go ports.audio.StartStream() + go streams.audio.Start() } if time.Since(lastPingAt) >= 100*time.Millisecond { - p.sendPkt7(nil, p.port.sendSeq) - p.port.sendPkt3() - p.port.sendSeq++ + p.sendPkt7(nil, p.stream.sendSeq) + p.stream.sendPkt3() + p.stream.sendSeq++ lastPingAt = time.Now() if time.Since(p.lastReauthAt) >= 60*time.Second { diff --git a/main.go b/main.go index 840709d..1d46deb 100644 --- a/main.go +++ b/main.go @@ -8,9 +8,9 @@ import ( "github.com/nonoo/kappanhang/log" ) -var ports struct { - control portControl - audio portAudio +var streams struct { + control controlStream + audio audioStream } func setupCloseHandler() { @@ -19,7 +19,7 @@ func setupCloseHandler() { go func() { <-c log.Print("disconnecting") - ports.control.SendDisconnect() + streams.control.SendDisconnect() os.Exit(0) }() } @@ -29,5 +29,5 @@ func main() { parseArgs() setupCloseHandler() - ports.control.StartStream() + streams.control.Start() } diff --git a/portaudio.go b/portaudio.go deleted file mode 100644 index 6039d9b..0000000 --- a/portaudio.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -import ( - "encoding/binary" - - "github.com/nonoo/kappanhang/log" -) - -type portAudio struct { - port portCommon -} - -func (p *portAudio) StartStream() { - p.port.open(50003) - - p.port.sendPkt3() - - // Expecting a Pkt4 answer. - // Example answer from radio: 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x8c, 0x7d, 0x45, 0x7a, 0x1d, 0xf6, 0xe9, 0x0b - r := p.port.expect(16, []byte{0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00}) - p.port.remoteSID = binary.BigEndian.Uint32(r[8:12]) - p.port.sendPkt6() - - log.Debugf("got remote session id %.8x", p.port.remoteSID) -} diff --git a/portcommon.go b/streamconnection.go similarity index 83% rename from portcommon.go rename to streamconnection.go index fe2b291..4d372da 100644 --- a/portcommon.go +++ b/streamconnection.go @@ -9,21 +9,21 @@ import ( "github.com/nonoo/kappanhang/log" ) -type portCommon struct { +type streamConnection struct { conn *net.UDPConn localSID uint32 remoteSID uint32 sendSeq uint16 } -func (p *portCommon) send(d []byte) { +func (p *streamConnection) send(d []byte) { _, err := p.conn.Write(d) if err != nil { log.Fatal(err) } } -func (p *portCommon) read() ([]byte, error) { +func (p *streamConnection) read() ([]byte, error) { err := p.conn.SetReadDeadline(time.Now().Add(time.Second)) if err != nil { log.Fatal(err) @@ -37,7 +37,7 @@ func (p *portCommon) read() ([]byte, error) { return b[:n], err } -func (p *portCommon) expect(packetLength int, b []byte) []byte { +func (p *streamConnection) expect(packetLength int, b []byte) []byte { var r []byte expectStart := time.Now() for { @@ -52,7 +52,7 @@ func (p *portCommon) expect(packetLength int, b []byte) []byte { return r } -func (p *portCommon) open(portNumber int) { +func (p *streamConnection) open(portNumber int) { hostPort := fmt.Sprint(connectAddress, ":", portNumber) log.Print("connecting to ", hostPort) raddr, err := net.ResolveUDPAddr("udp", hostPort) @@ -71,13 +71,13 @@ func (p *portCommon) open(portNumber int) { log.Debugf("using session id %.8x", p.localSID) } -func (p *portCommon) sendPkt3() { +func (p *streamConnection) sendPkt3() { p.send([]byte{0x10, 0x00, 0x00, 0x00, 0x03, 0x00, byte(p.sendSeq), byte(p.sendSeq >> 8), byte(p.localSID >> 24), byte(p.localSID >> 16), byte(p.localSID >> 8), byte(p.localSID), byte(p.remoteSID >> 24), byte(p.remoteSID >> 16), byte(p.remoteSID >> 8), byte(p.remoteSID)}) } -func (p *portCommon) sendPkt6() { +func (p *streamConnection) sendPkt6() { p.send([]byte{0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00, byte(p.localSID >> 24), byte(p.localSID >> 16), byte(p.localSID >> 8), byte(p.localSID), byte(p.remoteSID >> 24), byte(p.remoteSID >> 16), byte(p.remoteSID >> 8), byte(p.remoteSID)})