openthread/ot/types/
network_key.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2021 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::*;

/// Network Key.
/// Functional equivalent of [`otsys::otNetworkKey`](crate::otsys::otNetworkKey).
#[derive(Default, Copy, Clone)]
#[repr(transparent)]
pub struct NetworkKey(pub otNetworkKey);

impl_ot_castable!(NetworkKey, otNetworkKey);

impl std::fmt::Debug for NetworkKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "NetworkKey({})", hex::encode(self.as_slice()))
    }
}

impl NetworkKey {
    /// Returns the network key as a byte slice.
    pub fn as_slice(&self) -> &[u8] {
        &self.0.m8
    }

    /// Creates a `Vec<u8>` from this network key.
    pub fn to_vec(&self) -> Vec<u8> {
        self.as_slice().to_vec()
    }
}

impl From<[u8; OT_NETWORK_KEY_SIZE as usize]> for NetworkKey {
    fn from(key: [u8; OT_NETWORK_KEY_SIZE as usize]) -> Self {
        Self(otNetworkKey { m8: key })
    }
}

impl From<NetworkKey> for [u8; OT_NETWORK_KEY_SIZE as usize] {
    fn from(key: NetworkKey) -> Self {
        key.0.m8
    }
}

impl NetworkKey {
    /// Tries to create a `NetworkKey` reference from a byte slice.
    pub fn try_ref_from_slice(slice: &[u8]) -> Result<&NetworkKey, ot::WrongSize> {
        if slice.len() == OT_NETWORK_KEY_SIZE as usize {
            Ok(unsafe { Self::ref_from_ot_ptr((slice as *const [u8]) as *const otNetworkKey) }
                .unwrap())
        } else {
            Err(ot::WrongSize)
        }
    }
}

/// PSKC. Functional equivalent of [`otsys::otPskc`](crate::otsys::otPskc).
///
/// Used to establish the Commissioner Session.
#[derive(Default, Copy, Clone)]
#[repr(transparent)]
pub struct Pskc(pub otPskc);

impl_ot_castable!(Pskc, otPskc);

impl std::fmt::Debug for Pskc {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Pskc({:?})", hex::encode(self.as_slice()))
    }
}

impl Pskc {
    /// Returns the PSKC as a byte slice.
    pub fn as_slice(&self) -> &[u8] {
        &self.0.m8
    }

    /// Creates a `Vec<u8>` from this PSKC
    pub fn to_vec(&self) -> Vec<u8> {
        self.as_slice().to_vec()
    }
}

impl From<[u8; OT_PSKC_MAX_SIZE as usize]> for Pskc {
    fn from(key: [u8; OT_PSKC_MAX_SIZE as usize]) -> Self {
        Self(otPskc { m8: key })
    }
}

impl From<Pskc> for [u8; OT_PSKC_MAX_SIZE as usize] {
    fn from(key: Pskc) -> Self {
        key.0.m8
    }
}

impl Pskc {
    /// Tries to create a `Pskc` reference from a byte slice.
    pub fn try_ref_from_slice(slice: &[u8]) -> Result<&Pskc, ot::WrongSize> {
        if slice.len() == OT_PSKC_MAX_SIZE as usize {
            Ok(unsafe { Self::ref_from_ot_ptr((slice as *const [u8]) as *const otPskc) }.unwrap())
        } else {
            Err(ot::WrongSize)
        }
    }
}