Skip to content

Commit

Permalink
Fix serialization of Confirmed DataBlock
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
martinhpedersen committed May 7, 2019
1 parent 68378f8 commit 2851102
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down

0 comments on commit 2851102

Please sign in to comment.