use crate::{
eth::{EtherType, EthernetError},
getter_be,
};
use core::mem;
use num_traits::FromPrimitive as _;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "wincode", derive(wincode::SchemaRead, wincode::SchemaWrite))]
#[cfg_attr(feature = "wincode", wincode(assert_zero_copy))]
pub struct VlanHdr {
pub tci: [u8; 2],
pub ether_type: u16,
}
impl VlanHdr {
pub const LEN: usize = mem::size_of::<VlanHdr>();
#[inline]
fn tci(&self) -> u16 {
unsafe { getter_be!(self, tci, u16) }
}
#[inline]
pub fn pcp(&self) -> u8 {
(self.tci() >> 13) as u8
}
#[inline]
pub fn dei(&self) -> u8 {
((self.tci() >> 12) & 1) as u8
}
#[inline]
pub fn vid(&self) -> u16 {
self.tci() & 0xFFF
}
#[inline]
pub fn ether_type(&self) -> Result<EtherType, EthernetError> {
EtherType::from_u16(self.ether_type).ok_or(EthernetError::InvalidEtherType(self.ether_type))
}
}