Skip to content

Commit

Permalink
WIP Plaintext VIF parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
maebli committed Mar 28, 2024
1 parent 7ad790a commit 2e4985d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
45 changes: 33 additions & 12 deletions src/user_data/value_information.rs
@@ -1,9 +1,18 @@
use arrayvec::ArrayVec;

const MAX_PLAIN_TEXT_VIF_SIZE: usize = 10;
/* TODO add suppor for 2 - 10 VIFE */
const MAX_VIF_RECORDS: usize = 10;
#[derive(Debug)]
struct ValueInformationBlock {
_value_information: ValueInformation,
_value_information_extension: Option<ArrayVec<u8, MAX_VIF_RECORDS>>,
}

#[derive(Debug, PartialEq)]
pub enum ValueInformation {
Primary(u8),
PlainText,
PlainText(ArrayVec<u8, MAX_PLAIN_TEXT_VIF_SIZE>, Option<VIFExtension>),
Extended(VIFExtension),
Any,
ManufacturerSpecific,
Expand All @@ -13,7 +22,8 @@ impl ValueInformation {
pub fn get_size(&self) -> usize {
match self {
ValueInformation::Primary(_) => 1,
ValueInformation::PlainText => 1,
ValueInformation::PlainText(x, None) => x.len() + 2,
ValueInformation::PlainText(x, Some(_)) => x.len() + 3,
ValueInformation::Extended(_) => 2,
ValueInformation::Any => 1,
ValueInformation::ManufacturerSpecific => 1,
Expand All @@ -32,7 +42,15 @@ impl TryFrom<&[u8]> for ValueInformation {
fn try_from(data: &[u8]) -> Result<Self, ValueInformationError> {
Ok(match data[0] {
0x00..=0x7B | 0x80..=0xFA => ValueInformation::Primary(data[0]),
0x7C | 0xFC => ValueInformation::PlainText,
0x7C | 0xFC => {
let mut vif = ArrayVec::new();
let len = data[1] as usize;
for i in 0..len {
vif.push(data[i + 2]);
}
vif.reverse();
ValueInformation::PlainText(vif, None)
}
0xFD => ValueInformation::Extended(match data[1] {
0x00..=0x03 => VIFExtension::CreditOfCurrencyUnits(0b11 & data[1]),
0x04..=0x07 => VIFExtension::DebitOfCurrencyUnits(0b11 & data[1]),
Expand Down Expand Up @@ -261,7 +279,8 @@ impl TryFrom<&ValueInformation> for Unit {
0x78 => Ok(Unit::FabricationNumber),
_ => todo!("Implement the rest of the units: {:?}", x),
},
ValueInformation::PlainText => Ok(Unit::PlainText),
ValueInformation::PlainText(_, None) => Ok(Unit::PlainText),
ValueInformation::PlainText(_, Some(_)) => Ok(Unit::PlainText),
ValueInformation::Extended(x) => match x {
VIFExtension::EnergyMWh(_) => Ok(Unit::MegaWattHour),
VIFExtension::EnergyGJ(_) => Ok(Unit::GigaJoul),
Expand All @@ -284,12 +303,6 @@ impl TryFrom<&ValueInformation> for Unit {
}
}

const MAX_RECORDS: usize = 10;
#[derive(Debug)]
struct ValueInformationBlock {
_value_information: ValueInformation,
_value_information_extension: Option<ArrayVec<u8, MAX_RECORDS>>,
}
mod tests {

#[test]
Expand Down Expand Up @@ -328,13 +341,21 @@ mod tests {

#[test]
fn test_plain_text_vif() {
use crate::user_data::value_information::VIFExtension;
use crate::user_data::value_information::ValueInformation;

use arrayvec::ArrayVec;
// VIF LEN(3) 'R' 'H' '%' VIFE
//0xFC, 0x03, 0x48, 0x52, 0x25, 0x74,
// %RH
let data = [0xFC, 0x03, 0x48, 0x52, 0x25, 0x74];
let mut a = ArrayVec::<u8, 10>::new();
a.try_extend_from_slice(&data[2..5]).unwrap();
a.reverse();
let result = ValueInformation::try_from(data.as_slice()).unwrap();
assert_eq!(result, ValueInformation::PlainText);
assert_eq!(
result,
ValueInformation::PlainText(a, Some(VIFExtension::Reserved))
);
assert_eq!(result.get_size(), 6);
}
}
6 changes: 3 additions & 3 deletions src/user_data/variable_user_data.rs
Expand Up @@ -42,7 +42,7 @@ impl From<&ValueInformation> for Quantity {
0x78 => Quantity::IdentificationNumber,
_ => todo!("Implement the rest of the units: {:?}", x),
},
ValueInformation::PlainText => Quantity::PlainText,
ValueInformation::PlainText(_, _) => Quantity::PlainText,
ValueInformation::Extended(x) => match x {
value_information::VIFExtension::DigitalInput => Quantity::BinaryDigitalInput,
_ => todo!("Implement the rest of the units: {:?}", x),
Expand All @@ -69,7 +69,7 @@ impl From<&ValueInformation> for Exponent {
0x6E..=0x6F | 0x78 => Exponent { inner: None },
_ => todo!("Implement the rest of the units: {:?}", x),
},
ValueInformation::PlainText => Exponent { inner: None },
ValueInformation::PlainText(_, _) => Exponent { inner: None },
ValueInformation::Extended(x) => match x {
value_information::VIFExtension::DigitalInput => Exponent { inner: None },
_ => todo!("Implement the rest of the units: {:?}", x),
Expand Down Expand Up @@ -129,7 +129,7 @@ impl TryFrom<&[u8]> for DataRecord {
let value_and_data_information_size =
data_information.get_size() + value_information.get_size();
match value_information {
ValueInformation::PlainText => {
ValueInformation::PlainText(_, _) => {
let plaintext_size = data[value_and_data_information_size] as usize;
let total_size = value_and_data_information_size + plaintext_size + 1;
Ok(DataRecord {
Expand Down

0 comments on commit 2e4985d

Please sign in to comment.