Skip to content

Commit

Permalink
Merge pull request #15 from Frostie314159/defmt
Browse files Browse the repository at this point in the history
Implement rudimentary defmt support.
  • Loading branch information
Frostie314159 authored Aug 31, 2024
2 parents 7c29197 + a61dd31 commit 08fa956
Show file tree
Hide file tree
Showing 42 changed files with 469 additions and 159 deletions.
301 changes: 210 additions & 91 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ authors = ["Frostie314159"]
rust-version = "1.65.0"

[dependencies]
bitfield-struct = "0.6.0"
bitfield-struct = "0.8.0"
const_soft_float = { version = "0.1.4", features = ["no_std"] }
crc32fast = { version = "1.3.2", default-features = false }
defmt = { version = "0.3.8", optional = true }
hmac = { version = "0.12.1", optional = true }
mac-parser = "0.1.5"
mac-parser = { version = "0.1.6", features = ["defmt"] }
macro-bits = "0.1.4"
num = { version = "0.4.3", default-features = false }
pbkdf2 = { version = "0.12.2", optional = true }
scroll = { version = "0.12.0", default-features = false }
scroll = { version = "0.12.0", default-features = false, features = ["derive"] }
sha1 = { version = "0.10.6", default-features = false, optional = true }
tlv-rs = "0.2.3"

Expand All @@ -35,3 +36,4 @@ alloc = []
crypto = ["dep:pbkdf2", "dep:hmac", "dep:sha1"]
default = ["crypto"]
std = ["alloc", "scroll/std"]
defmt = ["dep:defmt"]
6 changes: 6 additions & 0 deletions src/common/aid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ impl Debug for AssociationID {
f.write_fmt(format_args!("{}", self.aid()))
}
}
#[cfg(feature = "defmt")]
impl defmt::Format for AssociationID {
fn format(&self, fmt: defmt::Formatter) {
defmt::write!(fmt, "{}", self.aid())
}
}
#[macro_export]
/// Generate a new [AssociationID], while performing all checks at compile-time.
///
Expand Down
1 change: 1 addition & 0 deletions src/common/auth_algo_num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use macro_bits::serializable_enum;

serializable_enum! {
#[non_exhaustive]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum IEEE80211AuthenticationAlgorithmNumber: u16 {
#[default]
Expand Down
2 changes: 1 addition & 1 deletion src/common/capabilities.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bitfield_struct::bitfield;

#[bitfield(u16)]
#[bitfield(u16, defmt = cfg(feature = "defmt"))]
#[derive(PartialEq, Eq, Hash)]
/// This bitfield contains the capabilities of the sender.
pub struct CapabilitiesInformation {
Expand Down
10 changes: 6 additions & 4 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub const TU: Duration = Duration::from_micros(1024);
pub const IEEE_OUI: [u8; 3] = [0x00, 0x0f, 0xac];
pub const WIFI_ALLIANCE_OUI: [u8; 3] = [0x50, 0x6f, 0x9a];

#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
/// The frame type of an IEEE 802.11 frame.
pub enum FrameType {
Expand Down Expand Up @@ -75,7 +76,7 @@ impl From<FrameType> for u16 {
}

/// These are the flags included in the frame control field.
#[bitfield(u8)]
#[bitfield(u8, defmt = cfg(feature = "defmt"))]
#[derive(PartialEq, Eq, Hash)]
pub struct FCFFlags {
/// This frame is going to the distribution system.
Expand All @@ -95,7 +96,7 @@ pub struct FCFFlags {
// TODO: Docs
pub order: bool,
}
#[bitfield(u16)]
#[bitfield(u16, defmt = cfg(feature = "defmt"))]
#[derive(PartialEq, Eq, Hash)]
/// This is the frame control field, which is at the beginning of every frame.
pub struct FrameControlField {
Expand All @@ -106,7 +107,7 @@ pub struct FrameControlField {
#[bits(8)]
pub flags: FCFFlags,
}
#[bitfield(u16)]
#[bitfield(u16, defmt = cfg(feature = "defmt"))]
#[derive(PartialEq, Eq, Hash)]
/// This is information about the sequence number and the potential fragment number.
pub struct SequenceControl {
Expand All @@ -116,8 +117,9 @@ pub struct SequenceControl {
pub sequence_number: u16,
}

/// An empty type, used for filling empty generics.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Hash)]
/// An empty type, used for filling empty generics.
pub struct Empty;
impl<'a> TryFromCtx<'a> for Empty {
type Error = scroll::Error;
Expand Down
22 changes: 22 additions & 0 deletions src/common/read_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ impl<'a, Ctx: Default + Copy, Type: TryFromCtx<'a, Ctx, Error = scroll::Error> +
f.debug_list().entries(*self).finish()
}
}
#[cfg(feature = "defmt")]
impl<
'a,
Ctx: Default + Copy,
Type: TryFromCtx<'a, Ctx, Error = scroll::Error> + defmt::Format + Copy,
> defmt::Format for ReadIterator<'a, Ctx, Type>
{
fn format(&self, fmt: defmt::Formatter) {
let mut iter = self.clone();
defmt::write!(fmt, "[");

if let Some(first) = iter.next() {
// We treat the first element differently, since the loop below prepends the comma to every element.
defmt::write!(fmt, "{}", first);
for next in iter {
defmt::write!(fmt, ", {}", next);
}
}

defmt::write!(fmt, "]");
}
}
impl<'a, Ctx: Default + Copy, Type: TryFromCtx<'a, Ctx, Error = scroll::Error>> Iterator
for ReadIterator<'a, Ctx, Type>
{
Expand Down
1 change: 1 addition & 0 deletions src/common/reason.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use macro_bits::serializable_enum;

serializable_enum! {
#[non_exhaustive]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
/// An IEEE 802.11 reason code used in certain management frames.
pub enum IEEE80211Reason: u16 {
Expand Down
1 change: 1 addition & 0 deletions src/common/status_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use macro_bits::serializable_enum;

serializable_enum! {
#[non_exhaustive]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
/// An IEEE 802.11 status code used in certain management frames.
pub enum IEEE80211StatusCode: u16 {
Expand Down
1 change: 1 addition & 0 deletions src/common/subtypes/control.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use macro_bits::serializable_enum;

serializable_enum! {
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
/// This is the subtype of a control frame.
///
Expand Down
2 changes: 2 additions & 0 deletions src/common/subtypes/data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use macro_bits::serializable_enum;

serializable_enum! {
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
/// The piggy-backed control frame of the data frame.
pub enum DataFrameCF: u8 {
Expand All @@ -13,6 +14,7 @@ serializable_enum! {
}

serializable_enum! {
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
/// The subtype of the data frame.
pub enum DataFrameSubtype: u8 {
Expand Down
1 change: 1 addition & 0 deletions src/common/subtypes/mgmt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use macro_bits::serializable_enum;

serializable_enum! {
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ManagementFrameSubtype: u8 {
AssociationRequest => 0b0000,
Expand Down
1 change: 1 addition & 0 deletions src/elements/bss_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use scroll::{

use super::{Element, ElementID};

#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
/// The BSS Load element contains information on the current STA population and traffic levels in the BSS.
pub struct BSSLoadElement {
Expand Down
1 change: 1 addition & 0 deletions src/elements/dsss_parameter_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use scroll::{

use super::{Element, ElementID};

#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
/// The DSSS Parameter Set element contains information to allow channel number identification for STAs.
pub struct DSSSParameterSetElement {
Expand Down
6 changes: 4 additions & 2 deletions src/elements/element_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ pub trait ChainElement {
fn append<T>(self, value: T) -> Self::Appended<T>;
}

#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Hash)]
/// This is the end of a chain.
///
/// Counterintuitively it's the point where you create a new chain.
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Hash)]
pub struct ElementChainEnd<Inner> {
/// The last element of the chain.
pub inner: Inner,
Expand Down Expand Up @@ -89,8 +90,9 @@ impl TryIntoCtx for ElementChainEnd<RawIEEE80211Element<'_>> {
}
}

/// A link in the element chain.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Hash)]
/// A link in the element chain.
pub struct ElementChainLink<Inner, Child: ChainElement> {
/// The current element.
pub inner: Inner,
Expand Down
17 changes: 12 additions & 5 deletions src/elements/ht/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::elements::{Element, ElementID};
use super::SupportedMCSSet;

serializable_enum! {
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
/// Spatial multiplexing power save mode.
pub enum SmPwSave: u8 {
Expand All @@ -20,7 +21,7 @@ serializable_enum! {
Disabled => 3
}
}
#[bitfield(u16)]
#[bitfield(u16, defmt = cfg(feature = "defmt"))]
#[derive(PartialEq, Eq, Hash)]
/// HT related capabilities info.
pub struct HTCapabilitiesInfo {
Expand Down Expand Up @@ -65,6 +66,7 @@ pub struct HTCapabilitiesInfo {
}

serializable_enum! {
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
/// The maximum length of an A-MPDU.
pub enum MAXAMpduLength: u8 {
Expand All @@ -81,6 +83,7 @@ serializable_enum! {
}

serializable_enum! {
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
/// Minimum time between the start of adjacent MPDUs in A-MPDU, measured in µs.
pub enum MpduDensity: u8 {
Expand All @@ -95,7 +98,7 @@ serializable_enum! {
Sixteen => 7
}
}
#[bitfield(u8)]
#[bitfield(u8, defmt = cfg(feature = "defmt"))]
#[derive(PartialEq, Eq, Hash)]
/// Parameters for A-MPDU operation.
pub struct AMpduParameters {
Expand All @@ -110,7 +113,7 @@ pub struct AMpduParameters {
#[bits(3)]
__: u8,
}
#[bitfield(u16)]
#[bitfield(u16, defmt = cfg(feature = "defmt"))]
#[derive(PartialEq, Eq, Hash)]
/// Extended HT capabilities
pub struct HTExtendedCapabilities {
Expand All @@ -127,6 +130,7 @@ pub struct HTExtendedCapabilities {
__: u8,
}
serializable_enum! {
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
/// The level of support for beamforming calibration.
pub enum BeamformingCalibration: u8 {
Expand All @@ -138,6 +142,7 @@ serializable_enum! {
}
}
serializable_enum! {
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
/// The level of support for beamforming feedback.
pub enum BeamformingFeedback: u8 {
Expand All @@ -149,6 +154,7 @@ serializable_enum! {
}
}
serializable_enum! {
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
/// The level of support for grouping.
pub enum GroupingCapability: u8 {
Expand All @@ -159,7 +165,7 @@ serializable_enum! {
GroupsOfTwoAndFour => 3
}
}
#[bitfield(u32)]
#[bitfield(u32, defmt = cfg(feature = "defmt"))]
#[derive(PartialEq, Eq, Hash)]
/// Capabilities related to transmit beamforming.
pub struct TransmitBeamformingCapabilities {
Expand Down Expand Up @@ -223,7 +229,7 @@ pub struct TransmitBeamformingCapabilities {
#[bits(3)]
__: u8,
}
#[bitfield(u8)]
#[bitfield(u8, defmt = cfg(feature = "defmt"))]
#[derive(PartialEq, Eq, Hash)]
/// The Antenna Selection capability of the STA.
pub struct ASELCapability {
Expand All @@ -243,6 +249,7 @@ pub struct ASELCapability {
pub transmit_sounding_ppdus_capable: bool,
pub reserved: bool,
}
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
/// The [HTCapabilitiesElement] contains information about the HT capbilities of the STA.
pub struct HTCapabilitiesElement {
Expand Down
3 changes: 2 additions & 1 deletion src/elements/ht/mcs_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use scroll::{
/// No Tx MCS set is defined | true | true | 0 | false
/// The Tx MCS set is defined to be equal to the Rx MCS set | true | false | 0 | false
/// The Tx MCS set may differ from the Rx MCS set | true | true | * | *
#[bitfield(u32)]
#[bitfield(u32, defmt = cfg(feature = "defmt"))]
#[derive(PartialEq, Eq, Hash)]
pub struct SupportedMCSSetFlags {
/// The highest supported data rate.
Expand Down Expand Up @@ -50,6 +50,7 @@ impl SupportedMCSSetFlags {
self.is_tx_mcs_undefined() && self.tx_rx_mcs_set_not_equal()
}
}
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
/// The MCS Set supported by the transmitter.
///
Expand Down
5 changes: 4 additions & 1 deletion src/elements/ht/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::elements::{Element, ElementID};
use super::SupportedMCSSet;

serializable_enum! {
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
/// The offset of the secondary channel from the primary.
///
Expand All @@ -33,6 +34,7 @@ serializable_enum! {
}

serializable_enum! {
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum HTProtectionMode: u8 {
#[default]
Expand All @@ -47,7 +49,7 @@ serializable_enum! {
}
}

#[bitfield(u64)]
#[bitfield(u64, defmt = cfg(feature = "defmt"))]
#[derive(PartialEq, Eq, Hash)]
/// Information about the operation of an HT-STA.
pub struct HTOperationInformation {
Expand Down Expand Up @@ -84,6 +86,7 @@ pub struct HTOperationInformation {
__: u64,
}

#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
/// The operation of HT STAs in the BSS is controlled by the HT Operation element.
pub struct HTOperationElement {
Expand Down
1 change: 1 addition & 0 deletions src/elements/ibss_parameter_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::common::TU;

use super::{Element, ElementID};

#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
/// The IBSS Parameter Set element contains the set of parameters necessary to support an IBSS.
pub struct IBSSParameterSetElement {
Expand Down
Loading

0 comments on commit 08fa956

Please sign in to comment.