use super::header::Address;
use super::DecodeError;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum FrameType {
Beacon = 0b000,
Data = 0b001,
Acknowledgement = 0b010,
MacCommand = 0b011,
Multipurpose = 0b101,
FragOrFragAck = 0b110,
Extended = 0b111,
}
impl FrameType {
pub fn from_bits(bits: u8) -> Option<Self> {
match bits {
0b000 => Some(FrameType::Beacon),
0b001 => Some(FrameType::Data),
0b010 => Some(FrameType::Acknowledgement),
0b011 => Some(FrameType::MacCommand),
0b101 => Some(FrameType::Multipurpose),
0b110 => Some(FrameType::FragOrFragAck),
0b111 => Some(FrameType::Extended),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum FrameVersion {
Ieee802154_2003 = 0b00,
Ieee802154_2006 = 0b01,
Ieee802154 = 0b10,
}
impl FrameVersion {
pub fn from_bits(bits: u8) -> Option<Self> {
match bits {
0b00 => Some(FrameVersion::Ieee802154_2003),
0b01 => Some(FrameVersion::Ieee802154_2006),
0b10 => Some(FrameVersion::Ieee802154),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum AddressMode {
None = 0b00,
Short = 0b10,
Extended = 0b11,
}
impl From<Option<Address>> for AddressMode {
fn from(opt_addr: Option<Address>) -> Self {
match opt_addr {
Some(Address::Short(..)) => Self::Short,
Some(Address::Extended(..)) => Self::Extended,
None => Self::None,
}
}
}
impl From<Address> for AddressMode {
fn from(addr: Address) -> Self {
match addr {
Address::Short(_, _) => Self::Short,
Address::Extended(_, _) => Self::Extended,
}
}
}
impl AddressMode {
pub fn from_bits(bits: u8) -> Result<Self, DecodeError> {
match bits {
0b00 => Ok(AddressMode::None),
0b10 => Ok(AddressMode::Short),
0b11 => Ok(AddressMode::Extended),
_ => Err(DecodeError::InvalidAddressMode(bits)),
}
}
}
pub mod offset {
pub const FRAME_TYPE: u16 = 0;
pub const SECURITY: u16 = 3;
pub const PENDING: u16 = 4;
pub const ACK: u16 = 5;
pub const PAN_ID_COMPRESS: u16 = 6;
pub const SEQ_NO_SUPPRESS: u16 = 8;
pub const IE_PRESENT: u16 = 9;
pub const DEST_ADDR_MODE: u16 = 10;
pub const VERSION: u16 = 12;
pub const SRC_ADDR_MODE: u16 = 14u16;
}
pub mod mask {
pub const FRAME_TYPE: u16 = 0x0007;
pub const SECURITY: u16 = 0x0008;
pub const PENDING: u16 = 0x0010;
pub const ACK: u16 = 0x0020;
pub const PAN_ID_COMPRESS: u16 = 0x0040;
pub const SEQ_NO_SUPPRESS: u16 = 0x0100;
pub const IE_PRESENT: u16 = 0x0200;
pub const DEST_ADDR_MODE: u16 = 0x0C00;
pub const VERSION: u16 = 0x3000;
pub const SRC_ADDR_MODE: u16 = 0xC000;
}