Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/learn_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub struct ScheduleValue {
pub struct TrendLogValue {
pub id: ObjectId,
pub name: String,
pub record_count: u32,
pub record_count: u64,
}

#[cfg(feature = "alloc")]
Expand Down
42 changes: 36 additions & 6 deletions src/application_protocol/primitives/data_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::{fmt::Display, str::from_utf8};
use crate::common::{
daily_schedule::WeeklySchedule,
error::Error,
helper::{decode_unsigned, encode_application_enumerated},
helper::{decode_signed, decode_unsigned, encode_application_enumerated, encode_application_signed, encode_application_unsigned},
io::{Reader, Writer},
object_id::{ObjectId, ObjectType},
property_id::PropertyId,
Expand Down Expand Up @@ -31,14 +31,17 @@ pub enum ApplicationDataValue<'a> {
CharacterString(CharacterString<'a>),
Enumerated(Enumerated),
BitString(BitString<'a>),
UnsignedInt(u32),
UnsignedInt(u64),
SignedInt(i64),
WeeklySchedule(WeeklySchedule<'a>),
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum ApplicationDataValueWrite<'a> {
Boolean(bool),
UnsignedInt(u64),
SignedInt(i64),
Enumerated(Enumerated),
Real(f32),
WeeklySchedule(WeeklySchedule<'a>),
Expand Down Expand Up @@ -372,6 +375,22 @@ impl<'a> ApplicationDataValueWrite<'a> {
let value = decode_enumerated(object_id, property_id, &tag, reader, buf)?;
Ok(Self::Enumerated(value))
}
TagNumber::Application(ApplicationTagNumber::UnsignedInt) => {
Ok(Self::UnsignedInt(decode_unsigned(tag.value, reader, buf)?))

}
TagNumber::Application(ApplicationTagNumber::SignedInt) => {
let len = tag.value as usize;
if len > 8 {
return Err(Error::Length(("integers bigger than 64 bits are not supported", tag.value)));
}
// Read into the most significant bits, then do a right shift to get sign extension for negative integers.
let mut bytes = [0; 8];
bytes[..len].copy_from_slice(reader.read_slice(len, buf)?);
let value = i64::from_be_bytes(bytes) >> (8 * (8 - len));
Ok(Self::SignedInt(value))

}
tag_number => Err(Error::TagNotSupported((
"ApplicationDataValueWrite decode",
tag_number,
Expand All @@ -396,6 +415,12 @@ impl<'a> ApplicationDataValueWrite<'a> {
tag.encode(writer);
writer.extend_from_slice(&f32::to_be_bytes(*x))
}
Self::UnsignedInt(x) => {
encode_application_unsigned(writer, *x);
}
Self::SignedInt(x) => {
encode_application_signed(writer, *x);
}
Self::Enumerated(x) => {
x.encode(writer);
}
Expand Down Expand Up @@ -457,9 +482,10 @@ impl<'a> ApplicationDataValue<'a> {
x.encode_application(writer);
}
ApplicationDataValue::UnsignedInt(x) => {
Tag::new(TagNumber::Application(ApplicationTagNumber::UnsignedInt), 4)
.encode(writer);
writer.extend_from_slice(&x.to_be_bytes());
encode_application_unsigned(writer, *x)
}
ApplicationDataValue::SignedInt(x) => {
encode_application_signed(writer, *x)
}
ApplicationDataValue::WeeklySchedule(x) => {
// no application tag required for weekly schedule
Expand Down Expand Up @@ -521,9 +547,13 @@ impl<'a> ApplicationDataValue<'a> {
Ok(ApplicationDataValue::Boolean(value))
}
ApplicationTagNumber::UnsignedInt => {
let value = decode_unsigned(tag.value, reader, buf)? as u32;
let value = decode_unsigned(tag.value, reader, buf)?;
Ok(ApplicationDataValue::UnsignedInt(value))
}
ApplicationTagNumber::SignedInt => {
let value = decode_signed(tag.value, reader, buf)?;
Ok(ApplicationDataValue::SignedInt(value))
}
ApplicationTagNumber::Time => {
if tag.value != 4 {
return Err(Error::Length((
Expand Down
Loading