Raise host frame payload limit so multi-client UART replies fit.
Use a uint16 LE length and 1024-byte max payload so CLIENT_INFO and BATTERY_STATUS no longer fail encoding past ~10 connected pods. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+21
-11
@@ -10,8 +10,10 @@ import (
|
||||
const (
|
||||
StartMarker = 0xAA
|
||||
StopMarker = 0xCC
|
||||
// Must match main/uart.h MAX_PAYLOAD_SIZE (MAX_BUF_SIZE - 4).
|
||||
MaxPayload = 248
|
||||
// Must match main/uart.h MAX_PAYLOAD_SIZE.
|
||||
MaxPayload = 1024
|
||||
// AA | len_lo | len_hi | payload | xor | CC
|
||||
FrameOverhead = 5
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -19,14 +21,15 @@ var (
|
||||
ErrTimeout = errors.New("read timeout")
|
||||
)
|
||||
|
||||
// EncodeFrame builds a framed packet: 0xAA len payload xor(payload) 0xCC.
|
||||
// EncodeFrame builds a framed packet: 0xAA len_lo len_hi payload xor(payload) 0xCC.
|
||||
func EncodeFrame(payload []byte) ([]byte, error) {
|
||||
if len(payload) == 0 || len(payload) > MaxPayload {
|
||||
return nil, fmt.Errorf("payload length %d out of range 1..%d", len(payload), MaxPayload)
|
||||
}
|
||||
|
||||
frame := make([]byte, 0, 4+len(payload))
|
||||
frame = append(frame, StartMarker, byte(len(payload)))
|
||||
n := len(payload)
|
||||
frame := make([]byte, 0, FrameOverhead+n)
|
||||
frame = append(frame, StartMarker, byte(n&0xff), byte((n>>8)&0xff))
|
||||
var checksum byte
|
||||
for _, b := range payload {
|
||||
frame = append(frame, b)
|
||||
@@ -39,6 +42,7 @@ func EncodeFrame(payload []byte) ([]byte, error) {
|
||||
type Parser struct {
|
||||
state int
|
||||
len int
|
||||
lenLo byte
|
||||
payload []byte
|
||||
index int
|
||||
checksum byte
|
||||
@@ -46,7 +50,8 @@ type Parser struct {
|
||||
|
||||
const (
|
||||
stateStart = iota
|
||||
stateLen
|
||||
stateLenLo
|
||||
stateLenHi
|
||||
stateData
|
||||
stateChecksum
|
||||
stateStop
|
||||
@@ -63,14 +68,19 @@ func (p *Parser) Feed(b byte) (payload []byte, ok bool, err error) {
|
||||
if b == StartMarker {
|
||||
p.index = 0
|
||||
p.checksum = 0
|
||||
p.state = stateLen
|
||||
p.state = stateLenLo
|
||||
}
|
||||
case stateLen:
|
||||
if b == 0 || b > MaxPayload {
|
||||
case stateLenLo:
|
||||
p.lenLo = b
|
||||
p.state = stateLenHi
|
||||
case stateLenHi:
|
||||
n := int(p.lenLo) | (int(b) << 8)
|
||||
if n == 0 || n > MaxPayload {
|
||||
p.state = stateStart
|
||||
} else {
|
||||
p.len = int(b)
|
||||
p.len = n
|
||||
p.payload = make([]byte, p.len)
|
||||
p.index = 0
|
||||
p.state = stateData
|
||||
}
|
||||
case stateData:
|
||||
@@ -102,7 +112,7 @@ func (p *Parser) Feed(b byte) (payload []byte, ok bool, err error) {
|
||||
// maxWait bounds total wait time; zero means no limit (serial read timeouts retry forever).
|
||||
func ReadFrame(r io.Reader, buf []byte, maxWait time.Duration) ([]byte, error) {
|
||||
if buf == nil {
|
||||
buf = make([]byte, 256)
|
||||
buf = make([]byte, 1024)
|
||||
}
|
||||
parser := NewParser()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user