Skip to main content

openthread/ot/types/
link_mode.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
7bitflags::bitflags! {
8    /// Link Mode Config.
9    /// Functional equivalent of [`otsys::otLinkModeConfig`](crate::otsys::otLinkModeConfig).
10    #[repr(C)]
11    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
12    pub struct LinkModeConfig : u8 {
13        /// Set if the sender is a Full Thread Device (FTD); clear if a Minimal Thread Device (MTD).
14        ///
15        /// See page 4-8 of the Thread 1.1.1 specification for more details.
16        const IS_FTD = (1<<1);
17
18        /// Set if the sender requires the full Network Data; clear if the sender only needs the stable Network Data.
19        ///
20        /// See page 4-8 of the Thread 1.1.1 specification for more details.
21        const NETWORK_DATA = (1<<0);
22
23        /// Set if the sender has its receiver on when not transmitting, cleared otherwise.
24        /// Only an MTD acting as a SED will set this flag.
25        ///
26        /// See page 4-8 of the Thread 1.1.1 specification for more details.
27        const RX_ON_WHEN_IDLE = (1<<3);
28    }
29}
30
31impl LinkModeConfig {
32    /// Returns true if the mode indicates a Full Thread Device (FTD)
33    pub fn is_ftd(&self) -> bool {
34        self.contains(Self::IS_FTD)
35    }
36
37    /// Returns true if the mode indicates a Minimal Thread Device (MTD)
38    pub fn is_mtd(&self) -> bool {
39        !self.is_ftd()
40    }
41
42    /// Returns true if the sender has its receiver on when not transmitting.
43    pub fn rx_on_while_idle(&self) -> bool {
44        self.contains(Self::RX_ON_WHEN_IDLE)
45    }
46
47    /// Returns true if the sender requires the full Network Data.
48    pub fn full_network_data(&self) -> bool {
49        self.contains(Self::NETWORK_DATA)
50    }
51}
52
53impl From<otLinkModeConfig> for LinkModeConfig {
54    fn from(x: otLinkModeConfig) -> Self {
55        let mut ret = Self::default();
56        if x.mDeviceType() {
57            ret |= LinkModeConfig::IS_FTD;
58        }
59        if x.mNetworkData() {
60            ret |= LinkModeConfig::NETWORK_DATA;
61        }
62        if x.mRxOnWhenIdle() {
63            ret |= LinkModeConfig::RX_ON_WHEN_IDLE;
64        }
65        ret
66    }
67}
68
69impl From<LinkModeConfig> for otLinkModeConfig {
70    fn from(x: LinkModeConfig) -> Self {
71        let mut ret = Self::default();
72        ret.set_mDeviceType(x.contains(LinkModeConfig::IS_FTD));
73        ret.set_mNetworkData(x.contains(LinkModeConfig::NETWORK_DATA));
74        ret.set_mRxOnWhenIdle(x.contains(LinkModeConfig::RX_ON_WHEN_IDLE));
75
76        ret
77    }
78}