claiming

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package claiming implements the NMEA 2000 / ISO 11783 address claiming protocol (PGN 60928). It supports two modes: auto mode, where the claimer negotiates downward from a starting address on contention, and explicit mode, where contention is treated as a fatal error.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Claimer

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

Claimer implements the NMEA 2000 address claiming state machine.

func New

func New(cfg Config) *Claimer

New creates a new Claimer from the given configuration.

func (*Claimer) Address

func (c *Claimer) Address() uint8

Address returns the currently claimed address. This value may change over time as contention is resolved or a matching Commanded Address is applied.

func (*Claimer) HandleAddressClaim

func (c *Claimer) HandleAddressClaim(source uint8, name uint64)

HandleAddressClaim processes an incoming Address Claim (PGN 60928) from another device. source is the CAN source address of the competing device and name is its 64-bit NAME.

If the competing device claims our address:

  • If our NAME is lower (we win), we re-broadcast our claim to defend.
  • If their NAME is lower (we lose), auto mode tries the next available address downward; explicit mode surfaces a fatal error.

If the competing device claims a different address, the message is ignored.

func (*Claimer) HandleCommandedAddress added in v0.3.0

func (c *Claimer) HandleCommandedAddress(commandedName uint64, newAddress uint8) (bool, error)

HandleCommandedAddress applies an ISO Commanded Address (PGN 65240) to this node. The caller is responsible for accepting the command only from a fully reassembled, nine-byte broadcast transport transfer.

A command is ignored unless its complete 64-bit NAME matches this node and the requested address is claimable (0-251). A real change resets automatic contention history, notifies the owner, and immediately broadcasts a fresh Address Claim from the commanded address. The returned bool reports whether a change was applied.

func (*Claimer) HandleISORequest

func (c *Claimer) HandleISORequest()

HandleISORequest processes an incoming ISO Request (PGN 59904) asking for PGN 60928. It responds with our current address claim.

func (*Claimer) Reclaim added in v1.0.0

func (c *Claimer) Reclaim() error

Reclaim starts a fresh contention epoch at the current address after a transport reconnect. It resets prior contention history and immediately broadcasts Address Claim.

func (*Claimer) Start

func (c *Claimer) Start() error

Start sends the initial address claim broadcast. It returns an error if the claim frame cannot be sent.

type Config

type Config struct {
	// Mode selects auto or explicit address claiming behaviour.
	Mode Mode

	// Address is the starting address in auto mode or the fixed address in
	// explicit mode. Valid range is 0-251.
	Address uint8

	// Name is our device's 64-bit ISO 11783 NAME used for arbitration.
	// Lower NAME values win contention.
	Name uint64

	// WriteFrame sends a CAN frame onto the bus.
	WriteFrame func(can.Frame) error

	// OnAddressChange is called when the claimed address changes, either while
	// resolving contention or after a matching Commanded Address transfer. It
	// may be nil if the caller does not need notifications.
	OnAddressChange func(newAddr uint8)

	// OnFatalError is called when an unrecoverable error occurs, such as
	// address contention in explicit mode. It may be nil.
	OnFatalError func(err error)

	// Logger is used for structured logging of claiming activity.
	// If nil, a no-op logger is used.
	Logger *slog.Logger
}

Config holds the parameters needed to construct a Claimer.

type Mode

type Mode int

Mode controls how the claimer responds to address contention.

const (
	// ModeAuto starts at the configured address (default 251) and works
	// downward on contention until all addresses are exhausted, at which
	// point it falls back to address 254 (cannot claim).
	ModeAuto Mode = iota

	// ModeExplicit uses a fixed address and surfaces a fatal error if
	// another device with a lower NAME claims the same address.
	ModeExplicit
)

Jump to

Keyboard shortcuts

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