1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//! Ember duty cycle state and limits.
use le_stream::{FromLeStream, ToLeStream};
use num_derive::FromPrimitive;
/// Ember duty cycle state.
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, FromPrimitive)]
#[repr(u8)]
pub enum State {
/// No Duty cycle tracking or metrics are taking place.
TrackingOff = 0x00,
/// Duty Cycle is tracked and has not exceeded any thresholds.
LbtNormal = 0x01,
/// We have exceeded the limited threshold of our total duty cycle allotment.
LbtLimitedThresholdReached = 0x02,
/// We have exceeded the critical threshold of our total duty cycle allotment.
LbtCriticalThresholdReached = 0x03,
/// We have reached the suspend limit and are blocking all outbound transmissions.
LbtSuspendLimitReached = 0x04,
}
impl From<State> for u8 {
fn from(state: State) -> Self {
state as Self
}
}
/// A structure containing duty cycle limit configurations.
///
/// All limits are absolute, and are required to be as follows:
///
/// `susp_limit` > `crit_thresh` > `limit_thresh`
///
/// For example:
///
/// `susp_limit = 250` (2.5%), `crit_thresh = 180` (1.8%), `limit_thresh = 100` (1.00%).
///
/// See [EmberDutyCycleLimits Struct Reference](https://docs.silabs.com/zigbee/6.6/em35x/structEmberDutyCycleLimits)
/// for more information.
#[derive(Clone, Debug, Eq, PartialEq, FromLeStream, ToLeStream)]
pub struct Limits {
crit_thresh: u16,
limit_thresh: u16,
susp_limit: u16,
}
impl Limits {
/// Attempt to create a new duty cycle limit configuration, checking the limits.
#[must_use]
pub const fn try_new(crit_thresh: u16, limit_thresh: u16, susp_limit: u16) -> Option<Self> {
if susp_limit > crit_thresh && crit_thresh > limit_thresh {
#[expect(unsafe_code)]
// SAFETY: We checked the limit constraints in the line above.
Some(unsafe { Self::new_unchecked(crit_thresh, limit_thresh, susp_limit) })
} else {
None
}
}
/// Create a new duty cycle limit configuration without checking the limits.
///
/// # Safety
/// If the limits are not as follows: `susp_limit` > `crit_thresh` > `limit_thresh`,
/// the limits will cause undefined behaviour (UB).
#[expect(unsafe_code)]
#[must_use]
pub const unsafe fn new_unchecked(
crit_thresh: u16,
limit_thresh: u16,
susp_limit: u16,
) -> Self {
Self {
crit_thresh,
limit_thresh,
susp_limit,
}
}
/// Return the critical threshold in % * 100.
#[must_use]
pub const fn crit_thresh(&self) -> u16 {
self.crit_thresh
}
/// Return the limited threshold in % * 100.
#[must_use]
pub const fn limit_thresh(&self) -> u16 {
self.limit_thresh
}
/// Return the suspended limit (LBT) in % * 100.
#[must_use]
pub const fn susp_limit(&self) -> u16 {
self.susp_limit
}
}