openthread/ot/
backbone_router.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
// 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::*;

/// Iterator type for multicast listeners.
#[allow(missing_debug_implementations)]
pub struct MulticastListenerIterator<'a, T: ?Sized> {
    ot_instance: &'a T,
    ot_listener_iter: otBackboneRouterMulticastListenerIterator,
}

impl<T: ?Sized + BackboneRouter> Iterator for MulticastListenerIterator<'_, T> {
    type Item = BackboneRouterMulticastListenerInfo;
    fn next(&mut self) -> Option<Self::Item> {
        self.ot_instance.multicast_listener_get_next(&mut self.ot_listener_iter)
    }
}

/// Methods from the [OpenThread "Backbone Router" Module][1].
/// Currently only multicast routing related methods are added.
///
/// [1]: https://openthread.io/reference/group/api-backbone-router
pub trait BackboneRouter {
    /// Functional equilvanet of
    /// [`otsys::otBackboneRouterSetEnabled`](crate::otsys::otBackboneRouterSetEnabled).
    fn set_backbone_router_enabled(&self, enable: bool);

    /// Functional equivalent of
    /// [`otsys::otBackboneRouterMulticastListenerAdd`](crate::otsys::otBackboneRouterMulticastListenerAdd).
    fn multicast_listener_add(&self, addr: &Ip6Address, timeout: u32) -> Result;

    /// Functional equivalent of
    /// [`otsys::otBackboneRouterMulticastListenerClear`](crate::otsys::otBackboneRouterMulticastListenerClear).
    fn multicast_listener_clear(&self);

    /// Functional equivalent of
    /// [`otsys::otBackboneRouterMulticastListenerGetNext`](crate::otsys::otBackboneRouterMulticastListenerGetNext).
    // TODO: Determine if the underlying implementation of
    //       this method has undefined behavior when network data
    //       is being mutated while iterating. If it is undefined,
    //       we may need to make it unsafe and provide a safe method
    //       that collects the results.
    fn multicast_listener_get_next(
        &self,
        listener_iter: &mut otBackboneRouterMulticastListenerIterator,
    ) -> Option<BackboneRouterMulticastListenerInfo>;

    /// Functional equivalent of
    /// [`otsys::otBackboneRouterSetMulticastListenerCallback`](crate::otsys::otBackboneRouterSetMulticastListenerCallback).
    fn set_multicast_listener_callback<'a, F>(&'a self, f: Option<F>)
    where
        F: FnMut(BackboneRouterMulticastListenerEvent, &Ip6Address) + 'a;

    /// Functional equivalent of
    /// [`otsys::otBackboneRouterConfigNextMulticastListenerRegistrationResponse`](crate::otsys::otBackboneRouterConfigNextMulticastListenerRegistrationResponse).
    fn config_next_multicast_listener_registration_response(&self, status: u8);

    /// Returns an iterator for iterating over multicast listeners.
    fn iter_multicaster_listeners(&self) -> MulticastListenerIterator<'_, Self> {
        MulticastListenerIterator {
            ot_instance: self,
            ot_listener_iter: OT_BACKBONE_ROUTER_MULTICAST_LISTENER_ITERATOR_INIT
                .try_into()
                .unwrap(),
        }
    }
}

impl<T: BackboneRouter + Boxable> BackboneRouter for ot::Box<T> {
    fn set_backbone_router_enabled(&self, enable: bool) {
        self.as_ref().set_backbone_router_enabled(enable)
    }

    fn multicast_listener_add(&self, addr: &Ip6Address, timeout: u32) -> Result {
        self.as_ref().multicast_listener_add(addr, timeout)
    }

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

    fn multicast_listener_get_next(
        &self,
        listener_iter: &mut otBackboneRouterMulticastListenerIterator,
    ) -> Option<BackboneRouterMulticastListenerInfo> {
        self.as_ref().multicast_listener_get_next(listener_iter)
    }

    fn config_next_multicast_listener_registration_response(&self, status: u8) {
        self.as_ref().config_next_multicast_listener_registration_response(status)
    }

    fn set_multicast_listener_callback<'a, F>(&'a self, f: Option<F>)
    where
        F: FnMut(BackboneRouterMulticastListenerEvent, &Ip6Address) + 'a,
    {
        self.as_ref().set_multicast_listener_callback(f)
    }
}

impl BackboneRouter for Instance {
    fn set_backbone_router_enabled(&self, enable: bool) {
        unsafe { otBackboneRouterSetEnabled(self.as_ot_ptr(), enable) }
    }

    fn multicast_listener_add(&self, addr: &Ip6Address, timeout: u32) -> Result {
        Error::from(unsafe {
            otBackboneRouterMulticastListenerAdd(self.as_ot_ptr(), addr.as_ot_ptr(), timeout)
        })
        .into()
    }

    fn multicast_listener_clear(&self) {
        unsafe { otBackboneRouterMulticastListenerClear(self.as_ot_ptr()) }
    }

    fn multicast_listener_get_next(
        &self,
        listener_iter: &mut otBackboneRouterMulticastListenerIterator,
    ) -> Option<BackboneRouterMulticastListenerInfo> {
        unsafe {
            let mut ret = BackboneRouterMulticastListenerInfo::default();
            match Error::from(otBackboneRouterMulticastListenerGetNext(
                self.as_ot_ptr(),
                listener_iter as *mut otBackboneRouterMulticastListenerIterator,
                ret.as_ot_mut_ptr(),
            )) {
                Error::NotFound => None,
                Error::None => Some(ret),
                err => panic!(
                    "Unexpected error from otBackboneRouterMulticastListenerIterator: {err:?}"
                ),
            }
        }
    }

    fn config_next_multicast_listener_registration_response(&self, status: u8) {
        unsafe {
            otBackboneRouterConfigNextMulticastListenerRegistrationResponse(
                self.as_ot_ptr(),
                status,
            )
        }
    }

    fn set_multicast_listener_callback<'a, F>(&'a self, f: Option<F>)
    where
        F: FnMut(BackboneRouterMulticastListenerEvent, &Ip6Address) + 'a,
    {
        unsafe extern "C" fn _ot_backbone_router_multicast_listener_callback<
            'a,
            F: FnMut(BackboneRouterMulticastListenerEvent, &Ip6Address) + 'a,
        >(
            context: *mut ::std::os::raw::c_void,
            event: otBackboneRouterMulticastListenerEvent,
            address: *const otIp6Address,
        ) {
            trace!("_ot_backbone_router_multicast_listener_callback");

            // Convert the `*otIp6Address` into an `&ot::Ip6AddressInfo`.
            let address = Ip6Address::ref_from_ot_ptr(address).unwrap();

            // Convert `otBackboneRouterMulticastListenerCallback` to
            // `BackboneRouterMulticastListenerEvent`
            let event = BackboneRouterMulticastListenerEvent::from(event);

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

            sender(event, address)
        }

        let (fn_ptr, fn_box, cb): (_, _, otBackboneRouterMulticastListenerCallback) =
            if let Some(f) = f {
                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(BackboneRouterMulticastListenerEvent, &Ip6Address) + 'a>,
                    ),
                    Some(_ot_backbone_router_multicast_listener_callback::<F>),
                )
            } else {
                (std::ptr::null_mut() as *mut ::std::os::raw::c_void, None, None)
            };

        unsafe {
            otBackboneRouterSetMulticastListenerCallback(self.as_ot_ptr(), cb, fn_ptr);

            // 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().multicast_listener_callback.set(std::mem::transmute::<
                Option<Box<dyn FnMut(BackboneRouterMulticastListenerEvent, &Ip6Address) + 'a>>,
                Option<Box<dyn FnMut(BackboneRouterMulticastListenerEvent, &Ip6Address) + 'static>>,
            >(fn_box));
        }
    }
}