Documentation
¶
Overview ¶
Protocol package implements a simple protocol for sending and receiving data.
Protocol format: ABBCCCCDDD...DDDFFFF Where: A: command byte B: counter (2 bytes, big endian) optional used to debug and validate package order C: payload length (32 bits, big endian) D: payload (array of bytes) F: checksum (FNV-1a, 32 bits, big endian)
The checksum is calculated over the command byte, counter, payload length, and the data itself.
Index ¶
Examples ¶
Constants ¶
const (
MaxPackageSize = constants.BufferSize + 11 // max package size
)
Variables ¶
var ( ErrInvalidSize = errors.New("invalid size") ErrInvalidChecksum = errors.New("invalid checksum") )
Functions ¶
func Decode ¶
Decode decodes the source buffer into the destination buffer. It returns the command byte, the number of bytes read, the counter value, and an error, if any. command byte + counter + data length + checksum length = 11 bytes
Example ¶
data := []byte("hello") out := make([]byte, MaxPackageSize) n, err := Encode(out, data, 0x01, 0x01) if err != nil { panic(err) } cmd, n, _, err := Decode(data, out[:n]) if err != nil { panic(err) } fmt.Printf("cmd: %02X\n", cmd) fmt.Printf("data: %v\n", string(data[:n]))
Output: cmd: 01 data: hello
func Encode ¶
Encode encodes the source data into the destination buffer using the specified command. It returns the number of bytes written and an error, if any.
Example ¶
data := []byte("hello") out := make([]byte, MaxPackageSize) n, err := Encode(out, data, 0x01, 0x01) if err != nil { panic(err) } fmt.Printf("data: %02X\n", string(out[:n]))
Output: data: 0100010000000568656C6C6F7CE86368
Types ¶
This section is empty.