protocol

package
v0.0.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2024 License: MIT Imports: 6 Imported by: 0

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

View Source
const (
	MaxPackageSize = constants.BufferSize + 11 // max package size
)

Variables

View Source
var (
	ErrInvalidSize     = errors.New("invalid size")
	ErrInvalidChecksum = errors.New("invalid checksum")
)

Functions

func Decode

func Decode(dest, src []byte) (cmd byte, n int, counter uint16, err error)

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

func Encode(dest, src []byte, cmd byte, counter uint16) (int, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL