#[cfg(not(feature = "std"))]
use core::{
clone::Clone,
cmp::{Eq, PartialEq},
fmt::Debug,
prelude::rust_2021::derive,
result::Result::Ok,
};
use byteorder_cursor::{Cursor, LittleEndian};
use super::{Result, SmaSerde};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SmaInvCounter {
pub fragment_id: u16,
pub packet_id: u16,
pub first_fragment: bool,
}
impl Default for SmaInvCounter {
fn default() -> Self {
Self {
fragment_id: 0,
packet_id: 0,
first_fragment: true,
}
}
}
impl SmaInvCounter {
pub const LENGTH: usize = 4;
pub const FIRST_FRAGMENT_BIT: u16 = 0x8000;
}
impl SmaSerde for SmaInvCounter {
fn serialize(&self, buffer: &mut Cursor<&mut [u8]>) -> Result<()> {
buffer.check_remaining(Self::LENGTH)?;
let packet_id = if self.first_fragment {
self.packet_id | Self::FIRST_FRAGMENT_BIT
} else {
self.packet_id
};
buffer.write_u16::<LittleEndian>(self.fragment_id);
buffer.write_u16::<LittleEndian>(packet_id);
Ok(())
}
fn deserialize(buffer: &mut Cursor<&[u8]>) -> Result<Self> {
buffer.check_remaining(Self::LENGTH)?;
let fragment_id = buffer.read_u16::<LittleEndian>();
let raw_packet_id = buffer.read_u16::<LittleEndian>();
let (packet_id, first_fragment) =
if (raw_packet_id & Self::FIRST_FRAGMENT_BIT) != 0 {
(raw_packet_id & !Self::FIRST_FRAGMENT_BIT, true)
} else {
(raw_packet_id, false)
};
Ok(Self {
fragment_id,
packet_id,
first_fragment,
})
}
}