Make opening serial/audio stream consistent

This commit is contained in:
Nonoo 2020-10-18 12:50:09 +02:00
parent aad9b54e3d
commit 91b3eb7358
4 changed files with 101 additions and 75 deletions

View file

@ -2,6 +2,8 @@ package main
import (
"bytes"
"crypto/rand"
"encoding/binary"
"fmt"
"net"
"time"
@ -14,7 +16,11 @@ type streamCommon struct {
conn *net.UDPConn
localSID uint32
remoteSID uint32
sendSeq uint16
pkt7 struct {
sendSeq uint16
randIDByte [1]byte
}
}
func (s *streamCommon) send(d []byte) {
@ -64,7 +70,7 @@ func (s *streamCommon) expect(packetLength int, b []byte) []byte {
break
}
if time.Since(expectStart) > time.Second {
log.Fatal("expect timeout")
log.Fatal(s.name + "/expect timeout")
}
}
return r
@ -78,26 +84,64 @@ func (s *streamCommon) open(name string, portNumber int) {
if err != nil {
log.Fatal(err)
}
laddr := net.UDPAddr{
Port: portNumber,
}
s.conn, err = net.DialUDP("udp", &laddr, raddr)
s.conn, err = net.DialUDP("udp", nil, raddr)
if err != nil {
log.Fatal(err)
}
s.localSID = uint32(time.Now().Unix())
// Constructing the local session ID by combining the local IP address and port.
laddr := s.conn.LocalAddr().(*net.UDPAddr)
s.localSID = binary.BigEndian.Uint32(laddr.IP[len(laddr.IP)-4:])<<16 | uint32(laddr.Port&0xffff)
log.Debugf(s.name+"/using session id %.8x", s.localSID)
_, err = rand.Read(s.pkt7.randIDByte[:])
if err != nil {
log.Fatal(err)
}
}
func (p *streamCommon) 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 (s *streamCommon) sendPkt3() {
s.send([]byte{0x10, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
byte(s.localSID >> 24), byte(s.localSID >> 16), byte(s.localSID >> 8), byte(s.localSID),
byte(s.remoteSID >> 24), byte(s.remoteSID >> 16), byte(s.remoteSID >> 8), byte(s.remoteSID)})
}
func (p *streamCommon) 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)})
func (s *streamCommon) sendPkt6() {
s.send([]byte{0x10, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00,
byte(s.localSID >> 24), byte(s.localSID >> 16), byte(s.localSID >> 8), byte(s.localSID),
byte(s.remoteSID >> 24), byte(s.remoteSID >> 16), byte(s.remoteSID >> 8), byte(s.remoteSID)})
}
func (s *streamCommon) sendPkt7Do(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
if replyID == nil {
replyID = make([]byte, 4)
var randID [2]byte
_, err := rand.Read(randID[:])
if err != nil {
log.Fatal(err)
}
replyID[0] = randID[0]
replyID[1] = randID[1]
replyID[2] = s.pkt7.randIDByte[0]
replyID[3] = 0x03
} else {
replyFlag = 0x01
}
s.send([]byte{0x15, 0x00, 0x00, 0x00, 0x07, 0x00, byte(seq), byte(seq >> 8),
byte(s.localSID >> 24), byte(s.localSID >> 16), byte(s.localSID >> 8), byte(s.localSID),
byte(s.remoteSID >> 24), byte(s.remoteSID >> 16), byte(s.remoteSID >> 8), byte(s.remoteSID),
replyFlag, replyID[0], replyID[1], replyID[2], replyID[3]})
}
func (s *streamCommon) sendPkt7() {
s.sendPkt7Do(nil, s.pkt7.sendSeq)
s.pkt7.sendSeq++
}
func (s *streamCommon) sendPkt7Reply(replyID []byte, seq uint16) {
s.sendPkt7Do(replyID, seq)
}