apigatt

package
v2.23.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: GPL-3.0 Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package apigatt defines the GATT contract the Zaparoo app uses to reach the JSON-RPC API over Bluetooth Low Energy: the service and characteristic UUIDs, the limits, and the framing that carries one API message across many small ATT packets. It has no dependency on the API itself so the framing can be fuzzed and reused on its own.

Index

Constants

View Source
const (
	// HeaderSize is the header on every chunk but the first of a message.
	HeaderSize = 5
	// FirstHeaderSize is the header on the first chunk of a message.
	FirstHeaderSize = 9
)

Chunk layout, both directions:

byte 0    flags   bits 7..4 protocol version, bit 2 ACK, bit 1 LAST,
                  bit 0 FIRST, bit 3 reserved and zero
byte 1-2  seq     big endian; see below
byte 3-4  tag     session tag, big endian
byte 5-8  length  total message length, big endian, FIRST only
payload           at least one byte, none on an ACK

On a data chunk seq counts every data chunk the sender has sent on this connection, starting at 0 and wrapping at 65536. It does not restart with each message, so the receiver can put chunks back in order across message boundaries as well as inside one.

An ACK chunk is sent by the client only. Its seq is the sequence number of the next data chunk the client expects from Core, which acknowledges everything before it. Core never sends more than SendWindow chunks beyond the last acknowledgement.

View Source
const (
	// ServiceUUID is the primary service the app scans for.
	ServiceUUID = "0da70001-b359-443b-836f-477d34b6a638"
	// RXCharUUID carries chunks from the app to Core (write, write without
	// response).
	RXCharUUID = "0da70002-b359-443b-836f-477d34b6a638"
	// TXCharUUID carries chunks from Core to the app (notify).
	TXCharUUID = "0da70003-b359-443b-836f-477d34b6a638"
	// InfoCharUUID is a read-only JSON description of the endpoint the app
	// reads before it speaks (see Info).
	InfoCharUUID = "0da70004-b359-443b-836f-477d34b6a638"
	// BondCharUUID reads empty and only over an encrypted link, so reading
	// it makes a phone finish pairing with the stack first. The app reads it
	// before subscribing to TX: BlueZ keeps a subscription under the address
	// the phone connected with, and a pairing that reveals the phone's
	// identity address leaves it there, so no notification would arrive.
	BondCharUUID = "0da70005-b359-443b-836f-477d34b6a638"
)

Service and characteristic UUIDs. The app must use the same values; they are fixed for the life of protocol version 1.

View Source
const (
	// ProtocolVersion is carried in every chunk header.
	ProtocolVersion = 1

	// MaxMessageSize caps one reassembled message from an authenticated
	// client. It matches the WebSocket limit. Messages from Core have no
	// cap: whatever the API returns is streamed.
	MaxMessageSize = 4 * 1024 * 1024

	// MaxUnauthenticatedMessageSize caps a message from a client that has
	// not authenticated yet, which is anyone in radio range. It covers a
	// pairing request or an encrypted first frame.
	MaxUnauthenticatedMessageSize = 64 * 1024

	// ReorderWindow is how far ahead of the expected chunk a chunk may
	// arrive and still be held. BlueZ hands each write to us on its own
	// goroutine, so chunks reach the reassembler in any order.
	ReorderWindow = 256

	// SendWindow is how many chunks Core sends beyond the last one the
	// client acknowledged. It is what keeps a slow link from piling
	// megabytes up inside bluetoothd.
	SendWindow = 64

	// MaxProtocolErrors is how many recoverable framing mistakes one
	// connection may make before it is dropped.
	MaxProtocolErrors = 3

	// DefaultMTU is the ATT MTU every link starts with; a peer that never
	// negotiated a larger one gets 20-byte chunks.
	DefaultMTU = 23

	// MaxChunkSize is the longest value an attribute can hold. A link may
	// negotiate an MTU that would carry more (517 is common), but the
	// stack cuts a longer value short, so a chunk never exceeds this.
	MaxChunkSize = 512

	// PreferredMTU is what the Info characteristic suggests the app request.
	PreferredMTU = 512
)
View Source
const ServiceHandle uint16 = 0x0100

ServiceHandle is where the service asks to sit in the ATT database, so its handles are the same on every start. It is well clear of what BlueZ registers for itself.

Variables

View Source
var (
	// ErrMalformedChunk means the chunk cannot be parsed at all: too short,
	// wrong version, reserved bits set. The connection should be dropped.
	ErrMalformedChunk = errors.New("apigatt: malformed chunk")
	// ErrMessageTooLarge means a message declared more bytes than the
	// receiver accepts.
	ErrMessageTooLarge = errors.New("apigatt: message too large")
	// ErrLengthMismatch means the chunks did not add up to the declared
	// length.
	ErrLengthMismatch = errors.New("apigatt: message length mismatch")
	// ErrSequence means a chunk arrived outside the reorder window or
	// repeated a sequence number.
	ErrSequence = errors.New("apigatt: chunk out of sequence")
	// ErrTooManyErrors means the peer made more recoverable mistakes than
	// MaxProtocolErrors allows.
	ErrTooManyErrors = errors.New("apigatt: too many protocol errors")
	// ErrEmptyMessage means there is nothing to send.
	ErrEmptyMessage = errors.New("apigatt: empty message")
)

Functions

func EncodeAck

func EncodeAck(tag, next uint16) []byte

EncodeAck builds the chunk that acknowledges every data chunk before next.

func EncodeChunk

func EncodeChunk(h Header, payload []byte) []byte

EncodeChunk builds one data chunk. Length is written only for a first chunk.

Types

type Chunker

type Chunker struct {
	// Tag is stamped on every chunk.
	Tag uint16
	// contains filtered or unexported fields
}

Chunker splits one side's messages into chunks for one connection. It carries the sequence counter, so a connection needs exactly one per direction and must not use it from two goroutines at once.

func (*Chunker) Next

func (c *Chunker) Next() uint16

Next is the sequence number the next data chunk will carry.

func (*Chunker) Split

func (c *Chunker) Split(msg []byte, mtu int, emit func(chunk []byte) error) error

Split cuts msg into chunks that fit the link's ATT MTU, and never more than MaxChunkSize, and hands them to emit in transmission order, one at a time, so a large message is never held as chunks all at once. It stops at the first error from emit; the sequence counter has then moved past the chunks already emitted.

type Header struct {
	Length  uint32 // valid only when First
	Seq     uint16
	Tag     uint16
	Version uint8
	First   bool
	Last    bool
	Ack     bool
}

Header is the decoded chunk header.

func ParseChunk

func ParseChunk(chunk []byte) (Header, []byte, error)

ParseChunk decodes one chunk into its header and payload. The payload aliases chunk and is empty for an ACK.

type Info

type Info struct {
	DeviceID string `json:"deviceId"`
	Version  int    `json:"v"`
	// MaxMessage and MaxUnauthenticated are the largest messages Core
	// accepts from the client, after and before authentication.
	MaxMessage         int `json:"maxMessage"`
	MaxUnauthenticated int `json:"maxUnauthenticated"`
	PreferredMTU       int `json:"preferredMtu"`
	// Window is the most chunks Core sends without an acknowledgement.
	Window int `json:"window"`
}

Info is the JSON document served by InfoCharUUID.

func NewInfo

func NewInfo(deviceID string) Info

NewInfo describes this endpoint for the given device.

type Reassembler

type Reassembler struct {
	// contains filtered or unexported fields
}

Reassembler rebuilds one side's messages from chunks for one connection. Chunks may arrive in any order within ReorderWindow; messages come out in the order they were sent.

func NewReassembler

func NewReassembler(maxMessage int) *Reassembler

NewReassembler returns a reassembler accepting messages up to maxMessage bytes. Zero or less means no limit, which is what a client receiving from Core wants; Core always sets one.

func (*Reassembler) Held

func (r *Reassembler) Held() int

Held is how many chunks are waiting for an earlier one that has not arrived. It staying above zero means a chunk was lost.

func (*Reassembler) Next

func (r *Reassembler) Next() uint16

Next is the sequence number of the next chunk the reassembler is waiting for, which is what a client sends in an ACK.

func (*Reassembler) Push

func (r *Reassembler) Push(h Header, payload []byte) ([][]byte, error)

Push consumes one data chunk already decoded by ParseChunk and returns the messages it completed, oldest first: a chunk that fills a gap can release several. A non-nil error means the connection should be dropped; the reassembler is unusable afterwards.

func (*Reassembler) SetMaxMessage

func (r *Reassembler) SetMaxMessage(maxMessage int)

SetMaxMessage changes the largest message accepted from now on. Zero or less means no limit.

Jump to

Keyboard shortcuts

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