openthread/ot/
trel.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// 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::*;

/// DNS-SD Service Name for TREL
pub const TREL_DNSSD_SERVICE_NAME: &str = "_trel._udp";

/// DNS-SD Service Name for TREL, with a dot at the end.
pub const TREL_DNSSD_SERVICE_NAME_WITH_DOT: &str = "_trel._udp.";

/// Methods from the [OpenThread TREL Module][1].
///
/// [1]: https://openthread.io/reference/group/api-trel
pub trait Trel {
    /// Enables or disables TREL operation.
    fn trel_set_enabled(&self, enabled: bool);

    /// Returns true if TREL is enabled.
    fn trel_is_enabled(&self) -> bool;

    /// Return all the TREL counters
    fn trel_get_counters(&self) -> Option<&TrelCounters>;

    /// Reset TREL counters
    fn trel_reset_counters(&self);

    /// Return the count of TREL peer
    fn trel_get_number_of_peers(&self) -> u16;
}

impl<T: Trel + Boxable> Trel for ot::Box<T> {
    fn trel_set_enabled(&self, enabled: bool) {
        self.as_ref().trel_set_enabled(enabled);
    }

    fn trel_is_enabled(&self) -> bool {
        self.as_ref().trel_is_enabled()
    }

    fn trel_get_counters(&self) -> Option<&TrelCounters> {
        self.as_ref().trel_get_counters()
    }

    fn trel_reset_counters(&self) {
        self.as_ref().trel_reset_counters()
    }

    fn trel_get_number_of_peers(&self) -> u16 {
        self.as_ref().trel_get_number_of_peers()
    }
}

impl Trel for Instance {
    fn trel_set_enabled(&self, enabled: bool) {
        unsafe { otTrelSetEnabled(self.as_ot_ptr(), enabled) }
    }

    fn trel_is_enabled(&self) -> bool {
        unsafe { otTrelIsEnabled(self.as_ot_ptr()) }
    }

    fn trel_get_counters(&self) -> Option<&TrelCounters> {
        unsafe { TrelCounters::ref_from_ot_ptr(otTrelGetCounters(self.as_ot_ptr())) }
    }

    fn trel_reset_counters(&self) {
        unsafe { otTrelResetCounters(self.as_ot_ptr()) }
    }

    fn trel_get_number_of_peers(&self) -> u16 {
        unsafe { otTrelGetNumberOfPeers(self.as_ot_ptr()) }
    }
}

/// Functional equivalent of [`otsys::otPlatTrelPeerInfo`](crate::otsys::otPlatTrelPeerInfo).
#[derive(Clone)]
#[repr(transparent)]
pub struct PlatTrelPeerInfo<'a>(
    otPlatTrelPeerInfo,
    PhantomData<*mut otMessage>,
    PhantomData<&'a ()>,
);
impl_ot_castable!(
    lifetime
    PlatTrelPeerInfo<'_>,
    otPlatTrelPeerInfo,
    PhantomData,
    PhantomData
);

impl std::fmt::Debug for PlatTrelPeerInfo<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PlatTrelPeerInfo")
            .field("removed", &self.is_removed())
            .field("txt", &self.txt_escaped())
            .field("sockaddr", &self.sockaddr())
            .finish()
    }
}

impl<'a> PlatTrelPeerInfo<'a> {
    /// Creates a new instance of `PlatTrelPeerInfo`.
    pub fn new(removed: bool, txt: &[u8], sockaddr: ot::SockAddr) -> PlatTrelPeerInfo<'_> {
        PlatTrelPeerInfo::from_ot(otPlatTrelPeerInfo {
            mRemoved: removed,
            mTxtData: txt.as_ptr(),
            mTxtLength: txt.len().try_into().unwrap(),
            mSockAddr: sockaddr.into_ot(),
        })
    }

    /// Returns true if this peer is being removed.
    pub fn is_removed(&self) -> bool {
        self.0.mRemoved
    }

    /// Returns the raw value of the TXT field.
    pub fn txt(&self) -> &'a [u8] {
        unsafe { core::slice::from_raw_parts(self.0.mTxtData, self.0.mTxtLength.into()) }
    }

    /// Returns the TXT field as an escaped ASCII string.
    pub fn txt_escaped(&self) -> String {
        self.txt()
            .iter()
            .map(Clone::clone)
            .flat_map(std::ascii::escape_default)
            .map(char::from)
            .collect::<String>()
    }

    /// Returns the SockAddr for this peer.
    pub fn sockaddr(&self) -> SockAddr {
        SockAddr::from_ot(self.0.mSockAddr)
    }
}

/// Platform methods from the [OpenThread TREL Module][1].
///
/// [1]: https://openthread.io/reference/group/plat-trel
pub trait PlatTrel {
    /// This function is a callback from platform to notify of a received TREL UDP packet.
    fn plat_trel_handle_received(&self, packet: &[u8]);

    /// This is a callback function from platform layer to report a discovered TREL peer info.
    fn plat_trel_handle_discovered_peer_info(&self, peer_info: &PlatTrelPeerInfo<'_>);
}

impl<T: PlatTrel + Boxable> PlatTrel for ot::Box<T> {
    fn plat_trel_handle_received(&self, packet: &[u8]) {
        self.as_ref().plat_trel_handle_received(packet);
    }

    fn plat_trel_handle_discovered_peer_info(&self, peer_info: &PlatTrelPeerInfo<'_>) {
        self.as_ref().plat_trel_handle_discovered_peer_info(peer_info);
    }
}

impl PlatTrel for Instance {
    fn plat_trel_handle_received(&self, packet: &[u8]) {
        unsafe {
            otPlatTrelHandleReceived(
                self.as_ot_ptr(),
                // TODO(https://fxbug.dev/42175496): Make sure they won't actually mutate.
                packet.as_ptr() as *mut u8,
                packet.len().try_into().unwrap(),
            )
        }
    }

    fn plat_trel_handle_discovered_peer_info(&self, peer_info: &PlatTrelPeerInfo<'_>) {
        unsafe { otPlatTrelHandleDiscoveredPeerInfo(self.as_ot_ptr(), peer_info.as_ot_ptr()) }
    }
}