pub struct CoapContext<'a> { /* private fields */ }
Expand description
A CoAP Context — container for general state and configuration information relating to CoAP
The equivalent to the [coap_context_t] type in libcoap.
Implementations§
Source§impl<'a> CoapContext<'a>
impl<'a> CoapContext<'a>
Sourcepub fn new() -> Result<CoapContext<'a>, ContextConfigurationError>
pub fn new() -> Result<CoapContext<'a>, ContextConfigurationError>
Creates a new context.
§Errors
Returns an error if the underlying libcoap library was unable to create a new context (probably an allocation error?).
Sourcepub fn set_psk_context(
&mut self,
psk_context: ServerPskContext<'a>,
) -> Result<(), ContextConfigurationError>
pub fn set_psk_context( &mut self, psk_context: ServerPskContext<'a>, ) -> Result<(), ContextConfigurationError>
Sets the server-side cryptography information provider.
§Errors
Returns ContextConfigurationError::Unknown
if the call to the underlying libcoap library
function fails and ContextConfigurationError::CryptoContextAlreadySet
if the PSK context
has already been set previously.
Sourcepub fn set_pki_rpk_context(
&mut self,
pki_context: impl Into<ServerPkiRpkCryptoContext<'a>>,
) -> Result<(), ContextConfigurationError>
pub fn set_pki_rpk_context( &mut self, pki_context: impl Into<ServerPkiRpkCryptoContext<'a>>, ) -> Result<(), ContextConfigurationError>
Sets the server-side cryptography information provider.
§Errors
Returns ContextConfigurationError::Unknown
if the call to the underlying libcoap library
function fails and ContextConfigurationError::CryptoContextAlreadySet
if the PSK context
has already been set previously.
Sourcepub fn set_pki_root_ca_paths(
&mut self,
ca_file: Option<impl AsRef<Path>>,
ca_dir: Option<impl AsRef<Path>>,
) -> Result<(), ContextConfigurationError>
pub fn set_pki_root_ca_paths( &mut self, ca_file: Option<impl AsRef<Path>>, ca_dir: Option<impl AsRef<Path>>, ) -> Result<(), ContextConfigurationError>
Convenience wrapper around set_pki_root_cas
that can be
provided with any type that implements AsRef<Path>
.
ca_file
should be the full path of a PEM-encoded file containing all root CAs to be used
or None
, ca_dir
should be a directory path containing PEM-encoded CA certificates to
be used or None
.
As not all implementations of [OsString
] (which is the basis of Path
) provide
conversions to non-zero byte sequences, this function is only available on Unix.
On other operating systems, perform manual conversion of paths into CString
and call
set_pki_root_cas
directly instead.
See the Rust standard library documentation on FFI conversions
and the OsString
type for more
information.
§Errors
Will return ContextConfigurationError::Unknown
if the call to the underlying libcoap
function fails (indicating an error in either libcoap or the underlying TLS library).
Sourcepub fn set_pki_root_cas(
&mut self,
ca_file: Option<CString>,
ca_dir: Option<CString>,
) -> Result<(), ContextConfigurationError>
pub fn set_pki_root_cas( &mut self, ca_file: Option<CString>, ca_dir: Option<CString>, ) -> Result<(), ContextConfigurationError>
Sets the path to a CA certificate file and/or a directory of CA certificate files that should be used as this context’s default root CA information.
ca_file
should be the full path of a PEM-encoded file containing all root CAs to be used
or None
, ca_dir
should be a directory path containing PEM-encoded CA certificates to
be used or None
.
§Errors
Will return ContextConfigurationError::Unknown
if the call to the underlying libcoap
function fails (indicating an error in either libcoap or the underlying TLS library).
Source§impl CoapContext<'_>
impl CoapContext<'_>
Sourcepub fn shutdown(
self,
exit_wait_timeout: Option<Duration>,
) -> Result<(), IoProcessError>
pub fn shutdown( self, exit_wait_timeout: Option<Duration>, ) -> Result<(), IoProcessError>
Performs a controlled shutdown of the CoAP context.
This will perform all still outstanding IO operations until [coap_can_exit()] confirms that the context has no more outstanding IO and can be dropped without interrupting sessions.
Sourcepub fn add_endpoint_udp(
&mut self,
addr: SocketAddr,
) -> Result<(), EndpointCreationError>
pub fn add_endpoint_udp( &mut self, addr: SocketAddr, ) -> Result<(), EndpointCreationError>
Creates a new UDP endpoint that is bound to the given address.
Sourcepub fn add_endpoint_tcp(
&mut self,
addr: SocketAddr,
) -> Result<(), EndpointCreationError>
pub fn add_endpoint_tcp( &mut self, addr: SocketAddr, ) -> Result<(), EndpointCreationError>
Creates a new TCP endpoint that is bound to the given address.
Sourcepub fn add_endpoint_dtls(
&mut self,
addr: SocketAddr,
) -> Result<(), EndpointCreationError>
pub fn add_endpoint_dtls( &mut self, addr: SocketAddr, ) -> Result<(), EndpointCreationError>
Creates a new DTLS endpoint that is bound to the given address.
Note that in order to actually connect to DTLS clients, you need to set a crypto provider using CoapContext::set_psk_context and/or CoapContext::set_pki_rpk_context.
Sourcepub fn add_resource<D: Any + ?Sized + Debug>(&mut self, res: CoapResource<D>)
pub fn add_resource<D: Any + ?Sized + Debug>(&mut self, res: CoapResource<D>)
Adds the given resource to the resource pool of this context.
Sourcepub fn do_io(
&mut self,
timeout: Option<Duration>,
) -> Result<Duration, IoProcessError>
pub fn do_io( &mut self, timeout: Option<Duration>, ) -> Result<Duration, IoProcessError>
Performs currently outstanding IO operations, waiting for a maximum duration of timeout
.
This is the function where most of the IO operations made using this library are actually executed. It is recommended to call this function in a loop for as long as the CoAP context is used.
Sourcepub fn session_timeout(&self) -> Duration
pub fn session_timeout(&self) -> Duration
Return the duration that idle server-side sessions are kept alive if they are not referenced or used anywhere else.
Sourcepub fn set_session_timeout(&self, timeout: Duration)
pub fn set_session_timeout(&self, timeout: Duration)
Set the duration that idle server-side sessions are kept alive if they are not referenced or used anywhere else.
§Panics
Panics if the provided duration is too large to be provided to libcoap (larger than a [libc::c_uint]).
Sourcepub fn max_handshake_sessions(&self) -> c_uint
pub fn max_handshake_sessions(&self) -> c_uint
Returns the maximum number of server-side sessions that can concurrently be in a handshake state.
If this number is exceeded, no new handshakes will be accepted.
Sourcepub fn set_max_handshake_sessions(&self, max_handshake_sessions: c_uint)
pub fn set_max_handshake_sessions(&self, max_handshake_sessions: c_uint)
Sets the maximum number of server-side sessions that can concurrently be in a handshake state.
If this number is exceeded, no new handshakes will be accepted.
Sourcepub fn max_idle_sessions(&self) -> c_uint
pub fn max_idle_sessions(&self) -> c_uint
Returns the maximum number of idle server-side sessions for this context.
If this number is exceeded, the oldest unreferenced session will be freed.
Sourcepub fn set_max_idle_sessions(&self, max_idle_sessions: c_uint)
pub fn set_max_idle_sessions(&self, max_idle_sessions: c_uint)
Sets the maximum number of idle server-side sessions for this context.
If this number is exceeded, the oldest unreferenced session will be freed.
Sourcepub fn csm_max_message_size(&self) -> u32
pub fn csm_max_message_size(&self) -> u32
Returns the maximum size for Capabilities and Settings Messages
CSMs are used in CoAP over TCP as specified in RFC 8323, Section 5.3.
Sourcepub fn set_csm_max_message_size(&self, csm_max_message_size: u32)
pub fn set_csm_max_message_size(&self, csm_max_message_size: u32)
Sets the maximum size for Capabilities and Settings Messages
CSMs are used in CoAP over TCP as specified in RFC 8323, Section 5.3.
Sourcepub fn csm_timeout(&self) -> Duration
pub fn csm_timeout(&self) -> Duration
Returns the timeout for Capabilities and Settings Messages
CSMs are used in CoAP over TCP as specified in RFC 8323, Section 5.3.
Sourcepub fn set_csm_timeout(&self, csm_timeout: Duration)
pub fn set_csm_timeout(&self, csm_timeout: Duration)
Sets the timeout for Capabilities and Settings Messages
CSMs are used in CoAP over TCP as specified in RFC 8323, Section 5.3.
§Panics
Panics if the provided timeout is too large for libcoap (> u32::MAX).
Sourcepub fn set_keepalive(&self, timeout: Option<Duration>)
pub fn set_keepalive(&self, timeout: Option<Duration>)
Sets the number of seconds to wait before sending a CoAP keepalive message for idle sessions.
If the provided value is None, CoAP-level keepalive messages will be disabled.
§Panics
Panics if the provided duration is too large to be provided to libcoap (larger than a [libc::c_uint]).