use heterob::{bit_numbering::Lsb, endianness::Le, Seq, P3, P5};
use super::{ExtendedCapabilityDataError, ExtendedCapabilityHeaderPlaceholder};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LnRequester {
pub lnr_capability: LnrCapability,
pub lnr_control: LnrControl,
}
impl LnRequester {
pub const SIZE: usize = 0x8;
}
impl From<[u8; Self::SIZE]> for LnRequester {
fn from(bytes: [u8; Self::SIZE]) -> Self {
let Le((ExtendedCapabilityHeaderPlaceholder, lnr_capability, lnr_control)) =
P3(bytes).into();
Self {
lnr_capability: From::<u16>::from(lnr_capability),
lnr_control: From::<u16>::from(lnr_control),
}
}
}
impl TryFrom<&[u8]> for LnRequester {
type Error = ExtendedCapabilityDataError;
fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
let Seq { head, .. } = slice.try_into().map_err(|_| ExtendedCapabilityDataError {
name: "LN Requester",
size: Self::SIZE,
})?;
Ok(From::<[u8; Self::SIZE]>::from(head))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LnrCapability {
pub lnr_64_supported: bool,
pub lnr_128_supported: bool,
pub lnr_registration_max: u8,
}
impl From<u16> for LnrCapability {
fn from(word: u16) -> Self {
let Lsb((lnr_64_supported, lnr_128_supported, (), lnr_registration_max, ())) =
P5::<_, 1, 1, 6, 5, 3>(word).into();
Self {
lnr_64_supported,
lnr_128_supported,
lnr_registration_max,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LnrControl {
pub lnr_enable: bool,
pub lnr_cls: bool,
pub lnr_registration_limit: u8,
}
impl From<u16> for LnrControl {
fn from(word: u16) -> Self {
let Lsb((lnr_enable, lnr_cls, (), lnr_registration_limit, ())) =
P5::<_, 1, 1, 6, 5, 3>(word).into();
Self {
lnr_enable,
lnr_cls,
lnr_registration_limit,
}
}
}