Skip to main content

rustflexstack/geonet/
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 super::gn_address::GNAddress;
5use super::position_vector::LongPositionVector;
6use crate::security::sn_sap::SecurityProfile;
7
8#[derive(Clone, PartialEq, Debug)]
9pub enum CommonNH {
10    Any,
11    BtpA,
12    BtpB,
13    IpV6,
14}
15
16impl CommonNH {
17    pub fn decode(value: u8) -> Self {
18        match value {
19            0 => CommonNH::Any,
20            1 => CommonNH::BtpA,
21            2 => CommonNH::BtpB,
22            3 => CommonNH::IpV6,
23            _ => panic!("Invalid Next Header Value!"),
24        }
25    }
26
27    pub fn encode(&self) -> u8 {
28        match self {
29            CommonNH::Any => 0,
30            CommonNH::BtpA => 1,
31            CommonNH::BtpB => 2,
32            CommonNH::IpV6 => 3,
33        }
34    }
35}
36
37#[derive(Clone, PartialEq, Debug)]
38pub enum HeaderType {
39    Any,
40    Beacon,
41    GeoUnicast,
42    GeoAnycast,
43    GeoBroadcast,
44    Tsb,
45    Ls,
46}
47
48impl HeaderType {
49    pub fn decode(value: u8) -> Self {
50        match value {
51            0 => HeaderType::Any,
52            1 => HeaderType::Beacon,
53            2 => HeaderType::GeoUnicast,
54            3 => HeaderType::GeoAnycast,
55            4 => HeaderType::GeoBroadcast,
56            5 => HeaderType::Tsb,
57            6 => HeaderType::Ls,
58            _ => panic!("Invalid Header Type Value!"),
59        }
60    }
61
62    pub fn encode(&self) -> u8 {
63        match self {
64            HeaderType::Any => 0,
65            HeaderType::Beacon => 1,
66            HeaderType::GeoUnicast => 2,
67            HeaderType::GeoAnycast => 3,
68            HeaderType::GeoBroadcast => 4,
69            HeaderType::Tsb => 5,
70            HeaderType::Ls => 6,
71        }
72    }
73}
74
75#[derive(Clone, PartialEq, Debug)]
76pub enum HeaderSubType {
77    Unspecified(UnspecifiedHST),
78    GeoAnycast(GeoAnycastHST),
79    GeoBroadcast(GeoBroadcastHST),
80    TopoBroadcast(TopoBroadcastHST),
81    LocationService(LocationServiceHST),
82}
83
84impl HeaderSubType {
85    pub fn decode(header_type: &HeaderType, value: u8) -> Self {
86        match header_type {
87            HeaderType::Any => HeaderSubType::Unspecified(UnspecifiedHST::decode(value)),
88            HeaderType::Beacon => HeaderSubType::Unspecified(UnspecifiedHST::decode(value)),
89            HeaderType::GeoUnicast => HeaderSubType::Unspecified(UnspecifiedHST::decode(value)),
90            HeaderType::GeoAnycast => HeaderSubType::GeoAnycast(GeoAnycastHST::decode(value)),
91            HeaderType::GeoBroadcast => HeaderSubType::GeoBroadcast(GeoBroadcastHST::decode(value)),
92            HeaderType::Tsb => HeaderSubType::TopoBroadcast(TopoBroadcastHST::decode(value)),
93            HeaderType::Ls => HeaderSubType::LocationService(LocationServiceHST::decode(value)),
94        }
95    }
96
97    pub fn encode(&self) -> u8 {
98        match self {
99            HeaderSubType::Unspecified(hst) => hst.encode(),
100            HeaderSubType::GeoAnycast(hst) => hst.encode(),
101            HeaderSubType::GeoBroadcast(hst) => hst.encode(),
102            HeaderSubType::TopoBroadcast(hst) => hst.encode(),
103            HeaderSubType::LocationService(hst) => hst.encode(),
104        }
105    }
106}
107
108#[derive(Clone, PartialEq, Copy, Debug)]
109pub enum UnspecifiedHST {
110    Unspecified,
111}
112
113impl UnspecifiedHST {
114    pub fn decode(value: u8) -> Self {
115        match value {
116            0 => UnspecifiedHST::Unspecified,
117            _ => panic!("Invalid Header Sub Type Value!"),
118        }
119    }
120
121    pub fn encode(&self) -> u8 {
122        match self {
123            UnspecifiedHST::Unspecified => 0,
124        }
125    }
126}
127
128#[derive(Clone, PartialEq, Copy, Debug)]
129pub enum GeoAnycastHST {
130    GeoAnycastCircle,
131    GeoAnycastRectangle,
132    GeoAnycastEllipse,
133}
134
135impl GeoAnycastHST {
136    pub fn encode(&self) -> u8 {
137        match self {
138            GeoAnycastHST::GeoAnycastCircle => 0,
139            GeoAnycastHST::GeoAnycastRectangle => 1,
140            GeoAnycastHST::GeoAnycastEllipse => 2,
141        }
142    }
143
144    pub fn decode(value: u8) -> Self {
145        match value {
146            0 => GeoAnycastHST::GeoAnycastCircle,
147            1 => GeoAnycastHST::GeoAnycastRectangle,
148            2 => GeoAnycastHST::GeoAnycastEllipse,
149            _ => panic!("Invalid GeoAnycast Header Sub Type Value!"),
150        }
151    }
152}
153
154#[derive(Clone, PartialEq, Copy, Debug)]
155pub enum GeoBroadcastHST {
156    GeoBroadcastCircle,
157    GeoBroadcastRectangle,
158    GeoBroadcastEllipse,
159}
160
161impl GeoBroadcastHST {
162    pub fn encode(&self) -> u8 {
163        match self {
164            GeoBroadcastHST::GeoBroadcastCircle => 0,
165            GeoBroadcastHST::GeoBroadcastRectangle => 1,
166            GeoBroadcastHST::GeoBroadcastEllipse => 2,
167        }
168    }
169
170    pub fn decode(value: u8) -> Self {
171        match value {
172            0 => GeoBroadcastHST::GeoBroadcastCircle,
173            1 => GeoBroadcastHST::GeoBroadcastRectangle,
174            2 => GeoBroadcastHST::GeoBroadcastEllipse,
175            _ => panic!("Invalid GeoBroadcast Header Sub Type Value!"),
176        }
177    }
178}
179
180#[derive(Clone, PartialEq, Copy, Debug)]
181pub enum TopoBroadcastHST {
182    SingleHop,
183    MultiHop,
184}
185
186impl TopoBroadcastHST {
187    pub fn encode(&self) -> u8 {
188        match self {
189            TopoBroadcastHST::SingleHop => 0,
190            TopoBroadcastHST::MultiHop => 1,
191        }
192    }
193
194    pub fn decode(value: u8) -> Self {
195        match value {
196            0 => TopoBroadcastHST::SingleHop,
197            1 => TopoBroadcastHST::MultiHop,
198            _ => panic!("Invalid TopoBroadcast Header Sub Type Value!"),
199        }
200    }
201}
202
203#[derive(Clone, PartialEq, Copy, Debug)]
204pub enum LocationServiceHST {
205    LsRequest,
206    LsReply,
207}
208
209impl LocationServiceHST {
210    pub fn encode(&self) -> u8 {
211        match self {
212            LocationServiceHST::LsRequest => 0,
213            LocationServiceHST::LsReply => 1,
214        }
215    }
216
217    pub fn decode(value: u8) -> Self {
218        match value {
219            0 => LocationServiceHST::LsRequest,
220            1 => LocationServiceHST::LsReply,
221            _ => panic!("Invalid LocationService Header Sub Type Value!"),
222        }
223    }
224}
225
226#[derive(Clone, PartialEq, Copy, Debug)]
227pub struct TrafficClass {
228    pub scf: bool,
229    pub channel_offload: bool,
230    pub tc_id: u8,
231}
232
233impl TrafficClass {
234    pub fn encode(&self) -> u8 {
235        let mut value: u8 = 0;
236        if self.scf {
237            value |= 0b1000_0000;
238        }
239        if self.channel_offload {
240            value |= 0b0100_0000;
241        }
242        value |= self.tc_id & 0b0011_1111;
243        value
244    }
245
246    pub fn decode(value: u8) -> Self {
247        let scf = value & 0b1000_0000 != 0;
248        let channel_offload = value & 0b0100_0000 != 0;
249        let tc_id = value & 0b0011_1111;
250        TrafficClass {
251            scf,
252            channel_offload,
253            tc_id,
254        }
255    }
256}
257
258#[derive(Clone, PartialEq, Debug)]
259pub struct PacketTransportType {
260    pub header_type: HeaderType,
261    pub header_sub_type: HeaderSubType,
262}
263
264#[derive(Clone, PartialEq)]
265pub enum CommunicationProfile {
266    Unspecified,
267}
268
269#[derive(Clone, PartialEq, Copy, Debug)]
270pub struct Area {
271    pub latitude: u32,
272    pub longitude: u32,
273    pub a: u16,
274    pub b: u16,
275    pub angle: u16,
276}
277
278pub struct GNDataRequest {
279    pub upper_protocol_entity: CommonNH,
280    pub packet_transport_type: PacketTransportType,
281    pub communication_profile: CommunicationProfile,
282    pub traffic_class: TrafficClass,
283    pub security_profile: SecurityProfile,
284    pub its_aid: u64,
285    pub security_permissions: Vec<u8>,
286    pub max_hop_limit: u8,
287    pub max_packet_lifetime: Option<f64>,
288    pub destination: Option<GNAddress>,
289    pub length: u16,
290    pub data: Vec<u8>,
291    pub area: Area,
292}
293
294#[derive(Debug, PartialEq)]
295pub enum ResultCode {
296    Accepted,
297    MaximumLengthExceeded,
298    MaximumLifetimeExceeded,
299    RepetitionIntervalTooSmall,
300    UnsupportedTrafficClass,
301    GeographicalScopeTooLarge,
302    Unspecified,
303}
304
305pub struct GNDataConfirm {
306    pub result_code: ResultCode,
307}
308
309/// Results of the security processing of a received secured packet
310/// (ETSI TS 103 836-4-1 V2.2.1, e.g. Table 38): report, signer certificate,
311/// ITS-AID and the SSP of the signer for that ITS-AID.
312#[derive(Clone, Debug, PartialEq)]
313pub struct SecurityIndication {
314    pub report: crate::security::sn_sap::ReportVerify,
315    pub certificate_id: Vec<u8>,
316    pub its_aid: u64,
317    pub permissions: Vec<u8>,
318}
319
320pub struct GNDataIndication {
321    pub upper_protocol_entity: CommonNH,
322    pub packet_transport_type: PacketTransportType,
323    pub source_position_vector: LongPositionVector,
324    pub traffic_class: TrafficClass,
325    pub destination_area: Option<Area>,
326    /// Destination address of a GeoUnicast (the DE PV GN_ADDR with which the source
327    /// generated it, TS 103 836-4-1 V2.2.1 clause J.4); `None` otherwise.
328    pub destination: Option<GNAddress>,
329    pub remaining_packet_lifetime: Option<f64>,
330    pub remaining_hop_limit: Option<u8>,
331    /// Security processing results; `None` for an unsecured packet.
332    pub security: Option<SecurityIndication>,
333    pub length: u16,
334    pub data: Vec<u8>,
335}
336
337#[cfg(test)]
338mod tests {
339    use super::*;
340
341    // ── CommonNH ──────────────────────────────────────────────────────
342
343    #[test]
344    fn common_nh_encode_decode_roundtrip() {
345        for (nh, val) in [
346            (CommonNH::Any, 0u8),
347            (CommonNH::BtpA, 1),
348            (CommonNH::BtpB, 2),
349            (CommonNH::IpV6, 3),
350        ] {
351            assert_eq!(nh.encode(), val);
352            assert_eq!(CommonNH::decode(val), nh);
353        }
354    }
355
356    #[test]
357    #[should_panic(expected = "Invalid Next Header Value")]
358    fn common_nh_decode_invalid() {
359        CommonNH::decode(99);
360    }
361
362    // ── HeaderType ────────────────────────────────────────────────────
363
364    #[test]
365    fn header_type_encode_decode_roundtrip() {
366        for (ht, val) in [
367            (HeaderType::Any, 0u8),
368            (HeaderType::Beacon, 1),
369            (HeaderType::GeoUnicast, 2),
370            (HeaderType::GeoAnycast, 3),
371            (HeaderType::GeoBroadcast, 4),
372            (HeaderType::Tsb, 5),
373            (HeaderType::Ls, 6),
374        ] {
375            assert_eq!(ht.encode(), val);
376            assert_eq!(HeaderType::decode(val), ht);
377        }
378    }
379
380    #[test]
381    #[should_panic(expected = "Invalid Header Type Value")]
382    fn header_type_decode_invalid() {
383        HeaderType::decode(99);
384    }
385
386    // ── HeaderSubType ─────────────────────────────────────────────────
387
388    #[test]
389    fn header_sub_type_unspecified() {
390        let hst = HeaderSubType::decode(&HeaderType::Any, 0);
391        assert_eq!(hst.encode(), 0);
392        assert!(matches!(hst, HeaderSubType::Unspecified(_)));
393    }
394
395    #[test]
396    fn header_sub_type_geo_anycast() {
397        for val in 0..3u8 {
398            let hst = HeaderSubType::decode(&HeaderType::GeoAnycast, val);
399            assert_eq!(hst.encode(), val);
400            assert!(matches!(hst, HeaderSubType::GeoAnycast(_)));
401        }
402    }
403
404    #[test]
405    fn header_sub_type_geo_broadcast() {
406        for val in 0..3u8 {
407            let hst = HeaderSubType::decode(&HeaderType::GeoBroadcast, val);
408            assert_eq!(hst.encode(), val);
409            assert!(matches!(hst, HeaderSubType::GeoBroadcast(_)));
410        }
411    }
412
413    #[test]
414    fn header_sub_type_topo_broadcast() {
415        let shb = HeaderSubType::decode(&HeaderType::Tsb, 0);
416        assert!(matches!(
417            shb,
418            HeaderSubType::TopoBroadcast(TopoBroadcastHST::SingleHop)
419        ));
420        let mhb = HeaderSubType::decode(&HeaderType::Tsb, 1);
421        assert!(matches!(
422            mhb,
423            HeaderSubType::TopoBroadcast(TopoBroadcastHST::MultiHop)
424        ));
425    }
426
427    #[test]
428    fn header_sub_type_location_service() {
429        let req = HeaderSubType::decode(&HeaderType::Ls, 0);
430        assert!(matches!(
431            req,
432            HeaderSubType::LocationService(LocationServiceHST::LsRequest)
433        ));
434        let rep = HeaderSubType::decode(&HeaderType::Ls, 1);
435        assert!(matches!(
436            rep,
437            HeaderSubType::LocationService(LocationServiceHST::LsReply)
438        ));
439    }
440
441    // ── TrafficClass ──────────────────────────────────────────────────
442
443    #[test]
444    fn traffic_class_encode_decode() {
445        let tc = TrafficClass {
446            scf: true,
447            channel_offload: false,
448            tc_id: 10,
449        };
450        let encoded = tc.encode();
451        let decoded = TrafficClass::decode(encoded);
452        assert_eq!(tc, decoded);
453    }
454
455    // ── GeoAnycastHST ─────────────────────────────────────────────────
456
457    #[test]
458    fn geo_anycast_hst_roundtrip() {
459        for val in 0..3u8 {
460            assert_eq!(GeoAnycastHST::decode(val).encode(), val);
461        }
462    }
463
464    #[test]
465    #[should_panic(expected = "Invalid GeoAnycast Header Sub Type Value")]
466    fn geo_anycast_hst_invalid() {
467        GeoAnycastHST::decode(99);
468    }
469
470    // ── GeoBroadcastHST ───────────────────────────────────────────────
471
472    #[test]
473    fn geo_broadcast_hst_roundtrip() {
474        for val in 0..3u8 {
475            assert_eq!(GeoBroadcastHST::decode(val).encode(), val);
476        }
477    }
478
479    #[test]
480    #[should_panic(expected = "Invalid GeoBroadcast Header Sub Type Value")]
481    fn geo_broadcast_hst_invalid() {
482        GeoBroadcastHST::decode(99);
483    }
484
485    // ── TopoBroadcastHST ──────────────────────────────────────────────
486
487    #[test]
488    fn topo_broadcast_hst_roundtrip() {
489        assert_eq!(TopoBroadcastHST::SingleHop.encode(), 0);
490        assert_eq!(TopoBroadcastHST::MultiHop.encode(), 1);
491        assert_eq!(TopoBroadcastHST::decode(0).encode(), 0);
492        assert_eq!(TopoBroadcastHST::decode(1).encode(), 1);
493    }
494
495    #[test]
496    #[should_panic(expected = "Invalid TopoBroadcast Header Sub Type Value")]
497    fn topo_broadcast_hst_invalid() {
498        TopoBroadcastHST::decode(99);
499    }
500
501    // ── LocationServiceHST ────────────────────────────────────────────
502
503    #[test]
504    fn location_service_hst_roundtrip() {
505        assert_eq!(LocationServiceHST::LsRequest.encode(), 0);
506        assert_eq!(LocationServiceHST::LsReply.encode(), 1);
507    }
508
509    #[test]
510    #[should_panic(expected = "Invalid LocationService Header Sub Type Value")]
511    fn location_service_hst_invalid() {
512        LocationServiceHST::decode(99);
513    }
514}