spodes_rs/interface.rs
1use crate::obis::ObisCode;
2use crate::types::{BerError, CosemDataType};
3use std::any::Any;
4
5/// The trait shared by every COSEM interface class defined in IEC 62056-6-2.
6///
7/// Each class exposes its class id, version, logical name (OBIS code),
8/// attributes and methods, and supports BER serialization/deserialization and
9/// method invocation.
10pub trait InterfaceClass: Any {
11 /// Returns the class id (`class_id`) as defined in IEC 62056-6-2.
12 fn class_id(&self) -> u16;
13
14 /// Returns the class version.
15 fn version(&self) -> u8;
16
17 /// Returns the object's logical name (OBIS code).
18 fn logical_name(&self) -> &ObisCode;
19
20 /// Returns the class attributes as `(attribute_id, value)` pairs.
21 fn attributes(&self) -> Vec<(u8, CosemDataType)>;
22
23 /// Returns the class methods as `(method_id, name)` pairs.
24 fn methods(&self) -> Vec<(u8, String)>;
25
26 /// Serializes the object into BER.
27 ///
28 /// # Arguments
29 /// * `buf` - The buffer the serialized bytes are appended to.
30 ///
31 /// # Returns
32 /// * `Ok(())` - On success.
33 /// * `Err(BerError)` - On a serialization error.
34 fn serialize_ber(&self, buf: &mut Vec<u8>) -> Result<(), BerError>;
35
36 /// Deserializes the object from BER data.
37 ///
38 /// # Arguments
39 /// * `data` - The input BER bytes.
40 ///
41 /// # Returns
42 /// * `Ok(())` - On success.
43 /// * `Err(BerError)` - On a deserialization error.
44 fn deserialize_ber(&mut self, data: &[u8]) -> Result<(), BerError>;
45
46 /// Writes the value of a writable attribute. The default implementation
47 /// rejects the write; classes with writable attributes override it.
48 ///
49 /// # Arguments
50 /// * `attribute_id` - The attribute to write.
51 /// * `value` - The new attribute value.
52 ///
53 /// # Returns
54 /// * `Ok(())` - If the attribute was written.
55 /// * `Err(String)` - If the attribute is not writable or the value is invalid.
56 fn set_attribute(&mut self, attribute_id: u8, value: CosemDataType) -> Result<(), String> {
57 let _ = (attribute_id, value);
58 Err("attribute is not writable".to_string())
59 }
60
61 /// Invokes the object method with the given id.
62 ///
63 /// # Arguments
64 /// * `method_id` - The method id.
65 /// * `params` - Optional method parameters as a `CosemDataType`.
66 ///
67 /// # Returns
68 /// * `Ok(CosemDataType)` - The method result.
69 /// * `Err(String)` - An error description if the method is not supported.
70 fn invoke_method(&mut self, method_id: u8, params: Option<CosemDataType>) -> Result<CosemDataType, String>;
71
72 /// Returns the object as `dyn Any` for dynamic downcasting.
73 fn as_any(&self) -> &dyn Any;
74}