Move pkt7 handling to a separate file

This commit is contained in:
Nonoo 2020-10-18 14:21:58 +02:00
parent 4612aaeba3
commit dec1be0c41
4 changed files with 129 additions and 109 deletions

View file

@ -10,8 +10,6 @@ import (
"github.com/nonoo/kappanhang/log"
)
const pkt7TimeoutDuration = 3 * time.Second
type controlStream struct {
common streamCommon
authSendSeq uint16
@ -20,10 +18,6 @@ type controlStream struct {
serialAndAudioStreamOpened bool
requestSerialAndAudioTimeout *time.Timer
pkt7TimeoutTimer *time.Timer
pkt7Latency time.Duration
lastPkt7SendAt time.Time
}
func (s *controlStream) sendPktAuth() {
@ -147,34 +141,6 @@ func (s *controlStream) sendRequestSerialAndAudio() {
func (s *controlStream) handleRead(r []byte) {
switch len(r) {
case 21:
if bytes.Equal(r[1:6], []byte{0x00, 0x00, 0x00, 0x07, 0x00}) { // Note that the first byte can be 0x15 or 0x00, so we ignore that.
gotSeq := binary.LittleEndian.Uint16(r[6:8])
if r[16] == 0x00 { // This is a pkt7 request from the radio.
// Replying to the radio.
// Example request from radio: 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x1c, 0x0e, 0xe4, 0x35, 0xdd, 0x72, 0xbe, 0xd9, 0xf2, 0x63, 0x00, 0x57, 0x2b, 0x12, 0x00
// Example answer from PC: 0x15, 0x00, 0x00, 0x00, 0x07, 0x00, 0x1c, 0x0e, 0xbe, 0xd9, 0xf2, 0x63, 0xe4, 0x35, 0xdd, 0x72, 0x01, 0x57, 0x2b, 0x12, 0x00
s.common.sendPkt7Reply(r[17:21], gotSeq)
} else { // This is a pkt7 reply to our request.
s.pkt7TimeoutTimer.Stop()
s.pkt7TimeoutTimer.Reset(pkt7TimeoutDuration)
s.pkt7Latency += time.Since(s.lastPkt7SendAt)
s.pkt7Latency /= 2
expectedSeq := s.common.pkt7.lastConfirmedSeq + 1
if expectedSeq != gotSeq {
var missingPkts int
if gotSeq > expectedSeq {
missingPkts = int(gotSeq) - int(expectedSeq)
} else {
missingPkts = int(gotSeq) + 65536 - int(expectedSeq)
}
log.Error("lost ", missingPkts, " packets ")
}
s.common.pkt7.lastConfirmedSeq = gotSeq
}
}
case 16:
if bytes.Equal(r[:6], []byte{0x10, 0x00, 0x00, 0x00, 0x00, 0x00}) {
// Replying to the radio.
@ -237,7 +203,7 @@ func (s *controlStream) start() {
s.common.sendPkt3()
s.common.pkt7.sendSeq = 1
s.common.sendPkt7()
s.common.pkt7.send(&s.common)
s.common.sendPkt3()
s.common.waitForPkt4Answer()
s.common.sendPkt6()
@ -246,8 +212,6 @@ func (s *controlStream) start() {
s.authSendSeq = 1
s.authInnerSendSeq = 0x1234
s.sendPktAuth()
s.common.pkt7.sendSeq = 5
s.common.pkt7.lastConfirmedSeq = s.common.pkt7.sendSeq - 1
log.Debug("expecting auth answer")
// Example success auth packet: 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
@ -273,8 +237,10 @@ func (s *controlStream) start() {
s.sendPkt0()
s.sendRequestSerialAndAudio()
pkt7SendTicker := time.NewTicker(100 * time.Millisecond)
s.pkt7TimeoutTimer = time.NewTimer(pkt7TimeoutDuration)
s.common.pkt7.sendSeq = 5
s.common.pkt7.startPeriodicSend(&s.common)
pkt0SendTicker := time.NewTicker(100 * time.Millisecond)
reauthTicker := time.NewTicker(60 * time.Second)
statusLogTicker := time.NewTicker(3 * time.Second)
@ -282,16 +248,12 @@ func (s *controlStream) start() {
select {
case r = <-s.common.readChan:
s.handleRead(r)
case <-pkt7SendTicker.C:
s.lastPkt7SendAt = time.Now()
s.common.sendPkt7()
case <-pkt0SendTicker.C:
s.sendPkt0()
case <-s.pkt7TimeoutTimer.C:
log.Fatal("ping timeout")
case <-reauthTicker.C:
s.sendPktReauth(false)
case <-statusLogTicker.C:
log.Print("roundtrip latency ", s.pkt7Latency)
log.Print("roundtrip latency ", s.common.pkt7.latency)
}
}
}