use core::{fmt::Debug, ops::RangeInclusive};
use bitfield_struct::bitfield;
#[bitfield(u16, conversion = false, debug = false)]
#[derive(PartialEq, Eq, Hash)]
pub struct AssociationID {
#[bits(14)]
internal_aid: u16,
#[bits(2, default = 0b11)]
padding: u8,
}
impl AssociationID {
pub const MIN_AID: u16 = 1;
pub const MAX_AID: u16 = 2007;
pub const VALID_AID_RANGE: RangeInclusive<u16> = Self::MIN_AID..=Self::MAX_AID;
pub const fn new_checked(aid: u16) -> Option<Self> {
let aid = aid & (!0xc000); if aid >= Self::MIN_AID && aid <= Self::MAX_AID {
Some(Self::new().with_internal_aid(aid))
} else {
None
}
}
#[doc(hidden)]
#[inline]
pub const fn new_unchecked(aid: u16) -> Self {
Self::new().with_internal_aid(aid)
}
pub const fn aid(&self) -> u16 {
self.internal_aid()
}
pub const fn into_bits(self) -> u16 {
self.0
}
}
impl Debug for AssociationID {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
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]
macro_rules! aid {
($aid:expr) => {
{
use ::ieee80211::common::AssociationID;
const AID: AssociationID = {
::core::assert!($aid != 0, "An AssociationID of zero is invalid.");
::core::assert!($aid <= AssociationID::MAX_AID, "An AssociationID greater than 2007 is invalid");
AssociationID::new_unchecked($aid)
};
AID
}
};
}