-
Notifications
You must be signed in to change notification settings - Fork 2
/
frame.go
36 lines (31 loc) · 840 Bytes
/
frame.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package hdlc
import (
"github.com/sigurn/crc16"
)
const (
flagSym byte = 0x7e
escapeSym byte = 0x7d
abortSym byte = 0x7f
)
var (
addressCtrlSeq = []byte{0xff, 0x03}
crcTable = crc16.MakeTable(crc16.CRC16_MCRF4XX)
crcGood uint16 = 0xf0b8
)
// A Frame is an HDLC frame.
// Use Encapsulate to create a new one starting from a payload.
type Frame struct {
Payload []byte
FCS []byte
HasAddressCtrlPrefix bool
}
// Valid performs a CRC on the frame, based on the FCS and the other fields.
func (f Frame) Valid() bool {
crc := crc16.Init(crcTable)
if f.HasAddressCtrlPrefix {
crc = crc16.Update(crc, addressCtrlSeq, crcTable)
}
crc = crc16.Update(crc, f.Payload, crcTable)
crc = crc16.Update(crc, f.FCS, crcTable)
return crc16.Complete(crc, crcTable) == crcGood
}