Skip to content

Commit

Permalink
Checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tehmaze committed Dec 18, 2015
1 parent 599d42d commit 3fe41e1
Show file tree
Hide file tree
Showing 5 changed files with 372 additions and 108 deletions.
68 changes: 34 additions & 34 deletions controlblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,40 @@ type ControlBlock struct {
Data ControlBlockData
}

func (cb *ControlBlock) Bytes() ([]byte, error) {
var data = make([]byte, InfoSize)

if err := cb.Data.Write(data); err != nil {
return nil, err
}
if cb.Last {
data[0] |= B10000000
}

data[4] = uint8(cb.DstID >> 16)
data[5] = uint8(cb.DstID >> 8)
data[6] = uint8(cb.DstID)
data[7] = uint8(cb.SrcID >> 16)
data[8] = uint8(cb.SrcID >> 8)
data[9] = uint8(cb.SrcID)

// Calculate CRC16
for i := 0; i < 10; i++ {
crc16(&cb.CRC, data[i])
}
crc16end(&cb.CRC)

// Inverting according to the inversion polynomial.
cb.CRC = ^cb.CRC
// Applying CRC mask, see DMR AI spec. page 143.
cb.CRC ^= 0xa5a5

data[10] = uint8(cb.CRC >> 8)
data[11] = uint8(cb.CRC)

return data, nil
}

func (cb *ControlBlock) String() string {
if cb.Data == nil {
return fmt.Sprintf("CSBK, last %t, %d->%d, unknown (opcode %d)",
Expand Down Expand Up @@ -196,40 +230,6 @@ func (d *Preamble) Write(data []byte) error {

var _ (ControlBlockData) = (*Preamble)(nil)

func (cb *ControlBlock) Bytes() ([]byte, error) {
var data = make([]byte, InfoSize)

if err := cb.Data.Write(data); err != nil {
return nil, err
}
if cb.Last {
data[0] |= B10000000
}

data[4] = uint8(cb.DstID >> 16)
data[5] = uint8(cb.DstID >> 8)
data[6] = uint8(cb.DstID)
data[7] = uint8(cb.SrcID >> 16)
data[8] = uint8(cb.SrcID >> 8)
data[9] = uint8(cb.SrcID)

// Calculate CRC16
for i := 0; i < 10; i++ {
crc16(&cb.CRC, data[i])
}
crc16end(&cb.CRC)

// Inverting according to the inversion polynomial.
cb.CRC = ^cb.CRC
// Applying CRC mask, see DMR AI spec. page 143.
cb.CRC ^= 0xa5a5

data[10] = uint8(cb.CRC >> 8)
data[11] = uint8(cb.CRC)

return data, nil
}

func ParseControlBlock(data []byte) (*ControlBlock, error) {
if len(data) != InfoSize {
return nil, fmt.Errorf("dmr: expected %d info bytes, got %d", InfoSize, len(data))
Expand Down
26 changes: 10 additions & 16 deletions crc.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
package dmr

const (
// G(x) = x^9+x^6+x^4+x^3+1
crc9poly = 0x59
// G(x) = x^16+x^12+x^5+1
crc16poly = 0x1021
crc32poly = 0x04c11db7
)

// G(x) = x^9+x^6+x^4+x^3+1
func crc9(crc *uint16, b uint8, bits int) {
var v uint8 = 0x80
for i := 0; i < 8-bits; i++ {
Expand All @@ -22,7 +15,7 @@ func crc9(crc *uint16, b uint8, bits int) {
(*crc)++
}
if xor {
(*crc) ^= crc9poly
(*crc) ^= 0x0059
}
v >>= 1
}
Expand All @@ -35,32 +28,33 @@ func crc9end(crc *uint16, bits int) {
// Limit the number of shift registers to 9.
*crc &= 0x01ff
if xor {
(*crc) ^= crc9poly
(*crc) ^= 0x0059
}
}
}

// G(x) = x^16+x^12+x^5+1
func crc16(crc *uint16, b byte) {
var v uint8 = 0x80
for i := 0; i < 8; i++ {
xor := ((*crc) & 0x8000) > 0
xor := ((*crc) & 0x8000) != 0
(*crc) <<= 1
if b&v > 0 {
(*crc)++
}
if xor {
(*crc) ^= crc16poly
(*crc) ^= 0x1021
}
v >>= 1
}
}

func crc16end(crc *uint16) {
for i := 0; i < 16; i++ {
xor := ((*crc) & 0x8000) > 0
xor := ((*crc) & 0x8000) != 0
(*crc) <<= 1
if xor {
(*crc) ^= crc16poly
(*crc) ^= 0x1021
}
}
}
Expand All @@ -74,7 +68,7 @@ func crc32(crc *uint32, b byte) {
(*crc)++
}
if xor {
(*crc) ^= crc32poly
(*crc) ^= 0x04c11db7
}
v >>= 1
}
Expand All @@ -85,7 +79,7 @@ func crc32end(crc *uint32) {
xor := ((*crc) & 0x80000000) > 0
(*crc) <<= 1
if xor {
(*crc) ^= crc32poly
(*crc) ^= 0x04c11db7
}
}
}
35 changes: 1 addition & 34 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ func ParseDataBlock(data []byte, dataType uint8, confirmed bool) (*DataBlock, er
db.Serial = data[0] >> 1
db.CRC = uint16(data[0]&B00000001)<<8 | uint16(data[1])
db.Data = make([]byte, db.Length)
copy(db.Data, data[2:db.Length])

//log.Debugf("data block serial: %#02x (%#07b), crc: %#02x (%#09b), length: %d, confirmed: %t:", db.Serial, db.Serial, db.CRC, db.CRC, db.Length, confirmed)
//log.Debug(hex.Dump(data[2 : 2+db.Length]))
copy(db.Data, data[2:2+db.Length])

for _, block := range db.Data {
crc9(&crc, block, 8)
Expand All @@ -56,7 +53,6 @@ func ParseDataBlock(data []byte, dataType uint8, confirmed bool) (*DataBlock, er
if crc != db.CRC {
return nil, fmt.Errorf("dmr: block CRC error (%#04x != %#04x)", crc, db.CRC)
}
log.Debugf("dmr: data block CRC passed %#04x", crc)
} else {
copy(db.Data, data[:db.Length])
}
Expand Down Expand Up @@ -239,8 +235,6 @@ func CombineDataBlocks(blocks []*DataBlock) (*DataFragment, error) {
if crc != f.CRC {
return nil, fmt.Errorf("dmr: fragment CRC error (%#08x != %#08x)", crc, f.CRC)
}
log.Debugf("dmr: data fragment CRC passed %#08x", crc)

return f, nil
}

Expand Down Expand Up @@ -279,33 +273,6 @@ func ParseMessageData(data []byte, ddFormat uint8, nullTerminated bool) (string,
}

func init() {
/*
DDFormatBinary: "binary",
DDFormatBCD: "BCD",
DDFormat7BitChar: "7-bit characters",
DDFormat8BitISO8859_1: "8-bit ISO 8859-1",
DDFormat8BitISO8859_2: "8-bit ISO 8859-2",
DDFormat8BitISO8859_3: "8-bit ISO 8859-3",
DDFormat8BitISO8859_4: "8-bit ISO 8859-4",
DDFormat8BitISO8859_5: "8-bit ISO 8859-5",
DDFormat8BitISO8859_6: "8-bit ISO 8859-6",
DDFormat8BitISO8859_7: "8-bit ISO 8859-7",
DDFormat8BitISO8859_8: "8-bit ISO 8859-8",
DDFormat8BitISO8859_9: "8-bit ISO 8859-9",
DDFormat8BitISO8859_10: "8-bit ISO 8859-10",
DDFormat8BitISO8859_11: "8-bit ISO 8859-11",
DDFormat8BitISO8859_13: "8-bit ISO 8859-13",
DDFormat8BitISO8859_14: "8-bit ISO 8859-14",
DDFormat8BitISO8859_15: "8-bit ISO 8859-15",
DDFormat8BitISO8859_16: "8-bit ISO 8859-16",
DDFormatUTF8: "UTF-8",
DDFormatUTF16: "UTF-16",
DDFormatUTF16BE: "UTF-16 big endian",
DDFormatUTF16LE: "UTF-16 little endian",
DDFormatUTF32: "UTF-32",
DDFormatUTF32BE: "UTF-32 big endian",
DDFormatUTF32LE: "UTF-32 little endian",
*/
encodingMap = map[uint8]encoding.Encoding{
DDFormatBinary: binaryEncoding{},
DDFormat8BitISO8859_2: charmap.ISO8859_2,
Expand Down
Loading

0 comments on commit 3fe41e1

Please sign in to comment.