pub struct EncodedMessage<'data> { /* private fields */ }Expand description
Encoded CoAP Message as it will be transmitted/received
Implementations§
Source§impl<'data> EncodedMessage<'data>
impl<'data> EncodedMessage<'data>
Sourcepub fn try_new(data: &'data [u8]) -> Result<Self, Error>
pub fn try_new(data: &'data [u8]) -> Result<Self, Error>
Make a new Message from the data. This method only fails if the message is shorter than 4
bytes, i.e. the 4-bytes header is not even complete. This is checked here first because
like that, some of the accessor methods do not need to return a Result but can access the
header directly in a safe way.
Sourcepub fn new_ack(message_id: u16, buf: &'data mut [u8; 4]) -> Self
pub fn new_ack(message_id: u16, buf: &'data mut [u8; 4]) -> Self
Convenience constructor for encoded ACKs (can not be used for piggybacked responses)
Sourcepub fn new_rst(message_id: u16, buf: &'data mut [u8; 4]) -> Self
pub fn new_rst(message_id: u16, buf: &'data mut [u8; 4]) -> Self
Convenience constructor for encoded RSTs
Sourcepub fn new_ping(message_id: u16, buf: &'data mut [u8; 4]) -> Self
pub fn new_ping(message_id: u16, buf: &'data mut [u8; 4]) -> Self
Convenience constructor for encoded pings
Sourcepub fn message_length(&self) -> usize
pub fn message_length(&self) -> usize
Length of the message
Sourcepub fn message_type(&self) -> Type
pub fn message_type(&self) -> Type
Type of the message
Sourcepub fn message_id(&self) -> u16
pub fn message_id(&self) -> u16
Id of the message
Sourcepub fn options_iter<'encmsg>(
&'encmsg self,
) -> Result<OptionIterator<'encmsg, 'data>, Error>
pub fn options_iter<'encmsg>( &'encmsg self, ) -> Result<OptionIterator<'encmsg, 'data>, Error>
Iterator over all options in this message. This parses the message options every time it is called, so be careful with calls to this iterator.
Sourcepub fn payload(&self) -> Result<Option<&'data [u8]>, Error>
pub fn payload(&self) -> Result<Option<&'data [u8]>, Error>
Return a reference to the payload of the message, if there is any
To determine the payload offset (i.e. find the payload marker after the options), all
options must be iterated. Since the OptionIterator is generic on the maximum option size
and this requirement bubbles up, this method requires a MAX_OPTION_SIZE, too. Specify a
large enough value here (the same as for accessing the options), otherwise iterating the
options will fail.
Sourcepub fn is_request(&self) -> Result<bool, Error>
pub fn is_request(&self) -> Result<bool, Error>
Checks if the message has a RequestCode
Sourcepub fn is_response(&self) -> Result<bool, Error>
pub fn is_response(&self) -> Result<bool, Error>
Checks if the message has a ResponseCode
Sourcepub fn is_empty_ack(&self) -> Result<bool, Error>
pub fn is_empty_ack(&self) -> Result<bool, Error>
Checks if the message is an empty ACK (an ACK which is not a piggybacked response)
Sourcepub fn is_rst(&self) -> Result<bool, Error>
pub fn is_rst(&self) -> Result<bool, Error>
Checks if the message is a RST (checks that the type is RST and the message is empty)
Sourcepub fn is_ping(&self) -> Result<bool, Error>
pub fn is_ping(&self) -> Result<bool, Error>
Checks if the message is a ping (an empty CON message)
Sourcepub fn check_msg_format<const MAX_OPTION_SIZE: usize>(
&self,
) -> Result<(), Error>
pub fn check_msg_format<const MAX_OPTION_SIZE: usize>( &self, ) -> Result<(), Error>
-
Checks that a valid code and token length is used. If this method succeeds,
EncodedMessage::code,EncodedMessage::is_empty,EncodedMessage::is_request,EncodedMessage::is_responseandEncodedMessage::tokenare guaranteed to succeed and the results can safely beunwrapped. -
Checks that “Table 1. Usage of Message Types from RFC 7252” is not violated:
+----------+-----+-----+-----+-----+
| | CON | NON | ACK | RST |
+----------+-----+-----+-----+-----+
| Request | X | X | - | - |
| Response | X | X | X | - |
| Empty | * | - | X | X |
+----------+-----+-----+-----+-----+- Checks that “empty” messages are actually empty (only the 4-bytes header is present, i.e. zero-length token, no options, no payload).
For efficiency, this method only does cheap checks. Specifically, it does not iterate the options (after all, we are not decoding the full message here). So even if this check succeeds, iterating the options or getting the payload may still fail.
Trait Implementations§
Source§impl<'data> Clone for EncodedMessage<'data>
impl<'data> Clone for EncodedMessage<'data>
Source§fn clone(&self) -> EncodedMessage<'data>
fn clone(&self) -> EncodedMessage<'data>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more