From 831853630bd2d4d911f963822283958066fba15c Mon Sep 17 00:00:00 2001 From: ivmarkov Date: Fri, 16 Jun 2023 18:42:11 +0000 Subject: [PATCH] Add from/to TLV for i16, i32 and i64 --- matter/src/tlv/parser.rs | 27 +++++++++++++++++++++++++++ matter/src/tlv/traits.rs | 4 ++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/matter/src/tlv/parser.rs b/matter/src/tlv/parser.rs index 8bfdd28..5e6964c 100644 --- a/matter/src/tlv/parser.rs +++ b/matter/src/tlv/parser.rs @@ -357,6 +357,14 @@ impl<'a> TLVElement<'a> { } } + pub fn i16(&self) -> Result { + match self.element_type { + ElementType::S8(a) => Ok(a.into()), + ElementType::S16(a) => Ok(a), + _ => Err(ErrorCode::TLVTypeMismatch.into()), + } + } + pub fn u16(&self) -> Result { match self.element_type { ElementType::U8(a) => Ok(a.into()), @@ -365,6 +373,15 @@ impl<'a> TLVElement<'a> { } } + pub fn i32(&self) -> Result { + match self.element_type { + ElementType::S8(a) => Ok(a.into()), + ElementType::S16(a) => Ok(a.into()), + ElementType::S32(a) => Ok(a), + _ => Err(ErrorCode::TLVTypeMismatch.into()), + } + } + pub fn u32(&self) -> Result { match self.element_type { ElementType::U8(a) => Ok(a.into()), @@ -374,6 +391,16 @@ impl<'a> TLVElement<'a> { } } + pub fn i64(&self) -> Result { + match self.element_type { + ElementType::S8(a) => Ok(a.into()), + ElementType::S16(a) => Ok(a.into()), + ElementType::S32(a) => Ok(a.into()), + ElementType::S64(a) => Ok(a), + _ => Err(ErrorCode::TLVTypeMismatch.into()), + } + } + pub fn u64(&self) -> Result { match self.element_type { ElementType::U8(a) => Ok(a.into()), diff --git a/matter/src/tlv/traits.rs b/matter/src/tlv/traits.rs index 9a8edcd..2156f58 100644 --- a/matter/src/tlv/traits.rs +++ b/matter/src/tlv/traits.rs @@ -91,7 +91,7 @@ macro_rules! fromtlv_for { }; } -fromtlv_for!(u8 u16 u32 u64 bool); +fromtlv_for!(i8 u8 i16 u16 i32 u32 i64 u64 bool); pub trait ToTLV { fn to_tlv(&self, tw: &mut TLVWriter, tag: TagType) -> Result<(), Error>; @@ -139,7 +139,7 @@ impl<'a, T: ToTLV> ToTLV for &'a [T] { } // Generate ToTLV for standard data types -totlv_for!(i8 u8 u16 u32 u64 bool); +totlv_for!(i8 u8 i16 u16 i32 u32 i64 u64 bool); // We define a few common data types that will be required here //