openthread/ot/
backbone_router.rsuse crate::prelude_internal::*;
#[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)
}
}
pub trait BackboneRouter {
fn set_backbone_router_enabled(&self, enable: bool);
fn multicast_listener_add(&self, addr: &Ip6Address, timeout: u32) -> Result;
fn multicast_listener_clear(&self);
fn multicast_listener_get_next(
&self,
listener_iter: &mut otBackboneRouterMulticastListenerIterator,
) -> Option<BackboneRouterMulticastListenerInfo>;
fn set_multicast_listener_callback<'a, F>(&'a self, f: Option<F>)
where
F: FnMut(BackboneRouterMulticastListenerEvent, &Ip6Address) + 'a;
fn config_next_multicast_listener_registration_response(&self, status: u8);
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");
let address = Ip6Address::ref_from_ot_ptr(address).unwrap();
let event = BackboneRouterMulticastListenerEvent::from(event);
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);
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));
}
}
}