use crate::prelude_internal::*;
pub const TREL_DNSSD_SERVICE_NAME: &str = "_trel._udp";
pub const TREL_DNSSD_SERVICE_NAME_WITH_DOT: &str = "_trel._udp.";
pub trait Trel {
fn trel_set_enabled(&self, enabled: bool);
fn trel_is_enabled(&self) -> bool;
fn trel_get_counters(&self) -> Option<&TrelCounters>;
fn trel_reset_counters(&self);
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()) }
}
}
#[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> {
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(),
})
}
pub fn is_removed(&self) -> bool {
self.0.mRemoved
}
pub fn txt(&self) -> &'a [u8] {
unsafe { core::slice::from_raw_parts(self.0.mTxtData, self.0.mTxtLength.into()) }
}
pub fn txt_escaped(&self) -> String {
self.txt()
.iter()
.map(Clone::clone)
.flat_map(std::ascii::escape_default)
.map(char::from)
.collect::<String>()
}
pub fn sockaddr(&self) -> SockAddr {
SockAddr::from_ot(self.0.mSockAddr)
}
}
pub trait PlatTrel {
fn plat_trel_handle_received(&self, packet: &[u8]);
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(),
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()) }
}
}