EncodedMessage

Struct EncodedMessage 

Source
pub struct EncodedMessage<'data> { /* private fields */ }
Expand description

Encoded CoAP Message as it will be transmitted/received

Implementations§

Source§

impl<'data> EncodedMessage<'data>

Source

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.

Source

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)

Source

pub fn new_rst(message_id: u16, buf: &'data mut [u8; 4]) -> Self

Convenience constructor for encoded RSTs

Source

pub fn new_ping(message_id: u16, buf: &'data mut [u8; 4]) -> Self

Convenience constructor for encoded pings

Source

pub fn ack(message_id: u16) -> [u8; 4]

Convenience method for creating an encoded ACK

Source

pub fn rst(message_id: u16) -> [u8; 4]

Convenience method for creating an encoded RST

Source

pub fn ping(message_id: u16) -> [u8; 4]

Convenience method for creating an encoded ping

Source

pub fn message_length(&self) -> usize

Length of the message

Source

pub fn version(&self) -> u8

CoAP Protocol Version

Source

pub fn message_type(&self) -> Type

Type of the message

Source

pub fn code(&self) -> Result<Code, Error>

Code of the message

Source

pub fn message_id(&self) -> u16

Id of the message

Source

pub fn token(&self) -> Result<Token, Error>

Token of the message

Source

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.

Source

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.

Source

pub fn is_empty(&self) -> Result<bool, Error>

Checks if the message has Code::Empty

Source

pub fn is_request(&self) -> Result<bool, Error>

Checks if the message has a RequestCode

Source

pub fn is_response(&self) -> Result<bool, Error>

Checks if the message has a ResponseCode

Source

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)

Source

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)

Source

pub fn is_ping(&self) -> Result<bool, Error>

Checks if the message is a ping (an empty CON message)

Source

pub fn check_msg_format<const MAX_OPTION_SIZE: usize>( &self, ) -> Result<(), Error>

  1. Checks that a valid code and token length is used. If this method succeeds, EncodedMessage::code, EncodedMessage::is_empty, EncodedMessage::is_request, EncodedMessage::is_response and EncodedMessage::token are guaranteed to succeed and the results can safely be unwrapped.

  2. 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   |
+----------+-----+-----+-----+-----+
  1. 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>

Source§

fn clone(&self) -> EncodedMessage<'data>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'data> Debug for EncodedMessage<'data>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'data> PartialEq for EncodedMessage<'data>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'data, const MAX_OPTION_COUNT: usize> TryFrom<EncodedMessage<'data>> for Message<'data, MAX_OPTION_COUNT>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(encoded: EncodedMessage<'data>) -> Result<Self, Error>

Performs the conversion.
Source§

impl<'data> Eq for EncodedMessage<'data>

Auto Trait Implementations§

§

impl<'data> !Freeze for EncodedMessage<'data>

§

impl<'data> !RefUnwindSafe for EncodedMessage<'data>

§

impl<'data> Send for EncodedMessage<'data>

§

impl<'data> !Sync for EncodedMessage<'data>

§

impl<'data> Unpin for EncodedMessage<'data>

§

impl<'data> UnwindSafe for EncodedMessage<'data>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.