openthread/ot/
link.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// 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::*;
use std::time::Duration;

/// Methods from the [OpenThread "Link" Module][1].
///
/// [1]: https://openthread.io/reference/group/api-link-link
pub trait Link {
    /// Functional equivalent of [`otsys::otLinkGetChannel`](crate::otsys::otLinkGetChannel).
    fn get_channel(&self) -> ChannelIndex;

    /// Functional equivalent of [`otsys::otLinkSetChannel`](crate::otsys::otLinkSetChannel).
    fn set_channel(&self, index: ChannelIndex) -> Result;

    /// Functional equivalent of [`otsys::otLinkGetCounters`](crate::otsys::otLinkGetCounters).
    fn link_get_counters(&self) -> &MacCounters;

    /// Functional equivalent of
    /// [`otsys::otLinkGetExtendedAddress`](crate::otsys::otLinkGetExtendedAddress).
    fn get_extended_address(&self) -> &ExtAddress;

    /// Functional equivalent of
    /// [`otsys::otLinkGetFactoryAssignedIeeeEui64`](crate::otsys::otLinkGetFactoryAssignedIeeeEui64).
    fn get_factory_assigned_ieee_eui_64(&self) -> ExtAddress;

    /// Functional equivalent of [`otsys::otLinkGetPanId`](crate::otsys::otLinkGetPanId).
    fn get_pan_id(&self) -> PanId;

    /// Functional equivalent of [`otsys::otLinkSetPanId`](crate::otsys::otLinkSetPanId).
    fn set_pan_id(&self, pan_id: PanId) -> Result;

    /// Functional equivalent of [`otsys::otLinkGetShortAddress`](crate::otsys::otLinkGetShortAddress).
    fn get_short_address(&self) -> ShortAddress;

    /// Functional equivalent of [`otsys::otLinkIsEnabled`](crate::otsys::otLinkIsEnabled).
    fn link_is_enabled(&self) -> bool;

    /// Functional equivalent of [`otsys::otLinkSetEnabled`](crate::otsys::otLinkSetEnabled).
    fn link_set_enabled(&self, enabled: bool) -> Result;

    /// Functional equivalent of [`otsys::otLinkIsPromiscuous`](crate::otsys::otLinkIsPromiscuous).
    fn link_is_promiscuous(&self) -> bool;

    /// Functional equivalent of
    /// [`otsys::otLinkSetPromiscuous`](crate::otsys::otLinkSetPromiscuous).
    fn link_set_promiscuous(&self, promiscuous: bool) -> Result;

    /// Functional equivalent of
    /// [`otsys::otLinkIsEnergyScanInProgress`](crate::otsys::otLinkIsEnergyScanInProgress).
    fn is_energy_scan_in_progress(&self) -> bool;

    /// Functional equivalent of
    /// [`otsys::otLinkIsActiveScanInProgress`](crate::otsys::otLinkIsActiveScanInProgress).
    fn is_active_scan_in_progress(&self) -> bool;

    /// Functional equivalent of
    /// [`otsys::otLinkGetSupportedChannelMask`](crate::otsys::otLinkGetSupportedChannelMask).
    fn get_supported_channel_mask(&self) -> ot::ChannelMask;

    /// Starts an active scan. Functional equivalent of
    /// [`otsys::otLinkActiveScan`](crate::otsys::otLinkActiveScan).
    ///
    /// The closure will ultimately be executed via
    /// [`ot::Tasklets::process`](crate::ot::Tasklets::process).
    fn start_active_scan<'a, F>(&self, channels: ChannelMask, dwell: Duration, f: F) -> Result
    where
        F: FnMut(Option<&ActiveScanResult>) + 'a;

    /// Starts an energy scan. Functional equivalent of
    /// [`otsys::otLinkEnergyScan`](crate::otsys::otLinkEnergyScan).
    ///
    /// The closure will ultimately be executed via
    /// [`ot::Tasklets::process`](crate::ot::Tasklets::process).
    fn start_energy_scan<'a, F>(&self, channels: ChannelMask, dwell: Duration, f: F) -> Result
    where
        F: FnMut(Option<&EnergyScanResult>) + 'a;
}

impl<T: Link + Boxable> Link for ot::Box<T> {
    fn get_channel(&self) -> ChannelIndex {
        self.as_ref().get_channel()
    }

    fn set_channel(&self, index: ChannelIndex) -> Result {
        self.as_ref().set_channel(index)
    }

    fn link_get_counters(&self) -> &MacCounters {
        self.as_ref().link_get_counters()
    }

    fn get_extended_address(&self) -> &ExtAddress {
        self.as_ref().get_extended_address()
    }
    fn get_factory_assigned_ieee_eui_64(&self) -> ExtAddress {
        self.as_ref().get_factory_assigned_ieee_eui_64()
    }

    fn get_pan_id(&self) -> PanId {
        self.as_ref().get_pan_id()
    }
    fn set_pan_id(&self, pan_id: PanId) -> Result<()> {
        self.as_ref().set_pan_id(pan_id)
    }

    fn get_short_address(&self) -> ShortAddress {
        self.as_ref().get_short_address()
    }

    fn link_is_enabled(&self) -> bool {
        self.as_ref().link_is_enabled()
    }
    fn link_set_enabled(&self, enabled: bool) -> Result {
        self.as_ref().link_set_enabled(enabled)
    }

    fn link_is_promiscuous(&self) -> bool {
        self.as_ref().link_is_promiscuous()
    }
    fn link_set_promiscuous(&self, promiscuous: bool) -> Result {
        self.as_ref().link_set_promiscuous(promiscuous)
    }

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

    fn get_supported_channel_mask(&self) -> ChannelMask {
        self.as_ref().get_supported_channel_mask()
    }

    fn start_active_scan<'a, F>(&self, channels: ChannelMask, dwell: Duration, f: F) -> Result
    where
        F: FnMut(Option<&ActiveScanResult>) + 'a,
    {
        self.as_ref().start_active_scan(channels, dwell, f)
    }

    fn start_energy_scan<'a, F>(&self, channels: ChannelMask, dwell: Duration, f: F) -> Result
    where
        F: FnMut(Option<&EnergyScanResult>) + 'a,
    {
        self.as_ref().start_energy_scan(channels, dwell, f)
    }
}

impl Link for Instance {
    fn get_channel(&self) -> ChannelIndex {
        unsafe { otLinkGetChannel(self.as_ot_ptr()) }
    }

    fn set_channel(&self, index: ChannelIndex) -> Result {
        Error::from(unsafe { otLinkSetChannel(self.as_ot_ptr(), index) }).into()
    }

    fn link_get_counters(&self) -> &MacCounters {
        unsafe {
            let mac_counters = otLinkGetCounters(self.as_ot_ptr());
            MacCounters::ref_from_ot_ptr(mac_counters).unwrap()
        }
    }

    fn get_extended_address(&self) -> &ExtAddress {
        unsafe {
            let ext_addr = otLinkGetExtendedAddress(self.as_ot_ptr());
            ExtAddress::ref_from_ot_ptr(ext_addr).unwrap()
        }
    }

    fn get_factory_assigned_ieee_eui_64(&self) -> ExtAddress {
        unsafe {
            let mut ext_addr = ExtAddress::default();
            otLinkGetFactoryAssignedIeeeEui64(self.as_ot_ptr(), ext_addr.as_ot_mut_ptr());
            ext_addr
        }
    }

    fn get_pan_id(&self) -> PanId {
        unsafe { otLinkGetPanId(self.as_ot_ptr()) }
    }

    fn set_pan_id(&self, pan_id: PanId) -> Result {
        Error::from(unsafe { otLinkSetPanId(self.as_ot_ptr(), pan_id) }).into()
    }

    fn get_short_address(&self) -> ShortAddress {
        unsafe { otLinkGetShortAddress(self.as_ot_ptr()) }
    }

    fn link_is_enabled(&self) -> bool {
        unsafe { otLinkIsEnabled(self.as_ot_ptr()) }
    }

    fn link_set_enabled(&self, enabled: bool) -> Result {
        Error::from(unsafe { otLinkSetEnabled(self.as_ot_ptr(), enabled) }).into()
    }

    fn link_is_promiscuous(&self) -> bool {
        unsafe { otLinkIsPromiscuous(self.as_ot_ptr()) }
    }

    fn link_set_promiscuous(&self, promiscuous: bool) -> Result {
        Error::from(unsafe { otLinkSetPromiscuous(self.as_ot_ptr(), promiscuous) }).into()
    }

    fn is_energy_scan_in_progress(&self) -> bool {
        unsafe { otLinkIsEnergyScanInProgress(self.as_ot_ptr()) }
    }

    fn is_active_scan_in_progress(&self) -> bool {
        unsafe { otLinkIsActiveScanInProgress(self.as_ot_ptr()) }
    }

    fn get_supported_channel_mask(&self) -> ChannelMask {
        let mask_u32 = unsafe { otLinkGetSupportedChannelMask(self.as_ot_ptr()) };
        mask_u32.into()
    }

    fn start_active_scan<'a, F>(&self, channels: ChannelMask, dwell: Duration, f: F) -> Result
    where
        F: FnMut(Option<&ActiveScanResult>) + 'a,
    {
        unsafe extern "C" fn _ot_handle_active_scan_result<
            'a,
            F: FnMut(Option<&ActiveScanResult>) + 'a,
        >(
            result: *mut otActiveScanResult,
            context: *mut ::std::os::raw::c_void,
        ) {
            trace!("_ot_handle_active_scan_result: {:?}", result);

            // Convert the `*otActiveScanResult` into an `Option<&ot::ActiveScanResult>`.
            let result = ActiveScanResult::ref_from_ot_ptr(result);

            // Reconstitute a reference to our closure.
            let sender = &mut *(context as *mut F);

            sender(result);
        }

        let (fn_ptr, fn_box, cb): (_, _, otHandleActiveScanResult) = {
            let mut x = Box::new(f);

            (
                x.as_mut() as *mut F as *mut ::std::os::raw::c_void,
                Some(x as Box<dyn FnMut(Option<&ActiveScanResult>) + 'a>),
                Some(_ot_handle_active_scan_result::<F>),
            )
        };

        unsafe {
            Error::from(otLinkActiveScan(
                self.as_ot_ptr(),
                channels.into(),
                dwell.as_millis().try_into().unwrap(),
                cb,
                fn_ptr,
            ))
            .into_result()?;

            // Make sure our object eventually gets cleaned up.
            // Here we must also transmute our closure to have a 'static lifetime.
            // We need to do this because the borrow checker cannot infer the
            // proper lifetime for the singleton instance backing, but
            // this is guaranteed by the API.
            self.borrow_backing().active_scan_fn.set(std::mem::transmute::<
                Option<Box<dyn FnMut(Option<&ActiveScanResult>) + 'a>>,
                Option<Box<dyn FnMut(Option<&ActiveScanResult>) + 'static>>,
            >(fn_box));
        }

        Ok(())
    }

    fn start_energy_scan<'a, F>(&self, channels: ChannelMask, dwell: Duration, f: F) -> Result
    where
        F: FnMut(Option<&EnergyScanResult>) + 'a,
    {
        unsafe extern "C" fn _ot_handle_energy_scan_result<
            'a,
            F: FnMut(Option<&EnergyScanResult>) + 'a,
        >(
            result: *mut otEnergyScanResult,
            context: *mut ::std::os::raw::c_void,
        ) {
            trace!("_ot_handle_energy_scan_result: {:?}", result);

            // Convert the `*otEnergyScanResult` into an `Option<&ot::EnergyScanResult>`.
            let result = EnergyScanResult::ref_from_ot_ptr(result);

            // Reconstitute a reference to our closure.
            let sender = &mut *(context as *mut F);

            sender(result);
        }

        let (fn_ptr, fn_box, cb): (_, _, otHandleEnergyScanResult) = {
            let mut x = Box::new(f);

            (
                x.as_mut() as *mut F as *mut ::std::os::raw::c_void,
                Some(x as Box<dyn FnMut(Option<&EnergyScanResult>) + 'a>),
                Some(_ot_handle_energy_scan_result::<F>),
            )
        };

        unsafe {
            Error::from(otLinkEnergyScan(
                self.as_ot_ptr(),
                channels.into(),
                dwell.as_millis().try_into().unwrap(),
                cb,
                fn_ptr,
            ))
            .into_result()?;

            // Make sure our object eventually gets cleaned up.
            // Here we must also transmute our closure to have a 'static lifetime.
            // We need to do this because the borrow checker cannot infer the
            // proper lifetime for the singleton instance backing, but
            // this is guaranteed by the API.
            self.borrow_backing().energy_scan_fn.set(std::mem::transmute::<
                Option<Box<dyn FnMut(Option<&EnergyScanResult>) + 'a>>,
                Option<Box<dyn FnMut(Option<&EnergyScanResult>) + 'static>>,
            >(fn_box));
        }

        Ok(())
    }
}