From 2851102398e565a3533d5d4f48b633e33d96cea5 Mon Sep 17 00:00:00 2001 From: Martin Hebnes Pedersen Date: Tue, 7 May 2019 22:38:16 +0200 Subject: [PATCH] Fix serialization of Confirmed DataBlock The serialized form should be 10 octets of user-data plus the two octets for serial/crc. Previous implementation forgot to account for the two extra octets when allocating the final data slice. --- data.go | 3 +++ data_test.go | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/data.go b/data.go index 049f812..0eb998d 100644 --- a/data.go +++ b/data.go @@ -81,6 +81,9 @@ func (db *DataBlock) Bytes(dataType uint8, confirmed bool) []byte { // Applying CRC mask, see DMR AI spec. page 143 db.CRC ^= 0x01ff + // Grow data slice to support the two byte prefix + data = append(data, make([]byte, 2)...) + data[0] = (db.Serial << 1) | (uint8(db.CRC>>8) & 0x01) data[1] = uint8(db.CRC) copy(data[2:], db.Data) diff --git a/data_test.go b/data_test.go index 04aaa84..f717d59 100644 --- a/data_test.go +++ b/data_test.go @@ -19,7 +19,8 @@ func TestDataBlock(t *testing.T) { if data == nil { t.Fatal("encode failed") } - size := int(dataBlockLength(Rate34Data, true)) + // Size is the user-data + two octets of serial/crc + size := int(dataBlockLength(Rate34Data, true)) + 2 if len(data) != size { t.Fatalf("encode failed: expected %d bytes, got %d", size, len(data)) }