diff --git a/crates/ziggurat-driver/src/zigbee_stack/route.rs b/crates/ziggurat-driver/src/zigbee_stack/route.rs index 3dbcc177..ac41edc8 100644 --- a/crates/ziggurat-driver/src/zigbee_stack/route.rs +++ b/crates/ziggurat-driver/src/zigbee_stack/route.rs @@ -324,13 +324,28 @@ impl ZigbeeStack { } pub async fn periodic_many_to_one_route_request_task(&self) { - // Receivers drop route requests from senders with a zero outgoing cost, so - // the first advertisement waits until link status exchanges establish a - // neighbor link, bounded by a fixed ceiling in case the network is silent - let startup_deadline = self.core_now() + 2 * self.tunables.link_status_period(); - + // Receivers drop route requests from senders with a zero outgoing cost, and the + // empty link status sent at startup has just zeroed it on every neighbor. Listen + // for one link status period, so every router neighbor gets to send its link + // status, then tell them all their side with one of our own before the first + // advertisement. A router that answers at once is not enough: the others would + // still drop it. Without any router heard by the ceiling, advertise anyway, in + // case the network is silent; costs kept from before a restart do not count. + let since = self.core_now(); + self.sleep_until_core(since + self.tunables.link_status_period()) + .await; + + let startup_deadline = since + 2 * self.tunables.link_status_period(); loop { - if self.core().nib.neighbors.any_live_router_link() { + if self + .core() + .nib + .neighbors + .any_router_link_confirmed_since(since) + { + if let Ok(link_status) = self.send_link_status_broadcast(false) { + let _ = link_status.delivered().await; + } break; } diff --git a/crates/ziggurat-zigbee/src/nwk/neighbors.rs b/crates/ziggurat-zigbee/src/nwk/neighbors.rs index 631e8f29..fb75c8c5 100644 --- a/crates/ziggurat-zigbee/src/nwk/neighbors.rs +++ b/crates/ziggurat-zigbee/src/nwk/neighbors.rs @@ -579,12 +579,15 @@ impl Neighbors { } } - /// Whether any router neighbor has established a bidirectional link with us, - /// i.e. a received link status lists us with a nonzero cost. - pub fn any_live_router_link(&self) -> bool { - self.table - .values() - .any(|entry| entry.device_type == NwkDeviceType::Router && entry.outgoing_cost > 0) + /// Whether a router neighbor sent a link status after `since` that lists us with a + /// nonzero cost. Costs from before (a table kept across a restart) do not count: + /// the empty link status a restart sends has zeroed the neighbors' side. + pub fn any_router_link_confirmed_since(&self, since: Instant) -> bool { + self.table.values().any(|entry| { + entry.device_type == NwkDeviceType::Router + && entry.outgoing_cost > 0 + && entry.last_link_status_timestamp > since + }) } /// The earliest end device child keepalive deadline, for scheduling the eviction @@ -1036,6 +1039,53 @@ mod test { ); } + const OWN: Nwk = Nwk(0x0000); + const ROUTER_NWK: Nwk = Nwk(0x4965); + + fn link_status_from_router(neighbors: &mut Neighbors, listing: Nwk, at: Instant) { + let cmd = NwkLinkStatusCommand { + is_first_frame: true, + is_last_frame: true, + link_statuses: vec![NwkLinkStatus { + address: listing, + incoming_cost: 1, + outgoing_cost: 1, + }], + }; + neighbors.on_link_status( + OWN, + Eui64::from_hex("20:6e:f1:ff:fe:0a:d8:8c"), + ROUTER_NWK, + 255, + &cmd, + at, + ); + } + + #[test] + fn test_router_link_heard_before_the_instant_is_not_confirmed() { + let mut neighbors = Neighbors::default(); + link_status_from_router(&mut neighbors, OWN, Instant::from_micros(0)); + + assert!(!neighbors.any_router_link_confirmed_since(Instant::from_micros(1))); + } + + #[test] + fn test_router_link_status_listing_us_after_the_instant_confirms_the_link() { + let mut neighbors = Neighbors::default(); + link_status_from_router(&mut neighbors, OWN, Instant::from_micros(2)); + + assert!(neighbors.any_router_link_confirmed_since(Instant::from_micros(1))); + } + + #[test] + fn test_router_link_status_not_listing_us_does_not_confirm_the_link() { + let mut neighbors = Neighbors::default(); + link_status_from_router(&mut neighbors, Nwk(0x1111), Instant::from_micros(2)); + + assert!(!neighbors.any_router_link_confirmed_since(Instant::from_micros(1))); + } + #[test] fn test_only_end_device_children_time_out() { let mut neighbors = neighbors_with(NwkDeviceType::EndDevice);