Skip to main content

rustflexstack/btp/
service_access_point.rs

1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2024 Fundació Privada Internet i Innovació Digital a Catalunya (i2CAT)
3
4use crate::geonet::gn_address::{GNAddress, M, MID, ST};
5use crate::geonet::position_vector::LongPositionVector;
6use crate::geonet::service_access_point::{
7    Area, CommonNH, CommunicationProfile, GNDataIndication, HeaderSubType, HeaderType,
8    PacketTransportType, TrafficClass, UnspecifiedHST,
9};
10use crate::security::sn_sap::SecurityProfile;
11
12/// BTP Data Request — ETSI EN 302 636-5-1 V2.2.1 Annex A.2.
13pub struct BTPDataRequest {
14    pub btp_type: CommonNH,
15    pub source_port: u16,
16    pub destination_port: u16,
17    pub destination_port_info: u16,
18    pub gn_packet_transport_type: PacketTransportType,
19    pub gn_destination_address: GNAddress,
20    pub gn_area: Area,
21    pub gn_max_hop_limit: u8,
22    pub gn_max_packet_lifetime: Option<f64>,
23    pub gn_repetition_interval: Option<u32>,
24    pub gn_max_repetition_time: Option<u32>,
25    pub communication_profile: CommunicationProfile,
26    pub traffic_class: TrafficClass,
27    pub security_profile: SecurityProfile,
28    pub its_aid: u64,
29    pub security_permissions: Vec<u8>,
30    pub destination: Option<GNAddress>,
31    pub length: u16,
32    pub data: Vec<u8>,
33}
34
35impl Default for BTPDataRequest {
36    fn default() -> Self {
37        Self::new()
38    }
39}
40
41impl BTPDataRequest {
42    pub fn new() -> Self {
43        BTPDataRequest {
44            btp_type: CommonNH::Any,
45            source_port: 0,
46            destination_port: 0,
47            destination_port_info: 0,
48            gn_packet_transport_type: PacketTransportType {
49                header_type: HeaderType::Any,
50                header_sub_type: HeaderSubType::Unspecified(UnspecifiedHST::Unspecified),
51            },
52            gn_destination_address: GNAddress {
53                m: M::GnUnicast,
54                st: ST::Unknown,
55                mid: MID::new([0; 6]),
56            },
57            gn_area: Area {
58                latitude: 0,
59                longitude: 0,
60                a: 0,
61                b: 0,
62                angle: 0,
63            },
64            gn_max_hop_limit: 1,
65            gn_max_packet_lifetime: None,
66            gn_repetition_interval: None,
67            gn_max_repetition_time: None,
68            communication_profile: CommunicationProfile::Unspecified,
69            traffic_class: TrafficClass {
70                scf: false,
71                channel_offload: false,
72                tc_id: 0,
73            },
74            security_profile: SecurityProfile::NoSecurity,
75            its_aid: 0,
76            security_permissions: vec![],
77            destination: None,
78            length: 0,
79            data: vec![],
80        }
81    }
82}
83
84/// BTP Data Indication — ETSI EN 302 636-5-1 V2.2.1 Annex A.3.
85pub struct BTPDataIndication {
86    pub source_port: u16,
87    pub destination_port: u16,
88    pub destination_port_info: u16,
89    pub gn_packet_transport_type: PacketTransportType,
90    pub gn_destination_address: GNAddress,
91    pub gn_source_position_vector: LongPositionVector,
92    pub gn_security_report: Option<Vec<u8>>,
93    pub gn_certificate_id: Option<Vec<u8>>,
94    pub gn_permissions: Option<Vec<u8>>,
95    /// ITS-AID (PSID) of a secured packet, which the receiving facility
96    /// checks against its own ITS-AID (e.g. ETSI TS 103 097 clause 7.1.1).
97    pub gn_its_aid: Option<u64>,
98    pub gn_traffic_class: TrafficClass,
99    pub gn_remaining_packet_lifetime: Option<f64>,
100    /// Destination area of a GeoBroadcast / GeoAnycast packet (the geographical
101    /// "GN destination address" of ETSI TS 103 836-5-1); `None` otherwise.
102    pub gn_destination_area: Option<Area>,
103    pub length: u16,
104    pub data: Vec<u8>,
105}
106
107impl Default for BTPDataIndication {
108    fn default() -> Self {
109        Self::new()
110    }
111}
112
113impl BTPDataIndication {
114    pub fn new() -> Self {
115        BTPDataIndication {
116            source_port: 0,
117            destination_port: 0,
118            destination_port_info: 0,
119            gn_packet_transport_type: PacketTransportType {
120                header_type: HeaderType::Any,
121                header_sub_type: HeaderSubType::Unspecified(UnspecifiedHST::Unspecified),
122            },
123            gn_destination_address: GNAddress {
124                m: M::GnUnicast,
125                st: ST::Unknown,
126                mid: MID::new([0; 6]),
127            },
128            gn_source_position_vector: LongPositionVector::decode([
129                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
130            ]),
131            gn_security_report: None,
132            gn_certificate_id: None,
133            gn_permissions: None,
134            gn_its_aid: None,
135            gn_traffic_class: TrafficClass {
136                scf: false,
137                channel_offload: false,
138                tc_id: 0,
139            },
140            gn_remaining_packet_lifetime: None,
141            gn_destination_area: None,
142            length: 0,
143            data: vec![],
144        }
145    }
146
147    /// Construct a BTPDataIndication from a GNDataIndication.
148    /// Strips the first 4 bytes (BTP header) from the payload.
149    pub fn initialize_with_gn_data_indication(gn_data_indication: &GNDataIndication) -> Self {
150        let payload = if gn_data_indication.data.len() > 4 {
151            gn_data_indication.data[4..].to_vec()
152        } else {
153            vec![]
154        };
155        let security = gn_data_indication.security.as_ref();
156        BTPDataIndication {
157            source_port: 0,
158            destination_port: 0,
159            destination_port_info: 0,
160            gn_packet_transport_type: gn_data_indication.packet_transport_type.clone(),
161            // EN 302 636-5-1 table 7: GN-Data.indication parameter Destination
162            // (GeoUnicast); all zero for the other packet types.
163            gn_destination_address: gn_data_indication.destination.unwrap_or(GNAddress {
164                m: M::GnUnicast,
165                st: ST::Unknown,
166                mid: MID::new([0; 6]),
167            }),
168            gn_source_position_vector: gn_data_indication.source_position_vector,
169            gn_security_report: security.map(|s| vec![s.report.code()]),
170            gn_certificate_id: security.map(|s| s.certificate_id.clone()),
171            gn_permissions: security.map(|s| s.permissions.clone()),
172            gn_its_aid: security.map(|s| s.its_aid),
173            gn_traffic_class: gn_data_indication.traffic_class,
174            gn_remaining_packet_lifetime: gn_data_indication.remaining_packet_lifetime,
175            gn_destination_area: gn_data_indication.destination_area,
176            length: payload.len() as u16,
177            data: payload,
178        }
179    }
180
181    /// Return a new indication with the destination port and port info set.
182    pub fn set_destination_port_and_info(
183        self,
184        destination_port: u16,
185        destination_port_info: u16,
186    ) -> Self {
187        BTPDataIndication {
188            destination_port,
189            destination_port_info,
190            ..self
191        }
192    }
193}
194
195#[cfg(test)]
196mod tests {
197    use super::*;
198    use crate::geonet::position_vector::LongPositionVector;
199    use crate::geonet::service_access_point::{
200        CommonNH, GNDataIndication, HeaderSubType, HeaderType, PacketTransportType,
201        TopoBroadcastHST, TrafficClass,
202    };
203
204    #[test]
205    fn btp_data_request_default() {
206        let req = BTPDataRequest::new();
207        assert_eq!(req.source_port, 0);
208        assert_eq!(req.destination_port, 0);
209        assert_eq!(req.destination_port_info, 0);
210        assert_eq!(req.gn_max_hop_limit, 1);
211        assert_eq!(req.length, 0);
212        assert!(req.data.is_empty());
213        assert!(req.gn_max_packet_lifetime.is_none());
214    }
215
216    #[test]
217    fn btp_data_indication_default() {
218        let ind = BTPDataIndication::new();
219        assert_eq!(ind.source_port, 0);
220        assert_eq!(ind.destination_port, 0);
221        assert_eq!(ind.length, 0);
222        assert!(ind.data.is_empty());
223        assert!(ind.gn_security_report.is_none());
224    }
225
226    #[test]
227    fn btp_data_indication_set_port() {
228        let ind = BTPDataIndication::new();
229        let updated = ind.set_destination_port_and_info(2001, 42);
230        assert_eq!(updated.destination_port, 2001);
231        assert_eq!(updated.destination_port_info, 42);
232        // Other fields should remain at defaults
233        assert_eq!(updated.source_port, 0);
234    }
235
236    #[test]
237    fn btp_data_indication_from_gn_data_indication() {
238        // Build a minimal GNDataIndication with 6 bytes payload (4 BTP header + 2 data)
239        let gn_ind = GNDataIndication {
240            upper_protocol_entity: CommonNH::BtpB,
241            packet_transport_type: PacketTransportType {
242                header_type: HeaderType::Tsb,
243                header_sub_type: HeaderSubType::TopoBroadcast(TopoBroadcastHST::SingleHop),
244            },
245            source_position_vector: LongPositionVector::decode([0u8; 24]),
246            traffic_class: TrafficClass {
247                scf: false,
248                channel_offload: false,
249                tc_id: 0,
250            },
251            destination_area: None,
252            destination: None,
253            remaining_packet_lifetime: Some(1.0),
254            remaining_hop_limit: None,
255            security: None,
256            length: 6,
257            data: vec![0x07, 0xD1, 0x00, 0x00, 0xAA, 0xBB],
258        };
259
260        let btp_ind = BTPDataIndication::initialize_with_gn_data_indication(&gn_ind);
261        // First 4 bytes (BTP header) should be stripped
262        assert_eq!(btp_ind.data, vec![0xAA, 0xBB]);
263        assert_eq!(btp_ind.length, 2);
264        assert_eq!(btp_ind.gn_remaining_packet_lifetime, Some(1.0));
265        assert_eq!(btp_ind.gn_its_aid, None);
266
267        // GN security results are passed in the BTP indication.
268        let secured = GNDataIndication {
269            security: Some(crate::geonet::service_access_point::SecurityIndication {
270                report: crate::security::sn_sap::ReportVerify::Success,
271                certificate_id: vec![1; 8],
272                its_aid: 36,
273                permissions: vec![2, 2, 0],
274            }),
275            ..gn_ind
276        };
277        let btp_ind = BTPDataIndication::initialize_with_gn_data_indication(&secured);
278        assert_eq!(btp_ind.gn_security_report, Some(vec![0]));
279        assert_eq!(btp_ind.gn_certificate_id, Some(vec![1; 8]));
280        assert_eq!(btp_ind.gn_its_aid, Some(36));
281        assert_eq!(btp_ind.gn_permissions, Some(vec![2, 2, 0]));
282    }
283
284    #[test]
285    fn btp_data_indication_from_gn_short_payload() {
286        let gn_ind = GNDataIndication {
287            upper_protocol_entity: CommonNH::BtpB,
288            packet_transport_type: PacketTransportType {
289                header_type: HeaderType::Tsb,
290                header_sub_type: HeaderSubType::TopoBroadcast(TopoBroadcastHST::SingleHop),
291            },
292            source_position_vector: LongPositionVector::decode([0u8; 24]),
293            traffic_class: TrafficClass {
294                scf: false,
295                channel_offload: false,
296                tc_id: 0,
297            },
298            destination_area: None,
299            destination: None,
300            remaining_packet_lifetime: None,
301            remaining_hop_limit: None,
302            security: None,
303            length: 2,
304            data: vec![0x07, 0xD1], // Only 2 bytes — too short for BTP header
305        };
306
307        let btp_ind = BTPDataIndication::initialize_with_gn_data_indication(&gn_ind);
308        assert!(btp_ind.data.is_empty());
309        assert_eq!(btp_ind.length, 0);
310    }
311}