Skip to main content

openthread/ot/types/
external_route_config.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::prelude_internal::*;
6
7use core::fmt::{Debug, Formatter};
8
9/// Data type representing an external route configuration.
10/// Functional equivalent of [`otsys::otExternalRouteConfig`](crate::otsys::otExternalRouteConfig).
11#[derive(Default, Clone, Copy)]
12#[repr(transparent)]
13pub struct ExternalRouteConfig(pub otExternalRouteConfig);
14
15impl_ot_castable!(ExternalRouteConfig, otExternalRouteConfig);
16
17impl Debug for ExternalRouteConfig {
18    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
19        self.prefix().fmt(f)?;
20        if self.is_stable() {
21            write!(f, " STABLE")?;
22        }
23        if self.is_next_hop_this_device() {
24            write!(f, " NEXT_HOP_IS_THIS_DEVICE")?;
25        }
26        Ok(())
27    }
28}
29
30impl std::fmt::Display for ExternalRouteConfig {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
32        std::fmt::Debug::fmt(self, f)
33    }
34}
35
36impl ExternalRouteConfig {
37    /// Creates a default, stable `ExternalRouteConfig` from the given `Ip6Prefix`.
38    pub fn from_prefix<T: Into<otIp6Prefix>>(prefix: T) -> ExternalRouteConfig {
39        let mut ret = ExternalRouteConfig(otExternalRouteConfig {
40            mPrefix: prefix.into(),
41            mRloc16: 0,
42            ..otExternalRouteConfig::default()
43        });
44        ret.set_stable(true);
45        ret
46    }
47
48    /// Returns the `Ip6Prefix` for this external route configuration.
49    pub fn prefix(&self) -> &Ip6Prefix {
50        (&self.0.mPrefix).into()
51    }
52
53    /// Returns the RLOC16 for the router that owns this external route configuration.
54    pub fn rloc16(&self) -> u16 {
55        self.0.mRloc16
56    }
57
58    /// Returns the route preference.
59    /// Since mPreference remains an unsigned value after conversion from otExternalRouteConfig
60    /// (see binding.rs), so explicitly sign-extend it from a u32 to an i32 here to ensure the
61    /// expected values of -1, 0, or 1.
62    pub fn route_preference(&self) -> RoutePreference {
63        RoutePreference::from_i32((self.0.mPreference() << 30) >> 30)
64            .expect("Invalid route preference")
65    }
66
67    /// Sets the route preference.
68    pub fn set_route_preference(&mut self, pref: RoutePreference) {
69        self.0.set_mPreference(pref as i32);
70    }
71
72    /// Returns the value of the `stable` flag.
73    pub fn is_stable(&self) -> bool {
74        self.0.mStable()
75    }
76
77    /// Sets the value of the `stable` flag.
78    pub fn set_stable(&mut self, x: bool) {
79        self.0.set_mStable(x)
80    }
81
82    /// Returns true if the next hop for this route is this device.
83    pub fn is_next_hop_this_device(&self) -> bool {
84        self.0.mNextHopIsThisDevice()
85    }
86
87    /// Returns whether this is a NAT64 prefix.
88    pub fn is_nat64(&self) -> bool {
89        self.0.mNat64()
90    }
91
92    /// Returns whether or not BR is advertising a ULA prefix in PIO (AP flag).
93    pub fn is_adv_pio(&self) -> bool {
94        self.0.mAdvPio()
95    }
96}