use macro_bits::{bit, bitfield, serializable_enum};
serializable_enum! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ChannelEncoding : u8 {
Simple => 0x00,
Legacy => 0x01,
OpClass => 0x03
}
}
impl ChannelEncoding {
#[inline]
pub const fn size(&self) -> u8 {
match self {
Self::Simple => 1,
_ => 2,
}
}
}
serializable_enum! {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum SupportChannel : u8 {
Lower => 0x01,
Upper => 0x02,
#[default]
Primary => 0x03
}
}
serializable_enum! {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ChannelBandwidth : u8{
#[default]
TwentyMHz => 0x01,
FourtyMHz => 0x02,
EightyMHz => 0x03
}
}
serializable_enum! {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Band : u8 {
#[default]
TwoPointFourGHz => 0x02,
FiveGHz => 0x01
}
}
bitfield! {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct LegacyFlags : u8 {
pub support_channel: SupportChannel => bit!(0, 1),
pub channel_bandwidth: ChannelBandwidth => bit!(2, 3),
pub band: Band => bit!(4, 5)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum Channel {
Simple { channel: u8 },
Legacy { flags: LegacyFlags, channel: u8 },
OpClass { channel: u8, opclass: u8 },
}
impl Channel {
#[inline]
pub const fn channel_encoding(&self) -> ChannelEncoding {
match self {
Self::Simple { .. } => ChannelEncoding::Simple,
Self::Legacy { .. } => ChannelEncoding::Legacy,
Self::OpClass { .. } => ChannelEncoding::OpClass,
}
}
#[inline]
pub const fn channel(&self) -> u8 {
match self {
Self::Simple { channel } => *channel,
Self::Legacy { flags, channel } => match flags.support_channel {
SupportChannel::Lower => *channel - 2,
SupportChannel::Upper => *channel + 2,
SupportChannel::Primary => *channel,
_ => *channel,
},
Self::OpClass { channel, .. } => *channel,
}
}
}