Skip to main content

Messager

Struct Messager 

Source
pub struct Messager<V: Validator = DjiValidator> { /* private fields */ }
Expand description

Frame encoder and decoder.

Messager packs typed messages into binary frames and unpacks validated frames from raw bytes. It performs no I/O and allocates no memory.

Generic over Validator to allow custom CRC implementations. The default validator is DjiValidator.

The internal sequence counter starts at the value passed to new and increments by one after each successful pack call, wrapping on overflow.

§Frame Layout

+--------+--------+--------+--------+--------+---------+--------+
|  SOF   |  LEN   |  SEQ   |  CRC8  | CMD_ID |  DATA   | CRC16  |
+--------+--------+--------+--------+--------+---------+--------+
| 1 byte | 2 byte | 1 byte | 1 byte | 2 byte | N bytes | 2 byte |
+--------+--------+--------+--------+--------+---------+--------+
  • SOF: start-of-frame marker (0xA5)
  • LEN: payload length, little-endian u16
  • SEQ: sequence number, u8
  • CRC8: checksum over [SOF, LEN, SEQ]
  • CMD_ID: command identifier, little-endian u16
  • DATA: payload bytes (N = crate::Marshaler::PAYLOAD_SIZE)
  • CRC16: checksum over the entire frame preceding this field

Implementations§

Source§

impl<V: Validator> Messager<V>

Source

pub const fn new(seq: u8) -> Self

Creates a new Messager with the given initial sequence number.

The sequence number is embedded in each packed frame header and increments automatically on each successful pack call.

Source

pub fn pack<M: ImplMarshal>( &mut self, msg: &M, dst: &mut [u8], ) -> Result<usize, PackError>

Encode a message into a binary frame.

Serializes msg and writes the complete frame — header, command ID, payload, and CRC — into dst. The payload is written directly into dst with no intermediate buffer.

On success, returns the total number of bytes written.

The sequence counter increments by one after each successful call.

§Errors
Source

pub fn unpack<'t>( &self, src: &'t [u8], ) -> Result<(RawFrame<'t>, usize), UnPackError>

Parse and validate one frame from raw bytes.

Reads from the start of src and performs these checks in order:

  1. Start-of-frame marker (0xA5)
  2. Header CRC8
  3. Frame CRC16

On success, returns a RawFrame whose payload borrows from src, and the number of bytes consumed.

The payload in the returned RawFrame is untyped. Use RawFrame::unmarshal to decode it, or call unmarshal instead of this method to do both steps at once.

§Errors
Source

pub fn unmarshal<M: ImplUnMarshal>( &self, src: &[u8], ) -> Result<(M, usize), UnPackError>

Parse a frame and decode its payload into a typed message.

Combines unpack and RawFrame::unmarshal in one call.

On success, returns the decoded message and the number of bytes consumed.

§Errors

Trait Implementations§

Source§

impl<V: Debug + Validator> Debug for Messager<V>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<V> Freeze for Messager<V>

§

impl<V> RefUnwindSafe for Messager<V>
where V: RefUnwindSafe,

§

impl<V> Send for Messager<V>
where V: Send,

§

impl<V> Sync for Messager<V>
where V: Sync,

§

impl<V> Unpin for Messager<V>
where V: Unpin,

§

impl<V> UnsafeUnpin for Messager<V>

§

impl<V> UnwindSafe for Messager<V>
where V: UnwindSafe,

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> 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, 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.