Skip to main content

rustflexstack/btp/
btp_header.rs

1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2024 Fundació Privada Internet i Innovació Digital a Catalunya (i2CAT)
3
4//! BTP (Basic Transport Protocol) header structures.
5//!
6//! ETSI EN 302 636-5-1 defines two header variants:
7//!
8//! | Header | Field 1            | Field 2          | Use case                  |
9//! |--------|--------------------|------------------|---------------------------|
10//! | BTP-A  | Destination port   | Source port      | Acknowledged / unicast    |
11//! | BTP-B  | Destination port   | Port info        | Connectionless / broadcast|
12//!
13//! Both headers are 4 bytes long.
14
15use super::service_access_point::BTPDataRequest;
16
17// ------------------------------------------------------------------
18// BTP-A header
19// ------------------------------------------------------------------
20
21/// BTP-A header: carries both destination *and* source port numbers.
22///
23/// Used when the sender wants the receiver to be able to reply to a specific
24/// source port (connection-oriented / acknowledged communication).
25#[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    /// Build a BTP-A header from a [`BTPDataRequest`].
46    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    /// Return the destination port number.
54    pub fn destination_port(&self) -> u16 {
55        self.destination_port
56    }
57
58    /// Return the source port number.
59    pub fn source_port(&self) -> u16 {
60        self.source_port
61    }
62
63    /// Encode to 4 bytes (big-endian): destination port, source port.
64    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    /// Decode from 4 bytes.
72    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// ------------------------------------------------------------------
81// BTP-B header
82// ------------------------------------------------------------------
83
84/// BTP-B header: carries a destination port and a 16-bit port info field.
85///
86/// The port info field can carry additional protocol information (e.g. the
87/// TPDU protocol ID for IP-over-GN use cases).  Used for connectionless
88/// broadcast (CAM port 2001, DENM port 2002, …).
89#[derive(Debug)]
90pub struct BTPBHeader {
91    /// Destination port (e.g. 2001 for CAM, 2002 for DENM).
92    pub destination_port: u16,
93    /// Extra port-info field (typically 0 for standard applications).
94    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    /// Build a BTP-B header from a [`BTPDataRequest`].
112    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    /// Encode to 4 bytes (big-endian): destination port, port info.
120    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    /// Decode from 4 bytes.
128    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    // ── BTPAHeader ────────────────────────────────────────────────────────
142
143    #[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, // 2001
166            source_port: 0x1388,      // 5000
167        };
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    // ── BTPBHeader ────────────────────────────────────────────────────────
194
195    #[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,      // 2002
218            destination_port_info: 0x002A, // 42
219        };
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}