Skip to main content

openthread/ot/
net_data.rs

1// Copyright 2022 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
7/// The maximum length of Thread network data, in bytes.
8pub const MAX_NET_DATA_LEN: usize = 255;
9
10/// Iterator type for on-mesh prefixes in network data.
11#[allow(missing_debug_implementations)]
12pub struct OnMeshPrefixIterator<'a, T: ?Sized> {
13    ot_instance: &'a T,
14    ot_iter: otNetworkDataIterator,
15}
16
17impl<T: ?Sized + NetData> Iterator for OnMeshPrefixIterator<'_, T> {
18    type Item = BorderRouterConfig;
19    fn next(&mut self) -> Option<Self::Item> {
20        self.ot_instance.iter_next_on_mesh_prefix(&mut self.ot_iter)
21    }
22}
23
24/// Methods from the [OpenThread "NetData" Module][1].
25///
26/// [1]: https://openthread.io/reference/group/api-thread-general
27pub trait NetData {
28    /// Functional equivalent of [`otsys::otNetDataGet`](crate::otsys::otNetDataGet).
29    fn net_data_get<'a>(&self, stable: bool, data: &'a mut [u8]) -> Result<&'a [u8]>;
30
31    /// Same as [`net_data_get`], but returns the net data as a vector.
32    fn net_data_as_vec(&self, stable: bool) -> Result<Vec<u8>> {
33        let mut ret = vec![0; MAX_NET_DATA_LEN];
34
35        let len = self.net_data_get(stable, ret.as_mut_slice())?.len();
36
37        ret.truncate(len);
38
39        Ok(ret)
40    }
41
42    /// Functional equivalent of [`otsys::otNetDataGetVersion`](crate::otsys::otNetDataGetVersion).
43    fn net_data_get_version(&self) -> u8;
44
45    /// Functional equivalent of
46    /// [`otsys::otNetDataGetStableVersion`](crate::otsys::otNetDataGetStableVersion).
47    fn net_data_get_stable_version(&self) -> u8;
48
49    /// Functional equivalent of [`otsys::otNetDataGetNextOnMeshPrefix`](crate::otsys::otNetDataGetNextOnMeshPrefix).
50    fn iter_next_on_mesh_prefix(
51        &self,
52        ot_iter: &mut otNetworkDataIterator,
53    ) -> Option<BorderRouterConfig>;
54
55    /// Returns an iterator for iterating over on-mesh prefixes.
56    fn iter_on_mesh_prefixes(&self) -> OnMeshPrefixIterator<'_, Self> {
57        OnMeshPrefixIterator { ot_instance: self, ot_iter: OT_NETWORK_DATA_ITERATOR_INIT }
58    }
59}
60
61impl<T: NetData + Boxable> NetData for ot::Box<T> {
62    fn net_data_get<'a>(&self, stable: bool, data: &'a mut [u8]) -> Result<&'a [u8]> {
63        self.as_ref().net_data_get(stable, data)
64    }
65
66    fn net_data_get_version(&self) -> u8 {
67        self.as_ref().net_data_get_version()
68    }
69
70    fn net_data_get_stable_version(&self) -> u8 {
71        self.as_ref().net_data_get_version()
72    }
73
74    fn iter_next_on_mesh_prefix(
75        &self,
76        ot_iter: &mut otNetworkDataIterator,
77    ) -> Option<BorderRouterConfig> {
78        self.as_ref().iter_next_on_mesh_prefix(ot_iter)
79    }
80}
81
82impl NetData for Instance {
83    fn net_data_get<'a>(&self, stable: bool, data: &'a mut [u8]) -> Result<&'a [u8]> {
84        let mut len: u8 = data.len().min(MAX_NET_DATA_LEN).try_into().unwrap();
85
86        Error::from(unsafe {
87            otNetDataGet(self.as_ot_ptr(), stable, data.as_mut_ptr(), (&mut len) as *mut u8)
88        })
89        .into_result()?;
90
91        Ok(&data[..(len as usize)])
92    }
93
94    fn net_data_get_version(&self) -> u8 {
95        unsafe { otNetDataGetVersion(self.as_ot_ptr()) }
96    }
97
98    fn net_data_get_stable_version(&self) -> u8 {
99        unsafe { otNetDataGetStableVersion(self.as_ot_ptr()) }
100    }
101
102    fn iter_next_on_mesh_prefix(
103        &self,
104        ot_iter: &mut otNetworkDataIterator,
105    ) -> Option<BorderRouterConfig> {
106        unsafe {
107            let mut ret = BorderRouterConfig::default();
108            match Error::from(otNetDataGetNextOnMeshPrefix(
109                self.as_ot_ptr(),
110                ot_iter as *mut otNetworkDataIterator,
111                ret.as_ot_mut_ptr(),
112            )) {
113                Error::NotFound => None,
114                Error::None => Some(ret),
115                err => panic!("Unexpected error from otNetDataGetNextOnMeshPrefix: {err:?}"),
116            }
117        }
118    }
119}