Skip to content

Commit 8318536

Browse files
committed
Add from/to TLV for i16, i32 and i64
1 parent 879f816 commit 8318536

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

matter/src/tlv/parser.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,14 @@ impl<'a> TLVElement<'a> {
357357
}
358358
}
359359

360+
pub fn i16(&self) -> Result<i16, Error> {
361+
match self.element_type {
362+
ElementType::S8(a) => Ok(a.into()),
363+
ElementType::S16(a) => Ok(a),
364+
_ => Err(ErrorCode::TLVTypeMismatch.into()),
365+
}
366+
}
367+
360368
pub fn u16(&self) -> Result<u16, Error> {
361369
match self.element_type {
362370
ElementType::U8(a) => Ok(a.into()),
@@ -365,6 +373,15 @@ impl<'a> TLVElement<'a> {
365373
}
366374
}
367375

376+
pub fn i32(&self) -> Result<i32, Error> {
377+
match self.element_type {
378+
ElementType::S8(a) => Ok(a.into()),
379+
ElementType::S16(a) => Ok(a.into()),
380+
ElementType::S32(a) => Ok(a),
381+
_ => Err(ErrorCode::TLVTypeMismatch.into()),
382+
}
383+
}
384+
368385
pub fn u32(&self) -> Result<u32, Error> {
369386
match self.element_type {
370387
ElementType::U8(a) => Ok(a.into()),
@@ -374,6 +391,16 @@ impl<'a> TLVElement<'a> {
374391
}
375392
}
376393

394+
pub fn i64(&self) -> Result<i64, Error> {
395+
match self.element_type {
396+
ElementType::S8(a) => Ok(a.into()),
397+
ElementType::S16(a) => Ok(a.into()),
398+
ElementType::S32(a) => Ok(a.into()),
399+
ElementType::S64(a) => Ok(a),
400+
_ => Err(ErrorCode::TLVTypeMismatch.into()),
401+
}
402+
}
403+
377404
pub fn u64(&self) -> Result<u64, Error> {
378405
match self.element_type {
379406
ElementType::U8(a) => Ok(a.into()),

matter/src/tlv/traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ macro_rules! fromtlv_for {
9191
};
9292
}
9393

94-
fromtlv_for!(u8 u16 u32 u64 bool);
94+
fromtlv_for!(i8 u8 i16 u16 i32 u32 i64 u64 bool);
9595

9696
pub trait ToTLV {
9797
fn to_tlv(&self, tw: &mut TLVWriter, tag: TagType) -> Result<(), Error>;
@@ -139,7 +139,7 @@ impl<'a, T: ToTLV> ToTLV for &'a [T] {
139139
}
140140

141141
// Generate ToTLV for standard data types
142-
totlv_for!(i8 u8 u16 u32 u64 bool);
142+
totlv_for!(i8 u8 i16 u16 i32 u32 i64 u64 bool);
143143

144144
// We define a few common data types that will be required here
145145
//

0 commit comments

Comments
 (0)