use crate::obis::Obis;
pub const ATTRIBUTE_STRIDE: u16 = 8;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShortNameTarget {
Attribute {
class_id: u16,
logical_name: Obis,
attribute_id: i8,
},
Method {
class_id: u16,
logical_name: Obis,
method_id: i8,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ShortName {
pub base: u16,
pub class_id: u16,
pub logical_name: Obis,
pub method_offset: Option<u16>,
pub attributes: u8,
pub methods: u8,
}
impl ShortName {
#[must_use]
pub const fn new(base: u16, class_id: u16, logical_name: Obis, attributes: u8) -> Self {
Self { base, class_id, logical_name, method_offset: None, attributes, methods: 0 }
}
#[must_use]
pub const fn with_methods(self, offset: u16, count: u8) -> Self {
Self { method_offset: Some(offset), methods: count, ..self }
}
#[must_use]
pub const fn attribute(&self, index: i8) -> Option<u16> {
if index < 1 || index as u8 > self.attributes {
return None;
}
let steps = (index as u16).wrapping_sub(1);
match steps.checked_mul(ATTRIBUTE_STRIDE) {
Some(offset) => self.base.checked_add(offset),
None => None,
}
}
#[must_use]
pub const fn method(&self, index: i8) -> Option<u16> {
let Some(method_offset) = self.method_offset else {
return None;
};
if index < 1 || index as u8 > self.methods {
return None;
}
let steps = (index as u16).wrapping_sub(1);
let Some(offset) = steps.checked_mul(ATTRIBUTE_STRIDE) else {
return None;
};
let Some(offset) = offset.checked_add(method_offset) else {
return None;
};
self.base.checked_add(offset)
}
#[must_use]
pub const fn resolve(&self, name: u16) -> Option<ShortNameTarget> {
let Some(offset) = name.checked_sub(self.base) else {
return None;
};
if offset % ATTRIBUTE_STRIDE != 0 {
return None;
}
let step = offset / ATTRIBUTE_STRIDE;
if let Some(method_offset) = self.method_offset {
if offset >= method_offset {
let steps = (offset - method_offset) / ATTRIBUTE_STRIDE;
if steps < 0x80 && (steps as u8) < self.methods {
return Some(ShortNameTarget::Method {
class_id: self.class_id,
logical_name: self.logical_name,
method_id: (steps as i8).wrapping_add(1),
});
}
return None;
}
}
if step >= 0x80 || (step as u8) >= self.attributes {
return None;
}
Some(ShortNameTarget::Attribute {
class_id: self.class_id,
logical_name: self.logical_name,
attribute_id: (step as i8).wrapping_add(1),
})
}
}
#[must_use]
pub fn resolve(objects: &[ShortName], name: u16) -> Option<ShortNameTarget> {
objects.iter().find_map(|o| o.resolve(name))
}
#[cfg(test)]
mod tests {
use super::*;
const ENERGY: Obis = Obis::new(1, 0, 1, 8, 0, 255);
#[test]
fn attributes_sit_eight_apart_from_the_base_name() {
let r = ShortName::new(0x0028, 3, ENERGY, 3);
assert_eq!(r.attribute(1), Some(0x0028));
assert_eq!(r.attribute(2), Some(0x0030));
assert_eq!(r.attribute(3), Some(0x0038));
assert_eq!(r.attribute(4), None, "the class has three");
assert_eq!(r.attribute(0), None, "indexes count from one");
}
#[test]
fn a_method_offset_is_the_classs_own_and_is_not_derivable() {
let r = ShortName::new(0x0028, 3, ENERGY, 3).with_methods(0x28, 1);
assert_eq!(r.method(1), Some(0x0050));
assert_ne!(r.method(1), r.attribute(3).map(|a| a + 8), "not simply after the attributes");
let unknown = ShortName::new(0x0028, 3, ENERGY, 3);
assert_eq!(unknown.method(1), None);
assert_eq!(unknown.resolve(0x0050), None);
}
#[test]
fn resolving_is_the_inverse_of_naming() {
let r = ShortName::new(0x0028, 3, ENERGY, 3).with_methods(0x28, 1);
for index in 1..=3i8 {
let name = r.attribute(index).unwrap();
assert_eq!(
r.resolve(name),
Some(ShortNameTarget::Attribute { class_id: 3, logical_name: ENERGY, attribute_id: index })
);
}
let name = r.method(1).unwrap();
assert_eq!(
r.resolve(name),
Some(ShortNameTarget::Method { class_id: 3, logical_name: ENERGY, method_id: 1 })
);
}
#[test]
fn a_name_that_is_not_this_objects_resolves_to_nothing() {
let r = ShortName::new(0x0028, 3, ENERGY, 3).with_methods(0x28, 1);
assert_eq!(r.resolve(0x0020), None, "below the base");
assert_eq!(r.resolve(0x0029), None, "off the eight-byte grid");
assert_eq!(r.resolve(0x0040), None, "between the last attribute and the methods");
assert_eq!(r.resolve(0x0058), None, "past the only method");
}
#[test]
fn a_table_finds_the_object_a_name_belongs_to() {
const CLOCK: Obis = Obis::new(0, 0, 1, 0, 0, 255);
let objects =
[ShortName::new(0x0028, 3, ENERGY, 3).with_methods(0x28, 1), ShortName::new(0x0100, 8, CLOCK, 9)];
assert!(matches!(
resolve(&objects, 0x0030),
Some(ShortNameTarget::Attribute { class_id: 3, attribute_id: 2, .. })
));
assert!(matches!(
resolve(&objects, 0x0108),
Some(ShortNameTarget::Attribute { class_id: 8, attribute_id: 2, .. })
));
assert_eq!(resolve(&objects, 0x0200), None);
}
#[test]
fn arithmetic_near_the_top_of_the_range_saturates_rather_than_wrapping() {
let r = ShortName::new(0xFFF8, 1, ENERGY, 2);
assert_eq!(r.attribute(1), Some(0xFFF8));
assert_eq!(r.attribute(2), None, "0xFFF8 + 8 does not fit");
}
}