use core::fmt;
use crate::error::Error;
use crate::im::{AttrPath, DataVersionFilter, EventFilter, EventPath, IM_REVISION};
use crate::tlv::{FromTLV, TLVArray, TLVElement, TagType, ToTLV};
use crate::utils::storage::WriteBuf;
#[derive(Clone, PartialEq, Eq, Hash, FromTLV, ToTLV)]
#[tlvargs(lifetime = "'a")]
pub struct SubscribeReq<'a>(TLVElement<'a>);
impl<'a> SubscribeReq<'a> {
pub const fn new(element: TLVElement<'a>) -> Self {
Self(element)
}
pub fn keep_subs(&self) -> Result<bool, Error> {
self.0.r#struct()?.find_ctx(0)?.bool()
}
pub fn min_int_floor(&self) -> Result<u16, Error> {
self.0.r#struct()?.find_ctx(1)?.u16()
}
pub fn max_int_ceil(&self) -> Result<u16, Error> {
self.0.r#struct()?.find_ctx(2)?.u16()
}
pub fn attr_requests(&self) -> Result<Option<TLVArray<'a, AttrPath>>, Error> {
Option::from_tlv(&self.0.r#struct()?.find_ctx(3)?)
}
pub fn event_requests(&self) -> Result<Option<TLVArray<'a, EventPath>>, Error> {
Option::from_tlv(&self.0.r#struct()?.find_ctx(4)?)
}
pub fn event_filters(&self) -> Result<Option<TLVArray<'a, EventFilter>>, Error> {
Option::from_tlv(&self.0.r#struct()?.find_ctx(5)?)
}
pub fn fabric_filtered(&self) -> Result<bool, Error> {
self.0.r#struct()?.find_ctx(7)?.bool()
}
pub fn dataver_filters(&self) -> Result<Option<TLVArray<'a, DataVersionFilter>>, Error> {
Option::from_tlv(&self.0.r#struct()?.find_ctx(8)?)
}
}
impl fmt::Debug for SubscribeReq<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SubscribeReqRef")
.field("keep_subs", &self.keep_subs())
.field("min_int_floor", &self.min_int_floor())
.field("max_int_ceil", &self.max_int_ceil())
.field("attr_requests", &self.attr_requests())
.field("event_requests", &self.event_requests())
.field("event_filters", &self.event_filters())
.field("fabric_filtered", &self.fabric_filtered())
.field("dataver_filters", &self.dataver_filters())
.finish()
}
}
#[cfg(feature = "defmt")]
impl defmt::Format for SubscribeReq<'_> {
fn format(&self, f: defmt::Formatter<'_>) {
defmt::write!(f,
"SubscribeReqRef {{\n keep_subs: {:?},\n min_int_floor: {:?},\n max_int_ceil: {:?},\n attr_requests: {:?},\n event_requests: {:?},\n event_filters: {:?},\n fabric_filtered: {:?},\n dataver_filters: {:?},\n}}",
self.keep_subs(),
self.min_int_floor(),
self.max_int_ceil(),
self.attr_requests(),
self.event_requests(),
self.event_filters(),
self.fabric_filtered(),
self.dataver_filters(),
)
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum SubscribeReqTag {
KeepSubs = 0,
MinIntFloor = 1,
MaxIntCeil = 2,
AttrRequests = 3,
EventRequests = 4,
EventFilters = 5,
FabricFiltered = 7,
DataVersionFilters = 8,
}
#[derive(Debug, Clone, FromTLV, ToTLV)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct SubscribeResp {
pub subs_id: u32,
pub _dummy: Option<u32>,
pub max_int: u16,
#[tagval(crate::im::encoding::IM_REVISION_TAG)]
pub interaction_model_revision: Option<u8>,
}
impl SubscribeResp {
pub fn new(subs_id: u32, max_int: u16) -> Self {
Self {
subs_id,
_dummy: None,
max_int,
interaction_model_revision: Some(IM_REVISION),
}
}
pub fn write<'a>(
wb: &'a mut WriteBuf,
subscription_id: u32,
max_int: u16,
) -> Result<&'a [u8], Error> {
Self::new(subscription_id, max_int).to_tlv(&TagType::Anonymous, &mut *wb)?;
Ok(wb.as_slice())
}
}