use std::os::raw::c_uint;
use libcoap_sys::{coap_endpoint_set_default_mtu, coap_endpoint_t};
#[cfg(feature = "dtls")]
pub use dtls::CoapDtlsEndpoint;
pub use udp::CoapUdpEndpoint;
#[cfg(feature = "dtls")]
mod dtls;
#[cfg(feature = "tcp")]
mod tcp;
#[cfg(all(feature = "dtls", feature = "tcp"))]
mod tls;
mod udp;
pub type EndpointMtu = c_uint;
pub trait EndpointCommon {
unsafe fn as_raw_endpoint(&self) -> &coap_endpoint_t;
unsafe fn as_mut_raw_endpoint(&mut self) -> &mut coap_endpoint_t;
fn set_default_mtu(&mut self, mtu: EndpointMtu) {
unsafe {
let raw_endpoint = self.as_mut_raw_endpoint();
coap_endpoint_set_default_mtu(raw_endpoint, mtu);
}
}
}
#[derive(Debug)]
pub enum CoapEndpoint {
Udp(CoapUdpEndpoint),
#[cfg(feature = "dtls")]
Dtls(CoapDtlsEndpoint),
}
impl From<CoapUdpEndpoint> for CoapEndpoint {
fn from(ep: CoapUdpEndpoint) -> Self {
CoapEndpoint::Udp(ep)
}
}
#[cfg(feature = "dtls")]
impl From<CoapDtlsEndpoint> for CoapEndpoint {
fn from(ep: CoapDtlsEndpoint) -> Self {
CoapEndpoint::Dtls(ep)
}
}
impl EndpointCommon for CoapEndpoint {
unsafe fn as_raw_endpoint(&self) -> &coap_endpoint_t {
match self {
CoapEndpoint::Udp(ep) => ep.as_raw_endpoint(),
#[cfg(feature = "dtls")]
CoapEndpoint::Dtls(ep) => ep.as_raw_endpoint(),
}
}
unsafe fn as_mut_raw_endpoint(&mut self) -> &mut coap_endpoint_t {
match self {
CoapEndpoint::Udp(ep) => ep.as_mut_raw_endpoint(),
#[cfg(feature = "dtls")]
CoapEndpoint::Dtls(ep) => ep.as_mut_raw_endpoint(),
}
}
}