Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
496006c
Report the stack's own tree depth in beacons and Mgmt_Lqi_rsp
puddly Nov 22, 2026
417f7af
Turn concentrator behavior on and off at runtime
puddly Nov 22, 2026
0033bd1
Discover routes for frames relayed from our end device children
puddly Nov 22, 2026
d904ad9
Send route records to concentrators over many-to-one routes
puddly Nov 22, 2026
f347690
Report broken many-to-one routes to the concentrator
puddly Nov 22, 2026
0f4492a
Drop relayed route records only when the frame is full
puddly Nov 22, 2026
243c0fa
Use a stored source route only over a good outbound first hop
puddly Nov 22, 2026
584ffb5
Directly send to neighbors without a known route when `cost <= 2`
puddly Nov 22, 2026
2a56c49
Jitter every link status period so routers do not stay in step
puddly Nov 22, 2026
4e2b057
Delay relayed route requests by the cost of the incoming link
puddly Nov 22, 2026
9a9c8ed
Stop relaying a route request copy once a cheaper copy is relayed
puddly Nov 22, 2026
8a77b37
Fill each link status frame with as many entries as fit
puddly Nov 22, 2026
9086ff3
Decide route records in next-hop resolution, for held and relayed fra…
puddly Nov 23, 2026
c5c8d3b
Send route records at transmit time and keep them due until the conce…
puddly Nov 23, 2026
61c07dd
Update the route record state only once our route record is acked
puddly Nov 23, 2026
f5111f9
Report route records as required in Mgmt_Rtg_rsp for concentrators wi…
puddly Nov 23, 2026
f313e3a
Report the coordinator at depth 0 in Mgmt_Lqi_rsp when it was first h…
puddly Nov 23, 2026
1f12fa0
Size secured NWK frames without encrypting them
puddly Nov 23, 2026
f13aadb
Use a route record with no relays even when source routes are capped …
puddly Nov 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions crates/ziggurat-driver/src/zigbee_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,11 @@ pub struct NetworkConfig {
pub tclk_seed: Option<TclkSeed>,
/// The radio transmit power in dBm
pub tx_power: i8,
pub source_routing: bool,
/// Whether we start out as a concentrator (see [`ZigbeeStack::set_concentrator`]).
pub concentrator: bool,
/// A router's tree depth: one more than its parent's when it joined. The
/// coordinator's is always 0, whatever this says.
pub depth: u8,
}

/// The frame a send holds, as enqueued.
Expand Down Expand Up @@ -364,6 +368,10 @@ pub struct Nib {
pub nwk_security: NwkSecurity,
/// The EUI64-to-network-address map, one owner per network address
pub address_map: AddressMap,
/// `nwkIsConcentrator`: we advertise many-to-one routes to ourselves and
/// source-route through the route records they bring back. Whoever controls the
/// node decides, at any time.
pub is_concentrator: bool,
}

impl Nib {
Expand Down Expand Up @@ -477,12 +485,13 @@ pub struct State {
pub role: NwkDeviceType,
pub capability_information: NwkCapabilityInformation,
pub nwk_manager_addr: Nwk,
/// Our tree depth, advertised in beacons. The coordinator is the root, at 0.
pub depth: u8,

pub ieee_address: Eui64,
pub network_address: Nwk,
pub extended_pan_id: Eui64,

pub is_concentrator: bool,
pub security_level: u8,

/// A flag that determines if a timestamp indication is provided on incoming and
Expand All @@ -507,6 +516,12 @@ pub struct State {

impl State {
pub fn new(config: &NetworkConfig, aps_counter: u8) -> Self {
assert!(
config.depth <= MAX_DEPTH,
"depth {} is deeper than nwkcMaxDepth",
config.depth
);

Self {
core: Mutex::new(ZigbeeCore {
nib: Nib {
Expand All @@ -525,6 +540,7 @@ impl State {
FRAME_COUNTER_NOTIFY_INTERVAL,
),
address_map: AddressMap::new(config.network_address, config.ieee_address),
is_concentrator: config.concentrator,
},
aib: Aib {
aps_counter,
Expand Down Expand Up @@ -553,6 +569,10 @@ impl State {
hack_force_route_discovery: false,

role: config.role,
depth: match config.role {
NwkDeviceType::Coordinator => 0,
NwkDeviceType::Router | NwkDeviceType::EndDevice => config.depth,
},
capability_information: NwkCapabilityInformation {
alternate_pan_coordinator: false,
device_type: match config.role {
Expand All @@ -571,7 +591,6 @@ impl State {
nwk_manager_addr: Nwk(0x0000),
network_address: config.network_address,
extended_pan_id: config.extended_pan_id,
is_concentrator: config.source_routing,
security_level: 5,
time_stamp: false,
leave_request_allowed: false,
Expand Down Expand Up @@ -727,6 +746,9 @@ pub struct ZigbeeStack<P: RadioPhy, R: Runtime> {
/// Wakes the many-to-one route request task when its next advertisement moved
/// earlier
pub(crate) mtorr_wake: Notify,
/// Wakes the many-to-one advertiser when the client turns concentrator behavior on
/// or off
pub(crate) concentrator_changed: Notify,

/// Signaled whenever a link status command is digested; the MTORR startup wait
/// uses it to advertise as soon as a neighbor link is established
Expand Down Expand Up @@ -880,6 +902,7 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
src_match_written: Mutex::new(SrcMatchTable::default()),
parent_annce_received: Mutex::new(None),
mtorr_wake: Notify::new(),
concentrator_changed: Notify::new(),
link_status_received: Notify::new(),
link_status_wake: Notify::new(),
address_conflict_wake: Notify::new(),
Expand Down Expand Up @@ -1134,12 +1157,10 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
});

// Advertise many-to-one routes to ourselves so that devices can route inbound
// traffic without per-device route discoveries
if self.state.is_concentrator {
self.spawn_tracked(|arc_self| async move {
arc_self.periodic_many_to_one_route_request_task().await;
});
}
// traffic without per-device route discoveries, while we are a concentrator
self.spawn_tracked(|arc_self| async move {
arc_self.periodic_many_to_one_route_request_task().await;
});

// Reprogram the radio whenever it resets out from under us
self.spawn_tracked(|arc_self| async move {
Expand Down
9 changes: 2 additions & 7 deletions crates/ziggurat-driver/src/zigbee_stack/joining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,6 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
})
}

pub(super) fn is_neighbor(&self, nwk: Nwk) -> bool {
self.core().nib.neighbors.has_network_address(nwk)
}

/// Register an install-code-derived link key for a device expected to join:
/// an `apsDeviceKeyPairSet` entry with `PROVISIONAL_KEY` attributes that
/// replaces the well-known key for that device's network key delivery.
Expand Down Expand Up @@ -628,8 +624,7 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
}
}

/// Send a serialized APS frame to an on-network device, with NWK security. Direct
/// children do not participate in route discovery, so they are addressed directly.
/// Send a serialized APS frame to an on-network device, with NWK security.
fn send_secured_aps_payload(
&self,
destination: Nwk,
Expand All @@ -644,7 +639,7 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
self.send_unicast(Unicast {
frame: nwk_frame,
security: NwkSecurityMode::NetworkKey,
mode: if self.is_neighbor(destination) {
mode: if self.sends_directly_to(destination) {
SendMode::Direct
} else {
SendMode::Route(RouteDirective::StackDecides)
Expand Down
2 changes: 1 addition & 1 deletion crates/ziggurat-driver/src/zigbee_stack/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
protocol_version: PROTOCOL_VERSION,
reserved1: 0b00,
router_capacity: true,
device_depth: 0,
device_depth: self.state.depth,
end_device_capacity,
extended_pan_id: self.state.extended_pan_id,
tx_offset: 0xFFFFFF,
Expand Down
67 changes: 47 additions & 20 deletions crates/ziggurat-driver/src/zigbee_stack/neighbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ use ziggurat_phy::RadioPhy;
use ziggurat_zigbee::nwk::NwkDeviceType;
use ziggurat_zigbee::nwk::commands::{NwkCommand, NwkLinkStatusCommand};
use ziggurat_zigbee::nwk::frame::{BROADCAST_ALL_ROUTERS_AND_COORDINATOR, NwkFrame};
use ziggurat_zigbee::nwk::next_hop;

use super::TrafficClass;

use super::{
Broadcast, EnqueueError, NwkSecurityMode, SendHandle, TxPolicy, TxPriority, ZigbeeStack,
};

/// Maximum number of link status entries that can be carried in a single frame.
const MAX_LINK_STATUSES: usize = 7;

impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
pub(super) fn maybe_recompute_lqa(&self, sender_nwk: Nwk, lqi: u8, _rssi: i8) {
let link_status_due = self.core().nib.neighbors.record_lqa(sender_nwk, lqi);
Expand Down Expand Up @@ -62,6 +60,18 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
self.core().nib.neighbors.end_device_child_eui64(nwk)
}

/// Whether a unicast goes straight to its destination, with no route: an end device
/// child, a router child that has sent no link status yet, or a neighbor with a good
/// link.
#[allow(clippy::significant_drop_tightening)]
pub(super) fn sends_directly_to(&self, destination: Nwk) -> bool {
let now = self.core_now();
let core = self.core();
let policy = self.next_hop_policy(&core.nib);

next_hop::reaches_directly(&core.nib.neighbors, destination, policy, now)
}

pub(super) fn sleepy_child_eui64(&self, nwk: Nwk) -> Option<Eui64> {
self.core().nib.neighbors.sleepy_child_eui64(nwk)
}
Expand Down Expand Up @@ -173,25 +183,38 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {
link_statuses.sort_by_key(|a| a.address.as_u16());

let total = link_statuses.len();
let build_frame = |start: usize, end: usize| {
self.nwk_command_frame(
BROADCAST_ALL_ROUTERS_AND_COORDINATOR,
NwkCommand::LinkStatus(NwkLinkStatusCommand {
is_first_frame: start == 0,
is_last_frame: end == total,
link_statuses: link_statuses[start..end].to_vec(),
}),
)
.with_radius(1)
};
// Measured with the auxiliary header the frame is sent with
let aux_header = self.outgoing_aux_header(0);
let fits = |start: usize, end: usize| {
let mut frame = build_frame(start, end);
frame.aux_header = Some(aux_header.clone());
frame.fits_secured()
};
let mut start = 0;

loop {
// Each frame carries up to `MAX_LINK_STATUSES` entries; consecutive frames
// repeat one boundary entry (the last of frame N is the first of frame N+1)
// so a receiver can stitch the advertised address range together (spec
// 3.6.4.4.2). An empty list still emits a single first+last frame.
let end = core::cmp::min(start + MAX_LINK_STATUSES, total);

let link_status_frame = self
.nwk_command_frame(
BROADCAST_ALL_ROUTERS_AND_COORDINATOR,
NwkCommand::LinkStatus(NwkLinkStatusCommand {
is_first_frame: start == 0,
is_last_frame: end == total,
link_statuses: link_statuses[start..end].to_vec(),
}),
)
.with_radius(1);
// Spec 3.4.8.3.2: the list is split only when it does not fit in one frame.
// Each frame takes as many entries as fit, and consecutive frames repeat
// one boundary entry (the last of frame N is the first of frame N+1), so a
// receiver can stitch the advertised address range together. An empty list
// still emits a single first+last frame.
let mut end = total.min(start + 1);
while end < total && fits(start, end + 1) {
end += 1;
}

let link_status_frame = build_frame(start, end);

// The last frame is the tracked one; earlier chunks are fire-and-forget.
// They drain the single sender in admission order, so the last frame's
Expand Down Expand Up @@ -224,7 +247,11 @@ impl<P: RadioPhy, R: Runtime> ZigbeeStack<P, R> {

fn restart_link_status_period(&self) {
let now = self.core_now();
let period = self.tunables.link_status_period();
let jitter = self
.tunables
.link_status_jitter()
.mul_f32(crate::rng::random_f32::<R>());
let period = self.tunables.link_status_period().saturating_sub(jitter);

self.core().nib.link_status_debouncer.fired(now, period);
}
Expand Down
Loading