Skip to main content

rustflexstack/geonet/
mib.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, M, MID, ST};
5
6#[derive(Clone, Copy, Debug, PartialEq)]
7pub enum LocalGnAddrConfMethod {
8    Auto,
9    Managed,
10    Anonymous,
11}
12
13impl LocalGnAddrConfMethod {
14    pub fn encode(&self) -> u8 {
15        match self {
16            LocalGnAddrConfMethod::Auto => 0,
17            LocalGnAddrConfMethod::Managed => 1,
18            LocalGnAddrConfMethod::Anonymous => 2,
19        }
20    }
21
22    pub fn decode(value: u8) -> Self {
23        match value {
24            0 => LocalGnAddrConfMethod::Auto,
25            1 => LocalGnAddrConfMethod::Managed,
26            2 => LocalGnAddrConfMethod::Anonymous,
27            _ => panic!("Invalid LocalGnAddrConfMethod Value"),
28        }
29    }
30}
31
32#[derive(Clone, Copy, Debug, PartialEq)]
33pub enum GnIsMobile {
34    Stationary,
35    Mobile,
36}
37
38impl GnIsMobile {
39    pub fn encode(&self) -> u8 {
40        match self {
41            GnIsMobile::Stationary => 0,
42            GnIsMobile::Mobile => 1,
43        }
44    }
45
46    pub fn decode(value: u8) -> Self {
47        match value {
48            0 => GnIsMobile::Stationary,
49            1 => GnIsMobile::Mobile,
50            _ => panic!("Invalid GnIsMobile Value"),
51        }
52    }
53}
54
55#[derive(Clone, Copy, Debug, PartialEq)]
56pub enum GnIfType {
57    Unspecified,
58    ItsG5,
59    LteV2x,
60}
61
62impl GnIfType {
63    pub fn encode(&self) -> u8 {
64        match self {
65            GnIfType::Unspecified => 0,
66            GnIfType::ItsG5 => 1,
67            GnIfType::LteV2x => 2,
68        }
69    }
70
71    pub fn decode(value: u8) -> Self {
72        match value {
73            0 => GnIfType::Unspecified,
74            1 => GnIfType::ItsG5,
75            2 => GnIfType::LteV2x,
76            _ => panic!("Invalid GnIfType Value"),
77        }
78    }
79}
80
81#[derive(Clone, Copy, Debug, PartialEq)]
82pub enum GnSecurity {
83    Disabled,
84    Enabled,
85}
86
87impl GnSecurity {
88    pub fn encode(&self) -> u8 {
89        match self {
90            GnSecurity::Disabled => 0,
91            GnSecurity::Enabled => 1,
92        }
93    }
94
95    pub fn decode(value: u8) -> Self {
96        match value {
97            0 => GnSecurity::Disabled,
98            1 => GnSecurity::Enabled,
99            _ => panic!("Invalid GnSecurity Value"),
100        }
101    }
102}
103
104#[derive(Clone, Copy, Debug, PartialEq)]
105pub enum SnDecapResultHandling {
106    Strict,
107    NonStrict,
108}
109
110impl SnDecapResultHandling {
111    pub fn encode(&self) -> u8 {
112        match self {
113            SnDecapResultHandling::Strict => 0,
114            SnDecapResultHandling::NonStrict => 1,
115        }
116    }
117
118    pub fn decode(value: u8) -> Self {
119        match value {
120            0 => SnDecapResultHandling::Strict,
121            1 => SnDecapResultHandling::NonStrict,
122            _ => panic!("Invalid SnDecapResultHandling Value"),
123        }
124    }
125}
126
127#[derive(Clone, Copy, Debug, PartialEq)]
128pub enum NonAreaForwardingAlgorithm {
129    Unspecified,
130    Greedy,
131    /// Non-area contention-based forwarding (ETSI TS 103 836-4-1 V2.2.1, clause E.3).
132    Cbf,
133}
134
135impl NonAreaForwardingAlgorithm {
136    pub fn encode(&self) -> u8 {
137        match self {
138            NonAreaForwardingAlgorithm::Unspecified => 0,
139            NonAreaForwardingAlgorithm::Greedy => 1,
140            NonAreaForwardingAlgorithm::Cbf => 2,
141        }
142    }
143
144    pub fn decode(value: u8) -> Self {
145        match value {
146            0 => NonAreaForwardingAlgorithm::Unspecified,
147            1 => NonAreaForwardingAlgorithm::Greedy,
148            2 => NonAreaForwardingAlgorithm::Cbf,
149            _ => panic!("Invalid NonAreaForwardingAlgorithm Value"),
150        }
151    }
152}
153
154#[derive(Clone, Copy, Debug, PartialEq)]
155pub enum AreaForwardingAlgorithm {
156    Unspecified,
157    Simple,
158    Cbf,
159}
160
161impl AreaForwardingAlgorithm {
162    pub fn encode(&self) -> u8 {
163        match self {
164            AreaForwardingAlgorithm::Unspecified => 0,
165            AreaForwardingAlgorithm::Simple => 1,
166            AreaForwardingAlgorithm::Cbf => 2,
167        }
168    }
169
170    pub fn decode(value: u8) -> Self {
171        match value {
172            0 => AreaForwardingAlgorithm::Unspecified,
173            1 => AreaForwardingAlgorithm::Simple,
174            2 => AreaForwardingAlgorithm::Cbf,
175            _ => panic!("Invalid AreaForwardingAlgorithm Value"),
176        }
177    }
178}
179
180#[allow(non_snake_case)]
181#[derive(Clone, Copy, Debug, PartialEq)]
182pub struct Mib {
183    pub itsGnLocalGnAddr: GNAddress,
184    pub itsGnLocalGnAddrConfMethod: LocalGnAddrConfMethod,
185    pub itsGnProtocolVersion: u8,
186    pub itsGnIsMobile: GnIsMobile,
187    pub itsGnIfType: GnIfType,
188    pub itsGnMinUpdateFrequencyEPV: u16,
189    pub itsGnPaiInterval: u8,
190    pub itsGnMaxSduSize: u16,
191    pub itsGnMaxGeoNetworkingHeaderSize: u16,
192    pub itsGnLifetimeLocTE: u16,
193    pub itsGnSecurity: GnSecurity,
194    pub itsGnSnDecapResultHandling: SnDecapResultHandling,
195    pub itsGnLocationServiceMaxRetrans: u8,
196    pub itsGnLocationServiceRetransmitTimer: u16,
197    pub itsGnLocationServicePacketBufferSize: u16,
198    pub itsGnBeaconServiceRetransmitTimer: u16,
199    pub itsGnBeaconServiceMaxJitter: u16,
200    pub itsGnDefaultHopLimit: u8,
201    pub itsGnDPLLength: u8,
202    pub itsGnMaxPacketLifetime: u16,
203    pub itsGnDefaultPacketLifetime: u8,
204    pub itsGnMaxPacketDataRate: u32,
205    pub itsGnMaxPacketDataRateEmaBeta: u8,
206    pub itsGnMaxGeoAreaSize: u16,
207    pub itsGnMinPacketRepetitionInterval: u16,
208    pub itsGnNonAreaForwardingAlgorithm: NonAreaForwardingAlgorithm,
209    pub itsGnAreaForwardingAlgorithm: AreaForwardingAlgorithm,
210    pub itsGnCbfMinTime: u16,
211    pub itsGnCbfMaxTime: u16,
212    pub itsGnDefaultMaxCommunicationRange: u16,
213    pub itsGnBroadcastCBFDefSectorAngle: u8,
214    pub itsGnUcForwardingPacketBufferSize: u16,
215    pub itsGnBcForwardingPacketBufferSize: u16,
216    pub itsGnCbfPacketBufferSize: u16,
217    pub itsGnDefaultTrafficClass: u8,
218}
219
220impl Default for Mib {
221    fn default() -> Self {
222        Self::new()
223    }
224}
225
226impl Mib {
227    /// Whether itsGnLocalGnAddr follows the media-dependent rule of
228    /// itsGnIfType: ITS-G5, MID = MAC address of the interface (ETSI TS
229    /// 103 836-4-2 V2.1.1 clause 6.1); LTE-V2X, MID = 24-bit Source Layer 2
230    /// ID followed by three zero octets (ETSI TS 103 836-4-3 V2.1.1 clause
231    /// 5.2). `link_layer_mid` is the MID reported by the link layer; when it
232    /// is unknown only the LTE-V2X zero octets are checked. Always true for
233    /// an unspecified interface type.
234    pub fn media_dependent_address_ok(&self, link_layer_mid: Option<MID>) -> bool {
235        let mid = self.itsGnLocalGnAddr.mid.as_bytes();
236        match self.itsGnIfType {
237            GnIfType::Unspecified => true,
238            GnIfType::LteV2x if mid[3..] != [0, 0, 0] => false,
239            _ => link_layer_mid.is_none_or(|l| l.as_bytes() == mid),
240        }
241    }
242
243    pub fn new() -> Self {
244        {
245            Mib {
246                itsGnLocalGnAddr: GNAddress::new(
247                    M::GnUnicast,
248                    ST::Unknown,
249                    MID::new([0, 0, 0, 0, 0, 0]),
250                ),
251                itsGnLocalGnAddrConfMethod: LocalGnAddrConfMethod::Auto,
252                itsGnProtocolVersion: 1,
253                itsGnIsMobile: GnIsMobile::Mobile,
254                itsGnIfType: GnIfType::Unspecified,
255                itsGnMinUpdateFrequencyEPV: 1000,
256                itsGnPaiInterval: 80,
257                itsGnMaxSduSize: 1398,
258                itsGnMaxGeoNetworkingHeaderSize: 88,
259                itsGnLifetimeLocTE: 20,
260                itsGnSecurity: GnSecurity::Disabled,
261                itsGnSnDecapResultHandling: SnDecapResultHandling::Strict,
262                itsGnLocationServiceMaxRetrans: 10,
263                itsGnLocationServiceRetransmitTimer: 1000,
264                itsGnLocationServicePacketBufferSize: 1024,
265                itsGnBeaconServiceRetransmitTimer: 3000,
266                itsGnBeaconServiceMaxJitter: 3000 / 4,
267                itsGnDefaultHopLimit: 10,
268                itsGnDPLLength: 8,
269                itsGnMaxPacketLifetime: 600,
270                itsGnDefaultPacketLifetime: 60,
271                itsGnMaxPacketDataRate: 100,
272                itsGnMaxPacketDataRateEmaBeta: 90,
273                itsGnMaxGeoAreaSize: 10,
274                itsGnMinPacketRepetitionInterval: 100,
275                itsGnNonAreaForwardingAlgorithm: NonAreaForwardingAlgorithm::Greedy,
276                itsGnAreaForwardingAlgorithm: AreaForwardingAlgorithm::Cbf,
277                itsGnCbfMinTime: 1,
278                itsGnCbfMaxTime: 100,
279                itsGnDefaultMaxCommunicationRange: 1000,
280                itsGnBroadcastCBFDefSectorAngle: 30,
281                itsGnUcForwardingPacketBufferSize: 256,
282                itsGnBcForwardingPacketBufferSize: 1024,
283                itsGnCbfPacketBufferSize: 256,
284                itsGnDefaultTrafficClass: 0,
285            }
286        }
287    }
288}
289
290#[cfg(test)]
291mod tests {
292    use super::*;
293
294    #[test]
295    fn mib_default_values() {
296        let mib = Mib::new();
297        assert_eq!(mib.itsGnProtocolVersion, 1);
298        assert_eq!(mib.itsGnDefaultHopLimit, 10);
299        assert_eq!(mib.itsGnMaxSduSize, 1398);
300        assert_eq!(mib.itsGnDefaultPacketLifetime, 60);
301        assert_eq!(mib.itsGnMaxPacketLifetime, 600);
302        assert_eq!(mib.itsGnBeaconServiceRetransmitTimer, 3000);
303        assert!(matches!(mib.itsGnIsMobile, GnIsMobile::Mobile));
304        assert!(matches!(mib.itsGnSecurity, GnSecurity::Disabled));
305        assert!(matches!(
306            mib.itsGnNonAreaForwardingAlgorithm,
307            NonAreaForwardingAlgorithm::Greedy
308        ));
309        assert!(matches!(
310            mib.itsGnAreaForwardingAlgorithm,
311            AreaForwardingAlgorithm::Cbf
312        ));
313    }
314
315    #[test]
316    fn mib_copy() {
317        let mib1 = Mib::new();
318        let mib2 = mib1;
319        assert_eq!(mib1, mib2);
320    }
321
322    #[test]
323    fn local_gn_addr_conf_method_roundtrip() {
324        for val in 0..3u8 {
325            assert_eq!(LocalGnAddrConfMethod::decode(val).encode(), val);
326        }
327    }
328
329    #[test]
330    #[should_panic(expected = "Invalid LocalGnAddrConfMethod Value")]
331    fn local_gn_addr_conf_method_invalid() {
332        LocalGnAddrConfMethod::decode(99);
333    }
334
335    #[test]
336    fn gn_is_mobile_roundtrip() {
337        assert_eq!(GnIsMobile::Stationary.encode(), 0);
338        assert_eq!(GnIsMobile::Mobile.encode(), 1);
339        assert_eq!(GnIsMobile::decode(0).encode(), 0);
340        assert_eq!(GnIsMobile::decode(1).encode(), 1);
341    }
342
343    #[test]
344    #[should_panic(expected = "Invalid GnIsMobile Value")]
345    fn gn_is_mobile_invalid() {
346        GnIsMobile::decode(99);
347    }
348
349    #[test]
350    fn gn_if_type_roundtrip() {
351        for val in 0..3u8 {
352            assert_eq!(GnIfType::decode(val).encode(), val);
353        }
354    }
355
356    #[test]
357    #[should_panic(expected = "Invalid GnIfType Value")]
358    fn gn_if_type_invalid() {
359        GnIfType::decode(99);
360    }
361
362    #[test]
363    fn gn_security_roundtrip() {
364        assert_eq!(GnSecurity::Disabled.encode(), 0);
365        assert_eq!(GnSecurity::Enabled.encode(), 1);
366    }
367
368    #[test]
369    #[should_panic(expected = "Invalid GnSecurity Value")]
370    fn gn_security_invalid() {
371        GnSecurity::decode(99);
372    }
373
374    #[test]
375    fn sn_decap_result_handling_roundtrip() {
376        assert_eq!(SnDecapResultHandling::Strict.encode(), 0);
377        assert_eq!(SnDecapResultHandling::NonStrict.encode(), 1);
378    }
379
380    #[test]
381    #[should_panic(expected = "Invalid SnDecapResultHandling Value")]
382    fn sn_decap_result_handling_invalid() {
383        SnDecapResultHandling::decode(99);
384    }
385
386    #[test]
387    fn non_area_forwarding_algorithm_roundtrip() {
388        assert_eq!(NonAreaForwardingAlgorithm::Unspecified.encode(), 0);
389        assert_eq!(NonAreaForwardingAlgorithm::Greedy.encode(), 1);
390    }
391
392    #[test]
393    #[should_panic(expected = "Invalid NonAreaForwardingAlgorithm Value")]
394    fn non_area_forwarding_algorithm_invalid() {
395        NonAreaForwardingAlgorithm::decode(99);
396    }
397
398    #[test]
399    fn area_forwarding_algorithm_roundtrip() {
400        for val in 0..3u8 {
401            assert_eq!(AreaForwardingAlgorithm::decode(val).encode(), val);
402        }
403    }
404
405    #[test]
406    #[should_panic(expected = "Invalid AreaForwardingAlgorithm Value")]
407    fn area_forwarding_algorithm_invalid() {
408        AreaForwardingAlgorithm::decode(99);
409    }
410}