dot15d4_frame/repr/ie/
nested.rsuse super::super::super::{
ChannelHopping, NestedInformationElement, NestedSubId, NestedSubIdLong, NestedSubIdShort,
TschSlotframeAndLink, TschSynchronization, TschTimeslot,
};
use super::super::super::{Error, Result};
#[derive(Debug)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub enum NestedInformationElementRepr {
TschSynchronization(TschSynchronizationRepr),
TschTimeslot(TschTimeslotRepr),
TschSlotframeAndLink(TschSlotframeAndLinkRepr),
ChannelHopping(ChannelHoppingRepr),
}
impl NestedInformationElementRepr {
pub fn parse(ie: &NestedInformationElement<&[u8]>) -> Result<Self> {
Ok(match ie.sub_id() {
NestedSubId::Short(NestedSubIdShort::TschSynchronization) => Self::TschSynchronization(
TschSynchronizationRepr::parse(&TschSynchronization::new(ie.content())?),
),
NestedSubId::Short(NestedSubIdShort::TschTimeslot) => {
Self::TschTimeslot(TschTimeslotRepr::parse(&TschTimeslot::new(ie.content())?))
}
NestedSubId::Short(NestedSubIdShort::TschSlotframeAndLink) => {
Self::TschSlotframeAndLink(TschSlotframeAndLinkRepr::parse(
&TschSlotframeAndLink::new(ie.content())?,
))
}
NestedSubId::Long(NestedSubIdLong::ChannelHopping) => Self::ChannelHopping(
ChannelHoppingRepr::parse(&ChannelHopping::new(ie.content())?),
),
_ => return Err(Error),
})
}
pub fn buffer_len(&self) -> usize {
2 + self.inner_len()
}
pub fn inner_len(&self) -> usize {
match self {
Self::TschSynchronization(repr) => repr.buffer_len(),
Self::TschTimeslot(repr) => repr.buffer_len(),
Self::TschSlotframeAndLink(repr) => repr.buffer_len(),
Self::ChannelHopping(repr) => repr.buffer_len(),
}
}
pub fn emit(&self, w: &mut NestedInformationElement<&mut [u8]>) {
let id = NestedSubId::from(self);
w.clear();
w.set_length(self.inner_len() as u16, id);
w.set_sub_id(id);
match self {
Self::TschSynchronization(repr) => {
repr.emit(&mut TschSynchronization::new_unchecked(w.content_mut()))
}
Self::TschTimeslot(repr) => {
repr.emit(&mut TschTimeslot::new_unchecked(w.content_mut()))
}
Self::TschSlotframeAndLink(repr) => {
repr.emit(&mut TschSlotframeAndLink::new_unchecked(w.content_mut()))
}
Self::ChannelHopping(repr) => {
repr.emit(&mut ChannelHopping::new_unchecked(w.content_mut()))
}
}
}
}
impl From<&NestedInformationElementRepr> for NestedSubId {
fn from(value: &NestedInformationElementRepr) -> Self {
match value {
NestedInformationElementRepr::TschSynchronization(_) => {
NestedSubId::Short(NestedSubIdShort::TschSynchronization)
}
NestedInformationElementRepr::TschTimeslot(_) => {
NestedSubId::Short(NestedSubIdShort::TschTimeslot)
}
NestedInformationElementRepr::TschSlotframeAndLink(_) => {
NestedSubId::Short(NestedSubIdShort::TschSlotframeAndLink)
}
NestedInformationElementRepr::ChannelHopping(_) => {
NestedSubId::Long(NestedSubIdLong::ChannelHopping)
}
}
}
}
#[derive(Debug)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct TschSynchronizationRepr {
pub absolute_slot_number: u64,
pub join_metric: u8,
}
impl TschSynchronizationRepr {
pub fn parse(ie: &TschSynchronization<&[u8]>) -> Self {
Self {
absolute_slot_number: ie.absolute_slot_number(),
join_metric: ie.join_metric(),
}
}
pub const fn buffer_len(&self) -> usize {
6
}
pub fn emit(&self, ie: &mut TschSynchronization<&mut [u8]>) {
ie.set_absolute_slot_number(self.absolute_slot_number);
ie.set_join_metric(self.join_metric);
}
}
#[derive(Debug)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct TschSlotframeAndLinkRepr {
pub number_of_slot_frames: u8,
}
impl TschSlotframeAndLinkRepr {
pub fn parse(ie: &TschSlotframeAndLink<&[u8]>) -> Self {
Self {
number_of_slot_frames: ie.number_of_slot_frames(),
}
}
pub fn buffer_len(&self) -> usize {
1
}
pub fn emit(&self, ie: &mut TschSlotframeAndLink<&mut [u8]>) {
ie.set_number_of_slot_frames(self.number_of_slot_frames);
}
}
#[derive(Debug)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct TschTimeslotRepr {
pub id: u8,
}
impl TschTimeslotRepr {
pub fn parse(ie: &TschTimeslot<&[u8]>) -> Self {
Self { id: ie.id() }
}
pub fn buffer_len(&self) -> usize {
1
}
pub fn emit(&self, ie: &mut TschTimeslot<&mut [u8]>) {
ie.set_time_slot_id(self.id);
}
}
#[derive(Debug)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct ChannelHoppingRepr {
pub hopping_sequence_id: u8,
}
impl ChannelHoppingRepr {
pub fn parse(ie: &ChannelHopping<&[u8]>) -> Self {
Self {
hopping_sequence_id: ie.hopping_sequence_id(),
}
}
pub fn buffer_len(&self) -> usize {
1
}
pub fn emit(&self, ie: &mut ChannelHopping<&mut [u8]>) {
ie.set_hopping_sequence_id(self.hopping_sequence_id);
}
}