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