fix: support jt808 versioned header
This commit is contained in:
@@ -77,31 +77,52 @@ func ParseFrame(raw []byte, receivedAtMS int64, sourceEndpoint string) (envelope
|
||||
messageID := binary.BigEndian.Uint16(raw[0:2])
|
||||
props := binary.BigEndian.Uint16(raw[2:4])
|
||||
bodySize := props & 0x03ff
|
||||
versioned := props&0x4000 != 0
|
||||
headerLen := 12
|
||||
phoneStart := 4
|
||||
phoneEnd := 10
|
||||
serialStart := 10
|
||||
packageInfo := map[string]any(nil)
|
||||
protocolVersion := byte(0)
|
||||
if versioned {
|
||||
headerLen = 17
|
||||
if len(raw) < headerLen {
|
||||
return envelope.FrameEnvelope{}, fmt.Errorf("%w: versioned header len=%d", ErrFrameTooShort, len(raw))
|
||||
}
|
||||
protocolVersion = raw[4]
|
||||
phoneStart = 5
|
||||
phoneEnd = 15
|
||||
serialStart = 15
|
||||
}
|
||||
if props&0x2000 != 0 {
|
||||
headerLen = 16
|
||||
headerLen += 4
|
||||
packageInfo = map[string]any{
|
||||
"total": binary.BigEndian.Uint16(raw[12:14]),
|
||||
"index": binary.BigEndian.Uint16(raw[14:16]),
|
||||
"total": binary.BigEndian.Uint16(raw[serialStart+2 : serialStart+4]),
|
||||
"index": binary.BigEndian.Uint16(raw[serialStart+4 : serialStart+6]),
|
||||
}
|
||||
}
|
||||
if len(raw) < headerLen+int(bodySize)+1 {
|
||||
return envelope.FrameEnvelope{}, fmt.Errorf("%w: bodySize=%d len=%d", ErrFrameTooShort, bodySize, len(raw))
|
||||
}
|
||||
|
||||
phoneBCD := raw[4:10]
|
||||
phoneBCD := raw[phoneStart:phoneEnd]
|
||||
phone := bcdString(phoneBCD)
|
||||
phoneTrimZero := strings.TrimLeft(phone, "0")
|
||||
if versioned && phoneTrimZero != "" {
|
||||
phone = phoneTrimZero
|
||||
}
|
||||
body := raw[headerLen : headerLen+int(bodySize)]
|
||||
parsed := map[string]any{
|
||||
"header": map[string]any{
|
||||
"message_id": fmt.Sprintf("0x%04X", messageID),
|
||||
"protocol_version": protocolVersion,
|
||||
"versioned": versioned,
|
||||
"properties": props,
|
||||
"body_size": bodySize,
|
||||
"phone_bcd_hex": strings.ToUpper(hex.EncodeToString(phoneBCD)),
|
||||
"phone": phone,
|
||||
"phone_trim_zero": strings.TrimLeft(phone, "0"),
|
||||
"sequence": binary.BigEndian.Uint16(raw[10:12]),
|
||||
"phone_trim_zero": phoneTrimZero,
|
||||
"sequence": binary.BigEndian.Uint16(raw[serialStart : serialStart+2]),
|
||||
"subpackage": props&0x2000 != 0,
|
||||
"package_info": packageInfo,
|
||||
"encryption_flags": (props >> 10) & 0x07,
|
||||
@@ -135,7 +156,7 @@ func ParseFrame(raw []byte, receivedAtMS int64, sourceEndpoint string) (envelope
|
||||
env := envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolJT808,
|
||||
MessageID: fmt.Sprintf("0x%04X", messageID),
|
||||
Sequence: binary.BigEndian.Uint16(raw[10:12]),
|
||||
Sequence: binary.BigEndian.Uint16(raw[serialStart : serialStart+2]),
|
||||
Phone: phone,
|
||||
SourceEndpoint: sourceEndpoint,
|
||||
EventTimeMS: eventTimeMS,
|
||||
|
||||
Reference in New Issue
Block a user