use crate::codec::{Decode, Encode, Reader, Result, Writer};
use crate::obis::Obis;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AttributeDescriptor {
pub class_id: u16,
pub instance_id: Obis,
pub attribute_id: i8,
}
impl AttributeDescriptor {
#[must_use]
pub const fn new(class_id: u16, instance_id: Obis, attribute_id: i8) -> Self {
Self { class_id, instance_id, attribute_id }
}
}
impl Encode for AttributeDescriptor {
fn encode(&self, w: &mut dyn Writer) -> Result<()> {
w.write_u16(self.class_id)?;
w.write_bytes(self.instance_id.as_bytes())?;
w.write_u8(self.attribute_id as u8)
}
fn encoded_len(&self) -> usize {
9
}
}
impl<'a> Decode<'a> for AttributeDescriptor {
fn decode(r: &mut Reader<'a>) -> Result<Self> {
Ok(Self { class_id: r.u16()?, instance_id: Obis::decode(r)?, attribute_id: r.i8()? })
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MethodDescriptor {
pub class_id: u16,
pub instance_id: Obis,
pub method_id: i8,
}
impl MethodDescriptor {
#[must_use]
pub const fn new(class_id: u16, instance_id: Obis, method_id: i8) -> Self {
Self { class_id, instance_id, method_id }
}
}
impl Encode for MethodDescriptor {
fn encode(&self, w: &mut dyn Writer) -> Result<()> {
w.write_u16(self.class_id)?;
w.write_bytes(self.instance_id.as_bytes())?;
w.write_u8(self.method_id as u8)
}
fn encoded_len(&self) -> usize {
9
}
}
impl<'a> Decode<'a> for MethodDescriptor {
fn decode(r: &mut Reader<'a>) -> Result<Self> {
Ok(Self { class_id: r.u16()?, instance_id: Obis::decode(r)?, method_id: r.i8()? })
}
}
pub use crate::cosem::SelectiveAccess;
pub(crate) fn encode_optional<T: Encode>(w: &mut dyn Writer, v: Option<&T>) -> Result<()> {
match v {
None => w.write_u8(0),
Some(v) => {
w.write_u8(1)?;
v.encode(w)
}
}
}
pub(crate) fn decode_optional<'a, T: Decode<'a>>(r: &mut Reader<'a>) -> Result<Option<T>> {
if r.u8()? == 0 { Ok(None) } else { Ok(Some(T::decode(r)?)) }
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AttributeDescriptorWithSelection<'a> {
pub descriptor: AttributeDescriptor,
pub access: Option<SelectiveAccess<'a>>,
}
impl Encode for AttributeDescriptorWithSelection<'_> {
fn encode(&self, w: &mut dyn Writer) -> Result<()> {
self.descriptor.encode(w)?;
encode_optional(w, self.access.as_ref())
}
}
impl<'a> Decode<'a> for AttributeDescriptorWithSelection<'a> {
fn decode(r: &mut Reader<'a>) -> Result<Self> {
Ok(Self { descriptor: AttributeDescriptor::decode(r)?, access: decode_optional(r)? })
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InvokeId(pub u8);
impl InvokeId {
#[must_use]
pub const fn confirmed(id: u8) -> Self {
Self((id & 0x0F) | 0x40)
}
#[must_use]
pub const fn id(self) -> u8 {
self.0 & 0x0F
}
#[must_use]
pub const fn is_confirmed(self) -> bool {
self.0 & 0x40 != 0
}
#[must_use]
pub const fn is_high_priority(self) -> bool {
self.0 & 0x80 != 0
}
#[must_use]
pub const fn with_high_priority(self) -> Self {
Self(self.0 | 0x80)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LongInvokeId(pub u32);
impl LongInvokeId {
#[must_use]
pub const fn new(id: u32) -> Self {
Self(id & 0x00FF_FFFF)
}
#[must_use]
pub const fn id(self) -> u32 {
self.0 & 0x00FF_FFFF
}
#[must_use]
pub const fn is_confirmed(self) -> bool {
self.0 & 0x2000_0000 != 0
}
#[must_use]
pub const fn is_self_descriptive(self) -> bool {
self.0 & 0x8000_0000 != 0
}
#[must_use]
pub const fn confirmed(self) -> Self {
Self(self.0 | 0x2000_0000)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::codec::SliceWriter;
#[test]
fn an_attribute_descriptor_is_nine_bytes_in_order() {
let d = AttributeDescriptor::new(3, Obis::new(1, 0, 1, 8, 0, 255), 2);
let mut buf = [0u8; 16];
let mut w = SliceWriter::new(&mut buf);
d.encode(&mut w).unwrap();
assert_eq!(w.as_slice(), [0x00, 0x03, 1, 0, 1, 8, 0, 255, 2]);
assert_eq!(d.encoded_len(), 9);
assert_eq!(AttributeDescriptor::from_bytes(w.as_slice()).unwrap(), d);
}
#[test]
fn a_negative_attribute_index_survives_the_round_trip() {
let d = AttributeDescriptor::new(7, Obis::new(1, 0, 99, 1, 0, 255), -1);
let mut buf = [0u8; 16];
let mut w = SliceWriter::new(&mut buf);
d.encode(&mut w).unwrap();
assert_eq!(w.as_slice()[8], 0xFF);
assert_eq!(AttributeDescriptor::from_bytes(w.as_slice()).unwrap().attribute_id, -1);
}
#[test]
fn invoke_id_bits() {
let i = InvokeId::confirmed(3);
assert_eq!(i.id(), 3);
assert!(i.is_confirmed());
assert!(!i.is_high_priority());
assert!(i.with_high_priority().is_high_priority());
assert_eq!(i.0, 0x43);
}
#[test]
fn long_invoke_id_bits() {
let i = LongInvokeId::new(0x01_0203).confirmed();
assert_eq!(i.id(), 0x01_0203);
assert!(i.is_confirmed());
assert!(!i.is_self_descriptive());
}
}