openthread/ot/
net_data.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::prelude_internal::*;

/// The maximum length of Thread network data, in bytes.
pub const MAX_NET_DATA_LEN: usize = 255;

/// Methods from the [OpenThread "NetData" Module][1].
///
/// [1]: https://openthread.io/reference/group/api-thread-general
pub trait NetData {
    /// Functional equivalent of [`otsys::otNetDataGet`](crate::otsys::otNetDataGet).
    fn net_data_get<'a>(&self, stable: bool, data: &'a mut [u8]) -> Result<&'a [u8]>;

    /// Same as [`net_data_get`], but returns the net data as a vector.
    fn net_data_as_vec(&self, stable: bool) -> Result<Vec<u8>> {
        let mut ret = vec![0; MAX_NET_DATA_LEN];

        let len = self.net_data_get(stable, ret.as_mut_slice())?.len();

        ret.truncate(len);

        Ok(ret)
    }

    /// Functional equivalent of [`otsys::otNetDataGetVersion`](crate::otsys::otNetDataGetVersion).
    fn net_data_get_version(&self) -> u8;

    /// Functional equivalent of
    /// [`otsys::otNetDataGetStableVersion`](crate::otsys::otNetDataGetStableVersion).
    fn net_data_get_stable_version(&self) -> u8;
}

impl<T: NetData + Boxable> NetData for ot::Box<T> {
    fn net_data_get<'a>(&self, stable: bool, data: &'a mut [u8]) -> Result<&'a [u8]> {
        self.as_ref().net_data_get(stable, data)
    }

    fn net_data_get_version(&self) -> u8 {
        self.as_ref().net_data_get_version()
    }

    fn net_data_get_stable_version(&self) -> u8 {
        self.as_ref().net_data_get_version()
    }
}

impl NetData for Instance {
    fn net_data_get<'a>(&self, stable: bool, data: &'a mut [u8]) -> Result<&'a [u8]> {
        let mut len: u8 = data.len().min(MAX_NET_DATA_LEN).try_into().unwrap();

        Error::from(unsafe {
            otNetDataGet(self.as_ot_ptr(), stable, data.as_mut_ptr(), (&mut len) as *mut u8)
        })
        .into_result()?;

        Ok(&data[..(len as usize)])
    }

    fn net_data_get_version(&self) -> u8 {
        unsafe { otNetDataGetVersion(self.as_ot_ptr()) }
    }

    fn net_data_get_stable_version(&self) -> u8 {
        unsafe { otNetDataGetStableVersion(self.as_ot_ptr()) }
    }
}