rustflexstack/btp/
btp_header.rs1use super::service_access_point::BTPDataRequest;
16
17#[derive(Debug)]
26pub struct BTPAHeader {
27 destination_port: u16,
28 source_port: u16,
29}
30
31impl Default for BTPAHeader {
32 fn default() -> Self {
33 Self::new()
34 }
35}
36
37impl BTPAHeader {
38 pub fn new() -> Self {
39 BTPAHeader {
40 destination_port: 0,
41 source_port: 0,
42 }
43 }
44
45 pub fn initialize_with_request(request: &BTPDataRequest) -> Self {
47 BTPAHeader {
48 destination_port: request.destination_port,
49 source_port: request.source_port,
50 }
51 }
52
53 pub fn destination_port(&self) -> u16 {
55 self.destination_port
56 }
57
58 pub fn source_port(&self) -> u16 {
60 self.source_port
61 }
62
63 pub fn encode(&self) -> [u8; 4] {
65 let mut bytes = [0u8; 4];
66 bytes[0..2].copy_from_slice(&self.destination_port.to_be_bytes());
67 bytes[2..4].copy_from_slice(&self.source_port.to_be_bytes());
68 bytes
69 }
70
71 pub fn decode(bytes: [u8; 4]) -> Self {
73 BTPAHeader {
74 destination_port: u16::from_be_bytes([bytes[0], bytes[1]]),
75 source_port: u16::from_be_bytes([bytes[2], bytes[3]]),
76 }
77 }
78}
79
80#[derive(Debug)]
90pub struct BTPBHeader {
91 pub destination_port: u16,
93 pub destination_port_info: u16,
95}
96
97impl Default for BTPBHeader {
98 fn default() -> Self {
99 Self::new()
100 }
101}
102
103impl BTPBHeader {
104 pub fn new() -> Self {
105 BTPBHeader {
106 destination_port: 0,
107 destination_port_info: 0,
108 }
109 }
110
111 pub fn initialize_with_request(request: &BTPDataRequest) -> Self {
113 BTPBHeader {
114 destination_port: request.destination_port,
115 destination_port_info: request.destination_port_info,
116 }
117 }
118
119 pub fn encode(&self) -> [u8; 4] {
121 let mut bytes = [0u8; 4];
122 bytes[0..2].copy_from_slice(&self.destination_port.to_be_bytes());
123 bytes[2..4].copy_from_slice(&self.destination_port_info.to_be_bytes());
124 bytes
125 }
126
127 pub fn decode(bytes: [u8; 4]) -> Self {
129 BTPBHeader {
130 destination_port: u16::from_be_bytes([bytes[0], bytes[1]]),
131 destination_port_info: u16::from_be_bytes([bytes[2], bytes[3]]),
132 }
133 }
134}
135
136#[cfg(test)]
137mod tests {
138 use super::*;
139 use crate::btp::service_access_point::BTPDataRequest;
140
141 #[test]
144 fn btpa_new_defaults_to_zero() {
145 let h = BTPAHeader::new();
146 assert_eq!(h.destination_port(), 0);
147 assert_eq!(h.source_port(), 0);
148 }
149
150 #[test]
151 fn btpa_encode_decode_roundtrip() {
152 let h = BTPAHeader {
153 destination_port: 2001,
154 source_port: 5000,
155 };
156 let encoded = h.encode();
157 let decoded = BTPAHeader::decode(encoded);
158 assert_eq!(decoded.destination_port(), 2001);
159 assert_eq!(decoded.source_port(), 5000);
160 }
161
162 #[test]
163 fn btpa_encode_big_endian() {
164 let h = BTPAHeader {
165 destination_port: 0x07D1, source_port: 0x1388, };
168 let bytes = h.encode();
169 assert_eq!(bytes, [0x07, 0xD1, 0x13, 0x88]);
170 }
171
172 #[test]
173 fn btpa_initialize_with_request() {
174 let mut req = BTPDataRequest::new();
175 req.destination_port = 2001;
176 req.source_port = 3000;
177 let h = BTPAHeader::initialize_with_request(&req);
178 assert_eq!(h.destination_port(), 2001);
179 assert_eq!(h.source_port(), 3000);
180 }
181
182 #[test]
183 fn btpa_max_port_values() {
184 let h = BTPAHeader {
185 destination_port: u16::MAX,
186 source_port: u16::MAX,
187 };
188 let decoded = BTPAHeader::decode(h.encode());
189 assert_eq!(decoded.destination_port(), u16::MAX);
190 assert_eq!(decoded.source_port(), u16::MAX);
191 }
192
193 #[test]
196 fn btpb_new_defaults_to_zero() {
197 let h = BTPBHeader::new();
198 assert_eq!(h.destination_port, 0);
199 assert_eq!(h.destination_port_info, 0);
200 }
201
202 #[test]
203 fn btpb_encode_decode_roundtrip() {
204 let h = BTPBHeader {
205 destination_port: 2002,
206 destination_port_info: 42,
207 };
208 let encoded = h.encode();
209 let decoded = BTPBHeader::decode(encoded);
210 assert_eq!(decoded.destination_port, 2002);
211 assert_eq!(decoded.destination_port_info, 42);
212 }
213
214 #[test]
215 fn btpb_encode_big_endian() {
216 let h = BTPBHeader {
217 destination_port: 0x07D2, destination_port_info: 0x002A, };
220 let bytes = h.encode();
221 assert_eq!(bytes, [0x07, 0xD2, 0x00, 0x2A]);
222 }
223
224 #[test]
225 fn btpb_initialize_with_request() {
226 let mut req = BTPDataRequest::new();
227 req.destination_port = 2001;
228 req.destination_port_info = 99;
229 let h = BTPBHeader::initialize_with_request(&req);
230 assert_eq!(h.destination_port, 2001);
231 assert_eq!(h.destination_port_info, 99);
232 }
233
234 #[test]
235 fn btpb_cam_port() {
236 let h = BTPBHeader {
237 destination_port: 2001,
238 destination_port_info: 0,
239 };
240 let decoded = BTPBHeader::decode(h.encode());
241 assert_eq!(decoded.destination_port, 2001);
242 assert_eq!(decoded.destination_port_info, 0);
243 }
244
245 #[test]
246 fn btpb_denm_port() {
247 let h = BTPBHeader {
248 destination_port: 2002,
249 destination_port_info: 0,
250 };
251 let decoded = BTPBHeader::decode(h.encode());
252 assert_eq!(decoded.destination_port, 2002);
253 }
254}