From b8b0acd3316e4b2ecdab1417a6e33bdba0bd32a0 Mon Sep 17 00:00:00 2001 From: zigpy-review-bot <286747149+zigpy-review-bot@users.noreply.github.com> Date: Mon, 23 Nov 2026 02:58:22 +0100 Subject: [PATCH 1/2] Count the first link status from a new router as one LQA sample The receive path buffers every frame's LQI before the frame is handled, so the first link status from a router without a neighbor entry already sits in the buffered samples. `on_link_status` then pushed the same LQI again, giving that one frame two of the window's three samples: a single outlier decided the median until two more frames came in. Push the frame's LQI only when nothing was buffered for the sender (a caller that did not go through the receive path). --- crates/ziggurat-zigbee/src/nwk/neighbors.rs | 44 ++++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/crates/ziggurat-zigbee/src/nwk/neighbors.rs b/crates/ziggurat-zigbee/src/nwk/neighbors.rs index cc09af65..679e5cd4 100644 --- a/crates/ziggurat-zigbee/src/nwk/neighbors.rs +++ b/crates/ziggurat-zigbee/src/nwk/neighbors.rs @@ -754,7 +754,9 @@ impl Neighbors { } /// Digest a received link status command, updating the sender's entry (creating a - /// sibling entry for previously unseen routers). + /// sibling entry for previously unseen routers). The frame's `lqi` is expected to + /// have gone through [`Self::record_lqa`] already, as the receive path does: it is + /// recorded here only for a new entry with no buffered samples. #[allow(clippy::too_many_arguments)] pub fn on_link_status( &mut self, @@ -850,10 +852,15 @@ impl Neighbors { security_timer: 0, }; - // Seed with the samples buffered before the entry existed plus the current - // one (the receive path could not record them without an entry). - entry.lqas = buffered_lqas; - entry.lqas.push(lqi); + // Seed with the samples buffered before the entry existed. The receive path + // has already buffered this frame's LQI, so it is only added here when + // nothing was buffered: counting it twice would give one frame two of the + // window's samples. + if buffered_lqas.is_empty() { + entry.lqas.push(lqi); + } else { + entry.lqas = buffered_lqas; + } entry }); @@ -1286,6 +1293,33 @@ mod test { assert_eq!(incoming_cost(&neighbors), lqi_to_link_cost(40)); } + #[test] + fn test_first_link_status_counts_as_one_sample() { + let router = Eui64::from_hex("aa:bb:cc:dd:ee:ff:00:11"); + let (own, router_nwk) = (Nwk(0x0000), Nwk(0x1111)); + let now = Instant::from_micros(0); + let mut neighbors = Neighbors::default(); + + // The receive path buffers the frame's LQI before the link status is digested + neighbors.record_lqa(router_nwk, 40); + neighbors.on_link_status( + own, + router, + router_nwk, + 40, + &link_status_listing(own), + now, + ACTIVITY, + ); + + // The next frame makes two samples, whose average is the link quality + neighbors.record_lqa(router_nwk, 200); + assert_eq!( + neighbors.link(router_nwk).unwrap().incoming_cost, + lqi_to_link_cost(120) + ); + } + #[test] fn test_restored_child_without_samples_has_no_link_cost() { let neighbors = neighbors_with(NwkDeviceType::Router); From 19bd7f5be4d3959416006faba90ce6cfcedfbe13 Mon Sep 17 00:00:00 2001 From: zigpy-review-bot <286747149+zigpy-review-bot@users.noreply.github.com> Date: Mon, 23 Nov 2026 04:19:51 +0100 Subject: [PATCH 2/2] Count the frame's LQI on a new entry when another neighbor holds its address The receive path buffers a frame's LQI only while no neighbor holds the sender's address. In an address conflict the sample goes to the neighbor that holds it, so a non-empty buffer for that address predates the frame and the new entry was seeded without the frame's own sample. --- crates/ziggurat-zigbee/src/nwk/neighbors.rs | 63 ++++++++++++++++++--- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/crates/ziggurat-zigbee/src/nwk/neighbors.rs b/crates/ziggurat-zigbee/src/nwk/neighbors.rs index 679e5cd4..113b5976 100644 --- a/crates/ziggurat-zigbee/src/nwk/neighbors.rs +++ b/crates/ziggurat-zigbee/src/nwk/neighbors.rs @@ -756,7 +756,8 @@ impl Neighbors { /// Digest a received link status command, updating the sender's entry (creating a /// sibling entry for previously unseen routers). The frame's `lqi` is expected to /// have gone through [`Self::record_lqa`] already, as the receive path does: it is - /// recorded here only for a new entry with no buffered samples. + /// recorded here only for a new entry whose sample `record_lqa` did not buffer + /// (nothing buffered for the address, or another neighbor holds it). #[allow(clippy::too_many_arguments)] pub fn on_link_status( &mut self, @@ -787,6 +788,11 @@ impl Neighbors { // Fold any LQI samples buffered for this address before its entry existed. let mut buffered_lqas = self.pending_lqas.remove(&source_nwk).unwrap_or_default(); + // The receive path buffers the frame's LQI only while no neighbor holds the + // address. When one does, the sample went to that neighbor's entry, and what + // is buffered predates it. + let frame_lqi_buffered = !buffered_lqas.is_empty() && !self.has_network_address(source_nwk); + // Spec 3.3.1.7: a frame carrying its source IEEE address, as every link status // does (3.4.8.2), SHOULD be checked against the neighbor table. A router that // changed its address (address conflict, rejoin) and whose Device_annce we @@ -852,14 +858,13 @@ impl Neighbors { security_timer: 0, }; - // Seed with the samples buffered before the entry existed. The receive path - // has already buffered this frame's LQI, so it is only added here when - // nothing was buffered: counting it twice would give one frame two of the - // window's samples. - if buffered_lqas.is_empty() { + // Seed with the samples buffered before the entry existed. This frame's LQI + // is added only when the receive path did not buffer it: counting it twice + // would give one frame two of the window's samples. + entry.lqas = buffered_lqas; + + if !frame_lqi_buffered { entry.lqas.push(lqi); - } else { - entry.lqas = buffered_lqas; } entry @@ -1320,6 +1325,48 @@ mod test { ); } + #[test] + fn test_first_link_status_from_a_held_address_counts_its_sample() { + let router = Eui64::from_hex("aa:bb:cc:dd:ee:ff:00:11"); + let other = Eui64::from_hex("aa:bb:cc:dd:ee:ff:00:22"); + let (own, contested) = (Nwk(0x0000), Nwk(0x2222)); + let now = Instant::from_micros(0); + let mut neighbors = Neighbors::default(); + + // A sample buffered for the address before another device took it as our child + neighbors.record_lqa(contested, 40); + neighbors.upsert_child( + ChildDescriptor { + eui64: other, + network_address: contested, + device_type: NwkDeviceType::Router, + rx_on_when_idle: true, + device_timeout: None, + relationship: Relationship::Child, + }, + now, + ACTIVITY, + ); + + // A new router's link status from that address: the receive path records the + // frame's LQI on the neighbor holding the address, not in the buffer + neighbors.record_lqa(contested, 200); + neighbors.on_link_status( + own, + router, + contested, + 200, + &link_status_listing(own), + now, + ACTIVITY, + ); + + assert_eq!( + neighbors.table.get(&router).unwrap().incoming_link_cost(), + lqi_to_link_cost(120) + ); + } + #[test] fn test_restored_child_without_samples_has_no_link_cost() { let neighbors = neighbors_with(NwkDeviceType::Router);