openthread/ot/
net_data.rs1use crate::prelude_internal::*;
6
7pub const MAX_NET_DATA_LEN: usize = 255;
9
10pub trait NetData {
14 fn net_data_get<'a>(&self, stable: bool, data: &'a mut [u8]) -> Result<&'a [u8]>;
16
17 fn net_data_as_vec(&self, stable: bool) -> Result<Vec<u8>> {
19 let mut ret = vec![0; MAX_NET_DATA_LEN];
20
21 let len = self.net_data_get(stable, ret.as_mut_slice())?.len();
22
23 ret.truncate(len);
24
25 Ok(ret)
26 }
27
28 fn net_data_get_version(&self) -> u8;
30
31 fn net_data_get_stable_version(&self) -> u8;
34}
35
36impl<T: NetData + Boxable> NetData for ot::Box<T> {
37 fn net_data_get<'a>(&self, stable: bool, data: &'a mut [u8]) -> Result<&'a [u8]> {
38 self.as_ref().net_data_get(stable, data)
39 }
40
41 fn net_data_get_version(&self) -> u8 {
42 self.as_ref().net_data_get_version()
43 }
44
45 fn net_data_get_stable_version(&self) -> u8 {
46 self.as_ref().net_data_get_version()
47 }
48}
49
50impl NetData for Instance {
51 fn net_data_get<'a>(&self, stable: bool, data: &'a mut [u8]) -> Result<&'a [u8]> {
52 let mut len: u8 = data.len().min(MAX_NET_DATA_LEN).try_into().unwrap();
53
54 Error::from(unsafe {
55 otNetDataGet(self.as_ot_ptr(), stable, data.as_mut_ptr(), (&mut len) as *mut u8)
56 })
57 .into_result()?;
58
59 Ok(&data[..(len as usize)])
60 }
61
62 fn net_data_get_version(&self) -> u8 {
63 unsafe { otNetDataGetVersion(self.as_ot_ptr()) }
64 }
65
66 fn net_data_get_stable_version(&self) -> u8 {
67 unsafe { otNetDataGetStableVersion(self.as_ot_ptr()) }
68 }
69}