Fix TCPClientInterface readLoop and handlePacket methods to streamline HDLC framing logic and improve packet handling. Remove KISS framing support and update logging for received packets. Ensure outgoing data uses HDLC framing consistently.

This commit is contained in:
2025-09-27 04:30:28 -05:00
parent 483b6e562b
commit 972d00df92

View File

@@ -143,34 +143,6 @@ func (tc *TCPClientInterface) readLoop() {
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
b := buffer[i] b := buffer[i]
if tc.kissFraming {
// KISS framing logic
if b == KISS_FEND {
if inFrame && len(dataBuffer) > 0 {
tc.handlePacket(dataBuffer)
dataBuffer = dataBuffer[:0]
}
inFrame = !inFrame
continue
}
if inFrame {
if b == KISS_FESC {
escape = true
} else {
if escape {
if b == KISS_TFEND {
b = KISS_FEND
} else if b == KISS_TFESC {
b = KISS_FESC
}
escape = false
}
dataBuffer = append(dataBuffer, b)
}
}
} else {
// HDLC framing logic
if b == HDLC_FLAG { if b == HDLC_FLAG {
if inFrame && len(dataBuffer) > 0 { if inFrame && len(dataBuffer) > 0 {
tc.handlePacket(dataBuffer) tc.handlePacket(dataBuffer)
@@ -193,7 +165,6 @@ func (tc *TCPClientInterface) readLoop() {
} }
} }
} }
}
} }
func (tc *TCPClientInterface) handlePacket(data []byte) { func (tc *TCPClientInterface) handlePacket(data []byte) {
@@ -203,38 +174,19 @@ func (tc *TCPClientInterface) handlePacket(data []byte) {
} }
tc.mutex.Lock() tc.mutex.Lock()
tc.packetType = data[0]
tc.RxBytes += uint64(len(data)) tc.RxBytes += uint64(len(data))
lastRx := time.Now() lastRx := time.Now()
tc.lastRx = lastRx tc.lastRx = lastRx
tc.mutex.Unlock() tc.mutex.Unlock()
log.Printf("[DEBUG-7] Received packet: type=0x%02x, size=%d bytes", tc.packetType, len(data)) log.Printf("[DEBUG-7] Received packet: type=0x%02x, size=%d bytes", data[0], len(data))
payload := data[1:] // For RNS packets, call the packet callback directly
if callback := tc.GetPacketCallback(); callback != nil {
switch tc.packetType { log.Printf("[DEBUG-7] Calling packet callback for RNS packet")
case 0x01: // Announce packet callback(data, tc)
log.Printf("[DEBUG-7] Processing announce packet: payload=%d bytes", len(payload))
if len(payload) >= 53 {
tc.BaseInterface.ProcessIncoming(payload)
} else { } else {
log.Printf("[DEBUG-7] Announce packet too small: %d bytes", len(payload)) log.Printf("[DEBUG-7] No packet callback set for TCP interface")
}
case 0x02: // Link packet
log.Printf("[DEBUG-7] Processing link packet: payload=%d bytes", len(payload))
if len(payload) < 40 {
log.Printf("[DEBUG-7] Link packet too small: %d bytes", len(payload))
return
}
tc.BaseInterface.ProcessIncoming(payload)
case 0x03: // Announce packet
tc.BaseInterface.ProcessIncoming(payload)
case 0x04: // Transport packet
tc.BaseInterface.ProcessIncoming(payload)
default:
// Unknown packet type
return
} }
} }
@@ -263,14 +215,10 @@ func (tc *TCPClientInterface) ProcessOutgoing(data []byte) error {
tc.writing = true tc.writing = true
defer func() { tc.writing = false }() defer func() { tc.writing = false }()
// For TCP connections, use HDLC framing like the server expects
var frame []byte var frame []byte
if tc.kissFraming {
frame = append([]byte{KISS_FEND}, escapeKISS(data)...)
frame = append(frame, KISS_FEND)
} else {
frame = append([]byte{HDLC_FLAG}, escapeHDLC(data)...) frame = append([]byte{HDLC_FLAG}, escapeHDLC(data)...)
frame = append(frame, HDLC_FLAG) frame = append(frame, HDLC_FLAG)
}
// Update TX stats before sending // Update TX stats before sending
tc.UpdateStats(uint64(len(frame)), false) tc.UpdateStats(uint64(len(frame)), false)