spodes_rs/classes/
mbus_slave_port_setup.rs1use crate::interface::InterfaceClass;
2use crate::obis::ObisCode;
3use crate::types::{BerError, CosemDataType};
4use serde::{Deserialize, Serialize};
5use std::any::Any;
6
7#[derive(Clone, Serialize, Deserialize, Debug)]
9pub struct MbusSlavePortSetupConfig {
10 pub logical_name: ObisCode,
12 pub default_baud: u8,
14 pub avail_baud: u8,
16 pub addr_state: u8,
18 pub bus_address: u8,
20}
21
22#[derive(Clone, Serialize, Deserialize, Debug)]
27pub struct MbusSlavePortSetup {
28 logical_name: ObisCode,
29 default_baud: u8,
30 avail_baud: u8,
31 addr_state: u8,
32 bus_address: u8,
33}
34
35impl MbusSlavePortSetup {
36 pub fn new(config: MbusSlavePortSetupConfig) -> Self {
38 MbusSlavePortSetup {
39 logical_name: config.logical_name,
40 default_baud: config.default_baud,
41 avail_baud: config.avail_baud,
42 addr_state: config.addr_state,
43 bus_address: config.bus_address,
44 }
45 }
46}
47
48impl InterfaceClass for MbusSlavePortSetup {
49 fn class_id(&self) -> u16 {
50 25
51 }
52
53 fn version(&self) -> u8 {
54 0
55 }
56
57 fn logical_name(&self) -> &ObisCode {
58 &self.logical_name
59 }
60
61 fn attributes(&self) -> Vec<(u8, CosemDataType)> {
62 vec![
63 (1, CosemDataType::OctetString(self.logical_name.to_bytes())),
64 (2, CosemDataType::Enum(self.default_baud)),
65 (3, CosemDataType::Enum(self.avail_baud)),
66 (4, CosemDataType::Enum(self.addr_state)),
67 (5, CosemDataType::Unsigned(self.bus_address)),
68 ]
69 }
70
71 fn methods(&self) -> Vec<(u8, String)> {
72 vec![]
74 }
75
76 fn serialize_ber(&self, buf: &mut Vec<u8>) -> Result<(), BerError> {
77 let mut seq_buf = Vec::new();
78 CosemDataType::LongUnsigned(self.class_id()).serialize_ber(&mut seq_buf)?;
79 for (_, attr) in self.attributes() {
80 attr.serialize_ber(&mut seq_buf)?;
81 }
82 buf.push(0x02); write_length(1 + self.attributes().len(), buf); buf.extend_from_slice(&seq_buf);
85 Ok(())
86 }
87
88 fn deserialize_ber(&mut self, data: &[u8]) -> Result<(), BerError> {
89 let (tlv, rest) = CosemDataType::deserialize_ber(data)?;
90 if !rest.is_empty() {
91 return Err(BerError::InvalidTag);
92 }
93 let CosemDataType::Structure(seq) = tlv else {
94 return Err(BerError::InvalidTag);
95 };
96 if seq.len() != 6 {
98 return Err(BerError::InvalidLength);
99 }
100 if let CosemDataType::LongUnsigned(class_id) = seq[0] {
101 if class_id != self.class_id() {
102 return Err(BerError::InvalidValue);
103 }
104 } else {
105 return Err(BerError::InvalidTag);
106 }
107 if let CosemDataType::OctetString(obis) = &seq[1] {
108 if obis.len() == 6 {
109 self.logical_name = ObisCode::new(obis[0], obis[1], obis[2], obis[3], obis[4], obis[5]);
110 } else {
111 return Err(BerError::InvalidLength);
112 }
113 } else {
114 return Err(BerError::InvalidTag);
115 }
116 self.default_baud = take_enum(&seq[2])?;
117 self.avail_baud = take_enum(&seq[3])?;
118 self.addr_state = take_enum(&seq[4])?;
119 self.bus_address = match seq[5] {
120 CosemDataType::Unsigned(v) => v,
121 _ => return Err(BerError::InvalidTag),
122 };
123 Ok(())
124 }
125
126 fn invoke_method(&mut self, method_id: u8, _params: Option<CosemDataType>) -> Result<CosemDataType, String> {
127 Err(format!("Method {method_id} not supported for M-Bus slave port setup (no specific methods)"))
128 }
129
130 fn as_any(&self) -> &dyn Any {
131 self
132 }
133}
134
135fn take_enum(value: &CosemDataType) -> Result<u8, BerError> {
136 match value {
137 CosemDataType::Enum(v) => Ok(*v),
138 _ => Err(BerError::InvalidTag),
139 }
140}
141
142#[allow(clippy::cast_possible_truncation)] fn write_length(length: usize, buf: &mut Vec<u8>) {
145 if length < 128 {
146 buf.push(length as u8);
147 } else {
148 let bytes = (length as u64).to_be_bytes();
149 let first_non_zero = bytes.iter().position(|&b| b != 0).unwrap_or(7);
150 let num_octets = 8 - first_non_zero;
151 buf.push(0x80 | num_octets as u8);
152 buf.extend_from_slice(&bytes[first_non_zero..]);
153 }
154}
155
156#[cfg(test)]
157mod tests {
158 use super::*;
159
160 fn sample() -> MbusSlavePortSetup {
161 MbusSlavePortSetup::new(MbusSlavePortSetupConfig {
162 logical_name: ObisCode::new(0, 0, 24, 0, 0, 255),
163 default_baud: 0,
164 avail_baud: 3,
165 addr_state: 1,
166 bus_address: 5,
167 })
168 }
169
170 #[test]
171 fn class_id_and_attributes() {
172 let obj = sample();
173 assert_eq!(obj.class_id(), 25);
174 assert_eq!(obj.version(), 0);
175 assert_eq!(obj.attributes().len(), 5);
176 assert!(obj.methods().is_empty());
177 }
178
179 #[test]
180 fn round_trip() {
181 let obj = sample();
182 let mut buf = Vec::new();
183 obj.serialize_ber(&mut buf).unwrap();
184 let mut decoded = sample();
185 decoded.bus_address = 0;
186 decoded.deserialize_ber(&buf).unwrap();
187 assert_eq!(decoded.attributes(), obj.attributes());
188 }
189}