Expand description
12-bit alias generation, using the pseudo-random sequence suggested by the CAN Frame Transfer Standard (seeded from the Node ID).
§Why CAN needs aliases
A CAN 2.0B extended identifier has 29 bits, of which OpenLCB can only
spare 12 for the source address. A 48-bit NodeId does not fit, so on
CAN each node picks a short alias (1..=0xFFF) and uses it as the
source address in every frame header. Addressed messages likewise carry
the destination’s alias rather than its Node ID. Aliases are local to
one CAN segment; gateways and TCP links translate them.
Since aliases are chosen without coordination, two nodes may pick the
same one. The CAN Frame Transfer Standard resolves this with a
reservation protocol (CID frames, a 200 ms wait, RID, AMD; see
crate::node) and by having each node generate candidates from a
pseudo-random sequence seeded with its own Node ID. Different nodes then
almost always try different aliases, and after a collision a node simply
moves on to the next value in its sequence.
The sequence here is the 48-bit LFSR-like generator recommended by the standard (also used by OpenMRN and JMRI), so a given Node ID tends to receive the same alias after each power cycle.
use rolcb::alias::AliasGenerator;
use rolcb::NodeId;
let mut gen = AliasGenerator::new(NodeId::new(0x0501_0101_2200));
let first = gen.next_alias();
assert!((1..=0xFFF).contains(&first));
assert_ne!(gen.next_alias(), first);
// Deterministic: the same Node ID gives the same sequence.
assert_eq!(AliasGenerator::new(NodeId::new(0x0501_0101_2200)).next_alias(), first);Structs§
- Alias
Generator - Produces a deterministic sequence of candidate aliases for a node.