use scroll::{
ctx::{MeasureWith, TryFromCtx, TryIntoCtx},
Endian, Pread, Pwrite,
};
use super::{Element, ElementID};
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct BSSLoadElement {
pub station_count: u16,
pub channel_utilization: u8,
pub available_admission_capacity: u16,
}
impl MeasureWith<()> for BSSLoadElement {
fn measure_with(&self, _ctx: &()) -> usize {
5
}
}
impl TryFromCtx<'_> for BSSLoadElement {
type Error = scroll::Error;
fn try_from_ctx(from: &[u8], _ctx: ()) -> Result<(Self, usize), Self::Error> {
let mut offset = 0;
let station_count = from.gread_with(&mut offset, Endian::Little)?;
let channel_utilization = from.gread(&mut offset)?;
let available_admission_capacity = from.gread_with(&mut offset, Endian::Little)?;
Ok((
Self {
station_count,
channel_utilization,
available_admission_capacity,
},
offset,
))
}
}
impl TryIntoCtx for BSSLoadElement {
type Error = scroll::Error;
fn try_into_ctx(self, buf: &mut [u8], _ctx: ()) -> Result<usize, Self::Error> {
let mut offset = 0;
buf.gwrite_with(self.station_count, &mut offset, Endian::Little)?;
buf.gwrite(self.channel_utilization, &mut offset)?;
buf.gwrite_with(
self.available_admission_capacity,
&mut offset,
Endian::Little,
)?;
Ok(offset)
}
}
impl Element for BSSLoadElement {
const ELEMENT_ID: ElementID = ElementID::Id(0x0b);
type ReadType<'a> = BSSLoadElement;
}