Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 29 additions & 3 deletions crates/ziggurat-zigbee/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::fmt;
use core::marker::PhantomData;
use core::ops::RangeBounds;
use core::sync::atomic::Ordering::Relaxed;
use core::sync::atomic::{AtomicBool, AtomicU8, AtomicU32, AtomicUsize};
use core::time::Duration;
Expand Down Expand Up @@ -218,8 +219,12 @@ impl TunableValue for EndDeviceTimeout {
/// Defines [`Tunables`] once: the struct, its defaults, the typed getters, and the
/// by-field-name `set` used by the `SetTunable` protocol command all come from this
/// single field list.
///
/// A field of an ordered type may end in `; valid <range>`: `set` then rejects a value
/// outside that range, for values the stack cannot run with at all (not every unwise
/// one).
macro_rules! tunables {
($($(#[$attr:meta])* $name:ident: $ty:ty = $default:expr,)*) => {
($($(#[$attr:meta])* $name:ident: $ty:ty = $default:expr $(; valid $valid:expr)?,)*) => {
/// Protocol parameters and policy knobs, initialized to spec-derived defaults.
///
/// Unlike the module-level constants above, every field here is tweakable at
Expand Down Expand Up @@ -253,6 +258,11 @@ macro_rules! tunables {
$(stringify!($name) => {
let value = <$ty as TunableValue>::decode(raw)
.ok_or(TunableError::InvalidValue)?;
$(
if !RangeBounds::<$ty>::contains(&$valid, &value) {
return Err(TunableError::InvalidValue);
}
)?
<$ty as TunableValue>::store(value, &self.$name);
})*
_ => return Err(TunableError::UnknownName),
Expand Down Expand Up @@ -312,8 +322,9 @@ tunables! {
/// many-to-one route request.
mtorr_delivery_failure_threshold: u8 = 1,

/// The time between link status command frames.
link_status_period: Duration = Duration::from_secs(15),
/// The time between link status command frames. Activity decay divides by it, so
/// it cannot be zero.
link_status_period: Duration = Duration::from_secs(15); valid Duration::from_micros(1)..,

/// `nwkcMinRouterBootstrapJitter`: the shortest delay before a gratuitous link
/// status.
Expand Down Expand Up @@ -452,4 +463,19 @@ mod test {
);
assert!(tunables.hack_beacon_spam_duration().is_zero());
}

#[test]
fn test_set_rejects_value_outside_valid_range() {
let tunables = Tunables::default();

// A zero link status period: rejected, value unchanged.
assert_eq!(
tunables.set("link_status_period", 0),
Err(TunableError::InvalidValue)
);
assert_eq!(tunables.link_status_period(), Duration::from_secs(15));

tunables.set("link_status_period", 1).unwrap();
assert_eq!(tunables.link_status_period(), Duration::from_micros(1));
}
}
3 changes: 3 additions & 0 deletions crates/ziggurat-zigbee/src/nwk/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ pub struct ActivityParams {
/// The decay steps owed for the time from `*decayed_at` to `now`, at one count per
/// `period`. `*decayed_at` moves forward by the steps returned, so the rest of the time
/// counts toward the next call.
///
/// `period` must be at least one microsecond: the `link_status_period` tunable it
/// comes from rejects anything shorter.
pub fn take_decay_steps(decayed_at: &mut Instant, now: Instant, period: Duration) -> u16 {
let steps_per_count = u64::from(STEPS_PER_COUNT);
let period = u64::try_from(period.as_micros()).unwrap();
Expand Down