ieee80211/elements/rates/
mod.rs

1//! This module contains support for the Supported- and Extended Supported Rates elements.
2
3mod supported_rates;
4
5use bitfield_struct::bitfield;
6pub use supported_rates::*;
7
8mod extended_supported_rates;
9pub use extended_supported_rates::*;
10
11mod rate_iter;
12pub use rate_iter::RatesReadIterator;
13
14#[bitfield(u8, defmt = cfg(feature = "defmt"))]
15#[derive(PartialEq, Eq, Hash)]
16/// Data rate encoded as specified in IEEE 802.11.
17pub struct EncodedRate {
18    #[bits(7)]
19    /// The value of the data rate.
20    ///
21    /// The formular is `rate * 500` to get kbps. Use [EncodedRate::rate_in_kbps] to calculate this.
22    pub rate: u8,
23
24    /// Is the data rate IEEE 802.11b.
25    pub is_b: bool,
26}
27
28impl EncodedRate {
29    #[inline]
30    /// Returns the data rate in kbps.
31    pub const fn rate_in_kbps(&self) -> usize {
32        self.rate() as usize * 500
33    }
34    #[inline]
35    /// Creates a rate from it's speed in kbps.
36    pub const fn from_rate_in_kbps(rate: usize, is_b: bool) -> Self {
37        Self::new().with_rate((rate / 500) as u8).with_is_b(is_b)
38    }
39}
40#[cfg(feature = "alloc")]
41impl ::alloc::fmt::Display for EncodedRate {
42    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
43        f.write_fmt(format_args!(
44            "{}Mbit/s {}",
45            self.rate() as f32 / 2f32,
46            if self.is_b() { " (B)" } else { "" }
47        ))
48    }
49}